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 | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 63 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 64 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 66 | void |
| 67 | xfs_bmap_compute_maxlevels( |
| 68 | xfs_mount_t *mp, /* file system mount structure */ |
| 69 | int whichfork) /* data or attr fork */ |
| 70 | { |
| 71 | int level; /* btree level */ |
| 72 | uint maxblocks; /* max blocks at this level */ |
| 73 | uint maxleafents; /* max leaf entries possible */ |
| 74 | int maxrootrecs; /* max records in root block */ |
| 75 | int minleafrecs; /* min records in leaf block */ |
| 76 | int minnoderecs; /* min records in node block */ |
| 77 | int sz; /* root block size */ |
| 78 | |
| 79 | /* |
| 80 | * The maximum number of extents in a file, hence the maximum |
| 81 | * number of leaf entries, is controlled by the type of di_nextents |
| 82 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 83 | * (a signed 16-bit number, xfs_aextnum_t). |
| 84 | * |
| 85 | * Note that we can no longer assume that if we are in ATTR1 that |
| 86 | * the fork offset of all the inodes will be |
| 87 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 88 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 89 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 90 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 91 | * of a minimum size available. |
| 92 | */ |
| 93 | if (whichfork == XFS_DATA_FORK) { |
| 94 | maxleafents = MAXEXTNUM; |
| 95 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 96 | } else { |
| 97 | maxleafents = MAXAEXTNUM; |
| 98 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 99 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 100 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 101 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 102 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 103 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 104 | for (level = 1; maxblocks > 1; level++) { |
| 105 | if (maxblocks <= maxrootrecs) |
| 106 | maxblocks = 1; |
| 107 | else |
| 108 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 109 | } |
| 110 | mp->m_bm_maxlevels[whichfork] = level; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | STATIC int /* error */ |
| 114 | xfs_bmbt_lookup_eq( |
| 115 | struct xfs_btree_cur *cur, |
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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | 9e5987a | 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 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 249 | struct xfs_log_item *lip; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 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 */ |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 263 | list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) { |
| 264 | struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip; |
| 265 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 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)); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 315 | xfs_err(mp, "%s: ptrs are equal in node\n", |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 316 | __func__); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 317 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * 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] | 325 | * btree leaves. THis becomes prohibitively expensive for large extent count |
| 326 | * files, so don't bother with inodes that have more than 10,000 extents in |
| 327 | * them. The btree record ordering checks will still be done, so for such large |
| 328 | * bmapbt constructs that is going to catch most corruptions. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 329 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 330 | STATIC void |
| 331 | xfs_bmap_check_leaf_extents( |
| 332 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 333 | xfs_inode_t *ip, /* incore inode pointer */ |
| 334 | int whichfork) /* data or attr fork */ |
| 335 | { |
| 336 | struct xfs_btree_block *block; /* current btree block */ |
| 337 | xfs_fsblock_t bno; /* block # of "block" */ |
| 338 | xfs_buf_t *bp; /* buffer for "block" */ |
| 339 | int error; /* error return value */ |
| 340 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 341 | xfs_ifork_t *ifp; /* fork structure */ |
| 342 | int level; /* btree level, for checking */ |
| 343 | xfs_mount_t *mp; /* file system mount structure */ |
| 344 | __be64 *pp; /* pointer to block address */ |
| 345 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 346 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 347 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 348 | int bp_release = 0; |
| 349 | |
| 350 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 351 | return; |
| 352 | } |
| 353 | |
Dave Chinner | e354381 | 2016-01-08 11:28:49 +1100 | [diff] [blame] | 354 | /* skip large extent count inodes */ |
| 355 | if (ip->i_d.di_nextents > 10000) |
| 356 | return; |
| 357 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 358 | bno = NULLFSBLOCK; |
| 359 | mp = ip->i_mount; |
| 360 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 361 | block = ifp->if_broot; |
| 362 | /* |
| 363 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 364 | */ |
| 365 | level = be16_to_cpu(block->bb_level); |
| 366 | ASSERT(level > 0); |
| 367 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 368 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 369 | bno = be64_to_cpu(*pp); |
| 370 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 371 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 372 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 373 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 374 | |
| 375 | /* |
| 376 | * Go down the tree until leaf level is reached, following the first |
| 377 | * pointer (leftmost) at each level. |
| 378 | */ |
| 379 | while (level-- > 0) { |
| 380 | /* See if buf is in cur first */ |
| 381 | bp_release = 0; |
| 382 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 383 | if (!bp) { |
| 384 | bp_release = 1; |
| 385 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 386 | XFS_BMAP_BTREE_REF, |
| 387 | &xfs_bmbt_buf_ops); |
| 388 | if (error) |
| 389 | goto error_norelse; |
| 390 | } |
| 391 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 392 | if (level == 0) |
| 393 | break; |
| 394 | |
| 395 | /* |
| 396 | * Check this block for basic sanity (increasing keys and |
| 397 | * no duplicate blocks). |
| 398 | */ |
| 399 | |
| 400 | xfs_check_block(block, mp, 0, 0); |
| 401 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 402 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 403 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 404 | xfs_verify_fsbno(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 405 | if (bp_release) { |
| 406 | bp_release = 0; |
| 407 | xfs_trans_brelse(NULL, bp); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 413 | */ |
| 414 | i = 0; |
| 415 | |
| 416 | /* |
| 417 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 418 | */ |
| 419 | for (;;) { |
| 420 | xfs_fsblock_t nextbno; |
| 421 | xfs_extnum_t num_recs; |
| 422 | |
| 423 | |
| 424 | num_recs = xfs_btree_get_numrecs(block); |
| 425 | |
| 426 | /* |
| 427 | * Read-ahead the next leaf block, if any. |
| 428 | */ |
| 429 | |
| 430 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 431 | |
| 432 | /* |
| 433 | * Check all the extents to make sure they are OK. |
| 434 | * If we had a previous block, the last entry should |
| 435 | * conform with the first entry in this one. |
| 436 | */ |
| 437 | |
| 438 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 439 | if (i) { |
| 440 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 441 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 442 | xfs_bmbt_disk_get_startoff(ep)); |
| 443 | } |
| 444 | for (j = 1; j < num_recs; j++) { |
| 445 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 446 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 447 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 448 | xfs_bmbt_disk_get_startoff(nextp)); |
| 449 | ep = nextp; |
| 450 | } |
| 451 | |
| 452 | last = *ep; |
| 453 | i += num_recs; |
| 454 | if (bp_release) { |
| 455 | bp_release = 0; |
| 456 | xfs_trans_brelse(NULL, bp); |
| 457 | } |
| 458 | bno = nextbno; |
| 459 | /* |
| 460 | * If we've reached the end, stop. |
| 461 | */ |
| 462 | if (bno == NULLFSBLOCK) |
| 463 | break; |
| 464 | |
| 465 | bp_release = 0; |
| 466 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 467 | if (!bp) { |
| 468 | bp_release = 1; |
| 469 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 470 | XFS_BMAP_BTREE_REF, |
| 471 | &xfs_bmbt_buf_ops); |
| 472 | if (error) |
| 473 | goto error_norelse; |
| 474 | } |
| 475 | block = XFS_BUF_TO_BLOCK(bp); |
| 476 | } |
Luis de Bethencourt | a5fd276 | 2016-03-09 08:17:56 +1100 | [diff] [blame] | 477 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 478 | return; |
| 479 | |
| 480 | error0: |
| 481 | xfs_warn(mp, "%s: at error0", __func__); |
| 482 | if (bp_release) |
| 483 | xfs_trans_brelse(NULL, bp); |
| 484 | error_norelse: |
| 485 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 486 | __func__, i); |
Darrick J. Wong | cec5725 | 2018-05-04 15:31:21 -0700 | [diff] [blame] | 487 | xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 488 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
| 492 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 493 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 494 | * given the caller's original parameters. Specifically check the |
| 495 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 496 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 497 | */ |
| 498 | STATIC void |
| 499 | xfs_bmap_validate_ret( |
| 500 | xfs_fileoff_t bno, |
| 501 | xfs_filblks_t len, |
| 502 | int flags, |
| 503 | xfs_bmbt_irec_t *mval, |
| 504 | int nmap, |
| 505 | int ret_nmap) |
| 506 | { |
| 507 | int i; /* index to map values */ |
| 508 | |
| 509 | ASSERT(ret_nmap <= nmap); |
| 510 | |
| 511 | for (i = 0; i < ret_nmap; i++) { |
| 512 | ASSERT(mval[i].br_blockcount > 0); |
| 513 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 514 | ASSERT(mval[i].br_startoff >= bno); |
| 515 | ASSERT(mval[i].br_blockcount <= len); |
| 516 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 517 | bno + len); |
| 518 | } else { |
| 519 | ASSERT(mval[i].br_startoff < bno + len); |
| 520 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 521 | bno); |
| 522 | } |
| 523 | ASSERT(i == 0 || |
| 524 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 525 | mval[i].br_startoff); |
| 526 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 527 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 528 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 529 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | #else |
| 534 | #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] | 535 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 536 | #endif /* DEBUG */ |
| 537 | |
| 538 | /* |
| 539 | * bmap free list manipulation functions |
| 540 | */ |
| 541 | |
| 542 | /* |
| 543 | * Add the extent to the list of extents to be free at transaction end. |
| 544 | * The list is maintained sorted (by block number). |
| 545 | */ |
| 546 | void |
| 547 | xfs_bmap_add_free( |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 548 | struct xfs_mount *mp, |
| 549 | struct xfs_defer_ops *dfops, |
| 550 | xfs_fsblock_t bno, |
| 551 | xfs_filblks_t len, |
| 552 | struct xfs_owner_info *oinfo) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 553 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 554 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 555 | #ifdef DEBUG |
| 556 | xfs_agnumber_t agno; |
| 557 | xfs_agblock_t agbno; |
| 558 | |
| 559 | ASSERT(bno != NULLFSBLOCK); |
| 560 | ASSERT(len > 0); |
| 561 | ASSERT(len <= MAXEXTLEN); |
| 562 | ASSERT(!isnullstartblock(bno)); |
| 563 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 564 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 565 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 566 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 567 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 568 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 569 | #endif |
| 570 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 571 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 572 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 573 | new->xefi_startblock = bno; |
| 574 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 575 | if (oinfo) |
| 576 | new->xefi_oinfo = *oinfo; |
| 577 | else |
| 578 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 579 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 580 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 581 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Inode fork format manipulation functions |
| 586 | */ |
| 587 | |
| 588 | /* |
| 589 | * Transform a btree format file with only one leaf node, where the |
| 590 | * extents list will fit in the inode, into an extents format file. |
| 591 | * Since the file extents are already in-core, all we have to do is |
| 592 | * give up the space for the btree root and pitch the leaf block. |
| 593 | */ |
| 594 | STATIC int /* error */ |
| 595 | xfs_bmap_btree_to_extents( |
| 596 | xfs_trans_t *tp, /* transaction pointer */ |
| 597 | xfs_inode_t *ip, /* incore inode pointer */ |
| 598 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 599 | int *logflagsp, /* inode logging flags */ |
| 600 | int whichfork) /* data or attr fork */ |
| 601 | { |
| 602 | /* REFERENCED */ |
| 603 | struct xfs_btree_block *cblock;/* child btree block */ |
| 604 | xfs_fsblock_t cbno; /* child block number */ |
| 605 | xfs_buf_t *cbp; /* child block's buffer */ |
| 606 | int error; /* error return value */ |
| 607 | xfs_ifork_t *ifp; /* inode fork data */ |
| 608 | xfs_mount_t *mp; /* mount point structure */ |
| 609 | __be64 *pp; /* ptr to block address */ |
| 610 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 611 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 612 | |
| 613 | mp = ip->i_mount; |
| 614 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 615 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 616 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 617 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 618 | rblock = ifp->if_broot; |
| 619 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 620 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 621 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 622 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 623 | cbno = be64_to_cpu(*pp); |
| 624 | *logflagsp = 0; |
| 625 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 626 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 627 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 628 | #endif |
| 629 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 630 | &xfs_bmbt_buf_ops); |
| 631 | if (error) |
| 632 | return error; |
| 633 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 634 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 635 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 636 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 637 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 638 | ip->i_d.di_nblocks--; |
| 639 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 640 | xfs_trans_binval(tp, cbp); |
| 641 | if (cur->bc_bufs[0] == cbp) |
| 642 | cur->bc_bufs[0] = NULL; |
| 643 | xfs_iroot_realloc(ip, -1, whichfork); |
| 644 | ASSERT(ifp->if_broot == NULL); |
| 645 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 646 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 647 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Convert an extents-format file into a btree-format file. |
| 653 | * The new file will have a root block (in the inode) and a single child block. |
| 654 | */ |
| 655 | STATIC int /* error */ |
| 656 | xfs_bmap_extents_to_btree( |
| 657 | xfs_trans_t *tp, /* transaction pointer */ |
| 658 | xfs_inode_t *ip, /* incore inode pointer */ |
| 659 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 660 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 661 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 662 | int wasdel, /* converting a delayed alloc */ |
| 663 | int *logflagsp, /* inode logging flags */ |
| 664 | int whichfork) /* data or attr fork */ |
| 665 | { |
| 666 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 667 | xfs_buf_t *abp; /* buffer for ablock */ |
| 668 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 669 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 670 | struct xfs_btree_block *block; /* btree root block */ |
| 671 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 672 | int error; /* error return value */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 673 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 674 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 675 | xfs_mount_t *mp; /* mount structure */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 676 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 677 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 678 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 679 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 680 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 681 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 682 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 683 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 684 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 685 | |
| 686 | /* |
| 687 | * Make space in the inode incore. |
| 688 | */ |
| 689 | xfs_iroot_realloc(ip, 1, whichfork); |
| 690 | ifp->if_flags |= XFS_IFBROOT; |
| 691 | |
| 692 | /* |
| 693 | * Fill in the root. |
| 694 | */ |
| 695 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 696 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 697 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 698 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 699 | /* |
| 700 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 701 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 702 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 703 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 704 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 705 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 706 | /* |
| 707 | * Convert to a btree with two levels, one record in root. |
| 708 | */ |
| 709 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 710 | memset(&args, 0, sizeof(args)); |
| 711 | args.tp = tp; |
| 712 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 713 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 714 | args.firstblock = *firstblock; |
| 715 | if (*firstblock == NULLFSBLOCK) { |
| 716 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 717 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 718 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 719 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 720 | args.fsbno = *firstblock; |
| 721 | } else { |
| 722 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 723 | args.fsbno = *firstblock; |
| 724 | } |
| 725 | args.minlen = args.maxlen = args.prod = 1; |
| 726 | args.wasdel = wasdel; |
| 727 | *logflagsp = 0; |
| 728 | if ((error = xfs_alloc_vextent(&args))) { |
| 729 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 730 | ASSERT(ifp->if_broot == NULL); |
| 731 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 732 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 733 | return error; |
| 734 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 735 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 736 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 737 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 738 | ASSERT(ifp->if_broot == NULL); |
| 739 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 740 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 741 | return -ENOSPC; |
| 742 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 743 | /* |
| 744 | * Allocation can't fail, the space was reserved. |
| 745 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 746 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 747 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 748 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 749 | cur->bc_private.b.allocated++; |
| 750 | ip->i_d.di_nblocks++; |
| 751 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 752 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 753 | /* |
| 754 | * Fill in the child block. |
| 755 | */ |
| 756 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 757 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 758 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 759 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 760 | XFS_BTREE_LONG_PTRS); |
| 761 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 762 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 763 | if (isnullstartblock(rec.br_startblock)) |
| 764 | continue; |
| 765 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 766 | xfs_bmbt_disk_set_all(arp, &rec); |
| 767 | cnt++; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 768 | } |
| 769 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 770 | xfs_btree_set_numrecs(ablock, cnt); |
| 771 | |
| 772 | /* |
| 773 | * Fill in the root key and pointer. |
| 774 | */ |
| 775 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 776 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 777 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 778 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 779 | be16_to_cpu(block->bb_level))); |
| 780 | *pp = cpu_to_be64(args.fsbno); |
| 781 | |
| 782 | /* |
| 783 | * Do all this logging at the end so that |
| 784 | * the root is at the right level. |
| 785 | */ |
| 786 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 787 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 788 | ASSERT(*curp == NULL); |
| 789 | *curp = cur; |
| 790 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * Convert a local file to an extents file. |
| 796 | * This code is out of bounds for data forks of regular files, |
| 797 | * since the file data needs to get logged so things will stay consistent. |
| 798 | * (The bmap-level manipulations are ok, though). |
| 799 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 800 | void |
| 801 | xfs_bmap_local_to_extents_empty( |
| 802 | struct xfs_inode *ip, |
| 803 | int whichfork) |
| 804 | { |
| 805 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 806 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 807 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 808 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 809 | ASSERT(ifp->if_bytes == 0); |
| 810 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 811 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 812 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 813 | ifp->if_flags &= ~XFS_IFINLINE; |
| 814 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 815 | ifp->if_u1.if_root = NULL; |
| 816 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 817 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 818 | } |
| 819 | |
| 820 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 821 | STATIC int /* error */ |
| 822 | xfs_bmap_local_to_extents( |
| 823 | xfs_trans_t *tp, /* transaction pointer */ |
| 824 | xfs_inode_t *ip, /* incore inode pointer */ |
| 825 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 826 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 827 | int *logflagsp, /* inode logging flags */ |
| 828 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 829 | void (*init_fn)(struct xfs_trans *tp, |
| 830 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 831 | struct xfs_inode *ip, |
| 832 | struct xfs_ifork *ifp)) |
| 833 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 834 | int error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 835 | int flags; /* logging flags returned */ |
| 836 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 837 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 838 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 839 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 840 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 841 | |
| 842 | /* |
| 843 | * We don't want to deal with the case of keeping inode data inline yet. |
| 844 | * So sending the data fork of a regular inode is invalid. |
| 845 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 846 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 847 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 848 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 849 | |
| 850 | if (!ifp->if_bytes) { |
| 851 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 852 | flags = XFS_ILOG_CORE; |
| 853 | goto done; |
| 854 | } |
| 855 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 856 | flags = 0; |
| 857 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 858 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 859 | memset(&args, 0, sizeof(args)); |
| 860 | args.tp = tp; |
| 861 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 862 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 863 | args.firstblock = *firstblock; |
| 864 | /* |
| 865 | * Allocate a block. We know we need only one, since the |
| 866 | * file currently fits in an inode. |
| 867 | */ |
| 868 | if (*firstblock == NULLFSBLOCK) { |
| 869 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 870 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 871 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 872 | args.fsbno = *firstblock; |
| 873 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 874 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 875 | args.total = total; |
| 876 | args.minlen = args.maxlen = args.prod = 1; |
| 877 | error = xfs_alloc_vextent(&args); |
| 878 | if (error) |
| 879 | goto done; |
| 880 | |
| 881 | /* Can't fail, the space was reserved. */ |
| 882 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 883 | ASSERT(args.len == 1); |
| 884 | *firstblock = args.fsbno; |
| 885 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 886 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 887 | /* |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 888 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 889 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 890 | * The callout is responsible for logging because the remote format |
| 891 | * might differ from the local format and thus we don't know how much to |
| 892 | * log here. Note that init_fn must also set the buffer log item type |
| 893 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 894 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 895 | init_fn(tp, bp, ip, ifp); |
| 896 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 897 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 898 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 899 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 900 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 901 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 902 | ifp->if_u1.if_root = NULL; |
| 903 | ifp->if_height = 0; |
| 904 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 905 | rec.br_startoff = 0; |
| 906 | rec.br_startblock = args.fsbno; |
| 907 | rec.br_blockcount = 1; |
| 908 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 909 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 910 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 911 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 912 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 913 | ip->i_d.di_nblocks = 1; |
| 914 | xfs_trans_mod_dquot_byino(tp, ip, |
| 915 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 916 | flags |= xfs_ilog_fext(whichfork); |
| 917 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 918 | done: |
| 919 | *logflagsp = flags; |
| 920 | return error; |
| 921 | } |
| 922 | |
| 923 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 925 | */ |
| 926 | STATIC int /* error */ |
| 927 | xfs_bmap_add_attrfork_btree( |
| 928 | xfs_trans_t *tp, /* transaction pointer */ |
| 929 | xfs_inode_t *ip, /* incore inode pointer */ |
| 930 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 931 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | int *flags) /* inode logging flags */ |
| 933 | { |
| 934 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 935 | int error; /* error return value */ |
| 936 | xfs_mount_t *mp; /* file system mount struct */ |
| 937 | int stat; /* newroot status */ |
| 938 | |
| 939 | mp = ip->i_mount; |
| 940 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 941 | *flags |= XFS_ILOG_DBROOT; |
| 942 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 943 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 944 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 946 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 947 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 949 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 950 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 951 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | goto error0; |
| 953 | if (stat == 0) { |
| 954 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 955 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | } |
| 957 | *firstblock = cur->bc_private.b.firstblock; |
| 958 | cur->bc_private.b.allocated = 0; |
| 959 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 960 | } |
| 961 | return 0; |
| 962 | error0: |
| 963 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 964 | return error; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 969 | */ |
| 970 | STATIC int /* error */ |
| 971 | xfs_bmap_add_attrfork_extents( |
| 972 | xfs_trans_t *tp, /* transaction pointer */ |
| 973 | xfs_inode_t *ip, /* incore inode pointer */ |
| 974 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 975 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | int *flags) /* inode logging flags */ |
| 977 | { |
| 978 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 979 | int error; /* error return value */ |
| 980 | |
| 981 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 982 | return 0; |
| 983 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 984 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | flags, XFS_DATA_FORK); |
| 986 | if (cur) { |
| 987 | cur->bc_private.b.allocated = 0; |
| 988 | xfs_btree_del_cursor(cur, |
| 989 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 990 | } |
| 991 | return error; |
| 992 | } |
| 993 | |
| 994 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 995 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 996 | * different data fork content type needs a different callout to do the |
| 997 | * conversion. Some are basic and only require special block initialisation |
| 998 | * callouts for the data formating, others (directories) are so specialised they |
| 999 | * handle everything themselves. |
| 1000 | * |
| 1001 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1002 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1003 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | */ |
| 1005 | STATIC int /* error */ |
| 1006 | xfs_bmap_add_attrfork_local( |
| 1007 | xfs_trans_t *tp, /* transaction pointer */ |
| 1008 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1009 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1010 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | int *flags) /* inode logging flags */ |
| 1012 | { |
| 1013 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
| 1015 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1016 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1017 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1018 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1020 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | dargs.dp = ip; |
| 1022 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1023 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1024 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | dargs.whichfork = XFS_DATA_FORK; |
| 1026 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1027 | return xfs_dir2_sf_to_block(&dargs); |
| 1028 | } |
| 1029 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1030 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1031 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1032 | flags, XFS_DATA_FORK, |
| 1033 | xfs_symlink_local_to_remote); |
| 1034 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1035 | /* should only be called for types that support local format data */ |
| 1036 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1037 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1041 | * Convert inode from non-attributed to attributed. |
| 1042 | * Must not be in a transaction, ip must not be locked. |
| 1043 | */ |
| 1044 | int /* error code */ |
| 1045 | xfs_bmap_add_attrfork( |
| 1046 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1047 | int size, /* space new attribute needs */ |
| 1048 | int rsvd) /* xact may use reserved blks */ |
| 1049 | { |
| 1050 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1051 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1052 | xfs_mount_t *mp; /* mount structure */ |
| 1053 | xfs_trans_t *tp; /* transaction pointer */ |
| 1054 | int blks; /* space reservation */ |
| 1055 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1056 | int logflags; /* logging flags */ |
| 1057 | int error; /* error return value */ |
| 1058 | |
| 1059 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1060 | |
| 1061 | mp = ip->i_mount; |
| 1062 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1063 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1064 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1065 | |
| 1066 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1067 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1068 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1069 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1070 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1071 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1072 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1073 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1074 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1075 | if (error) |
| 1076 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1077 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1078 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1079 | if (ip->i_d.di_anextents != 0) { |
| 1080 | error = -EFSCORRUPTED; |
| 1081 | goto trans_cancel; |
| 1082 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1083 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1084 | /* |
| 1085 | * For inodes coming from pre-6.2 filesystems. |
| 1086 | */ |
| 1087 | ASSERT(ip->i_d.di_aformat == 0); |
| 1088 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1089 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1090 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1091 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1092 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1093 | |
| 1094 | switch (ip->i_d.di_format) { |
| 1095 | case XFS_DINODE_FMT_DEV: |
| 1096 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1097 | break; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1098 | case XFS_DINODE_FMT_LOCAL: |
| 1099 | case XFS_DINODE_FMT_EXTENTS: |
| 1100 | case XFS_DINODE_FMT_BTREE: |
| 1101 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1102 | if (!ip->i_d.di_forkoff) |
| 1103 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1104 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1105 | version = 2; |
| 1106 | break; |
| 1107 | default: |
| 1108 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1109 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1110 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | ASSERT(ip->i_afp == NULL); |
| 1114 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1115 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1116 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1117 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1118 | switch (ip->i_d.di_format) { |
| 1119 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1120 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1121 | &logflags); |
| 1122 | break; |
| 1123 | case XFS_DINODE_FMT_EXTENTS: |
| 1124 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1125 | &dfops, &logflags); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1126 | break; |
| 1127 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1128 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | &logflags); |
| 1130 | break; |
| 1131 | default: |
| 1132 | error = 0; |
| 1133 | break; |
| 1134 | } |
| 1135 | if (logflags) |
| 1136 | xfs_trans_log_inode(tp, ip, logflags); |
| 1137 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1138 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1139 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1140 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1141 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1142 | |
| 1143 | spin_lock(&mp->m_sb_lock); |
| 1144 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1145 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1146 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1147 | } |
| 1148 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1149 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1150 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1151 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1152 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1153 | if (log_sb) |
| 1154 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1155 | } |
| 1156 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1157 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1158 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1159 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1160 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1161 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1162 | return error; |
| 1163 | |
| 1164 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1165 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1166 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1167 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1168 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1169 | return error; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * Internal and external extent tree search functions. |
| 1174 | */ |
| 1175 | |
| 1176 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1177 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1178 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1179 | int |
| 1180 | xfs_iread_extents( |
| 1181 | struct xfs_trans *tp, |
| 1182 | struct xfs_inode *ip, |
| 1183 | int whichfork) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1184 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1185 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1186 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1187 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1188 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1189 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1190 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1191 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1192 | xfs_fsblock_t bno; |
| 1193 | struct xfs_buf *bp; |
| 1194 | xfs_extnum_t i, j; |
| 1195 | int level; |
| 1196 | __be64 *pp; |
| 1197 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1198 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1199 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1200 | |
| 1201 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1202 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1203 | return -EFSCORRUPTED; |
| 1204 | } |
| 1205 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1206 | /* |
| 1207 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1208 | */ |
| 1209 | level = be16_to_cpu(block->bb_level); |
| 1210 | ASSERT(level > 0); |
| 1211 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1212 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1213 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1214 | /* |
| 1215 | * Go down the tree until leaf level is reached, following the first |
| 1216 | * pointer (leftmost) at each level. |
| 1217 | */ |
| 1218 | while (level-- > 0) { |
| 1219 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1220 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1221 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1222 | goto out; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1223 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1224 | if (level == 0) |
| 1225 | break; |
| 1226 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1227 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1228 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1229 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1230 | xfs_trans_brelse(tp, bp); |
| 1231 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1232 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1233 | /* |
| 1234 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1235 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1236 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1237 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1238 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1239 | /* |
| 1240 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1241 | */ |
| 1242 | for (;;) { |
| 1243 | xfs_bmbt_rec_t *frp; |
| 1244 | xfs_fsblock_t nextbno; |
| 1245 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1246 | |
| 1247 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1248 | if (unlikely(i + num_recs > nextents)) { |
| 1249 | ASSERT(i + num_recs <= nextents); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1250 | xfs_warn(ip->i_mount, |
| 1251 | "corrupt dinode %Lu, (btree extents).", |
| 1252 | (unsigned long long) ip->i_ino); |
Darrick J. Wong | 90a58f9 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1253 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1254 | __func__, block, sizeof(*block), |
| 1255 | __this_address); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1256 | error = -EFSCORRUPTED; |
| 1257 | goto out_brelse; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1258 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1259 | /* |
| 1260 | * Read-ahead the next leaf block, if any. |
| 1261 | */ |
| 1262 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1263 | if (nextbno != NULLFSBLOCK) |
| 1264 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1265 | &xfs_bmbt_buf_ops); |
| 1266 | /* |
| 1267 | * Copy records into the extent records. |
| 1268 | */ |
| 1269 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1270 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1271 | xfs_failaddr_t fa; |
| 1272 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1273 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1274 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1275 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1276 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1277 | xfs_inode_verifier_error(ip, error, |
| 1278 | "xfs_iread_extents(2)", |
| 1279 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1280 | goto out_brelse; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1281 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1282 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1283 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1284 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1285 | } |
| 1286 | xfs_trans_brelse(tp, bp); |
| 1287 | bno = nextbno; |
| 1288 | /* |
| 1289 | * If we've reached the end, stop. |
| 1290 | */ |
| 1291 | if (bno == NULLFSBLOCK) |
| 1292 | break; |
| 1293 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1294 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1295 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1296 | goto out; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1297 | block = XFS_BUF_TO_BLOCK(bp); |
| 1298 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1299 | |
| 1300 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1301 | error = -EFSCORRUPTED; |
| 1302 | goto out; |
| 1303 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1304 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1305 | |
| 1306 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1307 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1308 | |
| 1309 | out_brelse: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1310 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1311 | out: |
| 1312 | xfs_iext_destroy(ifp); |
| 1313 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1314 | } |
| 1315 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1316 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1317 | * Returns the relative block number of the first unused block(s) in the given |
| 1318 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1319 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1320 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1321 | */ |
| 1322 | int /* error */ |
| 1323 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1324 | struct xfs_trans *tp, /* transaction pointer */ |
| 1325 | struct xfs_inode *ip, /* incore inode */ |
| 1326 | xfs_extlen_t len, /* size of hole to find */ |
| 1327 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1328 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1329 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1330 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1331 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1332 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1333 | xfs_fileoff_t lastaddr = 0; |
| 1334 | xfs_fileoff_t lowest, max; |
| 1335 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1336 | |
| 1337 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1338 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1339 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1340 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1341 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1342 | *first_unused = 0; |
| 1343 | return 0; |
| 1344 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1345 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1346 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1347 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1348 | if (error) |
| 1349 | return error; |
| 1350 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1351 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1352 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1353 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1354 | /* |
| 1355 | * See if the hole before this extent will work. |
| 1356 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1357 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1358 | got.br_startoff - max >= len) |
| 1359 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1360 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1361 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1362 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1363 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1364 | *first_unused = max; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1369 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1370 | * last_block (input value) in the file. |
| 1371 | * This is not based on i_size, it is based on the extent records. |
| 1372 | * Returns 0 for local files, as they do not have extent records. |
| 1373 | */ |
| 1374 | int /* error */ |
| 1375 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1376 | struct xfs_trans *tp, /* transaction pointer */ |
| 1377 | struct xfs_inode *ip, /* incore inode */ |
| 1378 | xfs_fileoff_t *last_block, /* last block */ |
| 1379 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1380 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1381 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1382 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1383 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1384 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1385 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1386 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1387 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1388 | *last_block = 0; |
| 1389 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1390 | case XFS_DINODE_FMT_BTREE: |
| 1391 | case XFS_DINODE_FMT_EXTENTS: |
| 1392 | break; |
| 1393 | default: |
| 1394 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1395 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1396 | |
| 1397 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1398 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1399 | if (error) |
| 1400 | return error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1401 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1402 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1403 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1404 | *last_block = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1405 | return 0; |
| 1406 | } |
| 1407 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1408 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1409 | xfs_bmap_last_extent( |
| 1410 | struct xfs_trans *tp, |
| 1411 | struct xfs_inode *ip, |
| 1412 | int whichfork, |
| 1413 | struct xfs_bmbt_irec *rec, |
| 1414 | int *is_empty) |
| 1415 | { |
| 1416 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1417 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1418 | int error; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1419 | |
| 1420 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1421 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1422 | if (error) |
| 1423 | return error; |
| 1424 | } |
| 1425 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1426 | xfs_iext_last(ifp, &icur); |
| 1427 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1428 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1429 | else |
| 1430 | *is_empty = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * Check the last inode extent to determine whether this allocation will result |
| 1436 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1437 | * blocks at the end of the file which do not start at the previous data block, |
| 1438 | * we will try to align the new blocks at stripe unit boundaries. |
| 1439 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1440 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1441 | * at, or past the EOF. |
| 1442 | */ |
| 1443 | STATIC int |
| 1444 | xfs_bmap_isaeof( |
| 1445 | struct xfs_bmalloca *bma, |
| 1446 | int whichfork) |
| 1447 | { |
| 1448 | struct xfs_bmbt_irec rec; |
| 1449 | int is_empty; |
| 1450 | int error; |
| 1451 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1452 | bma->aeof = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1453 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1454 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1455 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1456 | return error; |
| 1457 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1458 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1459 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1463 | /* |
| 1464 | * Check if we are allocation or past the last extent, or at least into |
| 1465 | * the last delayed allocated extent. |
| 1466 | */ |
| 1467 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1468 | (bma->offset >= rec.br_startoff && |
| 1469 | isnullstartblock(rec.br_startblock)); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1474 | * Returns the file-relative block number of the first block past eof in |
| 1475 | * the file. This is not based on i_size, it is based on the extent records. |
| 1476 | * Returns 0 for local files, as they do not have extent records. |
| 1477 | */ |
| 1478 | int |
| 1479 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1480 | struct xfs_inode *ip, |
| 1481 | xfs_fileoff_t *last_block, |
| 1482 | int whichfork) |
| 1483 | { |
| 1484 | struct xfs_bmbt_irec rec; |
| 1485 | int is_empty; |
| 1486 | int error; |
| 1487 | |
| 1488 | *last_block = 0; |
| 1489 | |
| 1490 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1491 | return 0; |
| 1492 | |
| 1493 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1494 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1495 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1496 | |
| 1497 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1498 | if (error || is_empty) |
| 1499 | return error; |
| 1500 | |
| 1501 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | * Returns whether the selected fork of the inode has exactly one |
| 1507 | * block or not. For the data fork we check this matches di_size, |
| 1508 | * implying the file's range is 0..bsize-1. |
| 1509 | */ |
| 1510 | int /* 1=>1 block, 0=>otherwise */ |
| 1511 | xfs_bmap_one_block( |
| 1512 | xfs_inode_t *ip, /* incore inode */ |
| 1513 | int whichfork) /* data or attr fork */ |
| 1514 | { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1515 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1516 | int rval; /* return value */ |
| 1517 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1518 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1519 | |
| 1520 | #ifndef DEBUG |
| 1521 | if (whichfork == XFS_DATA_FORK) |
| 1522 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1523 | #endif /* !DEBUG */ |
| 1524 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1525 | return 0; |
| 1526 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1527 | return 0; |
| 1528 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1529 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1530 | xfs_iext_first(ifp, &icur); |
| 1531 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1532 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1533 | if (rval && whichfork == XFS_DATA_FORK) |
| 1534 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1535 | return rval; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * Extent tree manipulation functions used during allocation. |
| 1540 | */ |
| 1541 | |
| 1542 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1543 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | */ |
| 1545 | STATIC int /* error */ |
| 1546 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1547 | struct xfs_bmalloca *bma, |
| 1548 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1550 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1553 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1555 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1556 | /* left is 0, right is 1, prev is 2 */ |
| 1557 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1558 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1559 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1560 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1561 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1563 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1564 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1565 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1567 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1568 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1569 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1570 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1571 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1572 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1573 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1574 | ASSERT(!bma->cur || |
| 1575 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1576 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1577 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | #define LEFT r[0] |
| 1580 | #define RIGHT r[1] |
| 1581 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
| 1583 | /* |
| 1584 | * Set up a bunch of variables to make the tests simpler. |
| 1585 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1586 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1588 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1590 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1591 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1592 | da_old = startblockval(PREV.br_startblock); |
| 1593 | da_new = 0; |
| 1594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | /* |
| 1596 | * Set flags determining what part of the previous delayed allocation |
| 1597 | * extent is being replaced by a real allocation. |
| 1598 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1599 | if (PREV.br_startoff == new->br_startoff) |
| 1600 | state |= BMAP_LEFT_FILLING; |
| 1601 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1602 | state |= BMAP_RIGHT_FILLING; |
| 1603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | /* |
| 1605 | * Check and set flags if this segment has a left neighbor. |
| 1606 | * Don't set contiguous if the combined extent would be too large. |
| 1607 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1608 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1609 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1610 | if (isnullstartblock(LEFT.br_startblock)) |
| 1611 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1613 | |
| 1614 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1615 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1616 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1617 | LEFT.br_state == new->br_state && |
| 1618 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1619 | state |= BMAP_LEFT_CONTIG; |
| 1620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | /* |
| 1622 | * Check and set flags if this segment has a right neighbor. |
| 1623 | * Don't set contiguous if the combined extent would be too large. |
| 1624 | * Also check for all-three-contiguous being too large. |
| 1625 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1626 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1627 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1628 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1629 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1631 | |
| 1632 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1633 | new_endoff == RIGHT.br_startoff && |
| 1634 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1635 | new->br_state == RIGHT.br_state && |
| 1636 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1637 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1638 | BMAP_RIGHT_FILLING)) != |
| 1639 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1640 | BMAP_RIGHT_FILLING) || |
| 1641 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1642 | <= MAXEXTLEN)) |
| 1643 | state |= BMAP_RIGHT_CONTIG; |
| 1644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | error = 0; |
| 1646 | /* |
| 1647 | * Switch out based on the FILLING and CONTIG state bits. |
| 1648 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1649 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1650 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1651 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1652 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | /* |
| 1654 | * Filling in all of a previously delayed allocation extent. |
| 1655 | * The left and right neighbors are both contiguous with new. |
| 1656 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1657 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1658 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1659 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1660 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1661 | xfs_iext_prev(ifp, &bma->icur); |
| 1662 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1663 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1664 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1665 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1667 | else { |
| 1668 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1669 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1670 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1672 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1673 | error = xfs_btree_delete(bma->cur, &i); |
| 1674 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1676 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1677 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1678 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1680 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1681 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1682 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | goto done; |
| 1684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | break; |
| 1686 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1687 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | /* |
| 1689 | * Filling in all of a previously delayed allocation extent. |
| 1690 | * The left neighbor is contiguous, the right is not. |
| 1691 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1692 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1693 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1694 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1695 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1696 | xfs_iext_prev(ifp, &bma->icur); |
| 1697 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1698 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1699 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | rval = XFS_ILOG_DEXT; |
| 1701 | else { |
| 1702 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1703 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1704 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1706 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1707 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1708 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | goto done; |
| 1710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | break; |
| 1712 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1713 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | /* |
| 1715 | * Filling in all of a previously delayed allocation extent. |
| 1716 | * The right neighbor is contiguous, the left is not. |
| 1717 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1718 | PREV.br_startblock = new->br_startblock; |
| 1719 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1720 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1721 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1722 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1723 | xfs_iext_prev(ifp, &bma->icur); |
| 1724 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1725 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1726 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | rval = XFS_ILOG_DEXT; |
| 1728 | else { |
| 1729 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1730 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1731 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1733 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1734 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1735 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | goto done; |
| 1737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | break; |
| 1739 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1740 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | /* |
| 1742 | * Filling in all of a previously delayed allocation extent. |
| 1743 | * Neither the left nor right neighbors are contiguous with |
| 1744 | * the new one. |
| 1745 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1746 | PREV.br_startblock = new->br_startblock; |
| 1747 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1748 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1749 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1750 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1751 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1753 | else { |
| 1754 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1755 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1756 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1758 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1759 | error = xfs_btree_insert(bma->cur, &i); |
| 1760 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1762 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | break; |
| 1765 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1766 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | /* |
| 1768 | * Filling in the first part of a previous delayed allocation. |
| 1769 | * The left neighbor is contiguous. |
| 1770 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1771 | old = LEFT; |
| 1772 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1773 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1774 | startblockval(PREV.br_startblock)); |
| 1775 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1776 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1777 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1778 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1779 | PREV.br_startoff += new->br_blockcount; |
| 1780 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1781 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1782 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1783 | xfs_iext_prev(ifp, &bma->icur); |
| 1784 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1785 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1786 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | rval = XFS_ILOG_DEXT; |
| 1788 | else { |
| 1789 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1790 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1791 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1793 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1794 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1795 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | goto done; |
| 1797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | break; |
| 1799 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1800 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | /* |
| 1802 | * Filling in the first part of a previous delayed allocation. |
| 1803 | * The left neighbor is not contiguous. |
| 1804 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1805 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1806 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1807 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1809 | else { |
| 1810 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1811 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1812 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1814 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1815 | error = xfs_btree_insert(bma->cur, &i); |
| 1816 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1818 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1820 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1821 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1822 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1823 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1824 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | rval |= tmp_rval; |
| 1826 | if (error) |
| 1827 | goto done; |
| 1828 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1829 | |
| 1830 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1831 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1832 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1833 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1834 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1835 | PREV.br_startoff = new_endoff; |
| 1836 | PREV.br_blockcount = temp; |
| 1837 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1838 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1839 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1840 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | break; |
| 1842 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1843 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | /* |
| 1845 | * Filling in the last part of a previous delayed allocation. |
| 1846 | * The right neighbor is contiguous with the new allocation. |
| 1847 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1848 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1849 | RIGHT.br_startoff = new->br_startoff; |
| 1850 | RIGHT.br_startblock = new->br_startblock; |
| 1851 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1852 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1853 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | rval = XFS_ILOG_DEXT; |
| 1855 | else { |
| 1856 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1857 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1858 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1860 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1861 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1862 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | goto done; |
| 1864 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1865 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1866 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1867 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1868 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1869 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1870 | PREV.br_blockcount = temp; |
| 1871 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1872 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1873 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1874 | xfs_iext_next(ifp, &bma->icur); |
| 1875 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | break; |
| 1877 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1878 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | /* |
| 1880 | * Filling in the last part of a previous delayed allocation. |
| 1881 | * The right neighbor is not contiguous. |
| 1882 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1883 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1884 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1885 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1887 | else { |
| 1888 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1889 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1890 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1892 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | error = xfs_btree_insert(bma->cur, &i); |
| 1894 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1896 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1898 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1899 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1900 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1901 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1902 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | rval |= tmp_rval; |
| 1904 | if (error) |
| 1905 | goto done; |
| 1906 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1907 | |
| 1908 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1909 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1910 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1911 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1912 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1913 | PREV.br_startblock = nullstartblock(da_new); |
| 1914 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1915 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1916 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | break; |
| 1918 | |
| 1919 | case 0: |
| 1920 | /* |
| 1921 | * Filling in the middle part of a previous delayed allocation. |
| 1922 | * Contiguity is impossible here. |
| 1923 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1924 | * |
| 1925 | * We start with a delayed allocation: |
| 1926 | * |
| 1927 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1928 | * PREV @ idx |
| 1929 | * |
| 1930 | * and we are allocating: |
| 1931 | * +rrrrrrrrrrrrrrrrr+ |
| 1932 | * new |
| 1933 | * |
| 1934 | * and we set it up for insertion as: |
| 1935 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1936 | * new |
| 1937 | * PREV @ idx LEFT RIGHT |
| 1938 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1940 | old = PREV; |
| 1941 | |
| 1942 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1943 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1944 | |
| 1945 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1946 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1947 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1948 | RIGHT.br_blockcount = |
| 1949 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1950 | RIGHT.br_startblock = |
| 1951 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1952 | RIGHT.br_blockcount)); |
| 1953 | |
| 1954 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1955 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1956 | PREV.br_startblock = |
| 1957 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1958 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1959 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1960 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1961 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1962 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1963 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1964 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1965 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1966 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1968 | else { |
| 1969 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1970 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1971 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1973 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1974 | error = xfs_btree_insert(bma->cur, &i); |
| 1975 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1977 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1979 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1980 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1981 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1982 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1983 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | rval |= tmp_rval; |
| 1985 | if (error) |
| 1986 | goto done; |
| 1987 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1988 | |
| 1989 | da_new = startblockval(PREV.br_startblock) + |
| 1990 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | break; |
| 1992 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1993 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1994 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1995 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1996 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1997 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1998 | case BMAP_LEFT_CONTIG: |
| 1999 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | /* |
| 2001 | * These cases are all impossible. |
| 2002 | */ |
| 2003 | ASSERT(0); |
| 2004 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2005 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2006 | /* add reverse mapping */ |
| 2007 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new); |
| 2008 | if (error) |
| 2009 | goto done; |
| 2010 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2011 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2012 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2013 | int tmp_logflags; /* partial log flag return val */ |
| 2014 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2015 | ASSERT(bma->cur == NULL); |
| 2016 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2017 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2018 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2019 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2020 | if (error) |
| 2021 | goto done; |
| 2022 | } |
| 2023 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2024 | if (bma->cur) { |
| 2025 | da_new += bma->cur->bc_private.b.allocated; |
| 2026 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2029 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2030 | if (da_new != da_old) { |
| 2031 | ASSERT(state == 0 || da_new < da_old); |
| 2032 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2033 | false); |
| 2034 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2035 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2036 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2038 | if (whichfork != XFS_COW_FORK) |
| 2039 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | return error; |
| 2041 | #undef LEFT |
| 2042 | #undef RIGHT |
| 2043 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2047 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | */ |
| 2049 | STATIC int /* error */ |
| 2050 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2051 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2053 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2054 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2056 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2057 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2058 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2059 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2064 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2067 | /* left is 0, right is 1, prev is 2 */ |
| 2068 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2069 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2070 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2071 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2073 | *logflagsp = 0; |
| 2074 | |
| 2075 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2076 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2077 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2078 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2079 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2080 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | #define LEFT r[0] |
| 2083 | #define RIGHT r[1] |
| 2084 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | /* |
| 2087 | * Set up a bunch of variables to make the tests simpler. |
| 2088 | */ |
| 2089 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2090 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2091 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2093 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2094 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | /* |
| 2097 | * Set flags determining what part of the previous oldext allocation |
| 2098 | * extent is being replaced by a newext allocation. |
| 2099 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2100 | if (PREV.br_startoff == new->br_startoff) |
| 2101 | state |= BMAP_LEFT_FILLING; |
| 2102 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2103 | state |= BMAP_RIGHT_FILLING; |
| 2104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | /* |
| 2106 | * Check and set flags if this segment has a left neighbor. |
| 2107 | * Don't set contiguous if the combined extent would be too large. |
| 2108 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2109 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2110 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2111 | if (isnullstartblock(LEFT.br_startblock)) |
| 2112 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2114 | |
| 2115 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2116 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2117 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2118 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2119 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2120 | state |= BMAP_LEFT_CONTIG; |
| 2121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | /* |
| 2123 | * Check and set flags if this segment has a right neighbor. |
| 2124 | * Don't set contiguous if the combined extent would be too large. |
| 2125 | * Also check for all-three-contiguous being too large. |
| 2126 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2127 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2128 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2129 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2130 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2132 | |
| 2133 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2134 | new_endoff == RIGHT.br_startoff && |
| 2135 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2136 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2137 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2138 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2139 | BMAP_RIGHT_FILLING)) != |
| 2140 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2141 | BMAP_RIGHT_FILLING) || |
| 2142 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2143 | <= MAXEXTLEN)) |
| 2144 | state |= BMAP_RIGHT_CONTIG; |
| 2145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | /* |
| 2147 | * Switch out based on the FILLING and CONTIG state bits. |
| 2148 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2149 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2150 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2151 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2152 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | /* |
| 2154 | * Setting all of a previous oldext extent to newext. |
| 2155 | * The left and right neighbors are both contiguous with new. |
| 2156 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2157 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2158 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2159 | xfs_iext_remove(ip, icur, state); |
| 2160 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2161 | xfs_iext_prev(ifp, icur); |
| 2162 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2163 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2164 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | if (cur == NULL) |
| 2166 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2167 | else { |
| 2168 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2169 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2170 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2172 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2173 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2175 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2176 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2178 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2179 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2181 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2182 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2184 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2185 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2186 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | goto done; |
| 2188 | } |
| 2189 | break; |
| 2190 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2191 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | /* |
| 2193 | * Setting all of a previous oldext extent to newext. |
| 2194 | * The left neighbor is contiguous, the right is not. |
| 2195 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2196 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2197 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2198 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2199 | xfs_iext_prev(ifp, icur); |
| 2200 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2201 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2202 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | if (cur == NULL) |
| 2204 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2205 | else { |
| 2206 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2207 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2208 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2210 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2211 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2213 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2214 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2216 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2217 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2218 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | goto done; |
| 2220 | } |
| 2221 | break; |
| 2222 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2223 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | /* |
| 2225 | * Setting all of a previous oldext extent to newext. |
| 2226 | * The right neighbor is contiguous, the left is not. |
| 2227 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2228 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2229 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2230 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2231 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2232 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2233 | xfs_iext_prev(ifp, icur); |
| 2234 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2235 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2236 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2237 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | if (cur == NULL) |
| 2239 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2240 | else { |
| 2241 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2242 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2243 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2245 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2246 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2248 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2249 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2251 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2252 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2253 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | goto done; |
| 2255 | } |
| 2256 | break; |
| 2257 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2258 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | /* |
| 2260 | * Setting all of a previous oldext extent to newext. |
| 2261 | * Neither the left nor right neighbors are contiguous with |
| 2262 | * the new one. |
| 2263 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2264 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2265 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | if (cur == NULL) |
| 2268 | rval = XFS_ILOG_DEXT; |
| 2269 | else { |
| 2270 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2271 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2272 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2274 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2275 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2276 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | goto done; |
| 2278 | } |
| 2279 | break; |
| 2280 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2281 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | /* |
| 2283 | * Setting the first part of a previous oldext extent to newext. |
| 2284 | * The left neighbor is contiguous. |
| 2285 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2286 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2287 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2288 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2289 | PREV.br_startoff += new->br_blockcount; |
| 2290 | PREV.br_startblock += new->br_blockcount; |
| 2291 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2292 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2293 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2294 | xfs_iext_prev(ifp, icur); |
| 2295 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | if (cur == NULL) |
| 2298 | rval = XFS_ILOG_DEXT; |
| 2299 | else { |
| 2300 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2301 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2302 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2304 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2305 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2306 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2308 | error = xfs_btree_decrement(cur, 0, &i); |
| 2309 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2311 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2312 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | goto done; |
| 2314 | } |
| 2315 | break; |
| 2316 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2317 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | /* |
| 2319 | * Setting the first part of a previous oldext extent to newext. |
| 2320 | * The left neighbor is not contiguous. |
| 2321 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2322 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2323 | PREV.br_startoff += new->br_blockcount; |
| 2324 | PREV.br_startblock += new->br_blockcount; |
| 2325 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2326 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2327 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2328 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2329 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2330 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | if (cur == NULL) |
| 2332 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2333 | else { |
| 2334 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2335 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2336 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2338 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2339 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2340 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | goto done; |
| 2342 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2343 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2345 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | } |
| 2347 | break; |
| 2348 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2349 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | /* |
| 2351 | * Setting the last part of a previous oldext extent to newext. |
| 2352 | * The right neighbor is contiguous with the new allocation. |
| 2353 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2354 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2355 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2356 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2357 | RIGHT.br_startoff = new->br_startoff; |
| 2358 | RIGHT.br_startblock = new->br_startblock; |
| 2359 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2360 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2361 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2362 | xfs_iext_next(ifp, icur); |
| 2363 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | if (cur == NULL) |
| 2366 | rval = XFS_ILOG_DEXT; |
| 2367 | else { |
| 2368 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2369 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2370 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2372 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2373 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2374 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2376 | error = xfs_btree_increment(cur, 0, &i); |
| 2377 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2379 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2380 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | goto done; |
| 2382 | } |
| 2383 | break; |
| 2384 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2385 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | /* |
| 2387 | * Setting the last part of a previous oldext extent to newext. |
| 2388 | * The right neighbor is not contiguous. |
| 2389 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2390 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2391 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2392 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2393 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2394 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2395 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2396 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2397 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2398 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | if (cur == NULL) |
| 2400 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2401 | else { |
| 2402 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2403 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2404 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2406 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2407 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2408 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2410 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2411 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2413 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2414 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2416 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | } |
| 2418 | break; |
| 2419 | |
| 2420 | case 0: |
| 2421 | /* |
| 2422 | * Setting the middle part of a previous oldext extent to |
| 2423 | * newext. Contiguity is impossible here. |
| 2424 | * One extent becomes three extents. |
| 2425 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2426 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2427 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | r[0] = *new; |
| 2430 | r[1].br_startoff = new_endoff; |
| 2431 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2432 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2434 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2435 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2436 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2437 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2438 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2439 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2440 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2441 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2442 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | if (cur == NULL) |
| 2444 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2445 | else { |
| 2446 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2447 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2448 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2450 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2452 | error = xfs_bmbt_update(cur, &r[1]); |
| 2453 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | goto done; |
| 2455 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2457 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2459 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2460 | /* |
| 2461 | * Reset the cursor to the position of the new extent |
| 2462 | * we are about to insert as we can't trust it after |
| 2463 | * the previous insert. |
| 2464 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2465 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2466 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2468 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2470 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2472 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |
| 2474 | break; |
| 2475 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2476 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2477 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2478 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2479 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2480 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2481 | case BMAP_LEFT_CONTIG: |
| 2482 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | /* |
| 2484 | * These cases are all impossible. |
| 2485 | */ |
| 2486 | ASSERT(0); |
| 2487 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2488 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2489 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2490 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2491 | if (error) |
| 2492 | goto done; |
| 2493 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2494 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2495 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2496 | int tmp_logflags; /* partial log flag return val */ |
| 2497 | |
| 2498 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2499 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2500 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2501 | *logflagsp |= tmp_logflags; |
| 2502 | if (error) |
| 2503 | goto done; |
| 2504 | } |
| 2505 | |
| 2506 | /* clear out the allocated field, done with it now in any case. */ |
| 2507 | if (cur) { |
| 2508 | cur->bc_private.b.allocated = 0; |
| 2509 | *curp = cur; |
| 2510 | } |
| 2511 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2512 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2514 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | return error; |
| 2516 | #undef LEFT |
| 2517 | #undef RIGHT |
| 2518 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2522 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2524 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | xfs_bmap_add_extent_hole_delay( |
| 2526 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2527 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2528 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2529 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2531 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2533 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2534 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2535 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2536 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2537 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2539 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2540 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | /* |
| 2543 | * Check and set flags if this segment has a left neighbor |
| 2544 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2545 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2546 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2547 | if (isnullstartblock(left.br_startblock)) |
| 2548 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | /* |
| 2552 | * Check and set flags if the current (right) segment exists. |
| 2553 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2554 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2555 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2556 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2557 | if (isnullstartblock(right.br_startblock)) |
| 2558 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | /* |
| 2562 | * Set contiguity flags on the left and right neighbors. |
| 2563 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2564 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2565 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2566 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2567 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2568 | state |= BMAP_LEFT_CONTIG; |
| 2569 | |
| 2570 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2571 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2572 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2573 | (!(state & BMAP_LEFT_CONTIG) || |
| 2574 | (left.br_blockcount + new->br_blockcount + |
| 2575 | right.br_blockcount <= MAXEXTLEN))) |
| 2576 | state |= BMAP_RIGHT_CONTIG; |
| 2577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | /* |
| 2579 | * Switch out based on the contiguity flags. |
| 2580 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2581 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2582 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | /* |
| 2584 | * New allocation is contiguous with delayed allocations |
| 2585 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2586 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | */ |
| 2588 | temp = left.br_blockcount + new->br_blockcount + |
| 2589 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2590 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2591 | oldlen = startblockval(left.br_startblock) + |
| 2592 | startblockval(new->br_startblock) + |
| 2593 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2594 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2595 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2596 | left.br_startblock = nullstartblock(newlen); |
| 2597 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2598 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2599 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2600 | xfs_iext_prev(ifp, icur); |
| 2601 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | break; |
| 2603 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2604 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | /* |
| 2606 | * New allocation is contiguous with a delayed allocation |
| 2607 | * on the left. |
| 2608 | * Merge the new allocation with the left neighbor. |
| 2609 | */ |
| 2610 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2611 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2612 | oldlen = startblockval(left.br_startblock) + |
| 2613 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2614 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2615 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2616 | left.br_blockcount = temp; |
| 2617 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2618 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2619 | xfs_iext_prev(ifp, icur); |
| 2620 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | break; |
| 2622 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2623 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | /* |
| 2625 | * New allocation is contiguous with a delayed allocation |
| 2626 | * on the right. |
| 2627 | * Merge the new allocation with the right neighbor. |
| 2628 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2630 | oldlen = startblockval(new->br_startblock) + |
| 2631 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2632 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2633 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2634 | right.br_startoff = new->br_startoff; |
| 2635 | right.br_startblock = nullstartblock(newlen); |
| 2636 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2637 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | break; |
| 2639 | |
| 2640 | case 0: |
| 2641 | /* |
| 2642 | * New allocation is not contiguous with another |
| 2643 | * delayed allocation. |
| 2644 | * Insert a new entry. |
| 2645 | */ |
| 2646 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2647 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | break; |
| 2649 | } |
| 2650 | if (oldlen != newlen) { |
| 2651 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2652 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2653 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | /* |
| 2655 | * Nothing to do for disk quota accounting here. |
| 2656 | */ |
| 2657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2661 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | */ |
| 2663 | STATIC int /* error */ |
| 2664 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2665 | struct xfs_trans *tp, |
| 2666 | struct xfs_inode *ip, |
| 2667 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2668 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2669 | struct xfs_btree_cur **curp, |
| 2670 | struct xfs_bmbt_irec *new, |
| 2671 | xfs_fsblock_t *first, |
| 2672 | struct xfs_defer_ops *dfops, |
| 2673 | int *logflagsp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2675 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2676 | struct xfs_mount *mp = ip->i_mount; |
| 2677 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2681 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2682 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2683 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2684 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2686 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2687 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2688 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2689 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | /* |
| 2692 | * Check and set flags if this segment has a left neighbor. |
| 2693 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2694 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2695 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2696 | if (isnullstartblock(left.br_startblock)) |
| 2697 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | /* |
| 2701 | * Check and set flags if this segment has a current value. |
| 2702 | * Not true if we're inserting into the "hole" at eof. |
| 2703 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2704 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2705 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2706 | if (isnullstartblock(right.br_startblock)) |
| 2707 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | /* |
| 2711 | * We're inserting a real allocation between "left" and "right". |
| 2712 | * Set the contiguity flags. Don't let extents get too large. |
| 2713 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2714 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2715 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2716 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2717 | left.br_state == new->br_state && |
| 2718 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2719 | state |= BMAP_LEFT_CONTIG; |
| 2720 | |
| 2721 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2722 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2723 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2724 | new->br_state == right.br_state && |
| 2725 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2726 | (!(state & BMAP_LEFT_CONTIG) || |
| 2727 | left.br_blockcount + new->br_blockcount + |
| 2728 | right.br_blockcount <= MAXEXTLEN)) |
| 2729 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2731 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | /* |
| 2733 | * Select which case we're in here, and implement it. |
| 2734 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2735 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2736 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | /* |
| 2738 | * New allocation is contiguous with real allocations on the |
| 2739 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2740 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2742 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2743 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2744 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2745 | xfs_iext_prev(ifp, icur); |
| 2746 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2747 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2748 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2749 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2750 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2751 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2752 | } else { |
| 2753 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2754 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2755 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2756 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2757 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2758 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2759 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2760 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2761 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2762 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2763 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2764 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2765 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2766 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2767 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2768 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2770 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2772 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | /* |
| 2774 | * New allocation is contiguous with a real allocation |
| 2775 | * on the left. |
| 2776 | * Merge the new allocation with the left neighbor. |
| 2777 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2778 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2779 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2780 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2781 | xfs_iext_prev(ifp, icur); |
| 2782 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2783 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2784 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2785 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2786 | } else { |
| 2787 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2788 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2789 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2790 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2791 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2792 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2793 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2794 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2796 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2798 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | /* |
| 2800 | * New allocation is contiguous with a real allocation |
| 2801 | * on the right. |
| 2802 | * Merge the new allocation with the right neighbor. |
| 2803 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2804 | old = right; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2805 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2806 | right.br_startoff = new->br_startoff; |
| 2807 | right.br_startblock = new->br_startblock; |
| 2808 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2809 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2810 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2811 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2812 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2813 | } else { |
| 2814 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2815 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2816 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2817 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2818 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2819 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2820 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2821 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2823 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | |
| 2825 | case 0: |
| 2826 | /* |
| 2827 | * New allocation is not contiguous with another |
| 2828 | * real allocation. |
| 2829 | * Insert a new entry. |
| 2830 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2831 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2832 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2833 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2834 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2835 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2836 | } else { |
| 2837 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2838 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2839 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2840 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2841 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2842 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2843 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2844 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2845 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2847 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2849 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2850 | /* add reverse mapping */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2851 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2852 | if (error) |
| 2853 | goto done; |
| 2854 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2855 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2856 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2857 | int tmp_logflags; /* partial log flag return val */ |
| 2858 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2859 | ASSERT(cur == NULL); |
| 2860 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2861 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2862 | *logflagsp |= tmp_logflags; |
| 2863 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2864 | if (error) |
| 2865 | goto done; |
| 2866 | } |
| 2867 | |
| 2868 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2869 | if (cur) |
| 2870 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2871 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2872 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2873 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2874 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2875 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2878 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2879 | * Functions used in the extent read, allocate and remove paths |
| 2880 | */ |
| 2881 | |
| 2882 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2883 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2884 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2885 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2886 | xfs_bmap_extsize_align( |
| 2887 | xfs_mount_t *mp, |
| 2888 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2889 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2890 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2891 | int rt, /* is this a realtime inode? */ |
| 2892 | int eof, /* is extent at end-of-file? */ |
| 2893 | int delay, /* creating delalloc extent? */ |
| 2894 | int convert, /* overwriting unwritten extent? */ |
| 2895 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2896 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2897 | { |
| 2898 | xfs_fileoff_t orig_off; /* original offset */ |
| 2899 | xfs_extlen_t orig_alen; /* original length */ |
| 2900 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2901 | xfs_fileoff_t nexto; /* next file offset */ |
| 2902 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2903 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2904 | xfs_extlen_t align_alen; /* temp for length */ |
| 2905 | xfs_extlen_t temp; /* temp for calculations */ |
| 2906 | |
| 2907 | if (convert) |
| 2908 | return 0; |
| 2909 | |
| 2910 | orig_off = align_off = *offp; |
| 2911 | orig_alen = align_alen = *lenp; |
| 2912 | orig_end = orig_off + orig_alen; |
| 2913 | |
| 2914 | /* |
| 2915 | * If this request overlaps an existing extent, then don't |
| 2916 | * attempt to perform any additional alignment. |
| 2917 | */ |
| 2918 | if (!delay && !eof && |
| 2919 | (orig_off >= gotp->br_startoff) && |
| 2920 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2921 | return 0; |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * If the file offset is unaligned vs. the extent size |
| 2926 | * we need to align it. This will be possible unless |
| 2927 | * the file was previously written with a kernel that didn't |
| 2928 | * perform this alignment, or if a truncate shot us in the |
| 2929 | * foot. |
| 2930 | */ |
| 2931 | temp = do_mod(orig_off, extsz); |
| 2932 | if (temp) { |
| 2933 | align_alen += temp; |
| 2934 | align_off -= temp; |
| 2935 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2936 | |
| 2937 | /* Same adjustment for the end of the requested area. */ |
| 2938 | temp = (align_alen % extsz); |
| 2939 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2940 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2941 | |
| 2942 | /* |
| 2943 | * For large extent hint sizes, the aligned extent might be larger than |
| 2944 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2945 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2946 | * short allocation just fine, so it is safe to do this. We only want to |
| 2947 | * do it when we are forced to, though, because it means more allocation |
| 2948 | * operations are required. |
| 2949 | */ |
| 2950 | while (align_alen > MAXEXTLEN) |
| 2951 | align_alen -= extsz; |
| 2952 | ASSERT(align_alen <= MAXEXTLEN); |
| 2953 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2954 | /* |
| 2955 | * If the previous block overlaps with this proposed allocation |
| 2956 | * then move the start forward without adjusting the length. |
| 2957 | */ |
| 2958 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2959 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2960 | prevo = prevp->br_startoff; |
| 2961 | else |
| 2962 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2963 | } else |
| 2964 | prevo = 0; |
| 2965 | if (align_off != orig_off && align_off < prevo) |
| 2966 | align_off = prevo; |
| 2967 | /* |
| 2968 | * If the next block overlaps with this proposed allocation |
| 2969 | * then move the start back without adjusting the length, |
| 2970 | * but not before offset 0. |
| 2971 | * This may of course make the start overlap previous block, |
| 2972 | * and if we hit the offset 0 limit then the next block |
| 2973 | * can still overlap too. |
| 2974 | */ |
| 2975 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2976 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2977 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2978 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2979 | else |
| 2980 | nexto = gotp->br_startoff; |
| 2981 | } else |
| 2982 | nexto = NULLFILEOFF; |
| 2983 | if (!eof && |
| 2984 | align_off + align_alen != orig_end && |
| 2985 | align_off + align_alen > nexto) |
| 2986 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2987 | /* |
| 2988 | * If we're now overlapping the next or previous extent that |
| 2989 | * means we can't fit an extsz piece in this hole. Just move |
| 2990 | * the start forward to the first valid spot and set |
| 2991 | * the length so we hit the end. |
| 2992 | */ |
| 2993 | if (align_off != orig_off && align_off < prevo) |
| 2994 | align_off = prevo; |
| 2995 | if (align_off + align_alen != orig_end && |
| 2996 | align_off + align_alen > nexto && |
| 2997 | nexto != NULLFILEOFF) { |
| 2998 | ASSERT(nexto > prevo); |
| 2999 | align_alen = nexto - align_off; |
| 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | * If realtime, and the result isn't a multiple of the realtime |
| 3004 | * extent size we need to remove blocks until it is. |
| 3005 | */ |
| 3006 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3007 | /* |
| 3008 | * We're not covering the original request, or |
| 3009 | * we won't be able to once we fix the length. |
| 3010 | */ |
| 3011 | if (orig_off < align_off || |
| 3012 | orig_end > align_off + align_alen || |
| 3013 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3014 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3015 | /* |
| 3016 | * Try to fix it by moving the start up. |
| 3017 | */ |
| 3018 | if (align_off + temp <= orig_off) { |
| 3019 | align_alen -= temp; |
| 3020 | align_off += temp; |
| 3021 | } |
| 3022 | /* |
| 3023 | * Try to fix it by moving the end in. |
| 3024 | */ |
| 3025 | else if (align_off + align_alen - temp >= orig_end) |
| 3026 | align_alen -= temp; |
| 3027 | /* |
| 3028 | * Set the start to the minimum then trim the length. |
| 3029 | */ |
| 3030 | else { |
| 3031 | align_alen -= orig_off - align_off; |
| 3032 | align_off = orig_off; |
| 3033 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3034 | } |
| 3035 | /* |
| 3036 | * Result doesn't cover the request, fail it. |
| 3037 | */ |
| 3038 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3039 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3040 | } else { |
| 3041 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3042 | /* see MAXEXTLEN handling above */ |
| 3043 | ASSERT(orig_end <= align_off + align_alen || |
| 3044 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | #ifdef DEBUG |
| 3048 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3049 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3050 | if (prevp->br_startoff != NULLFILEOFF) |
| 3051 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3052 | #endif |
| 3053 | |
| 3054 | *lenp = align_alen; |
| 3055 | *offp = align_off; |
| 3056 | return 0; |
| 3057 | } |
| 3058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3060 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3061 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3062 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3063 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | { |
| 3065 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3067 | xfs_mount_t *mp; /* mount point structure */ |
| 3068 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3069 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | |
| 3071 | #define ISVALID(x,y) \ |
| 3072 | (rt ? \ |
| 3073 | (x) < mp->m_sb.sb_rblocks : \ |
| 3074 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3075 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3076 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3079 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3080 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3081 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3082 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | /* |
| 3084 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3085 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3087 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3088 | !isnullstartblock(ap->prev.br_startblock) && |
| 3089 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3090 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3091 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | /* |
| 3093 | * Adjust for the gap between prevp and us. |
| 3094 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3095 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3096 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3098 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3099 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | } |
| 3101 | /* |
| 3102 | * If not at eof, then compare the two neighbor blocks. |
| 3103 | * Figure out whether either one gives us a good starting point, |
| 3104 | * and pick the better one. |
| 3105 | */ |
| 3106 | else if (!ap->eof) { |
| 3107 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3108 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3109 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3110 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3111 | |
| 3112 | /* |
| 3113 | * If there's a previous (left) block, select a requested |
| 3114 | * start block based on it. |
| 3115 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3116 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3117 | !isnullstartblock(ap->prev.br_startblock) && |
| 3118 | (prevbno = ap->prev.br_startblock + |
| 3119 | ap->prev.br_blockcount) && |
| 3120 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | /* |
| 3122 | * Calculate gap to end of previous block. |
| 3123 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3124 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3125 | (ap->prev.br_startoff + |
| 3126 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | /* |
| 3128 | * Figure the startblock based on the previous block's |
| 3129 | * end and the gap size. |
| 3130 | * Heuristic! |
| 3131 | * If the gap is large relative to the piece we're |
| 3132 | * allocating, or using it gives us an invalid block |
| 3133 | * number, then just use the end of the previous block. |
| 3134 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3135 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3137 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | prevbno += adjust; |
| 3139 | else |
| 3140 | prevdiff += adjust; |
| 3141 | /* |
| 3142 | * If the firstblock forbids it, can't use it, |
| 3143 | * must use default. |
| 3144 | */ |
| 3145 | if (!rt && !nullfb && |
| 3146 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3147 | prevbno = NULLFSBLOCK; |
| 3148 | } |
| 3149 | /* |
| 3150 | * No previous block or can't follow it, just default. |
| 3151 | */ |
| 3152 | else |
| 3153 | prevbno = NULLFSBLOCK; |
| 3154 | /* |
| 3155 | * If there's a following (right) block, select a requested |
| 3156 | * start block based on it. |
| 3157 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3158 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | /* |
| 3160 | * Calculate gap to start of next block. |
| 3161 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3162 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | /* |
| 3164 | * Figure the startblock based on the next block's |
| 3165 | * start and the gap size. |
| 3166 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3167 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | /* |
| 3169 | * Heuristic! |
| 3170 | * If the gap is large relative to the piece we're |
| 3171 | * allocating, or using it gives us an invalid block |
| 3172 | * number, then just use the start of the next block |
| 3173 | * offset by our length. |
| 3174 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3175 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3177 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3178 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3179 | gotbno -= ap->length; |
| 3180 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | } else |
| 3182 | gotdiff += adjust; |
| 3183 | /* |
| 3184 | * If the firstblock forbids it, can't use it, |
| 3185 | * must use default. |
| 3186 | */ |
| 3187 | if (!rt && !nullfb && |
| 3188 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3189 | gotbno = NULLFSBLOCK; |
| 3190 | } |
| 3191 | /* |
| 3192 | * No next block, just default. |
| 3193 | */ |
| 3194 | else |
| 3195 | gotbno = NULLFSBLOCK; |
| 3196 | /* |
| 3197 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3198 | * 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] | 3199 | */ |
| 3200 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3201 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3203 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3205 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3207 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3208 | } |
| 3209 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3210 | static int |
| 3211 | xfs_bmap_longest_free_extent( |
| 3212 | struct xfs_trans *tp, |
| 3213 | xfs_agnumber_t ag, |
| 3214 | xfs_extlen_t *blen, |
| 3215 | int *notinit) |
| 3216 | { |
| 3217 | struct xfs_mount *mp = tp->t_mountp; |
| 3218 | struct xfs_perag *pag; |
| 3219 | xfs_extlen_t longest; |
| 3220 | int error = 0; |
| 3221 | |
| 3222 | pag = xfs_perag_get(mp, ag); |
| 3223 | if (!pag->pagf_init) { |
| 3224 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3225 | if (error) |
| 3226 | goto out; |
| 3227 | |
| 3228 | if (!pag->pagf_init) { |
| 3229 | *notinit = 1; |
| 3230 | goto out; |
| 3231 | } |
| 3232 | } |
| 3233 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3234 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3235 | xfs_alloc_min_freelist(mp, pag), |
| 3236 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3237 | if (*blen < longest) |
| 3238 | *blen = longest; |
| 3239 | |
| 3240 | out: |
| 3241 | xfs_perag_put(pag); |
| 3242 | return error; |
| 3243 | } |
| 3244 | |
| 3245 | static void |
| 3246 | xfs_bmap_select_minlen( |
| 3247 | struct xfs_bmalloca *ap, |
| 3248 | struct xfs_alloc_arg *args, |
| 3249 | xfs_extlen_t *blen, |
| 3250 | int notinit) |
| 3251 | { |
| 3252 | if (notinit || *blen < ap->minlen) { |
| 3253 | /* |
| 3254 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3255 | * there is space for this request. |
| 3256 | */ |
| 3257 | args->minlen = ap->minlen; |
| 3258 | } else if (*blen < args->maxlen) { |
| 3259 | /* |
| 3260 | * If the best seen length is less than the request length, |
| 3261 | * use the best as the minimum. |
| 3262 | */ |
| 3263 | args->minlen = *blen; |
| 3264 | } else { |
| 3265 | /* |
| 3266 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3267 | * as the minimum. |
| 3268 | */ |
| 3269 | args->minlen = args->maxlen; |
| 3270 | } |
| 3271 | } |
| 3272 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3273 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3274 | xfs_bmap_btalloc_nullfb( |
| 3275 | struct xfs_bmalloca *ap, |
| 3276 | struct xfs_alloc_arg *args, |
| 3277 | xfs_extlen_t *blen) |
| 3278 | { |
| 3279 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3280 | xfs_agnumber_t ag, startag; |
| 3281 | int notinit = 0; |
| 3282 | int error; |
| 3283 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3284 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3285 | args->total = ap->total; |
| 3286 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3287 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3288 | if (startag == NULLAGNUMBER) |
| 3289 | startag = ag = 0; |
| 3290 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3291 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3292 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3293 | ¬init); |
| 3294 | if (error) |
| 3295 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3296 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3297 | if (++ag == mp->m_sb.sb_agcount) |
| 3298 | ag = 0; |
| 3299 | if (ag == startag) |
| 3300 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3301 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3302 | |
| 3303 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3304 | return 0; |
| 3305 | } |
| 3306 | |
| 3307 | STATIC int |
| 3308 | xfs_bmap_btalloc_filestreams( |
| 3309 | struct xfs_bmalloca *ap, |
| 3310 | struct xfs_alloc_arg *args, |
| 3311 | xfs_extlen_t *blen) |
| 3312 | { |
| 3313 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3314 | xfs_agnumber_t ag; |
| 3315 | int notinit = 0; |
| 3316 | int error; |
| 3317 | |
| 3318 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3319 | args->total = ap->total; |
| 3320 | |
| 3321 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3322 | if (ag == NULLAGNUMBER) |
| 3323 | ag = 0; |
| 3324 | |
| 3325 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3326 | if (error) |
| 3327 | return error; |
| 3328 | |
| 3329 | if (*blen < args->maxlen) { |
| 3330 | error = xfs_filestream_new_ag(ap, &ag); |
| 3331 | if (error) |
| 3332 | return error; |
| 3333 | |
| 3334 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3335 | ¬init); |
| 3336 | if (error) |
| 3337 | return error; |
| 3338 | |
| 3339 | } |
| 3340 | |
| 3341 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3342 | |
| 3343 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3344 | * Set the failure fallback case to look in the selected AG as stream |
| 3345 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3346 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3347 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3348 | return 0; |
| 3349 | } |
| 3350 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3351 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3352 | static void |
| 3353 | xfs_bmap_btalloc_accounting( |
| 3354 | struct xfs_bmalloca *ap, |
| 3355 | struct xfs_alloc_arg *args) |
| 3356 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3357 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3358 | /* |
| 3359 | * COW fork blocks are in-core only and thus are treated as |
| 3360 | * in-core quota reservation (like delalloc blocks) even when |
| 3361 | * converted to real blocks. The quota reservation is not |
| 3362 | * accounted to disk until blocks are remapped to the data |
| 3363 | * fork. So if these blocks were previously delalloc, we |
| 3364 | * already have quota reservation and there's nothing to do |
| 3365 | * yet. |
| 3366 | */ |
| 3367 | if (ap->wasdel) |
| 3368 | return; |
| 3369 | |
| 3370 | /* |
| 3371 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3372 | * has acquired in-core quota reservation for this extent. |
| 3373 | * Rather than account these as real blocks, however, we reduce |
| 3374 | * the transaction quota reservation based on the allocation. |
| 3375 | * This essentially transfers the transaction quota reservation |
| 3376 | * to that of a delalloc extent. |
| 3377 | */ |
| 3378 | ap->ip->i_delayed_blks += args->len; |
| 3379 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3380 | -(long)args->len); |
| 3381 | return; |
| 3382 | } |
| 3383 | |
| 3384 | /* data/attr fork only */ |
| 3385 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3386 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3387 | if (ap->wasdel) |
| 3388 | ap->ip->i_delayed_blks -= args->len; |
| 3389 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3390 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3391 | args->len); |
| 3392 | } |
| 3393 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3394 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3395 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3396 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3397 | { |
| 3398 | xfs_mount_t *mp; /* mount point structure */ |
| 3399 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3400 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3401 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3402 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3403 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3404 | xfs_fileoff_t orig_offset; |
| 3405 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3406 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3407 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3408 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3409 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3410 | int tryagain; |
| 3411 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3412 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3413 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3414 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3415 | orig_offset = ap->offset; |
| 3416 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3417 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3419 | |
| 3420 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3421 | stripe_align = 0; |
| 3422 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3423 | stripe_align = mp->m_swidth; |
| 3424 | else if (mp->m_dalign) |
| 3425 | stripe_align = mp->m_dalign; |
| 3426 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3427 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3428 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3429 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3430 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3431 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3432 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3433 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3434 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3435 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3436 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3437 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3438 | |
| 3439 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3440 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3441 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3442 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3443 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3444 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3445 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3446 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3447 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3448 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3449 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3450 | } |
| 3451 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3452 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3453 | |
| 3454 | xfs_bmap_adjacent(ap); |
| 3455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3457 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | * it's in the right allocation group. |
| 3459 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3460 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | ; |
| 3462 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3463 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | * Normal allocation, done through xfs_alloc_vextent. |
| 3466 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3467 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3468 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3469 | args.tp = ap->tp; |
| 3470 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3471 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3472 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3473 | |
| 3474 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3475 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3476 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3477 | blen = 0; |
| 3478 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3479 | /* |
| 3480 | * Search for an allocation group with a single extent large |
| 3481 | * enough for the request. If one isn't found, then adjust |
| 3482 | * the minimum allocation size to the largest space found. |
| 3483 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3484 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3485 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3486 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3487 | else |
| 3488 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3489 | if (error) |
| 3490 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3491 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3492 | if (xfs_inode_is_filestream(ap->ip)) |
| 3493 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3494 | else |
| 3495 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3496 | args.total = args.minlen = ap->minlen; |
| 3497 | } else { |
| 3498 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3499 | args.total = ap->total; |
| 3500 | args.minlen = ap->minlen; |
| 3501 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3502 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3503 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3504 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3505 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3506 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3507 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3508 | args.prod = 1; |
| 3509 | args.mod = 0; |
| 3510 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3511 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3512 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3513 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3514 | } |
| 3515 | /* |
| 3516 | * If we are not low on available data blocks, and the |
| 3517 | * underlying logical volume manager is a stripe, and |
| 3518 | * the file offset is zero then try to allocate data |
| 3519 | * blocks on stripe unit boundary. |
| 3520 | * NOTE: ap->aeof is only set if the allocation length |
| 3521 | * is >= the stripe unit and the allocation offset is |
| 3522 | * at the end of file. |
| 3523 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3524 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3525 | if (!ap->offset) { |
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 | atype = args.type; |
| 3528 | isaligned = 1; |
| 3529 | /* |
| 3530 | * Adjust for alignment |
| 3531 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3532 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3533 | args.minlen = blen - args.alignment; |
| 3534 | args.minalignslop = 0; |
| 3535 | } else { |
| 3536 | /* |
| 3537 | * First try an exact bno allocation. |
| 3538 | * If it fails then do a near or start bno |
| 3539 | * allocation with alignment turned on. |
| 3540 | */ |
| 3541 | atype = args.type; |
| 3542 | tryagain = 1; |
| 3543 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3544 | args.alignment = 1; |
| 3545 | /* |
| 3546 | * Compute the minlen+alignment for the |
| 3547 | * next case. Set slop so that the value |
| 3548 | * of minlen+alignment+slop doesn't go up |
| 3549 | * between the calls. |
| 3550 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3551 | if (blen > stripe_align && blen <= args.maxlen) |
| 3552 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3553 | else |
| 3554 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3555 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3556 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3557 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3558 | args.minlen - 1; |
| 3559 | else |
| 3560 | args.minalignslop = 0; |
| 3561 | } |
| 3562 | } else { |
| 3563 | args.alignment = 1; |
| 3564 | args.minalignslop = 0; |
| 3565 | } |
| 3566 | args.minleft = ap->minleft; |
| 3567 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3568 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3569 | args.datatype = ap->datatype; |
| 3570 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3571 | args.ip = ap->ip; |
| 3572 | |
| 3573 | error = xfs_alloc_vextent(&args); |
| 3574 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3575 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3576 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3577 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3578 | /* |
| 3579 | * Exact allocation failed. Now try with alignment |
| 3580 | * turned on. |
| 3581 | */ |
| 3582 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3583 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3584 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3585 | args.minlen = nextminlen; |
| 3586 | args.minalignslop = 0; |
| 3587 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | if ((error = xfs_alloc_vextent(&args))) |
| 3589 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3590 | } |
| 3591 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3592 | /* |
| 3593 | * allocation failed, so turn off alignment and |
| 3594 | * try again. |
| 3595 | */ |
| 3596 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3597 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3598 | args.alignment = 0; |
| 3599 | if ((error = xfs_alloc_vextent(&args))) |
| 3600 | return error; |
| 3601 | } |
| 3602 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3603 | args.minlen > ap->minlen) { |
| 3604 | args.minlen = ap->minlen; |
| 3605 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3606 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3607 | if ((error = xfs_alloc_vextent(&args))) |
| 3608 | return error; |
| 3609 | } |
| 3610 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3611 | args.fsbno = 0; |
| 3612 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3613 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3614 | if ((error = xfs_alloc_vextent(&args))) |
| 3615 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3616 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3617 | } |
| 3618 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3619 | /* |
| 3620 | * check the allocation happened at the same or higher AG than |
| 3621 | * the first block that was allocated. |
| 3622 | */ |
| 3623 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3624 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3625 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3626 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3627 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3628 | if (*ap->firstblock == NULLFSBLOCK) |
| 3629 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3630 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3631 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3632 | /* |
| 3633 | * If the extent size hint is active, we tried to round the |
| 3634 | * caller's allocation request offset down to extsz and the |
| 3635 | * length up to another extsz boundary. If we found a free |
| 3636 | * extent we mapped it in starting at this new offset. If the |
| 3637 | * newly mapped space isn't long enough to cover any of the |
| 3638 | * range of offsets that was originally requested, move the |
| 3639 | * mapping up so that we can fill as much of the caller's |
| 3640 | * original request as possible. Free space is apparently |
| 3641 | * very fragmented so we're unlikely to be able to satisfy the |
| 3642 | * hints anyway. |
| 3643 | */ |
| 3644 | if (ap->length <= orig_length) |
| 3645 | ap->offset = orig_offset; |
| 3646 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3647 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3648 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3649 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3650 | ap->blkno = NULLFSBLOCK; |
| 3651 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | } |
| 3653 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /* |
| 3657 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3658 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3659 | */ |
| 3660 | STATIC int |
| 3661 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3662 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3663 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3664 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3665 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3666 | return xfs_bmap_rtalloc(ap); |
| 3667 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3670 | /* Trim extent to fit a logical block range. */ |
| 3671 | void |
| 3672 | xfs_trim_extent( |
| 3673 | struct xfs_bmbt_irec *irec, |
| 3674 | xfs_fileoff_t bno, |
| 3675 | xfs_filblks_t len) |
| 3676 | { |
| 3677 | xfs_fileoff_t distance; |
| 3678 | xfs_fileoff_t end = bno + len; |
| 3679 | |
| 3680 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3681 | irec->br_startoff >= end) { |
| 3682 | irec->br_blockcount = 0; |
| 3683 | return; |
| 3684 | } |
| 3685 | |
| 3686 | if (irec->br_startoff < bno) { |
| 3687 | distance = bno - irec->br_startoff; |
| 3688 | if (isnullstartblock(irec->br_startblock)) |
| 3689 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3690 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3691 | irec->br_startblock != HOLESTARTBLOCK) |
| 3692 | irec->br_startblock += distance; |
| 3693 | irec->br_startoff += distance; |
| 3694 | irec->br_blockcount -= distance; |
| 3695 | } |
| 3696 | |
| 3697 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3698 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3699 | irec->br_blockcount -= distance; |
| 3700 | } |
| 3701 | } |
| 3702 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3703 | /* trim extent to within eof */ |
| 3704 | void |
| 3705 | xfs_trim_extent_eof( |
| 3706 | struct xfs_bmbt_irec *irec, |
| 3707 | struct xfs_inode *ip) |
| 3708 | |
| 3709 | { |
| 3710 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3711 | i_size_read(VFS_I(ip)))); |
| 3712 | } |
| 3713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3715 | * Trim the returned map to the required bounds |
| 3716 | */ |
| 3717 | STATIC void |
| 3718 | xfs_bmapi_trim_map( |
| 3719 | struct xfs_bmbt_irec *mval, |
| 3720 | struct xfs_bmbt_irec *got, |
| 3721 | xfs_fileoff_t *bno, |
| 3722 | xfs_filblks_t len, |
| 3723 | xfs_fileoff_t obno, |
| 3724 | xfs_fileoff_t end, |
| 3725 | int n, |
| 3726 | int flags) |
| 3727 | { |
| 3728 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3729 | got->br_startoff + got->br_blockcount <= obno) { |
| 3730 | *mval = *got; |
| 3731 | if (isnullstartblock(got->br_startblock)) |
| 3732 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3733 | return; |
| 3734 | } |
| 3735 | |
| 3736 | if (obno > *bno) |
| 3737 | *bno = obno; |
| 3738 | ASSERT((*bno >= obno) || (n == 0)); |
| 3739 | ASSERT(*bno < end); |
| 3740 | mval->br_startoff = *bno; |
| 3741 | if (isnullstartblock(got->br_startblock)) |
| 3742 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3743 | else |
| 3744 | mval->br_startblock = got->br_startblock + |
| 3745 | (*bno - got->br_startoff); |
| 3746 | /* |
| 3747 | * Return the minimum of what we got and what we asked for for |
| 3748 | * the length. We can use the len variable here because it is |
| 3749 | * modified below and we could have been there before coming |
| 3750 | * here if the first part of the allocation didn't overlap what |
| 3751 | * was asked for. |
| 3752 | */ |
| 3753 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3754 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3755 | mval->br_state = got->br_state; |
| 3756 | ASSERT(mval->br_blockcount <= len); |
| 3757 | return; |
| 3758 | } |
| 3759 | |
| 3760 | /* |
| 3761 | * Update and validate the extent map to return |
| 3762 | */ |
| 3763 | STATIC void |
| 3764 | xfs_bmapi_update_map( |
| 3765 | struct xfs_bmbt_irec **map, |
| 3766 | xfs_fileoff_t *bno, |
| 3767 | xfs_filblks_t *len, |
| 3768 | xfs_fileoff_t obno, |
| 3769 | xfs_fileoff_t end, |
| 3770 | int *n, |
| 3771 | int flags) |
| 3772 | { |
| 3773 | xfs_bmbt_irec_t *mval = *map; |
| 3774 | |
| 3775 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3776 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3777 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3778 | (mval->br_startoff < obno)); |
| 3779 | |
| 3780 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3781 | *len = end - *bno; |
| 3782 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3783 | /* update previous map with new information */ |
| 3784 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3785 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3786 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3787 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3788 | mval[-1].br_state = mval->br_state; |
| 3789 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3790 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3791 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3792 | mval->br_startblock == mval[-1].br_startblock + |
| 3793 | mval[-1].br_blockcount && |
| 3794 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3795 | mval[-1].br_state == mval->br_state)) { |
| 3796 | ASSERT(mval->br_startoff == |
| 3797 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3798 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3799 | } else if (*n > 0 && |
| 3800 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3801 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3802 | mval->br_startoff == |
| 3803 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3804 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3805 | mval[-1].br_state = mval->br_state; |
| 3806 | } else if (!((*n == 0) && |
| 3807 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3808 | obno))) { |
| 3809 | mval++; |
| 3810 | (*n)++; |
| 3811 | } |
| 3812 | *map = mval; |
| 3813 | } |
| 3814 | |
| 3815 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3816 | * Map file blocks to filesystem blocks without allocation. |
| 3817 | */ |
| 3818 | int |
| 3819 | xfs_bmapi_read( |
| 3820 | struct xfs_inode *ip, |
| 3821 | xfs_fileoff_t bno, |
| 3822 | xfs_filblks_t len, |
| 3823 | struct xfs_bmbt_irec *mval, |
| 3824 | int *nmap, |
| 3825 | int flags) |
| 3826 | { |
| 3827 | struct xfs_mount *mp = ip->i_mount; |
| 3828 | struct xfs_ifork *ifp; |
| 3829 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3830 | xfs_fileoff_t obno; |
| 3831 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3832 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3833 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3834 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3835 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3836 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3837 | |
| 3838 | ASSERT(*nmap >= 1); |
| 3839 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3840 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3841 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3842 | |
| 3843 | if (unlikely(XFS_TEST_ERROR( |
| 3844 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3845 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3846 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3847 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3848 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3852 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3853 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3854 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3855 | |
| 3856 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3857 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3858 | /* No CoW fork? Return a hole. */ |
| 3859 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3860 | mval->br_startoff = bno; |
| 3861 | mval->br_startblock = HOLESTARTBLOCK; |
| 3862 | mval->br_blockcount = len; |
| 3863 | mval->br_state = XFS_EXT_NORM; |
| 3864 | *nmap = 1; |
| 3865 | return 0; |
| 3866 | } |
| 3867 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3868 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3869 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3870 | if (error) |
| 3871 | return error; |
| 3872 | } |
| 3873 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3874 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3875 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3876 | end = bno + len; |
| 3877 | obno = bno; |
| 3878 | |
| 3879 | while (bno < end && n < *nmap) { |
| 3880 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3881 | if (eof) |
| 3882 | got.br_startoff = end; |
| 3883 | if (got.br_startoff > bno) { |
| 3884 | /* Reading in a hole. */ |
| 3885 | mval->br_startoff = bno; |
| 3886 | mval->br_startblock = HOLESTARTBLOCK; |
| 3887 | mval->br_blockcount = |
| 3888 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3889 | mval->br_state = XFS_EXT_NORM; |
| 3890 | bno += mval->br_blockcount; |
| 3891 | len -= mval->br_blockcount; |
| 3892 | mval++; |
| 3893 | n++; |
| 3894 | continue; |
| 3895 | } |
| 3896 | |
| 3897 | /* set up the extent map to return. */ |
| 3898 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3899 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3900 | |
| 3901 | /* If we're done, stop now. */ |
| 3902 | if (bno >= end || n >= *nmap) |
| 3903 | break; |
| 3904 | |
| 3905 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3906 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3907 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3908 | } |
| 3909 | *nmap = n; |
| 3910 | return 0; |
| 3911 | } |
| 3912 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3913 | /* |
| 3914 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3915 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3916 | * |
| 3917 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3918 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3919 | * to the extent record that was inserted to the inode fork. |
| 3920 | * |
| 3921 | * Note that the allocated extent may have been merged with contiguous extents |
| 3922 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3923 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3924 | * look up the updated record in the inode fork. |
| 3925 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3926 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3927 | xfs_bmapi_reserve_delalloc( |
| 3928 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3929 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3930 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3931 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3932 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3933 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3934 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3935 | int eof) |
| 3936 | { |
| 3937 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3938 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3939 | xfs_extlen_t alen; |
| 3940 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3941 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3942 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3943 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3944 | /* |
| 3945 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3946 | * tag the inode before we return. |
| 3947 | */ |
| 3948 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3949 | if (!eof) |
| 3950 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3951 | if (prealloc && alen >= len) |
| 3952 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3953 | |
| 3954 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3955 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3956 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3957 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3958 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3959 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3960 | prev.br_startoff = NULLFILEOFF; |
| 3961 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3962 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3963 | 1, 0, &aoff, &alen); |
| 3964 | ASSERT(!error); |
| 3965 | } |
| 3966 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3967 | /* |
| 3968 | * Make a transaction-less quota reservation for delayed allocation |
| 3969 | * blocks. This number gets adjusted later. We return if we haven't |
| 3970 | * allocated blocks already inside this loop. |
| 3971 | */ |
| 3972 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3973 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3974 | if (error) |
| 3975 | return error; |
| 3976 | |
| 3977 | /* |
| 3978 | * Split changing sb for alen and indlen since they could be coming |
| 3979 | * from different places. |
| 3980 | */ |
| 3981 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3982 | ASSERT(indlen > 0); |
| 3983 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3984 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3985 | if (error) |
| 3986 | goto out_unreserve_quota; |
| 3987 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3988 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3989 | if (error) |
| 3990 | goto out_unreserve_blocks; |
| 3991 | |
| 3992 | |
| 3993 | ip->i_delayed_blks += alen; |
| 3994 | |
| 3995 | got->br_startoff = aoff; |
| 3996 | got->br_startblock = nullstartblock(indlen); |
| 3997 | got->br_blockcount = alen; |
| 3998 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3999 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4000 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4001 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4002 | /* |
| 4003 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4004 | * preallocation can occur at the start or end of the extent, even when |
| 4005 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4006 | */ |
| 4007 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4008 | xfs_inode_set_eofblocks_tag(ip); |
| 4009 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4010 | xfs_inode_set_cowblocks_tag(ip); |
| 4011 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4012 | return 0; |
| 4013 | |
| 4014 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4015 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4016 | out_unreserve_quota: |
| 4017 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4018 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4019 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4020 | return error; |
| 4021 | } |
| 4022 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4023 | static int |
| 4024 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4025 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4026 | { |
| 4027 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4028 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4029 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4030 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4031 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4032 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4033 | ASSERT(bma->length > 0); |
| 4034 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4035 | /* |
| 4036 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4037 | * for in this bmap call but that wouldn't be as good. |
| 4038 | */ |
| 4039 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4040 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4041 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4042 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4043 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4044 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4045 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4046 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4047 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4051 | * Set the data type being allocated. For the data fork, the first data |
| 4052 | * in the file is treated differently to all other allocations. For the |
| 4053 | * attribute fork, we only need to ensure the allocated range is not on |
| 4054 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4055 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4056 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4057 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4058 | if (whichfork == XFS_DATA_FORK) { |
| 4059 | if (bma->offset == 0) |
| 4060 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4061 | else |
| 4062 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4063 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4064 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4065 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4068 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4069 | |
| 4070 | /* |
| 4071 | * Only want to do the alignment at the eof if it is userdata and |
| 4072 | * allocation length is larger than a stripe unit. |
| 4073 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4074 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4075 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4076 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4077 | if (error) |
| 4078 | return error; |
| 4079 | } |
| 4080 | |
| 4081 | error = xfs_bmap_alloc(bma); |
| 4082 | if (error) |
| 4083 | return error; |
| 4084 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4085 | if (bma->cur) |
| 4086 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4087 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4088 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4089 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4090 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4091 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4092 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4093 | } |
| 4094 | /* |
| 4095 | * Bump the number of extents we've allocated |
| 4096 | * in this call. |
| 4097 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4098 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4099 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4100 | if (bma->cur) |
| 4101 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4102 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4103 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4104 | bma->got.br_startoff = bma->offset; |
| 4105 | bma->got.br_startblock = bma->blkno; |
| 4106 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4107 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4108 | |
| 4109 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4110 | * In the data fork, a wasdelay extent has been initialized, so |
| 4111 | * shouldn't be flagged as unwritten. |
| 4112 | * |
| 4113 | * For the cow fork, however, we convert delalloc reservations |
| 4114 | * (extents allocated for speculative preallocation) to |
| 4115 | * allocated unwritten extents, and only convert the unwritten |
| 4116 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4117 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4118 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4119 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4120 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4121 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4122 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4123 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4124 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4125 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4126 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4127 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4128 | bma->firstblock, bma->dfops, &bma->logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4129 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4130 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4131 | if (error) |
| 4132 | return error; |
| 4133 | |
| 4134 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4135 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4136 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4137 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4138 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4139 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4140 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4141 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4142 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4143 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4144 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4145 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4146 | return 0; |
| 4147 | } |
| 4148 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4149 | STATIC int |
| 4150 | xfs_bmapi_convert_unwritten( |
| 4151 | struct xfs_bmalloca *bma, |
| 4152 | struct xfs_bmbt_irec *mval, |
| 4153 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4154 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4155 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4156 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4157 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4158 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4159 | int error; |
| 4160 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4161 | /* check if we need to do unwritten->real conversion */ |
| 4162 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4163 | (flags & XFS_BMAPI_PREALLOC)) |
| 4164 | return 0; |
| 4165 | |
| 4166 | /* check if we need to do real->unwritten conversion */ |
| 4167 | if (mval->br_state == XFS_EXT_NORM && |
| 4168 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4169 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4170 | return 0; |
| 4171 | |
| 4172 | /* |
| 4173 | * Modify (by adding) the state flag, if writing. |
| 4174 | */ |
| 4175 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4176 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4177 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4178 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4179 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4180 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4181 | } |
| 4182 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4183 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4184 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4185 | /* |
| 4186 | * Before insertion into the bmbt, zero the range being converted |
| 4187 | * if required. |
| 4188 | */ |
| 4189 | if (flags & XFS_BMAPI_ZERO) { |
| 4190 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4191 | mval->br_blockcount); |
| 4192 | if (error) |
| 4193 | return error; |
| 4194 | } |
| 4195 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4196 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4197 | &bma->icur, &bma->cur, mval, bma->firstblock, |
| 4198 | bma->dfops, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4199 | /* |
| 4200 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4201 | * path because the conversion might not have done so (e.g., if the |
| 4202 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4203 | * in the transaction for the sake of fsync(), even if nothing has |
| 4204 | * changed, because fsync() will not force the log for this transaction |
| 4205 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4206 | * |
| 4207 | * Note: If we're only converting cow fork extents, there aren't |
| 4208 | * 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] | 4209 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4210 | if (whichfork != XFS_COW_FORK) |
| 4211 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4212 | if (error) |
| 4213 | return error; |
| 4214 | |
| 4215 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4216 | * Update our extent pointer, given that |
| 4217 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4218 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4219 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4220 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4221 | |
| 4222 | /* |
| 4223 | * We may have combined previously unwritten space with written space, |
| 4224 | * so generate another request. |
| 4225 | */ |
| 4226 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4227 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4228 | return 0; |
| 4229 | } |
| 4230 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4231 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4232 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4233 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4234 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4235 | * locking problems. |
| 4236 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | * The returned value in "firstblock" from the first call in a transaction |
| 4238 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4239 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4240 | * the first call in "total"; if no allocation group has that many free |
| 4241 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4242 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4243 | int |
| 4244 | xfs_bmapi_write( |
| 4245 | struct xfs_trans *tp, /* transaction pointer */ |
| 4246 | struct xfs_inode *ip, /* incore inode */ |
| 4247 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4248 | xfs_filblks_t len, /* length to map in file */ |
| 4249 | int flags, /* XFS_BMAPI_... */ |
| 4250 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4251 | controls a.g. for allocs */ |
| 4252 | xfs_extlen_t total, /* total blocks needed */ |
| 4253 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4254 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4255 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4257 | struct xfs_mount *mp = ip->i_mount; |
| 4258 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4259 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4260 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4261 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4262 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4263 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4264 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4265 | int whichfork; /* data or attr fork */ |
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 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4268 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4269 | int orig_flags; /* original flags arg value */ |
| 4270 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4271 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4272 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | |
| 4274 | orig_bno = bno; |
| 4275 | orig_len = len; |
| 4276 | orig_flags = flags; |
| 4277 | orig_mval = mval; |
| 4278 | orig_nmap = *nmap; |
| 4279 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4280 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4283 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4284 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4285 | ASSERT(tp != NULL || |
| 4286 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4287 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4288 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4289 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4290 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4291 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4292 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4293 | /* zeroing is for currently only for data extents, not metadata */ |
| 4294 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4295 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4296 | /* |
| 4297 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4298 | * but it makes no sense to do both at once. This would result in |
| 4299 | * zeroing the unwritten extent twice, but it still being an |
| 4300 | * unwritten extent.... |
| 4301 | */ |
| 4302 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4303 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | if (unlikely(XFS_TEST_ERROR( |
| 4306 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4307 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4308 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4309 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4310 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4314 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4317 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4318 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4319 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4320 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4322 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4324 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4325 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4326 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4330 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4331 | if (error) |
| 4332 | goto error0; |
| 4333 | } |
| 4334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | n = 0; |
| 4336 | end = bno + len; |
| 4337 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4338 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4339 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4340 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4341 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4342 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4343 | bma.tp = tp; |
| 4344 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4345 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4346 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4347 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4348 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4351 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4352 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4353 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4354 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4355 | /* |
| 4356 | * CoW fork conversions should /never/ hit EOF or |
| 4357 | * holes. There should always be something for us |
| 4358 | * to work on. |
| 4359 | */ |
| 4360 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4361 | (flags & XFS_BMAPI_COWFORK))); |
| 4362 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4363 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4364 | /* |
| 4365 | * For the COW fork we can reasonably get a |
| 4366 | * request for converting an extent that races |
| 4367 | * with other threads already having converted |
| 4368 | * part of it, as there converting COW to |
| 4369 | * regular blocks is not protected using the |
| 4370 | * IOLOCK. |
| 4371 | */ |
| 4372 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4373 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4374 | error = -EIO; |
| 4375 | goto error0; |
| 4376 | } |
| 4377 | |
| 4378 | if (eof || bno >= end) |
| 4379 | break; |
| 4380 | } else { |
| 4381 | need_alloc = true; |
| 4382 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4383 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4384 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4385 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4386 | |
| 4387 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | * First, deal with the hole before the allocated space |
| 4389 | * that we found, if any. |
| 4390 | */ |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 4391 | if ((need_alloc || wasdelay) && |
| 4392 | !(flags & XFS_BMAPI_CONVERT_ONLY)) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4393 | bma.eof = eof; |
| 4394 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4395 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4396 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4397 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4398 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4399 | /* |
| 4400 | * There's a 32/64 bit type mismatch between the |
| 4401 | * allocation length request (which can be 64 bits in |
| 4402 | * length) and the bma length request, which is |
| 4403 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4404 | * check for 32-bit overflows and handle them here. |
| 4405 | */ |
| 4406 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4407 | bma.length = MAXEXTLEN; |
| 4408 | else |
| 4409 | bma.length = len; |
| 4410 | |
| 4411 | ASSERT(len > 0); |
| 4412 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4413 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | if (error) |
| 4415 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4416 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4417 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * If this is a CoW allocation, record the data in |
| 4421 | * the refcount btree for orphan recovery. |
| 4422 | */ |
| 4423 | if (whichfork == XFS_COW_FORK) { |
| 4424 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4425 | bma.blkno, bma.length); |
| 4426 | if (error) |
| 4427 | goto error0; |
| 4428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4429 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4430 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4431 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4432 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4433 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4435 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4436 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4437 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4438 | continue; |
| 4439 | if (error) |
| 4440 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4442 | /* update the extent map to return */ |
| 4443 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | /* |
| 4446 | * If we're done, stop now. Stop when we've allocated |
| 4447 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4448 | * the transaction may get too big. |
| 4449 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4450 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4452 | |
| 4453 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4454 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4455 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4456 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | /* |
| 4461 | * Transform from btree to extents, give it cur. |
| 4462 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4463 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4464 | int tmp_logflags = 0; |
| 4465 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4466 | ASSERT(bma.cur); |
| 4467 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4469 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | if (error) |
| 4471 | goto error0; |
| 4472 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4475 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4476 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | error0: |
| 4479 | /* |
| 4480 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4481 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4483 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4485 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4486 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4488 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | /* |
| 4490 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4491 | * detecting a case where the data is changed, there's an error, |
| 4492 | * and it's not logged so we don't shutdown when we should. |
| 4493 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4494 | if (bma.logflags) |
| 4495 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4496 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4497 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | if (!error) { |
| 4499 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4500 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4502 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4503 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4505 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4507 | } |
| 4508 | if (!error) |
| 4509 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4510 | orig_nmap, *nmap); |
| 4511 | return error; |
| 4512 | } |
| 4513 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4514 | static int |
| 4515 | xfs_bmapi_remap( |
| 4516 | struct xfs_trans *tp, |
| 4517 | struct xfs_inode *ip, |
| 4518 | xfs_fileoff_t bno, |
| 4519 | xfs_filblks_t len, |
| 4520 | xfs_fsblock_t startblock, |
| 4521 | struct xfs_defer_ops *dfops) |
| 4522 | { |
| 4523 | struct xfs_mount *mp = ip->i_mount; |
| 4524 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4525 | struct xfs_btree_cur *cur = NULL; |
| 4526 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4527 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4528 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4529 | int logflags = 0, error; |
| 4530 | |
| 4531 | ASSERT(len > 0); |
| 4532 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4533 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4534 | |
| 4535 | if (unlikely(XFS_TEST_ERROR( |
| 4536 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4537 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4538 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4539 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4540 | return -EFSCORRUPTED; |
| 4541 | } |
| 4542 | |
| 4543 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4544 | return -EIO; |
| 4545 | |
| 4546 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4547 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4548 | if (error) |
| 4549 | return error; |
| 4550 | } |
| 4551 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4552 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4553 | /* make sure we only reflink into a hole. */ |
| 4554 | ASSERT(got.br_startoff > bno); |
| 4555 | ASSERT(got.br_startoff - bno >= len); |
| 4556 | } |
| 4557 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4558 | ip->i_d.di_nblocks += len; |
| 4559 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4560 | |
| 4561 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4562 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4563 | cur->bc_private.b.firstblock = firstblock; |
| 4564 | cur->bc_private.b.dfops = dfops; |
| 4565 | cur->bc_private.b.flags = 0; |
| 4566 | } |
| 4567 | |
| 4568 | got.br_startoff = bno; |
| 4569 | got.br_startblock = startblock; |
| 4570 | got.br_blockcount = len; |
| 4571 | got.br_state = XFS_EXT_NORM; |
| 4572 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4573 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur, |
| 4574 | &cur, &got, &firstblock, dfops, &logflags); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4575 | if (error) |
| 4576 | goto error0; |
| 4577 | |
| 4578 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4579 | int tmp_logflags = 0; |
| 4580 | |
| 4581 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4582 | &tmp_logflags, XFS_DATA_FORK); |
| 4583 | logflags |= tmp_logflags; |
| 4584 | } |
| 4585 | |
| 4586 | error0: |
| 4587 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4588 | logflags &= ~XFS_ILOG_DEXT; |
| 4589 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4590 | logflags &= ~XFS_ILOG_DBROOT; |
| 4591 | |
| 4592 | if (logflags) |
| 4593 | xfs_trans_log_inode(tp, ip, logflags); |
| 4594 | if (cur) { |
| 4595 | xfs_btree_del_cursor(cur, |
| 4596 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4597 | } |
| 4598 | return error; |
| 4599 | } |
| 4600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4602 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4603 | * indlen reservation must be shared across the two new extents that are left |
| 4604 | * behind. |
| 4605 | * |
| 4606 | * Given the original reservation and the worst case indlen for the two new |
| 4607 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4608 | * reservation fairly across the two new extents. If necessary, steal available |
| 4609 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4610 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4611 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4612 | */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4613 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4614 | xfs_bmap_split_indlen( |
| 4615 | xfs_filblks_t ores, /* original res. */ |
| 4616 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4617 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4618 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4619 | { |
| 4620 | xfs_filblks_t len1 = *indlen1; |
| 4621 | xfs_filblks_t len2 = *indlen2; |
| 4622 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4623 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4624 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4625 | |
| 4626 | /* |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4627 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4628 | * indlen for both new extents. |
| 4629 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4630 | if (ores < nres && avail) |
| 4631 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4632 | ores += stolen; |
| 4633 | |
| 4634 | /* nothing else to do if we've satisfied the new reservation */ |
| 4635 | if (ores >= nres) |
| 4636 | return stolen; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4637 | |
| 4638 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4639 | * We can't meet the total required reservation for the two extents. |
| 4640 | * Calculate the percent of the overall shortage between both extents |
| 4641 | * and apply this percentage to each of the requested indlen values. |
| 4642 | * This distributes the shortage fairly and reduces the chances that one |
| 4643 | * of the two extents is left with nothing when extents are repeatedly |
| 4644 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4645 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4646 | resfactor = (ores * 100); |
| 4647 | do_div(resfactor, nres); |
| 4648 | len1 *= resfactor; |
| 4649 | do_div(len1, 100); |
| 4650 | len2 *= resfactor; |
| 4651 | do_div(len2, 100); |
| 4652 | ASSERT(len1 + len2 <= ores); |
| 4653 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4654 | |
| 4655 | /* |
| 4656 | * Hand out the remainder to each extent. If one of the two reservations |
| 4657 | * is zero, we want to make sure that one gets a block first. The loop |
| 4658 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4659 | * is zero. |
| 4660 | */ |
| 4661 | ores -= (len1 + len2); |
| 4662 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4663 | if (ores && !len2 && *indlen2) { |
| 4664 | len2++; |
| 4665 | ores--; |
| 4666 | } |
| 4667 | while (ores) { |
| 4668 | if (len1 < *indlen1) { |
| 4669 | len1++; |
| 4670 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4671 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4672 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4673 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4674 | if (len2 < *indlen2) { |
| 4675 | len2++; |
| 4676 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | *indlen1 = len1; |
| 4681 | *indlen2 = len2; |
Brian Foster | d34999c9 | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4682 | |
| 4683 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4684 | } |
| 4685 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4686 | int |
| 4687 | xfs_bmap_del_extent_delay( |
| 4688 | struct xfs_inode *ip, |
| 4689 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4690 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4691 | struct xfs_bmbt_irec *got, |
| 4692 | struct xfs_bmbt_irec *del) |
| 4693 | { |
| 4694 | struct xfs_mount *mp = ip->i_mount; |
| 4695 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4696 | struct xfs_bmbt_irec new; |
| 4697 | int64_t da_old, da_new, da_diff = 0; |
| 4698 | xfs_fileoff_t del_endoff, got_endoff; |
| 4699 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4700 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4701 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4702 | bool isrt; |
| 4703 | |
| 4704 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4705 | |
| 4706 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4707 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4708 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4709 | da_old = startblockval(got->br_startblock); |
| 4710 | da_new = 0; |
| 4711 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4712 | ASSERT(del->br_blockcount > 0); |
| 4713 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4714 | ASSERT(got_endoff >= del_endoff); |
| 4715 | |
| 4716 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4717 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4718 | |
| 4719 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4720 | xfs_mod_frextents(mp, rtexts); |
| 4721 | } |
| 4722 | |
| 4723 | /* |
| 4724 | * Update the inode delalloc counter now and wait to update the |
| 4725 | * sb counters as we might have to borrow some blocks for the |
| 4726 | * indirect block accounting. |
| 4727 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4728 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4729 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4730 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4731 | if (error) |
| 4732 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4733 | ip->i_delayed_blks -= del->br_blockcount; |
| 4734 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4735 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4736 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4737 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4738 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4739 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4740 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4741 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4742 | /* |
| 4743 | * Matches the whole extent. Delete the entry. |
| 4744 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4745 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4746 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4747 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4748 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4749 | /* |
| 4750 | * Deleting the first part of the extent. |
| 4751 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4752 | got->br_startoff = del_endoff; |
| 4753 | got->br_blockcount -= del->br_blockcount; |
| 4754 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4755 | got->br_blockcount), da_old); |
| 4756 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4757 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4758 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4759 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4760 | /* |
| 4761 | * Deleting the last part of the extent. |
| 4762 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4763 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4764 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4765 | got->br_blockcount), da_old); |
| 4766 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4767 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4768 | break; |
| 4769 | case 0: |
| 4770 | /* |
| 4771 | * Deleting the middle of the extent. |
| 4772 | * |
| 4773 | * Distribute the original indlen reservation across the two new |
| 4774 | * extents. Steal blocks from the deleted extent if necessary. |
| 4775 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4776 | * Warn if either of the new indlen reservations is zero as this |
| 4777 | * can lead to delalloc problems. |
| 4778 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4779 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4780 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4781 | |
| 4782 | new.br_blockcount = got_endoff - del_endoff; |
| 4783 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4784 | |
| 4785 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4786 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4787 | del->br_blockcount); |
| 4788 | |
| 4789 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4790 | |
| 4791 | new.br_startoff = del_endoff; |
| 4792 | new.br_state = got->br_state; |
| 4793 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4794 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4795 | xfs_iext_update_extent(ip, state, icur, got); |
| 4796 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4797 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4798 | |
| 4799 | da_new = got_indlen + new_indlen - stolen; |
| 4800 | del->br_blockcount -= stolen; |
| 4801 | break; |
| 4802 | } |
| 4803 | |
| 4804 | ASSERT(da_old >= da_new); |
| 4805 | da_diff = da_old - da_new; |
| 4806 | if (!isrt) |
| 4807 | da_diff += del->br_blockcount; |
| 4808 | if (da_diff) |
| 4809 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4810 | return error; |
| 4811 | } |
| 4812 | |
| 4813 | void |
| 4814 | xfs_bmap_del_extent_cow( |
| 4815 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4816 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4817 | struct xfs_bmbt_irec *got, |
| 4818 | struct xfs_bmbt_irec *del) |
| 4819 | { |
| 4820 | struct xfs_mount *mp = ip->i_mount; |
| 4821 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4822 | struct xfs_bmbt_irec new; |
| 4823 | xfs_fileoff_t del_endoff, got_endoff; |
| 4824 | int state = BMAP_COWFORK; |
| 4825 | |
| 4826 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4827 | |
| 4828 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4829 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4830 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4831 | ASSERT(del->br_blockcount > 0); |
| 4832 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4833 | ASSERT(got_endoff >= del_endoff); |
| 4834 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4835 | |
| 4836 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4837 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4838 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4839 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4840 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4841 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4842 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4843 | /* |
| 4844 | * Matches the whole extent. Delete the entry. |
| 4845 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4846 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4847 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4848 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4849 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4850 | /* |
| 4851 | * Deleting the first part of the extent. |
| 4852 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4853 | got->br_startoff = del_endoff; |
| 4854 | got->br_blockcount -= del->br_blockcount; |
| 4855 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4856 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4857 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4858 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4859 | /* |
| 4860 | * Deleting the last part of the extent. |
| 4861 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4862 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4863 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4864 | break; |
| 4865 | case 0: |
| 4866 | /* |
| 4867 | * Deleting the middle of the extent. |
| 4868 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4869 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4870 | |
| 4871 | new.br_startoff = del_endoff; |
| 4872 | new.br_blockcount = got_endoff - del_endoff; |
| 4873 | new.br_state = got->br_state; |
| 4874 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4875 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4876 | xfs_iext_update_extent(ip, state, icur, got); |
| 4877 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4878 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4879 | break; |
| 4880 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4881 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4882 | } |
| 4883 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4884 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4885 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4886 | * after removing space. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4887 | */ |
| 4888 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4889 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4890 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4891 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4892 | struct xfs_iext_cursor *icur, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4893 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4894 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4895 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4896 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4897 | int whichfork, /* data or attr fork */ |
| 4898 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4899 | { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4900 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4901 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4902 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4903 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4904 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4905 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4906 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4907 | int i; /* temp state */ |
| 4908 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4909 | xfs_mount_t *mp; /* mount structure */ |
| 4910 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4911 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4912 | /* REFERENCED */ |
| 4913 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4914 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4915 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4916 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4917 | mp = ip->i_mount; |
| 4918 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4919 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4920 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4921 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4922 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4923 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4924 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4925 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4926 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4927 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4928 | qfield = 0; |
| 4929 | error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4930 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4931 | /* |
| 4932 | * If it's the case where the directory code is running with no block |
| 4933 | * reservation, and the deleted block is in the middle of its extent, |
| 4934 | * and the resulting insert of an extent would cause transformation to |
| 4935 | * btree format, then reject it. The calling code will then swap blocks |
| 4936 | * around instead. We have to do this now, rather than waiting for the |
| 4937 | * conversion to btree format, since the transaction will be dirty then. |
| 4938 | */ |
| 4939 | if (tp->t_blk_res == 0 && |
| 4940 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4941 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4942 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4943 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4944 | return -ENOSPC; |
| 4945 | |
| 4946 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4947 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4948 | xfs_fsblock_t bno; |
| 4949 | xfs_filblks_t len; |
| 4950 | |
| 4951 | ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0); |
| 4952 | ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0); |
| 4953 | bno = del->br_startblock; |
| 4954 | len = del->br_blockcount; |
| 4955 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4956 | do_div(len, mp->m_sb.sb_rextsize); |
| 4957 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4958 | if (error) |
| 4959 | goto done; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4960 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4961 | nblks = len * mp->m_sb.sb_rextsize; |
| 4962 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4963 | } else { |
| 4964 | do_fx = 1; |
| 4965 | nblks = del->br_blockcount; |
| 4966 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4967 | } |
| 4968 | |
| 4969 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4970 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4971 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4972 | if (error) |
| 4973 | goto done; |
| 4974 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4975 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4976 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4977 | if (got.br_startoff == del->br_startoff) |
| 4978 | state |= BMAP_LEFT_FILLING; |
| 4979 | if (got_endoff == del_endoff) |
| 4980 | state |= BMAP_RIGHT_FILLING; |
| 4981 | |
| 4982 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4983 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4984 | /* |
| 4985 | * Matches the whole extent. Delete the entry. |
| 4986 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4987 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4988 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4989 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4990 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4991 | flags |= XFS_ILOG_CORE; |
| 4992 | if (!cur) { |
| 4993 | flags |= xfs_ilog_fext(whichfork); |
| 4994 | break; |
| 4995 | } |
| 4996 | if ((error = xfs_btree_delete(cur, &i))) |
| 4997 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4998 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4999 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5000 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5001 | /* |
| 5002 | * Deleting the first part of the extent. |
| 5003 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5004 | got.br_startoff = del_endoff; |
| 5005 | got.br_startblock = del_endblock; |
| 5006 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5007 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5008 | if (!cur) { |
| 5009 | flags |= xfs_ilog_fext(whichfork); |
| 5010 | break; |
| 5011 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5012 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5013 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5014 | goto done; |
| 5015 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5016 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5017 | /* |
| 5018 | * Deleting the last part of the extent. |
| 5019 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5020 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5021 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5022 | if (!cur) { |
| 5023 | flags |= xfs_ilog_fext(whichfork); |
| 5024 | break; |
| 5025 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5026 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5027 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5028 | goto done; |
| 5029 | break; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5030 | case 0: |
| 5031 | /* |
| 5032 | * Deleting the middle of the extent. |
| 5033 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5034 | old = got; |
Christoph Hellwig | ca5d8e5 | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5035 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5036 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5037 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5038 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5039 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5040 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5041 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5042 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5043 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5044 | flags |= XFS_ILOG_CORE; |
| 5045 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5046 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5047 | if (error) |
| 5048 | goto done; |
| 5049 | error = xfs_btree_increment(cur, 0, &i); |
| 5050 | if (error) |
| 5051 | goto done; |
| 5052 | cur->bc_rec.b = new; |
| 5053 | error = xfs_btree_insert(cur, &i); |
| 5054 | if (error && error != -ENOSPC) |
| 5055 | goto done; |
| 5056 | /* |
| 5057 | * If get no-space back from btree insert, it tried a |
| 5058 | * split, and we have a zero block reservation. Fix up |
| 5059 | * our state and return the error. |
| 5060 | */ |
| 5061 | if (error == -ENOSPC) { |
| 5062 | /* |
| 5063 | * Reset the cursor, don't trust it after any |
| 5064 | * insert operation. |
| 5065 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5066 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5067 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5068 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5069 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5070 | /* |
| 5071 | * Update the btree record back |
| 5072 | * to the original value. |
| 5073 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5074 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5075 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5076 | goto done; |
| 5077 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5078 | * Reset the extent record back |
| 5079 | * to the original value. |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5080 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5081 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5082 | flags = 0; |
| 5083 | error = -ENOSPC; |
| 5084 | goto done; |
| 5085 | } |
| 5086 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5087 | } else |
| 5088 | flags |= xfs_ilog_fext(whichfork); |
| 5089 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5090 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5091 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5092 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5093 | break; |
| 5094 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5095 | |
| 5096 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5097 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5098 | if (error) |
| 5099 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5100 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5101 | /* |
| 5102 | * If we need to, add to list of extents to delete. |
| 5103 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5104 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5105 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5106 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5107 | if (error) |
| 5108 | goto done; |
| 5109 | } else |
| 5110 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
| 5111 | del->br_blockcount, NULL); |
| 5112 | } |
| 5113 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5114 | /* |
| 5115 | * Adjust inode # blocks in the file. |
| 5116 | */ |
| 5117 | if (nblks) |
| 5118 | ip->i_d.di_nblocks -= nblks; |
| 5119 | /* |
| 5120 | * Adjust quota data. |
| 5121 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5122 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5123 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5124 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5125 | done: |
| 5126 | *logflagsp = flags; |
| 5127 | return error; |
| 5128 | } |
| 5129 | |
| 5130 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | * Unmap (remove) blocks from a file. |
| 5132 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5133 | * that value. If not all extents in the block range can be removed then |
| 5134 | * *done is set. |
| 5135 | */ |
| 5136 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5137 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | xfs_trans_t *tp, /* transaction pointer */ |
| 5139 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5140 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5141 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | int flags, /* misc flags */ |
| 5143 | xfs_extnum_t nexts, /* number of extents max */ |
| 5144 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5145 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5146 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | { |
| 5148 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5149 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | int error; /* error return value */ |
| 5151 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5152 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5154 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | int logflags; /* transaction logging flags */ |
| 5156 | xfs_extlen_t mod; /* rt extent offset */ |
| 5157 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | int tmp_logflags; /* partial logging flags */ |
| 5159 | int wasdel; /* was a delayed alloc extent */ |
| 5160 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5162 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5163 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5164 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5165 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5166 | struct xfs_iext_cursor icur; |
| 5167 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5169 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5170 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5171 | whichfork = xfs_bmapi_whichfork(flags); |
| 5172 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5174 | if (unlikely( |
| 5175 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5176 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5177 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5178 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5179 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | } |
| 5181 | mp = ip->i_mount; |
| 5182 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5183 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5184 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5185 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | ASSERT(len > 0); |
| 5187 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5188 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5189 | /* |
| 5190 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5191 | * blowing out the transaction with a mix of EFIs and reflink |
| 5192 | * adjustments. |
| 5193 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5194 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5195 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5196 | else |
| 5197 | max_len = len; |
| 5198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5200 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5201 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5202 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5203 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | return 0; |
| 5205 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5206 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5207 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5208 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5209 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5210 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5211 | *rlen = 0; |
| 5212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5214 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | logflags = 0; |
| 5217 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5218 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5219 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5221 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | cur->bc_private.b.flags = 0; |
| 5223 | } else |
| 5224 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5225 | |
| 5226 | if (isrt) { |
| 5227 | /* |
| 5228 | * Synchronize by locking the bitmap inode. |
| 5229 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5230 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5231 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5232 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5233 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5237 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5238 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5240 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | * Just back up to the previous extent, if so. |
| 5242 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5243 | if (got.br_startoff > end && |
| 5244 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5245 | done = true; |
| 5246 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | } |
| 5248 | /* |
| 5249 | * Is the last block of this extent before the range |
| 5250 | * we're supposed to delete? If so, we're done. |
| 5251 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5252 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5254 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | break; |
| 5256 | /* |
| 5257 | * Then deal with the (possibly delayed) allocated space |
| 5258 | * we found. |
| 5259 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5261 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5262 | |
| 5263 | /* |
| 5264 | * Make sure we don't touch multiple AGF headers out of order |
| 5265 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5266 | */ |
| 5267 | if (!wasdel) { |
| 5268 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5269 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5270 | break; |
| 5271 | prev_agno = agno; |
| 5272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | if (got.br_startoff < start) { |
| 5274 | del.br_startoff = start; |
| 5275 | del.br_blockcount -= start - got.br_startoff; |
| 5276 | if (!wasdel) |
| 5277 | del.br_startblock += start - got.br_startoff; |
| 5278 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5279 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5280 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5281 | |
| 5282 | /* How much can we safely unmap? */ |
| 5283 | if (max_len < del.br_blockcount) { |
| 5284 | del.br_startoff += del.br_blockcount - max_len; |
| 5285 | if (!wasdel) |
| 5286 | del.br_startblock += del.br_blockcount - max_len; |
| 5287 | del.br_blockcount = max_len; |
| 5288 | } |
| 5289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | sum = del.br_startblock + del.br_blockcount; |
| 5291 | if (isrt && |
| 5292 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5293 | /* |
| 5294 | * Realtime extent not lined up at the end. |
| 5295 | * The extent could have been split into written |
| 5296 | * and unwritten pieces, or we could just be |
| 5297 | * unmapping part of it. But we can't really |
| 5298 | * get rid of part of a realtime extent. |
| 5299 | */ |
| 5300 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5301 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | /* |
| 5303 | * This piece is unwritten, or we're not |
| 5304 | * using unwritten extents. Skip over it. |
| 5305 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5306 | ASSERT(end >= mod); |
| 5307 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5309 | if (end < got.br_startoff && |
| 5310 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5311 | done = true; |
| 5312 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | } |
| 5314 | continue; |
| 5315 | } |
| 5316 | /* |
| 5317 | * It's written, turn it unwritten. |
| 5318 | * This is better than zeroing it. |
| 5319 | */ |
| 5320 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5321 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | /* |
| 5323 | * If this spans a realtime extent boundary, |
| 5324 | * chop it back to the start of the one we end at. |
| 5325 | */ |
| 5326 | if (del.br_blockcount > mod) { |
| 5327 | del.br_startoff += del.br_blockcount - mod; |
| 5328 | del.br_startblock += del.br_blockcount - mod; |
| 5329 | del.br_blockcount = mod; |
| 5330 | } |
| 5331 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5332 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5333 | whichfork, &icur, &cur, &del, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5334 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | if (error) |
| 5336 | goto error0; |
| 5337 | goto nodelete; |
| 5338 | } |
| 5339 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5340 | /* |
| 5341 | * Realtime extent is lined up at the end but not |
| 5342 | * at the front. We'll get rid of full extents if |
| 5343 | * we can. |
| 5344 | */ |
| 5345 | mod = mp->m_sb.sb_rextsize - mod; |
| 5346 | if (del.br_blockcount > mod) { |
| 5347 | del.br_blockcount -= mod; |
| 5348 | del.br_startoff += mod; |
| 5349 | del.br_startblock += mod; |
| 5350 | } else if ((del.br_startoff == start && |
| 5351 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5352 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5353 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | /* |
| 5355 | * Can't make it unwritten. There isn't |
| 5356 | * a full extent here so just skip it. |
| 5357 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5358 | ASSERT(end >= del.br_blockcount); |
| 5359 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5360 | if (got.br_startoff > end && |
| 5361 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5362 | done = true; |
| 5363 | break; |
| 5364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | continue; |
| 5366 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5367 | struct xfs_bmbt_irec prev; |
| 5368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | /* |
| 5370 | * This one is already unwritten. |
| 5371 | * It must have a written left neighbor. |
| 5372 | * Unwrite the killed part of that one and |
| 5373 | * try again. |
| 5374 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5375 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5376 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5378 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5379 | ASSERT(del.br_startblock == |
| 5380 | prev.br_startblock + prev.br_blockcount); |
| 5381 | if (prev.br_startoff < start) { |
| 5382 | mod = start - prev.br_startoff; |
| 5383 | prev.br_blockcount -= mod; |
| 5384 | prev.br_startblock += mod; |
| 5385 | prev.br_startoff = start; |
| 5386 | } |
| 5387 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5388 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5389 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5390 | &prev, firstblock, dfops, |
| 5391 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | if (error) |
| 5393 | goto error0; |
| 5394 | goto nodelete; |
| 5395 | } else { |
| 5396 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5397 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5398 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5399 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5400 | &del, firstblock, dfops, |
| 5401 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | if (error) |
| 5403 | goto error0; |
| 5404 | goto nodelete; |
| 5405 | } |
| 5406 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5407 | |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5408 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5409 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5410 | &got, &del); |
| 5411 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5412 | error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5413 | cur, &del, &tmp_logflags, whichfork, |
| 5414 | flags); |
| 5415 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5416 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | if (error) |
| 5419 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5420 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5421 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5422 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | /* |
| 5425 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5427 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5428 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5429 | (got.br_startoff > end && |
| 5430 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5431 | done = true; |
| 5432 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | extno++; |
| 5435 | } |
| 5436 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5437 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5438 | *rlen = 0; |
| 5439 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5440 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | /* |
| 5443 | * Convert to a btree if necessary. |
| 5444 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5445 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5447 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | &cur, 0, &tmp_logflags, whichfork); |
| 5449 | logflags |= tmp_logflags; |
| 5450 | if (error) |
| 5451 | goto error0; |
| 5452 | } |
| 5453 | /* |
| 5454 | * transform from btree to extents, give it cur |
| 5455 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5456 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | ASSERT(cur != NULL); |
| 5458 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5459 | whichfork); |
| 5460 | logflags |= tmp_logflags; |
| 5461 | if (error) |
| 5462 | goto error0; |
| 5463 | } |
| 5464 | /* |
| 5465 | * transform from extents to local? |
| 5466 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | error = 0; |
| 5468 | error0: |
| 5469 | /* |
| 5470 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5471 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5473 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5475 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5476 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5478 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | /* |
| 5480 | * Log inode even in the error case, if the transaction |
| 5481 | * is dirty we'll need to shut down the filesystem. |
| 5482 | */ |
| 5483 | if (logflags) |
| 5484 | xfs_trans_log_inode(tp, ip, logflags); |
| 5485 | if (cur) { |
| 5486 | if (!error) { |
| 5487 | *firstblock = cur->bc_private.b.firstblock; |
| 5488 | cur->bc_private.b.allocated = 0; |
| 5489 | } |
| 5490 | xfs_btree_del_cursor(cur, |
| 5491 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5492 | } |
| 5493 | return error; |
| 5494 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5495 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5496 | /* Unmap a range of a file. */ |
| 5497 | int |
| 5498 | xfs_bunmapi( |
| 5499 | xfs_trans_t *tp, |
| 5500 | struct xfs_inode *ip, |
| 5501 | xfs_fileoff_t bno, |
| 5502 | xfs_filblks_t len, |
| 5503 | int flags, |
| 5504 | xfs_extnum_t nexts, |
| 5505 | xfs_fsblock_t *firstblock, |
| 5506 | struct xfs_defer_ops *dfops, |
| 5507 | int *done) |
| 5508 | { |
| 5509 | int error; |
| 5510 | |
| 5511 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5512 | dfops); |
| 5513 | *done = (len == 0); |
| 5514 | return error; |
| 5515 | } |
| 5516 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5517 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5518 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5519 | * extent that precedes the target hole of the shift. |
| 5520 | */ |
| 5521 | STATIC bool |
| 5522 | xfs_bmse_can_merge( |
| 5523 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5524 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5525 | xfs_fileoff_t shift) /* shift fsb */ |
| 5526 | { |
| 5527 | xfs_fileoff_t startoff; |
| 5528 | |
| 5529 | startoff = got->br_startoff - shift; |
| 5530 | |
| 5531 | /* |
| 5532 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5533 | * the preceding extent. |
| 5534 | */ |
| 5535 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5536 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5537 | (left->br_state != got->br_state) || |
| 5538 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5539 | return false; |
| 5540 | |
| 5541 | return true; |
| 5542 | } |
| 5543 | |
| 5544 | /* |
| 5545 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5546 | * hole in the file. If an extent shift would result in the extent being fully |
| 5547 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5548 | * the preceding extent rather than do the shift. |
| 5549 | * |
| 5550 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5551 | * with the provided extents via xfs_bmse_can_merge(). |
| 5552 | */ |
| 5553 | STATIC int |
| 5554 | xfs_bmse_merge( |
| 5555 | struct xfs_inode *ip, |
| 5556 | int whichfork, |
| 5557 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5558 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5559 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5560 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5561 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5562 | int *logflags, /* output */ |
| 5563 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5564 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5565 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5566 | xfs_filblks_t blockcount; |
| 5567 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5568 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5569 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5570 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5571 | |
| 5572 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5573 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5574 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5575 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5576 | new = *left; |
| 5577 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5578 | |
| 5579 | /* |
| 5580 | * Update the on-disk extent count, the btree if necessary and log the |
| 5581 | * inode. |
| 5582 | */ |
| 5583 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5584 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5585 | *logflags |= XFS_ILOG_CORE; |
| 5586 | if (!cur) { |
| 5587 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5588 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5592 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5593 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5594 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5595 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5596 | |
| 5597 | error = xfs_btree_delete(cur, &i); |
| 5598 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5599 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5600 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5601 | |
| 5602 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5603 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5604 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5605 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5606 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5607 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5608 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5609 | if (error) |
| 5610 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5611 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5612 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5613 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5614 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5615 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5616 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5617 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5618 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5619 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5620 | if (error) |
| 5621 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5622 | memcpy(&new, got, sizeof(new)); |
| 5623 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5624 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5625 | } |
| 5626 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5627 | static int |
| 5628 | xfs_bmap_shift_update_extent( |
| 5629 | struct xfs_inode *ip, |
| 5630 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5631 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5632 | struct xfs_bmbt_irec *got, |
| 5633 | struct xfs_btree_cur *cur, |
| 5634 | int *logflags, |
| 5635 | struct xfs_defer_ops *dfops, |
| 5636 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5637 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5638 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5639 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5640 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5641 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5642 | *logflags |= XFS_ILOG_CORE; |
| 5643 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5644 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5645 | |
| 5646 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5647 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5648 | if (error) |
| 5649 | return error; |
| 5650 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5651 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5652 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5653 | if (error) |
| 5654 | return error; |
| 5655 | } else { |
| 5656 | *logflags |= XFS_ILOG_DEXT; |
| 5657 | } |
| 5658 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5659 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5660 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5661 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5662 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5663 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5664 | if (error) |
| 5665 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5666 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5667 | } |
| 5668 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5669 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5670 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5671 | struct xfs_trans *tp, |
| 5672 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5673 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5674 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5675 | bool *done, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5676 | xfs_fsblock_t *firstblock, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5677 | struct xfs_defer_ops *dfops) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5678 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5679 | int whichfork = XFS_DATA_FORK; |
| 5680 | struct xfs_mount *mp = ip->i_mount; |
| 5681 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5682 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5683 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5684 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5685 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5686 | int error = 0; |
| 5687 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5688 | |
| 5689 | if (unlikely(XFS_TEST_ERROR( |
| 5690 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5691 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5692 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5693 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5694 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5698 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5699 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5700 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5701 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5702 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5703 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5704 | if (error) |
| 5705 | return error; |
| 5706 | } |
| 5707 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5708 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5709 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5710 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5711 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5712 | cur->bc_private.b.flags = 0; |
| 5713 | } |
| 5714 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5715 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5716 | *done = true; |
| 5717 | goto del_cursor; |
| 5718 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5719 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5720 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5721 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5722 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5723 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5724 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5725 | error = -EINVAL; |
| 5726 | goto del_cursor; |
| 5727 | } |
| 5728 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5729 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5730 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5731 | &icur, &got, &prev, cur, &logflags, |
| 5732 | dfops); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5733 | if (error) |
| 5734 | goto del_cursor; |
| 5735 | goto done; |
| 5736 | } |
| 5737 | } else { |
| 5738 | if (got.br_startoff < offset_shift_fsb) { |
| 5739 | error = -EINVAL; |
| 5740 | goto del_cursor; |
| 5741 | } |
| 5742 | } |
| 5743 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5744 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5745 | &logflags, dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5746 | if (error) |
| 5747 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5748 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5749 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5750 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5751 | *done = true; |
| 5752 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5753 | } |
| 5754 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5755 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5756 | del_cursor: |
| 5757 | if (cur) |
| 5758 | xfs_btree_del_cursor(cur, |
| 5759 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5760 | if (logflags) |
| 5761 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5762 | return error; |
| 5763 | } |
| 5764 | |
| 5765 | int |
| 5766 | xfs_bmap_insert_extents( |
| 5767 | struct xfs_trans *tp, |
| 5768 | struct xfs_inode *ip, |
| 5769 | xfs_fileoff_t *next_fsb, |
| 5770 | xfs_fileoff_t offset_shift_fsb, |
| 5771 | bool *done, |
| 5772 | xfs_fileoff_t stop_fsb, |
| 5773 | xfs_fsblock_t *firstblock, |
| 5774 | struct xfs_defer_ops *dfops) |
| 5775 | { |
| 5776 | int whichfork = XFS_DATA_FORK; |
| 5777 | struct xfs_mount *mp = ip->i_mount; |
| 5778 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5779 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5780 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5781 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5782 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5783 | int error = 0; |
| 5784 | int logflags = 0; |
| 5785 | |
| 5786 | if (unlikely(XFS_TEST_ERROR( |
| 5787 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5788 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5789 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5790 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5791 | return -EFSCORRUPTED; |
| 5792 | } |
| 5793 | |
| 5794 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5795 | return -EIO; |
| 5796 | |
| 5797 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5798 | |
| 5799 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5800 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5801 | if (error) |
| 5802 | return error; |
| 5803 | } |
| 5804 | |
| 5805 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5806 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5807 | cur->bc_private.b.firstblock = *firstblock; |
| 5808 | cur->bc_private.b.dfops = dfops; |
| 5809 | cur->bc_private.b.flags = 0; |
| 5810 | } |
| 5811 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5812 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5813 | xfs_iext_last(ifp, &icur); |
| 5814 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5815 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5816 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5817 | goto del_cursor; |
| 5818 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5819 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5820 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5821 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5822 | goto del_cursor; |
| 5823 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5824 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5825 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5826 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5827 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5828 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5829 | error = -EIO; |
| 5830 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5831 | } |
| 5832 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5833 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5834 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5835 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5836 | error = -EINVAL; |
| 5837 | goto del_cursor; |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * Unlike a left shift (which involves a hole punch), a right |
| 5842 | * shift does not modify extent neighbors in any way. We should |
| 5843 | * never find mergeable extents in this scenario. Check anyways |
| 5844 | * and warn if we encounter two extents that could be one. |
| 5845 | */ |
| 5846 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5847 | WARN_ON_ONCE(1); |
| 5848 | } |
| 5849 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5850 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5851 | &logflags, dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5852 | if (error) |
| 5853 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5854 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5855 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5856 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5857 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5858 | goto del_cursor; |
| 5859 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5860 | |
| 5861 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5862 | del_cursor: |
| 5863 | if (cur) |
| 5864 | xfs_btree_del_cursor(cur, |
| 5865 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5866 | if (logflags) |
| 5867 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5868 | return error; |
| 5869 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5870 | |
| 5871 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5872 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5873 | * first block of the current_ext. @ext is a target extent to be split. |
| 5874 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5875 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5876 | */ |
| 5877 | STATIC int |
| 5878 | xfs_bmap_split_extent_at( |
| 5879 | struct xfs_trans *tp, |
| 5880 | struct xfs_inode *ip, |
| 5881 | xfs_fileoff_t split_fsb, |
| 5882 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5883 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5884 | { |
| 5885 | int whichfork = XFS_DATA_FORK; |
| 5886 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5887 | struct xfs_bmbt_irec got; |
| 5888 | struct xfs_bmbt_irec new; /* split extent */ |
| 5889 | struct xfs_mount *mp = ip->i_mount; |
| 5890 | struct xfs_ifork *ifp; |
| 5891 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5892 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5893 | int error = 0; |
| 5894 | int logflags = 0; |
| 5895 | int i = 0; |
| 5896 | |
| 5897 | if (unlikely(XFS_TEST_ERROR( |
| 5898 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5899 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5900 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5901 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5902 | XFS_ERRLEVEL_LOW, mp); |
| 5903 | return -EFSCORRUPTED; |
| 5904 | } |
| 5905 | |
| 5906 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5907 | return -EIO; |
| 5908 | |
| 5909 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5910 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5911 | /* Read in all the extents */ |
| 5912 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5913 | if (error) |
| 5914 | return error; |
| 5915 | } |
| 5916 | |
| 5917 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5918 | * 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] | 5919 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5920 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5921 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5922 | return 0; |
| 5923 | |
| 5924 | gotblkcnt = split_fsb - got.br_startoff; |
| 5925 | new.br_startoff = split_fsb; |
| 5926 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5927 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5928 | new.br_state = got.br_state; |
| 5929 | |
| 5930 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5931 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5932 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5933 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5934 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5935 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5936 | if (error) |
| 5937 | goto del_cursor; |
| 5938 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5939 | } |
| 5940 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5941 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5942 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5943 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5944 | |
| 5945 | logflags = XFS_ILOG_CORE; |
| 5946 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5947 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5948 | if (error) |
| 5949 | goto del_cursor; |
| 5950 | } else |
| 5951 | logflags |= XFS_ILOG_DEXT; |
| 5952 | |
| 5953 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5954 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5955 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5956 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5957 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5958 | |
| 5959 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5960 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5961 | if (error) |
| 5962 | goto del_cursor; |
| 5963 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5964 | error = xfs_btree_insert(cur, &i); |
| 5965 | if (error) |
| 5966 | goto del_cursor; |
| 5967 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5968 | } |
| 5969 | |
| 5970 | /* |
| 5971 | * Convert to a btree if necessary. |
| 5972 | */ |
| 5973 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5974 | int tmp_logflags; /* partial log flag return val */ |
| 5975 | |
| 5976 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5977 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5978 | &cur, 0, &tmp_logflags, whichfork); |
| 5979 | logflags |= tmp_logflags; |
| 5980 | } |
| 5981 | |
| 5982 | del_cursor: |
| 5983 | if (cur) { |
| 5984 | cur->bc_private.b.allocated = 0; |
| 5985 | xfs_btree_del_cursor(cur, |
| 5986 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5987 | } |
| 5988 | |
| 5989 | if (logflags) |
| 5990 | xfs_trans_log_inode(tp, ip, logflags); |
| 5991 | return error; |
| 5992 | } |
| 5993 | |
| 5994 | int |
| 5995 | xfs_bmap_split_extent( |
| 5996 | struct xfs_inode *ip, |
| 5997 | xfs_fileoff_t split_fsb) |
| 5998 | { |
| 5999 | struct xfs_mount *mp = ip->i_mount; |
| 6000 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6001 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6002 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6003 | int error; |
| 6004 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6005 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6006 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6007 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6008 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6009 | |
| 6010 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6011 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6012 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6013 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6014 | |
| 6015 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6016 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6017 | if (error) |
| 6018 | goto out; |
| 6019 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 6020 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6021 | if (error) |
| 6022 | goto out; |
| 6023 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6024 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6025 | |
| 6026 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6027 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6028 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6029 | return error; |
| 6030 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6031 | |
| 6032 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6033 | static bool |
| 6034 | xfs_bmap_is_update_needed( |
| 6035 | struct xfs_bmbt_irec *bmap) |
| 6036 | { |
| 6037 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6038 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6039 | } |
| 6040 | |
| 6041 | /* Record a bmap intent. */ |
| 6042 | static int |
| 6043 | __xfs_bmap_add( |
| 6044 | struct xfs_mount *mp, |
| 6045 | struct xfs_defer_ops *dfops, |
| 6046 | enum xfs_bmap_intent_type type, |
| 6047 | struct xfs_inode *ip, |
| 6048 | int whichfork, |
| 6049 | struct xfs_bmbt_irec *bmap) |
| 6050 | { |
| 6051 | int error; |
| 6052 | struct xfs_bmap_intent *bi; |
| 6053 | |
| 6054 | trace_xfs_bmap_defer(mp, |
| 6055 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6056 | type, |
| 6057 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6058 | ip->i_ino, whichfork, |
| 6059 | bmap->br_startoff, |
| 6060 | bmap->br_blockcount, |
| 6061 | bmap->br_state); |
| 6062 | |
| 6063 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6064 | INIT_LIST_HEAD(&bi->bi_list); |
| 6065 | bi->bi_type = type; |
| 6066 | bi->bi_owner = ip; |
| 6067 | bi->bi_whichfork = whichfork; |
| 6068 | bi->bi_bmap = *bmap; |
| 6069 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6070 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6071 | if (error) { |
| 6072 | kmem_free(bi); |
| 6073 | return error; |
| 6074 | } |
| 6075 | |
| 6076 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6077 | return 0; |
| 6078 | } |
| 6079 | |
| 6080 | /* Map an extent into a file. */ |
| 6081 | int |
| 6082 | xfs_bmap_map_extent( |
| 6083 | struct xfs_mount *mp, |
| 6084 | struct xfs_defer_ops *dfops, |
| 6085 | struct xfs_inode *ip, |
| 6086 | struct xfs_bmbt_irec *PREV) |
| 6087 | { |
| 6088 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6089 | return 0; |
| 6090 | |
| 6091 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6092 | XFS_DATA_FORK, PREV); |
| 6093 | } |
| 6094 | |
| 6095 | /* Unmap an extent out of a file. */ |
| 6096 | int |
| 6097 | xfs_bmap_unmap_extent( |
| 6098 | struct xfs_mount *mp, |
| 6099 | struct xfs_defer_ops *dfops, |
| 6100 | struct xfs_inode *ip, |
| 6101 | struct xfs_bmbt_irec *PREV) |
| 6102 | { |
| 6103 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6104 | return 0; |
| 6105 | |
| 6106 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6107 | XFS_DATA_FORK, PREV); |
| 6108 | } |
| 6109 | |
| 6110 | /* |
| 6111 | * Process one of the deferred bmap operations. We pass back the |
| 6112 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6113 | */ |
| 6114 | int |
| 6115 | xfs_bmap_finish_one( |
| 6116 | struct xfs_trans *tp, |
| 6117 | struct xfs_defer_ops *dfops, |
| 6118 | struct xfs_inode *ip, |
| 6119 | enum xfs_bmap_intent_type type, |
| 6120 | int whichfork, |
| 6121 | xfs_fileoff_t startoff, |
| 6122 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6123 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6124 | xfs_exntst_t state) |
| 6125 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6126 | xfs_fsblock_t firstfsb; |
| 6127 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6128 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6129 | /* |
| 6130 | * firstfsb is tied to the transaction lifetime and is used to |
| 6131 | * ensure correct AG locking order and schedule work item |
| 6132 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6133 | * to only making one bmap call per transaction, so it should |
| 6134 | * be safe to have it as a local variable here. |
| 6135 | */ |
| 6136 | firstfsb = NULLFSBLOCK; |
| 6137 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6138 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6139 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6140 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6141 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6142 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6143 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6144 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6145 | |
| 6146 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6147 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6148 | return -EIO; |
| 6149 | |
| 6150 | switch (type) { |
| 6151 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6152 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6153 | startblock, dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6154 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6155 | break; |
| 6156 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6157 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6158 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6159 | break; |
| 6160 | default: |
| 6161 | ASSERT(0); |
| 6162 | error = -EFSCORRUPTED; |
| 6163 | } |
| 6164 | |
| 6165 | return error; |
| 6166 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6167 | |
| 6168 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6169 | xfs_failaddr_t |
| 6170 | xfs_bmap_validate_extent( |
| 6171 | struct xfs_inode *ip, |
| 6172 | int whichfork, |
| 6173 | struct xfs_bmbt_irec *irec) |
| 6174 | { |
| 6175 | struct xfs_mount *mp = ip->i_mount; |
| 6176 | xfs_fsblock_t endfsb; |
| 6177 | bool isrt; |
| 6178 | |
| 6179 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6180 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6181 | if (isrt) { |
| 6182 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6183 | return __this_address; |
| 6184 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6185 | return __this_address; |
| 6186 | } else { |
| 6187 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6188 | return __this_address; |
| 6189 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6190 | return __this_address; |
| 6191 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6192 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6193 | return __this_address; |
| 6194 | } |
| 6195 | if (irec->br_state != XFS_EXT_NORM) { |
| 6196 | if (whichfork != XFS_DATA_FORK) |
| 6197 | return __this_address; |
| 6198 | if (!xfs_sb_version_hasextflgbit(&mp->m_sb)) |
| 6199 | return __this_address; |
| 6200 | } |
| 6201 | return NULL; |
| 6202 | } |