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 | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 378 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 380 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 382 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 384 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 385 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 388 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 389 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 392 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 394 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 395 | /* left is 0, right is 1, prev is 2 */ |
| 396 | int rval=0; /* return value (logging flags) */ |
| 397 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 398 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 399 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 400 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 401 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 404 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 405 | cur = *curp; |
| 406 | |
| 407 | ASSERT(*idx >= 0); |
| 408 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 409 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 410 | ASSERT(!cur || (cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
| 411 | |
| 412 | XFS_STATS_INC(xs_add_exlist); |
| 413 | |
| 414 | *logflagsp = 0; |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | #define LEFT r[0] |
| 417 | #define RIGHT r[1] |
| 418 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | /* |
| 421 | * Set up a bunch of variables to make the tests simpler. |
| 422 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 423 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | xfs_bmbt_get_all(ep, &PREV); |
| 425 | new_endoff = new->br_startoff + new->br_blockcount; |
| 426 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 427 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 428 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 429 | da_old = startblockval(PREV.br_startblock); |
| 430 | da_new = 0; |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* |
| 433 | * Set flags determining what part of the previous delayed allocation |
| 434 | * extent is being replaced by a real allocation. |
| 435 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 436 | if (PREV.br_startoff == new->br_startoff) |
| 437 | state |= BMAP_LEFT_FILLING; |
| 438 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 439 | state |= BMAP_RIGHT_FILLING; |
| 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | /* |
| 442 | * Check and set flags if this segment has a left neighbor. |
| 443 | * Don't set contiguous if the combined extent would be too large. |
| 444 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 445 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 446 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 447 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 448 | |
| 449 | if (isnullstartblock(LEFT.br_startblock)) |
| 450 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 452 | |
| 453 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 454 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 455 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 456 | LEFT.br_state == new->br_state && |
| 457 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 458 | state |= BMAP_LEFT_CONTIG; |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | /* |
| 461 | * Check and set flags if this segment has a right neighbor. |
| 462 | * Don't set contiguous if the combined extent would be too large. |
| 463 | * Also check for all-three-contiguous being too large. |
| 464 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 465 | 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] | 466 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 467 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 468 | |
| 469 | if (isnullstartblock(RIGHT.br_startblock)) |
| 470 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 472 | |
| 473 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 474 | new_endoff == RIGHT.br_startoff && |
| 475 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 476 | new->br_state == RIGHT.br_state && |
| 477 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 478 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 479 | BMAP_RIGHT_FILLING)) != |
| 480 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 481 | BMAP_RIGHT_FILLING) || |
| 482 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 483 | <= MAXEXTLEN)) |
| 484 | state |= BMAP_RIGHT_CONTIG; |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | error = 0; |
| 487 | /* |
| 488 | * Switch out based on the FILLING and CONTIG state bits. |
| 489 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 490 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 491 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 492 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 493 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | /* |
| 495 | * Filling in all of a previously delayed allocation extent. |
| 496 | * The left and right neighbors are both contiguous with new. |
| 497 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 498 | --*idx; |
| 499 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 500 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | LEFT.br_blockcount + PREV.br_blockcount + |
| 502 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 503 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 504 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 505 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | ip->i_d.di_nextents--; |
| 507 | if (cur == NULL) |
| 508 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 509 | else { |
| 510 | rval = XFS_ILOG_CORE; |
| 511 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 512 | RIGHT.br_startblock, |
| 513 | RIGHT.br_blockcount, &i))) |
| 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 | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 516 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 518 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 519 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 521 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 523 | LEFT.br_startblock, |
| 524 | LEFT.br_blockcount + |
| 525 | PREV.br_blockcount + |
| 526 | RIGHT.br_blockcount, LEFT.br_state))) |
| 527 | goto done; |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | break; |
| 530 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 531 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | /* |
| 533 | * Filling in all of a previously delayed allocation extent. |
| 534 | * The left neighbor is contiguous, the right is not. |
| 535 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 536 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 537 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 538 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 539 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 540 | LEFT.br_blockcount + PREV.br_blockcount); |
| 541 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 542 | |
| 543 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | if (cur == NULL) |
| 545 | rval = XFS_ILOG_DEXT; |
| 546 | else { |
| 547 | rval = 0; |
| 548 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 549 | LEFT.br_startblock, LEFT.br_blockcount, |
| 550 | &i))) |
| 551 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 552 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 554 | LEFT.br_startblock, |
| 555 | LEFT.br_blockcount + |
| 556 | PREV.br_blockcount, LEFT.br_state))) |
| 557 | goto done; |
| 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | break; |
| 560 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 561 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* |
| 563 | * Filling in all of a previously delayed allocation extent. |
| 564 | * The right neighbor is contiguous, the left is not. |
| 565 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 566 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 568 | xfs_bmbt_set_blockcount(ep, |
| 569 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 570 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 571 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 572 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (cur == NULL) |
| 574 | rval = XFS_ILOG_DEXT; |
| 575 | else { |
| 576 | rval = 0; |
| 577 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 578 | RIGHT.br_startblock, |
| 579 | RIGHT.br_blockcount, &i))) |
| 580 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 581 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 583 | new->br_startblock, |
| 584 | PREV.br_blockcount + |
| 585 | RIGHT.br_blockcount, PREV.br_state))) |
| 586 | goto done; |
| 587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | break; |
| 589 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 590 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | /* |
| 592 | * Filling in all of a previously delayed allocation extent. |
| 593 | * Neither the left nor right neighbors are contiguous with |
| 594 | * the new one. |
| 595 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 596 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 598 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | ip->i_d.di_nextents++; |
| 601 | if (cur == NULL) |
| 602 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 603 | else { |
| 604 | rval = XFS_ILOG_CORE; |
| 605 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 606 | new->br_startblock, new->br_blockcount, |
| 607 | &i))) |
| 608 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 609 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 611 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 613 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | break; |
| 616 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 617 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | /* |
| 619 | * Filling in the first part of a previous delayed allocation. |
| 620 | * The left neighbor is contiguous. |
| 621 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 622 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 623 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | LEFT.br_blockcount + new->br_blockcount); |
| 625 | xfs_bmbt_set_startoff(ep, |
| 626 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 627 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 630 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | if (cur == NULL) |
| 633 | rval = XFS_ILOG_DEXT; |
| 634 | else { |
| 635 | rval = 0; |
| 636 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 637 | LEFT.br_startblock, LEFT.br_blockcount, |
| 638 | &i))) |
| 639 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 640 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 642 | LEFT.br_startblock, |
| 643 | LEFT.br_blockcount + |
| 644 | new->br_blockcount, |
| 645 | LEFT.br_state))) |
| 646 | goto done; |
| 647 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 648 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 649 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 650 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 651 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 652 | |
| 653 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | break; |
| 655 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 656 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | /* |
| 658 | * Filling in the first part of a previous delayed allocation. |
| 659 | * The left neighbor is not contiguous. |
| 660 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 661 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 663 | temp = PREV.br_blockcount - new->br_blockcount; |
| 664 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 665 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | ip->i_d.di_nextents++; |
| 667 | if (cur == NULL) |
| 668 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 669 | else { |
| 670 | rval = XFS_ILOG_CORE; |
| 671 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 672 | new->br_startblock, new->br_blockcount, |
| 673 | &i))) |
| 674 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 675 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 677 | if ((error = xfs_btree_insert(cur, &i))) |
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 == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 682 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 683 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | first, flist, &cur, 1, &tmp_rval, |
| 685 | XFS_DATA_FORK); |
| 686 | rval |= tmp_rval; |
| 687 | if (error) |
| 688 | goto done; |
| 689 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 690 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 691 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 693 | ep = xfs_iext_get_ext(ifp, *idx + 1); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 694 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 695 | trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | break; |
| 697 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 698 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | /* |
| 700 | * Filling in the last part of a previous delayed allocation. |
| 701 | * The right neighbor is contiguous with the new allocation. |
| 702 | */ |
| 703 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 704 | trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 706 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 707 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | new->br_blockcount + RIGHT.br_blockcount, |
| 709 | RIGHT.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 710 | trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (cur == NULL) |
| 712 | rval = XFS_ILOG_DEXT; |
| 713 | else { |
| 714 | rval = 0; |
| 715 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 716 | RIGHT.br_startblock, |
| 717 | RIGHT.br_blockcount, &i))) |
| 718 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 719 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 721 | new->br_startblock, |
| 722 | new->br_blockcount + |
| 723 | RIGHT.br_blockcount, |
| 724 | RIGHT.br_state))) |
| 725 | goto done; |
| 726 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 727 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 728 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 729 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 730 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 731 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 732 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 733 | |
| 734 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | break; |
| 736 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 737 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | /* |
| 739 | * Filling in the last part of a previous delayed allocation. |
| 740 | * The right neighbor is not contiguous. |
| 741 | */ |
| 742 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 743 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 745 | xfs_iext_insert(ip, *idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | ip->i_d.di_nextents++; |
| 747 | if (cur == NULL) |
| 748 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 749 | else { |
| 750 | rval = XFS_ILOG_CORE; |
| 751 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 752 | new->br_startblock, new->br_blockcount, |
| 753 | &i))) |
| 754 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 755 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 757 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 759 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 762 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 763 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | first, flist, &cur, 1, &tmp_rval, |
| 765 | XFS_DATA_FORK); |
| 766 | rval |= tmp_rval; |
| 767 | if (error) |
| 768 | goto done; |
| 769 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 770 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 771 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 773 | ep = xfs_iext_get_ext(ifp, *idx); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 774 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 775 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 776 | |
| 777 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | break; |
| 779 | |
| 780 | case 0: |
| 781 | /* |
| 782 | * Filling in the middle part of a previous delayed allocation. |
| 783 | * Contiguity is impossible here. |
| 784 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 785 | * |
| 786 | * We start with a delayed allocation: |
| 787 | * |
| 788 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 789 | * PREV @ idx |
| 790 | * |
| 791 | * and we are allocating: |
| 792 | * +rrrrrrrrrrrrrrrrr+ |
| 793 | * new |
| 794 | * |
| 795 | * and we set it up for insertion as: |
| 796 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 797 | * new |
| 798 | * PREV @ idx LEFT RIGHT |
| 799 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | */ |
| 801 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 803 | trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 804 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 805 | LEFT = *new; |
| 806 | RIGHT.br_state = PREV.br_state; |
| 807 | RIGHT.br_startblock = nullstartblock( |
| 808 | (int)xfs_bmap_worst_indlen(ip, temp2)); |
| 809 | RIGHT.br_startoff = new_endoff; |
| 810 | RIGHT.br_blockcount = temp2; |
| 811 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 812 | xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | ip->i_d.di_nextents++; |
| 814 | if (cur == NULL) |
| 815 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 816 | else { |
| 817 | rval = XFS_ILOG_CORE; |
| 818 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 819 | new->br_startblock, new->br_blockcount, |
| 820 | &i))) |
| 821 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 822 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 824 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 826 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 829 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 830 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | first, flist, &cur, 1, &tmp_rval, |
| 832 | XFS_DATA_FORK); |
| 833 | rval |= tmp_rval; |
| 834 | if (error) |
| 835 | goto done; |
| 836 | } |
| 837 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 838 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 839 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 841 | if (diff > 0) { |
| 842 | error = xfs_icsb_modify_counters(ip->i_mount, |
| 843 | XFS_SBS_FDBLOCKS, |
| 844 | -((int64_t)diff), 0); |
| 845 | ASSERT(!error); |
| 846 | if (error) |
| 847 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 849 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 850 | ep = xfs_iext_get_ext(ifp, *idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 851 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 852 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 853 | trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_); |
| 854 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 855 | nullstartblock((int)temp2)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 856 | trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_); |
| 857 | |
| 858 | ++*idx; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 859 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | break; |
| 861 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 862 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 863 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 864 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 865 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 866 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 867 | case BMAP_LEFT_CONTIG: |
| 868 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | /* |
| 870 | * These cases are all impossible. |
| 871 | */ |
| 872 | ASSERT(0); |
| 873 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 874 | |
| 875 | /* convert to a btree if necessary */ |
| 876 | if (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS && |
| 877 | XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > ifp->if_ext_max) { |
| 878 | int tmp_logflags; /* partial log flag return val */ |
| 879 | |
| 880 | ASSERT(cur == NULL); |
| 881 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, |
| 882 | da_old > 0, &tmp_logflags, XFS_DATA_FORK); |
| 883 | *logflagsp |= tmp_logflags; |
| 884 | if (error) |
| 885 | goto done; |
| 886 | } |
| 887 | |
| 888 | /* adjust for changes in reserved delayed indirect blocks */ |
| 889 | if (da_old || da_new) { |
| 890 | temp = da_new; |
| 891 | if (cur) |
| 892 | temp += cur->bc_private.b.allocated; |
| 893 | ASSERT(temp <= da_old); |
| 894 | if (temp < da_old) |
| 895 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
| 896 | (int64_t)(da_old - temp), 0); |
| 897 | } |
| 898 | |
| 899 | /* clear out the allocated field, done with it now in any case. */ |
| 900 | if (cur) { |
| 901 | cur->bc_private.b.allocated = 0; |
| 902 | *curp = cur; |
| 903 | } |
| 904 | xfs_bmap_check_leaf_extents(cur, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 906 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | return error; |
| 908 | #undef LEFT |
| 909 | #undef RIGHT |
| 910 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 914 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | */ |
| 916 | STATIC int /* error */ |
| 917 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 918 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 920 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 922 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 923 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 924 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 925 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 928 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 931 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 933 | xfs_exntst_t newext; /* new extent state */ |
| 934 | xfs_exntst_t oldext; /* old extent state */ |
| 935 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 936 | /* left is 0, right is 1, prev is 2 */ |
| 937 | int rval=0; /* return value (logging flags) */ |
| 938 | int state = 0;/* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 940 | *logflagsp = 0; |
| 941 | |
| 942 | cur = *curp; |
| 943 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 944 | |
| 945 | ASSERT(*idx >= 0); |
| 946 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 947 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 948 | |
| 949 | XFS_STATS_INC(xs_add_exlist); |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | #define LEFT r[0] |
| 952 | #define RIGHT r[1] |
| 953 | #define PREV r[2] |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | /* |
| 956 | * Set up a bunch of variables to make the tests simpler. |
| 957 | */ |
| 958 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 959 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | xfs_bmbt_get_all(ep, &PREV); |
| 961 | newext = new->br_state; |
| 962 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 963 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 964 | ASSERT(PREV.br_state == oldext); |
| 965 | new_endoff = new->br_startoff + new->br_blockcount; |
| 966 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 967 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | /* |
| 970 | * Set flags determining what part of the previous oldext allocation |
| 971 | * extent is being replaced by a newext allocation. |
| 972 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 973 | if (PREV.br_startoff == new->br_startoff) |
| 974 | state |= BMAP_LEFT_FILLING; |
| 975 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 976 | state |= BMAP_RIGHT_FILLING; |
| 977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | /* |
| 979 | * Check and set flags if this segment has a left neighbor. |
| 980 | * Don't set contiguous if the combined extent would be too large. |
| 981 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 982 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 983 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 984 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 985 | |
| 986 | if (isnullstartblock(LEFT.br_startblock)) |
| 987 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 989 | |
| 990 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 991 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 992 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 993 | LEFT.br_state == newext && |
| 994 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 995 | state |= BMAP_LEFT_CONTIG; |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | /* |
| 998 | * Check and set flags if this segment has a right neighbor. |
| 999 | * Don't set contiguous if the combined extent would be too large. |
| 1000 | * Also check for all-three-contiguous being too large. |
| 1001 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1002 | 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] | 1003 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1004 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1005 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1006 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1008 | |
| 1009 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1010 | new_endoff == RIGHT.br_startoff && |
| 1011 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1012 | newext == RIGHT.br_state && |
| 1013 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1014 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1015 | BMAP_RIGHT_FILLING)) != |
| 1016 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1017 | BMAP_RIGHT_FILLING) || |
| 1018 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1019 | <= MAXEXTLEN)) |
| 1020 | state |= BMAP_RIGHT_CONTIG; |
| 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | /* |
| 1023 | * Switch out based on the FILLING and CONTIG state bits. |
| 1024 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1025 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1026 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1027 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1028 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | /* |
| 1030 | * Setting all of a previous oldext extent to newext. |
| 1031 | * The left and right neighbors are both contiguous with new. |
| 1032 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1033 | --*idx; |
| 1034 | |
| 1035 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1036 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1038 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1039 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1040 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1041 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | ip->i_d.di_nextents -= 2; |
| 1043 | if (cur == NULL) |
| 1044 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1045 | else { |
| 1046 | rval = XFS_ILOG_CORE; |
| 1047 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1048 | RIGHT.br_startblock, |
| 1049 | RIGHT.br_blockcount, &i))) |
| 1050 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1051 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1052 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1054 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1055 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1057 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1058 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1060 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1061 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1063 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1065 | LEFT.br_startblock, |
| 1066 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1067 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1068 | goto done; |
| 1069 | } |
| 1070 | break; |
| 1071 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1072 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | /* |
| 1074 | * Setting all of a previous oldext extent to newext. |
| 1075 | * The left neighbor is contiguous, the right is not. |
| 1076 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1077 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1078 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1079 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1080 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 1081 | LEFT.br_blockcount + PREV.br_blockcount); |
| 1082 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1083 | |
| 1084 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | ip->i_d.di_nextents--; |
| 1086 | if (cur == NULL) |
| 1087 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1088 | else { |
| 1089 | rval = XFS_ILOG_CORE; |
| 1090 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1091 | PREV.br_startblock, PREV.br_blockcount, |
| 1092 | &i))) |
| 1093 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1094 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1095 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1097 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1098 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1100 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1102 | LEFT.br_startblock, |
| 1103 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1104 | LEFT.br_state))) |
| 1105 | goto done; |
| 1106 | } |
| 1107 | break; |
| 1108 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1109 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | /* |
| 1111 | * Setting all of a previous oldext extent to newext. |
| 1112 | * The right neighbor is contiguous, the left is not. |
| 1113 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1114 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | xfs_bmbt_set_blockcount(ep, |
| 1116 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1117 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1118 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1119 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | ip->i_d.di_nextents--; |
| 1121 | if (cur == NULL) |
| 1122 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1123 | else { |
| 1124 | rval = XFS_ILOG_CORE; |
| 1125 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1126 | RIGHT.br_startblock, |
| 1127 | RIGHT.br_blockcount, &i))) |
| 1128 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1129 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1130 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1132 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1133 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1135 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1137 | new->br_startblock, |
| 1138 | new->br_blockcount + RIGHT.br_blockcount, |
| 1139 | newext))) |
| 1140 | goto done; |
| 1141 | } |
| 1142 | break; |
| 1143 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1144 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | /* |
| 1146 | * Setting all of a previous oldext extent to newext. |
| 1147 | * Neither the left nor right neighbors are contiguous with |
| 1148 | * the new one. |
| 1149 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1150 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1152 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | if (cur == NULL) |
| 1155 | rval = XFS_ILOG_DEXT; |
| 1156 | else { |
| 1157 | rval = 0; |
| 1158 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1159 | new->br_startblock, new->br_blockcount, |
| 1160 | &i))) |
| 1161 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1162 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1164 | new->br_startblock, new->br_blockcount, |
| 1165 | newext))) |
| 1166 | goto done; |
| 1167 | } |
| 1168 | break; |
| 1169 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1170 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | /* |
| 1172 | * Setting the first part of a previous oldext extent to newext. |
| 1173 | * The left neighbor is contiguous. |
| 1174 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1175 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 1176 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | LEFT.br_blockcount + new->br_blockcount); |
| 1178 | xfs_bmbt_set_startoff(ep, |
| 1179 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1180 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1181 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1182 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | xfs_bmbt_set_startblock(ep, |
| 1184 | new->br_startblock + new->br_blockcount); |
| 1185 | xfs_bmbt_set_blockcount(ep, |
| 1186 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1187 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1188 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1189 | --*idx; |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | if (cur == NULL) |
| 1192 | rval = XFS_ILOG_DEXT; |
| 1193 | else { |
| 1194 | rval = 0; |
| 1195 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1196 | PREV.br_startblock, PREV.br_blockcount, |
| 1197 | &i))) |
| 1198 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1199 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if ((error = xfs_bmbt_update(cur, |
| 1201 | PREV.br_startoff + new->br_blockcount, |
| 1202 | PREV.br_startblock + new->br_blockcount, |
| 1203 | PREV.br_blockcount - new->br_blockcount, |
| 1204 | oldext))) |
| 1205 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1206 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | goto done; |
| 1208 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1209 | LEFT.br_startblock, |
| 1210 | LEFT.br_blockcount + new->br_blockcount, |
| 1211 | LEFT.br_state)) |
| 1212 | goto done; |
| 1213 | } |
| 1214 | break; |
| 1215 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1216 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* |
| 1218 | * Setting the first part of a previous oldext extent to newext. |
| 1219 | * The left neighbor is not contiguous. |
| 1220 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1221 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1223 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1224 | xfs_bmbt_set_blockcount(ep, |
| 1225 | PREV.br_blockcount - new->br_blockcount); |
| 1226 | xfs_bmbt_set_startblock(ep, |
| 1227 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1228 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1229 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1230 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | ip->i_d.di_nextents++; |
| 1232 | if (cur == NULL) |
| 1233 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1234 | else { |
| 1235 | rval = XFS_ILOG_CORE; |
| 1236 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1237 | PREV.br_startblock, PREV.br_blockcount, |
| 1238 | &i))) |
| 1239 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1240 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | if ((error = xfs_bmbt_update(cur, |
| 1242 | PREV.br_startoff + new->br_blockcount, |
| 1243 | PREV.br_startblock + new->br_blockcount, |
| 1244 | PREV.br_blockcount - new->br_blockcount, |
| 1245 | oldext))) |
| 1246 | goto done; |
| 1247 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1248 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1250 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | } |
| 1252 | break; |
| 1253 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1254 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | /* |
| 1256 | * Setting the last part of a previous oldext extent to newext. |
| 1257 | * The right neighbor is contiguous with the new allocation. |
| 1258 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1259 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | xfs_bmbt_set_blockcount(ep, |
| 1261 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1262 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1263 | |
| 1264 | ++*idx; |
| 1265 | |
| 1266 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1267 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1268 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1270 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | if (cur == NULL) |
| 1273 | rval = XFS_ILOG_DEXT; |
| 1274 | else { |
| 1275 | rval = 0; |
| 1276 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1277 | PREV.br_startblock, |
| 1278 | PREV.br_blockcount, &i))) |
| 1279 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1280 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1282 | PREV.br_startblock, |
| 1283 | PREV.br_blockcount - new->br_blockcount, |
| 1284 | oldext))) |
| 1285 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1286 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | goto done; |
| 1288 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1289 | new->br_startblock, |
| 1290 | new->br_blockcount + RIGHT.br_blockcount, |
| 1291 | newext))) |
| 1292 | goto done; |
| 1293 | } |
| 1294 | break; |
| 1295 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1296 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | /* |
| 1298 | * Setting the last part of a previous oldext extent to newext. |
| 1299 | * The right neighbor is not contiguous. |
| 1300 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1301 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | xfs_bmbt_set_blockcount(ep, |
| 1303 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1304 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1305 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1306 | ++*idx; |
| 1307 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 1308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | ip->i_d.di_nextents++; |
| 1310 | if (cur == NULL) |
| 1311 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1312 | else { |
| 1313 | rval = XFS_ILOG_CORE; |
| 1314 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1315 | PREV.br_startblock, PREV.br_blockcount, |
| 1316 | &i))) |
| 1317 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1318 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1320 | PREV.br_startblock, |
| 1321 | PREV.br_blockcount - new->br_blockcount, |
| 1322 | oldext))) |
| 1323 | goto done; |
| 1324 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1325 | new->br_startblock, new->br_blockcount, |
| 1326 | &i))) |
| 1327 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1328 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1330 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1332 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | } |
| 1334 | break; |
| 1335 | |
| 1336 | case 0: |
| 1337 | /* |
| 1338 | * Setting the middle part of a previous oldext extent to |
| 1339 | * newext. Contiguity is impossible here. |
| 1340 | * One extent becomes three extents. |
| 1341 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1342 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | xfs_bmbt_set_blockcount(ep, |
| 1344 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1345 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | r[0] = *new; |
| 1348 | r[1].br_startoff = new_endoff; |
| 1349 | r[1].br_blockcount = |
| 1350 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1351 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1352 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1353 | |
| 1354 | ++*idx; |
| 1355 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 1356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | ip->i_d.di_nextents += 2; |
| 1358 | if (cur == NULL) |
| 1359 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1360 | else { |
| 1361 | rval = XFS_ILOG_CORE; |
| 1362 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1363 | PREV.br_startblock, PREV.br_blockcount, |
| 1364 | &i))) |
| 1365 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1366 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | /* new right extent - oldext */ |
| 1368 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1369 | r[1].br_startblock, r[1].br_blockcount, |
| 1370 | r[1].br_state))) |
| 1371 | goto done; |
| 1372 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1374 | cur->bc_rec.b.br_blockcount = |
| 1375 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1376 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1378 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1379 | /* |
| 1380 | * Reset the cursor to the position of the new extent |
| 1381 | * we are about to insert as we can't trust it after |
| 1382 | * the previous insert. |
| 1383 | */ |
| 1384 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1385 | new->br_startblock, new->br_blockcount, |
| 1386 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1388 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1390 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1391 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1393 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | } |
| 1395 | break; |
| 1396 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1397 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1398 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1399 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1400 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1401 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1402 | case BMAP_LEFT_CONTIG: |
| 1403 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | /* |
| 1405 | * These cases are all impossible. |
| 1406 | */ |
| 1407 | ASSERT(0); |
| 1408 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1409 | |
| 1410 | /* convert to a btree if necessary */ |
| 1411 | if (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) == XFS_DINODE_FMT_EXTENTS && |
| 1412 | XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) > ifp->if_ext_max) { |
| 1413 | int tmp_logflags; /* partial log flag return val */ |
| 1414 | |
| 1415 | ASSERT(cur == NULL); |
| 1416 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, |
| 1417 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 1418 | *logflagsp |= tmp_logflags; |
| 1419 | if (error) |
| 1420 | goto done; |
| 1421 | } |
| 1422 | |
| 1423 | /* clear out the allocated field, done with it now in any case. */ |
| 1424 | if (cur) { |
| 1425 | cur->bc_private.b.allocated = 0; |
| 1426 | *curp = cur; |
| 1427 | } |
| 1428 | |
| 1429 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1431 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | return error; |
| 1433 | #undef LEFT |
| 1434 | #undef RIGHT |
| 1435 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1439 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1441 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | xfs_bmap_add_extent_hole_delay( |
| 1443 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1444 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1445 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1447 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1449 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1450 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1451 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1452 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1453 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1455 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1457 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | /* |
| 1460 | * Check and set flags if this segment has a left neighbor |
| 1461 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1462 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1463 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1464 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1465 | |
| 1466 | if (isnullstartblock(left.br_startblock)) |
| 1467 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | /* |
| 1471 | * Check and set flags if the current (right) segment exists. |
| 1472 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1473 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1474 | 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] | 1475 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1476 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1477 | |
| 1478 | if (isnullstartblock(right.br_startblock)) |
| 1479 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | /* |
| 1483 | * Set contiguity flags on the left and right neighbors. |
| 1484 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1485 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1486 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1487 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1488 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1489 | state |= BMAP_LEFT_CONTIG; |
| 1490 | |
| 1491 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1492 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1493 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1494 | (!(state & BMAP_LEFT_CONTIG) || |
| 1495 | (left.br_blockcount + new->br_blockcount + |
| 1496 | right.br_blockcount <= MAXEXTLEN))) |
| 1497 | state |= BMAP_RIGHT_CONTIG; |
| 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | /* |
| 1500 | * Switch out based on the contiguity flags. |
| 1501 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1502 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1503 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | /* |
| 1505 | * New allocation is contiguous with delayed allocations |
| 1506 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1507 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1509 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | temp = left.br_blockcount + new->br_blockcount + |
| 1511 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1512 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1513 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1514 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1515 | oldlen = startblockval(left.br_startblock) + |
| 1516 | startblockval(new->br_startblock) + |
| 1517 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1519 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1520 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1521 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1522 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1523 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1526 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * New allocation is contiguous with a delayed allocation |
| 1529 | * on the left. |
| 1530 | * Merge the new allocation with the left neighbor. |
| 1531 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1532 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1534 | |
| 1535 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1536 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1537 | oldlen = startblockval(left.br_startblock) + |
| 1538 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1540 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1541 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1542 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | break; |
| 1544 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1545 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | /* |
| 1547 | * New allocation is contiguous with a delayed allocation |
| 1548 | * on the right. |
| 1549 | * Merge the new allocation with the right neighbor. |
| 1550 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1551 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1553 | oldlen = startblockval(new->br_startblock) + |
| 1554 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1556 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1557 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1558 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1559 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | break; |
| 1561 | |
| 1562 | case 0: |
| 1563 | /* |
| 1564 | * New allocation is not contiguous with another |
| 1565 | * delayed allocation. |
| 1566 | * Insert a new entry. |
| 1567 | */ |
| 1568 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1569 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | break; |
| 1571 | } |
| 1572 | if (oldlen != newlen) { |
| 1573 | ASSERT(oldlen > newlen); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1574 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1575 | (int64_t)(oldlen - newlen), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | /* |
| 1577 | * Nothing to do for disk quota accounting here. |
| 1578 | */ |
| 1579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1583 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | */ |
| 1585 | STATIC int /* error */ |
| 1586 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1587 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1589 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1590 | xfs_btree_cur_t **curp, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1591 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1592 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 1593 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | int *logflagsp, /* inode logging flags */ |
| 1595 | int whichfork) /* data or attr fork */ |
| 1596 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | int i; /* temp state */ |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1599 | xfs_btree_cur_t *cur; /* if null, not a btree */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1601 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1602 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1603 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1606 | *logflagsp = 0; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1607 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1608 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1609 | cur = *curp; |
| 1610 | |
| 1611 | ASSERT(*idx >= 0); |
| 1612 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 1613 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 1614 | ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
| 1615 | |
| 1616 | XFS_STATS_INC(xs_add_exlist); |
| 1617 | |
| 1618 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1619 | if (whichfork == XFS_ATTR_FORK) |
| 1620 | state |= BMAP_ATTRFORK; |
| 1621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | /* |
| 1623 | * Check and set flags if this segment has a left neighbor. |
| 1624 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1625 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1626 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1627 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1628 | if (isnullstartblock(left.br_startblock)) |
| 1629 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | /* |
| 1633 | * Check and set flags if this segment has a current value. |
| 1634 | * Not true if we're inserting into the "hole" at eof. |
| 1635 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1636 | if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1637 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1638 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1639 | if (isnullstartblock(right.br_startblock)) |
| 1640 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | /* |
| 1644 | * We're inserting a real allocation between "left" and "right". |
| 1645 | * Set the contiguity flags. Don't let extents get too large. |
| 1646 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1647 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1648 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1649 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1650 | left.br_state == new->br_state && |
| 1651 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1652 | state |= BMAP_LEFT_CONTIG; |
| 1653 | |
| 1654 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1655 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1656 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1657 | new->br_state == right.br_state && |
| 1658 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1659 | (!(state & BMAP_LEFT_CONTIG) || |
| 1660 | left.br_blockcount + new->br_blockcount + |
| 1661 | right.br_blockcount <= MAXEXTLEN)) |
| 1662 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1664 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | /* |
| 1666 | * Select which case we're in here, and implement it. |
| 1667 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1668 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1669 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * New allocation is contiguous with real allocations on the |
| 1672 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1673 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1675 | --*idx; |
| 1676 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1677 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | left.br_blockcount + new->br_blockcount + |
| 1679 | right.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1680 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1681 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1682 | xfs_iext_remove(ip, *idx + 1, 1, state); |
| 1683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1685 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 1686 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1687 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1688 | } else { |
| 1689 | rval = XFS_ILOG_CORE; |
| 1690 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1691 | right.br_startoff, |
| 1692 | right.br_startblock, |
| 1693 | right.br_blockcount, &i))) |
| 1694 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1695 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1696 | if ((error = xfs_btree_delete(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1697 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1698 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1699 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1700 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1701 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1702 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 1703 | left.br_startblock, |
| 1704 | left.br_blockcount + |
| 1705 | new->br_blockcount + |
| 1706 | right.br_blockcount, |
| 1707 | left.br_state))) |
| 1708 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1710 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1712 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | /* |
| 1714 | * New allocation is contiguous with a real allocation |
| 1715 | * on the left. |
| 1716 | * Merge the new allocation with the left neighbor. |
| 1717 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1718 | --*idx; |
| 1719 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1720 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1722 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1725 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1726 | } else { |
| 1727 | rval = 0; |
| 1728 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1729 | left.br_startoff, |
| 1730 | left.br_startblock, |
| 1731 | left.br_blockcount, &i))) |
| 1732 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1733 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1734 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 1735 | left.br_startblock, |
| 1736 | left.br_blockcount + |
| 1737 | new->br_blockcount, |
| 1738 | left.br_state))) |
| 1739 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1741 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1743 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | /* |
| 1745 | * New allocation is contiguous with a real allocation |
| 1746 | * on the right. |
| 1747 | * Merge the new allocation with the right neighbor. |
| 1748 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1749 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1750 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1751 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | new->br_blockcount + right.br_blockcount, |
| 1753 | right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1754 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1757 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1758 | } else { |
| 1759 | rval = 0; |
| 1760 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1761 | right.br_startoff, |
| 1762 | right.br_startblock, |
| 1763 | right.br_blockcount, &i))) |
| 1764 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1765 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1766 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1767 | new->br_startblock, |
| 1768 | new->br_blockcount + |
| 1769 | right.br_blockcount, |
| 1770 | right.br_state))) |
| 1771 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1773 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
| 1775 | case 0: |
| 1776 | /* |
| 1777 | * New allocation is not contiguous with another |
| 1778 | * real allocation. |
| 1779 | * Insert a new entry. |
| 1780 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1781 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1783 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 1784 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1785 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1786 | } else { |
| 1787 | rval = XFS_ILOG_CORE; |
| 1788 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1789 | new->br_startoff, |
| 1790 | new->br_startblock, |
| 1791 | new->br_blockcount, &i))) |
| 1792 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1793 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1794 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1795 | if ((error = xfs_btree_insert(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1796 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1797 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1799 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1801 | |
| 1802 | /* convert to a btree if necessary */ |
| 1803 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 1804 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 1805 | int tmp_logflags; /* partial log flag return val */ |
| 1806 | |
| 1807 | ASSERT(cur == NULL); |
| 1808 | error = xfs_bmap_extents_to_btree(tp, ip, first, |
| 1809 | flist, &cur, 0, &tmp_logflags, whichfork); |
| 1810 | *logflagsp |= tmp_logflags; |
| 1811 | if (error) |
| 1812 | goto done; |
| 1813 | } |
| 1814 | |
| 1815 | /* clear out the allocated field, done with it now in any case. */ |
| 1816 | if (cur) { |
| 1817 | cur->bc_private.b.allocated = 0; |
| 1818 | *curp = cur; |
| 1819 | } |
| 1820 | xfs_bmap_check_leaf_extents(cur, ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1821 | done: |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1822 | *logflagsp |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1823 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1826 | /* |
| 1827 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 1828 | */ |
| 1829 | STATIC int |
| 1830 | xfs_bmap_extsize_align( |
| 1831 | xfs_mount_t *mp, |
| 1832 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 1833 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 1834 | xfs_extlen_t extsz, /* align to this extent size */ |
| 1835 | int rt, /* is this a realtime inode? */ |
| 1836 | int eof, /* is extent at end-of-file? */ |
| 1837 | int delay, /* creating delalloc extent? */ |
| 1838 | int convert, /* overwriting unwritten extent? */ |
| 1839 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 1840 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 1841 | { |
| 1842 | xfs_fileoff_t orig_off; /* original offset */ |
| 1843 | xfs_extlen_t orig_alen; /* original length */ |
| 1844 | xfs_fileoff_t orig_end; /* original off+len */ |
| 1845 | xfs_fileoff_t nexto; /* next file offset */ |
| 1846 | xfs_fileoff_t prevo; /* previous file offset */ |
| 1847 | xfs_fileoff_t align_off; /* temp for offset */ |
| 1848 | xfs_extlen_t align_alen; /* temp for length */ |
| 1849 | xfs_extlen_t temp; /* temp for calculations */ |
| 1850 | |
| 1851 | if (convert) |
| 1852 | return 0; |
| 1853 | |
| 1854 | orig_off = align_off = *offp; |
| 1855 | orig_alen = align_alen = *lenp; |
| 1856 | orig_end = orig_off + orig_alen; |
| 1857 | |
| 1858 | /* |
| 1859 | * If this request overlaps an existing extent, then don't |
| 1860 | * attempt to perform any additional alignment. |
| 1861 | */ |
| 1862 | if (!delay && !eof && |
| 1863 | (orig_off >= gotp->br_startoff) && |
| 1864 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * If the file offset is unaligned vs. the extent size |
| 1870 | * we need to align it. This will be possible unless |
| 1871 | * the file was previously written with a kernel that didn't |
| 1872 | * perform this alignment, or if a truncate shot us in the |
| 1873 | * foot. |
| 1874 | */ |
| 1875 | temp = do_mod(orig_off, extsz); |
| 1876 | if (temp) { |
| 1877 | align_alen += temp; |
| 1878 | align_off -= temp; |
| 1879 | } |
| 1880 | /* |
| 1881 | * Same adjustment for the end of the requested area. |
| 1882 | */ |
| 1883 | if ((temp = (align_alen % extsz))) { |
| 1884 | align_alen += extsz - temp; |
| 1885 | } |
| 1886 | /* |
| 1887 | * If the previous block overlaps with this proposed allocation |
| 1888 | * then move the start forward without adjusting the length. |
| 1889 | */ |
| 1890 | if (prevp->br_startoff != NULLFILEOFF) { |
| 1891 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 1892 | prevo = prevp->br_startoff; |
| 1893 | else |
| 1894 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 1895 | } else |
| 1896 | prevo = 0; |
| 1897 | if (align_off != orig_off && align_off < prevo) |
| 1898 | align_off = prevo; |
| 1899 | /* |
| 1900 | * If the next block overlaps with this proposed allocation |
| 1901 | * then move the start back without adjusting the length, |
| 1902 | * but not before offset 0. |
| 1903 | * This may of course make the start overlap previous block, |
| 1904 | * and if we hit the offset 0 limit then the next block |
| 1905 | * can still overlap too. |
| 1906 | */ |
| 1907 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 1908 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 1909 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 1910 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 1911 | else |
| 1912 | nexto = gotp->br_startoff; |
| 1913 | } else |
| 1914 | nexto = NULLFILEOFF; |
| 1915 | if (!eof && |
| 1916 | align_off + align_alen != orig_end && |
| 1917 | align_off + align_alen > nexto) |
| 1918 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 1919 | /* |
| 1920 | * If we're now overlapping the next or previous extent that |
| 1921 | * means we can't fit an extsz piece in this hole. Just move |
| 1922 | * the start forward to the first valid spot and set |
| 1923 | * the length so we hit the end. |
| 1924 | */ |
| 1925 | if (align_off != orig_off && align_off < prevo) |
| 1926 | align_off = prevo; |
| 1927 | if (align_off + align_alen != orig_end && |
| 1928 | align_off + align_alen > nexto && |
| 1929 | nexto != NULLFILEOFF) { |
| 1930 | ASSERT(nexto > prevo); |
| 1931 | align_alen = nexto - align_off; |
| 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | * If realtime, and the result isn't a multiple of the realtime |
| 1936 | * extent size we need to remove blocks until it is. |
| 1937 | */ |
| 1938 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 1939 | /* |
| 1940 | * We're not covering the original request, or |
| 1941 | * we won't be able to once we fix the length. |
| 1942 | */ |
| 1943 | if (orig_off < align_off || |
| 1944 | orig_end > align_off + align_alen || |
| 1945 | align_alen - temp < orig_alen) |
| 1946 | return XFS_ERROR(EINVAL); |
| 1947 | /* |
| 1948 | * Try to fix it by moving the start up. |
| 1949 | */ |
| 1950 | if (align_off + temp <= orig_off) { |
| 1951 | align_alen -= temp; |
| 1952 | align_off += temp; |
| 1953 | } |
| 1954 | /* |
| 1955 | * Try to fix it by moving the end in. |
| 1956 | */ |
| 1957 | else if (align_off + align_alen - temp >= orig_end) |
| 1958 | align_alen -= temp; |
| 1959 | /* |
| 1960 | * Set the start to the minimum then trim the length. |
| 1961 | */ |
| 1962 | else { |
| 1963 | align_alen -= orig_off - align_off; |
| 1964 | align_off = orig_off; |
| 1965 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 1966 | } |
| 1967 | /* |
| 1968 | * Result doesn't cover the request, fail it. |
| 1969 | */ |
| 1970 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 1971 | return XFS_ERROR(EINVAL); |
| 1972 | } else { |
| 1973 | ASSERT(orig_off >= align_off); |
| 1974 | ASSERT(orig_end <= align_off + align_alen); |
| 1975 | } |
| 1976 | |
| 1977 | #ifdef DEBUG |
| 1978 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 1979 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 1980 | if (prevp->br_startoff != NULLFILEOFF) |
| 1981 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 1982 | #endif |
| 1983 | |
| 1984 | *lenp = align_alen; |
| 1985 | *offp = align_off; |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | #define XFS_ALLOC_GAP_UNITS 4 |
| 1990 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 1991 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 1992 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 1994 | { |
| 1995 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 1997 | xfs_mount_t *mp; /* mount point structure */ |
| 1998 | int nullfb; /* true if ap->firstblock isn't set */ |
| 1999 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
| 2001 | #define ISVALID(x,y) \ |
| 2002 | (rt ? \ |
| 2003 | (x) < mp->m_sb.sb_rblocks : \ |
| 2004 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2005 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2006 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2009 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2011 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | /* |
| 2013 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2014 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2016 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 2017 | !isnullstartblock(ap->prev.br_startblock) && |
| 2018 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 2019 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2020 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | /* |
| 2022 | * Adjust for the gap between prevp and us. |
| 2023 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2024 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2025 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2027 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 2028 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | } |
| 2030 | /* |
| 2031 | * If not at eof, then compare the two neighbor blocks. |
| 2032 | * Figure out whether either one gives us a good starting point, |
| 2033 | * and pick the better one. |
| 2034 | */ |
| 2035 | else if (!ap->eof) { |
| 2036 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2037 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2038 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2039 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2040 | |
| 2041 | /* |
| 2042 | * If there's a previous (left) block, select a requested |
| 2043 | * start block based on it. |
| 2044 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2045 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 2046 | !isnullstartblock(ap->prev.br_startblock) && |
| 2047 | (prevbno = ap->prev.br_startblock + |
| 2048 | ap->prev.br_blockcount) && |
| 2049 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | /* |
| 2051 | * Calculate gap to end of previous block. |
| 2052 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2053 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2054 | (ap->prev.br_startoff + |
| 2055 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | /* |
| 2057 | * Figure the startblock based on the previous block's |
| 2058 | * end and the gap size. |
| 2059 | * Heuristic! |
| 2060 | * If the gap is large relative to the piece we're |
| 2061 | * allocating, or using it gives us an invalid block |
| 2062 | * number, then just use the end of the previous block. |
| 2063 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2064 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2066 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | prevbno += adjust; |
| 2068 | else |
| 2069 | prevdiff += adjust; |
| 2070 | /* |
| 2071 | * If the firstblock forbids it, can't use it, |
| 2072 | * must use default. |
| 2073 | */ |
| 2074 | if (!rt && !nullfb && |
| 2075 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2076 | prevbno = NULLFSBLOCK; |
| 2077 | } |
| 2078 | /* |
| 2079 | * No previous block or can't follow it, just default. |
| 2080 | */ |
| 2081 | else |
| 2082 | prevbno = NULLFSBLOCK; |
| 2083 | /* |
| 2084 | * If there's a following (right) block, select a requested |
| 2085 | * start block based on it. |
| 2086 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2087 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | /* |
| 2089 | * Calculate gap to start of next block. |
| 2090 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2091 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | /* |
| 2093 | * Figure the startblock based on the next block's |
| 2094 | * start and the gap size. |
| 2095 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2096 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | /* |
| 2098 | * Heuristic! |
| 2099 | * If the gap is large relative to the piece we're |
| 2100 | * allocating, or using it gives us an invalid block |
| 2101 | * number, then just use the start of the next block |
| 2102 | * offset by our length. |
| 2103 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2104 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2106 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2107 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 2108 | gotbno -= ap->length; |
| 2109 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } else |
| 2111 | gotdiff += adjust; |
| 2112 | /* |
| 2113 | * If the firstblock forbids it, can't use it, |
| 2114 | * must use default. |
| 2115 | */ |
| 2116 | if (!rt && !nullfb && |
| 2117 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2118 | gotbno = NULLFSBLOCK; |
| 2119 | } |
| 2120 | /* |
| 2121 | * No next block, just default. |
| 2122 | */ |
| 2123 | else |
| 2124 | gotbno = NULLFSBLOCK; |
| 2125 | /* |
| 2126 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2127 | * 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] | 2128 | */ |
| 2129 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2130 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2132 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2134 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2136 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | STATIC int |
| 2140 | xfs_bmap_rtalloc( |
| 2141 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2142 | { |
| 2143 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2144 | int error; /* error return value */ |
| 2145 | xfs_mount_t *mp; /* mount point structure */ |
| 2146 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2147 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2148 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2149 | xfs_rtblock_t rtb; |
| 2150 | |
| 2151 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2152 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2153 | prod = align / mp->m_sb.sb_rextsize; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2154 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2155 | align, 1, ap->eof, 0, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2156 | ap->conv, &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2157 | if (error) |
| 2158 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2159 | ASSERT(ap->length); |
| 2160 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2161 | |
| 2162 | /* |
| 2163 | * If the offset & length are not perfectly aligned |
| 2164 | * then kill prod, it will just get us in trouble. |
| 2165 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2166 | if (do_mod(ap->offset, align) || ap->length % align) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2167 | prod = 1; |
| 2168 | /* |
| 2169 | * Set ralen to be the actual requested length in rtextents. |
| 2170 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2171 | ralen = ap->length / mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2172 | /* |
| 2173 | * If the old value was close enough to MAXEXTLEN that |
| 2174 | * we rounded up to it, cut it back so it's valid again. |
| 2175 | * Note that if it's a really large request (bigger than |
| 2176 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2177 | * adjust the starting point to match it. |
| 2178 | */ |
| 2179 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2180 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2181 | |
| 2182 | /* |
| 2183 | * Lock out other modifications to the RT bitmap inode. |
| 2184 | */ |
Christoph Hellwig | 1050c71 | 2011-02-13 13:25:31 +0000 | [diff] [blame] | 2185 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 2186 | xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2187 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2188 | /* |
| 2189 | * If it's an allocation to an empty file at offset 0, |
| 2190 | * pick an extent that will space things out in the rt area. |
| 2191 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2192 | if (ap->eof && ap->offset == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2193 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2194 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2195 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2196 | if (error) |
| 2197 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2198 | ap->blkno = rtx * mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2199 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2200 | ap->blkno = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | xfs_bmap_adjacent(ap); |
| 2204 | |
| 2205 | /* |
| 2206 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2207 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2208 | atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2209 | do_div(ap->blkno, mp->m_sb.sb_rextsize); |
| 2210 | rtb = ap->blkno; |
| 2211 | ap->length = ralen; |
| 2212 | if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2213 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2214 | return error; |
| 2215 | if (rtb == NULLFSBLOCK && prod > 1 && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2216 | (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, |
| 2217 | ap->length, &ralen, atype, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2218 | ap->wasdel, 1, &rtb))) |
| 2219 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2220 | ap->blkno = rtb; |
| 2221 | if (ap->blkno != NULLFSBLOCK) { |
| 2222 | ap->blkno *= mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2223 | ralen *= mp->m_sb.sb_rextsize; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2224 | ap->length = ralen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2225 | ap->ip->i_d.di_nblocks += ralen; |
| 2226 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2227 | if (ap->wasdel) |
| 2228 | ap->ip->i_delayed_blks -= ralen; |
| 2229 | /* |
| 2230 | * Adjust the disk quota also. This was reserved |
| 2231 | * earlier. |
| 2232 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2233 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2234 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2235 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2236 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2237 | ap->length = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2238 | } |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2243 | xfs_bmap_btalloc_nullfb( |
| 2244 | struct xfs_bmalloca *ap, |
| 2245 | struct xfs_alloc_arg *args, |
| 2246 | xfs_extlen_t *blen) |
| 2247 | { |
| 2248 | struct xfs_mount *mp = ap->ip->i_mount; |
| 2249 | struct xfs_perag *pag; |
| 2250 | xfs_agnumber_t ag, startag; |
| 2251 | int notinit = 0; |
| 2252 | int error; |
| 2253 | |
| 2254 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2255 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2256 | else |
| 2257 | args->type = XFS_ALLOCTYPE_START_BNO; |
| 2258 | args->total = ap->total; |
| 2259 | |
| 2260 | /* |
| 2261 | * Search for an allocation group with a single extent large enough |
| 2262 | * for the request. If one isn't found, then adjust the minimum |
| 2263 | * allocation size to the largest space found. |
| 2264 | */ |
| 2265 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2266 | if (startag == NULLAGNUMBER) |
| 2267 | startag = ag = 0; |
| 2268 | |
| 2269 | pag = xfs_perag_get(mp, ag); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2270 | while (*blen < args->maxlen) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2271 | if (!pag->pagf_init) { |
| 2272 | error = xfs_alloc_pagf_init(mp, args->tp, ag, |
| 2273 | XFS_ALLOC_FLAG_TRYLOCK); |
| 2274 | if (error) { |
| 2275 | xfs_perag_put(pag); |
| 2276 | return error; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | /* |
| 2281 | * See xfs_alloc_fix_freelist... |
| 2282 | */ |
| 2283 | if (pag->pagf_init) { |
| 2284 | xfs_extlen_t longest; |
| 2285 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 2286 | if (*blen < longest) |
| 2287 | *blen = longest; |
| 2288 | } else |
| 2289 | notinit = 1; |
| 2290 | |
| 2291 | if (xfs_inode_is_filestream(ap->ip)) { |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2292 | if (*blen >= args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2293 | break; |
| 2294 | |
| 2295 | if (ap->userdata) { |
| 2296 | /* |
| 2297 | * If startag is an invalid AG, we've |
| 2298 | * come here once before and |
| 2299 | * xfs_filestream_new_ag picked the |
| 2300 | * best currently available. |
| 2301 | * |
| 2302 | * Don't continue looping, since we |
| 2303 | * could loop forever. |
| 2304 | */ |
| 2305 | if (startag == NULLAGNUMBER) |
| 2306 | break; |
| 2307 | |
| 2308 | error = xfs_filestream_new_ag(ap, &ag); |
| 2309 | xfs_perag_put(pag); |
| 2310 | if (error) |
| 2311 | return error; |
| 2312 | |
| 2313 | /* loop again to set 'blen'*/ |
| 2314 | startag = NULLAGNUMBER; |
| 2315 | pag = xfs_perag_get(mp, ag); |
| 2316 | continue; |
| 2317 | } |
| 2318 | } |
| 2319 | if (++ag == mp->m_sb.sb_agcount) |
| 2320 | ag = 0; |
| 2321 | if (ag == startag) |
| 2322 | break; |
| 2323 | xfs_perag_put(pag); |
| 2324 | pag = xfs_perag_get(mp, ag); |
| 2325 | } |
| 2326 | xfs_perag_put(pag); |
| 2327 | |
| 2328 | /* |
| 2329 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2330 | * possible that there is space for this request. |
| 2331 | */ |
| 2332 | if (notinit || *blen < ap->minlen) |
| 2333 | args->minlen = ap->minlen; |
| 2334 | /* |
| 2335 | * If the best seen length is less than the request |
| 2336 | * length, use the best as the minimum. |
| 2337 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2338 | else if (*blen < args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2339 | args->minlen = *blen; |
| 2340 | /* |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2341 | * Otherwise we've seen an extent as big as maxlen, |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2342 | * use that as the minimum. |
| 2343 | */ |
| 2344 | else |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2345 | args->minlen = args->maxlen; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2346 | |
| 2347 | /* |
| 2348 | * set the failure fallback case to look in the selected |
| 2349 | * AG as the stream may have moved. |
| 2350 | */ |
| 2351 | if (xfs_inode_is_filestream(ap->ip)) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2352 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2353 | |
| 2354 | return 0; |
| 2355 | } |
| 2356 | |
| 2357 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2358 | xfs_bmap_btalloc( |
| 2359 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2360 | { |
| 2361 | xfs_mount_t *mp; /* mount point structure */ |
| 2362 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2363 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2364 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2365 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2366 | xfs_alloc_arg_t args; |
| 2367 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2368 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2369 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2370 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2371 | int tryagain; |
| 2372 | int error; |
| 2373 | |
| 2374 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2375 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2376 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2377 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2378 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2379 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2380 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2381 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2382 | } |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2383 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 2384 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2385 | if (nullfb) { |
| 2386 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2387 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2388 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2389 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2390 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2391 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2392 | } |
| 2393 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2394 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2395 | |
| 2396 | xfs_bmap_adjacent(ap); |
| 2397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2399 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | * it's in the right allocation group. |
| 2401 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2402 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | ; |
| 2404 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2405 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | * Normal allocation, done through xfs_alloc_vextent. |
| 2408 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2409 | tryagain = isaligned = 0; |
| 2410 | args.tp = ap->tp; |
| 2411 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2412 | args.fsbno = ap->blkno; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2413 | |
| 2414 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2415 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2416 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2417 | blen = 0; |
| 2418 | if (nullfb) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2419 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
| 2420 | if (error) |
| 2421 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2422 | } else if (ap->flist->xbf_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2423 | if (xfs_inode_is_filestream(ap->ip)) |
| 2424 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2425 | else |
| 2426 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2427 | args.total = args.minlen = ap->minlen; |
| 2428 | } else { |
| 2429 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2430 | args.total = ap->total; |
| 2431 | args.minlen = ap->minlen; |
| 2432 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2433 | /* apply extent size hints if obtained earlier */ |
| 2434 | if (unlikely(align)) { |
| 2435 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2436 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2437 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2438 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2439 | args.prod = 1; |
| 2440 | args.mod = 0; |
| 2441 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2442 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2443 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2444 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2445 | } |
| 2446 | /* |
| 2447 | * If we are not low on available data blocks, and the |
| 2448 | * underlying logical volume manager is a stripe, and |
| 2449 | * the file offset is zero then try to allocate data |
| 2450 | * blocks on stripe unit boundary. |
| 2451 | * NOTE: ap->aeof is only set if the allocation length |
| 2452 | * is >= the stripe unit and the allocation offset is |
| 2453 | * at the end of file. |
| 2454 | */ |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2455 | if (!ap->flist->xbf_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2456 | if (!ap->offset) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2457 | args.alignment = mp->m_dalign; |
| 2458 | atype = args.type; |
| 2459 | isaligned = 1; |
| 2460 | /* |
| 2461 | * Adjust for alignment |
| 2462 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2463 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2464 | args.minlen = blen - args.alignment; |
| 2465 | args.minalignslop = 0; |
| 2466 | } else { |
| 2467 | /* |
| 2468 | * First try an exact bno allocation. |
| 2469 | * If it fails then do a near or start bno |
| 2470 | * allocation with alignment turned on. |
| 2471 | */ |
| 2472 | atype = args.type; |
| 2473 | tryagain = 1; |
| 2474 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2475 | args.alignment = 1; |
| 2476 | /* |
| 2477 | * Compute the minlen+alignment for the |
| 2478 | * next case. Set slop so that the value |
| 2479 | * of minlen+alignment+slop doesn't go up |
| 2480 | * between the calls. |
| 2481 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2482 | if (blen > mp->m_dalign && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2483 | nextminlen = blen - mp->m_dalign; |
| 2484 | else |
| 2485 | nextminlen = args.minlen; |
| 2486 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2487 | args.minalignslop = |
| 2488 | nextminlen + mp->m_dalign - |
| 2489 | args.minlen - 1; |
| 2490 | else |
| 2491 | args.minalignslop = 0; |
| 2492 | } |
| 2493 | } else { |
| 2494 | args.alignment = 1; |
| 2495 | args.minalignslop = 0; |
| 2496 | } |
| 2497 | args.minleft = ap->minleft; |
| 2498 | args.wasdel = ap->wasdel; |
| 2499 | args.isfl = 0; |
| 2500 | args.userdata = ap->userdata; |
| 2501 | if ((error = xfs_alloc_vextent(&args))) |
| 2502 | return error; |
| 2503 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2504 | /* |
| 2505 | * Exact allocation failed. Now try with alignment |
| 2506 | * turned on. |
| 2507 | */ |
| 2508 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2509 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2510 | args.alignment = mp->m_dalign; |
| 2511 | args.minlen = nextminlen; |
| 2512 | args.minalignslop = 0; |
| 2513 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | if ((error = xfs_alloc_vextent(&args))) |
| 2515 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2516 | } |
| 2517 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2518 | /* |
| 2519 | * allocation failed, so turn off alignment and |
| 2520 | * try again. |
| 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 = 0; |
| 2525 | if ((error = xfs_alloc_vextent(&args))) |
| 2526 | return error; |
| 2527 | } |
| 2528 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2529 | args.minlen > ap->minlen) { |
| 2530 | args.minlen = ap->minlen; |
| 2531 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2532 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2533 | if ((error = xfs_alloc_vextent(&args))) |
| 2534 | return error; |
| 2535 | } |
| 2536 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2537 | args.fsbno = 0; |
| 2538 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2539 | args.total = ap->minlen; |
| 2540 | args.minleft = 0; |
| 2541 | if ((error = xfs_alloc_vextent(&args))) |
| 2542 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2543 | ap->flist->xbf_low = 1; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2544 | } |
| 2545 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2546 | /* |
| 2547 | * check the allocation happened at the same or higher AG than |
| 2548 | * the first block that was allocated. |
| 2549 | */ |
| 2550 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 2551 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 2552 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
| 2553 | (ap->flist->xbf_low && |
| 2554 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 2555 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 2556 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2557 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2558 | if (*ap->firstblock == NULLFSBLOCK) |
| 2559 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2560 | ASSERT(nullfb || fb_agno == args.agno || |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2561 | (ap->flist->xbf_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2562 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2563 | ap->ip->i_d.di_nblocks += args.len; |
| 2564 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2565 | if (ap->wasdel) |
| 2566 | ap->ip->i_delayed_blks -= args.len; |
| 2567 | /* |
| 2568 | * Adjust the disk quota also. This was reserved |
| 2569 | * earlier. |
| 2570 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2571 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2572 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2573 | XFS_TRANS_DQ_BCOUNT, |
| 2574 | (long) args.len); |
| 2575 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2576 | ap->blkno = NULLFSBLOCK; |
| 2577 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | } |
| 2579 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /* |
| 2583 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2584 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2585 | */ |
| 2586 | STATIC int |
| 2587 | xfs_bmap_alloc( |
| 2588 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2589 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2590 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2591 | return xfs_bmap_rtalloc(ap); |
| 2592 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | /* |
| 2596 | * Transform a btree format file with only one leaf node, where the |
| 2597 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2598 | * 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] | 2599 | * give up the space for the btree root and pitch the leaf block. |
| 2600 | */ |
| 2601 | STATIC int /* error */ |
| 2602 | xfs_bmap_btree_to_extents( |
| 2603 | xfs_trans_t *tp, /* transaction pointer */ |
| 2604 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2605 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2606 | int *logflagsp, /* inode logging flags */ |
| 2607 | int whichfork) /* data or attr fork */ |
| 2608 | { |
| 2609 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2610 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | xfs_fsblock_t cbno; /* child block number */ |
| 2612 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2613 | int error; /* error return value */ |
| 2614 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2615 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2616 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2617 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2619 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2621 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2622 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2623 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2624 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2625 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2626 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2627 | 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] | 2628 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | *logflagsp = 0; |
| 2630 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2631 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | return error; |
| 2633 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2635 | XFS_BMAP_BTREE_REF))) |
| 2636 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2637 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2638 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | return error; |
| 2640 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2641 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2642 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | xfs_trans_binval(tp, cbp); |
| 2644 | if (cur->bc_bufs[0] == cbp) |
| 2645 | cur->bc_bufs[0] = NULL; |
| 2646 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2647 | ASSERT(ifp->if_broot == NULL); |
| 2648 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2649 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2650 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | return 0; |
| 2652 | } |
| 2653 | |
| 2654 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2655 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | * after removing space (or undoing a delayed allocation). |
| 2657 | */ |
| 2658 | STATIC int /* error */ |
| 2659 | xfs_bmap_del_extent( |
| 2660 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2661 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2662 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2664 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2665 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2667 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | { |
| 2669 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2670 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2671 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2672 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2673 | int delay; /* current block is delayed allocated */ |
| 2674 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2675 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | int error; /* error return value */ |
| 2677 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2679 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2680 | int i; /* temp state */ |
| 2681 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2682 | xfs_mount_t *mp; /* mount structure */ |
| 2683 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2684 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2685 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | uint qfield; /* quota field to update */ |
| 2687 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2688 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2689 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | |
| 2691 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2692 | |
| 2693 | if (whichfork == XFS_ATTR_FORK) |
| 2694 | state |= BMAP_ATTRFORK; |
| 2695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | mp = ip->i_mount; |
| 2697 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2698 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2699 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2701 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | xfs_bmbt_get_all(ep, &got); |
| 2703 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2704 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2705 | got_endoff = got.br_startoff + got.br_blockcount; |
| 2706 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2707 | delay = isnullstartblock(got.br_startblock); |
| 2708 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | flags = 0; |
| 2710 | qfield = 0; |
| 2711 | error = 0; |
| 2712 | /* |
| 2713 | * If deleting a real allocation, must free up the disk space. |
| 2714 | */ |
| 2715 | if (!delay) { |
| 2716 | flags = XFS_ILOG_CORE; |
| 2717 | /* |
| 2718 | * Realtime allocation. Free it and record di_nblocks update. |
| 2719 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2720 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | xfs_fsblock_t bno; |
| 2722 | xfs_filblks_t len; |
| 2723 | |
| 2724 | ASSERT(do_mod(del->br_blockcount, |
| 2725 | mp->m_sb.sb_rextsize) == 0); |
| 2726 | ASSERT(do_mod(del->br_startblock, |
| 2727 | mp->m_sb.sb_rextsize) == 0); |
| 2728 | bno = del->br_startblock; |
| 2729 | len = del->br_blockcount; |
| 2730 | do_div(bno, mp->m_sb.sb_rextsize); |
| 2731 | do_div(len, mp->m_sb.sb_rextsize); |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 2732 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 2733 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | goto done; |
| 2735 | do_fx = 0; |
| 2736 | nblks = len * mp->m_sb.sb_rextsize; |
| 2737 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 2738 | } |
| 2739 | /* |
| 2740 | * Ordinary allocation. |
| 2741 | */ |
| 2742 | else { |
| 2743 | do_fx = 1; |
| 2744 | nblks = del->br_blockcount; |
| 2745 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 2746 | } |
| 2747 | /* |
| 2748 | * Set up del_endblock and cur for later. |
| 2749 | */ |
| 2750 | del_endblock = del->br_startblock + del->br_blockcount; |
| 2751 | if (cur) { |
| 2752 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 2753 | got.br_startblock, got.br_blockcount, |
| 2754 | &i))) |
| 2755 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2756 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | } |
| 2758 | da_old = da_new = 0; |
| 2759 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2760 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | da_new = 0; |
| 2762 | nblks = 0; |
| 2763 | do_fx = 0; |
| 2764 | } |
| 2765 | /* |
| 2766 | * Set flag value to use in switch statement. |
| 2767 | * Left-contig is 2, right-contig is 1. |
| 2768 | */ |
| 2769 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 2770 | (got_endoff == del_endoff)) { |
| 2771 | case 3: |
| 2772 | /* |
| 2773 | * Matches the whole extent. Delete the entry. |
| 2774 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2775 | xfs_iext_remove(ip, *idx, 1, |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2776 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2777 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | if (delay) |
| 2779 | break; |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2782 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2783 | flags |= XFS_ILOG_CORE; |
| 2784 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2785 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | break; |
| 2787 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2788 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2790 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | break; |
| 2792 | |
| 2793 | case 2: |
| 2794 | /* |
| 2795 | * Deleting the first part of the extent. |
| 2796 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2797 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 2799 | temp = got.br_blockcount - del->br_blockcount; |
| 2800 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | if (delay) { |
| 2802 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2803 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2804 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2805 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | da_new = temp; |
| 2807 | break; |
| 2808 | } |
| 2809 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2810 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2812 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | break; |
| 2814 | } |
| 2815 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 2816 | got.br_blockcount - del->br_blockcount, |
| 2817 | got.br_state))) |
| 2818 | goto done; |
| 2819 | break; |
| 2820 | |
| 2821 | case 1: |
| 2822 | /* |
| 2823 | * Deleting the last part of the extent. |
| 2824 | */ |
| 2825 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2826 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | if (delay) { |
| 2829 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2830 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2831 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2832 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | da_new = temp; |
| 2834 | break; |
| 2835 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2836 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2838 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | break; |
| 2840 | } |
| 2841 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 2842 | got.br_startblock, |
| 2843 | got.br_blockcount - del->br_blockcount, |
| 2844 | got.br_state))) |
| 2845 | goto done; |
| 2846 | break; |
| 2847 | |
| 2848 | case 0: |
| 2849 | /* |
| 2850 | * Deleting the middle of the extent. |
| 2851 | */ |
| 2852 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2853 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | xfs_bmbt_set_blockcount(ep, temp); |
| 2855 | new.br_startoff = del_endoff; |
| 2856 | temp2 = got_endoff - del_endoff; |
| 2857 | new.br_blockcount = temp2; |
| 2858 | new.br_state = got.br_state; |
| 2859 | if (!delay) { |
| 2860 | new.br_startblock = del_endblock; |
| 2861 | flags |= XFS_ILOG_CORE; |
| 2862 | if (cur) { |
| 2863 | if ((error = xfs_bmbt_update(cur, |
| 2864 | got.br_startoff, |
| 2865 | got.br_startblock, temp, |
| 2866 | got.br_state))) |
| 2867 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2868 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | goto done; |
| 2870 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2871 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | if (error && error != ENOSPC) |
| 2873 | goto done; |
| 2874 | /* |
| 2875 | * If get no-space back from btree insert, |
| 2876 | * it tried a split, and we have a zero |
| 2877 | * block reservation. |
| 2878 | * Fix up our state and return the error. |
| 2879 | */ |
| 2880 | if (error == ENOSPC) { |
| 2881 | /* |
| 2882 | * Reset the cursor, don't trust |
| 2883 | * it after any insert operation. |
| 2884 | */ |
| 2885 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2886 | got.br_startoff, |
| 2887 | got.br_startblock, |
| 2888 | temp, &i))) |
| 2889 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2890 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | /* |
| 2892 | * Update the btree record back |
| 2893 | * to the original value. |
| 2894 | */ |
| 2895 | if ((error = xfs_bmbt_update(cur, |
| 2896 | got.br_startoff, |
| 2897 | got.br_startblock, |
| 2898 | got.br_blockcount, |
| 2899 | got.br_state))) |
| 2900 | goto done; |
| 2901 | /* |
| 2902 | * Reset the extent record back |
| 2903 | * to the original value. |
| 2904 | */ |
| 2905 | xfs_bmbt_set_blockcount(ep, |
| 2906 | got.br_blockcount); |
| 2907 | flags = 0; |
| 2908 | error = XFS_ERROR(ENOSPC); |
| 2909 | goto done; |
| 2910 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2911 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2913 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2915 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2916 | } else { |
| 2917 | ASSERT(whichfork == XFS_DATA_FORK); |
| 2918 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2919 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2921 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | da_new = temp + temp2; |
| 2923 | while (da_new > da_old) { |
| 2924 | if (temp) { |
| 2925 | temp--; |
| 2926 | da_new--; |
| 2927 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2928 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | } |
| 2930 | if (da_new == da_old) |
| 2931 | break; |
| 2932 | if (temp2) { |
| 2933 | temp2--; |
| 2934 | da_new--; |
| 2935 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2936 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | } |
| 2938 | } |
| 2939 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2940 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2941 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 2942 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | break; |
| 2944 | } |
| 2945 | /* |
| 2946 | * If we need to, add to list of extents to delete. |
| 2947 | */ |
| 2948 | if (do_fx) |
| 2949 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 2950 | mp); |
| 2951 | /* |
| 2952 | * Adjust inode # blocks in the file. |
| 2953 | */ |
| 2954 | if (nblks) |
| 2955 | ip->i_d.di_nblocks -= nblks; |
| 2956 | /* |
| 2957 | * Adjust quota data. |
| 2958 | */ |
| 2959 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2960 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | |
| 2962 | /* |
| 2963 | * Account for change in delayed indirect blocks. |
| 2964 | * Nothing to do for disk quota accounting here. |
| 2965 | */ |
| 2966 | ASSERT(da_old >= da_new); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 2967 | if (da_old > da_new) { |
| 2968 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2969 | (int64_t)(da_old - da_new), 0); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 2970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | done: |
| 2972 | *logflagsp = flags; |
| 2973 | return error; |
| 2974 | } |
| 2975 | |
| 2976 | /* |
| 2977 | * Remove the entry "free" from the free item list. Prev points to the |
| 2978 | * previous entry, unless "free" is the head of the list. |
| 2979 | */ |
| 2980 | STATIC void |
| 2981 | xfs_bmap_del_free( |
| 2982 | xfs_bmap_free_t *flist, /* free item list header */ |
| 2983 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 2984 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 2985 | { |
| 2986 | if (prev) |
| 2987 | prev->xbfi_next = free->xbfi_next; |
| 2988 | else |
| 2989 | flist->xbf_first = free->xbfi_next; |
| 2990 | flist->xbf_count--; |
| 2991 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 2992 | } |
| 2993 | |
| 2994 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | * Convert an extents-format file into a btree-format file. |
| 2996 | * The new file will have a root block (in the inode) and a single child block. |
| 2997 | */ |
| 2998 | STATIC int /* error */ |
| 2999 | xfs_bmap_extents_to_btree( |
| 3000 | xfs_trans_t *tp, /* transaction pointer */ |
| 3001 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3002 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3003 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3004 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3005 | int wasdel, /* converting a delayed alloc */ |
| 3006 | int *logflagsp, /* inode logging flags */ |
| 3007 | int whichfork) /* data or attr fork */ |
| 3008 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3009 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3011 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3012 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3013 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3015 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3017 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3019 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3020 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3021 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3023 | |
| 3024 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3025 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3026 | ASSERT(ifp->if_ext_max == |
| 3027 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3028 | /* |
| 3029 | * Make space in the inode incore. |
| 3030 | */ |
| 3031 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3032 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | /* |
| 3035 | * Fill in the root. |
| 3036 | */ |
| 3037 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3038 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3039 | block->bb_level = cpu_to_be16(1); |
| 3040 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3041 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3042 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | /* |
| 3045 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3046 | */ |
| 3047 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3048 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | cur->bc_private.b.firstblock = *firstblock; |
| 3050 | cur->bc_private.b.flist = flist; |
| 3051 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3052 | /* |
| 3053 | * Convert to a btree with two levels, one record in root. |
| 3054 | */ |
| 3055 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3056 | args.tp = tp; |
| 3057 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3058 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | if (*firstblock == NULLFSBLOCK) { |
| 3060 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3061 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3062 | } else if (flist->xbf_low) { |
| 3063 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3064 | args.fsbno = *firstblock; |
| 3065 | } else { |
| 3066 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3067 | args.fsbno = *firstblock; |
| 3068 | } |
| 3069 | args.minlen = args.maxlen = args.prod = 1; |
| 3070 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3071 | args.minalignslop = 0; |
| 3072 | args.wasdel = wasdel; |
| 3073 | *logflagsp = 0; |
| 3074 | if ((error = xfs_alloc_vextent(&args))) { |
| 3075 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3076 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3077 | return error; |
| 3078 | } |
| 3079 | /* |
| 3080 | * Allocation can't fail, the space was reserved. |
| 3081 | */ |
| 3082 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3083 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3084 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3085 | (flist->xbf_low && |
| 3086 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3087 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3088 | cur->bc_private.b.allocated++; |
| 3089 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3090 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3092 | /* |
| 3093 | * Fill in the child block. |
| 3094 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3095 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3096 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3098 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3099 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3100 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3102 | for (cnt = i = 0; i < nextents; i++) { |
| 3103 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3104 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3105 | arp->l0 = cpu_to_be64(ep->l0); |
| 3106 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | arp++; cnt++; |
| 3108 | } |
| 3109 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3110 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3111 | xfs_btree_set_numrecs(ablock, cnt); |
| 3112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | /* |
| 3114 | * Fill in the root key and pointer. |
| 3115 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3116 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3117 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3118 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3119 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3120 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3121 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | /* |
| 3124 | * Do all this logging at the end so that |
| 3125 | * the root is at the right level. |
| 3126 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3127 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3128 | 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] | 3129 | ASSERT(*curp == NULL); |
| 3130 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3131 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | return 0; |
| 3133 | } |
| 3134 | |
| 3135 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3136 | * Calculate the default attribute fork offset for newly created inodes. |
| 3137 | */ |
| 3138 | uint |
| 3139 | xfs_default_attroffset( |
| 3140 | struct xfs_inode *ip) |
| 3141 | { |
| 3142 | struct xfs_mount *mp = ip->i_mount; |
| 3143 | uint offset; |
| 3144 | |
| 3145 | if (mp->m_sb.sb_inodesize == 256) { |
| 3146 | offset = XFS_LITINO(mp) - |
| 3147 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3148 | } else { |
| 3149 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3150 | } |
| 3151 | |
| 3152 | ASSERT(offset < XFS_LITINO(mp)); |
| 3153 | return offset; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3157 | * Helper routine to reset inode di_forkoff field when switching |
| 3158 | * attribute fork from local to extent format - we reset it where |
| 3159 | * possible to make space available for inline data fork extents. |
| 3160 | */ |
| 3161 | STATIC void |
| 3162 | xfs_bmap_forkoff_reset( |
| 3163 | xfs_mount_t *mp, |
| 3164 | xfs_inode_t *ip, |
| 3165 | int whichfork) |
| 3166 | { |
| 3167 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3168 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3169 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3170 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3171 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3172 | |
| 3173 | if (dfl_forkoff > ip->i_d.di_forkoff) { |
| 3174 | ip->i_d.di_forkoff = dfl_forkoff; |
| 3175 | ip->i_df.if_ext_max = |
| 3176 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3177 | ip->i_afp->if_ext_max = |
| 3178 | XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3179 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | * Convert a local file to an extents file. |
| 3185 | * This code is out of bounds for data forks of regular files, |
| 3186 | * since the file data needs to get logged so things will stay consistent. |
| 3187 | * (The bmap-level manipulations are ok, though). |
| 3188 | */ |
| 3189 | STATIC int /* error */ |
| 3190 | xfs_bmap_local_to_extents( |
| 3191 | xfs_trans_t *tp, /* transaction pointer */ |
| 3192 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3193 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3194 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3195 | int *logflagsp, /* inode logging flags */ |
| 3196 | int whichfork) /* data or attr fork */ |
| 3197 | { |
| 3198 | int error; /* error return value */ |
| 3199 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3201 | |
| 3202 | /* |
| 3203 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3204 | * So sending the data fork of a regular inode is invalid. |
| 3205 | */ |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3206 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3208 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3209 | flags = 0; |
| 3210 | error = 0; |
| 3211 | if (ifp->if_bytes) { |
| 3212 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3213 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3214 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | |
| 3216 | args.tp = tp; |
| 3217 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3218 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3219 | ASSERT((ifp->if_flags & |
| 3220 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | /* |
| 3222 | * Allocate a block. We know we need only one, since the |
| 3223 | * file currently fits in an inode. |
| 3224 | */ |
| 3225 | if (*firstblock == NULLFSBLOCK) { |
| 3226 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3227 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3228 | } else { |
| 3229 | args.fsbno = *firstblock; |
| 3230 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3231 | } |
| 3232 | args.total = total; |
| 3233 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3234 | args.isfl = args.minalignslop = 0; |
| 3235 | args.minlen = args.maxlen = args.prod = 1; |
| 3236 | if ((error = xfs_alloc_vextent(&args))) |
| 3237 | goto done; |
| 3238 | /* |
| 3239 | * Can't fail, the space was reserved. |
| 3240 | */ |
| 3241 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3242 | ASSERT(args.len == 1); |
| 3243 | *firstblock = args.fsbno; |
| 3244 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 3245 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3247 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3249 | xfs_iext_add(ifp, 0, 1); |
| 3250 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3252 | trace_xfs_bmap_post_update(ip, 0, |
| 3253 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3254 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3256 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3257 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3259 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3260 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3262 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3265 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3266 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3267 | flags |= XFS_ILOG_CORE; |
| 3268 | done: |
| 3269 | *logflagsp = flags; |
| 3270 | return error; |
| 3271 | } |
| 3272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3274 | * Search the extent records for the entry containing block bno. |
| 3275 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3276 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3277 | * entry (null if none). Else, *lastxp will be set to the index |
| 3278 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3279 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3280 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3281 | xfs_bmap_search_multi_extents( |
| 3282 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3283 | xfs_fileoff_t bno, /* block number searched for */ |
| 3284 | int *eofp, /* out: end of file found */ |
| 3285 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3286 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3287 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3288 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3289 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3290 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3291 | |
| 3292 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3293 | * Initialize the extent entry structure to catch access to |
| 3294 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3295 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3296 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3297 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3298 | gotp->br_state = XFS_EXT_INVALID; |
| 3299 | #if XFS_BIG_BLKNOS |
| 3300 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3301 | #else |
| 3302 | gotp->br_startblock = 0xffffa5a5; |
| 3303 | #endif |
| 3304 | prevp->br_startoff = NULLFILEOFF; |
| 3305 | |
| 3306 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3307 | if (lastx > 0) { |
| 3308 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3309 | } |
| 3310 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3311 | xfs_bmbt_get_all(ep, gotp); |
| 3312 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3313 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3314 | if (lastx > 0) { |
| 3315 | *gotp = *prevp; |
| 3316 | } |
| 3317 | *eofp = 1; |
| 3318 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3319 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3320 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3321 | return ep; |
| 3322 | } |
| 3323 | |
| 3324 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | * Search the extents list for the inode, for the extent containing bno. |
| 3326 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3327 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3328 | * Else, *lastxp will be set to the index of the found |
| 3329 | * entry; *gotp will contain the entry. |
| 3330 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3331 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | xfs_bmap_search_extents( |
| 3333 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3334 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3335 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | int *eofp, /* out: end of file found */ |
| 3337 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3338 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3339 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3340 | { |
| 3341 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3342 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | |
| 3344 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3345 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3347 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3348 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3349 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3350 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3351 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3352 | "Access to block zero in inode %llu " |
| 3353 | "start_block: %llx start_off: %llx " |
| 3354 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3355 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3356 | (unsigned long long)gotp->br_startblock, |
| 3357 | (unsigned long long)gotp->br_startoff, |
| 3358 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3359 | gotp->br_state, *lastxp); |
| 3360 | *lastxp = NULLEXTNUM; |
| 3361 | *eofp = 1; |
| 3362 | return NULL; |
| 3363 | } |
| 3364 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | /* |
| 3368 | * Compute the worst-case number of indirect blocks that will be used |
| 3369 | * for ip's delayed extent of length "len". |
| 3370 | */ |
| 3371 | STATIC xfs_filblks_t |
| 3372 | xfs_bmap_worst_indlen( |
| 3373 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3374 | xfs_filblks_t len) /* delayed extent length */ |
| 3375 | { |
| 3376 | int level; /* btree level number */ |
| 3377 | int maxrecs; /* maximum record count at this level */ |
| 3378 | xfs_mount_t *mp; /* mount structure */ |
| 3379 | xfs_filblks_t rval; /* return value */ |
| 3380 | |
| 3381 | mp = ip->i_mount; |
| 3382 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3383 | for (level = 0, rval = 0; |
| 3384 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3385 | level++) { |
| 3386 | len += maxrecs - 1; |
| 3387 | do_div(len, maxrecs); |
| 3388 | rval += len; |
| 3389 | if (len == 1) |
| 3390 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3391 | level - 1; |
| 3392 | if (level == 0) |
| 3393 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3394 | } |
| 3395 | return rval; |
| 3396 | } |
| 3397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | /* |
| 3399 | * Convert inode from non-attributed to attributed. |
| 3400 | * Must not be in a transaction, ip must not be locked. |
| 3401 | */ |
| 3402 | int /* error code */ |
| 3403 | xfs_bmap_add_attrfork( |
| 3404 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3405 | int size, /* space new attribute needs */ |
| 3406 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3409 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3412 | int blks; /* space reservation */ |
| 3413 | int version = 1; /* superblock attr version */ |
| 3414 | int committed; /* xaction was committed */ |
| 3415 | int logflags; /* logging flags */ |
| 3416 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3418 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | ASSERT(ip->i_df.if_ext_max == |
| 3420 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | mp = ip->i_mount; |
| 3423 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3424 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3425 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3426 | if (rsvd) |
| 3427 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3428 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3429 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3430 | goto error0; |
| 3431 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3432 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3434 | XFS_QMOPT_RES_REGBLKS); |
| 3435 | if (error) { |
| 3436 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3437 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3438 | return error; |
| 3439 | } |
| 3440 | if (XFS_IFORK_Q(ip)) |
| 3441 | goto error1; |
| 3442 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3443 | /* |
| 3444 | * For inodes coming from pre-6.2 filesystems. |
| 3445 | */ |
| 3446 | ASSERT(ip->i_d.di_aformat == 0); |
| 3447 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3448 | } |
| 3449 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3450 | |
| 3451 | xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | switch (ip->i_d.di_format) { |
| 3455 | case XFS_DINODE_FMT_DEV: |
| 3456 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3457 | break; |
| 3458 | case XFS_DINODE_FMT_UUID: |
| 3459 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3460 | break; |
| 3461 | case XFS_DINODE_FMT_LOCAL: |
| 3462 | case XFS_DINODE_FMT_EXTENTS: |
| 3463 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3464 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3465 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3466 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3467 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3468 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | break; |
| 3470 | default: |
| 3471 | ASSERT(0); |
| 3472 | error = XFS_ERROR(EINVAL); |
| 3473 | goto error1; |
| 3474 | } |
| 3475 | ip->i_df.if_ext_max = |
| 3476 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3477 | ASSERT(ip->i_afp == NULL); |
| 3478 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 3479 | ip->i_afp->if_ext_max = |
| 3480 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3481 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3482 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3483 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | switch (ip->i_d.di_format) { |
| 3485 | case XFS_DINODE_FMT_LOCAL: |
| 3486 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3487 | &logflags); |
| 3488 | break; |
| 3489 | case XFS_DINODE_FMT_EXTENTS: |
| 3490 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3491 | &flist, &logflags); |
| 3492 | break; |
| 3493 | case XFS_DINODE_FMT_BTREE: |
| 3494 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3495 | &logflags); |
| 3496 | break; |
| 3497 | default: |
| 3498 | error = 0; |
| 3499 | break; |
| 3500 | } |
| 3501 | if (logflags) |
| 3502 | xfs_trans_log_inode(tp, ip, logflags); |
| 3503 | if (error) |
| 3504 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3505 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3506 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3507 | __int64_t sbfields = 0; |
| 3508 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3509 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3510 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3511 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3512 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3513 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3514 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3515 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3516 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3517 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3518 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3519 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3520 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3521 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3522 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3524 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | goto error2; |
Dave Chinner | 713bf88 | 2010-03-08 11:26:23 +1100 | [diff] [blame] | 3526 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | ASSERT(ip->i_df.if_ext_max == |
| 3528 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3529 | return error; |
| 3530 | error2: |
| 3531 | xfs_bmap_cancel(&flist); |
| 3532 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3534 | error0: |
| 3535 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 3536 | ASSERT(ip->i_df.if_ext_max == |
| 3537 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3538 | return error; |
| 3539 | } |
| 3540 | |
| 3541 | /* |
| 3542 | * Add the extent to the list of extents to be free at transaction end. |
| 3543 | * The list is maintained sorted (by block number). |
| 3544 | */ |
| 3545 | /* ARGSUSED */ |
| 3546 | void |
| 3547 | xfs_bmap_add_free( |
| 3548 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3549 | xfs_filblks_t len, /* length of extent */ |
| 3550 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3551 | xfs_mount_t *mp) /* mount point structure */ |
| 3552 | { |
| 3553 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3554 | xfs_bmap_free_item_t *new; /* new element */ |
| 3555 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3556 | #ifdef DEBUG |
| 3557 | xfs_agnumber_t agno; |
| 3558 | xfs_agblock_t agbno; |
| 3559 | |
| 3560 | ASSERT(bno != NULLFSBLOCK); |
| 3561 | ASSERT(len > 0); |
| 3562 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3563 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3565 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3566 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3567 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3568 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3569 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3570 | #endif |
| 3571 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3572 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3573 | new->xbfi_startblock = bno; |
| 3574 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3575 | for (prev = NULL, cur = flist->xbf_first; |
| 3576 | cur != NULL; |
| 3577 | prev = cur, cur = cur->xbfi_next) { |
| 3578 | if (cur->xbfi_startblock >= bno) |
| 3579 | break; |
| 3580 | } |
| 3581 | if (prev) |
| 3582 | prev->xbfi_next = new; |
| 3583 | else |
| 3584 | flist->xbf_first = new; |
| 3585 | new->xbfi_next = cur; |
| 3586 | flist->xbf_count++; |
| 3587 | } |
| 3588 | |
| 3589 | /* |
| 3590 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3591 | * in this filesystem. Done once, during mount. |
| 3592 | */ |
| 3593 | void |
| 3594 | xfs_bmap_compute_maxlevels( |
| 3595 | xfs_mount_t *mp, /* file system mount structure */ |
| 3596 | int whichfork) /* data or attr fork */ |
| 3597 | { |
| 3598 | int level; /* btree level */ |
| 3599 | uint maxblocks; /* max blocks at this level */ |
| 3600 | uint maxleafents; /* max leaf entries possible */ |
| 3601 | int maxrootrecs; /* max records in root block */ |
| 3602 | int minleafrecs; /* min records in leaf block */ |
| 3603 | int minnoderecs; /* min records in node block */ |
| 3604 | int sz; /* root block size */ |
| 3605 | |
| 3606 | /* |
| 3607 | * The maximum number of extents in a file, hence the maximum |
| 3608 | * number of leaf entries, is controlled by the type of di_nextents |
| 3609 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3610 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3611 | * |
| 3612 | * 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] | 3613 | * the fork offset of all the inodes will be |
| 3614 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3615 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3616 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3617 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3618 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3620 | if (whichfork == XFS_DATA_FORK) { |
| 3621 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3622 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3623 | } else { |
| 3624 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3625 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3626 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3627 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3629 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3631 | for (level = 1; maxblocks > 1; level++) { |
| 3632 | if (maxblocks <= maxrootrecs) |
| 3633 | maxblocks = 1; |
| 3634 | else |
| 3635 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3636 | } |
| 3637 | mp->m_bm_maxlevels[whichfork] = level; |
| 3638 | } |
| 3639 | |
| 3640 | /* |
| 3641 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3642 | * caller. Frees all the extents that need freeing, which must be done |
| 3643 | * last due to locking considerations. We never free any extents in |
Christoph Hellwig | 859f57c | 2011-08-27 14:45:11 +0000 | [diff] [blame] | 3644 | * the first transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | * |
| 3646 | * Return 1 if the given transaction was committed and a new one |
| 3647 | * started, and 0 otherwise in the committed parameter. |
| 3648 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | int /* error */ |
| 3650 | xfs_bmap_finish( |
| 3651 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3652 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | int *committed) /* xact committed or not */ |
| 3654 | { |
| 3655 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3656 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3657 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3658 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | unsigned int logres; /* new log reservation */ |
| 3660 | unsigned int logcount; /* new log count */ |
| 3661 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3662 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3663 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3664 | |
| 3665 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3666 | if (flist->xbf_count == 0) { |
| 3667 | *committed = 0; |
| 3668 | return 0; |
| 3669 | } |
| 3670 | ntp = *tp; |
| 3671 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3672 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3673 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3674 | free->xbfi_blockcount); |
| 3675 | logres = ntp->t_log_res; |
| 3676 | logcount = ntp->t_log_count; |
| 3677 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3678 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3679 | *tp = ntp; |
| 3680 | *committed = 1; |
| 3681 | /* |
| 3682 | * We have a new transaction, so we should return committed=1, |
| 3683 | * even though we're returning an error. |
| 3684 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3685 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3687 | |
| 3688 | /* |
| 3689 | * transaction commit worked ok so we can drop the extra ticket |
| 3690 | * reference that we gained in xfs_trans_dup() |
| 3691 | */ |
| 3692 | xfs_log_ticket_put(ntp->t_ticket); |
| 3693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3694 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 3695 | logcount))) |
| 3696 | return error; |
| 3697 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 3698 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 3699 | next = free->xbfi_next; |
| 3700 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 3701 | free->xbfi_blockcount))) { |
| 3702 | /* |
| 3703 | * The bmap free list will be cleaned up at a |
| 3704 | * higher level. The EFI will be canceled when |
| 3705 | * this transaction is aborted. |
| 3706 | * Need to force shutdown here to make sure it |
| 3707 | * happens, since this transaction may not be |
| 3708 | * dirty yet. |
| 3709 | */ |
| 3710 | mp = ntp->t_mountp; |
| 3711 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 3712 | xfs_force_shutdown(mp, |
| 3713 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3714 | SHUTDOWN_CORRUPT_INCORE : |
| 3715 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | return error; |
| 3717 | } |
| 3718 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 3719 | free->xbfi_blockcount); |
| 3720 | xfs_bmap_del_free(flist, NULL, free); |
| 3721 | } |
| 3722 | return 0; |
| 3723 | } |
| 3724 | |
| 3725 | /* |
| 3726 | * Free up any items left in the list. |
| 3727 | */ |
| 3728 | void |
| 3729 | xfs_bmap_cancel( |
| 3730 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 3731 | { |
| 3732 | xfs_bmap_free_item_t *free; /* free list item */ |
| 3733 | xfs_bmap_free_item_t *next; |
| 3734 | |
| 3735 | if (flist->xbf_count == 0) |
| 3736 | return; |
| 3737 | ASSERT(flist->xbf_first != NULL); |
| 3738 | for (free = flist->xbf_first; free; free = next) { |
| 3739 | next = free->xbfi_next; |
| 3740 | xfs_bmap_del_free(flist, NULL, free); |
| 3741 | } |
| 3742 | ASSERT(flist->xbf_count == 0); |
| 3743 | } |
| 3744 | |
| 3745 | /* |
| 3746 | * Returns the file-relative block number of the first unused block(s) |
| 3747 | * in the file with at least "len" logically contiguous blocks free. |
| 3748 | * This is the lowest-address hole if the file has holes, else the first block |
| 3749 | * past the end of file. |
| 3750 | * Return 0 if the file is currently local (in-inode). |
| 3751 | */ |
| 3752 | int /* error */ |
| 3753 | xfs_bmap_first_unused( |
| 3754 | xfs_trans_t *tp, /* transaction pointer */ |
| 3755 | xfs_inode_t *ip, /* incore inode */ |
| 3756 | xfs_extlen_t len, /* size of hole to find */ |
| 3757 | xfs_fileoff_t *first_unused, /* unused block */ |
| 3758 | int whichfork) /* data or attr fork */ |
| 3759 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3761 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3763 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 3764 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 3765 | xfs_fileoff_t max; /* starting useful block */ |
| 3766 | xfs_fileoff_t off; /* offset for this block */ |
| 3767 | xfs_extnum_t nextents; /* number of extent entries */ |
| 3768 | |
| 3769 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 3770 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 3771 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3772 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3773 | *first_unused = 0; |
| 3774 | return 0; |
| 3775 | } |
| 3776 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3777 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3778 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3779 | return error; |
| 3780 | lowest = *first_unused; |
| 3781 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3782 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3783 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | off = xfs_bmbt_get_startoff(ep); |
| 3785 | /* |
| 3786 | * See if the hole before this extent will work. |
| 3787 | */ |
| 3788 | if (off >= lowest + len && off - max >= len) { |
| 3789 | *first_unused = max; |
| 3790 | return 0; |
| 3791 | } |
| 3792 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 3793 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 3794 | } |
| 3795 | *first_unused = max; |
| 3796 | return 0; |
| 3797 | } |
| 3798 | |
| 3799 | /* |
| 3800 | * Returns the file-relative block number of the last block + 1 before |
| 3801 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3802 | * This is not based on i_size, it is based on the extent records. |
| 3803 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | */ |
| 3805 | int /* error */ |
| 3806 | xfs_bmap_last_before( |
| 3807 | xfs_trans_t *tp, /* transaction pointer */ |
| 3808 | xfs_inode_t *ip, /* incore inode */ |
| 3809 | xfs_fileoff_t *last_block, /* last block */ |
| 3810 | int whichfork) /* data or attr fork */ |
| 3811 | { |
| 3812 | xfs_fileoff_t bno; /* input file offset */ |
| 3813 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3814 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | int error; /* error return value */ |
| 3816 | xfs_bmbt_irec_t got; /* current extent value */ |
| 3817 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3818 | xfs_extnum_t lastx; /* last extent used */ |
| 3819 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 3820 | |
| 3821 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 3822 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3823 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 3824 | return XFS_ERROR(EIO); |
| 3825 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3826 | *last_block = 0; |
| 3827 | return 0; |
| 3828 | } |
| 3829 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3830 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3831 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3832 | return error; |
| 3833 | bno = *last_block - 1; |
| 3834 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 3835 | &prev); |
| 3836 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 3837 | if (prev.br_startoff == NULLFILEOFF) |
| 3838 | *last_block = 0; |
| 3839 | else |
| 3840 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 3841 | } |
| 3842 | /* |
| 3843 | * Otherwise *last_block is already the right answer. |
| 3844 | */ |
| 3845 | return 0; |
| 3846 | } |
| 3847 | |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3848 | STATIC int |
| 3849 | xfs_bmap_last_extent( |
| 3850 | struct xfs_trans *tp, |
| 3851 | struct xfs_inode *ip, |
| 3852 | int whichfork, |
| 3853 | struct xfs_bmbt_irec *rec, |
| 3854 | int *is_empty) |
| 3855 | { |
| 3856 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3857 | int error; |
| 3858 | int nextents; |
| 3859 | |
| 3860 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3861 | error = xfs_iread_extents(tp, ip, whichfork); |
| 3862 | if (error) |
| 3863 | return error; |
| 3864 | } |
| 3865 | |
| 3866 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 3867 | if (nextents == 0) { |
| 3868 | *is_empty = 1; |
| 3869 | return 0; |
| 3870 | } |
| 3871 | |
| 3872 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 3873 | *is_empty = 0; |
| 3874 | return 0; |
| 3875 | } |
| 3876 | |
| 3877 | /* |
| 3878 | * Check the last inode extent to determine whether this allocation will result |
| 3879 | * in blocks being allocated at the end of the file. When we allocate new data |
| 3880 | * blocks at the end of the file which do not start at the previous data block, |
| 3881 | * we will try to align the new blocks at stripe unit boundaries. |
| 3882 | * |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3883 | * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be |
| 3884 | * at, or past the EOF. |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3885 | */ |
| 3886 | STATIC int |
| 3887 | xfs_bmap_isaeof( |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3888 | struct xfs_bmalloca *bma, |
| 3889 | int whichfork) |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3890 | { |
| 3891 | struct xfs_bmbt_irec rec; |
| 3892 | int is_empty; |
| 3893 | int error; |
| 3894 | |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3895 | bma->aeof = 0; |
| 3896 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 3897 | &is_empty); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3898 | if (error || is_empty) |
| 3899 | return error; |
| 3900 | |
| 3901 | /* |
| 3902 | * Check if we are allocation or past the last extent, or at least into |
| 3903 | * the last delayed allocated extent. |
| 3904 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3905 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 3906 | (bma->offset >= rec.br_startoff && |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3907 | isnullstartblock(rec.br_startblock)); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3908 | return 0; |
| 3909 | } |
| 3910 | |
| 3911 | /* |
| 3912 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 3913 | * the allocation to a stripe unit boundary. All offsets are considered outside |
| 3914 | * the end of file for an empty fork, so 1 is returned in *eof in that case. |
| 3915 | */ |
| 3916 | int |
| 3917 | xfs_bmap_eof( |
| 3918 | struct xfs_inode *ip, |
| 3919 | xfs_fileoff_t endoff, |
| 3920 | int whichfork, |
| 3921 | int *eof) |
| 3922 | { |
| 3923 | struct xfs_bmbt_irec rec; |
| 3924 | int error; |
| 3925 | |
| 3926 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); |
| 3927 | if (error || *eof) |
| 3928 | return error; |
| 3929 | |
| 3930 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; |
| 3931 | return 0; |
| 3932 | } |
| 3933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | /* |
| 3935 | * 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] | 3936 | * the file. This is not based on i_size, it is based on the extent records. |
| 3937 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | */ |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3939 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | xfs_bmap_last_offset( |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3941 | struct xfs_trans *tp, |
| 3942 | struct xfs_inode *ip, |
| 3943 | xfs_fileoff_t *last_block, |
| 3944 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | { |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3946 | struct xfs_bmbt_irec rec; |
| 3947 | int is_empty; |
| 3948 | int error; |
| 3949 | |
| 3950 | *last_block = 0; |
| 3951 | |
| 3952 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 3953 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | |
| 3955 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3956 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | return XFS_ERROR(EIO); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3958 | |
| 3959 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 3960 | if (error || is_empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | return error; |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3962 | |
| 3963 | *last_block = rec.br_startoff + rec.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | return 0; |
| 3965 | } |
| 3966 | |
| 3967 | /* |
| 3968 | * Returns whether the selected fork of the inode has exactly one |
| 3969 | * block or not. For the data fork we check this matches di_size, |
| 3970 | * implying the file's range is 0..bsize-1. |
| 3971 | */ |
| 3972 | int /* 1=>1 block, 0=>otherwise */ |
| 3973 | xfs_bmap_one_block( |
| 3974 | xfs_inode_t *ip, /* incore inode */ |
| 3975 | int whichfork) /* data or attr fork */ |
| 3976 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3977 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3979 | int rval; /* return value */ |
| 3980 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 3981 | |
| 3982 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3983 | if (whichfork == XFS_DATA_FORK) { |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3984 | return S_ISREG(ip->i_d.di_mode) ? |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3985 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 3986 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 3987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | #endif /* !DEBUG */ |
| 3989 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 3990 | return 0; |
| 3991 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 3992 | return 0; |
| 3993 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3994 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3995 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | xfs_bmbt_get_all(ep, &s); |
| 3997 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 3998 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3999 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | return rval; |
| 4001 | } |
| 4002 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4003 | STATIC int |
| 4004 | xfs_bmap_sanity_check( |
| 4005 | struct xfs_mount *mp, |
| 4006 | struct xfs_buf *bp, |
| 4007 | int level) |
| 4008 | { |
| 4009 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4010 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 4011 | if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) || |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4012 | be16_to_cpu(block->bb_level) != level || |
| 4013 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4014 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4015 | return 0; |
| 4016 | return 1; |
| 4017 | } |
| 4018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | /* |
| 4020 | * Read in the extents to if_extents. |
| 4021 | * All inode fields are set up by caller, we just traverse the btree |
| 4022 | * and copy the records in. If the file system cannot contain unwritten |
| 4023 | * extents, the records are checked for no "state" flags. |
| 4024 | */ |
| 4025 | int /* error */ |
| 4026 | xfs_bmap_read_extents( |
| 4027 | xfs_trans_t *tp, /* transaction pointer */ |
| 4028 | xfs_inode_t *ip, /* incore inode */ |
| 4029 | int whichfork) /* data or attr fork */ |
| 4030 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4031 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4033 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4034 | int error; /* error return value */ |
| 4035 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4036 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4037 | xfs_ifork_t *ifp; /* fork structure */ |
| 4038 | int level; /* btree level, for checking */ |
| 4039 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4040 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | /* REFERENCED */ |
| 4042 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | |
| 4044 | bno = NULLFSBLOCK; |
| 4045 | mp = ip->i_mount; |
| 4046 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4047 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4048 | XFS_EXTFMT_INODE(ip); |
| 4049 | block = ifp->if_broot; |
| 4050 | /* |
| 4051 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4052 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4053 | level = be16_to_cpu(block->bb_level); |
| 4054 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4055 | 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] | 4056 | bno = be64_to_cpu(*pp); |
| 4057 | ASSERT(bno != NULLDFSBNO); |
| 4058 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4059 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | /* |
| 4061 | * Go down the tree until leaf level is reached, following the first |
| 4062 | * pointer (leftmost) at each level. |
| 4063 | */ |
| 4064 | while (level-- > 0) { |
| 4065 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4066 | XFS_BMAP_BTREE_REF))) |
| 4067 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4068 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4070 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | error0); |
| 4072 | if (level == 0) |
| 4073 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4074 | 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] | 4075 | bno = be64_to_cpu(*pp); |
| 4076 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | xfs_trans_brelse(tp, bp); |
| 4078 | } |
| 4079 | /* |
| 4080 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4081 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4082 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | i = 0; |
| 4084 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4085 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4086 | */ |
| 4087 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4088 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | xfs_fsblock_t nextbno; |
| 4090 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4091 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4093 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | if (unlikely(i + num_recs > room)) { |
| 4095 | ASSERT(i + num_recs <= room); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4096 | xfs_warn(ip->i_mount, |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4097 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | (unsigned long long) ip->i_ino); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4099 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 4100 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | goto error0; |
| 4102 | } |
| 4103 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4104 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | error0); |
| 4106 | /* |
| 4107 | * Read-ahead the next leaf block, if any. |
| 4108 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4109 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | if (nextbno != NULLFSBLOCK) |
| 4111 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4112 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4113 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4115 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4116 | start = i; |
| 4117 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4118 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4119 | trp->l0 = be64_to_cpu(frp->l0); |
| 4120 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | } |
| 4122 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4123 | /* |
| 4124 | * Check all attribute bmap btree records and |
| 4125 | * any "older" data bmap btree records for a |
| 4126 | * set bit in the "extent flag" position. |
| 4127 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4128 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4129 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4131 | XFS_ERRLEVEL_LOW, |
| 4132 | ip->i_mount); |
| 4133 | goto error0; |
| 4134 | } |
| 4135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | xfs_trans_brelse(tp, bp); |
| 4137 | bno = nextbno; |
| 4138 | /* |
| 4139 | * If we've reached the end, stop. |
| 4140 | */ |
| 4141 | if (bno == NULLFSBLOCK) |
| 4142 | break; |
| 4143 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4144 | XFS_BMAP_BTREE_REF))) |
| 4145 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4146 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4148 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4150 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | return 0; |
| 4152 | error0: |
| 4153 | xfs_trans_brelse(tp, bp); |
| 4154 | return XFS_ERROR(EFSCORRUPTED); |
| 4155 | } |
| 4156 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4157 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4159 | * 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] | 4160 | */ |
| 4161 | void |
| 4162 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4164 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4165 | int whichfork, /* data or attr fork */ |
| 4166 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4168 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4170 | int state = 0; |
| 4171 | |
| 4172 | if (whichfork == XFS_ATTR_FORK) |
| 4173 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | |
| 4175 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4176 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4177 | for (idx = 0; idx < cnt; idx++) |
| 4178 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | /* |
| 4182 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4183 | * given the callers original parameters. Specifically check the |
| 4184 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4185 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4186 | */ |
| 4187 | STATIC void |
| 4188 | xfs_bmap_validate_ret( |
| 4189 | xfs_fileoff_t bno, |
| 4190 | xfs_filblks_t len, |
| 4191 | int flags, |
| 4192 | xfs_bmbt_irec_t *mval, |
| 4193 | int nmap, |
| 4194 | int ret_nmap) |
| 4195 | { |
| 4196 | int i; /* index to map values */ |
| 4197 | |
| 4198 | ASSERT(ret_nmap <= nmap); |
| 4199 | |
| 4200 | for (i = 0; i < ret_nmap; i++) { |
| 4201 | ASSERT(mval[i].br_blockcount > 0); |
| 4202 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4203 | ASSERT(mval[i].br_startoff >= bno); |
| 4204 | ASSERT(mval[i].br_blockcount <= len); |
| 4205 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4206 | bno + len); |
| 4207 | } else { |
| 4208 | ASSERT(mval[i].br_startoff < bno + len); |
| 4209 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4210 | bno); |
| 4211 | } |
| 4212 | ASSERT(i == 0 || |
| 4213 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4214 | mval[i].br_startoff); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4215 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4216 | mval[i].br_startblock != HOLESTARTBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4218 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4219 | } |
| 4220 | } |
| 4221 | #endif /* DEBUG */ |
| 4222 | |
| 4223 | |
| 4224 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4225 | * Trim the returned map to the required bounds |
| 4226 | */ |
| 4227 | STATIC void |
| 4228 | xfs_bmapi_trim_map( |
| 4229 | struct xfs_bmbt_irec *mval, |
| 4230 | struct xfs_bmbt_irec *got, |
| 4231 | xfs_fileoff_t *bno, |
| 4232 | xfs_filblks_t len, |
| 4233 | xfs_fileoff_t obno, |
| 4234 | xfs_fileoff_t end, |
| 4235 | int n, |
| 4236 | int flags) |
| 4237 | { |
| 4238 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 4239 | got->br_startoff + got->br_blockcount <= obno) { |
| 4240 | *mval = *got; |
| 4241 | if (isnullstartblock(got->br_startblock)) |
| 4242 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4243 | return; |
| 4244 | } |
| 4245 | |
| 4246 | if (obno > *bno) |
| 4247 | *bno = obno; |
| 4248 | ASSERT((*bno >= obno) || (n == 0)); |
| 4249 | ASSERT(*bno < end); |
| 4250 | mval->br_startoff = *bno; |
| 4251 | if (isnullstartblock(got->br_startblock)) |
| 4252 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4253 | else |
| 4254 | mval->br_startblock = got->br_startblock + |
| 4255 | (*bno - got->br_startoff); |
| 4256 | /* |
| 4257 | * Return the minimum of what we got and what we asked for for |
| 4258 | * the length. We can use the len variable here because it is |
| 4259 | * modified below and we could have been there before coming |
| 4260 | * here if the first part of the allocation didn't overlap what |
| 4261 | * was asked for. |
| 4262 | */ |
| 4263 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 4264 | got->br_blockcount - (*bno - got->br_startoff)); |
| 4265 | mval->br_state = got->br_state; |
| 4266 | ASSERT(mval->br_blockcount <= len); |
| 4267 | return; |
| 4268 | } |
| 4269 | |
| 4270 | /* |
| 4271 | * Update and validate the extent map to return |
| 4272 | */ |
| 4273 | STATIC void |
| 4274 | xfs_bmapi_update_map( |
| 4275 | struct xfs_bmbt_irec **map, |
| 4276 | xfs_fileoff_t *bno, |
| 4277 | xfs_filblks_t *len, |
| 4278 | xfs_fileoff_t obno, |
| 4279 | xfs_fileoff_t end, |
| 4280 | int *n, |
| 4281 | int flags) |
| 4282 | { |
| 4283 | xfs_bmbt_irec_t *mval = *map; |
| 4284 | |
| 4285 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4286 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 4287 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 4288 | (mval->br_startoff < obno)); |
| 4289 | |
| 4290 | *bno = mval->br_startoff + mval->br_blockcount; |
| 4291 | *len = end - *bno; |
| 4292 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4293 | /* update previous map with new information */ |
| 4294 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4295 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4296 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4297 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4298 | mval[-1].br_state = mval->br_state; |
| 4299 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4300 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4301 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4302 | mval->br_startblock == mval[-1].br_startblock + |
| 4303 | mval[-1].br_blockcount && |
| 4304 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4305 | mval[-1].br_state == mval->br_state)) { |
| 4306 | ASSERT(mval->br_startoff == |
| 4307 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4308 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4309 | } else if (*n > 0 && |
| 4310 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4311 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4312 | mval->br_startoff == |
| 4313 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4314 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4315 | mval[-1].br_state = mval->br_state; |
| 4316 | } else if (!((*n == 0) && |
| 4317 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4318 | obno))) { |
| 4319 | mval++; |
| 4320 | (*n)++; |
| 4321 | } |
| 4322 | *map = mval; |
| 4323 | } |
| 4324 | |
| 4325 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4326 | * Map file blocks to filesystem blocks without allocation. |
| 4327 | */ |
| 4328 | int |
| 4329 | xfs_bmapi_read( |
| 4330 | struct xfs_inode *ip, |
| 4331 | xfs_fileoff_t bno, |
| 4332 | xfs_filblks_t len, |
| 4333 | struct xfs_bmbt_irec *mval, |
| 4334 | int *nmap, |
| 4335 | int flags) |
| 4336 | { |
| 4337 | struct xfs_mount *mp = ip->i_mount; |
| 4338 | struct xfs_ifork *ifp; |
| 4339 | struct xfs_bmbt_irec got; |
| 4340 | struct xfs_bmbt_irec prev; |
| 4341 | xfs_fileoff_t obno; |
| 4342 | xfs_fileoff_t end; |
| 4343 | xfs_extnum_t lastx; |
| 4344 | int error; |
| 4345 | int eof; |
| 4346 | int n = 0; |
| 4347 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4348 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4349 | |
| 4350 | ASSERT(*nmap >= 1); |
| 4351 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 4352 | XFS_BMAPI_IGSTATE))); |
| 4353 | |
| 4354 | if (unlikely(XFS_TEST_ERROR( |
| 4355 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4356 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4357 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4358 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
| 4359 | return XFS_ERROR(EFSCORRUPTED); |
| 4360 | } |
| 4361 | |
| 4362 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4363 | return XFS_ERROR(EIO); |
| 4364 | |
| 4365 | XFS_STATS_INC(xs_blk_mapr); |
| 4366 | |
| 4367 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4368 | ASSERT(ifp->if_ext_max == |
| 4369 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4370 | |
| 4371 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4372 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4373 | if (error) |
| 4374 | return error; |
| 4375 | } |
| 4376 | |
| 4377 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4378 | end = bno + len; |
| 4379 | obno = bno; |
| 4380 | |
| 4381 | while (bno < end && n < *nmap) { |
| 4382 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4383 | if (eof) |
| 4384 | got.br_startoff = end; |
| 4385 | if (got.br_startoff > bno) { |
| 4386 | /* Reading in a hole. */ |
| 4387 | mval->br_startoff = bno; |
| 4388 | mval->br_startblock = HOLESTARTBLOCK; |
| 4389 | mval->br_blockcount = |
| 4390 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4391 | mval->br_state = XFS_EXT_NORM; |
| 4392 | bno += mval->br_blockcount; |
| 4393 | len -= mval->br_blockcount; |
| 4394 | mval++; |
| 4395 | n++; |
| 4396 | continue; |
| 4397 | } |
| 4398 | |
| 4399 | /* set up the extent map to return. */ |
| 4400 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4401 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4402 | |
| 4403 | /* If we're done, stop now. */ |
| 4404 | if (bno >= end || n >= *nmap) |
| 4405 | break; |
| 4406 | |
| 4407 | /* Else go on to the next record. */ |
| 4408 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4409 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4410 | else |
| 4411 | eof = 1; |
| 4412 | } |
| 4413 | *nmap = n; |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4417 | STATIC int |
| 4418 | xfs_bmapi_reserve_delalloc( |
| 4419 | struct xfs_inode *ip, |
| 4420 | xfs_fileoff_t aoff, |
| 4421 | xfs_filblks_t len, |
| 4422 | struct xfs_bmbt_irec *got, |
| 4423 | struct xfs_bmbt_irec *prev, |
| 4424 | xfs_extnum_t *lastx, |
| 4425 | int eof) |
| 4426 | { |
| 4427 | struct xfs_mount *mp = ip->i_mount; |
| 4428 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4429 | xfs_extlen_t alen; |
| 4430 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4431 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4432 | xfs_extlen_t extsz; |
| 4433 | int error; |
| 4434 | |
| 4435 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4436 | if (!eof) |
| 4437 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4438 | |
| 4439 | /* Figure out the extent size, adjust alen */ |
| 4440 | extsz = xfs_get_extsz_hint(ip); |
| 4441 | if (extsz) { |
| 4442 | /* |
| 4443 | * Make sure we don't exceed a single extent length when we |
| 4444 | * align the extent by reducing length we are going to |
| 4445 | * allocate by the maximum amount extent size aligment may |
| 4446 | * require. |
| 4447 | */ |
| 4448 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); |
| 4449 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4450 | 1, 0, &aoff, &alen); |
| 4451 | ASSERT(!error); |
| 4452 | } |
| 4453 | |
| 4454 | if (rt) |
| 4455 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4456 | |
| 4457 | /* |
| 4458 | * Make a transaction-less quota reservation for delayed allocation |
| 4459 | * blocks. This number gets adjusted later. We return if we haven't |
| 4460 | * allocated blocks already inside this loop. |
| 4461 | */ |
| 4462 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4463 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4464 | if (error) |
| 4465 | return error; |
| 4466 | |
| 4467 | /* |
| 4468 | * Split changing sb for alen and indlen since they could be coming |
| 4469 | * from different places. |
| 4470 | */ |
| 4471 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4472 | ASSERT(indlen > 0); |
| 4473 | |
| 4474 | if (rt) { |
| 4475 | error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
| 4476 | -((int64_t)extsz), 0); |
| 4477 | } else { |
| 4478 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4479 | -((int64_t)alen), 0); |
| 4480 | } |
| 4481 | |
| 4482 | if (error) |
| 4483 | goto out_unreserve_quota; |
| 4484 | |
| 4485 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4486 | -((int64_t)indlen), 0); |
| 4487 | if (error) |
| 4488 | goto out_unreserve_blocks; |
| 4489 | |
| 4490 | |
| 4491 | ip->i_delayed_blks += alen; |
| 4492 | |
| 4493 | got->br_startoff = aoff; |
| 4494 | got->br_startblock = nullstartblock(indlen); |
| 4495 | got->br_blockcount = alen; |
| 4496 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4497 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4498 | |
| 4499 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4500 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4501 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4502 | */ |
| 4503 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4504 | |
| 4505 | ASSERT(got->br_startoff <= aoff); |
| 4506 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4507 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4508 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4509 | return 0; |
| 4510 | |
| 4511 | out_unreserve_blocks: |
| 4512 | if (rt) |
| 4513 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0); |
| 4514 | else |
| 4515 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0); |
| 4516 | out_unreserve_quota: |
| 4517 | if (XFS_IS_QUOTA_ON(mp)) |
| 4518 | xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ? |
| 4519 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4520 | return error; |
| 4521 | } |
| 4522 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4523 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4524 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4525 | */ |
| 4526 | int |
| 4527 | xfs_bmapi_delay( |
| 4528 | struct xfs_inode *ip, /* incore inode */ |
| 4529 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4530 | xfs_filblks_t len, /* length to map in file */ |
| 4531 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4532 | int *nmap, /* i/o: mval size/count */ |
| 4533 | int flags) /* XFS_BMAPI_... */ |
| 4534 | { |
| 4535 | struct xfs_mount *mp = ip->i_mount; |
| 4536 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4537 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4538 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4539 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4540 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4541 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4542 | int eof; /* we've hit the end of extents */ |
| 4543 | int n = 0; /* current extent index */ |
| 4544 | int error = 0; |
| 4545 | |
| 4546 | ASSERT(*nmap >= 1); |
| 4547 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4548 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
| 4549 | |
| 4550 | if (unlikely(XFS_TEST_ERROR( |
| 4551 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4552 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4553 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4554 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
| 4555 | return XFS_ERROR(EFSCORRUPTED); |
| 4556 | } |
| 4557 | |
| 4558 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4559 | return XFS_ERROR(EIO); |
| 4560 | |
| 4561 | XFS_STATS_INC(xs_blk_mapw); |
| 4562 | |
| 4563 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4564 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4565 | if (error) |
| 4566 | return error; |
| 4567 | } |
| 4568 | |
| 4569 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4570 | end = bno + len; |
| 4571 | obno = bno; |
| 4572 | |
| 4573 | while (bno < end && n < *nmap) { |
| 4574 | if (eof || got.br_startoff > bno) { |
| 4575 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4576 | &prev, &lastx, eof); |
| 4577 | if (error) { |
| 4578 | if (n == 0) { |
| 4579 | *nmap = 0; |
| 4580 | return error; |
| 4581 | } |
| 4582 | break; |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | /* set up the extent map to return. */ |
| 4587 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4588 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4589 | |
| 4590 | /* If we're done, stop now. */ |
| 4591 | if (bno >= end || n >= *nmap) |
| 4592 | break; |
| 4593 | |
| 4594 | /* Else go on to the next record. */ |
| 4595 | prev = got; |
| 4596 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4597 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4598 | else |
| 4599 | eof = 1; |
| 4600 | } |
| 4601 | |
| 4602 | *nmap = n; |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
| 4606 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4607 | STATIC int |
| 4608 | xfs_bmapi_allocate( |
| 4609 | struct xfs_bmalloca *bma, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4610 | int flags) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4611 | { |
| 4612 | struct xfs_mount *mp = bma->ip->i_mount; |
| 4613 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4614 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4615 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4616 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4617 | int error; |
| 4618 | int rt; |
| 4619 | |
| 4620 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip); |
| 4621 | |
| 4622 | /* |
| 4623 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4624 | * for in this bmap call but that wouldn't be as good. |
| 4625 | */ |
| 4626 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4627 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4628 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4629 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4630 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4631 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4632 | } |
| 4633 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4634 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4635 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4636 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4637 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | /* |
| 4641 | * Indicate if this is the first user data in the file, or just any |
| 4642 | * user data. |
| 4643 | */ |
| 4644 | if (!(flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4645 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4646 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
| 4647 | } |
| 4648 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4649 | bma->minlen = (flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4650 | |
| 4651 | /* |
| 4652 | * Only want to do the alignment at the eof if it is userdata and |
| 4653 | * allocation length is larger than a stripe unit. |
| 4654 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4655 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4656 | !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4657 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4658 | if (error) |
| 4659 | return error; |
| 4660 | } |
| 4661 | |
| 4662 | error = xfs_bmap_alloc(bma); |
| 4663 | if (error) |
| 4664 | return error; |
| 4665 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4666 | if (bma->flist->xbf_low) |
| 4667 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4668 | if (bma->cur) |
| 4669 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4670 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4671 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4672 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4673 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4674 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4675 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4676 | } |
| 4677 | /* |
| 4678 | * Bump the number of extents we've allocated |
| 4679 | * in this call. |
| 4680 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4681 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4682 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4683 | if (bma->cur) |
| 4684 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4685 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4686 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4687 | bma->got.br_startoff = bma->offset; |
| 4688 | bma->got.br_startblock = bma->blkno; |
| 4689 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4690 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4691 | |
| 4692 | /* |
| 4693 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4694 | * as unwritten. |
| 4695 | */ |
| 4696 | if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) && |
| 4697 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4698 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4699 | |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4700 | if (bma->wasdel) { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4701 | error = xfs_bmap_add_extent_delay_real(bma->tp, bma->ip, |
| 4702 | &bma->idx, &bma->cur, &bma->got, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4703 | bma->firstblock, bma->flist, &tmp_logflags); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4704 | } else { |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4705 | error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip, |
| 4706 | &bma->idx, &bma->cur, &bma->got, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4707 | bma->firstblock, bma->flist, &tmp_logflags, |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4708 | whichfork); |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4711 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4712 | if (error) |
| 4713 | return error; |
| 4714 | |
| 4715 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4716 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4717 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4718 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4719 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4720 | 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] | 4721 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4722 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4723 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4724 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4725 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4726 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4727 | return 0; |
| 4728 | } |
| 4729 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4730 | STATIC int |
| 4731 | xfs_bmapi_convert_unwritten( |
| 4732 | struct xfs_bmalloca *bma, |
| 4733 | struct xfs_bmbt_irec *mval, |
| 4734 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4735 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4736 | { |
| 4737 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4738 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4739 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4740 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4741 | int error; |
| 4742 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4743 | /* check if we need to do unwritten->real conversion */ |
| 4744 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4745 | (flags & XFS_BMAPI_PREALLOC)) |
| 4746 | return 0; |
| 4747 | |
| 4748 | /* check if we need to do real->unwritten conversion */ |
| 4749 | if (mval->br_state == XFS_EXT_NORM && |
| 4750 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4751 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4752 | return 0; |
| 4753 | |
| 4754 | /* |
| 4755 | * Modify (by adding) the state flag, if writing. |
| 4756 | */ |
| 4757 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4758 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4759 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4760 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4761 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4762 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4763 | } |
| 4764 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4765 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4766 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4767 | 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^] | 4768 | &bma->cur, mval, bma->firstblock, bma->flist, |
| 4769 | &tmp_logflags); |
| 4770 | bma->logflags |= tmp_logflags; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4771 | if (error) |
| 4772 | return error; |
| 4773 | |
| 4774 | /* |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4775 | * Update our extent pointer, given that |
| 4776 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4777 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4778 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4779 | 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] | 4780 | |
| 4781 | /* |
| 4782 | * We may have combined previously unwritten space with written space, |
| 4783 | * so generate another request. |
| 4784 | */ |
| 4785 | if (mval->br_blockcount < len) |
| 4786 | return EAGAIN; |
| 4787 | return 0; |
| 4788 | } |
| 4789 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4790 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4791 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4792 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4793 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4794 | * locking problems. |
| 4795 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | * The returned value in "firstblock" from the first call in a transaction |
| 4797 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4798 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4799 | * the first call in "total"; if no allocation group has that many free |
| 4800 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4801 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4802 | int |
| 4803 | xfs_bmapi_write( |
| 4804 | struct xfs_trans *tp, /* transaction pointer */ |
| 4805 | struct xfs_inode *ip, /* incore inode */ |
| 4806 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4807 | xfs_filblks_t len, /* length to map in file */ |
| 4808 | int flags, /* XFS_BMAPI_... */ |
| 4809 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4810 | controls a.g. for allocs */ |
| 4811 | xfs_extlen_t total, /* total blocks needed */ |
| 4812 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4813 | int *nmap, /* i/o: mval size/count */ |
| 4814 | struct xfs_bmap_free *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4816 | struct xfs_mount *mp = ip->i_mount; |
| 4817 | struct xfs_ifork *ifp; |
| 4818 | struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4819 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4820 | int eof; /* after the end of extents */ |
| 4821 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4822 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4823 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4824 | int whichfork; /* data or attr fork */ |
| 4825 | char inhole; /* current location is hole in file */ |
| 4826 | char wasdelay; /* old extent was delayed */ |
| 4827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4829 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4830 | int orig_flags; /* original flags arg value */ |
| 4831 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4832 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4833 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | |
| 4835 | orig_bno = bno; |
| 4836 | orig_len = len; |
| 4837 | orig_flags = flags; |
| 4838 | orig_mval = mval; |
| 4839 | orig_nmap = *nmap; |
| 4840 | #endif |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4843 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4844 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4845 | ASSERT(tp != NULL); |
| 4846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4848 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | if (unlikely(XFS_TEST_ERROR( |
| 4851 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4852 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4853 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4854 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4855 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | return XFS_ERROR(EFSCORRUPTED); |
| 4857 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4860 | return XFS_ERROR(EIO); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4863 | ASSERT(ifp->if_ext_max == |
| 4864 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4865 | |
| 4866 | XFS_STATS_INC(xs_blk_mapw); |
| 4867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4869 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, total, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4870 | &bma.logflags, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4871 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | goto error0; |
| 4873 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4874 | |
| 4875 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4877 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4879 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4880 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4881 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4885 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4886 | if (error) |
| 4887 | goto error0; |
| 4888 | } |
| 4889 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4890 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4891 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | n = 0; |
| 4893 | end = bno + len; |
| 4894 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4895 | |
| 4896 | bma.tp = tp; |
| 4897 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4898 | bma.total = total; |
| 4899 | bma.userdata = 0; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4900 | bma.flist = flist; |
| 4901 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4904 | inhole = eof || bma.got.br_startoff > bno; |
| 4905 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | /* |
| 4908 | * First, deal with the hole before the allocated space |
| 4909 | * that we found, if any. |
| 4910 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4911 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4912 | bma.eof = eof; |
| 4913 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4914 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4915 | bma.length = len; |
| 4916 | bma.offset = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4917 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4918 | error = xfs_bmapi_allocate(&bma, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | if (error) |
| 4920 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4921 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4922 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4924 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4925 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4926 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4927 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4929 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4930 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4931 | if (error == EAGAIN) |
| 4932 | continue; |
| 4933 | if (error) |
| 4934 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4936 | /* update the extent map to return */ |
| 4937 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | /* |
| 4940 | * If we're done, stop now. Stop when we've allocated |
| 4941 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4942 | * the transaction may get too big. |
| 4943 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4944 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4946 | |
| 4947 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4948 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4949 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4950 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4951 | &bma.got); |
| 4952 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4953 | eof = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | *nmap = n; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | /* |
| 4958 | * Transform from btree to extents, give it cur. |
| 4959 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4960 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4962 | int tmp_logflags = 0; |
| 4963 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4964 | ASSERT(bma.cur); |
| 4965 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4967 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | if (error) |
| 4969 | goto error0; |
| 4970 | } |
| 4971 | ASSERT(ifp->if_ext_max == |
| 4972 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4973 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 4974 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 4975 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | error0: |
| 4977 | /* |
| 4978 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4979 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4981 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4983 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 4984 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4986 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | /* |
| 4988 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4989 | * detecting a case where the data is changed, there's an error, |
| 4990 | * and it's not logged so we don't shutdown when we should. |
| 4991 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame^] | 4992 | if (bma.logflags) |
| 4993 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4994 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4995 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | if (!error) { |
| 4997 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4998 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4999 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5000 | bma.cur->bc_private.b.firstblock) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | (flist->xbf_low && |
| 5002 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5003 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5004 | bma.cur->bc_private.b.firstblock))); |
| 5005 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5007 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5009 | } |
| 5010 | if (!error) |
| 5011 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5012 | orig_nmap, *nmap); |
| 5013 | return error; |
| 5014 | } |
| 5015 | |
| 5016 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | * Unmap (remove) blocks from a file. |
| 5018 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5019 | * that value. If not all extents in the block range can be removed then |
| 5020 | * *done is set. |
| 5021 | */ |
| 5022 | int /* error */ |
| 5023 | xfs_bunmapi( |
| 5024 | xfs_trans_t *tp, /* transaction pointer */ |
| 5025 | struct xfs_inode *ip, /* incore inode */ |
| 5026 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5027 | xfs_filblks_t len, /* length to unmap in file */ |
| 5028 | int flags, /* misc flags */ |
| 5029 | xfs_extnum_t nexts, /* number of extents max */ |
| 5030 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5031 | controls a.g. for allocs */ |
| 5032 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 5033 | int *done) /* set if not done yet */ |
| 5034 | { |
| 5035 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5036 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5037 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5038 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | int error; /* error return value */ |
| 5040 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5041 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5043 | int isrt; /* freeing in rt area */ |
| 5044 | xfs_extnum_t lastx; /* last extent index used */ |
| 5045 | int logflags; /* transaction logging flags */ |
| 5046 | xfs_extlen_t mod; /* rt extent offset */ |
| 5047 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5048 | xfs_extnum_t nextents; /* number of file extents */ |
| 5049 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5051 | int tmp_logflags; /* partial logging flags */ |
| 5052 | int wasdel; /* was a delayed alloc extent */ |
| 5053 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | xfs_fsblock_t sum; |
| 5055 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5056 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5059 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5060 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5061 | if (unlikely( |
| 5062 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5063 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5064 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5065 | ip->i_mount); |
| 5066 | return XFS_ERROR(EFSCORRUPTED); |
| 5067 | } |
| 5068 | mp = ip->i_mount; |
| 5069 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5070 | return XFS_ERROR(EIO); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | ASSERT(len > 0); |
| 5073 | ASSERT(nexts >= 0); |
| 5074 | ASSERT(ifp->if_ext_max == |
| 5075 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5076 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5077 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5078 | return error; |
| 5079 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5080 | if (nextents == 0) { |
| 5081 | *done = 1; |
| 5082 | return 0; |
| 5083 | } |
| 5084 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5085 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | start = bno; |
| 5087 | bno = start + len - 1; |
| 5088 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5089 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | /* |
| 5092 | * Check to see if the given block number is past the end of the |
| 5093 | * file, back up to the last block if so... |
| 5094 | */ |
| 5095 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5096 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | xfs_bmbt_get_all(ep, &got); |
| 5098 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5099 | } |
| 5100 | logflags = 0; |
| 5101 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5102 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5103 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | cur->bc_private.b.firstblock = *firstblock; |
| 5105 | cur->bc_private.b.flist = flist; |
| 5106 | cur->bc_private.b.flags = 0; |
| 5107 | } else |
| 5108 | cur = NULL; |
| 5109 | extno = 0; |
| 5110 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5111 | (nexts == 0 || extno < nexts)) { |
| 5112 | /* |
| 5113 | * Is the found extent after a hole in which bno lives? |
| 5114 | * Just back up to the previous extent, if so. |
| 5115 | */ |
| 5116 | if (got.br_startoff > bno) { |
| 5117 | if (--lastx < 0) |
| 5118 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5119 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | xfs_bmbt_get_all(ep, &got); |
| 5121 | } |
| 5122 | /* |
| 5123 | * Is the last block of this extent before the range |
| 5124 | * we're supposed to delete? If so, we're done. |
| 5125 | */ |
| 5126 | bno = XFS_FILEOFF_MIN(bno, |
| 5127 | got.br_startoff + got.br_blockcount - 1); |
| 5128 | if (bno < start) |
| 5129 | break; |
| 5130 | /* |
| 5131 | * Then deal with the (possibly delayed) allocated space |
| 5132 | * we found. |
| 5133 | */ |
| 5134 | ASSERT(ep != NULL); |
| 5135 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5136 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | if (got.br_startoff < start) { |
| 5138 | del.br_startoff = start; |
| 5139 | del.br_blockcount -= start - got.br_startoff; |
| 5140 | if (!wasdel) |
| 5141 | del.br_startblock += start - got.br_startoff; |
| 5142 | } |
| 5143 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5144 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5145 | sum = del.br_startblock + del.br_blockcount; |
| 5146 | if (isrt && |
| 5147 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5148 | /* |
| 5149 | * Realtime extent not lined up at the end. |
| 5150 | * The extent could have been split into written |
| 5151 | * and unwritten pieces, or we could just be |
| 5152 | * unmapping part of it. But we can't really |
| 5153 | * get rid of part of a realtime extent. |
| 5154 | */ |
| 5155 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5156 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | /* |
| 5158 | * This piece is unwritten, or we're not |
| 5159 | * using unwritten extents. Skip over it. |
| 5160 | */ |
| 5161 | ASSERT(bno >= mod); |
| 5162 | bno -= mod > del.br_blockcount ? |
| 5163 | del.br_blockcount : mod; |
| 5164 | if (bno < got.br_startoff) { |
| 5165 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5166 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5167 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | } |
| 5169 | continue; |
| 5170 | } |
| 5171 | /* |
| 5172 | * It's written, turn it unwritten. |
| 5173 | * This is better than zeroing it. |
| 5174 | */ |
| 5175 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5176 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5177 | /* |
| 5178 | * If this spans a realtime extent boundary, |
| 5179 | * chop it back to the start of the one we end at. |
| 5180 | */ |
| 5181 | if (del.br_blockcount > mod) { |
| 5182 | del.br_startoff += del.br_blockcount - mod; |
| 5183 | del.br_startblock += del.br_blockcount - mod; |
| 5184 | del.br_blockcount = mod; |
| 5185 | } |
| 5186 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5187 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
| 5188 | &lastx, &cur, &del, firstblock, flist, |
| 5189 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | if (error) |
| 5191 | goto error0; |
| 5192 | goto nodelete; |
| 5193 | } |
| 5194 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5195 | /* |
| 5196 | * Realtime extent is lined up at the end but not |
| 5197 | * at the front. We'll get rid of full extents if |
| 5198 | * we can. |
| 5199 | */ |
| 5200 | mod = mp->m_sb.sb_rextsize - mod; |
| 5201 | if (del.br_blockcount > mod) { |
| 5202 | del.br_blockcount -= mod; |
| 5203 | del.br_startoff += mod; |
| 5204 | del.br_startblock += mod; |
| 5205 | } else if ((del.br_startoff == start && |
| 5206 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5207 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5208 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | /* |
| 5210 | * Can't make it unwritten. There isn't |
| 5211 | * a full extent here so just skip it. |
| 5212 | */ |
| 5213 | ASSERT(bno >= del.br_blockcount); |
| 5214 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5215 | if (got.br_startoff > bno) { |
| 5216 | if (--lastx >= 0) { |
| 5217 | ep = xfs_iext_get_ext(ifp, |
| 5218 | lastx); |
| 5219 | xfs_bmbt_get_all(ep, &got); |
| 5220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | } |
| 5222 | continue; |
| 5223 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5224 | /* |
| 5225 | * This one is already unwritten. |
| 5226 | * It must have a written left neighbor. |
| 5227 | * Unwrite the killed part of that one and |
| 5228 | * try again. |
| 5229 | */ |
| 5230 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5231 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5232 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5234 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | ASSERT(del.br_startblock == |
| 5236 | prev.br_startblock + prev.br_blockcount); |
| 5237 | if (prev.br_startoff < start) { |
| 5238 | mod = start - prev.br_startoff; |
| 5239 | prev.br_blockcount -= mod; |
| 5240 | prev.br_startblock += mod; |
| 5241 | prev.br_startoff = start; |
| 5242 | } |
| 5243 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5244 | lastx--; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5245 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5246 | ip, &lastx, &cur, &prev, |
| 5247 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | if (error) |
| 5249 | goto error0; |
| 5250 | goto nodelete; |
| 5251 | } else { |
| 5252 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5253 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606b | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5254 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5255 | ip, &lastx, &cur, &del, |
| 5256 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | if (error) |
| 5258 | goto error0; |
| 5259 | goto nodelete; |
| 5260 | } |
| 5261 | } |
| 5262 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5263 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5264 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5265 | if (isrt) { |
| 5266 | xfs_filblks_t rtexts; |
| 5267 | |
| 5268 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5269 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5270 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5271 | (int64_t)rtexts, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5272 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5273 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5274 | XFS_QMOPT_RES_RTBLKS); |
| 5275 | } else { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 5276 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5277 | (int64_t)del.br_blockcount, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5278 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5279 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | ip->i_delayed_blks -= del.br_blockcount; |
| 5283 | if (cur) |
| 5284 | cur->bc_private.b.flags |= |
| 5285 | XFS_BTCUR_BPRV_WASDEL; |
| 5286 | } else if (cur) |
| 5287 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5288 | /* |
| 5289 | * If it's the case where the directory code is running |
| 5290 | * with no block reservation, and the deleted block is in |
| 5291 | * the middle of its extent, and the resulting insert |
| 5292 | * of an extent would cause transformation to btree format, |
| 5293 | * then reject it. The calling code will then swap |
| 5294 | * blocks around instead. |
| 5295 | * We have to do this now, rather than waiting for the |
| 5296 | * conversion to btree format, since the transaction |
| 5297 | * will be dirty. |
| 5298 | */ |
| 5299 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5300 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5301 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5302 | del.br_startoff > got.br_startoff && |
| 5303 | del.br_startoff + del.br_blockcount < |
| 5304 | got.br_startoff + got.br_blockcount) { |
| 5305 | error = XFS_ERROR(ENOSPC); |
| 5306 | goto error0; |
| 5307 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5308 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5309 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | logflags |= tmp_logflags; |
| 5311 | if (error) |
| 5312 | goto error0; |
| 5313 | bno = del.br_startoff - 1; |
| 5314 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | /* |
| 5316 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5319 | if (lastx >= 0) { |
| 5320 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5321 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5322 | if (--lastx >= 0) |
| 5323 | ep = xfs_iext_get_ext(ifp, |
| 5324 | lastx); |
| 5325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | extno++; |
| 5329 | } |
| 5330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5332 | ASSERT(ifp->if_ext_max == |
| 5333 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5334 | /* |
| 5335 | * Convert to a btree if necessary. |
| 5336 | */ |
| 5337 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5338 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5339 | ASSERT(cur == NULL); |
| 5340 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5341 | &cur, 0, &tmp_logflags, whichfork); |
| 5342 | logflags |= tmp_logflags; |
| 5343 | if (error) |
| 5344 | goto error0; |
| 5345 | } |
| 5346 | /* |
| 5347 | * transform from btree to extents, give it cur |
| 5348 | */ |
| 5349 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5350 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5351 | ASSERT(cur != NULL); |
| 5352 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5353 | whichfork); |
| 5354 | logflags |= tmp_logflags; |
| 5355 | if (error) |
| 5356 | goto error0; |
| 5357 | } |
| 5358 | /* |
| 5359 | * transform from extents to local? |
| 5360 | */ |
| 5361 | ASSERT(ifp->if_ext_max == |
| 5362 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5363 | error = 0; |
| 5364 | error0: |
| 5365 | /* |
| 5366 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5367 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5369 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5371 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5372 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5374 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5375 | /* |
| 5376 | * Log inode even in the error case, if the transaction |
| 5377 | * is dirty we'll need to shut down the filesystem. |
| 5378 | */ |
| 5379 | if (logflags) |
| 5380 | xfs_trans_log_inode(tp, ip, logflags); |
| 5381 | if (cur) { |
| 5382 | if (!error) { |
| 5383 | *firstblock = cur->bc_private.b.firstblock; |
| 5384 | cur->bc_private.b.allocated = 0; |
| 5385 | } |
| 5386 | xfs_btree_del_cursor(cur, |
| 5387 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5388 | } |
| 5389 | return error; |
| 5390 | } |
| 5391 | |
| 5392 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5393 | * returns 1 for success, 0 if we failed to map the extent. |
| 5394 | */ |
| 5395 | STATIC int |
| 5396 | xfs_getbmapx_fix_eof_hole( |
| 5397 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5398 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5399 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5400 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5401 | __int64_t end, /* last block requested */ |
| 5402 | xfs_fsblock_t startblock) |
| 5403 | { |
| 5404 | __int64_t fixlen; |
| 5405 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5406 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5407 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5408 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5409 | |
| 5410 | if (startblock == HOLESTARTBLOCK) { |
| 5411 | mp = ip->i_mount; |
| 5412 | out->bmv_block = -1; |
| 5413 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5414 | fixlen -= out->bmv_offset; |
| 5415 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5416 | /* Came to hole at EOF. Trim it. */ |
| 5417 | if (fixlen <= 0) |
| 5418 | return 0; |
| 5419 | out->bmv_length = fixlen; |
| 5420 | } |
| 5421 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5422 | if (startblock == DELAYSTARTBLOCK) |
| 5423 | out->bmv_block = -2; |
| 5424 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5425 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5426 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5427 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5428 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5429 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5430 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | return 1; |
| 5434 | } |
| 5435 | |
| 5436 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5437 | * Get inode's extents as described in bmv, and format for output. |
| 5438 | * Calls formatter to fill the user's buffer until all extents |
| 5439 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5440 | * been filled, or until the formatter short-circuits the loop, |
| 5441 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | */ |
| 5443 | int /* error code */ |
| 5444 | xfs_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5445 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5446 | struct getbmapx *bmv, /* user bmap structure */ |
| 5447 | xfs_bmap_format_t formatter, /* format to user */ |
| 5448 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | { |
| 5450 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5451 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | __int64_t fixlen; /* length for -1 case */ |
| 5453 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | int lock; /* lock state */ |
| 5455 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5456 | xfs_mount_t *mp; /* file system mount point */ |
| 5457 | int nex; /* # of user extents can do */ |
| 5458 | int nexleft; /* # of user extents left */ |
| 5459 | int subnex; /* # of bmapi's can do */ |
| 5460 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5461 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | int whichfork; /* data or attr fork */ |
| 5463 | int prealloced; /* this is a file with |
| 5464 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5465 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5467 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5470 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5471 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | if (whichfork == XFS_ATTR_FORK) { |
| 5474 | if (XFS_IFORK_Q(ip)) { |
| 5475 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5476 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5477 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5478 | return XFS_ERROR(EINVAL); |
| 5479 | } else if (unlikely( |
| 5480 | ip->i_d.di_aformat != 0 && |
| 5481 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5482 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5483 | ip->i_mount); |
| 5484 | return XFS_ERROR(EFSCORRUPTED); |
| 5485 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5486 | |
| 5487 | prealloced = 0; |
| 5488 | fixlen = 1LL << 32; |
| 5489 | } else { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5490 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5491 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5492 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5493 | return XFS_ERROR(EINVAL); |
| 5494 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5495 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5496 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | prealloced = 1; |
| 5498 | fixlen = XFS_MAXIOFFSET(mp); |
| 5499 | } else { |
| 5500 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5501 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | if (bmv->bmv_length == -1) { |
| 5506 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5507 | bmv->bmv_length = |
| 5508 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5509 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | bmv->bmv_entries = 0; |
| 5511 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5512 | } else if (bmv->bmv_length < 0) { |
| 5513 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | nex = bmv->bmv_count - 1; |
| 5517 | if (nex <= 0) |
| 5518 | return XFS_ERROR(EINVAL); |
| 5519 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5520 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5521 | |
| 5522 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5523 | return XFS_ERROR(ENOMEM); |
| 5524 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 5525 | if (!out) |
| 5526 | return XFS_ERROR(ENOMEM); |
| 5527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5529 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 5530 | if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { |
| 5531 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5532 | if (error) |
| 5533 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5534 | } |
Dave Chinner | 309c848 | 2010-11-30 15:16:02 +1100 | [diff] [blame] | 5535 | /* |
| 5536 | * even after flushing the inode, there can still be delalloc |
| 5537 | * blocks on the inode beyond EOF due to speculative |
| 5538 | * preallocation. These are not removed until the release |
| 5539 | * function is called or the inode is inactivated. Hence we |
| 5540 | * cannot assert here that ip->i_delayed_blks == 0. |
| 5541 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | |
| 5544 | lock = xfs_ilock_map_shared(ip); |
| 5545 | |
| 5546 | /* |
| 5547 | * Don't let nex be bigger than the number of extents |
| 5548 | * we can have assuming alternating holes and real extents. |
| 5549 | */ |
| 5550 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5551 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5552 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5553 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5554 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5555 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5556 | |
| 5557 | /* |
| 5558 | * Allocate enough space to handle "subnex" maps at a time. |
| 5559 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5560 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5562 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5563 | if (!map) |
| 5564 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | |
| 5566 | bmv->bmv_entries = 0; |
| 5567 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5568 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5569 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5570 | error = 0; |
| 5571 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | nexleft = nex; |
| 5575 | |
| 5576 | do { |
| 5577 | nmap = (nexleft > subnex) ? subnex : nexleft; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 5578 | error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5579 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
| 5580 | map, &nmap, bmapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5582 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | ASSERT(nmap <= subnex); |
| 5584 | |
| 5585 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5586 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5587 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5588 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5589 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5590 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5591 | out[cur_ext].bmv_offset = |
| 5592 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5593 | out[cur_ext].bmv_length = |
| 5594 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5595 | out[cur_ext].bmv_unused1 = 0; |
| 5596 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5597 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5598 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5599 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5600 | whichfork == XFS_ATTR_FORK) { |
| 5601 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5602 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5603 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5605 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5606 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5607 | prealloced, bmvend, |
| 5608 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5609 | goto out_free_map; |
| 5610 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5611 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5612 | out[cur_ext].bmv_offset + |
| 5613 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5614 | bmv->bmv_length = |
| 5615 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 5616 | |
| 5617 | /* |
| 5618 | * In case we don't want to return the hole, |
| 5619 | * don't increase cur_ext so that we can reuse |
| 5620 | * it in the next loop. |
| 5621 | */ |
| 5622 | if ((iflags & BMV_IF_NO_HOLES) && |
| 5623 | map[i].br_startblock == HOLESTARTBLOCK) { |
| 5624 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); |
| 5625 | continue; |
| 5626 | } |
| 5627 | |
| 5628 | nexleft--; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5629 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5630 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | } |
| 5632 | } while (nmap && nexleft && bmv->bmv_length); |
| 5633 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5634 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5635 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5636 | out_unlock_ilock: |
| 5637 | xfs_iunlock_map_shared(ip, lock); |
| 5638 | out_unlock_iolock: |
| 5639 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5640 | |
| 5641 | for (i = 0; i < cur_ext; i++) { |
| 5642 | int full = 0; /* user array is full */ |
| 5643 | |
| 5644 | /* format results & advance arg */ |
| 5645 | error = formatter(&arg, &out[i], &full); |
| 5646 | if (error || full) |
| 5647 | break; |
| 5648 | } |
| 5649 | |
Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5650 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | return error; |
| 5652 | } |
| 5653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | #ifdef DEBUG |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5655 | STATIC struct xfs_buf * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | xfs_bmap_get_bp( |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5657 | struct xfs_btree_cur *cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | xfs_fsblock_t bno) |
| 5659 | { |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5660 | struct xfs_log_item_desc *lidp; |
| 5661 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | |
| 5663 | if (!cur) |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5664 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5665 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5666 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5667 | if (!cur->bc_bufs[i]) |
| 5668 | break; |
| 5669 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 5670 | return cur->bc_bufs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5673 | /* Chase down all the log items to see if the bp is there */ |
| 5674 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5675 | struct xfs_buf_log_item *bip; |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5676 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 5677 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 5678 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 5679 | return bip->bli_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | } |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5681 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5682 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | } |
| 5684 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5685 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5687 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | xfs_mount_t *mp, |
| 5689 | int root, |
| 5690 | short sz) |
| 5691 | { |
| 5692 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5693 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | xfs_bmbt_key_t *prevp, *keyp; |
| 5695 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5696 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | |
| 5698 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5699 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5701 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | |
| 5703 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5704 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5705 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | } |
| 5707 | prevp = keyp; |
| 5708 | |
| 5709 | /* |
| 5710 | * Compare the block numbers to see if there are dups. |
| 5711 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5712 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5713 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5714 | else |
| 5715 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5716 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5717 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5718 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5719 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5720 | else |
| 5721 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5722 | if (*thispa == *pp) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5723 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5724 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5725 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5727 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | } |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | /* |
| 5734 | * Check that the extents for the inode ip are in the right order in all |
| 5735 | * btree leaves. |
| 5736 | */ |
| 5737 | |
| 5738 | STATIC void |
| 5739 | xfs_bmap_check_leaf_extents( |
| 5740 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5741 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5742 | int whichfork) /* data or attr fork */ |
| 5743 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5744 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5746 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5747 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5748 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | xfs_ifork_t *ifp; /* fork structure */ |
| 5750 | int level; /* btree level, for checking */ |
| 5751 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5752 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5753 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5754 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5755 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | int bp_release = 0; |
| 5757 | |
| 5758 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5759 | return; |
| 5760 | } |
| 5761 | |
| 5762 | bno = NULLFSBLOCK; |
| 5763 | mp = ip->i_mount; |
| 5764 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5765 | block = ifp->if_broot; |
| 5766 | /* |
| 5767 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5768 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5769 | level = be16_to_cpu(block->bb_level); |
| 5770 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5772 | 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] | 5773 | bno = be64_to_cpu(*pp); |
| 5774 | |
| 5775 | ASSERT(bno != NULLDFSBNO); |
| 5776 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5777 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | /* |
| 5780 | * Go down the tree until leaf level is reached, following the first |
| 5781 | * pointer (leftmost) at each level. |
| 5782 | */ |
| 5783 | while (level-- > 0) { |
| 5784 | /* See if buf is in cur first */ |
| 5785 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5786 | if (bp) { |
| 5787 | bp_release = 0; |
| 5788 | } else { |
| 5789 | bp_release = 1; |
| 5790 | } |
| 5791 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5792 | XFS_BMAP_BTREE_REF))) |
| 5793 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5794 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5796 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | error0); |
| 5798 | if (level == 0) |
| 5799 | break; |
| 5800 | |
| 5801 | /* |
| 5802 | * Check this block for basic sanity (increasing keys and |
| 5803 | * no duplicate blocks). |
| 5804 | */ |
| 5805 | |
| 5806 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5807 | 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] | 5808 | bno = be64_to_cpu(*pp); |
| 5809 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | if (bp_release) { |
| 5811 | bp_release = 0; |
| 5812 | xfs_trans_brelse(NULL, bp); |
| 5813 | } |
| 5814 | } |
| 5815 | |
| 5816 | /* |
| 5817 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 5818 | */ |
| 5819 | i = 0; |
| 5820 | |
| 5821 | /* |
| 5822 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 5823 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | xfs_fsblock_t nextbno; |
| 5826 | xfs_extnum_t num_recs; |
| 5827 | |
| 5828 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5829 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | |
| 5831 | /* |
| 5832 | * Read-ahead the next leaf block, if any. |
| 5833 | */ |
| 5834 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5835 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | |
| 5837 | /* |
| 5838 | * Check all the extents to make sure they are OK. |
| 5839 | * If we had a previous block, the last entry should |
| 5840 | * conform with the first entry in this one. |
| 5841 | */ |
| 5842 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5843 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5844 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5845 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 5846 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 5847 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5848 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5849 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5850 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5851 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 5852 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 5853 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5854 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5857 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | i += num_recs; |
| 5859 | if (bp_release) { |
| 5860 | bp_release = 0; |
| 5861 | xfs_trans_brelse(NULL, bp); |
| 5862 | } |
| 5863 | bno = nextbno; |
| 5864 | /* |
| 5865 | * If we've reached the end, stop. |
| 5866 | */ |
| 5867 | if (bno == NULLFSBLOCK) |
| 5868 | break; |
| 5869 | |
| 5870 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5871 | if (bp) { |
| 5872 | bp_release = 0; |
| 5873 | } else { |
| 5874 | bp_release = 1; |
| 5875 | } |
| 5876 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5877 | XFS_BMAP_BTREE_REF))) |
| 5878 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5879 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | } |
| 5881 | if (bp_release) { |
| 5882 | bp_release = 0; |
| 5883 | xfs_trans_brelse(NULL, bp); |
| 5884 | } |
| 5885 | return; |
| 5886 | |
| 5887 | error0: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5888 | xfs_warn(mp, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | if (bp_release) |
| 5890 | xfs_trans_brelse(NULL, bp); |
| 5891 | error_norelse: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5892 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5893 | __func__, i); |
| 5894 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | return; |
| 5896 | } |
| 5897 | #endif |
| 5898 | |
| 5899 | /* |
| 5900 | * Count fsblocks of the given fork. |
| 5901 | */ |
| 5902 | int /* error */ |
| 5903 | xfs_bmap_count_blocks( |
| 5904 | xfs_trans_t *tp, /* transaction pointer */ |
| 5905 | xfs_inode_t *ip, /* incore inode */ |
| 5906 | int whichfork, /* data or attr fork */ |
| 5907 | int *count) /* out: count of blocks */ |
| 5908 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5909 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5911 | xfs_ifork_t *ifp; /* fork structure */ |
| 5912 | int level; /* btree level, for checking */ |
| 5913 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5914 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | |
| 5916 | bno = NULLFSBLOCK; |
| 5917 | mp = ip->i_mount; |
| 5918 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5919 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5920 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5922 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | /* |
| 5927 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5928 | */ |
| 5929 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5930 | level = be16_to_cpu(block->bb_level); |
| 5931 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5932 | 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] | 5933 | bno = be64_to_cpu(*pp); |
| 5934 | ASSERT(bno != NULLDFSBNO); |
| 5935 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5936 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5938 | 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] | 5939 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 5940 | mp); |
| 5941 | return XFS_ERROR(EFSCORRUPTED); |
| 5942 | } |
| 5943 | |
| 5944 | return 0; |
| 5945 | } |
| 5946 | |
| 5947 | /* |
| 5948 | * Recursively walks each level of a btree |
| 5949 | * to count total fsblocks is use. |
| 5950 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 5951 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | xfs_bmap_count_tree( |
| 5953 | xfs_mount_t *mp, /* file system mount point */ |
| 5954 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5955 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | xfs_fsblock_t blockno, /* file system block number */ |
| 5957 | int levelin, /* level in btree */ |
| 5958 | int *count) /* Count of blocks */ |
| 5959 | { |
| 5960 | int error; |
| 5961 | xfs_buf_t *bp, *nbp; |
| 5962 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5963 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | xfs_fsblock_t bno = blockno; |
| 5965 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5966 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | |
| 5969 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 5970 | return error; |
| 5971 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5972 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | |
| 5974 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 5975 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5976 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | while (nextbno != NULLFSBLOCK) { |
| 5978 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 5979 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 5980 | return error; |
| 5981 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5982 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 5983 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | xfs_trans_brelse(tp, nbp); |
| 5985 | } |
| 5986 | |
| 5987 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5988 | 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] | 5989 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5991 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | xfs_trans_brelse(tp, bp); |
| 5993 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 5994 | XFS_ERRLEVEL_LOW, mp); |
| 5995 | return XFS_ERROR(EFSCORRUPTED); |
| 5996 | } |
| 5997 | xfs_trans_brelse(tp, bp); |
| 5998 | } else { |
| 5999 | /* count all level 1 nodes and their leaves */ |
| 6000 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6001 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6002 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6003 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | xfs_trans_brelse(tp, bp); |
| 6005 | if (nextbno == NULLFSBLOCK) |
| 6006 | break; |
| 6007 | bno = nextbno; |
| 6008 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6009 | XFS_BMAP_BTREE_REF))) |
| 6010 | return error; |
| 6011 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6012 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | } |
| 6014 | } |
| 6015 | return 0; |
| 6016 | } |
| 6017 | |
| 6018 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6019 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6021 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6023 | xfs_ifork_t *ifp, |
| 6024 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | int numrecs, |
| 6026 | int *count) |
| 6027 | { |
| 6028 | int b; |
| 6029 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6030 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6031 | 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] | 6032 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6033 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6037 | * Count leaf blocks given a range of extent records originally |
| 6038 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6039 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6040 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6041 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6042 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6043 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6044 | int numrecs, |
| 6045 | int *count) |
| 6046 | { |
| 6047 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6048 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6049 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6050 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6051 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | } |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6055 | |
| 6056 | /* |
| 6057 | * dead simple method of punching delalyed allocation blocks from a range in |
| 6058 | * the inode. Walks a block at a time so will be slow, but is only executed in |
| 6059 | * rare error cases so the overhead is not critical. This will alays punch out |
| 6060 | * both the start and end blocks, even if the ranges only partially overlap |
| 6061 | * them, so it is up to the caller to ensure that partial blocks are not |
| 6062 | * passed in. |
| 6063 | */ |
| 6064 | int |
| 6065 | xfs_bmap_punch_delalloc_range( |
| 6066 | struct xfs_inode *ip, |
| 6067 | xfs_fileoff_t start_fsb, |
| 6068 | xfs_fileoff_t length) |
| 6069 | { |
| 6070 | xfs_fileoff_t remaining = length; |
| 6071 | int error = 0; |
| 6072 | |
| 6073 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 6074 | |
| 6075 | do { |
| 6076 | int done; |
| 6077 | xfs_bmbt_irec_t imap; |
| 6078 | int nimaps = 1; |
| 6079 | xfs_fsblock_t firstblock; |
| 6080 | xfs_bmap_free_t flist; |
| 6081 | |
| 6082 | /* |
| 6083 | * Map the range first and check that it is a delalloc extent |
| 6084 | * before trying to unmap the range. Otherwise we will be |
| 6085 | * trying to remove a real extent (which requires a |
| 6086 | * transaction) or a hole, which is probably a bad idea... |
| 6087 | */ |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 6088 | error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, |
| 6089 | XFS_BMAPI_ENTIRE); |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6090 | |
| 6091 | if (error) { |
| 6092 | /* something screwed, just bail */ |
| 6093 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 6094 | xfs_alert(ip->i_mount, |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6095 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
| 6096 | ip->i_ino, start_fsb); |
| 6097 | } |
| 6098 | break; |
| 6099 | } |
| 6100 | if (!nimaps) { |
| 6101 | /* nothing there */ |
| 6102 | goto next_block; |
| 6103 | } |
| 6104 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 6105 | /* been converted, ignore */ |
| 6106 | goto next_block; |
| 6107 | } |
| 6108 | WARN_ON(imap.br_blockcount == 0); |
| 6109 | |
| 6110 | /* |
| 6111 | * Note: while we initialise the firstblock/flist pair, they |
| 6112 | * should never be used because blocks should never be |
| 6113 | * allocated or freed for a delalloc extent and hence we need |
| 6114 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 6115 | */ |
| 6116 | xfs_bmap_init(&flist, &firstblock); |
| 6117 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, |
| 6118 | &flist, &done); |
| 6119 | if (error) |
| 6120 | break; |
| 6121 | |
| 6122 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 6123 | next_block: |
| 6124 | start_fsb++; |
| 6125 | remaining--; |
| 6126 | } while(remaining > 0); |
| 6127 | |
| 6128 | return error; |
| 6129 | } |