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 | 70a9883c | 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" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 27 | #include "xfs_da_format.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_da_btree.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 29 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_btree.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 32 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_extfree_item.h" |
| 35 | #include "xfs_alloc.h" |
| 36 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 37 | #include "xfs_bmap_util.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 38 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_rtalloc.h" |
| 40 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_quota.h" |
| 42 | #include "xfs_trans_space.h" |
| 43 | #include "xfs_buf_item.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 44 | #include "xfs_trace.h" |
Dave Chinner | 19de735 | 2013-04-03 16:11:18 +1100 | [diff] [blame] | 45 | #include "xfs_symlink.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 46 | #include "xfs_attr_leaf.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 47 | #include "xfs_filestream.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 51 | |
| 52 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 53 | * Miscellaneous helper functions |
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 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 57 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 58 | * in this filesystem. Done once, during mount. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 60 | void |
| 61 | xfs_bmap_compute_maxlevels( |
| 62 | xfs_mount_t *mp, /* file system mount structure */ |
| 63 | int whichfork) /* data or attr fork */ |
| 64 | { |
| 65 | int level; /* btree level */ |
| 66 | uint maxblocks; /* max blocks at this level */ |
| 67 | uint maxleafents; /* max leaf entries possible */ |
| 68 | int maxrootrecs; /* max records in root block */ |
| 69 | int minleafrecs; /* min records in leaf block */ |
| 70 | int minnoderecs; /* min records in node block */ |
| 71 | int sz; /* root block size */ |
| 72 | |
| 73 | /* |
| 74 | * The maximum number of extents in a file, hence the maximum |
| 75 | * number of leaf entries, is controlled by the type of di_nextents |
| 76 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 77 | * (a signed 16-bit number, xfs_aextnum_t). |
| 78 | * |
| 79 | * Note that we can no longer assume that if we are in ATTR1 that |
| 80 | * the fork offset of all the inodes will be |
| 81 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 82 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 83 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 84 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 85 | * of a minimum size available. |
| 86 | */ |
| 87 | if (whichfork == XFS_DATA_FORK) { |
| 88 | maxleafents = MAXEXTNUM; |
| 89 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
| 90 | } else { |
| 91 | maxleafents = MAXAEXTNUM; |
| 92 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 93 | } |
Eric Sandeen | 152d93b | 2014-04-14 18:58:51 +1000 | [diff] [blame] | 94 | maxrootrecs = xfs_bmdr_maxrecs(sz, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 95 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 96 | minnoderecs = mp->m_bmap_dmnr[1]; |
| 97 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 98 | for (level = 1; maxblocks > 1; level++) { |
| 99 | if (maxblocks <= maxrootrecs) |
| 100 | maxblocks = 1; |
| 101 | else |
| 102 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 103 | } |
| 104 | mp->m_bm_maxlevels[whichfork] = level; |
| 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 107 | STATIC int /* error */ |
| 108 | xfs_bmbt_lookup_eq( |
| 109 | struct xfs_btree_cur *cur, |
| 110 | xfs_fileoff_t off, |
| 111 | xfs_fsblock_t bno, |
| 112 | xfs_filblks_t len, |
| 113 | int *stat) /* success/failure */ |
| 114 | { |
| 115 | cur->bc_rec.b.br_startoff = off; |
| 116 | cur->bc_rec.b.br_startblock = bno; |
| 117 | cur->bc_rec.b.br_blockcount = len; |
| 118 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 119 | } |
| 120 | |
| 121 | STATIC int /* error */ |
| 122 | xfs_bmbt_lookup_ge( |
| 123 | struct xfs_btree_cur *cur, |
| 124 | xfs_fileoff_t off, |
| 125 | xfs_fsblock_t bno, |
| 126 | xfs_filblks_t len, |
| 127 | int *stat) /* success/failure */ |
| 128 | { |
| 129 | cur->bc_rec.b.br_startoff = off; |
| 130 | cur->bc_rec.b.br_startblock = bno; |
| 131 | cur->bc_rec.b.br_blockcount = len; |
| 132 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 133 | } |
| 134 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 135 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 136 | * Check if the inode needs to be converted to btree format. |
| 137 | */ |
| 138 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 139 | { |
| 140 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 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 | { |
| 150 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 151 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 152 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 157 | * by [off, bno, len, state]. |
| 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, |
| 163 | xfs_fileoff_t off, |
| 164 | xfs_fsblock_t bno, |
| 165 | xfs_filblks_t len, |
| 166 | xfs_exntst_t state) |
| 167 | { |
| 168 | union xfs_btree_rec rec; |
| 169 | |
| 170 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 171 | return xfs_btree_update(cur, &rec); |
| 172 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 175 | * Compute the worst-case number of indirect blocks that will be used |
| 176 | * for ip's delayed extent of length "len". |
| 177 | */ |
| 178 | STATIC xfs_filblks_t |
| 179 | xfs_bmap_worst_indlen( |
| 180 | xfs_inode_t *ip, /* incore inode pointer */ |
| 181 | xfs_filblks_t len) /* delayed extent length */ |
| 182 | { |
| 183 | int level; /* btree level number */ |
| 184 | int maxrecs; /* maximum record count at this level */ |
| 185 | xfs_mount_t *mp; /* mount structure */ |
| 186 | xfs_filblks_t rval; /* return value */ |
| 187 | |
| 188 | mp = ip->i_mount; |
| 189 | maxrecs = mp->m_bmap_dmxr[0]; |
| 190 | for (level = 0, rval = 0; |
| 191 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 192 | level++) { |
| 193 | len += maxrecs - 1; |
| 194 | do_div(len, maxrecs); |
| 195 | rval += len; |
| 196 | if (len == 1) |
| 197 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 198 | level - 1; |
| 199 | if (level == 0) |
| 200 | maxrecs = mp->m_bmap_dmxr[1]; |
| 201 | } |
| 202 | return rval; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Calculate the default attribute fork offset for newly created inodes. |
| 207 | */ |
| 208 | uint |
| 209 | xfs_default_attroffset( |
| 210 | struct xfs_inode *ip) |
| 211 | { |
| 212 | struct xfs_mount *mp = ip->i_mount; |
| 213 | uint offset; |
| 214 | |
| 215 | if (mp->m_sb.sb_inodesize == 256) { |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 216 | offset = XFS_LITINO(mp, ip->i_d.di_version) - |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 217 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 218 | } else { |
| 219 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 220 | } |
| 221 | |
Christoph Hellwig | 56cea2d | 2013-03-12 23:30:36 +1100 | [diff] [blame] | 222 | ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version)); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 223 | return offset; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Helper routine to reset inode di_forkoff field when switching |
| 228 | * attribute fork from local to extent format - we reset it where |
| 229 | * possible to make space available for inline data fork extents. |
| 230 | */ |
| 231 | STATIC void |
| 232 | xfs_bmap_forkoff_reset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 233 | xfs_inode_t *ip, |
| 234 | int whichfork) |
| 235 | { |
| 236 | if (whichfork == XFS_ATTR_FORK && |
| 237 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 238 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 239 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 240 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 241 | |
| 242 | if (dfl_forkoff > ip->i_d.di_forkoff) |
| 243 | ip->i_d.di_forkoff = dfl_forkoff; |
| 244 | } |
| 245 | } |
| 246 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 247 | #ifdef DEBUG |
| 248 | STATIC struct xfs_buf * |
| 249 | xfs_bmap_get_bp( |
| 250 | struct xfs_btree_cur *cur, |
| 251 | xfs_fsblock_t bno) |
| 252 | { |
| 253 | struct xfs_log_item_desc *lidp; |
| 254 | int i; |
| 255 | |
| 256 | if (!cur) |
| 257 | return NULL; |
| 258 | |
| 259 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 260 | if (!cur->bc_bufs[i]) |
| 261 | break; |
| 262 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 263 | return cur->bc_bufs[i]; |
| 264 | } |
| 265 | |
| 266 | /* Chase down all the log items to see if the bp is there */ |
| 267 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 268 | struct xfs_buf_log_item *bip; |
| 269 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 270 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 271 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 272 | return bip->bli_buf; |
| 273 | } |
| 274 | |
| 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | STATIC void |
| 279 | xfs_check_block( |
| 280 | struct xfs_btree_block *block, |
| 281 | xfs_mount_t *mp, |
| 282 | int root, |
| 283 | short sz) |
| 284 | { |
| 285 | int i, j, dmxr; |
| 286 | __be64 *pp, *thispa; /* pointer to block address */ |
| 287 | xfs_bmbt_key_t *prevp, *keyp; |
| 288 | |
| 289 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 290 | |
| 291 | prevp = NULL; |
| 292 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 293 | dmxr = mp->m_bmap_dmxr[0]; |
| 294 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 295 | |
| 296 | if (prevp) { |
| 297 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 298 | be64_to_cpu(keyp->br_startoff)); |
| 299 | } |
| 300 | prevp = keyp; |
| 301 | |
| 302 | /* |
| 303 | * Compare the block numbers to see if there are dups. |
| 304 | */ |
| 305 | if (root) |
| 306 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 307 | else |
| 308 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 309 | |
| 310 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 311 | if (root) |
| 312 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 313 | else |
| 314 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 315 | if (*thispa == *pp) { |
| 316 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 317 | __func__, j, i, |
| 318 | (unsigned long long)be64_to_cpu(*thispa)); |
| 319 | panic("%s: ptrs are equal in node\n", |
| 320 | __func__); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Check that the extents for the inode ip are in the right order in all |
| 328 | * btree leaves. |
| 329 | */ |
| 330 | |
| 331 | STATIC void |
| 332 | xfs_bmap_check_leaf_extents( |
| 333 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 334 | xfs_inode_t *ip, /* incore inode pointer */ |
| 335 | int whichfork) /* data or attr fork */ |
| 336 | { |
| 337 | struct xfs_btree_block *block; /* current btree block */ |
| 338 | xfs_fsblock_t bno; /* block # of "block" */ |
| 339 | xfs_buf_t *bp; /* buffer for "block" */ |
| 340 | int error; /* error return value */ |
| 341 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 342 | xfs_ifork_t *ifp; /* fork structure */ |
| 343 | int level; /* btree level, for checking */ |
| 344 | xfs_mount_t *mp; /* file system mount structure */ |
| 345 | __be64 *pp; /* pointer to block address */ |
| 346 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 347 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 348 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 349 | int bp_release = 0; |
| 350 | |
| 351 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | bno = NULLFSBLOCK; |
| 356 | mp = ip->i_mount; |
| 357 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 358 | block = ifp->if_broot; |
| 359 | /* |
| 360 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 361 | */ |
| 362 | level = be16_to_cpu(block->bb_level); |
| 363 | ASSERT(level > 0); |
| 364 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 365 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 366 | bno = be64_to_cpu(*pp); |
| 367 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 368 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 369 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 370 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 371 | |
| 372 | /* |
| 373 | * Go down the tree until leaf level is reached, following the first |
| 374 | * pointer (leftmost) at each level. |
| 375 | */ |
| 376 | while (level-- > 0) { |
| 377 | /* See if buf is in cur first */ |
| 378 | bp_release = 0; |
| 379 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 380 | if (!bp) { |
| 381 | bp_release = 1; |
| 382 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 383 | XFS_BMAP_BTREE_REF, |
| 384 | &xfs_bmbt_buf_ops); |
| 385 | if (error) |
| 386 | goto error_norelse; |
| 387 | } |
| 388 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 389 | if (level == 0) |
| 390 | break; |
| 391 | |
| 392 | /* |
| 393 | * Check this block for basic sanity (increasing keys and |
| 394 | * no duplicate blocks). |
| 395 | */ |
| 396 | |
| 397 | xfs_check_block(block, mp, 0, 0); |
| 398 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 399 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 400 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 401 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 402 | if (bp_release) { |
| 403 | bp_release = 0; |
| 404 | xfs_trans_brelse(NULL, bp); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 410 | */ |
| 411 | i = 0; |
| 412 | |
| 413 | /* |
| 414 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 415 | */ |
| 416 | for (;;) { |
| 417 | xfs_fsblock_t nextbno; |
| 418 | xfs_extnum_t num_recs; |
| 419 | |
| 420 | |
| 421 | num_recs = xfs_btree_get_numrecs(block); |
| 422 | |
| 423 | /* |
| 424 | * Read-ahead the next leaf block, if any. |
| 425 | */ |
| 426 | |
| 427 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 428 | |
| 429 | /* |
| 430 | * Check all the extents to make sure they are OK. |
| 431 | * If we had a previous block, the last entry should |
| 432 | * conform with the first entry in this one. |
| 433 | */ |
| 434 | |
| 435 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 436 | if (i) { |
| 437 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 438 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 439 | xfs_bmbt_disk_get_startoff(ep)); |
| 440 | } |
| 441 | for (j = 1; j < num_recs; j++) { |
| 442 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 443 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 444 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 445 | xfs_bmbt_disk_get_startoff(nextp)); |
| 446 | ep = nextp; |
| 447 | } |
| 448 | |
| 449 | last = *ep; |
| 450 | i += num_recs; |
| 451 | if (bp_release) { |
| 452 | bp_release = 0; |
| 453 | xfs_trans_brelse(NULL, bp); |
| 454 | } |
| 455 | bno = nextbno; |
| 456 | /* |
| 457 | * If we've reached the end, stop. |
| 458 | */ |
| 459 | if (bno == NULLFSBLOCK) |
| 460 | break; |
| 461 | |
| 462 | bp_release = 0; |
| 463 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 464 | if (!bp) { |
| 465 | bp_release = 1; |
| 466 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 467 | XFS_BMAP_BTREE_REF, |
| 468 | &xfs_bmbt_buf_ops); |
| 469 | if (error) |
| 470 | goto error_norelse; |
| 471 | } |
| 472 | block = XFS_BUF_TO_BLOCK(bp); |
| 473 | } |
| 474 | if (bp_release) { |
| 475 | bp_release = 0; |
| 476 | xfs_trans_brelse(NULL, bp); |
| 477 | } |
| 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); |
| 487 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Add bmap trace insert entries for all the contents of the extent records. |
| 493 | */ |
| 494 | void |
| 495 | xfs_bmap_trace_exlist( |
| 496 | xfs_inode_t *ip, /* incore inode pointer */ |
| 497 | xfs_extnum_t cnt, /* count of entries in the list */ |
| 498 | int whichfork, /* data or attr fork */ |
| 499 | unsigned long caller_ip) |
| 500 | { |
| 501 | xfs_extnum_t idx; /* extent record index */ |
| 502 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 503 | int state = 0; |
| 504 | |
| 505 | if (whichfork == XFS_ATTR_FORK) |
| 506 | state |= BMAP_ATTRFORK; |
| 507 | |
| 508 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 509 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 510 | for (idx = 0; idx < cnt; idx++) |
| 511 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 516 | * given the caller's original parameters. Specifically check the |
| 517 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 518 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 519 | */ |
| 520 | STATIC void |
| 521 | xfs_bmap_validate_ret( |
| 522 | xfs_fileoff_t bno, |
| 523 | xfs_filblks_t len, |
| 524 | int flags, |
| 525 | xfs_bmbt_irec_t *mval, |
| 526 | int nmap, |
| 527 | int ret_nmap) |
| 528 | { |
| 529 | int i; /* index to map values */ |
| 530 | |
| 531 | ASSERT(ret_nmap <= nmap); |
| 532 | |
| 533 | for (i = 0; i < ret_nmap; i++) { |
| 534 | ASSERT(mval[i].br_blockcount > 0); |
| 535 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 536 | ASSERT(mval[i].br_startoff >= bno); |
| 537 | ASSERT(mval[i].br_blockcount <= len); |
| 538 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 539 | bno + len); |
| 540 | } else { |
| 541 | ASSERT(mval[i].br_startoff < bno + len); |
| 542 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 543 | bno); |
| 544 | } |
| 545 | ASSERT(i == 0 || |
| 546 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 547 | mval[i].br_startoff); |
| 548 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 549 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 550 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 551 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | #else |
| 556 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 557 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 558 | #endif /* DEBUG */ |
| 559 | |
| 560 | /* |
| 561 | * bmap free list manipulation functions |
| 562 | */ |
| 563 | |
| 564 | /* |
| 565 | * Add the extent to the list of extents to be free at transaction end. |
| 566 | * The list is maintained sorted (by block number). |
| 567 | */ |
| 568 | void |
| 569 | xfs_bmap_add_free( |
| 570 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 571 | xfs_filblks_t len, /* length of extent */ |
| 572 | xfs_bmap_free_t *flist, /* list of extents */ |
| 573 | xfs_mount_t *mp) /* mount point structure */ |
| 574 | { |
| 575 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 576 | xfs_bmap_free_item_t *new; /* new element */ |
| 577 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 578 | #ifdef DEBUG |
| 579 | xfs_agnumber_t agno; |
| 580 | xfs_agblock_t agbno; |
| 581 | |
| 582 | ASSERT(bno != NULLFSBLOCK); |
| 583 | ASSERT(len > 0); |
| 584 | ASSERT(len <= MAXEXTLEN); |
| 585 | ASSERT(!isnullstartblock(bno)); |
| 586 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 587 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 588 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 589 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 590 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 591 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 592 | #endif |
| 593 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 594 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 595 | new->xbfi_startblock = bno; |
| 596 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 597 | for (prev = NULL, cur = flist->xbf_first; |
| 598 | cur != NULL; |
| 599 | prev = cur, cur = cur->xbfi_next) { |
| 600 | if (cur->xbfi_startblock >= bno) |
| 601 | break; |
| 602 | } |
| 603 | if (prev) |
| 604 | prev->xbfi_next = new; |
| 605 | else |
| 606 | flist->xbf_first = new; |
| 607 | new->xbfi_next = cur; |
| 608 | flist->xbf_count++; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Remove the entry "free" from the free item list. Prev points to the |
| 613 | * previous entry, unless "free" is the head of the list. |
| 614 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 615 | void |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 616 | xfs_bmap_del_free( |
| 617 | xfs_bmap_free_t *flist, /* free item list header */ |
| 618 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 619 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 620 | { |
| 621 | if (prev) |
| 622 | prev->xbfi_next = free->xbfi_next; |
| 623 | else |
| 624 | flist->xbf_first = free->xbfi_next; |
| 625 | flist->xbf_count--; |
| 626 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 627 | } |
| 628 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 629 | /* |
| 630 | * Free up any items left in the list. |
| 631 | */ |
| 632 | void |
| 633 | xfs_bmap_cancel( |
| 634 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 635 | { |
| 636 | xfs_bmap_free_item_t *free; /* free list item */ |
| 637 | xfs_bmap_free_item_t *next; |
| 638 | |
| 639 | if (flist->xbf_count == 0) |
| 640 | return; |
| 641 | ASSERT(flist->xbf_first != NULL); |
| 642 | for (free = flist->xbf_first; free; free = next) { |
| 643 | next = free->xbfi_next; |
| 644 | xfs_bmap_del_free(flist, NULL, free); |
| 645 | } |
| 646 | ASSERT(flist->xbf_count == 0); |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Inode fork format manipulation functions |
| 651 | */ |
| 652 | |
| 653 | /* |
| 654 | * Transform a btree format file with only one leaf node, where the |
| 655 | * extents list will fit in the inode, into an extents format file. |
| 656 | * Since the file extents are already in-core, all we have to do is |
| 657 | * give up the space for the btree root and pitch the leaf block. |
| 658 | */ |
| 659 | STATIC int /* error */ |
| 660 | xfs_bmap_btree_to_extents( |
| 661 | xfs_trans_t *tp, /* transaction pointer */ |
| 662 | xfs_inode_t *ip, /* incore inode pointer */ |
| 663 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 664 | int *logflagsp, /* inode logging flags */ |
| 665 | int whichfork) /* data or attr fork */ |
| 666 | { |
| 667 | /* REFERENCED */ |
| 668 | struct xfs_btree_block *cblock;/* child btree block */ |
| 669 | xfs_fsblock_t cbno; /* child block number */ |
| 670 | xfs_buf_t *cbp; /* child block's buffer */ |
| 671 | int error; /* error return value */ |
| 672 | xfs_ifork_t *ifp; /* inode fork data */ |
| 673 | xfs_mount_t *mp; /* mount point structure */ |
| 674 | __be64 *pp; /* ptr to block address */ |
| 675 | struct xfs_btree_block *rblock;/* root btree block */ |
| 676 | |
| 677 | mp = ip->i_mount; |
| 678 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 679 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 680 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 681 | rblock = ifp->if_broot; |
| 682 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 683 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 684 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 685 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 686 | cbno = be64_to_cpu(*pp); |
| 687 | *logflagsp = 0; |
| 688 | #ifdef DEBUG |
| 689 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 690 | return error; |
| 691 | #endif |
| 692 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 693 | &xfs_bmbt_buf_ops); |
| 694 | if (error) |
| 695 | return error; |
| 696 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 697 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 698 | return error; |
| 699 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 700 | ip->i_d.di_nblocks--; |
| 701 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 702 | xfs_trans_binval(tp, cbp); |
| 703 | if (cur->bc_bufs[0] == cbp) |
| 704 | cur->bc_bufs[0] = NULL; |
| 705 | xfs_iroot_realloc(ip, -1, whichfork); |
| 706 | ASSERT(ifp->if_broot == NULL); |
| 707 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 708 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 709 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Convert an extents-format file into a btree-format file. |
| 715 | * The new file will have a root block (in the inode) and a single child block. |
| 716 | */ |
| 717 | STATIC int /* error */ |
| 718 | xfs_bmap_extents_to_btree( |
| 719 | xfs_trans_t *tp, /* transaction pointer */ |
| 720 | xfs_inode_t *ip, /* incore inode pointer */ |
| 721 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 722 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 723 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 724 | int wasdel, /* converting a delayed alloc */ |
| 725 | int *logflagsp, /* inode logging flags */ |
| 726 | int whichfork) /* data or attr fork */ |
| 727 | { |
| 728 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 729 | xfs_buf_t *abp; /* buffer for ablock */ |
| 730 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 731 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 732 | struct xfs_btree_block *block; /* btree root block */ |
| 733 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 734 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 735 | int error; /* error return value */ |
| 736 | xfs_extnum_t i, cnt; /* extent record index */ |
| 737 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 738 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 739 | xfs_mount_t *mp; /* mount structure */ |
| 740 | xfs_extnum_t nextents; /* number of file extents */ |
| 741 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 742 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 743 | mp = ip->i_mount; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 744 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 745 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 746 | |
| 747 | /* |
| 748 | * Make space in the inode incore. |
| 749 | */ |
| 750 | xfs_iroot_realloc(ip, 1, whichfork); |
| 751 | ifp->if_flags |= XFS_IFBROOT; |
| 752 | |
| 753 | /* |
| 754 | * Fill in the root. |
| 755 | */ |
| 756 | block = ifp->if_broot; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 757 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 758 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 759 | XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino, |
| 760 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 761 | else |
| 762 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 763 | XFS_BMAP_MAGIC, 1, 1, ip->i_ino, |
| 764 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 768 | */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 769 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 770 | cur->bc_private.b.firstblock = *firstblock; |
| 771 | cur->bc_private.b.flist = flist; |
| 772 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 773 | /* |
| 774 | * Convert to a btree with two levels, one record in root. |
| 775 | */ |
| 776 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 777 | memset(&args, 0, sizeof(args)); |
| 778 | args.tp = tp; |
| 779 | args.mp = mp; |
| 780 | args.firstblock = *firstblock; |
| 781 | if (*firstblock == NULLFSBLOCK) { |
| 782 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 783 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 784 | } else if (flist->xbf_low) { |
| 785 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 786 | args.fsbno = *firstblock; |
| 787 | } else { |
| 788 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 789 | args.fsbno = *firstblock; |
| 790 | } |
| 791 | args.minlen = args.maxlen = args.prod = 1; |
| 792 | args.wasdel = wasdel; |
| 793 | *logflagsp = 0; |
| 794 | if ((error = xfs_alloc_vextent(&args))) { |
| 795 | xfs_iroot_realloc(ip, -1, whichfork); |
| 796 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 797 | return error; |
| 798 | } |
| 799 | /* |
| 800 | * Allocation can't fail, the space was reserved. |
| 801 | */ |
| 802 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 803 | ASSERT(*firstblock == NULLFSBLOCK || |
| 804 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 805 | (flist->xbf_low && |
| 806 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 807 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 808 | cur->bc_private.b.allocated++; |
| 809 | ip->i_d.di_nblocks++; |
| 810 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 811 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 812 | /* |
| 813 | * Fill in the child block. |
| 814 | */ |
| 815 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 816 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 817 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 818 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 819 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, |
| 820 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 821 | else |
| 822 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 823 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, |
| 824 | XFS_BTREE_LONG_PTRS); |
| 825 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 826 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 827 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 828 | for (cnt = i = 0; i < nextents; i++) { |
| 829 | ep = xfs_iext_get_ext(ifp, i); |
| 830 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 831 | arp->l0 = cpu_to_be64(ep->l0); |
| 832 | arp->l1 = cpu_to_be64(ep->l1); |
| 833 | arp++; cnt++; |
| 834 | } |
| 835 | } |
| 836 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 837 | xfs_btree_set_numrecs(ablock, cnt); |
| 838 | |
| 839 | /* |
| 840 | * Fill in the root key and pointer. |
| 841 | */ |
| 842 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 843 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 844 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 845 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 846 | be16_to_cpu(block->bb_level))); |
| 847 | *pp = cpu_to_be64(args.fsbno); |
| 848 | |
| 849 | /* |
| 850 | * Do all this logging at the end so that |
| 851 | * the root is at the right level. |
| 852 | */ |
| 853 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 854 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 855 | ASSERT(*curp == NULL); |
| 856 | *curp = cur; |
| 857 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Convert a local file to an extents file. |
| 863 | * This code is out of bounds for data forks of regular files, |
| 864 | * since the file data needs to get logged so things will stay consistent. |
| 865 | * (The bmap-level manipulations are ok, though). |
| 866 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 867 | void |
| 868 | xfs_bmap_local_to_extents_empty( |
| 869 | struct xfs_inode *ip, |
| 870 | int whichfork) |
| 871 | { |
| 872 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 873 | |
| 874 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 875 | ASSERT(ifp->if_bytes == 0); |
| 876 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 877 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 878 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 879 | ifp->if_flags &= ~XFS_IFINLINE; |
| 880 | ifp->if_flags |= XFS_IFEXTENTS; |
| 881 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 882 | } |
| 883 | |
| 884 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 885 | STATIC int /* error */ |
| 886 | xfs_bmap_local_to_extents( |
| 887 | xfs_trans_t *tp, /* transaction pointer */ |
| 888 | xfs_inode_t *ip, /* incore inode pointer */ |
| 889 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 890 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 891 | int *logflagsp, /* inode logging flags */ |
| 892 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 893 | void (*init_fn)(struct xfs_trans *tp, |
| 894 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 895 | struct xfs_inode *ip, |
| 896 | struct xfs_ifork *ifp)) |
| 897 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 898 | int error = 0; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 899 | int flags; /* logging flags returned */ |
| 900 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 901 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 902 | xfs_buf_t *bp; /* buffer for extent block */ |
| 903 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * We don't want to deal with the case of keeping inode data inline yet. |
| 907 | * So sending the data fork of a regular inode is invalid. |
| 908 | */ |
| 909 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); |
| 910 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 911 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 912 | |
| 913 | if (!ifp->if_bytes) { |
| 914 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 915 | flags = XFS_ILOG_CORE; |
| 916 | goto done; |
| 917 | } |
| 918 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 919 | flags = 0; |
| 920 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 921 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 922 | XFS_IFINLINE); |
| 923 | memset(&args, 0, sizeof(args)); |
| 924 | args.tp = tp; |
| 925 | args.mp = ip->i_mount; |
| 926 | args.firstblock = *firstblock; |
| 927 | /* |
| 928 | * Allocate a block. We know we need only one, since the |
| 929 | * file currently fits in an inode. |
| 930 | */ |
| 931 | if (*firstblock == NULLFSBLOCK) { |
| 932 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 933 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 934 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 935 | args.fsbno = *firstblock; |
| 936 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 937 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 938 | args.total = total; |
| 939 | args.minlen = args.maxlen = args.prod = 1; |
| 940 | error = xfs_alloc_vextent(&args); |
| 941 | if (error) |
| 942 | goto done; |
| 943 | |
| 944 | /* Can't fail, the space was reserved. */ |
| 945 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 946 | ASSERT(args.len == 1); |
| 947 | *firstblock = args.fsbno; |
| 948 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 949 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 950 | /* |
| 951 | * Initialise the block and copy the data |
| 952 | * |
| 953 | * Note: init_fn must set the buffer log item type correctly! |
| 954 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 955 | init_fn(tp, bp, ip, ifp); |
| 956 | |
| 957 | /* account for the change in fork size and log everything */ |
| 958 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
| 959 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 960 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 961 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 962 | |
| 963 | xfs_iext_add(ifp, 0, 1); |
| 964 | ep = xfs_iext_get_ext(ifp, 0); |
| 965 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 966 | trace_xfs_bmap_post_update(ip, 0, |
| 967 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 968 | _THIS_IP_); |
| 969 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 970 | ip->i_d.di_nblocks = 1; |
| 971 | xfs_trans_mod_dquot_byino(tp, ip, |
| 972 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 973 | flags |= xfs_ilog_fext(whichfork); |
| 974 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 975 | done: |
| 976 | *logflagsp = flags; |
| 977 | return error; |
| 978 | } |
| 979 | |
| 980 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 982 | */ |
| 983 | STATIC int /* error */ |
| 984 | xfs_bmap_add_attrfork_btree( |
| 985 | xfs_trans_t *tp, /* transaction pointer */ |
| 986 | xfs_inode_t *ip, /* incore inode pointer */ |
| 987 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 988 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 989 | int *flags) /* inode logging flags */ |
| 990 | { |
| 991 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 992 | int error; /* error return value */ |
| 993 | xfs_mount_t *mp; /* file system mount struct */ |
| 994 | int stat; /* newroot status */ |
| 995 | |
| 996 | mp = ip->i_mount; |
| 997 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 998 | *flags |= XFS_ILOG_DBROOT; |
| 999 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1000 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | cur->bc_private.b.flist = flist; |
| 1002 | cur->bc_private.b.firstblock = *firstblock; |
| 1003 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 1004 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1005 | /* must be at least one entry */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1006 | XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 1007 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | goto error0; |
| 1009 | if (stat == 0) { |
| 1010 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1011 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } |
| 1013 | *firstblock = cur->bc_private.b.firstblock; |
| 1014 | cur->bc_private.b.allocated = 0; |
| 1015 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1016 | } |
| 1017 | return 0; |
| 1018 | error0: |
| 1019 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1020 | return error; |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1025 | */ |
| 1026 | STATIC int /* error */ |
| 1027 | xfs_bmap_add_attrfork_extents( |
| 1028 | xfs_trans_t *tp, /* transaction pointer */ |
| 1029 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1030 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 1031 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 1032 | int *flags) /* inode logging flags */ |
| 1033 | { |
| 1034 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1035 | int error; /* error return value */ |
| 1036 | |
| 1037 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1038 | return 0; |
| 1039 | cur = NULL; |
| 1040 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 1041 | flags, XFS_DATA_FORK); |
| 1042 | if (cur) { |
| 1043 | cur->bc_private.b.allocated = 0; |
| 1044 | xfs_btree_del_cursor(cur, |
| 1045 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1046 | } |
| 1047 | return error; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1051 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1052 | * different data fork content type needs a different callout to do the |
| 1053 | * conversion. Some are basic and only require special block initialisation |
| 1054 | * callouts for the data formating, others (directories) are so specialised they |
| 1055 | * handle everything themselves. |
| 1056 | * |
| 1057 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1058 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1059 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | */ |
| 1061 | STATIC int /* error */ |
| 1062 | xfs_bmap_add_attrfork_local( |
| 1063 | xfs_trans_t *tp, /* transaction pointer */ |
| 1064 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1065 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 1066 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 1067 | int *flags) /* inode logging flags */ |
| 1068 | { |
| 1069 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1072 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1073 | |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 1074 | if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1076 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | dargs.dp = ip; |
| 1078 | dargs.firstblock = firstblock; |
| 1079 | dargs.flist = flist; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1080 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | dargs.whichfork = XFS_DATA_FORK; |
| 1082 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1083 | return xfs_dir2_sf_to_block(&dargs); |
| 1084 | } |
| 1085 | |
| 1086 | if (S_ISLNK(ip->i_d.di_mode)) |
| 1087 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1088 | flags, XFS_DATA_FORK, |
| 1089 | xfs_symlink_local_to_remote); |
| 1090 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1091 | /* should only be called for types that support local format data */ |
| 1092 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1093 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1097 | * Convert inode from non-attributed to attributed. |
| 1098 | * Must not be in a transaction, ip must not be locked. |
| 1099 | */ |
| 1100 | int /* error code */ |
| 1101 | xfs_bmap_add_attrfork( |
| 1102 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1103 | int size, /* space new attribute needs */ |
| 1104 | int rsvd) /* xact may use reserved blks */ |
| 1105 | { |
| 1106 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
| 1107 | xfs_bmap_free_t flist; /* freed extent records */ |
| 1108 | xfs_mount_t *mp; /* mount structure */ |
| 1109 | xfs_trans_t *tp; /* transaction pointer */ |
| 1110 | int blks; /* space reservation */ |
| 1111 | int version = 1; /* superblock attr version */ |
| 1112 | int committed; /* xaction was committed */ |
| 1113 | int logflags; /* logging flags */ |
| 1114 | int error; /* error return value */ |
| 1115 | |
| 1116 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1117 | |
| 1118 | mp = ip->i_mount; |
| 1119 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 1120 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 1121 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 1122 | if (rsvd) |
| 1123 | tp->t_flags |= XFS_TRANS_RESERVE; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1124 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1125 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame^] | 1126 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1127 | return error; |
| 1128 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1129 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1130 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1131 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1132 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1133 | if (error) |
| 1134 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1135 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1136 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1137 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1138 | /* |
| 1139 | * For inodes coming from pre-6.2 filesystems. |
| 1140 | */ |
| 1141 | ASSERT(ip->i_d.di_aformat == 0); |
| 1142 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1143 | } |
| 1144 | ASSERT(ip->i_d.di_anextents == 0); |
| 1145 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1146 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1147 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1148 | |
| 1149 | switch (ip->i_d.di_format) { |
| 1150 | case XFS_DINODE_FMT_DEV: |
| 1151 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1152 | break; |
| 1153 | case XFS_DINODE_FMT_UUID: |
| 1154 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1155 | break; |
| 1156 | case XFS_DINODE_FMT_LOCAL: |
| 1157 | case XFS_DINODE_FMT_EXTENTS: |
| 1158 | case XFS_DINODE_FMT_BTREE: |
| 1159 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1160 | if (!ip->i_d.di_forkoff) |
| 1161 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1162 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1163 | version = 2; |
| 1164 | break; |
| 1165 | default: |
| 1166 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1167 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1168 | goto trans_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | ASSERT(ip->i_afp == NULL); |
| 1172 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1173 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1174 | logflags = 0; |
| 1175 | xfs_bmap_init(&flist, &firstblock); |
| 1176 | switch (ip->i_d.di_format) { |
| 1177 | case XFS_DINODE_FMT_LOCAL: |
| 1178 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 1179 | &logflags); |
| 1180 | break; |
| 1181 | case XFS_DINODE_FMT_EXTENTS: |
| 1182 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 1183 | &flist, &logflags); |
| 1184 | break; |
| 1185 | case XFS_DINODE_FMT_BTREE: |
| 1186 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 1187 | &logflags); |
| 1188 | break; |
| 1189 | default: |
| 1190 | error = 0; |
| 1191 | break; |
| 1192 | } |
| 1193 | if (logflags) |
| 1194 | xfs_trans_log_inode(tp, ip, logflags); |
| 1195 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1196 | goto bmap_cancel; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1197 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1198 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1199 | bool log_sb = false; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1200 | |
| 1201 | spin_lock(&mp->m_sb_lock); |
| 1202 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1203 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1204 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1205 | } |
| 1206 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1207 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1208 | log_sb = true; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1209 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1210 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1211 | if (log_sb) |
| 1212 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | error = xfs_bmap_finish(&tp, &flist, &committed); |
| 1216 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1217 | goto bmap_cancel; |
| 1218 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1219 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1220 | return error; |
| 1221 | |
| 1222 | bmap_cancel: |
| 1223 | xfs_bmap_cancel(&flist); |
| 1224 | trans_cancel: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame^] | 1225 | xfs_trans_cancel(tp); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1226 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1227 | return error; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * Internal and external extent tree search functions. |
| 1232 | */ |
| 1233 | |
| 1234 | /* |
| 1235 | * Read in the extents to if_extents. |
| 1236 | * All inode fields are set up by caller, we just traverse the btree |
| 1237 | * and copy the records in. If the file system cannot contain unwritten |
| 1238 | * extents, the records are checked for no "state" flags. |
| 1239 | */ |
| 1240 | int /* error */ |
| 1241 | xfs_bmap_read_extents( |
| 1242 | xfs_trans_t *tp, /* transaction pointer */ |
| 1243 | xfs_inode_t *ip, /* incore inode */ |
| 1244 | int whichfork) /* data or attr fork */ |
| 1245 | { |
| 1246 | struct xfs_btree_block *block; /* current btree block */ |
| 1247 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1248 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1249 | int error; /* error return value */ |
| 1250 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1251 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1252 | xfs_ifork_t *ifp; /* fork structure */ |
| 1253 | int level; /* btree level, for checking */ |
| 1254 | xfs_mount_t *mp; /* file system mount structure */ |
| 1255 | __be64 *pp; /* pointer to block address */ |
| 1256 | /* REFERENCED */ |
| 1257 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1258 | |
| 1259 | bno = NULLFSBLOCK; |
| 1260 | mp = ip->i_mount; |
| 1261 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1262 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1263 | XFS_EXTFMT_INODE(ip); |
| 1264 | block = ifp->if_broot; |
| 1265 | /* |
| 1266 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1267 | */ |
| 1268 | level = be16_to_cpu(block->bb_level); |
| 1269 | ASSERT(level > 0); |
| 1270 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1271 | bno = be64_to_cpu(*pp); |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1272 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1273 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 1274 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 1275 | /* |
| 1276 | * Go down the tree until leaf level is reached, following the first |
| 1277 | * pointer (leftmost) at each level. |
| 1278 | */ |
| 1279 | while (level-- > 0) { |
| 1280 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1281 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1282 | if (error) |
| 1283 | return error; |
| 1284 | block = XFS_BUF_TO_BLOCK(bp); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1285 | if (level == 0) |
| 1286 | break; |
| 1287 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1288 | bno = be64_to_cpu(*pp); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1289 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 1290 | XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1291 | xfs_trans_brelse(tp, bp); |
| 1292 | } |
| 1293 | /* |
| 1294 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1295 | */ |
| 1296 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1297 | i = 0; |
| 1298 | /* |
| 1299 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1300 | */ |
| 1301 | for (;;) { |
| 1302 | xfs_bmbt_rec_t *frp; |
| 1303 | xfs_fsblock_t nextbno; |
| 1304 | xfs_extnum_t num_recs; |
| 1305 | xfs_extnum_t start; |
| 1306 | |
| 1307 | num_recs = xfs_btree_get_numrecs(block); |
| 1308 | if (unlikely(i + num_recs > room)) { |
| 1309 | ASSERT(i + num_recs <= room); |
| 1310 | xfs_warn(ip->i_mount, |
| 1311 | "corrupt dinode %Lu, (btree extents).", |
| 1312 | (unsigned long long) ip->i_ino); |
| 1313 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1314 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1315 | goto error0; |
| 1316 | } |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1317 | /* |
| 1318 | * Read-ahead the next leaf block, if any. |
| 1319 | */ |
| 1320 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1321 | if (nextbno != NULLFSBLOCK) |
| 1322 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1323 | &xfs_bmbt_buf_ops); |
| 1324 | /* |
| 1325 | * Copy records into the extent records. |
| 1326 | */ |
| 1327 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1328 | start = i; |
| 1329 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1330 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1331 | trp->l0 = be64_to_cpu(frp->l0); |
| 1332 | trp->l1 = be64_to_cpu(frp->l1); |
| 1333 | } |
| 1334 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1335 | /* |
| 1336 | * Check all attribute bmap btree records and |
| 1337 | * any "older" data bmap btree records for a |
| 1338 | * set bit in the "extent flag" position. |
| 1339 | */ |
| 1340 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1341 | start, num_recs))) { |
| 1342 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1343 | XFS_ERRLEVEL_LOW, |
| 1344 | ip->i_mount); |
| 1345 | goto error0; |
| 1346 | } |
| 1347 | } |
| 1348 | xfs_trans_brelse(tp, bp); |
| 1349 | bno = nextbno; |
| 1350 | /* |
| 1351 | * If we've reached the end, stop. |
| 1352 | */ |
| 1353 | if (bno == NULLFSBLOCK) |
| 1354 | break; |
| 1355 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1356 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1357 | if (error) |
| 1358 | return error; |
| 1359 | block = XFS_BUF_TO_BLOCK(bp); |
| 1360 | } |
| 1361 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 1362 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 1363 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1364 | return 0; |
| 1365 | error0: |
| 1366 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1367 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | /* |
| 1372 | * Search the extent records for the entry containing block bno. |
| 1373 | * If bno lies in a hole, point to the next entry. If bno lies |
| 1374 | * past eof, *eofp will be set, and *prevp will contain the last |
| 1375 | * entry (null if none). Else, *lastxp will be set to the index |
| 1376 | * of the found entry; *gotp will contain the entry. |
| 1377 | */ |
| 1378 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1379 | xfs_bmap_search_multi_extents( |
| 1380 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 1381 | xfs_fileoff_t bno, /* block number searched for */ |
| 1382 | int *eofp, /* out: end of file found */ |
| 1383 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1384 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1385 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1386 | { |
| 1387 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1388 | xfs_extnum_t lastx; /* last extent index */ |
| 1389 | |
| 1390 | /* |
| 1391 | * Initialize the extent entry structure to catch access to |
| 1392 | * uninitialized br_startblock field. |
| 1393 | */ |
| 1394 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 1395 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 1396 | gotp->br_state = XFS_EXT_INVALID; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1397 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1398 | prevp->br_startoff = NULLFILEOFF; |
| 1399 | |
| 1400 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 1401 | if (lastx > 0) { |
| 1402 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 1403 | } |
| 1404 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 1405 | xfs_bmbt_get_all(ep, gotp); |
| 1406 | *eofp = 0; |
| 1407 | } else { |
| 1408 | if (lastx > 0) { |
| 1409 | *gotp = *prevp; |
| 1410 | } |
| 1411 | *eofp = 1; |
| 1412 | ep = NULL; |
| 1413 | } |
| 1414 | *lastxp = lastx; |
| 1415 | return ep; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * Search the extents list for the inode, for the extent containing bno. |
| 1420 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 1421 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 1422 | * Else, *lastxp will be set to the index of the found |
| 1423 | * entry; *gotp will contain the entry. |
| 1424 | */ |
| 1425 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1426 | xfs_bmap_search_extents( |
| 1427 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1428 | xfs_fileoff_t bno, /* block number searched for */ |
| 1429 | int fork, /* data or attr fork */ |
| 1430 | int *eofp, /* out: end of file found */ |
| 1431 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1432 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1433 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1434 | { |
| 1435 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1436 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1437 | |
| 1438 | XFS_STATS_INC(xs_look_exlist); |
| 1439 | ifp = XFS_IFORK_PTR(ip, fork); |
| 1440 | |
| 1441 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 1442 | |
| 1443 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 1444 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 1445 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
| 1446 | "Access to block zero in inode %llu " |
| 1447 | "start_block: %llx start_off: %llx " |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 1448 | "blkcnt: %llx extent-state: %x lastx: %x", |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1449 | (unsigned long long)ip->i_ino, |
| 1450 | (unsigned long long)gotp->br_startblock, |
| 1451 | (unsigned long long)gotp->br_startoff, |
| 1452 | (unsigned long long)gotp->br_blockcount, |
| 1453 | gotp->br_state, *lastxp); |
| 1454 | *lastxp = NULLEXTNUM; |
| 1455 | *eofp = 1; |
| 1456 | return NULL; |
| 1457 | } |
| 1458 | return ep; |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * Returns the file-relative block number of the first unused block(s) |
| 1463 | * in the file with at least "len" logically contiguous blocks free. |
| 1464 | * This is the lowest-address hole if the file has holes, else the first block |
| 1465 | * past the end of file. |
| 1466 | * Return 0 if the file is currently local (in-inode). |
| 1467 | */ |
| 1468 | int /* error */ |
| 1469 | xfs_bmap_first_unused( |
| 1470 | xfs_trans_t *tp, /* transaction pointer */ |
| 1471 | xfs_inode_t *ip, /* incore inode */ |
| 1472 | xfs_extlen_t len, /* size of hole to find */ |
| 1473 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1474 | int whichfork) /* data or attr fork */ |
| 1475 | { |
| 1476 | int error; /* error return value */ |
| 1477 | int idx; /* extent record index */ |
| 1478 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1479 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1480 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1481 | xfs_fileoff_t max; /* starting useful block */ |
| 1482 | xfs_fileoff_t off; /* offset for this block */ |
| 1483 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1484 | |
| 1485 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1486 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1487 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1488 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1489 | *first_unused = 0; |
| 1490 | return 0; |
| 1491 | } |
| 1492 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1493 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1494 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1495 | return error; |
| 1496 | lowest = *first_unused; |
| 1497 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1498 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1499 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1500 | off = xfs_bmbt_get_startoff(ep); |
| 1501 | /* |
| 1502 | * See if the hole before this extent will work. |
| 1503 | */ |
| 1504 | if (off >= lowest + len && off - max >= len) { |
| 1505 | *first_unused = max; |
| 1506 | return 0; |
| 1507 | } |
| 1508 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1509 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1510 | } |
| 1511 | *first_unused = max; |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1516 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1517 | * last_block (input value) in the file. |
| 1518 | * This is not based on i_size, it is based on the extent records. |
| 1519 | * Returns 0 for local files, as they do not have extent records. |
| 1520 | */ |
| 1521 | int /* error */ |
| 1522 | xfs_bmap_last_before( |
| 1523 | xfs_trans_t *tp, /* transaction pointer */ |
| 1524 | xfs_inode_t *ip, /* incore inode */ |
| 1525 | xfs_fileoff_t *last_block, /* last block */ |
| 1526 | int whichfork) /* data or attr fork */ |
| 1527 | { |
| 1528 | xfs_fileoff_t bno; /* input file offset */ |
| 1529 | int eof; /* hit end of file */ |
| 1530 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
| 1531 | int error; /* error return value */ |
| 1532 | xfs_bmbt_irec_t got; /* current extent value */ |
| 1533 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1534 | xfs_extnum_t lastx; /* last extent used */ |
| 1535 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 1536 | |
| 1537 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1538 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 1539 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1540 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1541 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1542 | *last_block = 0; |
| 1543 | return 0; |
| 1544 | } |
| 1545 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1546 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1547 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1548 | return error; |
| 1549 | bno = *last_block - 1; |
| 1550 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 1551 | &prev); |
| 1552 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 1553 | if (prev.br_startoff == NULLFILEOFF) |
| 1554 | *last_block = 0; |
| 1555 | else |
| 1556 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 1557 | } |
| 1558 | /* |
| 1559 | * Otherwise *last_block is already the right answer. |
| 1560 | */ |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1564 | int |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1565 | xfs_bmap_last_extent( |
| 1566 | struct xfs_trans *tp, |
| 1567 | struct xfs_inode *ip, |
| 1568 | int whichfork, |
| 1569 | struct xfs_bmbt_irec *rec, |
| 1570 | int *is_empty) |
| 1571 | { |
| 1572 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1573 | int error; |
| 1574 | int nextents; |
| 1575 | |
| 1576 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1577 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1578 | if (error) |
| 1579 | return error; |
| 1580 | } |
| 1581 | |
| 1582 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 1583 | if (nextents == 0) { |
| 1584 | *is_empty = 1; |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
| 1588 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1589 | *is_empty = 0; |
| 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * Check the last inode extent to determine whether this allocation will result |
| 1595 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1596 | * blocks at the end of the file which do not start at the previous data block, |
| 1597 | * we will try to align the new blocks at stripe unit boundaries. |
| 1598 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1599 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1600 | * at, or past the EOF. |
| 1601 | */ |
| 1602 | STATIC int |
| 1603 | xfs_bmap_isaeof( |
| 1604 | struct xfs_bmalloca *bma, |
| 1605 | int whichfork) |
| 1606 | { |
| 1607 | struct xfs_bmbt_irec rec; |
| 1608 | int is_empty; |
| 1609 | int error; |
| 1610 | |
| 1611 | bma->aeof = 0; |
| 1612 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1613 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1614 | if (error) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1615 | return error; |
| 1616 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1617 | if (is_empty) { |
| 1618 | bma->aeof = 1; |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1622 | /* |
| 1623 | * Check if we are allocation or past the last extent, or at least into |
| 1624 | * the last delayed allocated extent. |
| 1625 | */ |
| 1626 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1627 | (bma->offset >= rec.br_startoff && |
| 1628 | isnullstartblock(rec.br_startblock)); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
| 1632 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1633 | * Returns the file-relative block number of the first block past eof in |
| 1634 | * the file. This is not based on i_size, it is based on the extent records. |
| 1635 | * Returns 0 for local files, as they do not have extent records. |
| 1636 | */ |
| 1637 | int |
| 1638 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1639 | struct xfs_inode *ip, |
| 1640 | xfs_fileoff_t *last_block, |
| 1641 | int whichfork) |
| 1642 | { |
| 1643 | struct xfs_bmbt_irec rec; |
| 1644 | int is_empty; |
| 1645 | int error; |
| 1646 | |
| 1647 | *last_block = 0; |
| 1648 | |
| 1649 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1650 | return 0; |
| 1651 | |
| 1652 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1653 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1654 | return -EIO; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1655 | |
| 1656 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1657 | if (error || is_empty) |
| 1658 | return error; |
| 1659 | |
| 1660 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * Returns whether the selected fork of the inode has exactly one |
| 1666 | * block or not. For the data fork we check this matches di_size, |
| 1667 | * implying the file's range is 0..bsize-1. |
| 1668 | */ |
| 1669 | int /* 1=>1 block, 0=>otherwise */ |
| 1670 | xfs_bmap_one_block( |
| 1671 | xfs_inode_t *ip, /* incore inode */ |
| 1672 | int whichfork) /* data or attr fork */ |
| 1673 | { |
| 1674 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1675 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1676 | int rval; /* return value */ |
| 1677 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1678 | |
| 1679 | #ifndef DEBUG |
| 1680 | if (whichfork == XFS_DATA_FORK) |
| 1681 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1682 | #endif /* !DEBUG */ |
| 1683 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1684 | return 0; |
| 1685 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1686 | return 0; |
| 1687 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1688 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1689 | ep = xfs_iext_get_ext(ifp, 0); |
| 1690 | xfs_bmbt_get_all(ep, &s); |
| 1691 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1692 | if (rval && whichfork == XFS_DATA_FORK) |
| 1693 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1694 | return rval; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Extent tree manipulation functions used during allocation. |
| 1699 | */ |
| 1700 | |
| 1701 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1702 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | */ |
| 1704 | STATIC int /* error */ |
| 1705 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1706 | struct xfs_bmalloca *bma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1708 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1710 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1713 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1715 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1716 | /* left is 0, right is 1, prev is 2 */ |
| 1717 | int rval=0; /* return value (logging flags) */ |
| 1718 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1719 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1720 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1721 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1722 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | int tmp_rval; /* partial logging flags */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1724 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1726 | mp = bma->tp ? bma->tp->t_mountp : NULL; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1727 | ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1728 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1729 | ASSERT(bma->idx >= 0); |
| 1730 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1731 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1732 | ASSERT(!bma->cur || |
| 1733 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1734 | |
| 1735 | XFS_STATS_INC(xs_add_exlist); |
| 1736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | #define LEFT r[0] |
| 1738 | #define RIGHT r[1] |
| 1739 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
| 1741 | /* |
| 1742 | * Set up a bunch of variables to make the tests simpler. |
| 1743 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1744 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | xfs_bmbt_get_all(ep, &PREV); |
| 1746 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1747 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1748 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1749 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1750 | da_old = startblockval(PREV.br_startblock); |
| 1751 | da_new = 0; |
| 1752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | /* |
| 1754 | * Set flags determining what part of the previous delayed allocation |
| 1755 | * extent is being replaced by a real allocation. |
| 1756 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1757 | if (PREV.br_startoff == new->br_startoff) |
| 1758 | state |= BMAP_LEFT_FILLING; |
| 1759 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1760 | state |= BMAP_RIGHT_FILLING; |
| 1761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | /* |
| 1763 | * Check and set flags if this segment has a left neighbor. |
| 1764 | * Don't set contiguous if the combined extent would be too large. |
| 1765 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1766 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1767 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1768 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1769 | |
| 1770 | if (isnullstartblock(LEFT.br_startblock)) |
| 1771 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1773 | |
| 1774 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1775 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1776 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1777 | LEFT.br_state == new->br_state && |
| 1778 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1779 | state |= BMAP_LEFT_CONTIG; |
| 1780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | /* |
| 1782 | * Check and set flags if this segment has a right neighbor. |
| 1783 | * Don't set contiguous if the combined extent would be too large. |
| 1784 | * Also check for all-three-contiguous being too large. |
| 1785 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1786 | if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1787 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1788 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1791 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1793 | |
| 1794 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1795 | new_endoff == RIGHT.br_startoff && |
| 1796 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1797 | new->br_state == RIGHT.br_state && |
| 1798 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1799 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1800 | BMAP_RIGHT_FILLING)) != |
| 1801 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1802 | BMAP_RIGHT_FILLING) || |
| 1803 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1804 | <= MAXEXTLEN)) |
| 1805 | state |= BMAP_RIGHT_CONTIG; |
| 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | error = 0; |
| 1808 | /* |
| 1809 | * Switch out based on the FILLING and CONTIG state bits. |
| 1810 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1811 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1812 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1813 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1814 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | /* |
| 1816 | * Filling in all of a previously delayed allocation extent. |
| 1817 | * The left and right neighbors are both contiguous with new. |
| 1818 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1819 | bma->idx--; |
| 1820 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1821 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1823 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1824 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1825 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1826 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
| 1827 | bma->ip->i_d.di_nextents--; |
| 1828 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1830 | else { |
| 1831 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1832 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1834 | RIGHT.br_blockcount, &i); |
| 1835 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1837 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1838 | error = xfs_btree_delete(bma->cur, &i); |
| 1839 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1841 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1842 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1843 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1845 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1846 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | LEFT.br_startblock, |
| 1848 | LEFT.br_blockcount + |
| 1849 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1850 | RIGHT.br_blockcount, LEFT.br_state); |
| 1851 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | goto done; |
| 1853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | break; |
| 1855 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1856 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | /* |
| 1858 | * Filling in all of a previously delayed allocation extent. |
| 1859 | * The left neighbor is contiguous, the right is not. |
| 1860 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1861 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1862 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1863 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1864 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1865 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1866 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1867 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1868 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1869 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | rval = XFS_ILOG_DEXT; |
| 1871 | else { |
| 1872 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1873 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1875 | &i); |
| 1876 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1878 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1879 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | LEFT.br_startblock, |
| 1881 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1882 | PREV.br_blockcount, LEFT.br_state); |
| 1883 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | goto done; |
| 1885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | break; |
| 1887 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1888 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | /* |
| 1890 | * Filling in all of a previously delayed allocation extent. |
| 1891 | * The right neighbor is contiguous, the left is not. |
| 1892 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 1895 | xfs_bmbt_set_blockcount(ep, |
| 1896 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1897 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1898 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1899 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1900 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | rval = XFS_ILOG_DEXT; |
| 1902 | else { |
| 1903 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1904 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1906 | RIGHT.br_blockcount, &i); |
| 1907 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1909 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1910 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | new->br_startblock, |
| 1912 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1913 | RIGHT.br_blockcount, PREV.br_state); |
| 1914 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | goto done; |
| 1916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | break; |
| 1918 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1919 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | /* |
| 1921 | * Filling in all of a previously delayed allocation extent. |
| 1922 | * Neither the left nor right neighbors are contiguous with |
| 1923 | * the new one. |
| 1924 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1925 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1927 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1928 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1929 | bma->ip->i_d.di_nextents++; |
| 1930 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1932 | else { |
| 1933 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1934 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1936 | &i); |
| 1937 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1939 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1940 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1941 | error = xfs_btree_insert(bma->cur, &i); |
| 1942 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1944 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | break; |
| 1947 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1948 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | /* |
| 1950 | * Filling in the first part of a previous delayed allocation. |
| 1951 | * The left neighbor is contiguous. |
| 1952 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1953 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1954 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | LEFT.br_blockcount + new->br_blockcount); |
| 1956 | xfs_bmbt_set_startoff(ep, |
| 1957 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1958 | trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1961 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1963 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | rval = XFS_ILOG_DEXT; |
| 1965 | else { |
| 1966 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1967 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1969 | &i); |
| 1970 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 1972 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1973 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | LEFT.br_startblock, |
| 1975 | LEFT.br_blockcount + |
| 1976 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1977 | LEFT.br_state); |
| 1978 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | goto done; |
| 1980 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1981 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1982 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1983 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1984 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1985 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1986 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | break; |
| 1988 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1989 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | /* |
| 1991 | * Filling in the first part of a previous delayed allocation. |
| 1992 | * The left neighbor is not contiguous. |
| 1993 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1994 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1996 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1997 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1998 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 1999 | bma->ip->i_d.di_nextents++; |
| 2000 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2002 | else { |
| 2003 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2004 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2006 | &i); |
| 2007 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2009 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2010 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2011 | error = xfs_btree_insert(bma->cur, &i); |
| 2012 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2014 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2016 | |
| 2017 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2018 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2019 | bma->firstblock, bma->flist, |
| 2020 | &bma->cur, 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | rval |= tmp_rval; |
| 2022 | if (error) |
| 2023 | goto done; |
| 2024 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2025 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2026 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2027 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2028 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2029 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2030 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | break; |
| 2032 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2033 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | /* |
| 2035 | * Filling in the last part of a previous delayed allocation. |
| 2036 | * The right neighbor is contiguous with the new allocation. |
| 2037 | */ |
| 2038 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2039 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2041 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2042 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | new->br_blockcount + RIGHT.br_blockcount, |
| 2044 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2045 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 2046 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | rval = XFS_ILOG_DEXT; |
| 2048 | else { |
| 2049 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2050 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2052 | RIGHT.br_blockcount, &i); |
| 2053 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2055 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2056 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | new->br_startblock, |
| 2058 | new->br_blockcount + |
| 2059 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2060 | RIGHT.br_state); |
| 2061 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | goto done; |
| 2063 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2064 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2065 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2066 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2067 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2068 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2069 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2070 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2071 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | break; |
| 2073 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2074 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Filling in the last part of a previous delayed allocation. |
| 2077 | * The right neighbor is not contiguous. |
| 2078 | */ |
| 2079 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2080 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2082 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
| 2083 | bma->ip->i_d.di_nextents++; |
| 2084 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2086 | else { |
| 2087 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2088 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2090 | &i); |
| 2091 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2093 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2094 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2095 | error = xfs_btree_insert(bma->cur, &i); |
| 2096 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2098 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2100 | |
| 2101 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2102 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2103 | bma->firstblock, bma->flist, &bma->cur, 1, |
| 2104 | &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | rval |= tmp_rval; |
| 2106 | if (error) |
| 2107 | goto done; |
| 2108 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2109 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2110 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2111 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2112 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2113 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2114 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2115 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2116 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | break; |
| 2118 | |
| 2119 | case 0: |
| 2120 | /* |
| 2121 | * Filling in the middle part of a previous delayed allocation. |
| 2122 | * Contiguity is impossible here. |
| 2123 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2124 | * |
| 2125 | * We start with a delayed allocation: |
| 2126 | * |
| 2127 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2128 | * PREV @ idx |
| 2129 | * |
| 2130 | * and we are allocating: |
| 2131 | * +rrrrrrrrrrrrrrrrr+ |
| 2132 | * new |
| 2133 | * |
| 2134 | * and we set it up for insertion as: |
| 2135 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2136 | * new |
| 2137 | * PREV @ idx LEFT RIGHT |
| 2138 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | */ |
| 2140 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2142 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2143 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2144 | LEFT = *new; |
| 2145 | RIGHT.br_state = PREV.br_state; |
| 2146 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2147 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2148 | RIGHT.br_startoff = new_endoff; |
| 2149 | RIGHT.br_blockcount = temp2; |
| 2150 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2151 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
| 2152 | bma->ip->i_d.di_nextents++; |
| 2153 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2155 | else { |
| 2156 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2157 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2159 | &i); |
| 2160 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2162 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2163 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2164 | error = xfs_btree_insert(bma->cur, &i); |
| 2165 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2167 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2169 | |
| 2170 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2171 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2172 | bma->firstblock, bma->flist, &bma->cur, |
| 2173 | 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | rval |= tmp_rval; |
| 2175 | if (error) |
| 2176 | goto done; |
| 2177 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2178 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2179 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2180 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2181 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2182 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2183 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2184 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2185 | ASSERT(!error); |
| 2186 | if (error) |
| 2187 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2189 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2190 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2191 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2192 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2193 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2194 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2195 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2196 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2197 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2198 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2199 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | break; |
| 2201 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2202 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2203 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2204 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2205 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2206 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2207 | case BMAP_LEFT_CONTIG: |
| 2208 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | /* |
| 2210 | * These cases are all impossible. |
| 2211 | */ |
| 2212 | ASSERT(0); |
| 2213 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2214 | |
| 2215 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2216 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2217 | int tmp_logflags; /* partial log flag return val */ |
| 2218 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2219 | ASSERT(bma->cur == NULL); |
| 2220 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2221 | bma->firstblock, bma->flist, &bma->cur, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2222 | da_old > 0, &tmp_logflags, XFS_DATA_FORK); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2223 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2224 | if (error) |
| 2225 | goto done; |
| 2226 | } |
| 2227 | |
| 2228 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2229 | if (da_old || da_new) { |
| 2230 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2231 | if (bma->cur) |
| 2232 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2233 | ASSERT(temp <= da_old); |
| 2234 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2235 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2236 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2240 | if (bma->cur) |
| 2241 | bma->cur->bc_private.b.allocated = 0; |
| 2242 | |
| 2243 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | done: |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2245 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | return error; |
| 2247 | #undef LEFT |
| 2248 | #undef RIGHT |
| 2249 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2253 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | */ |
| 2255 | STATIC int /* error */ |
| 2256 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2257 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2259 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2261 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2262 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 2263 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2264 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2267 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2270 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2272 | xfs_exntst_t newext; /* new extent state */ |
| 2273 | xfs_exntst_t oldext; /* old extent state */ |
| 2274 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2275 | /* left is 0, right is 1, prev is 2 */ |
| 2276 | int rval=0; /* return value (logging flags) */ |
| 2277 | int state = 0;/* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2278 | struct xfs_mount *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2280 | *logflagsp = 0; |
| 2281 | |
| 2282 | cur = *curp; |
| 2283 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 2284 | |
| 2285 | ASSERT(*idx >= 0); |
| 2286 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 2287 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2288 | |
| 2289 | XFS_STATS_INC(xs_add_exlist); |
| 2290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | #define LEFT r[0] |
| 2292 | #define RIGHT r[1] |
| 2293 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | /* |
| 2296 | * Set up a bunch of variables to make the tests simpler. |
| 2297 | */ |
| 2298 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2299 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | xfs_bmbt_get_all(ep, &PREV); |
| 2301 | newext = new->br_state; |
| 2302 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2303 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2304 | ASSERT(PREV.br_state == oldext); |
| 2305 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2306 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2307 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | /* |
| 2310 | * Set flags determining what part of the previous oldext allocation |
| 2311 | * extent is being replaced by a newext allocation. |
| 2312 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2313 | if (PREV.br_startoff == new->br_startoff) |
| 2314 | state |= BMAP_LEFT_FILLING; |
| 2315 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2316 | state |= BMAP_RIGHT_FILLING; |
| 2317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | /* |
| 2319 | * Check and set flags if this segment has a left neighbor. |
| 2320 | * Don't set contiguous if the combined extent would be too large. |
| 2321 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2322 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2323 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2324 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2325 | |
| 2326 | if (isnullstartblock(LEFT.br_startblock)) |
| 2327 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2329 | |
| 2330 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2331 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2332 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2333 | LEFT.br_state == newext && |
| 2334 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2335 | state |= BMAP_LEFT_CONTIG; |
| 2336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | /* |
| 2338 | * Check and set flags if this segment has a right neighbor. |
| 2339 | * Don't set contiguous if the combined extent would be too large. |
| 2340 | * Also check for all-three-contiguous being too large. |
| 2341 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2342 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2343 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2344 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2345 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2346 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2348 | |
| 2349 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2350 | new_endoff == RIGHT.br_startoff && |
| 2351 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2352 | newext == RIGHT.br_state && |
| 2353 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2354 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2355 | BMAP_RIGHT_FILLING)) != |
| 2356 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2357 | BMAP_RIGHT_FILLING) || |
| 2358 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2359 | <= MAXEXTLEN)) |
| 2360 | state |= BMAP_RIGHT_CONTIG; |
| 2361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | /* |
| 2363 | * Switch out based on the FILLING and CONTIG state bits. |
| 2364 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2365 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2366 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2367 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2368 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | /* |
| 2370 | * Setting all of a previous oldext extent to newext. |
| 2371 | * The left and right neighbors are both contiguous with new. |
| 2372 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2373 | --*idx; |
| 2374 | |
| 2375 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2376 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2378 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2379 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2380 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2381 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | ip->i_d.di_nextents -= 2; |
| 2383 | if (cur == NULL) |
| 2384 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2385 | else { |
| 2386 | rval = XFS_ILOG_CORE; |
| 2387 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2388 | RIGHT.br_startblock, |
| 2389 | RIGHT.br_blockcount, &i))) |
| 2390 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2391 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2392 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2394 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2395 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2397 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2398 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2400 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2401 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2403 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2405 | LEFT.br_startblock, |
| 2406 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2407 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2408 | goto done; |
| 2409 | } |
| 2410 | break; |
| 2411 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2412 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | /* |
| 2414 | * Setting all of a previous oldext extent to newext. |
| 2415 | * The left neighbor is contiguous, the right is not. |
| 2416 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2417 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2418 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2419 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2420 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2421 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2422 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2423 | |
| 2424 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | ip->i_d.di_nextents--; |
| 2426 | if (cur == NULL) |
| 2427 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2428 | else { |
| 2429 | rval = XFS_ILOG_CORE; |
| 2430 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2431 | PREV.br_startblock, PREV.br_blockcount, |
| 2432 | &i))) |
| 2433 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2434 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2435 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2437 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2438 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2440 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2442 | LEFT.br_startblock, |
| 2443 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2444 | LEFT.br_state))) |
| 2445 | goto done; |
| 2446 | } |
| 2447 | break; |
| 2448 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2449 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | /* |
| 2451 | * Setting all of a previous oldext extent to newext. |
| 2452 | * The right neighbor is contiguous, the left is not. |
| 2453 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2454 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | xfs_bmbt_set_blockcount(ep, |
| 2456 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2457 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2458 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2459 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | ip->i_d.di_nextents--; |
| 2461 | if (cur == NULL) |
| 2462 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2463 | else { |
| 2464 | rval = XFS_ILOG_CORE; |
| 2465 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2466 | RIGHT.br_startblock, |
| 2467 | RIGHT.br_blockcount, &i))) |
| 2468 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2469 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2470 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2472 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2473 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2475 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2477 | new->br_startblock, |
| 2478 | new->br_blockcount + RIGHT.br_blockcount, |
| 2479 | newext))) |
| 2480 | goto done; |
| 2481 | } |
| 2482 | break; |
| 2483 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2484 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | /* |
| 2486 | * Setting all of a previous oldext extent to newext. |
| 2487 | * Neither the left nor right neighbors are contiguous with |
| 2488 | * the new one. |
| 2489 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2490 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2492 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | if (cur == NULL) |
| 2495 | rval = XFS_ILOG_DEXT; |
| 2496 | else { |
| 2497 | rval = 0; |
| 2498 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2499 | new->br_startblock, new->br_blockcount, |
| 2500 | &i))) |
| 2501 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2502 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2504 | new->br_startblock, new->br_blockcount, |
| 2505 | newext))) |
| 2506 | goto done; |
| 2507 | } |
| 2508 | break; |
| 2509 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2510 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | /* |
| 2512 | * Setting the first part of a previous oldext extent to newext. |
| 2513 | * The left neighbor is contiguous. |
| 2514 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2515 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2516 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | LEFT.br_blockcount + new->br_blockcount); |
| 2518 | xfs_bmbt_set_startoff(ep, |
| 2519 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2520 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2521 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2522 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | xfs_bmbt_set_startblock(ep, |
| 2524 | new->br_startblock + new->br_blockcount); |
| 2525 | xfs_bmbt_set_blockcount(ep, |
| 2526 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2527 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2528 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2529 | --*idx; |
| 2530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | if (cur == NULL) |
| 2532 | rval = XFS_ILOG_DEXT; |
| 2533 | else { |
| 2534 | rval = 0; |
| 2535 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2536 | PREV.br_startblock, PREV.br_blockcount, |
| 2537 | &i))) |
| 2538 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2539 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | if ((error = xfs_bmbt_update(cur, |
| 2541 | PREV.br_startoff + new->br_blockcount, |
| 2542 | PREV.br_startblock + new->br_blockcount, |
| 2543 | PREV.br_blockcount - new->br_blockcount, |
| 2544 | oldext))) |
| 2545 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2546 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2548 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | LEFT.br_startblock, |
| 2550 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2551 | LEFT.br_state); |
| 2552 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | goto done; |
| 2554 | } |
| 2555 | break; |
| 2556 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2557 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | /* |
| 2559 | * Setting the first part of a previous oldext extent to newext. |
| 2560 | * The left neighbor is not contiguous. |
| 2561 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2562 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2564 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2565 | xfs_bmbt_set_blockcount(ep, |
| 2566 | PREV.br_blockcount - new->br_blockcount); |
| 2567 | xfs_bmbt_set_startblock(ep, |
| 2568 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2569 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2570 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2571 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | ip->i_d.di_nextents++; |
| 2573 | if (cur == NULL) |
| 2574 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2575 | else { |
| 2576 | rval = XFS_ILOG_CORE; |
| 2577 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2578 | PREV.br_startblock, PREV.br_blockcount, |
| 2579 | &i))) |
| 2580 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2581 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | if ((error = xfs_bmbt_update(cur, |
| 2583 | PREV.br_startoff + new->br_blockcount, |
| 2584 | PREV.br_startblock + new->br_blockcount, |
| 2585 | PREV.br_blockcount - new->br_blockcount, |
| 2586 | oldext))) |
| 2587 | goto done; |
| 2588 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2589 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2591 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | } |
| 2593 | break; |
| 2594 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2595 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | /* |
| 2597 | * Setting the last part of a previous oldext extent to newext. |
| 2598 | * The right neighbor is contiguous with the new allocation. |
| 2599 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2600 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | xfs_bmbt_set_blockcount(ep, |
| 2602 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2603 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2604 | |
| 2605 | ++*idx; |
| 2606 | |
| 2607 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2608 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2609 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2611 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | if (cur == NULL) |
| 2614 | rval = XFS_ILOG_DEXT; |
| 2615 | else { |
| 2616 | rval = 0; |
| 2617 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2618 | PREV.br_startblock, |
| 2619 | PREV.br_blockcount, &i))) |
| 2620 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2621 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2623 | PREV.br_startblock, |
| 2624 | PREV.br_blockcount - new->br_blockcount, |
| 2625 | oldext))) |
| 2626 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2627 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | goto done; |
| 2629 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2630 | new->br_startblock, |
| 2631 | new->br_blockcount + RIGHT.br_blockcount, |
| 2632 | newext))) |
| 2633 | goto done; |
| 2634 | } |
| 2635 | break; |
| 2636 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2637 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | /* |
| 2639 | * Setting the last part of a previous oldext extent to newext. |
| 2640 | * The right neighbor is not contiguous. |
| 2641 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2642 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | xfs_bmbt_set_blockcount(ep, |
| 2644 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2645 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2646 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2647 | ++*idx; |
| 2648 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | ip->i_d.di_nextents++; |
| 2651 | if (cur == NULL) |
| 2652 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2653 | else { |
| 2654 | rval = XFS_ILOG_CORE; |
| 2655 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2656 | PREV.br_startblock, PREV.br_blockcount, |
| 2657 | &i))) |
| 2658 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2659 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2661 | PREV.br_startblock, |
| 2662 | PREV.br_blockcount - new->br_blockcount, |
| 2663 | oldext))) |
| 2664 | goto done; |
| 2665 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2666 | new->br_startblock, new->br_blockcount, |
| 2667 | &i))) |
| 2668 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2669 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2671 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2673 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | } |
| 2675 | break; |
| 2676 | |
| 2677 | case 0: |
| 2678 | /* |
| 2679 | * Setting the middle part of a previous oldext extent to |
| 2680 | * newext. Contiguity is impossible here. |
| 2681 | * One extent becomes three extents. |
| 2682 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2683 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | xfs_bmbt_set_blockcount(ep, |
| 2685 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2686 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | r[0] = *new; |
| 2689 | r[1].br_startoff = new_endoff; |
| 2690 | r[1].br_blockcount = |
| 2691 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2692 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2693 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2694 | |
| 2695 | ++*idx; |
| 2696 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | ip->i_d.di_nextents += 2; |
| 2699 | if (cur == NULL) |
| 2700 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2701 | else { |
| 2702 | rval = XFS_ILOG_CORE; |
| 2703 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2704 | PREV.br_startblock, PREV.br_blockcount, |
| 2705 | &i))) |
| 2706 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2707 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | /* new right extent - oldext */ |
| 2709 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2710 | r[1].br_startblock, r[1].br_blockcount, |
| 2711 | r[1].br_state))) |
| 2712 | goto done; |
| 2713 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2715 | cur->bc_rec.b.br_blockcount = |
| 2716 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2717 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2719 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2720 | /* |
| 2721 | * Reset the cursor to the position of the new extent |
| 2722 | * we are about to insert as we can't trust it after |
| 2723 | * the previous insert. |
| 2724 | */ |
| 2725 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2726 | new->br_startblock, new->br_blockcount, |
| 2727 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2729 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2731 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2732 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2734 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | } |
| 2736 | break; |
| 2737 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2738 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2739 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2740 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2741 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2742 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2743 | case BMAP_LEFT_CONTIG: |
| 2744 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | /* |
| 2746 | * These cases are all impossible. |
| 2747 | */ |
| 2748 | ASSERT(0); |
| 2749 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2750 | |
| 2751 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2752 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2753 | int tmp_logflags; /* partial log flag return val */ |
| 2754 | |
| 2755 | ASSERT(cur == NULL); |
| 2756 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, |
| 2757 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 2758 | *logflagsp |= tmp_logflags; |
| 2759 | if (error) |
| 2760 | goto done; |
| 2761 | } |
| 2762 | |
| 2763 | /* clear out the allocated field, done with it now in any case. */ |
| 2764 | if (cur) { |
| 2765 | cur->bc_private.b.allocated = 0; |
| 2766 | *curp = cur; |
| 2767 | } |
| 2768 | |
| 2769 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2771 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | return error; |
| 2773 | #undef LEFT |
| 2774 | #undef RIGHT |
| 2775 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2779 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2781 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | xfs_bmap_add_extent_hole_delay( |
| 2783 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2784 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2785 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2787 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2789 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2790 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2791 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2792 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2793 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2795 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2797 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | /* |
| 2800 | * Check and set flags if this segment has a left neighbor |
| 2801 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2802 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2803 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2804 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2805 | |
| 2806 | if (isnullstartblock(left.br_startblock)) |
| 2807 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | /* |
| 2811 | * Check and set flags if the current (right) segment exists. |
| 2812 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2813 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2814 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2815 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2816 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2817 | |
| 2818 | if (isnullstartblock(right.br_startblock)) |
| 2819 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | /* |
| 2823 | * Set contiguity flags on the left and right neighbors. |
| 2824 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2825 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2826 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2827 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2828 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2829 | state |= BMAP_LEFT_CONTIG; |
| 2830 | |
| 2831 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2832 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2833 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2834 | (!(state & BMAP_LEFT_CONTIG) || |
| 2835 | (left.br_blockcount + new->br_blockcount + |
| 2836 | right.br_blockcount <= MAXEXTLEN))) |
| 2837 | state |= BMAP_RIGHT_CONTIG; |
| 2838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | /* |
| 2840 | * Switch out based on the contiguity flags. |
| 2841 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2842 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2843 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | /* |
| 2845 | * New allocation is contiguous with delayed allocations |
| 2846 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2847 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2849 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | temp = left.br_blockcount + new->br_blockcount + |
| 2851 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2852 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2853 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2854 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2855 | oldlen = startblockval(left.br_startblock) + |
| 2856 | startblockval(new->br_startblock) + |
| 2857 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2859 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2860 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2861 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2862 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2863 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | break; |
| 2865 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2866 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | /* |
| 2868 | * New allocation is contiguous with a delayed allocation |
| 2869 | * on the left. |
| 2870 | * Merge the new allocation with the left neighbor. |
| 2871 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2872 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2874 | |
| 2875 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2876 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2877 | oldlen = startblockval(left.br_startblock) + |
| 2878 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2880 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2881 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2882 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | break; |
| 2884 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2885 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | /* |
| 2887 | * New allocation is contiguous with a delayed allocation |
| 2888 | * on the right. |
| 2889 | * Merge the new allocation with the right neighbor. |
| 2890 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2891 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2893 | oldlen = startblockval(new->br_startblock) + |
| 2894 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2896 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2897 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2898 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2899 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | break; |
| 2901 | |
| 2902 | case 0: |
| 2903 | /* |
| 2904 | * New allocation is not contiguous with another |
| 2905 | * delayed allocation. |
| 2906 | * Insert a new entry. |
| 2907 | */ |
| 2908 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2909 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | break; |
| 2911 | } |
| 2912 | if (oldlen != newlen) { |
| 2913 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 2914 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2915 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | /* |
| 2917 | * Nothing to do for disk quota accounting here. |
| 2918 | */ |
| 2919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2923 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | */ |
| 2925 | STATIC int /* error */ |
| 2926 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2927 | struct xfs_bmalloca *bma, |
| 2928 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2930 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | int i; /* temp state */ |
| 2933 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2934 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2935 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2936 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | int state; /* state bits, accessed thru macros */ |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2938 | struct xfs_mount *mp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 2940 | mp = bma->tp ? bma->tp->t_mountp : NULL; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2941 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2942 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2943 | ASSERT(bma->idx >= 0); |
| 2944 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2945 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2946 | ASSERT(!bma->cur || |
| 2947 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2948 | |
| 2949 | XFS_STATS_INC(xs_add_exlist); |
| 2950 | |
| 2951 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2952 | if (whichfork == XFS_ATTR_FORK) |
| 2953 | state |= BMAP_ATTRFORK; |
| 2954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | /* |
| 2956 | * Check and set flags if this segment has a left neighbor. |
| 2957 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2958 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2959 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2960 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2961 | if (isnullstartblock(left.br_startblock)) |
| 2962 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | /* |
| 2966 | * Check and set flags if this segment has a current value. |
| 2967 | * Not true if we're inserting into the "hole" at eof. |
| 2968 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2969 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2970 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2971 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2972 | if (isnullstartblock(right.br_startblock)) |
| 2973 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | /* |
| 2977 | * We're inserting a real allocation between "left" and "right". |
| 2978 | * Set the contiguity flags. Don't let extents get too large. |
| 2979 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2980 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2981 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2982 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2983 | left.br_state == new->br_state && |
| 2984 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2985 | state |= BMAP_LEFT_CONTIG; |
| 2986 | |
| 2987 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2988 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2989 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 2990 | new->br_state == right.br_state && |
| 2991 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2992 | (!(state & BMAP_LEFT_CONTIG) || |
| 2993 | left.br_blockcount + new->br_blockcount + |
| 2994 | right.br_blockcount <= MAXEXTLEN)) |
| 2995 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2997 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | /* |
| 2999 | * Select which case we're in here, and implement it. |
| 3000 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3001 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 3002 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | /* |
| 3004 | * New allocation is contiguous with real allocations on the |
| 3005 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3006 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3008 | --bma->idx; |
| 3009 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3010 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | left.br_blockcount + new->br_blockcount + |
| 3012 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3013 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3014 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3015 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 3016 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3017 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3018 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 3019 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3020 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3021 | } else { |
| 3022 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3023 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 3024 | right.br_startblock, right.br_blockcount, |
| 3025 | &i); |
| 3026 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3027 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3028 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3029 | error = xfs_btree_delete(bma->cur, &i); |
| 3030 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3031 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3032 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3033 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 3034 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3035 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3036 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3037 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3038 | left.br_startblock, |
| 3039 | left.br_blockcount + |
| 3040 | new->br_blockcount + |
| 3041 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3042 | left.br_state); |
| 3043 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3044 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3046 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3048 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | /* |
| 3050 | * New allocation is contiguous with a real allocation |
| 3051 | * on the left. |
| 3052 | * Merge the new allocation with the left neighbor. |
| 3053 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3054 | --bma->idx; |
| 3055 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3056 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3058 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3059 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3060 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3061 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3062 | } else { |
| 3063 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3064 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 3065 | left.br_startblock, left.br_blockcount, |
| 3066 | &i); |
| 3067 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3068 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3069 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3070 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3071 | left.br_startblock, |
| 3072 | left.br_blockcount + |
| 3073 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3074 | left.br_state); |
| 3075 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3076 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3078 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3080 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | /* |
| 3082 | * New allocation is contiguous with a real allocation |
| 3083 | * on the right. |
| 3084 | * Merge the new allocation with the right neighbor. |
| 3085 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3086 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3087 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3088 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | new->br_blockcount + right.br_blockcount, |
| 3090 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3091 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3092 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3093 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3094 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3095 | } else { |
| 3096 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3097 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3098 | right.br_startoff, |
| 3099 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3100 | right.br_blockcount, &i); |
| 3101 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3102 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3103 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3104 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3105 | new->br_startblock, |
| 3106 | new->br_blockcount + |
| 3107 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3108 | right.br_state); |
| 3109 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3110 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3112 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | |
| 3114 | case 0: |
| 3115 | /* |
| 3116 | * New allocation is not contiguous with another |
| 3117 | * real allocation. |
| 3118 | * Insert a new entry. |
| 3119 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3120 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 3121 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3122 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 3123 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3124 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3125 | } else { |
| 3126 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3127 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3128 | new->br_startoff, |
| 3129 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3130 | new->br_blockcount, &i); |
| 3131 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3132 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3133 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3134 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 3135 | error = xfs_btree_insert(bma->cur, &i); |
| 3136 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3137 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3138 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3140 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3142 | |
| 3143 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3144 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3145 | int tmp_logflags; /* partial log flag return val */ |
| 3146 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3147 | ASSERT(bma->cur == NULL); |
| 3148 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 3149 | bma->firstblock, bma->flist, &bma->cur, |
| 3150 | 0, &tmp_logflags, whichfork); |
| 3151 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3152 | if (error) |
| 3153 | goto done; |
| 3154 | } |
| 3155 | |
| 3156 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3157 | if (bma->cur) |
| 3158 | bma->cur->bc_private.b.allocated = 0; |
| 3159 | |
| 3160 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3161 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3162 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3163 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3166 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3167 | * Functions used in the extent read, allocate and remove paths |
| 3168 | */ |
| 3169 | |
| 3170 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3171 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3172 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3173 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3174 | xfs_bmap_extsize_align( |
| 3175 | xfs_mount_t *mp, |
| 3176 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3177 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3178 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3179 | int rt, /* is this a realtime inode? */ |
| 3180 | int eof, /* is extent at end-of-file? */ |
| 3181 | int delay, /* creating delalloc extent? */ |
| 3182 | int convert, /* overwriting unwritten extent? */ |
| 3183 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3184 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3185 | { |
| 3186 | xfs_fileoff_t orig_off; /* original offset */ |
| 3187 | xfs_extlen_t orig_alen; /* original length */ |
| 3188 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3189 | xfs_fileoff_t nexto; /* next file offset */ |
| 3190 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3191 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3192 | xfs_extlen_t align_alen; /* temp for length */ |
| 3193 | xfs_extlen_t temp; /* temp for calculations */ |
| 3194 | |
| 3195 | if (convert) |
| 3196 | return 0; |
| 3197 | |
| 3198 | orig_off = align_off = *offp; |
| 3199 | orig_alen = align_alen = *lenp; |
| 3200 | orig_end = orig_off + orig_alen; |
| 3201 | |
| 3202 | /* |
| 3203 | * If this request overlaps an existing extent, then don't |
| 3204 | * attempt to perform any additional alignment. |
| 3205 | */ |
| 3206 | if (!delay && !eof && |
| 3207 | (orig_off >= gotp->br_startoff) && |
| 3208 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3209 | return 0; |
| 3210 | } |
| 3211 | |
| 3212 | /* |
| 3213 | * If the file offset is unaligned vs. the extent size |
| 3214 | * we need to align it. This will be possible unless |
| 3215 | * the file was previously written with a kernel that didn't |
| 3216 | * perform this alignment, or if a truncate shot us in the |
| 3217 | * foot. |
| 3218 | */ |
| 3219 | temp = do_mod(orig_off, extsz); |
| 3220 | if (temp) { |
| 3221 | align_alen += temp; |
| 3222 | align_off -= temp; |
| 3223 | } |
| 3224 | /* |
| 3225 | * Same adjustment for the end of the requested area. |
| 3226 | */ |
| 3227 | if ((temp = (align_alen % extsz))) { |
| 3228 | align_alen += extsz - temp; |
| 3229 | } |
| 3230 | /* |
| 3231 | * If the previous block overlaps with this proposed allocation |
| 3232 | * then move the start forward without adjusting the length. |
| 3233 | */ |
| 3234 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3235 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3236 | prevo = prevp->br_startoff; |
| 3237 | else |
| 3238 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3239 | } else |
| 3240 | prevo = 0; |
| 3241 | if (align_off != orig_off && align_off < prevo) |
| 3242 | align_off = prevo; |
| 3243 | /* |
| 3244 | * If the next block overlaps with this proposed allocation |
| 3245 | * then move the start back without adjusting the length, |
| 3246 | * but not before offset 0. |
| 3247 | * This may of course make the start overlap previous block, |
| 3248 | * and if we hit the offset 0 limit then the next block |
| 3249 | * can still overlap too. |
| 3250 | */ |
| 3251 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3252 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3253 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3254 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3255 | else |
| 3256 | nexto = gotp->br_startoff; |
| 3257 | } else |
| 3258 | nexto = NULLFILEOFF; |
| 3259 | if (!eof && |
| 3260 | align_off + align_alen != orig_end && |
| 3261 | align_off + align_alen > nexto) |
| 3262 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3263 | /* |
| 3264 | * If we're now overlapping the next or previous extent that |
| 3265 | * means we can't fit an extsz piece in this hole. Just move |
| 3266 | * the start forward to the first valid spot and set |
| 3267 | * the length so we hit the end. |
| 3268 | */ |
| 3269 | if (align_off != orig_off && align_off < prevo) |
| 3270 | align_off = prevo; |
| 3271 | if (align_off + align_alen != orig_end && |
| 3272 | align_off + align_alen > nexto && |
| 3273 | nexto != NULLFILEOFF) { |
| 3274 | ASSERT(nexto > prevo); |
| 3275 | align_alen = nexto - align_off; |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * If realtime, and the result isn't a multiple of the realtime |
| 3280 | * extent size we need to remove blocks until it is. |
| 3281 | */ |
| 3282 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3283 | /* |
| 3284 | * We're not covering the original request, or |
| 3285 | * we won't be able to once we fix the length. |
| 3286 | */ |
| 3287 | if (orig_off < align_off || |
| 3288 | orig_end > align_off + align_alen || |
| 3289 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3290 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3291 | /* |
| 3292 | * Try to fix it by moving the start up. |
| 3293 | */ |
| 3294 | if (align_off + temp <= orig_off) { |
| 3295 | align_alen -= temp; |
| 3296 | align_off += temp; |
| 3297 | } |
| 3298 | /* |
| 3299 | * Try to fix it by moving the end in. |
| 3300 | */ |
| 3301 | else if (align_off + align_alen - temp >= orig_end) |
| 3302 | align_alen -= temp; |
| 3303 | /* |
| 3304 | * Set the start to the minimum then trim the length. |
| 3305 | */ |
| 3306 | else { |
| 3307 | align_alen -= orig_off - align_off; |
| 3308 | align_off = orig_off; |
| 3309 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3310 | } |
| 3311 | /* |
| 3312 | * Result doesn't cover the request, fail it. |
| 3313 | */ |
| 3314 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3315 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3316 | } else { |
| 3317 | ASSERT(orig_off >= align_off); |
| 3318 | ASSERT(orig_end <= align_off + align_alen); |
| 3319 | } |
| 3320 | |
| 3321 | #ifdef DEBUG |
| 3322 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3323 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3324 | if (prevp->br_startoff != NULLFILEOFF) |
| 3325 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3326 | #endif |
| 3327 | |
| 3328 | *lenp = align_alen; |
| 3329 | *offp = align_off; |
| 3330 | return 0; |
| 3331 | } |
| 3332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3334 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3335 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3336 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3337 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | { |
| 3339 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3341 | xfs_mount_t *mp; /* mount point structure */ |
| 3342 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3343 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | |
| 3345 | #define ISVALID(x,y) \ |
| 3346 | (rt ? \ |
| 3347 | (x) < mp->m_sb.sb_rblocks : \ |
| 3348 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3349 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3350 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3353 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3355 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | /* |
| 3357 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3358 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3360 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3361 | !isnullstartblock(ap->prev.br_startblock) && |
| 3362 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3363 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3364 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | /* |
| 3366 | * Adjust for the gap between prevp and us. |
| 3367 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3368 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3369 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3371 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3372 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | } |
| 3374 | /* |
| 3375 | * If not at eof, then compare the two neighbor blocks. |
| 3376 | * Figure out whether either one gives us a good starting point, |
| 3377 | * and pick the better one. |
| 3378 | */ |
| 3379 | else if (!ap->eof) { |
| 3380 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3381 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3382 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3383 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3384 | |
| 3385 | /* |
| 3386 | * If there's a previous (left) block, select a requested |
| 3387 | * start block based on it. |
| 3388 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3389 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3390 | !isnullstartblock(ap->prev.br_startblock) && |
| 3391 | (prevbno = ap->prev.br_startblock + |
| 3392 | ap->prev.br_blockcount) && |
| 3393 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | /* |
| 3395 | * Calculate gap to end of previous block. |
| 3396 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3397 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3398 | (ap->prev.br_startoff + |
| 3399 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3400 | /* |
| 3401 | * Figure the startblock based on the previous block's |
| 3402 | * end and the gap size. |
| 3403 | * Heuristic! |
| 3404 | * If the gap is large relative to the piece we're |
| 3405 | * allocating, or using it gives us an invalid block |
| 3406 | * number, then just use the end of the previous block. |
| 3407 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3408 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3410 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | prevbno += adjust; |
| 3412 | else |
| 3413 | prevdiff += adjust; |
| 3414 | /* |
| 3415 | * If the firstblock forbids it, can't use it, |
| 3416 | * must use default. |
| 3417 | */ |
| 3418 | if (!rt && !nullfb && |
| 3419 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3420 | prevbno = NULLFSBLOCK; |
| 3421 | } |
| 3422 | /* |
| 3423 | * No previous block or can't follow it, just default. |
| 3424 | */ |
| 3425 | else |
| 3426 | prevbno = NULLFSBLOCK; |
| 3427 | /* |
| 3428 | * If there's a following (right) block, select a requested |
| 3429 | * start block based on it. |
| 3430 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3431 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | /* |
| 3433 | * Calculate gap to start of next block. |
| 3434 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3435 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | /* |
| 3437 | * Figure the startblock based on the next block's |
| 3438 | * start and the gap size. |
| 3439 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3440 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | /* |
| 3442 | * Heuristic! |
| 3443 | * If the gap is large relative to the piece we're |
| 3444 | * allocating, or using it gives us an invalid block |
| 3445 | * number, then just use the start of the next block |
| 3446 | * offset by our length. |
| 3447 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3448 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3450 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3451 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3452 | gotbno -= ap->length; |
| 3453 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | } else |
| 3455 | gotdiff += adjust; |
| 3456 | /* |
| 3457 | * If the firstblock forbids it, can't use it, |
| 3458 | * must use default. |
| 3459 | */ |
| 3460 | if (!rt && !nullfb && |
| 3461 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3462 | gotbno = NULLFSBLOCK; |
| 3463 | } |
| 3464 | /* |
| 3465 | * No next block, just default. |
| 3466 | */ |
| 3467 | else |
| 3468 | gotbno = NULLFSBLOCK; |
| 3469 | /* |
| 3470 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3471 | * 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] | 3472 | */ |
| 3473 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3474 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3476 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3478 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3480 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3481 | } |
| 3482 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3483 | static int |
| 3484 | xfs_bmap_longest_free_extent( |
| 3485 | struct xfs_trans *tp, |
| 3486 | xfs_agnumber_t ag, |
| 3487 | xfs_extlen_t *blen, |
| 3488 | int *notinit) |
| 3489 | { |
| 3490 | struct xfs_mount *mp = tp->t_mountp; |
| 3491 | struct xfs_perag *pag; |
| 3492 | xfs_extlen_t longest; |
| 3493 | int error = 0; |
| 3494 | |
| 3495 | pag = xfs_perag_get(mp, ag); |
| 3496 | if (!pag->pagf_init) { |
| 3497 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3498 | if (error) |
| 3499 | goto out; |
| 3500 | |
| 3501 | if (!pag->pagf_init) { |
| 3502 | *notinit = 1; |
| 3503 | goto out; |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 3508 | if (*blen < longest) |
| 3509 | *blen = longest; |
| 3510 | |
| 3511 | out: |
| 3512 | xfs_perag_put(pag); |
| 3513 | return error; |
| 3514 | } |
| 3515 | |
| 3516 | static void |
| 3517 | xfs_bmap_select_minlen( |
| 3518 | struct xfs_bmalloca *ap, |
| 3519 | struct xfs_alloc_arg *args, |
| 3520 | xfs_extlen_t *blen, |
| 3521 | int notinit) |
| 3522 | { |
| 3523 | if (notinit || *blen < ap->minlen) { |
| 3524 | /* |
| 3525 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3526 | * there is space for this request. |
| 3527 | */ |
| 3528 | args->minlen = ap->minlen; |
| 3529 | } else if (*blen < args->maxlen) { |
| 3530 | /* |
| 3531 | * If the best seen length is less than the request length, |
| 3532 | * use the best as the minimum. |
| 3533 | */ |
| 3534 | args->minlen = *blen; |
| 3535 | } else { |
| 3536 | /* |
| 3537 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3538 | * as the minimum. |
| 3539 | */ |
| 3540 | args->minlen = args->maxlen; |
| 3541 | } |
| 3542 | } |
| 3543 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3544 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3545 | xfs_bmap_btalloc_nullfb( |
| 3546 | struct xfs_bmalloca *ap, |
| 3547 | struct xfs_alloc_arg *args, |
| 3548 | xfs_extlen_t *blen) |
| 3549 | { |
| 3550 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3551 | xfs_agnumber_t ag, startag; |
| 3552 | int notinit = 0; |
| 3553 | int error; |
| 3554 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3555 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3556 | args->total = ap->total; |
| 3557 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3558 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3559 | if (startag == NULLAGNUMBER) |
| 3560 | startag = ag = 0; |
| 3561 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3562 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3563 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3564 | ¬init); |
| 3565 | if (error) |
| 3566 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3567 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3568 | if (++ag == mp->m_sb.sb_agcount) |
| 3569 | ag = 0; |
| 3570 | if (ag == startag) |
| 3571 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3572 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3573 | |
| 3574 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | STATIC int |
| 3579 | xfs_bmap_btalloc_filestreams( |
| 3580 | struct xfs_bmalloca *ap, |
| 3581 | struct xfs_alloc_arg *args, |
| 3582 | xfs_extlen_t *blen) |
| 3583 | { |
| 3584 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3585 | xfs_agnumber_t ag; |
| 3586 | int notinit = 0; |
| 3587 | int error; |
| 3588 | |
| 3589 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3590 | args->total = ap->total; |
| 3591 | |
| 3592 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3593 | if (ag == NULLAGNUMBER) |
| 3594 | ag = 0; |
| 3595 | |
| 3596 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3597 | if (error) |
| 3598 | return error; |
| 3599 | |
| 3600 | if (*blen < args->maxlen) { |
| 3601 | error = xfs_filestream_new_ag(ap, &ag); |
| 3602 | if (error) |
| 3603 | return error; |
| 3604 | |
| 3605 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3606 | ¬init); |
| 3607 | if (error) |
| 3608 | return error; |
| 3609 | |
| 3610 | } |
| 3611 | |
| 3612 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3613 | |
| 3614 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3615 | * Set the failure fallback case to look in the selected AG as stream |
| 3616 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3617 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3618 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3619 | return 0; |
| 3620 | } |
| 3621 | |
| 3622 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3623 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3624 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3625 | { |
| 3626 | xfs_mount_t *mp; /* mount point structure */ |
| 3627 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 3628 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3629 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3630 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3631 | xfs_alloc_arg_t args; |
| 3632 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3633 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3634 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3635 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3636 | int tryagain; |
| 3637 | int error; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3638 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3639 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3640 | ASSERT(ap->length); |
| 3641 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3642 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3643 | |
| 3644 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3645 | stripe_align = 0; |
| 3646 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3647 | stripe_align = mp->m_swidth; |
| 3648 | else if (mp->m_dalign) |
| 3649 | stripe_align = mp->m_dalign; |
| 3650 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3651 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3652 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3653 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3655 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3656 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3657 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3658 | } |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3659 | |
| 3660 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3661 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3662 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3663 | if (nullfb) { |
| 3664 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 3665 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3666 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3667 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3668 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3669 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3670 | } |
| 3671 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3672 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3673 | |
| 3674 | xfs_bmap_adjacent(ap); |
| 3675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3677 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | * it's in the right allocation group. |
| 3679 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3680 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | ; |
| 3682 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3683 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | * Normal allocation, done through xfs_alloc_vextent. |
| 3686 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3687 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3688 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3689 | args.tp = ap->tp; |
| 3690 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3691 | args.fsbno = ap->blkno; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3692 | |
| 3693 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3694 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3695 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3696 | blen = 0; |
| 3697 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3698 | /* |
| 3699 | * Search for an allocation group with a single extent large |
| 3700 | * enough for the request. If one isn't found, then adjust |
| 3701 | * the minimum allocation size to the largest space found. |
| 3702 | */ |
| 3703 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 3704 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3705 | else |
| 3706 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3707 | if (error) |
| 3708 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3709 | } else if (ap->flist->xbf_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3710 | if (xfs_inode_is_filestream(ap->ip)) |
| 3711 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3712 | else |
| 3713 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3714 | args.total = args.minlen = ap->minlen; |
| 3715 | } else { |
| 3716 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3717 | args.total = ap->total; |
| 3718 | args.minlen = ap->minlen; |
| 3719 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3720 | /* apply extent size hints if obtained earlier */ |
| 3721 | if (unlikely(align)) { |
| 3722 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3723 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3724 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3725 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3726 | args.prod = 1; |
| 3727 | args.mod = 0; |
| 3728 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3729 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3730 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3731 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3732 | } |
| 3733 | /* |
| 3734 | * If we are not low on available data blocks, and the |
| 3735 | * underlying logical volume manager is a stripe, and |
| 3736 | * the file offset is zero then try to allocate data |
| 3737 | * blocks on stripe unit boundary. |
| 3738 | * NOTE: ap->aeof is only set if the allocation length |
| 3739 | * is >= the stripe unit and the allocation offset is |
| 3740 | * at the end of file. |
| 3741 | */ |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3742 | if (!ap->flist->xbf_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3743 | if (!ap->offset) { |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3744 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3745 | atype = args.type; |
| 3746 | isaligned = 1; |
| 3747 | /* |
| 3748 | * Adjust for alignment |
| 3749 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3750 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3751 | args.minlen = blen - args.alignment; |
| 3752 | args.minalignslop = 0; |
| 3753 | } else { |
| 3754 | /* |
| 3755 | * First try an exact bno allocation. |
| 3756 | * If it fails then do a near or start bno |
| 3757 | * allocation with alignment turned on. |
| 3758 | */ |
| 3759 | atype = args.type; |
| 3760 | tryagain = 1; |
| 3761 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3762 | args.alignment = 1; |
| 3763 | /* |
| 3764 | * Compute the minlen+alignment for the |
| 3765 | * next case. Set slop so that the value |
| 3766 | * of minlen+alignment+slop doesn't go up |
| 3767 | * between the calls. |
| 3768 | */ |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3769 | if (blen > stripe_align && blen <= args.maxlen) |
| 3770 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3771 | else |
| 3772 | nextminlen = args.minlen; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3773 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3774 | args.minalignslop = |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3775 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3776 | args.minlen - 1; |
| 3777 | else |
| 3778 | args.minalignslop = 0; |
| 3779 | } |
| 3780 | } else { |
| 3781 | args.alignment = 1; |
| 3782 | args.minalignslop = 0; |
| 3783 | } |
| 3784 | args.minleft = ap->minleft; |
| 3785 | args.wasdel = ap->wasdel; |
| 3786 | args.isfl = 0; |
| 3787 | args.userdata = ap->userdata; |
| 3788 | if ((error = xfs_alloc_vextent(&args))) |
| 3789 | return error; |
| 3790 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3791 | /* |
| 3792 | * Exact allocation failed. Now try with alignment |
| 3793 | * turned on. |
| 3794 | */ |
| 3795 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3796 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f0 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3797 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3798 | args.minlen = nextminlen; |
| 3799 | args.minalignslop = 0; |
| 3800 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | if ((error = xfs_alloc_vextent(&args))) |
| 3802 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3803 | } |
| 3804 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3805 | /* |
| 3806 | * allocation failed, so turn off alignment and |
| 3807 | * try again. |
| 3808 | */ |
| 3809 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3810 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3811 | args.alignment = 0; |
| 3812 | if ((error = xfs_alloc_vextent(&args))) |
| 3813 | return error; |
| 3814 | } |
| 3815 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3816 | args.minlen > ap->minlen) { |
| 3817 | args.minlen = ap->minlen; |
| 3818 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3819 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3820 | if ((error = xfs_alloc_vextent(&args))) |
| 3821 | return error; |
| 3822 | } |
| 3823 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3824 | args.fsbno = 0; |
| 3825 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3826 | args.total = ap->minlen; |
| 3827 | args.minleft = 0; |
| 3828 | if ((error = xfs_alloc_vextent(&args))) |
| 3829 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3830 | ap->flist->xbf_low = 1; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3831 | } |
| 3832 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3833 | /* |
| 3834 | * check the allocation happened at the same or higher AG than |
| 3835 | * the first block that was allocated. |
| 3836 | */ |
| 3837 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 3838 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 3839 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
| 3840 | (ap->flist->xbf_low && |
| 3841 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 3842 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 3843 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3844 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3845 | if (*ap->firstblock == NULLFSBLOCK) |
| 3846 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3847 | ASSERT(nullfb || fb_agno == args.agno || |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3848 | (ap->flist->xbf_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3849 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3850 | ap->ip->i_d.di_nblocks += args.len; |
| 3851 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3852 | if (ap->wasdel) |
| 3853 | ap->ip->i_delayed_blks -= args.len; |
| 3854 | /* |
| 3855 | * Adjust the disk quota also. This was reserved |
| 3856 | * earlier. |
| 3857 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3858 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3859 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3860 | XFS_TRANS_DQ_BCOUNT, |
| 3861 | (long) args.len); |
| 3862 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3863 | ap->blkno = NULLFSBLOCK; |
| 3864 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | } |
| 3866 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | /* |
| 3870 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3871 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3872 | */ |
| 3873 | STATIC int |
| 3874 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3875 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3876 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3877 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3878 | return xfs_bmap_rtalloc(ap); |
| 3879 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3883 | * Trim the returned map to the required bounds |
| 3884 | */ |
| 3885 | STATIC void |
| 3886 | xfs_bmapi_trim_map( |
| 3887 | struct xfs_bmbt_irec *mval, |
| 3888 | struct xfs_bmbt_irec *got, |
| 3889 | xfs_fileoff_t *bno, |
| 3890 | xfs_filblks_t len, |
| 3891 | xfs_fileoff_t obno, |
| 3892 | xfs_fileoff_t end, |
| 3893 | int n, |
| 3894 | int flags) |
| 3895 | { |
| 3896 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3897 | got->br_startoff + got->br_blockcount <= obno) { |
| 3898 | *mval = *got; |
| 3899 | if (isnullstartblock(got->br_startblock)) |
| 3900 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3901 | return; |
| 3902 | } |
| 3903 | |
| 3904 | if (obno > *bno) |
| 3905 | *bno = obno; |
| 3906 | ASSERT((*bno >= obno) || (n == 0)); |
| 3907 | ASSERT(*bno < end); |
| 3908 | mval->br_startoff = *bno; |
| 3909 | if (isnullstartblock(got->br_startblock)) |
| 3910 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3911 | else |
| 3912 | mval->br_startblock = got->br_startblock + |
| 3913 | (*bno - got->br_startoff); |
| 3914 | /* |
| 3915 | * Return the minimum of what we got and what we asked for for |
| 3916 | * the length. We can use the len variable here because it is |
| 3917 | * modified below and we could have been there before coming |
| 3918 | * here if the first part of the allocation didn't overlap what |
| 3919 | * was asked for. |
| 3920 | */ |
| 3921 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3922 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3923 | mval->br_state = got->br_state; |
| 3924 | ASSERT(mval->br_blockcount <= len); |
| 3925 | return; |
| 3926 | } |
| 3927 | |
| 3928 | /* |
| 3929 | * Update and validate the extent map to return |
| 3930 | */ |
| 3931 | STATIC void |
| 3932 | xfs_bmapi_update_map( |
| 3933 | struct xfs_bmbt_irec **map, |
| 3934 | xfs_fileoff_t *bno, |
| 3935 | xfs_filblks_t *len, |
| 3936 | xfs_fileoff_t obno, |
| 3937 | xfs_fileoff_t end, |
| 3938 | int *n, |
| 3939 | int flags) |
| 3940 | { |
| 3941 | xfs_bmbt_irec_t *mval = *map; |
| 3942 | |
| 3943 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3944 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3945 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3946 | (mval->br_startoff < obno)); |
| 3947 | |
| 3948 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3949 | *len = end - *bno; |
| 3950 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3951 | /* update previous map with new information */ |
| 3952 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3953 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3954 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3955 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3956 | mval[-1].br_state = mval->br_state; |
| 3957 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3958 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3959 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3960 | mval->br_startblock == mval[-1].br_startblock + |
| 3961 | mval[-1].br_blockcount && |
| 3962 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3963 | mval[-1].br_state == mval->br_state)) { |
| 3964 | ASSERT(mval->br_startoff == |
| 3965 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3966 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3967 | } else if (*n > 0 && |
| 3968 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3969 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3970 | mval->br_startoff == |
| 3971 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 3972 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3973 | mval[-1].br_state = mval->br_state; |
| 3974 | } else if (!((*n == 0) && |
| 3975 | ((mval->br_startoff + mval->br_blockcount) <= |
| 3976 | obno))) { |
| 3977 | mval++; |
| 3978 | (*n)++; |
| 3979 | } |
| 3980 | *map = mval; |
| 3981 | } |
| 3982 | |
| 3983 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 3984 | * Map file blocks to filesystem blocks without allocation. |
| 3985 | */ |
| 3986 | int |
| 3987 | xfs_bmapi_read( |
| 3988 | struct xfs_inode *ip, |
| 3989 | xfs_fileoff_t bno, |
| 3990 | xfs_filblks_t len, |
| 3991 | struct xfs_bmbt_irec *mval, |
| 3992 | int *nmap, |
| 3993 | int flags) |
| 3994 | { |
| 3995 | struct xfs_mount *mp = ip->i_mount; |
| 3996 | struct xfs_ifork *ifp; |
| 3997 | struct xfs_bmbt_irec got; |
| 3998 | struct xfs_bmbt_irec prev; |
| 3999 | xfs_fileoff_t obno; |
| 4000 | xfs_fileoff_t end; |
| 4001 | xfs_extnum_t lastx; |
| 4002 | int error; |
| 4003 | int eof; |
| 4004 | int n = 0; |
| 4005 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4006 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4007 | |
| 4008 | ASSERT(*nmap >= 1); |
| 4009 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 4010 | XFS_BMAPI_IGSTATE))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4011 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4012 | |
| 4013 | if (unlikely(XFS_TEST_ERROR( |
| 4014 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4015 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4016 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4017 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4018 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4022 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4023 | |
| 4024 | XFS_STATS_INC(xs_blk_mapr); |
| 4025 | |
| 4026 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4027 | |
| 4028 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4029 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4030 | if (error) |
| 4031 | return error; |
| 4032 | } |
| 4033 | |
| 4034 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4035 | end = bno + len; |
| 4036 | obno = bno; |
| 4037 | |
| 4038 | while (bno < end && n < *nmap) { |
| 4039 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4040 | if (eof) |
| 4041 | got.br_startoff = end; |
| 4042 | if (got.br_startoff > bno) { |
| 4043 | /* Reading in a hole. */ |
| 4044 | mval->br_startoff = bno; |
| 4045 | mval->br_startblock = HOLESTARTBLOCK; |
| 4046 | mval->br_blockcount = |
| 4047 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4048 | mval->br_state = XFS_EXT_NORM; |
| 4049 | bno += mval->br_blockcount; |
| 4050 | len -= mval->br_blockcount; |
| 4051 | mval++; |
| 4052 | n++; |
| 4053 | continue; |
| 4054 | } |
| 4055 | |
| 4056 | /* set up the extent map to return. */ |
| 4057 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4058 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4059 | |
| 4060 | /* If we're done, stop now. */ |
| 4061 | if (bno >= end || n >= *nmap) |
| 4062 | break; |
| 4063 | |
| 4064 | /* Else go on to the next record. */ |
| 4065 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4066 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4067 | else |
| 4068 | eof = 1; |
| 4069 | } |
| 4070 | *nmap = n; |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4074 | STATIC int |
| 4075 | xfs_bmapi_reserve_delalloc( |
| 4076 | struct xfs_inode *ip, |
| 4077 | xfs_fileoff_t aoff, |
| 4078 | xfs_filblks_t len, |
| 4079 | struct xfs_bmbt_irec *got, |
| 4080 | struct xfs_bmbt_irec *prev, |
| 4081 | xfs_extnum_t *lastx, |
| 4082 | int eof) |
| 4083 | { |
| 4084 | struct xfs_mount *mp = ip->i_mount; |
| 4085 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4086 | xfs_extlen_t alen; |
| 4087 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4088 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4089 | xfs_extlen_t extsz; |
| 4090 | int error; |
| 4091 | |
| 4092 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4093 | if (!eof) |
| 4094 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4095 | |
| 4096 | /* Figure out the extent size, adjust alen */ |
| 4097 | extsz = xfs_get_extsz_hint(ip); |
| 4098 | if (extsz) { |
| 4099 | /* |
| 4100 | * Make sure we don't exceed a single extent length when we |
| 4101 | * align the extent by reducing length we are going to |
| 4102 | * allocate by the maximum amount extent size aligment may |
| 4103 | * require. |
| 4104 | */ |
| 4105 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); |
| 4106 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4107 | 1, 0, &aoff, &alen); |
| 4108 | ASSERT(!error); |
| 4109 | } |
| 4110 | |
| 4111 | if (rt) |
| 4112 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4113 | |
| 4114 | /* |
| 4115 | * Make a transaction-less quota reservation for delayed allocation |
| 4116 | * blocks. This number gets adjusted later. We return if we haven't |
| 4117 | * allocated blocks already inside this loop. |
| 4118 | */ |
| 4119 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4120 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4121 | if (error) |
| 4122 | return error; |
| 4123 | |
| 4124 | /* |
| 4125 | * Split changing sb for alen and indlen since they could be coming |
| 4126 | * from different places. |
| 4127 | */ |
| 4128 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4129 | ASSERT(indlen > 0); |
| 4130 | |
| 4131 | if (rt) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4132 | error = xfs_mod_frextents(mp, -((int64_t)extsz)); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4133 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4134 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | if (error) |
| 4138 | goto out_unreserve_quota; |
| 4139 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4140 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4141 | if (error) |
| 4142 | goto out_unreserve_blocks; |
| 4143 | |
| 4144 | |
| 4145 | ip->i_delayed_blks += alen; |
| 4146 | |
| 4147 | got->br_startoff = aoff; |
| 4148 | got->br_startblock = nullstartblock(indlen); |
| 4149 | got->br_blockcount = alen; |
| 4150 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4151 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4152 | |
| 4153 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4154 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4155 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4156 | */ |
| 4157 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4158 | |
| 4159 | ASSERT(got->br_startoff <= aoff); |
| 4160 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4161 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4162 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4163 | return 0; |
| 4164 | |
| 4165 | out_unreserve_blocks: |
| 4166 | if (rt) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 4167 | xfs_mod_frextents(mp, extsz); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4168 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4169 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4170 | out_unreserve_quota: |
| 4171 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4172 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4173 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4174 | return error; |
| 4175 | } |
| 4176 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4177 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4178 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4179 | */ |
| 4180 | int |
| 4181 | xfs_bmapi_delay( |
| 4182 | struct xfs_inode *ip, /* incore inode */ |
| 4183 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4184 | xfs_filblks_t len, /* length to map in file */ |
| 4185 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4186 | int *nmap, /* i/o: mval size/count */ |
| 4187 | int flags) /* XFS_BMAPI_... */ |
| 4188 | { |
| 4189 | struct xfs_mount *mp = ip->i_mount; |
| 4190 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4191 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4192 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4193 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4194 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4195 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4196 | int eof; /* we've hit the end of extents */ |
| 4197 | int n = 0; /* current extent index */ |
| 4198 | int error = 0; |
| 4199 | |
| 4200 | ASSERT(*nmap >= 1); |
| 4201 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4202 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4203 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4204 | |
| 4205 | if (unlikely(XFS_TEST_ERROR( |
| 4206 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4207 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4208 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4209 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4210 | return -EFSCORRUPTED; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4214 | return -EIO; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4215 | |
| 4216 | XFS_STATS_INC(xs_blk_mapw); |
| 4217 | |
| 4218 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4219 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4220 | if (error) |
| 4221 | return error; |
| 4222 | } |
| 4223 | |
| 4224 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4225 | end = bno + len; |
| 4226 | obno = bno; |
| 4227 | |
| 4228 | while (bno < end && n < *nmap) { |
| 4229 | if (eof || got.br_startoff > bno) { |
| 4230 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4231 | &prev, &lastx, eof); |
| 4232 | if (error) { |
| 4233 | if (n == 0) { |
| 4234 | *nmap = 0; |
| 4235 | return error; |
| 4236 | } |
| 4237 | break; |
| 4238 | } |
| 4239 | } |
| 4240 | |
| 4241 | /* set up the extent map to return. */ |
| 4242 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4243 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4244 | |
| 4245 | /* If we're done, stop now. */ |
| 4246 | if (bno >= end || n >= *nmap) |
| 4247 | break; |
| 4248 | |
| 4249 | /* Else go on to the next record. */ |
| 4250 | prev = got; |
| 4251 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4252 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4253 | else |
| 4254 | eof = 1; |
| 4255 | } |
| 4256 | |
| 4257 | *nmap = n; |
| 4258 | return 0; |
| 4259 | } |
| 4260 | |
| 4261 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4262 | static int |
| 4263 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4264 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4265 | { |
| 4266 | struct xfs_mount *mp = bma->ip->i_mount; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4267 | int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4268 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4269 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4270 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4271 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4272 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4273 | ASSERT(bma->length > 0); |
| 4274 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4275 | /* |
| 4276 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4277 | * for in this bmap call but that wouldn't be as good. |
| 4278 | */ |
| 4279 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4280 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4281 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4282 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4283 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4284 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4285 | } |
| 4286 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4287 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4288 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4289 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4290 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | /* |
| 4294 | * Indicate if this is the first user data in the file, or just any |
| 4295 | * user data. |
| 4296 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4297 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4298 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4299 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
| 4300 | } |
| 4301 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4302 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4303 | |
| 4304 | /* |
| 4305 | * Only want to do the alignment at the eof if it is userdata and |
| 4306 | * allocation length is larger than a stripe unit. |
| 4307 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4308 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4309 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4310 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4311 | if (error) |
| 4312 | return error; |
| 4313 | } |
| 4314 | |
| 4315 | error = xfs_bmap_alloc(bma); |
| 4316 | if (error) |
| 4317 | return error; |
| 4318 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4319 | if (bma->flist->xbf_low) |
| 4320 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4321 | if (bma->cur) |
| 4322 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4323 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4324 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4325 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4326 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4327 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4328 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4329 | } |
| 4330 | /* |
| 4331 | * Bump the number of extents we've allocated |
| 4332 | * in this call. |
| 4333 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4334 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4335 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4336 | if (bma->cur) |
| 4337 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4338 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4339 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4340 | bma->got.br_startoff = bma->offset; |
| 4341 | bma->got.br_startblock = bma->blkno; |
| 4342 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4343 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4344 | |
| 4345 | /* |
| 4346 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4347 | * as unwritten. |
| 4348 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4349 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4350 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4351 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4352 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4353 | if (bma->wasdel) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 4354 | error = xfs_bmap_add_extent_delay_real(bma); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4355 | else |
| 4356 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4357 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4358 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4359 | if (error) |
| 4360 | return error; |
| 4361 | |
| 4362 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4363 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4364 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4365 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4366 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4367 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4368 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4369 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4370 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4371 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4372 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4373 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4374 | return 0; |
| 4375 | } |
| 4376 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4377 | STATIC int |
| 4378 | xfs_bmapi_convert_unwritten( |
| 4379 | struct xfs_bmalloca *bma, |
| 4380 | struct xfs_bmbt_irec *mval, |
| 4381 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4382 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4383 | { |
| 4384 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4385 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4386 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4387 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4388 | int error; |
| 4389 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4390 | /* check if we need to do unwritten->real conversion */ |
| 4391 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4392 | (flags & XFS_BMAPI_PREALLOC)) |
| 4393 | return 0; |
| 4394 | |
| 4395 | /* check if we need to do real->unwritten conversion */ |
| 4396 | if (mval->br_state == XFS_EXT_NORM && |
| 4397 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4398 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4399 | return 0; |
| 4400 | |
| 4401 | /* |
| 4402 | * Modify (by adding) the state flag, if writing. |
| 4403 | */ |
| 4404 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4405 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4406 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4407 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4408 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4409 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4410 | } |
| 4411 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4412 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4413 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4414 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4415 | &bma->cur, mval, bma->firstblock, bma->flist, |
| 4416 | &tmp_logflags); |
| 4417 | bma->logflags |= tmp_logflags; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4418 | if (error) |
| 4419 | return error; |
| 4420 | |
| 4421 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4422 | * Update our extent pointer, given that |
| 4423 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4424 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4425 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4426 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4427 | |
| 4428 | /* |
| 4429 | * We may have combined previously unwritten space with written space, |
| 4430 | * so generate another request. |
| 4431 | */ |
| 4432 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4433 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4434 | return 0; |
| 4435 | } |
| 4436 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4437 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4438 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4439 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4440 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4441 | * locking problems. |
| 4442 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | * The returned value in "firstblock" from the first call in a transaction |
| 4444 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4445 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4446 | * the first call in "total"; if no allocation group has that many free |
| 4447 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4448 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4449 | int |
| 4450 | xfs_bmapi_write( |
| 4451 | struct xfs_trans *tp, /* transaction pointer */ |
| 4452 | struct xfs_inode *ip, /* incore inode */ |
| 4453 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4454 | xfs_filblks_t len, /* length to map in file */ |
| 4455 | int flags, /* XFS_BMAPI_... */ |
| 4456 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4457 | controls a.g. for allocs */ |
| 4458 | xfs_extlen_t total, /* total blocks needed */ |
| 4459 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4460 | int *nmap, /* i/o: mval size/count */ |
| 4461 | struct xfs_bmap_free *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4463 | struct xfs_mount *mp = ip->i_mount; |
| 4464 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4465 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4466 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4467 | int eof; /* after the end of extents */ |
| 4468 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4469 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4470 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4471 | int whichfork; /* data or attr fork */ |
| 4472 | char inhole; /* current location is hole in file */ |
| 4473 | char wasdelay; /* old extent was delayed */ |
| 4474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4476 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4477 | int orig_flags; /* original flags arg value */ |
| 4478 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4479 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4480 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | |
| 4482 | orig_bno = bno; |
| 4483 | orig_len = len; |
| 4484 | orig_flags = flags; |
| 4485 | orig_mval = mval; |
| 4486 | orig_nmap = *nmap; |
| 4487 | #endif |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4488 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4489 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4492 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4493 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4494 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4495 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4496 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4497 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | if (unlikely(XFS_TEST_ERROR( |
| 4500 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4501 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4503 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4504 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4508 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4511 | |
| 4512 | XFS_STATS_INC(xs_blk_mapw); |
| 4513 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4514 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4516 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4518 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4519 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4520 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4524 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4525 | if (error) |
| 4526 | goto error0; |
| 4527 | } |
| 4528 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4529 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4530 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | n = 0; |
| 4532 | end = bno + len; |
| 4533 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4534 | |
| 4535 | bma.tp = tp; |
| 4536 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4537 | bma.total = total; |
| 4538 | bma.userdata = 0; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4539 | bma.flist = flist; |
| 4540 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4543 | inhole = eof || bma.got.br_startoff > bno; |
| 4544 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | /* |
| 4547 | * First, deal with the hole before the allocated space |
| 4548 | * that we found, if any. |
| 4549 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4550 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4551 | bma.eof = eof; |
| 4552 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4553 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4554 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4555 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4556 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4557 | /* |
| 4558 | * There's a 32/64 bit type mismatch between the |
| 4559 | * allocation length request (which can be 64 bits in |
| 4560 | * length) and the bma length request, which is |
| 4561 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4562 | * check for 32-bit overflows and handle them here. |
| 4563 | */ |
| 4564 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4565 | bma.length = MAXEXTLEN; |
| 4566 | else |
| 4567 | bma.length = len; |
| 4568 | |
| 4569 | ASSERT(len > 0); |
| 4570 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4571 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | if (error) |
| 4573 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4574 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4575 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4577 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4578 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4579 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4580 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4582 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4583 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4584 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4585 | continue; |
| 4586 | if (error) |
| 4587 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4589 | /* update the extent map to return */ |
| 4590 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | /* |
| 4593 | * If we're done, stop now. Stop when we've allocated |
| 4594 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4595 | * the transaction may get too big. |
| 4596 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4597 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4599 | |
| 4600 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4601 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4602 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4603 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4604 | &bma.got); |
| 4605 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4606 | eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | /* |
| 4611 | * Transform from btree to extents, give it cur. |
| 4612 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4613 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4614 | int tmp_logflags = 0; |
| 4615 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4616 | ASSERT(bma.cur); |
| 4617 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4619 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | if (error) |
| 4621 | goto error0; |
| 4622 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4625 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4626 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | error0: |
| 4629 | /* |
| 4630 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4631 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4633 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4635 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4636 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4638 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | /* |
| 4640 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4641 | * detecting a case where the data is changed, there's an error, |
| 4642 | * and it's not logged so we don't shutdown when we should. |
| 4643 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4644 | if (bma.logflags) |
| 4645 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4646 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4647 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | if (!error) { |
| 4649 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4650 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4651 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4652 | bma.cur->bc_private.b.firstblock) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | (flist->xbf_low && |
| 4654 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4655 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4656 | bma.cur->bc_private.b.firstblock))); |
| 4657 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4659 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4661 | } |
| 4662 | if (!error) |
| 4663 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4664 | orig_nmap, *nmap); |
| 4665 | return error; |
| 4666 | } |
| 4667 | |
| 4668 | /* |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4669 | * Called by xfs_bmapi to update file extent records and the btree |
| 4670 | * after removing space (or undoing a delayed allocation). |
| 4671 | */ |
| 4672 | STATIC int /* error */ |
| 4673 | xfs_bmap_del_extent( |
| 4674 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4675 | xfs_trans_t *tp, /* current transaction pointer */ |
| 4676 | xfs_extnum_t *idx, /* extent number to update/delete */ |
| 4677 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 4678 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4679 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4680 | int *logflagsp, /* inode logging flags */ |
| 4681 | int whichfork) /* data or attr fork */ |
| 4682 | { |
| 4683 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 4684 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 4685 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4686 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 4687 | int delay; /* current block is delayed allocated */ |
| 4688 | int do_fx; /* free extent at end of routine */ |
| 4689 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 4690 | int error; /* error return value */ |
| 4691 | int flags; /* inode logging flags */ |
| 4692 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 4693 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4694 | int i; /* temp state */ |
| 4695 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4696 | xfs_mount_t *mp; /* mount structure */ |
| 4697 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4698 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4699 | /* REFERENCED */ |
| 4700 | uint qfield; /* quota field to update */ |
| 4701 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 4702 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 4703 | int state = 0; |
| 4704 | |
| 4705 | XFS_STATS_INC(xs_del_exlist); |
| 4706 | |
| 4707 | if (whichfork == XFS_ATTR_FORK) |
| 4708 | state |= BMAP_ATTRFORK; |
| 4709 | |
| 4710 | mp = ip->i_mount; |
| 4711 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4712 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
| 4713 | (uint)sizeof(xfs_bmbt_rec_t))); |
| 4714 | ASSERT(del->br_blockcount > 0); |
| 4715 | ep = xfs_iext_get_ext(ifp, *idx); |
| 4716 | xfs_bmbt_get_all(ep, &got); |
| 4717 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4718 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4719 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4720 | ASSERT(got_endoff >= del_endoff); |
| 4721 | delay = isnullstartblock(got.br_startblock); |
| 4722 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 4723 | flags = 0; |
| 4724 | qfield = 0; |
| 4725 | error = 0; |
| 4726 | /* |
| 4727 | * If deleting a real allocation, must free up the disk space. |
| 4728 | */ |
| 4729 | if (!delay) { |
| 4730 | flags = XFS_ILOG_CORE; |
| 4731 | /* |
| 4732 | * Realtime allocation. Free it and record di_nblocks update. |
| 4733 | */ |
| 4734 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4735 | xfs_fsblock_t bno; |
| 4736 | xfs_filblks_t len; |
| 4737 | |
| 4738 | ASSERT(do_mod(del->br_blockcount, |
| 4739 | mp->m_sb.sb_rextsize) == 0); |
| 4740 | ASSERT(do_mod(del->br_startblock, |
| 4741 | mp->m_sb.sb_rextsize) == 0); |
| 4742 | bno = del->br_startblock; |
| 4743 | len = del->br_blockcount; |
| 4744 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4745 | do_div(len, mp->m_sb.sb_rextsize); |
| 4746 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4747 | if (error) |
| 4748 | goto done; |
| 4749 | do_fx = 0; |
| 4750 | nblks = len * mp->m_sb.sb_rextsize; |
| 4751 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4752 | } |
| 4753 | /* |
| 4754 | * Ordinary allocation. |
| 4755 | */ |
| 4756 | else { |
| 4757 | do_fx = 1; |
| 4758 | nblks = del->br_blockcount; |
| 4759 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4760 | } |
| 4761 | /* |
| 4762 | * Set up del_endblock and cur for later. |
| 4763 | */ |
| 4764 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4765 | if (cur) { |
| 4766 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 4767 | got.br_startblock, got.br_blockcount, |
| 4768 | &i))) |
| 4769 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4770 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4771 | } |
| 4772 | da_old = da_new = 0; |
| 4773 | } else { |
| 4774 | da_old = startblockval(got.br_startblock); |
| 4775 | da_new = 0; |
| 4776 | nblks = 0; |
| 4777 | do_fx = 0; |
| 4778 | } |
| 4779 | /* |
| 4780 | * Set flag value to use in switch statement. |
| 4781 | * Left-contig is 2, right-contig is 1. |
| 4782 | */ |
| 4783 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 4784 | (got_endoff == del_endoff)) { |
| 4785 | case 3: |
| 4786 | /* |
| 4787 | * Matches the whole extent. Delete the entry. |
| 4788 | */ |
| 4789 | xfs_iext_remove(ip, *idx, 1, |
| 4790 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 4791 | --*idx; |
| 4792 | if (delay) |
| 4793 | break; |
| 4794 | |
| 4795 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4796 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4797 | flags |= XFS_ILOG_CORE; |
| 4798 | if (!cur) { |
| 4799 | flags |= xfs_ilog_fext(whichfork); |
| 4800 | break; |
| 4801 | } |
| 4802 | if ((error = xfs_btree_delete(cur, &i))) |
| 4803 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4804 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4805 | break; |
| 4806 | |
| 4807 | case 2: |
| 4808 | /* |
| 4809 | * Deleting the first part of the extent. |
| 4810 | */ |
| 4811 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4812 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 4813 | temp = got.br_blockcount - del->br_blockcount; |
| 4814 | xfs_bmbt_set_blockcount(ep, temp); |
| 4815 | if (delay) { |
| 4816 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4817 | da_old); |
| 4818 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4819 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4820 | da_new = temp; |
| 4821 | break; |
| 4822 | } |
| 4823 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 4824 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4825 | if (!cur) { |
| 4826 | flags |= xfs_ilog_fext(whichfork); |
| 4827 | break; |
| 4828 | } |
| 4829 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 4830 | got.br_blockcount - del->br_blockcount, |
| 4831 | got.br_state))) |
| 4832 | goto done; |
| 4833 | break; |
| 4834 | |
| 4835 | case 1: |
| 4836 | /* |
| 4837 | * Deleting the last part of the extent. |
| 4838 | */ |
| 4839 | temp = got.br_blockcount - del->br_blockcount; |
| 4840 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4841 | xfs_bmbt_set_blockcount(ep, temp); |
| 4842 | if (delay) { |
| 4843 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4844 | da_old); |
| 4845 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4846 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4847 | da_new = temp; |
| 4848 | break; |
| 4849 | } |
| 4850 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4851 | if (!cur) { |
| 4852 | flags |= xfs_ilog_fext(whichfork); |
| 4853 | break; |
| 4854 | } |
| 4855 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 4856 | got.br_startblock, |
| 4857 | got.br_blockcount - del->br_blockcount, |
| 4858 | got.br_state))) |
| 4859 | goto done; |
| 4860 | break; |
| 4861 | |
| 4862 | case 0: |
| 4863 | /* |
| 4864 | * Deleting the middle of the extent. |
| 4865 | */ |
| 4866 | temp = del->br_startoff - got.br_startoff; |
| 4867 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4868 | xfs_bmbt_set_blockcount(ep, temp); |
| 4869 | new.br_startoff = del_endoff; |
| 4870 | temp2 = got_endoff - del_endoff; |
| 4871 | new.br_blockcount = temp2; |
| 4872 | new.br_state = got.br_state; |
| 4873 | if (!delay) { |
| 4874 | new.br_startblock = del_endblock; |
| 4875 | flags |= XFS_ILOG_CORE; |
| 4876 | if (cur) { |
| 4877 | if ((error = xfs_bmbt_update(cur, |
| 4878 | got.br_startoff, |
| 4879 | got.br_startblock, temp, |
| 4880 | got.br_state))) |
| 4881 | goto done; |
| 4882 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 4883 | goto done; |
| 4884 | cur->bc_rec.b = new; |
| 4885 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4886 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4887 | goto done; |
| 4888 | /* |
| 4889 | * If get no-space back from btree insert, |
| 4890 | * it tried a split, and we have a zero |
| 4891 | * block reservation. |
| 4892 | * Fix up our state and return the error. |
| 4893 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4894 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4895 | /* |
| 4896 | * Reset the cursor, don't trust |
| 4897 | * it after any insert operation. |
| 4898 | */ |
| 4899 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 4900 | got.br_startoff, |
| 4901 | got.br_startblock, |
| 4902 | temp, &i))) |
| 4903 | goto done; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4904 | XFS_WANT_CORRUPTED_GOTO(mp, |
| 4905 | i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4906 | /* |
| 4907 | * Update the btree record back |
| 4908 | * to the original value. |
| 4909 | */ |
| 4910 | if ((error = xfs_bmbt_update(cur, |
| 4911 | got.br_startoff, |
| 4912 | got.br_startblock, |
| 4913 | got.br_blockcount, |
| 4914 | got.br_state))) |
| 4915 | goto done; |
| 4916 | /* |
| 4917 | * Reset the extent record back |
| 4918 | * to the original value. |
| 4919 | */ |
| 4920 | xfs_bmbt_set_blockcount(ep, |
| 4921 | got.br_blockcount); |
| 4922 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4923 | error = -ENOSPC; |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4924 | goto done; |
| 4925 | } |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4926 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4927 | } else |
| 4928 | flags |= xfs_ilog_fext(whichfork); |
| 4929 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4930 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 4931 | } else { |
| 4932 | ASSERT(whichfork == XFS_DATA_FORK); |
| 4933 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 4934 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4935 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
| 4936 | new.br_startblock = nullstartblock((int)temp2); |
| 4937 | da_new = temp + temp2; |
| 4938 | while (da_new > da_old) { |
| 4939 | if (temp) { |
| 4940 | temp--; |
| 4941 | da_new--; |
| 4942 | xfs_bmbt_set_startblock(ep, |
| 4943 | nullstartblock((int)temp)); |
| 4944 | } |
| 4945 | if (da_new == da_old) |
| 4946 | break; |
| 4947 | if (temp2) { |
| 4948 | temp2--; |
| 4949 | da_new--; |
| 4950 | new.br_startblock = |
| 4951 | nullstartblock((int)temp2); |
| 4952 | } |
| 4953 | } |
| 4954 | } |
| 4955 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4956 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 4957 | ++*idx; |
| 4958 | break; |
| 4959 | } |
| 4960 | /* |
| 4961 | * If we need to, add to list of extents to delete. |
| 4962 | */ |
| 4963 | if (do_fx) |
| 4964 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 4965 | mp); |
| 4966 | /* |
| 4967 | * Adjust inode # blocks in the file. |
| 4968 | */ |
| 4969 | if (nblks) |
| 4970 | ip->i_d.di_nblocks -= nblks; |
| 4971 | /* |
| 4972 | * Adjust quota data. |
| 4973 | */ |
| 4974 | if (qfield) |
| 4975 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 4976 | |
| 4977 | /* |
| 4978 | * Account for change in delayed indirect blocks. |
| 4979 | * Nothing to do for disk quota accounting here. |
| 4980 | */ |
| 4981 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 4982 | if (da_old > da_new) |
| 4983 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4984 | done: |
| 4985 | *logflagsp = flags; |
| 4986 | return error; |
| 4987 | } |
| 4988 | |
| 4989 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | * Unmap (remove) blocks from a file. |
| 4991 | * If nexts is nonzero then the number of extents to remove is limited to |
| 4992 | * that value. If not all extents in the block range can be removed then |
| 4993 | * *done is set. |
| 4994 | */ |
| 4995 | int /* error */ |
| 4996 | xfs_bunmapi( |
| 4997 | xfs_trans_t *tp, /* transaction pointer */ |
| 4998 | struct xfs_inode *ip, /* incore inode */ |
| 4999 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5000 | xfs_filblks_t len, /* length to unmap in file */ |
| 5001 | int flags, /* misc flags */ |
| 5002 | xfs_extnum_t nexts, /* number of extents max */ |
| 5003 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5004 | controls a.g. for allocs */ |
| 5005 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 5006 | int *done) /* set if not done yet */ |
| 5007 | { |
| 5008 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5009 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5010 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5011 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | int error; /* error return value */ |
| 5013 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5014 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5016 | int isrt; /* freeing in rt area */ |
| 5017 | xfs_extnum_t lastx; /* last extent index used */ |
| 5018 | int logflags; /* transaction logging flags */ |
| 5019 | xfs_extlen_t mod; /* rt extent offset */ |
| 5020 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5021 | xfs_extnum_t nextents; /* number of file extents */ |
| 5022 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5024 | int tmp_logflags; /* partial logging flags */ |
| 5025 | int wasdel; /* was a delayed alloc extent */ |
| 5026 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | xfs_fsblock_t sum; |
| 5028 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5029 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5032 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5033 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5034 | if (unlikely( |
| 5035 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5036 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5037 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5038 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5039 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | } |
| 5041 | mp = ip->i_mount; |
| 5042 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5043 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5044 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5045 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | ASSERT(len > 0); |
| 5047 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5049 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5050 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5051 | return error; |
| 5052 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5053 | if (nextents == 0) { |
| 5054 | *done = 1; |
| 5055 | return 0; |
| 5056 | } |
| 5057 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5058 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | start = bno; |
| 5060 | bno = start + len - 1; |
| 5061 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5062 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | /* |
| 5065 | * Check to see if the given block number is past the end of the |
| 5066 | * file, back up to the last block if so... |
| 5067 | */ |
| 5068 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5069 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | xfs_bmbt_get_all(ep, &got); |
| 5071 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5072 | } |
| 5073 | logflags = 0; |
| 5074 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5075 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5076 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | cur->bc_private.b.firstblock = *firstblock; |
| 5078 | cur->bc_private.b.flist = flist; |
| 5079 | cur->bc_private.b.flags = 0; |
| 5080 | } else |
| 5081 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5082 | |
| 5083 | if (isrt) { |
| 5084 | /* |
| 5085 | * Synchronize by locking the bitmap inode. |
| 5086 | */ |
| 5087 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 5088 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
| 5089 | } |
| 5090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | extno = 0; |
| 5092 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5093 | (nexts == 0 || extno < nexts)) { |
| 5094 | /* |
| 5095 | * Is the found extent after a hole in which bno lives? |
| 5096 | * Just back up to the previous extent, if so. |
| 5097 | */ |
| 5098 | if (got.br_startoff > bno) { |
| 5099 | if (--lastx < 0) |
| 5100 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5101 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | xfs_bmbt_get_all(ep, &got); |
| 5103 | } |
| 5104 | /* |
| 5105 | * Is the last block of this extent before the range |
| 5106 | * we're supposed to delete? If so, we're done. |
| 5107 | */ |
| 5108 | bno = XFS_FILEOFF_MIN(bno, |
| 5109 | got.br_startoff + got.br_blockcount - 1); |
| 5110 | if (bno < start) |
| 5111 | break; |
| 5112 | /* |
| 5113 | * Then deal with the (possibly delayed) allocated space |
| 5114 | * we found. |
| 5115 | */ |
| 5116 | ASSERT(ep != NULL); |
| 5117 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5118 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | if (got.br_startoff < start) { |
| 5120 | del.br_startoff = start; |
| 5121 | del.br_blockcount -= start - got.br_startoff; |
| 5122 | if (!wasdel) |
| 5123 | del.br_startblock += start - got.br_startoff; |
| 5124 | } |
| 5125 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5126 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5127 | sum = del.br_startblock + del.br_blockcount; |
| 5128 | if (isrt && |
| 5129 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5130 | /* |
| 5131 | * Realtime extent not lined up at the end. |
| 5132 | * The extent could have been split into written |
| 5133 | * and unwritten pieces, or we could just be |
| 5134 | * unmapping part of it. But we can't really |
| 5135 | * get rid of part of a realtime extent. |
| 5136 | */ |
| 5137 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5138 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | /* |
| 5140 | * This piece is unwritten, or we're not |
| 5141 | * using unwritten extents. Skip over it. |
| 5142 | */ |
| 5143 | ASSERT(bno >= mod); |
| 5144 | bno -= mod > del.br_blockcount ? |
| 5145 | del.br_blockcount : mod; |
| 5146 | if (bno < got.br_startoff) { |
| 5147 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5148 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5149 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | } |
| 5151 | continue; |
| 5152 | } |
| 5153 | /* |
| 5154 | * It's written, turn it unwritten. |
| 5155 | * This is better than zeroing it. |
| 5156 | */ |
| 5157 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5158 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5159 | /* |
| 5160 | * If this spans a realtime extent boundary, |
| 5161 | * chop it back to the start of the one we end at. |
| 5162 | */ |
| 5163 | if (del.br_blockcount > mod) { |
| 5164 | del.br_startoff += del.br_blockcount - mod; |
| 5165 | del.br_startblock += del.br_blockcount - mod; |
| 5166 | del.br_blockcount = mod; |
| 5167 | } |
| 5168 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5169 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
| 5170 | &lastx, &cur, &del, firstblock, flist, |
| 5171 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5172 | if (error) |
| 5173 | goto error0; |
| 5174 | goto nodelete; |
| 5175 | } |
| 5176 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5177 | /* |
| 5178 | * Realtime extent is lined up at the end but not |
| 5179 | * at the front. We'll get rid of full extents if |
| 5180 | * we can. |
| 5181 | */ |
| 5182 | mod = mp->m_sb.sb_rextsize - mod; |
| 5183 | if (del.br_blockcount > mod) { |
| 5184 | del.br_blockcount -= mod; |
| 5185 | del.br_startoff += mod; |
| 5186 | del.br_startblock += mod; |
| 5187 | } else if ((del.br_startoff == start && |
| 5188 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5189 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5190 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | /* |
| 5192 | * Can't make it unwritten. There isn't |
| 5193 | * a full extent here so just skip it. |
| 5194 | */ |
| 5195 | ASSERT(bno >= del.br_blockcount); |
| 5196 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5197 | if (got.br_startoff > bno) { |
| 5198 | if (--lastx >= 0) { |
| 5199 | ep = xfs_iext_get_ext(ifp, |
| 5200 | lastx); |
| 5201 | xfs_bmbt_get_all(ep, &got); |
| 5202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | } |
| 5204 | continue; |
| 5205 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5206 | /* |
| 5207 | * This one is already unwritten. |
| 5208 | * It must have a written left neighbor. |
| 5209 | * Unwrite the killed part of that one and |
| 5210 | * try again. |
| 5211 | */ |
| 5212 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5213 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5214 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5216 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | ASSERT(del.br_startblock == |
| 5218 | prev.br_startblock + prev.br_blockcount); |
| 5219 | if (prev.br_startoff < start) { |
| 5220 | mod = start - prev.br_startoff; |
| 5221 | prev.br_blockcount -= mod; |
| 5222 | prev.br_startblock += mod; |
| 5223 | prev.br_startoff = start; |
| 5224 | } |
| 5225 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5226 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5227 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5228 | ip, &lastx, &cur, &prev, |
| 5229 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (error) |
| 5231 | goto error0; |
| 5232 | goto nodelete; |
| 5233 | } else { |
| 5234 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5235 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5236 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5237 | ip, &lastx, &cur, &del, |
| 5238 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | if (error) |
| 5240 | goto error0; |
| 5241 | goto nodelete; |
| 5242 | } |
| 5243 | } |
| 5244 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5245 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5246 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5247 | if (isrt) { |
| 5248 | xfs_filblks_t rtexts; |
| 5249 | |
| 5250 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5251 | do_div(rtexts, mp->m_sb.sb_rextsize); |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 5252 | xfs_mod_frextents(mp, (int64_t)rtexts); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5253 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5254 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5255 | XFS_QMOPT_RES_RTBLKS); |
| 5256 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 5257 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, |
| 5258 | false); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5259 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5260 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | ip->i_delayed_blks -= del.br_blockcount; |
| 5264 | if (cur) |
| 5265 | cur->bc_private.b.flags |= |
| 5266 | XFS_BTCUR_BPRV_WASDEL; |
| 5267 | } else if (cur) |
| 5268 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5269 | /* |
| 5270 | * If it's the case where the directory code is running |
| 5271 | * with no block reservation, and the deleted block is in |
| 5272 | * the middle of its extent, and the resulting insert |
| 5273 | * of an extent would cause transformation to btree format, |
| 5274 | * then reject it. The calling code will then swap |
| 5275 | * blocks around instead. |
| 5276 | * We have to do this now, rather than waiting for the |
| 5277 | * conversion to btree format, since the transaction |
| 5278 | * will be dirty. |
| 5279 | */ |
| 5280 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5281 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5282 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5283 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | del.br_startoff > got.br_startoff && |
| 5285 | del.br_startoff + del.br_blockcount < |
| 5286 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5287 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | goto error0; |
| 5289 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5290 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5291 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | logflags |= tmp_logflags; |
| 5293 | if (error) |
| 5294 | goto error0; |
| 5295 | bno = del.br_startoff - 1; |
| 5296 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | /* |
| 5298 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5301 | if (lastx >= 0) { |
| 5302 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5303 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5304 | if (--lastx >= 0) |
| 5305 | ep = xfs_iext_get_ext(ifp, |
| 5306 | lastx); |
| 5307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | extno++; |
| 5311 | } |
| 5312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | /* |
| 5316 | * Convert to a btree if necessary. |
| 5317 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5318 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | ASSERT(cur == NULL); |
| 5320 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5321 | &cur, 0, &tmp_logflags, whichfork); |
| 5322 | logflags |= tmp_logflags; |
| 5323 | if (error) |
| 5324 | goto error0; |
| 5325 | } |
| 5326 | /* |
| 5327 | * transform from btree to extents, give it cur |
| 5328 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5329 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | ASSERT(cur != NULL); |
| 5331 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5332 | whichfork); |
| 5333 | logflags |= tmp_logflags; |
| 5334 | if (error) |
| 5335 | goto error0; |
| 5336 | } |
| 5337 | /* |
| 5338 | * transform from extents to local? |
| 5339 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | error = 0; |
| 5341 | error0: |
| 5342 | /* |
| 5343 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5344 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5346 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5348 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5349 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5351 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | /* |
| 5353 | * Log inode even in the error case, if the transaction |
| 5354 | * is dirty we'll need to shut down the filesystem. |
| 5355 | */ |
| 5356 | if (logflags) |
| 5357 | xfs_trans_log_inode(tp, ip, logflags); |
| 5358 | if (cur) { |
| 5359 | if (!error) { |
| 5360 | *firstblock = cur->bc_private.b.firstblock; |
| 5361 | cur->bc_private.b.allocated = 0; |
| 5362 | } |
| 5363 | xfs_btree_del_cursor(cur, |
| 5364 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5365 | } |
| 5366 | return error; |
| 5367 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5368 | |
| 5369 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5370 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5371 | * extent that precedes the target hole of the shift. |
| 5372 | */ |
| 5373 | STATIC bool |
| 5374 | xfs_bmse_can_merge( |
| 5375 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5376 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5377 | xfs_fileoff_t shift) /* shift fsb */ |
| 5378 | { |
| 5379 | xfs_fileoff_t startoff; |
| 5380 | |
| 5381 | startoff = got->br_startoff - shift; |
| 5382 | |
| 5383 | /* |
| 5384 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5385 | * the preceding extent. |
| 5386 | */ |
| 5387 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5388 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5389 | (left->br_state != got->br_state) || |
| 5390 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5391 | return false; |
| 5392 | |
| 5393 | return true; |
| 5394 | } |
| 5395 | |
| 5396 | /* |
| 5397 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5398 | * hole in the file. If an extent shift would result in the extent being fully |
| 5399 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5400 | * the preceding extent rather than do the shift. |
| 5401 | * |
| 5402 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5403 | * with the provided extents via xfs_bmse_can_merge(). |
| 5404 | */ |
| 5405 | STATIC int |
| 5406 | xfs_bmse_merge( |
| 5407 | struct xfs_inode *ip, |
| 5408 | int whichfork, |
| 5409 | xfs_fileoff_t shift, /* shift fsb */ |
| 5410 | int current_ext, /* idx of gotp */ |
| 5411 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5412 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5413 | struct xfs_btree_cur *cur, |
| 5414 | int *logflags) /* output */ |
| 5415 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5416 | struct xfs_bmbt_irec got; |
| 5417 | struct xfs_bmbt_irec left; |
| 5418 | xfs_filblks_t blockcount; |
| 5419 | int error, i; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5420 | struct xfs_mount *mp = ip->i_mount; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5421 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5422 | xfs_bmbt_get_all(gotp, &got); |
| 5423 | xfs_bmbt_get_all(leftp, &left); |
| 5424 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5425 | |
| 5426 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5427 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5428 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5429 | |
| 5430 | /* |
| 5431 | * Merge the in-core extents. Note that the host record pointers and |
| 5432 | * current_ext index are invalid once the extent has been removed via |
| 5433 | * xfs_iext_remove(). |
| 5434 | */ |
| 5435 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5436 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5437 | |
| 5438 | /* |
| 5439 | * Update the on-disk extent count, the btree if necessary and log the |
| 5440 | * inode. |
| 5441 | */ |
| 5442 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5443 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5444 | *logflags |= XFS_ILOG_CORE; |
| 5445 | if (!cur) { |
| 5446 | *logflags |= XFS_ILOG_DEXT; |
| 5447 | return 0; |
| 5448 | } |
| 5449 | |
| 5450 | /* lookup and remove the extent to merge */ |
| 5451 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5452 | got.br_blockcount, &i); |
| 5453 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5454 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5455 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5456 | |
| 5457 | error = xfs_btree_delete(cur, &i); |
| 5458 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5459 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5460 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5461 | |
| 5462 | /* lookup and update size of the previous extent */ |
| 5463 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5464 | left.br_blockcount, &i); |
| 5465 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5466 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5467 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5468 | |
| 5469 | left.br_blockcount = blockcount; |
| 5470 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5471 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5472 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5476 | * Shift a single extent. |
| 5477 | */ |
| 5478 | STATIC int |
| 5479 | xfs_bmse_shift_one( |
| 5480 | struct xfs_inode *ip, |
| 5481 | int whichfork, |
| 5482 | xfs_fileoff_t offset_shift_fsb, |
| 5483 | int *current_ext, |
| 5484 | struct xfs_bmbt_rec_host *gotp, |
| 5485 | struct xfs_btree_cur *cur, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5486 | int *logflags, |
| 5487 | enum shift_direction direction) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5488 | { |
| 5489 | struct xfs_ifork *ifp; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5490 | struct xfs_mount *mp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5491 | xfs_fileoff_t startoff; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5492 | struct xfs_bmbt_rec_host *adj_irecp; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5493 | struct xfs_bmbt_irec got; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5494 | struct xfs_bmbt_irec adj_irec; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5495 | int error; |
| 5496 | int i; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5497 | int total_extents; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5498 | |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5499 | mp = ip->i_mount; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5500 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5501 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5502 | |
| 5503 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5504 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5505 | /* delalloc extents should be prevented by caller */ |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5506 | XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5507 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5508 | if (direction == SHIFT_LEFT) { |
| 5509 | startoff = got.br_startoff - offset_shift_fsb; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5510 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5511 | /* |
| 5512 | * Check for merge if we've got an extent to the left, |
| 5513 | * otherwise make sure there's enough room at the start |
| 5514 | * of the file for the shift. |
| 5515 | */ |
| 5516 | if (!*current_ext) { |
| 5517 | if (got.br_startoff < offset_shift_fsb) |
| 5518 | return -EINVAL; |
| 5519 | goto update_current_ext; |
| 5520 | } |
| 5521 | /* |
| 5522 | * grab the left extent and check for a large |
| 5523 | * enough hole. |
| 5524 | */ |
| 5525 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5526 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5527 | |
| 5528 | if (startoff < |
| 5529 | adj_irec.br_startoff + adj_irec.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5530 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5531 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5532 | /* check whether to merge the extent or shift it down */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5533 | if (xfs_bmse_can_merge(&adj_irec, &got, |
| 5534 | offset_shift_fsb)) { |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5535 | return xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5536 | *current_ext, gotp, adj_irecp, |
| 5537 | cur, logflags); |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5538 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5539 | } else { |
| 5540 | startoff = got.br_startoff + offset_shift_fsb; |
| 5541 | /* nothing to move if this is the last extent */ |
| 5542 | if (*current_ext >= (total_extents - 1)) |
| 5543 | goto update_current_ext; |
| 5544 | /* |
| 5545 | * If this is not the last extent in the file, make sure there |
| 5546 | * is enough room between current extent and next extent for |
| 5547 | * accommodating the shift. |
| 5548 | */ |
| 5549 | adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1); |
| 5550 | xfs_bmbt_get_all(adj_irecp, &adj_irec); |
| 5551 | if (startoff + got.br_blockcount > adj_irec.br_startoff) |
| 5552 | return -EINVAL; |
| 5553 | /* |
| 5554 | * Unlike a left shift (which involves a hole punch), |
| 5555 | * a right shift does not modify extent neighbors |
| 5556 | * in any way. We should never find mergeable extents |
| 5557 | * in this scenario. Check anyways and warn if we |
| 5558 | * encounter two extents that could be one. |
| 5559 | */ |
| 5560 | if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb)) |
| 5561 | WARN_ON_ONCE(1); |
| 5562 | } |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5563 | /* |
| 5564 | * Increment the extent index for the next iteration, update the start |
| 5565 | * offset of the in-core extent and update the btree if applicable. |
| 5566 | */ |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5567 | update_current_ext: |
| 5568 | if (direction == SHIFT_LEFT) |
| 5569 | (*current_ext)++; |
| 5570 | else |
| 5571 | (*current_ext)--; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5572 | xfs_bmbt_set_startoff(gotp, startoff); |
| 5573 | *logflags |= XFS_ILOG_CORE; |
| 5574 | if (!cur) { |
| 5575 | *logflags |= XFS_ILOG_DEXT; |
| 5576 | return 0; |
| 5577 | } |
| 5578 | |
| 5579 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5580 | got.br_blockcount, &i); |
| 5581 | if (error) |
| 5582 | return error; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 5583 | XFS_WANT_CORRUPTED_RETURN(mp, i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5584 | |
| 5585 | got.br_startoff = startoff; |
kbuild test robot | d254aae | 2014-12-01 08:42:52 +1100 | [diff] [blame] | 5586 | return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5587 | got.br_blockcount, got.br_state); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | /* |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5591 | * Shift extent records to the left/right to cover/create a hole. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5592 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5593 | * The maximum number of extents to be shifted in a single operation is |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5594 | * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5595 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 5596 | * is the length by which each extent is shifted. If there is no hole to shift |
| 5597 | * the extents into, this will be considered invalid operation and we abort |
| 5598 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5599 | */ |
| 5600 | int |
| 5601 | xfs_bmap_shift_extents( |
| 5602 | struct xfs_trans *tp, |
| 5603 | struct xfs_inode *ip, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5604 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5605 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5606 | int *done, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5607 | xfs_fileoff_t stop_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5608 | xfs_fsblock_t *firstblock, |
| 5609 | struct xfs_bmap_free *flist, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5610 | enum shift_direction direction, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5611 | int num_exts) |
| 5612 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5613 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5614 | struct xfs_bmbt_rec_host *gotp; |
| 5615 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5616 | struct xfs_mount *mp = ip->i_mount; |
| 5617 | struct xfs_ifork *ifp; |
| 5618 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5619 | xfs_extnum_t current_ext; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5620 | xfs_extnum_t total_extents; |
| 5621 | xfs_extnum_t stop_extent; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5622 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5623 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5624 | int logflags = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5625 | |
| 5626 | if (unlikely(XFS_TEST_ERROR( |
| 5627 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5628 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5629 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5630 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 5631 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5632 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5636 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5637 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5638 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5639 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5640 | ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT); |
| 5641 | ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5642 | |
| 5643 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5644 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5645 | /* Read in all the extents */ |
| 5646 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5647 | if (error) |
| 5648 | return error; |
| 5649 | } |
| 5650 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5651 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5652 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5653 | cur->bc_private.b.firstblock = *firstblock; |
| 5654 | cur->bc_private.b.flist = flist; |
| 5655 | cur->bc_private.b.flags = 0; |
| 5656 | } |
| 5657 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5658 | /* |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5659 | * There may be delalloc extents in the data fork before the range we |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5660 | * are collapsing out, so we cannot use the count of real extents here. |
| 5661 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5662 | */ |
| 5663 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5664 | if (total_extents == 0) { |
| 5665 | *done = 1; |
| 5666 | goto del_cursor; |
| 5667 | } |
| 5668 | |
| 5669 | /* |
| 5670 | * In case of first right shift, we need to initialize next_fsb |
| 5671 | */ |
| 5672 | if (*next_fsb == NULLFSBLOCK) { |
| 5673 | gotp = xfs_iext_get_ext(ifp, total_extents - 1); |
| 5674 | xfs_bmbt_get_all(gotp, &got); |
| 5675 | *next_fsb = got.br_startoff; |
| 5676 | if (stop_fsb > *next_fsb) { |
| 5677 | *done = 1; |
| 5678 | goto del_cursor; |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | /* Lookup the extent index at which we have to stop */ |
| 5683 | if (direction == SHIFT_RIGHT) { |
| 5684 | gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent); |
| 5685 | /* Make stop_extent exclusive of shift range */ |
| 5686 | stop_extent--; |
| 5687 | } else |
| 5688 | stop_extent = total_extents; |
| 5689 | |
| 5690 | /* |
| 5691 | * Look up the extent index for the fsb where we start shifting. We can |
| 5692 | * henceforth iterate with current_ext as extent list changes are locked |
| 5693 | * out via ilock. |
| 5694 | * |
| 5695 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 5696 | * *next_fsb lies in a hole beyond which there are no extents. Either |
| 5697 | * way, we are done. |
| 5698 | */ |
| 5699 | gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, ¤t_ext); |
| 5700 | if (!gotp) { |
| 5701 | *done = 1; |
| 5702 | goto del_cursor; |
| 5703 | } |
| 5704 | |
| 5705 | /* some sanity checking before we finally start shifting extents */ |
| 5706 | if ((direction == SHIFT_LEFT && current_ext >= stop_extent) || |
| 5707 | (direction == SHIFT_RIGHT && current_ext <= stop_extent)) { |
| 5708 | error = -EIO; |
| 5709 | goto del_cursor; |
| 5710 | } |
| 5711 | |
| 5712 | while (nexts++ < num_exts) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5713 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5714 | ¤t_ext, gotp, cur, &logflags, |
| 5715 | direction); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5716 | if (error) |
| 5717 | goto del_cursor; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5718 | /* |
| 5719 | * If there was an extent merge during the shift, the extent |
| 5720 | * count can change. Update the total and grade the next record. |
| 5721 | */ |
| 5722 | if (direction == SHIFT_LEFT) { |
| 5723 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 5724 | stop_extent = total_extents; |
| 5725 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5726 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5727 | if (current_ext == stop_extent) { |
| 5728 | *done = 1; |
| 5729 | *next_fsb = NULLFSBLOCK; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5730 | break; |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5731 | } |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5732 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5733 | } |
| 5734 | |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5735 | if (!*done) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5736 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5737 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5738 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5739 | |
| 5740 | del_cursor: |
| 5741 | if (cur) |
| 5742 | xfs_btree_del_cursor(cur, |
| 5743 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5744 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5745 | if (logflags) |
| 5746 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5747 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5748 | return error; |
| 5749 | } |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5750 | |
| 5751 | /* |
| 5752 | * Splits an extent into two extents at split_fsb block such that it is |
| 5753 | * the first block of the current_ext. @current_ext is a target extent |
| 5754 | * to be split. @split_fsb is a block where the extents is split. |
| 5755 | * If split_fsb lies in a hole or the first block of extents, just return 0. |
| 5756 | */ |
| 5757 | STATIC int |
| 5758 | xfs_bmap_split_extent_at( |
| 5759 | struct xfs_trans *tp, |
| 5760 | struct xfs_inode *ip, |
| 5761 | xfs_fileoff_t split_fsb, |
| 5762 | xfs_fsblock_t *firstfsb, |
| 5763 | struct xfs_bmap_free *free_list) |
| 5764 | { |
| 5765 | int whichfork = XFS_DATA_FORK; |
| 5766 | struct xfs_btree_cur *cur = NULL; |
| 5767 | struct xfs_bmbt_rec_host *gotp; |
| 5768 | struct xfs_bmbt_irec got; |
| 5769 | struct xfs_bmbt_irec new; /* split extent */ |
| 5770 | struct xfs_mount *mp = ip->i_mount; |
| 5771 | struct xfs_ifork *ifp; |
| 5772 | xfs_fsblock_t gotblkcnt; /* new block count for got */ |
| 5773 | xfs_extnum_t current_ext; |
| 5774 | int error = 0; |
| 5775 | int logflags = 0; |
| 5776 | int i = 0; |
| 5777 | |
| 5778 | if (unlikely(XFS_TEST_ERROR( |
| 5779 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5780 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5781 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5782 | XFS_ERROR_REPORT("xfs_bmap_split_extent_at", |
| 5783 | XFS_ERRLEVEL_LOW, mp); |
| 5784 | return -EFSCORRUPTED; |
| 5785 | } |
| 5786 | |
| 5787 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5788 | return -EIO; |
| 5789 | |
| 5790 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5791 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5792 | /* Read in all the extents */ |
| 5793 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5794 | if (error) |
| 5795 | return error; |
| 5796 | } |
| 5797 | |
| 5798 | /* |
| 5799 | * gotp can be null in 2 cases: 1) if there are no extents |
| 5800 | * or 2) split_fsb lies in a hole beyond which there are |
| 5801 | * no extents. Either way, we are done. |
| 5802 | */ |
| 5803 | gotp = xfs_iext_bno_to_ext(ifp, split_fsb, ¤t_ext); |
| 5804 | if (!gotp) |
| 5805 | return 0; |
| 5806 | |
| 5807 | xfs_bmbt_get_all(gotp, &got); |
| 5808 | |
| 5809 | /* |
| 5810 | * Check split_fsb lies in a hole or the start boundary offset |
| 5811 | * of the extent. |
| 5812 | */ |
| 5813 | if (got.br_startoff >= split_fsb) |
| 5814 | return 0; |
| 5815 | |
| 5816 | gotblkcnt = split_fsb - got.br_startoff; |
| 5817 | new.br_startoff = split_fsb; |
| 5818 | new.br_startblock = got.br_startblock + gotblkcnt; |
| 5819 | new.br_blockcount = got.br_blockcount - gotblkcnt; |
| 5820 | new.br_state = got.br_state; |
| 5821 | |
| 5822 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5823 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5824 | cur->bc_private.b.firstblock = *firstfsb; |
| 5825 | cur->bc_private.b.flist = free_list; |
| 5826 | cur->bc_private.b.flags = 0; |
| 5827 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 5828 | got.br_startblock, |
| 5829 | got.br_blockcount, |
| 5830 | &i); |
| 5831 | if (error) |
| 5832 | goto del_cursor; |
| 5833 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5834 | } |
| 5835 | |
| 5836 | xfs_bmbt_set_blockcount(gotp, gotblkcnt); |
| 5837 | got.br_blockcount = gotblkcnt; |
| 5838 | |
| 5839 | logflags = XFS_ILOG_CORE; |
| 5840 | if (cur) { |
| 5841 | error = xfs_bmbt_update(cur, got.br_startoff, |
| 5842 | got.br_startblock, |
| 5843 | got.br_blockcount, |
| 5844 | got.br_state); |
| 5845 | if (error) |
| 5846 | goto del_cursor; |
| 5847 | } else |
| 5848 | logflags |= XFS_ILOG_DEXT; |
| 5849 | |
| 5850 | /* Add new extent */ |
| 5851 | current_ext++; |
| 5852 | xfs_iext_insert(ip, current_ext, 1, &new, 0); |
| 5853 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5854 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 5855 | |
| 5856 | if (cur) { |
| 5857 | error = xfs_bmbt_lookup_eq(cur, new.br_startoff, |
| 5858 | new.br_startblock, new.br_blockcount, |
| 5859 | &i); |
| 5860 | if (error) |
| 5861 | goto del_cursor; |
| 5862 | XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor); |
| 5863 | cur->bc_rec.b.br_state = new.br_state; |
| 5864 | |
| 5865 | error = xfs_btree_insert(cur, &i); |
| 5866 | if (error) |
| 5867 | goto del_cursor; |
| 5868 | XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor); |
| 5869 | } |
| 5870 | |
| 5871 | /* |
| 5872 | * Convert to a btree if necessary. |
| 5873 | */ |
| 5874 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
| 5875 | int tmp_logflags; /* partial log flag return val */ |
| 5876 | |
| 5877 | ASSERT(cur == NULL); |
| 5878 | error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list, |
| 5879 | &cur, 0, &tmp_logflags, whichfork); |
| 5880 | logflags |= tmp_logflags; |
| 5881 | } |
| 5882 | |
| 5883 | del_cursor: |
| 5884 | if (cur) { |
| 5885 | cur->bc_private.b.allocated = 0; |
| 5886 | xfs_btree_del_cursor(cur, |
| 5887 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5888 | } |
| 5889 | |
| 5890 | if (logflags) |
| 5891 | xfs_trans_log_inode(tp, ip, logflags); |
| 5892 | return error; |
| 5893 | } |
| 5894 | |
| 5895 | int |
| 5896 | xfs_bmap_split_extent( |
| 5897 | struct xfs_inode *ip, |
| 5898 | xfs_fileoff_t split_fsb) |
| 5899 | { |
| 5900 | struct xfs_mount *mp = ip->i_mount; |
| 5901 | struct xfs_trans *tp; |
| 5902 | struct xfs_bmap_free free_list; |
| 5903 | xfs_fsblock_t firstfsb; |
| 5904 | int committed; |
| 5905 | int error; |
| 5906 | |
| 5907 | tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT); |
| 5908 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write, |
| 5909 | XFS_DIOSTRAT_SPACE_RES(mp, 0), 0); |
| 5910 | if (error) { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame^] | 5911 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5912 | return error; |
| 5913 | } |
| 5914 | |
| 5915 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 5916 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 5917 | |
| 5918 | xfs_bmap_init(&free_list, &firstfsb); |
| 5919 | |
| 5920 | error = xfs_bmap_split_extent_at(tp, ip, split_fsb, |
| 5921 | &firstfsb, &free_list); |
| 5922 | if (error) |
| 5923 | goto out; |
| 5924 | |
| 5925 | error = xfs_bmap_finish(&tp, &free_list, &committed); |
| 5926 | if (error) |
| 5927 | goto out; |
| 5928 | |
| 5929 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 5930 | |
| 5931 | |
| 5932 | out: |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame^] | 5933 | xfs_trans_cancel(tp); |
Namjae Jeon | a904b1c | 2015-03-25 15:08:56 +1100 | [diff] [blame] | 5934 | return error; |
| 5935 | } |