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_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_btree.h" |
| 37 | #include "xfs_dmapi.h" |
| 38 | #include "xfs_mount.h" |
| 39 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_itable.h" |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 41 | #include "xfs_dir2_data.h" |
| 42 | #include "xfs_dir2_leaf.h" |
| 43 | #include "xfs_dir2_block.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 44 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_extfree_item.h" |
| 46 | #include "xfs_alloc.h" |
| 47 | #include "xfs_bmap.h" |
| 48 | #include "xfs_rtalloc.h" |
| 49 | #include "xfs_error.h" |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 50 | #include "xfs_attr_leaf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "xfs_rw.h" |
| 52 | #include "xfs_quota.h" |
| 53 | #include "xfs_trans_space.h" |
| 54 | #include "xfs_buf_item.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 55 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 56 | #include "xfs_vnodeops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | #ifdef DEBUG |
| 60 | STATIC void |
| 61 | xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork); |
| 62 | #endif |
| 63 | |
| 64 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 65 | |
| 66 | /* |
| 67 | * Prototypes for internal bmap routines. |
| 68 | */ |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 73 | */ |
| 74 | STATIC int /* error */ |
| 75 | xfs_bmap_add_attrfork_extents( |
| 76 | xfs_trans_t *tp, /* transaction pointer */ |
| 77 | xfs_inode_t *ip, /* incore inode pointer */ |
| 78 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 79 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 80 | int *flags); /* inode logging flags */ |
| 81 | |
| 82 | /* |
| 83 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 84 | */ |
| 85 | STATIC int /* error */ |
| 86 | xfs_bmap_add_attrfork_local( |
| 87 | xfs_trans_t *tp, /* transaction pointer */ |
| 88 | xfs_inode_t *ip, /* incore inode pointer */ |
| 89 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 90 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 91 | int *flags); /* inode logging flags */ |
| 92 | |
| 93 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 94 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * after allocating space (or doing a delayed allocation). |
| 96 | */ |
| 97 | STATIC int /* error */ |
| 98 | xfs_bmap_add_extent( |
| 99 | xfs_inode_t *ip, /* incore inode pointer */ |
| 100 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 101 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 102 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 104 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 105 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 106 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | int whichfork, /* data or attr fork */ |
| 108 | int rsvd); /* OK to allocate reserved blocks */ |
| 109 | |
| 110 | /* |
| 111 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 112 | * allocation to a real allocation. |
| 113 | */ |
| 114 | STATIC int /* error */ |
| 115 | xfs_bmap_add_extent_delay_real( |
| 116 | xfs_inode_t *ip, /* incore inode pointer */ |
| 117 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 118 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 119 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 121 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 122 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 123 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 124 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | int rsvd); /* OK to allocate reserved blocks */ |
| 126 | |
| 127 | /* |
| 128 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 129 | * to a delayed allocation. |
| 130 | */ |
| 131 | STATIC int /* error */ |
| 132 | xfs_bmap_add_extent_hole_delay( |
| 133 | xfs_inode_t *ip, /* incore inode pointer */ |
| 134 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 135 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 137 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | int rsvd); /* OK to allocate reserved blocks */ |
| 139 | |
| 140 | /* |
| 141 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 142 | * to a real allocation. |
| 143 | */ |
| 144 | STATIC int /* error */ |
| 145 | xfs_bmap_add_extent_hole_real( |
| 146 | xfs_inode_t *ip, /* incore inode pointer */ |
| 147 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 148 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 149 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 151 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | int whichfork); /* data or attr fork */ |
| 153 | |
| 154 | /* |
| 155 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 156 | * allocation to a real allocation or vice versa. |
| 157 | */ |
| 158 | STATIC int /* error */ |
| 159 | xfs_bmap_add_extent_unwritten_real( |
| 160 | xfs_inode_t *ip, /* incore inode pointer */ |
| 161 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 162 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 163 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 164 | int *logflagsp, /* inode logging flags */ |
| 165 | xfs_extdelta_t *delta); /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 169 | * It figures out where to ask the underlying allocator to put the new extent. |
| 170 | */ |
| 171 | STATIC int /* error */ |
| 172 | xfs_bmap_alloc( |
| 173 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 174 | |
| 175 | /* |
| 176 | * Transform a btree format file with only one leaf node, where the |
| 177 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 178 | * 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] | 179 | * give up the space for the btree root and pitch the leaf block. |
| 180 | */ |
| 181 | STATIC int /* error */ |
| 182 | xfs_bmap_btree_to_extents( |
| 183 | xfs_trans_t *tp, /* transaction pointer */ |
| 184 | xfs_inode_t *ip, /* incore inode pointer */ |
| 185 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 186 | int *logflagsp, /* inode logging flags */ |
| 187 | int whichfork); /* data or attr fork */ |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 190 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * after removing space (or undoing a delayed allocation). |
| 192 | */ |
| 193 | STATIC int /* error */ |
| 194 | xfs_bmap_del_extent( |
| 195 | xfs_inode_t *ip, /* incore inode pointer */ |
| 196 | xfs_trans_t *tp, /* current trans pointer */ |
| 197 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 198 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 199 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 200 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 202 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | int whichfork, /* data or attr fork */ |
| 204 | int rsvd); /* OK to allocate reserved blocks */ |
| 205 | |
| 206 | /* |
| 207 | * Remove the entry "free" from the free item list. Prev points to the |
| 208 | * previous entry, unless "free" is the head of the list. |
| 209 | */ |
| 210 | STATIC void |
| 211 | xfs_bmap_del_free( |
| 212 | xfs_bmap_free_t *flist, /* free item list header */ |
| 213 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 214 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 215 | |
| 216 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | * Convert an extents-format file into a btree-format file. |
| 218 | * The new file will have a root block (in the inode) and a single child block. |
| 219 | */ |
| 220 | STATIC int /* error */ |
| 221 | xfs_bmap_extents_to_btree( |
| 222 | xfs_trans_t *tp, /* transaction pointer */ |
| 223 | xfs_inode_t *ip, /* incore inode pointer */ |
| 224 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 225 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 226 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 227 | int wasdel, /* converting a delayed alloc */ |
| 228 | int *logflagsp, /* inode logging flags */ |
| 229 | int whichfork); /* data or attr fork */ |
| 230 | |
| 231 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | * Convert a local file to an extents file. |
| 233 | * This code is sort of bogus, since the file data needs to get |
| 234 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 235 | */ |
| 236 | STATIC int /* error */ |
| 237 | xfs_bmap_local_to_extents( |
| 238 | xfs_trans_t *tp, /* transaction pointer */ |
| 239 | xfs_inode_t *ip, /* incore inode pointer */ |
| 240 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 241 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 242 | int *logflagsp, /* inode logging flags */ |
| 243 | int whichfork); /* data or attr fork */ |
| 244 | |
| 245 | /* |
| 246 | * Search the extents list for the inode, for the extent containing bno. |
| 247 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 248 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 249 | * Else, *lastxp will be set to the index of the found |
| 250 | * entry; *gotp will contain the entry. |
| 251 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 252 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | xfs_bmap_search_extents( |
| 254 | xfs_inode_t *ip, /* incore inode pointer */ |
| 255 | xfs_fileoff_t bno, /* block number searched for */ |
| 256 | int whichfork, /* data or attr fork */ |
| 257 | int *eofp, /* out: end of file found */ |
| 258 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 259 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 260 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 261 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 262 | /* |
| 263 | * Check the last inode extent to determine whether this allocation will result |
| 264 | * in blocks being allocated at the end of the file. When we allocate new data |
| 265 | * blocks at the end of the file which do not start at the previous data block, |
| 266 | * we will try to align the new blocks at stripe unit boundaries. |
| 267 | */ |
| 268 | STATIC int /* error */ |
| 269 | xfs_bmap_isaeof( |
| 270 | xfs_inode_t *ip, /* incore inode pointer */ |
| 271 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 272 | int whichfork, /* data or attribute fork */ |
| 273 | char *aeof); /* return value */ |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | #ifdef XFS_BMAP_TRACE |
| 276 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 277 | * Add bmap trace entry prior to a call to xfs_iext_remove. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | */ |
| 279 | STATIC void |
| 280 | xfs_bmap_trace_delete( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 281 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | char *desc, /* operation description */ |
| 283 | xfs_inode_t *ip, /* incore inode pointer */ |
| 284 | xfs_extnum_t idx, /* index of entry(entries) deleted */ |
| 285 | xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */ |
| 286 | int whichfork); /* data or attr fork */ |
| 287 | |
| 288 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 289 | * Add bmap trace entry prior to a call to xfs_iext_insert, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | * reading in the extents list from the disk (in the btree). |
| 291 | */ |
| 292 | STATIC void |
| 293 | xfs_bmap_trace_insert( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 294 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | char *desc, /* operation description */ |
| 296 | xfs_inode_t *ip, /* incore inode pointer */ |
| 297 | xfs_extnum_t idx, /* index of entry(entries) inserted */ |
| 298 | xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */ |
| 299 | xfs_bmbt_irec_t *r1, /* inserted record 1 */ |
| 300 | xfs_bmbt_irec_t *r2, /* inserted record 2 or null */ |
| 301 | int whichfork); /* data or attr fork */ |
| 302 | |
| 303 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 304 | * Add bmap trace entry after updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | */ |
| 306 | STATIC void |
| 307 | xfs_bmap_trace_post_update( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 308 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | char *desc, /* operation description */ |
| 310 | xfs_inode_t *ip, /* incore inode pointer */ |
| 311 | xfs_extnum_t idx, /* index of entry updated */ |
| 312 | int whichfork); /* data or attr fork */ |
| 313 | |
| 314 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 315 | * Add bmap trace entry prior to updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | */ |
| 317 | STATIC void |
| 318 | xfs_bmap_trace_pre_update( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 319 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | char *desc, /* operation description */ |
| 321 | xfs_inode_t *ip, /* incore inode pointer */ |
| 322 | xfs_extnum_t idx, /* index of entry to be updated */ |
| 323 | int whichfork); /* data or attr fork */ |
| 324 | |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 325 | #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) \ |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 326 | xfs_bmap_trace_delete(__func__,d,ip,i,c,w) |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 327 | #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) \ |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 328 | xfs_bmap_trace_insert(__func__,d,ip,i,c,r1,r2,w) |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 329 | #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) \ |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 330 | xfs_bmap_trace_post_update(__func__,d,ip,i,w) |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 331 | #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) \ |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 332 | xfs_bmap_trace_pre_update(__func__,d,ip,i,w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | #else |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 334 | #define XFS_BMAP_TRACE_DELETE(d,ip,i,c,w) |
| 335 | #define XFS_BMAP_TRACE_INSERT(d,ip,i,c,r1,r2,w) |
| 336 | #define XFS_BMAP_TRACE_POST_UPDATE(d,ip,i,w) |
| 337 | #define XFS_BMAP_TRACE_PRE_UPDATE(d,ip,i,w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | #endif /* XFS_BMAP_TRACE */ |
| 339 | |
| 340 | /* |
| 341 | * Compute the worst-case number of indirect blocks that will be used |
| 342 | * for ip's delayed extent of length "len". |
| 343 | */ |
| 344 | STATIC xfs_filblks_t |
| 345 | xfs_bmap_worst_indlen( |
| 346 | xfs_inode_t *ip, /* incore inode pointer */ |
| 347 | xfs_filblks_t len); /* delayed extent length */ |
| 348 | |
| 349 | #ifdef DEBUG |
| 350 | /* |
| 351 | * Perform various validation checks on the values being returned |
| 352 | * from xfs_bmapi(). |
| 353 | */ |
| 354 | STATIC void |
| 355 | xfs_bmap_validate_ret( |
| 356 | xfs_fileoff_t bno, |
| 357 | xfs_filblks_t len, |
| 358 | int flags, |
| 359 | xfs_bmbt_irec_t *mval, |
| 360 | int nmap, |
| 361 | int ret_nmap); |
| 362 | #else |
| 363 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 364 | #endif /* DEBUG */ |
| 365 | |
| 366 | #if defined(XFS_RW_TRACE) |
| 367 | STATIC void |
| 368 | xfs_bunmap_trace( |
| 369 | xfs_inode_t *ip, |
| 370 | xfs_fileoff_t bno, |
| 371 | xfs_filblks_t len, |
| 372 | int flags, |
| 373 | inst_t *ra); |
| 374 | #else |
| 375 | #define xfs_bunmap_trace(ip, bno, len, flags, ra) |
| 376 | #endif /* XFS_RW_TRACE */ |
| 377 | |
| 378 | STATIC int |
| 379 | xfs_bmap_count_tree( |
| 380 | xfs_mount_t *mp, |
| 381 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 382 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | xfs_fsblock_t blockno, |
| 384 | int levelin, |
| 385 | int *count); |
| 386 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 387 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 389 | xfs_ifork_t *ifp, |
| 390 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | int numrecs, |
| 392 | int *count); |
| 393 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 394 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 395 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 396 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 397 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 398 | int numrecs, |
| 399 | int *count); |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Bmap internal routines. |
| 403 | */ |
| 404 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 405 | STATIC int /* error */ |
| 406 | xfs_bmbt_lookup_eq( |
| 407 | struct xfs_btree_cur *cur, |
| 408 | xfs_fileoff_t off, |
| 409 | xfs_fsblock_t bno, |
| 410 | xfs_filblks_t len, |
| 411 | int *stat) /* success/failure */ |
| 412 | { |
| 413 | cur->bc_rec.b.br_startoff = off; |
| 414 | cur->bc_rec.b.br_startblock = bno; |
| 415 | cur->bc_rec.b.br_blockcount = len; |
| 416 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 417 | } |
| 418 | |
| 419 | STATIC int /* error */ |
| 420 | xfs_bmbt_lookup_ge( |
| 421 | struct xfs_btree_cur *cur, |
| 422 | xfs_fileoff_t off, |
| 423 | xfs_fsblock_t bno, |
| 424 | xfs_filblks_t len, |
| 425 | int *stat) /* success/failure */ |
| 426 | { |
| 427 | cur->bc_rec.b.br_startoff = off; |
| 428 | cur->bc_rec.b.br_startblock = bno; |
| 429 | cur->bc_rec.b.br_blockcount = len; |
| 430 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 431 | } |
| 432 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 433 | /* |
| 434 | * Update the record referred to by cur to the value given |
| 435 | * by [off, bno, len, state]. |
| 436 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 437 | */ |
| 438 | STATIC int |
| 439 | xfs_bmbt_update( |
| 440 | struct xfs_btree_cur *cur, |
| 441 | xfs_fileoff_t off, |
| 442 | xfs_fsblock_t bno, |
| 443 | xfs_filblks_t len, |
| 444 | xfs_exntst_t state) |
| 445 | { |
| 446 | union xfs_btree_rec rec; |
| 447 | |
| 448 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 449 | return xfs_btree_update(cur, &rec); |
| 450 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* |
| 453 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 454 | */ |
| 455 | STATIC int /* error */ |
| 456 | xfs_bmap_add_attrfork_btree( |
| 457 | xfs_trans_t *tp, /* transaction pointer */ |
| 458 | xfs_inode_t *ip, /* incore inode pointer */ |
| 459 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 460 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 461 | int *flags) /* inode logging flags */ |
| 462 | { |
| 463 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 464 | int error; /* error return value */ |
| 465 | xfs_mount_t *mp; /* file system mount struct */ |
| 466 | int stat; /* newroot status */ |
| 467 | |
| 468 | mp = ip->i_mount; |
| 469 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 470 | *flags |= XFS_ILOG_DBROOT; |
| 471 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 472 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | cur->bc_private.b.flist = flist; |
| 474 | cur->bc_private.b.firstblock = *firstblock; |
| 475 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 476 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 477 | /* must be at least one entry */ |
| 478 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 479 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | goto error0; |
| 481 | if (stat == 0) { |
| 482 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 483 | return XFS_ERROR(ENOSPC); |
| 484 | } |
| 485 | *firstblock = cur->bc_private.b.firstblock; |
| 486 | cur->bc_private.b.allocated = 0; |
| 487 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 488 | } |
| 489 | return 0; |
| 490 | error0: |
| 491 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 492 | return error; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 497 | */ |
| 498 | STATIC int /* error */ |
| 499 | xfs_bmap_add_attrfork_extents( |
| 500 | xfs_trans_t *tp, /* transaction pointer */ |
| 501 | xfs_inode_t *ip, /* incore inode pointer */ |
| 502 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 503 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 504 | int *flags) /* inode logging flags */ |
| 505 | { |
| 506 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 507 | int error; /* error return value */ |
| 508 | |
| 509 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 510 | return 0; |
| 511 | cur = NULL; |
| 512 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 513 | flags, XFS_DATA_FORK); |
| 514 | if (cur) { |
| 515 | cur->bc_private.b.allocated = 0; |
| 516 | xfs_btree_del_cursor(cur, |
| 517 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 518 | } |
| 519 | return error; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 524 | */ |
| 525 | STATIC int /* error */ |
| 526 | xfs_bmap_add_attrfork_local( |
| 527 | xfs_trans_t *tp, /* transaction pointer */ |
| 528 | xfs_inode_t *ip, /* incore inode pointer */ |
| 529 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 530 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 531 | int *flags) /* inode logging flags */ |
| 532 | { |
| 533 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 534 | int error; /* error return value */ |
| 535 | xfs_mount_t *mp; /* mount structure pointer */ |
| 536 | |
| 537 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 538 | return 0; |
| 539 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 540 | mp = ip->i_mount; |
| 541 | memset(&dargs, 0, sizeof(dargs)); |
| 542 | dargs.dp = ip; |
| 543 | dargs.firstblock = firstblock; |
| 544 | dargs.flist = flist; |
| 545 | dargs.total = mp->m_dirblkfsbs; |
| 546 | dargs.whichfork = XFS_DATA_FORK; |
| 547 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 548 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } else |
| 550 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 551 | XFS_DATA_FORK); |
| 552 | return error; |
| 553 | } |
| 554 | |
| 555 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 556 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * after allocating space (or doing a delayed allocation). |
| 558 | */ |
| 559 | STATIC int /* error */ |
| 560 | xfs_bmap_add_extent( |
| 561 | xfs_inode_t *ip, /* incore inode pointer */ |
| 562 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 563 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 564 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 566 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 567 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 568 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | int whichfork, /* data or attr fork */ |
| 570 | int rsvd) /* OK to use reserved data blocks */ |
| 571 | { |
| 572 | xfs_btree_cur_t *cur; /* btree cursor or null */ |
| 573 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 574 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 575 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | xfs_ifork_t *ifp; /* inode fork ptr */ |
| 577 | int logflags; /* returned value */ |
| 578 | xfs_extnum_t nextents; /* number of extents in file now */ |
| 579 | |
| 580 | XFS_STATS_INC(xs_add_exlist); |
| 581 | cur = *curp; |
| 582 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 583 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 584 | ASSERT(idx <= nextents); |
| 585 | da_old = da_new = 0; |
| 586 | error = 0; |
| 587 | /* |
| 588 | * This is the first extent added to a new/empty file. |
| 589 | * Special case this one, so other routines get to assume there are |
| 590 | * already extents in the list. |
| 591 | */ |
| 592 | if (nextents == 0) { |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 593 | XFS_BMAP_TRACE_INSERT("insert empty", ip, 0, 1, new, NULL, |
| 594 | whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 595 | xfs_iext_insert(ifp, 0, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | ASSERT(cur == NULL); |
| 597 | ifp->if_lastex = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 598 | if (!isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 600 | logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } else |
| 602 | logflags = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 603 | /* DELTA: single new extent */ |
| 604 | if (delta) { |
| 605 | if (delta->xed_startoff > new->br_startoff) |
| 606 | delta->xed_startoff = new->br_startoff; |
| 607 | if (delta->xed_blockcount < |
| 608 | new->br_startoff + new->br_blockcount) |
| 609 | delta->xed_blockcount = new->br_startoff + |
| 610 | new->br_blockcount; |
| 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } |
| 613 | /* |
| 614 | * Any kind of new delayed allocation goes here. |
| 615 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 616 | else if (isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | if (cur) |
| 618 | ASSERT((cur->bc_private.b.flags & |
| 619 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 620 | if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 621 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | goto done; |
| 623 | } |
| 624 | /* |
| 625 | * Real allocation off the end of the file. |
| 626 | */ |
| 627 | else if (idx == nextents) { |
| 628 | if (cur) |
| 629 | ASSERT((cur->bc_private.b.flags & |
| 630 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 631 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 632 | &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | goto done; |
| 634 | } else { |
| 635 | xfs_bmbt_irec_t prev; /* old extent at offset idx */ |
| 636 | |
| 637 | /* |
| 638 | * Get the record referred to by idx. |
| 639 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 640 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | /* |
| 642 | * If it's a real allocation record, and the new allocation ends |
| 643 | * after the start of the referred to record, then we're filling |
| 644 | * in a delayed or unwritten allocation with a real one, or |
| 645 | * converting real back to unwritten. |
| 646 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 647 | if (!isnullstartblock(new->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | new->br_startoff + new->br_blockcount > prev.br_startoff) { |
| 649 | if (prev.br_state != XFS_EXT_UNWRITTEN && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 650 | isnullstartblock(prev.br_startblock)) { |
| 651 | da_old = startblockval(prev.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (cur) |
| 653 | ASSERT(cur->bc_private.b.flags & |
| 654 | XFS_BTCUR_BPRV_WASDEL); |
| 655 | if ((error = xfs_bmap_add_extent_delay_real(ip, |
| 656 | idx, &cur, new, &da_new, first, flist, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 657 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | goto done; |
| 659 | } else if (new->br_state == XFS_EXT_NORM) { |
| 660 | ASSERT(new->br_state == XFS_EXT_NORM); |
| 661 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 662 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | goto done; |
| 664 | } else { |
| 665 | ASSERT(new->br_state == XFS_EXT_UNWRITTEN); |
| 666 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 667 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | goto done; |
| 669 | } |
| 670 | ASSERT(*curp == cur || *curp == NULL); |
| 671 | } |
| 672 | /* |
| 673 | * Otherwise we're filling in a hole with an allocation. |
| 674 | */ |
| 675 | else { |
| 676 | if (cur) |
| 677 | ASSERT((cur->bc_private.b.flags & |
| 678 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 679 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 680 | new, &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | goto done; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | ASSERT(*curp == cur || *curp == NULL); |
| 686 | /* |
| 687 | * Convert to a btree if necessary. |
| 688 | */ |
| 689 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 690 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 691 | int tmp_logflags; /* partial log flag return val */ |
| 692 | |
| 693 | ASSERT(cur == NULL); |
| 694 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first, |
| 695 | flist, &cur, da_old > 0, &tmp_logflags, whichfork); |
| 696 | logflags |= tmp_logflags; |
| 697 | if (error) |
| 698 | goto done; |
| 699 | } |
| 700 | /* |
| 701 | * Adjust for changes in reserved delayed indirect blocks. |
| 702 | * Nothing to do for disk quotas here. |
| 703 | */ |
| 704 | if (da_old || da_new) { |
| 705 | xfs_filblks_t nblks; |
| 706 | |
| 707 | nblks = da_new; |
| 708 | if (cur) |
| 709 | nblks += cur->bc_private.b.allocated; |
| 710 | ASSERT(nblks <= da_old); |
| 711 | if (nblks < da_old) |
| 712 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 713 | (int64_t)(da_old - nblks), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | /* |
| 716 | * Clear out the allocated field, done with it now in any case. |
| 717 | */ |
| 718 | if (cur) { |
| 719 | cur->bc_private.b.allocated = 0; |
| 720 | *curp = cur; |
| 721 | } |
| 722 | done: |
| 723 | #ifdef DEBUG |
| 724 | if (!error) |
| 725 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
| 726 | #endif |
| 727 | *logflagsp = logflags; |
| 728 | return error; |
| 729 | } |
| 730 | |
| 731 | /* |
| 732 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 733 | * allocation to a real allocation. |
| 734 | */ |
| 735 | STATIC int /* error */ |
| 736 | xfs_bmap_add_extent_delay_real( |
| 737 | xfs_inode_t *ip, /* incore inode pointer */ |
| 738 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 739 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 740 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 742 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 743 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 744 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 745 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | int rsvd) /* OK to use reserved data block allocation */ |
| 747 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 749 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 750 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 753 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 755 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 756 | /* left is 0, right is 1, prev is 2 */ |
| 757 | int rval=0; /* return value (logging flags) */ |
| 758 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 759 | xfs_filblks_t temp=0; /* value for dnew calculations */ |
| 760 | xfs_filblks_t temp2=0;/* value for dnew calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | int tmp_rval; /* partial logging flags */ |
| 762 | enum { /* bit number definitions for state */ |
| 763 | LEFT_CONTIG, RIGHT_CONTIG, |
| 764 | LEFT_FILLING, RIGHT_FILLING, |
| 765 | LEFT_DELAY, RIGHT_DELAY, |
| 766 | LEFT_VALID, RIGHT_VALID |
| 767 | }; |
| 768 | |
| 769 | #define LEFT r[0] |
| 770 | #define RIGHT r[1] |
| 771 | #define PREV r[2] |
| 772 | #define MASK(b) (1 << (b)) |
| 773 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 774 | #define MASK3(a,b,c) (MASK2(a,b) | MASK(c)) |
| 775 | #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d)) |
| 776 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 777 | #define STATE_TEST(b) (state & MASK(b)) |
| 778 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 779 | ((state &= ~MASK(b)), 0)) |
| 780 | #define SWITCH_STATE \ |
| 781 | (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG)) |
| 782 | |
| 783 | /* |
| 784 | * Set up a bunch of variables to make the tests simpler. |
| 785 | */ |
| 786 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 787 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 788 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | xfs_bmbt_get_all(ep, &PREV); |
| 790 | new_endoff = new->br_startoff + new->br_blockcount; |
| 791 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 792 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
| 793 | /* |
| 794 | * Set flags determining what part of the previous delayed allocation |
| 795 | * extent is being replaced by a real allocation. |
| 796 | */ |
| 797 | STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff); |
| 798 | STATE_SET(RIGHT_FILLING, |
| 799 | PREV.br_startoff + PREV.br_blockcount == new_endoff); |
| 800 | /* |
| 801 | * Check and set flags if this segment has a left neighbor. |
| 802 | * Don't set contiguous if the combined extent would be too large. |
| 803 | */ |
| 804 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 805 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 806 | STATE_SET(LEFT_DELAY, isnullstartblock(LEFT.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } |
| 808 | STATE_SET(LEFT_CONTIG, |
| 809 | STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) && |
| 810 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 811 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 812 | LEFT.br_state == new->br_state && |
| 813 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 814 | /* |
| 815 | * Check and set flags if this segment has a right neighbor. |
| 816 | * Don't set contiguous if the combined extent would be too large. |
| 817 | * Also check for all-three-contiguous being too large. |
| 818 | */ |
| 819 | if (STATE_SET_TEST(RIGHT_VALID, |
| 820 | idx < |
| 821 | ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 822 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 823 | STATE_SET(RIGHT_DELAY, isnullstartblock(RIGHT.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | STATE_SET(RIGHT_CONTIG, |
| 826 | STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) && |
| 827 | new_endoff == RIGHT.br_startoff && |
| 828 | new->br_startblock + new->br_blockcount == |
| 829 | RIGHT.br_startblock && |
| 830 | new->br_state == RIGHT.br_state && |
| 831 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 832 | ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) != |
| 833 | MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) || |
| 834 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 835 | <= MAXEXTLEN)); |
| 836 | error = 0; |
| 837 | /* |
| 838 | * Switch out based on the FILLING and CONTIG state bits. |
| 839 | */ |
| 840 | switch (SWITCH_STATE) { |
| 841 | |
| 842 | case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 843 | /* |
| 844 | * Filling in all of a previously delayed allocation extent. |
| 845 | * The left and right neighbors are both contiguous with new. |
| 846 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 847 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 849 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | LEFT.br_blockcount + PREV.br_blockcount + |
| 851 | RIGHT.br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 852 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | XFS_DATA_FORK); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 854 | XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 855 | xfs_iext_remove(ifp, idx, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | ip->i_df.if_lastex = idx - 1; |
| 857 | ip->i_d.di_nextents--; |
| 858 | if (cur == NULL) |
| 859 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 860 | else { |
| 861 | rval = XFS_ILOG_CORE; |
| 862 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 863 | RIGHT.br_startblock, |
| 864 | RIGHT.br_blockcount, &i))) |
| 865 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 866 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 867 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 869 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 870 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 872 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 874 | LEFT.br_startblock, |
| 875 | LEFT.br_blockcount + |
| 876 | PREV.br_blockcount + |
| 877 | RIGHT.br_blockcount, LEFT.br_state))) |
| 878 | goto done; |
| 879 | } |
| 880 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 881 | /* DELTA: Three in-core extents are replaced by one. */ |
| 882 | temp = LEFT.br_startoff; |
| 883 | temp2 = LEFT.br_blockcount + |
| 884 | PREV.br_blockcount + |
| 885 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | break; |
| 887 | |
| 888 | case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG): |
| 889 | /* |
| 890 | * Filling in all of a previously delayed allocation extent. |
| 891 | * The left neighbor is contiguous, the right is not. |
| 892 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 893 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 895 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | LEFT.br_blockcount + PREV.br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 897 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | XFS_DATA_FORK); |
| 899 | ip->i_df.if_lastex = idx - 1; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 900 | XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 901 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | if (cur == NULL) |
| 903 | rval = XFS_ILOG_DEXT; |
| 904 | else { |
| 905 | rval = 0; |
| 906 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 907 | LEFT.br_startblock, LEFT.br_blockcount, |
| 908 | &i))) |
| 909 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 910 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 912 | LEFT.br_startblock, |
| 913 | LEFT.br_blockcount + |
| 914 | PREV.br_blockcount, LEFT.br_state))) |
| 915 | goto done; |
| 916 | } |
| 917 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 918 | /* DELTA: Two in-core extents are replaced by one. */ |
| 919 | temp = LEFT.br_startoff; |
| 920 | temp2 = LEFT.br_blockcount + |
| 921 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | break; |
| 923 | |
| 924 | case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG): |
| 925 | /* |
| 926 | * Filling in all of a previously delayed allocation extent. |
| 927 | * The right neighbor is contiguous, the left is not. |
| 928 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 929 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 931 | xfs_bmbt_set_blockcount(ep, |
| 932 | PREV.br_blockcount + RIGHT.br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 933 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | ip->i_df.if_lastex = idx; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 935 | XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 936 | xfs_iext_remove(ifp, idx + 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (cur == NULL) |
| 938 | rval = XFS_ILOG_DEXT; |
| 939 | else { |
| 940 | rval = 0; |
| 941 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 942 | RIGHT.br_startblock, |
| 943 | RIGHT.br_blockcount, &i))) |
| 944 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 945 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 947 | new->br_startblock, |
| 948 | PREV.br_blockcount + |
| 949 | RIGHT.br_blockcount, PREV.br_state))) |
| 950 | goto done; |
| 951 | } |
| 952 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 953 | /* DELTA: Two in-core extents are replaced by one. */ |
| 954 | temp = PREV.br_startoff; |
| 955 | temp2 = PREV.br_blockcount + |
| 956 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | break; |
| 958 | |
| 959 | case MASK2(LEFT_FILLING, RIGHT_FILLING): |
| 960 | /* |
| 961 | * Filling in all of a previously delayed allocation extent. |
| 962 | * Neither the left nor right neighbors are contiguous with |
| 963 | * the new one. |
| 964 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 965 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 967 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | ip->i_df.if_lastex = idx; |
| 969 | ip->i_d.di_nextents++; |
| 970 | if (cur == NULL) |
| 971 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 972 | else { |
| 973 | rval = XFS_ILOG_CORE; |
| 974 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 975 | new->br_startblock, new->br_blockcount, |
| 976 | &i))) |
| 977 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 978 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 980 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 982 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | } |
| 984 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 985 | /* DELTA: The in-core extent described by new changed type. */ |
| 986 | temp = new->br_startoff; |
| 987 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | break; |
| 989 | |
| 990 | case MASK2(LEFT_FILLING, LEFT_CONTIG): |
| 991 | /* |
| 992 | * Filling in the first part of a previous delayed allocation. |
| 993 | * The left neighbor is contiguous. |
| 994 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 995 | XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 996 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | LEFT.br_blockcount + new->br_blockcount); |
| 998 | xfs_bmbt_set_startoff(ep, |
| 999 | PREV.br_startoff + new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1000 | XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | temp = PREV.br_blockcount - new->br_blockcount; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1002 | XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | xfs_bmbt_set_blockcount(ep, temp); |
| 1004 | ip->i_df.if_lastex = idx - 1; |
| 1005 | if (cur == NULL) |
| 1006 | rval = XFS_ILOG_DEXT; |
| 1007 | else { |
| 1008 | rval = 0; |
| 1009 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 1010 | LEFT.br_startblock, LEFT.br_blockcount, |
| 1011 | &i))) |
| 1012 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1013 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1015 | LEFT.br_startblock, |
| 1016 | LEFT.br_blockcount + |
| 1017 | new->br_blockcount, |
| 1018 | LEFT.br_state))) |
| 1019 | goto done; |
| 1020 | } |
| 1021 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1022 | startblockval(PREV.br_startblock)); |
| 1023 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1024 | XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1026 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1027 | temp = LEFT.br_startoff; |
| 1028 | temp2 = LEFT.br_blockcount + |
| 1029 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | break; |
| 1031 | |
| 1032 | case MASK(LEFT_FILLING): |
| 1033 | /* |
| 1034 | * Filling in the first part of a previous delayed allocation. |
| 1035 | * The left neighbor is not contiguous. |
| 1036 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1037 | XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1039 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1040 | xfs_bmbt_set_blockcount(ep, temp); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1041 | XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1043 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | ip->i_df.if_lastex = idx; |
| 1045 | ip->i_d.di_nextents++; |
| 1046 | if (cur == NULL) |
| 1047 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1048 | else { |
| 1049 | rval = XFS_ILOG_CORE; |
| 1050 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1051 | new->br_startblock, new->br_blockcount, |
| 1052 | &i))) |
| 1053 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1054 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1056 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1058 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1061 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1062 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1063 | first, flist, &cur, 1, &tmp_rval, |
| 1064 | XFS_DATA_FORK); |
| 1065 | rval |= tmp_rval; |
| 1066 | if (error) |
| 1067 | goto done; |
| 1068 | } |
| 1069 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1070 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1072 | ep = xfs_iext_get_ext(ifp, idx + 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1073 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1074 | XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx + 1, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1076 | /* DELTA: One in-core extent is split in two. */ |
| 1077 | temp = PREV.br_startoff; |
| 1078 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | break; |
| 1080 | |
| 1081 | case MASK2(RIGHT_FILLING, RIGHT_CONTIG): |
| 1082 | /* |
| 1083 | * Filling in the last part of a previous delayed allocation. |
| 1084 | * The right neighbor is contiguous with the new allocation. |
| 1085 | */ |
| 1086 | temp = PREV.br_blockcount - new->br_blockcount; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1087 | XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK); |
| 1088 | XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | xfs_bmbt_set_blockcount(ep, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1090 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1091 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | new->br_blockcount + RIGHT.br_blockcount, |
| 1093 | RIGHT.br_state); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1094 | XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | ip->i_df.if_lastex = idx + 1; |
| 1096 | if (cur == NULL) |
| 1097 | rval = XFS_ILOG_DEXT; |
| 1098 | else { |
| 1099 | rval = 0; |
| 1100 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1101 | RIGHT.br_startblock, |
| 1102 | RIGHT.br_blockcount, &i))) |
| 1103 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1104 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1106 | new->br_startblock, |
| 1107 | new->br_blockcount + |
| 1108 | RIGHT.br_blockcount, |
| 1109 | RIGHT.br_state))) |
| 1110 | goto done; |
| 1111 | } |
| 1112 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1113 | startblockval(PREV.br_startblock)); |
| 1114 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1115 | XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1117 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1118 | temp = PREV.br_startoff; |
| 1119 | temp2 = PREV.br_blockcount + |
| 1120 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | break; |
| 1122 | |
| 1123 | case MASK(RIGHT_FILLING): |
| 1124 | /* |
| 1125 | * Filling in the last part of a previous delayed allocation. |
| 1126 | * The right neighbor is not contiguous. |
| 1127 | */ |
| 1128 | temp = PREV.br_blockcount - new->br_blockcount; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1129 | XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | xfs_bmbt_set_blockcount(ep, temp); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1131 | XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL, |
| 1132 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1133 | xfs_iext_insert(ifp, idx + 1, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | ip->i_df.if_lastex = idx + 1; |
| 1135 | ip->i_d.di_nextents++; |
| 1136 | if (cur == NULL) |
| 1137 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1138 | else { |
| 1139 | rval = XFS_ILOG_CORE; |
| 1140 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1141 | new->br_startblock, new->br_blockcount, |
| 1142 | &i))) |
| 1143 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1144 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1146 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1148 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
| 1150 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1151 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1152 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1153 | first, flist, &cur, 1, &tmp_rval, |
| 1154 | XFS_DATA_FORK); |
| 1155 | rval |= tmp_rval; |
| 1156 | if (error) |
| 1157 | goto done; |
| 1158 | } |
| 1159 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1160 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1162 | ep = xfs_iext_get_ext(ifp, idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1163 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1164 | XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1166 | /* DELTA: One in-core extent is split in two. */ |
| 1167 | temp = PREV.br_startoff; |
| 1168 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | break; |
| 1170 | |
| 1171 | case 0: |
| 1172 | /* |
| 1173 | * Filling in the middle part of a previous delayed allocation. |
| 1174 | * Contiguity is impossible here. |
| 1175 | * This case is avoided almost all the time. |
| 1176 | */ |
| 1177 | temp = new->br_startoff - PREV.br_startoff; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1178 | XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | xfs_bmbt_set_blockcount(ep, temp); |
| 1180 | r[0] = *new; |
Lachlan McIlroy | d213371 | 2006-11-21 18:55:16 +1100 | [diff] [blame] | 1181 | r[1].br_state = PREV.br_state; |
| 1182 | r[1].br_startblock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | r[1].br_startoff = new_endoff; |
| 1184 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1185 | r[1].br_blockcount = temp2; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1186 | XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1188 | xfs_iext_insert(ifp, idx + 1, 2, &r[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | ip->i_df.if_lastex = idx + 1; |
| 1190 | ip->i_d.di_nextents++; |
| 1191 | if (cur == NULL) |
| 1192 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1193 | else { |
| 1194 | rval = XFS_ILOG_CORE; |
| 1195 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1196 | new->br_startblock, new->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 == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1201 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1203 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1206 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1207 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1208 | first, flist, &cur, 1, &tmp_rval, |
| 1209 | XFS_DATA_FORK); |
| 1210 | rval |= tmp_rval; |
| 1211 | if (error) |
| 1212 | goto done; |
| 1213 | } |
| 1214 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 1215 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1216 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | (cur ? cur->bc_private.b.allocated : 0)); |
| 1218 | if (diff > 0 && |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1219 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | /* |
| 1221 | * Ick gross gag me with a spoon. |
| 1222 | */ |
| 1223 | ASSERT(0); /* want to see if this ever happens! */ |
| 1224 | while (diff > 0) { |
| 1225 | if (temp) { |
| 1226 | temp--; |
| 1227 | diff--; |
| 1228 | if (!diff || |
| 1229 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1230 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | break; |
| 1232 | } |
| 1233 | if (temp2) { |
| 1234 | temp2--; |
| 1235 | diff--; |
| 1236 | if (!diff || |
| 1237 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1238 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | break; |
| 1240 | } |
| 1241 | } |
| 1242 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1243 | ep = xfs_iext_get_ext(ifp, idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1244 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1245 | XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK); |
| 1246 | XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx + 2, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1247 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1248 | nullstartblock((int)temp2)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1249 | XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx + 2, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | *dnew = temp + temp2; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1251 | /* DELTA: One in-core extent is split in three. */ |
| 1252 | temp = PREV.br_startoff; |
| 1253 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | break; |
| 1255 | |
| 1256 | case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1257 | case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1258 | case MASK2(LEFT_FILLING, RIGHT_CONTIG): |
| 1259 | case MASK2(RIGHT_FILLING, LEFT_CONTIG): |
| 1260 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 1261 | case MASK(LEFT_CONTIG): |
| 1262 | case MASK(RIGHT_CONTIG): |
| 1263 | /* |
| 1264 | * These cases are all impossible. |
| 1265 | */ |
| 1266 | ASSERT(0); |
| 1267 | } |
| 1268 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1269 | if (delta) { |
| 1270 | temp2 += temp; |
| 1271 | if (delta->xed_startoff > temp) |
| 1272 | delta->xed_startoff = temp; |
| 1273 | if (delta->xed_blockcount < temp2) |
| 1274 | delta->xed_blockcount = temp2; |
| 1275 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | done: |
| 1277 | *logflagsp = rval; |
| 1278 | return error; |
| 1279 | #undef LEFT |
| 1280 | #undef RIGHT |
| 1281 | #undef PREV |
| 1282 | #undef MASK |
| 1283 | #undef MASK2 |
| 1284 | #undef MASK3 |
| 1285 | #undef MASK4 |
| 1286 | #undef STATE_SET |
| 1287 | #undef STATE_TEST |
| 1288 | #undef STATE_SET_TEST |
| 1289 | #undef SWITCH_STATE |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 1294 | * allocation to a real allocation or vice versa. |
| 1295 | */ |
| 1296 | STATIC int /* error */ |
| 1297 | xfs_bmap_add_extent_unwritten_real( |
| 1298 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1299 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 1300 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1301 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1302 | int *logflagsp, /* inode logging flags */ |
| 1303 | xfs_extdelta_t *delta) /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1306 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1309 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1311 | xfs_exntst_t newext; /* new extent state */ |
| 1312 | xfs_exntst_t oldext; /* old extent state */ |
| 1313 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1314 | /* left is 0, right is 1, prev is 2 */ |
| 1315 | int rval=0; /* return value (logging flags) */ |
| 1316 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1317 | xfs_filblks_t temp=0; |
| 1318 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | enum { /* bit number definitions for state */ |
| 1320 | LEFT_CONTIG, RIGHT_CONTIG, |
| 1321 | LEFT_FILLING, RIGHT_FILLING, |
| 1322 | LEFT_DELAY, RIGHT_DELAY, |
| 1323 | LEFT_VALID, RIGHT_VALID |
| 1324 | }; |
| 1325 | |
| 1326 | #define LEFT r[0] |
| 1327 | #define RIGHT r[1] |
| 1328 | #define PREV r[2] |
| 1329 | #define MASK(b) (1 << (b)) |
| 1330 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 1331 | #define MASK3(a,b,c) (MASK2(a,b) | MASK(c)) |
| 1332 | #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d)) |
| 1333 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 1334 | #define STATE_TEST(b) (state & MASK(b)) |
| 1335 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 1336 | ((state &= ~MASK(b)), 0)) |
| 1337 | #define SWITCH_STATE \ |
| 1338 | (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG)) |
| 1339 | |
| 1340 | /* |
| 1341 | * Set up a bunch of variables to make the tests simpler. |
| 1342 | */ |
| 1343 | error = 0; |
| 1344 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1345 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1346 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | xfs_bmbt_get_all(ep, &PREV); |
| 1348 | newext = new->br_state; |
| 1349 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 1350 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 1351 | ASSERT(PREV.br_state == oldext); |
| 1352 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1353 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1354 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
| 1355 | /* |
| 1356 | * Set flags determining what part of the previous oldext allocation |
| 1357 | * extent is being replaced by a newext allocation. |
| 1358 | */ |
| 1359 | STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff); |
| 1360 | STATE_SET(RIGHT_FILLING, |
| 1361 | PREV.br_startoff + PREV.br_blockcount == new_endoff); |
| 1362 | /* |
| 1363 | * Check and set flags if this segment has a left neighbor. |
| 1364 | * Don't set contiguous if the combined extent would be too large. |
| 1365 | */ |
| 1366 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1367 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1368 | STATE_SET(LEFT_DELAY, isnullstartblock(LEFT.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | } |
| 1370 | STATE_SET(LEFT_CONTIG, |
| 1371 | STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) && |
| 1372 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1373 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1374 | LEFT.br_state == newext && |
| 1375 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 1376 | /* |
| 1377 | * Check and set flags if this segment has a right neighbor. |
| 1378 | * Don't set contiguous if the combined extent would be too large. |
| 1379 | * Also check for all-three-contiguous being too large. |
| 1380 | */ |
| 1381 | if (STATE_SET_TEST(RIGHT_VALID, |
| 1382 | idx < |
| 1383 | ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1384 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1385 | STATE_SET(RIGHT_DELAY, isnullstartblock(RIGHT.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | } |
| 1387 | STATE_SET(RIGHT_CONTIG, |
| 1388 | STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) && |
| 1389 | new_endoff == RIGHT.br_startoff && |
| 1390 | new->br_startblock + new->br_blockcount == |
| 1391 | RIGHT.br_startblock && |
| 1392 | newext == RIGHT.br_state && |
| 1393 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1394 | ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) != |
| 1395 | MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) || |
| 1396 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1397 | <= MAXEXTLEN)); |
| 1398 | /* |
| 1399 | * Switch out based on the FILLING and CONTIG state bits. |
| 1400 | */ |
| 1401 | switch (SWITCH_STATE) { |
| 1402 | |
| 1403 | case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1404 | /* |
| 1405 | * Setting all of a previous oldext extent to newext. |
| 1406 | * The left and right neighbors are both contiguous with new. |
| 1407 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1408 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1410 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1412 | RIGHT.br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1413 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | XFS_DATA_FORK); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1415 | XFS_BMAP_TRACE_DELETE("LF|RF|LC|RC", ip, idx, 2, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1416 | xfs_iext_remove(ifp, idx, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | ip->i_df.if_lastex = idx - 1; |
| 1418 | ip->i_d.di_nextents -= 2; |
| 1419 | if (cur == NULL) |
| 1420 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1421 | else { |
| 1422 | rval = XFS_ILOG_CORE; |
| 1423 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1424 | RIGHT.br_startblock, |
| 1425 | RIGHT.br_blockcount, &i))) |
| 1426 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1427 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1428 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1430 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1431 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1433 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1434 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1436 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1437 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1439 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1441 | LEFT.br_startblock, |
| 1442 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1443 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1444 | goto done; |
| 1445 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1446 | /* DELTA: Three in-core extents are replaced by one. */ |
| 1447 | temp = LEFT.br_startoff; |
| 1448 | temp2 = LEFT.br_blockcount + |
| 1449 | PREV.br_blockcount + |
| 1450 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | break; |
| 1452 | |
| 1453 | case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG): |
| 1454 | /* |
| 1455 | * Setting all of a previous oldext extent to newext. |
| 1456 | * The left neighbor is contiguous, the right is not. |
| 1457 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1458 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1460 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | LEFT.br_blockcount + PREV.br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1462 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF|LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | XFS_DATA_FORK); |
| 1464 | ip->i_df.if_lastex = idx - 1; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1465 | XFS_BMAP_TRACE_DELETE("LF|RF|LC", ip, idx, 1, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1466 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | ip->i_d.di_nextents--; |
| 1468 | if (cur == NULL) |
| 1469 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1470 | else { |
| 1471 | rval = XFS_ILOG_CORE; |
| 1472 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1473 | PREV.br_startblock, PREV.br_blockcount, |
| 1474 | &i))) |
| 1475 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1476 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1477 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1479 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1480 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1482 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1484 | LEFT.br_startblock, |
| 1485 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1486 | LEFT.br_state))) |
| 1487 | goto done; |
| 1488 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1489 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1490 | temp = LEFT.br_startoff; |
| 1491 | temp2 = LEFT.br_blockcount + |
| 1492 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | break; |
| 1494 | |
| 1495 | case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG): |
| 1496 | /* |
| 1497 | * Setting all of a previous oldext extent to newext. |
| 1498 | * The right neighbor is contiguous, the left is not. |
| 1499 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1500 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF|RC", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | XFS_DATA_FORK); |
| 1502 | xfs_bmbt_set_blockcount(ep, |
| 1503 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1504 | xfs_bmbt_set_state(ep, newext); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1505 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF|RC", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | XFS_DATA_FORK); |
| 1507 | ip->i_df.if_lastex = idx; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1508 | XFS_BMAP_TRACE_DELETE("LF|RF|RC", ip, idx + 1, 1, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1509 | xfs_iext_remove(ifp, idx + 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | ip->i_d.di_nextents--; |
| 1511 | if (cur == NULL) |
| 1512 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1513 | else { |
| 1514 | rval = XFS_ILOG_CORE; |
| 1515 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1516 | RIGHT.br_startblock, |
| 1517 | RIGHT.br_blockcount, &i))) |
| 1518 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1519 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1520 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1522 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1523 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1525 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1527 | new->br_startblock, |
| 1528 | new->br_blockcount + RIGHT.br_blockcount, |
| 1529 | newext))) |
| 1530 | goto done; |
| 1531 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1532 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1533 | temp = PREV.br_startoff; |
| 1534 | temp2 = PREV.br_blockcount + |
| 1535 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | break; |
| 1537 | |
| 1538 | case MASK2(LEFT_FILLING, RIGHT_FILLING): |
| 1539 | /* |
| 1540 | * Setting all of a previous oldext extent to newext. |
| 1541 | * Neither the left nor right neighbors are contiguous with |
| 1542 | * the new one. |
| 1543 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1544 | XFS_BMAP_TRACE_PRE_UPDATE("LF|RF", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | XFS_DATA_FORK); |
| 1546 | xfs_bmbt_set_state(ep, newext); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1547 | XFS_BMAP_TRACE_POST_UPDATE("LF|RF", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | XFS_DATA_FORK); |
| 1549 | ip->i_df.if_lastex = idx; |
| 1550 | if (cur == NULL) |
| 1551 | rval = XFS_ILOG_DEXT; |
| 1552 | else { |
| 1553 | rval = 0; |
| 1554 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1555 | new->br_startblock, new->br_blockcount, |
| 1556 | &i))) |
| 1557 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1558 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1560 | new->br_startblock, new->br_blockcount, |
| 1561 | newext))) |
| 1562 | goto done; |
| 1563 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1564 | /* DELTA: The in-core extent described by new changed type. */ |
| 1565 | temp = new->br_startoff; |
| 1566 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | break; |
| 1568 | |
| 1569 | case MASK2(LEFT_FILLING, LEFT_CONTIG): |
| 1570 | /* |
| 1571 | * Setting the first part of a previous oldext extent to newext. |
| 1572 | * The left neighbor is contiguous. |
| 1573 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1574 | XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1576 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | LEFT.br_blockcount + new->br_blockcount); |
| 1578 | xfs_bmbt_set_startoff(ep, |
| 1579 | PREV.br_startoff + new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1580 | XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | XFS_DATA_FORK); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1582 | XFS_BMAP_TRACE_PRE_UPDATE("LF|LC", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | XFS_DATA_FORK); |
| 1584 | xfs_bmbt_set_startblock(ep, |
| 1585 | new->br_startblock + new->br_blockcount); |
| 1586 | xfs_bmbt_set_blockcount(ep, |
| 1587 | PREV.br_blockcount - new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1588 | XFS_BMAP_TRACE_POST_UPDATE("LF|LC", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | XFS_DATA_FORK); |
| 1590 | ip->i_df.if_lastex = idx - 1; |
| 1591 | if (cur == NULL) |
| 1592 | rval = XFS_ILOG_DEXT; |
| 1593 | else { |
| 1594 | rval = 0; |
| 1595 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1596 | PREV.br_startblock, PREV.br_blockcount, |
| 1597 | &i))) |
| 1598 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1599 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | if ((error = xfs_bmbt_update(cur, |
| 1601 | PREV.br_startoff + new->br_blockcount, |
| 1602 | PREV.br_startblock + new->br_blockcount, |
| 1603 | PREV.br_blockcount - new->br_blockcount, |
| 1604 | oldext))) |
| 1605 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1606 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | goto done; |
| 1608 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1609 | LEFT.br_startblock, |
| 1610 | LEFT.br_blockcount + new->br_blockcount, |
| 1611 | LEFT.br_state)) |
| 1612 | goto done; |
| 1613 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1614 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1615 | temp = LEFT.br_startoff; |
| 1616 | temp2 = LEFT.br_blockcount + |
| 1617 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | break; |
| 1619 | |
| 1620 | case MASK(LEFT_FILLING): |
| 1621 | /* |
| 1622 | * Setting the first part of a previous oldext extent to newext. |
| 1623 | * The left neighbor is not contiguous. |
| 1624 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1625 | XFS_BMAP_TRACE_PRE_UPDATE("LF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1627 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1628 | xfs_bmbt_set_blockcount(ep, |
| 1629 | PREV.br_blockcount - new->br_blockcount); |
| 1630 | xfs_bmbt_set_startblock(ep, |
| 1631 | new->br_startblock + new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1632 | XFS_BMAP_TRACE_POST_UPDATE("LF", ip, idx, XFS_DATA_FORK); |
| 1633 | XFS_BMAP_TRACE_INSERT("LF", ip, idx, 1, new, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1635 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | ip->i_df.if_lastex = idx; |
| 1637 | ip->i_d.di_nextents++; |
| 1638 | if (cur == NULL) |
| 1639 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1640 | else { |
| 1641 | rval = XFS_ILOG_CORE; |
| 1642 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1643 | PREV.br_startblock, PREV.br_blockcount, |
| 1644 | &i))) |
| 1645 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1646 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | if ((error = xfs_bmbt_update(cur, |
| 1648 | PREV.br_startoff + new->br_blockcount, |
| 1649 | PREV.br_startblock + new->br_blockcount, |
| 1650 | PREV.br_blockcount - new->br_blockcount, |
| 1651 | oldext))) |
| 1652 | goto done; |
| 1653 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1654 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1656 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1658 | /* DELTA: One in-core extent is split in two. */ |
| 1659 | temp = PREV.br_startoff; |
| 1660 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | break; |
| 1662 | |
| 1663 | case MASK2(RIGHT_FILLING, RIGHT_CONTIG): |
| 1664 | /* |
| 1665 | * Setting the last part of a previous oldext extent to newext. |
| 1666 | * The right neighbor is contiguous with the new allocation. |
| 1667 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1668 | XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | XFS_DATA_FORK); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1670 | XFS_BMAP_TRACE_PRE_UPDATE("RF|RC", ip, idx + 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | XFS_DATA_FORK); |
| 1672 | xfs_bmbt_set_blockcount(ep, |
| 1673 | PREV.br_blockcount - new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1674 | XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1676 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1677 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1679 | XFS_BMAP_TRACE_POST_UPDATE("RF|RC", ip, idx + 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | XFS_DATA_FORK); |
| 1681 | ip->i_df.if_lastex = idx + 1; |
| 1682 | if (cur == NULL) |
| 1683 | rval = XFS_ILOG_DEXT; |
| 1684 | else { |
| 1685 | rval = 0; |
| 1686 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1687 | PREV.br_startblock, |
| 1688 | PREV.br_blockcount, &i))) |
| 1689 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1690 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1692 | PREV.br_startblock, |
| 1693 | PREV.br_blockcount - new->br_blockcount, |
| 1694 | oldext))) |
| 1695 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1696 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | goto done; |
| 1698 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1699 | new->br_startblock, |
| 1700 | new->br_blockcount + RIGHT.br_blockcount, |
| 1701 | newext))) |
| 1702 | goto done; |
| 1703 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1704 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1705 | temp = PREV.br_startoff; |
| 1706 | temp2 = PREV.br_blockcount + |
| 1707 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | break; |
| 1709 | |
| 1710 | case MASK(RIGHT_FILLING): |
| 1711 | /* |
| 1712 | * Setting the last part of a previous oldext extent to newext. |
| 1713 | * The right neighbor is not contiguous. |
| 1714 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1715 | XFS_BMAP_TRACE_PRE_UPDATE("RF", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | xfs_bmbt_set_blockcount(ep, |
| 1717 | PREV.br_blockcount - new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1718 | XFS_BMAP_TRACE_POST_UPDATE("RF", ip, idx, XFS_DATA_FORK); |
| 1719 | XFS_BMAP_TRACE_INSERT("RF", ip, idx + 1, 1, new, NULL, |
| 1720 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1721 | xfs_iext_insert(ifp, idx + 1, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | ip->i_df.if_lastex = idx + 1; |
| 1723 | ip->i_d.di_nextents++; |
| 1724 | if (cur == NULL) |
| 1725 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1726 | else { |
| 1727 | rval = XFS_ILOG_CORE; |
| 1728 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1729 | PREV.br_startblock, PREV.br_blockcount, |
| 1730 | &i))) |
| 1731 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1732 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1734 | PREV.br_startblock, |
| 1735 | PREV.br_blockcount - new->br_blockcount, |
| 1736 | oldext))) |
| 1737 | goto done; |
| 1738 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1739 | new->br_startblock, new->br_blockcount, |
| 1740 | &i))) |
| 1741 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1742 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1744 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1746 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1748 | /* DELTA: One in-core extent is split in two. */ |
| 1749 | temp = PREV.br_startoff; |
| 1750 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | break; |
| 1752 | |
| 1753 | case 0: |
| 1754 | /* |
| 1755 | * Setting the middle part of a previous oldext extent to |
| 1756 | * newext. Contiguity is impossible here. |
| 1757 | * One extent becomes three extents. |
| 1758 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1759 | XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | xfs_bmbt_set_blockcount(ep, |
| 1761 | new->br_startoff - PREV.br_startoff); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1762 | XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | r[0] = *new; |
| 1764 | r[1].br_startoff = new_endoff; |
| 1765 | r[1].br_blockcount = |
| 1766 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1767 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1768 | r[1].br_state = oldext; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1769 | XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 2, &r[0], &r[1], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1771 | xfs_iext_insert(ifp, idx + 1, 2, &r[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | ip->i_df.if_lastex = idx + 1; |
| 1773 | ip->i_d.di_nextents += 2; |
| 1774 | if (cur == NULL) |
| 1775 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1776 | else { |
| 1777 | rval = XFS_ILOG_CORE; |
| 1778 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1779 | PREV.br_startblock, PREV.br_blockcount, |
| 1780 | &i))) |
| 1781 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1782 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | /* new right extent - oldext */ |
| 1784 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1785 | r[1].br_startblock, r[1].br_blockcount, |
| 1786 | r[1].br_state))) |
| 1787 | goto done; |
| 1788 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1790 | cur->bc_rec.b.br_blockcount = |
| 1791 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1792 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1794 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1795 | /* |
| 1796 | * Reset the cursor to the position of the new extent |
| 1797 | * we are about to insert as we can't trust it after |
| 1798 | * the previous insert. |
| 1799 | */ |
| 1800 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1801 | new->br_startblock, new->br_blockcount, |
| 1802 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1804 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1806 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1807 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1809 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1811 | /* DELTA: One in-core extent is split in three. */ |
| 1812 | temp = PREV.br_startoff; |
| 1813 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | break; |
| 1815 | |
| 1816 | case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1817 | case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1818 | case MASK2(LEFT_FILLING, RIGHT_CONTIG): |
| 1819 | case MASK2(RIGHT_FILLING, LEFT_CONTIG): |
| 1820 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 1821 | case MASK(LEFT_CONTIG): |
| 1822 | case MASK(RIGHT_CONTIG): |
| 1823 | /* |
| 1824 | * These cases are all impossible. |
| 1825 | */ |
| 1826 | ASSERT(0); |
| 1827 | } |
| 1828 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1829 | if (delta) { |
| 1830 | temp2 += temp; |
| 1831 | if (delta->xed_startoff > temp) |
| 1832 | delta->xed_startoff = temp; |
| 1833 | if (delta->xed_blockcount < temp2) |
| 1834 | delta->xed_blockcount = temp2; |
| 1835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | done: |
| 1837 | *logflagsp = rval; |
| 1838 | return error; |
| 1839 | #undef LEFT |
| 1840 | #undef RIGHT |
| 1841 | #undef PREV |
| 1842 | #undef MASK |
| 1843 | #undef MASK2 |
| 1844 | #undef MASK3 |
| 1845 | #undef MASK4 |
| 1846 | #undef STATE_SET |
| 1847 | #undef STATE_TEST |
| 1848 | #undef STATE_SET_TEST |
| 1849 | #undef SWITCH_STATE |
| 1850 | } |
| 1851 | |
| 1852 | /* |
| 1853 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1854 | * to a delayed allocation. |
| 1855 | */ |
| 1856 | /*ARGSUSED*/ |
| 1857 | STATIC int /* error */ |
| 1858 | xfs_bmap_add_extent_hole_delay( |
| 1859 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1860 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1861 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1863 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | int rsvd) /* OK to allocate reserved blocks */ |
| 1865 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1866 | xfs_bmbt_rec_host_t *ep; /* extent record for idx */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1867 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1869 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1870 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1871 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1872 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1873 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
| 1874 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | enum { /* bit number definitions for state */ |
| 1876 | LEFT_CONTIG, RIGHT_CONTIG, |
| 1877 | LEFT_DELAY, RIGHT_DELAY, |
| 1878 | LEFT_VALID, RIGHT_VALID |
| 1879 | }; |
| 1880 | |
| 1881 | #define MASK(b) (1 << (b)) |
| 1882 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 1883 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 1884 | #define STATE_TEST(b) (state & MASK(b)) |
| 1885 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 1886 | ((state &= ~MASK(b)), 0)) |
| 1887 | #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG)) |
| 1888 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1889 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1890 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1892 | ASSERT(isnullstartblock(new->br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | /* |
| 1894 | * Check and set flags if this segment has a left neighbor |
| 1895 | */ |
| 1896 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1897 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1898 | STATE_SET(LEFT_DELAY, isnullstartblock(left.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | } |
| 1900 | /* |
| 1901 | * Check and set flags if the current (right) segment exists. |
| 1902 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1903 | */ |
| 1904 | if (STATE_SET_TEST(RIGHT_VALID, |
| 1905 | idx < |
| 1906 | ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 1907 | xfs_bmbt_get_all(ep, &right); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1908 | STATE_SET(RIGHT_DELAY, isnullstartblock(right.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | /* |
| 1911 | * Set contiguity flags on the left and right neighbors. |
| 1912 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1913 | */ |
| 1914 | STATE_SET(LEFT_CONTIG, |
| 1915 | STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) && |
| 1916 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1917 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 1918 | STATE_SET(RIGHT_CONTIG, |
| 1919 | STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) && |
| 1920 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1921 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1922 | (!STATE_TEST(LEFT_CONTIG) || |
| 1923 | (left.br_blockcount + new->br_blockcount + |
| 1924 | right.br_blockcount <= MAXEXTLEN))); |
| 1925 | /* |
| 1926 | * Switch out based on the contiguity flags. |
| 1927 | */ |
| 1928 | switch (SWITCH_STATE) { |
| 1929 | |
| 1930 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 1931 | /* |
| 1932 | * New allocation is contiguous with delayed allocations |
| 1933 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1934 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | */ |
| 1936 | temp = left.br_blockcount + new->br_blockcount + |
| 1937 | right.br_blockcount; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1938 | XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1940 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1941 | oldlen = startblockval(left.br_startblock) + |
| 1942 | startblockval(new->br_startblock) + |
| 1943 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1945 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1946 | nullstartblock((int)newlen)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1947 | XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | XFS_DATA_FORK); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1949 | XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1950 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1952 | /* DELTA: Two in-core extents were replaced by one. */ |
| 1953 | temp2 = temp; |
| 1954 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | break; |
| 1956 | |
| 1957 | case MASK(LEFT_CONTIG): |
| 1958 | /* |
| 1959 | * New allocation is contiguous with a delayed allocation |
| 1960 | * on the left. |
| 1961 | * Merge the new allocation with the left neighbor. |
| 1962 | */ |
| 1963 | temp = left.br_blockcount + new->br_blockcount; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1964 | XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1966 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1967 | oldlen = startblockval(left.br_startblock) + |
| 1968 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1970 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1971 | nullstartblock((int)newlen)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1972 | XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | XFS_DATA_FORK); |
| 1974 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1975 | /* DELTA: One in-core extent grew into a hole. */ |
| 1976 | temp2 = temp; |
| 1977 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | break; |
| 1979 | |
| 1980 | case MASK(RIGHT_CONTIG): |
| 1981 | /* |
| 1982 | * New allocation is contiguous with a delayed allocation |
| 1983 | * on the right. |
| 1984 | * Merge the new allocation with the right neighbor. |
| 1985 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1986 | XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1988 | oldlen = startblockval(new->br_startblock) + |
| 1989 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | newlen = xfs_bmap_worst_indlen(ip, temp); |
| 1991 | xfs_bmbt_set_allf(ep, new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1992 | nullstartblock((int)newlen), temp, right.br_state); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 1993 | XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1995 | /* DELTA: One in-core extent grew into a hole. */ |
| 1996 | temp2 = temp; |
| 1997 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | break; |
| 1999 | |
| 2000 | case 0: |
| 2001 | /* |
| 2002 | * New allocation is not contiguous with another |
| 2003 | * delayed allocation. |
| 2004 | * Insert a new entry. |
| 2005 | */ |
| 2006 | oldlen = newlen = 0; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2007 | XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2009 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2011 | /* DELTA: A new in-core extent was added in a hole. */ |
| 2012 | temp2 = new->br_blockcount; |
| 2013 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | break; |
| 2015 | } |
| 2016 | if (oldlen != newlen) { |
| 2017 | ASSERT(oldlen > newlen); |
| 2018 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 2019 | (int64_t)(oldlen - newlen), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | /* |
| 2021 | * Nothing to do for disk quota accounting here. |
| 2022 | */ |
| 2023 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2024 | if (delta) { |
| 2025 | temp2 += temp; |
| 2026 | if (delta->xed_startoff > temp) |
| 2027 | delta->xed_startoff = temp; |
| 2028 | if (delta->xed_blockcount < temp2) |
| 2029 | delta->xed_blockcount = temp2; |
| 2030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | *logflagsp = 0; |
| 2032 | return 0; |
| 2033 | #undef MASK |
| 2034 | #undef MASK2 |
| 2035 | #undef STATE_SET |
| 2036 | #undef STATE_TEST |
| 2037 | #undef STATE_SET_TEST |
| 2038 | #undef SWITCH_STATE |
| 2039 | } |
| 2040 | |
| 2041 | /* |
| 2042 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 2043 | * to a real allocation. |
| 2044 | */ |
| 2045 | STATIC int /* error */ |
| 2046 | xfs_bmap_add_extent_hole_real( |
| 2047 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2048 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 2049 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2050 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2052 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | int whichfork) /* data or attr fork */ |
| 2054 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2055 | xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | int i; /* temp state */ |
| 2058 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2059 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2060 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2061 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2063 | xfs_filblks_t temp=0; |
| 2064 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | enum { /* bit number definitions for state */ |
| 2066 | LEFT_CONTIG, RIGHT_CONTIG, |
| 2067 | LEFT_DELAY, RIGHT_DELAY, |
| 2068 | LEFT_VALID, RIGHT_VALID |
| 2069 | }; |
| 2070 | |
| 2071 | #define MASK(b) (1 << (b)) |
| 2072 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 2073 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 2074 | #define STATE_TEST(b) (state & MASK(b)) |
| 2075 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 2076 | ((state &= ~MASK(b)), 0)) |
| 2077 | #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG)) |
| 2078 | |
| 2079 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2080 | ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2081 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | state = 0; |
| 2083 | /* |
| 2084 | * Check and set flags if this segment has a left neighbor. |
| 2085 | */ |
| 2086 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2087 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2088 | STATE_SET(LEFT_DELAY, isnullstartblock(left.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | } |
| 2090 | /* |
| 2091 | * Check and set flags if this segment has a current value. |
| 2092 | * Not true if we're inserting into the "hole" at eof. |
| 2093 | */ |
| 2094 | if (STATE_SET_TEST(RIGHT_VALID, |
| 2095 | idx < |
| 2096 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 2097 | xfs_bmbt_get_all(ep, &right); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2098 | STATE_SET(RIGHT_DELAY, isnullstartblock(right.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | } |
| 2100 | /* |
| 2101 | * We're inserting a real allocation between "left" and "right". |
| 2102 | * Set the contiguity flags. Don't let extents get too large. |
| 2103 | */ |
| 2104 | STATE_SET(LEFT_CONTIG, |
| 2105 | STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) && |
| 2106 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2107 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2108 | left.br_state == new->br_state && |
| 2109 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 2110 | STATE_SET(RIGHT_CONTIG, |
| 2111 | STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) && |
| 2112 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2113 | new->br_startblock + new->br_blockcount == |
| 2114 | right.br_startblock && |
| 2115 | new->br_state == right.br_state && |
| 2116 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2117 | (!STATE_TEST(LEFT_CONTIG) || |
| 2118 | left.br_blockcount + new->br_blockcount + |
| 2119 | right.br_blockcount <= MAXEXTLEN)); |
| 2120 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2121 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | /* |
| 2123 | * Select which case we're in here, and implement it. |
| 2124 | */ |
| 2125 | switch (SWITCH_STATE) { |
| 2126 | |
| 2127 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 2128 | /* |
| 2129 | * New allocation is contiguous with real allocations on the |
| 2130 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2131 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2133 | XFS_BMAP_TRACE_PRE_UPDATE("LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2135 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | left.br_blockcount + new->br_blockcount + |
| 2137 | right.br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2138 | XFS_BMAP_TRACE_POST_UPDATE("LC|RC", ip, idx - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | whichfork); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2140 | XFS_BMAP_TRACE_DELETE("LC|RC", ip, idx, 1, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2141 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | ifp->if_lastex = idx - 1; |
| 2143 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2144 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2145 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2146 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2147 | } else { |
| 2148 | rval = XFS_ILOG_CORE; |
| 2149 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2150 | right.br_startoff, |
| 2151 | right.br_startblock, |
| 2152 | right.br_blockcount, &i))) |
| 2153 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2154 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2155 | if ((error = xfs_btree_delete(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2156 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2157 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2158 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2159 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2160 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2161 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2162 | left.br_startblock, |
| 2163 | left.br_blockcount + |
| 2164 | new->br_blockcount + |
| 2165 | right.br_blockcount, |
| 2166 | left.br_state))) |
| 2167 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2169 | /* DELTA: Two in-core extents were replaced by one. */ |
| 2170 | temp = left.br_startoff; |
| 2171 | temp2 = left.br_blockcount + |
| 2172 | new->br_blockcount + |
| 2173 | right.br_blockcount; |
| 2174 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
| 2176 | case MASK(LEFT_CONTIG): |
| 2177 | /* |
| 2178 | * New allocation is contiguous with a real allocation |
| 2179 | * on the left. |
| 2180 | * Merge the new allocation with the left neighbor. |
| 2181 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2182 | XFS_BMAP_TRACE_PRE_UPDATE("LC", ip, idx - 1, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2183 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | left.br_blockcount + new->br_blockcount); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2185 | XFS_BMAP_TRACE_POST_UPDATE("LC", ip, idx - 1, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | ifp->if_lastex = idx - 1; |
| 2187 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2188 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2189 | } else { |
| 2190 | rval = 0; |
| 2191 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2192 | left.br_startoff, |
| 2193 | left.br_startblock, |
| 2194 | left.br_blockcount, &i))) |
| 2195 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2196 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2197 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2198 | left.br_startblock, |
| 2199 | left.br_blockcount + |
| 2200 | new->br_blockcount, |
| 2201 | left.br_state))) |
| 2202 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2204 | /* DELTA: One in-core extent grew. */ |
| 2205 | temp = left.br_startoff; |
| 2206 | temp2 = left.br_blockcount + |
| 2207 | new->br_blockcount; |
| 2208 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | |
| 2210 | case MASK(RIGHT_CONTIG): |
| 2211 | /* |
| 2212 | * New allocation is contiguous with a real allocation |
| 2213 | * on the right. |
| 2214 | * Merge the new allocation with the right neighbor. |
| 2215 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2216 | XFS_BMAP_TRACE_PRE_UPDATE("RC", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock, |
| 2218 | new->br_blockcount + right.br_blockcount, |
| 2219 | right.br_state); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2220 | XFS_BMAP_TRACE_POST_UPDATE("RC", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | ifp->if_lastex = idx; |
| 2222 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2223 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2224 | } else { |
| 2225 | rval = 0; |
| 2226 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2227 | right.br_startoff, |
| 2228 | right.br_startblock, |
| 2229 | right.br_blockcount, &i))) |
| 2230 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2231 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2232 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2233 | new->br_startblock, |
| 2234 | new->br_blockcount + |
| 2235 | right.br_blockcount, |
| 2236 | right.br_state))) |
| 2237 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2239 | /* DELTA: One in-core extent grew. */ |
| 2240 | temp = new->br_startoff; |
| 2241 | temp2 = new->br_blockcount + |
| 2242 | right.br_blockcount; |
| 2243 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
| 2245 | case 0: |
| 2246 | /* |
| 2247 | * New allocation is not contiguous with another |
| 2248 | * real allocation. |
| 2249 | * Insert a new entry. |
| 2250 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 2251 | XFS_BMAP_TRACE_INSERT("0", ip, idx, 1, new, NULL, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2252 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | ifp->if_lastex = idx; |
| 2254 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2255 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2256 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2257 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2258 | } else { |
| 2259 | rval = XFS_ILOG_CORE; |
| 2260 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2261 | new->br_startoff, |
| 2262 | new->br_startblock, |
| 2263 | new->br_blockcount, &i))) |
| 2264 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2265 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2266 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2267 | if ((error = xfs_btree_insert(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2268 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2269 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2271 | /* DELTA: A new extent was added in a hole. */ |
| 2272 | temp = new->br_startoff; |
| 2273 | temp2 = new->br_blockcount; |
| 2274 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2276 | if (delta) { |
| 2277 | temp2 += temp; |
| 2278 | if (delta->xed_startoff > temp) |
| 2279 | delta->xed_startoff = temp; |
| 2280 | if (delta->xed_blockcount < temp2) |
| 2281 | delta->xed_blockcount = temp2; |
| 2282 | } |
| 2283 | done: |
| 2284 | *logflagsp = rval; |
| 2285 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | #undef MASK |
| 2287 | #undef MASK2 |
| 2288 | #undef STATE_SET |
| 2289 | #undef STATE_TEST |
| 2290 | #undef STATE_SET_TEST |
| 2291 | #undef SWITCH_STATE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2294 | /* |
| 2295 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2296 | */ |
| 2297 | STATIC int |
| 2298 | xfs_bmap_extsize_align( |
| 2299 | xfs_mount_t *mp, |
| 2300 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2301 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2302 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2303 | int rt, /* is this a realtime inode? */ |
| 2304 | int eof, /* is extent at end-of-file? */ |
| 2305 | int delay, /* creating delalloc extent? */ |
| 2306 | int convert, /* overwriting unwritten extent? */ |
| 2307 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2308 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2309 | { |
| 2310 | xfs_fileoff_t orig_off; /* original offset */ |
| 2311 | xfs_extlen_t orig_alen; /* original length */ |
| 2312 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2313 | xfs_fileoff_t nexto; /* next file offset */ |
| 2314 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2315 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2316 | xfs_extlen_t align_alen; /* temp for length */ |
| 2317 | xfs_extlen_t temp; /* temp for calculations */ |
| 2318 | |
| 2319 | if (convert) |
| 2320 | return 0; |
| 2321 | |
| 2322 | orig_off = align_off = *offp; |
| 2323 | orig_alen = align_alen = *lenp; |
| 2324 | orig_end = orig_off + orig_alen; |
| 2325 | |
| 2326 | /* |
| 2327 | * If this request overlaps an existing extent, then don't |
| 2328 | * attempt to perform any additional alignment. |
| 2329 | */ |
| 2330 | if (!delay && !eof && |
| 2331 | (orig_off >= gotp->br_startoff) && |
| 2332 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2333 | return 0; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * If the file offset is unaligned vs. the extent size |
| 2338 | * we need to align it. This will be possible unless |
| 2339 | * the file was previously written with a kernel that didn't |
| 2340 | * perform this alignment, or if a truncate shot us in the |
| 2341 | * foot. |
| 2342 | */ |
| 2343 | temp = do_mod(orig_off, extsz); |
| 2344 | if (temp) { |
| 2345 | align_alen += temp; |
| 2346 | align_off -= temp; |
| 2347 | } |
| 2348 | /* |
| 2349 | * Same adjustment for the end of the requested area. |
| 2350 | */ |
| 2351 | if ((temp = (align_alen % extsz))) { |
| 2352 | align_alen += extsz - temp; |
| 2353 | } |
| 2354 | /* |
| 2355 | * If the previous block overlaps with this proposed allocation |
| 2356 | * then move the start forward without adjusting the length. |
| 2357 | */ |
| 2358 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2359 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2360 | prevo = prevp->br_startoff; |
| 2361 | else |
| 2362 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2363 | } else |
| 2364 | prevo = 0; |
| 2365 | if (align_off != orig_off && align_off < prevo) |
| 2366 | align_off = prevo; |
| 2367 | /* |
| 2368 | * If the next block overlaps with this proposed allocation |
| 2369 | * then move the start back without adjusting the length, |
| 2370 | * but not before offset 0. |
| 2371 | * This may of course make the start overlap previous block, |
| 2372 | * and if we hit the offset 0 limit then the next block |
| 2373 | * can still overlap too. |
| 2374 | */ |
| 2375 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2376 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2377 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2378 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2379 | else |
| 2380 | nexto = gotp->br_startoff; |
| 2381 | } else |
| 2382 | nexto = NULLFILEOFF; |
| 2383 | if (!eof && |
| 2384 | align_off + align_alen != orig_end && |
| 2385 | align_off + align_alen > nexto) |
| 2386 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2387 | /* |
| 2388 | * If we're now overlapping the next or previous extent that |
| 2389 | * means we can't fit an extsz piece in this hole. Just move |
| 2390 | * the start forward to the first valid spot and set |
| 2391 | * the length so we hit the end. |
| 2392 | */ |
| 2393 | if (align_off != orig_off && align_off < prevo) |
| 2394 | align_off = prevo; |
| 2395 | if (align_off + align_alen != orig_end && |
| 2396 | align_off + align_alen > nexto && |
| 2397 | nexto != NULLFILEOFF) { |
| 2398 | ASSERT(nexto > prevo); |
| 2399 | align_alen = nexto - align_off; |
| 2400 | } |
| 2401 | |
| 2402 | /* |
| 2403 | * If realtime, and the result isn't a multiple of the realtime |
| 2404 | * extent size we need to remove blocks until it is. |
| 2405 | */ |
| 2406 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2407 | /* |
| 2408 | * We're not covering the original request, or |
| 2409 | * we won't be able to once we fix the length. |
| 2410 | */ |
| 2411 | if (orig_off < align_off || |
| 2412 | orig_end > align_off + align_alen || |
| 2413 | align_alen - temp < orig_alen) |
| 2414 | return XFS_ERROR(EINVAL); |
| 2415 | /* |
| 2416 | * Try to fix it by moving the start up. |
| 2417 | */ |
| 2418 | if (align_off + temp <= orig_off) { |
| 2419 | align_alen -= temp; |
| 2420 | align_off += temp; |
| 2421 | } |
| 2422 | /* |
| 2423 | * Try to fix it by moving the end in. |
| 2424 | */ |
| 2425 | else if (align_off + align_alen - temp >= orig_end) |
| 2426 | align_alen -= temp; |
| 2427 | /* |
| 2428 | * Set the start to the minimum then trim the length. |
| 2429 | */ |
| 2430 | else { |
| 2431 | align_alen -= orig_off - align_off; |
| 2432 | align_off = orig_off; |
| 2433 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 2434 | } |
| 2435 | /* |
| 2436 | * Result doesn't cover the request, fail it. |
| 2437 | */ |
| 2438 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2439 | return XFS_ERROR(EINVAL); |
| 2440 | } else { |
| 2441 | ASSERT(orig_off >= align_off); |
| 2442 | ASSERT(orig_end <= align_off + align_alen); |
| 2443 | } |
| 2444 | |
| 2445 | #ifdef DEBUG |
| 2446 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2447 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2448 | if (prevp->br_startoff != NULLFILEOFF) |
| 2449 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2450 | #endif |
| 2451 | |
| 2452 | *lenp = align_alen; |
| 2453 | *offp = align_off; |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2458 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2459 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2460 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2462 | { |
| 2463 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2465 | xfs_mount_t *mp; /* mount point structure */ |
| 2466 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2467 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | |
| 2469 | #define ISVALID(x,y) \ |
| 2470 | (rt ? \ |
| 2471 | (x) < mp->m_sb.sb_rblocks : \ |
| 2472 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2473 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2474 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | mp = ap->ip->i_mount; |
| 2477 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2478 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
| 2479 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | /* |
| 2481 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame^] | 2482 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | */ |
| 2484 | if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2485 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, |
| 2487 | ap->prevp->br_startblock)) { |
| 2488 | ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; |
| 2489 | /* |
| 2490 | * Adjust for the gap between prevp and us. |
| 2491 | */ |
| 2492 | adjust = ap->off - |
| 2493 | (ap->prevp->br_startoff + ap->prevp->br_blockcount); |
| 2494 | if (adjust && |
| 2495 | ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) |
| 2496 | ap->rval += adjust; |
| 2497 | } |
| 2498 | /* |
| 2499 | * If not at eof, then compare the two neighbor blocks. |
| 2500 | * Figure out whether either one gives us a good starting point, |
| 2501 | * and pick the better one. |
| 2502 | */ |
| 2503 | else if (!ap->eof) { |
| 2504 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2505 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2506 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2507 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2508 | |
| 2509 | /* |
| 2510 | * If there's a previous (left) block, select a requested |
| 2511 | * start block based on it. |
| 2512 | */ |
| 2513 | if (ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2514 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | (prevbno = ap->prevp->br_startblock + |
| 2516 | ap->prevp->br_blockcount) && |
| 2517 | ISVALID(prevbno, ap->prevp->br_startblock)) { |
| 2518 | /* |
| 2519 | * Calculate gap to end of previous block. |
| 2520 | */ |
| 2521 | adjust = prevdiff = ap->off - |
| 2522 | (ap->prevp->br_startoff + |
| 2523 | ap->prevp->br_blockcount); |
| 2524 | /* |
| 2525 | * Figure the startblock based on the previous block's |
| 2526 | * end and the gap size. |
| 2527 | * Heuristic! |
| 2528 | * If the gap is large relative to the piece we're |
| 2529 | * allocating, or using it gives us an invalid block |
| 2530 | * number, then just use the end of the previous block. |
| 2531 | */ |
| 2532 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2533 | ISVALID(prevbno + prevdiff, |
| 2534 | ap->prevp->br_startblock)) |
| 2535 | prevbno += adjust; |
| 2536 | else |
| 2537 | prevdiff += adjust; |
| 2538 | /* |
| 2539 | * If the firstblock forbids it, can't use it, |
| 2540 | * must use default. |
| 2541 | */ |
| 2542 | if (!rt && !nullfb && |
| 2543 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2544 | prevbno = NULLFSBLOCK; |
| 2545 | } |
| 2546 | /* |
| 2547 | * No previous block or can't follow it, just default. |
| 2548 | */ |
| 2549 | else |
| 2550 | prevbno = NULLFSBLOCK; |
| 2551 | /* |
| 2552 | * If there's a following (right) block, select a requested |
| 2553 | * start block based on it. |
| 2554 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2555 | if (!isnullstartblock(ap->gotp->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | /* |
| 2557 | * Calculate gap to start of next block. |
| 2558 | */ |
| 2559 | adjust = gotdiff = ap->gotp->br_startoff - ap->off; |
| 2560 | /* |
| 2561 | * Figure the startblock based on the next block's |
| 2562 | * start and the gap size. |
| 2563 | */ |
| 2564 | gotbno = ap->gotp->br_startblock; |
| 2565 | /* |
| 2566 | * Heuristic! |
| 2567 | * If the gap is large relative to the piece we're |
| 2568 | * allocating, or using it gives us an invalid block |
| 2569 | * number, then just use the start of the next block |
| 2570 | * offset by our length. |
| 2571 | */ |
| 2572 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2573 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2574 | gotbno -= adjust; |
| 2575 | else if (ISVALID(gotbno - ap->alen, gotbno)) { |
| 2576 | gotbno -= ap->alen; |
| 2577 | gotdiff += adjust - ap->alen; |
| 2578 | } else |
| 2579 | gotdiff += adjust; |
| 2580 | /* |
| 2581 | * If the firstblock forbids it, can't use it, |
| 2582 | * must use default. |
| 2583 | */ |
| 2584 | if (!rt && !nullfb && |
| 2585 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2586 | gotbno = NULLFSBLOCK; |
| 2587 | } |
| 2588 | /* |
| 2589 | * No next block, just default. |
| 2590 | */ |
| 2591 | else |
| 2592 | gotbno = NULLFSBLOCK; |
| 2593 | /* |
| 2594 | * If both valid, pick the better one, else the only good |
| 2595 | * one, else ap->rval is already set (to 0 or the inode block). |
| 2596 | */ |
| 2597 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
| 2598 | ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; |
| 2599 | else if (prevbno != NULLFSBLOCK) |
| 2600 | ap->rval = prevbno; |
| 2601 | else if (gotbno != NULLFSBLOCK) |
| 2602 | ap->rval = gotbno; |
| 2603 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2604 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | STATIC int |
| 2608 | xfs_bmap_rtalloc( |
| 2609 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2610 | { |
| 2611 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2612 | int error; /* error return value */ |
| 2613 | xfs_mount_t *mp; /* mount point structure */ |
| 2614 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2615 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2616 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2617 | xfs_rtblock_t rtb; |
| 2618 | |
| 2619 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2620 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2621 | prod = align / mp->m_sb.sb_rextsize; |
| 2622 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2623 | align, 1, ap->eof, 0, |
| 2624 | ap->conv, &ap->off, &ap->alen); |
| 2625 | if (error) |
| 2626 | return error; |
| 2627 | ASSERT(ap->alen); |
| 2628 | ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); |
| 2629 | |
| 2630 | /* |
| 2631 | * If the offset & length are not perfectly aligned |
| 2632 | * then kill prod, it will just get us in trouble. |
| 2633 | */ |
| 2634 | if (do_mod(ap->off, align) || ap->alen % align) |
| 2635 | prod = 1; |
| 2636 | /* |
| 2637 | * Set ralen to be the actual requested length in rtextents. |
| 2638 | */ |
| 2639 | ralen = ap->alen / mp->m_sb.sb_rextsize; |
| 2640 | /* |
| 2641 | * If the old value was close enough to MAXEXTLEN that |
| 2642 | * we rounded up to it, cut it back so it's valid again. |
| 2643 | * Note that if it's a really large request (bigger than |
| 2644 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2645 | * adjust the starting point to match it. |
| 2646 | */ |
| 2647 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2648 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
| 2649 | /* |
| 2650 | * If it's an allocation to an empty file at offset 0, |
| 2651 | * pick an extent that will space things out in the rt area. |
| 2652 | */ |
| 2653 | if (ap->eof && ap->off == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2654 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2655 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2656 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2657 | if (error) |
| 2658 | return error; |
| 2659 | ap->rval = rtx * mp->m_sb.sb_rextsize; |
| 2660 | } else { |
| 2661 | ap->rval = 0; |
| 2662 | } |
| 2663 | |
| 2664 | xfs_bmap_adjacent(ap); |
| 2665 | |
| 2666 | /* |
| 2667 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2668 | */ |
| 2669 | atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2670 | do_div(ap->rval, mp->m_sb.sb_rextsize); |
| 2671 | rtb = ap->rval; |
| 2672 | ap->alen = ralen; |
| 2673 | if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, |
| 2674 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2675 | return error; |
| 2676 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 2677 | (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, |
| 2678 | ap->alen, &ralen, atype, |
| 2679 | ap->wasdel, 1, &rtb))) |
| 2680 | return error; |
| 2681 | ap->rval = rtb; |
| 2682 | if (ap->rval != NULLFSBLOCK) { |
| 2683 | ap->rval *= mp->m_sb.sb_rextsize; |
| 2684 | ralen *= mp->m_sb.sb_rextsize; |
| 2685 | ap->alen = ralen; |
| 2686 | ap->ip->i_d.di_nblocks += ralen; |
| 2687 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2688 | if (ap->wasdel) |
| 2689 | ap->ip->i_delayed_blks -= ralen; |
| 2690 | /* |
| 2691 | * Adjust the disk quota also. This was reserved |
| 2692 | * earlier. |
| 2693 | */ |
| 2694 | XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip, |
| 2695 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2696 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2697 | } else { |
| 2698 | ap->alen = 0; |
| 2699 | } |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | STATIC int |
| 2704 | xfs_bmap_btalloc( |
| 2705 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2706 | { |
| 2707 | xfs_mount_t *mp; /* mount point structure */ |
| 2708 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2709 | xfs_extlen_t align; /* minimum allocation alignment */ |
| 2710 | xfs_agnumber_t ag; |
| 2711 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2712 | xfs_agnumber_t startag; |
| 2713 | xfs_alloc_arg_t args; |
| 2714 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2715 | xfs_extlen_t nextminlen = 0; |
| 2716 | xfs_perag_t *pag; |
| 2717 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2718 | int isaligned; |
| 2719 | int notinit; |
| 2720 | int tryagain; |
| 2721 | int error; |
| 2722 | |
| 2723 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2724 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2725 | if (unlikely(align)) { |
| 2726 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2727 | align, 0, ap->eof, 0, ap->conv, |
| 2728 | &ap->off, &ap->alen); |
| 2729 | ASSERT(!error); |
| 2730 | ASSERT(ap->alen); |
| 2731 | } |
| 2732 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2733 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2734 | if (nullfb) { |
| 2735 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2736 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2737 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
| 2738 | ap->rval = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2739 | } else { |
| 2740 | ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
| 2741 | } |
| 2742 | } else |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2743 | ap->rval = ap->firstblock; |
| 2744 | |
| 2745 | xfs_bmap_adjacent(ap); |
| 2746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | /* |
| 2748 | * If allowed, use ap->rval; otherwise must use firstblock since |
| 2749 | * it's in the right allocation group. |
| 2750 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2751 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | ; |
| 2753 | else |
| 2754 | ap->rval = ap->firstblock; |
| 2755 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | * Normal allocation, done through xfs_alloc_vextent. |
| 2757 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2758 | tryagain = isaligned = 0; |
| 2759 | args.tp = ap->tp; |
| 2760 | args.mp = mp; |
| 2761 | args.fsbno = ap->rval; |
| 2762 | args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks); |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2763 | args.firstblock = ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2764 | blen = 0; |
| 2765 | if (nullfb) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2766 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2767 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2768 | else |
| 2769 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2770 | args.total = ap->total; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2773 | * Search for an allocation group with a single extent |
| 2774 | * large enough for the request. |
| 2775 | * |
| 2776 | * If one isn't found, then adjust the minimum allocation |
| 2777 | * size to the largest space found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2779 | startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2780 | if (startag == NULLAGNUMBER) |
| 2781 | startag = ag = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2782 | notinit = 0; |
| 2783 | down_read(&mp->m_peraglock); |
| 2784 | while (blen < ap->alen) { |
| 2785 | pag = &mp->m_perag[ag]; |
| 2786 | if (!pag->pagf_init && |
| 2787 | (error = xfs_alloc_pagf_init(mp, args.tp, |
| 2788 | ag, XFS_ALLOC_FLAG_TRYLOCK))) { |
| 2789 | up_read(&mp->m_peraglock); |
| 2790 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2792 | /* |
| 2793 | * See xfs_alloc_fix_freelist... |
| 2794 | */ |
| 2795 | if (pag->pagf_init) { |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 2796 | xfs_extlen_t longest; |
| 2797 | longest = xfs_alloc_longest_free_extent(mp, pag); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2798 | if (blen < longest) |
| 2799 | blen = longest; |
| 2800 | } else |
| 2801 | notinit = 1; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2802 | |
| 2803 | if (xfs_inode_is_filestream(ap->ip)) { |
| 2804 | if (blen >= ap->alen) |
| 2805 | break; |
| 2806 | |
| 2807 | if (ap->userdata) { |
| 2808 | /* |
| 2809 | * If startag is an invalid AG, we've |
| 2810 | * come here once before and |
| 2811 | * xfs_filestream_new_ag picked the |
| 2812 | * best currently available. |
| 2813 | * |
| 2814 | * Don't continue looping, since we |
| 2815 | * could loop forever. |
| 2816 | */ |
| 2817 | if (startag == NULLAGNUMBER) |
| 2818 | break; |
| 2819 | |
| 2820 | error = xfs_filestream_new_ag(ap, &ag); |
| 2821 | if (error) { |
| 2822 | up_read(&mp->m_peraglock); |
| 2823 | return error; |
| 2824 | } |
| 2825 | |
| 2826 | /* loop again to set 'blen'*/ |
| 2827 | startag = NULLAGNUMBER; |
| 2828 | continue; |
| 2829 | } |
| 2830 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2831 | if (++ag == mp->m_sb.sb_agcount) |
| 2832 | ag = 0; |
| 2833 | if (ag == startag) |
| 2834 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2836 | up_read(&mp->m_peraglock); |
| 2837 | /* |
| 2838 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2839 | * possible that there is space for this request. |
| 2840 | */ |
| 2841 | if (notinit || blen < ap->minlen) |
| 2842 | args.minlen = ap->minlen; |
| 2843 | /* |
| 2844 | * If the best seen length is less than the request |
| 2845 | * length, use the best as the minimum. |
| 2846 | */ |
| 2847 | else if (blen < ap->alen) |
| 2848 | args.minlen = blen; |
| 2849 | /* |
| 2850 | * Otherwise we've seen an extent as big as alen, |
| 2851 | * use that as the minimum. |
| 2852 | */ |
| 2853 | else |
| 2854 | args.minlen = ap->alen; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2855 | |
| 2856 | /* |
| 2857 | * set the failure fallback case to look in the selected |
| 2858 | * AG as the stream may have moved. |
| 2859 | */ |
| 2860 | if (xfs_inode_is_filestream(ap->ip)) |
| 2861 | ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2862 | } else if (ap->low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2863 | if (xfs_inode_is_filestream(ap->ip)) |
| 2864 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2865 | else |
| 2866 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2867 | args.total = args.minlen = ap->minlen; |
| 2868 | } else { |
| 2869 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2870 | args.total = ap->total; |
| 2871 | args.minlen = ap->minlen; |
| 2872 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2873 | /* apply extent size hints if obtained earlier */ |
| 2874 | if (unlikely(align)) { |
| 2875 | args.prod = align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2876 | if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) |
| 2877 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2878 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2879 | args.prod = 1; |
| 2880 | args.mod = 0; |
| 2881 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2882 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2883 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) |
| 2884 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2885 | } |
| 2886 | /* |
| 2887 | * If we are not low on available data blocks, and the |
| 2888 | * underlying logical volume manager is a stripe, and |
| 2889 | * the file offset is zero then try to allocate data |
| 2890 | * blocks on stripe unit boundary. |
| 2891 | * NOTE: ap->aeof is only set if the allocation length |
| 2892 | * is >= the stripe unit and the allocation offset is |
| 2893 | * at the end of file. |
| 2894 | */ |
| 2895 | if (!ap->low && ap->aeof) { |
| 2896 | if (!ap->off) { |
| 2897 | args.alignment = mp->m_dalign; |
| 2898 | atype = args.type; |
| 2899 | isaligned = 1; |
| 2900 | /* |
| 2901 | * Adjust for alignment |
| 2902 | */ |
| 2903 | if (blen > args.alignment && blen <= ap->alen) |
| 2904 | args.minlen = blen - args.alignment; |
| 2905 | args.minalignslop = 0; |
| 2906 | } else { |
| 2907 | /* |
| 2908 | * First try an exact bno allocation. |
| 2909 | * If it fails then do a near or start bno |
| 2910 | * allocation with alignment turned on. |
| 2911 | */ |
| 2912 | atype = args.type; |
| 2913 | tryagain = 1; |
| 2914 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2915 | args.alignment = 1; |
| 2916 | /* |
| 2917 | * Compute the minlen+alignment for the |
| 2918 | * next case. Set slop so that the value |
| 2919 | * of minlen+alignment+slop doesn't go up |
| 2920 | * between the calls. |
| 2921 | */ |
| 2922 | if (blen > mp->m_dalign && blen <= ap->alen) |
| 2923 | nextminlen = blen - mp->m_dalign; |
| 2924 | else |
| 2925 | nextminlen = args.minlen; |
| 2926 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2927 | args.minalignslop = |
| 2928 | nextminlen + mp->m_dalign - |
| 2929 | args.minlen - 1; |
| 2930 | else |
| 2931 | args.minalignslop = 0; |
| 2932 | } |
| 2933 | } else { |
| 2934 | args.alignment = 1; |
| 2935 | args.minalignslop = 0; |
| 2936 | } |
| 2937 | args.minleft = ap->minleft; |
| 2938 | args.wasdel = ap->wasdel; |
| 2939 | args.isfl = 0; |
| 2940 | args.userdata = ap->userdata; |
| 2941 | if ((error = xfs_alloc_vextent(&args))) |
| 2942 | return error; |
| 2943 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2944 | /* |
| 2945 | * Exact allocation failed. Now try with alignment |
| 2946 | * turned on. |
| 2947 | */ |
| 2948 | args.type = atype; |
| 2949 | args.fsbno = ap->rval; |
| 2950 | args.alignment = mp->m_dalign; |
| 2951 | args.minlen = nextminlen; |
| 2952 | args.minalignslop = 0; |
| 2953 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | if ((error = xfs_alloc_vextent(&args))) |
| 2955 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2956 | } |
| 2957 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2958 | /* |
| 2959 | * allocation failed, so turn off alignment and |
| 2960 | * try again. |
| 2961 | */ |
| 2962 | args.type = atype; |
| 2963 | args.fsbno = ap->rval; |
| 2964 | args.alignment = 0; |
| 2965 | if ((error = xfs_alloc_vextent(&args))) |
| 2966 | return error; |
| 2967 | } |
| 2968 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2969 | args.minlen > ap->minlen) { |
| 2970 | args.minlen = ap->minlen; |
| 2971 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2972 | args.fsbno = ap->rval; |
| 2973 | if ((error = xfs_alloc_vextent(&args))) |
| 2974 | return error; |
| 2975 | } |
| 2976 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2977 | args.fsbno = 0; |
| 2978 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2979 | args.total = ap->minlen; |
| 2980 | args.minleft = 0; |
| 2981 | if ((error = xfs_alloc_vextent(&args))) |
| 2982 | return error; |
| 2983 | ap->low = 1; |
| 2984 | } |
| 2985 | if (args.fsbno != NULLFSBLOCK) { |
| 2986 | ap->firstblock = ap->rval = args.fsbno; |
| 2987 | ASSERT(nullfb || fb_agno == args.agno || |
| 2988 | (ap->low && fb_agno < args.agno)); |
| 2989 | ap->alen = args.len; |
| 2990 | ap->ip->i_d.di_nblocks += args.len; |
| 2991 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2992 | if (ap->wasdel) |
| 2993 | ap->ip->i_delayed_blks -= args.len; |
| 2994 | /* |
| 2995 | * Adjust the disk quota also. This was reserved |
| 2996 | * earlier. |
| 2997 | */ |
| 2998 | XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip, |
| 2999 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 3000 | XFS_TRANS_DQ_BCOUNT, |
| 3001 | (long) args.len); |
| 3002 | } else { |
| 3003 | ap->rval = NULLFSBLOCK; |
| 3004 | ap->alen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | } |
| 3006 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | /* |
| 3010 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 3011 | * It figures out where to ask the underlying allocator to put the new extent. |
| 3012 | */ |
| 3013 | STATIC int |
| 3014 | xfs_bmap_alloc( |
| 3015 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 3016 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3017 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 3018 | return xfs_bmap_rtalloc(ap); |
| 3019 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * Transform a btree format file with only one leaf node, where the |
| 3024 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3025 | * 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] | 3026 | * give up the space for the btree root and pitch the leaf block. |
| 3027 | */ |
| 3028 | STATIC int /* error */ |
| 3029 | xfs_bmap_btree_to_extents( |
| 3030 | xfs_trans_t *tp, /* transaction pointer */ |
| 3031 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3032 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 3033 | int *logflagsp, /* inode logging flags */ |
| 3034 | int whichfork) /* data or attr fork */ |
| 3035 | { |
| 3036 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3037 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | xfs_fsblock_t cbno; /* child block number */ |
| 3039 | xfs_buf_t *cbp; /* child block's buffer */ |
| 3040 | int error; /* error return value */ |
| 3041 | xfs_ifork_t *ifp; /* inode fork data */ |
| 3042 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3043 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3044 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3046 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3048 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 3049 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 3050 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3051 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 3052 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3053 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 3054 | 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] | 3055 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | *logflagsp = 0; |
| 3057 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3058 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | return error; |
| 3060 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 3062 | XFS_BMAP_BTREE_REF))) |
| 3063 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3064 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 3065 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | return error; |
| 3067 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 3068 | ip->i_d.di_nblocks--; |
| 3069 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 3070 | xfs_trans_binval(tp, cbp); |
| 3071 | if (cur->bc_bufs[0] == cbp) |
| 3072 | cur->bc_bufs[0] = NULL; |
| 3073 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3074 | ASSERT(ifp->if_broot == NULL); |
| 3075 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 3076 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3077 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | return 0; |
| 3079 | } |
| 3080 | |
| 3081 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3082 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | * after removing space (or undoing a delayed allocation). |
| 3084 | */ |
| 3085 | STATIC int /* error */ |
| 3086 | xfs_bmap_del_extent( |
| 3087 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3088 | xfs_trans_t *tp, /* current transaction pointer */ |
| 3089 | xfs_extnum_t idx, /* extent number to update/delete */ |
| 3090 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 3091 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3092 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3094 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | int whichfork, /* data or attr fork */ |
| 3096 | int rsvd) /* OK to allocate reserved blocks */ |
| 3097 | { |
| 3098 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 3099 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 3100 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 3101 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 3102 | int delay; /* current block is delayed allocated */ |
| 3103 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3104 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | int error; /* error return value */ |
| 3106 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 3108 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 3109 | int i; /* temp state */ |
| 3110 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3111 | xfs_mount_t *mp; /* mount structure */ |
| 3112 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 3113 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 3114 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | uint qfield; /* quota field to update */ |
| 3116 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 3117 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 3118 | |
| 3119 | XFS_STATS_INC(xs_del_exlist); |
| 3120 | mp = ip->i_mount; |
| 3121 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3122 | ASSERT((idx >= 0) && (idx < ifp->if_bytes / |
| 3123 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3124 | ASSERT(del->br_blockcount > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3125 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | xfs_bmbt_get_all(ep, &got); |
| 3127 | ASSERT(got.br_startoff <= del->br_startoff); |
| 3128 | del_endoff = del->br_startoff + del->br_blockcount; |
| 3129 | got_endoff = got.br_startoff + got.br_blockcount; |
| 3130 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3131 | delay = isnullstartblock(got.br_startblock); |
| 3132 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | flags = 0; |
| 3134 | qfield = 0; |
| 3135 | error = 0; |
| 3136 | /* |
| 3137 | * If deleting a real allocation, must free up the disk space. |
| 3138 | */ |
| 3139 | if (!delay) { |
| 3140 | flags = XFS_ILOG_CORE; |
| 3141 | /* |
| 3142 | * Realtime allocation. Free it and record di_nblocks update. |
| 3143 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3144 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | xfs_fsblock_t bno; |
| 3146 | xfs_filblks_t len; |
| 3147 | |
| 3148 | ASSERT(do_mod(del->br_blockcount, |
| 3149 | mp->m_sb.sb_rextsize) == 0); |
| 3150 | ASSERT(do_mod(del->br_startblock, |
| 3151 | mp->m_sb.sb_rextsize) == 0); |
| 3152 | bno = del->br_startblock; |
| 3153 | len = del->br_blockcount; |
| 3154 | do_div(bno, mp->m_sb.sb_rextsize); |
| 3155 | do_div(len, mp->m_sb.sb_rextsize); |
| 3156 | if ((error = xfs_rtfree_extent(ip->i_transp, bno, |
| 3157 | (xfs_extlen_t)len))) |
| 3158 | goto done; |
| 3159 | do_fx = 0; |
| 3160 | nblks = len * mp->m_sb.sb_rextsize; |
| 3161 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 3162 | } |
| 3163 | /* |
| 3164 | * Ordinary allocation. |
| 3165 | */ |
| 3166 | else { |
| 3167 | do_fx = 1; |
| 3168 | nblks = del->br_blockcount; |
| 3169 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 3170 | } |
| 3171 | /* |
| 3172 | * Set up del_endblock and cur for later. |
| 3173 | */ |
| 3174 | del_endblock = del->br_startblock + del->br_blockcount; |
| 3175 | if (cur) { |
| 3176 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 3177 | got.br_startblock, got.br_blockcount, |
| 3178 | &i))) |
| 3179 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3180 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | } |
| 3182 | da_old = da_new = 0; |
| 3183 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3184 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | da_new = 0; |
| 3186 | nblks = 0; |
| 3187 | do_fx = 0; |
| 3188 | } |
| 3189 | /* |
| 3190 | * Set flag value to use in switch statement. |
| 3191 | * Left-contig is 2, right-contig is 1. |
| 3192 | */ |
| 3193 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 3194 | (got_endoff == del_endoff)) { |
| 3195 | case 3: |
| 3196 | /* |
| 3197 | * Matches the whole extent. Delete the entry. |
| 3198 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3199 | XFS_BMAP_TRACE_DELETE("3", ip, idx, 1, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3200 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | ifp->if_lastex = idx; |
| 3202 | if (delay) |
| 3203 | break; |
| 3204 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3205 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 3206 | flags |= XFS_ILOG_CORE; |
| 3207 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3208 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | break; |
| 3210 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3211 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3213 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | break; |
| 3215 | |
| 3216 | case 2: |
| 3217 | /* |
| 3218 | * Deleting the first part of the extent. |
| 3219 | */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3220 | XFS_BMAP_TRACE_PRE_UPDATE("2", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 3222 | temp = got.br_blockcount - del->br_blockcount; |
| 3223 | xfs_bmbt_set_blockcount(ep, temp); |
| 3224 | ifp->if_lastex = idx; |
| 3225 | if (delay) { |
| 3226 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3227 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3228 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3229 | XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | whichfork); |
| 3231 | da_new = temp; |
| 3232 | break; |
| 3233 | } |
| 3234 | xfs_bmbt_set_startblock(ep, del_endblock); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3235 | XFS_BMAP_TRACE_POST_UPDATE("2", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3237 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | break; |
| 3239 | } |
| 3240 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 3241 | got.br_blockcount - del->br_blockcount, |
| 3242 | got.br_state))) |
| 3243 | goto done; |
| 3244 | break; |
| 3245 | |
| 3246 | case 1: |
| 3247 | /* |
| 3248 | * Deleting the last part of the extent. |
| 3249 | */ |
| 3250 | temp = got.br_blockcount - del->br_blockcount; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3251 | XFS_BMAP_TRACE_PRE_UPDATE("1", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | xfs_bmbt_set_blockcount(ep, temp); |
| 3253 | ifp->if_lastex = idx; |
| 3254 | if (delay) { |
| 3255 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3256 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3257 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3258 | XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | whichfork); |
| 3260 | da_new = temp; |
| 3261 | break; |
| 3262 | } |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3263 | XFS_BMAP_TRACE_POST_UPDATE("1", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3265 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3266 | break; |
| 3267 | } |
| 3268 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 3269 | got.br_startblock, |
| 3270 | got.br_blockcount - del->br_blockcount, |
| 3271 | got.br_state))) |
| 3272 | goto done; |
| 3273 | break; |
| 3274 | |
| 3275 | case 0: |
| 3276 | /* |
| 3277 | * Deleting the middle of the extent. |
| 3278 | */ |
| 3279 | temp = del->br_startoff - got.br_startoff; |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3280 | XFS_BMAP_TRACE_PRE_UPDATE("0", ip, idx, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | xfs_bmbt_set_blockcount(ep, temp); |
| 3282 | new.br_startoff = del_endoff; |
| 3283 | temp2 = got_endoff - del_endoff; |
| 3284 | new.br_blockcount = temp2; |
| 3285 | new.br_state = got.br_state; |
| 3286 | if (!delay) { |
| 3287 | new.br_startblock = del_endblock; |
| 3288 | flags |= XFS_ILOG_CORE; |
| 3289 | if (cur) { |
| 3290 | if ((error = xfs_bmbt_update(cur, |
| 3291 | got.br_startoff, |
| 3292 | got.br_startblock, temp, |
| 3293 | got.br_state))) |
| 3294 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 3295 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | goto done; |
| 3297 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3298 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | if (error && error != ENOSPC) |
| 3300 | goto done; |
| 3301 | /* |
| 3302 | * If get no-space back from btree insert, |
| 3303 | * it tried a split, and we have a zero |
| 3304 | * block reservation. |
| 3305 | * Fix up our state and return the error. |
| 3306 | */ |
| 3307 | if (error == ENOSPC) { |
| 3308 | /* |
| 3309 | * Reset the cursor, don't trust |
| 3310 | * it after any insert operation. |
| 3311 | */ |
| 3312 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 3313 | got.br_startoff, |
| 3314 | got.br_startblock, |
| 3315 | temp, &i))) |
| 3316 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3317 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | /* |
| 3319 | * Update the btree record back |
| 3320 | * to the original value. |
| 3321 | */ |
| 3322 | if ((error = xfs_bmbt_update(cur, |
| 3323 | got.br_startoff, |
| 3324 | got.br_startblock, |
| 3325 | got.br_blockcount, |
| 3326 | got.br_state))) |
| 3327 | goto done; |
| 3328 | /* |
| 3329 | * Reset the extent record back |
| 3330 | * to the original value. |
| 3331 | */ |
| 3332 | xfs_bmbt_set_blockcount(ep, |
| 3333 | got.br_blockcount); |
| 3334 | flags = 0; |
| 3335 | error = XFS_ERROR(ENOSPC); |
| 3336 | goto done; |
| 3337 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3338 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3340 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3341 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3342 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3343 | } else { |
| 3344 | ASSERT(whichfork == XFS_DATA_FORK); |
| 3345 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3346 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3348 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | da_new = temp + temp2; |
| 3350 | while (da_new > da_old) { |
| 3351 | if (temp) { |
| 3352 | temp--; |
| 3353 | da_new--; |
| 3354 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3355 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | } |
| 3357 | if (da_new == da_old) |
| 3358 | break; |
| 3359 | if (temp2) { |
| 3360 | temp2--; |
| 3361 | da_new--; |
| 3362 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3363 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | } |
| 3365 | } |
| 3366 | } |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3367 | XFS_BMAP_TRACE_POST_UPDATE("0", ip, idx, whichfork); |
| 3368 | XFS_BMAP_TRACE_INSERT("0", ip, idx + 1, 1, &new, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3370 | xfs_iext_insert(ifp, idx + 1, 1, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | ifp->if_lastex = idx + 1; |
| 3372 | break; |
| 3373 | } |
| 3374 | /* |
| 3375 | * If we need to, add to list of extents to delete. |
| 3376 | */ |
| 3377 | if (do_fx) |
| 3378 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 3379 | mp); |
| 3380 | /* |
| 3381 | * Adjust inode # blocks in the file. |
| 3382 | */ |
| 3383 | if (nblks) |
| 3384 | ip->i_d.di_nblocks -= nblks; |
| 3385 | /* |
| 3386 | * Adjust quota data. |
| 3387 | */ |
| 3388 | if (qfield) |
| 3389 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks); |
| 3390 | |
| 3391 | /* |
| 3392 | * Account for change in delayed indirect blocks. |
| 3393 | * Nothing to do for disk quota accounting here. |
| 3394 | */ |
| 3395 | ASSERT(da_old >= da_new); |
| 3396 | if (da_old > da_new) |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 3397 | xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int64_t)(da_old - da_new), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | rsvd); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3399 | if (delta) { |
| 3400 | /* DELTA: report the original extent. */ |
| 3401 | if (delta->xed_startoff > got.br_startoff) |
| 3402 | delta->xed_startoff = got.br_startoff; |
| 3403 | if (delta->xed_blockcount < got.br_startoff+got.br_blockcount) |
| 3404 | delta->xed_blockcount = got.br_startoff + |
| 3405 | got.br_blockcount; |
| 3406 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3407 | done: |
| 3408 | *logflagsp = flags; |
| 3409 | return error; |
| 3410 | } |
| 3411 | |
| 3412 | /* |
| 3413 | * Remove the entry "free" from the free item list. Prev points to the |
| 3414 | * previous entry, unless "free" is the head of the list. |
| 3415 | */ |
| 3416 | STATIC void |
| 3417 | xfs_bmap_del_free( |
| 3418 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3419 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3420 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3421 | { |
| 3422 | if (prev) |
| 3423 | prev->xbfi_next = free->xbfi_next; |
| 3424 | else |
| 3425 | flist->xbf_first = free->xbfi_next; |
| 3426 | flist->xbf_count--; |
| 3427 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3428 | } |
| 3429 | |
| 3430 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | * Convert an extents-format file into a btree-format file. |
| 3432 | * The new file will have a root block (in the inode) and a single child block. |
| 3433 | */ |
| 3434 | STATIC int /* error */ |
| 3435 | xfs_bmap_extents_to_btree( |
| 3436 | xfs_trans_t *tp, /* transaction pointer */ |
| 3437 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3438 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3439 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3440 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3441 | int wasdel, /* converting a delayed alloc */ |
| 3442 | int *logflagsp, /* inode logging flags */ |
| 3443 | int whichfork) /* data or attr fork */ |
| 3444 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3445 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3447 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3448 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3449 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3451 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3453 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3455 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3456 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3457 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3459 | |
| 3460 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3461 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3462 | ASSERT(ifp->if_ext_max == |
| 3463 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3464 | /* |
| 3465 | * Make space in the inode incore. |
| 3466 | */ |
| 3467 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3468 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | /* |
| 3471 | * Fill in the root. |
| 3472 | */ |
| 3473 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3474 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3475 | block->bb_level = cpu_to_be16(1); |
| 3476 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3477 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3478 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3480 | /* |
| 3481 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3482 | */ |
| 3483 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3484 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | cur->bc_private.b.firstblock = *firstblock; |
| 3486 | cur->bc_private.b.flist = flist; |
| 3487 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3488 | /* |
| 3489 | * Convert to a btree with two levels, one record in root. |
| 3490 | */ |
| 3491 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3492 | args.tp = tp; |
| 3493 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3494 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | if (*firstblock == NULLFSBLOCK) { |
| 3496 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3497 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3498 | } else if (flist->xbf_low) { |
| 3499 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3500 | args.fsbno = *firstblock; |
| 3501 | } else { |
| 3502 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3503 | args.fsbno = *firstblock; |
| 3504 | } |
| 3505 | args.minlen = args.maxlen = args.prod = 1; |
| 3506 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3507 | args.minalignslop = 0; |
| 3508 | args.wasdel = wasdel; |
| 3509 | *logflagsp = 0; |
| 3510 | if ((error = xfs_alloc_vextent(&args))) { |
| 3511 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3512 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3513 | return error; |
| 3514 | } |
| 3515 | /* |
| 3516 | * Allocation can't fail, the space was reserved. |
| 3517 | */ |
| 3518 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3519 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3520 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3521 | (flist->xbf_low && |
| 3522 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3523 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3524 | cur->bc_private.b.allocated++; |
| 3525 | ip->i_d.di_nblocks++; |
| 3526 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 3527 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3528 | /* |
| 3529 | * Fill in the child block. |
| 3530 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3531 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3532 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3534 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3535 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3536 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3538 | for (cnt = i = 0; i < nextents; i++) { |
| 3539 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3540 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3541 | arp->l0 = cpu_to_be64(ep->l0); |
| 3542 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | arp++; cnt++; |
| 3544 | } |
| 3545 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3546 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3547 | xfs_btree_set_numrecs(ablock, cnt); |
| 3548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | /* |
| 3550 | * Fill in the root key and pointer. |
| 3551 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3552 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3553 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3554 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3555 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3556 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3557 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | /* |
| 3560 | * Do all this logging at the end so that |
| 3561 | * the root is at the right level. |
| 3562 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3563 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3564 | 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] | 3565 | ASSERT(*curp == NULL); |
| 3566 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3567 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | return 0; |
| 3569 | } |
| 3570 | |
| 3571 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3572 | * Helper routine to reset inode di_forkoff field when switching |
| 3573 | * attribute fork from local to extent format - we reset it where |
| 3574 | * possible to make space available for inline data fork extents. |
| 3575 | */ |
| 3576 | STATIC void |
| 3577 | xfs_bmap_forkoff_reset( |
| 3578 | xfs_mount_t *mp, |
| 3579 | xfs_inode_t *ip, |
| 3580 | int whichfork) |
| 3581 | { |
| 3582 | if (whichfork == XFS_ATTR_FORK && |
| 3583 | (ip->i_d.di_format != XFS_DINODE_FMT_DEV) && |
| 3584 | (ip->i_d.di_format != XFS_DINODE_FMT_UUID) && |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 3585 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3586 | ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) { |
| 3587 | ip->i_d.di_forkoff = mp->m_attroffset >> 3; |
| 3588 | ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) / |
| 3589 | (uint)sizeof(xfs_bmbt_rec_t); |
| 3590 | ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) / |
| 3591 | (uint)sizeof(xfs_bmbt_rec_t); |
| 3592 | } |
| 3593 | } |
| 3594 | |
| 3595 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | * Convert a local file to an extents file. |
| 3597 | * This code is out of bounds for data forks of regular files, |
| 3598 | * since the file data needs to get logged so things will stay consistent. |
| 3599 | * (The bmap-level manipulations are ok, though). |
| 3600 | */ |
| 3601 | STATIC int /* error */ |
| 3602 | xfs_bmap_local_to_extents( |
| 3603 | xfs_trans_t *tp, /* transaction pointer */ |
| 3604 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3605 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3606 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3607 | int *logflagsp, /* inode logging flags */ |
| 3608 | int whichfork) /* data or attr fork */ |
| 3609 | { |
| 3610 | int error; /* error return value */ |
| 3611 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3613 | |
| 3614 | /* |
| 3615 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3616 | * So sending the data fork of a regular inode is invalid. |
| 3617 | */ |
| 3618 | ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG && |
| 3619 | whichfork == XFS_DATA_FORK)); |
| 3620 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3621 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3622 | flags = 0; |
| 3623 | error = 0; |
| 3624 | if (ifp->if_bytes) { |
| 3625 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3626 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3627 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | |
| 3629 | args.tp = tp; |
| 3630 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3631 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3632 | ASSERT((ifp->if_flags & |
| 3633 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | /* |
| 3635 | * Allocate a block. We know we need only one, since the |
| 3636 | * file currently fits in an inode. |
| 3637 | */ |
| 3638 | if (*firstblock == NULLFSBLOCK) { |
| 3639 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3640 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3641 | } else { |
| 3642 | args.fsbno = *firstblock; |
| 3643 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3644 | } |
| 3645 | args.total = total; |
| 3646 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3647 | args.isfl = args.minalignslop = 0; |
| 3648 | args.minlen = args.maxlen = args.prod = 1; |
| 3649 | if ((error = xfs_alloc_vextent(&args))) |
| 3650 | goto done; |
| 3651 | /* |
| 3652 | * Can't fail, the space was reserved. |
| 3653 | */ |
| 3654 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3655 | ASSERT(args.len == 1); |
| 3656 | *firstblock = args.fsbno; |
| 3657 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 3658 | memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data, |
| 3659 | ifp->if_bytes); |
| 3660 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3661 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3663 | xfs_iext_add(ifp, 0, 1); |
| 3664 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3666 | XFS_BMAP_TRACE_POST_UPDATE("new", ip, 0, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3668 | ip->i_d.di_nblocks = 1; |
| 3669 | XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip, |
| 3670 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3671 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3672 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3673 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3674 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3677 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3678 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3679 | flags |= XFS_ILOG_CORE; |
| 3680 | done: |
| 3681 | *logflagsp = flags; |
| 3682 | return error; |
| 3683 | } |
| 3684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3686 | * Search the extent records for the entry containing block bno. |
| 3687 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3688 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3689 | * entry (null if none). Else, *lastxp will be set to the index |
| 3690 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3691 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3692 | xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3693 | xfs_bmap_search_multi_extents( |
| 3694 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3695 | xfs_fileoff_t bno, /* block number searched for */ |
| 3696 | int *eofp, /* out: end of file found */ |
| 3697 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3698 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3699 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3700 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3701 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3702 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3703 | |
| 3704 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3705 | * Initialize the extent entry structure to catch access to |
| 3706 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3707 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3708 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3709 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3710 | gotp->br_state = XFS_EXT_INVALID; |
| 3711 | #if XFS_BIG_BLKNOS |
| 3712 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3713 | #else |
| 3714 | gotp->br_startblock = 0xffffa5a5; |
| 3715 | #endif |
| 3716 | prevp->br_startoff = NULLFILEOFF; |
| 3717 | |
| 3718 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3719 | if (lastx > 0) { |
| 3720 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3721 | } |
| 3722 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3723 | xfs_bmbt_get_all(ep, gotp); |
| 3724 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3725 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3726 | if (lastx > 0) { |
| 3727 | *gotp = *prevp; |
| 3728 | } |
| 3729 | *eofp = 1; |
| 3730 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3731 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3732 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3733 | return ep; |
| 3734 | } |
| 3735 | |
| 3736 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | * Search the extents list for the inode, for the extent containing bno. |
| 3738 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3739 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3740 | * Else, *lastxp will be set to the index of the found |
| 3741 | * entry; *gotp will contain the entry. |
| 3742 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3743 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | xfs_bmap_search_extents( |
| 3745 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3746 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3747 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | int *eofp, /* out: end of file found */ |
| 3749 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3750 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3751 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3752 | { |
| 3753 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3754 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | |
| 3756 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3757 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3759 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3760 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3761 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3762 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 3763 | xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount, |
| 3764 | "Access to block zero in inode %llu " |
| 3765 | "start_block: %llx start_off: %llx " |
| 3766 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3767 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3768 | (unsigned long long)gotp->br_startblock, |
| 3769 | (unsigned long long)gotp->br_startoff, |
| 3770 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3771 | gotp->br_state, *lastxp); |
| 3772 | *lastxp = NULLEXTNUM; |
| 3773 | *eofp = 1; |
| 3774 | return NULL; |
| 3775 | } |
| 3776 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | |
| 3780 | #ifdef XFS_BMAP_TRACE |
| 3781 | ktrace_t *xfs_bmap_trace_buf; |
| 3782 | |
| 3783 | /* |
| 3784 | * Add a bmap trace buffer entry. Base routine for the others. |
| 3785 | */ |
| 3786 | STATIC void |
| 3787 | xfs_bmap_trace_addentry( |
| 3788 | int opcode, /* operation */ |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3789 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | char *desc, /* operation description */ |
| 3791 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3792 | xfs_extnum_t idx, /* index of entry(ies) */ |
| 3793 | xfs_extnum_t cnt, /* count of entries, 1 or 2 */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3794 | xfs_bmbt_rec_host_t *r1, /* first record */ |
| 3795 | xfs_bmbt_rec_host_t *r2, /* second record or null */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | int whichfork) /* data or attr fork */ |
| 3797 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3798 | xfs_bmbt_rec_host_t tr2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | |
| 3800 | ASSERT(cnt == 1 || cnt == 2); |
| 3801 | ASSERT(r1 != NULL); |
| 3802 | if (cnt == 1) { |
| 3803 | ASSERT(r2 == NULL); |
| 3804 | r2 = &tr2; |
| 3805 | memset(&tr2, 0, sizeof(tr2)); |
| 3806 | } else |
| 3807 | ASSERT(r2 != NULL); |
| 3808 | ktrace_enter(xfs_bmap_trace_buf, |
| 3809 | (void *)(__psint_t)(opcode | (whichfork << 16)), |
| 3810 | (void *)fname, (void *)desc, (void *)ip, |
| 3811 | (void *)(__psint_t)idx, |
| 3812 | (void *)(__psint_t)cnt, |
| 3813 | (void *)(__psunsigned_t)(ip->i_ino >> 32), |
| 3814 | (void *)(__psunsigned_t)(unsigned)ip->i_ino, |
| 3815 | (void *)(__psunsigned_t)(r1->l0 >> 32), |
| 3816 | (void *)(__psunsigned_t)(unsigned)(r1->l0), |
| 3817 | (void *)(__psunsigned_t)(r1->l1 >> 32), |
| 3818 | (void *)(__psunsigned_t)(unsigned)(r1->l1), |
| 3819 | (void *)(__psunsigned_t)(r2->l0 >> 32), |
| 3820 | (void *)(__psunsigned_t)(unsigned)(r2->l0), |
| 3821 | (void *)(__psunsigned_t)(r2->l1 >> 32), |
| 3822 | (void *)(__psunsigned_t)(unsigned)(r2->l1) |
| 3823 | ); |
| 3824 | ASSERT(ip->i_xtrace); |
| 3825 | ktrace_enter(ip->i_xtrace, |
| 3826 | (void *)(__psint_t)(opcode | (whichfork << 16)), |
| 3827 | (void *)fname, (void *)desc, (void *)ip, |
| 3828 | (void *)(__psint_t)idx, |
| 3829 | (void *)(__psint_t)cnt, |
| 3830 | (void *)(__psunsigned_t)(ip->i_ino >> 32), |
| 3831 | (void *)(__psunsigned_t)(unsigned)ip->i_ino, |
| 3832 | (void *)(__psunsigned_t)(r1->l0 >> 32), |
| 3833 | (void *)(__psunsigned_t)(unsigned)(r1->l0), |
| 3834 | (void *)(__psunsigned_t)(r1->l1 >> 32), |
| 3835 | (void *)(__psunsigned_t)(unsigned)(r1->l1), |
| 3836 | (void *)(__psunsigned_t)(r2->l0 >> 32), |
| 3837 | (void *)(__psunsigned_t)(unsigned)(r2->l0), |
| 3838 | (void *)(__psunsigned_t)(r2->l1 >> 32), |
| 3839 | (void *)(__psunsigned_t)(unsigned)(r2->l1) |
| 3840 | ); |
| 3841 | } |
| 3842 | |
| 3843 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3844 | * Add bmap trace entry prior to a call to xfs_iext_remove. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | */ |
| 3846 | STATIC void |
| 3847 | xfs_bmap_trace_delete( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3848 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | char *desc, /* operation description */ |
| 3850 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3851 | xfs_extnum_t idx, /* index of entry(entries) deleted */ |
| 3852 | xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */ |
| 3853 | int whichfork) /* data or attr fork */ |
| 3854 | { |
| 3855 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3856 | |
| 3857 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3858 | xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3859 | cnt, xfs_iext_get_ext(ifp, idx), |
| 3860 | cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | whichfork); |
| 3862 | } |
| 3863 | |
| 3864 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3865 | * Add bmap trace entry prior to a call to xfs_iext_insert, or |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | * reading in the extents list from the disk (in the btree). |
| 3867 | */ |
| 3868 | STATIC void |
| 3869 | xfs_bmap_trace_insert( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3870 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | char *desc, /* operation description */ |
| 3872 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3873 | xfs_extnum_t idx, /* index of entry(entries) inserted */ |
| 3874 | xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */ |
| 3875 | xfs_bmbt_irec_t *r1, /* inserted record 1 */ |
| 3876 | xfs_bmbt_irec_t *r2, /* inserted record 2 or null */ |
| 3877 | int whichfork) /* data or attr fork */ |
| 3878 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3879 | xfs_bmbt_rec_host_t tr1; /* compressed record 1 */ |
| 3880 | xfs_bmbt_rec_host_t tr2; /* compressed record 2 if needed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
| 3882 | xfs_bmbt_set_all(&tr1, r1); |
| 3883 | if (cnt == 2) { |
| 3884 | ASSERT(r2 != NULL); |
| 3885 | xfs_bmbt_set_all(&tr2, r2); |
| 3886 | } else { |
| 3887 | ASSERT(cnt == 1); |
| 3888 | ASSERT(r2 == NULL); |
| 3889 | } |
| 3890 | xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx, |
| 3891 | cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork); |
| 3892 | } |
| 3893 | |
| 3894 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3895 | * Add bmap trace entry after updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | */ |
| 3897 | STATIC void |
| 3898 | xfs_bmap_trace_post_update( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3899 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | char *desc, /* operation description */ |
| 3901 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3902 | xfs_extnum_t idx, /* index of entry updated */ |
| 3903 | int whichfork) /* data or attr fork */ |
| 3904 | { |
| 3905 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3906 | |
| 3907 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3908 | xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_POST_UP, fname, desc, ip, idx, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3909 | 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | } |
| 3911 | |
| 3912 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3913 | * Add bmap trace entry prior to updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | */ |
| 3915 | STATIC void |
| 3916 | xfs_bmap_trace_pre_update( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 3917 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | char *desc, /* operation description */ |
| 3919 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3920 | xfs_extnum_t idx, /* index of entry to be updated */ |
| 3921 | int whichfork) /* data or attr fork */ |
| 3922 | { |
| 3923 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3924 | |
| 3925 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3926 | xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_PRE_UP, fname, desc, ip, idx, 1, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3927 | xfs_iext_get_ext(ifp, idx), NULL, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | } |
| 3929 | #endif /* XFS_BMAP_TRACE */ |
| 3930 | |
| 3931 | /* |
| 3932 | * Compute the worst-case number of indirect blocks that will be used |
| 3933 | * for ip's delayed extent of length "len". |
| 3934 | */ |
| 3935 | STATIC xfs_filblks_t |
| 3936 | xfs_bmap_worst_indlen( |
| 3937 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3938 | xfs_filblks_t len) /* delayed extent length */ |
| 3939 | { |
| 3940 | int level; /* btree level number */ |
| 3941 | int maxrecs; /* maximum record count at this level */ |
| 3942 | xfs_mount_t *mp; /* mount structure */ |
| 3943 | xfs_filblks_t rval; /* return value */ |
| 3944 | |
| 3945 | mp = ip->i_mount; |
| 3946 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3947 | for (level = 0, rval = 0; |
| 3948 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3949 | level++) { |
| 3950 | len += maxrecs - 1; |
| 3951 | do_div(len, maxrecs); |
| 3952 | rval += len; |
| 3953 | if (len == 1) |
| 3954 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3955 | level - 1; |
| 3956 | if (level == 0) |
| 3957 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3958 | } |
| 3959 | return rval; |
| 3960 | } |
| 3961 | |
| 3962 | #if defined(XFS_RW_TRACE) |
| 3963 | STATIC void |
| 3964 | xfs_bunmap_trace( |
| 3965 | xfs_inode_t *ip, |
| 3966 | xfs_fileoff_t bno, |
| 3967 | xfs_filblks_t len, |
| 3968 | int flags, |
| 3969 | inst_t *ra) |
| 3970 | { |
| 3971 | if (ip->i_rwtrace == NULL) |
| 3972 | return; |
| 3973 | ktrace_enter(ip->i_rwtrace, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3974 | (void *)(__psint_t)XFS_BUNMAP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | (void *)ip, |
| 3976 | (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff), |
| 3977 | (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff), |
| 3978 | (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff), |
| 3979 | (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff), |
| 3980 | (void *)(__psint_t)len, |
| 3981 | (void *)(__psint_t)flags, |
| 3982 | (void *)(unsigned long)current_cpu(), |
| 3983 | (void *)ra, |
| 3984 | (void *)0, |
| 3985 | (void *)0, |
| 3986 | (void *)0, |
| 3987 | (void *)0, |
| 3988 | (void *)0, |
| 3989 | (void *)0); |
| 3990 | } |
| 3991 | #endif |
| 3992 | |
| 3993 | /* |
| 3994 | * Convert inode from non-attributed to attributed. |
| 3995 | * Must not be in a transaction, ip must not be locked. |
| 3996 | */ |
| 3997 | int /* error code */ |
| 3998 | xfs_bmap_add_attrfork( |
| 3999 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4000 | int size, /* space new attribute needs */ |
| 4001 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4004 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4007 | int blks; /* space reservation */ |
| 4008 | int version = 1; /* superblock attr version */ |
| 4009 | int committed; /* xaction was committed */ |
| 4010 | int logflags; /* logging flags */ |
| 4011 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4013 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | ASSERT(ip->i_df.if_ext_max == |
| 4015 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | mp = ip->i_mount; |
| 4018 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 4019 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 4020 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 4021 | if (rsvd) |
| 4022 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 4023 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 4024 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 4025 | goto error0; |
| 4026 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 4027 | error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ? |
| 4028 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 4029 | XFS_QMOPT_RES_REGBLKS); |
| 4030 | if (error) { |
| 4031 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4032 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 4033 | return error; |
| 4034 | } |
| 4035 | if (XFS_IFORK_Q(ip)) |
| 4036 | goto error1; |
| 4037 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 4038 | /* |
| 4039 | * For inodes coming from pre-6.2 filesystems. |
| 4040 | */ |
| 4041 | ASSERT(ip->i_d.di_aformat == 0); |
| 4042 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 4043 | } |
| 4044 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 0b1f9177 | 2008-08-13 16:13:09 +1000 | [diff] [blame] | 4045 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 4047 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 4048 | switch (ip->i_d.di_format) { |
| 4049 | case XFS_DINODE_FMT_DEV: |
| 4050 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 4051 | break; |
| 4052 | case XFS_DINODE_FMT_UUID: |
| 4053 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 4054 | break; |
| 4055 | case XFS_DINODE_FMT_LOCAL: |
| 4056 | case XFS_DINODE_FMT_EXTENTS: |
| 4057 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4058 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 4059 | if (!ip->i_d.di_forkoff) |
| 4060 | ip->i_d.di_forkoff = mp->m_attroffset >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 4061 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4062 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | break; |
| 4064 | default: |
| 4065 | ASSERT(0); |
| 4066 | error = XFS_ERROR(EINVAL); |
| 4067 | goto error1; |
| 4068 | } |
| 4069 | ip->i_df.if_ext_max = |
| 4070 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 4071 | ASSERT(ip->i_afp == NULL); |
| 4072 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 4073 | ip->i_afp->if_ext_max = |
| 4074 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 4075 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 4076 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4077 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4078 | switch (ip->i_d.di_format) { |
| 4079 | case XFS_DINODE_FMT_LOCAL: |
| 4080 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 4081 | &logflags); |
| 4082 | break; |
| 4083 | case XFS_DINODE_FMT_EXTENTS: |
| 4084 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 4085 | &flist, &logflags); |
| 4086 | break; |
| 4087 | case XFS_DINODE_FMT_BTREE: |
| 4088 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 4089 | &logflags); |
| 4090 | break; |
| 4091 | default: |
| 4092 | error = 0; |
| 4093 | break; |
| 4094 | } |
| 4095 | if (logflags) |
| 4096 | xfs_trans_log_inode(tp, ip, logflags); |
| 4097 | if (error) |
| 4098 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4099 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 4100 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4101 | __int64_t sbfields = 0; |
| 4102 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 4103 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4104 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 4105 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4106 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4107 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4108 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 4109 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4110 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4111 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4112 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 4113 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4114 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 4116 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 4118 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | goto error2; |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4120 | error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | ASSERT(ip->i_df.if_ext_max == |
| 4122 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4123 | return error; |
| 4124 | error2: |
| 4125 | xfs_bmap_cancel(&flist); |
| 4126 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4128 | error0: |
| 4129 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 4130 | ASSERT(ip->i_df.if_ext_max == |
| 4131 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4132 | return error; |
| 4133 | } |
| 4134 | |
| 4135 | /* |
| 4136 | * Add the extent to the list of extents to be free at transaction end. |
| 4137 | * The list is maintained sorted (by block number). |
| 4138 | */ |
| 4139 | /* ARGSUSED */ |
| 4140 | void |
| 4141 | xfs_bmap_add_free( |
| 4142 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 4143 | xfs_filblks_t len, /* length of extent */ |
| 4144 | xfs_bmap_free_t *flist, /* list of extents */ |
| 4145 | xfs_mount_t *mp) /* mount point structure */ |
| 4146 | { |
| 4147 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 4148 | xfs_bmap_free_item_t *new; /* new element */ |
| 4149 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 4150 | #ifdef DEBUG |
| 4151 | xfs_agnumber_t agno; |
| 4152 | xfs_agblock_t agbno; |
| 4153 | |
| 4154 | ASSERT(bno != NULLFSBLOCK); |
| 4155 | ASSERT(len > 0); |
| 4156 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4157 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 4159 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 4160 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 4161 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 4162 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 4163 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 4164 | #endif |
| 4165 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 4166 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 4167 | new->xbfi_startblock = bno; |
| 4168 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 4169 | for (prev = NULL, cur = flist->xbf_first; |
| 4170 | cur != NULL; |
| 4171 | prev = cur, cur = cur->xbfi_next) { |
| 4172 | if (cur->xbfi_startblock >= bno) |
| 4173 | break; |
| 4174 | } |
| 4175 | if (prev) |
| 4176 | prev->xbfi_next = new; |
| 4177 | else |
| 4178 | flist->xbf_first = new; |
| 4179 | new->xbfi_next = cur; |
| 4180 | flist->xbf_count++; |
| 4181 | } |
| 4182 | |
| 4183 | /* |
| 4184 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 4185 | * in this filesystem. Done once, during mount. |
| 4186 | */ |
| 4187 | void |
| 4188 | xfs_bmap_compute_maxlevels( |
| 4189 | xfs_mount_t *mp, /* file system mount structure */ |
| 4190 | int whichfork) /* data or attr fork */ |
| 4191 | { |
| 4192 | int level; /* btree level */ |
| 4193 | uint maxblocks; /* max blocks at this level */ |
| 4194 | uint maxleafents; /* max leaf entries possible */ |
| 4195 | int maxrootrecs; /* max records in root block */ |
| 4196 | int minleafrecs; /* min records in leaf block */ |
| 4197 | int minnoderecs; /* min records in node block */ |
| 4198 | int sz; /* root block size */ |
| 4199 | |
| 4200 | /* |
| 4201 | * The maximum number of extents in a file, hence the maximum |
| 4202 | * number of leaf entries, is controlled by the type of di_nextents |
| 4203 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 4204 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 4205 | * |
| 4206 | * Note that we can no longer assume that if we are in ATTR1 that |
| 4207 | * the fork offset of all the inodes will be (m_attroffset >> 3) |
| 4208 | * because we could have mounted with ATTR2 and then mounted back |
| 4209 | * with ATTR1, keeping the di_forkoff's fixed but probably at |
| 4210 | * various positions. Therefore, for both ATTR1 and ATTR2 |
| 4211 | * we have to assume the worst case scenario of a minimum size |
| 4212 | * available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4214 | if (whichfork == XFS_DATA_FORK) { |
| 4215 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 4216 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4217 | } else { |
| 4218 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 4219 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4220 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4221 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4222 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 4223 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 4225 | for (level = 1; maxblocks > 1; level++) { |
| 4226 | if (maxblocks <= maxrootrecs) |
| 4227 | maxblocks = 1; |
| 4228 | else |
| 4229 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 4230 | } |
| 4231 | mp->m_bm_maxlevels[whichfork] = level; |
| 4232 | } |
| 4233 | |
| 4234 | /* |
| 4235 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 4236 | * caller. Frees all the extents that need freeing, which must be done |
| 4237 | * last due to locking considerations. We never free any extents in |
| 4238 | * the first transaction. This is to allow the caller to make the first |
| 4239 | * transaction a synchronous one so that the pointers to the data being |
| 4240 | * broken in this transaction will be permanent before the data is actually |
| 4241 | * freed. This is necessary to prevent blocks from being reallocated |
| 4242 | * and written to before the free and reallocation are actually permanent. |
| 4243 | * We do not just make the first transaction synchronous here, because |
| 4244 | * there are more efficient ways to gain the same protection in some cases |
| 4245 | * (see the file truncation code). |
| 4246 | * |
| 4247 | * Return 1 if the given transaction was committed and a new one |
| 4248 | * started, and 0 otherwise in the committed parameter. |
| 4249 | */ |
| 4250 | /*ARGSUSED*/ |
| 4251 | int /* error */ |
| 4252 | xfs_bmap_finish( |
| 4253 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 4254 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | int *committed) /* xact committed or not */ |
| 4256 | { |
| 4257 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 4258 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 4259 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4260 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | unsigned int logres; /* new log reservation */ |
| 4262 | unsigned int logcount; /* new log count */ |
| 4263 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 4264 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 4265 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 4266 | |
| 4267 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 4268 | if (flist->xbf_count == 0) { |
| 4269 | *committed = 0; |
| 4270 | return 0; |
| 4271 | } |
| 4272 | ntp = *tp; |
| 4273 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 4274 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 4275 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 4276 | free->xbfi_blockcount); |
| 4277 | logres = ntp->t_log_res; |
| 4278 | logcount = ntp->t_log_count; |
| 4279 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4280 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | *tp = ntp; |
| 4282 | *committed = 1; |
| 4283 | /* |
| 4284 | * We have a new transaction, so we should return committed=1, |
| 4285 | * even though we're returning an error. |
| 4286 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 4287 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 4289 | |
| 4290 | /* |
| 4291 | * transaction commit worked ok so we can drop the extra ticket |
| 4292 | * reference that we gained in xfs_trans_dup() |
| 4293 | */ |
| 4294 | xfs_log_ticket_put(ntp->t_ticket); |
| 4295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 4297 | logcount))) |
| 4298 | return error; |
| 4299 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 4300 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 4301 | next = free->xbfi_next; |
| 4302 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 4303 | free->xbfi_blockcount))) { |
| 4304 | /* |
| 4305 | * The bmap free list will be cleaned up at a |
| 4306 | * higher level. The EFI will be canceled when |
| 4307 | * this transaction is aborted. |
| 4308 | * Need to force shutdown here to make sure it |
| 4309 | * happens, since this transaction may not be |
| 4310 | * dirty yet. |
| 4311 | */ |
| 4312 | mp = ntp->t_mountp; |
| 4313 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 4314 | xfs_force_shutdown(mp, |
| 4315 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 4316 | SHUTDOWN_CORRUPT_INCORE : |
| 4317 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | return error; |
| 4319 | } |
| 4320 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 4321 | free->xbfi_blockcount); |
| 4322 | xfs_bmap_del_free(flist, NULL, free); |
| 4323 | } |
| 4324 | return 0; |
| 4325 | } |
| 4326 | |
| 4327 | /* |
| 4328 | * Free up any items left in the list. |
| 4329 | */ |
| 4330 | void |
| 4331 | xfs_bmap_cancel( |
| 4332 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 4333 | { |
| 4334 | xfs_bmap_free_item_t *free; /* free list item */ |
| 4335 | xfs_bmap_free_item_t *next; |
| 4336 | |
| 4337 | if (flist->xbf_count == 0) |
| 4338 | return; |
| 4339 | ASSERT(flist->xbf_first != NULL); |
| 4340 | for (free = flist->xbf_first; free; free = next) { |
| 4341 | next = free->xbfi_next; |
| 4342 | xfs_bmap_del_free(flist, NULL, free); |
| 4343 | } |
| 4344 | ASSERT(flist->xbf_count == 0); |
| 4345 | } |
| 4346 | |
| 4347 | /* |
| 4348 | * Returns the file-relative block number of the first unused block(s) |
| 4349 | * in the file with at least "len" logically contiguous blocks free. |
| 4350 | * This is the lowest-address hole if the file has holes, else the first block |
| 4351 | * past the end of file. |
| 4352 | * Return 0 if the file is currently local (in-inode). |
| 4353 | */ |
| 4354 | int /* error */ |
| 4355 | xfs_bmap_first_unused( |
| 4356 | xfs_trans_t *tp, /* transaction pointer */ |
| 4357 | xfs_inode_t *ip, /* incore inode */ |
| 4358 | xfs_extlen_t len, /* size of hole to find */ |
| 4359 | xfs_fileoff_t *first_unused, /* unused block */ |
| 4360 | int whichfork) /* data or attr fork */ |
| 4361 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4363 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4365 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 4366 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 4367 | xfs_fileoff_t max; /* starting useful block */ |
| 4368 | xfs_fileoff_t off; /* offset for this block */ |
| 4369 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4370 | |
| 4371 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 4372 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 4373 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 4374 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4375 | *first_unused = 0; |
| 4376 | return 0; |
| 4377 | } |
| 4378 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4379 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4380 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4381 | return error; |
| 4382 | lowest = *first_unused; |
| 4383 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4384 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4385 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | off = xfs_bmbt_get_startoff(ep); |
| 4387 | /* |
| 4388 | * See if the hole before this extent will work. |
| 4389 | */ |
| 4390 | if (off >= lowest + len && off - max >= len) { |
| 4391 | *first_unused = max; |
| 4392 | return 0; |
| 4393 | } |
| 4394 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 4395 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 4396 | } |
| 4397 | *first_unused = max; |
| 4398 | return 0; |
| 4399 | } |
| 4400 | |
| 4401 | /* |
| 4402 | * Returns the file-relative block number of the last block + 1 before |
| 4403 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4404 | * This is not based on i_size, it is based on the extent records. |
| 4405 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | */ |
| 4407 | int /* error */ |
| 4408 | xfs_bmap_last_before( |
| 4409 | xfs_trans_t *tp, /* transaction pointer */ |
| 4410 | xfs_inode_t *ip, /* incore inode */ |
| 4411 | xfs_fileoff_t *last_block, /* last block */ |
| 4412 | int whichfork) /* data or attr fork */ |
| 4413 | { |
| 4414 | xfs_fileoff_t bno; /* input file offset */ |
| 4415 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4416 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | int error; /* error return value */ |
| 4418 | xfs_bmbt_irec_t got; /* current extent value */ |
| 4419 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4420 | xfs_extnum_t lastx; /* last extent used */ |
| 4421 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 4422 | |
| 4423 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4424 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4425 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4426 | return XFS_ERROR(EIO); |
| 4427 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4428 | *last_block = 0; |
| 4429 | return 0; |
| 4430 | } |
| 4431 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4432 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4433 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4434 | return error; |
| 4435 | bno = *last_block - 1; |
| 4436 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4437 | &prev); |
| 4438 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 4439 | if (prev.br_startoff == NULLFILEOFF) |
| 4440 | *last_block = 0; |
| 4441 | else |
| 4442 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 4443 | } |
| 4444 | /* |
| 4445 | * Otherwise *last_block is already the right answer. |
| 4446 | */ |
| 4447 | return 0; |
| 4448 | } |
| 4449 | |
| 4450 | /* |
| 4451 | * 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] | 4452 | * the file. This is not based on i_size, it is based on the extent records. |
| 4453 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | */ |
| 4455 | int /* error */ |
| 4456 | xfs_bmap_last_offset( |
| 4457 | xfs_trans_t *tp, /* transaction pointer */ |
| 4458 | xfs_inode_t *ip, /* incore inode */ |
| 4459 | xfs_fileoff_t *last_block, /* last block */ |
| 4460 | int whichfork) /* data or attr fork */ |
| 4461 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4462 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | int error; /* error return value */ |
| 4464 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4465 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4466 | |
| 4467 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4468 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4469 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4470 | return XFS_ERROR(EIO); |
| 4471 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4472 | *last_block = 0; |
| 4473 | return 0; |
| 4474 | } |
| 4475 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4476 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4477 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4478 | return error; |
| 4479 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4480 | if (!nextents) { |
| 4481 | *last_block = 0; |
| 4482 | return 0; |
| 4483 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4484 | ep = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); |
| 4486 | return 0; |
| 4487 | } |
| 4488 | |
| 4489 | /* |
| 4490 | * Returns whether the selected fork of the inode has exactly one |
| 4491 | * block or not. For the data fork we check this matches di_size, |
| 4492 | * implying the file's range is 0..bsize-1. |
| 4493 | */ |
| 4494 | int /* 1=>1 block, 0=>otherwise */ |
| 4495 | xfs_bmap_one_block( |
| 4496 | xfs_inode_t *ip, /* incore inode */ |
| 4497 | int whichfork) /* data or attr fork */ |
| 4498 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4499 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4501 | int rval; /* return value */ |
| 4502 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 4503 | |
| 4504 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4505 | if (whichfork == XFS_DATA_FORK) { |
| 4506 | return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ? |
| 4507 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 4508 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 4509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | #endif /* !DEBUG */ |
| 4511 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4512 | return 0; |
| 4513 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4514 | return 0; |
| 4515 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4516 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4517 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | xfs_bmbt_get_all(ep, &s); |
| 4519 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4520 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4521 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | return rval; |
| 4523 | } |
| 4524 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4525 | STATIC int |
| 4526 | xfs_bmap_sanity_check( |
| 4527 | struct xfs_mount *mp, |
| 4528 | struct xfs_buf *bp, |
| 4529 | int level) |
| 4530 | { |
| 4531 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4532 | |
| 4533 | if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC || |
| 4534 | be16_to_cpu(block->bb_level) != level || |
| 4535 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4536 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4537 | return 0; |
| 4538 | return 1; |
| 4539 | } |
| 4540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | /* |
| 4542 | * Read in the extents to if_extents. |
| 4543 | * All inode fields are set up by caller, we just traverse the btree |
| 4544 | * and copy the records in. If the file system cannot contain unwritten |
| 4545 | * extents, the records are checked for no "state" flags. |
| 4546 | */ |
| 4547 | int /* error */ |
| 4548 | xfs_bmap_read_extents( |
| 4549 | xfs_trans_t *tp, /* transaction pointer */ |
| 4550 | xfs_inode_t *ip, /* incore inode */ |
| 4551 | int whichfork) /* data or attr fork */ |
| 4552 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4553 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4555 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4556 | int error; /* error return value */ |
| 4557 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4559 | xfs_ifork_t *ifp; /* fork structure */ |
| 4560 | int level; /* btree level, for checking */ |
| 4561 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4562 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | /* REFERENCED */ |
| 4564 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | |
| 4566 | bno = NULLFSBLOCK; |
| 4567 | mp = ip->i_mount; |
| 4568 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4569 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4570 | XFS_EXTFMT_INODE(ip); |
| 4571 | block = ifp->if_broot; |
| 4572 | /* |
| 4573 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4574 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4575 | level = be16_to_cpu(block->bb_level); |
| 4576 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4577 | 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] | 4578 | bno = be64_to_cpu(*pp); |
| 4579 | ASSERT(bno != NULLDFSBNO); |
| 4580 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4581 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | /* |
| 4583 | * Go down the tree until leaf level is reached, following the first |
| 4584 | * pointer (leftmost) at each level. |
| 4585 | */ |
| 4586 | while (level-- > 0) { |
| 4587 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4588 | XFS_BMAP_BTREE_REF))) |
| 4589 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4590 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4592 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | error0); |
| 4594 | if (level == 0) |
| 4595 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4596 | 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] | 4597 | bno = be64_to_cpu(*pp); |
| 4598 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | xfs_trans_brelse(tp, bp); |
| 4600 | } |
| 4601 | /* |
| 4602 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4603 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4604 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | i = 0; |
| 4606 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4607 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | */ |
| 4609 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4610 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | xfs_fsblock_t nextbno; |
| 4612 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4613 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | |
| 4615 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4616 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | if (unlikely(i + num_recs > room)) { |
| 4618 | ASSERT(i + num_recs <= room); |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4619 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 4620 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | (unsigned long long) ip->i_ino); |
| 4622 | XFS_ERROR_REPORT("xfs_bmap_read_extents(1)", |
| 4623 | XFS_ERRLEVEL_LOW, |
| 4624 | ip->i_mount); |
| 4625 | goto error0; |
| 4626 | } |
| 4627 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4628 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | error0); |
| 4630 | /* |
| 4631 | * Read-ahead the next leaf block, if any. |
| 4632 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4633 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | if (nextbno != NULLFSBLOCK) |
| 4635 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4636 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4637 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4639 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4640 | start = i; |
| 4641 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4642 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4643 | trp->l0 = be64_to_cpu(frp->l0); |
| 4644 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | } |
| 4646 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4647 | /* |
| 4648 | * Check all attribute bmap btree records and |
| 4649 | * any "older" data bmap btree records for a |
| 4650 | * set bit in the "extent flag" position. |
| 4651 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4652 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4653 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4655 | XFS_ERRLEVEL_LOW, |
| 4656 | ip->i_mount); |
| 4657 | goto error0; |
| 4658 | } |
| 4659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | xfs_trans_brelse(tp, bp); |
| 4661 | bno = nextbno; |
| 4662 | /* |
| 4663 | * If we've reached the end, stop. |
| 4664 | */ |
| 4665 | if (bno == NULLFSBLOCK) |
| 4666 | break; |
| 4667 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4668 | XFS_BMAP_BTREE_REF))) |
| 4669 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4670 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4672 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4674 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | return 0; |
| 4676 | error0: |
| 4677 | xfs_trans_brelse(tp, bp); |
| 4678 | return XFS_ERROR(EFSCORRUPTED); |
| 4679 | } |
| 4680 | |
| 4681 | #ifdef XFS_BMAP_TRACE |
| 4682 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4683 | * 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] | 4684 | */ |
| 4685 | void |
| 4686 | xfs_bmap_trace_exlist( |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4687 | const char *fname, /* function name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4689 | xfs_extnum_t cnt, /* count of entries in the list */ |
| 4690 | int whichfork) /* data or attr fork */ |
| 4691 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4692 | xfs_bmbt_rec_host_t *ep; /* current extent record */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4693 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4695 | xfs_bmbt_irec_t s; /* file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | |
| 4697 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4698 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 4699 | for (idx = 0; idx < cnt; idx++) { |
| 4700 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | xfs_bmbt_get_all(ep, &s); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4702 | XFS_BMAP_TRACE_INSERT("exlist", ip, idx, 1, &s, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | whichfork); |
| 4704 | } |
| 4705 | } |
| 4706 | #endif |
| 4707 | |
| 4708 | #ifdef DEBUG |
| 4709 | /* |
| 4710 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4711 | * given the callers original parameters. Specifically check the |
| 4712 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4713 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4714 | */ |
| 4715 | STATIC void |
| 4716 | xfs_bmap_validate_ret( |
| 4717 | xfs_fileoff_t bno, |
| 4718 | xfs_filblks_t len, |
| 4719 | int flags, |
| 4720 | xfs_bmbt_irec_t *mval, |
| 4721 | int nmap, |
| 4722 | int ret_nmap) |
| 4723 | { |
| 4724 | int i; /* index to map values */ |
| 4725 | |
| 4726 | ASSERT(ret_nmap <= nmap); |
| 4727 | |
| 4728 | for (i = 0; i < ret_nmap; i++) { |
| 4729 | ASSERT(mval[i].br_blockcount > 0); |
| 4730 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4731 | ASSERT(mval[i].br_startoff >= bno); |
| 4732 | ASSERT(mval[i].br_blockcount <= len); |
| 4733 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4734 | bno + len); |
| 4735 | } else { |
| 4736 | ASSERT(mval[i].br_startoff < bno + len); |
| 4737 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4738 | bno); |
| 4739 | } |
| 4740 | ASSERT(i == 0 || |
| 4741 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4742 | mval[i].br_startoff); |
| 4743 | if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY)) |
| 4744 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4745 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 4746 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4747 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4748 | } |
| 4749 | } |
| 4750 | #endif /* DEBUG */ |
| 4751 | |
| 4752 | |
| 4753 | /* |
| 4754 | * Map file blocks to filesystem blocks. |
| 4755 | * File range is given by the bno/len pair. |
| 4756 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 4757 | * into a hole or past eof. |
| 4758 | * Only allocates blocks from a single allocation group, |
| 4759 | * to avoid locking problems. |
| 4760 | * The returned value in "firstblock" from the first call in a transaction |
| 4761 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4762 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4763 | * the first call in "total"; if no allocation group has that many free |
| 4764 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4765 | */ |
| 4766 | int /* error */ |
| 4767 | xfs_bmapi( |
| 4768 | xfs_trans_t *tp, /* transaction pointer */ |
| 4769 | xfs_inode_t *ip, /* incore inode */ |
| 4770 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4771 | xfs_filblks_t len, /* length to map in file */ |
| 4772 | int flags, /* XFS_BMAPI_... */ |
| 4773 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4774 | controls a.g. for allocs */ |
| 4775 | xfs_extlen_t total, /* total blocks needed */ |
| 4776 | xfs_bmbt_irec_t *mval, /* output: map values */ |
| 4777 | int *nmap, /* i/o: mval size/count */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4778 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 4779 | xfs_extdelta_t *delta) /* o: change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | { |
| 4781 | xfs_fsblock_t abno; /* allocated block number */ |
| 4782 | xfs_extlen_t alen; /* allocated extent length */ |
| 4783 | xfs_fileoff_t aoff; /* allocated file offset */ |
| 4784 | xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | xfs_fileoff_t end; /* end of mapped file region */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4787 | int eof; /* we've hit the end of extents */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4788 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4790 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4792 | xfs_extlen_t indlen; /* indirect blocks length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4794 | int logflags; /* flags for transaction logging */ |
| 4795 | xfs_extlen_t minleft; /* min blocks left after allocation */ |
| 4796 | xfs_extlen_t minlen; /* min allocation size */ |
| 4797 | xfs_mount_t *mp; /* xfs mount structure */ |
| 4798 | int n; /* current extent index */ |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame^] | 4799 | int nallocs; /* number of extents alloc'd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4801 | xfs_fileoff_t obno; /* old block number (offset) */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4802 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | int tmp_logflags; /* temp flags holder */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4804 | int whichfork; /* data or attr fork */ |
| 4805 | char inhole; /* current location is hole in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | char wasdelay; /* old extent was delayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | char wr; /* this is a write request */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4808 | char rt; /* this is a realtime file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | #ifdef DEBUG |
| 4810 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4811 | int orig_flags; /* original flags arg value */ |
| 4812 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4813 | xfs_bmbt_irec_t *orig_mval; /* original value of mval */ |
| 4814 | int orig_nmap; /* original value of *nmap */ |
| 4815 | |
| 4816 | orig_bno = bno; |
| 4817 | orig_len = len; |
| 4818 | orig_flags = flags; |
| 4819 | orig_mval = mval; |
| 4820 | orig_nmap = *nmap; |
| 4821 | #endif |
| 4822 | ASSERT(*nmap >= 1); |
| 4823 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); |
| 4824 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4825 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4826 | mp = ip->i_mount; |
| 4827 | if (unlikely(XFS_TEST_ERROR( |
| 4828 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4829 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4830 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4831 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4832 | XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); |
| 4833 | return XFS_ERROR(EFSCORRUPTED); |
| 4834 | } |
| 4835 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4836 | return XFS_ERROR(EIO); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4837 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4839 | ASSERT(ifp->if_ext_max == |
| 4840 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4841 | if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) |
| 4842 | XFS_STATS_INC(xs_blk_mapw); |
| 4843 | else |
| 4844 | XFS_STATS_INC(xs_blk_mapr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | /* |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4846 | * IGSTATE flag is used to combine extents which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | * differ only due to the state of the extents. |
| 4848 | * This technique is used from xfs_getbmap() |
| 4849 | * when the caller does not wish to see the |
| 4850 | * separation (which is the default). |
| 4851 | * |
| 4852 | * This technique is also used when writing a |
| 4853 | * buffer which has been partially written, |
| 4854 | * (usually by being flushed during a chunkread), |
| 4855 | * to ensure one write takes place. This also |
| 4856 | * prevents a change in the xfs inode extents at |
| 4857 | * this time, intentionally. This change occurs |
| 4858 | * on completion of the write operation, in |
| 4859 | * xfs_strat_comp(), where the xfs_bmapi() call |
| 4860 | * is transactioned, and the extents combined. |
| 4861 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4862 | if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */ |
| 4863 | wr = 0; /* no allocations are allowed */ |
| 4864 | ASSERT(wr || !(flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | logflags = 0; |
| 4866 | nallocs = 0; |
| 4867 | cur = NULL; |
| 4868 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4869 | ASSERT(wr && tp); |
| 4870 | if ((error = xfs_bmap_local_to_extents(tp, ip, |
| 4871 | firstblock, total, &logflags, whichfork))) |
| 4872 | goto error0; |
| 4873 | } |
| 4874 | if (wr && *firstblock == NULLFSBLOCK) { |
| 4875 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4876 | minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | else |
| 4878 | minleft = 1; |
| 4879 | } else |
| 4880 | minleft = 0; |
| 4881 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4882 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4883 | goto error0; |
| 4884 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4885 | &prev); |
| 4886 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4887 | n = 0; |
| 4888 | end = bno + len; |
| 4889 | obno = bno; |
| 4890 | bma.ip = NULL; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4891 | if (delta) { |
| 4892 | delta->xed_startoff = NULLFILEOFF; |
| 4893 | delta->xed_blockcount = 0; |
| 4894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | while (bno < end && n < *nmap) { |
| 4896 | /* |
| 4897 | * Reading past eof, act as though there's a hole |
| 4898 | * up to end. |
| 4899 | */ |
| 4900 | if (eof && !wr) |
| 4901 | got.br_startoff = end; |
| 4902 | inhole = eof || got.br_startoff > bno; |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4903 | wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4904 | isnullstartblock(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | /* |
| 4906 | * First, deal with the hole before the allocated space |
| 4907 | * that we found, if any. |
| 4908 | */ |
| 4909 | if (wr && (inhole || wasdelay)) { |
| 4910 | /* |
| 4911 | * For the wasdelay case, we could also just |
| 4912 | * allocate the stuff asked for in this bmap call |
| 4913 | * but that wouldn't be as good. |
| 4914 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4915 | if (wasdelay && !(flags & XFS_BMAPI_EXACT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | alen = (xfs_extlen_t)got.br_blockcount; |
| 4917 | aoff = got.br_startoff; |
| 4918 | if (lastx != NULLEXTNUM && lastx) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4919 | ep = xfs_iext_get_ext(ifp, lastx - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | xfs_bmbt_get_all(ep, &prev); |
| 4921 | } |
| 4922 | } else if (wasdelay) { |
| 4923 | alen = (xfs_extlen_t) |
| 4924 | XFS_FILBLKS_MIN(len, |
| 4925 | (got.br_startoff + |
| 4926 | got.br_blockcount) - bno); |
| 4927 | aoff = bno; |
| 4928 | } else { |
| 4929 | alen = (xfs_extlen_t) |
| 4930 | XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4931 | if (!eof) |
| 4932 | alen = (xfs_extlen_t) |
| 4933 | XFS_FILBLKS_MIN(alen, |
| 4934 | got.br_startoff - bno); |
| 4935 | aoff = bno; |
| 4936 | } |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4937 | minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; |
| 4938 | if (flags & XFS_BMAPI_DELAY) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4939 | xfs_extlen_t extsz; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4940 | |
| 4941 | /* Figure out the extent size, adjust alen */ |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 4942 | extsz = xfs_get_extsz_hint(ip); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4943 | if (extsz) { |
| 4944 | error = xfs_bmap_extsize_align(mp, |
| 4945 | &got, &prev, extsz, |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4946 | rt, eof, |
| 4947 | flags&XFS_BMAPI_DELAY, |
| 4948 | flags&XFS_BMAPI_CONVERT, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4949 | &aoff, &alen); |
| 4950 | ASSERT(!error); |
| 4951 | } |
| 4952 | |
| 4953 | if (rt) |
| 4954 | extsz = alen / mp->m_sb.sb_rextsize; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | /* |
| 4957 | * Make a transaction-less quota reservation for |
| 4958 | * delayed allocation blocks. This number gets |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4959 | * adjusted later. We return if we haven't |
| 4960 | * allocated blocks already inside this loop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | */ |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4962 | if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS( |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4963 | mp, NULL, ip, (long)alen, 0, |
| 4964 | rt ? XFS_QMOPT_RES_RTBLKS : |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4965 | XFS_QMOPT_RES_REGBLKS))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | if (n == 0) { |
| 4967 | *nmap = 0; |
| 4968 | ASSERT(cur == NULL); |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4969 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | } |
| 4971 | break; |
| 4972 | } |
| 4973 | |
| 4974 | /* |
| 4975 | * Split changing sb for alen and indlen since |
| 4976 | * they could be coming from different places. |
| 4977 | */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4978 | indlen = (xfs_extlen_t) |
| 4979 | xfs_bmap_worst_indlen(ip, alen); |
| 4980 | ASSERT(indlen > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4981 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4982 | if (rt) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4983 | error = xfs_mod_incore_sb(mp, |
| 4984 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4985 | -((int64_t)extsz), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4986 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4987 | } else { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4988 | error = xfs_mod_incore_sb(mp, |
| 4989 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4990 | -((int64_t)alen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4991 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4992 | } |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4993 | if (!error) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4994 | error = xfs_mod_incore_sb(mp, |
| 4995 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4996 | -((int64_t)indlen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4997 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4998 | if (error && rt) |
| 4999 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 5000 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5001 | (int64_t)extsz, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5002 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 5003 | else if (error) |
| 5004 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 5005 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5006 | (int64_t)alen, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5007 | XFS_BMAPI_RSVBLOCKS)); |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 5008 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5009 | |
| 5010 | if (error) { |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 5011 | if (XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5012 | /* unreserve the blocks now */ |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5013 | (void) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5014 | XFS_TRANS_UNRESERVE_QUOTA_NBLKS( |
| 5015 | mp, NULL, ip, |
| 5016 | (long)alen, 0, rt ? |
| 5017 | XFS_QMOPT_RES_RTBLKS : |
| 5018 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | break; |
| 5020 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | ip->i_delayed_blks += alen; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5023 | abno = nullstartblock(indlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | } else { |
| 5025 | /* |
| 5026 | * If first time, allocate and fill in |
| 5027 | * once-only bma fields. |
| 5028 | */ |
| 5029 | if (bma.ip == NULL) { |
| 5030 | bma.tp = tp; |
| 5031 | bma.ip = ip; |
| 5032 | bma.prevp = &prev; |
| 5033 | bma.gotp = &got; |
| 5034 | bma.total = total; |
| 5035 | bma.userdata = 0; |
| 5036 | } |
| 5037 | /* Indicate if this is the first user data |
| 5038 | * in the file, or just any user data. |
| 5039 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5040 | if (!(flags & XFS_BMAPI_METADATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | bma.userdata = (aoff == 0) ? |
| 5042 | XFS_ALLOC_INITIAL_USER_DATA : |
| 5043 | XFS_ALLOC_USERDATA; |
| 5044 | } |
| 5045 | /* |
| 5046 | * Fill in changeable bma fields. |
| 5047 | */ |
| 5048 | bma.eof = eof; |
| 5049 | bma.firstblock = *firstblock; |
| 5050 | bma.alen = alen; |
| 5051 | bma.off = aoff; |
Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 5052 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5053 | bma.wasdel = wasdelay; |
| 5054 | bma.minlen = minlen; |
| 5055 | bma.low = flist->xbf_low; |
| 5056 | bma.minleft = minleft; |
| 5057 | /* |
| 5058 | * Only want to do the alignment at the |
| 5059 | * eof if it is userdata and allocation length |
| 5060 | * is larger than a stripe unit. |
| 5061 | */ |
| 5062 | if (mp->m_dalign && alen >= mp->m_dalign && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5063 | (!(flags & XFS_BMAPI_METADATA)) && |
| 5064 | (whichfork == XFS_DATA_FORK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | if ((error = xfs_bmap_isaeof(ip, aoff, |
| 5066 | whichfork, &bma.aeof))) |
| 5067 | goto error0; |
| 5068 | } else |
| 5069 | bma.aeof = 0; |
| 5070 | /* |
| 5071 | * Call allocator. |
| 5072 | */ |
| 5073 | if ((error = xfs_bmap_alloc(&bma))) |
| 5074 | goto error0; |
| 5075 | /* |
| 5076 | * Copy out result fields. |
| 5077 | */ |
| 5078 | abno = bma.rval; |
| 5079 | if ((flist->xbf_low = bma.low)) |
| 5080 | minleft = 0; |
| 5081 | alen = bma.alen; |
| 5082 | aoff = bma.off; |
| 5083 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5084 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5085 | XFS_FSB_TO_AGNO(mp, bma.firstblock) || |
| 5086 | (flist->xbf_low && |
| 5087 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5088 | XFS_FSB_TO_AGNO(mp, bma.firstblock))); |
| 5089 | *firstblock = bma.firstblock; |
| 5090 | if (cur) |
| 5091 | cur->bc_private.b.firstblock = |
| 5092 | *firstblock; |
| 5093 | if (abno == NULLFSBLOCK) |
| 5094 | break; |
| 5095 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5096 | cur = xfs_bmbt_init_cursor(mp, tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | ip, whichfork); |
| 5098 | cur->bc_private.b.firstblock = |
| 5099 | *firstblock; |
| 5100 | cur->bc_private.b.flist = flist; |
| 5101 | } |
| 5102 | /* |
| 5103 | * Bump the number of extents we've allocated |
| 5104 | * in this call. |
| 5105 | */ |
| 5106 | nallocs++; |
| 5107 | } |
| 5108 | if (cur) |
| 5109 | cur->bc_private.b.flags = |
| 5110 | wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 5111 | got.br_startoff = aoff; |
| 5112 | got.br_startblock = abno; |
| 5113 | got.br_blockcount = alen; |
| 5114 | got.br_state = XFS_EXT_NORM; /* assume normal */ |
| 5115 | /* |
| 5116 | * Determine state of extent, and the filesystem. |
| 5117 | * A wasdelay extent has been initialized, so |
| 5118 | * shouldn't be flagged as unwritten. |
| 5119 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5120 | if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) |
| 5122 | got.br_state = XFS_EXT_UNWRITTEN; |
| 5123 | } |
| 5124 | error = xfs_bmap_add_extent(ip, lastx, &cur, &got, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5125 | firstblock, flist, &tmp_logflags, delta, |
| 5126 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | logflags |= tmp_logflags; |
| 5128 | if (error) |
| 5129 | goto error0; |
| 5130 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5131 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5133 | xfs_bmbt_get_all(ep, &got); |
| 5134 | ASSERT(got.br_startoff <= aoff); |
| 5135 | ASSERT(got.br_startoff + got.br_blockcount >= |
| 5136 | aoff + alen); |
| 5137 | #ifdef DEBUG |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5138 | if (flags & XFS_BMAPI_DELAY) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5139 | ASSERT(isnullstartblock(got.br_startblock)); |
| 5140 | ASSERT(startblockval(got.br_startblock) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | } |
| 5142 | ASSERT(got.br_state == XFS_EXT_NORM || |
| 5143 | got.br_state == XFS_EXT_UNWRITTEN); |
| 5144 | #endif |
| 5145 | /* |
| 5146 | * Fall down into the found allocated space case. |
| 5147 | */ |
| 5148 | } else if (inhole) { |
| 5149 | /* |
| 5150 | * Reading in a hole. |
| 5151 | */ |
| 5152 | mval->br_startoff = bno; |
| 5153 | mval->br_startblock = HOLESTARTBLOCK; |
| 5154 | mval->br_blockcount = |
| 5155 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 5156 | mval->br_state = XFS_EXT_NORM; |
| 5157 | bno += mval->br_blockcount; |
| 5158 | len -= mval->br_blockcount; |
| 5159 | mval++; |
| 5160 | n++; |
| 5161 | continue; |
| 5162 | } |
| 5163 | /* |
| 5164 | * Then deal with the allocated space we found. |
| 5165 | */ |
| 5166 | ASSERT(ep != NULL); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5167 | if (!(flags & XFS_BMAPI_ENTIRE) && |
| 5168 | (got.br_startoff + got.br_blockcount > obno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | if (obno > bno) |
| 5170 | bno = obno; |
| 5171 | ASSERT((bno >= obno) || (n == 0)); |
| 5172 | ASSERT(bno < end); |
| 5173 | mval->br_startoff = bno; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5174 | if (isnullstartblock(got.br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5175 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | mval->br_startblock = DELAYSTARTBLOCK; |
| 5177 | } else |
| 5178 | mval->br_startblock = |
| 5179 | got.br_startblock + |
| 5180 | (bno - got.br_startoff); |
| 5181 | /* |
| 5182 | * Return the minimum of what we got and what we |
| 5183 | * asked for for the length. We can use the len |
| 5184 | * variable here because it is modified below |
| 5185 | * and we could have been there before coming |
| 5186 | * here if the first part of the allocation |
| 5187 | * didn't overlap what was asked for. |
| 5188 | */ |
| 5189 | mval->br_blockcount = |
| 5190 | XFS_FILBLKS_MIN(end - bno, got.br_blockcount - |
| 5191 | (bno - got.br_startoff)); |
| 5192 | mval->br_state = got.br_state; |
| 5193 | ASSERT(mval->br_blockcount <= len); |
| 5194 | } else { |
| 5195 | *mval = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5196 | if (isnullstartblock(mval->br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5197 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | mval->br_startblock = DELAYSTARTBLOCK; |
| 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | /* |
| 5203 | * Check if writing previously allocated but |
| 5204 | * unwritten extents. |
| 5205 | */ |
| 5206 | if (wr && mval->br_state == XFS_EXT_UNWRITTEN && |
| 5207 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) { |
| 5208 | /* |
| 5209 | * Modify (by adding) the state flag, if writing. |
| 5210 | */ |
| 5211 | ASSERT(mval->br_blockcount <= len); |
| 5212 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5213 | cur = xfs_bmbt_init_cursor(mp, |
| 5214 | tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | cur->bc_private.b.firstblock = |
| 5216 | *firstblock; |
| 5217 | cur->bc_private.b.flist = flist; |
| 5218 | } |
| 5219 | mval->br_state = XFS_EXT_NORM; |
| 5220 | error = xfs_bmap_add_extent(ip, lastx, &cur, mval, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5221 | firstblock, flist, &tmp_logflags, delta, |
| 5222 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | logflags |= tmp_logflags; |
| 5224 | if (error) |
| 5225 | goto error0; |
| 5226 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5227 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5229 | xfs_bmbt_get_all(ep, &got); |
| 5230 | /* |
| 5231 | * We may have combined previously unwritten |
| 5232 | * space with written space, so generate |
| 5233 | * another request. |
| 5234 | */ |
| 5235 | if (mval->br_blockcount < len) |
| 5236 | continue; |
| 5237 | } |
| 5238 | |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5239 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5241 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 5242 | (mval->br_blockcount <= len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | (mval->br_startoff < obno)); |
| 5244 | bno = mval->br_startoff + mval->br_blockcount; |
| 5245 | len = end - bno; |
| 5246 | if (n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 5247 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 5248 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 5249 | ASSERT(mval->br_state == mval[-1].br_state); |
| 5250 | mval[-1].br_blockcount = mval->br_blockcount; |
| 5251 | mval[-1].br_state = mval->br_state; |
| 5252 | } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 5253 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 5254 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 5255 | mval->br_startblock == |
| 5256 | mval[-1].br_startblock + mval[-1].br_blockcount && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5257 | ((flags & XFS_BMAPI_IGSTATE) || |
| 5258 | mval[-1].br_state == mval->br_state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | ASSERT(mval->br_startoff == |
| 5260 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 5261 | mval[-1].br_blockcount += mval->br_blockcount; |
| 5262 | } else if (n > 0 && |
| 5263 | mval->br_startblock == DELAYSTARTBLOCK && |
| 5264 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 5265 | mval->br_startoff == |
| 5266 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 5267 | mval[-1].br_blockcount += mval->br_blockcount; |
| 5268 | mval[-1].br_state = mval->br_state; |
| 5269 | } else if (!((n == 0) && |
| 5270 | ((mval->br_startoff + mval->br_blockcount) <= |
| 5271 | obno))) { |
| 5272 | mval++; |
| 5273 | n++; |
| 5274 | } |
| 5275 | /* |
| 5276 | * If we're done, stop now. Stop when we've allocated |
| 5277 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 5278 | * the transaction may get too big. |
| 5279 | */ |
| 5280 | if (bno >= end || n >= *nmap || nallocs >= *nmap) |
| 5281 | break; |
| 5282 | /* |
| 5283 | * Else go on to the next record. |
| 5284 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5285 | ep = xfs_iext_get_ext(ifp, ++lastx); |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 5286 | prev = got; |
| 5287 | if (lastx >= nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | eof = 1; |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 5289 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | xfs_bmbt_get_all(ep, &got); |
| 5291 | } |
| 5292 | ifp->if_lastex = lastx; |
| 5293 | *nmap = n; |
| 5294 | /* |
| 5295 | * Transform from btree to extents, give it cur. |
| 5296 | */ |
| 5297 | if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5298 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5299 | ASSERT(wr && cur); |
| 5300 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 5301 | &tmp_logflags, whichfork); |
| 5302 | logflags |= tmp_logflags; |
| 5303 | if (error) |
| 5304 | goto error0; |
| 5305 | } |
| 5306 | ASSERT(ifp->if_ext_max == |
| 5307 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5308 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 5309 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 5310 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5311 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5312 | /* A change was actually made. |
| 5313 | * Note that delta->xed_blockount is an offset at this |
| 5314 | * point and needs to be converted to a block count. |
| 5315 | */ |
| 5316 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5317 | delta->xed_blockcount -= delta->xed_startoff; |
| 5318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | error0: |
| 5320 | /* |
| 5321 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5322 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5324 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5326 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5327 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5329 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | /* |
| 5331 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5332 | * detecting a case where the data is changed, there's an error, |
| 5333 | * and it's not logged so we don't shutdown when we should. |
| 5334 | */ |
| 5335 | if (logflags) { |
| 5336 | ASSERT(tp && wr); |
| 5337 | xfs_trans_log_inode(tp, ip, logflags); |
| 5338 | } |
| 5339 | if (cur) { |
| 5340 | if (!error) { |
| 5341 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5342 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5343 | XFS_FSB_TO_AGNO(mp, |
| 5344 | cur->bc_private.b.firstblock) || |
| 5345 | (flist->xbf_low && |
| 5346 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5347 | XFS_FSB_TO_AGNO(mp, |
| 5348 | cur->bc_private.b.firstblock))); |
| 5349 | *firstblock = cur->bc_private.b.firstblock; |
| 5350 | } |
| 5351 | xfs_btree_del_cursor(cur, |
| 5352 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5353 | } |
| 5354 | if (!error) |
| 5355 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5356 | orig_nmap, *nmap); |
| 5357 | return error; |
| 5358 | } |
| 5359 | |
| 5360 | /* |
| 5361 | * Map file blocks to filesystem blocks, simple version. |
| 5362 | * One block (extent) only, read-only. |
| 5363 | * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. |
| 5364 | * For the other flag values, the effect is as if XFS_BMAPI_METADATA |
| 5365 | * was set and all the others were clear. |
| 5366 | */ |
| 5367 | int /* error */ |
| 5368 | xfs_bmapi_single( |
| 5369 | xfs_trans_t *tp, /* transaction pointer */ |
| 5370 | xfs_inode_t *ip, /* incore inode */ |
| 5371 | int whichfork, /* data or attr fork */ |
| 5372 | xfs_fsblock_t *fsb, /* output: mapped block */ |
| 5373 | xfs_fileoff_t bno) /* starting file offs. mapped */ |
| 5374 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5375 | int eof; /* we've hit the end of extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5377 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5379 | xfs_extnum_t lastx; /* last useful extent number */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5380 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | |
| 5382 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5383 | if (unlikely( |
| 5384 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 5385 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) { |
| 5386 | XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW, |
| 5387 | ip->i_mount); |
| 5388 | return XFS_ERROR(EFSCORRUPTED); |
| 5389 | } |
| 5390 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5391 | return XFS_ERROR(EIO); |
| 5392 | XFS_STATS_INC(xs_blk_mapr); |
| 5393 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5394 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5395 | return error; |
| 5396 | (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5397 | &prev); |
| 5398 | /* |
| 5399 | * Reading past eof, act as though there's a hole |
| 5400 | * up to end. |
| 5401 | */ |
| 5402 | if (eof || got.br_startoff > bno) { |
| 5403 | *fsb = NULLFSBLOCK; |
| 5404 | return 0; |
| 5405 | } |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5406 | ASSERT(!isnullstartblock(got.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | ASSERT(bno < got.br_startoff + got.br_blockcount); |
| 5408 | *fsb = got.br_startblock + (bno - got.br_startoff); |
| 5409 | ifp->if_lastex = lastx; |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
| 5413 | /* |
| 5414 | * Unmap (remove) blocks from a file. |
| 5415 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5416 | * that value. If not all extents in the block range can be removed then |
| 5417 | * *done is set. |
| 5418 | */ |
| 5419 | int /* error */ |
| 5420 | xfs_bunmapi( |
| 5421 | xfs_trans_t *tp, /* transaction pointer */ |
| 5422 | struct xfs_inode *ip, /* incore inode */ |
| 5423 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5424 | xfs_filblks_t len, /* length to unmap in file */ |
| 5425 | int flags, /* misc flags */ |
| 5426 | xfs_extnum_t nexts, /* number of extents max */ |
| 5427 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5428 | controls a.g. for allocs */ |
| 5429 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5430 | xfs_extdelta_t *delta, /* o: change made to incore |
| 5431 | extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | int *done) /* set if not done yet */ |
| 5433 | { |
| 5434 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5435 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5436 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5437 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | int error; /* error return value */ |
| 5439 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5440 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5442 | int isrt; /* freeing in rt area */ |
| 5443 | xfs_extnum_t lastx; /* last extent index used */ |
| 5444 | int logflags; /* transaction logging flags */ |
| 5445 | xfs_extlen_t mod; /* rt extent offset */ |
| 5446 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5447 | xfs_extnum_t nextents; /* number of file extents */ |
| 5448 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5450 | int tmp_logflags; /* partial logging flags */ |
| 5451 | int wasdel; /* was a delayed alloc extent */ |
| 5452 | int whichfork; /* data or attribute fork */ |
| 5453 | int rsvd; /* OK to allocate reserved blocks */ |
| 5454 | xfs_fsblock_t sum; |
| 5455 | |
| 5456 | xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address); |
| 5457 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5458 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5459 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5460 | if (unlikely( |
| 5461 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5462 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5463 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5464 | ip->i_mount); |
| 5465 | return XFS_ERROR(EFSCORRUPTED); |
| 5466 | } |
| 5467 | mp = ip->i_mount; |
| 5468 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5469 | return XFS_ERROR(EIO); |
| 5470 | rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0; |
| 5471 | ASSERT(len > 0); |
| 5472 | ASSERT(nexts >= 0); |
| 5473 | ASSERT(ifp->if_ext_max == |
| 5474 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5475 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5476 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5477 | return error; |
| 5478 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5479 | if (nextents == 0) { |
| 5480 | *done = 1; |
| 5481 | return 0; |
| 5482 | } |
| 5483 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5484 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | start = bno; |
| 5486 | bno = start + len - 1; |
| 5487 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5488 | &prev); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5489 | if (delta) { |
| 5490 | delta->xed_startoff = NULLFILEOFF; |
| 5491 | delta->xed_blockcount = 0; |
| 5492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | /* |
| 5494 | * Check to see if the given block number is past the end of the |
| 5495 | * file, back up to the last block if so... |
| 5496 | */ |
| 5497 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5498 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | xfs_bmbt_get_all(ep, &got); |
| 5500 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5501 | } |
| 5502 | logflags = 0; |
| 5503 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5504 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5505 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | cur->bc_private.b.firstblock = *firstblock; |
| 5507 | cur->bc_private.b.flist = flist; |
| 5508 | cur->bc_private.b.flags = 0; |
| 5509 | } else |
| 5510 | cur = NULL; |
| 5511 | extno = 0; |
| 5512 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5513 | (nexts == 0 || extno < nexts)) { |
| 5514 | /* |
| 5515 | * Is the found extent after a hole in which bno lives? |
| 5516 | * Just back up to the previous extent, if so. |
| 5517 | */ |
| 5518 | if (got.br_startoff > bno) { |
| 5519 | if (--lastx < 0) |
| 5520 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5521 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | xfs_bmbt_get_all(ep, &got); |
| 5523 | } |
| 5524 | /* |
| 5525 | * Is the last block of this extent before the range |
| 5526 | * we're supposed to delete? If so, we're done. |
| 5527 | */ |
| 5528 | bno = XFS_FILEOFF_MIN(bno, |
| 5529 | got.br_startoff + got.br_blockcount - 1); |
| 5530 | if (bno < start) |
| 5531 | break; |
| 5532 | /* |
| 5533 | * Then deal with the (possibly delayed) allocated space |
| 5534 | * we found. |
| 5535 | */ |
| 5536 | ASSERT(ep != NULL); |
| 5537 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5538 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | if (got.br_startoff < start) { |
| 5540 | del.br_startoff = start; |
| 5541 | del.br_blockcount -= start - got.br_startoff; |
| 5542 | if (!wasdel) |
| 5543 | del.br_startblock += start - got.br_startoff; |
| 5544 | } |
| 5545 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5546 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5547 | sum = del.br_startblock + del.br_blockcount; |
| 5548 | if (isrt && |
| 5549 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5550 | /* |
| 5551 | * Realtime extent not lined up at the end. |
| 5552 | * The extent could have been split into written |
| 5553 | * and unwritten pieces, or we could just be |
| 5554 | * unmapping part of it. But we can't really |
| 5555 | * get rid of part of a realtime extent. |
| 5556 | */ |
| 5557 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5558 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | /* |
| 5560 | * This piece is unwritten, or we're not |
| 5561 | * using unwritten extents. Skip over it. |
| 5562 | */ |
| 5563 | ASSERT(bno >= mod); |
| 5564 | bno -= mod > del.br_blockcount ? |
| 5565 | del.br_blockcount : mod; |
| 5566 | if (bno < got.br_startoff) { |
| 5567 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5568 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5569 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | } |
| 5571 | continue; |
| 5572 | } |
| 5573 | /* |
| 5574 | * It's written, turn it unwritten. |
| 5575 | * This is better than zeroing it. |
| 5576 | */ |
| 5577 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5578 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5579 | /* |
| 5580 | * If this spans a realtime extent boundary, |
| 5581 | * chop it back to the start of the one we end at. |
| 5582 | */ |
| 5583 | if (del.br_blockcount > mod) { |
| 5584 | del.br_startoff += del.br_blockcount - mod; |
| 5585 | del.br_startblock += del.br_blockcount - mod; |
| 5586 | del.br_blockcount = mod; |
| 5587 | } |
| 5588 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5589 | error = xfs_bmap_add_extent(ip, lastx, &cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5590 | firstblock, flist, &logflags, delta, |
| 5591 | XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | if (error) |
| 5593 | goto error0; |
| 5594 | goto nodelete; |
| 5595 | } |
| 5596 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5597 | /* |
| 5598 | * Realtime extent is lined up at the end but not |
| 5599 | * at the front. We'll get rid of full extents if |
| 5600 | * we can. |
| 5601 | */ |
| 5602 | mod = mp->m_sb.sb_rextsize - mod; |
| 5603 | if (del.br_blockcount > mod) { |
| 5604 | del.br_blockcount -= mod; |
| 5605 | del.br_startoff += mod; |
| 5606 | del.br_startblock += mod; |
| 5607 | } else if ((del.br_startoff == start && |
| 5608 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5609 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5610 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | /* |
| 5612 | * Can't make it unwritten. There isn't |
| 5613 | * a full extent here so just skip it. |
| 5614 | */ |
| 5615 | ASSERT(bno >= del.br_blockcount); |
| 5616 | bno -= del.br_blockcount; |
| 5617 | if (bno < got.br_startoff) { |
| 5618 | if (--lastx >= 0) |
| 5619 | xfs_bmbt_get_all(--ep, &got); |
| 5620 | } |
| 5621 | continue; |
| 5622 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5623 | /* |
| 5624 | * This one is already unwritten. |
| 5625 | * It must have a written left neighbor. |
| 5626 | * Unwrite the killed part of that one and |
| 5627 | * try again. |
| 5628 | */ |
| 5629 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5630 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5631 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5633 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | ASSERT(del.br_startblock == |
| 5635 | prev.br_startblock + prev.br_blockcount); |
| 5636 | if (prev.br_startoff < start) { |
| 5637 | mod = start - prev.br_startoff; |
| 5638 | prev.br_blockcount -= mod; |
| 5639 | prev.br_startblock += mod; |
| 5640 | prev.br_startoff = start; |
| 5641 | } |
| 5642 | prev.br_state = XFS_EXT_UNWRITTEN; |
| 5643 | error = xfs_bmap_add_extent(ip, lastx - 1, &cur, |
| 5644 | &prev, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5645 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | if (error) |
| 5647 | goto error0; |
| 5648 | goto nodelete; |
| 5649 | } else { |
| 5650 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5651 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5652 | error = xfs_bmap_add_extent(ip, lastx, &cur, |
| 5653 | &del, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5654 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | if (error) |
| 5656 | goto error0; |
| 5657 | goto nodelete; |
| 5658 | } |
| 5659 | } |
| 5660 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5661 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5662 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5663 | if (isrt) { |
| 5664 | xfs_filblks_t rtexts; |
| 5665 | |
| 5666 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5667 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5668 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5669 | (int64_t)rtexts, rsvd); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5670 | (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, |
| 5671 | NULL, ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5672 | XFS_QMOPT_RES_RTBLKS); |
| 5673 | } else { |
| 5674 | xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5675 | (int64_t)del.br_blockcount, rsvd); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5676 | (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, |
| 5677 | NULL, ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | ip->i_delayed_blks -= del.br_blockcount; |
| 5681 | if (cur) |
| 5682 | cur->bc_private.b.flags |= |
| 5683 | XFS_BTCUR_BPRV_WASDEL; |
| 5684 | } else if (cur) |
| 5685 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5686 | /* |
| 5687 | * If it's the case where the directory code is running |
| 5688 | * with no block reservation, and the deleted block is in |
| 5689 | * the middle of its extent, and the resulting insert |
| 5690 | * of an extent would cause transformation to btree format, |
| 5691 | * then reject it. The calling code will then swap |
| 5692 | * blocks around instead. |
| 5693 | * We have to do this now, rather than waiting for the |
| 5694 | * conversion to btree format, since the transaction |
| 5695 | * will be dirty. |
| 5696 | */ |
| 5697 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5698 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5699 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5700 | del.br_startoff > got.br_startoff && |
| 5701 | del.br_startoff + del.br_blockcount < |
| 5702 | got.br_startoff + got.br_blockcount) { |
| 5703 | error = XFS_ERROR(ENOSPC); |
| 5704 | goto error0; |
| 5705 | } |
| 5706 | error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5707 | &tmp_logflags, delta, whichfork, rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | logflags |= tmp_logflags; |
| 5709 | if (error) |
| 5710 | goto error0; |
| 5711 | bno = del.br_startoff - 1; |
| 5712 | nodelete: |
| 5713 | lastx = ifp->if_lastex; |
| 5714 | /* |
| 5715 | * If not done go on to the next (previous) record. |
| 5716 | * Reset ep in case the extents array was re-alloced. |
| 5717 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5718 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
| 5720 | if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) || |
| 5721 | xfs_bmbt_get_startoff(ep) > bno) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5722 | if (--lastx >= 0) |
| 5723 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | } |
| 5725 | if (lastx >= 0) |
| 5726 | xfs_bmbt_get_all(ep, &got); |
| 5727 | extno++; |
| 5728 | } |
| 5729 | } |
| 5730 | ifp->if_lastex = lastx; |
| 5731 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5732 | ASSERT(ifp->if_ext_max == |
| 5733 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5734 | /* |
| 5735 | * Convert to a btree if necessary. |
| 5736 | */ |
| 5737 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5738 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5739 | ASSERT(cur == NULL); |
| 5740 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5741 | &cur, 0, &tmp_logflags, whichfork); |
| 5742 | logflags |= tmp_logflags; |
| 5743 | if (error) |
| 5744 | goto error0; |
| 5745 | } |
| 5746 | /* |
| 5747 | * transform from btree to extents, give it cur |
| 5748 | */ |
| 5749 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5750 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5751 | ASSERT(cur != NULL); |
| 5752 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5753 | whichfork); |
| 5754 | logflags |= tmp_logflags; |
| 5755 | if (error) |
| 5756 | goto error0; |
| 5757 | } |
| 5758 | /* |
| 5759 | * transform from extents to local? |
| 5760 | */ |
| 5761 | ASSERT(ifp->if_ext_max == |
| 5762 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5763 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5764 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5765 | /* A change was actually made. |
| 5766 | * Note that delta->xed_blockount is an offset at this |
| 5767 | * point and needs to be converted to a block count. |
| 5768 | */ |
| 5769 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5770 | delta->xed_blockcount -= delta->xed_startoff; |
| 5771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | error0: |
| 5773 | /* |
| 5774 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5775 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5777 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5779 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5780 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5782 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5783 | /* |
| 5784 | * Log inode even in the error case, if the transaction |
| 5785 | * is dirty we'll need to shut down the filesystem. |
| 5786 | */ |
| 5787 | if (logflags) |
| 5788 | xfs_trans_log_inode(tp, ip, logflags); |
| 5789 | if (cur) { |
| 5790 | if (!error) { |
| 5791 | *firstblock = cur->bc_private.b.firstblock; |
| 5792 | cur->bc_private.b.allocated = 0; |
| 5793 | } |
| 5794 | xfs_btree_del_cursor(cur, |
| 5795 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5796 | } |
| 5797 | return error; |
| 5798 | } |
| 5799 | |
| 5800 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5801 | * returns 1 for success, 0 if we failed to map the extent. |
| 5802 | */ |
| 5803 | STATIC int |
| 5804 | xfs_getbmapx_fix_eof_hole( |
| 5805 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5806 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5807 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5808 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5809 | __int64_t end, /* last block requested */ |
| 5810 | xfs_fsblock_t startblock) |
| 5811 | { |
| 5812 | __int64_t fixlen; |
| 5813 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5814 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5815 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5816 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5817 | |
| 5818 | if (startblock == HOLESTARTBLOCK) { |
| 5819 | mp = ip->i_mount; |
| 5820 | out->bmv_block = -1; |
| 5821 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5822 | fixlen -= out->bmv_offset; |
| 5823 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5824 | /* Came to hole at EOF. Trim it. */ |
| 5825 | if (fixlen <= 0) |
| 5826 | return 0; |
| 5827 | out->bmv_length = fixlen; |
| 5828 | } |
| 5829 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5830 | if (startblock == DELAYSTARTBLOCK) |
| 5831 | out->bmv_block = -2; |
| 5832 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5833 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5834 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5835 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5836 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5837 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5838 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | return 1; |
| 5842 | } |
| 5843 | |
| 5844 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5845 | * Get inode's extents as described in bmv, and format for output. |
| 5846 | * Calls formatter to fill the user's buffer until all extents |
| 5847 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5848 | * been filled, or until the formatter short-circuits the loop, |
| 5849 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | */ |
| 5851 | int /* error code */ |
| 5852 | xfs_getbmap( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5853 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5854 | struct getbmapx *bmv, /* user bmap structure */ |
| 5855 | xfs_bmap_format_t formatter, /* format to user */ |
| 5856 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | { |
| 5858 | __int64_t bmvend; /* last block requested */ |
| 5859 | int error; /* return value */ |
| 5860 | __int64_t fixlen; /* length for -1 case */ |
| 5861 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | int lock; /* lock state */ |
| 5863 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5864 | xfs_mount_t *mp; /* file system mount point */ |
| 5865 | int nex; /* # of user extents can do */ |
| 5866 | int nexleft; /* # of user extents left */ |
| 5867 | int subnex; /* # of bmapi's can do */ |
| 5868 | int nmap; /* number of map entries */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5869 | struct getbmapx out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | int whichfork; /* data or attr fork */ |
| 5871 | int prealloced; /* this is a file with |
| 5872 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5873 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | int bmapi_flags; /* flags for xfs_bmapi */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5877 | iflags = bmv->bmv_iflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5879 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | |
| 5881 | /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not |
| 5882 | * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ |
| 5883 | * bit is set for the file, generate a read event in order |
| 5884 | * that the DMAPI application may do its thing before we return |
| 5885 | * the extents. Usually this means restoring user file data to |
| 5886 | * regions of the file that look like holes. |
| 5887 | * |
| 5888 | * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify |
| 5889 | * BMV_IF_NO_DMAPI_READ so that read events are generated. |
| 5890 | * If this were not true, callers of ioctl( XFS_IOC_GETBMAP ) |
| 5891 | * could misinterpret holes in a DMAPI file as true holes, |
| 5892 | * when in fact they may represent offline user data. |
| 5893 | */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5894 | if ((iflags & BMV_IF_NO_DMAPI_READ) == 0 && |
Christoph Hellwig | eb9df39 | 2007-08-16 18:42:07 +1000 | [diff] [blame] | 5895 | DM_EVENT_ENABLED(ip, DM_EVENT_READ) && |
| 5896 | whichfork == XFS_DATA_FORK) { |
Christoph Hellwig | bc4ac74 | 2008-03-06 13:45:58 +1100 | [diff] [blame] | 5897 | error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, 0, 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | if (error) |
| 5899 | return XFS_ERROR(error); |
| 5900 | } |
| 5901 | |
| 5902 | if (whichfork == XFS_ATTR_FORK) { |
| 5903 | if (XFS_IFORK_Q(ip)) { |
| 5904 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5905 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5906 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5907 | return XFS_ERROR(EINVAL); |
| 5908 | } else if (unlikely( |
| 5909 | ip->i_d.di_aformat != 0 && |
| 5910 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5911 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5912 | ip->i_mount); |
| 5913 | return XFS_ERROR(EFSCORRUPTED); |
| 5914 | } |
| 5915 | } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5916 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5917 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5918 | return XFS_ERROR(EINVAL); |
| 5919 | if (whichfork == XFS_DATA_FORK) { |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5920 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5921 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | prealloced = 1; |
| 5923 | fixlen = XFS_MAXIOFFSET(mp); |
| 5924 | } else { |
| 5925 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5926 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | } |
| 5928 | } else { |
| 5929 | prealloced = 0; |
| 5930 | fixlen = 1LL << 32; |
| 5931 | } |
| 5932 | |
| 5933 | if (bmv->bmv_length == -1) { |
| 5934 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
| 5935 | bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset), |
| 5936 | (__int64_t)0); |
| 5937 | } else if (bmv->bmv_length < 0) |
| 5938 | return XFS_ERROR(EINVAL); |
| 5939 | if (bmv->bmv_length == 0) { |
| 5940 | bmv->bmv_entries = 0; |
| 5941 | return 0; |
| 5942 | } |
| 5943 | nex = bmv->bmv_count - 1; |
| 5944 | if (nex <= 0) |
| 5945 | return XFS_ERROR(EINVAL); |
| 5946 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5947 | |
| 5948 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 5949 | |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5950 | if (((iflags & BMV_IF_DELALLOC) == 0) && |
| 5951 | (whichfork == XFS_DATA_FORK) && |
| 5952 | (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */ |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 5954 | error = xfs_flush_pages(ip, (xfs_off_t)0, |
| 5955 | -1, 0, FI_REMAPF); |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5956 | if (error) { |
| 5957 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 5958 | return error; |
| 5959 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | } |
| 5961 | |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5962 | ASSERT(whichfork == XFS_ATTR_FORK || (iflags & BMV_IF_DELALLOC) || |
| 5963 | ip->i_delayed_blks == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5964 | |
| 5965 | lock = xfs_ilock_map_shared(ip); |
| 5966 | |
| 5967 | /* |
| 5968 | * Don't let nex be bigger than the number of extents |
| 5969 | * we can have assuming alternating holes and real extents. |
| 5970 | */ |
| 5971 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5972 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5973 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5974 | bmapi_flags = xfs_bmapi_aflag(whichfork) | |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5975 | ((iflags & BMV_IF_PREALLOC) ? 0 : XFS_BMAPI_IGSTATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | |
| 5977 | /* |
| 5978 | * Allocate enough space to handle "subnex" maps at a time. |
| 5979 | */ |
| 5980 | subnex = 16; |
| 5981 | map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP); |
| 5982 | |
| 5983 | bmv->bmv_entries = 0; |
| 5984 | |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5985 | if ((XFS_IFORK_NEXTENTS(ip, whichfork) == 0)) { |
| 5986 | if (((iflags & BMV_IF_DELALLOC) == 0) || |
| 5987 | whichfork == XFS_ATTR_FORK) { |
| 5988 | error = 0; |
| 5989 | goto unlock_and_return; |
| 5990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | nexleft = nex; |
| 5994 | |
| 5995 | do { |
| 5996 | nmap = (nexleft > subnex) ? subnex : nexleft; |
| 5997 | error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5998 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5999 | bmapi_flags, NULL, 0, map, &nmap, |
| 6000 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | if (error) |
| 6002 | goto unlock_and_return; |
| 6003 | ASSERT(nmap <= subnex); |
| 6004 | |
| 6005 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 6006 | out.bmv_oflags = 0; |
| 6007 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
| 6008 | out.bmv_oflags |= BMV_OF_PREALLOC; |
| 6009 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
| 6010 | out.bmv_oflags |= BMV_OF_DELALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 6012 | out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 6013 | out.bmv_unused1 = out.bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 6014 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 6015 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 6016 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 6017 | whichfork == XFS_ATTR_FORK) { |
| 6018 | /* came to the end of attribute fork */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 6019 | out.bmv_oflags |= BMV_OF_LAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | goto unlock_and_return; |
| 6021 | } else { |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 6022 | int full = 0; /* user array is full */ |
| 6023 | |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 6024 | if (!xfs_getbmapx_fix_eof_hole(ip, &out, |
| 6025 | prealloced, bmvend, |
| 6026 | map[i].br_startblock)) { |
| 6027 | goto unlock_and_return; |
| 6028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 6030 | /* format results & advance arg */ |
| 6031 | error = formatter(&arg, &out, &full); |
| 6032 | if (error || full) |
| 6033 | goto unlock_and_return; |
| 6034 | nexleft--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | bmv->bmv_offset = |
| 6036 | out.bmv_offset + out.bmv_length; |
| 6037 | bmv->bmv_length = MAX((__int64_t)0, |
| 6038 | (__int64_t)(bmvend - bmv->bmv_offset)); |
| 6039 | bmv->bmv_entries++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | } |
| 6041 | } |
| 6042 | } while (nmap && nexleft && bmv->bmv_length); |
| 6043 | |
| 6044 | unlock_and_return: |
| 6045 | xfs_iunlock_map_shared(ip, lock); |
| 6046 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 6047 | |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 6048 | kmem_free(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | |
| 6050 | return error; |
| 6051 | } |
| 6052 | |
| 6053 | /* |
| 6054 | * Check the last inode extent to determine whether this allocation will result |
| 6055 | * in blocks being allocated at the end of the file. When we allocate new data |
| 6056 | * blocks at the end of the file which do not start at the previous data block, |
| 6057 | * we will try to align the new blocks at stripe unit boundaries. |
| 6058 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 6059 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | xfs_bmap_isaeof( |
| 6061 | xfs_inode_t *ip, /* incore inode pointer */ |
| 6062 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 6063 | int whichfork, /* data or attribute fork */ |
| 6064 | char *aeof) /* return value */ |
| 6065 | { |
| 6066 | int error; /* error return value */ |
| 6067 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6068 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6069 | xfs_extnum_t nextents; /* number of file extents */ |
| 6070 | xfs_bmbt_irec_t s; /* expanded extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | |
| 6072 | ASSERT(whichfork == XFS_DATA_FORK); |
| 6073 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6074 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 6075 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 6076 | return error; |
| 6077 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 6078 | if (nextents == 0) { |
| 6079 | *aeof = 1; |
| 6080 | return 0; |
| 6081 | } |
| 6082 | /* |
| 6083 | * Go to the last extent |
| 6084 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6085 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | xfs_bmbt_get_all(lastrec, &s); |
| 6087 | /* |
| 6088 | * Check we are allocating in the last extent (for delayed allocations) |
| 6089 | * or past the last extent for non-delayed allocations. |
| 6090 | */ |
| 6091 | *aeof = (off >= s.br_startoff && |
| 6092 | off < s.br_startoff + s.br_blockcount && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 6093 | isnullstartblock(s.br_startblock)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | off >= s.br_startoff + s.br_blockcount; |
| 6095 | return 0; |
| 6096 | } |
| 6097 | |
| 6098 | /* |
| 6099 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 6100 | * the allocation to a stripe unit boundary. |
| 6101 | */ |
| 6102 | int /* error */ |
| 6103 | xfs_bmap_eof( |
| 6104 | xfs_inode_t *ip, /* incore inode pointer */ |
| 6105 | xfs_fileoff_t endoff, /* file offset in fsblocks */ |
| 6106 | int whichfork, /* data or attribute fork */ |
| 6107 | int *eof) /* result value */ |
| 6108 | { |
| 6109 | xfs_fsblock_t blockcount; /* extent block count */ |
| 6110 | int error; /* error return value */ |
| 6111 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6112 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6113 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | xfs_fileoff_t startoff; /* extent starting file offset */ |
| 6115 | |
| 6116 | ASSERT(whichfork == XFS_DATA_FORK); |
| 6117 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6118 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 6119 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 6120 | return error; |
| 6121 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 6122 | if (nextents == 0) { |
| 6123 | *eof = 1; |
| 6124 | return 0; |
| 6125 | } |
| 6126 | /* |
| 6127 | * Go to the last extent |
| 6128 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6129 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | startoff = xfs_bmbt_get_startoff(lastrec); |
| 6131 | blockcount = xfs_bmbt_get_blockcount(lastrec); |
| 6132 | *eof = endoff >= startoff + blockcount; |
| 6133 | return 0; |
| 6134 | } |
| 6135 | |
| 6136 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | STATIC |
| 6138 | xfs_buf_t * |
| 6139 | xfs_bmap_get_bp( |
| 6140 | xfs_btree_cur_t *cur, |
| 6141 | xfs_fsblock_t bno) |
| 6142 | { |
| 6143 | int i; |
| 6144 | xfs_buf_t *bp; |
| 6145 | |
| 6146 | if (!cur) |
| 6147 | return(NULL); |
| 6148 | |
| 6149 | bp = NULL; |
| 6150 | for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 6151 | bp = cur->bc_bufs[i]; |
| 6152 | if (!bp) break; |
| 6153 | if (XFS_BUF_ADDR(bp) == bno) |
| 6154 | break; /* Found it */ |
| 6155 | } |
| 6156 | if (i == XFS_BTREE_MAXLEVELS) |
| 6157 | bp = NULL; |
| 6158 | |
| 6159 | if (!bp) { /* Chase down all the log items to see if the bp is there */ |
| 6160 | xfs_log_item_chunk_t *licp; |
| 6161 | xfs_trans_t *tp; |
| 6162 | |
| 6163 | tp = cur->bc_tp; |
| 6164 | licp = &tp->t_items; |
| 6165 | while (!bp && licp != NULL) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 6166 | if (xfs_lic_are_all_free(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | licp = licp->lic_next; |
| 6168 | continue; |
| 6169 | } |
| 6170 | for (i = 0; i < licp->lic_unused; i++) { |
| 6171 | xfs_log_item_desc_t *lidp; |
| 6172 | xfs_log_item_t *lip; |
| 6173 | xfs_buf_log_item_t *bip; |
| 6174 | xfs_buf_t *lbp; |
| 6175 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 6176 | if (xfs_lic_isfree(licp, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6177 | continue; |
| 6178 | } |
| 6179 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 6180 | lidp = xfs_lic_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | lip = lidp->lid_item; |
| 6182 | if (lip->li_type != XFS_LI_BUF) |
| 6183 | continue; |
| 6184 | |
| 6185 | bip = (xfs_buf_log_item_t *)lip; |
| 6186 | lbp = bip->bli_buf; |
| 6187 | |
| 6188 | if (XFS_BUF_ADDR(lbp) == bno) { |
| 6189 | bp = lbp; |
| 6190 | break; /* Found it */ |
| 6191 | } |
| 6192 | } |
| 6193 | licp = licp->lic_next; |
| 6194 | } |
| 6195 | } |
| 6196 | return(bp); |
| 6197 | } |
| 6198 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 6199 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6201 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | xfs_mount_t *mp, |
| 6203 | int root, |
| 6204 | short sz) |
| 6205 | { |
| 6206 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6207 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | xfs_bmbt_key_t *prevp, *keyp; |
| 6209 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6210 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | |
| 6212 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6213 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6215 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | |
| 6217 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6218 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 6219 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | } |
| 6221 | prevp = keyp; |
| 6222 | |
| 6223 | /* |
| 6224 | * Compare the block numbers to see if there are dups. |
| 6225 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6226 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6227 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6228 | else |
| 6229 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 6230 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6231 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6232 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6233 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6234 | else |
| 6235 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6236 | if (*thispa == *pp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6238 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6239 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6241 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | } |
| 6243 | } |
| 6244 | } |
| 6245 | } |
| 6246 | |
| 6247 | /* |
| 6248 | * Check that the extents for the inode ip are in the right order in all |
| 6249 | * btree leaves. |
| 6250 | */ |
| 6251 | |
| 6252 | STATIC void |
| 6253 | xfs_bmap_check_leaf_extents( |
| 6254 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 6255 | xfs_inode_t *ip, /* incore inode pointer */ |
| 6256 | int whichfork) /* data or attr fork */ |
| 6257 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6258 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6260 | xfs_buf_t *bp; /* buffer for "block" */ |
| 6261 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6262 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | xfs_ifork_t *ifp; /* fork structure */ |
| 6264 | int level; /* btree level, for checking */ |
| 6265 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6266 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6267 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6268 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6269 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | int bp_release = 0; |
| 6271 | |
| 6272 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 6273 | return; |
| 6274 | } |
| 6275 | |
| 6276 | bno = NULLFSBLOCK; |
| 6277 | mp = ip->i_mount; |
| 6278 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6279 | block = ifp->if_broot; |
| 6280 | /* |
| 6281 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6282 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6283 | level = be16_to_cpu(block->bb_level); |
| 6284 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6285 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6286 | 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] | 6287 | bno = be64_to_cpu(*pp); |
| 6288 | |
| 6289 | ASSERT(bno != NULLDFSBNO); |
| 6290 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6291 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 6292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | /* |
| 6294 | * Go down the tree until leaf level is reached, following the first |
| 6295 | * pointer (leftmost) at each level. |
| 6296 | */ |
| 6297 | while (level-- > 0) { |
| 6298 | /* See if buf is in cur first */ |
| 6299 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6300 | if (bp) { |
| 6301 | bp_release = 0; |
| 6302 | } else { |
| 6303 | bp_release = 1; |
| 6304 | } |
| 6305 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6306 | XFS_BMAP_BTREE_REF))) |
| 6307 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6308 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 6310 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | error0); |
| 6312 | if (level == 0) |
| 6313 | break; |
| 6314 | |
| 6315 | /* |
| 6316 | * Check this block for basic sanity (increasing keys and |
| 6317 | * no duplicate blocks). |
| 6318 | */ |
| 6319 | |
| 6320 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6321 | 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] | 6322 | bno = be64_to_cpu(*pp); |
| 6323 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | if (bp_release) { |
| 6325 | bp_release = 0; |
| 6326 | xfs_trans_brelse(NULL, bp); |
| 6327 | } |
| 6328 | } |
| 6329 | |
| 6330 | /* |
| 6331 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 6332 | */ |
| 6333 | i = 0; |
| 6334 | |
| 6335 | /* |
| 6336 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 6337 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | xfs_fsblock_t nextbno; |
| 6340 | xfs_extnum_t num_recs; |
| 6341 | |
| 6342 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6343 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | |
| 6345 | /* |
| 6346 | * Read-ahead the next leaf block, if any. |
| 6347 | */ |
| 6348 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6349 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | |
| 6351 | /* |
| 6352 | * Check all the extents to make sure they are OK. |
| 6353 | * If we had a previous block, the last entry should |
| 6354 | * conform with the first entry in this one. |
| 6355 | */ |
| 6356 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6357 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6358 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6359 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 6360 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 6361 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6362 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6363 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6364 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6365 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 6366 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 6367 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6368 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6371 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | i += num_recs; |
| 6373 | if (bp_release) { |
| 6374 | bp_release = 0; |
| 6375 | xfs_trans_brelse(NULL, bp); |
| 6376 | } |
| 6377 | bno = nextbno; |
| 6378 | /* |
| 6379 | * If we've reached the end, stop. |
| 6380 | */ |
| 6381 | if (bno == NULLFSBLOCK) |
| 6382 | break; |
| 6383 | |
| 6384 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6385 | if (bp) { |
| 6386 | bp_release = 0; |
| 6387 | } else { |
| 6388 | bp_release = 1; |
| 6389 | } |
| 6390 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6391 | XFS_BMAP_BTREE_REF))) |
| 6392 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6393 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | } |
| 6395 | if (bp_release) { |
| 6396 | bp_release = 0; |
| 6397 | xfs_trans_brelse(NULL, bp); |
| 6398 | } |
| 6399 | return; |
| 6400 | |
| 6401 | error0: |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6402 | cmn_err(CE_WARN, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | if (bp_release) |
| 6404 | xfs_trans_brelse(NULL, bp); |
| 6405 | error_norelse: |
| 6406 | cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6407 | __func__, i); |
| 6408 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | return; |
| 6410 | } |
| 6411 | #endif |
| 6412 | |
| 6413 | /* |
| 6414 | * Count fsblocks of the given fork. |
| 6415 | */ |
| 6416 | int /* error */ |
| 6417 | xfs_bmap_count_blocks( |
| 6418 | xfs_trans_t *tp, /* transaction pointer */ |
| 6419 | xfs_inode_t *ip, /* incore inode */ |
| 6420 | int whichfork, /* data or attr fork */ |
| 6421 | int *count) /* out: count of blocks */ |
| 6422 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6423 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6425 | xfs_ifork_t *ifp; /* fork structure */ |
| 6426 | int level; /* btree level, for checking */ |
| 6427 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6428 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6429 | |
| 6430 | bno = NULLFSBLOCK; |
| 6431 | mp = ip->i_mount; |
| 6432 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6433 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6434 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6436 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | return 0; |
| 6438 | } |
| 6439 | |
| 6440 | /* |
| 6441 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6442 | */ |
| 6443 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6444 | level = be16_to_cpu(block->bb_level); |
| 6445 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6446 | 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] | 6447 | bno = be64_to_cpu(*pp); |
| 6448 | ASSERT(bno != NULLDFSBNO); |
| 6449 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6450 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6452 | 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] | 6453 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 6454 | mp); |
| 6455 | return XFS_ERROR(EFSCORRUPTED); |
| 6456 | } |
| 6457 | |
| 6458 | return 0; |
| 6459 | } |
| 6460 | |
| 6461 | /* |
| 6462 | * Recursively walks each level of a btree |
| 6463 | * to count total fsblocks is use. |
| 6464 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 6465 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | xfs_bmap_count_tree( |
| 6467 | xfs_mount_t *mp, /* file system mount point */ |
| 6468 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6469 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6470 | xfs_fsblock_t blockno, /* file system block number */ |
| 6471 | int levelin, /* level in btree */ |
| 6472 | int *count) /* Count of blocks */ |
| 6473 | { |
| 6474 | int error; |
| 6475 | xfs_buf_t *bp, *nbp; |
| 6476 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6477 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | xfs_fsblock_t bno = blockno; |
| 6479 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6480 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | |
| 6483 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 6484 | return error; |
| 6485 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6486 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6487 | |
| 6488 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame^] | 6489 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6490 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | while (nextbno != NULLFSBLOCK) { |
| 6492 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6493 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6494 | return error; |
| 6495 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6496 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6497 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | xfs_trans_brelse(tp, nbp); |
| 6499 | } |
| 6500 | |
| 6501 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6502 | 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] | 6503 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6505 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | xfs_trans_brelse(tp, bp); |
| 6507 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6508 | XFS_ERRLEVEL_LOW, mp); |
| 6509 | return XFS_ERROR(EFSCORRUPTED); |
| 6510 | } |
| 6511 | xfs_trans_brelse(tp, bp); |
| 6512 | } else { |
| 6513 | /* count all level 1 nodes and their leaves */ |
| 6514 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6515 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6516 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6517 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | xfs_trans_brelse(tp, bp); |
| 6519 | if (nextbno == NULLFSBLOCK) |
| 6520 | break; |
| 6521 | bno = nextbno; |
| 6522 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6523 | XFS_BMAP_BTREE_REF))) |
| 6524 | return error; |
| 6525 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6526 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6527 | } |
| 6528 | } |
| 6529 | return 0; |
| 6530 | } |
| 6531 | |
| 6532 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6533 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6535 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6537 | xfs_ifork_t *ifp, |
| 6538 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | int numrecs, |
| 6540 | int *count) |
| 6541 | { |
| 6542 | int b; |
| 6543 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6544 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6545 | 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] | 6546 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6547 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6551 | * Count leaf blocks given a range of extent records originally |
| 6552 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6553 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6554 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6555 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6556 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6557 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6558 | int numrecs, |
| 6559 | int *count) |
| 6560 | { |
| 6561 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6562 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6563 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6564 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6565 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | } |