Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
| 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_sb.h" |
Dave Chinner | f5ea110 | 2013-04-24 18:58:02 +1000 | [diff] [blame] | 26 | #include "xfs_mount.h" |
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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | 9e5987a7 | 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 | |
| 247 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 248 | * Debug/sanity checking code |
| 249 | */ |
| 250 | |
| 251 | STATIC int |
| 252 | xfs_bmap_sanity_check( |
| 253 | struct xfs_mount *mp, |
| 254 | struct xfs_buf *bp, |
| 255 | int level) |
| 256 | { |
| 257 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 258 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 259 | if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) && |
| 260 | block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC)) |
| 261 | return 0; |
| 262 | |
| 263 | if (be16_to_cpu(block->bb_level) != level || |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 264 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 265 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 266 | return 0; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 267 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | #ifdef DEBUG |
| 272 | STATIC struct xfs_buf * |
| 273 | xfs_bmap_get_bp( |
| 274 | struct xfs_btree_cur *cur, |
| 275 | xfs_fsblock_t bno) |
| 276 | { |
| 277 | struct xfs_log_item_desc *lidp; |
| 278 | int i; |
| 279 | |
| 280 | if (!cur) |
| 281 | return NULL; |
| 282 | |
| 283 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 284 | if (!cur->bc_bufs[i]) |
| 285 | break; |
| 286 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 287 | return cur->bc_bufs[i]; |
| 288 | } |
| 289 | |
| 290 | /* Chase down all the log items to see if the bp is there */ |
| 291 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 292 | struct xfs_buf_log_item *bip; |
| 293 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 294 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 295 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 296 | return bip->bli_buf; |
| 297 | } |
| 298 | |
| 299 | return NULL; |
| 300 | } |
| 301 | |
| 302 | STATIC void |
| 303 | xfs_check_block( |
| 304 | struct xfs_btree_block *block, |
| 305 | xfs_mount_t *mp, |
| 306 | int root, |
| 307 | short sz) |
| 308 | { |
| 309 | int i, j, dmxr; |
| 310 | __be64 *pp, *thispa; /* pointer to block address */ |
| 311 | xfs_bmbt_key_t *prevp, *keyp; |
| 312 | |
| 313 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
| 314 | |
| 315 | prevp = NULL; |
| 316 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
| 317 | dmxr = mp->m_bmap_dmxr[0]; |
| 318 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
| 319 | |
| 320 | if (prevp) { |
| 321 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 322 | be64_to_cpu(keyp->br_startoff)); |
| 323 | } |
| 324 | prevp = keyp; |
| 325 | |
| 326 | /* |
| 327 | * Compare the block numbers to see if there are dups. |
| 328 | */ |
| 329 | if (root) |
| 330 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
| 331 | else |
| 332 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 333 | |
| 334 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
| 335 | if (root) |
| 336 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
| 337 | else |
| 338 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
| 339 | if (*thispa == *pp) { |
| 340 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
| 341 | __func__, j, i, |
| 342 | (unsigned long long)be64_to_cpu(*thispa)); |
| 343 | panic("%s: ptrs are equal in node\n", |
| 344 | __func__); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Check that the extents for the inode ip are in the right order in all |
| 352 | * btree leaves. |
| 353 | */ |
| 354 | |
| 355 | STATIC void |
| 356 | xfs_bmap_check_leaf_extents( |
| 357 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 358 | xfs_inode_t *ip, /* incore inode pointer */ |
| 359 | int whichfork) /* data or attr fork */ |
| 360 | { |
| 361 | struct xfs_btree_block *block; /* current btree block */ |
| 362 | xfs_fsblock_t bno; /* block # of "block" */ |
| 363 | xfs_buf_t *bp; /* buffer for "block" */ |
| 364 | int error; /* error return value */ |
| 365 | xfs_extnum_t i=0, j; /* index into the extents list */ |
| 366 | xfs_ifork_t *ifp; /* fork structure */ |
| 367 | int level; /* btree level, for checking */ |
| 368 | xfs_mount_t *mp; /* file system mount structure */ |
| 369 | __be64 *pp; /* pointer to block address */ |
| 370 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 371 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
| 372 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
| 373 | int bp_release = 0; |
| 374 | |
| 375 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | bno = NULLFSBLOCK; |
| 380 | mp = ip->i_mount; |
| 381 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 382 | block = ifp->if_broot; |
| 383 | /* |
| 384 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 385 | */ |
| 386 | level = be16_to_cpu(block->bb_level); |
| 387 | ASSERT(level > 0); |
| 388 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 389 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 390 | bno = be64_to_cpu(*pp); |
| 391 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 392 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 393 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 394 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 395 | |
| 396 | /* |
| 397 | * Go down the tree until leaf level is reached, following the first |
| 398 | * pointer (leftmost) at each level. |
| 399 | */ |
| 400 | while (level-- > 0) { |
| 401 | /* See if buf is in cur first */ |
| 402 | bp_release = 0; |
| 403 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 404 | if (!bp) { |
| 405 | bp_release = 1; |
| 406 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 407 | XFS_BMAP_BTREE_REF, |
| 408 | &xfs_bmbt_buf_ops); |
| 409 | if (error) |
| 410 | goto error_norelse; |
| 411 | } |
| 412 | block = XFS_BUF_TO_BLOCK(bp); |
| 413 | XFS_WANT_CORRUPTED_GOTO( |
| 414 | xfs_bmap_sanity_check(mp, bp, level), |
| 415 | error0); |
| 416 | if (level == 0) |
| 417 | break; |
| 418 | |
| 419 | /* |
| 420 | * Check this block for basic sanity (increasing keys and |
| 421 | * no duplicate blocks). |
| 422 | */ |
| 423 | |
| 424 | xfs_check_block(block, mp, 0, 0); |
| 425 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 426 | bno = be64_to_cpu(*pp); |
| 427 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
| 428 | if (bp_release) { |
| 429 | bp_release = 0; |
| 430 | xfs_trans_brelse(NULL, bp); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 436 | */ |
| 437 | i = 0; |
| 438 | |
| 439 | /* |
| 440 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 441 | */ |
| 442 | for (;;) { |
| 443 | xfs_fsblock_t nextbno; |
| 444 | xfs_extnum_t num_recs; |
| 445 | |
| 446 | |
| 447 | num_recs = xfs_btree_get_numrecs(block); |
| 448 | |
| 449 | /* |
| 450 | * Read-ahead the next leaf block, if any. |
| 451 | */ |
| 452 | |
| 453 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 454 | |
| 455 | /* |
| 456 | * Check all the extents to make sure they are OK. |
| 457 | * If we had a previous block, the last entry should |
| 458 | * conform with the first entry in this one. |
| 459 | */ |
| 460 | |
| 461 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 462 | if (i) { |
| 463 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 464 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 465 | xfs_bmbt_disk_get_startoff(ep)); |
| 466 | } |
| 467 | for (j = 1; j < num_recs; j++) { |
| 468 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
| 469 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 470 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 471 | xfs_bmbt_disk_get_startoff(nextp)); |
| 472 | ep = nextp; |
| 473 | } |
| 474 | |
| 475 | last = *ep; |
| 476 | i += num_recs; |
| 477 | if (bp_release) { |
| 478 | bp_release = 0; |
| 479 | xfs_trans_brelse(NULL, bp); |
| 480 | } |
| 481 | bno = nextbno; |
| 482 | /* |
| 483 | * If we've reached the end, stop. |
| 484 | */ |
| 485 | if (bno == NULLFSBLOCK) |
| 486 | break; |
| 487 | |
| 488 | bp_release = 0; |
| 489 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 490 | if (!bp) { |
| 491 | bp_release = 1; |
| 492 | error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 493 | XFS_BMAP_BTREE_REF, |
| 494 | &xfs_bmbt_buf_ops); |
| 495 | if (error) |
| 496 | goto error_norelse; |
| 497 | } |
| 498 | block = XFS_BUF_TO_BLOCK(bp); |
| 499 | } |
| 500 | if (bp_release) { |
| 501 | bp_release = 0; |
| 502 | xfs_trans_brelse(NULL, bp); |
| 503 | } |
| 504 | return; |
| 505 | |
| 506 | error0: |
| 507 | xfs_warn(mp, "%s: at error0", __func__); |
| 508 | if (bp_release) |
| 509 | xfs_trans_brelse(NULL, bp); |
| 510 | error_norelse: |
| 511 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
| 512 | __func__, i); |
| 513 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Add bmap trace insert entries for all the contents of the extent records. |
| 519 | */ |
| 520 | void |
| 521 | xfs_bmap_trace_exlist( |
| 522 | xfs_inode_t *ip, /* incore inode pointer */ |
| 523 | xfs_extnum_t cnt, /* count of entries in the list */ |
| 524 | int whichfork, /* data or attr fork */ |
| 525 | unsigned long caller_ip) |
| 526 | { |
| 527 | xfs_extnum_t idx; /* extent record index */ |
| 528 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 529 | int state = 0; |
| 530 | |
| 531 | if (whichfork == XFS_ATTR_FORK) |
| 532 | state |= BMAP_ATTRFORK; |
| 533 | |
| 534 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 535 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 536 | for (idx = 0; idx < cnt; idx++) |
| 537 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * Validate that the bmbt_irecs being returned from bmapi are valid |
Zhi Yong Wu | a97f4df | 2013-08-12 03:14:53 +0000 | [diff] [blame] | 542 | * given the caller's original parameters. Specifically check the |
| 543 | * ranges of the returned irecs to ensure that they only extend beyond |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 544 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 545 | */ |
| 546 | STATIC void |
| 547 | xfs_bmap_validate_ret( |
| 548 | xfs_fileoff_t bno, |
| 549 | xfs_filblks_t len, |
| 550 | int flags, |
| 551 | xfs_bmbt_irec_t *mval, |
| 552 | int nmap, |
| 553 | int ret_nmap) |
| 554 | { |
| 555 | int i; /* index to map values */ |
| 556 | |
| 557 | ASSERT(ret_nmap <= nmap); |
| 558 | |
| 559 | for (i = 0; i < ret_nmap; i++) { |
| 560 | ASSERT(mval[i].br_blockcount > 0); |
| 561 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 562 | ASSERT(mval[i].br_startoff >= bno); |
| 563 | ASSERT(mval[i].br_blockcount <= len); |
| 564 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 565 | bno + len); |
| 566 | } else { |
| 567 | ASSERT(mval[i].br_startoff < bno + len); |
| 568 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 569 | bno); |
| 570 | } |
| 571 | ASSERT(i == 0 || |
| 572 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 573 | mval[i].br_startoff); |
| 574 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 575 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 576 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 577 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | #else |
| 582 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 583 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 584 | #endif /* DEBUG */ |
| 585 | |
| 586 | /* |
| 587 | * bmap free list manipulation functions |
| 588 | */ |
| 589 | |
| 590 | /* |
| 591 | * Add the extent to the list of extents to be free at transaction end. |
| 592 | * The list is maintained sorted (by block number). |
| 593 | */ |
| 594 | void |
| 595 | xfs_bmap_add_free( |
| 596 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 597 | xfs_filblks_t len, /* length of extent */ |
| 598 | xfs_bmap_free_t *flist, /* list of extents */ |
| 599 | xfs_mount_t *mp) /* mount point structure */ |
| 600 | { |
| 601 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 602 | xfs_bmap_free_item_t *new; /* new element */ |
| 603 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 604 | #ifdef DEBUG |
| 605 | xfs_agnumber_t agno; |
| 606 | xfs_agblock_t agbno; |
| 607 | |
| 608 | ASSERT(bno != NULLFSBLOCK); |
| 609 | ASSERT(len > 0); |
| 610 | ASSERT(len <= MAXEXTLEN); |
| 611 | ASSERT(!isnullstartblock(bno)); |
| 612 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 613 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 614 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 615 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 616 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 617 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 618 | #endif |
| 619 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 620 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 621 | new->xbfi_startblock = bno; |
| 622 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 623 | for (prev = NULL, cur = flist->xbf_first; |
| 624 | cur != NULL; |
| 625 | prev = cur, cur = cur->xbfi_next) { |
| 626 | if (cur->xbfi_startblock >= bno) |
| 627 | break; |
| 628 | } |
| 629 | if (prev) |
| 630 | prev->xbfi_next = new; |
| 631 | else |
| 632 | flist->xbf_first = new; |
| 633 | new->xbfi_next = cur; |
| 634 | flist->xbf_count++; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Remove the entry "free" from the free item list. Prev points to the |
| 639 | * previous entry, unless "free" is the head of the list. |
| 640 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 641 | void |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 642 | xfs_bmap_del_free( |
| 643 | xfs_bmap_free_t *flist, /* free item list header */ |
| 644 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 645 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 646 | { |
| 647 | if (prev) |
| 648 | prev->xbfi_next = free->xbfi_next; |
| 649 | else |
| 650 | flist->xbf_first = free->xbfi_next; |
| 651 | flist->xbf_count--; |
| 652 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 653 | } |
| 654 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 655 | /* |
| 656 | * Free up any items left in the list. |
| 657 | */ |
| 658 | void |
| 659 | xfs_bmap_cancel( |
| 660 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 661 | { |
| 662 | xfs_bmap_free_item_t *free; /* free list item */ |
| 663 | xfs_bmap_free_item_t *next; |
| 664 | |
| 665 | if (flist->xbf_count == 0) |
| 666 | return; |
| 667 | ASSERT(flist->xbf_first != NULL); |
| 668 | for (free = flist->xbf_first; free; free = next) { |
| 669 | next = free->xbfi_next; |
| 670 | xfs_bmap_del_free(flist, NULL, free); |
| 671 | } |
| 672 | ASSERT(flist->xbf_count == 0); |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Inode fork format manipulation functions |
| 677 | */ |
| 678 | |
| 679 | /* |
| 680 | * Transform a btree format file with only one leaf node, where the |
| 681 | * extents list will fit in the inode, into an extents format file. |
| 682 | * Since the file extents are already in-core, all we have to do is |
| 683 | * give up the space for the btree root and pitch the leaf block. |
| 684 | */ |
| 685 | STATIC int /* error */ |
| 686 | xfs_bmap_btree_to_extents( |
| 687 | xfs_trans_t *tp, /* transaction pointer */ |
| 688 | xfs_inode_t *ip, /* incore inode pointer */ |
| 689 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 690 | int *logflagsp, /* inode logging flags */ |
| 691 | int whichfork) /* data or attr fork */ |
| 692 | { |
| 693 | /* REFERENCED */ |
| 694 | struct xfs_btree_block *cblock;/* child btree block */ |
| 695 | xfs_fsblock_t cbno; /* child block number */ |
| 696 | xfs_buf_t *cbp; /* child block's buffer */ |
| 697 | int error; /* error return value */ |
| 698 | xfs_ifork_t *ifp; /* inode fork data */ |
| 699 | xfs_mount_t *mp; /* mount point structure */ |
| 700 | __be64 *pp; /* ptr to block address */ |
| 701 | struct xfs_btree_block *rblock;/* root btree block */ |
| 702 | |
| 703 | mp = ip->i_mount; |
| 704 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 705 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 706 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 707 | rblock = ifp->if_broot; |
| 708 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 709 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
| 710 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 711 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
| 712 | cbno = be64_to_cpu(*pp); |
| 713 | *logflagsp = 0; |
| 714 | #ifdef DEBUG |
| 715 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
| 716 | return error; |
| 717 | #endif |
| 718 | error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF, |
| 719 | &xfs_bmbt_buf_ops); |
| 720 | if (error) |
| 721 | return error; |
| 722 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 723 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
| 724 | return error; |
| 725 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 726 | ip->i_d.di_nblocks--; |
| 727 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 728 | xfs_trans_binval(tp, cbp); |
| 729 | if (cur->bc_bufs[0] == cbp) |
| 730 | cur->bc_bufs[0] = NULL; |
| 731 | xfs_iroot_realloc(ip, -1, whichfork); |
| 732 | ASSERT(ifp->if_broot == NULL); |
| 733 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 734 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 735 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Convert an extents-format file into a btree-format file. |
| 741 | * The new file will have a root block (in the inode) and a single child block. |
| 742 | */ |
| 743 | STATIC int /* error */ |
| 744 | xfs_bmap_extents_to_btree( |
| 745 | xfs_trans_t *tp, /* transaction pointer */ |
| 746 | xfs_inode_t *ip, /* incore inode pointer */ |
| 747 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 748 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 749 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 750 | int wasdel, /* converting a delayed alloc */ |
| 751 | int *logflagsp, /* inode logging flags */ |
| 752 | int whichfork) /* data or attr fork */ |
| 753 | { |
| 754 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
| 755 | xfs_buf_t *abp; /* buffer for ablock */ |
| 756 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 757 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 758 | struct xfs_btree_block *block; /* btree root block */ |
| 759 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 760 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 761 | int error; /* error return value */ |
| 762 | xfs_extnum_t i, cnt; /* extent record index */ |
| 763 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 764 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 765 | xfs_mount_t *mp; /* mount structure */ |
| 766 | xfs_extnum_t nextents; /* number of file extents */ |
| 767 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 768 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 769 | mp = ip->i_mount; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 770 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 771 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 772 | |
| 773 | /* |
| 774 | * Make space in the inode incore. |
| 775 | */ |
| 776 | xfs_iroot_realloc(ip, 1, whichfork); |
| 777 | ifp->if_flags |= XFS_IFBROOT; |
| 778 | |
| 779 | /* |
| 780 | * Fill in the root. |
| 781 | */ |
| 782 | block = ifp->if_broot; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 783 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 784 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 785 | XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino, |
| 786 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 787 | else |
| 788 | xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL, |
| 789 | XFS_BMAP_MAGIC, 1, 1, ip->i_ino, |
| 790 | XFS_BTREE_LONG_PTRS); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 794 | */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 795 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 796 | cur->bc_private.b.firstblock = *firstblock; |
| 797 | cur->bc_private.b.flist = flist; |
| 798 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 799 | /* |
| 800 | * Convert to a btree with two levels, one record in root. |
| 801 | */ |
| 802 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 803 | memset(&args, 0, sizeof(args)); |
| 804 | args.tp = tp; |
| 805 | args.mp = mp; |
| 806 | args.firstblock = *firstblock; |
| 807 | if (*firstblock == NULLFSBLOCK) { |
| 808 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 809 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 810 | } else if (flist->xbf_low) { |
| 811 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 812 | args.fsbno = *firstblock; |
| 813 | } else { |
| 814 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 815 | args.fsbno = *firstblock; |
| 816 | } |
| 817 | args.minlen = args.maxlen = args.prod = 1; |
| 818 | args.wasdel = wasdel; |
| 819 | *logflagsp = 0; |
| 820 | if ((error = xfs_alloc_vextent(&args))) { |
| 821 | xfs_iroot_realloc(ip, -1, whichfork); |
| 822 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 823 | return error; |
| 824 | } |
| 825 | /* |
| 826 | * Allocation can't fail, the space was reserved. |
| 827 | */ |
| 828 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 829 | ASSERT(*firstblock == NULLFSBLOCK || |
| 830 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 831 | (flist->xbf_low && |
| 832 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 833 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 834 | cur->bc_private.b.allocated++; |
| 835 | ip->i_d.di_nblocks++; |
| 836 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 837 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 838 | /* |
| 839 | * Fill in the child block. |
| 840 | */ |
| 841 | abp->b_ops = &xfs_bmbt_buf_ops; |
| 842 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 843 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 844 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 845 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, |
| 846 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 847 | else |
| 848 | xfs_btree_init_block_int(mp, ablock, abp->b_bn, |
| 849 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, |
| 850 | XFS_BTREE_LONG_PTRS); |
| 851 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 852 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 853 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 854 | for (cnt = i = 0; i < nextents; i++) { |
| 855 | ep = xfs_iext_get_ext(ifp, i); |
| 856 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
| 857 | arp->l0 = cpu_to_be64(ep->l0); |
| 858 | arp->l1 = cpu_to_be64(ep->l1); |
| 859 | arp++; cnt++; |
| 860 | } |
| 861 | } |
| 862 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 863 | xfs_btree_set_numrecs(ablock, cnt); |
| 864 | |
| 865 | /* |
| 866 | * Fill in the root key and pointer. |
| 867 | */ |
| 868 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 869 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
| 870 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
| 871 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 872 | be16_to_cpu(block->bb_level))); |
| 873 | *pp = cpu_to_be64(args.fsbno); |
| 874 | |
| 875 | /* |
| 876 | * Do all this logging at the end so that |
| 877 | * the root is at the right level. |
| 878 | */ |
| 879 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 880 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
| 881 | ASSERT(*curp == NULL); |
| 882 | *curp = cur; |
| 883 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Convert a local file to an extents file. |
| 889 | * This code is out of bounds for data forks of regular files, |
| 890 | * since the file data needs to get logged so things will stay consistent. |
| 891 | * (The bmap-level manipulations are ok, though). |
| 892 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 893 | void |
| 894 | xfs_bmap_local_to_extents_empty( |
| 895 | struct xfs_inode *ip, |
| 896 | int whichfork) |
| 897 | { |
| 898 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 899 | |
| 900 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 901 | ASSERT(ifp->if_bytes == 0); |
| 902 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
| 903 | |
Eric Sandeen | 6a9edd3 | 2014-04-14 18:59:26 +1000 | [diff] [blame] | 904 | xfs_bmap_forkoff_reset(ip, whichfork); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 905 | ifp->if_flags &= ~XFS_IFINLINE; |
| 906 | ifp->if_flags |= XFS_IFEXTENTS; |
| 907 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 908 | } |
| 909 | |
| 910 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 911 | STATIC int /* error */ |
| 912 | xfs_bmap_local_to_extents( |
| 913 | xfs_trans_t *tp, /* transaction pointer */ |
| 914 | xfs_inode_t *ip, /* incore inode pointer */ |
| 915 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 916 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 917 | int *logflagsp, /* inode logging flags */ |
| 918 | int whichfork, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 919 | void (*init_fn)(struct xfs_trans *tp, |
| 920 | struct xfs_buf *bp, |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 921 | struct xfs_inode *ip, |
| 922 | struct xfs_ifork *ifp)) |
| 923 | { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 924 | int error = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 925 | int flags; /* logging flags returned */ |
| 926 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 927 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 928 | xfs_buf_t *bp; /* buffer for extent block */ |
| 929 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 930 | |
| 931 | /* |
| 932 | * We don't want to deal with the case of keeping inode data inline yet. |
| 933 | * So sending the data fork of a regular inode is invalid. |
| 934 | */ |
| 935 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); |
| 936 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 937 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 938 | |
| 939 | if (!ifp->if_bytes) { |
| 940 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
| 941 | flags = XFS_ILOG_CORE; |
| 942 | goto done; |
| 943 | } |
| 944 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 945 | flags = 0; |
| 946 | error = 0; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 947 | ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == |
| 948 | XFS_IFINLINE); |
| 949 | memset(&args, 0, sizeof(args)); |
| 950 | args.tp = tp; |
| 951 | args.mp = ip->i_mount; |
| 952 | args.firstblock = *firstblock; |
| 953 | /* |
| 954 | * Allocate a block. We know we need only one, since the |
| 955 | * file currently fits in an inode. |
| 956 | */ |
| 957 | if (*firstblock == NULLFSBLOCK) { |
| 958 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 959 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 960 | } else { |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 961 | args.fsbno = *firstblock; |
| 962 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 963 | } |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 964 | args.total = total; |
| 965 | args.minlen = args.maxlen = args.prod = 1; |
| 966 | error = xfs_alloc_vextent(&args); |
| 967 | if (error) |
| 968 | goto done; |
| 969 | |
| 970 | /* Can't fail, the space was reserved. */ |
| 971 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 972 | ASSERT(args.len == 1); |
| 973 | *firstblock = args.fsbno; |
| 974 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 975 | |
Dave Chinner | fe22d55 | 2015-01-22 09:30:06 +1100 | [diff] [blame] | 976 | /* |
| 977 | * Initialise the block and copy the data |
| 978 | * |
| 979 | * Note: init_fn must set the buffer log item type correctly! |
| 980 | */ |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 981 | init_fn(tp, bp, ip, ifp); |
| 982 | |
| 983 | /* account for the change in fork size and log everything */ |
| 984 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
| 985 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
| 986 | xfs_bmap_local_to_extents_empty(ip, whichfork); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 987 | flags |= XFS_ILOG_CORE; |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 988 | |
| 989 | xfs_iext_add(ifp, 0, 1); |
| 990 | ep = xfs_iext_get_ext(ifp, 0); |
| 991 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 992 | trace_xfs_bmap_post_update(ip, 0, |
| 993 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 994 | _THIS_IP_); |
| 995 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 996 | ip->i_d.di_nblocks = 1; |
| 997 | xfs_trans_mod_dquot_byino(tp, ip, |
| 998 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 999 | flags |= xfs_ilog_fext(whichfork); |
| 1000 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1001 | done: |
| 1002 | *logflagsp = flags; |
| 1003 | return error; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 1008 | */ |
| 1009 | STATIC int /* error */ |
| 1010 | xfs_bmap_add_attrfork_btree( |
| 1011 | xfs_trans_t *tp, /* transaction pointer */ |
| 1012 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1013 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 1014 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 1015 | int *flags) /* inode logging flags */ |
| 1016 | { |
| 1017 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 1018 | int error; /* error return value */ |
| 1019 | xfs_mount_t *mp; /* file system mount struct */ |
| 1020 | int stat; /* newroot status */ |
| 1021 | |
| 1022 | mp = ip->i_mount; |
| 1023 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1024 | *flags |= XFS_ILOG_DBROOT; |
| 1025 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 1026 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | cur->bc_private.b.flist = flist; |
| 1028 | cur->bc_private.b.firstblock = *firstblock; |
| 1029 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 1030 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1031 | /* must be at least one entry */ |
| 1032 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 1033 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | goto error0; |
| 1035 | if (stat == 0) { |
| 1036 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1037 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
| 1039 | *firstblock = cur->bc_private.b.firstblock; |
| 1040 | cur->bc_private.b.allocated = 0; |
| 1041 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 1042 | } |
| 1043 | return 0; |
| 1044 | error0: |
| 1045 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 1046 | return error; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
| 1050 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 1051 | */ |
| 1052 | STATIC int /* error */ |
| 1053 | xfs_bmap_add_attrfork_extents( |
| 1054 | xfs_trans_t *tp, /* transaction pointer */ |
| 1055 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1056 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 1057 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 1058 | int *flags) /* inode logging flags */ |
| 1059 | { |
| 1060 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 1061 | int error; /* error return value */ |
| 1062 | |
| 1063 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 1064 | return 0; |
| 1065 | cur = NULL; |
| 1066 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 1067 | flags, XFS_DATA_FORK); |
| 1068 | if (cur) { |
| 1069 | cur->bc_private.b.allocated = 0; |
| 1070 | xfs_btree_del_cursor(cur, |
| 1071 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 1072 | } |
| 1073 | return error; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1077 | * Called from xfs_bmap_add_attrfork to handle local format files. Each |
| 1078 | * different data fork content type needs a different callout to do the |
| 1079 | * conversion. Some are basic and only require special block initialisation |
| 1080 | * callouts for the data formating, others (directories) are so specialised they |
| 1081 | * handle everything themselves. |
| 1082 | * |
| 1083 | * XXX (dgc): investigate whether directory conversion can use the generic |
| 1084 | * formatting callout. It should be possible - it's just a very complex |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1085 | * formatter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | */ |
| 1087 | STATIC int /* error */ |
| 1088 | xfs_bmap_add_attrfork_local( |
| 1089 | xfs_trans_t *tp, /* transaction pointer */ |
| 1090 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1091 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 1092 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 1093 | int *flags) /* inode logging flags */ |
| 1094 | { |
| 1095 | xfs_da_args_t dargs; /* args for dir/attr code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 1098 | return 0; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1099 | |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 1100 | if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | memset(&dargs, 0, sizeof(dargs)); |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1102 | dargs.geo = ip->i_mount->m_dir_geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | dargs.dp = ip; |
| 1104 | dargs.firstblock = firstblock; |
| 1105 | dargs.flist = flist; |
Dave Chinner | d6cf130 | 2014-06-06 15:14:11 +1000 | [diff] [blame] | 1106 | dargs.total = dargs.geo->fsbcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | dargs.whichfork = XFS_DATA_FORK; |
| 1108 | dargs.trans = tp; |
Dave Chinner | 1e82379 | 2013-02-11 15:58:13 +1100 | [diff] [blame] | 1109 | return xfs_dir2_sf_to_block(&dargs); |
| 1110 | } |
| 1111 | |
| 1112 | if (S_ISLNK(ip->i_d.di_mode)) |
| 1113 | return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, |
| 1114 | flags, XFS_DATA_FORK, |
| 1115 | xfs_symlink_local_to_remote); |
| 1116 | |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 1117 | /* should only be called for types that support local format data */ |
| 1118 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1119 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1123 | * Convert inode from non-attributed to attributed. |
| 1124 | * Must not be in a transaction, ip must not be locked. |
| 1125 | */ |
| 1126 | int /* error code */ |
| 1127 | xfs_bmap_add_attrfork( |
| 1128 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1129 | int size, /* space new attribute needs */ |
| 1130 | int rsvd) /* xact may use reserved blks */ |
| 1131 | { |
| 1132 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
| 1133 | xfs_bmap_free_t flist; /* freed extent records */ |
| 1134 | xfs_mount_t *mp; /* mount structure */ |
| 1135 | xfs_trans_t *tp; /* transaction pointer */ |
| 1136 | int blks; /* space reservation */ |
| 1137 | int version = 1; /* superblock attr version */ |
| 1138 | int committed; /* xaction was committed */ |
| 1139 | int logflags; /* logging flags */ |
| 1140 | int error; /* error return value */ |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1141 | int cancel_flags = 0; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1142 | |
| 1143 | ASSERT(XFS_IFORK_Q(ip) == 0); |
| 1144 | |
| 1145 | mp = ip->i_mount; |
| 1146 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 1147 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 1148 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 1149 | if (rsvd) |
| 1150 | tp->t_flags |= XFS_TRANS_RESERVE; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1151 | error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1152 | if (error) { |
| 1153 | xfs_trans_cancel(tp, 0); |
| 1154 | return error; |
| 1155 | } |
| 1156 | cancel_flags = XFS_TRANS_RELEASE_LOG_RES; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1157 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1158 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
| 1159 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 1160 | XFS_QMOPT_RES_REGBLKS); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1161 | if (error) |
| 1162 | goto trans_cancel; |
| 1163 | cancel_flags |= XFS_TRANS_ABORT; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1164 | if (XFS_IFORK_Q(ip)) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1165 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1166 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 1167 | /* |
| 1168 | * For inodes coming from pre-6.2 filesystems. |
| 1169 | */ |
| 1170 | ASSERT(ip->i_d.di_aformat == 0); |
| 1171 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 1172 | } |
| 1173 | ASSERT(ip->i_d.di_anextents == 0); |
| 1174 | |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1175 | xfs_trans_ijoin(tp, ip, 0); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1176 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 1177 | |
| 1178 | switch (ip->i_d.di_format) { |
| 1179 | case XFS_DINODE_FMT_DEV: |
| 1180 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 1181 | break; |
| 1182 | case XFS_DINODE_FMT_UUID: |
| 1183 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 1184 | break; |
| 1185 | case XFS_DINODE_FMT_LOCAL: |
| 1186 | case XFS_DINODE_FMT_EXTENTS: |
| 1187 | case XFS_DINODE_FMT_BTREE: |
| 1188 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 1189 | if (!ip->i_d.di_forkoff) |
| 1190 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
| 1191 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
| 1192 | version = 2; |
| 1193 | break; |
| 1194 | default: |
| 1195 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1196 | error = -EINVAL; |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1197 | goto trans_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | ASSERT(ip->i_afp == NULL); |
| 1201 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 1202 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 1203 | logflags = 0; |
| 1204 | xfs_bmap_init(&flist, &firstblock); |
| 1205 | switch (ip->i_d.di_format) { |
| 1206 | case XFS_DINODE_FMT_LOCAL: |
| 1207 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 1208 | &logflags); |
| 1209 | break; |
| 1210 | case XFS_DINODE_FMT_EXTENTS: |
| 1211 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 1212 | &flist, &logflags); |
| 1213 | break; |
| 1214 | case XFS_DINODE_FMT_BTREE: |
| 1215 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 1216 | &logflags); |
| 1217 | break; |
| 1218 | default: |
| 1219 | error = 0; |
| 1220 | break; |
| 1221 | } |
| 1222 | if (logflags) |
| 1223 | xfs_trans_log_inode(tp, ip, logflags); |
| 1224 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1225 | goto bmap_cancel; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1226 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 1227 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1228 | bool log_sb = false; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1229 | |
| 1230 | spin_lock(&mp->m_sb_lock); |
| 1231 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 1232 | xfs_sb_version_addattr(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1233 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1234 | } |
| 1235 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 1236 | xfs_sb_version_addattr2(&mp->m_sb); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1237 | log_sb = true; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1238 | } |
Dave Chinner | 4d11a40 | 2015-01-22 09:10:26 +1100 | [diff] [blame] | 1239 | spin_unlock(&mp->m_sb_lock); |
Dave Chinner | 61e63ec | 2015-01-22 09:10:31 +1100 | [diff] [blame] | 1240 | if (log_sb) |
| 1241 | xfs_log_sb(tp); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | error = xfs_bmap_finish(&tp, &flist, &committed); |
| 1245 | if (error) |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1246 | goto bmap_cancel; |
| 1247 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1248 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Mark Tinguely | 9e3908e | 2013-11-07 15:43:28 -0600 | [diff] [blame] | 1249 | return error; |
| 1250 | |
| 1251 | bmap_cancel: |
| 1252 | xfs_bmap_cancel(&flist); |
| 1253 | trans_cancel: |
| 1254 | xfs_trans_cancel(tp, cancel_flags); |
| 1255 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1256 | return error; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * Internal and external extent tree search functions. |
| 1261 | */ |
| 1262 | |
| 1263 | /* |
| 1264 | * Read in the extents to if_extents. |
| 1265 | * All inode fields are set up by caller, we just traverse the btree |
| 1266 | * and copy the records in. If the file system cannot contain unwritten |
| 1267 | * extents, the records are checked for no "state" flags. |
| 1268 | */ |
| 1269 | int /* error */ |
| 1270 | xfs_bmap_read_extents( |
| 1271 | xfs_trans_t *tp, /* transaction pointer */ |
| 1272 | xfs_inode_t *ip, /* incore inode */ |
| 1273 | int whichfork) /* data or attr fork */ |
| 1274 | { |
| 1275 | struct xfs_btree_block *block; /* current btree block */ |
| 1276 | xfs_fsblock_t bno; /* block # of "block" */ |
| 1277 | xfs_buf_t *bp; /* buffer for "block" */ |
| 1278 | int error; /* error return value */ |
| 1279 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 1280 | xfs_extnum_t i, j; /* index into the extents list */ |
| 1281 | xfs_ifork_t *ifp; /* fork structure */ |
| 1282 | int level; /* btree level, for checking */ |
| 1283 | xfs_mount_t *mp; /* file system mount structure */ |
| 1284 | __be64 *pp; /* pointer to block address */ |
| 1285 | /* REFERENCED */ |
| 1286 | xfs_extnum_t room; /* number of entries there's room for */ |
| 1287 | |
| 1288 | bno = NULLFSBLOCK; |
| 1289 | mp = ip->i_mount; |
| 1290 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1291 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 1292 | XFS_EXTFMT_INODE(ip); |
| 1293 | block = ifp->if_broot; |
| 1294 | /* |
| 1295 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 1296 | */ |
| 1297 | level = be16_to_cpu(block->bb_level); |
| 1298 | ASSERT(level > 0); |
| 1299 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
| 1300 | bno = be64_to_cpu(*pp); |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1301 | ASSERT(bno != NULLFSBLOCK); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1302 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 1303 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 1304 | /* |
| 1305 | * Go down the tree until leaf level is reached, following the first |
| 1306 | * pointer (leftmost) at each level. |
| 1307 | */ |
| 1308 | while (level-- > 0) { |
| 1309 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1310 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1311 | if (error) |
| 1312 | return error; |
| 1313 | block = XFS_BUF_TO_BLOCK(bp); |
| 1314 | XFS_WANT_CORRUPTED_GOTO( |
| 1315 | xfs_bmap_sanity_check(mp, bp, level), |
| 1316 | error0); |
| 1317 | if (level == 0) |
| 1318 | break; |
| 1319 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
| 1320 | bno = be64_to_cpu(*pp); |
| 1321 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
| 1322 | xfs_trans_brelse(tp, bp); |
| 1323 | } |
| 1324 | /* |
| 1325 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 1326 | */ |
| 1327 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1328 | i = 0; |
| 1329 | /* |
| 1330 | * Loop over all leaf nodes. Copy information to the extent records. |
| 1331 | */ |
| 1332 | for (;;) { |
| 1333 | xfs_bmbt_rec_t *frp; |
| 1334 | xfs_fsblock_t nextbno; |
| 1335 | xfs_extnum_t num_recs; |
| 1336 | xfs_extnum_t start; |
| 1337 | |
| 1338 | num_recs = xfs_btree_get_numrecs(block); |
| 1339 | if (unlikely(i + num_recs > room)) { |
| 1340 | ASSERT(i + num_recs <= room); |
| 1341 | xfs_warn(ip->i_mount, |
| 1342 | "corrupt dinode %Lu, (btree extents).", |
| 1343 | (unsigned long long) ip->i_ino); |
| 1344 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 1345 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
| 1346 | goto error0; |
| 1347 | } |
| 1348 | XFS_WANT_CORRUPTED_GOTO( |
| 1349 | xfs_bmap_sanity_check(mp, bp, 0), |
| 1350 | error0); |
| 1351 | /* |
| 1352 | * Read-ahead the next leaf block, if any. |
| 1353 | */ |
| 1354 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
| 1355 | if (nextbno != NULLFSBLOCK) |
| 1356 | xfs_btree_reada_bufl(mp, nextbno, 1, |
| 1357 | &xfs_bmbt_buf_ops); |
| 1358 | /* |
| 1359 | * Copy records into the extent records. |
| 1360 | */ |
| 1361 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
| 1362 | start = i; |
| 1363 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 1364 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
| 1365 | trp->l0 = be64_to_cpu(frp->l0); |
| 1366 | trp->l1 = be64_to_cpu(frp->l1); |
| 1367 | } |
| 1368 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 1369 | /* |
| 1370 | * Check all attribute bmap btree records and |
| 1371 | * any "older" data bmap btree records for a |
| 1372 | * set bit in the "extent flag" position. |
| 1373 | */ |
| 1374 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 1375 | start, num_recs))) { |
| 1376 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 1377 | XFS_ERRLEVEL_LOW, |
| 1378 | ip->i_mount); |
| 1379 | goto error0; |
| 1380 | } |
| 1381 | } |
| 1382 | xfs_trans_brelse(tp, bp); |
| 1383 | bno = nextbno; |
| 1384 | /* |
| 1385 | * If we've reached the end, stop. |
| 1386 | */ |
| 1387 | if (bno == NULLFSBLOCK) |
| 1388 | break; |
| 1389 | error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 1390 | XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops); |
| 1391 | if (error) |
| 1392 | return error; |
| 1393 | block = XFS_BUF_TO_BLOCK(bp); |
| 1394 | } |
| 1395 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 1396 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 1397 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
| 1398 | return 0; |
| 1399 | error0: |
| 1400 | xfs_trans_brelse(tp, bp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1401 | return -EFSCORRUPTED; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | /* |
| 1406 | * Search the extent records for the entry containing block bno. |
| 1407 | * If bno lies in a hole, point to the next entry. If bno lies |
| 1408 | * past eof, *eofp will be set, and *prevp will contain the last |
| 1409 | * entry (null if none). Else, *lastxp will be set to the index |
| 1410 | * of the found entry; *gotp will contain the entry. |
| 1411 | */ |
| 1412 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1413 | xfs_bmap_search_multi_extents( |
| 1414 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 1415 | xfs_fileoff_t bno, /* block number searched for */ |
| 1416 | int *eofp, /* out: end of file found */ |
| 1417 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1418 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1419 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1420 | { |
| 1421 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1422 | xfs_extnum_t lastx; /* last extent index */ |
| 1423 | |
| 1424 | /* |
| 1425 | * Initialize the extent entry structure to catch access to |
| 1426 | * uninitialized br_startblock field. |
| 1427 | */ |
| 1428 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 1429 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 1430 | gotp->br_state = XFS_EXT_INVALID; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1431 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1432 | prevp->br_startoff = NULLFILEOFF; |
| 1433 | |
| 1434 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 1435 | if (lastx > 0) { |
| 1436 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 1437 | } |
| 1438 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 1439 | xfs_bmbt_get_all(ep, gotp); |
| 1440 | *eofp = 0; |
| 1441 | } else { |
| 1442 | if (lastx > 0) { |
| 1443 | *gotp = *prevp; |
| 1444 | } |
| 1445 | *eofp = 1; |
| 1446 | ep = NULL; |
| 1447 | } |
| 1448 | *lastxp = lastx; |
| 1449 | return ep; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * Search the extents list for the inode, for the extent containing bno. |
| 1454 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 1455 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 1456 | * Else, *lastxp will be set to the index of the found |
| 1457 | * entry; *gotp will contain the entry. |
| 1458 | */ |
| 1459 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
| 1460 | xfs_bmap_search_extents( |
| 1461 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1462 | xfs_fileoff_t bno, /* block number searched for */ |
| 1463 | int fork, /* data or attr fork */ |
| 1464 | int *eofp, /* out: end of file found */ |
| 1465 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 1466 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 1467 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 1468 | { |
| 1469 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1470 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
| 1471 | |
| 1472 | XFS_STATS_INC(xs_look_exlist); |
| 1473 | ifp = XFS_IFORK_PTR(ip, fork); |
| 1474 | |
| 1475 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 1476 | |
| 1477 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 1478 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 1479 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
| 1480 | "Access to block zero in inode %llu " |
| 1481 | "start_block: %llx start_off: %llx " |
Eric Sandeen | 08e96e1 | 2013-10-11 20:59:05 -0500 | [diff] [blame] | 1482 | "blkcnt: %llx extent-state: %x lastx: %x", |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1483 | (unsigned long long)ip->i_ino, |
| 1484 | (unsigned long long)gotp->br_startblock, |
| 1485 | (unsigned long long)gotp->br_startoff, |
| 1486 | (unsigned long long)gotp->br_blockcount, |
| 1487 | gotp->br_state, *lastxp); |
| 1488 | *lastxp = NULLEXTNUM; |
| 1489 | *eofp = 1; |
| 1490 | return NULL; |
| 1491 | } |
| 1492 | return ep; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * Returns the file-relative block number of the first unused block(s) |
| 1497 | * in the file with at least "len" logically contiguous blocks free. |
| 1498 | * This is the lowest-address hole if the file has holes, else the first block |
| 1499 | * past the end of file. |
| 1500 | * Return 0 if the file is currently local (in-inode). |
| 1501 | */ |
| 1502 | int /* error */ |
| 1503 | xfs_bmap_first_unused( |
| 1504 | xfs_trans_t *tp, /* transaction pointer */ |
| 1505 | xfs_inode_t *ip, /* incore inode */ |
| 1506 | xfs_extlen_t len, /* size of hole to find */ |
| 1507 | xfs_fileoff_t *first_unused, /* unused block */ |
| 1508 | int whichfork) /* data or attr fork */ |
| 1509 | { |
| 1510 | int error; /* error return value */ |
| 1511 | int idx; /* extent record index */ |
| 1512 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1513 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 1514 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 1515 | xfs_fileoff_t max; /* starting useful block */ |
| 1516 | xfs_fileoff_t off; /* offset for this block */ |
| 1517 | xfs_extnum_t nextents; /* number of extent entries */ |
| 1518 | |
| 1519 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 1520 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 1521 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 1522 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1523 | *first_unused = 0; |
| 1524 | return 0; |
| 1525 | } |
| 1526 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1527 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1528 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1529 | return error; |
| 1530 | lowest = *first_unused; |
| 1531 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 1532 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 1533 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
| 1534 | off = xfs_bmbt_get_startoff(ep); |
| 1535 | /* |
| 1536 | * See if the hole before this extent will work. |
| 1537 | */ |
| 1538 | if (off >= lowest + len && off - max >= len) { |
| 1539 | *first_unused = max; |
| 1540 | return 0; |
| 1541 | } |
| 1542 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 1543 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 1544 | } |
| 1545 | *first_unused = max; |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
Zhi Yong Wu | 02bb487 | 2013-08-12 03:14:54 +0000 | [diff] [blame] | 1550 | * Returns the file-relative block number of the last block - 1 before |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1551 | * last_block (input value) in the file. |
| 1552 | * This is not based on i_size, it is based on the extent records. |
| 1553 | * Returns 0 for local files, as they do not have extent records. |
| 1554 | */ |
| 1555 | int /* error */ |
| 1556 | xfs_bmap_last_before( |
| 1557 | xfs_trans_t *tp, /* transaction pointer */ |
| 1558 | xfs_inode_t *ip, /* incore inode */ |
| 1559 | xfs_fileoff_t *last_block, /* last block */ |
| 1560 | int whichfork) /* data or attr fork */ |
| 1561 | { |
| 1562 | xfs_fileoff_t bno; /* input file offset */ |
| 1563 | int eof; /* hit end of file */ |
| 1564 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
| 1565 | int error; /* error return value */ |
| 1566 | xfs_bmbt_irec_t got; /* current extent value */ |
| 1567 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1568 | xfs_extnum_t lastx; /* last extent used */ |
| 1569 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 1570 | |
| 1571 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1572 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 1573 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1574 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1575 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 1576 | *last_block = 0; |
| 1577 | return 0; |
| 1578 | } |
| 1579 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1580 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 1581 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 1582 | return error; |
| 1583 | bno = *last_block - 1; |
| 1584 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 1585 | &prev); |
| 1586 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 1587 | if (prev.br_startoff == NULLFILEOFF) |
| 1588 | *last_block = 0; |
| 1589 | else |
| 1590 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 1591 | } |
| 1592 | /* |
| 1593 | * Otherwise *last_block is already the right answer. |
| 1594 | */ |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 1598 | int |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1599 | xfs_bmap_last_extent( |
| 1600 | struct xfs_trans *tp, |
| 1601 | struct xfs_inode *ip, |
| 1602 | int whichfork, |
| 1603 | struct xfs_bmbt_irec *rec, |
| 1604 | int *is_empty) |
| 1605 | { |
| 1606 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1607 | int error; |
| 1608 | int nextents; |
| 1609 | |
| 1610 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 1611 | error = xfs_iread_extents(tp, ip, whichfork); |
| 1612 | if (error) |
| 1613 | return error; |
| 1614 | } |
| 1615 | |
| 1616 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 1617 | if (nextents == 0) { |
| 1618 | *is_empty = 1; |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 1623 | *is_empty = 0; |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * Check the last inode extent to determine whether this allocation will result |
| 1629 | * in blocks being allocated at the end of the file. When we allocate new data |
| 1630 | * blocks at the end of the file which do not start at the previous data block, |
| 1631 | * we will try to align the new blocks at stripe unit boundaries. |
| 1632 | * |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1633 | * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1634 | * at, or past the EOF. |
| 1635 | */ |
| 1636 | STATIC int |
| 1637 | xfs_bmap_isaeof( |
| 1638 | struct xfs_bmalloca *bma, |
| 1639 | int whichfork) |
| 1640 | { |
| 1641 | struct xfs_bmbt_irec rec; |
| 1642 | int is_empty; |
| 1643 | int error; |
| 1644 | |
| 1645 | bma->aeof = 0; |
| 1646 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 1647 | &is_empty); |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1648 | if (error) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1649 | return error; |
| 1650 | |
Dave Chinner | 6e708bc | 2013-11-22 10:41:16 +1100 | [diff] [blame] | 1651 | if (is_empty) { |
| 1652 | bma->aeof = 1; |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1656 | /* |
| 1657 | * Check if we are allocation or past the last extent, or at least into |
| 1658 | * the last delayed allocated extent. |
| 1659 | */ |
| 1660 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 1661 | (bma->offset >= rec.br_startoff && |
| 1662 | isnullstartblock(rec.br_startblock)); |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1667 | * Returns the file-relative block number of the first block past eof in |
| 1668 | * the file. This is not based on i_size, it is based on the extent records. |
| 1669 | * Returns 0 for local files, as they do not have extent records. |
| 1670 | */ |
| 1671 | int |
| 1672 | xfs_bmap_last_offset( |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1673 | struct xfs_inode *ip, |
| 1674 | xfs_fileoff_t *last_block, |
| 1675 | int whichfork) |
| 1676 | { |
| 1677 | struct xfs_bmbt_irec rec; |
| 1678 | int is_empty; |
| 1679 | int error; |
| 1680 | |
| 1681 | *last_block = 0; |
| 1682 | |
| 1683 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 1684 | return 0; |
| 1685 | |
| 1686 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 1687 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1688 | return -EIO; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 1689 | |
| 1690 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 1691 | if (error || is_empty) |
| 1692 | return error; |
| 1693 | |
| 1694 | *last_block = rec.br_startoff + rec.br_blockcount; |
| 1695 | return 0; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Returns whether the selected fork of the inode has exactly one |
| 1700 | * block or not. For the data fork we check this matches di_size, |
| 1701 | * implying the file's range is 0..bsize-1. |
| 1702 | */ |
| 1703 | int /* 1=>1 block, 0=>otherwise */ |
| 1704 | xfs_bmap_one_block( |
| 1705 | xfs_inode_t *ip, /* incore inode */ |
| 1706 | int whichfork) /* data or attr fork */ |
| 1707 | { |
| 1708 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
| 1709 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1710 | int rval; /* return value */ |
| 1711 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 1712 | |
| 1713 | #ifndef DEBUG |
| 1714 | if (whichfork == XFS_DATA_FORK) |
| 1715 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
| 1716 | #endif /* !DEBUG */ |
| 1717 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 1718 | return 0; |
| 1719 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 1720 | return 0; |
| 1721 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1722 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 1723 | ep = xfs_iext_get_ext(ifp, 0); |
| 1724 | xfs_bmbt_get_all(ep, &s); |
| 1725 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 1726 | if (rval && whichfork == XFS_DATA_FORK) |
| 1727 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
| 1728 | return rval; |
| 1729 | } |
| 1730 | |
| 1731 | /* |
| 1732 | * Extent tree manipulation functions used during allocation. |
| 1733 | */ |
| 1734 | |
| 1735 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1736 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | */ |
| 1738 | STATIC int /* error */ |
| 1739 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1740 | struct xfs_bmalloca *bma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1742 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1744 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1747 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1749 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1750 | /* left is 0, right is 1, prev is 2 */ |
| 1751 | int rval=0; /* return value (logging flags) */ |
| 1752 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1753 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 1754 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 1755 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 1756 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1759 | ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1760 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1761 | ASSERT(bma->idx >= 0); |
| 1762 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1763 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1764 | ASSERT(!bma->cur || |
| 1765 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1766 | |
| 1767 | XFS_STATS_INC(xs_add_exlist); |
| 1768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | #define LEFT r[0] |
| 1770 | #define RIGHT r[1] |
| 1771 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
| 1773 | /* |
| 1774 | * Set up a bunch of variables to make the tests simpler. |
| 1775 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1776 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | xfs_bmbt_get_all(ep, &PREV); |
| 1778 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1779 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1780 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1781 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1782 | da_old = startblockval(PREV.br_startblock); |
| 1783 | da_new = 0; |
| 1784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | /* |
| 1786 | * Set flags determining what part of the previous delayed allocation |
| 1787 | * extent is being replaced by a real allocation. |
| 1788 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1789 | if (PREV.br_startoff == new->br_startoff) |
| 1790 | state |= BMAP_LEFT_FILLING; |
| 1791 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1792 | state |= BMAP_RIGHT_FILLING; |
| 1793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | /* |
| 1795 | * Check and set flags if this segment has a left neighbor. |
| 1796 | * Don't set contiguous if the combined extent would be too large. |
| 1797 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1798 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1799 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1800 | 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] | 1801 | |
| 1802 | if (isnullstartblock(LEFT.br_startblock)) |
| 1803 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1805 | |
| 1806 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1807 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1808 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1809 | LEFT.br_state == new->br_state && |
| 1810 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1811 | state |= BMAP_LEFT_CONTIG; |
| 1812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | /* |
| 1814 | * Check and set flags if this segment has a right neighbor. |
| 1815 | * Don't set contiguous if the combined extent would be too large. |
| 1816 | * Also check for all-three-contiguous being too large. |
| 1817 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1818 | 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] | 1819 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1820 | 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] | 1821 | |
| 1822 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1823 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1825 | |
| 1826 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1827 | new_endoff == RIGHT.br_startoff && |
| 1828 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1829 | new->br_state == RIGHT.br_state && |
| 1830 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1831 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1832 | BMAP_RIGHT_FILLING)) != |
| 1833 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1834 | BMAP_RIGHT_FILLING) || |
| 1835 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1836 | <= MAXEXTLEN)) |
| 1837 | state |= BMAP_RIGHT_CONTIG; |
| 1838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | error = 0; |
| 1840 | /* |
| 1841 | * Switch out based on the FILLING and CONTIG state bits. |
| 1842 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1843 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1844 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1845 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1846 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | /* |
| 1848 | * Filling in all of a previously delayed allocation extent. |
| 1849 | * The left and right neighbors are both contiguous with new. |
| 1850 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1851 | bma->idx--; |
| 1852 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1853 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1855 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1856 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1857 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1858 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
| 1859 | bma->ip->i_d.di_nextents--; |
| 1860 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1862 | else { |
| 1863 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1864 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1866 | RIGHT.br_blockcount, &i); |
| 1867 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1869 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1870 | error = xfs_btree_delete(bma->cur, &i); |
| 1871 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1873 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1874 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1875 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1877 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1878 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | LEFT.br_startblock, |
| 1880 | LEFT.br_blockcount + |
| 1881 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1882 | RIGHT.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_LEFT_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 left neighbor is contiguous, the right is not. |
| 1892 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1893 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1894 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1895 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1896 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1897 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1898 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1899 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1900 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1901 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | rval = XFS_ILOG_DEXT; |
| 1903 | else { |
| 1904 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1905 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1907 | &i); |
| 1908 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1910 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1911 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | LEFT.br_startblock, |
| 1913 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1914 | PREV.br_blockcount, LEFT.br_state); |
| 1915 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | goto done; |
| 1917 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | break; |
| 1919 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1920 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | /* |
| 1922 | * Filling in all of a previously delayed allocation extent. |
| 1923 | * The right neighbor is contiguous, the left is not. |
| 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); |
| 1927 | xfs_bmbt_set_blockcount(ep, |
| 1928 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1929 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1930 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1931 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 1932 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | rval = XFS_ILOG_DEXT; |
| 1934 | else { |
| 1935 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1936 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1938 | RIGHT.br_blockcount, &i); |
| 1939 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1941 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1942 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | new->br_startblock, |
| 1944 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1945 | RIGHT.br_blockcount, PREV.br_state); |
| 1946 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | goto done; |
| 1948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | break; |
| 1950 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1951 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | /* |
| 1953 | * Filling in all of a previously delayed allocation extent. |
| 1954 | * Neither the left nor right neighbors are contiguous with |
| 1955 | * the new one. |
| 1956 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1957 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1959 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1960 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1961 | bma->ip->i_d.di_nextents++; |
| 1962 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1964 | else { |
| 1965 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1966 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1968 | &i); |
| 1969 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1971 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1972 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1973 | error = xfs_btree_insert(bma->cur, &i); |
| 1974 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1976 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | break; |
| 1979 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1980 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | /* |
| 1982 | * Filling in the first part of a previous delayed allocation. |
| 1983 | * The left neighbor is contiguous. |
| 1984 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1985 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 1986 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | LEFT.br_blockcount + new->br_blockcount); |
| 1988 | xfs_bmbt_set_startoff(ep, |
| 1989 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1990 | 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] | 1991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1993 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1995 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | rval = XFS_ILOG_DEXT; |
| 1997 | else { |
| 1998 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 1999 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2001 | &i); |
| 2002 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2004 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2005 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | LEFT.br_startblock, |
| 2007 | LEFT.br_blockcount + |
| 2008 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2009 | LEFT.br_state); |
| 2010 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | goto done; |
| 2012 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2013 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2014 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2015 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2016 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2017 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2018 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | break; |
| 2020 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2021 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | /* |
| 2023 | * Filling in the first part of a previous delayed allocation. |
| 2024 | * The left neighbor is not contiguous. |
| 2025 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2026 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2028 | temp = PREV.br_blockcount - new->br_blockcount; |
| 2029 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2030 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 2031 | bma->ip->i_d.di_nextents++; |
| 2032 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2034 | else { |
| 2035 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2036 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2038 | &i); |
| 2039 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2041 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2042 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2043 | error = xfs_btree_insert(bma->cur, &i); |
| 2044 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2046 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2048 | |
| 2049 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2050 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2051 | bma->firstblock, bma->flist, |
| 2052 | &bma->cur, 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | rval |= tmp_rval; |
| 2054 | if (error) |
| 2055 | goto done; |
| 2056 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2057 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2058 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2059 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2060 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2061 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2062 | 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] | 2063 | break; |
| 2064 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2065 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | /* |
| 2067 | * Filling in the last part of a previous delayed allocation. |
| 2068 | * The right neighbor is contiguous with the new allocation. |
| 2069 | */ |
| 2070 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2071 | 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] | 2072 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2073 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2074 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | new->br_blockcount + RIGHT.br_blockcount, |
| 2076 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2077 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 2078 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | rval = XFS_ILOG_DEXT; |
| 2080 | else { |
| 2081 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2082 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2084 | RIGHT.br_blockcount, &i); |
| 2085 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2087 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2088 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | new->br_startblock, |
| 2090 | new->br_blockcount + |
| 2091 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2092 | RIGHT.br_state); |
| 2093 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | goto done; |
| 2095 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2096 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2097 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2098 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2099 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2100 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2101 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2102 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2103 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | break; |
| 2105 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2106 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | /* |
| 2108 | * Filling in the last part of a previous delayed allocation. |
| 2109 | * The right neighbor is not contiguous. |
| 2110 | */ |
| 2111 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2112 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2114 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
| 2115 | bma->ip->i_d.di_nextents++; |
| 2116 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2118 | else { |
| 2119 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2120 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2122 | &i); |
| 2123 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2125 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2126 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2127 | error = xfs_btree_insert(bma->cur, &i); |
| 2128 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2130 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2132 | |
| 2133 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2134 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2135 | bma->firstblock, bma->flist, &bma->cur, 1, |
| 2136 | &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | rval |= tmp_rval; |
| 2138 | if (error) |
| 2139 | goto done; |
| 2140 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2141 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2142 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2143 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 2144 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2145 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2146 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2147 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2148 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | break; |
| 2150 | |
| 2151 | case 0: |
| 2152 | /* |
| 2153 | * Filling in the middle part of a previous delayed allocation. |
| 2154 | * Contiguity is impossible here. |
| 2155 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2156 | * |
| 2157 | * We start with a delayed allocation: |
| 2158 | * |
| 2159 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 2160 | * PREV @ idx |
| 2161 | * |
| 2162 | * and we are allocating: |
| 2163 | * +rrrrrrrrrrrrrrrrr+ |
| 2164 | * new |
| 2165 | * |
| 2166 | * and we set it up for insertion as: |
| 2167 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 2168 | * new |
| 2169 | * PREV @ idx LEFT RIGHT |
| 2170 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | */ |
| 2172 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2174 | 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] | 2175 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 2176 | LEFT = *new; |
| 2177 | RIGHT.br_state = PREV.br_state; |
| 2178 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2179 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 2180 | RIGHT.br_startoff = new_endoff; |
| 2181 | RIGHT.br_blockcount = temp2; |
| 2182 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2183 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
| 2184 | bma->ip->i_d.di_nextents++; |
| 2185 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2187 | else { |
| 2188 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2189 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2191 | &i); |
| 2192 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2194 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2195 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 2196 | error = xfs_btree_insert(bma->cur, &i); |
| 2197 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2199 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2203 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2204 | bma->firstblock, bma->flist, &bma->cur, |
| 2205 | 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | rval |= tmp_rval; |
| 2207 | if (error) |
| 2208 | goto done; |
| 2209 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2210 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 2211 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2212 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2213 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2214 | if (diff > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 2215 | error = xfs_mod_fdblocks(bma->ip->i_mount, |
| 2216 | -((int64_t)diff), false); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2217 | ASSERT(!error); |
| 2218 | if (error) |
| 2219 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 2221 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2222 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2223 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2224 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 2225 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 2226 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2227 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2228 | 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] | 2229 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2230 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2231 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | break; |
| 2233 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2234 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2235 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2236 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2237 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2238 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2239 | case BMAP_LEFT_CONTIG: |
| 2240 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | /* |
| 2242 | * These cases are all impossible. |
| 2243 | */ |
| 2244 | ASSERT(0); |
| 2245 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2246 | |
| 2247 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2248 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2249 | int tmp_logflags; /* partial log flag return val */ |
| 2250 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2251 | ASSERT(bma->cur == NULL); |
| 2252 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 2253 | bma->firstblock, bma->flist, &bma->cur, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2254 | da_old > 0, &tmp_logflags, XFS_DATA_FORK); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2255 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2256 | if (error) |
| 2257 | goto done; |
| 2258 | } |
| 2259 | |
| 2260 | /* adjust for changes in reserved delayed indirect blocks */ |
| 2261 | if (da_old || da_new) { |
| 2262 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2263 | if (bma->cur) |
| 2264 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2265 | ASSERT(temp <= da_old); |
| 2266 | if (temp < da_old) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 2267 | xfs_mod_fdblocks(bma->ip->i_mount, |
| 2268 | (int64_t)(da_old - temp), false); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2272 | if (bma->cur) |
| 2273 | bma->cur->bc_private.b.allocated = 0; |
| 2274 | |
| 2275 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | done: |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 2277 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | return error; |
| 2279 | #undef LEFT |
| 2280 | #undef RIGHT |
| 2281 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2285 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | */ |
| 2287 | STATIC int /* error */ |
| 2288 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2289 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2291 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2293 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2294 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 2295 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 2296 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2299 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2302 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 2304 | xfs_exntst_t newext; /* new extent state */ |
| 2305 | xfs_exntst_t oldext; /* old extent state */ |
| 2306 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 2307 | /* left is 0, right is 1, prev is 2 */ |
| 2308 | int rval=0; /* return value (logging flags) */ |
| 2309 | int state = 0;/* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2311 | *logflagsp = 0; |
| 2312 | |
| 2313 | cur = *curp; |
| 2314 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 2315 | |
| 2316 | ASSERT(*idx >= 0); |
| 2317 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 2318 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 2319 | |
| 2320 | XFS_STATS_INC(xs_add_exlist); |
| 2321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | #define LEFT r[0] |
| 2323 | #define RIGHT r[1] |
| 2324 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | /* |
| 2327 | * Set up a bunch of variables to make the tests simpler. |
| 2328 | */ |
| 2329 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2330 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | xfs_bmbt_get_all(ep, &PREV); |
| 2332 | newext = new->br_state; |
| 2333 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 2334 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 2335 | ASSERT(PREV.br_state == oldext); |
| 2336 | new_endoff = new->br_startoff + new->br_blockcount; |
| 2337 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 2338 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | /* |
| 2341 | * Set flags determining what part of the previous oldext allocation |
| 2342 | * extent is being replaced by a newext allocation. |
| 2343 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2344 | if (PREV.br_startoff == new->br_startoff) |
| 2345 | state |= BMAP_LEFT_FILLING; |
| 2346 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 2347 | state |= BMAP_RIGHT_FILLING; |
| 2348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | /* |
| 2350 | * Check and set flags if this segment has a left neighbor. |
| 2351 | * Don't set contiguous if the combined extent would be too large. |
| 2352 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2353 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2354 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2355 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2356 | |
| 2357 | if (isnullstartblock(LEFT.br_startblock)) |
| 2358 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2360 | |
| 2361 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 2362 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 2363 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 2364 | LEFT.br_state == newext && |
| 2365 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2366 | state |= BMAP_LEFT_CONTIG; |
| 2367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | /* |
| 2369 | * Check and set flags if this segment has a right neighbor. |
| 2370 | * Don't set contiguous if the combined extent would be too large. |
| 2371 | * Also check for all-three-contiguous being too large. |
| 2372 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2373 | 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] | 2374 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2375 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2376 | if (isnullstartblock(RIGHT.br_startblock)) |
| 2377 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2379 | |
| 2380 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 2381 | new_endoff == RIGHT.br_startoff && |
| 2382 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 2383 | newext == RIGHT.br_state && |
| 2384 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 2385 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2386 | BMAP_RIGHT_FILLING)) != |
| 2387 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 2388 | BMAP_RIGHT_FILLING) || |
| 2389 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 2390 | <= MAXEXTLEN)) |
| 2391 | state |= BMAP_RIGHT_CONTIG; |
| 2392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | /* |
| 2394 | * Switch out based on the FILLING and CONTIG state bits. |
| 2395 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2396 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2397 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 2398 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 2399 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | /* |
| 2401 | * Setting all of a previous oldext extent to newext. |
| 2402 | * The left and right neighbors are both contiguous with new. |
| 2403 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2404 | --*idx; |
| 2405 | |
| 2406 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2407 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2409 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2410 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2411 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2412 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | ip->i_d.di_nextents -= 2; |
| 2414 | if (cur == NULL) |
| 2415 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2416 | else { |
| 2417 | rval = XFS_ILOG_CORE; |
| 2418 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2419 | RIGHT.br_startblock, |
| 2420 | RIGHT.br_blockcount, &i))) |
| 2421 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2422 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2423 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2425 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2426 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2428 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2429 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2431 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2432 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2434 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2436 | LEFT.br_startblock, |
| 2437 | LEFT.br_blockcount + PREV.br_blockcount + |
| 2438 | RIGHT.br_blockcount, LEFT.br_state))) |
| 2439 | goto done; |
| 2440 | } |
| 2441 | break; |
| 2442 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2443 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | /* |
| 2445 | * Setting all of a previous oldext extent to newext. |
| 2446 | * The left neighbor is contiguous, the right is not. |
| 2447 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2448 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2449 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2450 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2451 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 2452 | LEFT.br_blockcount + PREV.br_blockcount); |
| 2453 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2454 | |
| 2455 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | ip->i_d.di_nextents--; |
| 2457 | if (cur == NULL) |
| 2458 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2459 | else { |
| 2460 | rval = XFS_ILOG_CORE; |
| 2461 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2462 | PREV.br_startblock, PREV.br_blockcount, |
| 2463 | &i))) |
| 2464 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2465 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2466 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2468 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2469 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2471 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 2473 | LEFT.br_startblock, |
| 2474 | LEFT.br_blockcount + PREV.br_blockcount, |
| 2475 | LEFT.br_state))) |
| 2476 | goto done; |
| 2477 | } |
| 2478 | break; |
| 2479 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2480 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | /* |
| 2482 | * Setting all of a previous oldext extent to newext. |
| 2483 | * The right neighbor is contiguous, the left is not. |
| 2484 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2485 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | xfs_bmbt_set_blockcount(ep, |
| 2487 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 2488 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2489 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2490 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | ip->i_d.di_nextents--; |
| 2492 | if (cur == NULL) |
| 2493 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2494 | else { |
| 2495 | rval = XFS_ILOG_CORE; |
| 2496 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 2497 | RIGHT.br_startblock, |
| 2498 | RIGHT.br_blockcount, &i))) |
| 2499 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2500 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2501 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2503 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2504 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2506 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2508 | new->br_startblock, |
| 2509 | new->br_blockcount + RIGHT.br_blockcount, |
| 2510 | newext))) |
| 2511 | goto done; |
| 2512 | } |
| 2513 | break; |
| 2514 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2515 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | /* |
| 2517 | * Setting all of a previous oldext extent to newext. |
| 2518 | * Neither the left nor right neighbors are contiguous with |
| 2519 | * the new one. |
| 2520 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2521 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2523 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | if (cur == NULL) |
| 2526 | rval = XFS_ILOG_DEXT; |
| 2527 | else { |
| 2528 | rval = 0; |
| 2529 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2530 | new->br_startblock, new->br_blockcount, |
| 2531 | &i))) |
| 2532 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2533 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2535 | new->br_startblock, new->br_blockcount, |
| 2536 | newext))) |
| 2537 | goto done; |
| 2538 | } |
| 2539 | break; |
| 2540 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2541 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | /* |
| 2543 | * Setting the first part of a previous oldext extent to newext. |
| 2544 | * The left neighbor is contiguous. |
| 2545 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2546 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 2547 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | LEFT.br_blockcount + new->br_blockcount); |
| 2549 | xfs_bmbt_set_startoff(ep, |
| 2550 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2551 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2552 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2553 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | xfs_bmbt_set_startblock(ep, |
| 2555 | new->br_startblock + new->br_blockcount); |
| 2556 | xfs_bmbt_set_blockcount(ep, |
| 2557 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2558 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2559 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2560 | --*idx; |
| 2561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | if (cur == NULL) |
| 2563 | rval = XFS_ILOG_DEXT; |
| 2564 | else { |
| 2565 | rval = 0; |
| 2566 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2567 | PREV.br_startblock, PREV.br_blockcount, |
| 2568 | &i))) |
| 2569 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2570 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | if ((error = xfs_bmbt_update(cur, |
| 2572 | PREV.br_startoff + new->br_blockcount, |
| 2573 | PREV.br_startblock + new->br_blockcount, |
| 2574 | PREV.br_blockcount - new->br_blockcount, |
| 2575 | oldext))) |
| 2576 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2577 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2579 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | LEFT.br_startblock, |
| 2581 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 2582 | LEFT.br_state); |
| 2583 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | goto done; |
| 2585 | } |
| 2586 | break; |
| 2587 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2588 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | /* |
| 2590 | * Setting the first part of a previous oldext extent to newext. |
| 2591 | * The left neighbor is not contiguous. |
| 2592 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2593 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 2595 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 2596 | xfs_bmbt_set_blockcount(ep, |
| 2597 | PREV.br_blockcount - new->br_blockcount); |
| 2598 | xfs_bmbt_set_startblock(ep, |
| 2599 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2600 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2601 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2602 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | ip->i_d.di_nextents++; |
| 2604 | if (cur == NULL) |
| 2605 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2606 | else { |
| 2607 | rval = XFS_ILOG_CORE; |
| 2608 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2609 | PREV.br_startblock, PREV.br_blockcount, |
| 2610 | &i))) |
| 2611 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2612 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | if ((error = xfs_bmbt_update(cur, |
| 2614 | PREV.br_startoff + new->br_blockcount, |
| 2615 | PREV.br_startblock + new->br_blockcount, |
| 2616 | PREV.br_blockcount - new->br_blockcount, |
| 2617 | oldext))) |
| 2618 | goto done; |
| 2619 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2620 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2622 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | } |
| 2624 | break; |
| 2625 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2626 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | /* |
| 2628 | * Setting the last part of a previous oldext extent to newext. |
| 2629 | * The right neighbor is contiguous with the new allocation. |
| 2630 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2631 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | xfs_bmbt_set_blockcount(ep, |
| 2633 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2634 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2635 | |
| 2636 | ++*idx; |
| 2637 | |
| 2638 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2639 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2640 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2642 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | if (cur == NULL) |
| 2645 | rval = XFS_ILOG_DEXT; |
| 2646 | else { |
| 2647 | rval = 0; |
| 2648 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2649 | PREV.br_startblock, |
| 2650 | PREV.br_blockcount, &i))) |
| 2651 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2652 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2654 | PREV.br_startblock, |
| 2655 | PREV.br_blockcount - new->br_blockcount, |
| 2656 | oldext))) |
| 2657 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2658 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | goto done; |
| 2660 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2661 | new->br_startblock, |
| 2662 | new->br_blockcount + RIGHT.br_blockcount, |
| 2663 | newext))) |
| 2664 | goto done; |
| 2665 | } |
| 2666 | break; |
| 2667 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2668 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | /* |
| 2670 | * Setting the last part of a previous oldext extent to newext. |
| 2671 | * The right neighbor is not contiguous. |
| 2672 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2673 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | xfs_bmbt_set_blockcount(ep, |
| 2675 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2676 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2677 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2678 | ++*idx; |
| 2679 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 2680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | ip->i_d.di_nextents++; |
| 2682 | if (cur == NULL) |
| 2683 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2684 | else { |
| 2685 | rval = XFS_ILOG_CORE; |
| 2686 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2687 | PREV.br_startblock, PREV.br_blockcount, |
| 2688 | &i))) |
| 2689 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2690 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 2692 | PREV.br_startblock, |
| 2693 | PREV.br_blockcount - new->br_blockcount, |
| 2694 | oldext))) |
| 2695 | goto done; |
| 2696 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2697 | new->br_startblock, new->br_blockcount, |
| 2698 | &i))) |
| 2699 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2700 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2702 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2704 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | } |
| 2706 | break; |
| 2707 | |
| 2708 | case 0: |
| 2709 | /* |
| 2710 | * Setting the middle part of a previous oldext extent to |
| 2711 | * newext. Contiguity is impossible here. |
| 2712 | * One extent becomes three extents. |
| 2713 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2714 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | xfs_bmbt_set_blockcount(ep, |
| 2716 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2717 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | r[0] = *new; |
| 2720 | r[1].br_startoff = new_endoff; |
| 2721 | r[1].br_blockcount = |
| 2722 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 2723 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 2724 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2725 | |
| 2726 | ++*idx; |
| 2727 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 2728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | ip->i_d.di_nextents += 2; |
| 2730 | if (cur == NULL) |
| 2731 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 2732 | else { |
| 2733 | rval = XFS_ILOG_CORE; |
| 2734 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 2735 | PREV.br_startblock, PREV.br_blockcount, |
| 2736 | &i))) |
| 2737 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2738 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | /* new right extent - oldext */ |
| 2740 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 2741 | r[1].br_startblock, r[1].br_blockcount, |
| 2742 | r[1].br_state))) |
| 2743 | goto done; |
| 2744 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 2746 | cur->bc_rec.b.br_blockcount = |
| 2747 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2748 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2750 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2751 | /* |
| 2752 | * Reset the cursor to the position of the new extent |
| 2753 | * we are about to insert as we can't trust it after |
| 2754 | * the previous insert. |
| 2755 | */ |
| 2756 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 2757 | new->br_startblock, new->br_blockcount, |
| 2758 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2760 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 2762 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2763 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2765 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | } |
| 2767 | break; |
| 2768 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2769 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2770 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2771 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 2772 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 2773 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 2774 | case BMAP_LEFT_CONTIG: |
| 2775 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | /* |
| 2777 | * These cases are all impossible. |
| 2778 | */ |
| 2779 | ASSERT(0); |
| 2780 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2781 | |
| 2782 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 2783 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2784 | int tmp_logflags; /* partial log flag return val */ |
| 2785 | |
| 2786 | ASSERT(cur == NULL); |
| 2787 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, |
| 2788 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 2789 | *logflagsp |= tmp_logflags; |
| 2790 | if (error) |
| 2791 | goto done; |
| 2792 | } |
| 2793 | |
| 2794 | /* clear out the allocated field, done with it now in any case. */ |
| 2795 | if (cur) { |
| 2796 | cur->bc_private.b.allocated = 0; |
| 2797 | *curp = cur; |
| 2798 | } |
| 2799 | |
| 2800 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2802 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | return error; |
| 2804 | #undef LEFT |
| 2805 | #undef RIGHT |
| 2806 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2810 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2812 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | xfs_bmap_add_extent_hole_delay( |
| 2814 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2815 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 2816 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2818 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2820 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 2821 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 2822 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 2823 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2824 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2826 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2828 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | /* |
| 2831 | * Check and set flags if this segment has a left neighbor |
| 2832 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2833 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2834 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2835 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2836 | |
| 2837 | if (isnullstartblock(left.br_startblock)) |
| 2838 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | /* |
| 2842 | * Check and set flags if the current (right) segment exists. |
| 2843 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 2844 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2845 | 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] | 2846 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2847 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2848 | |
| 2849 | if (isnullstartblock(right.br_startblock)) |
| 2850 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | /* |
| 2854 | * Set contiguity flags on the left and right neighbors. |
| 2855 | * Don't let extents get too large, even if the pieces are contiguous. |
| 2856 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2857 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 2858 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2859 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 2860 | state |= BMAP_LEFT_CONTIG; |
| 2861 | |
| 2862 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 2863 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2864 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2865 | (!(state & BMAP_LEFT_CONTIG) || |
| 2866 | (left.br_blockcount + new->br_blockcount + |
| 2867 | right.br_blockcount <= MAXEXTLEN))) |
| 2868 | state |= BMAP_RIGHT_CONTIG; |
| 2869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | /* |
| 2871 | * Switch out based on the contiguity flags. |
| 2872 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2873 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 2874 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | /* |
| 2876 | * New allocation is contiguous with delayed allocations |
| 2877 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2878 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2880 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | temp = left.br_blockcount + new->br_blockcount + |
| 2882 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2883 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2884 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2885 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2886 | oldlen = startblockval(left.br_startblock) + |
| 2887 | startblockval(new->br_startblock) + |
| 2888 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2890 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2891 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2892 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2893 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2894 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | break; |
| 2896 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2897 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | /* |
| 2899 | * New allocation is contiguous with a delayed allocation |
| 2900 | * on the left. |
| 2901 | * Merge the new allocation with the left neighbor. |
| 2902 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2903 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2905 | |
| 2906 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 2907 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2908 | oldlen = startblockval(left.br_startblock) + |
| 2909 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2911 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2912 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2913 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | break; |
| 2915 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2916 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | /* |
| 2918 | * New allocation is contiguous with a delayed allocation |
| 2919 | * on the right. |
| 2920 | * Merge the new allocation with the right neighbor. |
| 2921 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2922 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2924 | oldlen = startblockval(new->br_startblock) + |
| 2925 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 2927 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 2928 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2929 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2930 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | break; |
| 2932 | |
| 2933 | case 0: |
| 2934 | /* |
| 2935 | * New allocation is not contiguous with another |
| 2936 | * delayed allocation. |
| 2937 | * Insert a new entry. |
| 2938 | */ |
| 2939 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2940 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | break; |
| 2942 | } |
| 2943 | if (oldlen != newlen) { |
| 2944 | ASSERT(oldlen > newlen); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 2945 | xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen), |
| 2946 | false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | /* |
| 2948 | * Nothing to do for disk quota accounting here. |
| 2949 | */ |
| 2950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2954 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | */ |
| 2956 | STATIC int /* error */ |
| 2957 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2958 | struct xfs_bmalloca *bma, |
| 2959 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2961 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | int i; /* temp state */ |
| 2964 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2965 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2966 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2967 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2970 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2971 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2972 | ASSERT(bma->idx >= 0); |
| 2973 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2974 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2975 | ASSERT(!bma->cur || |
| 2976 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 2977 | |
| 2978 | XFS_STATS_INC(xs_add_exlist); |
| 2979 | |
| 2980 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2981 | if (whichfork == XFS_ATTR_FORK) |
| 2982 | state |= BMAP_ATTRFORK; |
| 2983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | /* |
| 2985 | * Check and set flags if this segment has a left neighbor. |
| 2986 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2987 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2988 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2989 | 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] | 2990 | if (isnullstartblock(left.br_startblock)) |
| 2991 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | /* |
| 2995 | * Check and set flags if this segment has a current value. |
| 2996 | * Not true if we're inserting into the "hole" at eof. |
| 2997 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 2998 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2999 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3000 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3001 | if (isnullstartblock(right.br_startblock)) |
| 3002 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | /* |
| 3006 | * We're inserting a real allocation between "left" and "right". |
| 3007 | * Set the contiguity flags. Don't let extents get too large. |
| 3008 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3009 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 3010 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 3011 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 3012 | left.br_state == new->br_state && |
| 3013 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 3014 | state |= BMAP_LEFT_CONTIG; |
| 3015 | |
| 3016 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 3017 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 3018 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 3019 | new->br_state == right.br_state && |
| 3020 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 3021 | (!(state & BMAP_LEFT_CONTIG) || |
| 3022 | left.br_blockcount + new->br_blockcount + |
| 3023 | right.br_blockcount <= MAXEXTLEN)) |
| 3024 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3026 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3027 | /* |
| 3028 | * Select which case we're in here, and implement it. |
| 3029 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3030 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 3031 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | /* |
| 3033 | * New allocation is contiguous with real allocations on the |
| 3034 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3035 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3037 | --bma->idx; |
| 3038 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3039 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | left.br_blockcount + new->br_blockcount + |
| 3041 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3042 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3043 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3044 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 3045 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3046 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3047 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 3048 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3049 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3050 | } else { |
| 3051 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3052 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 3053 | right.br_startblock, right.br_blockcount, |
| 3054 | &i); |
| 3055 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3056 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3057 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3058 | error = xfs_btree_delete(bma->cur, &i); |
| 3059 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3060 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3061 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3062 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 3063 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3064 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3065 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3066 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3067 | left.br_startblock, |
| 3068 | left.br_blockcount + |
| 3069 | new->br_blockcount + |
| 3070 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3071 | left.br_state); |
| 3072 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3073 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3075 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3077 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | /* |
| 3079 | * New allocation is contiguous with a real allocation |
| 3080 | * on the left. |
| 3081 | * Merge the new allocation with the left neighbor. |
| 3082 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3083 | --bma->idx; |
| 3084 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3085 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3087 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3088 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3089 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3090 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3091 | } else { |
| 3092 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3093 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 3094 | left.br_startblock, left.br_blockcount, |
| 3095 | &i); |
| 3096 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3097 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3098 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3099 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3100 | left.br_startblock, |
| 3101 | left.br_blockcount + |
| 3102 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3103 | left.br_state); |
| 3104 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3105 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3107 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 3109 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | /* |
| 3111 | * New allocation is contiguous with a real allocation |
| 3112 | * on the right. |
| 3113 | * Merge the new allocation with the right neighbor. |
| 3114 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3115 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 3116 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 3117 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | new->br_blockcount + right.br_blockcount, |
| 3119 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3120 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3121 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3122 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3123 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3124 | } else { |
| 3125 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3126 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3127 | right.br_startoff, |
| 3128 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3129 | right.br_blockcount, &i); |
| 3130 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3131 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3132 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3133 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3134 | new->br_startblock, |
| 3135 | new->br_blockcount + |
| 3136 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3137 | right.br_state); |
| 3138 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3139 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3141 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | |
| 3143 | case 0: |
| 3144 | /* |
| 3145 | * New allocation is not contiguous with another |
| 3146 | * real allocation. |
| 3147 | * Insert a new entry. |
| 3148 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3149 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 3150 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 3151 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 3152 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3153 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3154 | } else { |
| 3155 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3156 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3157 | new->br_startoff, |
| 3158 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3159 | new->br_blockcount, &i); |
| 3160 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3161 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3162 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3163 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 3164 | error = xfs_btree_insert(bma->cur, &i); |
| 3165 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3166 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3167 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3169 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3171 | |
| 3172 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3173 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3174 | int tmp_logflags; /* partial log flag return val */ |
| 3175 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3176 | ASSERT(bma->cur == NULL); |
| 3177 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 3178 | bma->firstblock, bma->flist, &bma->cur, |
| 3179 | 0, &tmp_logflags, whichfork); |
| 3180 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 3181 | if (error) |
| 3182 | goto done; |
| 3183 | } |
| 3184 | |
| 3185 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3186 | if (bma->cur) |
| 3187 | bma->cur->bc_private.b.allocated = 0; |
| 3188 | |
| 3189 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3190 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 3191 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3192 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3195 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 3196 | * Functions used in the extent read, allocate and remove paths |
| 3197 | */ |
| 3198 | |
| 3199 | /* |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3200 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 3201 | */ |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3202 | int |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3203 | xfs_bmap_extsize_align( |
| 3204 | xfs_mount_t *mp, |
| 3205 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 3206 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 3207 | xfs_extlen_t extsz, /* align to this extent size */ |
| 3208 | int rt, /* is this a realtime inode? */ |
| 3209 | int eof, /* is extent at end-of-file? */ |
| 3210 | int delay, /* creating delalloc extent? */ |
| 3211 | int convert, /* overwriting unwritten extent? */ |
| 3212 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 3213 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 3214 | { |
| 3215 | xfs_fileoff_t orig_off; /* original offset */ |
| 3216 | xfs_extlen_t orig_alen; /* original length */ |
| 3217 | xfs_fileoff_t orig_end; /* original off+len */ |
| 3218 | xfs_fileoff_t nexto; /* next file offset */ |
| 3219 | xfs_fileoff_t prevo; /* previous file offset */ |
| 3220 | xfs_fileoff_t align_off; /* temp for offset */ |
| 3221 | xfs_extlen_t align_alen; /* temp for length */ |
| 3222 | xfs_extlen_t temp; /* temp for calculations */ |
| 3223 | |
| 3224 | if (convert) |
| 3225 | return 0; |
| 3226 | |
| 3227 | orig_off = align_off = *offp; |
| 3228 | orig_alen = align_alen = *lenp; |
| 3229 | orig_end = orig_off + orig_alen; |
| 3230 | |
| 3231 | /* |
| 3232 | * If this request overlaps an existing extent, then don't |
| 3233 | * attempt to perform any additional alignment. |
| 3234 | */ |
| 3235 | if (!delay && !eof && |
| 3236 | (orig_off >= gotp->br_startoff) && |
| 3237 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 3238 | return 0; |
| 3239 | } |
| 3240 | |
| 3241 | /* |
| 3242 | * If the file offset is unaligned vs. the extent size |
| 3243 | * we need to align it. This will be possible unless |
| 3244 | * the file was previously written with a kernel that didn't |
| 3245 | * perform this alignment, or if a truncate shot us in the |
| 3246 | * foot. |
| 3247 | */ |
| 3248 | temp = do_mod(orig_off, extsz); |
| 3249 | if (temp) { |
| 3250 | align_alen += temp; |
| 3251 | align_off -= temp; |
| 3252 | } |
| 3253 | /* |
| 3254 | * Same adjustment for the end of the requested area. |
| 3255 | */ |
| 3256 | if ((temp = (align_alen % extsz))) { |
| 3257 | align_alen += extsz - temp; |
| 3258 | } |
| 3259 | /* |
| 3260 | * If the previous block overlaps with this proposed allocation |
| 3261 | * then move the start forward without adjusting the length. |
| 3262 | */ |
| 3263 | if (prevp->br_startoff != NULLFILEOFF) { |
| 3264 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 3265 | prevo = prevp->br_startoff; |
| 3266 | else |
| 3267 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 3268 | } else |
| 3269 | prevo = 0; |
| 3270 | if (align_off != orig_off && align_off < prevo) |
| 3271 | align_off = prevo; |
| 3272 | /* |
| 3273 | * If the next block overlaps with this proposed allocation |
| 3274 | * then move the start back without adjusting the length, |
| 3275 | * but not before offset 0. |
| 3276 | * This may of course make the start overlap previous block, |
| 3277 | * and if we hit the offset 0 limit then the next block |
| 3278 | * can still overlap too. |
| 3279 | */ |
| 3280 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 3281 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 3282 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 3283 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 3284 | else |
| 3285 | nexto = gotp->br_startoff; |
| 3286 | } else |
| 3287 | nexto = NULLFILEOFF; |
| 3288 | if (!eof && |
| 3289 | align_off + align_alen != orig_end && |
| 3290 | align_off + align_alen > nexto) |
| 3291 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 3292 | /* |
| 3293 | * If we're now overlapping the next or previous extent that |
| 3294 | * means we can't fit an extsz piece in this hole. Just move |
| 3295 | * the start forward to the first valid spot and set |
| 3296 | * the length so we hit the end. |
| 3297 | */ |
| 3298 | if (align_off != orig_off && align_off < prevo) |
| 3299 | align_off = prevo; |
| 3300 | if (align_off + align_alen != orig_end && |
| 3301 | align_off + align_alen > nexto && |
| 3302 | nexto != NULLFILEOFF) { |
| 3303 | ASSERT(nexto > prevo); |
| 3304 | align_alen = nexto - align_off; |
| 3305 | } |
| 3306 | |
| 3307 | /* |
| 3308 | * If realtime, and the result isn't a multiple of the realtime |
| 3309 | * extent size we need to remove blocks until it is. |
| 3310 | */ |
| 3311 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 3312 | /* |
| 3313 | * We're not covering the original request, or |
| 3314 | * we won't be able to once we fix the length. |
| 3315 | */ |
| 3316 | if (orig_off < align_off || |
| 3317 | orig_end > align_off + align_alen || |
| 3318 | align_alen - temp < orig_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3319 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3320 | /* |
| 3321 | * Try to fix it by moving the start up. |
| 3322 | */ |
| 3323 | if (align_off + temp <= orig_off) { |
| 3324 | align_alen -= temp; |
| 3325 | align_off += temp; |
| 3326 | } |
| 3327 | /* |
| 3328 | * Try to fix it by moving the end in. |
| 3329 | */ |
| 3330 | else if (align_off + align_alen - temp >= orig_end) |
| 3331 | align_alen -= temp; |
| 3332 | /* |
| 3333 | * Set the start to the minimum then trim the length. |
| 3334 | */ |
| 3335 | else { |
| 3336 | align_alen -= orig_off - align_off; |
| 3337 | align_off = orig_off; |
| 3338 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 3339 | } |
| 3340 | /* |
| 3341 | * Result doesn't cover the request, fail it. |
| 3342 | */ |
| 3343 | if (orig_off < align_off || orig_end > align_off + align_alen) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 3344 | return -EINVAL; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 3345 | } else { |
| 3346 | ASSERT(orig_off >= align_off); |
| 3347 | ASSERT(orig_end <= align_off + align_alen); |
| 3348 | } |
| 3349 | |
| 3350 | #ifdef DEBUG |
| 3351 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 3352 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 3353 | if (prevp->br_startoff != NULLFILEOFF) |
| 3354 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 3355 | #endif |
| 3356 | |
| 3357 | *lenp = align_alen; |
| 3358 | *offp = align_off; |
| 3359 | return 0; |
| 3360 | } |
| 3361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | #define XFS_ALLOC_GAP_UNITS 4 |
| 3363 | |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3364 | void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3365 | xfs_bmap_adjacent( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3366 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | { |
| 3368 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 3370 | xfs_mount_t *mp; /* mount point structure */ |
| 3371 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3372 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | |
| 3374 | #define ISVALID(x,y) \ |
| 3375 | (rt ? \ |
| 3376 | (x) < mp->m_sb.sb_rblocks : \ |
| 3377 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 3378 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 3379 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 3380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3382 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3384 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | /* |
| 3386 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3387 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3389 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 3390 | !isnullstartblock(ap->prev.br_startblock) && |
| 3391 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 3392 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3393 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | /* |
| 3395 | * Adjust for the gap between prevp and us. |
| 3396 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3397 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3398 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3400 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 3401 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | } |
| 3403 | /* |
| 3404 | * If not at eof, then compare the two neighbor blocks. |
| 3405 | * Figure out whether either one gives us a good starting point, |
| 3406 | * and pick the better one. |
| 3407 | */ |
| 3408 | else if (!ap->eof) { |
| 3409 | xfs_fsblock_t gotbno; /* right side block number */ |
| 3410 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 3411 | xfs_fsblock_t prevbno; /* left side block number */ |
| 3412 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 3413 | |
| 3414 | /* |
| 3415 | * If there's a previous (left) block, select a requested |
| 3416 | * start block based on it. |
| 3417 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3418 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 3419 | !isnullstartblock(ap->prev.br_startblock) && |
| 3420 | (prevbno = ap->prev.br_startblock + |
| 3421 | ap->prev.br_blockcount) && |
| 3422 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | /* |
| 3424 | * Calculate gap to end of previous block. |
| 3425 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3426 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3427 | (ap->prev.br_startoff + |
| 3428 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | /* |
| 3430 | * Figure the startblock based on the previous block's |
| 3431 | * end and the gap size. |
| 3432 | * Heuristic! |
| 3433 | * If the gap is large relative to the piece we're |
| 3434 | * allocating, or using it gives us an invalid block |
| 3435 | * number, then just use the end of the previous block. |
| 3436 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3437 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3439 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | prevbno += adjust; |
| 3441 | else |
| 3442 | prevdiff += adjust; |
| 3443 | /* |
| 3444 | * If the firstblock forbids it, can't use it, |
| 3445 | * must use default. |
| 3446 | */ |
| 3447 | if (!rt && !nullfb && |
| 3448 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 3449 | prevbno = NULLFSBLOCK; |
| 3450 | } |
| 3451 | /* |
| 3452 | * No previous block or can't follow it, just default. |
| 3453 | */ |
| 3454 | else |
| 3455 | prevbno = NULLFSBLOCK; |
| 3456 | /* |
| 3457 | * If there's a following (right) block, select a requested |
| 3458 | * start block based on it. |
| 3459 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3460 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | /* |
| 3462 | * Calculate gap to start of next block. |
| 3463 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3464 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | /* |
| 3466 | * Figure the startblock based on the next block's |
| 3467 | * start and the gap size. |
| 3468 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3469 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | /* |
| 3471 | * Heuristic! |
| 3472 | * If the gap is large relative to the piece we're |
| 3473 | * allocating, or using it gives us an invalid block |
| 3474 | * number, then just use the start of the next block |
| 3475 | * offset by our length. |
| 3476 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3477 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | ISVALID(gotbno - gotdiff, gotbno)) |
| 3479 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3480 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 3481 | gotbno -= ap->length; |
| 3482 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | } else |
| 3484 | gotdiff += adjust; |
| 3485 | /* |
| 3486 | * If the firstblock forbids it, can't use it, |
| 3487 | * must use default. |
| 3488 | */ |
| 3489 | if (!rt && !nullfb && |
| 3490 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 3491 | gotbno = NULLFSBLOCK; |
| 3492 | } |
| 3493 | /* |
| 3494 | * No next block, just default. |
| 3495 | */ |
| 3496 | else |
| 3497 | gotbno = NULLFSBLOCK; |
| 3498 | /* |
| 3499 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3500 | * 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] | 3501 | */ |
| 3502 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3503 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3505 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3507 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3509 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3510 | } |
| 3511 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3512 | static int |
| 3513 | xfs_bmap_longest_free_extent( |
| 3514 | struct xfs_trans *tp, |
| 3515 | xfs_agnumber_t ag, |
| 3516 | xfs_extlen_t *blen, |
| 3517 | int *notinit) |
| 3518 | { |
| 3519 | struct xfs_mount *mp = tp->t_mountp; |
| 3520 | struct xfs_perag *pag; |
| 3521 | xfs_extlen_t longest; |
| 3522 | int error = 0; |
| 3523 | |
| 3524 | pag = xfs_perag_get(mp, ag); |
| 3525 | if (!pag->pagf_init) { |
| 3526 | error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK); |
| 3527 | if (error) |
| 3528 | goto out; |
| 3529 | |
| 3530 | if (!pag->pagf_init) { |
| 3531 | *notinit = 1; |
| 3532 | goto out; |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 3537 | if (*blen < longest) |
| 3538 | *blen = longest; |
| 3539 | |
| 3540 | out: |
| 3541 | xfs_perag_put(pag); |
| 3542 | return error; |
| 3543 | } |
| 3544 | |
| 3545 | static void |
| 3546 | xfs_bmap_select_minlen( |
| 3547 | struct xfs_bmalloca *ap, |
| 3548 | struct xfs_alloc_arg *args, |
| 3549 | xfs_extlen_t *blen, |
| 3550 | int notinit) |
| 3551 | { |
| 3552 | if (notinit || *blen < ap->minlen) { |
| 3553 | /* |
| 3554 | * Since we did a BUF_TRYLOCK above, it is possible that |
| 3555 | * there is space for this request. |
| 3556 | */ |
| 3557 | args->minlen = ap->minlen; |
| 3558 | } else if (*blen < args->maxlen) { |
| 3559 | /* |
| 3560 | * If the best seen length is less than the request length, |
| 3561 | * use the best as the minimum. |
| 3562 | */ |
| 3563 | args->minlen = *blen; |
| 3564 | } else { |
| 3565 | /* |
| 3566 | * Otherwise we've seen an extent as big as maxlen, use that |
| 3567 | * as the minimum. |
| 3568 | */ |
| 3569 | args->minlen = args->maxlen; |
| 3570 | } |
| 3571 | } |
| 3572 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3573 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3574 | xfs_bmap_btalloc_nullfb( |
| 3575 | struct xfs_bmalloca *ap, |
| 3576 | struct xfs_alloc_arg *args, |
| 3577 | xfs_extlen_t *blen) |
| 3578 | { |
| 3579 | struct xfs_mount *mp = ap->ip->i_mount; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3580 | xfs_agnumber_t ag, startag; |
| 3581 | int notinit = 0; |
| 3582 | int error; |
| 3583 | |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3584 | args->type = XFS_ALLOCTYPE_START_BNO; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3585 | args->total = ap->total; |
| 3586 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3587 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3588 | if (startag == NULLAGNUMBER) |
| 3589 | startag = ag = 0; |
| 3590 | |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3591 | while (*blen < args->maxlen) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3592 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3593 | ¬init); |
| 3594 | if (error) |
| 3595 | return error; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3596 | |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3597 | if (++ag == mp->m_sb.sb_agcount) |
| 3598 | ag = 0; |
| 3599 | if (ag == startag) |
| 3600 | break; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3601 | } |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3602 | |
| 3603 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
| 3604 | return 0; |
| 3605 | } |
| 3606 | |
| 3607 | STATIC int |
| 3608 | xfs_bmap_btalloc_filestreams( |
| 3609 | struct xfs_bmalloca *ap, |
| 3610 | struct xfs_alloc_arg *args, |
| 3611 | xfs_extlen_t *blen) |
| 3612 | { |
| 3613 | struct xfs_mount *mp = ap->ip->i_mount; |
| 3614 | xfs_agnumber_t ag; |
| 3615 | int notinit = 0; |
| 3616 | int error; |
| 3617 | |
| 3618 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3619 | args->total = ap->total; |
| 3620 | |
| 3621 | ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 3622 | if (ag == NULLAGNUMBER) |
| 3623 | ag = 0; |
| 3624 | |
| 3625 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, ¬init); |
| 3626 | if (error) |
| 3627 | return error; |
| 3628 | |
| 3629 | if (*blen < args->maxlen) { |
| 3630 | error = xfs_filestream_new_ag(ap, &ag); |
| 3631 | if (error) |
| 3632 | return error; |
| 3633 | |
| 3634 | error = xfs_bmap_longest_free_extent(args->tp, ag, blen, |
| 3635 | ¬init); |
| 3636 | if (error) |
| 3637 | return error; |
| 3638 | |
| 3639 | } |
| 3640 | |
| 3641 | xfs_bmap_select_minlen(ap, args, blen, notinit); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3642 | |
| 3643 | /* |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3644 | * Set the failure fallback case to look in the selected AG as stream |
| 3645 | * may have moved. |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3646 | */ |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3647 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3648 | return 0; |
| 3649 | } |
| 3650 | |
| 3651 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3652 | xfs_bmap_btalloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3653 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3654 | { |
| 3655 | xfs_mount_t *mp; /* mount point structure */ |
| 3656 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 3657 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3658 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3659 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3660 | xfs_alloc_arg_t args; |
| 3661 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3662 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3663 | int nullfb; /* true if ap->firstblock isn't set */ |
| 3664 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3665 | int tryagain; |
| 3666 | int error; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3667 | int stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3668 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 3669 | ASSERT(ap->length); |
| 3670 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3671 | mp = ap->ip->i_mount; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3672 | |
| 3673 | /* stripe alignment for allocation is determined by mount parameters */ |
| 3674 | stripe_align = 0; |
| 3675 | if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC)) |
| 3676 | stripe_align = mp->m_swidth; |
| 3677 | else if (mp->m_dalign) |
| 3678 | stripe_align = mp->m_dalign; |
| 3679 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3680 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3681 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 3682 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3683 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3684 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3685 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3686 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3687 | } |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3688 | |
| 3689 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3690 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 3691 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3692 | if (nullfb) { |
| 3693 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 3694 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 3695 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3696 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3697 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3698 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3699 | } |
| 3700 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3701 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3702 | |
| 3703 | xfs_bmap_adjacent(ap); |
| 3704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3706 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | * it's in the right allocation group. |
| 3708 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3709 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | ; |
| 3711 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3712 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | * Normal allocation, done through xfs_alloc_vextent. |
| 3715 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3716 | tryagain = isaligned = 0; |
Mark Tinguely | a004168 | 2012-09-20 13:16:45 -0500 | [diff] [blame] | 3717 | memset(&args, 0, sizeof(args)); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3718 | args.tp = ap->tp; |
| 3719 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3720 | args.fsbno = ap->blkno; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3721 | |
| 3722 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3723 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3724 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3725 | blen = 0; |
| 3726 | if (nullfb) { |
Christoph Hellwig | c977eb1 | 2014-04-23 07:11:41 +1000 | [diff] [blame] | 3727 | /* |
| 3728 | * Search for an allocation group with a single extent large |
| 3729 | * enough for the request. If one isn't found, then adjust |
| 3730 | * the minimum allocation size to the largest space found. |
| 3731 | */ |
| 3732 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 3733 | error = xfs_bmap_btalloc_filestreams(ap, &args, &blen); |
| 3734 | else |
| 3735 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 3736 | if (error) |
| 3737 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3738 | } else if (ap->flist->xbf_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 3739 | if (xfs_inode_is_filestream(ap->ip)) |
| 3740 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3741 | else |
| 3742 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3743 | args.total = args.minlen = ap->minlen; |
| 3744 | } else { |
| 3745 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3746 | args.total = ap->total; |
| 3747 | args.minlen = ap->minlen; |
| 3748 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 3749 | /* apply extent size hints if obtained earlier */ |
| 3750 | if (unlikely(align)) { |
| 3751 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3752 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3753 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3754 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3755 | args.prod = 1; |
| 3756 | args.mod = 0; |
| 3757 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 3758 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3759 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3760 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 3761 | } |
| 3762 | /* |
| 3763 | * If we are not low on available data blocks, and the |
| 3764 | * underlying logical volume manager is a stripe, and |
| 3765 | * the file offset is zero then try to allocate data |
| 3766 | * blocks on stripe unit boundary. |
| 3767 | * NOTE: ap->aeof is only set if the allocation length |
| 3768 | * is >= the stripe unit and the allocation offset is |
| 3769 | * at the end of file. |
| 3770 | */ |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3771 | if (!ap->flist->xbf_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3772 | if (!ap->offset) { |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3773 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3774 | atype = args.type; |
| 3775 | isaligned = 1; |
| 3776 | /* |
| 3777 | * Adjust for alignment |
| 3778 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 3779 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3780 | args.minlen = blen - args.alignment; |
| 3781 | args.minalignslop = 0; |
| 3782 | } else { |
| 3783 | /* |
| 3784 | * First try an exact bno allocation. |
| 3785 | * If it fails then do a near or start bno |
| 3786 | * allocation with alignment turned on. |
| 3787 | */ |
| 3788 | atype = args.type; |
| 3789 | tryagain = 1; |
| 3790 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 3791 | args.alignment = 1; |
| 3792 | /* |
| 3793 | * Compute the minlen+alignment for the |
| 3794 | * next case. Set slop so that the value |
| 3795 | * of minlen+alignment+slop doesn't go up |
| 3796 | * between the calls. |
| 3797 | */ |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3798 | if (blen > stripe_align && blen <= args.maxlen) |
| 3799 | nextminlen = blen - stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3800 | else |
| 3801 | nextminlen = args.minlen; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3802 | if (nextminlen + stripe_align > args.minlen + 1) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3803 | args.minalignslop = |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3804 | nextminlen + stripe_align - |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3805 | args.minlen - 1; |
| 3806 | else |
| 3807 | args.minalignslop = 0; |
| 3808 | } |
| 3809 | } else { |
| 3810 | args.alignment = 1; |
| 3811 | args.minalignslop = 0; |
| 3812 | } |
| 3813 | args.minleft = ap->minleft; |
| 3814 | args.wasdel = ap->wasdel; |
| 3815 | args.isfl = 0; |
| 3816 | args.userdata = ap->userdata; |
| 3817 | if ((error = xfs_alloc_vextent(&args))) |
| 3818 | return error; |
| 3819 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 3820 | /* |
| 3821 | * Exact allocation failed. Now try with alignment |
| 3822 | * turned on. |
| 3823 | */ |
| 3824 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3825 | args.fsbno = ap->blkno; |
Dave Chinner | 33177f05 | 2013-12-12 16:34:36 +1100 | [diff] [blame] | 3826 | args.alignment = stripe_align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3827 | args.minlen = nextminlen; |
| 3828 | args.minalignslop = 0; |
| 3829 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | if ((error = xfs_alloc_vextent(&args))) |
| 3831 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3832 | } |
| 3833 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 3834 | /* |
| 3835 | * allocation failed, so turn off alignment and |
| 3836 | * try again. |
| 3837 | */ |
| 3838 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3839 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3840 | args.alignment = 0; |
| 3841 | if ((error = xfs_alloc_vextent(&args))) |
| 3842 | return error; |
| 3843 | } |
| 3844 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 3845 | args.minlen > ap->minlen) { |
| 3846 | args.minlen = ap->minlen; |
| 3847 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3848 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3849 | if ((error = xfs_alloc_vextent(&args))) |
| 3850 | return error; |
| 3851 | } |
| 3852 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 3853 | args.fsbno = 0; |
| 3854 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 3855 | args.total = ap->minlen; |
| 3856 | args.minleft = 0; |
| 3857 | if ((error = xfs_alloc_vextent(&args))) |
| 3858 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3859 | ap->flist->xbf_low = 1; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3860 | } |
| 3861 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3862 | /* |
| 3863 | * check the allocation happened at the same or higher AG than |
| 3864 | * the first block that was allocated. |
| 3865 | */ |
| 3866 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 3867 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 3868 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
| 3869 | (ap->flist->xbf_low && |
| 3870 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 3871 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 3872 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3873 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3874 | if (*ap->firstblock == NULLFSBLOCK) |
| 3875 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3876 | ASSERT(nullfb || fb_agno == args.agno || |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 3877 | (ap->flist->xbf_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3878 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3879 | ap->ip->i_d.di_nblocks += args.len; |
| 3880 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 3881 | if (ap->wasdel) |
| 3882 | ap->ip->i_delayed_blks -= args.len; |
| 3883 | /* |
| 3884 | * Adjust the disk quota also. This was reserved |
| 3885 | * earlier. |
| 3886 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3887 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3888 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3889 | XFS_TRANS_DQ_BCOUNT, |
| 3890 | (long) args.len); |
| 3891 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3892 | ap->blkno = NULLFSBLOCK; |
| 3893 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | } |
| 3895 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | /* |
| 3899 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3900 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3901 | */ |
| 3902 | STATIC int |
| 3903 | xfs_bmap_alloc( |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 3904 | struct xfs_bmalloca *ap) /* bmap alloc argument struct */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3905 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3906 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3907 | return xfs_bmap_rtalloc(ap); |
| 3908 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 3912 | * Trim the returned map to the required bounds |
| 3913 | */ |
| 3914 | STATIC void |
| 3915 | xfs_bmapi_trim_map( |
| 3916 | struct xfs_bmbt_irec *mval, |
| 3917 | struct xfs_bmbt_irec *got, |
| 3918 | xfs_fileoff_t *bno, |
| 3919 | xfs_filblks_t len, |
| 3920 | xfs_fileoff_t obno, |
| 3921 | xfs_fileoff_t end, |
| 3922 | int n, |
| 3923 | int flags) |
| 3924 | { |
| 3925 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 3926 | got->br_startoff + got->br_blockcount <= obno) { |
| 3927 | *mval = *got; |
| 3928 | if (isnullstartblock(got->br_startblock)) |
| 3929 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3930 | return; |
| 3931 | } |
| 3932 | |
| 3933 | if (obno > *bno) |
| 3934 | *bno = obno; |
| 3935 | ASSERT((*bno >= obno) || (n == 0)); |
| 3936 | ASSERT(*bno < end); |
| 3937 | mval->br_startoff = *bno; |
| 3938 | if (isnullstartblock(got->br_startblock)) |
| 3939 | mval->br_startblock = DELAYSTARTBLOCK; |
| 3940 | else |
| 3941 | mval->br_startblock = got->br_startblock + |
| 3942 | (*bno - got->br_startoff); |
| 3943 | /* |
| 3944 | * Return the minimum of what we got and what we asked for for |
| 3945 | * the length. We can use the len variable here because it is |
| 3946 | * modified below and we could have been there before coming |
| 3947 | * here if the first part of the allocation didn't overlap what |
| 3948 | * was asked for. |
| 3949 | */ |
| 3950 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 3951 | got->br_blockcount - (*bno - got->br_startoff)); |
| 3952 | mval->br_state = got->br_state; |
| 3953 | ASSERT(mval->br_blockcount <= len); |
| 3954 | return; |
| 3955 | } |
| 3956 | |
| 3957 | /* |
| 3958 | * Update and validate the extent map to return |
| 3959 | */ |
| 3960 | STATIC void |
| 3961 | xfs_bmapi_update_map( |
| 3962 | struct xfs_bmbt_irec **map, |
| 3963 | xfs_fileoff_t *bno, |
| 3964 | xfs_filblks_t *len, |
| 3965 | xfs_fileoff_t obno, |
| 3966 | xfs_fileoff_t end, |
| 3967 | int *n, |
| 3968 | int flags) |
| 3969 | { |
| 3970 | xfs_bmbt_irec_t *mval = *map; |
| 3971 | |
| 3972 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 3973 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 3974 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 3975 | (mval->br_startoff < obno)); |
| 3976 | |
| 3977 | *bno = mval->br_startoff + mval->br_blockcount; |
| 3978 | *len = end - *bno; |
| 3979 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 3980 | /* update previous map with new information */ |
| 3981 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 3982 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 3983 | ASSERT(mval->br_state == mval[-1].br_state); |
| 3984 | mval[-1].br_blockcount = mval->br_blockcount; |
| 3985 | mval[-1].br_state = mval->br_state; |
| 3986 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 3987 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 3988 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 3989 | mval->br_startblock == mval[-1].br_startblock + |
| 3990 | mval[-1].br_blockcount && |
| 3991 | ((flags & XFS_BMAPI_IGSTATE) || |
| 3992 | mval[-1].br_state == mval->br_state)) { |
| 3993 | ASSERT(mval->br_startoff == |
| 3994 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 3995 | mval[-1].br_blockcount += mval->br_blockcount; |
| 3996 | } else if (*n > 0 && |
| 3997 | mval->br_startblock == DELAYSTARTBLOCK && |
| 3998 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 3999 | mval->br_startoff == |
| 4000 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4001 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4002 | mval[-1].br_state = mval->br_state; |
| 4003 | } else if (!((*n == 0) && |
| 4004 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4005 | obno))) { |
| 4006 | mval++; |
| 4007 | (*n)++; |
| 4008 | } |
| 4009 | *map = mval; |
| 4010 | } |
| 4011 | |
| 4012 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4013 | * Map file blocks to filesystem blocks without allocation. |
| 4014 | */ |
| 4015 | int |
| 4016 | xfs_bmapi_read( |
| 4017 | struct xfs_inode *ip, |
| 4018 | xfs_fileoff_t bno, |
| 4019 | xfs_filblks_t len, |
| 4020 | struct xfs_bmbt_irec *mval, |
| 4021 | int *nmap, |
| 4022 | int flags) |
| 4023 | { |
| 4024 | struct xfs_mount *mp = ip->i_mount; |
| 4025 | struct xfs_ifork *ifp; |
| 4026 | struct xfs_bmbt_irec got; |
| 4027 | struct xfs_bmbt_irec prev; |
| 4028 | xfs_fileoff_t obno; |
| 4029 | xfs_fileoff_t end; |
| 4030 | xfs_extnum_t lastx; |
| 4031 | int error; |
| 4032 | int eof; |
| 4033 | int n = 0; |
| 4034 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4035 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4036 | |
| 4037 | ASSERT(*nmap >= 1); |
| 4038 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 4039 | XFS_BMAPI_IGSTATE))); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4040 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4041 | |
| 4042 | if (unlikely(XFS_TEST_ERROR( |
| 4043 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4044 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4045 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4046 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4047 | return -EFSCORRUPTED; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4051 | return -EIO; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4052 | |
| 4053 | XFS_STATS_INC(xs_blk_mapr); |
| 4054 | |
| 4055 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4056 | |
| 4057 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4058 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4059 | if (error) |
| 4060 | return error; |
| 4061 | } |
| 4062 | |
| 4063 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4064 | end = bno + len; |
| 4065 | obno = bno; |
| 4066 | |
| 4067 | while (bno < end && n < *nmap) { |
| 4068 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4069 | if (eof) |
| 4070 | got.br_startoff = end; |
| 4071 | if (got.br_startoff > bno) { |
| 4072 | /* Reading in a hole. */ |
| 4073 | mval->br_startoff = bno; |
| 4074 | mval->br_startblock = HOLESTARTBLOCK; |
| 4075 | mval->br_blockcount = |
| 4076 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4077 | mval->br_state = XFS_EXT_NORM; |
| 4078 | bno += mval->br_blockcount; |
| 4079 | len -= mval->br_blockcount; |
| 4080 | mval++; |
| 4081 | n++; |
| 4082 | continue; |
| 4083 | } |
| 4084 | |
| 4085 | /* set up the extent map to return. */ |
| 4086 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4087 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4088 | |
| 4089 | /* If we're done, stop now. */ |
| 4090 | if (bno >= end || n >= *nmap) |
| 4091 | break; |
| 4092 | |
| 4093 | /* Else go on to the next record. */ |
| 4094 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4095 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4096 | else |
| 4097 | eof = 1; |
| 4098 | } |
| 4099 | *nmap = n; |
| 4100 | return 0; |
| 4101 | } |
| 4102 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4103 | STATIC int |
| 4104 | xfs_bmapi_reserve_delalloc( |
| 4105 | struct xfs_inode *ip, |
| 4106 | xfs_fileoff_t aoff, |
| 4107 | xfs_filblks_t len, |
| 4108 | struct xfs_bmbt_irec *got, |
| 4109 | struct xfs_bmbt_irec *prev, |
| 4110 | xfs_extnum_t *lastx, |
| 4111 | int eof) |
| 4112 | { |
| 4113 | struct xfs_mount *mp = ip->i_mount; |
| 4114 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4115 | xfs_extlen_t alen; |
| 4116 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4117 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4118 | xfs_extlen_t extsz; |
| 4119 | int error; |
| 4120 | |
| 4121 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4122 | if (!eof) |
| 4123 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4124 | |
| 4125 | /* Figure out the extent size, adjust alen */ |
| 4126 | extsz = xfs_get_extsz_hint(ip); |
| 4127 | if (extsz) { |
| 4128 | /* |
| 4129 | * Make sure we don't exceed a single extent length when we |
| 4130 | * align the extent by reducing length we are going to |
| 4131 | * allocate by the maximum amount extent size aligment may |
| 4132 | * require. |
| 4133 | */ |
| 4134 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); |
| 4135 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4136 | 1, 0, &aoff, &alen); |
| 4137 | ASSERT(!error); |
| 4138 | } |
| 4139 | |
| 4140 | if (rt) |
| 4141 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4142 | |
| 4143 | /* |
| 4144 | * Make a transaction-less quota reservation for delayed allocation |
| 4145 | * blocks. This number gets adjusted later. We return if we haven't |
| 4146 | * allocated blocks already inside this loop. |
| 4147 | */ |
| 4148 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4149 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4150 | if (error) |
| 4151 | return error; |
| 4152 | |
| 4153 | /* |
| 4154 | * Split changing sb for alen and indlen since they could be coming |
| 4155 | * from different places. |
| 4156 | */ |
| 4157 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4158 | ASSERT(indlen > 0); |
| 4159 | |
| 4160 | if (rt) { |
| 4161 | error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
| 4162 | -((int64_t)extsz), 0); |
| 4163 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 4164 | error = xfs_mod_fdblocks(mp, -((int64_t)alen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | if (error) |
| 4168 | goto out_unreserve_quota; |
| 4169 | |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 4170 | error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4171 | if (error) |
| 4172 | goto out_unreserve_blocks; |
| 4173 | |
| 4174 | |
| 4175 | ip->i_delayed_blks += alen; |
| 4176 | |
| 4177 | got->br_startoff = aoff; |
| 4178 | got->br_startblock = nullstartblock(indlen); |
| 4179 | got->br_blockcount = alen; |
| 4180 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4181 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4182 | |
| 4183 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4184 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4185 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4186 | */ |
| 4187 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4188 | |
| 4189 | ASSERT(got->br_startoff <= aoff); |
| 4190 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4191 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4192 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4193 | return 0; |
| 4194 | |
| 4195 | out_unreserve_blocks: |
| 4196 | if (rt) |
| 4197 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0); |
| 4198 | else |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 4199 | xfs_mod_fdblocks(mp, alen, false); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4200 | out_unreserve_quota: |
| 4201 | if (XFS_IS_QUOTA_ON(mp)) |
Dave Chinner | ea562ed | 2012-05-08 20:48:53 +1000 | [diff] [blame] | 4202 | xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ? |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4203 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4204 | return error; |
| 4205 | } |
| 4206 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4207 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4208 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4209 | */ |
| 4210 | int |
| 4211 | xfs_bmapi_delay( |
| 4212 | struct xfs_inode *ip, /* incore inode */ |
| 4213 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4214 | xfs_filblks_t len, /* length to map in file */ |
| 4215 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4216 | int *nmap, /* i/o: mval size/count */ |
| 4217 | int flags) /* XFS_BMAPI_... */ |
| 4218 | { |
| 4219 | struct xfs_mount *mp = ip->i_mount; |
| 4220 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4221 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4222 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4223 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4224 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4225 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4226 | int eof; /* we've hit the end of extents */ |
| 4227 | int n = 0; /* current extent index */ |
| 4228 | int error = 0; |
| 4229 | |
| 4230 | ASSERT(*nmap >= 1); |
| 4231 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4232 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4233 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4234 | |
| 4235 | if (unlikely(XFS_TEST_ERROR( |
| 4236 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4237 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4238 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4239 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4240 | return -EFSCORRUPTED; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4244 | return -EIO; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4245 | |
| 4246 | XFS_STATS_INC(xs_blk_mapw); |
| 4247 | |
| 4248 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4249 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4250 | if (error) |
| 4251 | return error; |
| 4252 | } |
| 4253 | |
| 4254 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4255 | end = bno + len; |
| 4256 | obno = bno; |
| 4257 | |
| 4258 | while (bno < end && n < *nmap) { |
| 4259 | if (eof || got.br_startoff > bno) { |
| 4260 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4261 | &prev, &lastx, eof); |
| 4262 | if (error) { |
| 4263 | if (n == 0) { |
| 4264 | *nmap = 0; |
| 4265 | return error; |
| 4266 | } |
| 4267 | break; |
| 4268 | } |
| 4269 | } |
| 4270 | |
| 4271 | /* set up the extent map to return. */ |
| 4272 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4273 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4274 | |
| 4275 | /* If we're done, stop now. */ |
| 4276 | if (bno >= end || n >= *nmap) |
| 4277 | break; |
| 4278 | |
| 4279 | /* Else go on to the next record. */ |
| 4280 | prev = got; |
| 4281 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4282 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4283 | else |
| 4284 | eof = 1; |
| 4285 | } |
| 4286 | |
| 4287 | *nmap = n; |
| 4288 | return 0; |
| 4289 | } |
| 4290 | |
| 4291 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 4292 | static int |
| 4293 | xfs_bmapi_allocate( |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4294 | struct xfs_bmalloca *bma) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4295 | { |
| 4296 | struct xfs_mount *mp = bma->ip->i_mount; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4297 | int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4298 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4299 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4300 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4301 | int error; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4302 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4303 | ASSERT(bma->length > 0); |
| 4304 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4305 | /* |
| 4306 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4307 | * for in this bmap call but that wouldn't be as good. |
| 4308 | */ |
| 4309 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4310 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4311 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4312 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4313 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4314 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4315 | } |
| 4316 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4317 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4318 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4319 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4320 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | /* |
| 4324 | * Indicate if this is the first user data in the file, or just any |
| 4325 | * user data. |
| 4326 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4327 | if (!(bma->flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4328 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4329 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
| 4330 | } |
| 4331 | |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4332 | bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4333 | |
| 4334 | /* |
| 4335 | * Only want to do the alignment at the eof if it is userdata and |
| 4336 | * allocation length is larger than a stripe unit. |
| 4337 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4338 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4339 | !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4340 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4341 | if (error) |
| 4342 | return error; |
| 4343 | } |
| 4344 | |
| 4345 | error = xfs_bmap_alloc(bma); |
| 4346 | if (error) |
| 4347 | return error; |
| 4348 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4349 | if (bma->flist->xbf_low) |
| 4350 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4351 | if (bma->cur) |
| 4352 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4353 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4354 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4355 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4356 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4357 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4358 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4359 | } |
| 4360 | /* |
| 4361 | * Bump the number of extents we've allocated |
| 4362 | * in this call. |
| 4363 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4364 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4365 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4366 | if (bma->cur) |
| 4367 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4368 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4369 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4370 | bma->got.br_startoff = bma->offset; |
| 4371 | bma->got.br_startblock = bma->blkno; |
| 4372 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4373 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4374 | |
| 4375 | /* |
| 4376 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4377 | * as unwritten. |
| 4378 | */ |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4379 | if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4380 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4381 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4382 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4383 | if (bma->wasdel) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 4384 | error = xfs_bmap_add_extent_delay_real(bma); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4385 | else |
| 4386 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4387 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4388 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4389 | if (error) |
| 4390 | return error; |
| 4391 | |
| 4392 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4393 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4394 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4395 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4396 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4397 | 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] | 4398 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4399 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4400 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4401 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4402 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4403 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4404 | return 0; |
| 4405 | } |
| 4406 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4407 | STATIC int |
| 4408 | xfs_bmapi_convert_unwritten( |
| 4409 | struct xfs_bmalloca *bma, |
| 4410 | struct xfs_bmbt_irec *mval, |
| 4411 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4412 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4413 | { |
| 4414 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4415 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4416 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4417 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4418 | int error; |
| 4419 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4420 | /* check if we need to do unwritten->real conversion */ |
| 4421 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4422 | (flags & XFS_BMAPI_PREALLOC)) |
| 4423 | return 0; |
| 4424 | |
| 4425 | /* check if we need to do real->unwritten conversion */ |
| 4426 | if (mval->br_state == XFS_EXT_NORM && |
| 4427 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4428 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4429 | return 0; |
| 4430 | |
| 4431 | /* |
| 4432 | * Modify (by adding) the state flag, if writing. |
| 4433 | */ |
| 4434 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4435 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4436 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4437 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4438 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4439 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4440 | } |
| 4441 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4442 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4443 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4444 | 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] | 4445 | &bma->cur, mval, bma->firstblock, bma->flist, |
| 4446 | &tmp_logflags); |
| 4447 | bma->logflags |= tmp_logflags; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4448 | if (error) |
| 4449 | return error; |
| 4450 | |
| 4451 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4452 | * Update our extent pointer, given that |
| 4453 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4454 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4455 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4456 | 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] | 4457 | |
| 4458 | /* |
| 4459 | * We may have combined previously unwritten space with written space, |
| 4460 | * so generate another request. |
| 4461 | */ |
| 4462 | if (mval->br_blockcount < len) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4463 | return -EAGAIN; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4464 | return 0; |
| 4465 | } |
| 4466 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4467 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4468 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4469 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4470 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4471 | * locking problems. |
| 4472 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | * The returned value in "firstblock" from the first call in a transaction |
| 4474 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4475 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4476 | * the first call in "total"; if no allocation group has that many free |
| 4477 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4478 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4479 | int |
| 4480 | xfs_bmapi_write( |
| 4481 | struct xfs_trans *tp, /* transaction pointer */ |
| 4482 | struct xfs_inode *ip, /* incore inode */ |
| 4483 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4484 | xfs_filblks_t len, /* length to map in file */ |
| 4485 | int flags, /* XFS_BMAPI_... */ |
| 4486 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4487 | controls a.g. for allocs */ |
| 4488 | xfs_extlen_t total, /* total blocks needed */ |
| 4489 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4490 | int *nmap, /* i/o: mval size/count */ |
| 4491 | struct xfs_bmap_free *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4493 | struct xfs_mount *mp = ip->i_mount; |
| 4494 | struct xfs_ifork *ifp; |
Dave Chinner | a30b036 | 2013-09-02 20:49:36 +1000 | [diff] [blame] | 4495 | struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4496 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4497 | int eof; /* after the end of extents */ |
| 4498 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4499 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4500 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4501 | int whichfork; /* data or attr fork */ |
| 4502 | char inhole; /* current location is hole in file */ |
| 4503 | char wasdelay; /* old extent was delayed */ |
| 4504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4506 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4507 | int orig_flags; /* original flags arg value */ |
| 4508 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4509 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4510 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | |
| 4512 | orig_bno = bno; |
| 4513 | orig_len = len; |
| 4514 | orig_flags = flags; |
| 4515 | orig_mval = mval; |
| 4516 | orig_nmap = *nmap; |
| 4517 | #endif |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4518 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4519 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4522 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4523 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4524 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4525 | ASSERT(len > 0); |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4526 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL); |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 4527 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | if (unlikely(XFS_TEST_ERROR( |
| 4530 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
Dave Chinner | f3508bc | 2013-07-10 07:04:00 +1000 | [diff] [blame] | 4531 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4533 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4534 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4536 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4538 | return -EIO; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4541 | |
| 4542 | XFS_STATS_INC(xs_blk_mapw); |
| 4543 | |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4544 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4546 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4548 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4549 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4550 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4554 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4555 | if (error) |
| 4556 | goto error0; |
| 4557 | } |
| 4558 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4559 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4560 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | n = 0; |
| 4562 | end = bno + len; |
| 4563 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4564 | |
| 4565 | bma.tp = tp; |
| 4566 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4567 | bma.total = total; |
| 4568 | bma.userdata = 0; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4569 | bma.flist = flist; |
| 4570 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4573 | inhole = eof || bma.got.br_startoff > bno; |
| 4574 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | /* |
| 4577 | * First, deal with the hole before the allocated space |
| 4578 | * that we found, if any. |
| 4579 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4580 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4581 | bma.eof = eof; |
| 4582 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4583 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4584 | bma.offset = bno; |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4585 | bma.flags = flags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4586 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4587 | /* |
| 4588 | * There's a 32/64 bit type mismatch between the |
| 4589 | * allocation length request (which can be 64 bits in |
| 4590 | * length) and the bma length request, which is |
| 4591 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4592 | * check for 32-bit overflows and handle them here. |
| 4593 | */ |
| 4594 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4595 | bma.length = MAXEXTLEN; |
| 4596 | else |
| 4597 | bma.length = len; |
| 4598 | |
| 4599 | ASSERT(len > 0); |
| 4600 | ASSERT(bma.length > 0); |
Dave Chinner | e04426b | 2012-10-05 11:06:59 +1000 | [diff] [blame] | 4601 | error = xfs_bmapi_allocate(&bma); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | if (error) |
| 4603 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4604 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4605 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4607 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4608 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4609 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4610 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4612 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4613 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4614 | if (error == -EAGAIN) |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4615 | continue; |
| 4616 | if (error) |
| 4617 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4619 | /* update the extent map to return */ |
| 4620 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | /* |
| 4623 | * If we're done, stop now. Stop when we've allocated |
| 4624 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4625 | * the transaction may get too big. |
| 4626 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4627 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4629 | |
| 4630 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4631 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4632 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4633 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4634 | &bma.got); |
| 4635 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4636 | eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | /* |
| 4641 | * Transform from btree to extents, give it cur. |
| 4642 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4643 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4644 | int tmp_logflags = 0; |
| 4645 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4646 | ASSERT(bma.cur); |
| 4647 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4649 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | if (error) |
| 4651 | goto error0; |
| 4652 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4655 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4656 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | error0: |
| 4659 | /* |
| 4660 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4661 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4663 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4665 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4666 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4668 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | /* |
| 4670 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4671 | * detecting a case where the data is changed, there's an error, |
| 4672 | * and it's not logged so we don't shutdown when we should. |
| 4673 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4674 | if (bma.logflags) |
| 4675 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4676 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4677 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | if (!error) { |
| 4679 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4680 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4681 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4682 | bma.cur->bc_private.b.firstblock) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | (flist->xbf_low && |
| 4684 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4685 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4686 | bma.cur->bc_private.b.firstblock))); |
| 4687 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4689 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4691 | } |
| 4692 | if (!error) |
| 4693 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4694 | orig_nmap, *nmap); |
| 4695 | return error; |
| 4696 | } |
| 4697 | |
| 4698 | /* |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4699 | * Called by xfs_bmapi to update file extent records and the btree |
| 4700 | * after removing space (or undoing a delayed allocation). |
| 4701 | */ |
| 4702 | STATIC int /* error */ |
| 4703 | xfs_bmap_del_extent( |
| 4704 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4705 | xfs_trans_t *tp, /* current transaction pointer */ |
| 4706 | xfs_extnum_t *idx, /* extent number to update/delete */ |
| 4707 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 4708 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
| 4709 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
| 4710 | int *logflagsp, /* inode logging flags */ |
| 4711 | int whichfork) /* data or attr fork */ |
| 4712 | { |
| 4713 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 4714 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 4715 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 4716 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 4717 | int delay; /* current block is delayed allocated */ |
| 4718 | int do_fx; /* free extent at end of routine */ |
| 4719 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
| 4720 | int error; /* error return value */ |
| 4721 | int flags; /* inode logging flags */ |
| 4722 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 4723 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 4724 | int i; /* temp state */ |
| 4725 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4726 | xfs_mount_t *mp; /* mount structure */ |
| 4727 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 4728 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 4729 | /* REFERENCED */ |
| 4730 | uint qfield; /* quota field to update */ |
| 4731 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 4732 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 4733 | int state = 0; |
| 4734 | |
| 4735 | XFS_STATS_INC(xs_del_exlist); |
| 4736 | |
| 4737 | if (whichfork == XFS_ATTR_FORK) |
| 4738 | state |= BMAP_ATTRFORK; |
| 4739 | |
| 4740 | mp = ip->i_mount; |
| 4741 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4742 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
| 4743 | (uint)sizeof(xfs_bmbt_rec_t))); |
| 4744 | ASSERT(del->br_blockcount > 0); |
| 4745 | ep = xfs_iext_get_ext(ifp, *idx); |
| 4746 | xfs_bmbt_get_all(ep, &got); |
| 4747 | ASSERT(got.br_startoff <= del->br_startoff); |
| 4748 | del_endoff = del->br_startoff + del->br_blockcount; |
| 4749 | got_endoff = got.br_startoff + got.br_blockcount; |
| 4750 | ASSERT(got_endoff >= del_endoff); |
| 4751 | delay = isnullstartblock(got.br_startblock); |
| 4752 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
| 4753 | flags = 0; |
| 4754 | qfield = 0; |
| 4755 | error = 0; |
| 4756 | /* |
| 4757 | * If deleting a real allocation, must free up the disk space. |
| 4758 | */ |
| 4759 | if (!delay) { |
| 4760 | flags = XFS_ILOG_CORE; |
| 4761 | /* |
| 4762 | * Realtime allocation. Free it and record di_nblocks update. |
| 4763 | */ |
| 4764 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
| 4765 | xfs_fsblock_t bno; |
| 4766 | xfs_filblks_t len; |
| 4767 | |
| 4768 | ASSERT(do_mod(del->br_blockcount, |
| 4769 | mp->m_sb.sb_rextsize) == 0); |
| 4770 | ASSERT(do_mod(del->br_startblock, |
| 4771 | mp->m_sb.sb_rextsize) == 0); |
| 4772 | bno = del->br_startblock; |
| 4773 | len = del->br_blockcount; |
| 4774 | do_div(bno, mp->m_sb.sb_rextsize); |
| 4775 | do_div(len, mp->m_sb.sb_rextsize); |
| 4776 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 4777 | if (error) |
| 4778 | goto done; |
| 4779 | do_fx = 0; |
| 4780 | nblks = len * mp->m_sb.sb_rextsize; |
| 4781 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 4782 | } |
| 4783 | /* |
| 4784 | * Ordinary allocation. |
| 4785 | */ |
| 4786 | else { |
| 4787 | do_fx = 1; |
| 4788 | nblks = del->br_blockcount; |
| 4789 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 4790 | } |
| 4791 | /* |
| 4792 | * Set up del_endblock and cur for later. |
| 4793 | */ |
| 4794 | del_endblock = del->br_startblock + del->br_blockcount; |
| 4795 | if (cur) { |
| 4796 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 4797 | got.br_startblock, got.br_blockcount, |
| 4798 | &i))) |
| 4799 | goto done; |
| 4800 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
| 4801 | } |
| 4802 | da_old = da_new = 0; |
| 4803 | } else { |
| 4804 | da_old = startblockval(got.br_startblock); |
| 4805 | da_new = 0; |
| 4806 | nblks = 0; |
| 4807 | do_fx = 0; |
| 4808 | } |
| 4809 | /* |
| 4810 | * Set flag value to use in switch statement. |
| 4811 | * Left-contig is 2, right-contig is 1. |
| 4812 | */ |
| 4813 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 4814 | (got_endoff == del_endoff)) { |
| 4815 | case 3: |
| 4816 | /* |
| 4817 | * Matches the whole extent. Delete the entry. |
| 4818 | */ |
| 4819 | xfs_iext_remove(ip, *idx, 1, |
| 4820 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 4821 | --*idx; |
| 4822 | if (delay) |
| 4823 | break; |
| 4824 | |
| 4825 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4826 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 4827 | flags |= XFS_ILOG_CORE; |
| 4828 | if (!cur) { |
| 4829 | flags |= xfs_ilog_fext(whichfork); |
| 4830 | break; |
| 4831 | } |
| 4832 | if ((error = xfs_btree_delete(cur, &i))) |
| 4833 | goto done; |
| 4834 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
| 4835 | break; |
| 4836 | |
| 4837 | case 2: |
| 4838 | /* |
| 4839 | * Deleting the first part of the extent. |
| 4840 | */ |
| 4841 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4842 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 4843 | temp = got.br_blockcount - del->br_blockcount; |
| 4844 | xfs_bmbt_set_blockcount(ep, temp); |
| 4845 | if (delay) { |
| 4846 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4847 | da_old); |
| 4848 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4849 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4850 | da_new = temp; |
| 4851 | break; |
| 4852 | } |
| 4853 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 4854 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4855 | if (!cur) { |
| 4856 | flags |= xfs_ilog_fext(whichfork); |
| 4857 | break; |
| 4858 | } |
| 4859 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 4860 | got.br_blockcount - del->br_blockcount, |
| 4861 | got.br_state))) |
| 4862 | goto done; |
| 4863 | break; |
| 4864 | |
| 4865 | case 1: |
| 4866 | /* |
| 4867 | * Deleting the last part of the extent. |
| 4868 | */ |
| 4869 | temp = got.br_blockcount - del->br_blockcount; |
| 4870 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4871 | xfs_bmbt_set_blockcount(ep, temp); |
| 4872 | if (delay) { |
| 4873 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 4874 | da_old); |
| 4875 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4876 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4877 | da_new = temp; |
| 4878 | break; |
| 4879 | } |
| 4880 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4881 | if (!cur) { |
| 4882 | flags |= xfs_ilog_fext(whichfork); |
| 4883 | break; |
| 4884 | } |
| 4885 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 4886 | got.br_startblock, |
| 4887 | got.br_blockcount - del->br_blockcount, |
| 4888 | got.br_state))) |
| 4889 | goto done; |
| 4890 | break; |
| 4891 | |
| 4892 | case 0: |
| 4893 | /* |
| 4894 | * Deleting the middle of the extent. |
| 4895 | */ |
| 4896 | temp = del->br_startoff - got.br_startoff; |
| 4897 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 4898 | xfs_bmbt_set_blockcount(ep, temp); |
| 4899 | new.br_startoff = del_endoff; |
| 4900 | temp2 = got_endoff - del_endoff; |
| 4901 | new.br_blockcount = temp2; |
| 4902 | new.br_state = got.br_state; |
| 4903 | if (!delay) { |
| 4904 | new.br_startblock = del_endblock; |
| 4905 | flags |= XFS_ILOG_CORE; |
| 4906 | if (cur) { |
| 4907 | if ((error = xfs_bmbt_update(cur, |
| 4908 | got.br_startoff, |
| 4909 | got.br_startblock, temp, |
| 4910 | got.br_state))) |
| 4911 | goto done; |
| 4912 | if ((error = xfs_btree_increment(cur, 0, &i))) |
| 4913 | goto done; |
| 4914 | cur->bc_rec.b = new; |
| 4915 | error = xfs_btree_insert(cur, &i); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4916 | if (error && error != -ENOSPC) |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4917 | goto done; |
| 4918 | /* |
| 4919 | * If get no-space back from btree insert, |
| 4920 | * it tried a split, and we have a zero |
| 4921 | * block reservation. |
| 4922 | * Fix up our state and return the error. |
| 4923 | */ |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4924 | if (error == -ENOSPC) { |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4925 | /* |
| 4926 | * Reset the cursor, don't trust |
| 4927 | * it after any insert operation. |
| 4928 | */ |
| 4929 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 4930 | got.br_startoff, |
| 4931 | got.br_startblock, |
| 4932 | temp, &i))) |
| 4933 | goto done; |
| 4934 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
| 4935 | /* |
| 4936 | * Update the btree record back |
| 4937 | * to the original value. |
| 4938 | */ |
| 4939 | if ((error = xfs_bmbt_update(cur, |
| 4940 | got.br_startoff, |
| 4941 | got.br_startblock, |
| 4942 | got.br_blockcount, |
| 4943 | got.br_state))) |
| 4944 | goto done; |
| 4945 | /* |
| 4946 | * Reset the extent record back |
| 4947 | * to the original value. |
| 4948 | */ |
| 4949 | xfs_bmbt_set_blockcount(ep, |
| 4950 | got.br_blockcount); |
| 4951 | flags = 0; |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 4952 | error = -ENOSPC; |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 4953 | goto done; |
| 4954 | } |
| 4955 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
| 4956 | } else |
| 4957 | flags |= xfs_ilog_fext(whichfork); |
| 4958 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 4959 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 4960 | } else { |
| 4961 | ASSERT(whichfork == XFS_DATA_FORK); |
| 4962 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 4963 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
| 4964 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
| 4965 | new.br_startblock = nullstartblock((int)temp2); |
| 4966 | da_new = temp + temp2; |
| 4967 | while (da_new > da_old) { |
| 4968 | if (temp) { |
| 4969 | temp--; |
| 4970 | da_new--; |
| 4971 | xfs_bmbt_set_startblock(ep, |
| 4972 | nullstartblock((int)temp)); |
| 4973 | } |
| 4974 | if (da_new == da_old) |
| 4975 | break; |
| 4976 | if (temp2) { |
| 4977 | temp2--; |
| 4978 | da_new--; |
| 4979 | new.br_startblock = |
| 4980 | nullstartblock((int)temp2); |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 4985 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 4986 | ++*idx; |
| 4987 | break; |
| 4988 | } |
| 4989 | /* |
| 4990 | * If we need to, add to list of extents to delete. |
| 4991 | */ |
| 4992 | if (do_fx) |
| 4993 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 4994 | mp); |
| 4995 | /* |
| 4996 | * Adjust inode # blocks in the file. |
| 4997 | */ |
| 4998 | if (nblks) |
| 4999 | ip->i_d.di_nblocks -= nblks; |
| 5000 | /* |
| 5001 | * Adjust quota data. |
| 5002 | */ |
| 5003 | if (qfield) |
| 5004 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
| 5005 | |
| 5006 | /* |
| 5007 | * Account for change in delayed indirect blocks. |
| 5008 | * Nothing to do for disk quota accounting here. |
| 5009 | */ |
| 5010 | ASSERT(da_old >= da_new); |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 5011 | if (da_old > da_new) |
| 5012 | xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false); |
Dave Chinner | 9e5987a7 | 2013-02-25 12:31:26 +1100 | [diff] [blame] | 5013 | done: |
| 5014 | *logflagsp = flags; |
| 5015 | return error; |
| 5016 | } |
| 5017 | |
| 5018 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | * Unmap (remove) blocks from a file. |
| 5020 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5021 | * that value. If not all extents in the block range can be removed then |
| 5022 | * *done is set. |
| 5023 | */ |
| 5024 | int /* error */ |
| 5025 | xfs_bunmapi( |
| 5026 | xfs_trans_t *tp, /* transaction pointer */ |
| 5027 | struct xfs_inode *ip, /* incore inode */ |
| 5028 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5029 | xfs_filblks_t len, /* length to unmap in file */ |
| 5030 | int flags, /* misc flags */ |
| 5031 | xfs_extnum_t nexts, /* number of extents max */ |
| 5032 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5033 | controls a.g. for allocs */ |
| 5034 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 5035 | int *done) /* set if not done yet */ |
| 5036 | { |
| 5037 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5038 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5039 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5040 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | int error; /* error return value */ |
| 5042 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5043 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5044 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5045 | int isrt; /* freeing in rt area */ |
| 5046 | xfs_extnum_t lastx; /* last extent index used */ |
| 5047 | int logflags; /* transaction logging flags */ |
| 5048 | xfs_extlen_t mod; /* rt extent offset */ |
| 5049 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5050 | xfs_extnum_t nextents; /* number of file extents */ |
| 5051 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5053 | int tmp_logflags; /* partial logging flags */ |
| 5054 | int wasdel; /* was a delayed alloc extent */ |
| 5055 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | xfs_fsblock_t sum; |
| 5057 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5058 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5061 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5062 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5063 | if (unlikely( |
| 5064 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5065 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5066 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5067 | ip->i_mount); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5068 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | } |
| 5070 | mp = ip->i_mount; |
| 5071 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5072 | return -EIO; |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5073 | |
Christoph Hellwig | eef334e | 2013-12-06 12:30:17 -0800 | [diff] [blame] | 5074 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | ASSERT(len > 0); |
| 5076 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5079 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5080 | return error; |
| 5081 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5082 | if (nextents == 0) { |
| 5083 | *done = 1; |
| 5084 | return 0; |
| 5085 | } |
| 5086 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5087 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | start = bno; |
| 5089 | bno = start + len - 1; |
| 5090 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5091 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | /* |
| 5094 | * Check to see if the given block number is past the end of the |
| 5095 | * file, back up to the last block if so... |
| 5096 | */ |
| 5097 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5098 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | xfs_bmbt_get_all(ep, &got); |
| 5100 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5101 | } |
| 5102 | logflags = 0; |
| 5103 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5104 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5105 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | cur->bc_private.b.firstblock = *firstblock; |
| 5107 | cur->bc_private.b.flist = flist; |
| 5108 | cur->bc_private.b.flags = 0; |
| 5109 | } else |
| 5110 | cur = NULL; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5111 | |
| 5112 | if (isrt) { |
| 5113 | /* |
| 5114 | * Synchronize by locking the bitmap inode. |
| 5115 | */ |
| 5116 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 5117 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
| 5118 | } |
| 5119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | extno = 0; |
| 5121 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5122 | (nexts == 0 || extno < nexts)) { |
| 5123 | /* |
| 5124 | * Is the found extent after a hole in which bno lives? |
| 5125 | * Just back up to the previous extent, if so. |
| 5126 | */ |
| 5127 | if (got.br_startoff > bno) { |
| 5128 | if (--lastx < 0) |
| 5129 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5130 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | xfs_bmbt_get_all(ep, &got); |
| 5132 | } |
| 5133 | /* |
| 5134 | * Is the last block of this extent before the range |
| 5135 | * we're supposed to delete? If so, we're done. |
| 5136 | */ |
| 5137 | bno = XFS_FILEOFF_MIN(bno, |
| 5138 | got.br_startoff + got.br_blockcount - 1); |
| 5139 | if (bno < start) |
| 5140 | break; |
| 5141 | /* |
| 5142 | * Then deal with the (possibly delayed) allocated space |
| 5143 | * we found. |
| 5144 | */ |
| 5145 | ASSERT(ep != NULL); |
| 5146 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5147 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | if (got.br_startoff < start) { |
| 5149 | del.br_startoff = start; |
| 5150 | del.br_blockcount -= start - got.br_startoff; |
| 5151 | if (!wasdel) |
| 5152 | del.br_startblock += start - got.br_startoff; |
| 5153 | } |
| 5154 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5155 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5156 | sum = del.br_startblock + del.br_blockcount; |
| 5157 | if (isrt && |
| 5158 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5159 | /* |
| 5160 | * Realtime extent not lined up at the end. |
| 5161 | * The extent could have been split into written |
| 5162 | * and unwritten pieces, or we could just be |
| 5163 | * unmapping part of it. But we can't really |
| 5164 | * get rid of part of a realtime extent. |
| 5165 | */ |
| 5166 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5167 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | /* |
| 5169 | * This piece is unwritten, or we're not |
| 5170 | * using unwritten extents. Skip over it. |
| 5171 | */ |
| 5172 | ASSERT(bno >= mod); |
| 5173 | bno -= mod > del.br_blockcount ? |
| 5174 | del.br_blockcount : mod; |
| 5175 | if (bno < got.br_startoff) { |
| 5176 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5177 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5178 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | } |
| 5180 | continue; |
| 5181 | } |
| 5182 | /* |
| 5183 | * It's written, turn it unwritten. |
| 5184 | * This is better than zeroing it. |
| 5185 | */ |
| 5186 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5187 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5188 | /* |
| 5189 | * If this spans a realtime extent boundary, |
| 5190 | * chop it back to the start of the one we end at. |
| 5191 | */ |
| 5192 | if (del.br_blockcount > mod) { |
| 5193 | del.br_startoff += del.br_blockcount - mod; |
| 5194 | del.br_startblock += del.br_blockcount - mod; |
| 5195 | del.br_blockcount = mod; |
| 5196 | } |
| 5197 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5198 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
| 5199 | &lastx, &cur, &del, firstblock, flist, |
| 5200 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | if (error) |
| 5202 | goto error0; |
| 5203 | goto nodelete; |
| 5204 | } |
| 5205 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5206 | /* |
| 5207 | * Realtime extent is lined up at the end but not |
| 5208 | * at the front. We'll get rid of full extents if |
| 5209 | * we can. |
| 5210 | */ |
| 5211 | mod = mp->m_sb.sb_rextsize - mod; |
| 5212 | if (del.br_blockcount > mod) { |
| 5213 | del.br_blockcount -= mod; |
| 5214 | del.br_startoff += mod; |
| 5215 | del.br_startblock += mod; |
| 5216 | } else if ((del.br_startoff == start && |
| 5217 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5218 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5219 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | /* |
| 5221 | * Can't make it unwritten. There isn't |
| 5222 | * a full extent here so just skip it. |
| 5223 | */ |
| 5224 | ASSERT(bno >= del.br_blockcount); |
| 5225 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5226 | if (got.br_startoff > bno) { |
| 5227 | if (--lastx >= 0) { |
| 5228 | ep = xfs_iext_get_ext(ifp, |
| 5229 | lastx); |
| 5230 | xfs_bmbt_get_all(ep, &got); |
| 5231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | } |
| 5233 | continue; |
| 5234 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5235 | /* |
| 5236 | * This one is already unwritten. |
| 5237 | * It must have a written left neighbor. |
| 5238 | * Unwrite the killed part of that one and |
| 5239 | * try again. |
| 5240 | */ |
| 5241 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5242 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5243 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5245 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | ASSERT(del.br_startblock == |
| 5247 | prev.br_startblock + prev.br_blockcount); |
| 5248 | if (prev.br_startoff < start) { |
| 5249 | mod = start - prev.br_startoff; |
| 5250 | prev.br_blockcount -= mod; |
| 5251 | prev.br_startblock += mod; |
| 5252 | prev.br_startoff = start; |
| 5253 | } |
| 5254 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5255 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5256 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5257 | ip, &lastx, &cur, &prev, |
| 5258 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | if (error) |
| 5260 | goto error0; |
| 5261 | goto nodelete; |
| 5262 | } else { |
| 5263 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5264 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5265 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5266 | ip, &lastx, &cur, &del, |
| 5267 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | if (error) |
| 5269 | goto error0; |
| 5270 | goto nodelete; |
| 5271 | } |
| 5272 | } |
| 5273 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5274 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5275 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5276 | if (isrt) { |
| 5277 | xfs_filblks_t rtexts; |
| 5278 | |
| 5279 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5280 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5281 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5282 | (int64_t)rtexts, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5283 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5284 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5285 | XFS_QMOPT_RES_RTBLKS); |
| 5286 | } else { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame^] | 5287 | xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, |
| 5288 | false); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5289 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5290 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | ip->i_delayed_blks -= del.br_blockcount; |
| 5294 | if (cur) |
| 5295 | cur->bc_private.b.flags |= |
| 5296 | XFS_BTCUR_BPRV_WASDEL; |
| 5297 | } else if (cur) |
| 5298 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5299 | /* |
| 5300 | * If it's the case where the directory code is running |
| 5301 | * with no block reservation, and the deleted block is in |
| 5302 | * the middle of its extent, and the resulting insert |
| 5303 | * of an extent would cause transformation to btree format, |
| 5304 | * then reject it. The calling code will then swap |
| 5305 | * blocks around instead. |
| 5306 | * We have to do this now, rather than waiting for the |
| 5307 | * conversion to btree format, since the transaction |
| 5308 | * will be dirty. |
| 5309 | */ |
| 5310 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5311 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5312 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5313 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | del.br_startoff > got.br_startoff && |
| 5315 | del.br_startoff + del.br_blockcount < |
| 5316 | got.br_startoff + got.br_blockcount) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5317 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | goto error0; |
| 5319 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5320 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5321 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | logflags |= tmp_logflags; |
| 5323 | if (error) |
| 5324 | goto error0; |
| 5325 | bno = del.br_startoff - 1; |
| 5326 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | /* |
| 5328 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5329 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5331 | if (lastx >= 0) { |
| 5332 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5333 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5334 | if (--lastx >= 0) |
| 5335 | ep = xfs_iext_get_ext(ifp, |
| 5336 | lastx); |
| 5337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5340 | extno++; |
| 5341 | } |
| 5342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | /* |
| 5346 | * Convert to a btree if necessary. |
| 5347 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5348 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | ASSERT(cur == NULL); |
| 5350 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5351 | &cur, 0, &tmp_logflags, whichfork); |
| 5352 | logflags |= tmp_logflags; |
| 5353 | if (error) |
| 5354 | goto error0; |
| 5355 | } |
| 5356 | /* |
| 5357 | * transform from btree to extents, give it cur |
| 5358 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5359 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | ASSERT(cur != NULL); |
| 5361 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5362 | whichfork); |
| 5363 | logflags |= tmp_logflags; |
| 5364 | if (error) |
| 5365 | goto error0; |
| 5366 | } |
| 5367 | /* |
| 5368 | * transform from extents to local? |
| 5369 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | error = 0; |
| 5371 | error0: |
| 5372 | /* |
| 5373 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5374 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5376 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5378 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5379 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5381 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | /* |
| 5383 | * Log inode even in the error case, if the transaction |
| 5384 | * is dirty we'll need to shut down the filesystem. |
| 5385 | */ |
| 5386 | if (logflags) |
| 5387 | xfs_trans_log_inode(tp, ip, logflags); |
| 5388 | if (cur) { |
| 5389 | if (!error) { |
| 5390 | *firstblock = cur->bc_private.b.firstblock; |
| 5391 | cur->bc_private.b.allocated = 0; |
| 5392 | } |
| 5393 | xfs_btree_del_cursor(cur, |
| 5394 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5395 | } |
| 5396 | return error; |
| 5397 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5398 | |
| 5399 | /* |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5400 | * Determine whether an extent shift can be accomplished by a merge with the |
| 5401 | * extent that precedes the target hole of the shift. |
| 5402 | */ |
| 5403 | STATIC bool |
| 5404 | xfs_bmse_can_merge( |
| 5405 | struct xfs_bmbt_irec *left, /* preceding extent */ |
| 5406 | struct xfs_bmbt_irec *got, /* current extent to shift */ |
| 5407 | xfs_fileoff_t shift) /* shift fsb */ |
| 5408 | { |
| 5409 | xfs_fileoff_t startoff; |
| 5410 | |
| 5411 | startoff = got->br_startoff - shift; |
| 5412 | |
| 5413 | /* |
| 5414 | * The extent, once shifted, must be adjacent in-file and on-disk with |
| 5415 | * the preceding extent. |
| 5416 | */ |
| 5417 | if ((left->br_startoff + left->br_blockcount != startoff) || |
| 5418 | (left->br_startblock + left->br_blockcount != got->br_startblock) || |
| 5419 | (left->br_state != got->br_state) || |
| 5420 | (left->br_blockcount + got->br_blockcount > MAXEXTLEN)) |
| 5421 | return false; |
| 5422 | |
| 5423 | return true; |
| 5424 | } |
| 5425 | |
| 5426 | /* |
| 5427 | * A bmap extent shift adjusts the file offset of an extent to fill a preceding |
| 5428 | * hole in the file. If an extent shift would result in the extent being fully |
| 5429 | * adjacent to the extent that currently precedes the hole, we can merge with |
| 5430 | * the preceding extent rather than do the shift. |
| 5431 | * |
| 5432 | * This function assumes the caller has verified a shift-by-merge is possible |
| 5433 | * with the provided extents via xfs_bmse_can_merge(). |
| 5434 | */ |
| 5435 | STATIC int |
| 5436 | xfs_bmse_merge( |
| 5437 | struct xfs_inode *ip, |
| 5438 | int whichfork, |
| 5439 | xfs_fileoff_t shift, /* shift fsb */ |
| 5440 | int current_ext, /* idx of gotp */ |
| 5441 | struct xfs_bmbt_rec_host *gotp, /* extent to shift */ |
| 5442 | struct xfs_bmbt_rec_host *leftp, /* preceding extent */ |
| 5443 | struct xfs_btree_cur *cur, |
| 5444 | int *logflags) /* output */ |
| 5445 | { |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5446 | struct xfs_bmbt_irec got; |
| 5447 | struct xfs_bmbt_irec left; |
| 5448 | xfs_filblks_t blockcount; |
| 5449 | int error, i; |
| 5450 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5451 | xfs_bmbt_get_all(gotp, &got); |
| 5452 | xfs_bmbt_get_all(leftp, &left); |
| 5453 | blockcount = left.br_blockcount + got.br_blockcount; |
| 5454 | |
| 5455 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5456 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 5457 | ASSERT(xfs_bmse_can_merge(&left, &got, shift)); |
| 5458 | |
| 5459 | /* |
| 5460 | * Merge the in-core extents. Note that the host record pointers and |
| 5461 | * current_ext index are invalid once the extent has been removed via |
| 5462 | * xfs_iext_remove(). |
| 5463 | */ |
| 5464 | xfs_bmbt_set_blockcount(leftp, blockcount); |
| 5465 | xfs_iext_remove(ip, current_ext, 1, 0); |
| 5466 | |
| 5467 | /* |
| 5468 | * Update the on-disk extent count, the btree if necessary and log the |
| 5469 | * inode. |
| 5470 | */ |
| 5471 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 5472 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 5473 | *logflags |= XFS_ILOG_CORE; |
| 5474 | if (!cur) { |
| 5475 | *logflags |= XFS_ILOG_DEXT; |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
| 5479 | /* lookup and remove the extent to merge */ |
| 5480 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5481 | got.br_blockcount, &i); |
| 5482 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5483 | return error; |
| 5484 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5485 | |
| 5486 | error = xfs_btree_delete(cur, &i); |
| 5487 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5488 | return error; |
| 5489 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5490 | |
| 5491 | /* lookup and update size of the previous extent */ |
| 5492 | error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock, |
| 5493 | left.br_blockcount, &i); |
| 5494 | if (error) |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5495 | return error; |
| 5496 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5497 | |
| 5498 | left.br_blockcount = blockcount; |
| 5499 | |
Dave Chinner | 4db431f | 2014-12-04 09:42:40 +1100 | [diff] [blame] | 5500 | return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock, |
| 5501 | left.br_blockcount, left.br_state); |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | /* |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5505 | * Shift a single extent. |
| 5506 | */ |
| 5507 | STATIC int |
| 5508 | xfs_bmse_shift_one( |
| 5509 | struct xfs_inode *ip, |
| 5510 | int whichfork, |
| 5511 | xfs_fileoff_t offset_shift_fsb, |
| 5512 | int *current_ext, |
| 5513 | struct xfs_bmbt_rec_host *gotp, |
| 5514 | struct xfs_btree_cur *cur, |
| 5515 | int *logflags) |
| 5516 | { |
| 5517 | struct xfs_ifork *ifp; |
| 5518 | xfs_fileoff_t startoff; |
| 5519 | struct xfs_bmbt_rec_host *leftp; |
| 5520 | struct xfs_bmbt_irec got; |
| 5521 | struct xfs_bmbt_irec left; |
| 5522 | int error; |
| 5523 | int i; |
| 5524 | |
| 5525 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5526 | |
| 5527 | xfs_bmbt_get_all(gotp, &got); |
| 5528 | startoff = got.br_startoff - offset_shift_fsb; |
| 5529 | |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5530 | /* delalloc extents should be prevented by caller */ |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5531 | XFS_WANT_CORRUPTED_RETURN(!isnullstartblock(got.br_startblock)); |
Brian Foster | f71721d | 2014-09-23 15:39:05 +1000 | [diff] [blame] | 5532 | |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5533 | /* |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5534 | * Check for merge if we've got an extent to the left, otherwise make |
| 5535 | * sure there's enough room at the start of the file for the shift. |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5536 | */ |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5537 | if (*current_ext) { |
| 5538 | /* grab the left extent and check for a large enough hole */ |
| 5539 | leftp = xfs_iext_get_ext(ifp, *current_ext - 1); |
| 5540 | xfs_bmbt_get_all(leftp, &left); |
| 5541 | |
| 5542 | if (startoff < left.br_startoff + left.br_blockcount) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5543 | return -EINVAL; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5544 | |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5545 | /* check whether to merge the extent or shift it down */ |
| 5546 | if (xfs_bmse_can_merge(&left, &got, offset_shift_fsb)) { |
| 5547 | return xfs_bmse_merge(ip, whichfork, offset_shift_fsb, |
| 5548 | *current_ext, gotp, leftp, cur, |
| 5549 | logflags); |
| 5550 | } |
| 5551 | } else if (got.br_startoff < offset_shift_fsb) |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5552 | return -EINVAL; |
| 5553 | |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5554 | /* |
| 5555 | * Increment the extent index for the next iteration, update the start |
| 5556 | * offset of the in-core extent and update the btree if applicable. |
| 5557 | */ |
| 5558 | (*current_ext)++; |
| 5559 | xfs_bmbt_set_startoff(gotp, startoff); |
| 5560 | *logflags |= XFS_ILOG_CORE; |
| 5561 | if (!cur) { |
| 5562 | *logflags |= XFS_ILOG_DEXT; |
| 5563 | return 0; |
| 5564 | } |
| 5565 | |
| 5566 | error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock, |
| 5567 | got.br_blockcount, &i); |
| 5568 | if (error) |
| 5569 | return error; |
Dave Chinner | b11bd67 | 2014-12-04 09:42:24 +1100 | [diff] [blame] | 5570 | XFS_WANT_CORRUPTED_RETURN(i == 1); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5571 | |
| 5572 | got.br_startoff = startoff; |
kbuild test robot | d254aae | 2014-12-01 08:42:52 +1100 | [diff] [blame] | 5573 | return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock, |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5574 | got.br_blockcount, got.br_state); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | /* |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5578 | * Shift extent records to the left to cover a hole. |
| 5579 | * |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5580 | * The maximum number of extents to be shifted in a single operation is |
| 5581 | * @num_exts. @start_fsb specifies the file offset to start the shift and the |
| 5582 | * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb |
| 5583 | * is the length by which each extent is shifted. If there is no hole to shift |
| 5584 | * the extents into, this will be considered invalid operation and we abort |
| 5585 | * immediately. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5586 | */ |
| 5587 | int |
| 5588 | xfs_bmap_shift_extents( |
| 5589 | struct xfs_trans *tp, |
| 5590 | struct xfs_inode *ip, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5591 | xfs_fileoff_t start_fsb, |
| 5592 | xfs_fileoff_t offset_shift_fsb, |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5593 | int *done, |
| 5594 | xfs_fileoff_t *next_fsb, |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5595 | xfs_fsblock_t *firstblock, |
| 5596 | struct xfs_bmap_free *flist, |
| 5597 | int num_exts) |
| 5598 | { |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5599 | struct xfs_btree_cur *cur = NULL; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5600 | struct xfs_bmbt_rec_host *gotp; |
| 5601 | struct xfs_bmbt_irec got; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5602 | struct xfs_mount *mp = ip->i_mount; |
| 5603 | struct xfs_ifork *ifp; |
| 5604 | xfs_extnum_t nexts = 0; |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5605 | xfs_extnum_t current_ext; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5606 | int error = 0; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5607 | int whichfork = XFS_DATA_FORK; |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5608 | int logflags = 0; |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5609 | int total_extents; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5610 | |
| 5611 | if (unlikely(XFS_TEST_ERROR( |
| 5612 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5613 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 5614 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 5615 | XFS_ERROR_REPORT("xfs_bmap_shift_extents", |
| 5616 | XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5617 | return -EFSCORRUPTED; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | if (XFS_FORCED_SHUTDOWN(mp)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 5621 | return -EIO; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5622 | |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5623 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
| 5624 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5625 | |
| 5626 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5627 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 5628 | /* Read in all the extents */ |
| 5629 | error = xfs_iread_extents(tp, ip, whichfork); |
| 5630 | if (error) |
| 5631 | return error; |
| 5632 | } |
| 5633 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5634 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5635 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
| 5636 | cur->bc_private.b.firstblock = *firstblock; |
| 5637 | cur->bc_private.b.flist = flist; |
| 5638 | cur->bc_private.b.flags = 0; |
| 5639 | } |
| 5640 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5641 | /* |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5642 | * Look up the extent index for the fsb where we start shifting. We can |
| 5643 | * henceforth iterate with current_ext as extent list changes are locked |
| 5644 | * out via ilock. |
| 5645 | * |
| 5646 | * gotp can be null in 2 cases: 1) if there are no extents or 2) |
| 5647 | * start_fsb lies in a hole beyond which there are no extents. Either |
| 5648 | * way, we are done. |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5649 | */ |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5650 | gotp = xfs_iext_bno_to_ext(ifp, start_fsb, ¤t_ext); |
| 5651 | if (!gotp) { |
| 5652 | *done = 1; |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5653 | goto del_cursor; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5654 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5655 | |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5656 | /* |
| 5657 | * 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] | 5658 | * are collapsing out, so we cannot use the count of real extents here. |
| 5659 | * Instead we have to calculate it from the incore fork. |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5660 | */ |
| 5661 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5662 | while (nexts++ < num_exts && current_ext < total_extents) { |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5663 | error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb, |
| 5664 | ¤t_ext, gotp, cur, &logflags); |
| 5665 | if (error) |
| 5666 | goto del_cursor; |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5667 | |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5668 | /* update total extent count and grab the next record */ |
Dave Chinner | d39a2ce | 2014-04-17 08:15:25 +1000 | [diff] [blame] | 5669 | total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5670 | if (current_ext >= total_extents) |
Brian Foster | ddb19e3 | 2014-09-23 15:38:09 +1000 | [diff] [blame] | 5671 | break; |
| 5672 | gotp = xfs_iext_get_ext(ifp, current_ext); |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5673 | } |
| 5674 | |
| 5675 | /* Check if we are done */ |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5676 | if (current_ext == total_extents) { |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5677 | *done = 1; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5678 | } else if (next_fsb) { |
| 5679 | xfs_bmbt_get_all(gotp, &got); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5680 | *next_fsb = got.br_startoff; |
Brian Foster | a979bdf | 2014-09-23 15:39:04 +1000 | [diff] [blame] | 5681 | } |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5682 | |
| 5683 | del_cursor: |
| 5684 | if (cur) |
| 5685 | xfs_btree_del_cursor(cur, |
| 5686 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5687 | |
Brian Foster | ca446d8 | 2014-09-02 12:12:53 +1000 | [diff] [blame] | 5688 | if (logflags) |
| 5689 | xfs_trans_log_inode(tp, ip, logflags); |
Brian Foster | 2c845f5 | 2014-09-23 15:37:09 +1000 | [diff] [blame] | 5690 | |
Namjae Jeon | e1d8fb8 | 2014-02-24 10:58:19 +1100 | [diff] [blame] | 5691 | return error; |
| 5692 | } |