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