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