Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 30 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 33 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_extfree_item.h" |
| 36 | #include "xfs_alloc.h" |
| 37 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 38 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 39 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_rtalloc.h" |
Darrick J. Wong | e9e899a | 2017-10-31 12:04:49 -0700 | [diff] [blame] | 41 | #include "xfs_errortag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include "xfs_quota.h" |
| 44 | #include "xfs_trans_space.h" |
| 45 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 46 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 47 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 48 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 49 | #include "xfs_filestream.h" |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 50 | #include "xfs_rmap.h" |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 51 | #include "xfs_ag_resv.h" |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 52 | #include "xfs_refcount.h" |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 53 | #include "xfs_icache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 57 | |
| 58 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 59 | * Miscellaneous helper functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 63 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 64 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 66 | void |
| 67 | xfs_bmap_compute_maxlevels( |
| 68 | xfs_mount_t *mp, /* file system mount structure */ |
| 69 | int whichfork) /* data or attr fork */ |
| 70 | { |
| 71 | int level; /* btree level */ |
| 72 | uint maxblocks; /* max blocks at this level */ |
| 73 | uint maxleafents; /* max leaf entries possible */ |
| 74 | int maxrootrecs; /* max records in root block */ |
| 75 | int minleafrecs; /* min records in leaf block */ |
| 76 | int minnoderecs; /* min records in node block */ |
| 77 | int sz; /* root block size */ |
| 78 | |
| 79 | /* |
| 80 | * The maximum number of extents in a file, hence the maximum |
| 81 | * number of leaf entries, is controlled by the type of di_nextents |
| 82 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 83 | * (a signed 16-bit number, xfs_aextnum_t). |
| 84 | * |
| 85 | * Note that we can no longer assume that if we are in ATTR1 that |
| 86 | * the fork offset of all the inodes will be |
| 87 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 88 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 89 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 90 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 91 | * of a minimum size available. |
| 92 | */ |
| 93 | if (whichfork == XFS_DATA_FORK) { |
| 94 | maxleafents = MAXEXTNUM; |
| 95 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 96 | } else { |
| 97 | maxleafents = MAXAEXTNUM; |
| 98 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 99 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 100 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 101 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 102 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 103 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 104 | for (level = 1; maxblocks > 1; level++) { |
| 105 | if (maxblocks <= maxrootrecs) |
| 106 | maxblocks = 1; |
| 107 | else |
| 108 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 109 | } |
| 110 | mp->m_bm_maxlevels[whichfork] = level; |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 113 | STATIC int /* error */ |
| 114 | xfs_bmbt_lookup_eq( |
| 115 | struct xfs_btree_cur *cur, |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 116 | struct xfs_bmbt_irec *irec, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 117 | int *stat) /* success/failure */ |
| 118 | { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 119 | cur->bc_rec.b = *irec; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 120 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 121 | } |
| 122 | |
| 123 | STATIC int /* error */ |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 124 | xfs_bmbt_lookup_first( |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 125 | struct xfs_btree_cur *cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 126 | int *stat) /* success/failure */ |
| 127 | { |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 128 | cur->bc_rec.b.br_startoff = 0; |
| 129 | cur->bc_rec.b.br_startblock = 0; |
| 130 | cur->bc_rec.b.br_blockcount = 0; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 131 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 132 | } |
| 133 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 134 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 135 | * Check if the inode needs to be converted to btree format. |
| 136 | */ |
| 137 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 138 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 139 | return whichfork != XFS_COW_FORK && |
| 140 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 141 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 142 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Check if the inode should be converted to extent format. |
| 147 | */ |
| 148 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 149 | { |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 150 | return whichfork != XFS_COW_FORK && |
| 151 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 152 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 153 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 154 | } |
| 155 | |
| 156 | /* |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 157 | * Update the record referred to by cur to the value given by irec |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 158 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 159 | */ |
| 160 | STATIC int |
| 161 | xfs_bmbt_update( |
| 162 | struct xfs_btree_cur *cur, |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 163 | struct xfs_bmbt_irec *irec) |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 164 | { |
| 165 | union xfs_btree_rec rec; |
| 166 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 167 | xfs_bmbt_disk_set_all(&rec.bmbt, irec); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 168 | return xfs_btree_update(cur, &rec); |
| 169 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 172 | * Compute the worst-case number of indirect blocks that will be used |
| 173 | * for ip's delayed extent of length "len". |
| 174 | */ |
| 175 | STATIC xfs_filblks_t |
| 176 | xfs_bmap_worst_indlen( |
| 177 | xfs_inode_t *ip, /* incore inode pointer */ |
| 178 | xfs_filblks_t len) /* delayed extent length */ |
| 179 | { |
| 180 | int level; /* btree level number */ |
| 181 | int maxrecs; /* maximum record count at this level */ |
| 182 | xfs_mount_t *mp; /* mount structure */ |
| 183 | xfs_filblks_t rval; /* return value */ |
| 184 | |
| 185 | mp = ip->i_mount; |
| 186 | maxrecs = mp->m_bmap_dmxr[0]; |
| 187 | for (level = 0, rval = 0; |
| 188 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 189 | level++) { |
| 190 | len += maxrecs - 1; |
| 191 | do_div(len, maxrecs); |
| 192 | rval += len; |
Darrick J. Wong | 5e5c943 | 2017-09-18 09:41:17 -0700 | [diff] [blame] | 193 | if (len == 1) |
| 194 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 195 | level - 1; |
| 196 | if (level == 0) |
| 197 | maxrecs = mp->m_bmap_dmxr[1]; |
| 198 | } |
| 199 | return rval; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Calculate the default attribute fork offset for newly created inodes. |
| 204 | */ |
| 205 | uint |
| 206 | xfs_default_attroffset( |
| 207 | struct xfs_inode *ip) |
| 208 | { |
| 209 | struct xfs_mount *mp = ip->i_mount; |
| 210 | uint offset; |
| 211 | |
| 212 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 213 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 214 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 215 | } else { |
| 216 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 217 | } |
| 218 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 219 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 220 | return offset; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Helper routine to reset inode di_forkoff field when switching |
| 225 | * attribute fork from local to extent format - we reset it where |
| 226 | * possible to make space available for inline data fork extents. |
| 227 | */ |
| 228 | STATIC void |
| 229 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 230 | xfs_inode_t *ip, |
| 231 | int whichfork) |
| 232 | { |
| 233 | if (whichfork == XFS_ATTR_FORK && |
| 234 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 235 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 236 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 237 | |
| 238 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 239 | ip->i_d.di_forkoff = dfl_forkoff; |
| 240 | } |
| 241 | } |
| 242 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 243 | #ifdef DEBUG |
| 244 | STATIC struct xfs_buf * |
| 245 | xfs_bmap_get_bp( |
| 246 | struct xfs_btree_cur *cur, |
| 247 | xfs_fsblock_t bno) |
| 248 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame] | 249 | struct xfs_log_item *lip; |
Dave Chinner | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 329 | */ |
Dave Chinner | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
| 492 | /* |
Dave Chinner | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 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, |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 552 | struct xfs_owner_info *oinfo, |
| 553 | bool skip_discard) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 554 | { |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 555 | struct xfs_extent_free_item *new; /* new element */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 556 | #ifdef DEBUG |
| 557 | xfs_agnumber_t agno; |
| 558 | xfs_agblock_t agbno; |
| 559 | |
| 560 | ASSERT(bno != NULLFSBLOCK); |
| 561 | ASSERT(len > 0); |
| 562 | ASSERT(len <= MAXEXTLEN); |
| 563 | ASSERT(!isnullstartblock(bno)); |
| 564 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 565 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 566 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 567 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 568 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 569 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 570 | #endif |
| 571 | ASSERT(xfs_bmap_free_item_zone != NULL); |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 572 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 573 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
Darrick J. Wong | 310a75a | 2016-08-03 11:18:10 +1000 | [diff] [blame] | 574 | new->xefi_startblock = bno; |
| 575 | new->xefi_blockcount = (xfs_extlen_t)len; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 576 | if (oinfo) |
| 577 | new->xefi_oinfo = *oinfo; |
| 578 | else |
| 579 | xfs_rmap_skip_owner_update(&new->xefi_oinfo); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 580 | new->xefi_skip_discard = skip_discard; |
Darrick J. Wong | ba9e780 | 2016-08-03 11:26:33 +1000 | [diff] [blame] | 581 | trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0, |
| 582 | XFS_FSB_TO_AGBNO(mp, bno), len); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 583 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Inode fork format manipulation functions |
| 588 | */ |
| 589 | |
| 590 | /* |
| 591 | * Transform a btree format file with only one leaf node, where the |
| 592 | * extents list will fit in the inode, into an extents format file. |
| 593 | * Since the file extents are already in-core, all we have to do is |
| 594 | * give up the space for the btree root and pitch the leaf block. |
| 595 | */ |
| 596 | STATIC int /* error */ |
| 597 | xfs_bmap_btree_to_extents( |
| 598 | xfs_trans_t *tp, /* transaction pointer */ |
| 599 | xfs_inode_t *ip, /* incore inode pointer */ |
| 600 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 601 | int *logflagsp, /* inode logging flags */ |
| 602 | int whichfork) /* data or attr fork */ |
| 603 | { |
| 604 | /* REFERENCED */ |
| 605 | struct xfs_btree_block *cblock;/* child btree block */ |
| 606 | xfs_fsblock_t cbno; /* child block number */ |
| 607 | xfs_buf_t *cbp; /* child block's buffer */ |
| 608 | int error; /* error return value */ |
| 609 | xfs_ifork_t *ifp; /* inode fork data */ |
| 610 | xfs_mount_t *mp; /* mount point structure */ |
| 611 | __be64 *pp; /* ptr to block address */ |
| 612 | struct xfs_btree_block *rblock;/* root btree block */ |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 613 | struct xfs_owner_info oinfo; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 614 | |
| 615 | mp = ip->i_mount; |
| 616 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 617 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 618 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 619 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 620 | rblock = ifp->if_broot; |
| 621 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 622 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 623 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 624 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 625 | cbno = be64_to_cpu(*pp); |
| 626 | *logflagsp = 0; |
| 627 | #ifdef DEBUG |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 628 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 629 | xfs_btree_check_lptr(cur, cbno, 1)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 630 | #endif |
| 631 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 632 | &xfs_bmbt_buf_ops); |
| 633 | if (error) |
| 634 | return error; |
| 635 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 636 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 637 | return error; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 638 | xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork); |
| 639 | xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 640 | ip->i_d.di_nblocks--; |
| 641 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 642 | xfs_trans_binval(tp, cbp); |
| 643 | if (cur->bc_bufs[0] == cbp) |
| 644 | cur->bc_bufs[0] = NULL; |
| 645 | xfs_iroot_realloc(ip, -1, whichfork); |
| 646 | ASSERT(ifp->if_broot == NULL); |
| 647 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 648 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 649 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Convert an extents-format file into a btree-format file. |
| 655 | * The new file will have a root block (in the inode) and a single child block. |
| 656 | */ |
| 657 | STATIC int /* error */ |
| 658 | xfs_bmap_extents_to_btree( |
| 659 | xfs_trans_t *tp, /* transaction pointer */ |
| 660 | xfs_inode_t *ip, /* incore inode pointer */ |
| 661 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 662 | struct xfs_defer_ops *dfops, /* blocks freed in xaction */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 663 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 664 | int wasdel, /* converting a delayed alloc */ |
| 665 | int *logflagsp, /* inode logging flags */ |
| 666 | int whichfork) /* data or attr fork */ |
| 667 | { |
| 668 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 669 | xfs_buf_t *abp; /* buffer for ablock */ |
| 670 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 671 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 672 | struct xfs_btree_block *block; /* btree root block */ |
| 673 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 674 | int error; /* error return value */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 675 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 676 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 677 | xfs_mount_t *mp; /* mount structure */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 678 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 679 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 680 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 681 | xfs_extnum_t cnt = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 682 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 683 | mp = ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 684 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 685 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 686 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 687 | |
| 688 | /* |
| 689 | * Make space in the inode incore. |
| 690 | */ |
| 691 | xfs_iroot_realloc(ip, 1, whichfork); |
| 692 | ifp->if_flags |= XFS_IFBROOT; |
| 693 | |
| 694 | /* |
| 695 | * Fill in the root. |
| 696 | */ |
| 697 | block = ifp->if_broot; |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 698 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 699 | XFS_BTNUM_BMAP, 1, 1, ip->i_ino, |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 700 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 701 | /* |
| 702 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 703 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 704 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 705 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 706 | cur->bc_private.b.dfops = dfops; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 707 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 708 | /* |
| 709 | * Convert to a btree with two levels, one record in root. |
| 710 | */ |
| 711 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 712 | memset(&args, 0, sizeof(args)); |
| 713 | args.tp = tp; |
| 714 | args.mp = mp; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 715 | xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 716 | args.firstblock = *firstblock; |
| 717 | if (*firstblock == NULLFSBLOCK) { |
| 718 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 719 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 720 | } else if (dfops->dop_low) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 721 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 722 | args.fsbno = *firstblock; |
| 723 | } else { |
| 724 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 725 | args.fsbno = *firstblock; |
| 726 | } |
| 727 | args.minlen = args.maxlen = args.prod = 1; |
| 728 | args.wasdel = wasdel; |
| 729 | *logflagsp = 0; |
| 730 | if ((error = xfs_alloc_vextent(&args))) { |
| 731 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 732 | ASSERT(ifp->if_broot == NULL); |
| 733 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 734 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 735 | return error; |
| 736 | } |
Darrick J. Wong | 90e2056 | 2016-10-03 09:11:45 -0700 | [diff] [blame] | 737 | |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 738 | if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) { |
| 739 | xfs_iroot_realloc(ip, -1, whichfork); |
Eric Sandeen | 2c4306f | 2018-04-16 23:07:27 -0700 | [diff] [blame] | 740 | ASSERT(ifp->if_broot == NULL); |
| 741 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | 2fcc319 | 2017-03-08 10:38:53 -0800 | [diff] [blame] | 742 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 743 | return -ENOSPC; |
| 744 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 745 | /* |
| 746 | * Allocation can't fail, the space was reserved. |
| 747 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 748 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 749 | args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 750 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 751 | cur->bc_private.b.allocated++; |
| 752 | ip->i_d.di_nblocks++; |
| 753 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 754 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 755 | /* |
| 756 | * Fill in the child block. |
| 757 | */ |
| 758 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 759 | ablock = XFS_BUF_TO_BLOCK(abp); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 760 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 761 | XFS_BTNUM_BMAP, 0, 0, ip->i_ino, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 762 | XFS_BTREE_LONG_PTRS); |
| 763 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 764 | for_each_xfs_iext(ifp, &icur, &rec) { |
Christoph Hellwig | 906abed | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 765 | if (isnullstartblock(rec.br_startblock)) |
| 766 | continue; |
| 767 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt); |
| 768 | xfs_bmbt_disk_set_all(arp, &rec); |
| 769 | cnt++; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 770 | } |
| 771 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 772 | xfs_btree_set_numrecs(ablock, cnt); |
| 773 | |
| 774 | /* |
| 775 | * Fill in the root key and pointer. |
| 776 | */ |
| 777 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 778 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 779 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 780 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 781 | be16_to_cpu(block->bb_level))); |
| 782 | *pp = cpu_to_be64(args.fsbno); |
| 783 | |
| 784 | /* |
| 785 | * Do all this logging at the end so that |
| 786 | * the root is at the right level. |
| 787 | */ |
| 788 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 789 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 790 | ASSERT(*curp == NULL); |
| 791 | *curp = cur; |
| 792 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Convert a local file to an extents file. |
| 798 | * This code is out of bounds for data forks of regular files, |
| 799 | * since the file data needs to get logged so things will stay consistent. |
| 800 | * (The bmap-level manipulations are ok, though). |
| 801 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 802 | void |
| 803 | xfs_bmap_local_to_extents_empty( |
| 804 | struct xfs_inode *ip, |
| 805 | int whichfork) |
| 806 | { |
| 807 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 808 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 809 | ASSERT(whichfork != XFS_COW_FORK); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 810 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 811 | ASSERT(ifp->if_bytes == 0); |
| 812 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 813 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 814 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 815 | ifp->if_flags &= ~XFS_IFINLINE; |
| 816 | ifp->if_flags |= XFS_IFEXTENTS; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 817 | ifp->if_u1.if_root = NULL; |
| 818 | ifp->if_height = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 819 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 820 | } |
| 821 | |
| 822 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 823 | STATIC int /* error */ |
| 824 | xfs_bmap_local_to_extents( |
| 825 | xfs_trans_t *tp, /* transaction pointer */ |
| 826 | xfs_inode_t *ip, /* incore inode pointer */ |
| 827 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 828 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 829 | int *logflagsp, /* inode logging flags */ |
| 830 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 831 | void (*init_fn)(struct xfs_trans *tp, |
| 832 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 833 | struct xfs_inode *ip, |
| 834 | struct xfs_ifork *ifp)) |
| 835 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 836 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 837 | int flags; /* logging flags returned */ |
| 838 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 839 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 840 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 841 | struct xfs_bmbt_irec rec; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 842 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 843 | |
| 844 | /* |
| 845 | * We don't want to deal with the case of keeping inode data inline yet. |
| 846 | * So sending the data fork of a regular inode is invalid. |
| 847 | */ |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 848 | ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 849 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 850 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 851 | |
| 852 | if (!ifp->if_bytes) { |
| 853 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 854 | flags = XFS_ILOG_CORE; |
| 855 | goto done; |
| 856 | } |
| 857 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 858 | flags = 0; |
| 859 | error = 0; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 860 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 861 | memset(&args, 0, sizeof(args)); |
| 862 | args.tp = tp; |
| 863 | args.mp = ip->i_mount; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 864 | xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 865 | args.firstblock = *firstblock; |
| 866 | /* |
| 867 | * Allocate a block. We know we need only one, since the |
| 868 | * file currently fits in an inode. |
| 869 | */ |
| 870 | if (*firstblock == NULLFSBLOCK) { |
| 871 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 872 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 873 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 874 | args.fsbno = *firstblock; |
| 875 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 876 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 877 | args.total = total; |
| 878 | args.minlen = args.maxlen = args.prod = 1; |
| 879 | error = xfs_alloc_vextent(&args); |
| 880 | if (error) |
| 881 | goto done; |
| 882 | |
| 883 | /* Can't fail, the space was reserved. */ |
| 884 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 885 | ASSERT(args.len == 1); |
| 886 | *firstblock = args.fsbno; |
| 887 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 888 | |
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 | * Initialize the block, copy the data and log the remote buffer. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 891 | * |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 892 | * The callout is responsible for logging because the remote format |
| 893 | * might differ from the local format and thus we don't know how much to |
| 894 | * log here. Note that init_fn must also set the buffer log item type |
| 895 | * correctly. |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 896 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 897 | init_fn(tp, bp, ip, ifp); |
| 898 | |
Brian Foster | b7cdc66 | 2015-10-12 15:40:24 +1100 | [diff] [blame] | 899 | /* account for the change in fork size */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 900 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 901 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 902 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 903 | |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 904 | ifp->if_u1.if_root = NULL; |
| 905 | ifp->if_height = 0; |
| 906 | |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 907 | rec.br_startoff = 0; |
| 908 | rec.br_startblock = args.fsbno; |
| 909 | rec.br_blockcount = 1; |
| 910 | rec.br_state = XFS_EXT_NORM; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 911 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 912 | xfs_iext_insert(ip, &icur, &rec, 0); |
Christoph Hellwig | 50bb44c | 2017-08-29 15:44:11 -0700 | [diff] [blame] | 913 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 914 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 915 | ip->i_d.di_nblocks = 1; |
| 916 | xfs_trans_mod_dquot_byino(tp, ip, |
| 917 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 918 | flags |= xfs_ilog_fext(whichfork); |
| 919 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 920 | done: |
| 921 | *logflagsp = flags; |
| 922 | return error; |
| 923 | } |
| 924 | |
| 925 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 927 | */ |
| 928 | STATIC int /* error */ |
| 929 | xfs_bmap_add_attrfork_btree( |
| 930 | xfs_trans_t *tp, /* transaction pointer */ |
| 931 | xfs_inode_t *ip, /* incore inode pointer */ |
| 932 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 933 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | int *flags) /* inode logging flags */ |
| 935 | { |
| 936 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 937 | int error; /* error return value */ |
| 938 | xfs_mount_t *mp; /* file system mount struct */ |
| 939 | int stat; /* newroot status */ |
| 940 | |
| 941 | mp = ip->i_mount; |
| 942 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 943 | *flags |= XFS_ILOG_DBROOT; |
| 944 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 945 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 946 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | cur->bc_private.b.firstblock = *firstblock; |
Christoph Hellwig | b5cfbc2 | 2017-10-17 14:16:27 -0700 | [diff] [blame] | 948 | error = xfs_bmbt_lookup_first(cur, &stat); |
| 949 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 951 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 952 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 953 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | goto error0; |
| 955 | if (stat == 0) { |
| 956 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 957 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | *firstblock = cur->bc_private.b.firstblock; |
| 960 | cur->bc_private.b.allocated = 0; |
| 961 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 962 | } |
| 963 | return 0; |
| 964 | error0: |
| 965 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 966 | return error; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 971 | */ |
| 972 | STATIC int /* error */ |
| 973 | xfs_bmap_add_attrfork_extents( |
| 974 | xfs_trans_t *tp, /* transaction pointer */ |
| 975 | xfs_inode_t *ip, /* incore inode pointer */ |
| 976 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 977 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | int *flags) /* inode logging flags */ |
| 979 | { |
| 980 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 981 | int error; /* error return value */ |
| 982 | |
| 983 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 984 | return 0; |
| 985 | cur = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 986 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | flags, XFS_DATA_FORK); |
| 988 | if (cur) { |
| 989 | cur->bc_private.b.allocated = 0; |
| 990 | xfs_btree_del_cursor(cur, |
| 991 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 992 | } |
| 993 | return error; |
| 994 | } |
| 995 | |
| 996 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 997 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 998 | * different data fork content type needs a different callout to do the |
| 999 | * conversion. Some are basic and only require special block initialisation |
| 1000 | * callouts for the data formating, others (directories) are so specialised they |
| 1001 | * handle everything themselves. |
| 1002 | * |
| 1003 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1004 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1005 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | */ |
| 1007 | STATIC int /* error */ |
| 1008 | xfs_bmap_add_attrfork_local( |
| 1009 | xfs_trans_t *tp, /* transaction pointer */ |
| 1010 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1011 | xfs_fsblock_t *firstblock, /* first block allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1012 | struct xfs_defer_ops *dfops, /* blocks to free at commit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | int *flags) /* inode logging flags */ |
| 1014 | { |
| 1015 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1018 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1019 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1020 | if (S_ISDIR(VFS_I(ip)->i_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1022 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | dargs.dp = ip; |
| 1024 | dargs.firstblock = firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1025 | dargs.dfops = dfops; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1026 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | dargs.whichfork = XFS_DATA_FORK; |
| 1028 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1029 | return xfs_dir2_sf_to_block(&dargs); |
| 1030 | } |
| 1031 | |
Dave Chinner | c19b3b05 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 1032 | if (S_ISLNK(VFS_I(ip)->i_mode)) |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1033 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1034 | flags, XFS_DATA_FORK, |
| 1035 | xfs_symlink_local_to_remote); |
| 1036 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1037 | /* should only be called for types that support local format data */ |
| 1038 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1039 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1043 | * Convert inode from non-attributed to attributed. |
| 1044 | * Must not be in a transaction, ip must not be locked. |
| 1045 | */ |
| 1046 | int /* error code */ |
| 1047 | xfs_bmap_add_attrfork( |
| 1048 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1049 | int size, /* space new attribute needs */ |
| 1050 | int rsvd) /* xact may use reserved blks */ |
| 1051 | { |
| 1052 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1053 | struct xfs_defer_ops dfops; /* freed extent records */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1054 | xfs_mount_t *mp; /* mount structure */ |
| 1055 | xfs_trans_t *tp; /* transaction pointer */ |
| 1056 | int blks; /* space reservation */ |
| 1057 | int version = 1; /* superblock attr version */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1058 | int logflags; /* logging flags */ |
| 1059 | int error; /* error return value */ |
| 1060 | |
| 1061 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1062 | |
| 1063 | mp = ip->i_mount; |
| 1064 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1065 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1066 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1067 | |
| 1068 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0, |
| 1069 | rsvd ? XFS_TRANS_RESERVE : 0, &tp); |
| 1070 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1071 | return error; |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 1072 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1073 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1074 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1075 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1076 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1077 | if (error) |
| 1078 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1079 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1080 | goto trans_cancel; |
Darrick J. Wong | 0f352f8 | 2016-12-05 12:38:11 +1100 | [diff] [blame] | 1081 | if (ip->i_d.di_anextents != 0) { |
| 1082 | error = -EFSCORRUPTED; |
| 1083 | goto trans_cancel; |
| 1084 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1085 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1086 | /* |
| 1087 | * For inodes coming from pre-6.2 filesystems. |
| 1088 | */ |
| 1089 | ASSERT(ip->i_d.di_aformat == 0); |
| 1090 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1091 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1092 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1093 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1094 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1095 | |
| 1096 | switch (ip->i_d.di_format) { |
| 1097 | case XFS_DINODE_FMT_DEV: |
| 1098 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1099 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1100 | case XFS_DINODE_FMT_LOCAL: |
| 1101 | case XFS_DINODE_FMT_EXTENTS: |
| 1102 | case XFS_DINODE_FMT_BTREE: |
| 1103 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1104 | if (!ip->i_d.di_forkoff) |
| 1105 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1106 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1107 | version = 2; |
| 1108 | break; |
| 1109 | default: |
| 1110 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1111 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1112 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | ASSERT(ip->i_afp == NULL); |
| 1116 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1117 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1118 | logflags = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1119 | xfs_defer_init(&dfops, &firstblock); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1120 | switch (ip->i_d.di_format) { |
| 1121 | case XFS_DINODE_FMT_LOCAL: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1122 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1123 | &logflags); |
| 1124 | break; |
| 1125 | case XFS_DINODE_FMT_EXTENTS: |
| 1126 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1127 | &dfops, &logflags); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1128 | break; |
| 1129 | case XFS_DINODE_FMT_BTREE: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1130 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1131 | &logflags); |
| 1132 | break; |
| 1133 | default: |
| 1134 | error = 0; |
| 1135 | break; |
| 1136 | } |
| 1137 | if (logflags) |
| 1138 | xfs_trans_log_inode(tp, ip, logflags); |
| 1139 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1140 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1141 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1142 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1143 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1144 | |
| 1145 | spin_lock(&mp->m_sb_lock); |
| 1146 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1147 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1148 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1149 | } |
| 1150 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1151 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1152 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1153 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1154 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1155 | if (log_sb) |
| 1156 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1157 | } |
| 1158 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 1159 | error = xfs_defer_finish(&tp, &dfops); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1160 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1161 | goto bmap_cancel; |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1162 | error = xfs_trans_commit(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1163 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1164 | return error; |
| 1165 | |
| 1166 | bmap_cancel: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1167 | xfs_defer_cancel(&dfops); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1168 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1169 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1170 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1171 | return error; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * Internal and external extent tree search functions. |
| 1176 | */ |
| 1177 | |
| 1178 | /* |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1179 | * Read in extents from a btree-format inode. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1180 | */ |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1181 | int |
| 1182 | xfs_iread_extents( |
| 1183 | struct xfs_trans *tp, |
| 1184 | struct xfs_inode *ip, |
| 1185 | int whichfork) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1186 | { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1187 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | e8e0e17 | 2017-10-19 11:06:29 -0700 | [diff] [blame] | 1188 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1189 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1190 | xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork); |
| 1191 | struct xfs_btree_block *block = ifp->if_broot; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1192 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1193 | struct xfs_bmbt_irec new; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1194 | xfs_fsblock_t bno; |
| 1195 | struct xfs_buf *bp; |
| 1196 | xfs_extnum_t i, j; |
| 1197 | int level; |
| 1198 | __be64 *pp; |
| 1199 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1201 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 1202 | |
| 1203 | if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 1204 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 1205 | return -EFSCORRUPTED; |
| 1206 | } |
| 1207 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1208 | /* |
| 1209 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1210 | */ |
| 1211 | level = be16_to_cpu(block->bb_level); |
| 1212 | ASSERT(level > 0); |
| 1213 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1214 | bno = be64_to_cpu(*pp); |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 1215 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1216 | /* |
| 1217 | * Go down the tree until leaf level is reached, following the first |
| 1218 | * pointer (leftmost) at each level. |
| 1219 | */ |
| 1220 | while (level-- > 0) { |
| 1221 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1222 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1223 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1224 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1225 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1226 | if (level == 0) |
| 1227 | break; |
| 1228 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1229 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1230 | XFS_WANT_CORRUPTED_GOTO(mp, |
Darrick J. Wong | 59f6fec | 2018-01-08 10:51:00 -0800 | [diff] [blame] | 1231 | xfs_verify_fsbno(mp, bno), out_brelse); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1232 | xfs_trans_brelse(tp, bp); |
| 1233 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1234 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1235 | /* |
| 1236 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1237 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1238 | i = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1239 | xfs_iext_first(ifp, &icur); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1240 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1241 | /* |
| 1242 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1243 | */ |
| 1244 | for (;;) { |
| 1245 | xfs_bmbt_rec_t *frp; |
| 1246 | xfs_fsblock_t nextbno; |
| 1247 | xfs_extnum_t num_recs; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1248 | |
| 1249 | num_recs = xfs_btree_get_numrecs(block); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1250 | if (unlikely(i + num_recs > nextents)) { |
| 1251 | ASSERT(i + num_recs <= nextents); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1252 | xfs_warn(ip->i_mount, |
| 1253 | "corrupt dinode %Lu, (btree extents).", |
| 1254 | (unsigned long long) ip->i_ino); |
Darrick J. Wong | 90a58f9 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1255 | xfs_inode_verifier_error(ip, -EFSCORRUPTED, |
| 1256 | __func__, block, sizeof(*block), |
| 1257 | __this_address); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1258 | error = -EFSCORRUPTED; |
| 1259 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1260 | } |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1261 | /* |
| 1262 | * Read-ahead the next leaf block, if any. |
| 1263 | */ |
| 1264 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1265 | if (nextbno != NULLFSBLOCK) |
| 1266 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1267 | &xfs_bmbt_buf_ops); |
| 1268 | /* |
| 1269 | * Copy records into the extent records. |
| 1270 | */ |
| 1271 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Christoph Hellwig | 6bdcf26 | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1272 | for (j = 0; j < num_recs; j++, frp++, i++) { |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1273 | xfs_failaddr_t fa; |
| 1274 | |
Christoph Hellwig | dac9c9b | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1275 | xfs_bmbt_disk_get_all(frp, &new); |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1276 | fa = xfs_bmap_validate_extent(ip, whichfork, &new); |
| 1277 | if (fa) { |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1278 | error = -EFSCORRUPTED; |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 1279 | xfs_inode_verifier_error(ip, error, |
| 1280 | "xfs_iread_extents(2)", |
| 1281 | frp, sizeof(*frp), fa); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1282 | goto out_brelse; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1283 | } |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1284 | xfs_iext_insert(ip, &icur, &new, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1285 | trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); |
| 1286 | xfs_iext_next(ifp, &icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1287 | } |
| 1288 | xfs_trans_brelse(tp, bp); |
| 1289 | bno = nextbno; |
| 1290 | /* |
| 1291 | * If we've reached the end, stop. |
| 1292 | */ |
| 1293 | if (bno == NULLFSBLOCK) |
| 1294 | break; |
| 1295 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1296 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1297 | if (error) |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1298 | goto out; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1299 | block = XFS_BUF_TO_BLOCK(bp); |
| 1300 | } |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1301 | |
| 1302 | if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) { |
| 1303 | error = -EFSCORRUPTED; |
| 1304 | goto out; |
| 1305 | } |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 1306 | ASSERT(i == xfs_iext_count(ifp)); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1307 | |
| 1308 | ifp->if_flags |= XFS_IFEXTENTS; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1309 | return 0; |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1310 | |
| 1311 | out_brelse: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1312 | xfs_trans_brelse(tp, bp); |
Christoph Hellwig | 211e95b | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1313 | out: |
| 1314 | xfs_iext_destroy(ifp); |
| 1315 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1316 | } |
| 1317 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1318 | /* |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1319 | * Returns the relative block number of the first unused block(s) in the given |
| 1320 | * fork with at least "len" logically contiguous blocks free. This is the |
| 1321 | * lowest-address hole if the fork has holes, else the first block past the end |
| 1322 | * of fork. Return 0 if the fork is currently local (in-inode). |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1323 | */ |
| 1324 | int /* error */ |
| 1325 | xfs_bmap_first_unused( |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1326 | struct xfs_trans *tp, /* transaction pointer */ |
| 1327 | struct xfs_inode *ip, /* incore inode */ |
| 1328 | xfs_extlen_t len, /* size of hole to find */ |
| 1329 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1330 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1331 | { |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1332 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1333 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1334 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1335 | xfs_fileoff_t lastaddr = 0; |
| 1336 | xfs_fileoff_t lowest, max; |
| 1337 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1338 | |
| 1339 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1340 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1341 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1342 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1343 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1344 | *first_unused = 0; |
| 1345 | return 0; |
| 1346 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1347 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1348 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1349 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1350 | if (error) |
| 1351 | return error; |
| 1352 | } |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1353 | |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1354 | lowest = max = *first_unused; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1355 | for_each_xfs_iext(ifp, &icur, &got) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1356 | /* |
| 1357 | * See if the hole before this extent will work. |
| 1358 | */ |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1359 | if (got.br_startoff >= lowest + len && |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1360 | got.br_startoff - max >= len) |
| 1361 | break; |
Christoph Hellwig | f2285c1 | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 1362 | lastaddr = got.br_startoff + got.br_blockcount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1363 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1364 | } |
Christoph Hellwig | 29b3e94 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 1365 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1366 | *first_unused = max; |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1371 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1372 | * last_block (input value) in the file. |
| 1373 | * This is not based on i_size, it is based on the extent records. |
| 1374 | * Returns 0 for local files, as they do not have extent records. |
| 1375 | */ |
| 1376 | int /* error */ |
| 1377 | xfs_bmap_last_before( |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1378 | struct xfs_trans *tp, /* transaction pointer */ |
| 1379 | struct xfs_inode *ip, /* incore inode */ |
| 1380 | xfs_fileoff_t *last_block, /* last block */ |
| 1381 | int whichfork) /* data or attr fork */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1382 | { |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1383 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1384 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1385 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1386 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1387 | |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1388 | switch (XFS_IFORK_FORMAT(ip, whichfork)) { |
| 1389 | case XFS_DINODE_FMT_LOCAL: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1390 | *last_block = 0; |
| 1391 | return 0; |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1392 | case XFS_DINODE_FMT_BTREE: |
| 1393 | case XFS_DINODE_FMT_EXTENTS: |
| 1394 | break; |
| 1395 | default: |
| 1396 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1397 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1398 | |
| 1399 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1400 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1401 | if (error) |
| 1402 | return error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1403 | } |
Christoph Hellwig | 86685f7 | 2016-11-24 11:39:38 +1100 | [diff] [blame] | 1404 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1405 | if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got)) |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 1406 | *last_block = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1407 | return 0; |
| 1408 | } |
| 1409 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1410 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1411 | xfs_bmap_last_extent( |
| 1412 | struct xfs_trans *tp, |
| 1413 | struct xfs_inode *ip, |
| 1414 | int whichfork, |
| 1415 | struct xfs_bmbt_irec *rec, |
| 1416 | int *is_empty) |
| 1417 | { |
| 1418 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1419 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1420 | int error; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1421 | |
| 1422 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1423 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1424 | if (error) |
| 1425 | return error; |
| 1426 | } |
| 1427 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1428 | xfs_iext_last(ifp, &icur); |
| 1429 | if (!xfs_iext_get_extent(ifp, &icur, rec)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1430 | *is_empty = 1; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1431 | else |
| 1432 | *is_empty = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | /* |
| 1437 | * Check the last inode extent to determine whether this allocation will result |
| 1438 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1439 | * blocks at the end of the file which do not start at the previous data block, |
| 1440 | * we will try to align the new blocks at stripe unit boundaries. |
| 1441 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1442 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1443 | * at, or past the EOF. |
| 1444 | */ |
| 1445 | STATIC int |
| 1446 | xfs_bmap_isaeof( |
| 1447 | struct xfs_bmalloca *bma, |
| 1448 | int whichfork) |
| 1449 | { |
| 1450 | struct xfs_bmbt_irec rec; |
| 1451 | int is_empty; |
| 1452 | int error; |
| 1453 | |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1454 | bma->aeof = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1455 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1456 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1457 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1458 | return error; |
| 1459 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1460 | if (is_empty) { |
Thomas Meyer | 749f24f | 2017-10-09 11:38:54 -0700 | [diff] [blame] | 1461 | bma->aeof = true; |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1465 | /* |
| 1466 | * Check if we are allocation or past the last extent, or at least into |
| 1467 | * the last delayed allocated extent. |
| 1468 | */ |
| 1469 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1470 | (bma->offset >= rec.br_startoff && |
| 1471 | isnullstartblock(rec.br_startblock)); |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1476 | * Returns the file-relative block number of the first block past eof in |
| 1477 | * the file. This is not based on i_size, it is based on the extent records. |
| 1478 | * Returns 0 for local files, as they do not have extent records. |
| 1479 | */ |
| 1480 | int |
| 1481 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1482 | struct xfs_inode *ip, |
| 1483 | xfs_fileoff_t *last_block, |
| 1484 | int whichfork) |
| 1485 | { |
| 1486 | struct xfs_bmbt_irec rec; |
| 1487 | int is_empty; |
| 1488 | int error; |
| 1489 | |
| 1490 | *last_block = 0; |
| 1491 | |
| 1492 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1493 | return 0; |
| 1494 | |
| 1495 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1496 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1497 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1498 | |
| 1499 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1500 | if (error || is_empty) |
| 1501 | return error; |
| 1502 | |
| 1503 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * Returns whether the selected fork of the inode has exactly one |
| 1509 | * block or not. For the data fork we check this matches di_size, |
| 1510 | * implying the file's range is 0..bsize-1. |
| 1511 | */ |
| 1512 | int /* 1=>1 block, 0=>otherwise */ |
| 1513 | xfs_bmap_one_block( |
| 1514 | xfs_inode_t *ip, /* incore inode */ |
| 1515 | int whichfork) /* data or attr fork */ |
| 1516 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1517 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1518 | int rval; /* return value */ |
| 1519 | xfs_bmbt_irec_t s; /* internal version of extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1520 | struct xfs_iext_cursor icur; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1521 | |
| 1522 | #ifndef DEBUG |
| 1523 | if (whichfork == XFS_DATA_FORK) |
| 1524 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1525 | #endif /* !DEBUG */ |
| 1526 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1527 | return 0; |
| 1528 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1529 | return 0; |
| 1530 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1531 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1532 | xfs_iext_first(ifp, &icur); |
| 1533 | xfs_iext_get_extent(ifp, &icur, &s); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1534 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1535 | if (rval && whichfork == XFS_DATA_FORK) |
| 1536 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1537 | return rval; |
| 1538 | } |
| 1539 | |
| 1540 | /* |
| 1541 | * Extent tree manipulation functions used during allocation. |
| 1542 | */ |
| 1543 | |
| 1544 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1545 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | */ |
| 1547 | STATIC int /* error */ |
| 1548 | xfs_bmap_add_extent_delay_real( |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1549 | struct xfs_bmalloca *bma, |
| 1550 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1552 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1555 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1557 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1558 | /* left is 0, right is 1, prev is 2 */ |
| 1559 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 1560 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1561 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1562 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1563 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1565 | struct xfs_mount *mp; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1566 | xfs_extnum_t *nextents; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1567 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
Eric Sandeen | f1f96c4 | 2016-01-04 16:10:42 +1100 | [diff] [blame] | 1569 | mp = bma->ip->i_mount; |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1570 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1571 | ASSERT(whichfork != XFS_ATTR_FORK); |
| 1572 | nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents : |
| 1573 | &bma->ip->i_d.di_nextents); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1574 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1575 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1576 | ASSERT(!bma->cur || |
| 1577 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1578 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 1579 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | #define LEFT r[0] |
| 1582 | #define RIGHT r[1] |
| 1583 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | |
| 1585 | /* |
| 1586 | * Set up a bunch of variables to make the tests simpler. |
| 1587 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1588 | xfs_iext_get_extent(ifp, &bma->icur, &PREV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | new_endoff = new->br_startoff + new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1590 | ASSERT(isnullstartblock(PREV.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1592 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1593 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1594 | da_old = startblockval(PREV.br_startblock); |
| 1595 | da_new = 0; |
| 1596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | /* |
| 1598 | * Set flags determining what part of the previous delayed allocation |
| 1599 | * extent is being replaced by a real allocation. |
| 1600 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1601 | if (PREV.br_startoff == new->br_startoff) |
| 1602 | state |= BMAP_LEFT_FILLING; |
| 1603 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1604 | state |= BMAP_RIGHT_FILLING; |
| 1605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | /* |
| 1607 | * Check and set flags if this segment has a left neighbor. |
| 1608 | * Don't set contiguous if the combined extent would be too large. |
| 1609 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1610 | if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1611 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1612 | if (isnullstartblock(LEFT.br_startblock)) |
| 1613 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1615 | |
| 1616 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1617 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1618 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1619 | LEFT.br_state == new->br_state && |
| 1620 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1621 | state |= BMAP_LEFT_CONTIG; |
| 1622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | /* |
| 1624 | * Check and set flags if this segment has a right neighbor. |
| 1625 | * Don't set contiguous if the combined extent would be too large. |
| 1626 | * Also check for all-three-contiguous being too large. |
| 1627 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1628 | if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1629 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1630 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1631 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1633 | |
| 1634 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1635 | new_endoff == RIGHT.br_startoff && |
| 1636 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1637 | new->br_state == RIGHT.br_state && |
| 1638 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1639 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1640 | BMAP_RIGHT_FILLING)) != |
| 1641 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1642 | BMAP_RIGHT_FILLING) || |
| 1643 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1644 | <= MAXEXTLEN)) |
| 1645 | state |= BMAP_RIGHT_CONTIG; |
| 1646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | error = 0; |
| 1648 | /* |
| 1649 | * Switch out based on the FILLING and CONTIG state bits. |
| 1650 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1651 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1652 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1653 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1654 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | /* |
| 1656 | * Filling in all of a previously delayed allocation extent. |
| 1657 | * The left and right neighbors are both contiguous with new. |
| 1658 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1659 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1660 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1661 | xfs_iext_remove(bma->ip, &bma->icur, state); |
| 1662 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1663 | xfs_iext_prev(ifp, &bma->icur); |
| 1664 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1665 | (*nextents)--; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1666 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1667 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1669 | else { |
| 1670 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1671 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1672 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1674 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1675 | error = xfs_btree_delete(bma->cur, &i); |
| 1676 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1678 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1679 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1680 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1682 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1683 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1684 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | goto done; |
| 1686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | break; |
| 1688 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1689 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | /* |
| 1691 | * Filling in all of a previously delayed allocation extent. |
| 1692 | * The left neighbor is contiguous, the right is not. |
| 1693 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1694 | old = LEFT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1695 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1696 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1697 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1698 | xfs_iext_prev(ifp, &bma->icur); |
| 1699 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1700 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1701 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | rval = XFS_ILOG_DEXT; |
| 1703 | else { |
| 1704 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1705 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1706 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1708 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1709 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1710 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | goto done; |
| 1712 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | break; |
| 1714 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1715 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | /* |
| 1717 | * Filling in all of a previously delayed allocation extent. |
| 1718 | * The right neighbor is contiguous, the left is not. |
| 1719 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1720 | PREV.br_startblock = new->br_startblock; |
| 1721 | PREV.br_blockcount += RIGHT.br_blockcount; |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1722 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1723 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 1724 | xfs_iext_remove(bma->ip, &bma->icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1725 | xfs_iext_prev(ifp, &bma->icur); |
| 1726 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1727 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1728 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | rval = XFS_ILOG_DEXT; |
| 1730 | else { |
| 1731 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1732 | error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1733 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1735 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1736 | error = xfs_bmbt_update(bma->cur, &PREV); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1737 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | goto done; |
| 1739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | break; |
| 1741 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1742 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | /* |
| 1744 | * Filling in all of a previously delayed allocation extent. |
| 1745 | * Neither the left nor right neighbors are contiguous with |
| 1746 | * the new one. |
| 1747 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1748 | PREV.br_startblock = new->br_startblock; |
| 1749 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1750 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1751 | |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1752 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1753 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1755 | else { |
| 1756 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1757 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1758 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1760 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1761 | error = xfs_btree_insert(bma->cur, &i); |
| 1762 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1764 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | break; |
| 1767 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1768 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | /* |
| 1770 | * Filling in the first part of a previous delayed allocation. |
| 1771 | * The left neighbor is contiguous. |
| 1772 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1773 | old = LEFT; |
| 1774 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1775 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
| 1776 | startblockval(PREV.br_startblock)); |
| 1777 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1778 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1779 | |
Christoph Hellwig | bf99971 | 2017-11-03 10:34:38 -0700 | [diff] [blame] | 1780 | PREV.br_blockcount = temp; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1781 | PREV.br_startoff += new->br_blockcount; |
| 1782 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | 0d04554 | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 1783 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1784 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1785 | xfs_iext_prev(ifp, &bma->icur); |
| 1786 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1787 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1788 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | rval = XFS_ILOG_DEXT; |
| 1790 | else { |
| 1791 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1792 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1793 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1795 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1796 | error = xfs_bmbt_update(bma->cur, &LEFT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1797 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | goto done; |
| 1799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | break; |
| 1801 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1802 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | /* |
| 1804 | * Filling in the first part of a previous delayed allocation. |
| 1805 | * The left neighbor is not contiguous. |
| 1806 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1807 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1808 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1809 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1811 | else { |
| 1812 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1813 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1814 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1816 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1817 | error = xfs_btree_insert(bma->cur, &i); |
| 1818 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1820 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1822 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1823 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1824 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1825 | bma->firstblock, bma->dfops, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1826 | &bma->cur, 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | rval |= tmp_rval; |
| 1828 | if (error) |
| 1829 | goto done; |
| 1830 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1831 | |
| 1832 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1833 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1834 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1835 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1836 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1837 | PREV.br_startoff = new_endoff; |
| 1838 | PREV.br_blockcount = temp; |
| 1839 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1840 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1841 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1842 | xfs_iext_prev(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | break; |
| 1844 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1845 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | /* |
| 1847 | * Filling in the last part of a previous delayed allocation. |
| 1848 | * The right neighbor is contiguous with the new allocation. |
| 1849 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1850 | old = RIGHT; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1851 | RIGHT.br_startoff = new->br_startoff; |
| 1852 | RIGHT.br_startblock = new->br_startblock; |
| 1853 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1854 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1855 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | rval = XFS_ILOG_DEXT; |
| 1857 | else { |
| 1858 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1859 | error = xfs_bmbt_lookup_eq(bma->cur, &old, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1860 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1862 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1863 | error = xfs_bmbt_update(bma->cur, &RIGHT); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1864 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | goto done; |
| 1866 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1867 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1868 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1869 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1870 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1871 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1872 | PREV.br_blockcount = temp; |
| 1873 | PREV.br_startblock = nullstartblock(da_new); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1874 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1875 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
| 1876 | xfs_iext_next(ifp, &bma->icur); |
| 1877 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | break; |
| 1879 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1880 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | /* |
| 1882 | * Filling in the last part of a previous delayed allocation. |
| 1883 | * The right neighbor is not contiguous. |
| 1884 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1885 | xfs_iext_update_extent(bma->ip, state, &bma->icur, new); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1886 | (*nextents)++; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1887 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1889 | else { |
| 1890 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1891 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1892 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1894 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1895 | error = xfs_btree_insert(bma->cur, &i); |
| 1896 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1898 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1900 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1901 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1902 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1903 | bma->firstblock, bma->dfops, &bma->cur, 1, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1904 | &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | rval |= tmp_rval; |
| 1906 | if (error) |
| 1907 | goto done; |
| 1908 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1909 | |
| 1910 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1911 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1912 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1913 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1914 | |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1915 | PREV.br_startblock = nullstartblock(da_new); |
| 1916 | PREV.br_blockcount = temp; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1917 | xfs_iext_insert(bma->ip, &bma->icur, &PREV, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1918 | xfs_iext_next(ifp, &bma->icur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | break; |
| 1920 | |
| 1921 | case 0: |
| 1922 | /* |
| 1923 | * Filling in the middle part of a previous delayed allocation. |
| 1924 | * Contiguity is impossible here. |
| 1925 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1926 | * |
| 1927 | * We start with a delayed allocation: |
| 1928 | * |
| 1929 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1930 | * PREV @ idx |
| 1931 | * |
| 1932 | * and we are allocating: |
| 1933 | * +rrrrrrrrrrrrrrrrr+ |
| 1934 | * new |
| 1935 | * |
| 1936 | * and we set it up for insertion as: |
| 1937 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1938 | * new |
| 1939 | * PREV @ idx LEFT RIGHT |
| 1940 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1942 | old = PREV; |
| 1943 | |
| 1944 | /* LEFT is the new middle */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1945 | LEFT = *new; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1946 | |
| 1947 | /* RIGHT is the new right */ |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1948 | RIGHT.br_state = PREV.br_state; |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1949 | RIGHT.br_startoff = new_endoff; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1950 | RIGHT.br_blockcount = |
| 1951 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1952 | RIGHT.br_startblock = |
| 1953 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1954 | RIGHT.br_blockcount)); |
| 1955 | |
| 1956 | /* truncate PREV */ |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1957 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
| 1958 | PREV.br_startblock = |
| 1959 | nullstartblock(xfs_bmap_worst_indlen(bma->ip, |
| 1960 | PREV.br_blockcount)); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1961 | xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV); |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1962 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 1963 | xfs_iext_next(ifp, &bma->icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 1964 | xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state); |
| 1965 | xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state); |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 1966 | (*nextents)++; |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1967 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1968 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1970 | else { |
| 1971 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 1972 | error = xfs_bmbt_lookup_eq(bma->cur, new, &i); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1973 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1975 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1976 | error = xfs_btree_insert(bma->cur, &i); |
| 1977 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1979 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1981 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1982 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1983 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 1984 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 1985 | 1, &tmp_rval, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | rval |= tmp_rval; |
| 1987 | if (error) |
| 1988 | goto done; |
| 1989 | } |
Christoph Hellwig | 4dcb8869 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 1990 | |
| 1991 | da_new = startblockval(PREV.br_startblock) + |
| 1992 | startblockval(RIGHT.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | break; |
| 1994 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1995 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1996 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1997 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1998 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1999 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2000 | case BMAP_LEFT_CONTIG: |
| 2001 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | /* |
| 2003 | * These cases are all impossible. |
| 2004 | */ |
| 2005 | ASSERT(0); |
| 2006 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2007 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2008 | /* add reverse mapping unless caller opted out */ |
| 2009 | if (!(bma->flags & XFS_BMAPI_NORMAP)) { |
| 2010 | error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, |
| 2011 | whichfork, new); |
| 2012 | if (error) |
| 2013 | goto done; |
| 2014 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2015 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2016 | /* convert to a btree if necessary */ |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2017 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2018 | int tmp_logflags; /* partial log flag return val */ |
| 2019 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2020 | ASSERT(bma->cur == NULL); |
| 2021 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2022 | bma->firstblock, bma->dfops, &bma->cur, |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2023 | da_old > 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2024 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2025 | if (error) |
| 2026 | goto done; |
| 2027 | } |
| 2028 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2029 | if (bma->cur) { |
| 2030 | da_new += bma->cur->bc_private.b.allocated; |
| 2031 | bma->cur->bc_private.b.allocated = 0; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Christoph Hellwig | ca1862b | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2034 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2035 | if (da_new != da_old) { |
| 2036 | ASSERT(state == 0 || da_new < da_old); |
| 2037 | error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), |
| 2038 | false); |
| 2039 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2040 | |
Darrick J. Wong | 6d3eb1e | 2016-01-04 16:12:42 +1100 | [diff] [blame] | 2041 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | done: |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 2043 | if (whichfork != XFS_COW_FORK) |
| 2044 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | return error; |
| 2046 | #undef LEFT |
| 2047 | #undef RIGHT |
| 2048 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2052 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | */ |
| 2054 | STATIC int /* error */ |
| 2055 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2056 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2058 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2059 | struct xfs_iext_cursor *icur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2061 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2062 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2063 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2064 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | xfs_btree_cur_t *cur; /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2069 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2072 | /* left is 0, right is 1, prev is 2 */ |
| 2073 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2074 | int state = xfs_bmap_fork_to_state(whichfork); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2075 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2076 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2078 | *logflagsp = 0; |
| 2079 | |
| 2080 | cur = *curp; |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2081 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2082 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2083 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2084 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2085 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | #define LEFT r[0] |
| 2088 | #define RIGHT r[1] |
| 2089 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | /* |
| 2092 | * Set up a bunch of variables to make the tests simpler. |
| 2093 | */ |
| 2094 | error = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2095 | xfs_iext_get_extent(ifp, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2096 | ASSERT(new->br_state != PREV.br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2098 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2099 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | /* |
| 2102 | * Set flags determining what part of the previous oldext allocation |
| 2103 | * extent is being replaced by a newext allocation. |
| 2104 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2105 | if (PREV.br_startoff == new->br_startoff) |
| 2106 | state |= BMAP_LEFT_FILLING; |
| 2107 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2108 | state |= BMAP_RIGHT_FILLING; |
| 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | /* |
| 2111 | * Check and set flags if this segment has a left neighbor. |
| 2112 | * Don't set contiguous if the combined extent would be too large. |
| 2113 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2114 | if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2115 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2116 | if (isnullstartblock(LEFT.br_startblock)) |
| 2117 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2119 | |
| 2120 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2121 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2122 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2123 | LEFT.br_state == new->br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2124 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2125 | state |= BMAP_LEFT_CONTIG; |
| 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | /* |
| 2128 | * Check and set flags if this segment has a right neighbor. |
| 2129 | * Don't set contiguous if the combined extent would be too large. |
| 2130 | * Also check for all-three-contiguous being too large. |
| 2131 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2132 | if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2133 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2134 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2135 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2137 | |
| 2138 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2139 | new_endoff == RIGHT.br_startoff && |
| 2140 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2141 | new->br_state == RIGHT.br_state && |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2142 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2143 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2144 | BMAP_RIGHT_FILLING)) != |
| 2145 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2146 | BMAP_RIGHT_FILLING) || |
| 2147 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2148 | <= MAXEXTLEN)) |
| 2149 | state |= BMAP_RIGHT_CONTIG; |
| 2150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | /* |
| 2152 | * Switch out based on the FILLING and CONTIG state bits. |
| 2153 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2154 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2155 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2156 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2157 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | /* |
| 2159 | * Setting all of a previous oldext extent to newext. |
| 2160 | * The left and right neighbors are both contiguous with new. |
| 2161 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2162 | LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2163 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2164 | xfs_iext_remove(ip, icur, state); |
| 2165 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2166 | xfs_iext_prev(ifp, icur); |
| 2167 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2168 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2169 | XFS_IFORK_NEXTENTS(ip, whichfork) - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | if (cur == NULL) |
| 2171 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2172 | else { |
| 2173 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2174 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2175 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2177 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2178 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2180 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2181 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2183 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2184 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2186 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2187 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2189 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2190 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2191 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | goto done; |
| 2193 | } |
| 2194 | break; |
| 2195 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2196 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | /* |
| 2198 | * Setting all of a previous oldext extent to newext. |
| 2199 | * The left neighbor is contiguous, the right is not. |
| 2200 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2201 | LEFT.br_blockcount += PREV.br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2202 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2203 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2204 | xfs_iext_prev(ifp, icur); |
| 2205 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2206 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2207 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | if (cur == NULL) |
| 2209 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2210 | else { |
| 2211 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2212 | error = xfs_bmbt_lookup_eq(cur, &PREV, &i); |
| 2213 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2215 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2216 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2218 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2219 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2221 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2222 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2223 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | goto done; |
| 2225 | } |
| 2226 | break; |
| 2227 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2228 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | /* |
| 2230 | * Setting all of a previous oldext extent to newext. |
| 2231 | * The right neighbor is contiguous, the left is not. |
| 2232 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2233 | PREV.br_blockcount += RIGHT.br_blockcount; |
| 2234 | PREV.br_state = new->br_state; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2235 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2236 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2237 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2238 | xfs_iext_prev(ifp, icur); |
| 2239 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2240 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2241 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2242 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | if (cur == NULL) |
| 2244 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2245 | else { |
| 2246 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2247 | error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i); |
| 2248 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2250 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2251 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2253 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2254 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2256 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2257 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2258 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | goto done; |
| 2260 | } |
| 2261 | break; |
| 2262 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2263 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | /* |
| 2265 | * Setting all of a previous oldext extent to newext. |
| 2266 | * Neither the left nor right neighbors are contiguous with |
| 2267 | * the new one. |
| 2268 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2269 | PREV.br_state = new->br_state; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2270 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | if (cur == NULL) |
| 2273 | rval = XFS_ILOG_DEXT; |
| 2274 | else { |
| 2275 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2276 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2277 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2279 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2280 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2281 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | goto done; |
| 2283 | } |
| 2284 | break; |
| 2285 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2286 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | /* |
| 2288 | * Setting the first part of a previous oldext extent to newext. |
| 2289 | * The left neighbor is contiguous. |
| 2290 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2291 | LEFT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2292 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2293 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2294 | PREV.br_startoff += new->br_blockcount; |
| 2295 | PREV.br_startblock += new->br_blockcount; |
| 2296 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2297 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2298 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2299 | xfs_iext_prev(ifp, icur); |
| 2300 | xfs_iext_update_extent(ip, state, icur, &LEFT); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | if (cur == NULL) |
| 2303 | rval = XFS_ILOG_DEXT; |
| 2304 | else { |
| 2305 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2306 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2307 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2309 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2310 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2311 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2313 | error = xfs_btree_decrement(cur, 0, &i); |
| 2314 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2316 | error = xfs_bmbt_update(cur, &LEFT); |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2317 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | goto done; |
| 2319 | } |
| 2320 | break; |
| 2321 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2322 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | /* |
| 2324 | * Setting the first part of a previous oldext extent to newext. |
| 2325 | * The left neighbor is not contiguous. |
| 2326 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2327 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2328 | PREV.br_startoff += new->br_blockcount; |
| 2329 | PREV.br_startblock += new->br_blockcount; |
| 2330 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2331 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2332 | xfs_iext_update_extent(ip, state, icur, &PREV); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2333 | xfs_iext_insert(ip, icur, new, state); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2334 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2335 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | if (cur == NULL) |
| 2337 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2338 | else { |
| 2339 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2340 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2341 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2343 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2344 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2345 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | goto done; |
| 2347 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2348 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2350 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | } |
| 2352 | break; |
| 2353 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2354 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | /* |
| 2356 | * Setting the last part of a previous oldext extent to newext. |
| 2357 | * The right neighbor is contiguous with the new allocation. |
| 2358 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2359 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2360 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2361 | |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2362 | RIGHT.br_startoff = new->br_startoff; |
| 2363 | RIGHT.br_startblock = new->br_startblock; |
| 2364 | RIGHT.br_blockcount += new->br_blockcount; |
Christoph Hellwig | a681847 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2365 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2366 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2367 | xfs_iext_next(ifp, icur); |
| 2368 | xfs_iext_update_extent(ip, state, icur, &RIGHT); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | if (cur == NULL) |
| 2371 | rval = XFS_ILOG_DEXT; |
| 2372 | else { |
| 2373 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2374 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2375 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2377 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2378 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2379 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | goto done; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2381 | error = xfs_btree_increment(cur, 0, &i); |
| 2382 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | goto done; |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2384 | error = xfs_bmbt_update(cur, &RIGHT); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2385 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | goto done; |
| 2387 | } |
| 2388 | break; |
| 2389 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2390 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | /* |
| 2392 | * Setting the last part of a previous oldext extent to newext. |
| 2393 | * The right neighbor is not contiguous. |
| 2394 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2395 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2396 | PREV.br_blockcount -= new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2397 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2398 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2399 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2400 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2401 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2402 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2403 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if (cur == NULL) |
| 2405 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2406 | else { |
| 2407 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2408 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2409 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2411 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2412 | error = xfs_bmbt_update(cur, &PREV); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2413 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | goto done; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2415 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2416 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2418 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2419 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2421 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | } |
| 2423 | break; |
| 2424 | |
| 2425 | case 0: |
| 2426 | /* |
| 2427 | * Setting the middle part of a previous oldext extent to |
| 2428 | * newext. Contiguity is impossible here. |
| 2429 | * One extent becomes three extents. |
| 2430 | */ |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2431 | old = PREV; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2432 | PREV.br_blockcount = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | r[0] = *new; |
| 2435 | r[1].br_startoff = new_endoff; |
| 2436 | r[1].br_blockcount = |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2437 | old.br_startoff + old.br_blockcount - new_endoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2439 | r[1].br_state = PREV.br_state; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2440 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2441 | xfs_iext_update_extent(ip, state, icur, &PREV); |
| 2442 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2443 | xfs_iext_insert(ip, icur, &r[1], state); |
| 2444 | xfs_iext_insert(ip, icur, &r[0], state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2445 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2446 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2447 | XFS_IFORK_NEXTENTS(ip, whichfork) + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | if (cur == NULL) |
| 2449 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2450 | else { |
| 2451 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2452 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | 79fa614 | 2017-10-17 14:16:25 -0700 | [diff] [blame] | 2453 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2455 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | /* new right extent - oldext */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2457 | error = xfs_bmbt_update(cur, &r[1]); |
| 2458 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | goto done; |
| 2460 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | cur->bc_rec.b = PREV; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2462 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2464 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2465 | /* |
| 2466 | * Reset the cursor to the position of the new extent |
| 2467 | * we are about to insert as we can't trust it after |
| 2468 | * the previous insert. |
| 2469 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2470 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
| 2471 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2473 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | /* new middle extent - newext */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2475 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2477 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | } |
| 2479 | break; |
| 2480 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2481 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2482 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2483 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2484 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2485 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2486 | case BMAP_LEFT_CONTIG: |
| 2487 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | /* |
| 2489 | * These cases are all impossible. |
| 2490 | */ |
| 2491 | ASSERT(0); |
| 2492 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2493 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2494 | /* update reverse mappings */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2495 | error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2496 | if (error) |
| 2497 | goto done; |
| 2498 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2499 | /* convert to a btree if necessary */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2500 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2501 | int tmp_logflags; /* partial log flag return val */ |
| 2502 | |
| 2503 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 2504 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2505 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2506 | *logflagsp |= tmp_logflags; |
| 2507 | if (error) |
| 2508 | goto done; |
| 2509 | } |
| 2510 | |
| 2511 | /* clear out the allocated field, done with it now in any case. */ |
| 2512 | if (cur) { |
| 2513 | cur->bc_private.b.allocated = 0; |
| 2514 | *curp = cur; |
| 2515 | } |
| 2516 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 2517 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2519 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | return error; |
| 2521 | #undef LEFT |
| 2522 | #undef RIGHT |
| 2523 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2527 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2529 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | xfs_bmap_add_extent_hole_delay( |
| 2531 | xfs_inode_t *ip, /* incore inode pointer */ |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2532 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2533 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2534 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2536 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2538 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2539 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2540 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2541 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2542 | xfs_filblks_t temp; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 2544 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2545 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | /* |
| 2548 | * Check and set flags if this segment has a left neighbor |
| 2549 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2550 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2551 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2552 | if (isnullstartblock(left.br_startblock)) |
| 2553 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | /* |
| 2557 | * Check and set flags if the current (right) segment exists. |
| 2558 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2559 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2560 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2561 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2562 | if (isnullstartblock(right.br_startblock)) |
| 2563 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | /* |
| 2567 | * Set contiguity flags on the left and right neighbors. |
| 2568 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2569 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2570 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2571 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2572 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2573 | state |= BMAP_LEFT_CONTIG; |
| 2574 | |
| 2575 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2576 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2577 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2578 | (!(state & BMAP_LEFT_CONTIG) || |
| 2579 | (left.br_blockcount + new->br_blockcount + |
| 2580 | right.br_blockcount <= MAXEXTLEN))) |
| 2581 | state |= BMAP_RIGHT_CONTIG; |
| 2582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | /* |
| 2584 | * Switch out based on the contiguity flags. |
| 2585 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2586 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2587 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | /* |
| 2589 | * New allocation is contiguous with delayed allocations |
| 2590 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2591 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | */ |
| 2593 | temp = left.br_blockcount + new->br_blockcount + |
| 2594 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2595 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2596 | oldlen = startblockval(left.br_startblock) + |
| 2597 | startblockval(new->br_startblock) + |
| 2598 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2599 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2600 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2601 | left.br_startblock = nullstartblock(newlen); |
| 2602 | left.br_blockcount = temp; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2603 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2604 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2605 | xfs_iext_prev(ifp, icur); |
| 2606 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | break; |
| 2608 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2609 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | /* |
| 2611 | * New allocation is contiguous with a delayed allocation |
| 2612 | * on the left. |
| 2613 | * Merge the new allocation with the left neighbor. |
| 2614 | */ |
| 2615 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2616 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2617 | oldlen = startblockval(left.br_startblock) + |
| 2618 | startblockval(new->br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2619 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2620 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2621 | left.br_blockcount = temp; |
| 2622 | left.br_startblock = nullstartblock(newlen); |
Christoph Hellwig | 41d196f | 2017-11-03 10:34:39 -0700 | [diff] [blame] | 2623 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2624 | xfs_iext_prev(ifp, icur); |
| 2625 | xfs_iext_update_extent(ip, state, icur, &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | break; |
| 2627 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2628 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | /* |
| 2630 | * New allocation is contiguous with a delayed allocation |
| 2631 | * on the right. |
| 2632 | * Merge the new allocation with the right neighbor. |
| 2633 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2635 | oldlen = startblockval(new->br_startblock) + |
| 2636 | startblockval(right.br_startblock); |
Brian Foster | 0e339ef | 2017-02-13 22:48:18 -0800 | [diff] [blame] | 2637 | newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2638 | oldlen); |
Christoph Hellwig | 3ffc18e | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 2639 | right.br_startoff = new->br_startoff; |
| 2640 | right.br_startblock = nullstartblock(newlen); |
| 2641 | right.br_blockcount = temp; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2642 | xfs_iext_update_extent(ip, state, icur, &right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | break; |
| 2644 | |
| 2645 | case 0: |
| 2646 | /* |
| 2647 | * New allocation is not contiguous with another |
| 2648 | * delayed allocation. |
| 2649 | * Insert a new entry. |
| 2650 | */ |
| 2651 | oldlen = newlen = 0; |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2652 | xfs_iext_insert(ip, icur, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | break; |
| 2654 | } |
| 2655 | if (oldlen != newlen) { |
| 2656 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2657 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2658 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | /* |
| 2660 | * Nothing to do for disk quota accounting here. |
| 2661 | */ |
| 2662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2666 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | */ |
| 2668 | STATIC int /* error */ |
| 2669 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2670 | struct xfs_trans *tp, |
| 2671 | struct xfs_inode *ip, |
| 2672 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2673 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2674 | struct xfs_btree_cur **curp, |
| 2675 | struct xfs_bmbt_irec *new, |
| 2676 | xfs_fsblock_t *first, |
| 2677 | struct xfs_defer_ops *dfops, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2678 | int *logflagsp, |
| 2679 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | { |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2681 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2682 | struct xfs_mount *mp = ip->i_mount; |
| 2683 | struct xfs_btree_cur *cur = *curp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | int i; /* temp state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2687 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2688 | int rval=0; /* return value (logging flags) */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 2689 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2690 | struct xfs_bmbt_irec old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2692 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2693 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2694 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 2695 | XFS_STATS_INC(mp, xs_add_exlist); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | /* |
| 2698 | * Check and set flags if this segment has a left neighbor. |
| 2699 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2700 | if (xfs_iext_peek_prev_extent(ifp, icur, &left)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2701 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2702 | if (isnullstartblock(left.br_startblock)) |
| 2703 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | /* |
| 2707 | * Check and set flags if this segment has a current value. |
| 2708 | * Not true if we're inserting into the "hole" at eof. |
| 2709 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2710 | if (xfs_iext_get_extent(ifp, icur, &right)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2711 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2712 | if (isnullstartblock(right.br_startblock)) |
| 2713 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2715 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | /* |
| 2717 | * We're inserting a real allocation between "left" and "right". |
| 2718 | * Set the contiguity flags. Don't let extents get too large. |
| 2719 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2720 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2721 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2722 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2723 | left.br_state == new->br_state && |
| 2724 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2725 | state |= BMAP_LEFT_CONTIG; |
| 2726 | |
| 2727 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2728 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2729 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2730 | new->br_state == right.br_state && |
| 2731 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2732 | (!(state & BMAP_LEFT_CONTIG) || |
| 2733 | left.br_blockcount + new->br_blockcount + |
| 2734 | right.br_blockcount <= MAXEXTLEN)) |
| 2735 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2737 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | /* |
| 2739 | * Select which case we're in here, and implement it. |
| 2740 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2741 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2742 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | /* |
| 2744 | * New allocation is contiguous with real allocations on the |
| 2745 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2746 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2748 | left.br_blockcount += new->br_blockcount + right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2749 | |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 2750 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2751 | xfs_iext_prev(ifp, icur); |
| 2752 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2753 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2754 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2755 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2756 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2757 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2758 | } else { |
| 2759 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2760 | error = xfs_bmbt_lookup_eq(cur, &right, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2761 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2762 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2763 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2764 | error = xfs_btree_delete(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2765 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2766 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2767 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2768 | error = xfs_btree_decrement(cur, 0, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2769 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2770 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2771 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2772 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2773 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2774 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2776 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2778 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | /* |
| 2780 | * New allocation is contiguous with a real allocation |
| 2781 | * on the left. |
| 2782 | * Merge the new allocation with the left neighbor. |
| 2783 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2784 | old = left; |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2785 | left.br_blockcount += new->br_blockcount; |
Christoph Hellwig | 1d2e008 | 2017-11-03 10:34:40 -0700 | [diff] [blame] | 2786 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2787 | xfs_iext_prev(ifp, icur); |
| 2788 | xfs_iext_update_extent(ip, state, icur, &left); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2789 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2790 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2791 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2792 | } else { |
| 2793 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2794 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2795 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2796 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2797 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2798 | error = xfs_bmbt_update(cur, &left); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2799 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2800 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2802 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2804 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | /* |
| 2806 | * New allocation is contiguous with a real allocation |
| 2807 | * on the right. |
| 2808 | * Merge the new allocation with the right neighbor. |
| 2809 | */ |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2810 | old = right; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 2811 | |
Christoph Hellwig | 1abb9e5 | 2017-10-17 14:16:24 -0700 | [diff] [blame] | 2812 | right.br_startoff = new->br_startoff; |
| 2813 | right.br_startblock = new->br_startblock; |
| 2814 | right.br_blockcount += new->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 2815 | xfs_iext_update_extent(ip, state, icur, &right); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2816 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2817 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2818 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2819 | } else { |
| 2820 | rval = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2821 | error = xfs_bmbt_lookup_eq(cur, &old, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2822 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2823 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2824 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2825 | error = xfs_bmbt_update(cur, &right); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2826 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2827 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2829 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | |
| 2831 | case 0: |
| 2832 | /* |
| 2833 | * New allocation is not contiguous with another |
| 2834 | * real allocation. |
| 2835 | * Insert a new entry. |
| 2836 | */ |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 2837 | xfs_iext_insert(ip, icur, new, state); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2838 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2839 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2840 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2841 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2842 | } else { |
| 2843 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 2844 | error = xfs_bmbt_lookup_eq(cur, new, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2845 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2846 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2847 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2848 | error = xfs_btree_insert(cur, &i); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2849 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2850 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2851 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2853 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2855 | |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 2856 | /* add reverse mapping unless caller opted out */ |
| 2857 | if (!(flags & XFS_BMAPI_NORMAP)) { |
| 2858 | error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new); |
| 2859 | if (error) |
| 2860 | goto done; |
| 2861 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 2862 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2863 | /* convert to a btree if necessary */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2864 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2865 | int tmp_logflags; /* partial log flag return val */ |
| 2866 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2867 | ASSERT(cur == NULL); |
| 2868 | error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2869 | 0, &tmp_logflags, whichfork); |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2870 | *logflagsp |= tmp_logflags; |
| 2871 | cur = *curp; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2872 | if (error) |
| 2873 | goto done; |
| 2874 | } |
| 2875 | |
| 2876 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2877 | if (cur) |
| 2878 | cur->bc_private.b.allocated = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2879 | |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2880 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2881 | done: |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 2882 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2883 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | } |
| 2885 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2886 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 2887 | * Functions used in the extent read, allocate and remove paths |
| 2888 | */ |
| 2889 | |
| 2890 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2891 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2892 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 2893 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2894 | xfs_bmap_extsize_align( |
| 2895 | xfs_mount_t *mp, |
| 2896 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2897 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2898 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2899 | int rt, /* is this a realtime inode? */ |
| 2900 | int eof, /* is extent at end-of-file? */ |
| 2901 | int delay, /* creating delalloc extent? */ |
| 2902 | int convert, /* overwriting unwritten extent? */ |
| 2903 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2904 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2905 | { |
| 2906 | xfs_fileoff_t orig_off; /* original offset */ |
| 2907 | xfs_extlen_t orig_alen; /* original length */ |
| 2908 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2909 | xfs_fileoff_t nexto; /* next file offset */ |
| 2910 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2911 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2912 | xfs_extlen_t align_alen; /* temp for length */ |
| 2913 | xfs_extlen_t temp; /* temp for calculations */ |
| 2914 | |
| 2915 | if (convert) |
| 2916 | return 0; |
| 2917 | |
| 2918 | orig_off = align_off = *offp; |
| 2919 | orig_alen = align_alen = *lenp; |
| 2920 | orig_end = orig_off + orig_alen; |
| 2921 | |
| 2922 | /* |
| 2923 | * If this request overlaps an existing extent, then don't |
| 2924 | * attempt to perform any additional alignment. |
| 2925 | */ |
| 2926 | if (!delay && !eof && |
| 2927 | (orig_off >= gotp->br_startoff) && |
| 2928 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
| 2932 | /* |
| 2933 | * If the file offset is unaligned vs. the extent size |
| 2934 | * we need to align it. This will be possible unless |
| 2935 | * the file was previously written with a kernel that didn't |
| 2936 | * perform this alignment, or if a truncate shot us in the |
| 2937 | * foot. |
| 2938 | */ |
| 2939 | temp = do_mod(orig_off, extsz); |
| 2940 | if (temp) { |
| 2941 | align_alen += temp; |
| 2942 | align_off -= temp; |
| 2943 | } |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2944 | |
| 2945 | /* Same adjustment for the end of the requested area. */ |
| 2946 | temp = (align_alen % extsz); |
| 2947 | if (temp) |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2948 | align_alen += extsz - temp; |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 2949 | |
| 2950 | /* |
| 2951 | * For large extent hint sizes, the aligned extent might be larger than |
| 2952 | * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls |
| 2953 | * the length back under MAXEXTLEN. The outer allocation loops handle |
| 2954 | * short allocation just fine, so it is safe to do this. We only want to |
| 2955 | * do it when we are forced to, though, because it means more allocation |
| 2956 | * operations are required. |
| 2957 | */ |
| 2958 | while (align_alen > MAXEXTLEN) |
| 2959 | align_alen -= extsz; |
| 2960 | ASSERT(align_alen <= MAXEXTLEN); |
| 2961 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2962 | /* |
| 2963 | * If the previous block overlaps with this proposed allocation |
| 2964 | * then move the start forward without adjusting the length. |
| 2965 | */ |
| 2966 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2967 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2968 | prevo = prevp->br_startoff; |
| 2969 | else |
| 2970 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2971 | } else |
| 2972 | prevo = 0; |
| 2973 | if (align_off != orig_off && align_off < prevo) |
| 2974 | align_off = prevo; |
| 2975 | /* |
| 2976 | * If the next block overlaps with this proposed allocation |
| 2977 | * then move the start back without adjusting the length, |
| 2978 | * but not before offset 0. |
| 2979 | * This may of course make the start overlap previous block, |
| 2980 | * and if we hit the offset 0 limit then the next block |
| 2981 | * can still overlap too. |
| 2982 | */ |
| 2983 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2984 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2985 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2986 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2987 | else |
| 2988 | nexto = gotp->br_startoff; |
| 2989 | } else |
| 2990 | nexto = NULLFILEOFF; |
| 2991 | if (!eof && |
| 2992 | align_off + align_alen != orig_end && |
| 2993 | align_off + align_alen > nexto) |
| 2994 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2995 | /* |
| 2996 | * If we're now overlapping the next or previous extent that |
| 2997 | * means we can't fit an extsz piece in this hole. Just move |
| 2998 | * the start forward to the first valid spot and set |
| 2999 | * the length so we hit the end. |
| 3000 | */ |
| 3001 | if (align_off != orig_off && align_off < prevo) |
| 3002 | align_off = prevo; |
| 3003 | if (align_off + align_alen != orig_end && |
| 3004 | align_off + align_alen > nexto && |
| 3005 | nexto != NULLFILEOFF) { |
| 3006 | ASSERT(nexto > prevo); |
| 3007 | align_alen = nexto - align_off; |
| 3008 | } |
| 3009 | |
| 3010 | /* |
| 3011 | * If realtime, and the result isn't a multiple of the realtime |
| 3012 | * extent size we need to remove blocks until it is. |
| 3013 | */ |
| 3014 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3015 | /* |
| 3016 | * We're not covering the original request, or |
| 3017 | * we won't be able to once we fix the length. |
| 3018 | */ |
| 3019 | if (orig_off < align_off || |
| 3020 | orig_end > align_off + align_alen || |
| 3021 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3022 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3023 | /* |
| 3024 | * Try to fix it by moving the start up. |
| 3025 | */ |
| 3026 | if (align_off + temp <= orig_off) { |
| 3027 | align_alen -= temp; |
| 3028 | align_off += temp; |
| 3029 | } |
| 3030 | /* |
| 3031 | * Try to fix it by moving the end in. |
| 3032 | */ |
| 3033 | else if (align_off + align_alen - temp >= orig_end) |
| 3034 | align_alen -= temp; |
| 3035 | /* |
| 3036 | * Set the start to the minimum then trim the length. |
| 3037 | */ |
| 3038 | else { |
| 3039 | align_alen -= orig_off - align_off; |
| 3040 | align_off = orig_off; |
| 3041 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3042 | } |
| 3043 | /* |
| 3044 | * Result doesn't cover the request, fail it. |
| 3045 | */ |
| 3046 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3047 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3048 | } else { |
| 3049 | ASSERT(orig_off >= align_off); |
Dave Chinner | 6dea405e | 2015-05-29 07:40:06 +1000 | [diff] [blame] | 3050 | /* see MAXEXTLEN handling above */ |
| 3051 | ASSERT(orig_end <= align_off + align_alen || |
| 3052 | align_alen + extsz > MAXEXTLEN); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | #ifdef DEBUG |
| 3056 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3057 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3058 | if (prevp->br_startoff != NULLFILEOFF) |
| 3059 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3060 | #endif |
| 3061 | |
| 3062 | *lenp = align_alen; |
| 3063 | *offp = align_off; |
| 3064 | return 0; |
| 3065 | } |
| 3066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3068 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3069 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3070 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3071 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | { |
| 3073 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3075 | xfs_mount_t *mp; /* mount point structure */ |
| 3076 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3077 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | |
| 3079 | #define ISVALID(x,y) \ |
| 3080 | (rt ? \ |
| 3081 | (x) < mp->m_sb.sb_rblocks : \ |
| 3082 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3083 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3084 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3087 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3088 | rt = XFS_IS_REALTIME_INODE(ap->ip) && |
| 3089 | xfs_alloc_is_userdata(ap->datatype); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3090 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | /* |
| 3092 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3093 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3095 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3096 | !isnullstartblock(ap->prev.br_startblock) && |
| 3097 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3098 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3099 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | /* |
| 3101 | * Adjust for the gap between prevp and us. |
| 3102 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3103 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3104 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3106 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3107 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | } |
| 3109 | /* |
| 3110 | * If not at eof, then compare the two neighbor blocks. |
| 3111 | * Figure out whether either one gives us a good starting point, |
| 3112 | * and pick the better one. |
| 3113 | */ |
| 3114 | else if (!ap->eof) { |
| 3115 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3116 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3117 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3118 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3119 | |
| 3120 | /* |
| 3121 | * If there's a previous (left) block, select a requested |
| 3122 | * start block based on it. |
| 3123 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3124 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3125 | !isnullstartblock(ap->prev.br_startblock) && |
| 3126 | (prevbno = ap->prev.br_startblock + |
| 3127 | ap->prev.br_blockcount) && |
| 3128 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | /* |
| 3130 | * Calculate gap to end of previous block. |
| 3131 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3132 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3133 | (ap->prev.br_startoff + |
| 3134 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | /* |
| 3136 | * Figure the startblock based on the previous block's |
| 3137 | * end and the gap size. |
| 3138 | * Heuristic! |
| 3139 | * If the gap is large relative to the piece we're |
| 3140 | * allocating, or using it gives us an invalid block |
| 3141 | * number, then just use the end of the previous block. |
| 3142 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3143 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3145 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | prevbno += adjust; |
| 3147 | else |
| 3148 | prevdiff += adjust; |
| 3149 | /* |
| 3150 | * If the firstblock forbids it, can't use it, |
| 3151 | * must use default. |
| 3152 | */ |
| 3153 | if (!rt && !nullfb && |
| 3154 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3155 | prevbno = NULLFSBLOCK; |
| 3156 | } |
| 3157 | /* |
| 3158 | * No previous block or can't follow it, just default. |
| 3159 | */ |
| 3160 | else |
| 3161 | prevbno = NULLFSBLOCK; |
| 3162 | /* |
| 3163 | * If there's a following (right) block, select a requested |
| 3164 | * start block based on it. |
| 3165 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3166 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | /* |
| 3168 | * Calculate gap to start of next block. |
| 3169 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3170 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | /* |
| 3172 | * Figure the startblock based on the next block's |
| 3173 | * start and the gap size. |
| 3174 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3175 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | /* |
| 3177 | * Heuristic! |
| 3178 | * If the gap is large relative to the piece we're |
| 3179 | * allocating, or using it gives us an invalid block |
| 3180 | * number, then just use the start of the next block |
| 3181 | * offset by our length. |
| 3182 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3183 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3185 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3186 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3187 | gotbno -= ap->length; |
| 3188 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | } else |
| 3190 | gotdiff += adjust; |
| 3191 | /* |
| 3192 | * If the firstblock forbids it, can't use it, |
| 3193 | * must use default. |
| 3194 | */ |
| 3195 | if (!rt && !nullfb && |
| 3196 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3197 | gotbno = NULLFSBLOCK; |
| 3198 | } |
| 3199 | /* |
| 3200 | * No next block, just default. |
| 3201 | */ |
| 3202 | else |
| 3203 | gotbno = NULLFSBLOCK; |
| 3204 | /* |
| 3205 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3206 | * 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] | 3207 | */ |
| 3208 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3209 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3211 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3213 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3215 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3216 | } |
| 3217 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3218 | static int |
| 3219 | xfs_bmap_longest_free_extent( |
| 3220 | struct xfs_trans *tp, |
| 3221 | xfs_agnumber_t ag, |
| 3222 | xfs_extlen_t *blen, |
| 3223 | int *notinit) |
| 3224 | { |
| 3225 | struct xfs_mount *mp = tp->t_mountp; |
| 3226 | struct xfs_perag *pag; |
| 3227 | xfs_extlen_t longest; |
| 3228 | int error = 0; |
| 3229 | |
| 3230 | pag = xfs_perag_get(mp, ag); |
| 3231 | if (!pag->pagf_init) { |
| 3232 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3233 | if (error) |
| 3234 | goto out; |
| 3235 | |
| 3236 | if (!pag->pagf_init) { |
| 3237 | *notinit = 1; |
| 3238 | goto out; |
| 3239 | } |
| 3240 | } |
| 3241 | |
Eric Sandeen | a1f6941 | 2018-04-06 10:09:42 -0700 | [diff] [blame] | 3242 | longest = xfs_alloc_longest_free_extent(pag, |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3243 | xfs_alloc_min_freelist(mp, pag), |
| 3244 | xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE)); |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3245 | if (*blen < longest) |
| 3246 | *blen = longest; |
| 3247 | |
| 3248 | out: |
| 3249 | xfs_perag_put(pag); |
| 3250 | return error; |
| 3251 | } |
| 3252 | |
| 3253 | static void |
| 3254 | xfs_bmap_select_minlen( |
| 3255 | struct xfs_bmalloca *ap, |
| 3256 | struct xfs_alloc_arg *args, |
| 3257 | xfs_extlen_t *blen, |
| 3258 | int notinit) |
| 3259 | { |
| 3260 | if (notinit || *blen < ap->minlen) { |
| 3261 | /* |
| 3262 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3263 | * there is space for this request. |
| 3264 | */ |
| 3265 | args->minlen = ap->minlen; |
| 3266 | } else if (*blen < args->maxlen) { |
| 3267 | /* |
| 3268 | * If the best seen length is less than the request length, |
| 3269 | * use the best as the minimum. |
| 3270 | */ |
| 3271 | args->minlen = *blen; |
| 3272 | } else { |
| 3273 | /* |
| 3274 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3275 | * as the minimum. |
| 3276 | */ |
| 3277 | args->minlen = args->maxlen; |
| 3278 | } |
| 3279 | } |
| 3280 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3281 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3282 | xfs_bmap_btalloc_nullfb( |
| 3283 | struct xfs_bmalloca *ap, |
| 3284 | struct xfs_alloc_arg *args, |
| 3285 | xfs_extlen_t *blen) |
| 3286 | { |
| 3287 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3288 | xfs_agnumber_t ag, startag; |
| 3289 | int notinit = 0; |
| 3290 | int error; |
| 3291 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3292 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3293 | args->total = ap->total; |
| 3294 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3295 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3296 | if (startag == NULLAGNUMBER) |
| 3297 | startag = ag = 0; |
| 3298 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3299 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3300 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3301 | ¬init); |
| 3302 | if (error) |
| 3303 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3304 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3305 | if (++ag == mp->m_sb.sb_agcount) |
| 3306 | ag = 0; |
| 3307 | if (ag == startag) |
| 3308 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3309 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3310 | |
| 3311 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3312 | return 0; |
| 3313 | } |
| 3314 | |
| 3315 | STATIC int |
| 3316 | xfs_bmap_btalloc_filestreams( |
| 3317 | struct xfs_bmalloca *ap, |
| 3318 | struct xfs_alloc_arg *args, |
| 3319 | xfs_extlen_t *blen) |
| 3320 | { |
| 3321 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3322 | xfs_agnumber_t ag; |
| 3323 | int notinit = 0; |
| 3324 | int error; |
| 3325 | |
| 3326 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3327 | args->total = ap->total; |
| 3328 | |
| 3329 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3330 | if (ag == NULLAGNUMBER) |
| 3331 | ag = 0; |
| 3332 | |
| 3333 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3334 | if (error) |
| 3335 | return error; |
| 3336 | |
| 3337 | if (*blen < args->maxlen) { |
| 3338 | error = xfs_filestream_new_ag(ap, &ag); |
| 3339 | if (error) |
| 3340 | return error; |
| 3341 | |
| 3342 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3343 | ¬init); |
| 3344 | if (error) |
| 3345 | return error; |
| 3346 | |
| 3347 | } |
| 3348 | |
| 3349 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3350 | |
| 3351 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3352 | * Set the failure fallback case to look in the selected AG as stream |
| 3353 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3354 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3355 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3356 | return 0; |
| 3357 | } |
| 3358 | |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3359 | /* Update all inode and quota accounting for the allocation we just did. */ |
| 3360 | static void |
| 3361 | xfs_bmap_btalloc_accounting( |
| 3362 | struct xfs_bmalloca *ap, |
| 3363 | struct xfs_alloc_arg *args) |
| 3364 | { |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 3365 | if (ap->flags & XFS_BMAPI_COWFORK) { |
| 3366 | /* |
| 3367 | * COW fork blocks are in-core only and thus are treated as |
| 3368 | * in-core quota reservation (like delalloc blocks) even when |
| 3369 | * converted to real blocks. The quota reservation is not |
| 3370 | * accounted to disk until blocks are remapped to the data |
| 3371 | * fork. So if these blocks were previously delalloc, we |
| 3372 | * already have quota reservation and there's nothing to do |
| 3373 | * yet. |
| 3374 | */ |
| 3375 | if (ap->wasdel) |
| 3376 | return; |
| 3377 | |
| 3378 | /* |
| 3379 | * Otherwise, we've allocated blocks in a hole. The transaction |
| 3380 | * has acquired in-core quota reservation for this extent. |
| 3381 | * Rather than account these as real blocks, however, we reduce |
| 3382 | * the transaction quota reservation based on the allocation. |
| 3383 | * This essentially transfers the transaction quota reservation |
| 3384 | * to that of a delalloc extent. |
| 3385 | */ |
| 3386 | ap->ip->i_delayed_blks += args->len; |
| 3387 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS, |
| 3388 | -(long)args->len); |
| 3389 | return; |
| 3390 | } |
| 3391 | |
| 3392 | /* data/attr fork only */ |
| 3393 | ap->ip->i_d.di_nblocks += args->len; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3394 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3395 | if (ap->wasdel) |
| 3396 | ap->ip->i_delayed_blks -= args->len; |
| 3397 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
| 3398 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT, |
| 3399 | args->len); |
| 3400 | } |
| 3401 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3402 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3403 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3404 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3405 | { |
| 3406 | xfs_mount_t *mp; /* mount point structure */ |
| 3407 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3408 | xfs_extlen_t align = 0; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3409 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3410 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3411 | xfs_alloc_arg_t args; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3412 | xfs_fileoff_t orig_offset; |
| 3413 | xfs_extlen_t orig_length; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3414 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3415 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3416 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3417 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3418 | int tryagain; |
| 3419 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3420 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3421 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3422 | ASSERT(ap->length); |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3423 | orig_offset = ap->offset; |
| 3424 | orig_length = ap->length; |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3425 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3426 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3427 | |
| 3428 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3429 | stripe_align = 0; |
| 3430 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3431 | stripe_align = mp->m_swidth; |
| 3432 | else if (mp->m_dalign) |
| 3433 | stripe_align = mp->m_dalign; |
| 3434 | |
Darrick J. Wong | f7ca352 | 2016-10-03 09:11:43 -0700 | [diff] [blame] | 3435 | if (ap->flags & XFS_BMAPI_COWFORK) |
| 3436 | align = xfs_get_cowextsz_hint(ap->ip); |
| 3437 | else if (xfs_alloc_is_userdata(ap->datatype)) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3438 | align = xfs_get_extsz_hint(ap->ip); |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3439 | if (align) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3440 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3441 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3442 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3443 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3444 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3445 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3446 | |
| 3447 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3448 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3449 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3450 | if (nullfb) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3451 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3452 | xfs_inode_is_filestream(ap->ip)) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3453 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3454 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3455 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3456 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3457 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3458 | } |
| 3459 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3460 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3461 | |
| 3462 | xfs_bmap_adjacent(ap); |
| 3463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3465 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | * it's in the right allocation group. |
| 3467 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3468 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | ; |
| 3470 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3471 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3473 | * Normal allocation, done through xfs_alloc_vextent. |
| 3474 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3475 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3476 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3477 | args.tp = ap->tp; |
| 3478 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3479 | args.fsbno = ap->blkno; |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 3480 | xfs_rmap_skip_owner_update(&args.oinfo); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3481 | |
| 3482 | /* Trim the allocation back to the maximum an AG can fit. */ |
Darrick J. Wong | 5254885 | 2016-08-03 11:38:24 +1000 | [diff] [blame] | 3483 | args.maxlen = MIN(ap->length, mp->m_ag_max_usable); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3484 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3485 | blen = 0; |
| 3486 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3487 | /* |
| 3488 | * Search for an allocation group with a single extent large |
| 3489 | * enough for the request. If one isn't found, then adjust |
| 3490 | * the minimum allocation size to the largest space found. |
| 3491 | */ |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3492 | if (xfs_alloc_is_userdata(ap->datatype) && |
| 3493 | xfs_inode_is_filestream(ap->ip)) |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3494 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3495 | else |
| 3496 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3497 | if (error) |
| 3498 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3499 | } else if (ap->dfops->dop_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3500 | if (xfs_inode_is_filestream(ap->ip)) |
| 3501 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3502 | else |
| 3503 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3504 | args.total = args.minlen = ap->minlen; |
| 3505 | } else { |
| 3506 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3507 | args.total = ap->total; |
| 3508 | args.minlen = ap->minlen; |
| 3509 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3510 | /* apply extent size hints if obtained earlier */ |
Christoph Hellwig | 493611e | 2017-01-25 08:59:43 -0800 | [diff] [blame] | 3511 | if (align) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3512 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3513 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3514 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3515 | } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3516 | args.prod = 1; |
| 3517 | args.mod = 0; |
| 3518 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3519 | args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3520 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3521 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3522 | } |
| 3523 | /* |
| 3524 | * If we are not low on available data blocks, and the |
| 3525 | * underlying logical volume manager is a stripe, and |
| 3526 | * the file offset is zero then try to allocate data |
| 3527 | * blocks on stripe unit boundary. |
| 3528 | * NOTE: ap->aeof is only set if the allocation length |
| 3529 | * is >= the stripe unit and the allocation offset is |
| 3530 | * at the end of file. |
| 3531 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3532 | if (!ap->dfops->dop_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3533 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3534 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3535 | atype = args.type; |
| 3536 | isaligned = 1; |
| 3537 | /* |
| 3538 | * Adjust for alignment |
| 3539 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3540 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3541 | args.minlen = blen - args.alignment; |
| 3542 | args.minalignslop = 0; |
| 3543 | } else { |
| 3544 | /* |
| 3545 | * First try an exact bno allocation. |
| 3546 | * If it fails then do a near or start bno |
| 3547 | * allocation with alignment turned on. |
| 3548 | */ |
| 3549 | atype = args.type; |
| 3550 | tryagain = 1; |
| 3551 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3552 | args.alignment = 1; |
| 3553 | /* |
| 3554 | * Compute the minlen+alignment for the |
| 3555 | * next case. Set slop so that the value |
| 3556 | * of minlen+alignment+slop doesn't go up |
| 3557 | * between the calls. |
| 3558 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3559 | if (blen > stripe_align && blen <= args.maxlen) |
| 3560 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3561 | else |
| 3562 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3563 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3564 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3565 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3566 | args.minlen - 1; |
| 3567 | else |
| 3568 | args.minalignslop = 0; |
| 3569 | } |
| 3570 | } else { |
| 3571 | args.alignment = 1; |
| 3572 | args.minalignslop = 0; |
| 3573 | } |
| 3574 | args.minleft = ap->minleft; |
| 3575 | args.wasdel = ap->wasdel; |
Darrick J. Wong | 3fd129b | 2016-09-19 10:30:52 +1000 | [diff] [blame] | 3576 | args.resv = XFS_AG_RESV_NONE; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3577 | args.datatype = ap->datatype; |
| 3578 | if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3579 | args.ip = ap->ip; |
| 3580 | |
| 3581 | error = xfs_alloc_vextent(&args); |
| 3582 | if (error) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3583 | return error; |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 3584 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3585 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3586 | /* |
| 3587 | * Exact allocation failed. Now try with alignment |
| 3588 | * turned on. |
| 3589 | */ |
| 3590 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3591 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3592 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3593 | args.minlen = nextminlen; |
| 3594 | args.minalignslop = 0; |
| 3595 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | if ((error = xfs_alloc_vextent(&args))) |
| 3597 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3598 | } |
| 3599 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3600 | /* |
| 3601 | * allocation failed, so turn off alignment and |
| 3602 | * try again. |
| 3603 | */ |
| 3604 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3605 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3606 | args.alignment = 0; |
| 3607 | if ((error = xfs_alloc_vextent(&args))) |
| 3608 | return error; |
| 3609 | } |
| 3610 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3611 | args.minlen > ap->minlen) { |
| 3612 | args.minlen = ap->minlen; |
| 3613 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3614 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3615 | if ((error = xfs_alloc_vextent(&args))) |
| 3616 | return error; |
| 3617 | } |
| 3618 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3619 | args.fsbno = 0; |
| 3620 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3621 | args.total = ap->minlen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3622 | if ((error = xfs_alloc_vextent(&args))) |
| 3623 | return error; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 3624 | ap->dfops->dop_low = true; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3625 | } |
| 3626 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3627 | /* |
| 3628 | * check the allocation happened at the same or higher AG than |
| 3629 | * the first block that was allocated. |
| 3630 | */ |
| 3631 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3632 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) <= |
| 3633 | XFS_FSB_TO_AGNO(mp, args.fsbno)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3634 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3635 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3636 | if (*ap->firstblock == NULLFSBLOCK) |
| 3637 | *ap->firstblock = args.fsbno; |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 3638 | ASSERT(nullfb || fb_agno <= args.agno); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3639 | ap->length = args.len; |
Darrick J. Wong | 6d8a45c | 2018-01-19 17:47:36 -0800 | [diff] [blame] | 3640 | /* |
| 3641 | * If the extent size hint is active, we tried to round the |
| 3642 | * caller's allocation request offset down to extsz and the |
| 3643 | * length up to another extsz boundary. If we found a free |
| 3644 | * extent we mapped it in starting at this new offset. If the |
| 3645 | * newly mapped space isn't long enough to cover any of the |
| 3646 | * range of offsets that was originally requested, move the |
| 3647 | * mapping up so that we can fill as much of the caller's |
| 3648 | * original request as possible. Free space is apparently |
| 3649 | * very fragmented so we're unlikely to be able to satisfy the |
| 3650 | * hints anyway. |
| 3651 | */ |
| 3652 | if (ap->length <= orig_length) |
| 3653 | ap->offset = orig_offset; |
| 3654 | else if (ap->offset + ap->length < orig_offset + orig_length) |
| 3655 | ap->offset = orig_offset + orig_length - ap->length; |
Darrick J. Wong | 751f376 | 2018-01-25 13:58:13 -0800 | [diff] [blame] | 3656 | xfs_bmap_btalloc_accounting(ap, &args); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3657 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3658 | ap->blkno = NULLFSBLOCK; |
| 3659 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3660 | } |
| 3661 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | /* |
| 3665 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3666 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3667 | */ |
| 3668 | STATIC int |
| 3669 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3670 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3671 | { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 3672 | if (XFS_IS_REALTIME_INODE(ap->ip) && |
| 3673 | xfs_alloc_is_userdata(ap->datatype)) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3674 | return xfs_bmap_rtalloc(ap); |
| 3675 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | } |
| 3677 | |
Darrick J. Wong | 0a0af28 | 2016-10-20 15:51:50 +1100 | [diff] [blame] | 3678 | /* Trim extent to fit a logical block range. */ |
| 3679 | void |
| 3680 | xfs_trim_extent( |
| 3681 | struct xfs_bmbt_irec *irec, |
| 3682 | xfs_fileoff_t bno, |
| 3683 | xfs_filblks_t len) |
| 3684 | { |
| 3685 | xfs_fileoff_t distance; |
| 3686 | xfs_fileoff_t end = bno + len; |
| 3687 | |
| 3688 | if (irec->br_startoff + irec->br_blockcount <= bno || |
| 3689 | irec->br_startoff >= end) { |
| 3690 | irec->br_blockcount = 0; |
| 3691 | return; |
| 3692 | } |
| 3693 | |
| 3694 | if (irec->br_startoff < bno) { |
| 3695 | distance = bno - irec->br_startoff; |
| 3696 | if (isnullstartblock(irec->br_startblock)) |
| 3697 | irec->br_startblock = DELAYSTARTBLOCK; |
| 3698 | if (irec->br_startblock != DELAYSTARTBLOCK && |
| 3699 | irec->br_startblock != HOLESTARTBLOCK) |
| 3700 | irec->br_startblock += distance; |
| 3701 | irec->br_startoff += distance; |
| 3702 | irec->br_blockcount -= distance; |
| 3703 | } |
| 3704 | |
| 3705 | if (end < irec->br_startoff + irec->br_blockcount) { |
| 3706 | distance = irec->br_startoff + irec->br_blockcount - end; |
| 3707 | irec->br_blockcount -= distance; |
| 3708 | } |
| 3709 | } |
| 3710 | |
Brian Foster | 40214d1 | 2017-10-13 09:47:46 -0700 | [diff] [blame] | 3711 | /* trim extent to within eof */ |
| 3712 | void |
| 3713 | xfs_trim_extent_eof( |
| 3714 | struct xfs_bmbt_irec *irec, |
| 3715 | struct xfs_inode *ip) |
| 3716 | |
| 3717 | { |
| 3718 | xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount, |
| 3719 | i_size_read(VFS_I(ip)))); |
| 3720 | } |
| 3721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3722 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3723 | * Trim the returned map to the required bounds |
| 3724 | */ |
| 3725 | STATIC void |
| 3726 | xfs_bmapi_trim_map( |
| 3727 | struct xfs_bmbt_irec *mval, |
| 3728 | struct xfs_bmbt_irec *got, |
| 3729 | xfs_fileoff_t *bno, |
| 3730 | xfs_filblks_t len, |
| 3731 | xfs_fileoff_t obno, |
| 3732 | xfs_fileoff_t end, |
| 3733 | int n, |
| 3734 | int flags) |
| 3735 | { |
| 3736 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3737 | got->br_startoff + got->br_blockcount <= obno) { |
| 3738 | *mval = *got; |
| 3739 | if (isnullstartblock(got->br_startblock)) |
| 3740 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3741 | return; |
| 3742 | } |
| 3743 | |
| 3744 | if (obno > *bno) |
| 3745 | *bno = obno; |
| 3746 | ASSERT((*bno >= obno) || (n == 0)); |
| 3747 | ASSERT(*bno < end); |
| 3748 | mval->br_startoff = *bno; |
| 3749 | if (isnullstartblock(got->br_startblock)) |
| 3750 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3751 | else |
| 3752 | mval->br_startblock = got->br_startblock + |
| 3753 | (*bno - got->br_startoff); |
| 3754 | /* |
| 3755 | * Return the minimum of what we got and what we asked for for |
| 3756 | * the length. We can use the len variable here because it is |
| 3757 | * modified below and we could have been there before coming |
| 3758 | * here if the first part of the allocation didn't overlap what |
| 3759 | * was asked for. |
| 3760 | */ |
| 3761 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3762 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3763 | mval->br_state = got->br_state; |
| 3764 | ASSERT(mval->br_blockcount <= len); |
| 3765 | return; |
| 3766 | } |
| 3767 | |
| 3768 | /* |
| 3769 | * Update and validate the extent map to return |
| 3770 | */ |
| 3771 | STATIC void |
| 3772 | xfs_bmapi_update_map( |
| 3773 | struct xfs_bmbt_irec **map, |
| 3774 | xfs_fileoff_t *bno, |
| 3775 | xfs_filblks_t *len, |
| 3776 | xfs_fileoff_t obno, |
| 3777 | xfs_fileoff_t end, |
| 3778 | int *n, |
| 3779 | int flags) |
| 3780 | { |
| 3781 | xfs_bmbt_irec_t *mval = *map; |
| 3782 | |
| 3783 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3784 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3785 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3786 | (mval->br_startoff < obno)); |
| 3787 | |
| 3788 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3789 | *len = end - *bno; |
| 3790 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3791 | /* update previous map with new information */ |
| 3792 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3793 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3794 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3795 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3796 | mval[-1].br_state = mval->br_state; |
| 3797 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3798 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3799 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3800 | mval->br_startblock == mval[-1].br_startblock + |
| 3801 | mval[-1].br_blockcount && |
| 3802 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3803 | mval[-1].br_state == mval->br_state)) { |
| 3804 | ASSERT(mval->br_startoff == |
| 3805 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3806 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3807 | } else if (*n > 0 && |
| 3808 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3809 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3810 | mval->br_startoff == |
| 3811 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3812 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3813 | mval[-1].br_state = mval->br_state; |
| 3814 | } else if (!((*n == 0) && |
| 3815 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3816 | obno))) { |
| 3817 | mval++; |
| 3818 | (*n)++; |
| 3819 | } |
| 3820 | *map = mval; |
| 3821 | } |
| 3822 | |
| 3823 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3824 | * Map file blocks to filesystem blocks without allocation. |
| 3825 | */ |
| 3826 | int |
| 3827 | xfs_bmapi_read( |
| 3828 | struct xfs_inode *ip, |
| 3829 | xfs_fileoff_t bno, |
| 3830 | xfs_filblks_t len, |
| 3831 | struct xfs_bmbt_irec *mval, |
| 3832 | int *nmap, |
| 3833 | int flags) |
| 3834 | { |
| 3835 | struct xfs_mount *mp = ip->i_mount; |
| 3836 | struct xfs_ifork *ifp; |
| 3837 | struct xfs_bmbt_irec got; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3838 | xfs_fileoff_t obno; |
| 3839 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3840 | struct xfs_iext_cursor icur; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3841 | int error; |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3842 | bool eof = false; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3843 | int n = 0; |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3844 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3845 | |
| 3846 | ASSERT(*nmap >= 1); |
| 3847 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3848 | XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 3849 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3850 | |
| 3851 | if (unlikely(XFS_TEST_ERROR( |
| 3852 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3853 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 3854 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3855 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3856 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3860 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3861 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 3862 | XFS_STATS_INC(mp, xs_blk_mapr); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3863 | |
| 3864 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3865 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3866 | /* No CoW fork? Return a hole. */ |
| 3867 | if (whichfork == XFS_COW_FORK && !ifp) { |
| 3868 | mval->br_startoff = bno; |
| 3869 | mval->br_startblock = HOLESTARTBLOCK; |
| 3870 | mval->br_blockcount = len; |
| 3871 | mval->br_state = XFS_EXT_NORM; |
| 3872 | *nmap = 1; |
| 3873 | return 0; |
| 3874 | } |
| 3875 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3876 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3877 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 3878 | if (error) |
| 3879 | return error; |
| 3880 | } |
| 3881 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3882 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3883 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3884 | end = bno + len; |
| 3885 | obno = bno; |
| 3886 | |
| 3887 | while (bno < end && n < *nmap) { |
| 3888 | /* Reading past eof, act as though there's a hole up to end. */ |
| 3889 | if (eof) |
| 3890 | got.br_startoff = end; |
| 3891 | if (got.br_startoff > bno) { |
| 3892 | /* Reading in a hole. */ |
| 3893 | mval->br_startoff = bno; |
| 3894 | mval->br_startblock = HOLESTARTBLOCK; |
| 3895 | mval->br_blockcount = |
| 3896 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 3897 | mval->br_state = XFS_EXT_NORM; |
| 3898 | bno += mval->br_blockcount; |
| 3899 | len -= mval->br_blockcount; |
| 3900 | mval++; |
| 3901 | n++; |
| 3902 | continue; |
| 3903 | } |
| 3904 | |
| 3905 | /* set up the extent map to return. */ |
| 3906 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 3907 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 3908 | |
| 3909 | /* If we're done, stop now. */ |
| 3910 | if (bno >= end || n >= *nmap) |
| 3911 | break; |
| 3912 | |
| 3913 | /* Else go on to the next record. */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3914 | if (!xfs_iext_next_extent(ifp, &icur, &got)) |
Christoph Hellwig | 334f342 | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 3915 | eof = true; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3916 | } |
| 3917 | *nmap = n; |
| 3918 | return 0; |
| 3919 | } |
| 3920 | |
Brian Foster | f65e6fa | 2017-03-08 09:58:08 -0800 | [diff] [blame] | 3921 | /* |
| 3922 | * Add a delayed allocation extent to an inode. Blocks are reserved from the |
| 3923 | * global pool and the extent inserted into the inode in-core extent tree. |
| 3924 | * |
| 3925 | * On entry, got refers to the first extent beyond the offset of the extent to |
| 3926 | * allocate or eof is specified if no such extent exists. On return, got refers |
| 3927 | * to the extent record that was inserted to the inode fork. |
| 3928 | * |
| 3929 | * Note that the allocated extent may have been merged with contiguous extents |
| 3930 | * during insertion into the inode fork. Thus, got does not reflect the current |
| 3931 | * state of the inode fork on return. If necessary, the caller can use lastx to |
| 3932 | * look up the updated record in the inode fork. |
| 3933 | */ |
Christoph Hellwig | 51446f5 | 2016-09-19 11:10:21 +1000 | [diff] [blame] | 3934 | int |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3935 | xfs_bmapi_reserve_delalloc( |
| 3936 | struct xfs_inode *ip, |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3937 | int whichfork, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3938 | xfs_fileoff_t off, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3939 | xfs_filblks_t len, |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3940 | xfs_filblks_t prealloc, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3941 | struct xfs_bmbt_irec *got, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3942 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3943 | int eof) |
| 3944 | { |
| 3945 | struct xfs_mount *mp = ip->i_mount; |
Darrick J. Wong | be51f81 | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 3946 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3947 | xfs_extlen_t alen; |
| 3948 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3949 | int error; |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3950 | xfs_fileoff_t aoff = off; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3951 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3952 | /* |
| 3953 | * Cap the alloc length. Keep track of prealloc so we know whether to |
| 3954 | * tag the inode before we return. |
| 3955 | */ |
| 3956 | alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3957 | if (!eof) |
| 3958 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 3959 | if (prealloc && alen >= len) |
| 3960 | prealloc = alen - len; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3961 | |
| 3962 | /* Figure out the extent size, adjust alen */ |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3963 | if (whichfork == XFS_COW_FORK) { |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3964 | struct xfs_bmbt_irec prev; |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3965 | xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip); |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3966 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 3967 | if (!xfs_iext_peek_prev_extent(ifp, icur, &prev)) |
Christoph Hellwig | 65c5f41 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 3968 | prev.br_startoff = NULLFILEOFF; |
| 3969 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3970 | error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof, |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3971 | 1, 0, &aoff, &alen); |
| 3972 | ASSERT(!error); |
| 3973 | } |
| 3974 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3975 | /* |
| 3976 | * Make a transaction-less quota reservation for delayed allocation |
| 3977 | * blocks. This number gets adjusted later. We return if we haven't |
| 3978 | * allocated blocks already inside this loop. |
| 3979 | */ |
| 3980 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3981 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3982 | if (error) |
| 3983 | return error; |
| 3984 | |
| 3985 | /* |
| 3986 | * Split changing sb for alen and indlen since they could be coming |
| 3987 | * from different places. |
| 3988 | */ |
| 3989 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 3990 | ASSERT(indlen > 0); |
| 3991 | |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 3992 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3993 | if (error) |
| 3994 | goto out_unreserve_quota; |
| 3995 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 3996 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 3997 | if (error) |
| 3998 | goto out_unreserve_blocks; |
| 3999 | |
| 4000 | |
| 4001 | ip->i_delayed_blks += alen; |
| 4002 | |
| 4003 | got->br_startoff = aoff; |
| 4004 | got->br_startblock = nullstartblock(indlen); |
| 4005 | got->br_blockcount = alen; |
| 4006 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4007 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4008 | xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4009 | |
Brian Foster | 974ae92 | 2016-11-28 14:57:42 +1100 | [diff] [blame] | 4010 | /* |
| 4011 | * Tag the inode if blocks were preallocated. Note that COW fork |
| 4012 | * preallocation can occur at the start or end of the extent, even when |
| 4013 | * prealloc == 0, so we must also check the aligned offset and length. |
| 4014 | */ |
| 4015 | if (whichfork == XFS_DATA_FORK && prealloc) |
| 4016 | xfs_inode_set_eofblocks_tag(ip); |
| 4017 | if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len)) |
| 4018 | xfs_inode_set_cowblocks_tag(ip); |
| 4019 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4020 | return 0; |
| 4021 | |
| 4022 | out_unreserve_blocks: |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4023 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4024 | out_unreserve_quota: |
| 4025 | if (XFS_IS_QUOTA_ON(mp)) |
Shan Hai | 6ca3072 | 2018-01-23 13:56:11 -0800 | [diff] [blame] | 4026 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4027 | XFS_QMOPT_RES_REGBLKS); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4028 | return error; |
| 4029 | } |
| 4030 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4031 | static int |
| 4032 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4033 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4034 | { |
| 4035 | struct xfs_mount *mp = bma->ip->i_mount; |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4036 | int whichfork = xfs_bmapi_whichfork(bma->flags); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4037 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4038 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4039 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4040 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4041 | ASSERT(bma->length > 0); |
| 4042 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4043 | /* |
| 4044 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4045 | * for in this bmap call but that wouldn't be as good. |
| 4046 | */ |
| 4047 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4048 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4049 | bma->offset = bma->got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4050 | xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4051 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4052 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4053 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4054 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4055 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | /* |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4059 | * Set the data type being allocated. For the data fork, the first data |
| 4060 | * in the file is treated differently to all other allocations. For the |
| 4061 | * attribute fork, we only need to ensure the allocated range is not on |
| 4062 | * the busy list. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4063 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4064 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4065 | bma->datatype = XFS_ALLOC_NOBUSY; |
| 4066 | if (whichfork == XFS_DATA_FORK) { |
| 4067 | if (bma->offset == 0) |
| 4068 | bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA; |
| 4069 | else |
| 4070 | bma->datatype |= XFS_ALLOC_USERDATA; |
| 4071 | } |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4072 | if (bma->flags & XFS_BMAPI_ZERO) |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4073 | bma->datatype |= XFS_ALLOC_USERDATA_ZERO; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4076 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4077 | |
| 4078 | /* |
| 4079 | * Only want to do the alignment at the eof if it is userdata and |
| 4080 | * allocation length is larger than a stripe unit. |
| 4081 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4082 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4083 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4084 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4085 | if (error) |
| 4086 | return error; |
| 4087 | } |
| 4088 | |
| 4089 | error = xfs_bmap_alloc(bma); |
| 4090 | if (error) |
| 4091 | return error; |
| 4092 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4093 | if (bma->cur) |
| 4094 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4095 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4096 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4097 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4098 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4099 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4100 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4101 | } |
| 4102 | /* |
| 4103 | * Bump the number of extents we've allocated |
| 4104 | * in this call. |
| 4105 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4106 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4107 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4108 | if (bma->cur) |
| 4109 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4110 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4111 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4112 | bma->got.br_startoff = bma->offset; |
| 4113 | bma->got.br_startblock = bma->blkno; |
| 4114 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4115 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4116 | |
| 4117 | /* |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4118 | * In the data fork, a wasdelay extent has been initialized, so |
| 4119 | * shouldn't be flagged as unwritten. |
| 4120 | * |
| 4121 | * For the cow fork, however, we convert delalloc reservations |
| 4122 | * (extents allocated for speculative preallocation) to |
| 4123 | * allocated unwritten extents, and only convert the unwritten |
| 4124 | * extents to real extents when we're about to write the data. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4125 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4126 | if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) && |
| 4127 | (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4128 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4129 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4130 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4131 | if (bma->wasdel) |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4132 | error = xfs_bmap_add_extent_delay_real(bma, whichfork); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4133 | else |
Christoph Hellwig | 6d04558 | 2017-04-11 16:45:54 -0700 | [diff] [blame] | 4134 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4135 | whichfork, &bma->icur, &bma->cur, &bma->got, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 4136 | bma->firstblock, bma->dfops, &bma->logflags, |
| 4137 | bma->flags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4138 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4139 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4140 | if (error) |
| 4141 | return error; |
| 4142 | |
| 4143 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4144 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4145 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4146 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4147 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4148 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4149 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4150 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4151 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4152 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4153 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4154 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4155 | return 0; |
| 4156 | } |
| 4157 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4158 | STATIC int |
| 4159 | xfs_bmapi_convert_unwritten( |
| 4160 | struct xfs_bmalloca *bma, |
| 4161 | struct xfs_bmbt_irec *mval, |
| 4162 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4163 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4164 | { |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 4165 | int whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4166 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4167 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4168 | int error; |
| 4169 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4170 | /* check if we need to do unwritten->real conversion */ |
| 4171 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4172 | (flags & XFS_BMAPI_PREALLOC)) |
| 4173 | return 0; |
| 4174 | |
| 4175 | /* check if we need to do real->unwritten conversion */ |
| 4176 | if (mval->br_state == XFS_EXT_NORM && |
| 4177 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4178 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4179 | return 0; |
| 4180 | |
| 4181 | /* |
| 4182 | * Modify (by adding) the state flag, if writing. |
| 4183 | */ |
| 4184 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4185 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4186 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4187 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4188 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4189 | bma->cur->bc_private.b.dfops = bma->dfops; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4190 | } |
| 4191 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4192 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4193 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4194 | /* |
| 4195 | * Before insertion into the bmbt, zero the range being converted |
| 4196 | * if required. |
| 4197 | */ |
| 4198 | if (flags & XFS_BMAPI_ZERO) { |
| 4199 | error = xfs_zero_extent(bma->ip, mval->br_startblock, |
| 4200 | mval->br_blockcount); |
| 4201 | if (error) |
| 4202 | return error; |
| 4203 | } |
| 4204 | |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4205 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4206 | &bma->icur, &bma->cur, mval, bma->firstblock, |
| 4207 | bma->dfops, &tmp_logflags); |
Brian Foster | 2e588a4 | 2015-06-01 07:15:23 +1000 | [diff] [blame] | 4208 | /* |
| 4209 | * Log the inode core unconditionally in the unwritten extent conversion |
| 4210 | * path because the conversion might not have done so (e.g., if the |
| 4211 | * extent count hasn't changed). We need to make sure the inode is dirty |
| 4212 | * in the transaction for the sake of fsync(), even if nothing has |
| 4213 | * changed, because fsync() will not force the log for this transaction |
| 4214 | * unless it sees the inode pinned. |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4215 | * |
| 4216 | * Note: If we're only converting cow fork extents, there aren't |
| 4217 | * 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] | 4218 | */ |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4219 | if (whichfork != XFS_COW_FORK) |
| 4220 | bma->logflags |= tmp_logflags | XFS_ILOG_CORE; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4221 | if (error) |
| 4222 | return error; |
| 4223 | |
| 4224 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4225 | * Update our extent pointer, given that |
| 4226 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4227 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4228 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4229 | xfs_iext_get_extent(ifp, &bma->icur, &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4230 | |
| 4231 | /* |
| 4232 | * We may have combined previously unwritten space with written space, |
| 4233 | * so generate another request. |
| 4234 | */ |
| 4235 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4236 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4237 | return 0; |
| 4238 | } |
| 4239 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4240 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4241 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4242 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4243 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4244 | * locking problems. |
| 4245 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | * The returned value in "firstblock" from the first call in a transaction |
| 4247 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4248 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4249 | * the first call in "total"; if no allocation group has that many free |
| 4250 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4251 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4252 | int |
| 4253 | xfs_bmapi_write( |
| 4254 | struct xfs_trans *tp, /* transaction pointer */ |
| 4255 | struct xfs_inode *ip, /* incore inode */ |
| 4256 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4257 | xfs_filblks_t len, /* length to map in file */ |
| 4258 | int flags, /* XFS_BMAPI_... */ |
| 4259 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4260 | controls a.g. for allocs */ |
| 4261 | xfs_extlen_t total, /* total blocks needed */ |
| 4262 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4263 | int *nmap, /* i/o: mval size/count */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4264 | struct xfs_defer_ops *dfops) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4266 | struct xfs_mount *mp = ip->i_mount; |
| 4267 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4268 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4269 | xfs_fileoff_t end; /* end of mapped file region */ |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4270 | bool eof = false; /* after the end of extents */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4271 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4272 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4273 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4274 | int whichfork; /* data or attr fork */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4277 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4278 | int orig_flags; /* original flags arg value */ |
| 4279 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4280 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4281 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | |
| 4283 | orig_bno = bno; |
| 4284 | orig_len = len; |
| 4285 | orig_flags = flags; |
| 4286 | orig_mval = mval; |
| 4287 | orig_nmap = *nmap; |
| 4288 | #endif |
Darrick J. Wong | 60b4984 | 2016-10-03 09:11:34 -0700 | [diff] [blame] | 4289 | whichfork = xfs_bmapi_whichfork(flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4292 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4293 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 4294 | ASSERT(tp != NULL || |
| 4295 | (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) == |
| 4296 | (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4297 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4298 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4299 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4300 | ASSERT(!(flags & XFS_BMAPI_REMAP)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4301 | |
Dave Chinner | 3fbbbea | 2015-11-03 12:27:22 +1100 | [diff] [blame] | 4302 | /* zeroing is for currently only for data extents, not metadata */ |
| 4303 | ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) != |
| 4304 | (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)); |
| 4305 | /* |
| 4306 | * we can allocate unwritten extents or pre-zero allocated blocks, |
| 4307 | * but it makes no sense to do both at once. This would result in |
| 4308 | * zeroing the unwritten extent twice, but it still being an |
| 4309 | * unwritten extent.... |
| 4310 | */ |
| 4311 | ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) != |
| 4312 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)); |
| 4313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | if (unlikely(XFS_TEST_ERROR( |
| 4315 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4316 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4317 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4318 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4319 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4323 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4326 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4327 | XFS_STATS_INC(mp, xs_blk_mapw); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4328 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4329 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4331 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4333 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4334 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4335 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4339 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4340 | if (error) |
| 4341 | goto error0; |
| 4342 | } |
| 4343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | n = 0; |
| 4345 | end = bno + len; |
| 4346 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4347 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4348 | if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4349 | eof = true; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4350 | if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4351 | bma.prev.br_startoff = NULLFILEOFF; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4352 | bma.tp = tp; |
| 4353 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4354 | bma.total = total; |
Dave Chinner | 292378e | 2016-09-26 08:21:28 +1000 | [diff] [blame] | 4355 | bma.datatype = 0; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4356 | bma.dfops = dfops; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4357 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | while (bno < end && n < *nmap) { |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4360 | bool need_alloc = false, wasdelay = false; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4361 | |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4362 | /* in hole or beyond EOF? */ |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4363 | if (eof || bma.got.br_startoff > bno) { |
Darrick J. Wong | be78ff0 | 2018-01-16 19:03:59 -0800 | [diff] [blame] | 4364 | /* |
| 4365 | * CoW fork conversions should /never/ hit EOF or |
| 4366 | * holes. There should always be something for us |
| 4367 | * to work on. |
| 4368 | */ |
| 4369 | ASSERT(!((flags & XFS_BMAPI_CONVERT) && |
| 4370 | (flags & XFS_BMAPI_COWFORK))); |
| 4371 | |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4372 | if (flags & XFS_BMAPI_DELALLOC) { |
| 4373 | /* |
| 4374 | * For the COW fork we can reasonably get a |
| 4375 | * request for converting an extent that races |
| 4376 | * with other threads already having converted |
| 4377 | * part of it, as there converting COW to |
| 4378 | * regular blocks is not protected using the |
| 4379 | * IOLOCK. |
| 4380 | */ |
| 4381 | ASSERT(flags & XFS_BMAPI_COWFORK); |
| 4382 | if (!(flags & XFS_BMAPI_COWFORK)) { |
| 4383 | error = -EIO; |
| 4384 | goto error0; |
| 4385 | } |
| 4386 | |
| 4387 | if (eof || bno >= end) |
| 4388 | break; |
| 4389 | } else { |
| 4390 | need_alloc = true; |
| 4391 | } |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4392 | } else if (isnullstartblock(bma.got.br_startblock)) { |
| 4393 | wasdelay = true; |
Christoph Hellwig | d2b3964 | 2017-01-20 09:31:54 -0800 | [diff] [blame] | 4394 | } |
Darrick J. Wong | f65306e | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4395 | |
| 4396 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | * First, deal with the hole before the allocated space |
| 4398 | * that we found, if any. |
| 4399 | */ |
Christoph Hellwig | b121459 | 2017-11-03 10:34:44 -0700 | [diff] [blame] | 4400 | if ((need_alloc || wasdelay) && |
| 4401 | !(flags & XFS_BMAPI_CONVERT_ONLY)) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4402 | bma.eof = eof; |
| 4403 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4404 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4405 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4406 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4407 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4408 | /* |
| 4409 | * There's a 32/64 bit type mismatch between the |
| 4410 | * allocation length request (which can be 64 bits in |
| 4411 | * length) and the bma length request, which is |
| 4412 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4413 | * check for 32-bit overflows and handle them here. |
| 4414 | */ |
| 4415 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4416 | bma.length = MAXEXTLEN; |
| 4417 | else |
| 4418 | bma.length = len; |
| 4419 | |
| 4420 | ASSERT(len > 0); |
| 4421 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4422 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | if (error) |
| 4424 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4425 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4426 | break; |
Darrick J. Wong | 174edb0 | 2016-10-03 09:11:39 -0700 | [diff] [blame] | 4427 | |
| 4428 | /* |
| 4429 | * If this is a CoW allocation, record the data in |
| 4430 | * the refcount btree for orphan recovery. |
| 4431 | */ |
| 4432 | if (whichfork == XFS_COW_FORK) { |
| 4433 | error = xfs_refcount_alloc_cow_extent(mp, dfops, |
| 4434 | bma.blkno, bma.length); |
| 4435 | if (error) |
| 4436 | goto error0; |
| 4437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4439 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4440 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4441 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4442 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4444 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4445 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4446 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4447 | continue; |
| 4448 | if (error) |
| 4449 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4451 | /* update the extent map to return */ |
| 4452 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | /* |
| 4455 | * If we're done, stop now. Stop when we've allocated |
| 4456 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4457 | * the transaction may get too big. |
| 4458 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4459 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4461 | |
| 4462 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4463 | bma.prev = bma.got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4464 | if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got)) |
Christoph Hellwig | 2d58f6e | 2016-11-24 11:39:43 +1100 | [diff] [blame] | 4465 | eof = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | /* |
| 4470 | * Transform from btree to extents, give it cur. |
| 4471 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4472 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4473 | int tmp_logflags = 0; |
| 4474 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4475 | ASSERT(bma.cur); |
| 4476 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4478 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | if (error) |
| 4480 | goto error0; |
| 4481 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4484 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4485 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | error0: |
| 4488 | /* |
| 4489 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4490 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4492 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4494 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4495 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4497 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | /* |
| 4499 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4500 | * detecting a case where the data is changed, there's an error, |
| 4501 | * and it's not logged so we don't shutdown when we should. |
| 4502 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4503 | if (bma.logflags) |
| 4504 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4505 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4506 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | if (!error) { |
| 4508 | ASSERT(*firstblock == NULLFSBLOCK || |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4509 | XFS_FSB_TO_AGNO(mp, *firstblock) <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | XFS_FSB_TO_AGNO(mp, |
Christoph Hellwig | 410d17f | 2017-02-16 17:12:51 -0800 | [diff] [blame] | 4511 | bma.cur->bc_private.b.firstblock)); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4512 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4514 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4516 | } |
| 4517 | if (!error) |
| 4518 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4519 | orig_nmap, *nmap); |
| 4520 | return error; |
| 4521 | } |
| 4522 | |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4523 | static int |
| 4524 | xfs_bmapi_remap( |
| 4525 | struct xfs_trans *tp, |
| 4526 | struct xfs_inode *ip, |
| 4527 | xfs_fileoff_t bno, |
| 4528 | xfs_filblks_t len, |
| 4529 | xfs_fsblock_t startblock, |
| 4530 | struct xfs_defer_ops *dfops) |
| 4531 | { |
| 4532 | struct xfs_mount *mp = ip->i_mount; |
| 4533 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4534 | struct xfs_btree_cur *cur = NULL; |
| 4535 | xfs_fsblock_t firstblock = NULLFSBLOCK; |
| 4536 | struct xfs_bmbt_irec got; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4537 | struct xfs_iext_cursor icur; |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4538 | int logflags = 0, error; |
| 4539 | |
| 4540 | ASSERT(len > 0); |
| 4541 | ASSERT(len <= (xfs_filblks_t)MAXEXTLEN); |
| 4542 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 4543 | |
| 4544 | if (unlikely(XFS_TEST_ERROR( |
| 4545 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4546 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 4547 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4548 | XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp); |
| 4549 | return -EFSCORRUPTED; |
| 4550 | } |
| 4551 | |
| 4552 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4553 | return -EIO; |
| 4554 | |
| 4555 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4556 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4557 | if (error) |
| 4558 | return error; |
| 4559 | } |
| 4560 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4561 | if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) { |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4562 | /* make sure we only reflink into a hole. */ |
| 4563 | ASSERT(got.br_startoff > bno); |
| 4564 | ASSERT(got.br_startoff - bno >= len); |
| 4565 | } |
| 4566 | |
Christoph Hellwig | bf8eadb | 2017-04-11 16:45:56 -0700 | [diff] [blame] | 4567 | ip->i_d.di_nblocks += len; |
| 4568 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4569 | |
| 4570 | if (ifp->if_flags & XFS_IFBROOT) { |
| 4571 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
| 4572 | cur->bc_private.b.firstblock = firstblock; |
| 4573 | cur->bc_private.b.dfops = dfops; |
| 4574 | cur->bc_private.b.flags = 0; |
| 4575 | } |
| 4576 | |
| 4577 | got.br_startoff = bno; |
| 4578 | got.br_startblock = startblock; |
| 4579 | got.br_blockcount = len; |
| 4580 | got.br_state = XFS_EXT_NORM; |
| 4581 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4582 | error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur, |
Darrick J. Wong | 95eb308 | 2018-05-09 10:02:32 -0700 | [diff] [blame] | 4583 | &cur, &got, &firstblock, dfops, &logflags, 0); |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 4584 | if (error) |
| 4585 | goto error0; |
| 4586 | |
| 4587 | if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) { |
| 4588 | int tmp_logflags = 0; |
| 4589 | |
| 4590 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4591 | &tmp_logflags, XFS_DATA_FORK); |
| 4592 | logflags |= tmp_logflags; |
| 4593 | } |
| 4594 | |
| 4595 | error0: |
| 4596 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) |
| 4597 | logflags &= ~XFS_ILOG_DEXT; |
| 4598 | else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) |
| 4599 | logflags &= ~XFS_ILOG_DBROOT; |
| 4600 | |
| 4601 | if (logflags) |
| 4602 | xfs_trans_log_inode(tp, ip, logflags); |
| 4603 | if (cur) { |
| 4604 | xfs_btree_del_cursor(cur, |
| 4605 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4606 | } |
| 4607 | return error; |
| 4608 | } |
| 4609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | /* |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4611 | * When a delalloc extent is split (e.g., due to a hole punch), the original |
| 4612 | * indlen reservation must be shared across the two new extents that are left |
| 4613 | * behind. |
| 4614 | * |
| 4615 | * Given the original reservation and the worst case indlen for the two new |
| 4616 | * extents (as calculated by xfs_bmap_worst_indlen()), split the original |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4617 | * reservation fairly across the two new extents. If necessary, steal available |
| 4618 | * blocks from a deleted extent to make up a reservation deficiency (e.g., if |
| 4619 | * ores == 1). The number of stolen blocks is returned. The availability and |
| 4620 | * subsequent accounting of stolen blocks is the responsibility of the caller. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4621 | */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4622 | static xfs_filblks_t |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4623 | xfs_bmap_split_indlen( |
| 4624 | xfs_filblks_t ores, /* original res. */ |
| 4625 | xfs_filblks_t *indlen1, /* ext1 worst indlen */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4626 | xfs_filblks_t *indlen2, /* ext2 worst indlen */ |
| 4627 | xfs_filblks_t avail) /* stealable blocks */ |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4628 | { |
| 4629 | xfs_filblks_t len1 = *indlen1; |
| 4630 | xfs_filblks_t len2 = *indlen2; |
| 4631 | xfs_filblks_t nres = len1 + len2; /* new total res. */ |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4632 | xfs_filblks_t stolen = 0; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4633 | xfs_filblks_t resfactor; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4634 | |
| 4635 | /* |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4636 | * Steal as many blocks as we can to try and satisfy the worst case |
| 4637 | * indlen for both new extents. |
| 4638 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4639 | if (ores < nres && avail) |
| 4640 | stolen = XFS_FILBLKS_MIN(nres - ores, avail); |
| 4641 | ores += stolen; |
| 4642 | |
| 4643 | /* nothing else to do if we've satisfied the new reservation */ |
| 4644 | if (ores >= nres) |
| 4645 | return stolen; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4646 | |
| 4647 | /* |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4648 | * We can't meet the total required reservation for the two extents. |
| 4649 | * Calculate the percent of the overall shortage between both extents |
| 4650 | * and apply this percentage to each of the requested indlen values. |
| 4651 | * This distributes the shortage fairly and reduces the chances that one |
| 4652 | * of the two extents is left with nothing when extents are repeatedly |
| 4653 | * split. |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4654 | */ |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4655 | resfactor = (ores * 100); |
| 4656 | do_div(resfactor, nres); |
| 4657 | len1 *= resfactor; |
| 4658 | do_div(len1, 100); |
| 4659 | len2 *= resfactor; |
| 4660 | do_div(len2, 100); |
| 4661 | ASSERT(len1 + len2 <= ores); |
| 4662 | ASSERT(len1 < *indlen1 && len2 < *indlen2); |
| 4663 | |
| 4664 | /* |
| 4665 | * Hand out the remainder to each extent. If one of the two reservations |
| 4666 | * is zero, we want to make sure that one gets a block first. The loop |
| 4667 | * below starts with len1, so hand len2 a block right off the bat if it |
| 4668 | * is zero. |
| 4669 | */ |
| 4670 | ores -= (len1 + len2); |
| 4671 | ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores); |
| 4672 | if (ores && !len2 && *indlen2) { |
| 4673 | len2++; |
| 4674 | ores--; |
| 4675 | } |
| 4676 | while (ores) { |
| 4677 | if (len1 < *indlen1) { |
| 4678 | len1++; |
| 4679 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4680 | } |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4681 | if (!ores) |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4682 | break; |
Brian Foster | 75d6536 | 2017-02-13 22:48:30 -0800 | [diff] [blame] | 4683 | if (len2 < *indlen2) { |
| 4684 | len2++; |
| 4685 | ores--; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | *indlen1 = len1; |
| 4690 | *indlen2 = len2; |
Brian Foster | d34999c | 2016-03-15 11:42:47 +1100 | [diff] [blame] | 4691 | |
| 4692 | return stolen; |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4693 | } |
| 4694 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4695 | int |
| 4696 | xfs_bmap_del_extent_delay( |
| 4697 | struct xfs_inode *ip, |
| 4698 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4699 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4700 | struct xfs_bmbt_irec *got, |
| 4701 | struct xfs_bmbt_irec *del) |
| 4702 | { |
| 4703 | struct xfs_mount *mp = ip->i_mount; |
| 4704 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4705 | struct xfs_bmbt_irec new; |
| 4706 | int64_t da_old, da_new, da_diff = 0; |
| 4707 | xfs_fileoff_t del_endoff, got_endoff; |
| 4708 | xfs_filblks_t got_indlen, new_indlen, stolen; |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4709 | int state = xfs_bmap_fork_to_state(whichfork); |
| 4710 | int error = 0; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4711 | bool isrt; |
| 4712 | |
| 4713 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4714 | |
| 4715 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
| 4716 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4717 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4718 | da_old = startblockval(got->br_startblock); |
| 4719 | da_new = 0; |
| 4720 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4721 | ASSERT(del->br_blockcount > 0); |
| 4722 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4723 | ASSERT(got_endoff >= del_endoff); |
| 4724 | |
| 4725 | if (isrt) { |
Eric Sandeen | 4f1adf3 | 2017-04-19 15:19:32 -0700 | [diff] [blame] | 4726 | uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4727 | |
| 4728 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 4729 | xfs_mod_frextents(mp, rtexts); |
| 4730 | } |
| 4731 | |
| 4732 | /* |
| 4733 | * Update the inode delalloc counter now and wait to update the |
| 4734 | * sb counters as we might have to borrow some blocks for the |
| 4735 | * indirect block accounting. |
| 4736 | */ |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4737 | error = xfs_trans_reserve_quota_nblks(NULL, ip, |
| 4738 | -((long)del->br_blockcount), 0, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4739 | isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
Darrick J. Wong | 4fd29ec4 | 2016-11-08 11:59:26 +1100 | [diff] [blame] | 4740 | if (error) |
| 4741 | return error; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4742 | ip->i_delayed_blks -= del->br_blockcount; |
| 4743 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4744 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4745 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4746 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4747 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4748 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4749 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4750 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4751 | /* |
| 4752 | * Matches the whole extent. Delete the entry. |
| 4753 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4754 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4755 | xfs_iext_prev(ifp, icur); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4756 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4757 | case BMAP_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4758 | /* |
| 4759 | * Deleting the first part of the extent. |
| 4760 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4761 | got->br_startoff = del_endoff; |
| 4762 | got->br_blockcount -= del->br_blockcount; |
| 4763 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4764 | got->br_blockcount), da_old); |
| 4765 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4766 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4767 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4768 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4769 | /* |
| 4770 | * Deleting the last part of the extent. |
| 4771 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4772 | got->br_blockcount = got->br_blockcount - del->br_blockcount; |
| 4773 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, |
| 4774 | got->br_blockcount), da_old); |
| 4775 | got->br_startblock = nullstartblock((int)da_new); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4776 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4777 | break; |
| 4778 | case 0: |
| 4779 | /* |
| 4780 | * Deleting the middle of the extent. |
| 4781 | * |
| 4782 | * Distribute the original indlen reservation across the two new |
| 4783 | * extents. Steal blocks from the deleted extent if necessary. |
| 4784 | * Stealing blocks simply fudges the fdblocks accounting below. |
| 4785 | * Warn if either of the new indlen reservations is zero as this |
| 4786 | * can lead to delalloc problems. |
| 4787 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4788 | got->br_blockcount = del->br_startoff - got->br_startoff; |
| 4789 | got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount); |
| 4790 | |
| 4791 | new.br_blockcount = got_endoff - del_endoff; |
| 4792 | new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount); |
| 4793 | |
| 4794 | WARN_ON_ONCE(!got_indlen || !new_indlen); |
| 4795 | stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen, |
| 4796 | del->br_blockcount); |
| 4797 | |
| 4798 | got->br_startblock = nullstartblock((int)got_indlen); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4799 | |
| 4800 | new.br_startoff = del_endoff; |
| 4801 | new.br_state = got->br_state; |
| 4802 | new.br_startblock = nullstartblock((int)new_indlen); |
| 4803 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4804 | xfs_iext_update_extent(ip, state, icur, got); |
| 4805 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4806 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4807 | |
| 4808 | da_new = got_indlen + new_indlen - stolen; |
| 4809 | del->br_blockcount -= stolen; |
| 4810 | break; |
| 4811 | } |
| 4812 | |
| 4813 | ASSERT(da_old >= da_new); |
| 4814 | da_diff = da_old - da_new; |
| 4815 | if (!isrt) |
| 4816 | da_diff += del->br_blockcount; |
| 4817 | if (da_diff) |
| 4818 | xfs_mod_fdblocks(mp, da_diff, false); |
| 4819 | return error; |
| 4820 | } |
| 4821 | |
| 4822 | void |
| 4823 | xfs_bmap_del_extent_cow( |
| 4824 | struct xfs_inode *ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4825 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4826 | struct xfs_bmbt_irec *got, |
| 4827 | struct xfs_bmbt_irec *del) |
| 4828 | { |
| 4829 | struct xfs_mount *mp = ip->i_mount; |
| 4830 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); |
| 4831 | struct xfs_bmbt_irec new; |
| 4832 | xfs_fileoff_t del_endoff, got_endoff; |
| 4833 | int state = BMAP_COWFORK; |
| 4834 | |
| 4835 | XFS_STATS_INC(mp, xs_del_exlist); |
| 4836 | |
| 4837 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4838 | got_endoff = got->br_startoff + got->br_blockcount; |
| 4839 | |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4840 | ASSERT(del->br_blockcount > 0); |
| 4841 | ASSERT(got->br_startoff <= del->br_startoff); |
| 4842 | ASSERT(got_endoff >= del_endoff); |
| 4843 | ASSERT(!isnullstartblock(got->br_startblock)); |
| 4844 | |
| 4845 | if (got->br_startoff == del->br_startoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4846 | state |= BMAP_LEFT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4847 | if (got_endoff == del_endoff) |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4848 | state |= BMAP_RIGHT_FILLING; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4849 | |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4850 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4851 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4852 | /* |
| 4853 | * Matches the whole extent. Delete the entry. |
| 4854 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4855 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4856 | xfs_iext_prev(ifp, icur); |
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_LEFT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4859 | /* |
| 4860 | * Deleting the first part of the extent. |
| 4861 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4862 | got->br_startoff = del_endoff; |
| 4863 | got->br_blockcount -= del->br_blockcount; |
| 4864 | got->br_startblock = del->br_startblock + del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4865 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4866 | break; |
Christoph Hellwig | 0173c68 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4867 | case BMAP_RIGHT_FILLING: |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4868 | /* |
| 4869 | * Deleting the last part of the extent. |
| 4870 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4871 | got->br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4872 | xfs_iext_update_extent(ip, state, icur, got); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4873 | break; |
| 4874 | case 0: |
| 4875 | /* |
| 4876 | * Deleting the middle of the extent. |
| 4877 | */ |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4878 | got->br_blockcount = del->br_startoff - got->br_startoff; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4879 | |
| 4880 | new.br_startoff = del_endoff; |
| 4881 | new.br_blockcount = got_endoff - del_endoff; |
| 4882 | new.br_state = got->br_state; |
| 4883 | new.br_startblock = del->br_startblock + del->br_blockcount; |
| 4884 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4885 | xfs_iext_update_extent(ip, state, icur, got); |
| 4886 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 4887 | xfs_iext_insert(ip, icur, &new, state); |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4888 | break; |
| 4889 | } |
Darrick J. Wong | 4b4c132 | 2018-01-19 09:05:48 -0800 | [diff] [blame] | 4890 | ip->i_delayed_blks -= del->br_blockcount; |
Christoph Hellwig | fa5c836 | 2016-10-20 15:54:14 +1100 | [diff] [blame] | 4891 | } |
| 4892 | |
Brian Foster | a9bd24a | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 4893 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4894 | * Called by xfs_bmapi to update file extent records and the btree |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4895 | * after removing space. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4896 | */ |
| 4897 | STATIC int /* error */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4898 | xfs_bmap_del_extent_real( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4899 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4900 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4901 | struct xfs_iext_cursor *icur, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 4902 | struct xfs_defer_ops *dfops, /* list of extents to be freed */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4903 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4904 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4905 | int *logflagsp, /* inode logging flags */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 4906 | int whichfork, /* data or attr fork */ |
| 4907 | int bflags) /* bmapi flags */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4908 | { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4909 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4910 | xfs_fileoff_t del_endoff; /* first offset past del */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4911 | int do_fx; /* free extent at end of routine */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4912 | int error; /* error return value */ |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4913 | int flags = 0;/* inode logging flags */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4914 | struct xfs_bmbt_irec got; /* current extent entry */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4915 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4916 | int i; /* temp state */ |
| 4917 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4918 | xfs_mount_t *mp; /* mount structure */ |
| 4919 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4920 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4921 | /* REFERENCED */ |
| 4922 | uint qfield; /* quota field to update */ |
Christoph Hellwig | 060ea65 | 2017-10-19 11:02:29 -0700 | [diff] [blame] | 4923 | int state = xfs_bmap_fork_to_state(whichfork); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4924 | struct xfs_bmbt_irec old; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4925 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 4926 | mp = ip->i_mount; |
| 4927 | XFS_STATS_INC(mp, xs_del_exlist); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4928 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4929 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4930 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4931 | xfs_iext_get_extent(ifp, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4932 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4933 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4934 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4935 | ASSERT(got_endoff >= del_endoff); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4936 | ASSERT(!isnullstartblock(got.br_startblock)); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4937 | qfield = 0; |
| 4938 | error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4939 | |
Christoph Hellwig | 1b24b63 | 2017-10-17 14:16:22 -0700 | [diff] [blame] | 4940 | /* |
| 4941 | * If it's the case where the directory code is running with no block |
| 4942 | * reservation, and the deleted block is in the middle of its extent, |
| 4943 | * and the resulting insert of an extent would cause transformation to |
| 4944 | * btree format, then reject it. The calling code will then swap blocks |
| 4945 | * around instead. We have to do this now, rather than waiting for the |
| 4946 | * conversion to btree format, since the transaction will be dirty then. |
| 4947 | */ |
| 4948 | if (tp->t_blk_res == 0 && |
| 4949 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 4950 | XFS_IFORK_NEXTENTS(ip, whichfork) >= |
| 4951 | XFS_IFORK_MAXEXT(ip, whichfork) && |
| 4952 | del->br_startoff > got.br_startoff && del_endoff < got_endoff) |
| 4953 | return -ENOSPC; |
| 4954 | |
| 4955 | flags = XFS_ILOG_CORE; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4956 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4957 | xfs_fsblock_t bno; |
| 4958 | xfs_filblks_t len; |
| 4959 | |
| 4960 | ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0); |
| 4961 | ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0); |
| 4962 | bno = del->br_startblock; |
| 4963 | len = del->br_blockcount; |
| 4964 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4965 | do_div(len, mp->m_sb.sb_rextsize); |
| 4966 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4967 | if (error) |
| 4968 | goto done; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4969 | do_fx = 0; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4970 | nblks = len * mp->m_sb.sb_rextsize; |
| 4971 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4972 | } else { |
| 4973 | do_fx = 1; |
| 4974 | nblks = del->br_blockcount; |
| 4975 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4976 | } |
| 4977 | |
| 4978 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4979 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 4980 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 4981 | if (error) |
| 4982 | goto done; |
| 4983 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4984 | } |
Darrick J. Wong | 340785c | 2016-08-03 11:33:42 +1000 | [diff] [blame] | 4985 | |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 4986 | if (got.br_startoff == del->br_startoff) |
| 4987 | state |= BMAP_LEFT_FILLING; |
| 4988 | if (got_endoff == del_endoff) |
| 4989 | state |= BMAP_RIGHT_FILLING; |
| 4990 | |
| 4991 | switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) { |
| 4992 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4993 | /* |
| 4994 | * Matches the whole extent. Delete the entry. |
| 4995 | */ |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 4996 | xfs_iext_remove(ip, icur, state); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 4997 | xfs_iext_prev(ifp, icur); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4998 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4999 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5000 | flags |= XFS_ILOG_CORE; |
| 5001 | if (!cur) { |
| 5002 | flags |= xfs_ilog_fext(whichfork); |
| 5003 | break; |
| 5004 | } |
| 5005 | if ((error = xfs_btree_delete(cur, &i))) |
| 5006 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 5007 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5008 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5009 | case BMAP_LEFT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5010 | /* |
| 5011 | * Deleting the first part of the extent. |
| 5012 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5013 | got.br_startoff = del_endoff; |
| 5014 | got.br_startblock = del_endblock; |
| 5015 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5016 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5017 | if (!cur) { |
| 5018 | flags |= xfs_ilog_fext(whichfork); |
| 5019 | break; |
| 5020 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5021 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5022 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5023 | goto done; |
| 5024 | break; |
Christoph Hellwig | 491f6f8a | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5025 | case BMAP_RIGHT_FILLING: |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5026 | /* |
| 5027 | * Deleting the last part of the extent. |
| 5028 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5029 | got.br_blockcount -= del->br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5030 | xfs_iext_update_extent(ip, state, icur, &got); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5031 | if (!cur) { |
| 5032 | flags |= xfs_ilog_fext(whichfork); |
| 5033 | break; |
| 5034 | } |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5035 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5036 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5037 | goto done; |
| 5038 | break; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5039 | case 0: |
| 5040 | /* |
| 5041 | * Deleting the middle of the extent. |
| 5042 | */ |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5043 | old = got; |
Christoph Hellwig | ca5d8e5b | 2017-10-19 11:04:44 -0700 | [diff] [blame] | 5044 | |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5045 | got.br_blockcount = del->br_startoff - got.br_startoff; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5046 | xfs_iext_update_extent(ip, state, icur, &got); |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5047 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5048 | new.br_startoff = del_endoff; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5049 | new.br_blockcount = got_endoff - del_endoff; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5050 | new.br_state = got.br_state; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5051 | new.br_startblock = del_endblock; |
Christoph Hellwig | 48fd52b | 2017-10-17 14:16:23 -0700 | [diff] [blame] | 5052 | |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5053 | flags |= XFS_ILOG_CORE; |
| 5054 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5055 | error = xfs_bmbt_update(cur, &got); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5056 | if (error) |
| 5057 | goto done; |
| 5058 | error = xfs_btree_increment(cur, 0, &i); |
| 5059 | if (error) |
| 5060 | goto done; |
| 5061 | cur->bc_rec.b = new; |
| 5062 | error = xfs_btree_insert(cur, &i); |
| 5063 | if (error && error != -ENOSPC) |
| 5064 | goto done; |
| 5065 | /* |
| 5066 | * If get no-space back from btree insert, it tried a |
| 5067 | * split, and we have a zero block reservation. Fix up |
| 5068 | * our state and return the error. |
| 5069 | */ |
| 5070 | if (error == -ENOSPC) { |
| 5071 | /* |
| 5072 | * Reset the cursor, don't trust it after any |
| 5073 | * insert operation. |
| 5074 | */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5075 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5076 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5077 | goto done; |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5078 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5079 | /* |
| 5080 | * Update the btree record back |
| 5081 | * to the original value. |
| 5082 | */ |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5083 | error = xfs_bmbt_update(cur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5084 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5085 | goto done; |
| 5086 | /* |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5087 | * Reset the extent record back |
| 5088 | * to the original value. |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5089 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5090 | xfs_iext_update_extent(ip, state, icur, &old); |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5091 | flags = 0; |
| 5092 | error = -ENOSPC; |
| 5093 | goto done; |
| 5094 | } |
| 5095 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
| 5096 | } else |
| 5097 | flags |= xfs_ilog_fext(whichfork); |
| 5098 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5099 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5100 | xfs_iext_next(ifp, icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5101 | xfs_iext_insert(ip, icur, &new, state); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5102 | break; |
| 5103 | } |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5104 | |
| 5105 | /* remove reverse mapping */ |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5106 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del); |
| 5107 | if (error) |
| 5108 | goto done; |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5109 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5110 | /* |
| 5111 | * If we need to, add to list of extents to delete. |
| 5112 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5113 | if (do_fx && !(bflags & XFS_BMAPI_REMAP)) { |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5114 | if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) { |
| 5115 | error = xfs_refcount_decrease_extent(mp, dfops, del); |
| 5116 | if (error) |
| 5117 | goto done; |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5118 | } else { |
Brian Foster | 84ca484 | 2018-05-09 08:45:05 -0700 | [diff] [blame] | 5119 | if ((bflags & XFS_BMAPI_NODISCARD) || |
| 5120 | (del->br_state == XFS_EXT_UNWRITTEN)) { |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5121 | xfs_bmap_add_free_nodiscard(mp, dfops, |
| 5122 | del->br_startblock, del->br_blockcount, |
| 5123 | NULL); |
| 5124 | } else { |
| 5125 | xfs_bmap_add_free(mp, dfops, del->br_startblock, |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5126 | del->br_blockcount, NULL); |
Brian Foster | fcb762f | 2018-05-09 08:45:04 -0700 | [diff] [blame] | 5127 | } |
| 5128 | } |
Darrick J. Wong | 62aab20 | 2016-10-03 09:11:23 -0700 | [diff] [blame] | 5129 | } |
| 5130 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5131 | /* |
| 5132 | * Adjust inode # blocks in the file. |
| 5133 | */ |
| 5134 | if (nblks) |
| 5135 | ip->i_d.di_nblocks -= nblks; |
| 5136 | /* |
| 5137 | * Adjust quota data. |
| 5138 | */ |
Darrick J. Wong | 4847acf | 2016-10-03 09:11:27 -0700 | [diff] [blame] | 5139 | if (qfield && !(bflags & XFS_BMAPI_REMAP)) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5140 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5141 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5142 | done: |
| 5143 | *logflagsp = flags; |
| 5144 | return error; |
| 5145 | } |
| 5146 | |
| 5147 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | * Unmap (remove) blocks from a file. |
| 5149 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5150 | * that value. If not all extents in the block range can be removed then |
| 5151 | * *done is set. |
| 5152 | */ |
| 5153 | int /* error */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5154 | __xfs_bunmapi( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | xfs_trans_t *tp, /* transaction pointer */ |
| 5156 | struct xfs_inode *ip, /* incore inode */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5157 | xfs_fileoff_t start, /* first file offset deleted */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5158 | xfs_filblks_t *rlen, /* i/o: amount remaining */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | int flags, /* misc flags */ |
| 5160 | xfs_extnum_t nexts, /* number of extents max */ |
| 5161 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5162 | controls a.g. for allocs */ |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5163 | struct xfs_defer_ops *dfops) /* i/o: deferred updates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | { |
| 5165 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5166 | xfs_bmbt_irec_t del; /* extent being deleted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | int error; /* error return value */ |
| 5168 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5169 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5171 | int isrt; /* freeing in rt area */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | int logflags; /* transaction logging flags */ |
| 5173 | xfs_extlen_t mod; /* rt extent offset */ |
| 5174 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | int tmp_logflags; /* partial logging flags */ |
| 5176 | int wasdel; /* was a delayed alloc extent */ |
| 5177 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5178 | xfs_fsblock_t sum; |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5179 | xfs_filblks_t len = *rlen; /* length to unmap in file */ |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5180 | xfs_fileoff_t max_len; |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5181 | xfs_agnumber_t prev_agno = NULLAGNUMBER, agno; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5182 | xfs_fileoff_t end; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5183 | struct xfs_iext_cursor icur; |
| 5184 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5185 | |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5186 | trace_xfs_bunmap(ip, start, len, flags, _RET_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5187 | |
Darrick J. Wong | 3993bae | 2016-10-03 09:11:32 -0700 | [diff] [blame] | 5188 | whichfork = xfs_bmapi_whichfork(flags); |
| 5189 | ASSERT(whichfork != XFS_COW_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5191 | if (unlikely( |
| 5192 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5193 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5194 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5195 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5196 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | } |
| 5198 | mp = ip->i_mount; |
| 5199 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5200 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5201 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5202 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | ASSERT(len > 0); |
| 5204 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5205 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5206 | /* |
| 5207 | * Guesstimate how many blocks we can unmap without running the risk of |
| 5208 | * blowing out the transaction with a mix of EFIs and reflink |
| 5209 | * adjustments. |
| 5210 | */ |
Darrick J. Wong | 8c57b88 | 2017-12-10 18:03:53 -0800 | [diff] [blame] | 5211 | if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5212 | max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res)); |
| 5213 | else |
| 5214 | max_len = len; |
| 5215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5217 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5218 | return error; |
Eric Sandeen | 5d82930 | 2016-11-08 12:59:42 +1100 | [diff] [blame] | 5219 | if (xfs_iext_count(ifp) == 0) { |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5220 | *rlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | return 0; |
| 5222 | } |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 5223 | XFS_STATS_INC(mp, xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5224 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5225 | end = start + len; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5226 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5227 | if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) { |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5228 | *rlen = 0; |
| 5229 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | } |
Christoph Hellwig | dc56015 | 2017-10-23 16:32:39 -0700 | [diff] [blame] | 5231 | end--; |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | logflags = 0; |
| 5234 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5235 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5236 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5238 | cur->bc_private.b.dfops = dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | cur->bc_private.b.flags = 0; |
| 5240 | } else |
| 5241 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5242 | |
| 5243 | if (isrt) { |
| 5244 | /* |
| 5245 | * Synchronize by locking the bitmap inode. |
| 5246 | */ |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5247 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5248 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Darrick J. Wong | f4a0660 | 2016-08-03 11:00:42 +1000 | [diff] [blame] | 5249 | xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM); |
| 5250 | xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL); |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | extno = 0; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5254 | while (end != (xfs_fileoff_t)-1 && end >= start && |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5255 | (nexts == 0 || extno < nexts) && max_len > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5256 | /* |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5257 | * Is the found extent after a hole in which end lives? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | * Just back up to the previous extent, if so. |
| 5259 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5260 | if (got.br_startoff > end && |
| 5261 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5262 | done = true; |
| 5263 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | } |
| 5265 | /* |
| 5266 | * Is the last block of this extent before the range |
| 5267 | * we're supposed to delete? If so, we're done. |
| 5268 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5269 | end = XFS_FILEOFF_MIN(end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | got.br_startoff + got.br_blockcount - 1); |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5271 | if (end < start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | break; |
| 5273 | /* |
| 5274 | * Then deal with the (possibly delayed) allocated space |
| 5275 | * we found. |
| 5276 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5278 | wasdel = isnullstartblock(del.br_startblock); |
Christoph Hellwig | 5b094d6 | 2017-07-18 11:16:51 -0700 | [diff] [blame] | 5279 | |
| 5280 | /* |
| 5281 | * Make sure we don't touch multiple AGF headers out of order |
| 5282 | * in a single transaction, as that could cause AB-BA deadlocks. |
| 5283 | */ |
| 5284 | if (!wasdel) { |
| 5285 | agno = XFS_FSB_TO_AGNO(mp, del.br_startblock); |
| 5286 | if (prev_agno != NULLAGNUMBER && prev_agno > agno) |
| 5287 | break; |
| 5288 | prev_agno = agno; |
| 5289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | if (got.br_startoff < start) { |
| 5291 | del.br_startoff = start; |
| 5292 | del.br_blockcount -= start - got.br_startoff; |
| 5293 | if (!wasdel) |
| 5294 | del.br_startblock += start - got.br_startoff; |
| 5295 | } |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5296 | if (del.br_startoff + del.br_blockcount > end + 1) |
| 5297 | del.br_blockcount = end + 1 - del.br_startoff; |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5298 | |
| 5299 | /* How much can we safely unmap? */ |
| 5300 | if (max_len < del.br_blockcount) { |
| 5301 | del.br_startoff += del.br_blockcount - max_len; |
| 5302 | if (!wasdel) |
| 5303 | del.br_startblock += del.br_blockcount - max_len; |
| 5304 | del.br_blockcount = max_len; |
| 5305 | } |
| 5306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | sum = del.br_startblock + del.br_blockcount; |
| 5308 | if (isrt && |
| 5309 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5310 | /* |
| 5311 | * Realtime extent not lined up at the end. |
| 5312 | * The extent could have been split into written |
| 5313 | * and unwritten pieces, or we could just be |
| 5314 | * unmapping part of it. But we can't really |
| 5315 | * get rid of part of a realtime extent. |
| 5316 | */ |
| 5317 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5318 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | /* |
| 5320 | * This piece is unwritten, or we're not |
| 5321 | * using unwritten extents. Skip over it. |
| 5322 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5323 | ASSERT(end >= mod); |
| 5324 | end -= mod > del.br_blockcount ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | del.br_blockcount : mod; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5326 | if (end < got.br_startoff && |
| 5327 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5328 | done = true; |
| 5329 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | } |
| 5331 | continue; |
| 5332 | } |
| 5333 | /* |
| 5334 | * It's written, turn it unwritten. |
| 5335 | * This is better than zeroing it. |
| 5336 | */ |
| 5337 | ASSERT(del.br_state == XFS_EXT_NORM); |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5338 | ASSERT(tp->t_blk_res > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | /* |
| 5340 | * If this spans a realtime extent boundary, |
| 5341 | * chop it back to the start of the one we end at. |
| 5342 | */ |
| 5343 | if (del.br_blockcount > mod) { |
| 5344 | del.br_startoff += del.br_blockcount - mod; |
| 5345 | del.br_startblock += del.br_blockcount - mod; |
| 5346 | del.br_blockcount = mod; |
| 5347 | } |
| 5348 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5349 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5350 | whichfork, &icur, &cur, &del, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5351 | firstblock, dfops, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | if (error) |
| 5353 | goto error0; |
| 5354 | goto nodelete; |
| 5355 | } |
| 5356 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5357 | /* |
| 5358 | * Realtime extent is lined up at the end but not |
| 5359 | * at the front. We'll get rid of full extents if |
| 5360 | * we can. |
| 5361 | */ |
| 5362 | mod = mp->m_sb.sb_rextsize - mod; |
| 5363 | if (del.br_blockcount > mod) { |
| 5364 | del.br_blockcount -= mod; |
| 5365 | del.br_startoff += mod; |
| 5366 | del.br_startblock += mod; |
| 5367 | } else if ((del.br_startoff == start && |
| 5368 | (del.br_state == XFS_EXT_UNWRITTEN || |
Christoph Hellwig | a7e5d03 | 2016-03-02 09:58:21 +1100 | [diff] [blame] | 5369 | tp->t_blk_res == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5370 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | /* |
| 5372 | * Can't make it unwritten. There isn't |
| 5373 | * a full extent here so just skip it. |
| 5374 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5375 | ASSERT(end >= del.br_blockcount); |
| 5376 | end -= del.br_blockcount; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5377 | if (got.br_startoff > end && |
| 5378 | !xfs_iext_prev_extent(ifp, &icur, &got)) { |
| 5379 | done = true; |
| 5380 | break; |
| 5381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | continue; |
| 5383 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
Christoph Hellwig | 7efc794 | 2016-11-24 11:39:44 +1100 | [diff] [blame] | 5384 | struct xfs_bmbt_irec prev; |
| 5385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | /* |
| 5387 | * This one is already unwritten. |
| 5388 | * It must have a written left neighbor. |
| 5389 | * Unwrite the killed part of that one and |
| 5390 | * try again. |
| 5391 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5392 | if (!xfs_iext_prev_extent(ifp, &icur, &prev)) |
| 5393 | ASSERT(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5395 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | ASSERT(del.br_startblock == |
| 5397 | prev.br_startblock + prev.br_blockcount); |
| 5398 | if (prev.br_startoff < start) { |
| 5399 | mod = start - prev.br_startoff; |
| 5400 | prev.br_blockcount -= mod; |
| 5401 | prev.br_startblock += mod; |
| 5402 | prev.br_startoff = start; |
| 5403 | } |
| 5404 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5405 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5406 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5407 | &prev, firstblock, dfops, |
| 5408 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | if (error) |
| 5410 | goto error0; |
| 5411 | goto nodelete; |
| 5412 | } else { |
| 5413 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5414 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5415 | error = xfs_bmap_add_extent_unwritten_real(tp, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5416 | ip, whichfork, &icur, &cur, |
Darrick J. Wong | 05a630d | 2017-02-02 15:14:01 -0800 | [diff] [blame] | 5417 | &del, firstblock, dfops, |
| 5418 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | if (error) |
| 5420 | goto error0; |
| 5421 | goto nodelete; |
| 5422 | } |
| 5423 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5424 | |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5425 | if (wasdel) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5426 | error = xfs_bmap_del_extent_delay(ip, whichfork, &icur, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5427 | &got, &del); |
| 5428 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5429 | error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops, |
Christoph Hellwig | e1d7553 | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5430 | cur, &del, &tmp_logflags, whichfork, |
| 5431 | flags); |
| 5432 | logflags |= tmp_logflags; |
Christoph Hellwig | b213d69 | 2017-10-17 14:16:20 -0700 | [diff] [blame] | 5433 | } |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | if (error) |
| 5436 | goto error0; |
Brian Foster | b2706a0 | 2016-03-15 11:42:46 +1100 | [diff] [blame] | 5437 | |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 5438 | max_len -= del.br_blockcount; |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5439 | end = del.br_startoff - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | /* |
| 5442 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | */ |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5444 | if (end != (xfs_fileoff_t)-1 && end >= start) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5445 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
| 5446 | (got.br_startoff > end && |
| 5447 | !xfs_iext_prev_extent(ifp, &icur, &got))) { |
| 5448 | done = true; |
| 5449 | break; |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | extno++; |
| 5452 | } |
| 5453 | } |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5454 | if (done || end == (xfs_fileoff_t)-1 || end < start) |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5455 | *rlen = 0; |
| 5456 | else |
Christoph Hellwig | 8280f6e | 2017-10-17 14:16:21 -0700 | [diff] [blame] | 5457 | *rlen = end - start + 1; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | /* |
| 5460 | * Convert to a btree if necessary. |
| 5461 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5462 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5464 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | &cur, 0, &tmp_logflags, whichfork); |
| 5466 | logflags |= tmp_logflags; |
| 5467 | if (error) |
| 5468 | goto error0; |
| 5469 | } |
| 5470 | /* |
| 5471 | * transform from btree to extents, give it cur |
| 5472 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5473 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | ASSERT(cur != NULL); |
| 5475 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5476 | whichfork); |
| 5477 | logflags |= tmp_logflags; |
| 5478 | if (error) |
| 5479 | goto error0; |
| 5480 | } |
| 5481 | /* |
| 5482 | * transform from extents to local? |
| 5483 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | error = 0; |
| 5485 | error0: |
| 5486 | /* |
| 5487 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5488 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5490 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5492 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5493 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5495 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | /* |
| 5497 | * Log inode even in the error case, if the transaction |
| 5498 | * is dirty we'll need to shut down the filesystem. |
| 5499 | */ |
| 5500 | if (logflags) |
| 5501 | xfs_trans_log_inode(tp, ip, logflags); |
| 5502 | if (cur) { |
| 5503 | if (!error) { |
| 5504 | *firstblock = cur->bc_private.b.firstblock; |
| 5505 | cur->bc_private.b.allocated = 0; |
| 5506 | } |
| 5507 | xfs_btree_del_cursor(cur, |
| 5508 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5509 | } |
| 5510 | return error; |
| 5511 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5512 | |
Darrick J. Wong | 4453593 | 2016-10-03 09:11:29 -0700 | [diff] [blame] | 5513 | /* Unmap a range of a file. */ |
| 5514 | int |
| 5515 | xfs_bunmapi( |
| 5516 | xfs_trans_t *tp, |
| 5517 | struct xfs_inode *ip, |
| 5518 | xfs_fileoff_t bno, |
| 5519 | xfs_filblks_t len, |
| 5520 | int flags, |
| 5521 | xfs_extnum_t nexts, |
| 5522 | xfs_fsblock_t *firstblock, |
| 5523 | struct xfs_defer_ops *dfops, |
| 5524 | int *done) |
| 5525 | { |
| 5526 | int error; |
| 5527 | |
| 5528 | error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock, |
| 5529 | dfops); |
| 5530 | *done = (len == 0); |
| 5531 | return error; |
| 5532 | } |
| 5533 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5534 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5535 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5536 | * extent that precedes the target hole of the shift. |
| 5537 | */ |
| 5538 | STATIC bool |
| 5539 | xfs_bmse_can_merge( |
| 5540 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5541 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5542 | xfs_fileoff_t shift) /* shift fsb */ |
| 5543 | { |
| 5544 | xfs_fileoff_t startoff; |
| 5545 | |
| 5546 | startoff = got->br_startoff - shift; |
| 5547 | |
| 5548 | /* |
| 5549 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5550 | * the preceding extent. |
| 5551 | */ |
| 5552 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5553 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5554 | (left->br_state != got->br_state) || |
| 5555 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5556 | return false; |
| 5557 | |
| 5558 | return true; |
| 5559 | } |
| 5560 | |
| 5561 | /* |
| 5562 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5563 | * hole in the file. If an extent shift would result in the extent being fully |
| 5564 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5565 | * the preceding extent rather than do the shift. |
| 5566 | * |
| 5567 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5568 | * with the provided extents via xfs_bmse_can_merge(). |
| 5569 | */ |
| 5570 | STATIC int |
| 5571 | xfs_bmse_merge( |
| 5572 | struct xfs_inode *ip, |
| 5573 | int whichfork, |
| 5574 | xfs_fileoff_t shift, /* shift fsb */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5575 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5576 | struct xfs_bmbt_irec *got, /* extent to shift */ |
| 5577 | struct xfs_bmbt_irec *left, /* preceding extent */ |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5578 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5579 | int *logflags, /* output */ |
| 5580 | struct xfs_defer_ops *dfops) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5581 | { |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5582 | struct xfs_bmbt_irec new; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5583 | xfs_filblks_t blockcount; |
| 5584 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5585 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5586 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5587 | blockcount = left->br_blockcount + got->br_blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5588 | |
| 5589 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5590 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5591 | ASSERT(xfs_bmse_can_merge(left, got, shift)); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5592 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5593 | new = *left; |
| 5594 | new.br_blockcount = blockcount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5595 | |
| 5596 | /* |
| 5597 | * Update the on-disk extent count, the btree if necessary and log the |
| 5598 | * inode. |
| 5599 | */ |
| 5600 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5601 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5602 | *logflags |= XFS_ILOG_CORE; |
| 5603 | if (!cur) { |
| 5604 | *logflags |= XFS_ILOG_DEXT; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5605 | goto done; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | /* lookup and remove the extent to merge */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5609 | error = xfs_bmbt_lookup_eq(cur, got, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5610 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5611 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5612 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5613 | |
| 5614 | error = xfs_btree_delete(cur, &i); |
| 5615 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5616 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5617 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5618 | |
| 5619 | /* lookup and update size of the previous extent */ |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5620 | error = xfs_bmbt_lookup_eq(cur, left, &i); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5621 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5622 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5623 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5624 | |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5625 | error = xfs_bmbt_update(cur, &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5626 | if (error) |
| 5627 | return error; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5628 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5629 | done: |
Christoph Hellwig | c38ccf5 | 2017-11-03 10:34:47 -0700 | [diff] [blame] | 5630 | xfs_iext_remove(ip, icur, 0); |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5631 | xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur); |
| 5632 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5633 | &new); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5634 | |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5635 | /* update reverse mapping. rmap functions merge the rmaps for us */ |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5636 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got); |
| 5637 | if (error) |
| 5638 | return error; |
Darrick J. Wong | 4cc1ee5 | 2017-08-30 16:06:36 -0700 | [diff] [blame] | 5639 | memcpy(&new, got, sizeof(new)); |
| 5640 | new.br_startoff = left->br_startoff + left->br_blockcount; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5641 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5642 | } |
| 5643 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5644 | static int |
| 5645 | xfs_bmap_shift_update_extent( |
| 5646 | struct xfs_inode *ip, |
| 5647 | int whichfork, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5648 | struct xfs_iext_cursor *icur, |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5649 | struct xfs_bmbt_irec *got, |
| 5650 | struct xfs_btree_cur *cur, |
| 5651 | int *logflags, |
| 5652 | struct xfs_defer_ops *dfops, |
| 5653 | xfs_fileoff_t startoff) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5654 | { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5655 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5656 | struct xfs_bmbt_irec prev = *got; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5657 | int error, i; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5658 | |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5659 | *logflags |= XFS_ILOG_CORE; |
| 5660 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5661 | got->br_startoff = startoff; |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5662 | |
| 5663 | if (cur) { |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5664 | error = xfs_bmbt_lookup_eq(cur, &prev, &i); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5665 | if (error) |
| 5666 | return error; |
| 5667 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
| 5668 | |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5669 | error = xfs_bmbt_update(cur, got); |
Christoph Hellwig | 4da6b51 | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5670 | if (error) |
| 5671 | return error; |
| 5672 | } else { |
| 5673 | *logflags |= XFS_ILOG_DEXT; |
| 5674 | } |
| 5675 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5676 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur, |
| 5677 | got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5678 | |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5679 | /* update reverse mapping */ |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5680 | error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev); |
Darrick J. Wong | 9c19464 | 2016-08-03 12:16:05 +1000 | [diff] [blame] | 5681 | if (error) |
| 5682 | return error; |
Christoph Hellwig | 11f75b3 | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5683 | return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5684 | } |
| 5685 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5686 | int |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5687 | xfs_bmap_collapse_extents( |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5688 | struct xfs_trans *tp, |
| 5689 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5690 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5691 | xfs_fileoff_t offset_shift_fsb, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5692 | bool *done, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5693 | xfs_fsblock_t *firstblock, |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5694 | struct xfs_defer_ops *dfops) |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5695 | { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5696 | int whichfork = XFS_DATA_FORK; |
| 5697 | struct xfs_mount *mp = ip->i_mount; |
| 5698 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5699 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5700 | struct xfs_bmbt_irec got, prev; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5701 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5702 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5703 | int error = 0; |
| 5704 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5705 | |
| 5706 | if (unlikely(XFS_TEST_ERROR( |
| 5707 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5708 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5709 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5710 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5711 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5715 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5716 | |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5717 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5718 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5719 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5720 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5721 | if (error) |
| 5722 | return error; |
| 5723 | } |
| 5724 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5725 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5726 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5727 | cur->bc_private.b.firstblock = *firstblock; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5728 | cur->bc_private.b.dfops = dfops; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5729 | cur->bc_private.b.flags = 0; |
| 5730 | } |
| 5731 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5732 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5733 | *done = true; |
| 5734 | goto del_cursor; |
| 5735 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5736 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5737 | del_cursor); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5738 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5739 | new_startoff = got.br_startoff - offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5740 | if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5741 | if (new_startoff < prev.br_startoff + prev.br_blockcount) { |
| 5742 | error = -EINVAL; |
| 5743 | goto del_cursor; |
| 5744 | } |
| 5745 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5746 | if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) { |
| 5747 | error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5748 | &icur, &got, &prev, cur, &logflags, |
| 5749 | dfops); |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5750 | if (error) |
| 5751 | goto del_cursor; |
| 5752 | goto done; |
| 5753 | } |
| 5754 | } else { |
| 5755 | if (got.br_startoff < offset_shift_fsb) { |
| 5756 | error = -EINVAL; |
| 5757 | goto del_cursor; |
| 5758 | } |
| 5759 | } |
| 5760 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5761 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5762 | &logflags, dfops, new_startoff); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5763 | if (error) |
| 5764 | goto del_cursor; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5765 | |
Christoph Hellwig | 4263036 | 2017-11-03 10:34:41 -0700 | [diff] [blame] | 5766 | done: |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5767 | if (!xfs_iext_next_extent(ifp, &icur, &got)) { |
| 5768 | *done = true; |
| 5769 | goto del_cursor; |
Christoph Hellwig | 40591bd | 2017-10-19 11:08:51 -0700 | [diff] [blame] | 5770 | } |
| 5771 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5772 | *next_fsb = got.br_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5773 | del_cursor: |
| 5774 | if (cur) |
| 5775 | xfs_btree_del_cursor(cur, |
| 5776 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5777 | if (logflags) |
| 5778 | xfs_trans_log_inode(tp, ip, logflags); |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5779 | return error; |
| 5780 | } |
| 5781 | |
| 5782 | int |
| 5783 | xfs_bmap_insert_extents( |
| 5784 | struct xfs_trans *tp, |
| 5785 | struct xfs_inode *ip, |
| 5786 | xfs_fileoff_t *next_fsb, |
| 5787 | xfs_fileoff_t offset_shift_fsb, |
| 5788 | bool *done, |
| 5789 | xfs_fileoff_t stop_fsb, |
| 5790 | xfs_fsblock_t *firstblock, |
| 5791 | struct xfs_defer_ops *dfops) |
| 5792 | { |
| 5793 | int whichfork = XFS_DATA_FORK; |
| 5794 | struct xfs_mount *mp = ip->i_mount; |
| 5795 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5796 | struct xfs_btree_cur *cur = NULL; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5797 | struct xfs_bmbt_irec got, next; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5798 | struct xfs_iext_cursor icur; |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5799 | xfs_fileoff_t new_startoff; |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5800 | int error = 0; |
| 5801 | int logflags = 0; |
| 5802 | |
| 5803 | if (unlikely(XFS_TEST_ERROR( |
| 5804 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5805 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5806 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
| 5807 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
| 5808 | return -EFSCORRUPTED; |
| 5809 | } |
| 5810 | |
| 5811 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5812 | return -EIO; |
| 5813 | |
| 5814 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL)); |
| 5815 | |
| 5816 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5817 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5818 | if (error) |
| 5819 | return error; |
| 5820 | } |
| 5821 | |
| 5822 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5823 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5824 | cur->bc_private.b.firstblock = *firstblock; |
| 5825 | cur->bc_private.b.dfops = dfops; |
| 5826 | cur->bc_private.b.flags = 0; |
| 5827 | } |
| 5828 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5829 | if (*next_fsb == NULLFSBLOCK) { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5830 | xfs_iext_last(ifp, &icur); |
| 5831 | if (!xfs_iext_get_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5832 | stop_fsb > got.br_startoff) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5833 | *done = true; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5834 | goto del_cursor; |
| 5835 | } |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5836 | } else { |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5837 | if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5838 | *done = true; |
Christoph Hellwig | 05b7c8a | 2017-08-29 15:44:12 -0700 | [diff] [blame] | 5839 | goto del_cursor; |
| 5840 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5841 | } |
Eric Sandeen | d41c617 | 2017-11-27 18:23:32 -0800 | [diff] [blame] | 5842 | XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock), |
| 5843 | del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5844 | |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5845 | if (stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5846 | error = -EIO; |
| 5847 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5848 | } |
| 5849 | |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5850 | new_startoff = got.br_startoff + offset_shift_fsb; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5851 | if (xfs_iext_peek_next_extent(ifp, &icur, &next)) { |
Christoph Hellwig | bf80628 | 2017-10-19 11:07:34 -0700 | [diff] [blame] | 5852 | if (new_startoff + got.br_blockcount > next.br_startoff) { |
| 5853 | error = -EINVAL; |
| 5854 | goto del_cursor; |
| 5855 | } |
| 5856 | |
| 5857 | /* |
| 5858 | * Unlike a left shift (which involves a hole punch), a right |
| 5859 | * shift does not modify extent neighbors in any way. We should |
| 5860 | * never find mergeable extents in this scenario. Check anyways |
| 5861 | * and warn if we encounter two extents that could be one. |
| 5862 | */ |
| 5863 | if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb)) |
| 5864 | WARN_ON_ONCE(1); |
| 5865 | } |
| 5866 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5867 | error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur, |
| 5868 | &logflags, dfops, new_startoff); |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5869 | if (error) |
| 5870 | goto del_cursor; |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5871 | |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5872 | if (!xfs_iext_prev_extent(ifp, &icur, &got) || |
Christoph Hellwig | 5936dc5 | 2017-10-19 11:08:52 -0700 | [diff] [blame] | 5873 | stop_fsb >= got.br_startoff + got.br_blockcount) { |
Christoph Hellwig | ecfea3f | 2017-10-19 11:07:11 -0700 | [diff] [blame] | 5874 | *done = true; |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5875 | goto del_cursor; |
| 5876 | } |
Christoph Hellwig | 6b18af0 | 2017-10-19 11:07:10 -0700 | [diff] [blame] | 5877 | |
| 5878 | *next_fsb = got.br_startoff; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5879 | del_cursor: |
| 5880 | if (cur) |
| 5881 | xfs_btree_del_cursor(cur, |
| 5882 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5883 | if (logflags) |
| 5884 | xfs_trans_log_inode(tp, ip, logflags); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5885 | return error; |
| 5886 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5887 | |
| 5888 | /* |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5889 | * Splits an extent into two extents at split_fsb block such that it is the |
| 5890 | * first block of the current_ext. @ext is a target extent to be split. |
| 5891 | * @split_fsb is a block where the extents is split. If split_fsb lies in a |
| 5892 | * hole or the first block of extents, just return 0. |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5893 | */ |
| 5894 | STATIC int |
| 5895 | xfs_bmap_split_extent_at( |
| 5896 | struct xfs_trans *tp, |
| 5897 | struct xfs_inode *ip, |
| 5898 | xfs_fileoff_t split_fsb, |
| 5899 | xfs_fsblock_t *firstfsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5900 | struct xfs_defer_ops *dfops) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5901 | { |
| 5902 | int whichfork = XFS_DATA_FORK; |
| 5903 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5904 | struct xfs_bmbt_irec got; |
| 5905 | struct xfs_bmbt_irec new; /* split extent */ |
| 5906 | struct xfs_mount *mp = ip->i_mount; |
| 5907 | struct xfs_ifork *ifp; |
| 5908 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5909 | struct xfs_iext_cursor icur; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5910 | int error = 0; |
| 5911 | int logflags = 0; |
| 5912 | int i = 0; |
| 5913 | |
| 5914 | if (unlikely(XFS_TEST_ERROR( |
| 5915 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5916 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 5917 | mp, XFS_ERRTAG_BMAPIFORMAT))) { |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5918 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5919 | XFS_ERRLEVEL_LOW, mp); |
| 5920 | return -EFSCORRUPTED; |
| 5921 | } |
| 5922 | |
| 5923 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5924 | return -EIO; |
| 5925 | |
| 5926 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5927 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5928 | /* Read in all the extents */ |
| 5929 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5930 | if (error) |
| 5931 | return error; |
| 5932 | } |
| 5933 | |
| 5934 | /* |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5935 | * 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] | 5936 | */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5937 | if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) || |
Christoph Hellwig | 4c35445b | 2017-08-29 15:44:13 -0700 | [diff] [blame] | 5938 | got.br_startoff >= split_fsb) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5939 | return 0; |
| 5940 | |
| 5941 | gotblkcnt = split_fsb - got.br_startoff; |
| 5942 | new.br_startoff = split_fsb; |
| 5943 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5944 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5945 | new.br_state = got.br_state; |
| 5946 | |
| 5947 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5948 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5949 | cur->bc_private.b.firstblock = *firstfsb; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5950 | cur->bc_private.b.dfops = dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5951 | cur->bc_private.b.flags = 0; |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5952 | error = xfs_bmbt_lookup_eq(cur, &got, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5953 | if (error) |
| 5954 | goto del_cursor; |
| 5955 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5956 | } |
| 5957 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5958 | got.br_blockcount = gotblkcnt; |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5959 | xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur, |
| 5960 | &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5961 | |
| 5962 | logflags = XFS_ILOG_CORE; |
| 5963 | if (cur) { |
Christoph Hellwig | a67d00a | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5964 | error = xfs_bmbt_update(cur, &got); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5965 | if (error) |
| 5966 | goto del_cursor; |
| 5967 | } else |
| 5968 | logflags |= XFS_ILOG_DEXT; |
| 5969 | |
| 5970 | /* Add new extent */ |
Christoph Hellwig | b2b1712 | 2017-11-03 10:34:43 -0700 | [diff] [blame] | 5971 | xfs_iext_next(ifp, &icur); |
Christoph Hellwig | 0254c2f | 2017-11-03 10:34:46 -0700 | [diff] [blame] | 5972 | xfs_iext_insert(ip, &icur, &new, 0); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5973 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5974 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5975 | |
| 5976 | if (cur) { |
Christoph Hellwig | e16cf9b | 2017-10-17 14:16:26 -0700 | [diff] [blame] | 5977 | error = xfs_bmbt_lookup_eq(cur, &new, &i); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5978 | if (error) |
| 5979 | goto del_cursor; |
| 5980 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5981 | error = xfs_btree_insert(cur, &i); |
| 5982 | if (error) |
| 5983 | goto del_cursor; |
| 5984 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5985 | } |
| 5986 | |
| 5987 | /* |
| 5988 | * Convert to a btree if necessary. |
| 5989 | */ |
| 5990 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5991 | int tmp_logflags; /* partial log flag return val */ |
| 5992 | |
| 5993 | ASSERT(cur == NULL); |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 5994 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5995 | &cur, 0, &tmp_logflags, whichfork); |
| 5996 | logflags |= tmp_logflags; |
| 5997 | } |
| 5998 | |
| 5999 | del_cursor: |
| 6000 | if (cur) { |
| 6001 | cur->bc_private.b.allocated = 0; |
| 6002 | xfs_btree_del_cursor(cur, |
| 6003 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 6004 | } |
| 6005 | |
| 6006 | if (logflags) |
| 6007 | xfs_trans_log_inode(tp, ip, logflags); |
| 6008 | return error; |
| 6009 | } |
| 6010 | |
| 6011 | int |
| 6012 | xfs_bmap_split_extent( |
| 6013 | struct xfs_inode *ip, |
| 6014 | xfs_fileoff_t split_fsb) |
| 6015 | { |
| 6016 | struct xfs_mount *mp = ip->i_mount; |
| 6017 | struct xfs_trans *tp; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6018 | struct xfs_defer_ops dfops; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6019 | xfs_fsblock_t firstfsb; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6020 | int error; |
| 6021 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 6022 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, |
| 6023 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp); |
| 6024 | if (error) |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6025 | return error; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6026 | |
| 6027 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 6028 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 6029 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6030 | xfs_defer_init(&dfops, &firstfsb); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6031 | |
| 6032 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6033 | &firstfsb, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6034 | if (error) |
| 6035 | goto out; |
| 6036 | |
Christoph Hellwig | 8ad7c629 | 2017-08-28 10:21:04 -0700 | [diff] [blame] | 6037 | error = xfs_defer_finish(&tp, &dfops); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6038 | if (error) |
| 6039 | goto out; |
| 6040 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 6041 | return xfs_trans_commit(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6042 | |
| 6043 | out: |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 6044 | xfs_defer_cancel(&dfops); |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 6045 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 6046 | return error; |
| 6047 | } |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6048 | |
| 6049 | /* Deferred mapping is only for real extents in the data fork. */ |
| 6050 | static bool |
| 6051 | xfs_bmap_is_update_needed( |
| 6052 | struct xfs_bmbt_irec *bmap) |
| 6053 | { |
| 6054 | return bmap->br_startblock != HOLESTARTBLOCK && |
| 6055 | bmap->br_startblock != DELAYSTARTBLOCK; |
| 6056 | } |
| 6057 | |
| 6058 | /* Record a bmap intent. */ |
| 6059 | static int |
| 6060 | __xfs_bmap_add( |
| 6061 | struct xfs_mount *mp, |
| 6062 | struct xfs_defer_ops *dfops, |
| 6063 | enum xfs_bmap_intent_type type, |
| 6064 | struct xfs_inode *ip, |
| 6065 | int whichfork, |
| 6066 | struct xfs_bmbt_irec *bmap) |
| 6067 | { |
| 6068 | int error; |
| 6069 | struct xfs_bmap_intent *bi; |
| 6070 | |
| 6071 | trace_xfs_bmap_defer(mp, |
| 6072 | XFS_FSB_TO_AGNO(mp, bmap->br_startblock), |
| 6073 | type, |
| 6074 | XFS_FSB_TO_AGBNO(mp, bmap->br_startblock), |
| 6075 | ip->i_ino, whichfork, |
| 6076 | bmap->br_startoff, |
| 6077 | bmap->br_blockcount, |
| 6078 | bmap->br_state); |
| 6079 | |
| 6080 | bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS); |
| 6081 | INIT_LIST_HEAD(&bi->bi_list); |
| 6082 | bi->bi_type = type; |
| 6083 | bi->bi_owner = ip; |
| 6084 | bi->bi_whichfork = whichfork; |
| 6085 | bi->bi_bmap = *bmap; |
| 6086 | |
Christoph Hellwig | 882d878 | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 6087 | error = xfs_defer_ijoin(dfops, bi->bi_owner); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6088 | if (error) { |
| 6089 | kmem_free(bi); |
| 6090 | return error; |
| 6091 | } |
| 6092 | |
| 6093 | xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list); |
| 6094 | return 0; |
| 6095 | } |
| 6096 | |
| 6097 | /* Map an extent into a file. */ |
| 6098 | int |
| 6099 | xfs_bmap_map_extent( |
| 6100 | struct xfs_mount *mp, |
| 6101 | struct xfs_defer_ops *dfops, |
| 6102 | struct xfs_inode *ip, |
| 6103 | struct xfs_bmbt_irec *PREV) |
| 6104 | { |
| 6105 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6106 | return 0; |
| 6107 | |
| 6108 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip, |
| 6109 | XFS_DATA_FORK, PREV); |
| 6110 | } |
| 6111 | |
| 6112 | /* Unmap an extent out of a file. */ |
| 6113 | int |
| 6114 | xfs_bmap_unmap_extent( |
| 6115 | struct xfs_mount *mp, |
| 6116 | struct xfs_defer_ops *dfops, |
| 6117 | struct xfs_inode *ip, |
| 6118 | struct xfs_bmbt_irec *PREV) |
| 6119 | { |
| 6120 | if (!xfs_bmap_is_update_needed(PREV)) |
| 6121 | return 0; |
| 6122 | |
| 6123 | return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip, |
| 6124 | XFS_DATA_FORK, PREV); |
| 6125 | } |
| 6126 | |
| 6127 | /* |
| 6128 | * Process one of the deferred bmap operations. We pass back the |
| 6129 | * btree cursor to maintain our lock on the bmapbt between calls. |
| 6130 | */ |
| 6131 | int |
| 6132 | xfs_bmap_finish_one( |
| 6133 | struct xfs_trans *tp, |
| 6134 | struct xfs_defer_ops *dfops, |
| 6135 | struct xfs_inode *ip, |
| 6136 | enum xfs_bmap_intent_type type, |
| 6137 | int whichfork, |
| 6138 | xfs_fileoff_t startoff, |
| 6139 | xfs_fsblock_t startblock, |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6140 | xfs_filblks_t *blockcount, |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6141 | xfs_exntst_t state) |
| 6142 | { |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6143 | xfs_fsblock_t firstfsb; |
| 6144 | int error = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6145 | |
Darrick J. Wong | 4c1a67b | 2017-07-17 14:30:51 -0700 | [diff] [blame] | 6146 | /* |
| 6147 | * firstfsb is tied to the transaction lifetime and is used to |
| 6148 | * ensure correct AG locking order and schedule work item |
| 6149 | * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us |
| 6150 | * to only making one bmap call per transaction, so it should |
| 6151 | * be safe to have it as a local variable here. |
| 6152 | */ |
| 6153 | firstfsb = NULLFSBLOCK; |
| 6154 | |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6155 | trace_xfs_bmap_deferred(tp->t_mountp, |
| 6156 | XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type, |
| 6157 | XFS_FSB_TO_AGBNO(tp->t_mountp, startblock), |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6158 | ip->i_ino, whichfork, startoff, *blockcount, state); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6159 | |
Christoph Hellwig | 39e07da | 2017-04-11 16:45:53 -0700 | [diff] [blame] | 6160 | if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6161 | return -EFSCORRUPTED; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6162 | |
| 6163 | if (XFS_TEST_ERROR(false, tp->t_mountp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 6164 | XFS_ERRTAG_BMAP_FINISH_ONE)) |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6165 | return -EIO; |
| 6166 | |
| 6167 | switch (type) { |
| 6168 | case XFS_BMAP_MAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6169 | error = xfs_bmapi_remap(tp, ip, startoff, *blockcount, |
Christoph Hellwig | 6ebd5a4 | 2017-04-11 16:45:55 -0700 | [diff] [blame] | 6170 | startblock, dfops); |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6171 | *blockcount = 0; |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6172 | break; |
| 6173 | case XFS_BMAP_UNMAP: |
Darrick J. Wong | e1a4e37 | 2017-06-14 21:25:57 -0700 | [diff] [blame] | 6174 | error = __xfs_bunmapi(tp, ip, startoff, blockcount, |
| 6175 | XFS_BMAPI_REMAP, 1, &firstfsb, dfops); |
Darrick J. Wong | 9f3afb5 | 2016-10-03 09:11:28 -0700 | [diff] [blame] | 6176 | break; |
| 6177 | default: |
| 6178 | ASSERT(0); |
| 6179 | error = -EFSCORRUPTED; |
| 6180 | } |
| 6181 | |
| 6182 | return error; |
| 6183 | } |
Darrick J. Wong | 30b0984 | 2018-03-23 10:06:52 -0700 | [diff] [blame] | 6184 | |
| 6185 | /* Check that an inode's extent does not have invalid flags or bad ranges. */ |
| 6186 | xfs_failaddr_t |
| 6187 | xfs_bmap_validate_extent( |
| 6188 | struct xfs_inode *ip, |
| 6189 | int whichfork, |
| 6190 | struct xfs_bmbt_irec *irec) |
| 6191 | { |
| 6192 | struct xfs_mount *mp = ip->i_mount; |
| 6193 | xfs_fsblock_t endfsb; |
| 6194 | bool isrt; |
| 6195 | |
| 6196 | isrt = XFS_IS_REALTIME_INODE(ip); |
| 6197 | endfsb = irec->br_startblock + irec->br_blockcount - 1; |
| 6198 | if (isrt) { |
| 6199 | if (!xfs_verify_rtbno(mp, irec->br_startblock)) |
| 6200 | return __this_address; |
| 6201 | if (!xfs_verify_rtbno(mp, endfsb)) |
| 6202 | return __this_address; |
| 6203 | } else { |
| 6204 | if (!xfs_verify_fsbno(mp, irec->br_startblock)) |
| 6205 | return __this_address; |
| 6206 | if (!xfs_verify_fsbno(mp, endfsb)) |
| 6207 | return __this_address; |
| 6208 | if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) != |
| 6209 | XFS_FSB_TO_AGNO(mp, endfsb)) |
| 6210 | return __this_address; |
| 6211 | } |
| 6212 | if (irec->br_state != XFS_EXT_NORM) { |
| 6213 | if (whichfork != XFS_DATA_FORK) |
| 6214 | return __this_address; |
| 6215 | if (!xfs_sb_version_hasextflgbit(&mp->m_sb)) |
| 6216 | return __this_address; |
| 6217 | } |
| 6218 | return NULL; |
| 6219 | } |