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" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 41 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_quota.h" |
| 44 | #include "xfs_trans_space.h" |
| 45 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 46 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 47 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 49 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 50 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 51 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 52 | #include "xfs_refcount.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, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 116 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 117 | int *stat) /* success/failure */ |
| 118 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 119 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 120 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 121 | } |
| 122 | |
| 123 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 124 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 125 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 126 | int *stat) /* success/failure */ |
| 127 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 128 | cur->bc_rec.b.br_startoff = 0; |
| 129 | cur->bc_rec.b.br_startblock = 0; |
| 130 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 131 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 132 | } |
| 133 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 134 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 135 | * Check if the inode needs to be converted to btree format. |
| 136 | */ |
| 137 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 138 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 139 | return whichfork != XFS_COW_FORK && |
| 140 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 141 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 142 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Check if the inode should be converted to extent format. |
| 147 | */ |
| 148 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 149 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 150 | return whichfork != XFS_COW_FORK && |
| 151 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 152 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 153 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 154 | } |
| 155 | |
| 156 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 157 | * Update the record referred to by cur to the value given by irec |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 158 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 159 | */ |
| 160 | STATIC int |
| 161 | xfs_bmbt_update( |
| 162 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 163 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 164 | { |
| 165 | union xfs_btree_rec rec; |
| 166 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 167 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 168 | return xfs_btree_update(cur, &rec); |
| 169 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 172 | * Compute the worst-case number of indirect blocks that will be used |
| 173 | * for ip's delayed extent of length "len". |
| 174 | */ |
| 175 | STATIC xfs_filblks_t |
| 176 | xfs_bmap_worst_indlen( |
| 177 | xfs_inode_t *ip, /* incore inode pointer */ |
| 178 | xfs_filblks_t len) /* delayed extent length */ |
| 179 | { |
| 180 | int level; /* btree level number */ |
| 181 | int maxrecs; /* maximum record count at this level */ |
| 182 | xfs_mount_t *mp; /* mount structure */ |
| 183 | xfs_filblks_t rval; /* return value */ |
| 184 | |
| 185 | mp = ip->i_mount; |
| 186 | maxrecs = mp->m_bmap_dmxr[0]; |
| 187 | for (level = 0, rval = 0; |
| 188 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 189 | level++) { |
| 190 | len += maxrecs - 1; |
| 191 | do_div(len, maxrecs); |
| 192 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 193 | if (len == 1) |
| 194 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 195 | level - 1; |
| 196 | if (level == 0) |
| 197 | maxrecs = mp->m_bmap_dmxr[1]; |
| 198 | } |
| 199 | return rval; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Calculate the default attribute fork offset for newly created inodes. |
| 204 | */ |
| 205 | uint |
| 206 | xfs_default_attroffset( |
| 207 | struct xfs_inode *ip) |
| 208 | { |
| 209 | struct xfs_mount *mp = ip->i_mount; |
| 210 | uint offset; |
| 211 | |
| 212 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 213 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 214 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 215 | } else { |
| 216 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 217 | } |
| 218 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 219 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 220 | return offset; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Helper routine to reset inode di_forkoff field when switching |
| 225 | * attribute fork from local to extent format - we reset it where |
| 226 | * possible to make space available for inline data fork extents. |
| 227 | */ |
| 228 | STATIC void |
| 229 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 230 | xfs_inode_t *ip, |
| 231 | int whichfork) |
| 232 | { |
| 233 | if (whichfork == XFS_ATTR_FORK && |
| 234 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 235 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 236 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 237 | |
| 238 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 239 | ip->i_d.di_forkoff = dfl_forkoff; |
| 240 | } |
| 241 | } |
| 242 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 243 | #ifdef DEBUG |
| 244 | STATIC struct xfs_buf * |
| 245 | xfs_bmap_get_bp( |
| 246 | struct xfs_btree_cur *cur, |
| 247 | xfs_fsblock_t bno) |
| 248 | { |
| 249 | struct xfs_log_item_desc *lidp; |
| 250 | int i; |
| 251 | |
| 252 | if (!cur) |
| 253 | return NULL; |
| 254 | |
| 255 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 256 | if (!cur->bc_bufs[i]) |
| 257 | break; |
| 258 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 259 | return cur->bc_bufs[i]; |
| 260 | } |
| 261 | |
| 262 | /* Chase down all the log items to see if the bp is there */ |
| 263 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 264 | struct xfs_buf_log_item *bip; |
| 265 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 266 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 267 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 268 | return bip->bli_buf; |
| 269 | } |
| 270 | |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | STATIC void |
| 275 | xfs_check_block( |
| 276 | struct xfs_btree_block *block, |
| 277 | xfs_mount_t *mp, |
| 278 | int root, |
| 279 | short sz) |
| 280 | { |
| 281 | int i, j, dmxr; |
| 282 | __be64 *pp, *thispa; /* pointer to block address */ |
| 283 | xfs_bmbt_key_t *prevp, *keyp; |
| 284 | |
| 285 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 286 | |
| 287 | prevp = NULL; |
| 288 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 289 | dmxr = mp->m_bmap_dmxr[0]; |
| 290 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 291 | |
| 292 | if (prevp) { |
| 293 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 294 | be64_to_cpu(keyp->br_startoff)); |
| 295 | } |
| 296 | prevp = keyp; |
| 297 | |
| 298 | /* |
| 299 | * Compare the block numbers to see if there are dups. |
| 300 | */ |
| 301 | if (root) |
| 302 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 303 | else |
| 304 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 305 | |
| 306 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 307 | if (root) |
| 308 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 309 | else |
| 310 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 311 | if (*thispa == *pp) { |
| 312 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 313 | __func__, j, i, |
| 314 | (unsigned long long)be64_to_cpu(*thispa)); |
| 315 | panic("%s: ptrs are equal in node\n", |
| 316 | __func__); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * 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] | 324 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 325 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 326 | * them. The btree record ordering checks will still be done, so for such large |
| 327 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 328 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 329 | STATIC void |
| 330 | xfs_bmap_check_leaf_extents( |
| 331 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 332 | xfs_inode_t *ip, /* incore inode pointer */ |
| 333 | int whichfork) /* data or attr fork */ |
| 334 | { |
| 335 | struct xfs_btree_block *block; /* current btree block */ |
| 336 | xfs_fsblock_t bno; /* block # of "block" */ |
| 337 | xfs_buf_t *bp; /* buffer for "block" */ |
| 338 | int error; /* error return value */ |
| 339 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 340 | xfs_ifork_t *ifp; /* fork structure */ |
| 341 | int level; /* btree level, for checking */ |
| 342 | xfs_mount_t *mp; /* file system mount structure */ |
| 343 | __be64 *pp; /* pointer to block address */ |
| 344 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 345 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 346 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 347 | int bp_release = 0; |
| 348 | |
| 349 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 350 | return; |
| 351 | } |
| 352 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 353 | /* skip large extent count inodes */ |
| 354 | if (ip->i_d.di_nextents > 10000) |
| 355 | return; |
| 356 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 357 | bno = NULLFSBLOCK; |
| 358 | mp = ip->i_mount; |
| 359 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 360 | block = ifp->if_broot; |
| 361 | /* |
| 362 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 363 | */ |
| 364 | level = be16_to_cpu(block->bb_level); |
| 365 | ASSERT(level > 0); |
| 366 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 367 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 368 | bno = be64_to_cpu(*pp); |
| 369 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 370 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 371 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 372 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 373 | |
| 374 | /* |
| 375 | * Go down the tree until leaf level is reached, following the first |
| 376 | * pointer (leftmost) at each level. |
| 377 | */ |
| 378 | while (level-- > 0) { |
| 379 | /* See if buf is in cur first */ |
| 380 | bp_release = 0; |
| 381 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 382 | if (!bp) { |
| 383 | bp_release = 1; |
| 384 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 385 | XFS_BMAP_BTREE_REF, |
| 386 | &xfs_bmbt_buf_ops); |
| 387 | if (error) |
| 388 | goto error_norelse; |
| 389 | } |
| 390 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 391 | if (level == 0) |
| 392 | break; |
| 393 | |
| 394 | /* |
| 395 | * Check this block for basic sanity (increasing keys and |
| 396 | * no duplicate blocks). |
| 397 | */ |
| 398 | |
| 399 | xfs_check_block(block, mp, 0, 0); |
| 400 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 401 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 402 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 403 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 404 | if (bp_release) { |
| 405 | bp_release = 0; |
| 406 | xfs_trans_brelse(NULL, bp); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 412 | */ |
| 413 | i = 0; |
| 414 | |
| 415 | /* |
| 416 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 417 | */ |
| 418 | for (;;) { |
| 419 | xfs_fsblock_t nextbno; |
| 420 | xfs_extnum_t num_recs; |
| 421 | |
| 422 | |
| 423 | num_recs = xfs_btree_get_numrecs(block); |
| 424 | |
| 425 | /* |
| 426 | * Read-ahead the next leaf block, if any. |
| 427 | */ |
| 428 | |
| 429 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 430 | |
| 431 | /* |
| 432 | * Check all the extents to make sure they are OK. |
| 433 | * If we had a previous block, the last entry should |
| 434 | * conform with the first entry in this one. |
| 435 | */ |
| 436 | |
| 437 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 438 | if (i) { |
| 439 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 440 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 441 | xfs_bmbt_disk_get_startoff(ep)); |
| 442 | } |
| 443 | for (j = 1; j < num_recs; j++) { |
| 444 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 445 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 446 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 447 | xfs_bmbt_disk_get_startoff(nextp)); |
| 448 | ep = nextp; |
| 449 | } |
| 450 | |
| 451 | last = *ep; |
| 452 | i += num_recs; |
| 453 | if (bp_release) { |
| 454 | bp_release = 0; |
| 455 | xfs_trans_brelse(NULL, bp); |
| 456 | } |
| 457 | bno = nextbno; |
| 458 | /* |
| 459 | * If we've reached the end, stop. |
| 460 | */ |
| 461 | if (bno == NULLFSBLOCK) |
| 462 | break; |
| 463 | |
| 464 | bp_release = 0; |
| 465 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 466 | if (!bp) { |
| 467 | bp_release = 1; |
| 468 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 469 | XFS_BMAP_BTREE_REF, |
| 470 | &xfs_bmbt_buf_ops); |
| 471 | if (error) |
| 472 | goto error_norelse; |
| 473 | } |
| 474 | block = XFS_BUF_TO_BLOCK(bp); |
| 475 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 476 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 477 | return; |
| 478 | |
| 479 | error0: |
| 480 | xfs_warn(mp, "%s: at error0", __func__); |
| 481 | if (bp_release) |
| 482 | xfs_trans_brelse(NULL, bp); |
| 483 | error_norelse: |
| 484 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 485 | __func__, i); |
| 486 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 491 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 492 | * given the caller's original parameters. Specifically check the |
| 493 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 494 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 495 | */ |
| 496 | STATIC void |
| 497 | xfs_bmap_validate_ret( |
| 498 | xfs_fileoff_t bno, |
| 499 | xfs_filblks_t len, |
| 500 | int flags, |
| 501 | xfs_bmbt_irec_t *mval, |
| 502 | int nmap, |
| 503 | int ret_nmap) |
| 504 | { |
| 505 | int i; /* index to map values */ |
| 506 | |
| 507 | ASSERT(ret_nmap <= nmap); |
| 508 | |
| 509 | for (i = 0; i < ret_nmap; i++) { |
| 510 | ASSERT(mval[i].br_blockcount > 0); |
| 511 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 512 | ASSERT(mval[i].br_startoff >= bno); |
| 513 | ASSERT(mval[i].br_blockcount <= len); |
| 514 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 515 | bno + len); |
| 516 | } else { |
| 517 | ASSERT(mval[i].br_startoff < bno + len); |
| 518 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 519 | bno); |
| 520 | } |
| 521 | ASSERT(i == 0 || |
| 522 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 523 | mval[i].br_startoff); |
| 524 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 525 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 526 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 527 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | #else |
| 532 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
Darrick J. Wong | 7bf7a19 | 2017-08-31 15:11:06 -0700 | [diff] [blame] | 533 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 534 | #endif /* DEBUG */ |
| 535 | |
| 536 | /* |
| 537 | * bmap free list manipulation functions |
| 538 | */ |
| 539 | |
| 540 | /* |
| 541 | * Add the extent to the list of extents to be free at transaction end. |
| 542 | * The list is maintained sorted (by block number). |
| 543 | */ |
| 544 | void |
| 545 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 546 | struct xfs_mount *mp, |
| 547 | struct xfs_defer_ops *dfops, |
| 548 | xfs_fsblock_t bno, |
| 549 | xfs_filblks_t len, |
| 550 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 551 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 552 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 553 | #ifdef DEBUG |
| 554 | xfs_agnumber_t agno; |
| 555 | xfs_agblock_t agbno; |
| 556 | |
| 557 | ASSERT(bno != NULLFSBLOCK); |
| 558 | ASSERT(len > 0); |
| 559 | ASSERT(len <= MAXEXTLEN); |
| 560 | ASSERT(!isnullstartblock(bno)); |
| 561 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 562 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 563 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 564 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 565 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 566 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 567 | #endif |
| 568 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 569 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 570 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 571 | new->xefi_startblock = bno; |
| 572 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 573 | if (oinfo) |
| 574 | new->xefi_oinfo = *oinfo; |
| 575 | else |
| 576 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 577 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 578 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 579 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Inode fork format manipulation functions |
| 584 | */ |
| 585 | |
| 586 | /* |
| 587 | * Transform a btree format file with only one leaf node, where the |
| 588 | * extents list will fit in the inode, into an extents format file. |
| 589 | * Since the file extents are already in-core, all we have to do is |
| 590 | * give up the space for the btree root and pitch the leaf block. |
| 591 | */ |
| 592 | STATIC int /* error */ |
| 593 | xfs_bmap_btree_to_extents( |
| 594 | xfs_trans_t *tp, /* transaction pointer */ |
| 595 | xfs_inode_t *ip, /* incore inode pointer */ |
| 596 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 597 | int *logflagsp, /* inode logging flags */ |
| 598 | int whichfork) /* data or attr fork */ |
| 599 | { |
| 600 | /* REFERENCED */ |
| 601 | struct xfs_btree_block *cblock;/* child btree block */ |
| 602 | xfs_fsblock_t cbno; /* child block number */ |
| 603 | xfs_buf_t *cbp; /* child block's buffer */ |
| 604 | int error; /* error return value */ |
| 605 | xfs_ifork_t *ifp; /* inode fork data */ |
| 606 | xfs_mount_t *mp; /* mount point structure */ |
| 607 | __be64 *pp; /* ptr to block address */ |
| 608 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 609 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 610 | |
| 611 | mp = ip->i_mount; |
| 612 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 613 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 614 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 615 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 616 | rblock = ifp->if_broot; |
| 617 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 618 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 619 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 620 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 621 | cbno = be64_to_cpu(*pp); |
| 622 | *logflagsp = 0; |
| 623 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 624 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 625 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 626 | #endif |
| 627 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 628 | &xfs_bmbt_buf_ops); |
| 629 | if (error) |
| 630 | return error; |
| 631 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 632 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 633 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 634 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 635 | 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] | 636 | ip->i_d.di_nblocks--; |
| 637 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 638 | xfs_trans_binval(tp, cbp); |
| 639 | if (cur->bc_bufs[0] == cbp) |
| 640 | cur->bc_bufs[0] = NULL; |
| 641 | xfs_iroot_realloc(ip, -1, whichfork); |
| 642 | ASSERT(ifp->if_broot == NULL); |
| 643 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 644 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 645 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Convert an extents-format file into a btree-format file. |
| 651 | * The new file will have a root block (in the inode) and a single child block. |
| 652 | */ |
| 653 | STATIC int /* error */ |
| 654 | xfs_bmap_extents_to_btree( |
| 655 | xfs_trans_t *tp, /* transaction pointer */ |
| 656 | xfs_inode_t *ip, /* incore inode pointer */ |
| 657 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 658 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 659 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 660 | int wasdel, /* converting a delayed alloc */ |
| 661 | int *logflagsp, /* inode logging flags */ |
| 662 | int whichfork) /* data or attr fork */ |
| 663 | { |
| 664 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 665 | xfs_buf_t *abp; /* buffer for ablock */ |
| 666 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 667 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 668 | struct xfs_btree_block *block; /* btree root block */ |
| 669 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 670 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 671 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 672 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 673 | xfs_mount_t *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 674 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 675 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 676 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 677 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 678 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 679 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 680 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 681 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 682 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 683 | |
| 684 | /* |
| 685 | * Make space in the inode incore. |
| 686 | */ |
| 687 | xfs_iroot_realloc(ip, 1, whichfork); |
| 688 | ifp->if_flags |= XFS_IFBROOT; |
| 689 | |
| 690 | /* |
| 691 | * Fill in the root. |
| 692 | */ |
| 693 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 694 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 695 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 696 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 697 | /* |
| 698 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 699 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 700 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 701 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 702 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 703 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 704 | /* |
| 705 | * Convert to a btree with two levels, one record in root. |
| 706 | */ |
| 707 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 708 | memset(&args, 0, sizeof(args)); |
| 709 | args.tp = tp; |
| 710 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 711 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 712 | args.firstblock = *firstblock; |
| 713 | if (*firstblock == NULLFSBLOCK) { |
| 714 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 715 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 716 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 717 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 718 | args.fsbno = *firstblock; |
| 719 | } else { |
| 720 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 721 | args.fsbno = *firstblock; |
| 722 | } |
| 723 | args.minlen = args.maxlen = args.prod = 1; |
| 724 | args.wasdel = wasdel; |
| 725 | *logflagsp = 0; |
| 726 | if ((error = xfs_alloc_vextent(&args))) { |
| 727 | xfs_iroot_realloc(ip, -1, whichfork); |
| 728 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 729 | return error; |
| 730 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 731 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 732 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 733 | xfs_iroot_realloc(ip, -1, whichfork); |
| 734 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 735 | return -ENOSPC; |
| 736 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 737 | /* |
| 738 | * Allocation can't fail, the space was reserved. |
| 739 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 740 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 741 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 742 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 743 | cur->bc_private.b.allocated++; |
| 744 | ip->i_d.di_nblocks++; |
| 745 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 746 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 747 | /* |
| 748 | * Fill in the child block. |
| 749 | */ |
| 750 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 751 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 752 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 753 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 754 | XFS_BTREE_LONG_PTRS); |
| 755 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 756 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 757 | if (isnullstartblock(rec.br_startblock)) |
| 758 | continue; |
| 759 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 760 | xfs_bmbt_disk_set_all(arp, &rec); |
| 761 | cnt++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 762 | } |
| 763 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 764 | xfs_btree_set_numrecs(ablock, cnt); |
| 765 | |
| 766 | /* |
| 767 | * Fill in the root key and pointer. |
| 768 | */ |
| 769 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 770 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 771 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 772 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 773 | be16_to_cpu(block->bb_level))); |
| 774 | *pp = cpu_to_be64(args.fsbno); |
| 775 | |
| 776 | /* |
| 777 | * Do all this logging at the end so that |
| 778 | * the root is at the right level. |
| 779 | */ |
| 780 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 781 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 782 | ASSERT(*curp == NULL); |
| 783 | *curp = cur; |
| 784 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Convert a local file to an extents file. |
| 790 | * This code is out of bounds for data forks of regular files, |
| 791 | * since the file data needs to get logged so things will stay consistent. |
| 792 | * (The bmap-level manipulations are ok, though). |
| 793 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 794 | void |
| 795 | xfs_bmap_local_to_extents_empty( |
| 796 | struct xfs_inode *ip, |
| 797 | int whichfork) |
| 798 | { |
| 799 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 800 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 801 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 802 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 803 | ASSERT(ifp->if_bytes == 0); |
| 804 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 805 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 806 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 807 | ifp->if_flags &= ~XFS_IFINLINE; |
| 808 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 809 | ifp->if_u1.if_root = NULL; |
| 810 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 811 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 812 | } |
| 813 | |
| 814 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 815 | STATIC int /* error */ |
| 816 | xfs_bmap_local_to_extents( |
| 817 | xfs_trans_t *tp, /* transaction pointer */ |
| 818 | xfs_inode_t *ip, /* incore inode pointer */ |
| 819 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 820 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 821 | int *logflagsp, /* inode logging flags */ |
| 822 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 823 | void (*init_fn)(struct xfs_trans *tp, |
| 824 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 825 | struct xfs_inode *ip, |
| 826 | struct xfs_ifork *ifp)) |
| 827 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 828 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 829 | int flags; /* logging flags returned */ |
| 830 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 831 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 832 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 833 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 834 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 835 | |
| 836 | /* |
| 837 | * We don't want to deal with the case of keeping inode data inline yet. |
| 838 | * So sending the data fork of a regular inode is invalid. |
| 839 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 840 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 841 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 842 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 843 | |
| 844 | if (!ifp->if_bytes) { |
| 845 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 846 | flags = XFS_ILOG_CORE; |
| 847 | goto done; |
| 848 | } |
| 849 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 850 | flags = 0; |
| 851 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 852 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 853 | memset(&args, 0, sizeof(args)); |
| 854 | args.tp = tp; |
| 855 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 856 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 857 | args.firstblock = *firstblock; |
| 858 | /* |
| 859 | * Allocate a block. We know we need only one, since the |
| 860 | * file currently fits in an inode. |
| 861 | */ |
| 862 | if (*firstblock == NULLFSBLOCK) { |
| 863 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 864 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 865 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 866 | args.fsbno = *firstblock; |
| 867 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 868 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 869 | args.total = total; |
| 870 | args.minlen = args.maxlen = args.prod = 1; |
| 871 | error = xfs_alloc_vextent(&args); |
| 872 | if (error) |
| 873 | goto done; |
| 874 | |
| 875 | /* Can't fail, the space was reserved. */ |
| 876 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 877 | ASSERT(args.len == 1); |
| 878 | *firstblock = args.fsbno; |
| 879 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 880 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 881 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 882 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 883 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 884 | * The callout is responsible for logging because the remote format |
| 885 | * might differ from the local format and thus we don't know how much to |
| 886 | * log here. Note that init_fn must also set the buffer log item type |
| 887 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 888 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 889 | init_fn(tp, bp, ip, ifp); |
| 890 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 891 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 892 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 893 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 894 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 895 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 896 | ifp->if_u1.if_root = NULL; |
| 897 | ifp->if_height = 0; |
| 898 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 899 | rec.br_startoff = 0; |
| 900 | rec.br_startblock = args.fsbno; |
| 901 | rec.br_blockcount = 1; |
| 902 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 903 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 904 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 905 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 906 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 907 | ip->i_d.di_nblocks = 1; |
| 908 | xfs_trans_mod_dquot_byino(tp, ip, |
| 909 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 910 | flags |= xfs_ilog_fext(whichfork); |
| 911 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 912 | done: |
| 913 | *logflagsp = flags; |
| 914 | return error; |
| 915 | } |
| 916 | |
| 917 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 919 | */ |
| 920 | STATIC int /* error */ |
| 921 | xfs_bmap_add_attrfork_btree( |
| 922 | xfs_trans_t *tp, /* transaction pointer */ |
| 923 | xfs_inode_t *ip, /* incore inode pointer */ |
| 924 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 925 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | int *flags) /* inode logging flags */ |
| 927 | { |
| 928 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 929 | int error; /* error return value */ |
| 930 | xfs_mount_t *mp; /* file system mount struct */ |
| 931 | int stat; /* newroot status */ |
| 932 | |
| 933 | mp = ip->i_mount; |
| 934 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 935 | *flags |= XFS_ILOG_DBROOT; |
| 936 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 937 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 938 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 940 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 941 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 943 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 944 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 945 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | goto error0; |
| 947 | if (stat == 0) { |
| 948 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 949 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | *firstblock = cur->bc_private.b.firstblock; |
| 952 | cur->bc_private.b.allocated = 0; |
| 953 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 954 | } |
| 955 | return 0; |
| 956 | error0: |
| 957 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 958 | return error; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 963 | */ |
| 964 | STATIC int /* error */ |
| 965 | xfs_bmap_add_attrfork_extents( |
| 966 | xfs_trans_t *tp, /* transaction pointer */ |
| 967 | xfs_inode_t *ip, /* incore inode pointer */ |
| 968 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 969 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | int *flags) /* inode logging flags */ |
| 971 | { |
| 972 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 973 | int error; /* error return value */ |
| 974 | |
| 975 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 976 | return 0; |
| 977 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 978 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | flags, XFS_DATA_FORK); |
| 980 | if (cur) { |
| 981 | cur->bc_private.b.allocated = 0; |
| 982 | xfs_btree_del_cursor(cur, |
| 983 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 984 | } |
| 985 | return error; |
| 986 | } |
| 987 | |
| 988 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 989 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 990 | * different data fork content type needs a different callout to do the |
| 991 | * conversion. Some are basic and only require special block initialisation |
| 992 | * callouts for the data formating, others (directories) are so specialised they |
| 993 | * handle everything themselves. |
| 994 | * |
| 995 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 996 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 997 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | */ |
| 999 | STATIC int /* error */ |
| 1000 | xfs_bmap_add_attrfork_local( |
| 1001 | xfs_trans_t *tp, /* transaction pointer */ |
| 1002 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1003 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1004 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | int *flags) /* inode logging flags */ |
| 1006 | { |
| 1007 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1010 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1011 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1012 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1014 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | dargs.dp = ip; |
| 1016 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1017 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1018 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | dargs.whichfork = XFS_DATA_FORK; |
| 1020 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1021 | return xfs_dir2_sf_to_block(&dargs); |
| 1022 | } |
| 1023 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1024 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1025 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1026 | flags, XFS_DATA_FORK, |
| 1027 | xfs_symlink_local_to_remote); |
| 1028 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1029 | /* should only be called for types that support local format data */ |
| 1030 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1031 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1035 | * Convert inode from non-attributed to attributed. |
| 1036 | * Must not be in a transaction, ip must not be locked. |
| 1037 | */ |
| 1038 | int /* error code */ |
| 1039 | xfs_bmap_add_attrfork( |
| 1040 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1041 | int size, /* space new attribute needs */ |
| 1042 | int rsvd) /* xact may use reserved blks */ |
| 1043 | { |
| 1044 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1045 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1046 | xfs_mount_t *mp; /* mount structure */ |
| 1047 | xfs_trans_t *tp; /* transaction pointer */ |
| 1048 | int blks; /* space reservation */ |
| 1049 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1050 | int logflags; /* logging flags */ |
| 1051 | int error; /* error return value */ |
| 1052 | |
| 1053 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1054 | |
| 1055 | mp = ip->i_mount; |
| 1056 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1057 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1058 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1059 | |
| 1060 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1061 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1062 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1063 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1064 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1065 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1066 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1067 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1068 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1069 | if (error) |
| 1070 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1071 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1072 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1073 | if (ip->i_d.di_anextents != 0) { |
| 1074 | error = -EFSCORRUPTED; |
| 1075 | goto trans_cancel; |
| 1076 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1077 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1078 | /* |
| 1079 | * For inodes coming from pre-6.2 filesystems. |
| 1080 | */ |
| 1081 | ASSERT(ip->i_d.di_aformat == 0); |
| 1082 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1083 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1084 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1085 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1086 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1087 | |
| 1088 | switch (ip->i_d.di_format) { |
| 1089 | case XFS_DINODE_FMT_DEV: |
| 1090 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1091 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1092 | case XFS_DINODE_FMT_LOCAL: |
| 1093 | case XFS_DINODE_FMT_EXTENTS: |
| 1094 | case XFS_DINODE_FMT_BTREE: |
| 1095 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1096 | if (!ip->i_d.di_forkoff) |
| 1097 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1098 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1099 | version = 2; |
| 1100 | break; |
| 1101 | default: |
| 1102 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1103 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1104 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | ASSERT(ip->i_afp == NULL); |
| 1108 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1109 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1110 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1111 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1112 | switch (ip->i_d.di_format) { |
| 1113 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1114 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1115 | &logflags); |
| 1116 | break; |
| 1117 | case XFS_DINODE_FMT_EXTENTS: |
| 1118 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1119 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1120 | break; |
| 1121 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1122 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1123 | &logflags); |
| 1124 | break; |
| 1125 | default: |
| 1126 | error = 0; |
| 1127 | break; |
| 1128 | } |
| 1129 | if (logflags) |
| 1130 | xfs_trans_log_inode(tp, ip, logflags); |
| 1131 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1132 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1133 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1134 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1135 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1136 | |
| 1137 | spin_lock(&mp->m_sb_lock); |
| 1138 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1139 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1140 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1141 | } |
| 1142 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1143 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1144 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1145 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1146 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1147 | if (log_sb) |
| 1148 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1149 | } |
| 1150 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1151 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1152 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1153 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1154 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1155 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1156 | return error; |
| 1157 | |
| 1158 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1159 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1160 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1161 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1162 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | return error; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * Internal and external extent tree search functions. |
| 1168 | */ |
| 1169 | |
| 1170 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1171 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1172 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1173 | int |
| 1174 | xfs_iread_extents( |
| 1175 | struct xfs_trans *tp, |
| 1176 | struct xfs_inode *ip, |
| 1177 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1178 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1179 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1180 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1181 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1182 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1183 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1184 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1185 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1186 | xfs_fsblock_t bno; |
| 1187 | struct xfs_buf *bp; |
| 1188 | xfs_extnum_t i, j; |
| 1189 | int level; |
| 1190 | __be64 *pp; |
| 1191 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1192 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1193 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1194 | |
| 1195 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1196 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1197 | return -EFSCORRUPTED; |
| 1198 | } |
| 1199 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | /* |
| 1201 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1202 | */ |
| 1203 | level = be16_to_cpu(block->bb_level); |
| 1204 | ASSERT(level > 0); |
| 1205 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1206 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1207 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1208 | /* |
| 1209 | * Go down the tree until leaf level is reached, following the first |
| 1210 | * pointer (leftmost) at each level. |
| 1211 | */ |
| 1212 | while (level-- > 0) { |
| 1213 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1214 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1215 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1216 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1217 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1218 | if (level == 0) |
| 1219 | break; |
| 1220 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1221 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1222 | XFS_WANT_CORRUPTED_GOTO(mp, |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1223 | XFS_FSB_SANITY_CHECK(mp, bno), out_brelse); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1224 | xfs_trans_brelse(tp, bp); |
| 1225 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1226 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1227 | /* |
| 1228 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1229 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1231 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1232 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1233 | /* |
| 1234 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1235 | */ |
| 1236 | for (;;) { |
| 1237 | xfs_bmbt_rec_t *frp; |
| 1238 | xfs_fsblock_t nextbno; |
| 1239 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1240 | |
| 1241 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1242 | if (unlikely(i + num_recs > nextents)) { |
| 1243 | ASSERT(i + num_recs <= nextents); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1244 | xfs_warn(ip->i_mount, |
| 1245 | "corrupt dinode %Lu, (btree extents).", |
| 1246 | (unsigned long long) ip->i_ino); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1247 | XFS_CORRUPTION_ERROR(__func__, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1248 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1249 | error = -EFSCORRUPTED; |
| 1250 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1251 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1252 | /* |
| 1253 | * Read-ahead the next leaf block, if any. |
| 1254 | */ |
| 1255 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1256 | if (nextbno != NULLFSBLOCK) |
| 1257 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1258 | &xfs_bmbt_buf_ops); |
| 1259 | /* |
| 1260 | * Copy records into the extent records. |
| 1261 | */ |
| 1262 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1263 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1264 | xfs_bmbt_disk_get_all(frp, &new); |
| 1265 | if (!xfs_bmbt_validate_extent(mp, whichfork, &new)) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1266 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
Christoph Hellwig | 0c1d9e4 | 2017-04-20 09:42:48 -0700 | [diff] [blame] | 1267 | XFS_ERRLEVEL_LOW, mp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1268 | error = -EFSCORRUPTED; |
| 1269 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1270 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1271 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1272 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1273 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1274 | } |
| 1275 | xfs_trans_brelse(tp, bp); |
| 1276 | bno = nextbno; |
| 1277 | /* |
| 1278 | * If we've reached the end, stop. |
| 1279 | */ |
| 1280 | if (bno == NULLFSBLOCK) |
| 1281 | break; |
| 1282 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1283 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1284 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1285 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1286 | block = XFS_BUF_TO_BLOCK(bp); |
| 1287 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1288 | |
| 1289 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1290 | error = -EFSCORRUPTED; |
| 1291 | goto out; |
| 1292 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1293 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1294 | |
| 1295 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1296 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1297 | |
| 1298 | out_brelse: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1299 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1300 | out: |
| 1301 | xfs_iext_destroy(ifp); |
| 1302 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1303 | } |
| 1304 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1305 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1306 | * Returns the relative block number of the first unused block(s) in the given |
| 1307 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1308 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1309 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1310 | */ |
| 1311 | int /* error */ |
| 1312 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1313 | struct xfs_trans *tp, /* transaction pointer */ |
| 1314 | struct xfs_inode *ip, /* incore inode */ |
| 1315 | xfs_extlen_t len, /* size of hole to find */ |
| 1316 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1317 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1318 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1319 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1320 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1321 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1322 | xfs_fileoff_t lastaddr = 0; |
| 1323 | xfs_fileoff_t lowest, max; |
| 1324 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1325 | |
| 1326 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1327 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1328 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1329 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1330 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1331 | *first_unused = 0; |
| 1332 | return 0; |
| 1333 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1334 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1335 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1336 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1337 | if (error) |
| 1338 | return error; |
| 1339 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1340 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1341 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1342 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1343 | /* |
| 1344 | * See if the hole before this extent will work. |
| 1345 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1346 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1347 | got.br_startoff - max >= len) |
| 1348 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1349 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1350 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1351 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1352 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1353 | *first_unused = max; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1358 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1359 | * last_block (input value) in the file. |
| 1360 | * This is not based on i_size, it is based on the extent records. |
| 1361 | * Returns 0 for local files, as they do not have extent records. |
| 1362 | */ |
| 1363 | int /* error */ |
| 1364 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1365 | struct xfs_trans *tp, /* transaction pointer */ |
| 1366 | struct xfs_inode *ip, /* incore inode */ |
| 1367 | xfs_fileoff_t *last_block, /* last block */ |
| 1368 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1369 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1370 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1371 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1372 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1373 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1374 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1375 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1376 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1377 | *last_block = 0; |
| 1378 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1379 | case XFS_DINODE_FMT_BTREE: |
| 1380 | case XFS_DINODE_FMT_EXTENTS: |
| 1381 | break; |
| 1382 | default: |
| 1383 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1384 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1385 | |
| 1386 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1387 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1388 | if (error) |
| 1389 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1390 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1391 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1392 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1393 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1397 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1398 | xfs_bmap_last_extent( |
| 1399 | struct xfs_trans *tp, |
| 1400 | struct xfs_inode *ip, |
| 1401 | int whichfork, |
| 1402 | struct xfs_bmbt_irec *rec, |
| 1403 | int *is_empty) |
| 1404 | { |
| 1405 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1406 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1407 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1408 | |
| 1409 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1410 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1411 | if (error) |
| 1412 | return error; |
| 1413 | } |
| 1414 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1415 | xfs_iext_last(ifp, &icur); |
| 1416 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1417 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1418 | else |
| 1419 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Check the last inode extent to determine whether this allocation will result |
| 1425 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1426 | * blocks at the end of the file which do not start at the previous data block, |
| 1427 | * we will try to align the new blocks at stripe unit boundaries. |
| 1428 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1429 | * 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] | 1430 | * at, or past the EOF. |
| 1431 | */ |
| 1432 | STATIC int |
| 1433 | xfs_bmap_isaeof( |
| 1434 | struct xfs_bmalloca *bma, |
| 1435 | int whichfork) |
| 1436 | { |
| 1437 | struct xfs_bmbt_irec rec; |
| 1438 | int is_empty; |
| 1439 | int error; |
| 1440 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1441 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1442 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1443 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1444 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1445 | return error; |
| 1446 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1447 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1448 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1452 | /* |
| 1453 | * Check if we are allocation or past the last extent, or at least into |
| 1454 | * the last delayed allocated extent. |
| 1455 | */ |
| 1456 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1457 | (bma->offset >= rec.br_startoff && |
| 1458 | isnullstartblock(rec.br_startblock)); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1463 | * Returns the file-relative block number of the first block past eof in |
| 1464 | * the file. This is not based on i_size, it is based on the extent records. |
| 1465 | * Returns 0 for local files, as they do not have extent records. |
| 1466 | */ |
| 1467 | int |
| 1468 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1469 | struct xfs_inode *ip, |
| 1470 | xfs_fileoff_t *last_block, |
| 1471 | int whichfork) |
| 1472 | { |
| 1473 | struct xfs_bmbt_irec rec; |
| 1474 | int is_empty; |
| 1475 | int error; |
| 1476 | |
| 1477 | *last_block = 0; |
| 1478 | |
| 1479 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1480 | return 0; |
| 1481 | |
| 1482 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1483 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1484 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1485 | |
| 1486 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1487 | if (error || is_empty) |
| 1488 | return error; |
| 1489 | |
| 1490 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | * Returns whether the selected fork of the inode has exactly one |
| 1496 | * block or not. For the data fork we check this matches di_size, |
| 1497 | * implying the file's range is 0..bsize-1. |
| 1498 | */ |
| 1499 | int /* 1=>1 block, 0=>otherwise */ |
| 1500 | xfs_bmap_one_block( |
| 1501 | xfs_inode_t *ip, /* incore inode */ |
| 1502 | int whichfork) /* data or attr fork */ |
| 1503 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1504 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1505 | int rval; /* return value */ |
| 1506 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1507 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1508 | |
| 1509 | #ifndef DEBUG |
| 1510 | if (whichfork == XFS_DATA_FORK) |
| 1511 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1512 | #endif /* !DEBUG */ |
| 1513 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1514 | return 0; |
| 1515 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1516 | return 0; |
| 1517 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1518 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1519 | xfs_iext_first(ifp, &icur); |
| 1520 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1521 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1522 | if (rval && whichfork == XFS_DATA_FORK) |
| 1523 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1524 | return rval; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * Extent tree manipulation functions used during allocation. |
| 1529 | */ |
| 1530 | |
| 1531 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1532 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | */ |
| 1534 | STATIC int /* error */ |
| 1535 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1536 | struct xfs_bmalloca *bma, |
| 1537 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1539 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1542 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1544 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1545 | /* left is 0, right is 1, prev is 2 */ |
| 1546 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1547 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1548 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1549 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1550 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1552 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1553 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1554 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1556 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1557 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1558 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1559 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1560 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1561 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1562 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1563 | ASSERT(!bma->cur || |
| 1564 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1565 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1566 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | #define LEFT r[0] |
| 1569 | #define RIGHT r[1] |
| 1570 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
| 1572 | /* |
| 1573 | * Set up a bunch of variables to make the tests simpler. |
| 1574 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1575 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1577 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1579 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1580 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1581 | da_old = startblockval(PREV.br_startblock); |
| 1582 | da_new = 0; |
| 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | /* |
| 1585 | * Set flags determining what part of the previous delayed allocation |
| 1586 | * extent is being replaced by a real allocation. |
| 1587 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1588 | if (PREV.br_startoff == new->br_startoff) |
| 1589 | state |= BMAP_LEFT_FILLING; |
| 1590 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1591 | state |= BMAP_RIGHT_FILLING; |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | /* |
| 1594 | * Check and set flags if this segment has a left neighbor. |
| 1595 | * Don't set contiguous if the combined extent would be too large. |
| 1596 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1597 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1598 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1599 | if (isnullstartblock(LEFT.br_startblock)) |
| 1600 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1602 | |
| 1603 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1604 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1605 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1606 | LEFT.br_state == new->br_state && |
| 1607 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1608 | state |= BMAP_LEFT_CONTIG; |
| 1609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | /* |
| 1611 | * Check and set flags if this segment has a right neighbor. |
| 1612 | * Don't set contiguous if the combined extent would be too large. |
| 1613 | * Also check for all-three-contiguous being too large. |
| 1614 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1615 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1616 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1617 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1618 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1620 | |
| 1621 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1622 | new_endoff == RIGHT.br_startoff && |
| 1623 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1624 | new->br_state == RIGHT.br_state && |
| 1625 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1626 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1627 | BMAP_RIGHT_FILLING)) != |
| 1628 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1629 | BMAP_RIGHT_FILLING) || |
| 1630 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1631 | <= MAXEXTLEN)) |
| 1632 | state |= BMAP_RIGHT_CONTIG; |
| 1633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | error = 0; |
| 1635 | /* |
| 1636 | * Switch out based on the FILLING and CONTIG state bits. |
| 1637 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1638 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1639 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1640 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1641 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | /* |
| 1643 | * Filling in all of a previously delayed allocation extent. |
| 1644 | * The left and right neighbors are both contiguous with new. |
| 1645 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1646 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1647 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1648 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1649 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1650 | xfs_iext_prev(ifp, &bma->icur); |
| 1651 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1652 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1653 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1654 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1656 | else { |
| 1657 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1658 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1659 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1661 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1662 | error = xfs_btree_delete(bma->cur, &i); |
| 1663 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1665 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1666 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1667 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1669 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1670 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1671 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | goto done; |
| 1673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | break; |
| 1675 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1676 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | /* |
| 1678 | * Filling in all of a previously delayed allocation extent. |
| 1679 | * The left neighbor is contiguous, the right is not. |
| 1680 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1681 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1682 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1683 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1684 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1685 | xfs_iext_prev(ifp, &bma->icur); |
| 1686 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1687 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1688 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | rval = XFS_ILOG_DEXT; |
| 1690 | else { |
| 1691 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1692 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1693 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1695 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1696 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1697 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | goto done; |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | break; |
| 1701 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1702 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | /* |
| 1704 | * Filling in all of a previously delayed allocation extent. |
| 1705 | * The right neighbor is contiguous, the left is not. |
| 1706 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1707 | PREV.br_startblock = new->br_startblock; |
| 1708 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1709 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1710 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1711 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1712 | xfs_iext_prev(ifp, &bma->icur); |
| 1713 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1714 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1715 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | rval = XFS_ILOG_DEXT; |
| 1717 | else { |
| 1718 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1719 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1720 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1722 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1723 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1724 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | goto done; |
| 1726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | break; |
| 1728 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1729 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | /* |
| 1731 | * Filling in all of a previously delayed allocation extent. |
| 1732 | * Neither the left nor right neighbors are contiguous with |
| 1733 | * the new one. |
| 1734 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1735 | PREV.br_startblock = new->br_startblock; |
| 1736 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1737 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1738 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1739 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1740 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1742 | else { |
| 1743 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1744 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1745 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1747 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1748 | error = xfs_btree_insert(bma->cur, &i); |
| 1749 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1751 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | break; |
| 1754 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1755 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * Filling in the first part of a previous delayed allocation. |
| 1758 | * The left neighbor is contiguous. |
| 1759 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1760 | old = LEFT; |
| 1761 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1762 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1763 | startblockval(PREV.br_startblock)); |
| 1764 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1765 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1766 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1767 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1768 | PREV.br_startoff += new->br_blockcount; |
| 1769 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1770 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1771 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1772 | xfs_iext_prev(ifp, &bma->icur); |
| 1773 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1774 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1775 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | rval = XFS_ILOG_DEXT; |
| 1777 | else { |
| 1778 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1779 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1780 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1782 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1783 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1784 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | goto done; |
| 1786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | break; |
| 1788 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1789 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | /* |
| 1791 | * Filling in the first part of a previous delayed allocation. |
| 1792 | * The left neighbor is not contiguous. |
| 1793 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1794 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1795 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1796 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1798 | else { |
| 1799 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1800 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1801 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1803 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1804 | error = xfs_btree_insert(bma->cur, &i); |
| 1805 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1807 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1809 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1810 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1811 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1812 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1813 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | rval |= tmp_rval; |
| 1815 | if (error) |
| 1816 | goto done; |
| 1817 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1818 | |
| 1819 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1820 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1821 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1822 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1823 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1824 | PREV.br_startoff = new_endoff; |
| 1825 | PREV.br_blockcount = temp; |
| 1826 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1827 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1828 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1829 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | break; |
| 1831 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1832 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | /* |
| 1834 | * Filling in the last part of a previous delayed allocation. |
| 1835 | * The right neighbor is contiguous with the new allocation. |
| 1836 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1837 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1838 | RIGHT.br_startoff = new->br_startoff; |
| 1839 | RIGHT.br_startblock = new->br_startblock; |
| 1840 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1841 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1842 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | rval = XFS_ILOG_DEXT; |
| 1844 | else { |
| 1845 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1846 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1847 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1849 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1850 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | goto done; |
| 1853 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1854 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1855 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1856 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1857 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1858 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1859 | PREV.br_blockcount = temp; |
| 1860 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1861 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1862 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1863 | xfs_iext_next(ifp, &bma->icur); |
| 1864 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | break; |
| 1866 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1867 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | /* |
| 1869 | * Filling in the last part of a previous delayed allocation. |
| 1870 | * The right neighbor is not contiguous. |
| 1871 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1872 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1873 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1874 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1876 | else { |
| 1877 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1878 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1879 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1881 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1882 | error = xfs_btree_insert(bma->cur, &i); |
| 1883 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1885 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1887 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1888 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1889 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1890 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1891 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | rval |= tmp_rval; |
| 1893 | if (error) |
| 1894 | goto done; |
| 1895 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1896 | |
| 1897 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1898 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1899 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1900 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1901 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1902 | PREV.br_startblock = nullstartblock(da_new); |
| 1903 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1904 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1905 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | case 0: |
| 1909 | /* |
| 1910 | * Filling in the middle part of a previous delayed allocation. |
| 1911 | * Contiguity is impossible here. |
| 1912 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1913 | * |
| 1914 | * We start with a delayed allocation: |
| 1915 | * |
| 1916 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1917 | * PREV @ idx |
| 1918 | * |
| 1919 | * and we are allocating: |
| 1920 | * +rrrrrrrrrrrrrrrrr+ |
| 1921 | * new |
| 1922 | * |
| 1923 | * and we set it up for insertion as: |
| 1924 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1925 | * new |
| 1926 | * PREV @ idx LEFT RIGHT |
| 1927 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1929 | old = PREV; |
| 1930 | |
| 1931 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1932 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1933 | |
| 1934 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1935 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1936 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1937 | RIGHT.br_blockcount = |
| 1938 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1939 | RIGHT.br_startblock = |
| 1940 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1941 | RIGHT.br_blockcount)); |
| 1942 | |
| 1943 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1944 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1945 | PREV.br_startblock = |
| 1946 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1947 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1948 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1949 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1950 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1951 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1952 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1953 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1954 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1955 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1957 | else { |
| 1958 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1959 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1960 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1962 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1963 | error = xfs_btree_insert(bma->cur, &i); |
| 1964 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1966 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1968 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1969 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1970 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1971 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1972 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | rval |= tmp_rval; |
| 1974 | if (error) |
| 1975 | goto done; |
| 1976 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1977 | |
| 1978 | da_new = startblockval(PREV.br_startblock) + |
| 1979 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | break; |
| 1981 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1982 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1983 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1984 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1985 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1986 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1987 | case BMAP_LEFT_CONTIG: |
| 1988 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | /* |
| 1990 | * These cases are all impossible. |
| 1991 | */ |
| 1992 | ASSERT(0); |
| 1993 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1994 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 1995 | /* add reverse mapping */ |
| 1996 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 1997 | if (error) |
| 1998 | goto done; |
| 1999 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2000 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2001 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2002 | int tmp_logflags; /* partial log flag return val */ |
| 2003 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2004 | ASSERT(bma->cur == NULL); |
| 2005 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2006 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2007 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2008 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2009 | if (error) |
| 2010 | goto done; |
| 2011 | } |
| 2012 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2013 | if (bma->cur) { |
| 2014 | da_new += bma->cur->bc_private.b.allocated; |
| 2015 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2018 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2019 | if (da_new != da_old) { |
| 2020 | ASSERT(state == 0 || da_new < da_old); |
| 2021 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2022 | false); |
| 2023 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2024 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2025 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2027 | if (whichfork != XFS_COW_FORK) |
| 2028 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | return error; |
| 2030 | #undef LEFT |
| 2031 | #undef RIGHT |
| 2032 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2036 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | */ |
| 2038 | STATIC int /* error */ |
| 2039 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2040 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2042 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2043 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2045 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2046 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2047 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2048 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2053 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2056 | /* left is 0, right is 1, prev is 2 */ |
| 2057 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2058 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2059 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2060 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2062 | *logflagsp = 0; |
| 2063 | |
| 2064 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2065 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2066 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2067 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2068 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2069 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | #define LEFT r[0] |
| 2072 | #define RIGHT r[1] |
| 2073 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Set up a bunch of variables to make the tests simpler. |
| 2077 | */ |
| 2078 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2079 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2080 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2082 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2083 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | /* |
| 2086 | * Set flags determining what part of the previous oldext allocation |
| 2087 | * extent is being replaced by a newext allocation. |
| 2088 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2089 | if (PREV.br_startoff == new->br_startoff) |
| 2090 | state |= BMAP_LEFT_FILLING; |
| 2091 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2092 | state |= BMAP_RIGHT_FILLING; |
| 2093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | /* |
| 2095 | * Check and set flags if this segment has a left neighbor. |
| 2096 | * Don't set contiguous if the combined extent would be too large. |
| 2097 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2098 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2099 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2100 | if (isnullstartblock(LEFT.br_startblock)) |
| 2101 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2103 | |
| 2104 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2105 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2106 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2107 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2108 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2109 | state |= BMAP_LEFT_CONTIG; |
| 2110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | /* |
| 2112 | * Check and set flags if this segment has a right neighbor. |
| 2113 | * Don't set contiguous if the combined extent would be too large. |
| 2114 | * Also check for all-three-contiguous being too large. |
| 2115 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2116 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2117 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2118 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2119 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2121 | |
| 2122 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2123 | new_endoff == RIGHT.br_startoff && |
| 2124 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2125 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2126 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2127 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2128 | BMAP_RIGHT_FILLING)) != |
| 2129 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2130 | BMAP_RIGHT_FILLING) || |
| 2131 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2132 | <= MAXEXTLEN)) |
| 2133 | state |= BMAP_RIGHT_CONTIG; |
| 2134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | /* |
| 2136 | * Switch out based on the FILLING and CONTIG state bits. |
| 2137 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2138 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2139 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2140 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2141 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | /* |
| 2143 | * Setting all of a previous oldext extent to newext. |
| 2144 | * The left and right neighbors are both contiguous with new. |
| 2145 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2146 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2147 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2148 | xfs_iext_remove(ip, icur, state); |
| 2149 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2150 | xfs_iext_prev(ifp, icur); |
| 2151 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2152 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2153 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | if (cur == NULL) |
| 2155 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2156 | else { |
| 2157 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2158 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2159 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2161 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2162 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2164 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2165 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2167 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2168 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2170 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2171 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2173 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2174 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2175 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | goto done; |
| 2177 | } |
| 2178 | break; |
| 2179 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2180 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | /* |
| 2182 | * Setting all of a previous oldext extent to newext. |
| 2183 | * The left neighbor is contiguous, the right is not. |
| 2184 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2185 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2186 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2187 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2188 | xfs_iext_prev(ifp, icur); |
| 2189 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2190 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2191 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | if (cur == NULL) |
| 2193 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2194 | else { |
| 2195 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2196 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2197 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2199 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2200 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2202 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2203 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2205 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2206 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2207 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | goto done; |
| 2209 | } |
| 2210 | break; |
| 2211 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2212 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | /* |
| 2214 | * Setting all of a previous oldext extent to newext. |
| 2215 | * The right neighbor is contiguous, the left is not. |
| 2216 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2217 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2218 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2219 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2220 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2221 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2222 | xfs_iext_prev(ifp, icur); |
| 2223 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2224 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2225 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2226 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | if (cur == NULL) |
| 2228 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2229 | else { |
| 2230 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2231 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2232 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2234 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2235 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2237 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2238 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2240 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2241 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2242 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | goto done; |
| 2244 | } |
| 2245 | break; |
| 2246 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2247 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | /* |
| 2249 | * Setting all of a previous oldext extent to newext. |
| 2250 | * Neither the left nor right neighbors are contiguous with |
| 2251 | * the new one. |
| 2252 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2253 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2254 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | if (cur == NULL) |
| 2257 | rval = XFS_ILOG_DEXT; |
| 2258 | else { |
| 2259 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2260 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2261 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2263 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2264 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2265 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | goto done; |
| 2267 | } |
| 2268 | break; |
| 2269 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2270 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | /* |
| 2272 | * Setting the first part of a previous oldext extent to newext. |
| 2273 | * The left neighbor is contiguous. |
| 2274 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2275 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2276 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2277 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2278 | PREV.br_startoff += new->br_blockcount; |
| 2279 | PREV.br_startblock += new->br_blockcount; |
| 2280 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2281 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2282 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2283 | xfs_iext_prev(ifp, icur); |
| 2284 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | if (cur == NULL) |
| 2287 | rval = XFS_ILOG_DEXT; |
| 2288 | else { |
| 2289 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2290 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2291 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2293 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2294 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2295 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2297 | error = xfs_btree_decrement(cur, 0, &i); |
| 2298 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2300 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2301 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | goto done; |
| 2303 | } |
| 2304 | break; |
| 2305 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2306 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | /* |
| 2308 | * Setting the first part of a previous oldext extent to newext. |
| 2309 | * The left neighbor is not contiguous. |
| 2310 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2311 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2312 | PREV.br_startoff += new->br_blockcount; |
| 2313 | PREV.br_startblock += new->br_blockcount; |
| 2314 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2315 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2316 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2317 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2318 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2319 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | if (cur == NULL) |
| 2321 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2322 | else { |
| 2323 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2324 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2325 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2327 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2328 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2329 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | goto done; |
| 2331 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2332 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2334 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | } |
| 2336 | break; |
| 2337 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2338 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | /* |
| 2340 | * Setting the last part of a previous oldext extent to newext. |
| 2341 | * The right neighbor is contiguous with the new allocation. |
| 2342 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2343 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2344 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2345 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2346 | RIGHT.br_startoff = new->br_startoff; |
| 2347 | RIGHT.br_startblock = new->br_startblock; |
| 2348 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2349 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2350 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2351 | xfs_iext_next(ifp, icur); |
| 2352 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | if (cur == NULL) |
| 2355 | rval = XFS_ILOG_DEXT; |
| 2356 | else { |
| 2357 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2358 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2359 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2361 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2362 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2363 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2365 | error = xfs_btree_increment(cur, 0, &i); |
| 2366 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2368 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2369 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | goto done; |
| 2371 | } |
| 2372 | break; |
| 2373 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2374 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | /* |
| 2376 | * Setting the last part of a previous oldext extent to newext. |
| 2377 | * The right neighbor is not contiguous. |
| 2378 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2379 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2380 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2381 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2382 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2383 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2384 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2385 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2386 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2387 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | if (cur == NULL) |
| 2389 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2390 | else { |
| 2391 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2392 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2393 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2395 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2396 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2397 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2399 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2400 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2402 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2403 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2405 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | } |
| 2407 | break; |
| 2408 | |
| 2409 | case 0: |
| 2410 | /* |
| 2411 | * Setting the middle part of a previous oldext extent to |
| 2412 | * newext. Contiguity is impossible here. |
| 2413 | * One extent becomes three extents. |
| 2414 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2415 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2416 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | r[0] = *new; |
| 2419 | r[1].br_startoff = new_endoff; |
| 2420 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2421 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2423 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2424 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2425 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2426 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2427 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2428 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2429 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2430 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2431 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | if (cur == NULL) |
| 2433 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2434 | else { |
| 2435 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2436 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2437 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2439 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2441 | error = xfs_bmbt_update(cur, &r[1]); |
| 2442 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | goto done; |
| 2444 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2446 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2448 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2449 | /* |
| 2450 | * Reset the cursor to the position of the new extent |
| 2451 | * we are about to insert as we can't trust it after |
| 2452 | * the previous insert. |
| 2453 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2454 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2455 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2457 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2459 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2461 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | } |
| 2463 | break; |
| 2464 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2465 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2466 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2467 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2468 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2469 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2470 | case BMAP_LEFT_CONTIG: |
| 2471 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | /* |
| 2473 | * These cases are all impossible. |
| 2474 | */ |
| 2475 | ASSERT(0); |
| 2476 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2477 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2478 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2479 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2480 | if (error) |
| 2481 | goto done; |
| 2482 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2483 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2484 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2485 | int tmp_logflags; /* partial log flag return val */ |
| 2486 | |
| 2487 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2488 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2489 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2490 | *logflagsp |= tmp_logflags; |
| 2491 | if (error) |
| 2492 | goto done; |
| 2493 | } |
| 2494 | |
| 2495 | /* clear out the allocated field, done with it now in any case. */ |
| 2496 | if (cur) { |
| 2497 | cur->bc_private.b.allocated = 0; |
| 2498 | *curp = cur; |
| 2499 | } |
| 2500 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2501 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2503 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | return error; |
| 2505 | #undef LEFT |
| 2506 | #undef RIGHT |
| 2507 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2511 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2513 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | xfs_bmap_add_extent_hole_delay( |
| 2515 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2516 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2517 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2518 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2520 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2522 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2523 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2524 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2525 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2526 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2528 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2529 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | /* |
| 2532 | * Check and set flags if this segment has a left neighbor |
| 2533 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2534 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2535 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2536 | if (isnullstartblock(left.br_startblock)) |
| 2537 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | /* |
| 2541 | * Check and set flags if the current (right) segment exists. |
| 2542 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2543 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2544 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2545 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2546 | if (isnullstartblock(right.br_startblock)) |
| 2547 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | /* |
| 2551 | * Set contiguity flags on the left and right neighbors. |
| 2552 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2553 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2554 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2555 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2556 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2557 | state |= BMAP_LEFT_CONTIG; |
| 2558 | |
| 2559 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2560 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2561 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2562 | (!(state & BMAP_LEFT_CONTIG) || |
| 2563 | (left.br_blockcount + new->br_blockcount + |
| 2564 | right.br_blockcount <= MAXEXTLEN))) |
| 2565 | state |= BMAP_RIGHT_CONTIG; |
| 2566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | /* |
| 2568 | * Switch out based on the contiguity flags. |
| 2569 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2570 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2571 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | /* |
| 2573 | * New allocation is contiguous with delayed allocations |
| 2574 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2575 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | */ |
| 2577 | temp = left.br_blockcount + new->br_blockcount + |
| 2578 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2579 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2580 | oldlen = startblockval(left.br_startblock) + |
| 2581 | startblockval(new->br_startblock) + |
| 2582 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2583 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2584 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2585 | left.br_startblock = nullstartblock(newlen); |
| 2586 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2587 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2588 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2589 | xfs_iext_prev(ifp, icur); |
| 2590 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | break; |
| 2592 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2593 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | /* |
| 2595 | * New allocation is contiguous with a delayed allocation |
| 2596 | * on the left. |
| 2597 | * Merge the new allocation with the left neighbor. |
| 2598 | */ |
| 2599 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2600 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2601 | oldlen = startblockval(left.br_startblock) + |
| 2602 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2603 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2604 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2605 | left.br_blockcount = temp; |
| 2606 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2607 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2608 | xfs_iext_prev(ifp, icur); |
| 2609 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | break; |
| 2611 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2612 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | /* |
| 2614 | * New allocation is contiguous with a delayed allocation |
| 2615 | * on the right. |
| 2616 | * Merge the new allocation with the right neighbor. |
| 2617 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2619 | oldlen = startblockval(new->br_startblock) + |
| 2620 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2621 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2622 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2623 | right.br_startoff = new->br_startoff; |
| 2624 | right.br_startblock = nullstartblock(newlen); |
| 2625 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2626 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | break; |
| 2628 | |
| 2629 | case 0: |
| 2630 | /* |
| 2631 | * New allocation is not contiguous with another |
| 2632 | * delayed allocation. |
| 2633 | * Insert a new entry. |
| 2634 | */ |
| 2635 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2636 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | break; |
| 2638 | } |
| 2639 | if (oldlen != newlen) { |
| 2640 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2641 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2642 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | /* |
| 2644 | * Nothing to do for disk quota accounting here. |
| 2645 | */ |
| 2646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2650 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | */ |
| 2652 | STATIC int /* error */ |
| 2653 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2654 | struct xfs_trans *tp, |
| 2655 | struct xfs_inode *ip, |
| 2656 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2657 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2658 | struct xfs_btree_cur **curp, |
| 2659 | struct xfs_bmbt_irec *new, |
| 2660 | xfs_fsblock_t *first, |
| 2661 | struct xfs_defer_ops *dfops, |
| 2662 | int *logflagsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2664 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2665 | struct xfs_mount *mp = ip->i_mount; |
| 2666 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2670 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2671 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2672 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2673 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2675 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2676 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2677 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2678 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | /* |
| 2681 | * Check and set flags if this segment has a left neighbor. |
| 2682 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2683 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2684 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2685 | if (isnullstartblock(left.br_startblock)) |
| 2686 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | /* |
| 2690 | * Check and set flags if this segment has a current value. |
| 2691 | * Not true if we're inserting into the "hole" at eof. |
| 2692 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2693 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2694 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2695 | if (isnullstartblock(right.br_startblock)) |
| 2696 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | /* |
| 2700 | * We're inserting a real allocation between "left" and "right". |
| 2701 | * Set the contiguity flags. Don't let extents get too large. |
| 2702 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2703 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2704 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2705 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2706 | left.br_state == new->br_state && |
| 2707 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2708 | state |= BMAP_LEFT_CONTIG; |
| 2709 | |
| 2710 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2711 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2712 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2713 | new->br_state == right.br_state && |
| 2714 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2715 | (!(state & BMAP_LEFT_CONTIG) || |
| 2716 | left.br_blockcount + new->br_blockcount + |
| 2717 | right.br_blockcount <= MAXEXTLEN)) |
| 2718 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2720 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | /* |
| 2722 | * Select which case we're in here, and implement it. |
| 2723 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2724 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2725 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | /* |
| 2727 | * New allocation is contiguous with real allocations on the |
| 2728 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2729 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2731 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2732 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2733 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2734 | xfs_iext_prev(ifp, icur); |
| 2735 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2736 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2737 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2738 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2739 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2740 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2741 | } else { |
| 2742 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2743 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2744 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2745 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2746 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2747 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2748 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2749 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2750 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2751 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2752 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2753 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2754 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2755 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2756 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2757 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2759 | break; |
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 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | /* |
| 2763 | * New allocation is contiguous with a real allocation |
| 2764 | * on the left. |
| 2765 | * Merge the new allocation with the left neighbor. |
| 2766 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2767 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2768 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2769 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2770 | xfs_iext_prev(ifp, icur); |
| 2771 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2772 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2773 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2774 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2775 | } else { |
| 2776 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2777 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2778 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2779 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2780 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2781 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2782 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2783 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2785 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2787 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | /* |
| 2789 | * New allocation is contiguous with a real allocation |
| 2790 | * on the right. |
| 2791 | * Merge the new allocation with the right neighbor. |
| 2792 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2793 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2794 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2795 | right.br_startoff = new->br_startoff; |
| 2796 | right.br_startblock = new->br_startblock; |
| 2797 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2798 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2799 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2800 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2801 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2802 | } else { |
| 2803 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2804 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2805 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2806 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2807 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2808 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2809 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2810 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2812 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | |
| 2814 | case 0: |
| 2815 | /* |
| 2816 | * New allocation is not contiguous with another |
| 2817 | * real allocation. |
| 2818 | * Insert a new entry. |
| 2819 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2820 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2821 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2822 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2823 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2824 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2825 | } else { |
| 2826 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2827 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2828 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2829 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2830 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2831 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2832 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2833 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2834 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2836 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2838 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2839 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2840 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2841 | if (error) |
| 2842 | goto done; |
| 2843 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2844 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2845 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2846 | int tmp_logflags; /* partial log flag return val */ |
| 2847 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2848 | ASSERT(cur == NULL); |
| 2849 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2850 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2851 | *logflagsp |= tmp_logflags; |
| 2852 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2853 | if (error) |
| 2854 | goto done; |
| 2855 | } |
| 2856 | |
| 2857 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2858 | if (cur) |
| 2859 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2860 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2861 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2862 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2863 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2864 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | } |
| 2866 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2867 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2868 | * Functions used in the extent read, allocate and remove paths |
| 2869 | */ |
| 2870 | |
| 2871 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2872 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2873 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2874 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2875 | xfs_bmap_extsize_align( |
| 2876 | xfs_mount_t *mp, |
| 2877 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2878 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2879 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2880 | int rt, /* is this a realtime inode? */ |
| 2881 | int eof, /* is extent at end-of-file? */ |
| 2882 | int delay, /* creating delalloc extent? */ |
| 2883 | int convert, /* overwriting unwritten extent? */ |
| 2884 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2885 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2886 | { |
| 2887 | xfs_fileoff_t orig_off; /* original offset */ |
| 2888 | xfs_extlen_t orig_alen; /* original length */ |
| 2889 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2890 | xfs_fileoff_t nexto; /* next file offset */ |
| 2891 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2892 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2893 | xfs_extlen_t align_alen; /* temp for length */ |
| 2894 | xfs_extlen_t temp; /* temp for calculations */ |
| 2895 | |
| 2896 | if (convert) |
| 2897 | return 0; |
| 2898 | |
| 2899 | orig_off = align_off = *offp; |
| 2900 | orig_alen = align_alen = *lenp; |
| 2901 | orig_end = orig_off + orig_alen; |
| 2902 | |
| 2903 | /* |
| 2904 | * If this request overlaps an existing extent, then don't |
| 2905 | * attempt to perform any additional alignment. |
| 2906 | */ |
| 2907 | if (!delay && !eof && |
| 2908 | (orig_off >= gotp->br_startoff) && |
| 2909 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2910 | return 0; |
| 2911 | } |
| 2912 | |
| 2913 | /* |
| 2914 | * If the file offset is unaligned vs. the extent size |
| 2915 | * we need to align it. This will be possible unless |
| 2916 | * the file was previously written with a kernel that didn't |
| 2917 | * perform this alignment, or if a truncate shot us in the |
| 2918 | * foot. |
| 2919 | */ |
| 2920 | temp = do_mod(orig_off, extsz); |
| 2921 | if (temp) { |
| 2922 | align_alen += temp; |
| 2923 | align_off -= temp; |
| 2924 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2925 | |
| 2926 | /* Same adjustment for the end of the requested area. */ |
| 2927 | temp = (align_alen % extsz); |
| 2928 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2929 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2930 | |
| 2931 | /* |
| 2932 | * For large extent hint sizes, the aligned extent might be larger than |
| 2933 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2934 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2935 | * short allocation just fine, so it is safe to do this. We only want to |
| 2936 | * do it when we are forced to, though, because it means more allocation |
| 2937 | * operations are required. |
| 2938 | */ |
| 2939 | while (align_alen > MAXEXTLEN) |
| 2940 | align_alen -= extsz; |
| 2941 | ASSERT(align_alen <= MAXEXTLEN); |
| 2942 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2943 | /* |
| 2944 | * If the previous block overlaps with this proposed allocation |
| 2945 | * then move the start forward without adjusting the length. |
| 2946 | */ |
| 2947 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2948 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2949 | prevo = prevp->br_startoff; |
| 2950 | else |
| 2951 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2952 | } else |
| 2953 | prevo = 0; |
| 2954 | if (align_off != orig_off && align_off < prevo) |
| 2955 | align_off = prevo; |
| 2956 | /* |
| 2957 | * If the next block overlaps with this proposed allocation |
| 2958 | * then move the start back without adjusting the length, |
| 2959 | * but not before offset 0. |
| 2960 | * This may of course make the start overlap previous block, |
| 2961 | * and if we hit the offset 0 limit then the next block |
| 2962 | * can still overlap too. |
| 2963 | */ |
| 2964 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2965 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2966 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2967 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2968 | else |
| 2969 | nexto = gotp->br_startoff; |
| 2970 | } else |
| 2971 | nexto = NULLFILEOFF; |
| 2972 | if (!eof && |
| 2973 | align_off + align_alen != orig_end && |
| 2974 | align_off + align_alen > nexto) |
| 2975 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2976 | /* |
| 2977 | * If we're now overlapping the next or previous extent that |
| 2978 | * means we can't fit an extsz piece in this hole. Just move |
| 2979 | * the start forward to the first valid spot and set |
| 2980 | * the length so we hit the end. |
| 2981 | */ |
| 2982 | if (align_off != orig_off && align_off < prevo) |
| 2983 | align_off = prevo; |
| 2984 | if (align_off + align_alen != orig_end && |
| 2985 | align_off + align_alen > nexto && |
| 2986 | nexto != NULLFILEOFF) { |
| 2987 | ASSERT(nexto > prevo); |
| 2988 | align_alen = nexto - align_off; |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | * If realtime, and the result isn't a multiple of the realtime |
| 2993 | * extent size we need to remove blocks until it is. |
| 2994 | */ |
| 2995 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2996 | /* |
| 2997 | * We're not covering the original request, or |
| 2998 | * we won't be able to once we fix the length. |
| 2999 | */ |
| 3000 | if (orig_off < align_off || |
| 3001 | orig_end > align_off + align_alen || |
| 3002 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3003 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3004 | /* |
| 3005 | * Try to fix it by moving the start up. |
| 3006 | */ |
| 3007 | if (align_off + temp <= orig_off) { |
| 3008 | align_alen -= temp; |
| 3009 | align_off += temp; |
| 3010 | } |
| 3011 | /* |
| 3012 | * Try to fix it by moving the end in. |
| 3013 | */ |
| 3014 | else if (align_off + align_alen - temp >= orig_end) |
| 3015 | align_alen -= temp; |
| 3016 | /* |
| 3017 | * Set the start to the minimum then trim the length. |
| 3018 | */ |
| 3019 | else { |
| 3020 | align_alen -= orig_off - align_off; |
| 3021 | align_off = orig_off; |
| 3022 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3023 | } |
| 3024 | /* |
| 3025 | * Result doesn't cover the request, fail it. |
| 3026 | */ |
| 3027 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3028 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3029 | } else { |
| 3030 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3031 | /* see MAXEXTLEN handling above */ |
| 3032 | ASSERT(orig_end <= align_off + align_alen || |
| 3033 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | #ifdef DEBUG |
| 3037 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3038 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3039 | if (prevp->br_startoff != NULLFILEOFF) |
| 3040 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3041 | #endif |
| 3042 | |
| 3043 | *lenp = align_alen; |
| 3044 | *offp = align_off; |
| 3045 | return 0; |
| 3046 | } |
| 3047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3049 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3050 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3051 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3052 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | { |
| 3054 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3056 | xfs_mount_t *mp; /* mount point structure */ |
| 3057 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3058 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | |
| 3060 | #define ISVALID(x,y) \ |
| 3061 | (rt ? \ |
| 3062 | (x) < mp->m_sb.sb_rblocks : \ |
| 3063 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3064 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3065 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3068 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3069 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3070 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3071 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | /* |
| 3073 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3074 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3076 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3077 | !isnullstartblock(ap->prev.br_startblock) && |
| 3078 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3079 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3080 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | /* |
| 3082 | * Adjust for the gap between prevp and us. |
| 3083 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3084 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3085 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3087 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3088 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | } |
| 3090 | /* |
| 3091 | * If not at eof, then compare the two neighbor blocks. |
| 3092 | * Figure out whether either one gives us a good starting point, |
| 3093 | * and pick the better one. |
| 3094 | */ |
| 3095 | else if (!ap->eof) { |
| 3096 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3097 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3098 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3099 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3100 | |
| 3101 | /* |
| 3102 | * If there's a previous (left) block, select a requested |
| 3103 | * start block based on it. |
| 3104 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3105 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3106 | !isnullstartblock(ap->prev.br_startblock) && |
| 3107 | (prevbno = ap->prev.br_startblock + |
| 3108 | ap->prev.br_blockcount) && |
| 3109 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | /* |
| 3111 | * Calculate gap to end of previous block. |
| 3112 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3113 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3114 | (ap->prev.br_startoff + |
| 3115 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | /* |
| 3117 | * Figure the startblock based on the previous block's |
| 3118 | * end and the gap size. |
| 3119 | * Heuristic! |
| 3120 | * If the gap is large relative to the piece we're |
| 3121 | * allocating, or using it gives us an invalid block |
| 3122 | * number, then just use the end of the previous block. |
| 3123 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3124 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3126 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | prevbno += adjust; |
| 3128 | else |
| 3129 | prevdiff += adjust; |
| 3130 | /* |
| 3131 | * If the firstblock forbids it, can't use it, |
| 3132 | * must use default. |
| 3133 | */ |
| 3134 | if (!rt && !nullfb && |
| 3135 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3136 | prevbno = NULLFSBLOCK; |
| 3137 | } |
| 3138 | /* |
| 3139 | * No previous block or can't follow it, just default. |
| 3140 | */ |
| 3141 | else |
| 3142 | prevbno = NULLFSBLOCK; |
| 3143 | /* |
| 3144 | * If there's a following (right) block, select a requested |
| 3145 | * start block based on it. |
| 3146 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3147 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | /* |
| 3149 | * Calculate gap to start of next block. |
| 3150 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3151 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | /* |
| 3153 | * Figure the startblock based on the next block's |
| 3154 | * start and the gap size. |
| 3155 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3156 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | /* |
| 3158 | * Heuristic! |
| 3159 | * If the gap is large relative to the piece we're |
| 3160 | * allocating, or using it gives us an invalid block |
| 3161 | * number, then just use the start of the next block |
| 3162 | * offset by our length. |
| 3163 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3164 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3166 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3167 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3168 | gotbno -= ap->length; |
| 3169 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | } else |
| 3171 | gotdiff += adjust; |
| 3172 | /* |
| 3173 | * If the firstblock forbids it, can't use it, |
| 3174 | * must use default. |
| 3175 | */ |
| 3176 | if (!rt && !nullfb && |
| 3177 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3178 | gotbno = NULLFSBLOCK; |
| 3179 | } |
| 3180 | /* |
| 3181 | * No next block, just default. |
| 3182 | */ |
| 3183 | else |
| 3184 | gotbno = NULLFSBLOCK; |
| 3185 | /* |
| 3186 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3187 | * 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] | 3188 | */ |
| 3189 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3190 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3192 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3194 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3196 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3197 | } |
| 3198 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3199 | static int |
| 3200 | xfs_bmap_longest_free_extent( |
| 3201 | struct xfs_trans *tp, |
| 3202 | xfs_agnumber_t ag, |
| 3203 | xfs_extlen_t *blen, |
| 3204 | int *notinit) |
| 3205 | { |
| 3206 | struct xfs_mount *mp = tp->t_mountp; |
| 3207 | struct xfs_perag *pag; |
| 3208 | xfs_extlen_t longest; |
| 3209 | int error = 0; |
| 3210 | |
| 3211 | pag = xfs_perag_get(mp, ag); |
| 3212 | if (!pag->pagf_init) { |
| 3213 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3214 | if (error) |
| 3215 | goto out; |
| 3216 | |
| 3217 | if (!pag->pagf_init) { |
| 3218 | *notinit = 1; |
| 3219 | goto out; |
| 3220 | } |
| 3221 | } |
| 3222 | |
Dave Chinner | 50adbcb | 2015-06-22 10:04:31 +1000 | [diff] [blame] | 3223 | longest = xfs_alloc_longest_free_extent(mp, pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3224 | xfs_alloc_min_freelist(mp, pag), |
| 3225 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3226 | if (*blen < longest) |
| 3227 | *blen = longest; |
| 3228 | |
| 3229 | out: |
| 3230 | xfs_perag_put(pag); |
| 3231 | return error; |
| 3232 | } |
| 3233 | |
| 3234 | static void |
| 3235 | xfs_bmap_select_minlen( |
| 3236 | struct xfs_bmalloca *ap, |
| 3237 | struct xfs_alloc_arg *args, |
| 3238 | xfs_extlen_t *blen, |
| 3239 | int notinit) |
| 3240 | { |
| 3241 | if (notinit || *blen < ap->minlen) { |
| 3242 | /* |
| 3243 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3244 | * there is space for this request. |
| 3245 | */ |
| 3246 | args->minlen = ap->minlen; |
| 3247 | } else if (*blen < args->maxlen) { |
| 3248 | /* |
| 3249 | * If the best seen length is less than the request length, |
| 3250 | * use the best as the minimum. |
| 3251 | */ |
| 3252 | args->minlen = *blen; |
| 3253 | } else { |
| 3254 | /* |
| 3255 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3256 | * as the minimum. |
| 3257 | */ |
| 3258 | args->minlen = args->maxlen; |
| 3259 | } |
| 3260 | } |
| 3261 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3262 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3263 | xfs_bmap_btalloc_nullfb( |
| 3264 | struct xfs_bmalloca *ap, |
| 3265 | struct xfs_alloc_arg *args, |
| 3266 | xfs_extlen_t *blen) |
| 3267 | { |
| 3268 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3269 | xfs_agnumber_t ag, startag; |
| 3270 | int notinit = 0; |
| 3271 | int error; |
| 3272 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3273 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3274 | args->total = ap->total; |
| 3275 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3276 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3277 | if (startag == NULLAGNUMBER) |
| 3278 | startag = ag = 0; |
| 3279 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3280 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3281 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3282 | ¬init); |
| 3283 | if (error) |
| 3284 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3285 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3286 | if (++ag == mp->m_sb.sb_agcount) |
| 3287 | ag = 0; |
| 3288 | if (ag == startag) |
| 3289 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3290 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3291 | |
| 3292 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3293 | return 0; |
| 3294 | } |
| 3295 | |
| 3296 | STATIC int |
| 3297 | xfs_bmap_btalloc_filestreams( |
| 3298 | struct xfs_bmalloca *ap, |
| 3299 | struct xfs_alloc_arg *args, |
| 3300 | xfs_extlen_t *blen) |
| 3301 | { |
| 3302 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3303 | xfs_agnumber_t ag; |
| 3304 | int notinit = 0; |
| 3305 | int error; |
| 3306 | |
| 3307 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3308 | args->total = ap->total; |
| 3309 | |
| 3310 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3311 | if (ag == NULLAGNUMBER) |
| 3312 | ag = 0; |
| 3313 | |
| 3314 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3315 | if (error) |
| 3316 | return error; |
| 3317 | |
| 3318 | if (*blen < args->maxlen) { |
| 3319 | error = xfs_filestream_new_ag(ap, &ag); |
| 3320 | if (error) |
| 3321 | return error; |
| 3322 | |
| 3323 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3324 | ¬init); |
| 3325 | if (error) |
| 3326 | return error; |
| 3327 | |
| 3328 | } |
| 3329 | |
| 3330 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3331 | |
| 3332 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3333 | * Set the failure fallback case to look in the selected AG as stream |
| 3334 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3335 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3336 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3337 | return 0; |
| 3338 | } |
| 3339 | |
| 3340 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3341 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3342 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3343 | { |
| 3344 | xfs_mount_t *mp; /* mount point structure */ |
| 3345 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3346 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3347 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3348 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3349 | xfs_alloc_arg_t args; |
| 3350 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3351 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3352 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3353 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3354 | int tryagain; |
| 3355 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3356 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3357 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3358 | ASSERT(ap->length); |
| 3359 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3360 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3361 | |
| 3362 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3363 | stripe_align = 0; |
| 3364 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3365 | stripe_align = mp->m_swidth; |
| 3366 | else if (mp->m_dalign) |
| 3367 | stripe_align = mp->m_dalign; |
| 3368 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3369 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3370 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3371 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3372 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3373 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3374 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3375 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3376 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3377 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3378 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3379 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3380 | |
| 3381 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3382 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3383 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3384 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3385 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3386 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3387 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3388 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3389 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3390 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3391 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3392 | } |
| 3393 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3394 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3395 | |
| 3396 | xfs_bmap_adjacent(ap); |
| 3397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3399 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | * it's in the right allocation group. |
| 3401 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3402 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | ; |
| 3404 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3405 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | * Normal allocation, done through xfs_alloc_vextent. |
| 3408 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3409 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3410 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3411 | args.tp = ap->tp; |
| 3412 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3413 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3414 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3415 | |
| 3416 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3417 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3418 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3419 | blen = 0; |
| 3420 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3421 | /* |
| 3422 | * Search for an allocation group with a single extent large |
| 3423 | * enough for the request. If one isn't found, then adjust |
| 3424 | * the minimum allocation size to the largest space found. |
| 3425 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3426 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3427 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3428 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3429 | else |
| 3430 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3431 | if (error) |
| 3432 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3433 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3434 | if (xfs_inode_is_filestream(ap->ip)) |
| 3435 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3436 | else |
| 3437 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3438 | args.total = args.minlen = ap->minlen; |
| 3439 | } else { |
| 3440 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3441 | args.total = ap->total; |
| 3442 | args.minlen = ap->minlen; |
| 3443 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3444 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3445 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3446 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3447 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3448 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3449 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3450 | args.prod = 1; |
| 3451 | args.mod = 0; |
| 3452 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3453 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3454 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3455 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3456 | } |
| 3457 | /* |
| 3458 | * If we are not low on available data blocks, and the |
| 3459 | * underlying logical volume manager is a stripe, and |
| 3460 | * the file offset is zero then try to allocate data |
| 3461 | * blocks on stripe unit boundary. |
| 3462 | * NOTE: ap->aeof is only set if the allocation length |
| 3463 | * is >= the stripe unit and the allocation offset is |
| 3464 | * at the end of file. |
| 3465 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3466 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3467 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3468 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3469 | atype = args.type; |
| 3470 | isaligned = 1; |
| 3471 | /* |
| 3472 | * Adjust for alignment |
| 3473 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3474 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3475 | args.minlen = blen - args.alignment; |
| 3476 | args.minalignslop = 0; |
| 3477 | } else { |
| 3478 | /* |
| 3479 | * First try an exact bno allocation. |
| 3480 | * If it fails then do a near or start bno |
| 3481 | * allocation with alignment turned on. |
| 3482 | */ |
| 3483 | atype = args.type; |
| 3484 | tryagain = 1; |
| 3485 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3486 | args.alignment = 1; |
| 3487 | /* |
| 3488 | * Compute the minlen+alignment for the |
| 3489 | * next case. Set slop so that the value |
| 3490 | * of minlen+alignment+slop doesn't go up |
| 3491 | * between the calls. |
| 3492 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3493 | if (blen > stripe_align && blen <= args.maxlen) |
| 3494 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3495 | else |
| 3496 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3497 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3498 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3499 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3500 | args.minlen - 1; |
| 3501 | else |
| 3502 | args.minalignslop = 0; |
| 3503 | } |
| 3504 | } else { |
| 3505 | args.alignment = 1; |
| 3506 | args.minalignslop = 0; |
| 3507 | } |
| 3508 | args.minleft = ap->minleft; |
| 3509 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3510 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3511 | args.datatype = ap->datatype; |
| 3512 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3513 | args.ip = ap->ip; |
| 3514 | |
| 3515 | error = xfs_alloc_vextent(&args); |
| 3516 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3517 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3518 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3519 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3520 | /* |
| 3521 | * Exact allocation failed. Now try with alignment |
| 3522 | * turned on. |
| 3523 | */ |
| 3524 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3525 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3526 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3527 | args.minlen = nextminlen; |
| 3528 | args.minalignslop = 0; |
| 3529 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | if ((error = xfs_alloc_vextent(&args))) |
| 3531 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3532 | } |
| 3533 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3534 | /* |
| 3535 | * allocation failed, so turn off alignment and |
| 3536 | * try again. |
| 3537 | */ |
| 3538 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3539 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3540 | args.alignment = 0; |
| 3541 | if ((error = xfs_alloc_vextent(&args))) |
| 3542 | return error; |
| 3543 | } |
| 3544 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3545 | args.minlen > ap->minlen) { |
| 3546 | args.minlen = ap->minlen; |
| 3547 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3548 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3549 | if ((error = xfs_alloc_vextent(&args))) |
| 3550 | return error; |
| 3551 | } |
| 3552 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3553 | args.fsbno = 0; |
| 3554 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3555 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3556 | if ((error = xfs_alloc_vextent(&args))) |
| 3557 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3558 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3559 | } |
| 3560 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3561 | /* |
| 3562 | * check the allocation happened at the same or higher AG than |
| 3563 | * the first block that was allocated. |
| 3564 | */ |
| 3565 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3566 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3567 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3568 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3569 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3570 | if (*ap->firstblock == NULLFSBLOCK) |
| 3571 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3572 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3573 | ap->length = args.len; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3574 | if (!(ap->flags & XFS_BMAPI_COWFORK)) |
| 3575 | ap->ip->i_d.di_nblocks += args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3576 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3577 | if (ap->wasdel) |
| 3578 | ap->ip->i_delayed_blks -= args.len; |
| 3579 | /* |
| 3580 | * Adjust the disk quota also. This was reserved |
| 3581 | * earlier. |
| 3582 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3583 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3584 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3585 | XFS_TRANS_DQ_BCOUNT, |
| 3586 | (long) args.len); |
| 3587 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3588 | ap->blkno = NULLFSBLOCK; |
| 3589 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | } |
| 3591 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | /* |
| 3595 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3596 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3597 | */ |
| 3598 | STATIC int |
| 3599 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3600 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3601 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3602 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3603 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3604 | return xfs_bmap_rtalloc(ap); |
| 3605 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | } |
| 3607 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3608 | /* Trim extent to fit a logical block range. */ |
| 3609 | void |
| 3610 | xfs_trim_extent( |
| 3611 | struct xfs_bmbt_irec *irec, |
| 3612 | xfs_fileoff_t bno, |
| 3613 | xfs_filblks_t len) |
| 3614 | { |
| 3615 | xfs_fileoff_t distance; |
| 3616 | xfs_fileoff_t end = bno + len; |
| 3617 | |
| 3618 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3619 | irec->br_startoff >= end) { |
| 3620 | irec->br_blockcount = 0; |
| 3621 | return; |
| 3622 | } |
| 3623 | |
| 3624 | if (irec->br_startoff < bno) { |
| 3625 | distance = bno - irec->br_startoff; |
| 3626 | if (isnullstartblock(irec->br_startblock)) |
| 3627 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3628 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3629 | irec->br_startblock != HOLESTARTBLOCK) |
| 3630 | irec->br_startblock += distance; |
| 3631 | irec->br_startoff += distance; |
| 3632 | irec->br_blockcount -= distance; |
| 3633 | } |
| 3634 | |
| 3635 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3636 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3637 | irec->br_blockcount -= distance; |
| 3638 | } |
| 3639 | } |
| 3640 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3641 | /* trim extent to within eof */ |
| 3642 | void |
| 3643 | xfs_trim_extent_eof( |
| 3644 | struct xfs_bmbt_irec *irec, |
| 3645 | struct xfs_inode *ip) |
| 3646 | |
| 3647 | { |
| 3648 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3649 | i_size_read(VFS_I(ip)))); |
| 3650 | } |
| 3651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3653 | * Trim the returned map to the required bounds |
| 3654 | */ |
| 3655 | STATIC void |
| 3656 | xfs_bmapi_trim_map( |
| 3657 | struct xfs_bmbt_irec *mval, |
| 3658 | struct xfs_bmbt_irec *got, |
| 3659 | xfs_fileoff_t *bno, |
| 3660 | xfs_filblks_t len, |
| 3661 | xfs_fileoff_t obno, |
| 3662 | xfs_fileoff_t end, |
| 3663 | int n, |
| 3664 | int flags) |
| 3665 | { |
| 3666 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3667 | got->br_startoff + got->br_blockcount <= obno) { |
| 3668 | *mval = *got; |
| 3669 | if (isnullstartblock(got->br_startblock)) |
| 3670 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3671 | return; |
| 3672 | } |
| 3673 | |
| 3674 | if (obno > *bno) |
| 3675 | *bno = obno; |
| 3676 | ASSERT((*bno >= obno) || (n == 0)); |
| 3677 | ASSERT(*bno < end); |
| 3678 | mval->br_startoff = *bno; |
| 3679 | if (isnullstartblock(got->br_startblock)) |
| 3680 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3681 | else |
| 3682 | mval->br_startblock = got->br_startblock + |
| 3683 | (*bno - got->br_startoff); |
| 3684 | /* |
| 3685 | * Return the minimum of what we got and what we asked for for |
| 3686 | * the length. We can use the len variable here because it is |
| 3687 | * modified below and we could have been there before coming |
| 3688 | * here if the first part of the allocation didn't overlap what |
| 3689 | * was asked for. |
| 3690 | */ |
| 3691 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3692 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3693 | mval->br_state = got->br_state; |
| 3694 | ASSERT(mval->br_blockcount <= len); |
| 3695 | return; |
| 3696 | } |
| 3697 | |
| 3698 | /* |
| 3699 | * Update and validate the extent map to return |
| 3700 | */ |
| 3701 | STATIC void |
| 3702 | xfs_bmapi_update_map( |
| 3703 | struct xfs_bmbt_irec **map, |
| 3704 | xfs_fileoff_t *bno, |
| 3705 | xfs_filblks_t *len, |
| 3706 | xfs_fileoff_t obno, |
| 3707 | xfs_fileoff_t end, |
| 3708 | int *n, |
| 3709 | int flags) |
| 3710 | { |
| 3711 | xfs_bmbt_irec_t *mval = *map; |
| 3712 | |
| 3713 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3714 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3715 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3716 | (mval->br_startoff < obno)); |
| 3717 | |
| 3718 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3719 | *len = end - *bno; |
| 3720 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3721 | /* update previous map with new information */ |
| 3722 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3723 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3724 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3725 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3726 | mval[-1].br_state = mval->br_state; |
| 3727 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3728 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3729 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3730 | mval->br_startblock == mval[-1].br_startblock + |
| 3731 | mval[-1].br_blockcount && |
| 3732 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3733 | mval[-1].br_state == mval->br_state)) { |
| 3734 | ASSERT(mval->br_startoff == |
| 3735 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3736 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3737 | } else if (*n > 0 && |
| 3738 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3739 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3740 | mval->br_startoff == |
| 3741 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3742 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3743 | mval[-1].br_state = mval->br_state; |
| 3744 | } else if (!((*n == 0) && |
| 3745 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3746 | obno))) { |
| 3747 | mval++; |
| 3748 | (*n)++; |
| 3749 | } |
| 3750 | *map = mval; |
| 3751 | } |
| 3752 | |
| 3753 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3754 | * Map file blocks to filesystem blocks without allocation. |
| 3755 | */ |
| 3756 | int |
| 3757 | xfs_bmapi_read( |
| 3758 | struct xfs_inode *ip, |
| 3759 | xfs_fileoff_t bno, |
| 3760 | xfs_filblks_t len, |
| 3761 | struct xfs_bmbt_irec *mval, |
| 3762 | int *nmap, |
| 3763 | int flags) |
| 3764 | { |
| 3765 | struct xfs_mount *mp = ip->i_mount; |
| 3766 | struct xfs_ifork *ifp; |
| 3767 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3768 | xfs_fileoff_t obno; |
| 3769 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3770 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3771 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3772 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3773 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3774 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3775 | |
| 3776 | ASSERT(*nmap >= 1); |
| 3777 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3778 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3779 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3780 | |
| 3781 | if (unlikely(XFS_TEST_ERROR( |
| 3782 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3783 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3784 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3785 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3786 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3790 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3791 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3792 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3793 | |
| 3794 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3795 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3796 | /* No CoW fork? Return a hole. */ |
| 3797 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3798 | mval->br_startoff = bno; |
| 3799 | mval->br_startblock = HOLESTARTBLOCK; |
| 3800 | mval->br_blockcount = len; |
| 3801 | mval->br_state = XFS_EXT_NORM; |
| 3802 | *nmap = 1; |
| 3803 | return 0; |
| 3804 | } |
| 3805 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3806 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3807 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3808 | if (error) |
| 3809 | return error; |
| 3810 | } |
| 3811 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3812 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3813 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3814 | end = bno + len; |
| 3815 | obno = bno; |
| 3816 | |
| 3817 | while (bno < end && n < *nmap) { |
| 3818 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3819 | if (eof) |
| 3820 | got.br_startoff = end; |
| 3821 | if (got.br_startoff > bno) { |
| 3822 | /* Reading in a hole. */ |
| 3823 | mval->br_startoff = bno; |
| 3824 | mval->br_startblock = HOLESTARTBLOCK; |
| 3825 | mval->br_blockcount = |
| 3826 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3827 | mval->br_state = XFS_EXT_NORM; |
| 3828 | bno += mval->br_blockcount; |
| 3829 | len -= mval->br_blockcount; |
| 3830 | mval++; |
| 3831 | n++; |
| 3832 | continue; |
| 3833 | } |
| 3834 | |
| 3835 | /* set up the extent map to return. */ |
| 3836 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3837 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3838 | |
| 3839 | /* If we're done, stop now. */ |
| 3840 | if (bno >= end || n >= *nmap) |
| 3841 | break; |
| 3842 | |
| 3843 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3844 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3845 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3846 | } |
| 3847 | *nmap = n; |
| 3848 | return 0; |
| 3849 | } |
| 3850 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3851 | /* |
| 3852 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3853 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3854 | * |
| 3855 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3856 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3857 | * to the extent record that was inserted to the inode fork. |
| 3858 | * |
| 3859 | * Note that the allocated extent may have been merged with contiguous extents |
| 3860 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3861 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3862 | * look up the updated record in the inode fork. |
| 3863 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3864 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3865 | xfs_bmapi_reserve_delalloc( |
| 3866 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3867 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3868 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3869 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3870 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3871 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3872 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3873 | int eof) |
| 3874 | { |
| 3875 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3876 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3877 | xfs_extlen_t alen; |
| 3878 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3879 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 3880 | xfs_extlen_t extsz; |
| 3881 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3882 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3883 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3884 | /* |
| 3885 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3886 | * tag the inode before we return. |
| 3887 | */ |
| 3888 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3889 | if (!eof) |
| 3890 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3891 | if (prealloc && alen >= len) |
| 3892 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3893 | |
| 3894 | /* Figure out the extent size, adjust alen */ |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3895 | if (whichfork == XFS_COW_FORK) |
| 3896 | extsz = xfs_get_cowextsz_hint(ip); |
| 3897 | else |
| 3898 | extsz = xfs_get_extsz_hint(ip); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3899 | if (extsz) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3900 | struct xfs_bmbt_irec prev; |
| 3901 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3902 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3903 | prev.br_startoff = NULLFILEOFF; |
| 3904 | |
| 3905 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3906 | 1, 0, &aoff, &alen); |
| 3907 | ASSERT(!error); |
| 3908 | } |
| 3909 | |
| 3910 | if (rt) |
| 3911 | extsz = alen / mp->m_sb.sb_rextsize; |
| 3912 | |
| 3913 | /* |
| 3914 | * Make a transaction-less quota reservation for delayed allocation |
| 3915 | * blocks. This number gets adjusted later. We return if we haven't |
| 3916 | * allocated blocks already inside this loop. |
| 3917 | */ |
| 3918 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 3919 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 3920 | if (error) |
| 3921 | return error; |
| 3922 | |
| 3923 | /* |
| 3924 | * Split changing sb for alen and indlen since they could be coming |
| 3925 | * from different places. |
| 3926 | */ |
| 3927 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3928 | ASSERT(indlen > 0); |
| 3929 | |
| 3930 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 3931 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3932 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3933 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | if (error) |
| 3937 | goto out_unreserve_quota; |
| 3938 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3939 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3940 | if (error) |
| 3941 | goto out_unreserve_blocks; |
| 3942 | |
| 3943 | |
| 3944 | ip->i_delayed_blks += alen; |
| 3945 | |
| 3946 | got->br_startoff = aoff; |
| 3947 | got->br_startblock = nullstartblock(indlen); |
| 3948 | got->br_blockcount = alen; |
| 3949 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3950 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3951 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3952 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3953 | /* |
| 3954 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 3955 | * preallocation can occur at the start or end of the extent, even when |
| 3956 | * prealloc == 0, so we must also check the aligned offset and length. |
| 3957 | */ |
| 3958 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 3959 | xfs_inode_set_eofblocks_tag(ip); |
| 3960 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 3961 | xfs_inode_set_cowblocks_tag(ip); |
| 3962 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3963 | return 0; |
| 3964 | |
| 3965 | out_unreserve_blocks: |
| 3966 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 3967 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3968 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3969 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3970 | out_unreserve_quota: |
| 3971 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 3972 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3973 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 3974 | return error; |
| 3975 | } |
| 3976 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 3977 | static int |
| 3978 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 3979 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3980 | { |
| 3981 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 3982 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3983 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 3984 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3985 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3986 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3987 | ASSERT(bma->length > 0); |
| 3988 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3989 | /* |
| 3990 | * For the wasdelay case, we could also just allocate the stuff asked |
| 3991 | * for in this bmap call but that wouldn't be as good. |
| 3992 | */ |
| 3993 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 3994 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 3995 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3996 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3997 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 3998 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 3999 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4000 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4001 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4005 | * Set the data type being allocated. For the data fork, the first data |
| 4006 | * in the file is treated differently to all other allocations. For the |
| 4007 | * attribute fork, we only need to ensure the allocated range is not on |
| 4008 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4009 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4010 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4011 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4012 | if (whichfork == XFS_DATA_FORK) { |
| 4013 | if (bma->offset == 0) |
| 4014 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4015 | else |
| 4016 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4017 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4018 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4019 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4022 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4023 | |
| 4024 | /* |
| 4025 | * Only want to do the alignment at the eof if it is userdata and |
| 4026 | * allocation length is larger than a stripe unit. |
| 4027 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4028 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4029 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4030 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4031 | if (error) |
| 4032 | return error; |
| 4033 | } |
| 4034 | |
| 4035 | error = xfs_bmap_alloc(bma); |
| 4036 | if (error) |
| 4037 | return error; |
| 4038 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4039 | if (bma->cur) |
| 4040 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4041 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4042 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4043 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4044 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4045 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4046 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4047 | } |
| 4048 | /* |
| 4049 | * Bump the number of extents we've allocated |
| 4050 | * in this call. |
| 4051 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4052 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4053 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4054 | if (bma->cur) |
| 4055 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4056 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4057 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4058 | bma->got.br_startoff = bma->offset; |
| 4059 | bma->got.br_startblock = bma->blkno; |
| 4060 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4061 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4062 | |
| 4063 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4064 | * In the data fork, a wasdelay extent has been initialized, so |
| 4065 | * shouldn't be flagged as unwritten. |
| 4066 | * |
| 4067 | * For the cow fork, however, we convert delalloc reservations |
| 4068 | * (extents allocated for speculative preallocation) to |
| 4069 | * allocated unwritten extents, and only convert the unwritten |
| 4070 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4071 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4072 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4073 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4074 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4075 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4076 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4077 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4078 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4079 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4080 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4081 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4082 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4083 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4084 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4085 | if (error) |
| 4086 | return error; |
| 4087 | |
| 4088 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4089 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4090 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4091 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4092 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4093 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4094 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4095 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4096 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4097 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4098 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4099 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4100 | return 0; |
| 4101 | } |
| 4102 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4103 | STATIC int |
| 4104 | xfs_bmapi_convert_unwritten( |
| 4105 | struct xfs_bmalloca *bma, |
| 4106 | struct xfs_bmbt_irec *mval, |
| 4107 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4108 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4109 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4110 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4111 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4112 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4113 | int error; |
| 4114 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4115 | /* check if we need to do unwritten->real conversion */ |
| 4116 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4117 | (flags & XFS_BMAPI_PREALLOC)) |
| 4118 | return 0; |
| 4119 | |
| 4120 | /* check if we need to do real->unwritten conversion */ |
| 4121 | if (mval->br_state == XFS_EXT_NORM && |
| 4122 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4123 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4124 | return 0; |
| 4125 | |
| 4126 | /* |
| 4127 | * Modify (by adding) the state flag, if writing. |
| 4128 | */ |
| 4129 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4130 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4131 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4132 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4133 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4134 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4135 | } |
| 4136 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4137 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4138 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4139 | /* |
| 4140 | * Before insertion into the bmbt, zero the range being converted |
| 4141 | * if required. |
| 4142 | */ |
| 4143 | if (flags & XFS_BMAPI_ZERO) { |
| 4144 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4145 | mval->br_blockcount); |
| 4146 | if (error) |
| 4147 | return error; |
| 4148 | } |
| 4149 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4150 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4151 | &bma->icur, &bma->cur, mval, bma->firstblock, |
| 4152 | bma->dfops, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4153 | /* |
| 4154 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4155 | * path because the conversion might not have done so (e.g., if the |
| 4156 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4157 | * in the transaction for the sake of fsync(), even if nothing has |
| 4158 | * changed, because fsync() will not force the log for this transaction |
| 4159 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4160 | * |
| 4161 | * Note: If we're only converting cow fork extents, there aren't |
| 4162 | * any on-disk updates to make, so we don't need to log anything. |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4163 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4164 | if (whichfork != XFS_COW_FORK) |
| 4165 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4166 | if (error) |
| 4167 | return error; |
| 4168 | |
| 4169 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4170 | * Update our extent pointer, given that |
| 4171 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4172 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4173 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4174 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4175 | |
| 4176 | /* |
| 4177 | * We may have combined previously unwritten space with written space, |
| 4178 | * so generate another request. |
| 4179 | */ |
| 4180 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4181 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4182 | return 0; |
| 4183 | } |
| 4184 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4185 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4186 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4187 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4188 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4189 | * locking problems. |
| 4190 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | * The returned value in "firstblock" from the first call in a transaction |
| 4192 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4193 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4194 | * the first call in "total"; if no allocation group has that many free |
| 4195 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4196 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4197 | int |
| 4198 | xfs_bmapi_write( |
| 4199 | struct xfs_trans *tp, /* transaction pointer */ |
| 4200 | struct xfs_inode *ip, /* incore inode */ |
| 4201 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4202 | xfs_filblks_t len, /* length to map in file */ |
| 4203 | int flags, /* XFS_BMAPI_... */ |
| 4204 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4205 | controls a.g. for allocs */ |
| 4206 | xfs_extlen_t total, /* total blocks needed */ |
| 4207 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4208 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4209 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4211 | struct xfs_mount *mp = ip->i_mount; |
| 4212 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4213 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4214 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4215 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4216 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4217 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4218 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4219 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4222 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4223 | int orig_flags; /* original flags arg value */ |
| 4224 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4225 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4226 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | |
| 4228 | orig_bno = bno; |
| 4229 | orig_len = len; |
| 4230 | orig_flags = flags; |
| 4231 | orig_mval = mval; |
| 4232 | orig_nmap = *nmap; |
| 4233 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4234 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4237 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4238 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4239 | ASSERT(tp != NULL || |
| 4240 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4241 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4242 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4243 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4244 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4245 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4246 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4247 | /* zeroing is for currently only for data extents, not metadata */ |
| 4248 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4249 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4250 | /* |
| 4251 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4252 | * but it makes no sense to do both at once. This would result in |
| 4253 | * zeroing the unwritten extent twice, but it still being an |
| 4254 | * unwritten extent.... |
| 4255 | */ |
| 4256 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4257 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | if (unlikely(XFS_TEST_ERROR( |
| 4260 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4261 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4262 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4263 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4264 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4268 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4271 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4272 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4273 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4274 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4276 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4278 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4279 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4280 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4284 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4285 | if (error) |
| 4286 | goto error0; |
| 4287 | } |
| 4288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | n = 0; |
| 4290 | end = bno + len; |
| 4291 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4292 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4293 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4294 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4295 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4296 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4297 | bma.tp = tp; |
| 4298 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4299 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4300 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4301 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4302 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4305 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4306 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4307 | /* in hole or beyoned EOF? */ |
| 4308 | if (eof || bma.got.br_startoff > bno) { |
| 4309 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4310 | /* |
| 4311 | * For the COW fork we can reasonably get a |
| 4312 | * request for converting an extent that races |
| 4313 | * with other threads already having converted |
| 4314 | * part of it, as there converting COW to |
| 4315 | * regular blocks is not protected using the |
| 4316 | * IOLOCK. |
| 4317 | */ |
| 4318 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4319 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4320 | error = -EIO; |
| 4321 | goto error0; |
| 4322 | } |
| 4323 | |
| 4324 | if (eof || bno >= end) |
| 4325 | break; |
| 4326 | } else { |
| 4327 | need_alloc = true; |
| 4328 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4329 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4330 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4331 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4332 | |
| 4333 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | * First, deal with the hole before the allocated space |
| 4335 | * that we found, if any. |
| 4336 | */ |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 4337 | if ((need_alloc || wasdelay) && |
| 4338 | !(flags & XFS_BMAPI_CONVERT_ONLY)) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4339 | bma.eof = eof; |
| 4340 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4341 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4342 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4343 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4344 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4345 | /* |
| 4346 | * There's a 32/64 bit type mismatch between the |
| 4347 | * allocation length request (which can be 64 bits in |
| 4348 | * length) and the bma length request, which is |
| 4349 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4350 | * check for 32-bit overflows and handle them here. |
| 4351 | */ |
| 4352 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4353 | bma.length = MAXEXTLEN; |
| 4354 | else |
| 4355 | bma.length = len; |
| 4356 | |
| 4357 | ASSERT(len > 0); |
| 4358 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4359 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | if (error) |
| 4361 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4362 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4363 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4364 | |
| 4365 | /* |
| 4366 | * If this is a CoW allocation, record the data in |
| 4367 | * the refcount btree for orphan recovery. |
| 4368 | */ |
| 4369 | if (whichfork == XFS_COW_FORK) { |
| 4370 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4371 | bma.blkno, bma.length); |
| 4372 | if (error) |
| 4373 | goto error0; |
| 4374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4376 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4377 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4378 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4379 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4381 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4382 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4383 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4384 | continue; |
| 4385 | if (error) |
| 4386 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4388 | /* update the extent map to return */ |
| 4389 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | /* |
| 4392 | * If we're done, stop now. Stop when we've allocated |
| 4393 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4394 | * the transaction may get too big. |
| 4395 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4396 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4398 | |
| 4399 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4400 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4401 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4402 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | /* |
| 4407 | * Transform from btree to extents, give it cur. |
| 4408 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4409 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4410 | int tmp_logflags = 0; |
| 4411 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4412 | ASSERT(bma.cur); |
| 4413 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4415 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | if (error) |
| 4417 | goto error0; |
| 4418 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4421 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4422 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | error0: |
| 4425 | /* |
| 4426 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4427 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4429 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4431 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4432 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4434 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | /* |
| 4436 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4437 | * detecting a case where the data is changed, there's an error, |
| 4438 | * and it's not logged so we don't shutdown when we should. |
| 4439 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4440 | if (bma.logflags) |
| 4441 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4442 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4443 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | if (!error) { |
| 4445 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4446 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4448 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4449 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4451 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4453 | } |
| 4454 | if (!error) |
| 4455 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4456 | orig_nmap, *nmap); |
| 4457 | return error; |
| 4458 | } |
| 4459 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4460 | static int |
| 4461 | xfs_bmapi_remap( |
| 4462 | struct xfs_trans *tp, |
| 4463 | struct xfs_inode *ip, |
| 4464 | xfs_fileoff_t bno, |
| 4465 | xfs_filblks_t len, |
| 4466 | xfs_fsblock_t startblock, |
| 4467 | struct xfs_defer_ops *dfops) |
| 4468 | { |
| 4469 | struct xfs_mount *mp = ip->i_mount; |
| 4470 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4471 | struct xfs_btree_cur *cur = NULL; |
| 4472 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4473 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4474 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4475 | int logflags = 0, error; |
| 4476 | |
| 4477 | ASSERT(len > 0); |
| 4478 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4479 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4480 | |
| 4481 | if (unlikely(XFS_TEST_ERROR( |
| 4482 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4483 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4484 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4485 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4486 | return -EFSCORRUPTED; |
| 4487 | } |
| 4488 | |
| 4489 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4490 | return -EIO; |
| 4491 | |
| 4492 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4493 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4494 | if (error) |
| 4495 | return error; |
| 4496 | } |
| 4497 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4498 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4499 | /* make sure we only reflink into a hole. */ |
| 4500 | ASSERT(got.br_startoff > bno); |
| 4501 | ASSERT(got.br_startoff - bno >= len); |
| 4502 | } |
| 4503 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4504 | ip->i_d.di_nblocks += len; |
| 4505 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4506 | |
| 4507 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4508 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4509 | cur->bc_private.b.firstblock = firstblock; |
| 4510 | cur->bc_private.b.dfops = dfops; |
| 4511 | cur->bc_private.b.flags = 0; |
| 4512 | } |
| 4513 | |
| 4514 | got.br_startoff = bno; |
| 4515 | got.br_startblock = startblock; |
| 4516 | got.br_blockcount = len; |
| 4517 | got.br_state = XFS_EXT_NORM; |
| 4518 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4519 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur, |
| 4520 | &cur, &got, &firstblock, dfops, &logflags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4521 | if (error) |
| 4522 | goto error0; |
| 4523 | |
| 4524 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4525 | int tmp_logflags = 0; |
| 4526 | |
| 4527 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4528 | &tmp_logflags, XFS_DATA_FORK); |
| 4529 | logflags |= tmp_logflags; |
| 4530 | } |
| 4531 | |
| 4532 | error0: |
| 4533 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4534 | logflags &= ~XFS_ILOG_DEXT; |
| 4535 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4536 | logflags &= ~XFS_ILOG_DBROOT; |
| 4537 | |
| 4538 | if (logflags) |
| 4539 | xfs_trans_log_inode(tp, ip, logflags); |
| 4540 | if (cur) { |
| 4541 | xfs_btree_del_cursor(cur, |
| 4542 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4543 | } |
| 4544 | return error; |
| 4545 | } |
| 4546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4548 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4549 | * indlen reservation must be shared across the two new extents that are left |
| 4550 | * behind. |
| 4551 | * |
| 4552 | * Given the original reservation and the worst case indlen for the two new |
| 4553 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4554 | * reservation fairly across the two new extents. If necessary, steal available |
| 4555 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4556 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4557 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4558 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4559 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4560 | xfs_bmap_split_indlen( |
| 4561 | xfs_filblks_t ores, /* original res. */ |
| 4562 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4563 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4564 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4565 | { |
| 4566 | xfs_filblks_t len1 = *indlen1; |
| 4567 | xfs_filblks_t len2 = *indlen2; |
| 4568 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4569 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4570 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4571 | |
| 4572 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4573 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4574 | * indlen for both new extents. |
| 4575 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4576 | if (ores < nres && avail) |
| 4577 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4578 | ores += stolen; |
| 4579 | |
| 4580 | /* nothing else to do if we've satisfied the new reservation */ |
| 4581 | if (ores >= nres) |
| 4582 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4583 | |
| 4584 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4585 | * We can't meet the total required reservation for the two extents. |
| 4586 | * Calculate the percent of the overall shortage between both extents |
| 4587 | * and apply this percentage to each of the requested indlen values. |
| 4588 | * This distributes the shortage fairly and reduces the chances that one |
| 4589 | * of the two extents is left with nothing when extents are repeatedly |
| 4590 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4591 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4592 | resfactor = (ores * 100); |
| 4593 | do_div(resfactor, nres); |
| 4594 | len1 *= resfactor; |
| 4595 | do_div(len1, 100); |
| 4596 | len2 *= resfactor; |
| 4597 | do_div(len2, 100); |
| 4598 | ASSERT(len1 + len2 <= ores); |
| 4599 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4600 | |
| 4601 | /* |
| 4602 | * Hand out the remainder to each extent. If one of the two reservations |
| 4603 | * is zero, we want to make sure that one gets a block first. The loop |
| 4604 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4605 | * is zero. |
| 4606 | */ |
| 4607 | ores -= (len1 + len2); |
| 4608 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4609 | if (ores && !len2 && *indlen2) { |
| 4610 | len2++; |
| 4611 | ores--; |
| 4612 | } |
| 4613 | while (ores) { |
| 4614 | if (len1 < *indlen1) { |
| 4615 | len1++; |
| 4616 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4617 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4618 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4619 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4620 | if (len2 < *indlen2) { |
| 4621 | len2++; |
| 4622 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | *indlen1 = len1; |
| 4627 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4628 | |
| 4629 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4630 | } |
| 4631 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4632 | int |
| 4633 | xfs_bmap_del_extent_delay( |
| 4634 | struct xfs_inode *ip, |
| 4635 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4636 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4637 | struct xfs_bmbt_irec *got, |
| 4638 | struct xfs_bmbt_irec *del) |
| 4639 | { |
| 4640 | struct xfs_mount *mp = ip->i_mount; |
| 4641 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4642 | struct xfs_bmbt_irec new; |
| 4643 | int64_t da_old, da_new, da_diff = 0; |
| 4644 | xfs_fileoff_t del_endoff, got_endoff; |
| 4645 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4646 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4647 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4648 | bool isrt; |
| 4649 | |
| 4650 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4651 | |
| 4652 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4653 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4654 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4655 | da_old = startblockval(got->br_startblock); |
| 4656 | da_new = 0; |
| 4657 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4658 | ASSERT(del->br_blockcount > 0); |
| 4659 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4660 | ASSERT(got_endoff >= del_endoff); |
| 4661 | |
| 4662 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4663 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4664 | |
| 4665 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4666 | xfs_mod_frextents(mp, rtexts); |
| 4667 | } |
| 4668 | |
| 4669 | /* |
| 4670 | * Update the inode delalloc counter now and wait to update the |
| 4671 | * sb counters as we might have to borrow some blocks for the |
| 4672 | * indirect block accounting. |
| 4673 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4674 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4675 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4676 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4677 | if (error) |
| 4678 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4679 | ip->i_delayed_blks -= del->br_blockcount; |
| 4680 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4681 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4682 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4683 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4684 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4685 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4686 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4687 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4688 | /* |
| 4689 | * Matches the whole extent. Delete the entry. |
| 4690 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4691 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4692 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4693 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4694 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4695 | /* |
| 4696 | * Deleting the first part of the extent. |
| 4697 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4698 | got->br_startoff = del_endoff; |
| 4699 | got->br_blockcount -= del->br_blockcount; |
| 4700 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4701 | got->br_blockcount), da_old); |
| 4702 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4703 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4704 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4705 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4706 | /* |
| 4707 | * Deleting the last part of the extent. |
| 4708 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4709 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4710 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4711 | got->br_blockcount), da_old); |
| 4712 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4713 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4714 | break; |
| 4715 | case 0: |
| 4716 | /* |
| 4717 | * Deleting the middle of the extent. |
| 4718 | * |
| 4719 | * Distribute the original indlen reservation across the two new |
| 4720 | * extents. Steal blocks from the deleted extent if necessary. |
| 4721 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4722 | * Warn if either of the new indlen reservations is zero as this |
| 4723 | * can lead to delalloc problems. |
| 4724 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4725 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4726 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4727 | |
| 4728 | new.br_blockcount = got_endoff - del_endoff; |
| 4729 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4730 | |
| 4731 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4732 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4733 | del->br_blockcount); |
| 4734 | |
| 4735 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4736 | |
| 4737 | new.br_startoff = del_endoff; |
| 4738 | new.br_state = got->br_state; |
| 4739 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4740 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4741 | xfs_iext_update_extent(ip, state, icur, got); |
| 4742 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4743 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4744 | |
| 4745 | da_new = got_indlen + new_indlen - stolen; |
| 4746 | del->br_blockcount -= stolen; |
| 4747 | break; |
| 4748 | } |
| 4749 | |
| 4750 | ASSERT(da_old >= da_new); |
| 4751 | da_diff = da_old - da_new; |
| 4752 | if (!isrt) |
| 4753 | da_diff += del->br_blockcount; |
| 4754 | if (da_diff) |
| 4755 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4756 | return error; |
| 4757 | } |
| 4758 | |
| 4759 | void |
| 4760 | xfs_bmap_del_extent_cow( |
| 4761 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4762 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4763 | struct xfs_bmbt_irec *got, |
| 4764 | struct xfs_bmbt_irec *del) |
| 4765 | { |
| 4766 | struct xfs_mount *mp = ip->i_mount; |
| 4767 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4768 | struct xfs_bmbt_irec new; |
| 4769 | xfs_fileoff_t del_endoff, got_endoff; |
| 4770 | int state = BMAP_COWFORK; |
| 4771 | |
| 4772 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4773 | |
| 4774 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4775 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4776 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4777 | ASSERT(del->br_blockcount > 0); |
| 4778 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4779 | ASSERT(got_endoff >= del_endoff); |
| 4780 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4781 | |
| 4782 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4783 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4784 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4785 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4786 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4787 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4788 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4789 | /* |
| 4790 | * Matches the whole extent. Delete the entry. |
| 4791 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4792 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4793 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4794 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4795 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4796 | /* |
| 4797 | * Deleting the first part of the extent. |
| 4798 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4799 | got->br_startoff = del_endoff; |
| 4800 | got->br_blockcount -= del->br_blockcount; |
| 4801 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4802 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4803 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4804 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4805 | /* |
| 4806 | * Deleting the last part of the extent. |
| 4807 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4808 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4809 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4810 | break; |
| 4811 | case 0: |
| 4812 | /* |
| 4813 | * Deleting the middle of the extent. |
| 4814 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4815 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4816 | |
| 4817 | new.br_startoff = del_endoff; |
| 4818 | new.br_blockcount = got_endoff - del_endoff; |
| 4819 | new.br_state = got->br_state; |
| 4820 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4821 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4822 | xfs_iext_update_extent(ip, state, icur, got); |
| 4823 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4824 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4825 | break; |
| 4826 | } |
| 4827 | } |
| 4828 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4829 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4830 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4831 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4832 | */ |
| 4833 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4834 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4835 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4836 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4837 | struct xfs_iext_cursor *icur, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4838 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4839 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4840 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4841 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4842 | int whichfork, /* data or attr fork */ |
| 4843 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4844 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4845 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4846 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4847 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4848 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4849 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4850 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4851 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4852 | int i; /* temp state */ |
| 4853 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4854 | xfs_mount_t *mp; /* mount structure */ |
| 4855 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4856 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4857 | /* REFERENCED */ |
| 4858 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4859 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4860 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4861 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4862 | mp = ip->i_mount; |
| 4863 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4864 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4865 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4866 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4867 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4868 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4869 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4870 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4871 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4872 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4873 | qfield = 0; |
| 4874 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4875 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4876 | /* |
| 4877 | * If it's the case where the directory code is running with no block |
| 4878 | * reservation, and the deleted block is in the middle of its extent, |
| 4879 | * and the resulting insert of an extent would cause transformation to |
| 4880 | * btree format, then reject it. The calling code will then swap blocks |
| 4881 | * around instead. We have to do this now, rather than waiting for the |
| 4882 | * conversion to btree format, since the transaction will be dirty then. |
| 4883 | */ |
| 4884 | if (tp->t_blk_res == 0 && |
| 4885 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4886 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4887 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4888 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4889 | return -ENOSPC; |
| 4890 | |
| 4891 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4892 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4893 | xfs_fsblock_t bno; |
| 4894 | xfs_filblks_t len; |
| 4895 | |
| 4896 | ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0); |
| 4897 | ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0); |
| 4898 | bno = del->br_startblock; |
| 4899 | len = del->br_blockcount; |
| 4900 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4901 | do_div(len, mp->m_sb.sb_rextsize); |
| 4902 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4903 | if (error) |
| 4904 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4905 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4906 | nblks = len * mp->m_sb.sb_rextsize; |
| 4907 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4908 | } else { |
| 4909 | do_fx = 1; |
| 4910 | nblks = del->br_blockcount; |
| 4911 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4912 | } |
| 4913 | |
| 4914 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4915 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4916 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4917 | if (error) |
| 4918 | goto done; |
| 4919 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4920 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4921 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4922 | if (got.br_startoff == del->br_startoff) |
| 4923 | state |= BMAP_LEFT_FILLING; |
| 4924 | if (got_endoff == del_endoff) |
| 4925 | state |= BMAP_RIGHT_FILLING; |
| 4926 | |
| 4927 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4928 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4929 | /* |
| 4930 | * Matches the whole extent. Delete the entry. |
| 4931 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4932 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4933 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4934 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4935 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4936 | flags |= XFS_ILOG_CORE; |
| 4937 | if (!cur) { |
| 4938 | flags |= xfs_ilog_fext(whichfork); |
| 4939 | break; |
| 4940 | } |
| 4941 | if ((error = xfs_btree_delete(cur, &i))) |
| 4942 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4943 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4944 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4945 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4946 | /* |
| 4947 | * Deleting the first part of the extent. |
| 4948 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4949 | got.br_startoff = del_endoff; |
| 4950 | got.br_startblock = del_endblock; |
| 4951 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4952 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4953 | if (!cur) { |
| 4954 | flags |= xfs_ilog_fext(whichfork); |
| 4955 | break; |
| 4956 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4957 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4958 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4959 | goto done; |
| 4960 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4961 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4962 | /* |
| 4963 | * Deleting the last part of the extent. |
| 4964 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4965 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4966 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4967 | if (!cur) { |
| 4968 | flags |= xfs_ilog_fext(whichfork); |
| 4969 | break; |
| 4970 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4971 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4972 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4973 | goto done; |
| 4974 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4975 | case 0: |
| 4976 | /* |
| 4977 | * Deleting the middle of the extent. |
| 4978 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4979 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 4980 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4981 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4982 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4983 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4984 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4985 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4986 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4987 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4988 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4989 | flags |= XFS_ILOG_CORE; |
| 4990 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4991 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4992 | if (error) |
| 4993 | goto done; |
| 4994 | error = xfs_btree_increment(cur, 0, &i); |
| 4995 | if (error) |
| 4996 | goto done; |
| 4997 | cur->bc_rec.b = new; |
| 4998 | error = xfs_btree_insert(cur, &i); |
| 4999 | if (error && error != -ENOSPC) |
| 5000 | goto done; |
| 5001 | /* |
| 5002 | * If get no-space back from btree insert, it tried a |
| 5003 | * split, and we have a zero block reservation. Fix up |
| 5004 | * our state and return the error. |
| 5005 | */ |
| 5006 | if (error == -ENOSPC) { |
| 5007 | /* |
| 5008 | * Reset the cursor, don't trust it after any |
| 5009 | * insert operation. |
| 5010 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5011 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5012 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5013 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5014 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5015 | /* |
| 5016 | * Update the btree record back |
| 5017 | * to the original value. |
| 5018 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5019 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5020 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5021 | goto done; |
| 5022 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5023 | * Reset the extent record back |
| 5024 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5025 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5026 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5027 | flags = 0; |
| 5028 | error = -ENOSPC; |
| 5029 | goto done; |
| 5030 | } |
| 5031 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5032 | } else |
| 5033 | flags |= xfs_ilog_fext(whichfork); |
| 5034 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5035 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5036 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5037 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5038 | break; |
| 5039 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5040 | |
| 5041 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5042 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5043 | if (error) |
| 5044 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5045 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5046 | /* |
| 5047 | * If we need to, add to list of extents to delete. |
| 5048 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5049 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5050 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5051 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5052 | if (error) |
| 5053 | goto done; |
| 5054 | } else |
| 5055 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5056 | del->br_blockcount, NULL); |
| 5057 | } |
| 5058 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5059 | /* |
| 5060 | * Adjust inode # blocks in the file. |
| 5061 | */ |
| 5062 | if (nblks) |
| 5063 | ip->i_d.di_nblocks -= nblks; |
| 5064 | /* |
| 5065 | * Adjust quota data. |
| 5066 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5067 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5068 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5069 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5070 | done: |
| 5071 | *logflagsp = flags; |
| 5072 | return error; |
| 5073 | } |
| 5074 | |
| 5075 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | * Unmap (remove) blocks from a file. |
| 5077 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5078 | * that value. If not all extents in the block range can be removed then |
| 5079 | * *done is set. |
| 5080 | */ |
| 5081 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5082 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | xfs_trans_t *tp, /* transaction pointer */ |
| 5084 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5085 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5086 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | int flags, /* misc flags */ |
| 5088 | xfs_extnum_t nexts, /* number of extents max */ |
| 5089 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5090 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5091 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | { |
| 5093 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5094 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | int error; /* error return value */ |
| 5096 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5097 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5099 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | int logflags; /* transaction logging flags */ |
| 5101 | xfs_extlen_t mod; /* rt extent offset */ |
| 5102 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | int tmp_logflags; /* partial logging flags */ |
| 5104 | int wasdel; /* was a delayed alloc extent */ |
| 5105 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5107 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5108 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5109 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5110 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5111 | struct xfs_iext_cursor icur; |
| 5112 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5114 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5115 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5116 | whichfork = xfs_bmapi_whichfork(flags); |
| 5117 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5119 | if (unlikely( |
| 5120 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5121 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5122 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5123 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5124 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | } |
| 5126 | mp = ip->i_mount; |
| 5127 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5128 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5129 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5130 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | ASSERT(len > 0); |
| 5132 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5133 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5134 | /* |
| 5135 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5136 | * blowing out the transaction with a mix of EFIs and reflink |
| 5137 | * adjustments. |
| 5138 | */ |
| 5139 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
| 5140 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5141 | else |
| 5142 | max_len = len; |
| 5143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5145 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5146 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5147 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5148 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | return 0; |
| 5150 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5151 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5152 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5153 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5154 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5155 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5156 | *rlen = 0; |
| 5157 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5159 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | logflags = 0; |
| 5162 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5163 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5164 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5166 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | cur->bc_private.b.flags = 0; |
| 5168 | } else |
| 5169 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5170 | |
| 5171 | if (isrt) { |
| 5172 | /* |
| 5173 | * Synchronize by locking the bitmap inode. |
| 5174 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5175 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5176 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5177 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5178 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5182 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5183 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5185 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | * Just back up to the previous extent, if so. |
| 5187 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5188 | if (got.br_startoff > end && |
| 5189 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5190 | done = true; |
| 5191 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | } |
| 5193 | /* |
| 5194 | * Is the last block of this extent before the range |
| 5195 | * we're supposed to delete? If so, we're done. |
| 5196 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5197 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5199 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | break; |
| 5201 | /* |
| 5202 | * Then deal with the (possibly delayed) allocated space |
| 5203 | * we found. |
| 5204 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5206 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5207 | |
| 5208 | /* |
| 5209 | * Make sure we don't touch multiple AGF headers out of order |
| 5210 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5211 | */ |
| 5212 | if (!wasdel) { |
| 5213 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5214 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5215 | break; |
| 5216 | prev_agno = agno; |
| 5217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | if (got.br_startoff < start) { |
| 5219 | del.br_startoff = start; |
| 5220 | del.br_blockcount -= start - got.br_startoff; |
| 5221 | if (!wasdel) |
| 5222 | del.br_startblock += start - got.br_startoff; |
| 5223 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5224 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5225 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5226 | |
| 5227 | /* How much can we safely unmap? */ |
| 5228 | if (max_len < del.br_blockcount) { |
| 5229 | del.br_startoff += del.br_blockcount - max_len; |
| 5230 | if (!wasdel) |
| 5231 | del.br_startblock += del.br_blockcount - max_len; |
| 5232 | del.br_blockcount = max_len; |
| 5233 | } |
| 5234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | sum = del.br_startblock + del.br_blockcount; |
| 5236 | if (isrt && |
| 5237 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5238 | /* |
| 5239 | * Realtime extent not lined up at the end. |
| 5240 | * The extent could have been split into written |
| 5241 | * and unwritten pieces, or we could just be |
| 5242 | * unmapping part of it. But we can't really |
| 5243 | * get rid of part of a realtime extent. |
| 5244 | */ |
| 5245 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5246 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | /* |
| 5248 | * This piece is unwritten, or we're not |
| 5249 | * using unwritten extents. Skip over it. |
| 5250 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5251 | ASSERT(end >= mod); |
| 5252 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5254 | if (end < got.br_startoff && |
| 5255 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5256 | done = true; |
| 5257 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | } |
| 5259 | continue; |
| 5260 | } |
| 5261 | /* |
| 5262 | * It's written, turn it unwritten. |
| 5263 | * This is better than zeroing it. |
| 5264 | */ |
| 5265 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5266 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | /* |
| 5268 | * If this spans a realtime extent boundary, |
| 5269 | * chop it back to the start of the one we end at. |
| 5270 | */ |
| 5271 | if (del.br_blockcount > mod) { |
| 5272 | del.br_startoff += del.br_blockcount - mod; |
| 5273 | del.br_startblock += del.br_blockcount - mod; |
| 5274 | del.br_blockcount = mod; |
| 5275 | } |
| 5276 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5277 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5278 | whichfork, &icur, &cur, &del, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5279 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | if (error) |
| 5281 | goto error0; |
| 5282 | goto nodelete; |
| 5283 | } |
| 5284 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5285 | /* |
| 5286 | * Realtime extent is lined up at the end but not |
| 5287 | * at the front. We'll get rid of full extents if |
| 5288 | * we can. |
| 5289 | */ |
| 5290 | mod = mp->m_sb.sb_rextsize - mod; |
| 5291 | if (del.br_blockcount > mod) { |
| 5292 | del.br_blockcount -= mod; |
| 5293 | del.br_startoff += mod; |
| 5294 | del.br_startblock += mod; |
| 5295 | } else if ((del.br_startoff == start && |
| 5296 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5297 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5298 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | /* |
| 5300 | * Can't make it unwritten. There isn't |
| 5301 | * a full extent here so just skip it. |
| 5302 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5303 | ASSERT(end >= del.br_blockcount); |
| 5304 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5305 | if (got.br_startoff > end && |
| 5306 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5307 | done = true; |
| 5308 | break; |
| 5309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | continue; |
| 5311 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5312 | struct xfs_bmbt_irec prev; |
| 5313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | /* |
| 5315 | * This one is already unwritten. |
| 5316 | * It must have a written left neighbor. |
| 5317 | * Unwrite the killed part of that one and |
| 5318 | * try again. |
| 5319 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5320 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5321 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5323 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | ASSERT(del.br_startblock == |
| 5325 | prev.br_startblock + prev.br_blockcount); |
| 5326 | if (prev.br_startoff < start) { |
| 5327 | mod = start - prev.br_startoff; |
| 5328 | prev.br_blockcount -= mod; |
| 5329 | prev.br_startblock += mod; |
| 5330 | prev.br_startoff = start; |
| 5331 | } |
| 5332 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5333 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5334 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5335 | &prev, firstblock, dfops, |
| 5336 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | if (error) |
| 5338 | goto error0; |
| 5339 | goto nodelete; |
| 5340 | } else { |
| 5341 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5342 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5343 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5344 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5345 | &del, firstblock, dfops, |
| 5346 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | if (error) |
| 5348 | goto error0; |
| 5349 | goto nodelete; |
| 5350 | } |
| 5351 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5352 | |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5353 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5354 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5355 | &got, &del); |
| 5356 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5357 | error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5358 | cur, &del, &tmp_logflags, whichfork, |
| 5359 | flags); |
| 5360 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5361 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | if (error) |
| 5364 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5365 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5366 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5367 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | /* |
| 5370 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5372 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5373 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5374 | (got.br_startoff > end && |
| 5375 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5376 | done = true; |
| 5377 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | extno++; |
| 5380 | } |
| 5381 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5382 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5383 | *rlen = 0; |
| 5384 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5385 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | /* |
| 5388 | * Convert to a btree if necessary. |
| 5389 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5390 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5392 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | &cur, 0, &tmp_logflags, whichfork); |
| 5394 | logflags |= tmp_logflags; |
| 5395 | if (error) |
| 5396 | goto error0; |
| 5397 | } |
| 5398 | /* |
| 5399 | * transform from btree to extents, give it cur |
| 5400 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5401 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | ASSERT(cur != NULL); |
| 5403 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5404 | whichfork); |
| 5405 | logflags |= tmp_logflags; |
| 5406 | if (error) |
| 5407 | goto error0; |
| 5408 | } |
| 5409 | /* |
| 5410 | * transform from extents to local? |
| 5411 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | error = 0; |
| 5413 | error0: |
| 5414 | /* |
| 5415 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5416 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5418 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5420 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5421 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5423 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | /* |
| 5425 | * Log inode even in the error case, if the transaction |
| 5426 | * is dirty we'll need to shut down the filesystem. |
| 5427 | */ |
| 5428 | if (logflags) |
| 5429 | xfs_trans_log_inode(tp, ip, logflags); |
| 5430 | if (cur) { |
| 5431 | if (!error) { |
| 5432 | *firstblock = cur->bc_private.b.firstblock; |
| 5433 | cur->bc_private.b.allocated = 0; |
| 5434 | } |
| 5435 | xfs_btree_del_cursor(cur, |
| 5436 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5437 | } |
| 5438 | return error; |
| 5439 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5440 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5441 | /* Unmap a range of a file. */ |
| 5442 | int |
| 5443 | xfs_bunmapi( |
| 5444 | xfs_trans_t *tp, |
| 5445 | struct xfs_inode *ip, |
| 5446 | xfs_fileoff_t bno, |
| 5447 | xfs_filblks_t len, |
| 5448 | int flags, |
| 5449 | xfs_extnum_t nexts, |
| 5450 | xfs_fsblock_t *firstblock, |
| 5451 | struct xfs_defer_ops *dfops, |
| 5452 | int *done) |
| 5453 | { |
| 5454 | int error; |
| 5455 | |
| 5456 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5457 | dfops); |
| 5458 | *done = (len == 0); |
| 5459 | return error; |
| 5460 | } |
| 5461 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5462 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5463 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5464 | * extent that precedes the target hole of the shift. |
| 5465 | */ |
| 5466 | STATIC bool |
| 5467 | xfs_bmse_can_merge( |
| 5468 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5469 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5470 | xfs_fileoff_t shift) /* shift fsb */ |
| 5471 | { |
| 5472 | xfs_fileoff_t startoff; |
| 5473 | |
| 5474 | startoff = got->br_startoff - shift; |
| 5475 | |
| 5476 | /* |
| 5477 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5478 | * the preceding extent. |
| 5479 | */ |
| 5480 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5481 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5482 | (left->br_state != got->br_state) || |
| 5483 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5484 | return false; |
| 5485 | |
| 5486 | return true; |
| 5487 | } |
| 5488 | |
| 5489 | /* |
| 5490 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5491 | * hole in the file. If an extent shift would result in the extent being fully |
| 5492 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5493 | * the preceding extent rather than do the shift. |
| 5494 | * |
| 5495 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5496 | * with the provided extents via xfs_bmse_can_merge(). |
| 5497 | */ |
| 5498 | STATIC int |
| 5499 | xfs_bmse_merge( |
| 5500 | struct xfs_inode *ip, |
| 5501 | int whichfork, |
| 5502 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5503 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5504 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5505 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5506 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5507 | int *logflags, /* output */ |
| 5508 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5509 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5510 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5511 | xfs_filblks_t blockcount; |
| 5512 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5513 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5514 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5515 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5516 | |
| 5517 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5518 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5519 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5520 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5521 | new = *left; |
| 5522 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5523 | |
| 5524 | /* |
| 5525 | * Update the on-disk extent count, the btree if necessary and log the |
| 5526 | * inode. |
| 5527 | */ |
| 5528 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5529 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5530 | *logflags |= XFS_ILOG_CORE; |
| 5531 | if (!cur) { |
| 5532 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5533 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5537 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5538 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5539 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5540 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5541 | |
| 5542 | error = xfs_btree_delete(cur, &i); |
| 5543 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5544 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5545 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5546 | |
| 5547 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5548 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5549 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5550 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5551 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5552 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5553 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5554 | if (error) |
| 5555 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5556 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5557 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5558 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5559 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5560 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5561 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5562 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5563 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5564 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5565 | if (error) |
| 5566 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5567 | memcpy(&new, got, sizeof(new)); |
| 5568 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5569 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5570 | } |
| 5571 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5572 | static int |
| 5573 | xfs_bmap_shift_update_extent( |
| 5574 | struct xfs_inode *ip, |
| 5575 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5576 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5577 | struct xfs_bmbt_irec *got, |
| 5578 | struct xfs_btree_cur *cur, |
| 5579 | int *logflags, |
| 5580 | struct xfs_defer_ops *dfops, |
| 5581 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5582 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5583 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5584 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5585 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5586 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5587 | *logflags |= XFS_ILOG_CORE; |
| 5588 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5589 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5590 | |
| 5591 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5592 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5593 | if (error) |
| 5594 | return error; |
| 5595 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5596 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5597 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5598 | if (error) |
| 5599 | return error; |
| 5600 | } else { |
| 5601 | *logflags |= XFS_ILOG_DEXT; |
| 5602 | } |
| 5603 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5604 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5605 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5606 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5607 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5608 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5609 | if (error) |
| 5610 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5611 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5612 | } |
| 5613 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5614 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5615 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5616 | struct xfs_trans *tp, |
| 5617 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5618 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5619 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5620 | bool *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5621 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5622 | xfs_fsblock_t *firstblock, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5623 | struct xfs_defer_ops *dfops) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5624 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5625 | int whichfork = XFS_DATA_FORK; |
| 5626 | struct xfs_mount *mp = ip->i_mount; |
| 5627 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5628 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5629 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5630 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5631 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5632 | int error = 0; |
| 5633 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5634 | |
| 5635 | if (unlikely(XFS_TEST_ERROR( |
| 5636 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5637 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5638 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5639 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5640 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5644 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5645 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5646 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5647 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5648 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5649 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5650 | if (error) |
| 5651 | return error; |
| 5652 | } |
| 5653 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5654 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5655 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5656 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5657 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5658 | cur->bc_private.b.flags = 0; |
| 5659 | } |
| 5660 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5661 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5662 | *done = true; |
| 5663 | goto del_cursor; |
| 5664 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame^] | 5665 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5666 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5667 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5668 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5669 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5670 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5671 | error = -EINVAL; |
| 5672 | goto del_cursor; |
| 5673 | } |
| 5674 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5675 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5676 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5677 | &icur, &got, &prev, cur, &logflags, |
| 5678 | dfops); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5679 | if (error) |
| 5680 | goto del_cursor; |
| 5681 | goto done; |
| 5682 | } |
| 5683 | } else { |
| 5684 | if (got.br_startoff < offset_shift_fsb) { |
| 5685 | error = -EINVAL; |
| 5686 | goto del_cursor; |
| 5687 | } |
| 5688 | } |
| 5689 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5690 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5691 | &logflags, dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5692 | if (error) |
| 5693 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5694 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5695 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5696 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5697 | *done = true; |
| 5698 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5699 | } |
| 5700 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5701 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5702 | del_cursor: |
| 5703 | if (cur) |
| 5704 | xfs_btree_del_cursor(cur, |
| 5705 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5706 | if (logflags) |
| 5707 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5708 | return error; |
| 5709 | } |
| 5710 | |
| 5711 | int |
| 5712 | xfs_bmap_insert_extents( |
| 5713 | struct xfs_trans *tp, |
| 5714 | struct xfs_inode *ip, |
| 5715 | xfs_fileoff_t *next_fsb, |
| 5716 | xfs_fileoff_t offset_shift_fsb, |
| 5717 | bool *done, |
| 5718 | xfs_fileoff_t stop_fsb, |
| 5719 | xfs_fsblock_t *firstblock, |
| 5720 | struct xfs_defer_ops *dfops) |
| 5721 | { |
| 5722 | int whichfork = XFS_DATA_FORK; |
| 5723 | struct xfs_mount *mp = ip->i_mount; |
| 5724 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5725 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5726 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5727 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5728 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5729 | int error = 0; |
| 5730 | int logflags = 0; |
| 5731 | |
| 5732 | if (unlikely(XFS_TEST_ERROR( |
| 5733 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5734 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5735 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5736 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5737 | return -EFSCORRUPTED; |
| 5738 | } |
| 5739 | |
| 5740 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5741 | return -EIO; |
| 5742 | |
| 5743 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5744 | |
| 5745 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5746 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5747 | if (error) |
| 5748 | return error; |
| 5749 | } |
| 5750 | |
| 5751 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5752 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5753 | cur->bc_private.b.firstblock = *firstblock; |
| 5754 | cur->bc_private.b.dfops = dfops; |
| 5755 | cur->bc_private.b.flags = 0; |
| 5756 | } |
| 5757 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5758 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5759 | xfs_iext_last(ifp, &icur); |
| 5760 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5761 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5762 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5763 | goto del_cursor; |
| 5764 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5765 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5766 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5767 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5768 | goto del_cursor; |
| 5769 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5770 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame^] | 5771 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5772 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5773 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5774 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5775 | error = -EIO; |
| 5776 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5777 | } |
| 5778 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5779 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5780 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5781 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5782 | error = -EINVAL; |
| 5783 | goto del_cursor; |
| 5784 | } |
| 5785 | |
| 5786 | /* |
| 5787 | * Unlike a left shift (which involves a hole punch), a right |
| 5788 | * shift does not modify extent neighbors in any way. We should |
| 5789 | * never find mergeable extents in this scenario. Check anyways |
| 5790 | * and warn if we encounter two extents that could be one. |
| 5791 | */ |
| 5792 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5793 | WARN_ON_ONCE(1); |
| 5794 | } |
| 5795 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5796 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5797 | &logflags, dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5798 | if (error) |
| 5799 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5800 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5801 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5802 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5803 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5804 | goto del_cursor; |
| 5805 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5806 | |
| 5807 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5808 | del_cursor: |
| 5809 | if (cur) |
| 5810 | xfs_btree_del_cursor(cur, |
| 5811 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5812 | if (logflags) |
| 5813 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5814 | return error; |
| 5815 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5816 | |
| 5817 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5818 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5819 | * first block of the current_ext. @ext is a target extent to be split. |
| 5820 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5821 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5822 | */ |
| 5823 | STATIC int |
| 5824 | xfs_bmap_split_extent_at( |
| 5825 | struct xfs_trans *tp, |
| 5826 | struct xfs_inode *ip, |
| 5827 | xfs_fileoff_t split_fsb, |
| 5828 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5829 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5830 | { |
| 5831 | int whichfork = XFS_DATA_FORK; |
| 5832 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5833 | struct xfs_bmbt_irec got; |
| 5834 | struct xfs_bmbt_irec new; /* split extent */ |
| 5835 | struct xfs_mount *mp = ip->i_mount; |
| 5836 | struct xfs_ifork *ifp; |
| 5837 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5838 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5839 | int error = 0; |
| 5840 | int logflags = 0; |
| 5841 | int i = 0; |
| 5842 | |
| 5843 | if (unlikely(XFS_TEST_ERROR( |
| 5844 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5845 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5846 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5847 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5848 | XFS_ERRLEVEL_LOW, mp); |
| 5849 | return -EFSCORRUPTED; |
| 5850 | } |
| 5851 | |
| 5852 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5853 | return -EIO; |
| 5854 | |
| 5855 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5856 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5857 | /* Read in all the extents */ |
| 5858 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5859 | if (error) |
| 5860 | return error; |
| 5861 | } |
| 5862 | |
| 5863 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5864 | * If there are not extents, or split_fsb lies in a hole we are done. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5865 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5866 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5867 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5868 | return 0; |
| 5869 | |
| 5870 | gotblkcnt = split_fsb - got.br_startoff; |
| 5871 | new.br_startoff = split_fsb; |
| 5872 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5873 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5874 | new.br_state = got.br_state; |
| 5875 | |
| 5876 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5877 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5878 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5879 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5880 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5881 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5882 | if (error) |
| 5883 | goto del_cursor; |
| 5884 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5885 | } |
| 5886 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5887 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5888 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5889 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5890 | |
| 5891 | logflags = XFS_ILOG_CORE; |
| 5892 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5893 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5894 | if (error) |
| 5895 | goto del_cursor; |
| 5896 | } else |
| 5897 | logflags |= XFS_ILOG_DEXT; |
| 5898 | |
| 5899 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5900 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5901 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5902 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5903 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5904 | |
| 5905 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5906 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5907 | if (error) |
| 5908 | goto del_cursor; |
| 5909 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5910 | error = xfs_btree_insert(cur, &i); |
| 5911 | if (error) |
| 5912 | goto del_cursor; |
| 5913 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5914 | } |
| 5915 | |
| 5916 | /* |
| 5917 | * Convert to a btree if necessary. |
| 5918 | */ |
| 5919 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5920 | int tmp_logflags; /* partial log flag return val */ |
| 5921 | |
| 5922 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5923 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5924 | &cur, 0, &tmp_logflags, whichfork); |
| 5925 | logflags |= tmp_logflags; |
| 5926 | } |
| 5927 | |
| 5928 | del_cursor: |
| 5929 | if (cur) { |
| 5930 | cur->bc_private.b.allocated = 0; |
| 5931 | xfs_btree_del_cursor(cur, |
| 5932 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5933 | } |
| 5934 | |
| 5935 | if (logflags) |
| 5936 | xfs_trans_log_inode(tp, ip, logflags); |
| 5937 | return error; |
| 5938 | } |
| 5939 | |
| 5940 | int |
| 5941 | xfs_bmap_split_extent( |
| 5942 | struct xfs_inode *ip, |
| 5943 | xfs_fileoff_t split_fsb) |
| 5944 | { |
| 5945 | struct xfs_mount *mp = ip->i_mount; |
| 5946 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5947 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5948 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5949 | int error; |
| 5950 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 5951 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 5952 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 5953 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5954 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5955 | |
| 5956 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5957 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 5958 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5959 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5960 | |
| 5961 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5962 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5963 | if (error) |
| 5964 | goto out; |
| 5965 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 5966 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5967 | if (error) |
| 5968 | goto out; |
| 5969 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 5970 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5971 | |
| 5972 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5973 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 5974 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5975 | return error; |
| 5976 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 5977 | |
| 5978 | /* Deferred mapping is only for real extents in the data fork. */ |
| 5979 | static bool |
| 5980 | xfs_bmap_is_update_needed( |
| 5981 | struct xfs_bmbt_irec *bmap) |
| 5982 | { |
| 5983 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 5984 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 5985 | } |
| 5986 | |
| 5987 | /* Record a bmap intent. */ |
| 5988 | static int |
| 5989 | __xfs_bmap_add( |
| 5990 | struct xfs_mount *mp, |
| 5991 | struct xfs_defer_ops *dfops, |
| 5992 | enum xfs_bmap_intent_type type, |
| 5993 | struct xfs_inode *ip, |
| 5994 | int whichfork, |
| 5995 | struct xfs_bmbt_irec *bmap) |
| 5996 | { |
| 5997 | int error; |
| 5998 | struct xfs_bmap_intent *bi; |
| 5999 | |
| 6000 | trace_xfs_bmap_defer(mp, |
| 6001 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6002 | type, |
| 6003 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6004 | ip->i_ino, whichfork, |
| 6005 | bmap->br_startoff, |
| 6006 | bmap->br_blockcount, |
| 6007 | bmap->br_state); |
| 6008 | |
| 6009 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6010 | INIT_LIST_HEAD(&bi->bi_list); |
| 6011 | bi->bi_type = type; |
| 6012 | bi->bi_owner = ip; |
| 6013 | bi->bi_whichfork = whichfork; |
| 6014 | bi->bi_bmap = *bmap; |
| 6015 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6016 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6017 | if (error) { |
| 6018 | kmem_free(bi); |
| 6019 | return error; |
| 6020 | } |
| 6021 | |
| 6022 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6023 | return 0; |
| 6024 | } |
| 6025 | |
| 6026 | /* Map an extent into a file. */ |
| 6027 | int |
| 6028 | xfs_bmap_map_extent( |
| 6029 | struct xfs_mount *mp, |
| 6030 | struct xfs_defer_ops *dfops, |
| 6031 | struct xfs_inode *ip, |
| 6032 | struct xfs_bmbt_irec *PREV) |
| 6033 | { |
| 6034 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6035 | return 0; |
| 6036 | |
| 6037 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6038 | XFS_DATA_FORK, PREV); |
| 6039 | } |
| 6040 | |
| 6041 | /* Unmap an extent out of a file. */ |
| 6042 | int |
| 6043 | xfs_bmap_unmap_extent( |
| 6044 | struct xfs_mount *mp, |
| 6045 | struct xfs_defer_ops *dfops, |
| 6046 | struct xfs_inode *ip, |
| 6047 | struct xfs_bmbt_irec *PREV) |
| 6048 | { |
| 6049 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6050 | return 0; |
| 6051 | |
| 6052 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6053 | XFS_DATA_FORK, PREV); |
| 6054 | } |
| 6055 | |
| 6056 | /* |
| 6057 | * Process one of the deferred bmap operations. We pass back the |
| 6058 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6059 | */ |
| 6060 | int |
| 6061 | xfs_bmap_finish_one( |
| 6062 | struct xfs_trans *tp, |
| 6063 | struct xfs_defer_ops *dfops, |
| 6064 | struct xfs_inode *ip, |
| 6065 | enum xfs_bmap_intent_type type, |
| 6066 | int whichfork, |
| 6067 | xfs_fileoff_t startoff, |
| 6068 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6069 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6070 | xfs_exntst_t state) |
| 6071 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6072 | xfs_fsblock_t firstfsb; |
| 6073 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6074 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6075 | /* |
| 6076 | * firstfsb is tied to the transaction lifetime and is used to |
| 6077 | * ensure correct AG locking order and schedule work item |
| 6078 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6079 | * to only making one bmap call per transaction, so it should |
| 6080 | * be safe to have it as a local variable here. |
| 6081 | */ |
| 6082 | firstfsb = NULLFSBLOCK; |
| 6083 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6084 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6085 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6086 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6087 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6088 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6089 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6090 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6091 | |
| 6092 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6093 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6094 | return -EIO; |
| 6095 | |
| 6096 | switch (type) { |
| 6097 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6098 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6099 | startblock, dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6100 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6101 | break; |
| 6102 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6103 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6104 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6105 | break; |
| 6106 | default: |
| 6107 | ASSERT(0); |
| 6108 | error = -EFSCORRUPTED; |
| 6109 | } |
| 6110 | |
| 6111 | return error; |
| 6112 | } |