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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_itable.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_extfree_item.h" |
| 39 | #include "xfs_alloc.h" |
| 40 | #include "xfs_bmap.h" |
| 41 | #include "xfs_rtalloc.h" |
| 42 | #include "xfs_error.h" |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 43 | #include "xfs_attr_leaf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_rw.h" |
| 45 | #include "xfs_quota.h" |
| 46 | #include "xfs_trans_space.h" |
| 47 | #include "xfs_buf_item.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 49 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 50 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 54 | |
| 55 | /* |
| 56 | * Prototypes for internal bmap routines. |
| 57 | */ |
| 58 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 59 | #ifdef DEBUG |
| 60 | STATIC void |
| 61 | xfs_bmap_check_leaf_extents( |
| 62 | struct xfs_btree_cur *cur, |
| 63 | struct xfs_inode *ip, |
| 64 | int whichfork); |
| 65 | #else |
| 66 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 67 | #endif |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 72 | */ |
| 73 | STATIC int /* error */ |
| 74 | xfs_bmap_add_attrfork_extents( |
| 75 | xfs_trans_t *tp, /* transaction pointer */ |
| 76 | xfs_inode_t *ip, /* incore inode pointer */ |
| 77 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 78 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 79 | int *flags); /* inode logging flags */ |
| 80 | |
| 81 | /* |
| 82 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 83 | */ |
| 84 | STATIC int /* error */ |
| 85 | xfs_bmap_add_attrfork_local( |
| 86 | xfs_trans_t *tp, /* transaction pointer */ |
| 87 | xfs_inode_t *ip, /* incore inode pointer */ |
| 88 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 89 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 90 | int *flags); /* inode logging flags */ |
| 91 | |
| 92 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 94 | * It figures out where to ask the underlying allocator to put the new extent. |
| 95 | */ |
| 96 | STATIC int /* error */ |
| 97 | xfs_bmap_alloc( |
| 98 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 99 | |
| 100 | /* |
| 101 | * Transform a btree format file with only one leaf node, where the |
| 102 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 103 | * Since the file extents are already in-core, all we have to do is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * give up the space for the btree root and pitch the leaf block. |
| 105 | */ |
| 106 | STATIC int /* error */ |
| 107 | xfs_bmap_btree_to_extents( |
| 108 | xfs_trans_t *tp, /* transaction pointer */ |
| 109 | xfs_inode_t *ip, /* incore inode pointer */ |
| 110 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 111 | int *logflagsp, /* inode logging flags */ |
| 112 | int whichfork); /* data or attr fork */ |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * Remove the entry "free" from the free item list. Prev points to the |
| 116 | * previous entry, unless "free" is the head of the list. |
| 117 | */ |
| 118 | STATIC void |
| 119 | xfs_bmap_del_free( |
| 120 | xfs_bmap_free_t *flist, /* free item list header */ |
| 121 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 122 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 123 | |
| 124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * Convert an extents-format file into a btree-format file. |
| 126 | * The new file will have a root block (in the inode) and a single child block. |
| 127 | */ |
| 128 | STATIC int /* error */ |
| 129 | xfs_bmap_extents_to_btree( |
| 130 | xfs_trans_t *tp, /* transaction pointer */ |
| 131 | xfs_inode_t *ip, /* incore inode pointer */ |
| 132 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 133 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 134 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 135 | int wasdel, /* converting a delayed alloc */ |
| 136 | int *logflagsp, /* inode logging flags */ |
| 137 | int whichfork); /* data or attr fork */ |
| 138 | |
| 139 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * Convert a local file to an extents file. |
| 141 | * This code is sort of bogus, since the file data needs to get |
| 142 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 143 | */ |
| 144 | STATIC int /* error */ |
| 145 | xfs_bmap_local_to_extents( |
| 146 | xfs_trans_t *tp, /* transaction pointer */ |
| 147 | xfs_inode_t *ip, /* incore inode pointer */ |
| 148 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 149 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 150 | int *logflagsp, /* inode logging flags */ |
| 151 | int whichfork); /* data or attr fork */ |
| 152 | |
| 153 | /* |
| 154 | * Search the extents list for the inode, for the extent containing bno. |
| 155 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 156 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 157 | * Else, *lastxp will be set to the index of the found |
| 158 | * entry; *gotp will contain the entry. |
| 159 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 160 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | xfs_bmap_search_extents( |
| 162 | xfs_inode_t *ip, /* incore inode pointer */ |
| 163 | xfs_fileoff_t bno, /* block number searched for */ |
| 164 | int whichfork, /* data or attr fork */ |
| 165 | int *eofp, /* out: end of file found */ |
| 166 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 167 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 168 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 169 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 170 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * Compute the worst-case number of indirect blocks that will be used |
| 172 | * for ip's delayed extent of length "len". |
| 173 | */ |
| 174 | STATIC xfs_filblks_t |
| 175 | xfs_bmap_worst_indlen( |
| 176 | xfs_inode_t *ip, /* incore inode pointer */ |
| 177 | xfs_filblks_t len); /* delayed extent length */ |
| 178 | |
| 179 | #ifdef DEBUG |
| 180 | /* |
| 181 | * Perform various validation checks on the values being returned |
| 182 | * from xfs_bmapi(). |
| 183 | */ |
| 184 | STATIC void |
| 185 | xfs_bmap_validate_ret( |
| 186 | xfs_fileoff_t bno, |
| 187 | xfs_filblks_t len, |
| 188 | int flags, |
| 189 | xfs_bmbt_irec_t *mval, |
| 190 | int nmap, |
| 191 | int ret_nmap); |
| 192 | #else |
| 193 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 194 | #endif /* DEBUG */ |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | STATIC int |
| 197 | xfs_bmap_count_tree( |
| 198 | xfs_mount_t *mp, |
| 199 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 200 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | xfs_fsblock_t blockno, |
| 202 | int levelin, |
| 203 | int *count); |
| 204 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 205 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 207 | xfs_ifork_t *ifp, |
| 208 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | int numrecs, |
| 210 | int *count); |
| 211 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 212 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 213 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 214 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 215 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 216 | int numrecs, |
| 217 | int *count); |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* |
| 220 | * Bmap internal routines. |
| 221 | */ |
| 222 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 223 | STATIC int /* error */ |
| 224 | xfs_bmbt_lookup_eq( |
| 225 | struct xfs_btree_cur *cur, |
| 226 | xfs_fileoff_t off, |
| 227 | xfs_fsblock_t bno, |
| 228 | xfs_filblks_t len, |
| 229 | int *stat) /* success/failure */ |
| 230 | { |
| 231 | cur->bc_rec.b.br_startoff = off; |
| 232 | cur->bc_rec.b.br_startblock = bno; |
| 233 | cur->bc_rec.b.br_blockcount = len; |
| 234 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 235 | } |
| 236 | |
| 237 | STATIC int /* error */ |
| 238 | xfs_bmbt_lookup_ge( |
| 239 | struct xfs_btree_cur *cur, |
| 240 | xfs_fileoff_t off, |
| 241 | xfs_fsblock_t bno, |
| 242 | xfs_filblks_t len, |
| 243 | int *stat) /* success/failure */ |
| 244 | { |
| 245 | cur->bc_rec.b.br_startoff = off; |
| 246 | cur->bc_rec.b.br_startblock = bno; |
| 247 | cur->bc_rec.b.br_blockcount = len; |
| 248 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 249 | } |
| 250 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 251 | /* |
| 252 | * Update the record referred to by cur to the value given |
| 253 | * by [off, bno, len, state]. |
| 254 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 255 | */ |
| 256 | STATIC int |
| 257 | xfs_bmbt_update( |
| 258 | struct xfs_btree_cur *cur, |
| 259 | xfs_fileoff_t off, |
| 260 | xfs_fsblock_t bno, |
| 261 | xfs_filblks_t len, |
| 262 | xfs_exntst_t state) |
| 263 | { |
| 264 | union xfs_btree_rec rec; |
| 265 | |
| 266 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 267 | return xfs_btree_update(cur, &rec); |
| 268 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | /* |
| 271 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 272 | */ |
| 273 | STATIC int /* error */ |
| 274 | xfs_bmap_add_attrfork_btree( |
| 275 | xfs_trans_t *tp, /* transaction pointer */ |
| 276 | xfs_inode_t *ip, /* incore inode pointer */ |
| 277 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 278 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 279 | int *flags) /* inode logging flags */ |
| 280 | { |
| 281 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 282 | int error; /* error return value */ |
| 283 | xfs_mount_t *mp; /* file system mount struct */ |
| 284 | int stat; /* newroot status */ |
| 285 | |
| 286 | mp = ip->i_mount; |
| 287 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 288 | *flags |= XFS_ILOG_DBROOT; |
| 289 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 290 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | cur->bc_private.b.flist = flist; |
| 292 | cur->bc_private.b.firstblock = *firstblock; |
| 293 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 294 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 295 | /* must be at least one entry */ |
| 296 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 297 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | goto error0; |
| 299 | if (stat == 0) { |
| 300 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 301 | return XFS_ERROR(ENOSPC); |
| 302 | } |
| 303 | *firstblock = cur->bc_private.b.firstblock; |
| 304 | cur->bc_private.b.allocated = 0; |
| 305 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 306 | } |
| 307 | return 0; |
| 308 | error0: |
| 309 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 310 | return error; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 315 | */ |
| 316 | STATIC int /* error */ |
| 317 | xfs_bmap_add_attrfork_extents( |
| 318 | xfs_trans_t *tp, /* transaction pointer */ |
| 319 | xfs_inode_t *ip, /* incore inode pointer */ |
| 320 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 321 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 322 | int *flags) /* inode logging flags */ |
| 323 | { |
| 324 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 325 | int error; /* error return value */ |
| 326 | |
| 327 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 328 | return 0; |
| 329 | cur = NULL; |
| 330 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 331 | flags, XFS_DATA_FORK); |
| 332 | if (cur) { |
| 333 | cur->bc_private.b.allocated = 0; |
| 334 | xfs_btree_del_cursor(cur, |
| 335 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 336 | } |
| 337 | return error; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 342 | */ |
| 343 | STATIC int /* error */ |
| 344 | xfs_bmap_add_attrfork_local( |
| 345 | xfs_trans_t *tp, /* transaction pointer */ |
| 346 | xfs_inode_t *ip, /* incore inode pointer */ |
| 347 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 348 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 349 | int *flags) /* inode logging flags */ |
| 350 | { |
| 351 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 352 | int error; /* error return value */ |
| 353 | xfs_mount_t *mp; /* mount structure pointer */ |
| 354 | |
| 355 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 356 | return 0; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 357 | if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | mp = ip->i_mount; |
| 359 | memset(&dargs, 0, sizeof(dargs)); |
| 360 | dargs.dp = ip; |
| 361 | dargs.firstblock = firstblock; |
| 362 | dargs.flist = flist; |
| 363 | dargs.total = mp->m_dirblkfsbs; |
| 364 | dargs.whichfork = XFS_DATA_FORK; |
| 365 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 366 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } else |
| 368 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 369 | XFS_DATA_FORK); |
| 370 | return error; |
| 371 | } |
| 372 | |
| 373 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 374 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | */ |
| 376 | STATIC int /* error */ |
| 377 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 378 | struct xfs_bmalloca *bma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 380 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 382 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 385 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 387 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 388 | /* left is 0, right is 1, prev is 2 */ |
| 389 | int rval=0; /* return value (logging flags) */ |
| 390 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 391 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 392 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 393 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 394 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 397 | ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 398 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 399 | ASSERT(bma->idx >= 0); |
| 400 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 401 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 402 | ASSERT(!bma->cur || |
| 403 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 404 | |
| 405 | XFS_STATS_INC(xs_add_exlist); |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | #define LEFT r[0] |
| 408 | #define RIGHT r[1] |
| 409 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * Set up a bunch of variables to make the tests simpler. |
| 413 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 414 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | xfs_bmbt_get_all(ep, &PREV); |
| 416 | new_endoff = new->br_startoff + new->br_blockcount; |
| 417 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 418 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 419 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 420 | da_old = startblockval(PREV.br_startblock); |
| 421 | da_new = 0; |
| 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | /* |
| 424 | * Set flags determining what part of the previous delayed allocation |
| 425 | * extent is being replaced by a real allocation. |
| 426 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 427 | if (PREV.br_startoff == new->br_startoff) |
| 428 | state |= BMAP_LEFT_FILLING; |
| 429 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 430 | state |= BMAP_RIGHT_FILLING; |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* |
| 433 | * Check and set flags if this segment has a left neighbor. |
| 434 | * Don't set contiguous if the combined extent would be too large. |
| 435 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 436 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 437 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 438 | 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] | 439 | |
| 440 | if (isnullstartblock(LEFT.br_startblock)) |
| 441 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 443 | |
| 444 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 445 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 446 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 447 | LEFT.br_state == new->br_state && |
| 448 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 449 | state |= BMAP_LEFT_CONTIG; |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* |
| 452 | * Check and set flags if this segment has a right neighbor. |
| 453 | * Don't set contiguous if the combined extent would be too large. |
| 454 | * Also check for all-three-contiguous being too large. |
| 455 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 456 | 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] | 457 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 458 | 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] | 459 | |
| 460 | if (isnullstartblock(RIGHT.br_startblock)) |
| 461 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 463 | |
| 464 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 465 | new_endoff == RIGHT.br_startoff && |
| 466 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 467 | new->br_state == RIGHT.br_state && |
| 468 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 469 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 470 | BMAP_RIGHT_FILLING)) != |
| 471 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 472 | BMAP_RIGHT_FILLING) || |
| 473 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 474 | <= MAXEXTLEN)) |
| 475 | state |= BMAP_RIGHT_CONTIG; |
| 476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | error = 0; |
| 478 | /* |
| 479 | * Switch out based on the FILLING and CONTIG state bits. |
| 480 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 481 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 482 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 483 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 484 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /* |
| 486 | * Filling in all of a previously delayed allocation extent. |
| 487 | * The left and right neighbors are both contiguous with new. |
| 488 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 489 | bma->idx--; |
| 490 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 491 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | LEFT.br_blockcount + PREV.br_blockcount + |
| 493 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 494 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 495 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 496 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
| 497 | bma->ip->i_d.di_nextents--; |
| 498 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 500 | else { |
| 501 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 502 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 504 | RIGHT.br_blockcount, &i); |
| 505 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 507 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 508 | error = xfs_btree_delete(bma->cur, &i); |
| 509 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 511 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 512 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 513 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 515 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 516 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | LEFT.br_startblock, |
| 518 | LEFT.br_blockcount + |
| 519 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 520 | RIGHT.br_blockcount, LEFT.br_state); |
| 521 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | goto done; |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | break; |
| 525 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 526 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* |
| 528 | * Filling in all of a previously delayed allocation extent. |
| 529 | * The left neighbor is contiguous, the right is not. |
| 530 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 531 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 532 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 533 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 534 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 535 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 536 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 537 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 538 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 539 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | rval = XFS_ILOG_DEXT; |
| 541 | else { |
| 542 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 543 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 545 | &i); |
| 546 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 548 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 549 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | LEFT.br_startblock, |
| 551 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 552 | PREV.br_blockcount, LEFT.br_state); |
| 553 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | goto done; |
| 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | break; |
| 557 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 558 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | /* |
| 560 | * Filling in all of a previously delayed allocation extent. |
| 561 | * The right neighbor is contiguous, the left is not. |
| 562 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 563 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 565 | xfs_bmbt_set_blockcount(ep, |
| 566 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 567 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 568 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 569 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 570 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | rval = XFS_ILOG_DEXT; |
| 572 | else { |
| 573 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 574 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 576 | RIGHT.br_blockcount, &i); |
| 577 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 579 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 580 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | new->br_startblock, |
| 582 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 583 | RIGHT.br_blockcount, PREV.br_state); |
| 584 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | goto done; |
| 586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | break; |
| 588 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 589 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | /* |
| 591 | * Filling in all of a previously delayed allocation extent. |
| 592 | * Neither the left nor right neighbors are contiguous with |
| 593 | * the new one. |
| 594 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 595 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 597 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 598 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 599 | bma->ip->i_d.di_nextents++; |
| 600 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 602 | else { |
| 603 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 604 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 606 | &i); |
| 607 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 609 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 610 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 611 | error = xfs_btree_insert(bma->cur, &i); |
| 612 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 614 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | break; |
| 617 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 618 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | /* |
| 620 | * Filling in the first part of a previous delayed allocation. |
| 621 | * The left neighbor is contiguous. |
| 622 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 623 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 624 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | LEFT.br_blockcount + new->br_blockcount); |
| 626 | xfs_bmbt_set_startoff(ep, |
| 627 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 628 | 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] | 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 631 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 633 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | rval = XFS_ILOG_DEXT; |
| 635 | else { |
| 636 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 637 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 639 | &i); |
| 640 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 642 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 643 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | LEFT.br_startblock, |
| 645 | LEFT.br_blockcount + |
| 646 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 647 | LEFT.br_state); |
| 648 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | goto done; |
| 650 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 651 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 652 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 653 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 654 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 655 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 656 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | break; |
| 658 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 659 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | /* |
| 661 | * Filling in the first part of a previous delayed allocation. |
| 662 | * The left neighbor is not contiguous. |
| 663 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 664 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 666 | temp = PREV.br_blockcount - new->br_blockcount; |
| 667 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 668 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 669 | bma->ip->i_d.di_nextents++; |
| 670 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 672 | else { |
| 673 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 674 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 676 | &i); |
| 677 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 679 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 680 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 681 | error = xfs_btree_insert(bma->cur, &i); |
| 682 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 684 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 686 | if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 687 | bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) { |
| 688 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 689 | bma->firstblock, bma->flist, |
| 690 | &bma->cur, 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | rval |= tmp_rval; |
| 692 | if (error) |
| 693 | goto done; |
| 694 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 695 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 696 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 697 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 698 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 699 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 700 | 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] | 701 | break; |
| 702 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 703 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | /* |
| 705 | * Filling in the last part of a previous delayed allocation. |
| 706 | * The right neighbor is contiguous with the new allocation. |
| 707 | */ |
| 708 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 709 | 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] | 710 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 711 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 712 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | new->br_blockcount + RIGHT.br_blockcount, |
| 714 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 715 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 716 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | rval = XFS_ILOG_DEXT; |
| 718 | else { |
| 719 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 720 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 722 | RIGHT.br_blockcount, &i); |
| 723 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 725 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 726 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | new->br_startblock, |
| 728 | new->br_blockcount + |
| 729 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 730 | RIGHT.br_state); |
| 731 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | goto done; |
| 733 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 734 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 735 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 736 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 737 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 738 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 739 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 740 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 741 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | break; |
| 743 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 744 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | /* |
| 746 | * Filling in the last part of a previous delayed allocation. |
| 747 | * The right neighbor is not contiguous. |
| 748 | */ |
| 749 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 750 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 752 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
| 753 | bma->ip->i_d.di_nextents++; |
| 754 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 756 | else { |
| 757 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 758 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 760 | &i); |
| 761 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 763 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 764 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 765 | error = xfs_btree_insert(bma->cur, &i); |
| 766 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 768 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 770 | if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 771 | bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) { |
| 772 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 773 | bma->firstblock, bma->flist, &bma->cur, 1, |
| 774 | &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | rval |= tmp_rval; |
| 776 | if (error) |
| 777 | goto done; |
| 778 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 779 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 780 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 781 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 782 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 783 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 784 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 785 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 786 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | break; |
| 788 | |
| 789 | case 0: |
| 790 | /* |
| 791 | * Filling in the middle part of a previous delayed allocation. |
| 792 | * Contiguity is impossible here. |
| 793 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 794 | * |
| 795 | * We start with a delayed allocation: |
| 796 | * |
| 797 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 798 | * PREV @ idx |
| 799 | * |
| 800 | * and we are allocating: |
| 801 | * +rrrrrrrrrrrrrrrrr+ |
| 802 | * new |
| 803 | * |
| 804 | * and we set it up for insertion as: |
| 805 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 806 | * new |
| 807 | * PREV @ idx LEFT RIGHT |
| 808 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | */ |
| 810 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 812 | 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] | 813 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 814 | LEFT = *new; |
| 815 | RIGHT.br_state = PREV.br_state; |
| 816 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 817 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 818 | RIGHT.br_startoff = new_endoff; |
| 819 | RIGHT.br_blockcount = temp2; |
| 820 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 821 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
| 822 | bma->ip->i_d.di_nextents++; |
| 823 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 825 | else { |
| 826 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 827 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 829 | &i); |
| 830 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 832 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 833 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 834 | error = xfs_btree_insert(bma->cur, &i); |
| 835 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 837 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 839 | if (bma->ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 840 | bma->ip->i_d.di_nextents > bma->ip->i_df.if_ext_max) { |
| 841 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 842 | bma->firstblock, bma->flist, &bma->cur, |
| 843 | 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | rval |= tmp_rval; |
| 845 | if (error) |
| 846 | goto done; |
| 847 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 848 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 849 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 850 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 851 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 852 | if (diff > 0) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 853 | error = xfs_icsb_modify_counters(bma->ip->i_mount, |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 854 | XFS_SBS_FDBLOCKS, |
| 855 | -((int64_t)diff), 0); |
| 856 | ASSERT(!error); |
| 857 | if (error) |
| 858 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 860 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 861 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 862 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 863 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 864 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 865 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 866 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 867 | 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] | 868 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 869 | bma->idx++; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 870 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | break; |
| 872 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 873 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 874 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 875 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 876 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 877 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 878 | case BMAP_LEFT_CONTIG: |
| 879 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | /* |
| 881 | * These cases are all impossible. |
| 882 | */ |
| 883 | ASSERT(0); |
| 884 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 885 | |
| 886 | /* convert to a btree if necessary */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 887 | if (XFS_IFORK_FORMAT(bma->ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS && |
| 888 | XFS_IFORK_NEXTENTS(bma->ip, XFS_DATA_FORK) > ifp->if_ext_max) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 889 | int tmp_logflags; /* partial log flag return val */ |
| 890 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 891 | ASSERT(bma->cur == NULL); |
| 892 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 893 | bma->firstblock, bma->flist, &bma->cur, |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 894 | da_old > 0, &tmp_logflags, XFS_DATA_FORK); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 895 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 896 | if (error) |
| 897 | goto done; |
| 898 | } |
| 899 | |
| 900 | /* adjust for changes in reserved delayed indirect blocks */ |
| 901 | if (da_old || da_new) { |
| 902 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 903 | if (bma->cur) |
| 904 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 905 | ASSERT(temp <= da_old); |
| 906 | if (temp < da_old) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 907 | xfs_icsb_modify_counters(bma->ip->i_mount, |
| 908 | XFS_SBS_FDBLOCKS, |
| 909 | (int64_t)(da_old - temp), 0); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 913 | if (bma->cur) |
| 914 | bma->cur->bc_private.b.allocated = 0; |
| 915 | |
| 916 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | done: |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 918 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return error; |
| 920 | #undef LEFT |
| 921 | #undef RIGHT |
| 922 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 926 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | */ |
| 928 | STATIC int /* error */ |
| 929 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 930 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 932 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 934 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 935 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 936 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 937 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 940 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 943 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 945 | xfs_exntst_t newext; /* new extent state */ |
| 946 | xfs_exntst_t oldext; /* old extent state */ |
| 947 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 948 | /* left is 0, right is 1, prev is 2 */ |
| 949 | int rval=0; /* return value (logging flags) */ |
| 950 | int state = 0;/* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 952 | *logflagsp = 0; |
| 953 | |
| 954 | cur = *curp; |
| 955 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 956 | |
| 957 | ASSERT(*idx >= 0); |
| 958 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 959 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 960 | |
| 961 | XFS_STATS_INC(xs_add_exlist); |
| 962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | #define LEFT r[0] |
| 964 | #define RIGHT r[1] |
| 965 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | /* |
| 968 | * Set up a bunch of variables to make the tests simpler. |
| 969 | */ |
| 970 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 971 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | xfs_bmbt_get_all(ep, &PREV); |
| 973 | newext = new->br_state; |
| 974 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 975 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 976 | ASSERT(PREV.br_state == oldext); |
| 977 | new_endoff = new->br_startoff + new->br_blockcount; |
| 978 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 979 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* |
| 982 | * Set flags determining what part of the previous oldext allocation |
| 983 | * extent is being replaced by a newext allocation. |
| 984 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 985 | if (PREV.br_startoff == new->br_startoff) |
| 986 | state |= BMAP_LEFT_FILLING; |
| 987 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 988 | state |= BMAP_RIGHT_FILLING; |
| 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | /* |
| 991 | * Check and set flags if this segment has a left neighbor. |
| 992 | * Don't set contiguous if the combined extent would be too large. |
| 993 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 994 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 995 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 996 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 997 | |
| 998 | if (isnullstartblock(LEFT.br_startblock)) |
| 999 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1001 | |
| 1002 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1003 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1004 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1005 | LEFT.br_state == newext && |
| 1006 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1007 | state |= BMAP_LEFT_CONTIG; |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | /* |
| 1010 | * Check and set flags if this segment has a right neighbor. |
| 1011 | * Don't set contiguous if the combined extent would be too large. |
| 1012 | * Also check for all-three-contiguous being too large. |
| 1013 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1014 | 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] | 1015 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1016 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1017 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1018 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1020 | |
| 1021 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1022 | new_endoff == RIGHT.br_startoff && |
| 1023 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1024 | newext == RIGHT.br_state && |
| 1025 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1026 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1027 | BMAP_RIGHT_FILLING)) != |
| 1028 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1029 | BMAP_RIGHT_FILLING) || |
| 1030 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1031 | <= MAXEXTLEN)) |
| 1032 | state |= BMAP_RIGHT_CONTIG; |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * Switch out based on the FILLING and CONTIG state bits. |
| 1036 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1037 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1038 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1039 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1040 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | * Setting all of a previous oldext extent to newext. |
| 1043 | * The left and right neighbors are both contiguous with new. |
| 1044 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1045 | --*idx; |
| 1046 | |
| 1047 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1048 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1050 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1051 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1052 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1053 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | ip->i_d.di_nextents -= 2; |
| 1055 | if (cur == NULL) |
| 1056 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1057 | else { |
| 1058 | rval = XFS_ILOG_CORE; |
| 1059 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1060 | RIGHT.br_startblock, |
| 1061 | RIGHT.br_blockcount, &i))) |
| 1062 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1063 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1064 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1066 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1067 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1069 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1070 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1072 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1073 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1075 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1077 | LEFT.br_startblock, |
| 1078 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1079 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1080 | goto done; |
| 1081 | } |
| 1082 | break; |
| 1083 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1084 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | /* |
| 1086 | * Setting all of a previous oldext extent to newext. |
| 1087 | * The left neighbor is contiguous, the right is not. |
| 1088 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1089 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1090 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1091 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1092 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 1093 | LEFT.br_blockcount + PREV.br_blockcount); |
| 1094 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1095 | |
| 1096 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | ip->i_d.di_nextents--; |
| 1098 | if (cur == NULL) |
| 1099 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1100 | else { |
| 1101 | rval = XFS_ILOG_CORE; |
| 1102 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1103 | PREV.br_startblock, PREV.br_blockcount, |
| 1104 | &i))) |
| 1105 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1106 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1107 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1109 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1110 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1112 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1114 | LEFT.br_startblock, |
| 1115 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1116 | LEFT.br_state))) |
| 1117 | goto done; |
| 1118 | } |
| 1119 | break; |
| 1120 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1121 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * Setting all of a previous oldext extent to newext. |
| 1124 | * The right neighbor is contiguous, the left is not. |
| 1125 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1126 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | xfs_bmbt_set_blockcount(ep, |
| 1128 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1129 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1130 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1131 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | ip->i_d.di_nextents--; |
| 1133 | if (cur == NULL) |
| 1134 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1135 | else { |
| 1136 | rval = XFS_ILOG_CORE; |
| 1137 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1138 | RIGHT.br_startblock, |
| 1139 | RIGHT.br_blockcount, &i))) |
| 1140 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1141 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1142 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1144 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1145 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1147 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1149 | new->br_startblock, |
| 1150 | new->br_blockcount + RIGHT.br_blockcount, |
| 1151 | newext))) |
| 1152 | goto done; |
| 1153 | } |
| 1154 | break; |
| 1155 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1156 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | /* |
| 1158 | * Setting all of a previous oldext extent to newext. |
| 1159 | * Neither the left nor right neighbors are contiguous with |
| 1160 | * the new one. |
| 1161 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1162 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1164 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (cur == NULL) |
| 1167 | rval = XFS_ILOG_DEXT; |
| 1168 | else { |
| 1169 | rval = 0; |
| 1170 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1171 | new->br_startblock, new->br_blockcount, |
| 1172 | &i))) |
| 1173 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1174 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1176 | new->br_startblock, new->br_blockcount, |
| 1177 | newext))) |
| 1178 | goto done; |
| 1179 | } |
| 1180 | break; |
| 1181 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1182 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | /* |
| 1184 | * Setting the first part of a previous oldext extent to newext. |
| 1185 | * The left neighbor is contiguous. |
| 1186 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1187 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 1188 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | LEFT.br_blockcount + new->br_blockcount); |
| 1190 | xfs_bmbt_set_startoff(ep, |
| 1191 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1192 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1193 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1194 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | xfs_bmbt_set_startblock(ep, |
| 1196 | new->br_startblock + new->br_blockcount); |
| 1197 | xfs_bmbt_set_blockcount(ep, |
| 1198 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1199 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1200 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1201 | --*idx; |
| 1202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | if (cur == NULL) |
| 1204 | rval = XFS_ILOG_DEXT; |
| 1205 | else { |
| 1206 | rval = 0; |
| 1207 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1208 | PREV.br_startblock, PREV.br_blockcount, |
| 1209 | &i))) |
| 1210 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1211 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | if ((error = xfs_bmbt_update(cur, |
| 1213 | PREV.br_startoff + new->br_blockcount, |
| 1214 | PREV.br_startblock + new->br_blockcount, |
| 1215 | PREV.br_blockcount - new->br_blockcount, |
| 1216 | oldext))) |
| 1217 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1218 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 1220 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | LEFT.br_startblock, |
| 1222 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 1223 | LEFT.br_state); |
| 1224 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | goto done; |
| 1226 | } |
| 1227 | break; |
| 1228 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1229 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | /* |
| 1231 | * Setting the first part of a previous oldext extent to newext. |
| 1232 | * The left neighbor is not contiguous. |
| 1233 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1234 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1236 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1237 | xfs_bmbt_set_blockcount(ep, |
| 1238 | PREV.br_blockcount - new->br_blockcount); |
| 1239 | xfs_bmbt_set_startblock(ep, |
| 1240 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1241 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1242 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1243 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | ip->i_d.di_nextents++; |
| 1245 | if (cur == NULL) |
| 1246 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1247 | else { |
| 1248 | rval = XFS_ILOG_CORE; |
| 1249 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1250 | PREV.br_startblock, PREV.br_blockcount, |
| 1251 | &i))) |
| 1252 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1253 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if ((error = xfs_bmbt_update(cur, |
| 1255 | PREV.br_startoff + new->br_blockcount, |
| 1256 | PREV.br_startblock + new->br_blockcount, |
| 1257 | PREV.br_blockcount - new->br_blockcount, |
| 1258 | oldext))) |
| 1259 | goto done; |
| 1260 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1261 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1263 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } |
| 1265 | break; |
| 1266 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1267 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | /* |
| 1269 | * Setting the last part of a previous oldext extent to newext. |
| 1270 | * The right neighbor is contiguous with the new allocation. |
| 1271 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1272 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | xfs_bmbt_set_blockcount(ep, |
| 1274 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1275 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1276 | |
| 1277 | ++*idx; |
| 1278 | |
| 1279 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1280 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1281 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1283 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | if (cur == NULL) |
| 1286 | rval = XFS_ILOG_DEXT; |
| 1287 | else { |
| 1288 | rval = 0; |
| 1289 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1290 | PREV.br_startblock, |
| 1291 | PREV.br_blockcount, &i))) |
| 1292 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1293 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1295 | PREV.br_startblock, |
| 1296 | PREV.br_blockcount - new->br_blockcount, |
| 1297 | oldext))) |
| 1298 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1299 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | goto done; |
| 1301 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1302 | new->br_startblock, |
| 1303 | new->br_blockcount + RIGHT.br_blockcount, |
| 1304 | newext))) |
| 1305 | goto done; |
| 1306 | } |
| 1307 | break; |
| 1308 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1309 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * Setting the last part of a previous oldext extent to newext. |
| 1312 | * The right neighbor is not contiguous. |
| 1313 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1314 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | xfs_bmbt_set_blockcount(ep, |
| 1316 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1317 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1318 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1319 | ++*idx; |
| 1320 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 1321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | ip->i_d.di_nextents++; |
| 1323 | if (cur == NULL) |
| 1324 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1325 | else { |
| 1326 | rval = XFS_ILOG_CORE; |
| 1327 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1328 | PREV.br_startblock, PREV.br_blockcount, |
| 1329 | &i))) |
| 1330 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1331 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1333 | PREV.br_startblock, |
| 1334 | PREV.br_blockcount - new->br_blockcount, |
| 1335 | oldext))) |
| 1336 | goto done; |
| 1337 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1338 | new->br_startblock, new->br_blockcount, |
| 1339 | &i))) |
| 1340 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1341 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1343 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1345 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | } |
| 1347 | break; |
| 1348 | |
| 1349 | case 0: |
| 1350 | /* |
| 1351 | * Setting the middle part of a previous oldext extent to |
| 1352 | * newext. Contiguity is impossible here. |
| 1353 | * One extent becomes three extents. |
| 1354 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1355 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | xfs_bmbt_set_blockcount(ep, |
| 1357 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1358 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | r[0] = *new; |
| 1361 | r[1].br_startoff = new_endoff; |
| 1362 | r[1].br_blockcount = |
| 1363 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1364 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1365 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1366 | |
| 1367 | ++*idx; |
| 1368 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 1369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | ip->i_d.di_nextents += 2; |
| 1371 | if (cur == NULL) |
| 1372 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1373 | else { |
| 1374 | rval = XFS_ILOG_CORE; |
| 1375 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1376 | PREV.br_startblock, PREV.br_blockcount, |
| 1377 | &i))) |
| 1378 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1379 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | /* new right extent - oldext */ |
| 1381 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1382 | r[1].br_startblock, r[1].br_blockcount, |
| 1383 | r[1].br_state))) |
| 1384 | goto done; |
| 1385 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1387 | cur->bc_rec.b.br_blockcount = |
| 1388 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1389 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1391 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1392 | /* |
| 1393 | * Reset the cursor to the position of the new extent |
| 1394 | * we are about to insert as we can't trust it after |
| 1395 | * the previous insert. |
| 1396 | */ |
| 1397 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1398 | new->br_startblock, new->br_blockcount, |
| 1399 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1401 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1403 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1404 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1406 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
| 1408 | break; |
| 1409 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1410 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1411 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1412 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1413 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1414 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1415 | case BMAP_LEFT_CONTIG: |
| 1416 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | /* |
| 1418 | * These cases are all impossible. |
| 1419 | */ |
| 1420 | ASSERT(0); |
| 1421 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1422 | |
| 1423 | /* convert to a btree if necessary */ |
| 1424 | if (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS && |
| 1425 | XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > ifp->if_ext_max) { |
| 1426 | int tmp_logflags; /* partial log flag return val */ |
| 1427 | |
| 1428 | ASSERT(cur == NULL); |
| 1429 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, |
| 1430 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 1431 | *logflagsp |= tmp_logflags; |
| 1432 | if (error) |
| 1433 | goto done; |
| 1434 | } |
| 1435 | |
| 1436 | /* clear out the allocated field, done with it now in any case. */ |
| 1437 | if (cur) { |
| 1438 | cur->bc_private.b.allocated = 0; |
| 1439 | *curp = cur; |
| 1440 | } |
| 1441 | |
| 1442 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1444 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | return error; |
| 1446 | #undef LEFT |
| 1447 | #undef RIGHT |
| 1448 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1452 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1454 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | xfs_bmap_add_extent_hole_delay( |
| 1456 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1457 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1458 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1460 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1462 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1463 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1464 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1465 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1466 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1468 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1470 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | /* |
| 1473 | * Check and set flags if this segment has a left neighbor |
| 1474 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1475 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1476 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1477 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1478 | |
| 1479 | if (isnullstartblock(left.br_startblock)) |
| 1480 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * Check and set flags if the current (right) segment exists. |
| 1485 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1486 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1487 | 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] | 1488 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1489 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1490 | |
| 1491 | if (isnullstartblock(right.br_startblock)) |
| 1492 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | /* |
| 1496 | * Set contiguity flags on the left and right neighbors. |
| 1497 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1498 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1499 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1500 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1501 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1502 | state |= BMAP_LEFT_CONTIG; |
| 1503 | |
| 1504 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1505 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1506 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1507 | (!(state & BMAP_LEFT_CONTIG) || |
| 1508 | (left.br_blockcount + new->br_blockcount + |
| 1509 | right.br_blockcount <= MAXEXTLEN))) |
| 1510 | state |= BMAP_RIGHT_CONTIG; |
| 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | /* |
| 1513 | * Switch out based on the contiguity flags. |
| 1514 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1515 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1516 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | /* |
| 1518 | * New allocation is contiguous with delayed allocations |
| 1519 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1520 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1522 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | temp = left.br_blockcount + new->br_blockcount + |
| 1524 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1525 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1526 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1527 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1528 | oldlen = startblockval(left.br_startblock) + |
| 1529 | startblockval(new->br_startblock) + |
| 1530 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1532 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1533 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1534 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1535 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1536 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | break; |
| 1538 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1539 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | /* |
| 1541 | * New allocation is contiguous with a delayed allocation |
| 1542 | * on the left. |
| 1543 | * Merge the new allocation with the left neighbor. |
| 1544 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1545 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1547 | |
| 1548 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1549 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1550 | oldlen = startblockval(left.br_startblock) + |
| 1551 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1553 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1554 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1555 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | break; |
| 1557 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1558 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | /* |
| 1560 | * New allocation is contiguous with a delayed allocation |
| 1561 | * on the right. |
| 1562 | * Merge the new allocation with the right neighbor. |
| 1563 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1564 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1566 | oldlen = startblockval(new->br_startblock) + |
| 1567 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1569 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1570 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1571 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1572 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | break; |
| 1574 | |
| 1575 | case 0: |
| 1576 | /* |
| 1577 | * New allocation is not contiguous with another |
| 1578 | * delayed allocation. |
| 1579 | * Insert a new entry. |
| 1580 | */ |
| 1581 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1582 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | break; |
| 1584 | } |
| 1585 | if (oldlen != newlen) { |
| 1586 | ASSERT(oldlen > newlen); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1587 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1588 | (int64_t)(oldlen - newlen), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | /* |
| 1590 | * Nothing to do for disk quota accounting here. |
| 1591 | */ |
| 1592 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1596 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | */ |
| 1598 | STATIC int /* error */ |
| 1599 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1600 | struct xfs_bmalloca *bma, |
| 1601 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1603 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | int i; /* temp state */ |
| 1606 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1607 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1608 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1609 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1612 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1613 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1614 | ASSERT(bma->idx >= 0); |
| 1615 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1616 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1617 | ASSERT(!bma->cur || |
| 1618 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1619 | |
| 1620 | XFS_STATS_INC(xs_add_exlist); |
| 1621 | |
| 1622 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1623 | if (whichfork == XFS_ATTR_FORK) |
| 1624 | state |= BMAP_ATTRFORK; |
| 1625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | /* |
| 1627 | * Check and set flags if this segment has a left neighbor. |
| 1628 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1629 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1630 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1631 | 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] | 1632 | if (isnullstartblock(left.br_startblock)) |
| 1633 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | /* |
| 1637 | * Check and set flags if this segment has a current value. |
| 1638 | * Not true if we're inserting into the "hole" at eof. |
| 1639 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1640 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1641 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1642 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1643 | if (isnullstartblock(right.br_startblock)) |
| 1644 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | /* |
| 1648 | * We're inserting a real allocation between "left" and "right". |
| 1649 | * Set the contiguity flags. Don't let extents get too large. |
| 1650 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1651 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1652 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1653 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1654 | left.br_state == new->br_state && |
| 1655 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1656 | state |= BMAP_LEFT_CONTIG; |
| 1657 | |
| 1658 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1659 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1660 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1661 | new->br_state == right.br_state && |
| 1662 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1663 | (!(state & BMAP_LEFT_CONTIG) || |
| 1664 | left.br_blockcount + new->br_blockcount + |
| 1665 | right.br_blockcount <= MAXEXTLEN)) |
| 1666 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1668 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | /* |
| 1670 | * Select which case we're in here, and implement it. |
| 1671 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1672 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1673 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | /* |
| 1675 | * New allocation is contiguous with real allocations on the |
| 1676 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1677 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1679 | --bma->idx; |
| 1680 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1681 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | left.br_blockcount + new->br_blockcount + |
| 1683 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1684 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1685 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1686 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1687 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1688 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 1689 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 1690 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1691 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1692 | } else { |
| 1693 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1694 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 1695 | right.br_startblock, right.br_blockcount, |
| 1696 | &i); |
| 1697 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1698 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1699 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1700 | error = xfs_btree_delete(bma->cur, &i); |
| 1701 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1702 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1703 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1704 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1705 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1706 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1707 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1708 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1709 | left.br_startblock, |
| 1710 | left.br_blockcount + |
| 1711 | new->br_blockcount + |
| 1712 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1713 | left.br_state); |
| 1714 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1715 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1717 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1719 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | /* |
| 1721 | * New allocation is contiguous with a real allocation |
| 1722 | * on the left. |
| 1723 | * Merge the new allocation with the left neighbor. |
| 1724 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1725 | --bma->idx; |
| 1726 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1727 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1729 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1730 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1731 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1732 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1733 | } else { |
| 1734 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1735 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 1736 | left.br_startblock, left.br_blockcount, |
| 1737 | &i); |
| 1738 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1739 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1740 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1741 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1742 | left.br_startblock, |
| 1743 | left.br_blockcount + |
| 1744 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1745 | left.br_state); |
| 1746 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1747 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1749 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1751 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | /* |
| 1753 | * New allocation is contiguous with a real allocation |
| 1754 | * on the right. |
| 1755 | * Merge the new allocation with the right neighbor. |
| 1756 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1757 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1758 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1759 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | new->br_blockcount + right.br_blockcount, |
| 1761 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1762 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1763 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1764 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1765 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1766 | } else { |
| 1767 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1768 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1769 | right.br_startoff, |
| 1770 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1771 | right.br_blockcount, &i); |
| 1772 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1773 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1774 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1775 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1776 | new->br_startblock, |
| 1777 | new->br_blockcount + |
| 1778 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1779 | right.br_state); |
| 1780 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1781 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1783 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | |
| 1785 | case 0: |
| 1786 | /* |
| 1787 | * New allocation is not contiguous with another |
| 1788 | * real allocation. |
| 1789 | * Insert a new entry. |
| 1790 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1791 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 1792 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 1793 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 1794 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1795 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1796 | } else { |
| 1797 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1798 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1799 | new->br_startoff, |
| 1800 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1801 | new->br_blockcount, &i); |
| 1802 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1803 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1804 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1805 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 1806 | error = xfs_btree_insert(bma->cur, &i); |
| 1807 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1808 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1809 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1811 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1813 | |
| 1814 | /* convert to a btree if necessary */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1815 | if (XFS_IFORK_FORMAT(bma->ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 1816 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) > ifp->if_ext_max) { |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1817 | int tmp_logflags; /* partial log flag return val */ |
| 1818 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1819 | ASSERT(bma->cur == NULL); |
| 1820 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 1821 | bma->firstblock, bma->flist, &bma->cur, |
| 1822 | 0, &tmp_logflags, whichfork); |
| 1823 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1824 | if (error) |
| 1825 | goto done; |
| 1826 | } |
| 1827 | |
| 1828 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1829 | if (bma->cur) |
| 1830 | bma->cur->bc_private.b.allocated = 0; |
| 1831 | |
| 1832 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1833 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1834 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1835 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1838 | /* |
| 1839 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 1840 | */ |
| 1841 | STATIC int |
| 1842 | xfs_bmap_extsize_align( |
| 1843 | xfs_mount_t *mp, |
| 1844 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 1845 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 1846 | xfs_extlen_t extsz, /* align to this extent size */ |
| 1847 | int rt, /* is this a realtime inode? */ |
| 1848 | int eof, /* is extent at end-of-file? */ |
| 1849 | int delay, /* creating delalloc extent? */ |
| 1850 | int convert, /* overwriting unwritten extent? */ |
| 1851 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 1852 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 1853 | { |
| 1854 | xfs_fileoff_t orig_off; /* original offset */ |
| 1855 | xfs_extlen_t orig_alen; /* original length */ |
| 1856 | xfs_fileoff_t orig_end; /* original off+len */ |
| 1857 | xfs_fileoff_t nexto; /* next file offset */ |
| 1858 | xfs_fileoff_t prevo; /* previous file offset */ |
| 1859 | xfs_fileoff_t align_off; /* temp for offset */ |
| 1860 | xfs_extlen_t align_alen; /* temp for length */ |
| 1861 | xfs_extlen_t temp; /* temp for calculations */ |
| 1862 | |
| 1863 | if (convert) |
| 1864 | return 0; |
| 1865 | |
| 1866 | orig_off = align_off = *offp; |
| 1867 | orig_alen = align_alen = *lenp; |
| 1868 | orig_end = orig_off + orig_alen; |
| 1869 | |
| 1870 | /* |
| 1871 | * If this request overlaps an existing extent, then don't |
| 1872 | * attempt to perform any additional alignment. |
| 1873 | */ |
| 1874 | if (!delay && !eof && |
| 1875 | (orig_off >= gotp->br_startoff) && |
| 1876 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 1877 | return 0; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * If the file offset is unaligned vs. the extent size |
| 1882 | * we need to align it. This will be possible unless |
| 1883 | * the file was previously written with a kernel that didn't |
| 1884 | * perform this alignment, or if a truncate shot us in the |
| 1885 | * foot. |
| 1886 | */ |
| 1887 | temp = do_mod(orig_off, extsz); |
| 1888 | if (temp) { |
| 1889 | align_alen += temp; |
| 1890 | align_off -= temp; |
| 1891 | } |
| 1892 | /* |
| 1893 | * Same adjustment for the end of the requested area. |
| 1894 | */ |
| 1895 | if ((temp = (align_alen % extsz))) { |
| 1896 | align_alen += extsz - temp; |
| 1897 | } |
| 1898 | /* |
| 1899 | * If the previous block overlaps with this proposed allocation |
| 1900 | * then move the start forward without adjusting the length. |
| 1901 | */ |
| 1902 | if (prevp->br_startoff != NULLFILEOFF) { |
| 1903 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 1904 | prevo = prevp->br_startoff; |
| 1905 | else |
| 1906 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 1907 | } else |
| 1908 | prevo = 0; |
| 1909 | if (align_off != orig_off && align_off < prevo) |
| 1910 | align_off = prevo; |
| 1911 | /* |
| 1912 | * If the next block overlaps with this proposed allocation |
| 1913 | * then move the start back without adjusting the length, |
| 1914 | * but not before offset 0. |
| 1915 | * This may of course make the start overlap previous block, |
| 1916 | * and if we hit the offset 0 limit then the next block |
| 1917 | * can still overlap too. |
| 1918 | */ |
| 1919 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 1920 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 1921 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 1922 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 1923 | else |
| 1924 | nexto = gotp->br_startoff; |
| 1925 | } else |
| 1926 | nexto = NULLFILEOFF; |
| 1927 | if (!eof && |
| 1928 | align_off + align_alen != orig_end && |
| 1929 | align_off + align_alen > nexto) |
| 1930 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 1931 | /* |
| 1932 | * If we're now overlapping the next or previous extent that |
| 1933 | * means we can't fit an extsz piece in this hole. Just move |
| 1934 | * the start forward to the first valid spot and set |
| 1935 | * the length so we hit the end. |
| 1936 | */ |
| 1937 | if (align_off != orig_off && align_off < prevo) |
| 1938 | align_off = prevo; |
| 1939 | if (align_off + align_alen != orig_end && |
| 1940 | align_off + align_alen > nexto && |
| 1941 | nexto != NULLFILEOFF) { |
| 1942 | ASSERT(nexto > prevo); |
| 1943 | align_alen = nexto - align_off; |
| 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * If realtime, and the result isn't a multiple of the realtime |
| 1948 | * extent size we need to remove blocks until it is. |
| 1949 | */ |
| 1950 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 1951 | /* |
| 1952 | * We're not covering the original request, or |
| 1953 | * we won't be able to once we fix the length. |
| 1954 | */ |
| 1955 | if (orig_off < align_off || |
| 1956 | orig_end > align_off + align_alen || |
| 1957 | align_alen - temp < orig_alen) |
| 1958 | return XFS_ERROR(EINVAL); |
| 1959 | /* |
| 1960 | * Try to fix it by moving the start up. |
| 1961 | */ |
| 1962 | if (align_off + temp <= orig_off) { |
| 1963 | align_alen -= temp; |
| 1964 | align_off += temp; |
| 1965 | } |
| 1966 | /* |
| 1967 | * Try to fix it by moving the end in. |
| 1968 | */ |
| 1969 | else if (align_off + align_alen - temp >= orig_end) |
| 1970 | align_alen -= temp; |
| 1971 | /* |
| 1972 | * Set the start to the minimum then trim the length. |
| 1973 | */ |
| 1974 | else { |
| 1975 | align_alen -= orig_off - align_off; |
| 1976 | align_off = orig_off; |
| 1977 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 1978 | } |
| 1979 | /* |
| 1980 | * Result doesn't cover the request, fail it. |
| 1981 | */ |
| 1982 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 1983 | return XFS_ERROR(EINVAL); |
| 1984 | } else { |
| 1985 | ASSERT(orig_off >= align_off); |
| 1986 | ASSERT(orig_end <= align_off + align_alen); |
| 1987 | } |
| 1988 | |
| 1989 | #ifdef DEBUG |
| 1990 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 1991 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 1992 | if (prevp->br_startoff != NULLFILEOFF) |
| 1993 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 1994 | #endif |
| 1995 | |
| 1996 | *lenp = align_alen; |
| 1997 | *offp = align_off; |
| 1998 | return 0; |
| 1999 | } |
| 2000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2002 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2003 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2004 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2006 | { |
| 2007 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2009 | xfs_mount_t *mp; /* mount point structure */ |
| 2010 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2011 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
| 2013 | #define ISVALID(x,y) \ |
| 2014 | (rt ? \ |
| 2015 | (x) < mp->m_sb.sb_rblocks : \ |
| 2016 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2017 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2018 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2021 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2023 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | /* |
| 2025 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2026 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2028 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 2029 | !isnullstartblock(ap->prev.br_startblock) && |
| 2030 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 2031 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2032 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | /* |
| 2034 | * Adjust for the gap between prevp and us. |
| 2035 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2036 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2037 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2039 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 2040 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | } |
| 2042 | /* |
| 2043 | * If not at eof, then compare the two neighbor blocks. |
| 2044 | * Figure out whether either one gives us a good starting point, |
| 2045 | * and pick the better one. |
| 2046 | */ |
| 2047 | else if (!ap->eof) { |
| 2048 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2049 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2050 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2051 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2052 | |
| 2053 | /* |
| 2054 | * If there's a previous (left) block, select a requested |
| 2055 | * start block based on it. |
| 2056 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2057 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 2058 | !isnullstartblock(ap->prev.br_startblock) && |
| 2059 | (prevbno = ap->prev.br_startblock + |
| 2060 | ap->prev.br_blockcount) && |
| 2061 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | /* |
| 2063 | * Calculate gap to end of previous block. |
| 2064 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2065 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2066 | (ap->prev.br_startoff + |
| 2067 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | /* |
| 2069 | * Figure the startblock based on the previous block's |
| 2070 | * end and the gap size. |
| 2071 | * Heuristic! |
| 2072 | * If the gap is large relative to the piece we're |
| 2073 | * allocating, or using it gives us an invalid block |
| 2074 | * number, then just use the end of the previous block. |
| 2075 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2076 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2078 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | prevbno += adjust; |
| 2080 | else |
| 2081 | prevdiff += adjust; |
| 2082 | /* |
| 2083 | * If the firstblock forbids it, can't use it, |
| 2084 | * must use default. |
| 2085 | */ |
| 2086 | if (!rt && !nullfb && |
| 2087 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2088 | prevbno = NULLFSBLOCK; |
| 2089 | } |
| 2090 | /* |
| 2091 | * No previous block or can't follow it, just default. |
| 2092 | */ |
| 2093 | else |
| 2094 | prevbno = NULLFSBLOCK; |
| 2095 | /* |
| 2096 | * If there's a following (right) block, select a requested |
| 2097 | * start block based on it. |
| 2098 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2099 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | /* |
| 2101 | * Calculate gap to start of next block. |
| 2102 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2103 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | /* |
| 2105 | * Figure the startblock based on the next block's |
| 2106 | * start and the gap size. |
| 2107 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2108 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | /* |
| 2110 | * Heuristic! |
| 2111 | * If the gap is large relative to the piece we're |
| 2112 | * allocating, or using it gives us an invalid block |
| 2113 | * number, then just use the start of the next block |
| 2114 | * offset by our length. |
| 2115 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2116 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2118 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2119 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 2120 | gotbno -= ap->length; |
| 2121 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } else |
| 2123 | gotdiff += adjust; |
| 2124 | /* |
| 2125 | * If the firstblock forbids it, can't use it, |
| 2126 | * must use default. |
| 2127 | */ |
| 2128 | if (!rt && !nullfb && |
| 2129 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2130 | gotbno = NULLFSBLOCK; |
| 2131 | } |
| 2132 | /* |
| 2133 | * No next block, just default. |
| 2134 | */ |
| 2135 | else |
| 2136 | gotbno = NULLFSBLOCK; |
| 2137 | /* |
| 2138 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2139 | * 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] | 2140 | */ |
| 2141 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2142 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2144 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2146 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2148 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | STATIC int |
| 2152 | xfs_bmap_rtalloc( |
| 2153 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2154 | { |
| 2155 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2156 | int error; /* error return value */ |
| 2157 | xfs_mount_t *mp; /* mount point structure */ |
| 2158 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2159 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2160 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2161 | xfs_rtblock_t rtb; |
| 2162 | |
| 2163 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2164 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2165 | prod = align / mp->m_sb.sb_rextsize; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2166 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2167 | align, 1, ap->eof, 0, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2168 | ap->conv, &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2169 | if (error) |
| 2170 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2171 | ASSERT(ap->length); |
| 2172 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2173 | |
| 2174 | /* |
| 2175 | * If the offset & length are not perfectly aligned |
| 2176 | * then kill prod, it will just get us in trouble. |
| 2177 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2178 | if (do_mod(ap->offset, align) || ap->length % align) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2179 | prod = 1; |
| 2180 | /* |
| 2181 | * Set ralen to be the actual requested length in rtextents. |
| 2182 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2183 | ralen = ap->length / mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2184 | /* |
| 2185 | * If the old value was close enough to MAXEXTLEN that |
| 2186 | * we rounded up to it, cut it back so it's valid again. |
| 2187 | * Note that if it's a really large request (bigger than |
| 2188 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2189 | * adjust the starting point to match it. |
| 2190 | */ |
| 2191 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2192 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2193 | |
| 2194 | /* |
| 2195 | * Lock out other modifications to the RT bitmap inode. |
| 2196 | */ |
Christoph Hellwig | 1050c71 | 2011-02-13 13:25:31 +0000 | [diff] [blame] | 2197 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 2198 | xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2199 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2200 | /* |
| 2201 | * If it's an allocation to an empty file at offset 0, |
| 2202 | * pick an extent that will space things out in the rt area. |
| 2203 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2204 | if (ap->eof && ap->offset == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2205 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2206 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2207 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2208 | if (error) |
| 2209 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2210 | ap->blkno = rtx * mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2211 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2212 | ap->blkno = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | xfs_bmap_adjacent(ap); |
| 2216 | |
| 2217 | /* |
| 2218 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2219 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2220 | atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2221 | do_div(ap->blkno, mp->m_sb.sb_rextsize); |
| 2222 | rtb = ap->blkno; |
| 2223 | ap->length = ralen; |
| 2224 | if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2225 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2226 | return error; |
| 2227 | if (rtb == NULLFSBLOCK && prod > 1 && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2228 | (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, |
| 2229 | ap->length, &ralen, atype, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2230 | ap->wasdel, 1, &rtb))) |
| 2231 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2232 | ap->blkno = rtb; |
| 2233 | if (ap->blkno != NULLFSBLOCK) { |
| 2234 | ap->blkno *= mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2235 | ralen *= mp->m_sb.sb_rextsize; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2236 | ap->length = ralen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2237 | ap->ip->i_d.di_nblocks += ralen; |
| 2238 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2239 | if (ap->wasdel) |
| 2240 | ap->ip->i_delayed_blks -= ralen; |
| 2241 | /* |
| 2242 | * Adjust the disk quota also. This was reserved |
| 2243 | * earlier. |
| 2244 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2245 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2246 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2247 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2248 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2249 | ap->length = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2250 | } |
| 2251 | return 0; |
| 2252 | } |
| 2253 | |
| 2254 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2255 | xfs_bmap_btalloc_nullfb( |
| 2256 | struct xfs_bmalloca *ap, |
| 2257 | struct xfs_alloc_arg *args, |
| 2258 | xfs_extlen_t *blen) |
| 2259 | { |
| 2260 | struct xfs_mount *mp = ap->ip->i_mount; |
| 2261 | struct xfs_perag *pag; |
| 2262 | xfs_agnumber_t ag, startag; |
| 2263 | int notinit = 0; |
| 2264 | int error; |
| 2265 | |
| 2266 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2267 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2268 | else |
| 2269 | args->type = XFS_ALLOCTYPE_START_BNO; |
| 2270 | args->total = ap->total; |
| 2271 | |
| 2272 | /* |
| 2273 | * Search for an allocation group with a single extent large enough |
| 2274 | * for the request. If one isn't found, then adjust the minimum |
| 2275 | * allocation size to the largest space found. |
| 2276 | */ |
| 2277 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2278 | if (startag == NULLAGNUMBER) |
| 2279 | startag = ag = 0; |
| 2280 | |
| 2281 | pag = xfs_perag_get(mp, ag); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2282 | while (*blen < args->maxlen) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2283 | if (!pag->pagf_init) { |
| 2284 | error = xfs_alloc_pagf_init(mp, args->tp, ag, |
| 2285 | XFS_ALLOC_FLAG_TRYLOCK); |
| 2286 | if (error) { |
| 2287 | xfs_perag_put(pag); |
| 2288 | return error; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | /* |
| 2293 | * See xfs_alloc_fix_freelist... |
| 2294 | */ |
| 2295 | if (pag->pagf_init) { |
| 2296 | xfs_extlen_t longest; |
| 2297 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 2298 | if (*blen < longest) |
| 2299 | *blen = longest; |
| 2300 | } else |
| 2301 | notinit = 1; |
| 2302 | |
| 2303 | if (xfs_inode_is_filestream(ap->ip)) { |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2304 | if (*blen >= args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | |
| 2307 | if (ap->userdata) { |
| 2308 | /* |
| 2309 | * If startag is an invalid AG, we've |
| 2310 | * come here once before and |
| 2311 | * xfs_filestream_new_ag picked the |
| 2312 | * best currently available. |
| 2313 | * |
| 2314 | * Don't continue looping, since we |
| 2315 | * could loop forever. |
| 2316 | */ |
| 2317 | if (startag == NULLAGNUMBER) |
| 2318 | break; |
| 2319 | |
| 2320 | error = xfs_filestream_new_ag(ap, &ag); |
| 2321 | xfs_perag_put(pag); |
| 2322 | if (error) |
| 2323 | return error; |
| 2324 | |
| 2325 | /* loop again to set 'blen'*/ |
| 2326 | startag = NULLAGNUMBER; |
| 2327 | pag = xfs_perag_get(mp, ag); |
| 2328 | continue; |
| 2329 | } |
| 2330 | } |
| 2331 | if (++ag == mp->m_sb.sb_agcount) |
| 2332 | ag = 0; |
| 2333 | if (ag == startag) |
| 2334 | break; |
| 2335 | xfs_perag_put(pag); |
| 2336 | pag = xfs_perag_get(mp, ag); |
| 2337 | } |
| 2338 | xfs_perag_put(pag); |
| 2339 | |
| 2340 | /* |
| 2341 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2342 | * possible that there is space for this request. |
| 2343 | */ |
| 2344 | if (notinit || *blen < ap->minlen) |
| 2345 | args->minlen = ap->minlen; |
| 2346 | /* |
| 2347 | * If the best seen length is less than the request |
| 2348 | * length, use the best as the minimum. |
| 2349 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2350 | else if (*blen < args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2351 | args->minlen = *blen; |
| 2352 | /* |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2353 | * Otherwise we've seen an extent as big as maxlen, |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2354 | * use that as the minimum. |
| 2355 | */ |
| 2356 | else |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2357 | args->minlen = args->maxlen; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2358 | |
| 2359 | /* |
| 2360 | * set the failure fallback case to look in the selected |
| 2361 | * AG as the stream may have moved. |
| 2362 | */ |
| 2363 | if (xfs_inode_is_filestream(ap->ip)) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2364 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2365 | |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
| 2369 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2370 | xfs_bmap_btalloc( |
| 2371 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2372 | { |
| 2373 | xfs_mount_t *mp; /* mount point structure */ |
| 2374 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2375 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2376 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2377 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2378 | xfs_alloc_arg_t args; |
| 2379 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2380 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2381 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2382 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2383 | int tryagain; |
| 2384 | int error; |
| 2385 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 2386 | ASSERT(ap->length); |
| 2387 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2388 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2389 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2390 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2391 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2392 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2393 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2394 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2395 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2396 | } |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2397 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 2398 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2399 | if (nullfb) { |
| 2400 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2401 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2402 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2403 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2404 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2405 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2406 | } |
| 2407 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2408 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2409 | |
| 2410 | xfs_bmap_adjacent(ap); |
| 2411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2413 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | * it's in the right allocation group. |
| 2415 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2416 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | ; |
| 2418 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2419 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | * Normal allocation, done through xfs_alloc_vextent. |
| 2422 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2423 | tryagain = isaligned = 0; |
| 2424 | args.tp = ap->tp; |
| 2425 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2426 | args.fsbno = ap->blkno; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2427 | |
| 2428 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2429 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2430 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2431 | blen = 0; |
| 2432 | if (nullfb) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2433 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
| 2434 | if (error) |
| 2435 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2436 | } else if (ap->flist->xbf_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2437 | if (xfs_inode_is_filestream(ap->ip)) |
| 2438 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2439 | else |
| 2440 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2441 | args.total = args.minlen = ap->minlen; |
| 2442 | } else { |
| 2443 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2444 | args.total = ap->total; |
| 2445 | args.minlen = ap->minlen; |
| 2446 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2447 | /* apply extent size hints if obtained earlier */ |
| 2448 | if (unlikely(align)) { |
| 2449 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2450 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2451 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2452 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2453 | args.prod = 1; |
| 2454 | args.mod = 0; |
| 2455 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2456 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2457 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2458 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2459 | } |
| 2460 | /* |
| 2461 | * If we are not low on available data blocks, and the |
| 2462 | * underlying logical volume manager is a stripe, and |
| 2463 | * the file offset is zero then try to allocate data |
| 2464 | * blocks on stripe unit boundary. |
| 2465 | * NOTE: ap->aeof is only set if the allocation length |
| 2466 | * is >= the stripe unit and the allocation offset is |
| 2467 | * at the end of file. |
| 2468 | */ |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2469 | if (!ap->flist->xbf_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2470 | if (!ap->offset) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2471 | args.alignment = mp->m_dalign; |
| 2472 | atype = args.type; |
| 2473 | isaligned = 1; |
| 2474 | /* |
| 2475 | * Adjust for alignment |
| 2476 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2477 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2478 | args.minlen = blen - args.alignment; |
| 2479 | args.minalignslop = 0; |
| 2480 | } else { |
| 2481 | /* |
| 2482 | * First try an exact bno allocation. |
| 2483 | * If it fails then do a near or start bno |
| 2484 | * allocation with alignment turned on. |
| 2485 | */ |
| 2486 | atype = args.type; |
| 2487 | tryagain = 1; |
| 2488 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2489 | args.alignment = 1; |
| 2490 | /* |
| 2491 | * Compute the minlen+alignment for the |
| 2492 | * next case. Set slop so that the value |
| 2493 | * of minlen+alignment+slop doesn't go up |
| 2494 | * between the calls. |
| 2495 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2496 | if (blen > mp->m_dalign && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2497 | nextminlen = blen - mp->m_dalign; |
| 2498 | else |
| 2499 | nextminlen = args.minlen; |
| 2500 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2501 | args.minalignslop = |
| 2502 | nextminlen + mp->m_dalign - |
| 2503 | args.minlen - 1; |
| 2504 | else |
| 2505 | args.minalignslop = 0; |
| 2506 | } |
| 2507 | } else { |
| 2508 | args.alignment = 1; |
| 2509 | args.minalignslop = 0; |
| 2510 | } |
| 2511 | args.minleft = ap->minleft; |
| 2512 | args.wasdel = ap->wasdel; |
| 2513 | args.isfl = 0; |
| 2514 | args.userdata = ap->userdata; |
| 2515 | if ((error = xfs_alloc_vextent(&args))) |
| 2516 | return error; |
| 2517 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2518 | /* |
| 2519 | * Exact allocation failed. Now try with alignment |
| 2520 | * turned on. |
| 2521 | */ |
| 2522 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2523 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2524 | args.alignment = mp->m_dalign; |
| 2525 | args.minlen = nextminlen; |
| 2526 | args.minalignslop = 0; |
| 2527 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | if ((error = xfs_alloc_vextent(&args))) |
| 2529 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2530 | } |
| 2531 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2532 | /* |
| 2533 | * allocation failed, so turn off alignment and |
| 2534 | * try again. |
| 2535 | */ |
| 2536 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2537 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2538 | args.alignment = 0; |
| 2539 | if ((error = xfs_alloc_vextent(&args))) |
| 2540 | return error; |
| 2541 | } |
| 2542 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2543 | args.minlen > ap->minlen) { |
| 2544 | args.minlen = ap->minlen; |
| 2545 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2546 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2547 | if ((error = xfs_alloc_vextent(&args))) |
| 2548 | return error; |
| 2549 | } |
| 2550 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2551 | args.fsbno = 0; |
| 2552 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2553 | args.total = ap->minlen; |
| 2554 | args.minleft = 0; |
| 2555 | if ((error = xfs_alloc_vextent(&args))) |
| 2556 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2557 | ap->flist->xbf_low = 1; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2558 | } |
| 2559 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2560 | /* |
| 2561 | * check the allocation happened at the same or higher AG than |
| 2562 | * the first block that was allocated. |
| 2563 | */ |
| 2564 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 2565 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 2566 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
| 2567 | (ap->flist->xbf_low && |
| 2568 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 2569 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 2570 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2571 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2572 | if (*ap->firstblock == NULLFSBLOCK) |
| 2573 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2574 | ASSERT(nullfb || fb_agno == args.agno || |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2575 | (ap->flist->xbf_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2576 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2577 | ap->ip->i_d.di_nblocks += args.len; |
| 2578 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2579 | if (ap->wasdel) |
| 2580 | ap->ip->i_delayed_blks -= args.len; |
| 2581 | /* |
| 2582 | * Adjust the disk quota also. This was reserved |
| 2583 | * earlier. |
| 2584 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2585 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2586 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2587 | XFS_TRANS_DQ_BCOUNT, |
| 2588 | (long) args.len); |
| 2589 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2590 | ap->blkno = NULLFSBLOCK; |
| 2591 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | } |
| 2593 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | /* |
| 2597 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2598 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2599 | */ |
| 2600 | STATIC int |
| 2601 | xfs_bmap_alloc( |
| 2602 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2603 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2604 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2605 | return xfs_bmap_rtalloc(ap); |
| 2606 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | /* |
| 2610 | * Transform a btree format file with only one leaf node, where the |
| 2611 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2612 | * Since the file extents are already in-core, all we have to do is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | * give up the space for the btree root and pitch the leaf block. |
| 2614 | */ |
| 2615 | STATIC int /* error */ |
| 2616 | xfs_bmap_btree_to_extents( |
| 2617 | xfs_trans_t *tp, /* transaction pointer */ |
| 2618 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2619 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2620 | int *logflagsp, /* inode logging flags */ |
| 2621 | int whichfork) /* data or attr fork */ |
| 2622 | { |
| 2623 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2624 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | xfs_fsblock_t cbno; /* child block number */ |
| 2626 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2627 | int error; /* error return value */ |
| 2628 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2629 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2630 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2631 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2633 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2635 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2636 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2637 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2638 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2639 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2640 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2641 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2642 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | *logflagsp = 0; |
| 2644 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2645 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | return error; |
| 2647 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2649 | XFS_BMAP_BTREE_REF))) |
| 2650 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2651 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2652 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | return error; |
| 2654 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2655 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2656 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | xfs_trans_binval(tp, cbp); |
| 2658 | if (cur->bc_bufs[0] == cbp) |
| 2659 | cur->bc_bufs[0] = NULL; |
| 2660 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2661 | ASSERT(ifp->if_broot == NULL); |
| 2662 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2663 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2664 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | return 0; |
| 2666 | } |
| 2667 | |
| 2668 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2669 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | * after removing space (or undoing a delayed allocation). |
| 2671 | */ |
| 2672 | STATIC int /* error */ |
| 2673 | xfs_bmap_del_extent( |
| 2674 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2675 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2676 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2678 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2679 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2681 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | { |
| 2683 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2684 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2685 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2686 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2687 | int delay; /* current block is delayed allocated */ |
| 2688 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2689 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | int error; /* error return value */ |
| 2691 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2693 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2694 | int i; /* temp state */ |
| 2695 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2696 | xfs_mount_t *mp; /* mount structure */ |
| 2697 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2698 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2699 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | uint qfield; /* quota field to update */ |
| 2701 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2702 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2703 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | |
| 2705 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2706 | |
| 2707 | if (whichfork == XFS_ATTR_FORK) |
| 2708 | state |= BMAP_ATTRFORK; |
| 2709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | mp = ip->i_mount; |
| 2711 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2712 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2713 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2715 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | xfs_bmbt_get_all(ep, &got); |
| 2717 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2718 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2719 | got_endoff = got.br_startoff + got.br_blockcount; |
| 2720 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2721 | delay = isnullstartblock(got.br_startblock); |
| 2722 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | flags = 0; |
| 2724 | qfield = 0; |
| 2725 | error = 0; |
| 2726 | /* |
| 2727 | * If deleting a real allocation, must free up the disk space. |
| 2728 | */ |
| 2729 | if (!delay) { |
| 2730 | flags = XFS_ILOG_CORE; |
| 2731 | /* |
| 2732 | * Realtime allocation. Free it and record di_nblocks update. |
| 2733 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2734 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | xfs_fsblock_t bno; |
| 2736 | xfs_filblks_t len; |
| 2737 | |
| 2738 | ASSERT(do_mod(del->br_blockcount, |
| 2739 | mp->m_sb.sb_rextsize) == 0); |
| 2740 | ASSERT(do_mod(del->br_startblock, |
| 2741 | mp->m_sb.sb_rextsize) == 0); |
| 2742 | bno = del->br_startblock; |
| 2743 | len = del->br_blockcount; |
| 2744 | do_div(bno, mp->m_sb.sb_rextsize); |
| 2745 | do_div(len, mp->m_sb.sb_rextsize); |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 2746 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 2747 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | goto done; |
| 2749 | do_fx = 0; |
| 2750 | nblks = len * mp->m_sb.sb_rextsize; |
| 2751 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 2752 | } |
| 2753 | /* |
| 2754 | * Ordinary allocation. |
| 2755 | */ |
| 2756 | else { |
| 2757 | do_fx = 1; |
| 2758 | nblks = del->br_blockcount; |
| 2759 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 2760 | } |
| 2761 | /* |
| 2762 | * Set up del_endblock and cur for later. |
| 2763 | */ |
| 2764 | del_endblock = del->br_startblock + del->br_blockcount; |
| 2765 | if (cur) { |
| 2766 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 2767 | got.br_startblock, got.br_blockcount, |
| 2768 | &i))) |
| 2769 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2770 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | } |
| 2772 | da_old = da_new = 0; |
| 2773 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2774 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | da_new = 0; |
| 2776 | nblks = 0; |
| 2777 | do_fx = 0; |
| 2778 | } |
| 2779 | /* |
| 2780 | * Set flag value to use in switch statement. |
| 2781 | * Left-contig is 2, right-contig is 1. |
| 2782 | */ |
| 2783 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 2784 | (got_endoff == del_endoff)) { |
| 2785 | case 3: |
| 2786 | /* |
| 2787 | * Matches the whole extent. Delete the entry. |
| 2788 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2789 | xfs_iext_remove(ip, *idx, 1, |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2790 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2791 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | if (delay) |
| 2793 | break; |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2796 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2797 | flags |= XFS_ILOG_CORE; |
| 2798 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2799 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | break; |
| 2801 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2802 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2804 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | break; |
| 2806 | |
| 2807 | case 2: |
| 2808 | /* |
| 2809 | * Deleting the first part of the extent. |
| 2810 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2811 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 2813 | temp = got.br_blockcount - del->br_blockcount; |
| 2814 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | if (delay) { |
| 2816 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2817 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2818 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2819 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | da_new = temp; |
| 2821 | break; |
| 2822 | } |
| 2823 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2824 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2826 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | break; |
| 2828 | } |
| 2829 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 2830 | got.br_blockcount - del->br_blockcount, |
| 2831 | got.br_state))) |
| 2832 | goto done; |
| 2833 | break; |
| 2834 | |
| 2835 | case 1: |
| 2836 | /* |
| 2837 | * Deleting the last part of the extent. |
| 2838 | */ |
| 2839 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2840 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | if (delay) { |
| 2843 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2844 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2845 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2846 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | da_new = temp; |
| 2848 | break; |
| 2849 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2850 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2852 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | break; |
| 2854 | } |
| 2855 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 2856 | got.br_startblock, |
| 2857 | got.br_blockcount - del->br_blockcount, |
| 2858 | got.br_state))) |
| 2859 | goto done; |
| 2860 | break; |
| 2861 | |
| 2862 | case 0: |
| 2863 | /* |
| 2864 | * Deleting the middle of the extent. |
| 2865 | */ |
| 2866 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2867 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | xfs_bmbt_set_blockcount(ep, temp); |
| 2869 | new.br_startoff = del_endoff; |
| 2870 | temp2 = got_endoff - del_endoff; |
| 2871 | new.br_blockcount = temp2; |
| 2872 | new.br_state = got.br_state; |
| 2873 | if (!delay) { |
| 2874 | new.br_startblock = del_endblock; |
| 2875 | flags |= XFS_ILOG_CORE; |
| 2876 | if (cur) { |
| 2877 | if ((error = xfs_bmbt_update(cur, |
| 2878 | got.br_startoff, |
| 2879 | got.br_startblock, temp, |
| 2880 | got.br_state))) |
| 2881 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2882 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | goto done; |
| 2884 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2885 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | if (error && error != ENOSPC) |
| 2887 | goto done; |
| 2888 | /* |
| 2889 | * If get no-space back from btree insert, |
| 2890 | * it tried a split, and we have a zero |
| 2891 | * block reservation. |
| 2892 | * Fix up our state and return the error. |
| 2893 | */ |
| 2894 | if (error == ENOSPC) { |
| 2895 | /* |
| 2896 | * Reset the cursor, don't trust |
| 2897 | * it after any insert operation. |
| 2898 | */ |
| 2899 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2900 | got.br_startoff, |
| 2901 | got.br_startblock, |
| 2902 | temp, &i))) |
| 2903 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2904 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | /* |
| 2906 | * Update the btree record back |
| 2907 | * to the original value. |
| 2908 | */ |
| 2909 | if ((error = xfs_bmbt_update(cur, |
| 2910 | got.br_startoff, |
| 2911 | got.br_startblock, |
| 2912 | got.br_blockcount, |
| 2913 | got.br_state))) |
| 2914 | goto done; |
| 2915 | /* |
| 2916 | * Reset the extent record back |
| 2917 | * to the original value. |
| 2918 | */ |
| 2919 | xfs_bmbt_set_blockcount(ep, |
| 2920 | got.br_blockcount); |
| 2921 | flags = 0; |
| 2922 | error = XFS_ERROR(ENOSPC); |
| 2923 | goto done; |
| 2924 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2925 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2927 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2929 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2930 | } else { |
| 2931 | ASSERT(whichfork == XFS_DATA_FORK); |
| 2932 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2933 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2935 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | da_new = temp + temp2; |
| 2937 | while (da_new > da_old) { |
| 2938 | if (temp) { |
| 2939 | temp--; |
| 2940 | da_new--; |
| 2941 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2942 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | } |
| 2944 | if (da_new == da_old) |
| 2945 | break; |
| 2946 | if (temp2) { |
| 2947 | temp2--; |
| 2948 | da_new--; |
| 2949 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2950 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2954 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2955 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 2956 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | break; |
| 2958 | } |
| 2959 | /* |
| 2960 | * If we need to, add to list of extents to delete. |
| 2961 | */ |
| 2962 | if (do_fx) |
| 2963 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 2964 | mp); |
| 2965 | /* |
| 2966 | * Adjust inode # blocks in the file. |
| 2967 | */ |
| 2968 | if (nblks) |
| 2969 | ip->i_d.di_nblocks -= nblks; |
| 2970 | /* |
| 2971 | * Adjust quota data. |
| 2972 | */ |
| 2973 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2974 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | |
| 2976 | /* |
| 2977 | * Account for change in delayed indirect blocks. |
| 2978 | * Nothing to do for disk quota accounting here. |
| 2979 | */ |
| 2980 | ASSERT(da_old >= da_new); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 2981 | if (da_old > da_new) { |
| 2982 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2983 | (int64_t)(da_old - da_new), 0); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 2984 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | done: |
| 2986 | *logflagsp = flags; |
| 2987 | return error; |
| 2988 | } |
| 2989 | |
| 2990 | /* |
| 2991 | * Remove the entry "free" from the free item list. Prev points to the |
| 2992 | * previous entry, unless "free" is the head of the list. |
| 2993 | */ |
| 2994 | STATIC void |
| 2995 | xfs_bmap_del_free( |
| 2996 | xfs_bmap_free_t *flist, /* free item list header */ |
| 2997 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 2998 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 2999 | { |
| 3000 | if (prev) |
| 3001 | prev->xbfi_next = free->xbfi_next; |
| 3002 | else |
| 3003 | flist->xbf_first = free->xbfi_next; |
| 3004 | flist->xbf_count--; |
| 3005 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3006 | } |
| 3007 | |
| 3008 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | * Convert an extents-format file into a btree-format file. |
| 3010 | * The new file will have a root block (in the inode) and a single child block. |
| 3011 | */ |
| 3012 | STATIC int /* error */ |
| 3013 | xfs_bmap_extents_to_btree( |
| 3014 | xfs_trans_t *tp, /* transaction pointer */ |
| 3015 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3016 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3017 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3018 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3019 | int wasdel, /* converting a delayed alloc */ |
| 3020 | int *logflagsp, /* inode logging flags */ |
| 3021 | int whichfork) /* data or attr fork */ |
| 3022 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3023 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3025 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3026 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3027 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3029 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3031 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3033 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3034 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3035 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3037 | |
| 3038 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3039 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3040 | ASSERT(ifp->if_ext_max == |
| 3041 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3042 | /* |
| 3043 | * Make space in the inode incore. |
| 3044 | */ |
| 3045 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3046 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | /* |
| 3049 | * Fill in the root. |
| 3050 | */ |
| 3051 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3052 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3053 | block->bb_level = cpu_to_be16(1); |
| 3054 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3055 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3056 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | /* |
| 3059 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3060 | */ |
| 3061 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3062 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | cur->bc_private.b.firstblock = *firstblock; |
| 3064 | cur->bc_private.b.flist = flist; |
| 3065 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3066 | /* |
| 3067 | * Convert to a btree with two levels, one record in root. |
| 3068 | */ |
| 3069 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3070 | args.tp = tp; |
| 3071 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3072 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | if (*firstblock == NULLFSBLOCK) { |
| 3074 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3075 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3076 | } else if (flist->xbf_low) { |
| 3077 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3078 | args.fsbno = *firstblock; |
| 3079 | } else { |
| 3080 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3081 | args.fsbno = *firstblock; |
| 3082 | } |
| 3083 | args.minlen = args.maxlen = args.prod = 1; |
| 3084 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3085 | args.minalignslop = 0; |
| 3086 | args.wasdel = wasdel; |
| 3087 | *logflagsp = 0; |
| 3088 | if ((error = xfs_alloc_vextent(&args))) { |
| 3089 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3090 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3091 | return error; |
| 3092 | } |
| 3093 | /* |
| 3094 | * Allocation can't fail, the space was reserved. |
| 3095 | */ |
| 3096 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3097 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3098 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3099 | (flist->xbf_low && |
| 3100 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3101 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3102 | cur->bc_private.b.allocated++; |
| 3103 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3104 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3106 | /* |
| 3107 | * Fill in the child block. |
| 3108 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3109 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3110 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3112 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3113 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3114 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3116 | for (cnt = i = 0; i < nextents; i++) { |
| 3117 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3118 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3119 | arp->l0 = cpu_to_be64(ep->l0); |
| 3120 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | arp++; cnt++; |
| 3122 | } |
| 3123 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3124 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3125 | xfs_btree_set_numrecs(ablock, cnt); |
| 3126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | /* |
| 3128 | * Fill in the root key and pointer. |
| 3129 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3130 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3131 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3132 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3133 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3134 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3135 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | /* |
| 3138 | * Do all this logging at the end so that |
| 3139 | * the root is at the right level. |
| 3140 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3141 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3142 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | ASSERT(*curp == NULL); |
| 3144 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3145 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3150 | * Calculate the default attribute fork offset for newly created inodes. |
| 3151 | */ |
| 3152 | uint |
| 3153 | xfs_default_attroffset( |
| 3154 | struct xfs_inode *ip) |
| 3155 | { |
| 3156 | struct xfs_mount *mp = ip->i_mount; |
| 3157 | uint offset; |
| 3158 | |
| 3159 | if (mp->m_sb.sb_inodesize == 256) { |
| 3160 | offset = XFS_LITINO(mp) - |
| 3161 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3162 | } else { |
| 3163 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3164 | } |
| 3165 | |
| 3166 | ASSERT(offset < XFS_LITINO(mp)); |
| 3167 | return offset; |
| 3168 | } |
| 3169 | |
| 3170 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3171 | * Helper routine to reset inode di_forkoff field when switching |
| 3172 | * attribute fork from local to extent format - we reset it where |
| 3173 | * possible to make space available for inline data fork extents. |
| 3174 | */ |
| 3175 | STATIC void |
| 3176 | xfs_bmap_forkoff_reset( |
| 3177 | xfs_mount_t *mp, |
| 3178 | xfs_inode_t *ip, |
| 3179 | int whichfork) |
| 3180 | { |
| 3181 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3182 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3183 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3184 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3185 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3186 | |
| 3187 | if (dfl_forkoff > ip->i_d.di_forkoff) { |
| 3188 | ip->i_d.di_forkoff = dfl_forkoff; |
| 3189 | ip->i_df.if_ext_max = |
| 3190 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3191 | ip->i_afp->if_ext_max = |
| 3192 | XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3193 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | * Convert a local file to an extents file. |
| 3199 | * This code is out of bounds for data forks of regular files, |
| 3200 | * since the file data needs to get logged so things will stay consistent. |
| 3201 | * (The bmap-level manipulations are ok, though). |
| 3202 | */ |
| 3203 | STATIC int /* error */ |
| 3204 | xfs_bmap_local_to_extents( |
| 3205 | xfs_trans_t *tp, /* transaction pointer */ |
| 3206 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3207 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3208 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3209 | int *logflagsp, /* inode logging flags */ |
| 3210 | int whichfork) /* data or attr fork */ |
| 3211 | { |
| 3212 | int error; /* error return value */ |
| 3213 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3215 | |
| 3216 | /* |
| 3217 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3218 | * So sending the data fork of a regular inode is invalid. |
| 3219 | */ |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3220 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3222 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3223 | flags = 0; |
| 3224 | error = 0; |
| 3225 | if (ifp->if_bytes) { |
| 3226 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3227 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3228 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | |
| 3230 | args.tp = tp; |
| 3231 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3232 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3233 | ASSERT((ifp->if_flags & |
| 3234 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | /* |
| 3236 | * Allocate a block. We know we need only one, since the |
| 3237 | * file currently fits in an inode. |
| 3238 | */ |
| 3239 | if (*firstblock == NULLFSBLOCK) { |
| 3240 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3241 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3242 | } else { |
| 3243 | args.fsbno = *firstblock; |
| 3244 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3245 | } |
| 3246 | args.total = total; |
| 3247 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3248 | args.isfl = args.minalignslop = 0; |
| 3249 | args.minlen = args.maxlen = args.prod = 1; |
| 3250 | if ((error = xfs_alloc_vextent(&args))) |
| 3251 | goto done; |
| 3252 | /* |
| 3253 | * Can't fail, the space was reserved. |
| 3254 | */ |
| 3255 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3256 | ASSERT(args.len == 1); |
| 3257 | *firstblock = args.fsbno; |
| 3258 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 3259 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3261 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3263 | xfs_iext_add(ifp, 0, 1); |
| 3264 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3266 | trace_xfs_bmap_post_update(ip, 0, |
| 3267 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3268 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3270 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3271 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3273 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3274 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3276 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3279 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3280 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3281 | flags |= XFS_ILOG_CORE; |
| 3282 | done: |
| 3283 | *logflagsp = flags; |
| 3284 | return error; |
| 3285 | } |
| 3286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3288 | * Search the extent records for the entry containing block bno. |
| 3289 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3290 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3291 | * entry (null if none). Else, *lastxp will be set to the index |
| 3292 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3293 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3294 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3295 | xfs_bmap_search_multi_extents( |
| 3296 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3297 | xfs_fileoff_t bno, /* block number searched for */ |
| 3298 | int *eofp, /* out: end of file found */ |
| 3299 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3300 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3301 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3302 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3303 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3304 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3305 | |
| 3306 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3307 | * Initialize the extent entry structure to catch access to |
| 3308 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3309 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3310 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3311 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3312 | gotp->br_state = XFS_EXT_INVALID; |
| 3313 | #if XFS_BIG_BLKNOS |
| 3314 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3315 | #else |
| 3316 | gotp->br_startblock = 0xffffa5a5; |
| 3317 | #endif |
| 3318 | prevp->br_startoff = NULLFILEOFF; |
| 3319 | |
| 3320 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3321 | if (lastx > 0) { |
| 3322 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3323 | } |
| 3324 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3325 | xfs_bmbt_get_all(ep, gotp); |
| 3326 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3327 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3328 | if (lastx > 0) { |
| 3329 | *gotp = *prevp; |
| 3330 | } |
| 3331 | *eofp = 1; |
| 3332 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3333 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3334 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3335 | return ep; |
| 3336 | } |
| 3337 | |
| 3338 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | * Search the extents list for the inode, for the extent containing bno. |
| 3340 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3341 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3342 | * Else, *lastxp will be set to the index of the found |
| 3343 | * entry; *gotp will contain the entry. |
| 3344 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3345 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | xfs_bmap_search_extents( |
| 3347 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3348 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3349 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | int *eofp, /* out: end of file found */ |
| 3351 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3352 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3353 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3354 | { |
| 3355 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3356 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | |
| 3358 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3359 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3361 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3362 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3363 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3364 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3365 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3366 | "Access to block zero in inode %llu " |
| 3367 | "start_block: %llx start_off: %llx " |
| 3368 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3369 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3370 | (unsigned long long)gotp->br_startblock, |
| 3371 | (unsigned long long)gotp->br_startoff, |
| 3372 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3373 | gotp->br_state, *lastxp); |
| 3374 | *lastxp = NULLEXTNUM; |
| 3375 | *eofp = 1; |
| 3376 | return NULL; |
| 3377 | } |
| 3378 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | } |
| 3380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3381 | /* |
| 3382 | * Compute the worst-case number of indirect blocks that will be used |
| 3383 | * for ip's delayed extent of length "len". |
| 3384 | */ |
| 3385 | STATIC xfs_filblks_t |
| 3386 | xfs_bmap_worst_indlen( |
| 3387 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3388 | xfs_filblks_t len) /* delayed extent length */ |
| 3389 | { |
| 3390 | int level; /* btree level number */ |
| 3391 | int maxrecs; /* maximum record count at this level */ |
| 3392 | xfs_mount_t *mp; /* mount structure */ |
| 3393 | xfs_filblks_t rval; /* return value */ |
| 3394 | |
| 3395 | mp = ip->i_mount; |
| 3396 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3397 | for (level = 0, rval = 0; |
| 3398 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3399 | level++) { |
| 3400 | len += maxrecs - 1; |
| 3401 | do_div(len, maxrecs); |
| 3402 | rval += len; |
| 3403 | if (len == 1) |
| 3404 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3405 | level - 1; |
| 3406 | if (level == 0) |
| 3407 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3408 | } |
| 3409 | return rval; |
| 3410 | } |
| 3411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | /* |
| 3413 | * Convert inode from non-attributed to attributed. |
| 3414 | * Must not be in a transaction, ip must not be locked. |
| 3415 | */ |
| 3416 | int /* error code */ |
| 3417 | xfs_bmap_add_attrfork( |
| 3418 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3419 | int size, /* space new attribute needs */ |
| 3420 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3423 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3426 | int blks; /* space reservation */ |
| 3427 | int version = 1; /* superblock attr version */ |
| 3428 | int committed; /* xaction was committed */ |
| 3429 | int logflags; /* logging flags */ |
| 3430 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3432 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | ASSERT(ip->i_df.if_ext_max == |
| 3434 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | mp = ip->i_mount; |
| 3437 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3438 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3439 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3440 | if (rsvd) |
| 3441 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3442 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3443 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3444 | goto error0; |
| 3445 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3446 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3448 | XFS_QMOPT_RES_REGBLKS); |
| 3449 | if (error) { |
| 3450 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3451 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3452 | return error; |
| 3453 | } |
| 3454 | if (XFS_IFORK_Q(ip)) |
| 3455 | goto error1; |
| 3456 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3457 | /* |
| 3458 | * For inodes coming from pre-6.2 filesystems. |
| 3459 | */ |
| 3460 | ASSERT(ip->i_d.di_aformat == 0); |
| 3461 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3462 | } |
| 3463 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3464 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 3465 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3468 | switch (ip->i_d.di_format) { |
| 3469 | case XFS_DINODE_FMT_DEV: |
| 3470 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3471 | break; |
| 3472 | case XFS_DINODE_FMT_UUID: |
| 3473 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3474 | break; |
| 3475 | case XFS_DINODE_FMT_LOCAL: |
| 3476 | case XFS_DINODE_FMT_EXTENTS: |
| 3477 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3478 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3479 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3480 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3481 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3482 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | break; |
| 3484 | default: |
| 3485 | ASSERT(0); |
| 3486 | error = XFS_ERROR(EINVAL); |
| 3487 | goto error1; |
| 3488 | } |
| 3489 | ip->i_df.if_ext_max = |
| 3490 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3491 | ASSERT(ip->i_afp == NULL); |
| 3492 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 3493 | ip->i_afp->if_ext_max = |
| 3494 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3495 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3496 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3497 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | switch (ip->i_d.di_format) { |
| 3499 | case XFS_DINODE_FMT_LOCAL: |
| 3500 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3501 | &logflags); |
| 3502 | break; |
| 3503 | case XFS_DINODE_FMT_EXTENTS: |
| 3504 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3505 | &flist, &logflags); |
| 3506 | break; |
| 3507 | case XFS_DINODE_FMT_BTREE: |
| 3508 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3509 | &logflags); |
| 3510 | break; |
| 3511 | default: |
| 3512 | error = 0; |
| 3513 | break; |
| 3514 | } |
| 3515 | if (logflags) |
| 3516 | xfs_trans_log_inode(tp, ip, logflags); |
| 3517 | if (error) |
| 3518 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3519 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3520 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3521 | __int64_t sbfields = 0; |
| 3522 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3523 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3524 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3525 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3526 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3527 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3528 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3529 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3530 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3531 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3532 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3533 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3534 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3536 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3538 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | goto error2; |
Dave Chinner | 713bf88 | 2010-03-08 11:26:23 +1100 | [diff] [blame] | 3540 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | ASSERT(ip->i_df.if_ext_max == |
| 3542 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3543 | return error; |
| 3544 | error2: |
| 3545 | xfs_bmap_cancel(&flist); |
| 3546 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3548 | error0: |
| 3549 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 3550 | ASSERT(ip->i_df.if_ext_max == |
| 3551 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3552 | return error; |
| 3553 | } |
| 3554 | |
| 3555 | /* |
| 3556 | * Add the extent to the list of extents to be free at transaction end. |
| 3557 | * The list is maintained sorted (by block number). |
| 3558 | */ |
| 3559 | /* ARGSUSED */ |
| 3560 | void |
| 3561 | xfs_bmap_add_free( |
| 3562 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3563 | xfs_filblks_t len, /* length of extent */ |
| 3564 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3565 | xfs_mount_t *mp) /* mount point structure */ |
| 3566 | { |
| 3567 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3568 | xfs_bmap_free_item_t *new; /* new element */ |
| 3569 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3570 | #ifdef DEBUG |
| 3571 | xfs_agnumber_t agno; |
| 3572 | xfs_agblock_t agbno; |
| 3573 | |
| 3574 | ASSERT(bno != NULLFSBLOCK); |
| 3575 | ASSERT(len > 0); |
| 3576 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3577 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3579 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3580 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3581 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3582 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3583 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3584 | #endif |
| 3585 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3586 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3587 | new->xbfi_startblock = bno; |
| 3588 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3589 | for (prev = NULL, cur = flist->xbf_first; |
| 3590 | cur != NULL; |
| 3591 | prev = cur, cur = cur->xbfi_next) { |
| 3592 | if (cur->xbfi_startblock >= bno) |
| 3593 | break; |
| 3594 | } |
| 3595 | if (prev) |
| 3596 | prev->xbfi_next = new; |
| 3597 | else |
| 3598 | flist->xbf_first = new; |
| 3599 | new->xbfi_next = cur; |
| 3600 | flist->xbf_count++; |
| 3601 | } |
| 3602 | |
| 3603 | /* |
| 3604 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3605 | * in this filesystem. Done once, during mount. |
| 3606 | */ |
| 3607 | void |
| 3608 | xfs_bmap_compute_maxlevels( |
| 3609 | xfs_mount_t *mp, /* file system mount structure */ |
| 3610 | int whichfork) /* data or attr fork */ |
| 3611 | { |
| 3612 | int level; /* btree level */ |
| 3613 | uint maxblocks; /* max blocks at this level */ |
| 3614 | uint maxleafents; /* max leaf entries possible */ |
| 3615 | int maxrootrecs; /* max records in root block */ |
| 3616 | int minleafrecs; /* min records in leaf block */ |
| 3617 | int minnoderecs; /* min records in node block */ |
| 3618 | int sz; /* root block size */ |
| 3619 | |
| 3620 | /* |
| 3621 | * The maximum number of extents in a file, hence the maximum |
| 3622 | * number of leaf entries, is controlled by the type of di_nextents |
| 3623 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3624 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3625 | * |
| 3626 | * Note that we can no longer assume that if we are in ATTR1 that |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3627 | * the fork offset of all the inodes will be |
| 3628 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3629 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3630 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3631 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3632 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3634 | if (whichfork == XFS_DATA_FORK) { |
| 3635 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3636 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3637 | } else { |
| 3638 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3639 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3640 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3641 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3643 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3645 | for (level = 1; maxblocks > 1; level++) { |
| 3646 | if (maxblocks <= maxrootrecs) |
| 3647 | maxblocks = 1; |
| 3648 | else |
| 3649 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3650 | } |
| 3651 | mp->m_bm_maxlevels[whichfork] = level; |
| 3652 | } |
| 3653 | |
| 3654 | /* |
| 3655 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3656 | * caller. Frees all the extents that need freeing, which must be done |
| 3657 | * last due to locking considerations. We never free any extents in |
Christoph Hellwig | 859f57c | 2011-08-27 14:45:11 +0000 | [diff] [blame] | 3658 | * the first transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | * |
| 3660 | * Return 1 if the given transaction was committed and a new one |
| 3661 | * started, and 0 otherwise in the committed parameter. |
| 3662 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | int /* error */ |
| 3664 | xfs_bmap_finish( |
| 3665 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3666 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | int *committed) /* xact committed or not */ |
| 3668 | { |
| 3669 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3670 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3671 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3672 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | unsigned int logres; /* new log reservation */ |
| 3674 | unsigned int logcount; /* new log count */ |
| 3675 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3676 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3677 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3678 | |
| 3679 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3680 | if (flist->xbf_count == 0) { |
| 3681 | *committed = 0; |
| 3682 | return 0; |
| 3683 | } |
| 3684 | ntp = *tp; |
| 3685 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3686 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3687 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3688 | free->xbfi_blockcount); |
| 3689 | logres = ntp->t_log_res; |
| 3690 | logcount = ntp->t_log_count; |
| 3691 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3692 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | *tp = ntp; |
| 3694 | *committed = 1; |
| 3695 | /* |
| 3696 | * We have a new transaction, so we should return committed=1, |
| 3697 | * even though we're returning an error. |
| 3698 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3699 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3700 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3701 | |
| 3702 | /* |
| 3703 | * transaction commit worked ok so we can drop the extra ticket |
| 3704 | * reference that we gained in xfs_trans_dup() |
| 3705 | */ |
| 3706 | xfs_log_ticket_put(ntp->t_ticket); |
| 3707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 3709 | logcount))) |
| 3710 | return error; |
| 3711 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 3712 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 3713 | next = free->xbfi_next; |
| 3714 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 3715 | free->xbfi_blockcount))) { |
| 3716 | /* |
| 3717 | * The bmap free list will be cleaned up at a |
| 3718 | * higher level. The EFI will be canceled when |
| 3719 | * this transaction is aborted. |
| 3720 | * Need to force shutdown here to make sure it |
| 3721 | * happens, since this transaction may not be |
| 3722 | * dirty yet. |
| 3723 | */ |
| 3724 | mp = ntp->t_mountp; |
| 3725 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 3726 | xfs_force_shutdown(mp, |
| 3727 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3728 | SHUTDOWN_CORRUPT_INCORE : |
| 3729 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | return error; |
| 3731 | } |
| 3732 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 3733 | free->xbfi_blockcount); |
| 3734 | xfs_bmap_del_free(flist, NULL, free); |
| 3735 | } |
| 3736 | return 0; |
| 3737 | } |
| 3738 | |
| 3739 | /* |
| 3740 | * Free up any items left in the list. |
| 3741 | */ |
| 3742 | void |
| 3743 | xfs_bmap_cancel( |
| 3744 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 3745 | { |
| 3746 | xfs_bmap_free_item_t *free; /* free list item */ |
| 3747 | xfs_bmap_free_item_t *next; |
| 3748 | |
| 3749 | if (flist->xbf_count == 0) |
| 3750 | return; |
| 3751 | ASSERT(flist->xbf_first != NULL); |
| 3752 | for (free = flist->xbf_first; free; free = next) { |
| 3753 | next = free->xbfi_next; |
| 3754 | xfs_bmap_del_free(flist, NULL, free); |
| 3755 | } |
| 3756 | ASSERT(flist->xbf_count == 0); |
| 3757 | } |
| 3758 | |
| 3759 | /* |
| 3760 | * Returns the file-relative block number of the first unused block(s) |
| 3761 | * in the file with at least "len" logically contiguous blocks free. |
| 3762 | * This is the lowest-address hole if the file has holes, else the first block |
| 3763 | * past the end of file. |
| 3764 | * Return 0 if the file is currently local (in-inode). |
| 3765 | */ |
| 3766 | int /* error */ |
| 3767 | xfs_bmap_first_unused( |
| 3768 | xfs_trans_t *tp, /* transaction pointer */ |
| 3769 | xfs_inode_t *ip, /* incore inode */ |
| 3770 | xfs_extlen_t len, /* size of hole to find */ |
| 3771 | xfs_fileoff_t *first_unused, /* unused block */ |
| 3772 | int whichfork) /* data or attr fork */ |
| 3773 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3775 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3777 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 3778 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 3779 | xfs_fileoff_t max; /* starting useful block */ |
| 3780 | xfs_fileoff_t off; /* offset for this block */ |
| 3781 | xfs_extnum_t nextents; /* number of extent entries */ |
| 3782 | |
| 3783 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 3784 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 3785 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3786 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3787 | *first_unused = 0; |
| 3788 | return 0; |
| 3789 | } |
| 3790 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3791 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3792 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3793 | return error; |
| 3794 | lowest = *first_unused; |
| 3795 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3796 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3797 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | off = xfs_bmbt_get_startoff(ep); |
| 3799 | /* |
| 3800 | * See if the hole before this extent will work. |
| 3801 | */ |
| 3802 | if (off >= lowest + len && off - max >= len) { |
| 3803 | *first_unused = max; |
| 3804 | return 0; |
| 3805 | } |
| 3806 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 3807 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 3808 | } |
| 3809 | *first_unused = max; |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | /* |
| 3814 | * Returns the file-relative block number of the last block + 1 before |
| 3815 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3816 | * This is not based on i_size, it is based on the extent records. |
| 3817 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | */ |
| 3819 | int /* error */ |
| 3820 | xfs_bmap_last_before( |
| 3821 | xfs_trans_t *tp, /* transaction pointer */ |
| 3822 | xfs_inode_t *ip, /* incore inode */ |
| 3823 | xfs_fileoff_t *last_block, /* last block */ |
| 3824 | int whichfork) /* data or attr fork */ |
| 3825 | { |
| 3826 | xfs_fileoff_t bno; /* input file offset */ |
| 3827 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3828 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | int error; /* error return value */ |
| 3830 | xfs_bmbt_irec_t got; /* current extent value */ |
| 3831 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3832 | xfs_extnum_t lastx; /* last extent used */ |
| 3833 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 3834 | |
| 3835 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 3836 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3837 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 3838 | return XFS_ERROR(EIO); |
| 3839 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3840 | *last_block = 0; |
| 3841 | return 0; |
| 3842 | } |
| 3843 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3844 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3845 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3846 | return error; |
| 3847 | bno = *last_block - 1; |
| 3848 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 3849 | &prev); |
| 3850 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 3851 | if (prev.br_startoff == NULLFILEOFF) |
| 3852 | *last_block = 0; |
| 3853 | else |
| 3854 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 3855 | } |
| 3856 | /* |
| 3857 | * Otherwise *last_block is already the right answer. |
| 3858 | */ |
| 3859 | return 0; |
| 3860 | } |
| 3861 | |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3862 | STATIC int |
| 3863 | xfs_bmap_last_extent( |
| 3864 | struct xfs_trans *tp, |
| 3865 | struct xfs_inode *ip, |
| 3866 | int whichfork, |
| 3867 | struct xfs_bmbt_irec *rec, |
| 3868 | int *is_empty) |
| 3869 | { |
| 3870 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3871 | int error; |
| 3872 | int nextents; |
| 3873 | |
| 3874 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3875 | error = xfs_iread_extents(tp, ip, whichfork); |
| 3876 | if (error) |
| 3877 | return error; |
| 3878 | } |
| 3879 | |
| 3880 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 3881 | if (nextents == 0) { |
| 3882 | *is_empty = 1; |
| 3883 | return 0; |
| 3884 | } |
| 3885 | |
| 3886 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 3887 | *is_empty = 0; |
| 3888 | return 0; |
| 3889 | } |
| 3890 | |
| 3891 | /* |
| 3892 | * Check the last inode extent to determine whether this allocation will result |
| 3893 | * in blocks being allocated at the end of the file. When we allocate new data |
| 3894 | * blocks at the end of the file which do not start at the previous data block, |
| 3895 | * we will try to align the new blocks at stripe unit boundaries. |
| 3896 | * |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3897 | * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be |
| 3898 | * at, or past the EOF. |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3899 | */ |
| 3900 | STATIC int |
| 3901 | xfs_bmap_isaeof( |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3902 | struct xfs_bmalloca *bma, |
| 3903 | int whichfork) |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3904 | { |
| 3905 | struct xfs_bmbt_irec rec; |
| 3906 | int is_empty; |
| 3907 | int error; |
| 3908 | |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3909 | bma->aeof = 0; |
| 3910 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 3911 | &is_empty); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3912 | if (error || is_empty) |
| 3913 | return error; |
| 3914 | |
| 3915 | /* |
| 3916 | * Check if we are allocation or past the last extent, or at least into |
| 3917 | * the last delayed allocated extent. |
| 3918 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3919 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 3920 | (bma->offset >= rec.br_startoff && |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3921 | isnullstartblock(rec.br_startblock)); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3922 | return 0; |
| 3923 | } |
| 3924 | |
| 3925 | /* |
| 3926 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 3927 | * the allocation to a stripe unit boundary. All offsets are considered outside |
| 3928 | * the end of file for an empty fork, so 1 is returned in *eof in that case. |
| 3929 | */ |
| 3930 | int |
| 3931 | xfs_bmap_eof( |
| 3932 | struct xfs_inode *ip, |
| 3933 | xfs_fileoff_t endoff, |
| 3934 | int whichfork, |
| 3935 | int *eof) |
| 3936 | { |
| 3937 | struct xfs_bmbt_irec rec; |
| 3938 | int error; |
| 3939 | |
| 3940 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); |
| 3941 | if (error || *eof) |
| 3942 | return error; |
| 3943 | |
| 3944 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; |
| 3945 | return 0; |
| 3946 | } |
| 3947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | /* |
| 3949 | * Returns the file-relative block number of the first block past eof in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3950 | * the file. This is not based on i_size, it is based on the extent records. |
| 3951 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | */ |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3953 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | xfs_bmap_last_offset( |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3955 | struct xfs_trans *tp, |
| 3956 | struct xfs_inode *ip, |
| 3957 | xfs_fileoff_t *last_block, |
| 3958 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | { |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3960 | struct xfs_bmbt_irec rec; |
| 3961 | int is_empty; |
| 3962 | int error; |
| 3963 | |
| 3964 | *last_block = 0; |
| 3965 | |
| 3966 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 3967 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | |
| 3969 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3970 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | return XFS_ERROR(EIO); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3972 | |
| 3973 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 3974 | if (error || is_empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | return error; |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3976 | |
| 3977 | *last_block = rec.br_startoff + rec.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | return 0; |
| 3979 | } |
| 3980 | |
| 3981 | /* |
| 3982 | * Returns whether the selected fork of the inode has exactly one |
| 3983 | * block or not. For the data fork we check this matches di_size, |
| 3984 | * implying the file's range is 0..bsize-1. |
| 3985 | */ |
| 3986 | int /* 1=>1 block, 0=>otherwise */ |
| 3987 | xfs_bmap_one_block( |
| 3988 | xfs_inode_t *ip, /* incore inode */ |
| 3989 | int whichfork) /* data or attr fork */ |
| 3990 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3991 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3993 | int rval; /* return value */ |
| 3994 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 3995 | |
| 3996 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3997 | if (whichfork == XFS_DATA_FORK) { |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3998 | return S_ISREG(ip->i_d.di_mode) ? |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3999 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 4000 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 4001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | #endif /* !DEBUG */ |
| 4003 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4004 | return 0; |
| 4005 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4006 | return 0; |
| 4007 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4008 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4009 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | xfs_bmbt_get_all(ep, &s); |
| 4011 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4012 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4013 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | return rval; |
| 4015 | } |
| 4016 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4017 | STATIC int |
| 4018 | xfs_bmap_sanity_check( |
| 4019 | struct xfs_mount *mp, |
| 4020 | struct xfs_buf *bp, |
| 4021 | int level) |
| 4022 | { |
| 4023 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4024 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 4025 | if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) || |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4026 | be16_to_cpu(block->bb_level) != level || |
| 4027 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4028 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4029 | return 0; |
| 4030 | return 1; |
| 4031 | } |
| 4032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | /* |
| 4034 | * Read in the extents to if_extents. |
| 4035 | * All inode fields are set up by caller, we just traverse the btree |
| 4036 | * and copy the records in. If the file system cannot contain unwritten |
| 4037 | * extents, the records are checked for no "state" flags. |
| 4038 | */ |
| 4039 | int /* error */ |
| 4040 | xfs_bmap_read_extents( |
| 4041 | xfs_trans_t *tp, /* transaction pointer */ |
| 4042 | xfs_inode_t *ip, /* incore inode */ |
| 4043 | int whichfork) /* data or attr fork */ |
| 4044 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4045 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4047 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4048 | int error; /* error return value */ |
| 4049 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4051 | xfs_ifork_t *ifp; /* fork structure */ |
| 4052 | int level; /* btree level, for checking */ |
| 4053 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4054 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | /* REFERENCED */ |
| 4056 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | |
| 4058 | bno = NULLFSBLOCK; |
| 4059 | mp = ip->i_mount; |
| 4060 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4061 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4062 | XFS_EXTFMT_INODE(ip); |
| 4063 | block = ifp->if_broot; |
| 4064 | /* |
| 4065 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4066 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4067 | level = be16_to_cpu(block->bb_level); |
| 4068 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4069 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4070 | bno = be64_to_cpu(*pp); |
| 4071 | ASSERT(bno != NULLDFSBNO); |
| 4072 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4073 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | /* |
| 4075 | * Go down the tree until leaf level is reached, following the first |
| 4076 | * pointer (leftmost) at each level. |
| 4077 | */ |
| 4078 | while (level-- > 0) { |
| 4079 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4080 | XFS_BMAP_BTREE_REF))) |
| 4081 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4082 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4084 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | error0); |
| 4086 | if (level == 0) |
| 4087 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4088 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4089 | bno = be64_to_cpu(*pp); |
| 4090 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | xfs_trans_brelse(tp, bp); |
| 4092 | } |
| 4093 | /* |
| 4094 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4095 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4096 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | i = 0; |
| 4098 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4099 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | */ |
| 4101 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4102 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | xfs_fsblock_t nextbno; |
| 4104 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4105 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4107 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | if (unlikely(i + num_recs > room)) { |
| 4109 | ASSERT(i + num_recs <= room); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4110 | xfs_warn(ip->i_mount, |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4111 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | (unsigned long long) ip->i_ino); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4113 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 4114 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | goto error0; |
| 4116 | } |
| 4117 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4118 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | error0); |
| 4120 | /* |
| 4121 | * Read-ahead the next leaf block, if any. |
| 4122 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4123 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | if (nextbno != NULLFSBLOCK) |
| 4125 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4126 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4127 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4129 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4130 | start = i; |
| 4131 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4132 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4133 | trp->l0 = be64_to_cpu(frp->l0); |
| 4134 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | } |
| 4136 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4137 | /* |
| 4138 | * Check all attribute bmap btree records and |
| 4139 | * any "older" data bmap btree records for a |
| 4140 | * set bit in the "extent flag" position. |
| 4141 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4142 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4143 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4145 | XFS_ERRLEVEL_LOW, |
| 4146 | ip->i_mount); |
| 4147 | goto error0; |
| 4148 | } |
| 4149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | xfs_trans_brelse(tp, bp); |
| 4151 | bno = nextbno; |
| 4152 | /* |
| 4153 | * If we've reached the end, stop. |
| 4154 | */ |
| 4155 | if (bno == NULLFSBLOCK) |
| 4156 | break; |
| 4157 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4158 | XFS_BMAP_BTREE_REF))) |
| 4159 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4160 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4162 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4164 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | return 0; |
| 4166 | error0: |
| 4167 | xfs_trans_brelse(tp, bp); |
| 4168 | return XFS_ERROR(EFSCORRUPTED); |
| 4169 | } |
| 4170 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4171 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4173 | * Add bmap trace insert entries for all the contents of the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | */ |
| 4175 | void |
| 4176 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4178 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4179 | int whichfork, /* data or attr fork */ |
| 4180 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4182 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4184 | int state = 0; |
| 4185 | |
| 4186 | if (whichfork == XFS_ATTR_FORK) |
| 4187 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | |
| 4189 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4190 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4191 | for (idx = 0; idx < cnt; idx++) |
| 4192 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | /* |
| 4196 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4197 | * given the callers original parameters. Specifically check the |
| 4198 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4199 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4200 | */ |
| 4201 | STATIC void |
| 4202 | xfs_bmap_validate_ret( |
| 4203 | xfs_fileoff_t bno, |
| 4204 | xfs_filblks_t len, |
| 4205 | int flags, |
| 4206 | xfs_bmbt_irec_t *mval, |
| 4207 | int nmap, |
| 4208 | int ret_nmap) |
| 4209 | { |
| 4210 | int i; /* index to map values */ |
| 4211 | |
| 4212 | ASSERT(ret_nmap <= nmap); |
| 4213 | |
| 4214 | for (i = 0; i < ret_nmap; i++) { |
| 4215 | ASSERT(mval[i].br_blockcount > 0); |
| 4216 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4217 | ASSERT(mval[i].br_startoff >= bno); |
| 4218 | ASSERT(mval[i].br_blockcount <= len); |
| 4219 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4220 | bno + len); |
| 4221 | } else { |
| 4222 | ASSERT(mval[i].br_startoff < bno + len); |
| 4223 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4224 | bno); |
| 4225 | } |
| 4226 | ASSERT(i == 0 || |
| 4227 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4228 | mval[i].br_startoff); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4229 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4230 | mval[i].br_startblock != HOLESTARTBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4232 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4233 | } |
| 4234 | } |
| 4235 | #endif /* DEBUG */ |
| 4236 | |
| 4237 | |
| 4238 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4239 | * Trim the returned map to the required bounds |
| 4240 | */ |
| 4241 | STATIC void |
| 4242 | xfs_bmapi_trim_map( |
| 4243 | struct xfs_bmbt_irec *mval, |
| 4244 | struct xfs_bmbt_irec *got, |
| 4245 | xfs_fileoff_t *bno, |
| 4246 | xfs_filblks_t len, |
| 4247 | xfs_fileoff_t obno, |
| 4248 | xfs_fileoff_t end, |
| 4249 | int n, |
| 4250 | int flags) |
| 4251 | { |
| 4252 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 4253 | got->br_startoff + got->br_blockcount <= obno) { |
| 4254 | *mval = *got; |
| 4255 | if (isnullstartblock(got->br_startblock)) |
| 4256 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4257 | return; |
| 4258 | } |
| 4259 | |
| 4260 | if (obno > *bno) |
| 4261 | *bno = obno; |
| 4262 | ASSERT((*bno >= obno) || (n == 0)); |
| 4263 | ASSERT(*bno < end); |
| 4264 | mval->br_startoff = *bno; |
| 4265 | if (isnullstartblock(got->br_startblock)) |
| 4266 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4267 | else |
| 4268 | mval->br_startblock = got->br_startblock + |
| 4269 | (*bno - got->br_startoff); |
| 4270 | /* |
| 4271 | * Return the minimum of what we got and what we asked for for |
| 4272 | * the length. We can use the len variable here because it is |
| 4273 | * modified below and we could have been there before coming |
| 4274 | * here if the first part of the allocation didn't overlap what |
| 4275 | * was asked for. |
| 4276 | */ |
| 4277 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 4278 | got->br_blockcount - (*bno - got->br_startoff)); |
| 4279 | mval->br_state = got->br_state; |
| 4280 | ASSERT(mval->br_blockcount <= len); |
| 4281 | return; |
| 4282 | } |
| 4283 | |
| 4284 | /* |
| 4285 | * Update and validate the extent map to return |
| 4286 | */ |
| 4287 | STATIC void |
| 4288 | xfs_bmapi_update_map( |
| 4289 | struct xfs_bmbt_irec **map, |
| 4290 | xfs_fileoff_t *bno, |
| 4291 | xfs_filblks_t *len, |
| 4292 | xfs_fileoff_t obno, |
| 4293 | xfs_fileoff_t end, |
| 4294 | int *n, |
| 4295 | int flags) |
| 4296 | { |
| 4297 | xfs_bmbt_irec_t *mval = *map; |
| 4298 | |
| 4299 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4300 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 4301 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 4302 | (mval->br_startoff < obno)); |
| 4303 | |
| 4304 | *bno = mval->br_startoff + mval->br_blockcount; |
| 4305 | *len = end - *bno; |
| 4306 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4307 | /* update previous map with new information */ |
| 4308 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4309 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4310 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4311 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4312 | mval[-1].br_state = mval->br_state; |
| 4313 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4314 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4315 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4316 | mval->br_startblock == mval[-1].br_startblock + |
| 4317 | mval[-1].br_blockcount && |
| 4318 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4319 | mval[-1].br_state == mval->br_state)) { |
| 4320 | ASSERT(mval->br_startoff == |
| 4321 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4322 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4323 | } else if (*n > 0 && |
| 4324 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4325 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4326 | mval->br_startoff == |
| 4327 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4328 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4329 | mval[-1].br_state = mval->br_state; |
| 4330 | } else if (!((*n == 0) && |
| 4331 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4332 | obno))) { |
| 4333 | mval++; |
| 4334 | (*n)++; |
| 4335 | } |
| 4336 | *map = mval; |
| 4337 | } |
| 4338 | |
| 4339 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4340 | * Map file blocks to filesystem blocks without allocation. |
| 4341 | */ |
| 4342 | int |
| 4343 | xfs_bmapi_read( |
| 4344 | struct xfs_inode *ip, |
| 4345 | xfs_fileoff_t bno, |
| 4346 | xfs_filblks_t len, |
| 4347 | struct xfs_bmbt_irec *mval, |
| 4348 | int *nmap, |
| 4349 | int flags) |
| 4350 | { |
| 4351 | struct xfs_mount *mp = ip->i_mount; |
| 4352 | struct xfs_ifork *ifp; |
| 4353 | struct xfs_bmbt_irec got; |
| 4354 | struct xfs_bmbt_irec prev; |
| 4355 | xfs_fileoff_t obno; |
| 4356 | xfs_fileoff_t end; |
| 4357 | xfs_extnum_t lastx; |
| 4358 | int error; |
| 4359 | int eof; |
| 4360 | int n = 0; |
| 4361 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4362 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4363 | |
| 4364 | ASSERT(*nmap >= 1); |
| 4365 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 4366 | XFS_BMAPI_IGSTATE))); |
| 4367 | |
| 4368 | if (unlikely(XFS_TEST_ERROR( |
| 4369 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4370 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4371 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4372 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
| 4373 | return XFS_ERROR(EFSCORRUPTED); |
| 4374 | } |
| 4375 | |
| 4376 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4377 | return XFS_ERROR(EIO); |
| 4378 | |
| 4379 | XFS_STATS_INC(xs_blk_mapr); |
| 4380 | |
| 4381 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4382 | ASSERT(ifp->if_ext_max == |
| 4383 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4384 | |
| 4385 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4386 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4387 | if (error) |
| 4388 | return error; |
| 4389 | } |
| 4390 | |
| 4391 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4392 | end = bno + len; |
| 4393 | obno = bno; |
| 4394 | |
| 4395 | while (bno < end && n < *nmap) { |
| 4396 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4397 | if (eof) |
| 4398 | got.br_startoff = end; |
| 4399 | if (got.br_startoff > bno) { |
| 4400 | /* Reading in a hole. */ |
| 4401 | mval->br_startoff = bno; |
| 4402 | mval->br_startblock = HOLESTARTBLOCK; |
| 4403 | mval->br_blockcount = |
| 4404 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4405 | mval->br_state = XFS_EXT_NORM; |
| 4406 | bno += mval->br_blockcount; |
| 4407 | len -= mval->br_blockcount; |
| 4408 | mval++; |
| 4409 | n++; |
| 4410 | continue; |
| 4411 | } |
| 4412 | |
| 4413 | /* set up the extent map to return. */ |
| 4414 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4415 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4416 | |
| 4417 | /* If we're done, stop now. */ |
| 4418 | if (bno >= end || n >= *nmap) |
| 4419 | break; |
| 4420 | |
| 4421 | /* Else go on to the next record. */ |
| 4422 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4423 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4424 | else |
| 4425 | eof = 1; |
| 4426 | } |
| 4427 | *nmap = n; |
| 4428 | return 0; |
| 4429 | } |
| 4430 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4431 | STATIC int |
| 4432 | xfs_bmapi_reserve_delalloc( |
| 4433 | struct xfs_inode *ip, |
| 4434 | xfs_fileoff_t aoff, |
| 4435 | xfs_filblks_t len, |
| 4436 | struct xfs_bmbt_irec *got, |
| 4437 | struct xfs_bmbt_irec *prev, |
| 4438 | xfs_extnum_t *lastx, |
| 4439 | int eof) |
| 4440 | { |
| 4441 | struct xfs_mount *mp = ip->i_mount; |
| 4442 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4443 | xfs_extlen_t alen; |
| 4444 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4445 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4446 | xfs_extlen_t extsz; |
| 4447 | int error; |
| 4448 | |
| 4449 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4450 | if (!eof) |
| 4451 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4452 | |
| 4453 | /* Figure out the extent size, adjust alen */ |
| 4454 | extsz = xfs_get_extsz_hint(ip); |
| 4455 | if (extsz) { |
| 4456 | /* |
| 4457 | * Make sure we don't exceed a single extent length when we |
| 4458 | * align the extent by reducing length we are going to |
| 4459 | * allocate by the maximum amount extent size aligment may |
| 4460 | * require. |
| 4461 | */ |
| 4462 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); |
| 4463 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4464 | 1, 0, &aoff, &alen); |
| 4465 | ASSERT(!error); |
| 4466 | } |
| 4467 | |
| 4468 | if (rt) |
| 4469 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4470 | |
| 4471 | /* |
| 4472 | * Make a transaction-less quota reservation for delayed allocation |
| 4473 | * blocks. This number gets adjusted later. We return if we haven't |
| 4474 | * allocated blocks already inside this loop. |
| 4475 | */ |
| 4476 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4477 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4478 | if (error) |
| 4479 | return error; |
| 4480 | |
| 4481 | /* |
| 4482 | * Split changing sb for alen and indlen since they could be coming |
| 4483 | * from different places. |
| 4484 | */ |
| 4485 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4486 | ASSERT(indlen > 0); |
| 4487 | |
| 4488 | if (rt) { |
| 4489 | error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
| 4490 | -((int64_t)extsz), 0); |
| 4491 | } else { |
| 4492 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4493 | -((int64_t)alen), 0); |
| 4494 | } |
| 4495 | |
| 4496 | if (error) |
| 4497 | goto out_unreserve_quota; |
| 4498 | |
| 4499 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4500 | -((int64_t)indlen), 0); |
| 4501 | if (error) |
| 4502 | goto out_unreserve_blocks; |
| 4503 | |
| 4504 | |
| 4505 | ip->i_delayed_blks += alen; |
| 4506 | |
| 4507 | got->br_startoff = aoff; |
| 4508 | got->br_startblock = nullstartblock(indlen); |
| 4509 | got->br_blockcount = alen; |
| 4510 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4511 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4512 | |
| 4513 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4514 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4515 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4516 | */ |
| 4517 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4518 | |
| 4519 | ASSERT(got->br_startoff <= aoff); |
| 4520 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4521 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4522 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4523 | return 0; |
| 4524 | |
| 4525 | out_unreserve_blocks: |
| 4526 | if (rt) |
| 4527 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0); |
| 4528 | else |
| 4529 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0); |
| 4530 | out_unreserve_quota: |
| 4531 | if (XFS_IS_QUOTA_ON(mp)) |
| 4532 | xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ? |
| 4533 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4534 | return error; |
| 4535 | } |
| 4536 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4537 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4538 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4539 | */ |
| 4540 | int |
| 4541 | xfs_bmapi_delay( |
| 4542 | struct xfs_inode *ip, /* incore inode */ |
| 4543 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4544 | xfs_filblks_t len, /* length to map in file */ |
| 4545 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4546 | int *nmap, /* i/o: mval size/count */ |
| 4547 | int flags) /* XFS_BMAPI_... */ |
| 4548 | { |
| 4549 | struct xfs_mount *mp = ip->i_mount; |
| 4550 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4551 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4552 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4553 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4554 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4555 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4556 | int eof; /* we've hit the end of extents */ |
| 4557 | int n = 0; /* current extent index */ |
| 4558 | int error = 0; |
| 4559 | |
| 4560 | ASSERT(*nmap >= 1); |
| 4561 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4562 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
| 4563 | |
| 4564 | if (unlikely(XFS_TEST_ERROR( |
| 4565 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4566 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4567 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4568 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
| 4569 | return XFS_ERROR(EFSCORRUPTED); |
| 4570 | } |
| 4571 | |
| 4572 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4573 | return XFS_ERROR(EIO); |
| 4574 | |
| 4575 | XFS_STATS_INC(xs_blk_mapw); |
| 4576 | |
| 4577 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4578 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4579 | if (error) |
| 4580 | return error; |
| 4581 | } |
| 4582 | |
| 4583 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4584 | end = bno + len; |
| 4585 | obno = bno; |
| 4586 | |
| 4587 | while (bno < end && n < *nmap) { |
| 4588 | if (eof || got.br_startoff > bno) { |
| 4589 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4590 | &prev, &lastx, eof); |
| 4591 | if (error) { |
| 4592 | if (n == 0) { |
| 4593 | *nmap = 0; |
| 4594 | return error; |
| 4595 | } |
| 4596 | break; |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | /* set up the extent map to return. */ |
| 4601 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4602 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4603 | |
| 4604 | /* If we're done, stop now. */ |
| 4605 | if (bno >= end || n >= *nmap) |
| 4606 | break; |
| 4607 | |
| 4608 | /* Else go on to the next record. */ |
| 4609 | prev = got; |
| 4610 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4611 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4612 | else |
| 4613 | eof = 1; |
| 4614 | } |
| 4615 | |
| 4616 | *nmap = n; |
| 4617 | return 0; |
| 4618 | } |
| 4619 | |
| 4620 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4621 | STATIC int |
| 4622 | xfs_bmapi_allocate( |
| 4623 | struct xfs_bmalloca *bma, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4624 | int flags) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4625 | { |
| 4626 | struct xfs_mount *mp = bma->ip->i_mount; |
| 4627 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4628 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4629 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4630 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4631 | int error; |
| 4632 | int rt; |
| 4633 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4634 | ASSERT(bma->length > 0); |
| 4635 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4636 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip); |
| 4637 | |
| 4638 | /* |
| 4639 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4640 | * for in this bmap call but that wouldn't be as good. |
| 4641 | */ |
| 4642 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4643 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4644 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4645 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4646 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4647 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4648 | } |
| 4649 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4650 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4651 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4652 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4653 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | /* |
| 4657 | * Indicate if this is the first user data in the file, or just any |
| 4658 | * user data. |
| 4659 | */ |
| 4660 | if (!(flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4661 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4662 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
| 4663 | } |
| 4664 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4665 | bma->minlen = (flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4666 | |
| 4667 | /* |
| 4668 | * Only want to do the alignment at the eof if it is userdata and |
| 4669 | * allocation length is larger than a stripe unit. |
| 4670 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4671 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4672 | !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4673 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4674 | if (error) |
| 4675 | return error; |
| 4676 | } |
| 4677 | |
| 4678 | error = xfs_bmap_alloc(bma); |
| 4679 | if (error) |
| 4680 | return error; |
| 4681 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4682 | if (bma->flist->xbf_low) |
| 4683 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4684 | if (bma->cur) |
| 4685 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4686 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4687 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4688 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4689 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4690 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4691 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4692 | } |
| 4693 | /* |
| 4694 | * Bump the number of extents we've allocated |
| 4695 | * in this call. |
| 4696 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4697 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4698 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4699 | if (bma->cur) |
| 4700 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4701 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4702 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4703 | bma->got.br_startoff = bma->offset; |
| 4704 | bma->got.br_startblock = bma->blkno; |
| 4705 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4706 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4707 | |
| 4708 | /* |
| 4709 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4710 | * as unwritten. |
| 4711 | */ |
| 4712 | if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) && |
| 4713 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4714 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4715 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4716 | if (bma->wasdel) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 4717 | error = xfs_bmap_add_extent_delay_real(bma); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4718 | else |
| 4719 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4720 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4721 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4722 | if (error) |
| 4723 | return error; |
| 4724 | |
| 4725 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4726 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4727 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4728 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4729 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4730 | 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] | 4731 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4732 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4733 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4734 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4735 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4736 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4737 | return 0; |
| 4738 | } |
| 4739 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4740 | STATIC int |
| 4741 | xfs_bmapi_convert_unwritten( |
| 4742 | struct xfs_bmalloca *bma, |
| 4743 | struct xfs_bmbt_irec *mval, |
| 4744 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4745 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4746 | { |
| 4747 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4748 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4749 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4750 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4751 | int error; |
| 4752 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4753 | /* check if we need to do unwritten->real conversion */ |
| 4754 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4755 | (flags & XFS_BMAPI_PREALLOC)) |
| 4756 | return 0; |
| 4757 | |
| 4758 | /* check if we need to do real->unwritten conversion */ |
| 4759 | if (mval->br_state == XFS_EXT_NORM && |
| 4760 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4761 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4762 | return 0; |
| 4763 | |
| 4764 | /* |
| 4765 | * Modify (by adding) the state flag, if writing. |
| 4766 | */ |
| 4767 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4768 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4769 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4770 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4771 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4772 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4773 | } |
| 4774 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4775 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4776 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4777 | 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] | 4778 | &bma->cur, mval, bma->firstblock, bma->flist, |
| 4779 | &tmp_logflags); |
| 4780 | bma->logflags |= tmp_logflags; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4781 | if (error) |
| 4782 | return error; |
| 4783 | |
| 4784 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4785 | * Update our extent pointer, given that |
| 4786 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4787 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4788 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4789 | 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] | 4790 | |
| 4791 | /* |
| 4792 | * We may have combined previously unwritten space with written space, |
| 4793 | * so generate another request. |
| 4794 | */ |
| 4795 | if (mval->br_blockcount < len) |
| 4796 | return EAGAIN; |
| 4797 | return 0; |
| 4798 | } |
| 4799 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4800 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4801 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4802 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4803 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4804 | * locking problems. |
| 4805 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | * The returned value in "firstblock" from the first call in a transaction |
| 4807 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4808 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4809 | * the first call in "total"; if no allocation group has that many free |
| 4810 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4811 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4812 | int |
| 4813 | xfs_bmapi_write( |
| 4814 | struct xfs_trans *tp, /* transaction pointer */ |
| 4815 | struct xfs_inode *ip, /* incore inode */ |
| 4816 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4817 | xfs_filblks_t len, /* length to map in file */ |
| 4818 | int flags, /* XFS_BMAPI_... */ |
| 4819 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4820 | controls a.g. for allocs */ |
| 4821 | xfs_extlen_t total, /* total blocks needed */ |
| 4822 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4823 | int *nmap, /* i/o: mval size/count */ |
| 4824 | struct xfs_bmap_free *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4826 | struct xfs_mount *mp = ip->i_mount; |
| 4827 | struct xfs_ifork *ifp; |
| 4828 | struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4829 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4830 | int eof; /* after the end of extents */ |
| 4831 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4832 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4833 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4834 | int whichfork; /* data or attr fork */ |
| 4835 | char inhole; /* current location is hole in file */ |
| 4836 | char wasdelay; /* old extent was delayed */ |
| 4837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4839 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4840 | int orig_flags; /* original flags arg value */ |
| 4841 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4842 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4843 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | |
| 4845 | orig_bno = bno; |
| 4846 | orig_len = len; |
| 4847 | orig_flags = flags; |
| 4848 | orig_mval = mval; |
| 4849 | orig_nmap = *nmap; |
| 4850 | #endif |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4853 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4854 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4855 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4856 | ASSERT(len > 0); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4859 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | if (unlikely(XFS_TEST_ERROR( |
| 4862 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4863 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4864 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4865 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4866 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | return XFS_ERROR(EFSCORRUPTED); |
| 4868 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4871 | return XFS_ERROR(EIO); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4874 | ASSERT(ifp->if_ext_max == |
| 4875 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4876 | |
| 4877 | XFS_STATS_INC(xs_blk_mapw); |
| 4878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4880 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, total, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4881 | &bma.logflags, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4882 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | goto error0; |
| 4884 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4885 | |
| 4886 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4888 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4890 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4891 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4892 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4896 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4897 | if (error) |
| 4898 | goto error0; |
| 4899 | } |
| 4900 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4901 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4902 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | n = 0; |
| 4904 | end = bno + len; |
| 4905 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4906 | |
| 4907 | bma.tp = tp; |
| 4908 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4909 | bma.total = total; |
| 4910 | bma.userdata = 0; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4911 | bma.flist = flist; |
| 4912 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4915 | inhole = eof || bma.got.br_startoff > bno; |
| 4916 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | /* |
| 4919 | * First, deal with the hole before the allocated space |
| 4920 | * that we found, if any. |
| 4921 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4922 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4923 | bma.eof = eof; |
| 4924 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4925 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4926 | bma.offset = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4927 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4928 | /* |
| 4929 | * There's a 32/64 bit type mismatch between the |
| 4930 | * allocation length request (which can be 64 bits in |
| 4931 | * length) and the bma length request, which is |
| 4932 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4933 | * check for 32-bit overflows and handle them here. |
| 4934 | */ |
| 4935 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4936 | bma.length = MAXEXTLEN; |
| 4937 | else |
| 4938 | bma.length = len; |
| 4939 | |
| 4940 | ASSERT(len > 0); |
| 4941 | ASSERT(bma.length > 0); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4942 | error = xfs_bmapi_allocate(&bma, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | if (error) |
| 4944 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4945 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4946 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4948 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4949 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4950 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4951 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4953 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4954 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4955 | if (error == EAGAIN) |
| 4956 | continue; |
| 4957 | if (error) |
| 4958 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4960 | /* update the extent map to return */ |
| 4961 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4963 | /* |
| 4964 | * If we're done, stop now. Stop when we've allocated |
| 4965 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4966 | * the transaction may get too big. |
| 4967 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4968 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4970 | |
| 4971 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4972 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4973 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4974 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4975 | &bma.got); |
| 4976 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4977 | eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | /* |
| 4982 | * Transform from btree to extents, give it cur. |
| 4983 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4984 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4986 | int tmp_logflags = 0; |
| 4987 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4988 | ASSERT(bma.cur); |
| 4989 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4991 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | if (error) |
| 4993 | goto error0; |
| 4994 | } |
| 4995 | ASSERT(ifp->if_ext_max == |
| 4996 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4997 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 4998 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 4999 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | error0: |
| 5001 | /* |
| 5002 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5003 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5005 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5007 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 5008 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5010 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | /* |
| 5012 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5013 | * detecting a case where the data is changed, there's an error, |
| 5014 | * and it's not logged so we don't shutdown when we should. |
| 5015 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5016 | if (bma.logflags) |
| 5017 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 5018 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5019 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | if (!error) { |
| 5021 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5022 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5023 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5024 | bma.cur->bc_private.b.firstblock) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | (flist->xbf_low && |
| 5026 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5027 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5028 | bma.cur->bc_private.b.firstblock))); |
| 5029 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5031 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5033 | } |
| 5034 | if (!error) |
| 5035 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5036 | orig_nmap, *nmap); |
| 5037 | return error; |
| 5038 | } |
| 5039 | |
| 5040 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | * Unmap (remove) blocks from a file. |
| 5042 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5043 | * that value. If not all extents in the block range can be removed then |
| 5044 | * *done is set. |
| 5045 | */ |
| 5046 | int /* error */ |
| 5047 | xfs_bunmapi( |
| 5048 | xfs_trans_t *tp, /* transaction pointer */ |
| 5049 | struct xfs_inode *ip, /* incore inode */ |
| 5050 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5051 | xfs_filblks_t len, /* length to unmap in file */ |
| 5052 | int flags, /* misc flags */ |
| 5053 | xfs_extnum_t nexts, /* number of extents max */ |
| 5054 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5055 | controls a.g. for allocs */ |
| 5056 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 5057 | int *done) /* set if not done yet */ |
| 5058 | { |
| 5059 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5060 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5061 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5062 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | int error; /* error return value */ |
| 5064 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5065 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5067 | int isrt; /* freeing in rt area */ |
| 5068 | xfs_extnum_t lastx; /* last extent index used */ |
| 5069 | int logflags; /* transaction logging flags */ |
| 5070 | xfs_extlen_t mod; /* rt extent offset */ |
| 5071 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5072 | xfs_extnum_t nextents; /* number of file extents */ |
| 5073 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5075 | int tmp_logflags; /* partial logging flags */ |
| 5076 | int wasdel; /* was a delayed alloc extent */ |
| 5077 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | xfs_fsblock_t sum; |
| 5079 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5080 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5083 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5084 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5085 | if (unlikely( |
| 5086 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5087 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5088 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5089 | ip->i_mount); |
| 5090 | return XFS_ERROR(EFSCORRUPTED); |
| 5091 | } |
| 5092 | mp = ip->i_mount; |
| 5093 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5094 | return XFS_ERROR(EIO); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | ASSERT(len > 0); |
| 5097 | ASSERT(nexts >= 0); |
| 5098 | ASSERT(ifp->if_ext_max == |
| 5099 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5100 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5101 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5102 | return error; |
| 5103 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5104 | if (nextents == 0) { |
| 5105 | *done = 1; |
| 5106 | return 0; |
| 5107 | } |
| 5108 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5109 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | start = bno; |
| 5111 | bno = start + len - 1; |
| 5112 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5113 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | /* |
| 5116 | * Check to see if the given block number is past the end of the |
| 5117 | * file, back up to the last block if so... |
| 5118 | */ |
| 5119 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5120 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | xfs_bmbt_get_all(ep, &got); |
| 5122 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5123 | } |
| 5124 | logflags = 0; |
| 5125 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5126 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5127 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | cur->bc_private.b.firstblock = *firstblock; |
| 5129 | cur->bc_private.b.flist = flist; |
| 5130 | cur->bc_private.b.flags = 0; |
| 5131 | } else |
| 5132 | cur = NULL; |
| 5133 | extno = 0; |
| 5134 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5135 | (nexts == 0 || extno < nexts)) { |
| 5136 | /* |
| 5137 | * Is the found extent after a hole in which bno lives? |
| 5138 | * Just back up to the previous extent, if so. |
| 5139 | */ |
| 5140 | if (got.br_startoff > bno) { |
| 5141 | if (--lastx < 0) |
| 5142 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5143 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | xfs_bmbt_get_all(ep, &got); |
| 5145 | } |
| 5146 | /* |
| 5147 | * Is the last block of this extent before the range |
| 5148 | * we're supposed to delete? If so, we're done. |
| 5149 | */ |
| 5150 | bno = XFS_FILEOFF_MIN(bno, |
| 5151 | got.br_startoff + got.br_blockcount - 1); |
| 5152 | if (bno < start) |
| 5153 | break; |
| 5154 | /* |
| 5155 | * Then deal with the (possibly delayed) allocated space |
| 5156 | * we found. |
| 5157 | */ |
| 5158 | ASSERT(ep != NULL); |
| 5159 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5160 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | if (got.br_startoff < start) { |
| 5162 | del.br_startoff = start; |
| 5163 | del.br_blockcount -= start - got.br_startoff; |
| 5164 | if (!wasdel) |
| 5165 | del.br_startblock += start - got.br_startoff; |
| 5166 | } |
| 5167 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5168 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5169 | sum = del.br_startblock + del.br_blockcount; |
| 5170 | if (isrt && |
| 5171 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5172 | /* |
| 5173 | * Realtime extent not lined up at the end. |
| 5174 | * The extent could have been split into written |
| 5175 | * and unwritten pieces, or we could just be |
| 5176 | * unmapping part of it. But we can't really |
| 5177 | * get rid of part of a realtime extent. |
| 5178 | */ |
| 5179 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5180 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | /* |
| 5182 | * This piece is unwritten, or we're not |
| 5183 | * using unwritten extents. Skip over it. |
| 5184 | */ |
| 5185 | ASSERT(bno >= mod); |
| 5186 | bno -= mod > del.br_blockcount ? |
| 5187 | del.br_blockcount : mod; |
| 5188 | if (bno < got.br_startoff) { |
| 5189 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5190 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5191 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | } |
| 5193 | continue; |
| 5194 | } |
| 5195 | /* |
| 5196 | * It's written, turn it unwritten. |
| 5197 | * This is better than zeroing it. |
| 5198 | */ |
| 5199 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5200 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5201 | /* |
| 5202 | * If this spans a realtime extent boundary, |
| 5203 | * chop it back to the start of the one we end at. |
| 5204 | */ |
| 5205 | if (del.br_blockcount > mod) { |
| 5206 | del.br_startoff += del.br_blockcount - mod; |
| 5207 | del.br_startblock += del.br_blockcount - mod; |
| 5208 | del.br_blockcount = mod; |
| 5209 | } |
| 5210 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5211 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
| 5212 | &lastx, &cur, &del, firstblock, flist, |
| 5213 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | if (error) |
| 5215 | goto error0; |
| 5216 | goto nodelete; |
| 5217 | } |
| 5218 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5219 | /* |
| 5220 | * Realtime extent is lined up at the end but not |
| 5221 | * at the front. We'll get rid of full extents if |
| 5222 | * we can. |
| 5223 | */ |
| 5224 | mod = mp->m_sb.sb_rextsize - mod; |
| 5225 | if (del.br_blockcount > mod) { |
| 5226 | del.br_blockcount -= mod; |
| 5227 | del.br_startoff += mod; |
| 5228 | del.br_startblock += mod; |
| 5229 | } else if ((del.br_startoff == start && |
| 5230 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5231 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5232 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | /* |
| 5234 | * Can't make it unwritten. There isn't |
| 5235 | * a full extent here so just skip it. |
| 5236 | */ |
| 5237 | ASSERT(bno >= del.br_blockcount); |
| 5238 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5239 | if (got.br_startoff > bno) { |
| 5240 | if (--lastx >= 0) { |
| 5241 | ep = xfs_iext_get_ext(ifp, |
| 5242 | lastx); |
| 5243 | xfs_bmbt_get_all(ep, &got); |
| 5244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5245 | } |
| 5246 | continue; |
| 5247 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5248 | /* |
| 5249 | * This one is already unwritten. |
| 5250 | * It must have a written left neighbor. |
| 5251 | * Unwrite the killed part of that one and |
| 5252 | * try again. |
| 5253 | */ |
| 5254 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5255 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5256 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5258 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | ASSERT(del.br_startblock == |
| 5260 | prev.br_startblock + prev.br_blockcount); |
| 5261 | if (prev.br_startoff < start) { |
| 5262 | mod = start - prev.br_startoff; |
| 5263 | prev.br_blockcount -= mod; |
| 5264 | prev.br_startblock += mod; |
| 5265 | prev.br_startoff = start; |
| 5266 | } |
| 5267 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5268 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5269 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5270 | ip, &lastx, &cur, &prev, |
| 5271 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | if (error) |
| 5273 | goto error0; |
| 5274 | goto nodelete; |
| 5275 | } else { |
| 5276 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5277 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5278 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5279 | ip, &lastx, &cur, &del, |
| 5280 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | if (error) |
| 5282 | goto error0; |
| 5283 | goto nodelete; |
| 5284 | } |
| 5285 | } |
| 5286 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5287 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5288 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5289 | if (isrt) { |
| 5290 | xfs_filblks_t rtexts; |
| 5291 | |
| 5292 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5293 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5294 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5295 | (int64_t)rtexts, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5296 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5297 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5298 | XFS_QMOPT_RES_RTBLKS); |
| 5299 | } else { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 5300 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5301 | (int64_t)del.br_blockcount, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5302 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5303 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | ip->i_delayed_blks -= del.br_blockcount; |
| 5307 | if (cur) |
| 5308 | cur->bc_private.b.flags |= |
| 5309 | XFS_BTCUR_BPRV_WASDEL; |
| 5310 | } else if (cur) |
| 5311 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5312 | /* |
| 5313 | * If it's the case where the directory code is running |
| 5314 | * with no block reservation, and the deleted block is in |
| 5315 | * the middle of its extent, and the resulting insert |
| 5316 | * of an extent would cause transformation to btree format, |
| 5317 | * then reject it. The calling code will then swap |
| 5318 | * blocks around instead. |
| 5319 | * We have to do this now, rather than waiting for the |
| 5320 | * conversion to btree format, since the transaction |
| 5321 | * will be dirty. |
| 5322 | */ |
| 5323 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5324 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5325 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5326 | del.br_startoff > got.br_startoff && |
| 5327 | del.br_startoff + del.br_blockcount < |
| 5328 | got.br_startoff + got.br_blockcount) { |
| 5329 | error = XFS_ERROR(ENOSPC); |
| 5330 | goto error0; |
| 5331 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5332 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5333 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | logflags |= tmp_logflags; |
| 5335 | if (error) |
| 5336 | goto error0; |
| 5337 | bno = del.br_startoff - 1; |
| 5338 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | /* |
| 5340 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5343 | if (lastx >= 0) { |
| 5344 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5345 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5346 | if (--lastx >= 0) |
| 5347 | ep = xfs_iext_get_ext(ifp, |
| 5348 | lastx); |
| 5349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | extno++; |
| 5353 | } |
| 5354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5356 | ASSERT(ifp->if_ext_max == |
| 5357 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5358 | /* |
| 5359 | * Convert to a btree if necessary. |
| 5360 | */ |
| 5361 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5362 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5363 | ASSERT(cur == NULL); |
| 5364 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5365 | &cur, 0, &tmp_logflags, whichfork); |
| 5366 | logflags |= tmp_logflags; |
| 5367 | if (error) |
| 5368 | goto error0; |
| 5369 | } |
| 5370 | /* |
| 5371 | * transform from btree to extents, give it cur |
| 5372 | */ |
| 5373 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5374 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5375 | ASSERT(cur != NULL); |
| 5376 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5377 | whichfork); |
| 5378 | logflags |= tmp_logflags; |
| 5379 | if (error) |
| 5380 | goto error0; |
| 5381 | } |
| 5382 | /* |
| 5383 | * transform from extents to local? |
| 5384 | */ |
| 5385 | ASSERT(ifp->if_ext_max == |
| 5386 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5387 | error = 0; |
| 5388 | error0: |
| 5389 | /* |
| 5390 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5391 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5393 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5395 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5396 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5398 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | /* |
| 5400 | * Log inode even in the error case, if the transaction |
| 5401 | * is dirty we'll need to shut down the filesystem. |
| 5402 | */ |
| 5403 | if (logflags) |
| 5404 | xfs_trans_log_inode(tp, ip, logflags); |
| 5405 | if (cur) { |
| 5406 | if (!error) { |
| 5407 | *firstblock = cur->bc_private.b.firstblock; |
| 5408 | cur->bc_private.b.allocated = 0; |
| 5409 | } |
| 5410 | xfs_btree_del_cursor(cur, |
| 5411 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5412 | } |
| 5413 | return error; |
| 5414 | } |
| 5415 | |
| 5416 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5417 | * returns 1 for success, 0 if we failed to map the extent. |
| 5418 | */ |
| 5419 | STATIC int |
| 5420 | xfs_getbmapx_fix_eof_hole( |
| 5421 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5422 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5423 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5424 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5425 | __int64_t end, /* last block requested */ |
| 5426 | xfs_fsblock_t startblock) |
| 5427 | { |
| 5428 | __int64_t fixlen; |
| 5429 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5430 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5431 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5432 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5433 | |
| 5434 | if (startblock == HOLESTARTBLOCK) { |
| 5435 | mp = ip->i_mount; |
| 5436 | out->bmv_block = -1; |
| 5437 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5438 | fixlen -= out->bmv_offset; |
| 5439 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5440 | /* Came to hole at EOF. Trim it. */ |
| 5441 | if (fixlen <= 0) |
| 5442 | return 0; |
| 5443 | out->bmv_length = fixlen; |
| 5444 | } |
| 5445 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5446 | if (startblock == DELAYSTARTBLOCK) |
| 5447 | out->bmv_block = -2; |
| 5448 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5449 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5450 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5451 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5452 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5453 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5454 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5455 | } |
| 5456 | |
| 5457 | return 1; |
| 5458 | } |
| 5459 | |
| 5460 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5461 | * Get inode's extents as described in bmv, and format for output. |
| 5462 | * Calls formatter to fill the user's buffer until all extents |
| 5463 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5464 | * been filled, or until the formatter short-circuits the loop, |
| 5465 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | */ |
| 5467 | int /* error code */ |
| 5468 | xfs_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5469 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5470 | struct getbmapx *bmv, /* user bmap structure */ |
| 5471 | xfs_bmap_format_t formatter, /* format to user */ |
| 5472 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | { |
| 5474 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5475 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | __int64_t fixlen; /* length for -1 case */ |
| 5477 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | int lock; /* lock state */ |
| 5479 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5480 | xfs_mount_t *mp; /* file system mount point */ |
| 5481 | int nex; /* # of user extents can do */ |
| 5482 | int nexleft; /* # of user extents left */ |
| 5483 | int subnex; /* # of bmapi's can do */ |
| 5484 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5485 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | int whichfork; /* data or attr fork */ |
| 5487 | int prealloced; /* this is a file with |
| 5488 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5489 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5491 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5494 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5495 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | if (whichfork == XFS_ATTR_FORK) { |
| 5498 | if (XFS_IFORK_Q(ip)) { |
| 5499 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5500 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5501 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5502 | return XFS_ERROR(EINVAL); |
| 5503 | } else if (unlikely( |
| 5504 | ip->i_d.di_aformat != 0 && |
| 5505 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5506 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5507 | ip->i_mount); |
| 5508 | return XFS_ERROR(EFSCORRUPTED); |
| 5509 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5510 | |
| 5511 | prealloced = 0; |
| 5512 | fixlen = 1LL << 32; |
| 5513 | } else { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5514 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5515 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5516 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5517 | return XFS_ERROR(EINVAL); |
| 5518 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5519 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5520 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | prealloced = 1; |
| 5522 | fixlen = XFS_MAXIOFFSET(mp); |
| 5523 | } else { |
| 5524 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5525 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | if (bmv->bmv_length == -1) { |
| 5530 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5531 | bmv->bmv_length = |
| 5532 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5533 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | bmv->bmv_entries = 0; |
| 5535 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5536 | } else if (bmv->bmv_length < 0) { |
| 5537 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | nex = bmv->bmv_count - 1; |
| 5541 | if (nex <= 0) |
| 5542 | return XFS_ERROR(EINVAL); |
| 5543 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5544 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5545 | |
| 5546 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5547 | return XFS_ERROR(ENOMEM); |
| 5548 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 5549 | if (!out) |
| 5550 | return XFS_ERROR(ENOMEM); |
| 5551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5553 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 5554 | if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { |
| 5555 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5556 | if (error) |
| 5557 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5558 | } |
Dave Chinner | 309c848 | 2010-11-30 15:16:02 +1100 | [diff] [blame] | 5559 | /* |
| 5560 | * even after flushing the inode, there can still be delalloc |
| 5561 | * blocks on the inode beyond EOF due to speculative |
| 5562 | * preallocation. These are not removed until the release |
| 5563 | * function is called or the inode is inactivated. Hence we |
| 5564 | * cannot assert here that ip->i_delayed_blks == 0. |
| 5565 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | |
| 5568 | lock = xfs_ilock_map_shared(ip); |
| 5569 | |
| 5570 | /* |
| 5571 | * Don't let nex be bigger than the number of extents |
| 5572 | * we can have assuming alternating holes and real extents. |
| 5573 | */ |
| 5574 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5575 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5576 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5577 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5578 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5579 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | |
| 5581 | /* |
| 5582 | * Allocate enough space to handle "subnex" maps at a time. |
| 5583 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5584 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5586 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5587 | if (!map) |
| 5588 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | |
| 5590 | bmv->bmv_entries = 0; |
| 5591 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5592 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5593 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5594 | error = 0; |
| 5595 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | } |
| 5597 | |
| 5598 | nexleft = nex; |
| 5599 | |
| 5600 | do { |
| 5601 | nmap = (nexleft > subnex) ? subnex : nexleft; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 5602 | error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5603 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
| 5604 | map, &nmap, bmapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5606 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | ASSERT(nmap <= subnex); |
| 5608 | |
| 5609 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5610 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5611 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5612 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5613 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5614 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5615 | out[cur_ext].bmv_offset = |
| 5616 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5617 | out[cur_ext].bmv_length = |
| 5618 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5619 | out[cur_ext].bmv_unused1 = 0; |
| 5620 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5621 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5622 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5623 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5624 | whichfork == XFS_ATTR_FORK) { |
| 5625 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5626 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5627 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5629 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5630 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5631 | prealloced, bmvend, |
| 5632 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5633 | goto out_free_map; |
| 5634 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5635 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5636 | out[cur_ext].bmv_offset + |
| 5637 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5638 | bmv->bmv_length = |
| 5639 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 5640 | |
| 5641 | /* |
| 5642 | * In case we don't want to return the hole, |
| 5643 | * don't increase cur_ext so that we can reuse |
| 5644 | * it in the next loop. |
| 5645 | */ |
| 5646 | if ((iflags & BMV_IF_NO_HOLES) && |
| 5647 | map[i].br_startblock == HOLESTARTBLOCK) { |
| 5648 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); |
| 5649 | continue; |
| 5650 | } |
| 5651 | |
| 5652 | nexleft--; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5653 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5654 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | } |
| 5656 | } while (nmap && nexleft && bmv->bmv_length); |
| 5657 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5658 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5659 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5660 | out_unlock_ilock: |
| 5661 | xfs_iunlock_map_shared(ip, lock); |
| 5662 | out_unlock_iolock: |
| 5663 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5664 | |
| 5665 | for (i = 0; i < cur_ext; i++) { |
| 5666 | int full = 0; /* user array is full */ |
| 5667 | |
| 5668 | /* format results & advance arg */ |
| 5669 | error = formatter(&arg, &out[i], &full); |
| 5670 | if (error || full) |
| 5671 | break; |
| 5672 | } |
| 5673 | |
Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5674 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | return error; |
| 5676 | } |
| 5677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | #ifdef DEBUG |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5679 | STATIC struct xfs_buf * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | xfs_bmap_get_bp( |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5681 | struct xfs_btree_cur *cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | xfs_fsblock_t bno) |
| 5683 | { |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5684 | struct xfs_log_item_desc *lidp; |
| 5685 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | |
| 5687 | if (!cur) |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5688 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5690 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5691 | if (!cur->bc_bufs[i]) |
| 5692 | break; |
| 5693 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 5694 | return cur->bc_bufs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5697 | /* Chase down all the log items to see if the bp is there */ |
| 5698 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5699 | struct xfs_buf_log_item *bip; |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5700 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 5701 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 5702 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 5703 | return bip->bli_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | } |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5705 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5706 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | } |
| 5708 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5709 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5711 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | xfs_mount_t *mp, |
| 5713 | int root, |
| 5714 | short sz) |
| 5715 | { |
| 5716 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5717 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | xfs_bmbt_key_t *prevp, *keyp; |
| 5719 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5720 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | |
| 5722 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5723 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5725 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | |
| 5727 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5728 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5729 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | } |
| 5731 | prevp = keyp; |
| 5732 | |
| 5733 | /* |
| 5734 | * Compare the block numbers to see if there are dups. |
| 5735 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5736 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5737 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5738 | else |
| 5739 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5740 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5741 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5742 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5743 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5744 | else |
| 5745 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5746 | if (*thispa == *pp) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5747 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5748 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5749 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5751 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | } |
| 5753 | } |
| 5754 | } |
| 5755 | } |
| 5756 | |
| 5757 | /* |
| 5758 | * Check that the extents for the inode ip are in the right order in all |
| 5759 | * btree leaves. |
| 5760 | */ |
| 5761 | |
| 5762 | STATIC void |
| 5763 | xfs_bmap_check_leaf_extents( |
| 5764 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5765 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5766 | int whichfork) /* data or attr fork */ |
| 5767 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5768 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5770 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5771 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5772 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | xfs_ifork_t *ifp; /* fork structure */ |
| 5774 | int level; /* btree level, for checking */ |
| 5775 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5776 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5777 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5778 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5779 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | int bp_release = 0; |
| 5781 | |
| 5782 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5783 | return; |
| 5784 | } |
| 5785 | |
| 5786 | bno = NULLFSBLOCK; |
| 5787 | mp = ip->i_mount; |
| 5788 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5789 | block = ifp->if_broot; |
| 5790 | /* |
| 5791 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5792 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5793 | level = be16_to_cpu(block->bb_level); |
| 5794 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5796 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5797 | bno = be64_to_cpu(*pp); |
| 5798 | |
| 5799 | ASSERT(bno != NULLDFSBNO); |
| 5800 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5801 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | /* |
| 5804 | * Go down the tree until leaf level is reached, following the first |
| 5805 | * pointer (leftmost) at each level. |
| 5806 | */ |
| 5807 | while (level-- > 0) { |
| 5808 | /* See if buf is in cur first */ |
| 5809 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5810 | if (bp) { |
| 5811 | bp_release = 0; |
| 5812 | } else { |
| 5813 | bp_release = 1; |
| 5814 | } |
| 5815 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5816 | XFS_BMAP_BTREE_REF))) |
| 5817 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5818 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5820 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | error0); |
| 5822 | if (level == 0) |
| 5823 | break; |
| 5824 | |
| 5825 | /* |
| 5826 | * Check this block for basic sanity (increasing keys and |
| 5827 | * no duplicate blocks). |
| 5828 | */ |
| 5829 | |
| 5830 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5831 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5832 | bno = be64_to_cpu(*pp); |
| 5833 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | if (bp_release) { |
| 5835 | bp_release = 0; |
| 5836 | xfs_trans_brelse(NULL, bp); |
| 5837 | } |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 5842 | */ |
| 5843 | i = 0; |
| 5844 | |
| 5845 | /* |
| 5846 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 5847 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | xfs_fsblock_t nextbno; |
| 5850 | xfs_extnum_t num_recs; |
| 5851 | |
| 5852 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5853 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | |
| 5855 | /* |
| 5856 | * Read-ahead the next leaf block, if any. |
| 5857 | */ |
| 5858 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5859 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | |
| 5861 | /* |
| 5862 | * Check all the extents to make sure they are OK. |
| 5863 | * If we had a previous block, the last entry should |
| 5864 | * conform with the first entry in this one. |
| 5865 | */ |
| 5866 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5867 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5868 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5869 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 5870 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 5871 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5872 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5873 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5874 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5875 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 5876 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 5877 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5878 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5881 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | i += num_recs; |
| 5883 | if (bp_release) { |
| 5884 | bp_release = 0; |
| 5885 | xfs_trans_brelse(NULL, bp); |
| 5886 | } |
| 5887 | bno = nextbno; |
| 5888 | /* |
| 5889 | * If we've reached the end, stop. |
| 5890 | */ |
| 5891 | if (bno == NULLFSBLOCK) |
| 5892 | break; |
| 5893 | |
| 5894 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5895 | if (bp) { |
| 5896 | bp_release = 0; |
| 5897 | } else { |
| 5898 | bp_release = 1; |
| 5899 | } |
| 5900 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5901 | XFS_BMAP_BTREE_REF))) |
| 5902 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5903 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | } |
| 5905 | if (bp_release) { |
| 5906 | bp_release = 0; |
| 5907 | xfs_trans_brelse(NULL, bp); |
| 5908 | } |
| 5909 | return; |
| 5910 | |
| 5911 | error0: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5912 | xfs_warn(mp, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | if (bp_release) |
| 5914 | xfs_trans_brelse(NULL, bp); |
| 5915 | error_norelse: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5916 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5917 | __func__, i); |
| 5918 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | return; |
| 5920 | } |
| 5921 | #endif |
| 5922 | |
| 5923 | /* |
| 5924 | * Count fsblocks of the given fork. |
| 5925 | */ |
| 5926 | int /* error */ |
| 5927 | xfs_bmap_count_blocks( |
| 5928 | xfs_trans_t *tp, /* transaction pointer */ |
| 5929 | xfs_inode_t *ip, /* incore inode */ |
| 5930 | int whichfork, /* data or attr fork */ |
| 5931 | int *count) /* out: count of blocks */ |
| 5932 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5933 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5935 | xfs_ifork_t *ifp; /* fork structure */ |
| 5936 | int level; /* btree level, for checking */ |
| 5937 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5938 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | |
| 5940 | bno = NULLFSBLOCK; |
| 5941 | mp = ip->i_mount; |
| 5942 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5943 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5944 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5946 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | return 0; |
| 5948 | } |
| 5949 | |
| 5950 | /* |
| 5951 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5952 | */ |
| 5953 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5954 | level = be16_to_cpu(block->bb_level); |
| 5955 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5956 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5957 | bno = be64_to_cpu(*pp); |
| 5958 | ASSERT(bno != NULLDFSBNO); |
| 5959 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5960 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5962 | if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 5964 | mp); |
| 5965 | return XFS_ERROR(EFSCORRUPTED); |
| 5966 | } |
| 5967 | |
| 5968 | return 0; |
| 5969 | } |
| 5970 | |
| 5971 | /* |
| 5972 | * Recursively walks each level of a btree |
| 5973 | * to count total fsblocks is use. |
| 5974 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 5975 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | xfs_bmap_count_tree( |
| 5977 | xfs_mount_t *mp, /* file system mount point */ |
| 5978 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5979 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | xfs_fsblock_t blockno, /* file system block number */ |
| 5981 | int levelin, /* level in btree */ |
| 5982 | int *count) /* Count of blocks */ |
| 5983 | { |
| 5984 | int error; |
| 5985 | xfs_buf_t *bp, *nbp; |
| 5986 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5987 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | xfs_fsblock_t bno = blockno; |
| 5989 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5990 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | |
| 5993 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 5994 | return error; |
| 5995 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5996 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | |
| 5998 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 5999 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6000 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | while (nextbno != NULLFSBLOCK) { |
| 6002 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6003 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6004 | return error; |
| 6005 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6006 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6007 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | xfs_trans_brelse(tp, nbp); |
| 6009 | } |
| 6010 | |
| 6011 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6012 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6013 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6015 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | xfs_trans_brelse(tp, bp); |
| 6017 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6018 | XFS_ERRLEVEL_LOW, mp); |
| 6019 | return XFS_ERROR(EFSCORRUPTED); |
| 6020 | } |
| 6021 | xfs_trans_brelse(tp, bp); |
| 6022 | } else { |
| 6023 | /* count all level 1 nodes and their leaves */ |
| 6024 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6025 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6026 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6027 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | xfs_trans_brelse(tp, bp); |
| 6029 | if (nextbno == NULLFSBLOCK) |
| 6030 | break; |
| 6031 | bno = nextbno; |
| 6032 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6033 | XFS_BMAP_BTREE_REF))) |
| 6034 | return error; |
| 6035 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6036 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6037 | } |
| 6038 | } |
| 6039 | return 0; |
| 6040 | } |
| 6041 | |
| 6042 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6043 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6045 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6047 | xfs_ifork_t *ifp, |
| 6048 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | int numrecs, |
| 6050 | int *count) |
| 6051 | { |
| 6052 | int b; |
| 6053 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6054 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6055 | xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b); |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6056 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6057 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6061 | * Count leaf blocks given a range of extent records originally |
| 6062 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6063 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6064 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6065 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6066 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6067 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6068 | int numrecs, |
| 6069 | int *count) |
| 6070 | { |
| 6071 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6072 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6073 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6074 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6075 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6077 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | } |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6079 | |
| 6080 | /* |
| 6081 | * dead simple method of punching delalyed allocation blocks from a range in |
| 6082 | * the inode. Walks a block at a time so will be slow, but is only executed in |
| 6083 | * rare error cases so the overhead is not critical. This will alays punch out |
| 6084 | * both the start and end blocks, even if the ranges only partially overlap |
| 6085 | * them, so it is up to the caller to ensure that partial blocks are not |
| 6086 | * passed in. |
| 6087 | */ |
| 6088 | int |
| 6089 | xfs_bmap_punch_delalloc_range( |
| 6090 | struct xfs_inode *ip, |
| 6091 | xfs_fileoff_t start_fsb, |
| 6092 | xfs_fileoff_t length) |
| 6093 | { |
| 6094 | xfs_fileoff_t remaining = length; |
| 6095 | int error = 0; |
| 6096 | |
| 6097 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 6098 | |
| 6099 | do { |
| 6100 | int done; |
| 6101 | xfs_bmbt_irec_t imap; |
| 6102 | int nimaps = 1; |
| 6103 | xfs_fsblock_t firstblock; |
| 6104 | xfs_bmap_free_t flist; |
| 6105 | |
| 6106 | /* |
| 6107 | * Map the range first and check that it is a delalloc extent |
| 6108 | * before trying to unmap the range. Otherwise we will be |
| 6109 | * trying to remove a real extent (which requires a |
| 6110 | * transaction) or a hole, which is probably a bad idea... |
| 6111 | */ |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 6112 | error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, |
| 6113 | XFS_BMAPI_ENTIRE); |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6114 | |
| 6115 | if (error) { |
| 6116 | /* something screwed, just bail */ |
| 6117 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 6118 | xfs_alert(ip->i_mount, |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6119 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
| 6120 | ip->i_ino, start_fsb); |
| 6121 | } |
| 6122 | break; |
| 6123 | } |
| 6124 | if (!nimaps) { |
| 6125 | /* nothing there */ |
| 6126 | goto next_block; |
| 6127 | } |
| 6128 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 6129 | /* been converted, ignore */ |
| 6130 | goto next_block; |
| 6131 | } |
| 6132 | WARN_ON(imap.br_blockcount == 0); |
| 6133 | |
| 6134 | /* |
| 6135 | * Note: while we initialise the firstblock/flist pair, they |
| 6136 | * should never be used because blocks should never be |
| 6137 | * allocated or freed for a delalloc extent and hence we need |
| 6138 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 6139 | */ |
| 6140 | xfs_bmap_init(&flist, &firstblock); |
| 6141 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, |
| 6142 | &flist, &done); |
| 6143 | if (error) |
| 6144 | break; |
| 6145 | |
| 6146 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 6147 | next_block: |
| 6148 | start_fsb++; |
| 6149 | remaining--; |
| 6150 | } while(remaining > 0); |
| 6151 | |
| 6152 | return error; |
| 6153 | } |