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" |
| 55 | |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | STATIC void |
| 59 | xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork); |
| 60 | #endif |
| 61 | |
| 62 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 63 | |
| 64 | /* |
| 65 | * Prototypes for internal bmap routines. |
| 66 | */ |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 71 | */ |
| 72 | STATIC int /* error */ |
| 73 | xfs_bmap_add_attrfork_extents( |
| 74 | xfs_trans_t *tp, /* transaction pointer */ |
| 75 | xfs_inode_t *ip, /* incore inode pointer */ |
| 76 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 77 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 78 | int *flags); /* inode logging flags */ |
| 79 | |
| 80 | /* |
| 81 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 82 | */ |
| 83 | STATIC int /* error */ |
| 84 | xfs_bmap_add_attrfork_local( |
| 85 | xfs_trans_t *tp, /* transaction pointer */ |
| 86 | xfs_inode_t *ip, /* incore inode pointer */ |
| 87 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 88 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 89 | int *flags); /* inode logging flags */ |
| 90 | |
| 91 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 92 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * after allocating space (or doing a delayed allocation). |
| 94 | */ |
| 95 | STATIC int /* error */ |
| 96 | xfs_bmap_add_extent( |
| 97 | xfs_inode_t *ip, /* incore inode pointer */ |
| 98 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 99 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 100 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 102 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 103 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 104 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | int whichfork, /* data or attr fork */ |
| 106 | int rsvd); /* OK to allocate reserved blocks */ |
| 107 | |
| 108 | /* |
| 109 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 110 | * allocation to a real allocation. |
| 111 | */ |
| 112 | STATIC int /* error */ |
| 113 | xfs_bmap_add_extent_delay_real( |
| 114 | xfs_inode_t *ip, /* incore inode pointer */ |
| 115 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 116 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 117 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 119 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 120 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 121 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 122 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | int rsvd); /* OK to allocate reserved blocks */ |
| 124 | |
| 125 | /* |
| 126 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 127 | * to a delayed allocation. |
| 128 | */ |
| 129 | STATIC int /* error */ |
| 130 | xfs_bmap_add_extent_hole_delay( |
| 131 | xfs_inode_t *ip, /* incore inode pointer */ |
| 132 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 133 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 135 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int rsvd); /* OK to allocate reserved blocks */ |
| 137 | |
| 138 | /* |
| 139 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 140 | * to a real allocation. |
| 141 | */ |
| 142 | STATIC int /* error */ |
| 143 | xfs_bmap_add_extent_hole_real( |
| 144 | xfs_inode_t *ip, /* incore inode pointer */ |
| 145 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 146 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 147 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 149 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | int whichfork); /* data or attr fork */ |
| 151 | |
| 152 | /* |
| 153 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 154 | * allocation to a real allocation or vice versa. |
| 155 | */ |
| 156 | STATIC int /* error */ |
| 157 | xfs_bmap_add_extent_unwritten_real( |
| 158 | xfs_inode_t *ip, /* incore inode pointer */ |
| 159 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 160 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 161 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 162 | int *logflagsp, /* inode logging flags */ |
| 163 | xfs_extdelta_t *delta); /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 167 | * It figures out where to ask the underlying allocator to put the new extent. |
| 168 | */ |
| 169 | STATIC int /* error */ |
| 170 | xfs_bmap_alloc( |
| 171 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 172 | |
| 173 | /* |
| 174 | * Transform a btree format file with only one leaf node, where the |
| 175 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 176 | * 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] | 177 | * give up the space for the btree root and pitch the leaf block. |
| 178 | */ |
| 179 | STATIC int /* error */ |
| 180 | xfs_bmap_btree_to_extents( |
| 181 | xfs_trans_t *tp, /* transaction pointer */ |
| 182 | xfs_inode_t *ip, /* incore inode pointer */ |
| 183 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 184 | int *logflagsp, /* inode logging flags */ |
| 185 | int whichfork); /* data or attr fork */ |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 188 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * after removing space (or undoing a delayed allocation). |
| 190 | */ |
| 191 | STATIC int /* error */ |
| 192 | xfs_bmap_del_extent( |
| 193 | xfs_inode_t *ip, /* incore inode pointer */ |
| 194 | xfs_trans_t *tp, /* current trans pointer */ |
| 195 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 196 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 197 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 198 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 200 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | int whichfork, /* data or attr fork */ |
| 202 | int rsvd); /* OK to allocate reserved blocks */ |
| 203 | |
| 204 | /* |
| 205 | * Remove the entry "free" from the free item list. Prev points to the |
| 206 | * previous entry, unless "free" is the head of the list. |
| 207 | */ |
| 208 | STATIC void |
| 209 | xfs_bmap_del_free( |
| 210 | xfs_bmap_free_t *flist, /* free item list header */ |
| 211 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 212 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 213 | |
| 214 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * Convert an extents-format file into a btree-format file. |
| 216 | * The new file will have a root block (in the inode) and a single child block. |
| 217 | */ |
| 218 | STATIC int /* error */ |
| 219 | xfs_bmap_extents_to_btree( |
| 220 | xfs_trans_t *tp, /* transaction pointer */ |
| 221 | xfs_inode_t *ip, /* incore inode pointer */ |
| 222 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 223 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 224 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 225 | int wasdel, /* converting a delayed alloc */ |
| 226 | int *logflagsp, /* inode logging flags */ |
| 227 | int whichfork); /* data or attr fork */ |
| 228 | |
| 229 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * Convert a local file to an extents file. |
| 231 | * This code is sort of bogus, since the file data needs to get |
| 232 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 233 | */ |
| 234 | STATIC int /* error */ |
| 235 | xfs_bmap_local_to_extents( |
| 236 | xfs_trans_t *tp, /* transaction pointer */ |
| 237 | xfs_inode_t *ip, /* incore inode pointer */ |
| 238 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 239 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 240 | int *logflagsp, /* inode logging flags */ |
| 241 | int whichfork); /* data or attr fork */ |
| 242 | |
| 243 | /* |
| 244 | * Search the extents list for the inode, for the extent containing bno. |
| 245 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 246 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 247 | * Else, *lastxp will be set to the index of the found |
| 248 | * entry; *gotp will contain the entry. |
| 249 | */ |
| 250 | STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */ |
| 251 | xfs_bmap_search_extents( |
| 252 | xfs_inode_t *ip, /* incore inode pointer */ |
| 253 | xfs_fileoff_t bno, /* block number searched for */ |
| 254 | int whichfork, /* data or attr fork */ |
| 255 | int *eofp, /* out: end of file found */ |
| 256 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 257 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 258 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 259 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 260 | /* |
| 261 | * Check the last inode extent to determine whether this allocation will result |
| 262 | * in blocks being allocated at the end of the file. When we allocate new data |
| 263 | * blocks at the end of the file which do not start at the previous data block, |
| 264 | * we will try to align the new blocks at stripe unit boundaries. |
| 265 | */ |
| 266 | STATIC int /* error */ |
| 267 | xfs_bmap_isaeof( |
| 268 | xfs_inode_t *ip, /* incore inode pointer */ |
| 269 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 270 | int whichfork, /* data or attribute fork */ |
| 271 | char *aeof); /* return value */ |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | #ifdef XFS_BMAP_TRACE |
| 274 | /* |
| 275 | * Add a bmap trace buffer entry. Base routine for the others. |
| 276 | */ |
| 277 | STATIC void |
| 278 | xfs_bmap_trace_addentry( |
| 279 | int opcode, /* operation */ |
| 280 | char *fname, /* function name */ |
| 281 | char *desc, /* operation description */ |
| 282 | xfs_inode_t *ip, /* incore inode pointer */ |
| 283 | xfs_extnum_t idx, /* index of entry(ies) */ |
| 284 | xfs_extnum_t cnt, /* count of entries, 1 or 2 */ |
| 285 | xfs_bmbt_rec_t *r1, /* first record */ |
| 286 | xfs_bmbt_rec_t *r2, /* second record or null */ |
| 287 | int whichfork); /* data or attr fork */ |
| 288 | |
| 289 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 290 | * Add bmap trace entry prior to a call to xfs_iext_remove. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | */ |
| 292 | STATIC void |
| 293 | xfs_bmap_trace_delete( |
| 294 | char *fname, /* function name */ |
| 295 | char *desc, /* operation description */ |
| 296 | xfs_inode_t *ip, /* incore inode pointer */ |
| 297 | xfs_extnum_t idx, /* index of entry(entries) deleted */ |
| 298 | xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */ |
| 299 | int whichfork); /* data or attr fork */ |
| 300 | |
| 301 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 302 | * 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] | 303 | * reading in the extents list from the disk (in the btree). |
| 304 | */ |
| 305 | STATIC void |
| 306 | xfs_bmap_trace_insert( |
| 307 | char *fname, /* function name */ |
| 308 | char *desc, /* operation description */ |
| 309 | xfs_inode_t *ip, /* incore inode pointer */ |
| 310 | xfs_extnum_t idx, /* index of entry(entries) inserted */ |
| 311 | xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */ |
| 312 | xfs_bmbt_irec_t *r1, /* inserted record 1 */ |
| 313 | xfs_bmbt_irec_t *r2, /* inserted record 2 or null */ |
| 314 | int whichfork); /* data or attr fork */ |
| 315 | |
| 316 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 317 | * Add bmap trace entry after updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | */ |
| 319 | STATIC void |
| 320 | xfs_bmap_trace_post_update( |
| 321 | char *fname, /* function name */ |
| 322 | char *desc, /* operation description */ |
| 323 | xfs_inode_t *ip, /* incore inode pointer */ |
| 324 | xfs_extnum_t idx, /* index of entry updated */ |
| 325 | int whichfork); /* data or attr fork */ |
| 326 | |
| 327 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 328 | * Add bmap trace entry prior to updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ |
| 330 | STATIC void |
| 331 | xfs_bmap_trace_pre_update( |
| 332 | char *fname, /* function name */ |
| 333 | char *desc, /* operation description */ |
| 334 | xfs_inode_t *ip, /* incore inode pointer */ |
| 335 | xfs_extnum_t idx, /* index of entry to be updated */ |
| 336 | int whichfork); /* data or attr fork */ |
| 337 | |
| 338 | #else |
| 339 | #define xfs_bmap_trace_delete(f,d,ip,i,c,w) |
| 340 | #define xfs_bmap_trace_insert(f,d,ip,i,c,r1,r2,w) |
| 341 | #define xfs_bmap_trace_post_update(f,d,ip,i,w) |
| 342 | #define xfs_bmap_trace_pre_update(f,d,ip,i,w) |
| 343 | #endif /* XFS_BMAP_TRACE */ |
| 344 | |
| 345 | /* |
| 346 | * Compute the worst-case number of indirect blocks that will be used |
| 347 | * for ip's delayed extent of length "len". |
| 348 | */ |
| 349 | STATIC xfs_filblks_t |
| 350 | xfs_bmap_worst_indlen( |
| 351 | xfs_inode_t *ip, /* incore inode pointer */ |
| 352 | xfs_filblks_t len); /* delayed extent length */ |
| 353 | |
| 354 | #ifdef DEBUG |
| 355 | /* |
| 356 | * Perform various validation checks on the values being returned |
| 357 | * from xfs_bmapi(). |
| 358 | */ |
| 359 | STATIC void |
| 360 | xfs_bmap_validate_ret( |
| 361 | xfs_fileoff_t bno, |
| 362 | xfs_filblks_t len, |
| 363 | int flags, |
| 364 | xfs_bmbt_irec_t *mval, |
| 365 | int nmap, |
| 366 | int ret_nmap); |
| 367 | #else |
| 368 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 369 | #endif /* DEBUG */ |
| 370 | |
| 371 | #if defined(XFS_RW_TRACE) |
| 372 | STATIC void |
| 373 | xfs_bunmap_trace( |
| 374 | xfs_inode_t *ip, |
| 375 | xfs_fileoff_t bno, |
| 376 | xfs_filblks_t len, |
| 377 | int flags, |
| 378 | inst_t *ra); |
| 379 | #else |
| 380 | #define xfs_bunmap_trace(ip, bno, len, flags, ra) |
| 381 | #endif /* XFS_RW_TRACE */ |
| 382 | |
| 383 | STATIC int |
| 384 | xfs_bmap_count_tree( |
| 385 | xfs_mount_t *mp, |
| 386 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 387 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | xfs_fsblock_t blockno, |
| 389 | int levelin, |
| 390 | int *count); |
| 391 | |
| 392 | STATIC int |
| 393 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 394 | xfs_ifork_t *ifp, |
| 395 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | int numrecs, |
| 397 | int *count); |
| 398 | |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 399 | STATIC int |
| 400 | xfs_bmap_disk_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 401 | xfs_extnum_t idx, |
| 402 | xfs_bmbt_block_t *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 403 | int numrecs, |
| 404 | int *count); |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | /* |
| 407 | * Bmap internal routines. |
| 408 | */ |
| 409 | |
| 410 | /* |
| 411 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 412 | */ |
| 413 | STATIC int /* error */ |
| 414 | xfs_bmap_add_attrfork_btree( |
| 415 | xfs_trans_t *tp, /* transaction pointer */ |
| 416 | xfs_inode_t *ip, /* incore inode pointer */ |
| 417 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 418 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 419 | int *flags) /* inode logging flags */ |
| 420 | { |
| 421 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 422 | int error; /* error return value */ |
| 423 | xfs_mount_t *mp; /* file system mount struct */ |
| 424 | int stat; /* newroot status */ |
| 425 | |
| 426 | mp = ip->i_mount; |
| 427 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 428 | *flags |= XFS_ILOG_DBROOT; |
| 429 | else { |
| 430 | cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip, |
| 431 | XFS_DATA_FORK); |
| 432 | cur->bc_private.b.flist = flist; |
| 433 | cur->bc_private.b.firstblock = *firstblock; |
| 434 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 435 | goto error0; |
| 436 | ASSERT(stat == 1); /* must be at least one entry */ |
| 437 | if ((error = xfs_bmbt_newroot(cur, flags, &stat))) |
| 438 | goto error0; |
| 439 | if (stat == 0) { |
| 440 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 441 | return XFS_ERROR(ENOSPC); |
| 442 | } |
| 443 | *firstblock = cur->bc_private.b.firstblock; |
| 444 | cur->bc_private.b.allocated = 0; |
| 445 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 446 | } |
| 447 | return 0; |
| 448 | error0: |
| 449 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 450 | return error; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 455 | */ |
| 456 | STATIC int /* error */ |
| 457 | xfs_bmap_add_attrfork_extents( |
| 458 | xfs_trans_t *tp, /* transaction pointer */ |
| 459 | xfs_inode_t *ip, /* incore inode pointer */ |
| 460 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 461 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 462 | int *flags) /* inode logging flags */ |
| 463 | { |
| 464 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 465 | int error; /* error return value */ |
| 466 | |
| 467 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 468 | return 0; |
| 469 | cur = NULL; |
| 470 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 471 | flags, XFS_DATA_FORK); |
| 472 | if (cur) { |
| 473 | cur->bc_private.b.allocated = 0; |
| 474 | xfs_btree_del_cursor(cur, |
| 475 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 476 | } |
| 477 | return error; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 482 | */ |
| 483 | STATIC int /* error */ |
| 484 | xfs_bmap_add_attrfork_local( |
| 485 | xfs_trans_t *tp, /* transaction pointer */ |
| 486 | xfs_inode_t *ip, /* incore inode pointer */ |
| 487 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 488 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 489 | int *flags) /* inode logging flags */ |
| 490 | { |
| 491 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 492 | int error; /* error return value */ |
| 493 | xfs_mount_t *mp; /* mount structure pointer */ |
| 494 | |
| 495 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 496 | return 0; |
| 497 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 498 | mp = ip->i_mount; |
| 499 | memset(&dargs, 0, sizeof(dargs)); |
| 500 | dargs.dp = ip; |
| 501 | dargs.firstblock = firstblock; |
| 502 | dargs.flist = flist; |
| 503 | dargs.total = mp->m_dirblkfsbs; |
| 504 | dargs.whichfork = XFS_DATA_FORK; |
| 505 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 506 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } else |
| 508 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 509 | XFS_DATA_FORK); |
| 510 | return error; |
| 511 | } |
| 512 | |
| 513 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 514 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | * after allocating space (or doing a delayed allocation). |
| 516 | */ |
| 517 | STATIC int /* error */ |
| 518 | xfs_bmap_add_extent( |
| 519 | xfs_inode_t *ip, /* incore inode pointer */ |
| 520 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 521 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 522 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 524 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 525 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 526 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | int whichfork, /* data or attr fork */ |
| 528 | int rsvd) /* OK to use reserved data blocks */ |
| 529 | { |
| 530 | xfs_btree_cur_t *cur; /* btree cursor or null */ |
| 531 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 532 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 533 | int error; /* error return value */ |
| 534 | #ifdef XFS_BMAP_TRACE |
| 535 | static char fname[] = "xfs_bmap_add_extent"; |
| 536 | #endif |
| 537 | xfs_ifork_t *ifp; /* inode fork ptr */ |
| 538 | int logflags; /* returned value */ |
| 539 | xfs_extnum_t nextents; /* number of extents in file now */ |
| 540 | |
| 541 | XFS_STATS_INC(xs_add_exlist); |
| 542 | cur = *curp; |
| 543 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 544 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 545 | ASSERT(idx <= nextents); |
| 546 | da_old = da_new = 0; |
| 547 | error = 0; |
| 548 | /* |
| 549 | * This is the first extent added to a new/empty file. |
| 550 | * Special case this one, so other routines get to assume there are |
| 551 | * already extents in the list. |
| 552 | */ |
| 553 | if (nextents == 0) { |
| 554 | xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new, |
| 555 | NULL, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 556 | xfs_iext_insert(ifp, 0, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | ASSERT(cur == NULL); |
| 558 | ifp->if_lastex = 0; |
| 559 | if (!ISNULLSTARTBLOCK(new->br_startblock)) { |
| 560 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 561 | logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork); |
| 562 | } else |
| 563 | logflags = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 564 | /* DELTA: single new extent */ |
| 565 | if (delta) { |
| 566 | if (delta->xed_startoff > new->br_startoff) |
| 567 | delta->xed_startoff = new->br_startoff; |
| 568 | if (delta->xed_blockcount < |
| 569 | new->br_startoff + new->br_blockcount) |
| 570 | delta->xed_blockcount = new->br_startoff + |
| 571 | new->br_blockcount; |
| 572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
| 574 | /* |
| 575 | * Any kind of new delayed allocation goes here. |
| 576 | */ |
| 577 | else if (ISNULLSTARTBLOCK(new->br_startblock)) { |
| 578 | if (cur) |
| 579 | ASSERT((cur->bc_private.b.flags & |
| 580 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 581 | if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 582 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | goto done; |
| 584 | } |
| 585 | /* |
| 586 | * Real allocation off the end of the file. |
| 587 | */ |
| 588 | else if (idx == nextents) { |
| 589 | if (cur) |
| 590 | ASSERT((cur->bc_private.b.flags & |
| 591 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 592 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 593 | &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | goto done; |
| 595 | } else { |
| 596 | xfs_bmbt_irec_t prev; /* old extent at offset idx */ |
| 597 | |
| 598 | /* |
| 599 | * Get the record referred to by idx. |
| 600 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 601 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | /* |
| 603 | * If it's a real allocation record, and the new allocation ends |
| 604 | * after the start of the referred to record, then we're filling |
| 605 | * in a delayed or unwritten allocation with a real one, or |
| 606 | * converting real back to unwritten. |
| 607 | */ |
| 608 | if (!ISNULLSTARTBLOCK(new->br_startblock) && |
| 609 | new->br_startoff + new->br_blockcount > prev.br_startoff) { |
| 610 | if (prev.br_state != XFS_EXT_UNWRITTEN && |
| 611 | ISNULLSTARTBLOCK(prev.br_startblock)) { |
| 612 | da_old = STARTBLOCKVAL(prev.br_startblock); |
| 613 | if (cur) |
| 614 | ASSERT(cur->bc_private.b.flags & |
| 615 | XFS_BTCUR_BPRV_WASDEL); |
| 616 | if ((error = xfs_bmap_add_extent_delay_real(ip, |
| 617 | idx, &cur, new, &da_new, first, flist, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 618 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | goto done; |
| 620 | } else if (new->br_state == XFS_EXT_NORM) { |
| 621 | ASSERT(new->br_state == XFS_EXT_NORM); |
| 622 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 623 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | goto done; |
| 625 | } else { |
| 626 | ASSERT(new->br_state == XFS_EXT_UNWRITTEN); |
| 627 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 628 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | goto done; |
| 630 | } |
| 631 | ASSERT(*curp == cur || *curp == NULL); |
| 632 | } |
| 633 | /* |
| 634 | * Otherwise we're filling in a hole with an allocation. |
| 635 | */ |
| 636 | else { |
| 637 | if (cur) |
| 638 | ASSERT((cur->bc_private.b.flags & |
| 639 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 640 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 641 | new, &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | goto done; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | ASSERT(*curp == cur || *curp == NULL); |
| 647 | /* |
| 648 | * Convert to a btree if necessary. |
| 649 | */ |
| 650 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 651 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 652 | int tmp_logflags; /* partial log flag return val */ |
| 653 | |
| 654 | ASSERT(cur == NULL); |
| 655 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first, |
| 656 | flist, &cur, da_old > 0, &tmp_logflags, whichfork); |
| 657 | logflags |= tmp_logflags; |
| 658 | if (error) |
| 659 | goto done; |
| 660 | } |
| 661 | /* |
| 662 | * Adjust for changes in reserved delayed indirect blocks. |
| 663 | * Nothing to do for disk quotas here. |
| 664 | */ |
| 665 | if (da_old || da_new) { |
| 666 | xfs_filblks_t nblks; |
| 667 | |
| 668 | nblks = da_new; |
| 669 | if (cur) |
| 670 | nblks += cur->bc_private.b.allocated; |
| 671 | ASSERT(nblks <= da_old); |
| 672 | if (nblks < da_old) |
| 673 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 674 | (int64_t)(da_old - nblks), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | /* |
| 677 | * Clear out the allocated field, done with it now in any case. |
| 678 | */ |
| 679 | if (cur) { |
| 680 | cur->bc_private.b.allocated = 0; |
| 681 | *curp = cur; |
| 682 | } |
| 683 | done: |
| 684 | #ifdef DEBUG |
| 685 | if (!error) |
| 686 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
| 687 | #endif |
| 688 | *logflagsp = logflags; |
| 689 | return error; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 694 | * allocation to a real allocation. |
| 695 | */ |
| 696 | STATIC int /* error */ |
| 697 | xfs_bmap_add_extent_delay_real( |
| 698 | xfs_inode_t *ip, /* incore inode pointer */ |
| 699 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 700 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 701 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 703 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 704 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 705 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 706 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | int rsvd) /* OK to use reserved data block allocation */ |
| 708 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 710 | int diff; /* temp value */ |
| 711 | xfs_bmbt_rec_t *ep; /* extent entry for idx */ |
| 712 | int error; /* error return value */ |
| 713 | #ifdef XFS_BMAP_TRACE |
| 714 | static char fname[] = "xfs_bmap_add_extent_delay_real"; |
| 715 | #endif |
| 716 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 717 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 719 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 720 | /* left is 0, right is 1, prev is 2 */ |
| 721 | int rval=0; /* return value (logging flags) */ |
| 722 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 723 | xfs_filblks_t temp=0; /* value for dnew calculations */ |
| 724 | xfs_filblks_t temp2=0;/* value for dnew calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | int tmp_rval; /* partial logging flags */ |
| 726 | enum { /* bit number definitions for state */ |
| 727 | LEFT_CONTIG, RIGHT_CONTIG, |
| 728 | LEFT_FILLING, RIGHT_FILLING, |
| 729 | LEFT_DELAY, RIGHT_DELAY, |
| 730 | LEFT_VALID, RIGHT_VALID |
| 731 | }; |
| 732 | |
| 733 | #define LEFT r[0] |
| 734 | #define RIGHT r[1] |
| 735 | #define PREV r[2] |
| 736 | #define MASK(b) (1 << (b)) |
| 737 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 738 | #define MASK3(a,b,c) (MASK2(a,b) | MASK(c)) |
| 739 | #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d)) |
| 740 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 741 | #define STATE_TEST(b) (state & MASK(b)) |
| 742 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 743 | ((state &= ~MASK(b)), 0)) |
| 744 | #define SWITCH_STATE \ |
| 745 | (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG)) |
| 746 | |
| 747 | /* |
| 748 | * Set up a bunch of variables to make the tests simpler. |
| 749 | */ |
| 750 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 751 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 752 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | xfs_bmbt_get_all(ep, &PREV); |
| 754 | new_endoff = new->br_startoff + new->br_blockcount; |
| 755 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 756 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
| 757 | /* |
| 758 | * Set flags determining what part of the previous delayed allocation |
| 759 | * extent is being replaced by a real allocation. |
| 760 | */ |
| 761 | STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff); |
| 762 | STATE_SET(RIGHT_FILLING, |
| 763 | PREV.br_startoff + PREV.br_blockcount == new_endoff); |
| 764 | /* |
| 765 | * Check and set flags if this segment has a left neighbor. |
| 766 | * Don't set contiguous if the combined extent would be too large. |
| 767 | */ |
| 768 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 769 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock)); |
| 771 | } |
| 772 | STATE_SET(LEFT_CONTIG, |
| 773 | STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) && |
| 774 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 775 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 776 | LEFT.br_state == new->br_state && |
| 777 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 778 | /* |
| 779 | * Check and set flags if this segment has a right neighbor. |
| 780 | * Don't set contiguous if the combined extent would be too large. |
| 781 | * Also check for all-three-contiguous being too large. |
| 782 | */ |
| 783 | if (STATE_SET_TEST(RIGHT_VALID, |
| 784 | idx < |
| 785 | ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 786 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock)); |
| 788 | } |
| 789 | STATE_SET(RIGHT_CONTIG, |
| 790 | STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) && |
| 791 | new_endoff == RIGHT.br_startoff && |
| 792 | new->br_startblock + new->br_blockcount == |
| 793 | RIGHT.br_startblock && |
| 794 | new->br_state == RIGHT.br_state && |
| 795 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 796 | ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) != |
| 797 | MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) || |
| 798 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 799 | <= MAXEXTLEN)); |
| 800 | error = 0; |
| 801 | /* |
| 802 | * Switch out based on the FILLING and CONTIG state bits. |
| 803 | */ |
| 804 | switch (SWITCH_STATE) { |
| 805 | |
| 806 | case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 807 | /* |
| 808 | * Filling in all of a previously delayed allocation extent. |
| 809 | * The left and right neighbors are both contiguous with new. |
| 810 | */ |
| 811 | xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1, |
| 812 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 813 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | LEFT.br_blockcount + PREV.br_blockcount + |
| 815 | RIGHT.br_blockcount); |
| 816 | xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1, |
| 817 | XFS_DATA_FORK); |
| 818 | xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2, |
| 819 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 820 | xfs_iext_remove(ifp, idx, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | ip->i_df.if_lastex = idx - 1; |
| 822 | ip->i_d.di_nextents--; |
| 823 | if (cur == NULL) |
| 824 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 825 | else { |
| 826 | rval = XFS_ILOG_CORE; |
| 827 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 828 | RIGHT.br_startblock, |
| 829 | RIGHT.br_blockcount, &i))) |
| 830 | goto done; |
| 831 | ASSERT(i == 1); |
| 832 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 833 | goto done; |
| 834 | ASSERT(i == 1); |
| 835 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 836 | goto done; |
| 837 | ASSERT(i == 1); |
| 838 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 839 | LEFT.br_startblock, |
| 840 | LEFT.br_blockcount + |
| 841 | PREV.br_blockcount + |
| 842 | RIGHT.br_blockcount, LEFT.br_state))) |
| 843 | goto done; |
| 844 | } |
| 845 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 846 | /* DELTA: Three in-core extents are replaced by one. */ |
| 847 | temp = LEFT.br_startoff; |
| 848 | temp2 = LEFT.br_blockcount + |
| 849 | PREV.br_blockcount + |
| 850 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | break; |
| 852 | |
| 853 | case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG): |
| 854 | /* |
| 855 | * Filling in all of a previously delayed allocation extent. |
| 856 | * The left neighbor is contiguous, the right is not. |
| 857 | */ |
| 858 | xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1, |
| 859 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 860 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | LEFT.br_blockcount + PREV.br_blockcount); |
| 862 | xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1, |
| 863 | XFS_DATA_FORK); |
| 864 | ip->i_df.if_lastex = idx - 1; |
| 865 | xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1, |
| 866 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 867 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (cur == NULL) |
| 869 | rval = XFS_ILOG_DEXT; |
| 870 | else { |
| 871 | rval = 0; |
| 872 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 873 | LEFT.br_startblock, LEFT.br_blockcount, |
| 874 | &i))) |
| 875 | goto done; |
| 876 | ASSERT(i == 1); |
| 877 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 878 | LEFT.br_startblock, |
| 879 | LEFT.br_blockcount + |
| 880 | PREV.br_blockcount, LEFT.br_state))) |
| 881 | goto done; |
| 882 | } |
| 883 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 884 | /* DELTA: Two in-core extents are replaced by one. */ |
| 885 | temp = LEFT.br_startoff; |
| 886 | temp2 = LEFT.br_blockcount + |
| 887 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | break; |
| 889 | |
| 890 | case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG): |
| 891 | /* |
| 892 | * Filling in all of a previously delayed allocation extent. |
| 893 | * The right neighbor is contiguous, the left is not. |
| 894 | */ |
| 895 | xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx, |
| 896 | XFS_DATA_FORK); |
| 897 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 898 | xfs_bmbt_set_blockcount(ep, |
| 899 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 900 | xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx, |
| 901 | XFS_DATA_FORK); |
| 902 | ip->i_df.if_lastex = idx; |
| 903 | xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1, |
| 904 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 905 | xfs_iext_remove(ifp, idx + 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | if (cur == NULL) |
| 907 | rval = XFS_ILOG_DEXT; |
| 908 | else { |
| 909 | rval = 0; |
| 910 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 911 | RIGHT.br_startblock, |
| 912 | RIGHT.br_blockcount, &i))) |
| 913 | goto done; |
| 914 | ASSERT(i == 1); |
| 915 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 916 | new->br_startblock, |
| 917 | PREV.br_blockcount + |
| 918 | RIGHT.br_blockcount, PREV.br_state))) |
| 919 | goto done; |
| 920 | } |
| 921 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 922 | /* DELTA: Two in-core extents are replaced by one. */ |
| 923 | temp = PREV.br_startoff; |
| 924 | temp2 = PREV.br_blockcount + |
| 925 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | break; |
| 927 | |
| 928 | case MASK2(LEFT_FILLING, RIGHT_FILLING): |
| 929 | /* |
| 930 | * Filling in all of a previously delayed allocation extent. |
| 931 | * Neither the left nor right neighbors are contiguous with |
| 932 | * the new one. |
| 933 | */ |
| 934 | xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx, |
| 935 | XFS_DATA_FORK); |
| 936 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 937 | xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx, |
| 938 | XFS_DATA_FORK); |
| 939 | ip->i_df.if_lastex = idx; |
| 940 | ip->i_d.di_nextents++; |
| 941 | if (cur == NULL) |
| 942 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 943 | else { |
| 944 | rval = XFS_ILOG_CORE; |
| 945 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 946 | new->br_startblock, new->br_blockcount, |
| 947 | &i))) |
| 948 | goto done; |
| 949 | ASSERT(i == 0); |
| 950 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 951 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 952 | goto done; |
| 953 | ASSERT(i == 1); |
| 954 | } |
| 955 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 956 | /* DELTA: The in-core extent described by new changed type. */ |
| 957 | temp = new->br_startoff; |
| 958 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | break; |
| 960 | |
| 961 | case MASK2(LEFT_FILLING, LEFT_CONTIG): |
| 962 | /* |
| 963 | * Filling in the first part of a previous delayed allocation. |
| 964 | * The left neighbor is contiguous. |
| 965 | */ |
| 966 | xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1, |
| 967 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 968 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | LEFT.br_blockcount + new->br_blockcount); |
| 970 | xfs_bmbt_set_startoff(ep, |
| 971 | PREV.br_startoff + new->br_blockcount); |
| 972 | xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1, |
| 973 | XFS_DATA_FORK); |
| 974 | temp = PREV.br_blockcount - new->br_blockcount; |
| 975 | xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx, |
| 976 | XFS_DATA_FORK); |
| 977 | xfs_bmbt_set_blockcount(ep, temp); |
| 978 | ip->i_df.if_lastex = idx - 1; |
| 979 | if (cur == NULL) |
| 980 | rval = XFS_ILOG_DEXT; |
| 981 | else { |
| 982 | rval = 0; |
| 983 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 984 | LEFT.br_startblock, LEFT.br_blockcount, |
| 985 | &i))) |
| 986 | goto done; |
| 987 | ASSERT(i == 1); |
| 988 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 989 | LEFT.br_startblock, |
| 990 | LEFT.br_blockcount + |
| 991 | new->br_blockcount, |
| 992 | LEFT.br_state))) |
| 993 | goto done; |
| 994 | } |
| 995 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 996 | STARTBLOCKVAL(PREV.br_startblock)); |
| 997 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 998 | xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx, |
| 999 | XFS_DATA_FORK); |
| 1000 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1001 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1002 | temp = LEFT.br_startoff; |
| 1003 | temp2 = LEFT.br_blockcount + |
| 1004 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | break; |
| 1006 | |
| 1007 | case MASK(LEFT_FILLING): |
| 1008 | /* |
| 1009 | * Filling in the first part of a previous delayed allocation. |
| 1010 | * The left neighbor is not contiguous. |
| 1011 | */ |
| 1012 | xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK); |
| 1013 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1014 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1015 | xfs_bmbt_set_blockcount(ep, temp); |
| 1016 | xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL, |
| 1017 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1018 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | ip->i_df.if_lastex = idx; |
| 1020 | ip->i_d.di_nextents++; |
| 1021 | if (cur == NULL) |
| 1022 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1023 | else { |
| 1024 | rval = XFS_ILOG_CORE; |
| 1025 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1026 | new->br_startblock, new->br_blockcount, |
| 1027 | &i))) |
| 1028 | goto done; |
| 1029 | ASSERT(i == 0); |
| 1030 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1031 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1032 | goto done; |
| 1033 | ASSERT(i == 1); |
| 1034 | } |
| 1035 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1036 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1037 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1038 | first, flist, &cur, 1, &tmp_rval, |
| 1039 | XFS_DATA_FORK); |
| 1040 | rval |= tmp_rval; |
| 1041 | if (error) |
| 1042 | goto done; |
| 1043 | } |
| 1044 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 1045 | STARTBLOCKVAL(PREV.br_startblock) - |
| 1046 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1047 | ep = xfs_iext_get_ext(ifp, idx + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 1049 | xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1, |
| 1050 | XFS_DATA_FORK); |
| 1051 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1052 | /* DELTA: One in-core extent is split in two. */ |
| 1053 | temp = PREV.br_startoff; |
| 1054 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | break; |
| 1056 | |
| 1057 | case MASK2(RIGHT_FILLING, RIGHT_CONTIG): |
| 1058 | /* |
| 1059 | * Filling in the last part of a previous delayed allocation. |
| 1060 | * The right neighbor is contiguous with the new allocation. |
| 1061 | */ |
| 1062 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1063 | xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx, |
| 1064 | XFS_DATA_FORK); |
| 1065 | xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1, |
| 1066 | XFS_DATA_FORK); |
| 1067 | xfs_bmbt_set_blockcount(ep, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1068 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1069 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | new->br_blockcount + RIGHT.br_blockcount, |
| 1071 | RIGHT.br_state); |
| 1072 | xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1, |
| 1073 | XFS_DATA_FORK); |
| 1074 | ip->i_df.if_lastex = idx + 1; |
| 1075 | if (cur == NULL) |
| 1076 | rval = XFS_ILOG_DEXT; |
| 1077 | else { |
| 1078 | rval = 0; |
| 1079 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1080 | RIGHT.br_startblock, |
| 1081 | RIGHT.br_blockcount, &i))) |
| 1082 | goto done; |
| 1083 | ASSERT(i == 1); |
| 1084 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1085 | new->br_startblock, |
| 1086 | new->br_blockcount + |
| 1087 | RIGHT.br_blockcount, |
| 1088 | RIGHT.br_state))) |
| 1089 | goto done; |
| 1090 | } |
| 1091 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 1092 | STARTBLOCKVAL(PREV.br_startblock)); |
| 1093 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 1094 | xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx, |
| 1095 | XFS_DATA_FORK); |
| 1096 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1097 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1098 | temp = PREV.br_startoff; |
| 1099 | temp2 = PREV.br_blockcount + |
| 1100 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | break; |
| 1102 | |
| 1103 | case MASK(RIGHT_FILLING): |
| 1104 | /* |
| 1105 | * Filling in the last part of a previous delayed allocation. |
| 1106 | * The right neighbor is not contiguous. |
| 1107 | */ |
| 1108 | temp = PREV.br_blockcount - new->br_blockcount; |
| 1109 | xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK); |
| 1110 | xfs_bmbt_set_blockcount(ep, temp); |
| 1111 | xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1, |
| 1112 | new, NULL, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1113 | xfs_iext_insert(ifp, idx + 1, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | ip->i_df.if_lastex = idx + 1; |
| 1115 | ip->i_d.di_nextents++; |
| 1116 | if (cur == NULL) |
| 1117 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1118 | else { |
| 1119 | rval = XFS_ILOG_CORE; |
| 1120 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1121 | new->br_startblock, new->br_blockcount, |
| 1122 | &i))) |
| 1123 | goto done; |
| 1124 | ASSERT(i == 0); |
| 1125 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1126 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1127 | goto done; |
| 1128 | ASSERT(i == 1); |
| 1129 | } |
| 1130 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1131 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1132 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1133 | first, flist, &cur, 1, &tmp_rval, |
| 1134 | XFS_DATA_FORK); |
| 1135 | rval |= tmp_rval; |
| 1136 | if (error) |
| 1137 | goto done; |
| 1138 | } |
| 1139 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 1140 | STARTBLOCKVAL(PREV.br_startblock) - |
| 1141 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1142 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 1144 | xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK); |
| 1145 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1146 | /* DELTA: One in-core extent is split in two. */ |
| 1147 | temp = PREV.br_startoff; |
| 1148 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | break; |
| 1150 | |
| 1151 | case 0: |
| 1152 | /* |
| 1153 | * Filling in the middle part of a previous delayed allocation. |
| 1154 | * Contiguity is impossible here. |
| 1155 | * This case is avoided almost all the time. |
| 1156 | */ |
| 1157 | temp = new->br_startoff - PREV.br_startoff; |
| 1158 | xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK); |
| 1159 | xfs_bmbt_set_blockcount(ep, temp); |
| 1160 | r[0] = *new; |
Lachlan McIlroy | d213371 | 2006-11-21 18:55:16 +1100 | [diff] [blame] | 1161 | r[1].br_state = PREV.br_state; |
| 1162 | r[1].br_startblock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | r[1].br_startoff = new_endoff; |
| 1164 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1165 | r[1].br_blockcount = temp2; |
| 1166 | xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1], |
| 1167 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1168 | xfs_iext_insert(ifp, idx + 1, 2, &r[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | ip->i_df.if_lastex = idx + 1; |
| 1170 | ip->i_d.di_nextents++; |
| 1171 | if (cur == NULL) |
| 1172 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1173 | else { |
| 1174 | rval = XFS_ILOG_CORE; |
| 1175 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1176 | new->br_startblock, new->br_blockcount, |
| 1177 | &i))) |
| 1178 | goto done; |
| 1179 | ASSERT(i == 0); |
| 1180 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1181 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1182 | goto done; |
| 1183 | ASSERT(i == 1); |
| 1184 | } |
| 1185 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1186 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1187 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1188 | first, flist, &cur, 1, &tmp_rval, |
| 1189 | XFS_DATA_FORK); |
| 1190 | rval |= tmp_rval; |
| 1191 | if (error) |
| 1192 | goto done; |
| 1193 | } |
| 1194 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 1195 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
| 1196 | diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) - |
| 1197 | (cur ? cur->bc_private.b.allocated : 0)); |
| 1198 | if (diff > 0 && |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1199 | 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] | 1200 | /* |
| 1201 | * Ick gross gag me with a spoon. |
| 1202 | */ |
| 1203 | ASSERT(0); /* want to see if this ever happens! */ |
| 1204 | while (diff > 0) { |
| 1205 | if (temp) { |
| 1206 | temp--; |
| 1207 | diff--; |
| 1208 | if (!diff || |
| 1209 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1210 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | break; |
| 1212 | } |
| 1213 | if (temp2) { |
| 1214 | temp2--; |
| 1215 | diff--; |
| 1216 | if (!diff || |
| 1217 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1218 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1223 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 1225 | xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK); |
| 1226 | xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2, |
| 1227 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1228 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2), |
| 1229 | NULLSTARTBLOCK((int)temp2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | xfs_bmap_trace_post_update(fname, "0", ip, idx + 2, |
| 1231 | XFS_DATA_FORK); |
| 1232 | *dnew = temp + temp2; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1233 | /* DELTA: One in-core extent is split in three. */ |
| 1234 | temp = PREV.br_startoff; |
| 1235 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | break; |
| 1237 | |
| 1238 | case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1239 | case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1240 | case MASK2(LEFT_FILLING, RIGHT_CONTIG): |
| 1241 | case MASK2(RIGHT_FILLING, LEFT_CONTIG): |
| 1242 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 1243 | case MASK(LEFT_CONTIG): |
| 1244 | case MASK(RIGHT_CONTIG): |
| 1245 | /* |
| 1246 | * These cases are all impossible. |
| 1247 | */ |
| 1248 | ASSERT(0); |
| 1249 | } |
| 1250 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1251 | if (delta) { |
| 1252 | temp2 += temp; |
| 1253 | if (delta->xed_startoff > temp) |
| 1254 | delta->xed_startoff = temp; |
| 1255 | if (delta->xed_blockcount < temp2) |
| 1256 | delta->xed_blockcount = temp2; |
| 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | done: |
| 1259 | *logflagsp = rval; |
| 1260 | return error; |
| 1261 | #undef LEFT |
| 1262 | #undef RIGHT |
| 1263 | #undef PREV |
| 1264 | #undef MASK |
| 1265 | #undef MASK2 |
| 1266 | #undef MASK3 |
| 1267 | #undef MASK4 |
| 1268 | #undef STATE_SET |
| 1269 | #undef STATE_TEST |
| 1270 | #undef STATE_SET_TEST |
| 1271 | #undef SWITCH_STATE |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 1276 | * allocation to a real allocation or vice versa. |
| 1277 | */ |
| 1278 | STATIC int /* error */ |
| 1279 | xfs_bmap_add_extent_unwritten_real( |
| 1280 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1281 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 1282 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1283 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1284 | int *logflagsp, /* inode logging flags */ |
| 1285 | xfs_extdelta_t *delta) /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 1288 | xfs_bmbt_rec_t *ep; /* extent entry for idx */ |
| 1289 | int error; /* error return value */ |
| 1290 | #ifdef XFS_BMAP_TRACE |
| 1291 | static char fname[] = "xfs_bmap_add_extent_unwritten_real"; |
| 1292 | #endif |
| 1293 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1294 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1296 | xfs_exntst_t newext; /* new extent state */ |
| 1297 | xfs_exntst_t oldext; /* old extent state */ |
| 1298 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1299 | /* left is 0, right is 1, prev is 2 */ |
| 1300 | int rval=0; /* return value (logging flags) */ |
| 1301 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1302 | xfs_filblks_t temp=0; |
| 1303 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | enum { /* bit number definitions for state */ |
| 1305 | LEFT_CONTIG, RIGHT_CONTIG, |
| 1306 | LEFT_FILLING, RIGHT_FILLING, |
| 1307 | LEFT_DELAY, RIGHT_DELAY, |
| 1308 | LEFT_VALID, RIGHT_VALID |
| 1309 | }; |
| 1310 | |
| 1311 | #define LEFT r[0] |
| 1312 | #define RIGHT r[1] |
| 1313 | #define PREV r[2] |
| 1314 | #define MASK(b) (1 << (b)) |
| 1315 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 1316 | #define MASK3(a,b,c) (MASK2(a,b) | MASK(c)) |
| 1317 | #define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d)) |
| 1318 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 1319 | #define STATE_TEST(b) (state & MASK(b)) |
| 1320 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 1321 | ((state &= ~MASK(b)), 0)) |
| 1322 | #define SWITCH_STATE \ |
| 1323 | (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG)) |
| 1324 | |
| 1325 | /* |
| 1326 | * Set up a bunch of variables to make the tests simpler. |
| 1327 | */ |
| 1328 | error = 0; |
| 1329 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1330 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1331 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | xfs_bmbt_get_all(ep, &PREV); |
| 1333 | newext = new->br_state; |
| 1334 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 1335 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 1336 | ASSERT(PREV.br_state == oldext); |
| 1337 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1338 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1339 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
| 1340 | /* |
| 1341 | * Set flags determining what part of the previous oldext allocation |
| 1342 | * extent is being replaced by a newext allocation. |
| 1343 | */ |
| 1344 | STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff); |
| 1345 | STATE_SET(RIGHT_FILLING, |
| 1346 | PREV.br_startoff + PREV.br_blockcount == new_endoff); |
| 1347 | /* |
| 1348 | * Check and set flags if this segment has a left neighbor. |
| 1349 | * Don't set contiguous if the combined extent would be too large. |
| 1350 | */ |
| 1351 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1352 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock)); |
| 1354 | } |
| 1355 | STATE_SET(LEFT_CONTIG, |
| 1356 | STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) && |
| 1357 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1358 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1359 | LEFT.br_state == newext && |
| 1360 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 1361 | /* |
| 1362 | * Check and set flags if this segment has a right neighbor. |
| 1363 | * Don't set contiguous if the combined extent would be too large. |
| 1364 | * Also check for all-three-contiguous being too large. |
| 1365 | */ |
| 1366 | if (STATE_SET_TEST(RIGHT_VALID, |
| 1367 | idx < |
| 1368 | ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1369 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock)); |
| 1371 | } |
| 1372 | STATE_SET(RIGHT_CONTIG, |
| 1373 | STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) && |
| 1374 | new_endoff == RIGHT.br_startoff && |
| 1375 | new->br_startblock + new->br_blockcount == |
| 1376 | RIGHT.br_startblock && |
| 1377 | newext == RIGHT.br_state && |
| 1378 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1379 | ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) != |
| 1380 | MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) || |
| 1381 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1382 | <= MAXEXTLEN)); |
| 1383 | /* |
| 1384 | * Switch out based on the FILLING and CONTIG state bits. |
| 1385 | */ |
| 1386 | switch (SWITCH_STATE) { |
| 1387 | |
| 1388 | case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1389 | /* |
| 1390 | * Setting all of a previous oldext extent to newext. |
| 1391 | * The left and right neighbors are both contiguous with new. |
| 1392 | */ |
| 1393 | xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1, |
| 1394 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1395 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1397 | RIGHT.br_blockcount); |
| 1398 | xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1, |
| 1399 | XFS_DATA_FORK); |
| 1400 | xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2, |
| 1401 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1402 | xfs_iext_remove(ifp, idx, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | ip->i_df.if_lastex = idx - 1; |
| 1404 | ip->i_d.di_nextents -= 2; |
| 1405 | if (cur == NULL) |
| 1406 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1407 | else { |
| 1408 | rval = XFS_ILOG_CORE; |
| 1409 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1410 | RIGHT.br_startblock, |
| 1411 | RIGHT.br_blockcount, &i))) |
| 1412 | goto done; |
| 1413 | ASSERT(i == 1); |
| 1414 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 1415 | goto done; |
| 1416 | ASSERT(i == 1); |
| 1417 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 1418 | goto done; |
| 1419 | ASSERT(i == 1); |
| 1420 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 1421 | goto done; |
| 1422 | ASSERT(i == 1); |
| 1423 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 1424 | goto done; |
| 1425 | ASSERT(i == 1); |
| 1426 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1427 | LEFT.br_startblock, |
| 1428 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1429 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1430 | goto done; |
| 1431 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1432 | /* DELTA: Three in-core extents are replaced by one. */ |
| 1433 | temp = LEFT.br_startoff; |
| 1434 | temp2 = LEFT.br_blockcount + |
| 1435 | PREV.br_blockcount + |
| 1436 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | break; |
| 1438 | |
| 1439 | case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG): |
| 1440 | /* |
| 1441 | * Setting all of a previous oldext extent to newext. |
| 1442 | * The left neighbor is contiguous, the right is not. |
| 1443 | */ |
| 1444 | xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1, |
| 1445 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1446 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | LEFT.br_blockcount + PREV.br_blockcount); |
| 1448 | xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1, |
| 1449 | XFS_DATA_FORK); |
| 1450 | ip->i_df.if_lastex = idx - 1; |
| 1451 | xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1, |
| 1452 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1453 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | ip->i_d.di_nextents--; |
| 1455 | if (cur == NULL) |
| 1456 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1457 | else { |
| 1458 | rval = XFS_ILOG_CORE; |
| 1459 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1460 | PREV.br_startblock, PREV.br_blockcount, |
| 1461 | &i))) |
| 1462 | goto done; |
| 1463 | ASSERT(i == 1); |
| 1464 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 1465 | goto done; |
| 1466 | ASSERT(i == 1); |
| 1467 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 1468 | goto done; |
| 1469 | ASSERT(i == 1); |
| 1470 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1471 | LEFT.br_startblock, |
| 1472 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1473 | LEFT.br_state))) |
| 1474 | goto done; |
| 1475 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1476 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1477 | temp = LEFT.br_startoff; |
| 1478 | temp2 = LEFT.br_blockcount + |
| 1479 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | break; |
| 1481 | |
| 1482 | case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG): |
| 1483 | /* |
| 1484 | * Setting all of a previous oldext extent to newext. |
| 1485 | * The right neighbor is contiguous, the left is not. |
| 1486 | */ |
| 1487 | xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx, |
| 1488 | XFS_DATA_FORK); |
| 1489 | xfs_bmbt_set_blockcount(ep, |
| 1490 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1491 | xfs_bmbt_set_state(ep, newext); |
| 1492 | xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx, |
| 1493 | XFS_DATA_FORK); |
| 1494 | ip->i_df.if_lastex = idx; |
| 1495 | xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1, |
| 1496 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1497 | xfs_iext_remove(ifp, idx + 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | ip->i_d.di_nextents--; |
| 1499 | if (cur == NULL) |
| 1500 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1501 | else { |
| 1502 | rval = XFS_ILOG_CORE; |
| 1503 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1504 | RIGHT.br_startblock, |
| 1505 | RIGHT.br_blockcount, &i))) |
| 1506 | goto done; |
| 1507 | ASSERT(i == 1); |
| 1508 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 1509 | goto done; |
| 1510 | ASSERT(i == 1); |
| 1511 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 1512 | goto done; |
| 1513 | ASSERT(i == 1); |
| 1514 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1515 | new->br_startblock, |
| 1516 | new->br_blockcount + RIGHT.br_blockcount, |
| 1517 | newext))) |
| 1518 | goto done; |
| 1519 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1520 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1521 | temp = PREV.br_startoff; |
| 1522 | temp2 = PREV.br_blockcount + |
| 1523 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | |
| 1526 | case MASK2(LEFT_FILLING, RIGHT_FILLING): |
| 1527 | /* |
| 1528 | * Setting all of a previous oldext extent to newext. |
| 1529 | * Neither the left nor right neighbors are contiguous with |
| 1530 | * the new one. |
| 1531 | */ |
| 1532 | xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx, |
| 1533 | XFS_DATA_FORK); |
| 1534 | xfs_bmbt_set_state(ep, newext); |
| 1535 | xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx, |
| 1536 | XFS_DATA_FORK); |
| 1537 | ip->i_df.if_lastex = idx; |
| 1538 | if (cur == NULL) |
| 1539 | rval = XFS_ILOG_DEXT; |
| 1540 | else { |
| 1541 | rval = 0; |
| 1542 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1543 | new->br_startblock, new->br_blockcount, |
| 1544 | &i))) |
| 1545 | goto done; |
| 1546 | ASSERT(i == 1); |
| 1547 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1548 | new->br_startblock, new->br_blockcount, |
| 1549 | newext))) |
| 1550 | goto done; |
| 1551 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1552 | /* DELTA: The in-core extent described by new changed type. */ |
| 1553 | temp = new->br_startoff; |
| 1554 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | break; |
| 1556 | |
| 1557 | case MASK2(LEFT_FILLING, LEFT_CONTIG): |
| 1558 | /* |
| 1559 | * Setting the first part of a previous oldext extent to newext. |
| 1560 | * The left neighbor is contiguous. |
| 1561 | */ |
| 1562 | xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1, |
| 1563 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1564 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | LEFT.br_blockcount + new->br_blockcount); |
| 1566 | xfs_bmbt_set_startoff(ep, |
| 1567 | PREV.br_startoff + new->br_blockcount); |
| 1568 | xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1, |
| 1569 | XFS_DATA_FORK); |
| 1570 | xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx, |
| 1571 | XFS_DATA_FORK); |
| 1572 | xfs_bmbt_set_startblock(ep, |
| 1573 | new->br_startblock + new->br_blockcount); |
| 1574 | xfs_bmbt_set_blockcount(ep, |
| 1575 | PREV.br_blockcount - new->br_blockcount); |
| 1576 | xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx, |
| 1577 | XFS_DATA_FORK); |
| 1578 | ip->i_df.if_lastex = idx - 1; |
| 1579 | if (cur == NULL) |
| 1580 | rval = XFS_ILOG_DEXT; |
| 1581 | else { |
| 1582 | rval = 0; |
| 1583 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1584 | PREV.br_startblock, PREV.br_blockcount, |
| 1585 | &i))) |
| 1586 | goto done; |
| 1587 | ASSERT(i == 1); |
| 1588 | if ((error = xfs_bmbt_update(cur, |
| 1589 | PREV.br_startoff + new->br_blockcount, |
| 1590 | PREV.br_startblock + new->br_blockcount, |
| 1591 | PREV.br_blockcount - new->br_blockcount, |
| 1592 | oldext))) |
| 1593 | goto done; |
| 1594 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 1595 | goto done; |
| 1596 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1597 | LEFT.br_startblock, |
| 1598 | LEFT.br_blockcount + new->br_blockcount, |
| 1599 | LEFT.br_state)) |
| 1600 | goto done; |
| 1601 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1602 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1603 | temp = LEFT.br_startoff; |
| 1604 | temp2 = LEFT.br_blockcount + |
| 1605 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | break; |
| 1607 | |
| 1608 | case MASK(LEFT_FILLING): |
| 1609 | /* |
| 1610 | * Setting the first part of a previous oldext extent to newext. |
| 1611 | * The left neighbor is not contiguous. |
| 1612 | */ |
| 1613 | xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK); |
| 1614 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1615 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1616 | xfs_bmbt_set_blockcount(ep, |
| 1617 | PREV.br_blockcount - new->br_blockcount); |
| 1618 | xfs_bmbt_set_startblock(ep, |
| 1619 | new->br_startblock + new->br_blockcount); |
| 1620 | xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK); |
| 1621 | xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL, |
| 1622 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1623 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | ip->i_df.if_lastex = idx; |
| 1625 | ip->i_d.di_nextents++; |
| 1626 | if (cur == NULL) |
| 1627 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1628 | else { |
| 1629 | rval = XFS_ILOG_CORE; |
| 1630 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1631 | PREV.br_startblock, PREV.br_blockcount, |
| 1632 | &i))) |
| 1633 | goto done; |
| 1634 | ASSERT(i == 1); |
| 1635 | if ((error = xfs_bmbt_update(cur, |
| 1636 | PREV.br_startoff + new->br_blockcount, |
| 1637 | PREV.br_startblock + new->br_blockcount, |
| 1638 | PREV.br_blockcount - new->br_blockcount, |
| 1639 | oldext))) |
| 1640 | goto done; |
| 1641 | cur->bc_rec.b = *new; |
| 1642 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1643 | goto done; |
| 1644 | ASSERT(i == 1); |
| 1645 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1646 | /* DELTA: One in-core extent is split in two. */ |
| 1647 | temp = PREV.br_startoff; |
| 1648 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | break; |
| 1650 | |
| 1651 | case MASK2(RIGHT_FILLING, RIGHT_CONTIG): |
| 1652 | /* |
| 1653 | * Setting the last part of a previous oldext extent to newext. |
| 1654 | * The right neighbor is contiguous with the new allocation. |
| 1655 | */ |
| 1656 | xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx, |
| 1657 | XFS_DATA_FORK); |
| 1658 | xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1, |
| 1659 | XFS_DATA_FORK); |
| 1660 | xfs_bmbt_set_blockcount(ep, |
| 1661 | PREV.br_blockcount - new->br_blockcount); |
| 1662 | xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx, |
| 1663 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1664 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1665 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | new->br_blockcount + RIGHT.br_blockcount, newext); |
| 1667 | xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1, |
| 1668 | XFS_DATA_FORK); |
| 1669 | ip->i_df.if_lastex = idx + 1; |
| 1670 | if (cur == NULL) |
| 1671 | rval = XFS_ILOG_DEXT; |
| 1672 | else { |
| 1673 | rval = 0; |
| 1674 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1675 | PREV.br_startblock, |
| 1676 | PREV.br_blockcount, &i))) |
| 1677 | goto done; |
| 1678 | ASSERT(i == 1); |
| 1679 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1680 | PREV.br_startblock, |
| 1681 | PREV.br_blockcount - new->br_blockcount, |
| 1682 | oldext))) |
| 1683 | goto done; |
| 1684 | if ((error = xfs_bmbt_increment(cur, 0, &i))) |
| 1685 | goto done; |
| 1686 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1687 | new->br_startblock, |
| 1688 | new->br_blockcount + RIGHT.br_blockcount, |
| 1689 | newext))) |
| 1690 | goto done; |
| 1691 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1692 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1693 | temp = PREV.br_startoff; |
| 1694 | temp2 = PREV.br_blockcount + |
| 1695 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | break; |
| 1697 | |
| 1698 | case MASK(RIGHT_FILLING): |
| 1699 | /* |
| 1700 | * Setting the last part of a previous oldext extent to newext. |
| 1701 | * The right neighbor is not contiguous. |
| 1702 | */ |
| 1703 | xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK); |
| 1704 | xfs_bmbt_set_blockcount(ep, |
| 1705 | PREV.br_blockcount - new->br_blockcount); |
| 1706 | xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK); |
| 1707 | xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1, |
| 1708 | new, NULL, XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1709 | xfs_iext_insert(ifp, idx + 1, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | ip->i_df.if_lastex = idx + 1; |
| 1711 | ip->i_d.di_nextents++; |
| 1712 | if (cur == NULL) |
| 1713 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1714 | else { |
| 1715 | rval = XFS_ILOG_CORE; |
| 1716 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1717 | PREV.br_startblock, PREV.br_blockcount, |
| 1718 | &i))) |
| 1719 | goto done; |
| 1720 | ASSERT(i == 1); |
| 1721 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1722 | PREV.br_startblock, |
| 1723 | PREV.br_blockcount - new->br_blockcount, |
| 1724 | oldext))) |
| 1725 | goto done; |
| 1726 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1727 | new->br_startblock, new->br_blockcount, |
| 1728 | &i))) |
| 1729 | goto done; |
| 1730 | ASSERT(i == 0); |
| 1731 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 1732 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1733 | goto done; |
| 1734 | ASSERT(i == 1); |
| 1735 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1736 | /* DELTA: One in-core extent is split in two. */ |
| 1737 | temp = PREV.br_startoff; |
| 1738 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | break; |
| 1740 | |
| 1741 | case 0: |
| 1742 | /* |
| 1743 | * Setting the middle part of a previous oldext extent to |
| 1744 | * newext. Contiguity is impossible here. |
| 1745 | * One extent becomes three extents. |
| 1746 | */ |
| 1747 | xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK); |
| 1748 | xfs_bmbt_set_blockcount(ep, |
| 1749 | new->br_startoff - PREV.br_startoff); |
| 1750 | xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK); |
| 1751 | r[0] = *new; |
| 1752 | r[1].br_startoff = new_endoff; |
| 1753 | r[1].br_blockcount = |
| 1754 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1755 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1756 | r[1].br_state = oldext; |
| 1757 | xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1], |
| 1758 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1759 | xfs_iext_insert(ifp, idx + 1, 2, &r[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | ip->i_df.if_lastex = idx + 1; |
| 1761 | ip->i_d.di_nextents += 2; |
| 1762 | if (cur == NULL) |
| 1763 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1764 | else { |
| 1765 | rval = XFS_ILOG_CORE; |
| 1766 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1767 | PREV.br_startblock, PREV.br_blockcount, |
| 1768 | &i))) |
| 1769 | goto done; |
| 1770 | ASSERT(i == 1); |
| 1771 | /* new right extent - oldext */ |
| 1772 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1773 | r[1].br_startblock, r[1].br_blockcount, |
| 1774 | r[1].br_state))) |
| 1775 | goto done; |
| 1776 | /* new left extent - oldext */ |
| 1777 | PREV.br_blockcount = |
| 1778 | new->br_startoff - PREV.br_startoff; |
| 1779 | cur->bc_rec.b = PREV; |
| 1780 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1781 | goto done; |
| 1782 | ASSERT(i == 1); |
| 1783 | if ((error = xfs_bmbt_increment(cur, 0, &i))) |
| 1784 | goto done; |
| 1785 | ASSERT(i == 1); |
| 1786 | /* new middle extent - newext */ |
| 1787 | cur->bc_rec.b = *new; |
| 1788 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 1789 | goto done; |
| 1790 | ASSERT(i == 1); |
| 1791 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1792 | /* DELTA: One in-core extent is split in three. */ |
| 1793 | temp = PREV.br_startoff; |
| 1794 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | break; |
| 1796 | |
| 1797 | case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1798 | case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG): |
| 1799 | case MASK2(LEFT_FILLING, RIGHT_CONTIG): |
| 1800 | case MASK2(RIGHT_FILLING, LEFT_CONTIG): |
| 1801 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 1802 | case MASK(LEFT_CONTIG): |
| 1803 | case MASK(RIGHT_CONTIG): |
| 1804 | /* |
| 1805 | * These cases are all impossible. |
| 1806 | */ |
| 1807 | ASSERT(0); |
| 1808 | } |
| 1809 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1810 | if (delta) { |
| 1811 | temp2 += temp; |
| 1812 | if (delta->xed_startoff > temp) |
| 1813 | delta->xed_startoff = temp; |
| 1814 | if (delta->xed_blockcount < temp2) |
| 1815 | delta->xed_blockcount = temp2; |
| 1816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | done: |
| 1818 | *logflagsp = rval; |
| 1819 | return error; |
| 1820 | #undef LEFT |
| 1821 | #undef RIGHT |
| 1822 | #undef PREV |
| 1823 | #undef MASK |
| 1824 | #undef MASK2 |
| 1825 | #undef MASK3 |
| 1826 | #undef MASK4 |
| 1827 | #undef STATE_SET |
| 1828 | #undef STATE_TEST |
| 1829 | #undef STATE_SET_TEST |
| 1830 | #undef SWITCH_STATE |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1835 | * to a delayed allocation. |
| 1836 | */ |
| 1837 | /*ARGSUSED*/ |
| 1838 | STATIC int /* error */ |
| 1839 | xfs_bmap_add_extent_hole_delay( |
| 1840 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1841 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1842 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1844 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | int rsvd) /* OK to allocate reserved blocks */ |
| 1846 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1847 | xfs_bmbt_rec_t *ep; /* extent record for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | #ifdef XFS_BMAP_TRACE |
| 1849 | static char fname[] = "xfs_bmap_add_extent_hole_delay"; |
| 1850 | #endif |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1851 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1853 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1854 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1855 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1856 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1857 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
| 1858 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | enum { /* bit number definitions for state */ |
| 1860 | LEFT_CONTIG, RIGHT_CONTIG, |
| 1861 | LEFT_DELAY, RIGHT_DELAY, |
| 1862 | LEFT_VALID, RIGHT_VALID |
| 1863 | }; |
| 1864 | |
| 1865 | #define MASK(b) (1 << (b)) |
| 1866 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 1867 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 1868 | #define STATE_TEST(b) (state & MASK(b)) |
| 1869 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 1870 | ((state &= ~MASK(b)), 0)) |
| 1871 | #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG)) |
| 1872 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1873 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1874 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | state = 0; |
| 1876 | ASSERT(ISNULLSTARTBLOCK(new->br_startblock)); |
| 1877 | /* |
| 1878 | * Check and set flags if this segment has a left neighbor |
| 1879 | */ |
| 1880 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1881 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock)); |
| 1883 | } |
| 1884 | /* |
| 1885 | * Check and set flags if the current (right) segment exists. |
| 1886 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1887 | */ |
| 1888 | if (STATE_SET_TEST(RIGHT_VALID, |
| 1889 | idx < |
| 1890 | ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 1891 | xfs_bmbt_get_all(ep, &right); |
| 1892 | STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock)); |
| 1893 | } |
| 1894 | /* |
| 1895 | * Set contiguity flags on the left and right neighbors. |
| 1896 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1897 | */ |
| 1898 | STATE_SET(LEFT_CONTIG, |
| 1899 | STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) && |
| 1900 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1901 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 1902 | STATE_SET(RIGHT_CONTIG, |
| 1903 | STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) && |
| 1904 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1905 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1906 | (!STATE_TEST(LEFT_CONTIG) || |
| 1907 | (left.br_blockcount + new->br_blockcount + |
| 1908 | right.br_blockcount <= MAXEXTLEN))); |
| 1909 | /* |
| 1910 | * Switch out based on the contiguity flags. |
| 1911 | */ |
| 1912 | switch (SWITCH_STATE) { |
| 1913 | |
| 1914 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 1915 | /* |
| 1916 | * New allocation is contiguous with delayed allocations |
| 1917 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1918 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | */ |
| 1920 | temp = left.br_blockcount + new->br_blockcount + |
| 1921 | right.br_blockcount; |
| 1922 | xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1, |
| 1923 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1924 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | oldlen = STARTBLOCKVAL(left.br_startblock) + |
| 1926 | STARTBLOCKVAL(new->br_startblock) + |
| 1927 | STARTBLOCKVAL(right.br_startblock); |
| 1928 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1929 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
| 1930 | NULLSTARTBLOCK((int)newlen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1, |
| 1932 | XFS_DATA_FORK); |
| 1933 | xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1, |
| 1934 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1935 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1937 | /* DELTA: Two in-core extents were replaced by one. */ |
| 1938 | temp2 = temp; |
| 1939 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | break; |
| 1941 | |
| 1942 | case MASK(LEFT_CONTIG): |
| 1943 | /* |
| 1944 | * New allocation is contiguous with a delayed allocation |
| 1945 | * on the left. |
| 1946 | * Merge the new allocation with the left neighbor. |
| 1947 | */ |
| 1948 | temp = left.br_blockcount + new->br_blockcount; |
| 1949 | xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, |
| 1950 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1951 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | oldlen = STARTBLOCKVAL(left.br_startblock) + |
| 1953 | STARTBLOCKVAL(new->br_startblock); |
| 1954 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1955 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
| 1956 | NULLSTARTBLOCK((int)newlen)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, |
| 1958 | XFS_DATA_FORK); |
| 1959 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1960 | /* DELTA: One in-core extent grew into a hole. */ |
| 1961 | temp2 = temp; |
| 1962 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | break; |
| 1964 | |
| 1965 | case MASK(RIGHT_CONTIG): |
| 1966 | /* |
| 1967 | * New allocation is contiguous with a delayed allocation |
| 1968 | * on the right. |
| 1969 | * Merge the new allocation with the right neighbor. |
| 1970 | */ |
| 1971 | xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK); |
| 1972 | temp = new->br_blockcount + right.br_blockcount; |
| 1973 | oldlen = STARTBLOCKVAL(new->br_startblock) + |
| 1974 | STARTBLOCKVAL(right.br_startblock); |
| 1975 | newlen = xfs_bmap_worst_indlen(ip, temp); |
| 1976 | xfs_bmbt_set_allf(ep, new->br_startoff, |
| 1977 | NULLSTARTBLOCK((int)newlen), temp, right.br_state); |
| 1978 | xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK); |
| 1979 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1980 | /* DELTA: One in-core extent grew into a hole. */ |
| 1981 | temp2 = temp; |
| 1982 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | break; |
| 1984 | |
| 1985 | case 0: |
| 1986 | /* |
| 1987 | * New allocation is not contiguous with another |
| 1988 | * delayed allocation. |
| 1989 | * Insert a new entry. |
| 1990 | */ |
| 1991 | oldlen = newlen = 0; |
| 1992 | xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL, |
| 1993 | XFS_DATA_FORK); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1994 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1996 | /* DELTA: A new in-core extent was added in a hole. */ |
| 1997 | temp2 = new->br_blockcount; |
| 1998 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | break; |
| 2000 | } |
| 2001 | if (oldlen != newlen) { |
| 2002 | ASSERT(oldlen > newlen); |
| 2003 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 2004 | (int64_t)(oldlen - newlen), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | /* |
| 2006 | * Nothing to do for disk quota accounting here. |
| 2007 | */ |
| 2008 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2009 | if (delta) { |
| 2010 | temp2 += temp; |
| 2011 | if (delta->xed_startoff > temp) |
| 2012 | delta->xed_startoff = temp; |
| 2013 | if (delta->xed_blockcount < temp2) |
| 2014 | delta->xed_blockcount = temp2; |
| 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | *logflagsp = 0; |
| 2017 | return 0; |
| 2018 | #undef MASK |
| 2019 | #undef MASK2 |
| 2020 | #undef STATE_SET |
| 2021 | #undef STATE_TEST |
| 2022 | #undef STATE_SET_TEST |
| 2023 | #undef SWITCH_STATE |
| 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 2028 | * to a real allocation. |
| 2029 | */ |
| 2030 | STATIC int /* error */ |
| 2031 | xfs_bmap_add_extent_hole_real( |
| 2032 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2033 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 2034 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2035 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2037 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | int whichfork) /* data or attr fork */ |
| 2039 | { |
| 2040 | xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */ |
| 2041 | int error; /* error return value */ |
| 2042 | #ifdef XFS_BMAP_TRACE |
| 2043 | static char fname[] = "xfs_bmap_add_extent_hole_real"; |
| 2044 | #endif |
| 2045 | int i; /* temp state */ |
| 2046 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2047 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 2048 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2049 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2051 | xfs_filblks_t temp=0; |
| 2052 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | enum { /* bit number definitions for state */ |
| 2054 | LEFT_CONTIG, RIGHT_CONTIG, |
| 2055 | LEFT_DELAY, RIGHT_DELAY, |
| 2056 | LEFT_VALID, RIGHT_VALID |
| 2057 | }; |
| 2058 | |
| 2059 | #define MASK(b) (1 << (b)) |
| 2060 | #define MASK2(a,b) (MASK(a) | MASK(b)) |
| 2061 | #define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b))) |
| 2062 | #define STATE_TEST(b) (state & MASK(b)) |
| 2063 | #define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \ |
| 2064 | ((state &= ~MASK(b)), 0)) |
| 2065 | #define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG)) |
| 2066 | |
| 2067 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2068 | ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2069 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | state = 0; |
| 2071 | /* |
| 2072 | * Check and set flags if this segment has a left neighbor. |
| 2073 | */ |
| 2074 | if (STATE_SET_TEST(LEFT_VALID, idx > 0)) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2075 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock)); |
| 2077 | } |
| 2078 | /* |
| 2079 | * Check and set flags if this segment has a current value. |
| 2080 | * Not true if we're inserting into the "hole" at eof. |
| 2081 | */ |
| 2082 | if (STATE_SET_TEST(RIGHT_VALID, |
| 2083 | idx < |
| 2084 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 2085 | xfs_bmbt_get_all(ep, &right); |
| 2086 | STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock)); |
| 2087 | } |
| 2088 | /* |
| 2089 | * We're inserting a real allocation between "left" and "right". |
| 2090 | * Set the contiguity flags. Don't let extents get too large. |
| 2091 | */ |
| 2092 | STATE_SET(LEFT_CONTIG, |
| 2093 | STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) && |
| 2094 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 2095 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 2096 | left.br_state == new->br_state && |
| 2097 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN); |
| 2098 | STATE_SET(RIGHT_CONTIG, |
| 2099 | STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) && |
| 2100 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 2101 | new->br_startblock + new->br_blockcount == |
| 2102 | right.br_startblock && |
| 2103 | new->br_state == right.br_state && |
| 2104 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 2105 | (!STATE_TEST(LEFT_CONTIG) || |
| 2106 | left.br_blockcount + new->br_blockcount + |
| 2107 | right.br_blockcount <= MAXEXTLEN)); |
| 2108 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2109 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | /* |
| 2111 | * Select which case we're in here, and implement it. |
| 2112 | */ |
| 2113 | switch (SWITCH_STATE) { |
| 2114 | |
| 2115 | case MASK2(LEFT_CONTIG, RIGHT_CONTIG): |
| 2116 | /* |
| 2117 | * New allocation is contiguous with real allocations on the |
| 2118 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2119 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | */ |
| 2121 | xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1, |
| 2122 | whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2123 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | left.br_blockcount + new->br_blockcount + |
| 2125 | right.br_blockcount); |
| 2126 | xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1, |
| 2127 | whichfork); |
| 2128 | xfs_bmap_trace_delete(fname, "LC|RC", ip, |
| 2129 | idx, 1, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2130 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | ifp->if_lastex = idx - 1; |
| 2132 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2133 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2134 | if (cur == NULL) { |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2135 | rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork); |
| 2136 | } else { |
| 2137 | rval = XFS_ILOG_CORE; |
| 2138 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2139 | right.br_startoff, |
| 2140 | right.br_startblock, |
| 2141 | right.br_blockcount, &i))) |
| 2142 | goto done; |
| 2143 | ASSERT(i == 1); |
| 2144 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 2145 | goto done; |
| 2146 | ASSERT(i == 1); |
| 2147 | if ((error = xfs_bmbt_decrement(cur, 0, &i))) |
| 2148 | goto done; |
| 2149 | ASSERT(i == 1); |
| 2150 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2151 | left.br_startblock, |
| 2152 | left.br_blockcount + |
| 2153 | new->br_blockcount + |
| 2154 | right.br_blockcount, |
| 2155 | left.br_state))) |
| 2156 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2158 | /* DELTA: Two in-core extents were replaced by one. */ |
| 2159 | temp = left.br_startoff; |
| 2160 | temp2 = left.br_blockcount + |
| 2161 | new->br_blockcount + |
| 2162 | right.br_blockcount; |
| 2163 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
| 2165 | case MASK(LEFT_CONTIG): |
| 2166 | /* |
| 2167 | * New allocation is contiguous with a real allocation |
| 2168 | * on the left. |
| 2169 | * Merge the new allocation with the left neighbor. |
| 2170 | */ |
| 2171 | xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2172 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | left.br_blockcount + new->br_blockcount); |
| 2174 | xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork); |
| 2175 | ifp->if_lastex = idx - 1; |
| 2176 | if (cur == NULL) { |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2177 | rval = XFS_ILOG_FEXT(whichfork); |
| 2178 | } else { |
| 2179 | rval = 0; |
| 2180 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2181 | left.br_startoff, |
| 2182 | left.br_startblock, |
| 2183 | left.br_blockcount, &i))) |
| 2184 | goto done; |
| 2185 | ASSERT(i == 1); |
| 2186 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2187 | left.br_startblock, |
| 2188 | left.br_blockcount + |
| 2189 | new->br_blockcount, |
| 2190 | left.br_state))) |
| 2191 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2193 | /* DELTA: One in-core extent grew. */ |
| 2194 | temp = left.br_startoff; |
| 2195 | temp2 = left.br_blockcount + |
| 2196 | new->br_blockcount; |
| 2197 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | |
| 2199 | case MASK(RIGHT_CONTIG): |
| 2200 | /* |
| 2201 | * New allocation is contiguous with a real allocation |
| 2202 | * on the right. |
| 2203 | * Merge the new allocation with the right neighbor. |
| 2204 | */ |
| 2205 | xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork); |
| 2206 | xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock, |
| 2207 | new->br_blockcount + right.br_blockcount, |
| 2208 | right.br_state); |
| 2209 | xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork); |
| 2210 | ifp->if_lastex = idx; |
| 2211 | if (cur == NULL) { |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2212 | rval = XFS_ILOG_FEXT(whichfork); |
| 2213 | } else { |
| 2214 | rval = 0; |
| 2215 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2216 | right.br_startoff, |
| 2217 | right.br_startblock, |
| 2218 | right.br_blockcount, &i))) |
| 2219 | goto done; |
| 2220 | ASSERT(i == 1); |
| 2221 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2222 | new->br_startblock, |
| 2223 | new->br_blockcount + |
| 2224 | right.br_blockcount, |
| 2225 | right.br_state))) |
| 2226 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2228 | /* DELTA: One in-core extent grew. */ |
| 2229 | temp = new->br_startoff; |
| 2230 | temp2 = new->br_blockcount + |
| 2231 | right.br_blockcount; |
| 2232 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | |
| 2234 | case 0: |
| 2235 | /* |
| 2236 | * New allocation is not contiguous with another |
| 2237 | * real allocation. |
| 2238 | * Insert a new entry. |
| 2239 | */ |
| 2240 | xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL, |
| 2241 | whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2242 | xfs_iext_insert(ifp, idx, 1, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | ifp->if_lastex = idx; |
| 2244 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2245 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2246 | if (cur == NULL) { |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2247 | rval = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork); |
| 2248 | } else { |
| 2249 | rval = XFS_ILOG_CORE; |
| 2250 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2251 | new->br_startoff, |
| 2252 | new->br_startblock, |
| 2253 | new->br_blockcount, &i))) |
| 2254 | goto done; |
| 2255 | ASSERT(i == 0); |
| 2256 | cur->bc_rec.b.br_state = new->br_state; |
| 2257 | if ((error = xfs_bmbt_insert(cur, &i))) |
| 2258 | goto done; |
| 2259 | ASSERT(i == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2261 | /* DELTA: A new extent was added in a hole. */ |
| 2262 | temp = new->br_startoff; |
| 2263 | temp2 = new->br_blockcount; |
| 2264 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2266 | if (delta) { |
| 2267 | temp2 += temp; |
| 2268 | if (delta->xed_startoff > temp) |
| 2269 | delta->xed_startoff = temp; |
| 2270 | if (delta->xed_blockcount < temp2) |
| 2271 | delta->xed_blockcount = temp2; |
| 2272 | } |
| 2273 | done: |
| 2274 | *logflagsp = rval; |
| 2275 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | #undef MASK |
| 2277 | #undef MASK2 |
| 2278 | #undef STATE_SET |
| 2279 | #undef STATE_TEST |
| 2280 | #undef STATE_SET_TEST |
| 2281 | #undef SWITCH_STATE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2284 | /* |
| 2285 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2286 | */ |
| 2287 | STATIC int |
| 2288 | xfs_bmap_extsize_align( |
| 2289 | xfs_mount_t *mp, |
| 2290 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2291 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2292 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2293 | int rt, /* is this a realtime inode? */ |
| 2294 | int eof, /* is extent at end-of-file? */ |
| 2295 | int delay, /* creating delalloc extent? */ |
| 2296 | int convert, /* overwriting unwritten extent? */ |
| 2297 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2298 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2299 | { |
| 2300 | xfs_fileoff_t orig_off; /* original offset */ |
| 2301 | xfs_extlen_t orig_alen; /* original length */ |
| 2302 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2303 | xfs_fileoff_t nexto; /* next file offset */ |
| 2304 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2305 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2306 | xfs_extlen_t align_alen; /* temp for length */ |
| 2307 | xfs_extlen_t temp; /* temp for calculations */ |
| 2308 | |
| 2309 | if (convert) |
| 2310 | return 0; |
| 2311 | |
| 2312 | orig_off = align_off = *offp; |
| 2313 | orig_alen = align_alen = *lenp; |
| 2314 | orig_end = orig_off + orig_alen; |
| 2315 | |
| 2316 | /* |
| 2317 | * If this request overlaps an existing extent, then don't |
| 2318 | * attempt to perform any additional alignment. |
| 2319 | */ |
| 2320 | if (!delay && !eof && |
| 2321 | (orig_off >= gotp->br_startoff) && |
| 2322 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | /* |
| 2327 | * If the file offset is unaligned vs. the extent size |
| 2328 | * we need to align it. This will be possible unless |
| 2329 | * the file was previously written with a kernel that didn't |
| 2330 | * perform this alignment, or if a truncate shot us in the |
| 2331 | * foot. |
| 2332 | */ |
| 2333 | temp = do_mod(orig_off, extsz); |
| 2334 | if (temp) { |
| 2335 | align_alen += temp; |
| 2336 | align_off -= temp; |
| 2337 | } |
| 2338 | /* |
| 2339 | * Same adjustment for the end of the requested area. |
| 2340 | */ |
| 2341 | if ((temp = (align_alen % extsz))) { |
| 2342 | align_alen += extsz - temp; |
| 2343 | } |
| 2344 | /* |
| 2345 | * If the previous block overlaps with this proposed allocation |
| 2346 | * then move the start forward without adjusting the length. |
| 2347 | */ |
| 2348 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2349 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2350 | prevo = prevp->br_startoff; |
| 2351 | else |
| 2352 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2353 | } else |
| 2354 | prevo = 0; |
| 2355 | if (align_off != orig_off && align_off < prevo) |
| 2356 | align_off = prevo; |
| 2357 | /* |
| 2358 | * If the next block overlaps with this proposed allocation |
| 2359 | * then move the start back without adjusting the length, |
| 2360 | * but not before offset 0. |
| 2361 | * This may of course make the start overlap previous block, |
| 2362 | * and if we hit the offset 0 limit then the next block |
| 2363 | * can still overlap too. |
| 2364 | */ |
| 2365 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2366 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2367 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2368 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2369 | else |
| 2370 | nexto = gotp->br_startoff; |
| 2371 | } else |
| 2372 | nexto = NULLFILEOFF; |
| 2373 | if (!eof && |
| 2374 | align_off + align_alen != orig_end && |
| 2375 | align_off + align_alen > nexto) |
| 2376 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2377 | /* |
| 2378 | * If we're now overlapping the next or previous extent that |
| 2379 | * means we can't fit an extsz piece in this hole. Just move |
| 2380 | * the start forward to the first valid spot and set |
| 2381 | * the length so we hit the end. |
| 2382 | */ |
| 2383 | if (align_off != orig_off && align_off < prevo) |
| 2384 | align_off = prevo; |
| 2385 | if (align_off + align_alen != orig_end && |
| 2386 | align_off + align_alen > nexto && |
| 2387 | nexto != NULLFILEOFF) { |
| 2388 | ASSERT(nexto > prevo); |
| 2389 | align_alen = nexto - align_off; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * If realtime, and the result isn't a multiple of the realtime |
| 2394 | * extent size we need to remove blocks until it is. |
| 2395 | */ |
| 2396 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2397 | /* |
| 2398 | * We're not covering the original request, or |
| 2399 | * we won't be able to once we fix the length. |
| 2400 | */ |
| 2401 | if (orig_off < align_off || |
| 2402 | orig_end > align_off + align_alen || |
| 2403 | align_alen - temp < orig_alen) |
| 2404 | return XFS_ERROR(EINVAL); |
| 2405 | /* |
| 2406 | * Try to fix it by moving the start up. |
| 2407 | */ |
| 2408 | if (align_off + temp <= orig_off) { |
| 2409 | align_alen -= temp; |
| 2410 | align_off += temp; |
| 2411 | } |
| 2412 | /* |
| 2413 | * Try to fix it by moving the end in. |
| 2414 | */ |
| 2415 | else if (align_off + align_alen - temp >= orig_end) |
| 2416 | align_alen -= temp; |
| 2417 | /* |
| 2418 | * Set the start to the minimum then trim the length. |
| 2419 | */ |
| 2420 | else { |
| 2421 | align_alen -= orig_off - align_off; |
| 2422 | align_off = orig_off; |
| 2423 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 2424 | } |
| 2425 | /* |
| 2426 | * Result doesn't cover the request, fail it. |
| 2427 | */ |
| 2428 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2429 | return XFS_ERROR(EINVAL); |
| 2430 | } else { |
| 2431 | ASSERT(orig_off >= align_off); |
| 2432 | ASSERT(orig_end <= align_off + align_alen); |
| 2433 | } |
| 2434 | |
| 2435 | #ifdef DEBUG |
| 2436 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2437 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2438 | if (prevp->br_startoff != NULLFILEOFF) |
| 2439 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2440 | #endif |
| 2441 | |
| 2442 | *lenp = align_alen; |
| 2443 | *offp = align_off; |
| 2444 | return 0; |
| 2445 | } |
| 2446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2448 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2449 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2450 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2452 | { |
| 2453 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2455 | xfs_mount_t *mp; /* mount point structure */ |
| 2456 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2457 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | |
| 2459 | #define ISVALID(x,y) \ |
| 2460 | (rt ? \ |
| 2461 | (x) < mp->m_sb.sb_rblocks : \ |
| 2462 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2463 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2464 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | mp = ap->ip->i_mount; |
| 2467 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2468 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
| 2469 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | /* |
| 2471 | * If allocating at eof, and there's a previous real block, |
| 2472 | * try to use it's last block as our starting point. |
| 2473 | */ |
| 2474 | if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && |
| 2475 | !ISNULLSTARTBLOCK(ap->prevp->br_startblock) && |
| 2476 | ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, |
| 2477 | ap->prevp->br_startblock)) { |
| 2478 | ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; |
| 2479 | /* |
| 2480 | * Adjust for the gap between prevp and us. |
| 2481 | */ |
| 2482 | adjust = ap->off - |
| 2483 | (ap->prevp->br_startoff + ap->prevp->br_blockcount); |
| 2484 | if (adjust && |
| 2485 | ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) |
| 2486 | ap->rval += adjust; |
| 2487 | } |
| 2488 | /* |
| 2489 | * If not at eof, then compare the two neighbor blocks. |
| 2490 | * Figure out whether either one gives us a good starting point, |
| 2491 | * and pick the better one. |
| 2492 | */ |
| 2493 | else if (!ap->eof) { |
| 2494 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2495 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2496 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2497 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2498 | |
| 2499 | /* |
| 2500 | * If there's a previous (left) block, select a requested |
| 2501 | * start block based on it. |
| 2502 | */ |
| 2503 | if (ap->prevp->br_startoff != NULLFILEOFF && |
| 2504 | !ISNULLSTARTBLOCK(ap->prevp->br_startblock) && |
| 2505 | (prevbno = ap->prevp->br_startblock + |
| 2506 | ap->prevp->br_blockcount) && |
| 2507 | ISVALID(prevbno, ap->prevp->br_startblock)) { |
| 2508 | /* |
| 2509 | * Calculate gap to end of previous block. |
| 2510 | */ |
| 2511 | adjust = prevdiff = ap->off - |
| 2512 | (ap->prevp->br_startoff + |
| 2513 | ap->prevp->br_blockcount); |
| 2514 | /* |
| 2515 | * Figure the startblock based on the previous block's |
| 2516 | * end and the gap size. |
| 2517 | * Heuristic! |
| 2518 | * If the gap is large relative to the piece we're |
| 2519 | * allocating, or using it gives us an invalid block |
| 2520 | * number, then just use the end of the previous block. |
| 2521 | */ |
| 2522 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2523 | ISVALID(prevbno + prevdiff, |
| 2524 | ap->prevp->br_startblock)) |
| 2525 | prevbno += adjust; |
| 2526 | else |
| 2527 | prevdiff += adjust; |
| 2528 | /* |
| 2529 | * If the firstblock forbids it, can't use it, |
| 2530 | * must use default. |
| 2531 | */ |
| 2532 | if (!rt && !nullfb && |
| 2533 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2534 | prevbno = NULLFSBLOCK; |
| 2535 | } |
| 2536 | /* |
| 2537 | * No previous block or can't follow it, just default. |
| 2538 | */ |
| 2539 | else |
| 2540 | prevbno = NULLFSBLOCK; |
| 2541 | /* |
| 2542 | * If there's a following (right) block, select a requested |
| 2543 | * start block based on it. |
| 2544 | */ |
| 2545 | if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) { |
| 2546 | /* |
| 2547 | * Calculate gap to start of next block. |
| 2548 | */ |
| 2549 | adjust = gotdiff = ap->gotp->br_startoff - ap->off; |
| 2550 | /* |
| 2551 | * Figure the startblock based on the next block's |
| 2552 | * start and the gap size. |
| 2553 | */ |
| 2554 | gotbno = ap->gotp->br_startblock; |
| 2555 | /* |
| 2556 | * Heuristic! |
| 2557 | * If the gap is large relative to the piece we're |
| 2558 | * allocating, or using it gives us an invalid block |
| 2559 | * number, then just use the start of the next block |
| 2560 | * offset by our length. |
| 2561 | */ |
| 2562 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2563 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2564 | gotbno -= adjust; |
| 2565 | else if (ISVALID(gotbno - ap->alen, gotbno)) { |
| 2566 | gotbno -= ap->alen; |
| 2567 | gotdiff += adjust - ap->alen; |
| 2568 | } else |
| 2569 | gotdiff += adjust; |
| 2570 | /* |
| 2571 | * If the firstblock forbids it, can't use it, |
| 2572 | * must use default. |
| 2573 | */ |
| 2574 | if (!rt && !nullfb && |
| 2575 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2576 | gotbno = NULLFSBLOCK; |
| 2577 | } |
| 2578 | /* |
| 2579 | * No next block, just default. |
| 2580 | */ |
| 2581 | else |
| 2582 | gotbno = NULLFSBLOCK; |
| 2583 | /* |
| 2584 | * If both valid, pick the better one, else the only good |
| 2585 | * one, else ap->rval is already set (to 0 or the inode block). |
| 2586 | */ |
| 2587 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
| 2588 | ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; |
| 2589 | else if (prevbno != NULLFSBLOCK) |
| 2590 | ap->rval = prevbno; |
| 2591 | else if (gotbno != NULLFSBLOCK) |
| 2592 | ap->rval = gotbno; |
| 2593 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2594 | #undef ISVALID |
| 2595 | return 0; |
| 2596 | } |
| 2597 | |
| 2598 | STATIC int |
| 2599 | xfs_bmap_rtalloc( |
| 2600 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2601 | { |
| 2602 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2603 | int error; /* error return value */ |
| 2604 | xfs_mount_t *mp; /* mount point structure */ |
| 2605 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2606 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2607 | xfs_extlen_t align; /* minimum allocation alignment */ |
| 2608 | xfs_rtblock_t rtx; /* realtime extent number */ |
| 2609 | xfs_rtblock_t rtb; |
| 2610 | |
| 2611 | mp = ap->ip->i_mount; |
| 2612 | align = ap->ip->i_d.di_extsize ? |
| 2613 | ap->ip->i_d.di_extsize : mp->m_sb.sb_rextsize; |
| 2614 | prod = align / mp->m_sb.sb_rextsize; |
| 2615 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2616 | align, 1, ap->eof, 0, |
| 2617 | ap->conv, &ap->off, &ap->alen); |
| 2618 | if (error) |
| 2619 | return error; |
| 2620 | ASSERT(ap->alen); |
| 2621 | ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); |
| 2622 | |
| 2623 | /* |
| 2624 | * If the offset & length are not perfectly aligned |
| 2625 | * then kill prod, it will just get us in trouble. |
| 2626 | */ |
| 2627 | if (do_mod(ap->off, align) || ap->alen % align) |
| 2628 | prod = 1; |
| 2629 | /* |
| 2630 | * Set ralen to be the actual requested length in rtextents. |
| 2631 | */ |
| 2632 | ralen = ap->alen / mp->m_sb.sb_rextsize; |
| 2633 | /* |
| 2634 | * If the old value was close enough to MAXEXTLEN that |
| 2635 | * we rounded up to it, cut it back so it's valid again. |
| 2636 | * Note that if it's a really large request (bigger than |
| 2637 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2638 | * adjust the starting point to match it. |
| 2639 | */ |
| 2640 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2641 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
| 2642 | /* |
| 2643 | * If it's an allocation to an empty file at offset 0, |
| 2644 | * pick an extent that will space things out in the rt area. |
| 2645 | */ |
| 2646 | if (ap->eof && ap->off == 0) { |
| 2647 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2648 | if (error) |
| 2649 | return error; |
| 2650 | ap->rval = rtx * mp->m_sb.sb_rextsize; |
| 2651 | } else { |
| 2652 | ap->rval = 0; |
| 2653 | } |
| 2654 | |
| 2655 | xfs_bmap_adjacent(ap); |
| 2656 | |
| 2657 | /* |
| 2658 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2659 | */ |
| 2660 | atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2661 | do_div(ap->rval, mp->m_sb.sb_rextsize); |
| 2662 | rtb = ap->rval; |
| 2663 | ap->alen = ralen; |
| 2664 | if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, |
| 2665 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2666 | return error; |
| 2667 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 2668 | (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, |
| 2669 | ap->alen, &ralen, atype, |
| 2670 | ap->wasdel, 1, &rtb))) |
| 2671 | return error; |
| 2672 | ap->rval = rtb; |
| 2673 | if (ap->rval != NULLFSBLOCK) { |
| 2674 | ap->rval *= mp->m_sb.sb_rextsize; |
| 2675 | ralen *= mp->m_sb.sb_rextsize; |
| 2676 | ap->alen = ralen; |
| 2677 | ap->ip->i_d.di_nblocks += ralen; |
| 2678 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2679 | if (ap->wasdel) |
| 2680 | ap->ip->i_delayed_blks -= ralen; |
| 2681 | /* |
| 2682 | * Adjust the disk quota also. This was reserved |
| 2683 | * earlier. |
| 2684 | */ |
| 2685 | XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip, |
| 2686 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2687 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2688 | } else { |
| 2689 | ap->alen = 0; |
| 2690 | } |
| 2691 | return 0; |
| 2692 | } |
| 2693 | |
| 2694 | STATIC int |
| 2695 | xfs_bmap_btalloc( |
| 2696 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2697 | { |
| 2698 | xfs_mount_t *mp; /* mount point structure */ |
| 2699 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2700 | xfs_extlen_t align; /* minimum allocation alignment */ |
| 2701 | xfs_agnumber_t ag; |
| 2702 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2703 | xfs_agnumber_t startag; |
| 2704 | xfs_alloc_arg_t args; |
| 2705 | xfs_extlen_t blen; |
| 2706 | xfs_extlen_t delta; |
| 2707 | xfs_extlen_t longest; |
| 2708 | xfs_extlen_t need; |
| 2709 | xfs_extlen_t nextminlen = 0; |
| 2710 | xfs_perag_t *pag; |
| 2711 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2712 | int isaligned; |
| 2713 | int notinit; |
| 2714 | int tryagain; |
| 2715 | int error; |
| 2716 | |
| 2717 | mp = ap->ip->i_mount; |
| 2718 | align = (ap->userdata && ap->ip->i_d.di_extsize && |
| 2719 | (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)) ? |
| 2720 | ap->ip->i_d.di_extsize : 0; |
| 2721 | if (unlikely(align)) { |
| 2722 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2723 | align, 0, ap->eof, 0, ap->conv, |
| 2724 | &ap->off, &ap->alen); |
| 2725 | ASSERT(!error); |
| 2726 | ASSERT(ap->alen); |
| 2727 | } |
| 2728 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2729 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
| 2730 | if (nullfb) |
| 2731 | ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
| 2732 | else |
| 2733 | ap->rval = ap->firstblock; |
| 2734 | |
| 2735 | xfs_bmap_adjacent(ap); |
| 2736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | /* |
| 2738 | * If allowed, use ap->rval; otherwise must use firstblock since |
| 2739 | * it's in the right allocation group. |
| 2740 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2741 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | ; |
| 2743 | else |
| 2744 | ap->rval = ap->firstblock; |
| 2745 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | * Normal allocation, done through xfs_alloc_vextent. |
| 2747 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2748 | tryagain = isaligned = 0; |
| 2749 | args.tp = ap->tp; |
| 2750 | args.mp = mp; |
| 2751 | args.fsbno = ap->rval; |
| 2752 | args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks); |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2753 | args.firstblock = ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2754 | blen = 0; |
| 2755 | if (nullfb) { |
| 2756 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2757 | args.total = ap->total; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | /* |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2759 | * Find the longest available space. |
| 2760 | * We're going to try for the whole allocation at once. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2762 | startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno); |
| 2763 | notinit = 0; |
| 2764 | down_read(&mp->m_peraglock); |
| 2765 | while (blen < ap->alen) { |
| 2766 | pag = &mp->m_perag[ag]; |
| 2767 | if (!pag->pagf_init && |
| 2768 | (error = xfs_alloc_pagf_init(mp, args.tp, |
| 2769 | ag, XFS_ALLOC_FLAG_TRYLOCK))) { |
| 2770 | up_read(&mp->m_peraglock); |
| 2771 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2773 | /* |
| 2774 | * See xfs_alloc_fix_freelist... |
| 2775 | */ |
| 2776 | if (pag->pagf_init) { |
| 2777 | need = XFS_MIN_FREELIST_PAG(pag, mp); |
| 2778 | delta = need > pag->pagf_flcount ? |
| 2779 | need - pag->pagf_flcount : 0; |
| 2780 | longest = (pag->pagf_longest > delta) ? |
| 2781 | (pag->pagf_longest - delta) : |
| 2782 | (pag->pagf_flcount > 0 || |
| 2783 | pag->pagf_longest > 0); |
| 2784 | if (blen < longest) |
| 2785 | blen = longest; |
| 2786 | } else |
| 2787 | notinit = 1; |
| 2788 | if (++ag == mp->m_sb.sb_agcount) |
| 2789 | ag = 0; |
| 2790 | if (ag == startag) |
| 2791 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2793 | up_read(&mp->m_peraglock); |
| 2794 | /* |
| 2795 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2796 | * possible that there is space for this request. |
| 2797 | */ |
| 2798 | if (notinit || blen < ap->minlen) |
| 2799 | args.minlen = ap->minlen; |
| 2800 | /* |
| 2801 | * If the best seen length is less than the request |
| 2802 | * length, use the best as the minimum. |
| 2803 | */ |
| 2804 | else if (blen < ap->alen) |
| 2805 | args.minlen = blen; |
| 2806 | /* |
| 2807 | * Otherwise we've seen an extent as big as alen, |
| 2808 | * use that as the minimum. |
| 2809 | */ |
| 2810 | else |
| 2811 | args.minlen = ap->alen; |
| 2812 | } else if (ap->low) { |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2813 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2814 | args.total = args.minlen = ap->minlen; |
| 2815 | } else { |
| 2816 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2817 | args.total = ap->total; |
| 2818 | args.minlen = ap->minlen; |
| 2819 | } |
| 2820 | if (unlikely(ap->userdata && ap->ip->i_d.di_extsize && |
| 2821 | (ap->ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE))) { |
| 2822 | args.prod = ap->ip->i_d.di_extsize; |
| 2823 | if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) |
| 2824 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Nathan Scott | 6fe90e6d | 2006-06-19 08:40:12 +1000 | [diff] [blame] | 2825 | } else if (mp->m_sb.sb_blocksize >= NBPP) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2826 | args.prod = 1; |
| 2827 | args.mod = 0; |
| 2828 | } else { |
| 2829 | args.prod = NBPP >> mp->m_sb.sb_blocklog; |
| 2830 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) |
| 2831 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2832 | } |
| 2833 | /* |
| 2834 | * If we are not low on available data blocks, and the |
| 2835 | * underlying logical volume manager is a stripe, and |
| 2836 | * the file offset is zero then try to allocate data |
| 2837 | * blocks on stripe unit boundary. |
| 2838 | * NOTE: ap->aeof is only set if the allocation length |
| 2839 | * is >= the stripe unit and the allocation offset is |
| 2840 | * at the end of file. |
| 2841 | */ |
| 2842 | if (!ap->low && ap->aeof) { |
| 2843 | if (!ap->off) { |
| 2844 | args.alignment = mp->m_dalign; |
| 2845 | atype = args.type; |
| 2846 | isaligned = 1; |
| 2847 | /* |
| 2848 | * Adjust for alignment |
| 2849 | */ |
| 2850 | if (blen > args.alignment && blen <= ap->alen) |
| 2851 | args.minlen = blen - args.alignment; |
| 2852 | args.minalignslop = 0; |
| 2853 | } else { |
| 2854 | /* |
| 2855 | * First try an exact bno allocation. |
| 2856 | * If it fails then do a near or start bno |
| 2857 | * allocation with alignment turned on. |
| 2858 | */ |
| 2859 | atype = args.type; |
| 2860 | tryagain = 1; |
| 2861 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2862 | args.alignment = 1; |
| 2863 | /* |
| 2864 | * Compute the minlen+alignment for the |
| 2865 | * next case. Set slop so that the value |
| 2866 | * of minlen+alignment+slop doesn't go up |
| 2867 | * between the calls. |
| 2868 | */ |
| 2869 | if (blen > mp->m_dalign && blen <= ap->alen) |
| 2870 | nextminlen = blen - mp->m_dalign; |
| 2871 | else |
| 2872 | nextminlen = args.minlen; |
| 2873 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2874 | args.minalignslop = |
| 2875 | nextminlen + mp->m_dalign - |
| 2876 | args.minlen - 1; |
| 2877 | else |
| 2878 | args.minalignslop = 0; |
| 2879 | } |
| 2880 | } else { |
| 2881 | args.alignment = 1; |
| 2882 | args.minalignslop = 0; |
| 2883 | } |
| 2884 | args.minleft = ap->minleft; |
| 2885 | args.wasdel = ap->wasdel; |
| 2886 | args.isfl = 0; |
| 2887 | args.userdata = ap->userdata; |
| 2888 | if ((error = xfs_alloc_vextent(&args))) |
| 2889 | return error; |
| 2890 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2891 | /* |
| 2892 | * Exact allocation failed. Now try with alignment |
| 2893 | * turned on. |
| 2894 | */ |
| 2895 | args.type = atype; |
| 2896 | args.fsbno = ap->rval; |
| 2897 | args.alignment = mp->m_dalign; |
| 2898 | args.minlen = nextminlen; |
| 2899 | args.minalignslop = 0; |
| 2900 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | if ((error = xfs_alloc_vextent(&args))) |
| 2902 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2903 | } |
| 2904 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2905 | /* |
| 2906 | * allocation failed, so turn off alignment and |
| 2907 | * try again. |
| 2908 | */ |
| 2909 | args.type = atype; |
| 2910 | args.fsbno = ap->rval; |
| 2911 | args.alignment = 0; |
| 2912 | if ((error = xfs_alloc_vextent(&args))) |
| 2913 | return error; |
| 2914 | } |
| 2915 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2916 | args.minlen > ap->minlen) { |
| 2917 | args.minlen = ap->minlen; |
| 2918 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2919 | args.fsbno = ap->rval; |
| 2920 | if ((error = xfs_alloc_vextent(&args))) |
| 2921 | return error; |
| 2922 | } |
| 2923 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2924 | args.fsbno = 0; |
| 2925 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2926 | args.total = ap->minlen; |
| 2927 | args.minleft = 0; |
| 2928 | if ((error = xfs_alloc_vextent(&args))) |
| 2929 | return error; |
| 2930 | ap->low = 1; |
| 2931 | } |
| 2932 | if (args.fsbno != NULLFSBLOCK) { |
| 2933 | ap->firstblock = ap->rval = args.fsbno; |
| 2934 | ASSERT(nullfb || fb_agno == args.agno || |
| 2935 | (ap->low && fb_agno < args.agno)); |
| 2936 | ap->alen = args.len; |
| 2937 | ap->ip->i_d.di_nblocks += args.len; |
| 2938 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2939 | if (ap->wasdel) |
| 2940 | ap->ip->i_delayed_blks -= args.len; |
| 2941 | /* |
| 2942 | * Adjust the disk quota also. This was reserved |
| 2943 | * earlier. |
| 2944 | */ |
| 2945 | XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip, |
| 2946 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2947 | XFS_TRANS_DQ_BCOUNT, |
| 2948 | (long) args.len); |
| 2949 | } else { |
| 2950 | ap->rval = NULLFSBLOCK; |
| 2951 | ap->alen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | } |
| 2953 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | /* |
| 2957 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2958 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2959 | */ |
| 2960 | STATIC int |
| 2961 | xfs_bmap_alloc( |
| 2962 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2963 | { |
| 2964 | if ((ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata) |
| 2965 | return xfs_bmap_rtalloc(ap); |
| 2966 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | /* |
| 2970 | * Transform a btree format file with only one leaf node, where the |
| 2971 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2972 | * 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] | 2973 | * give up the space for the btree root and pitch the leaf block. |
| 2974 | */ |
| 2975 | STATIC int /* error */ |
| 2976 | xfs_bmap_btree_to_extents( |
| 2977 | xfs_trans_t *tp, /* transaction pointer */ |
| 2978 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2979 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2980 | int *logflagsp, /* inode logging flags */ |
| 2981 | int whichfork) /* data or attr fork */ |
| 2982 | { |
| 2983 | /* REFERENCED */ |
| 2984 | xfs_bmbt_block_t *cblock;/* child btree block */ |
| 2985 | xfs_fsblock_t cbno; /* child block number */ |
| 2986 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2987 | int error; /* error return value */ |
| 2988 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2989 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2990 | __be64 *pp; /* ptr to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | xfs_bmbt_block_t *rblock;/* root btree block */ |
| 2992 | |
| 2993 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2994 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2995 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2996 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2997 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2998 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1); |
| 3000 | mp = ip->i_mount; |
| 3001 | pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3002 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | *logflagsp = 0; |
| 3004 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3005 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | return error; |
| 3007 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 3009 | XFS_BMAP_BTREE_REF))) |
| 3010 | return error; |
| 3011 | cblock = XFS_BUF_TO_BMBT_BLOCK(cbp); |
| 3012 | if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp))) |
| 3013 | return error; |
| 3014 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 3015 | ip->i_d.di_nblocks--; |
| 3016 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
| 3017 | xfs_trans_binval(tp, cbp); |
| 3018 | if (cur->bc_bufs[0] == cbp) |
| 3019 | cur->bc_bufs[0] = NULL; |
| 3020 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3021 | ASSERT(ifp->if_broot == NULL); |
| 3022 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 3023 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3024 | *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork); |
| 3025 | return 0; |
| 3026 | } |
| 3027 | |
| 3028 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3029 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | * after removing space (or undoing a delayed allocation). |
| 3031 | */ |
| 3032 | STATIC int /* error */ |
| 3033 | xfs_bmap_del_extent( |
| 3034 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3035 | xfs_trans_t *tp, /* current transaction pointer */ |
| 3036 | xfs_extnum_t idx, /* extent number to update/delete */ |
| 3037 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 3038 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3039 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3041 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | int whichfork, /* data or attr fork */ |
| 3043 | int rsvd) /* OK to allocate reserved blocks */ |
| 3044 | { |
| 3045 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 3046 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 3047 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 3048 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 3049 | int delay; /* current block is delayed allocated */ |
| 3050 | int do_fx; /* free extent at end of routine */ |
| 3051 | xfs_bmbt_rec_t *ep; /* current extent entry pointer */ |
| 3052 | int error; /* error return value */ |
| 3053 | int flags; /* inode logging flags */ |
| 3054 | #ifdef XFS_BMAP_TRACE |
| 3055 | static char fname[] = "xfs_bmap_del_extent"; |
| 3056 | #endif |
| 3057 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 3058 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 3059 | int i; /* temp state */ |
| 3060 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3061 | xfs_mount_t *mp; /* mount structure */ |
| 3062 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 3063 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 3064 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | uint qfield; /* quota field to update */ |
| 3066 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 3067 | xfs_filblks_t temp2; /* for indirect length calculations */ |
| 3068 | |
| 3069 | XFS_STATS_INC(xs_del_exlist); |
| 3070 | mp = ip->i_mount; |
| 3071 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3072 | ASSERT((idx >= 0) && (idx < ifp->if_bytes / |
| 3073 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | ASSERT(del->br_blockcount > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3075 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | xfs_bmbt_get_all(ep, &got); |
| 3077 | ASSERT(got.br_startoff <= del->br_startoff); |
| 3078 | del_endoff = del->br_startoff + del->br_blockcount; |
| 3079 | got_endoff = got.br_startoff + got.br_blockcount; |
| 3080 | ASSERT(got_endoff >= del_endoff); |
| 3081 | delay = ISNULLSTARTBLOCK(got.br_startblock); |
| 3082 | ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay); |
| 3083 | flags = 0; |
| 3084 | qfield = 0; |
| 3085 | error = 0; |
| 3086 | /* |
| 3087 | * If deleting a real allocation, must free up the disk space. |
| 3088 | */ |
| 3089 | if (!delay) { |
| 3090 | flags = XFS_ILOG_CORE; |
| 3091 | /* |
| 3092 | * Realtime allocation. Free it and record di_nblocks update. |
| 3093 | */ |
| 3094 | if (whichfork == XFS_DATA_FORK && |
| 3095 | (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) { |
| 3096 | xfs_fsblock_t bno; |
| 3097 | xfs_filblks_t len; |
| 3098 | |
| 3099 | ASSERT(do_mod(del->br_blockcount, |
| 3100 | mp->m_sb.sb_rextsize) == 0); |
| 3101 | ASSERT(do_mod(del->br_startblock, |
| 3102 | mp->m_sb.sb_rextsize) == 0); |
| 3103 | bno = del->br_startblock; |
| 3104 | len = del->br_blockcount; |
| 3105 | do_div(bno, mp->m_sb.sb_rextsize); |
| 3106 | do_div(len, mp->m_sb.sb_rextsize); |
| 3107 | if ((error = xfs_rtfree_extent(ip->i_transp, bno, |
| 3108 | (xfs_extlen_t)len))) |
| 3109 | goto done; |
| 3110 | do_fx = 0; |
| 3111 | nblks = len * mp->m_sb.sb_rextsize; |
| 3112 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 3113 | } |
| 3114 | /* |
| 3115 | * Ordinary allocation. |
| 3116 | */ |
| 3117 | else { |
| 3118 | do_fx = 1; |
| 3119 | nblks = del->br_blockcount; |
| 3120 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 3121 | } |
| 3122 | /* |
| 3123 | * Set up del_endblock and cur for later. |
| 3124 | */ |
| 3125 | del_endblock = del->br_startblock + del->br_blockcount; |
| 3126 | if (cur) { |
| 3127 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 3128 | got.br_startblock, got.br_blockcount, |
| 3129 | &i))) |
| 3130 | goto done; |
| 3131 | ASSERT(i == 1); |
| 3132 | } |
| 3133 | da_old = da_new = 0; |
| 3134 | } else { |
| 3135 | da_old = STARTBLOCKVAL(got.br_startblock); |
| 3136 | da_new = 0; |
| 3137 | nblks = 0; |
| 3138 | do_fx = 0; |
| 3139 | } |
| 3140 | /* |
| 3141 | * Set flag value to use in switch statement. |
| 3142 | * Left-contig is 2, right-contig is 1. |
| 3143 | */ |
| 3144 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 3145 | (got_endoff == del_endoff)) { |
| 3146 | case 3: |
| 3147 | /* |
| 3148 | * Matches the whole extent. Delete the entry. |
| 3149 | */ |
| 3150 | xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3151 | xfs_iext_remove(ifp, idx, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | ifp->if_lastex = idx; |
| 3153 | if (delay) |
| 3154 | break; |
| 3155 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3156 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 3157 | flags |= XFS_ILOG_CORE; |
| 3158 | if (!cur) { |
| 3159 | flags |= XFS_ILOG_FEXT(whichfork); |
| 3160 | break; |
| 3161 | } |
| 3162 | if ((error = xfs_bmbt_delete(cur, &i))) |
| 3163 | goto done; |
| 3164 | ASSERT(i == 1); |
| 3165 | break; |
| 3166 | |
| 3167 | case 2: |
| 3168 | /* |
| 3169 | * Deleting the first part of the extent. |
| 3170 | */ |
| 3171 | xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork); |
| 3172 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 3173 | temp = got.br_blockcount - del->br_blockcount; |
| 3174 | xfs_bmbt_set_blockcount(ep, temp); |
| 3175 | ifp->if_lastex = idx; |
| 3176 | if (delay) { |
| 3177 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3178 | da_old); |
| 3179 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 3180 | xfs_bmap_trace_post_update(fname, "2", ip, idx, |
| 3181 | whichfork); |
| 3182 | da_new = temp; |
| 3183 | break; |
| 3184 | } |
| 3185 | xfs_bmbt_set_startblock(ep, del_endblock); |
| 3186 | xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork); |
| 3187 | if (!cur) { |
| 3188 | flags |= XFS_ILOG_FEXT(whichfork); |
| 3189 | break; |
| 3190 | } |
| 3191 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 3192 | got.br_blockcount - del->br_blockcount, |
| 3193 | got.br_state))) |
| 3194 | goto done; |
| 3195 | break; |
| 3196 | |
| 3197 | case 1: |
| 3198 | /* |
| 3199 | * Deleting the last part of the extent. |
| 3200 | */ |
| 3201 | temp = got.br_blockcount - del->br_blockcount; |
| 3202 | xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork); |
| 3203 | xfs_bmbt_set_blockcount(ep, temp); |
| 3204 | ifp->if_lastex = idx; |
| 3205 | if (delay) { |
| 3206 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3207 | da_old); |
| 3208 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 3209 | xfs_bmap_trace_post_update(fname, "1", ip, idx, |
| 3210 | whichfork); |
| 3211 | da_new = temp; |
| 3212 | break; |
| 3213 | } |
| 3214 | xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork); |
| 3215 | if (!cur) { |
| 3216 | flags |= XFS_ILOG_FEXT(whichfork); |
| 3217 | break; |
| 3218 | } |
| 3219 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 3220 | got.br_startblock, |
| 3221 | got.br_blockcount - del->br_blockcount, |
| 3222 | got.br_state))) |
| 3223 | goto done; |
| 3224 | break; |
| 3225 | |
| 3226 | case 0: |
| 3227 | /* |
| 3228 | * Deleting the middle of the extent. |
| 3229 | */ |
| 3230 | temp = del->br_startoff - got.br_startoff; |
| 3231 | xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork); |
| 3232 | xfs_bmbt_set_blockcount(ep, temp); |
| 3233 | new.br_startoff = del_endoff; |
| 3234 | temp2 = got_endoff - del_endoff; |
| 3235 | new.br_blockcount = temp2; |
| 3236 | new.br_state = got.br_state; |
| 3237 | if (!delay) { |
| 3238 | new.br_startblock = del_endblock; |
| 3239 | flags |= XFS_ILOG_CORE; |
| 3240 | if (cur) { |
| 3241 | if ((error = xfs_bmbt_update(cur, |
| 3242 | got.br_startoff, |
| 3243 | got.br_startblock, temp, |
| 3244 | got.br_state))) |
| 3245 | goto done; |
| 3246 | if ((error = xfs_bmbt_increment(cur, 0, &i))) |
| 3247 | goto done; |
| 3248 | cur->bc_rec.b = new; |
| 3249 | error = xfs_bmbt_insert(cur, &i); |
| 3250 | if (error && error != ENOSPC) |
| 3251 | goto done; |
| 3252 | /* |
| 3253 | * If get no-space back from btree insert, |
| 3254 | * it tried a split, and we have a zero |
| 3255 | * block reservation. |
| 3256 | * Fix up our state and return the error. |
| 3257 | */ |
| 3258 | if (error == ENOSPC) { |
| 3259 | /* |
| 3260 | * Reset the cursor, don't trust |
| 3261 | * it after any insert operation. |
| 3262 | */ |
| 3263 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 3264 | got.br_startoff, |
| 3265 | got.br_startblock, |
| 3266 | temp, &i))) |
| 3267 | goto done; |
| 3268 | ASSERT(i == 1); |
| 3269 | /* |
| 3270 | * Update the btree record back |
| 3271 | * to the original value. |
| 3272 | */ |
| 3273 | if ((error = xfs_bmbt_update(cur, |
| 3274 | got.br_startoff, |
| 3275 | got.br_startblock, |
| 3276 | got.br_blockcount, |
| 3277 | got.br_state))) |
| 3278 | goto done; |
| 3279 | /* |
| 3280 | * Reset the extent record back |
| 3281 | * to the original value. |
| 3282 | */ |
| 3283 | xfs_bmbt_set_blockcount(ep, |
| 3284 | got.br_blockcount); |
| 3285 | flags = 0; |
| 3286 | error = XFS_ERROR(ENOSPC); |
| 3287 | goto done; |
| 3288 | } |
| 3289 | ASSERT(i == 1); |
| 3290 | } else |
| 3291 | flags |= XFS_ILOG_FEXT(whichfork); |
| 3292 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3293 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3294 | } else { |
| 3295 | ASSERT(whichfork == XFS_DATA_FORK); |
| 3296 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 3297 | xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp)); |
| 3298 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
| 3299 | new.br_startblock = NULLSTARTBLOCK((int)temp2); |
| 3300 | da_new = temp + temp2; |
| 3301 | while (da_new > da_old) { |
| 3302 | if (temp) { |
| 3303 | temp--; |
| 3304 | da_new--; |
| 3305 | xfs_bmbt_set_startblock(ep, |
| 3306 | NULLSTARTBLOCK((int)temp)); |
| 3307 | } |
| 3308 | if (da_new == da_old) |
| 3309 | break; |
| 3310 | if (temp2) { |
| 3311 | temp2--; |
| 3312 | da_new--; |
| 3313 | new.br_startblock = |
| 3314 | NULLSTARTBLOCK((int)temp2); |
| 3315 | } |
| 3316 | } |
| 3317 | } |
| 3318 | xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork); |
| 3319 | xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL, |
| 3320 | whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3321 | xfs_iext_insert(ifp, idx + 1, 1, &new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | ifp->if_lastex = idx + 1; |
| 3323 | break; |
| 3324 | } |
| 3325 | /* |
| 3326 | * If we need to, add to list of extents to delete. |
| 3327 | */ |
| 3328 | if (do_fx) |
| 3329 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 3330 | mp); |
| 3331 | /* |
| 3332 | * Adjust inode # blocks in the file. |
| 3333 | */ |
| 3334 | if (nblks) |
| 3335 | ip->i_d.di_nblocks -= nblks; |
| 3336 | /* |
| 3337 | * Adjust quota data. |
| 3338 | */ |
| 3339 | if (qfield) |
| 3340 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks); |
| 3341 | |
| 3342 | /* |
| 3343 | * Account for change in delayed indirect blocks. |
| 3344 | * Nothing to do for disk quota accounting here. |
| 3345 | */ |
| 3346 | ASSERT(da_old >= da_new); |
| 3347 | if (da_old > da_new) |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 3348 | 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] | 3349 | rsvd); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3350 | if (delta) { |
| 3351 | /* DELTA: report the original extent. */ |
| 3352 | if (delta->xed_startoff > got.br_startoff) |
| 3353 | delta->xed_startoff = got.br_startoff; |
| 3354 | if (delta->xed_blockcount < got.br_startoff+got.br_blockcount) |
| 3355 | delta->xed_blockcount = got.br_startoff + |
| 3356 | got.br_blockcount; |
| 3357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | done: |
| 3359 | *logflagsp = flags; |
| 3360 | return error; |
| 3361 | } |
| 3362 | |
| 3363 | /* |
| 3364 | * Remove the entry "free" from the free item list. Prev points to the |
| 3365 | * previous entry, unless "free" is the head of the list. |
| 3366 | */ |
| 3367 | STATIC void |
| 3368 | xfs_bmap_del_free( |
| 3369 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3370 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3371 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3372 | { |
| 3373 | if (prev) |
| 3374 | prev->xbfi_next = free->xbfi_next; |
| 3375 | else |
| 3376 | flist->xbf_first = free->xbfi_next; |
| 3377 | flist->xbf_count--; |
| 3378 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3379 | } |
| 3380 | |
| 3381 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | * Convert an extents-format file into a btree-format file. |
| 3383 | * The new file will have a root block (in the inode) and a single child block. |
| 3384 | */ |
| 3385 | STATIC int /* error */ |
| 3386 | xfs_bmap_extents_to_btree( |
| 3387 | xfs_trans_t *tp, /* transaction pointer */ |
| 3388 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3389 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3390 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3391 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3392 | int wasdel, /* converting a delayed alloc */ |
| 3393 | int *logflagsp, /* inode logging flags */ |
| 3394 | int whichfork) /* data or attr fork */ |
| 3395 | { |
| 3396 | xfs_bmbt_block_t *ablock; /* allocated (child) bt block */ |
| 3397 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3398 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3399 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
| 3400 | xfs_bmbt_block_t *block; /* btree root block */ |
| 3401 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3402 | xfs_bmbt_rec_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3404 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3406 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3407 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3408 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3410 | |
| 3411 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3412 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3413 | ASSERT(ifp->if_ext_max == |
| 3414 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3415 | /* |
| 3416 | * Make space in the inode incore. |
| 3417 | */ |
| 3418 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3419 | ifp->if_flags |= XFS_IFBROOT; |
| 3420 | /* |
| 3421 | * Fill in the root. |
| 3422 | */ |
| 3423 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3424 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3425 | block->bb_level = cpu_to_be16(1); |
| 3426 | block->bb_numrecs = cpu_to_be16(1); |
| 3427 | block->bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3428 | block->bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | /* |
| 3430 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3431 | */ |
| 3432 | mp = ip->i_mount; |
| 3433 | cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip, |
| 3434 | whichfork); |
| 3435 | cur->bc_private.b.firstblock = *firstblock; |
| 3436 | cur->bc_private.b.flist = flist; |
| 3437 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3438 | /* |
| 3439 | * Convert to a btree with two levels, one record in root. |
| 3440 | */ |
| 3441 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3442 | args.tp = tp; |
| 3443 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3444 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | if (*firstblock == NULLFSBLOCK) { |
| 3446 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3447 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3448 | } else if (flist->xbf_low) { |
| 3449 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3450 | args.fsbno = *firstblock; |
| 3451 | } else { |
| 3452 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3453 | args.fsbno = *firstblock; |
| 3454 | } |
| 3455 | args.minlen = args.maxlen = args.prod = 1; |
| 3456 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3457 | args.minalignslop = 0; |
| 3458 | args.wasdel = wasdel; |
| 3459 | *logflagsp = 0; |
| 3460 | if ((error = xfs_alloc_vextent(&args))) { |
| 3461 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3462 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3463 | return error; |
| 3464 | } |
| 3465 | /* |
| 3466 | * Allocation can't fail, the space was reserved. |
| 3467 | */ |
| 3468 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3469 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3470 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3471 | (flist->xbf_low && |
| 3472 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3473 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3474 | cur->bc_private.b.allocated++; |
| 3475 | ip->i_d.di_nblocks++; |
| 3476 | XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
| 3477 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3478 | /* |
| 3479 | * Fill in the child block. |
| 3480 | */ |
| 3481 | ablock = XFS_BUF_TO_BMBT_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3482 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | ablock->bb_level = 0; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3484 | ablock->bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3485 | ablock->bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | arp = XFS_BMAP_REC_IADDR(ablock, 1, cur); |
| 3487 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3488 | for (cnt = i = 0; i < nextents; i++) { |
| 3489 | ep = xfs_iext_get_ext(ifp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) { |
| 3491 | arp->l0 = INT_GET(ep->l0, ARCH_CONVERT); |
| 3492 | arp->l1 = INT_GET(ep->l1, ARCH_CONVERT); |
| 3493 | arp++; cnt++; |
| 3494 | } |
| 3495 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3496 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 3497 | ablock->bb_numrecs = cpu_to_be16(cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | /* |
| 3499 | * Fill in the root key and pointer. |
| 3500 | */ |
| 3501 | kp = XFS_BMAP_KEY_IADDR(block, 1, cur); |
| 3502 | arp = XFS_BMAP_REC_IADDR(ablock, 1, cur); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3503 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | pp = XFS_BMAP_PTR_IADDR(block, 1, cur); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3505 | *pp = cpu_to_be64(args.fsbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | /* |
| 3507 | * Do all this logging at the end so that |
| 3508 | * the root is at the right level. |
| 3509 | */ |
| 3510 | xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3511 | xfs_bmbt_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | ASSERT(*curp == NULL); |
| 3513 | *curp = cur; |
| 3514 | *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork); |
| 3515 | return 0; |
| 3516 | } |
| 3517 | |
| 3518 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3519 | * Helper routine to reset inode di_forkoff field when switching |
| 3520 | * attribute fork from local to extent format - we reset it where |
| 3521 | * possible to make space available for inline data fork extents. |
| 3522 | */ |
| 3523 | STATIC void |
| 3524 | xfs_bmap_forkoff_reset( |
| 3525 | xfs_mount_t *mp, |
| 3526 | xfs_inode_t *ip, |
| 3527 | int whichfork) |
| 3528 | { |
| 3529 | if (whichfork == XFS_ATTR_FORK && |
| 3530 | (ip->i_d.di_format != XFS_DINODE_FMT_DEV) && |
| 3531 | (ip->i_d.di_format != XFS_DINODE_FMT_UUID) && |
Barry Naujok | e5889e9 | 2007-02-10 18:35:58 +1100 | [diff] [blame] | 3532 | (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) && |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3533 | ((mp->m_attroffset >> 3) > ip->i_d.di_forkoff)) { |
| 3534 | ip->i_d.di_forkoff = mp->m_attroffset >> 3; |
| 3535 | ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) / |
| 3536 | (uint)sizeof(xfs_bmbt_rec_t); |
| 3537 | ip->i_afp->if_ext_max = XFS_IFORK_ASIZE(ip) / |
| 3538 | (uint)sizeof(xfs_bmbt_rec_t); |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | * Convert a local file to an extents file. |
| 3544 | * This code is out of bounds for data forks of regular files, |
| 3545 | * since the file data needs to get logged so things will stay consistent. |
| 3546 | * (The bmap-level manipulations are ok, though). |
| 3547 | */ |
| 3548 | STATIC int /* error */ |
| 3549 | xfs_bmap_local_to_extents( |
| 3550 | xfs_trans_t *tp, /* transaction pointer */ |
| 3551 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3552 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3553 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3554 | int *logflagsp, /* inode logging flags */ |
| 3555 | int whichfork) /* data or attr fork */ |
| 3556 | { |
| 3557 | int error; /* error return value */ |
| 3558 | int flags; /* logging flags returned */ |
| 3559 | #ifdef XFS_BMAP_TRACE |
| 3560 | static char fname[] = "xfs_bmap_local_to_extents"; |
| 3561 | #endif |
| 3562 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3563 | |
| 3564 | /* |
| 3565 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3566 | * So sending the data fork of a regular inode is invalid. |
| 3567 | */ |
| 3568 | ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG && |
| 3569 | whichfork == XFS_DATA_FORK)); |
| 3570 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3571 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3572 | flags = 0; |
| 3573 | error = 0; |
| 3574 | if (ifp->if_bytes) { |
| 3575 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3576 | xfs_buf_t *bp; /* buffer for extent block */ |
| 3577 | xfs_bmbt_rec_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | |
| 3579 | args.tp = tp; |
| 3580 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3581 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3582 | ASSERT((ifp->if_flags & |
| 3583 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | /* |
| 3585 | * Allocate a block. We know we need only one, since the |
| 3586 | * file currently fits in an inode. |
| 3587 | */ |
| 3588 | if (*firstblock == NULLFSBLOCK) { |
| 3589 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3590 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3591 | } else { |
| 3592 | args.fsbno = *firstblock; |
| 3593 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3594 | } |
| 3595 | args.total = total; |
| 3596 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3597 | args.isfl = args.minalignslop = 0; |
| 3598 | args.minlen = args.maxlen = args.prod = 1; |
| 3599 | if ((error = xfs_alloc_vextent(&args))) |
| 3600 | goto done; |
| 3601 | /* |
| 3602 | * Can't fail, the space was reserved. |
| 3603 | */ |
| 3604 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3605 | ASSERT(args.len == 1); |
| 3606 | *firstblock = args.fsbno; |
| 3607 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 3608 | memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data, |
| 3609 | ifp->if_bytes); |
| 3610 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3611 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3613 | xfs_iext_add(ifp, 0, 1); |
| 3614 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
| 3616 | xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork); |
| 3617 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3618 | ip->i_d.di_nblocks = 1; |
| 3619 | XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip, |
| 3620 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 3621 | flags |= XFS_ILOG_FEXT(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3622 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3624 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3627 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3628 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3629 | flags |= XFS_ILOG_CORE; |
| 3630 | done: |
| 3631 | *logflagsp = flags; |
| 3632 | return error; |
| 3633 | } |
| 3634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3636 | * Search the extent records for the entry containing block bno. |
| 3637 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3638 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3639 | * entry (null if none). Else, *lastxp will be set to the index |
| 3640 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3641 | */ |
| 3642 | xfs_bmbt_rec_t * /* pointer to found extent entry */ |
| 3643 | xfs_bmap_search_multi_extents( |
| 3644 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3645 | xfs_fileoff_t bno, /* block number searched for */ |
| 3646 | int *eofp, /* out: end of file found */ |
| 3647 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3648 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3649 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3650 | { |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3651 | xfs_bmbt_rec_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3652 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3653 | |
| 3654 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3655 | * Initialize the extent entry structure to catch access to |
| 3656 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3657 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3658 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3659 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3660 | gotp->br_state = XFS_EXT_INVALID; |
| 3661 | #if XFS_BIG_BLKNOS |
| 3662 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3663 | #else |
| 3664 | gotp->br_startblock = 0xffffa5a5; |
| 3665 | #endif |
| 3666 | prevp->br_startoff = NULLFILEOFF; |
| 3667 | |
| 3668 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3669 | if (lastx > 0) { |
| 3670 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3671 | } |
| 3672 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3673 | xfs_bmbt_get_all(ep, gotp); |
| 3674 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3675 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3676 | if (lastx > 0) { |
| 3677 | *gotp = *prevp; |
| 3678 | } |
| 3679 | *eofp = 1; |
| 3680 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3681 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3682 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3683 | return ep; |
| 3684 | } |
| 3685 | |
| 3686 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3687 | * Search the extents list for the inode, for the extent containing bno. |
| 3688 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3689 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3690 | * Else, *lastxp will be set to the index of the found |
| 3691 | * entry; *gotp will contain the entry. |
| 3692 | */ |
| 3693 | STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */ |
| 3694 | xfs_bmap_search_extents( |
| 3695 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3696 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3697 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | int *eofp, /* out: end of file found */ |
| 3699 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3700 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3701 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3702 | { |
| 3703 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3704 | xfs_bmbt_rec_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | |
| 3706 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3707 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3708 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3709 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3710 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3711 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3712 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 3713 | xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount, |
| 3714 | "Access to block zero in inode %llu " |
| 3715 | "start_block: %llx start_off: %llx " |
| 3716 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3717 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3718 | (unsigned long long)gotp->br_startblock, |
| 3719 | (unsigned long long)gotp->br_startoff, |
| 3720 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3721 | gotp->br_state, *lastxp); |
| 3722 | *lastxp = NULLEXTNUM; |
| 3723 | *eofp = 1; |
| 3724 | return NULL; |
| 3725 | } |
| 3726 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | |
| 3730 | #ifdef XFS_BMAP_TRACE |
| 3731 | ktrace_t *xfs_bmap_trace_buf; |
| 3732 | |
| 3733 | /* |
| 3734 | * Add a bmap trace buffer entry. Base routine for the others. |
| 3735 | */ |
| 3736 | STATIC void |
| 3737 | xfs_bmap_trace_addentry( |
| 3738 | int opcode, /* operation */ |
| 3739 | char *fname, /* function name */ |
| 3740 | char *desc, /* operation description */ |
| 3741 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3742 | xfs_extnum_t idx, /* index of entry(ies) */ |
| 3743 | xfs_extnum_t cnt, /* count of entries, 1 or 2 */ |
| 3744 | xfs_bmbt_rec_t *r1, /* first record */ |
| 3745 | xfs_bmbt_rec_t *r2, /* second record or null */ |
| 3746 | int whichfork) /* data or attr fork */ |
| 3747 | { |
| 3748 | xfs_bmbt_rec_t tr2; |
| 3749 | |
| 3750 | ASSERT(cnt == 1 || cnt == 2); |
| 3751 | ASSERT(r1 != NULL); |
| 3752 | if (cnt == 1) { |
| 3753 | ASSERT(r2 == NULL); |
| 3754 | r2 = &tr2; |
| 3755 | memset(&tr2, 0, sizeof(tr2)); |
| 3756 | } else |
| 3757 | ASSERT(r2 != NULL); |
| 3758 | ktrace_enter(xfs_bmap_trace_buf, |
| 3759 | (void *)(__psint_t)(opcode | (whichfork << 16)), |
| 3760 | (void *)fname, (void *)desc, (void *)ip, |
| 3761 | (void *)(__psint_t)idx, |
| 3762 | (void *)(__psint_t)cnt, |
| 3763 | (void *)(__psunsigned_t)(ip->i_ino >> 32), |
| 3764 | (void *)(__psunsigned_t)(unsigned)ip->i_ino, |
| 3765 | (void *)(__psunsigned_t)(r1->l0 >> 32), |
| 3766 | (void *)(__psunsigned_t)(unsigned)(r1->l0), |
| 3767 | (void *)(__psunsigned_t)(r1->l1 >> 32), |
| 3768 | (void *)(__psunsigned_t)(unsigned)(r1->l1), |
| 3769 | (void *)(__psunsigned_t)(r2->l0 >> 32), |
| 3770 | (void *)(__psunsigned_t)(unsigned)(r2->l0), |
| 3771 | (void *)(__psunsigned_t)(r2->l1 >> 32), |
| 3772 | (void *)(__psunsigned_t)(unsigned)(r2->l1) |
| 3773 | ); |
| 3774 | ASSERT(ip->i_xtrace); |
| 3775 | ktrace_enter(ip->i_xtrace, |
| 3776 | (void *)(__psint_t)(opcode | (whichfork << 16)), |
| 3777 | (void *)fname, (void *)desc, (void *)ip, |
| 3778 | (void *)(__psint_t)idx, |
| 3779 | (void *)(__psint_t)cnt, |
| 3780 | (void *)(__psunsigned_t)(ip->i_ino >> 32), |
| 3781 | (void *)(__psunsigned_t)(unsigned)ip->i_ino, |
| 3782 | (void *)(__psunsigned_t)(r1->l0 >> 32), |
| 3783 | (void *)(__psunsigned_t)(unsigned)(r1->l0), |
| 3784 | (void *)(__psunsigned_t)(r1->l1 >> 32), |
| 3785 | (void *)(__psunsigned_t)(unsigned)(r1->l1), |
| 3786 | (void *)(__psunsigned_t)(r2->l0 >> 32), |
| 3787 | (void *)(__psunsigned_t)(unsigned)(r2->l0), |
| 3788 | (void *)(__psunsigned_t)(r2->l1 >> 32), |
| 3789 | (void *)(__psunsigned_t)(unsigned)(r2->l1) |
| 3790 | ); |
| 3791 | } |
| 3792 | |
| 3793 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3794 | * Add bmap trace entry prior to a call to xfs_iext_remove. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | */ |
| 3796 | STATIC void |
| 3797 | xfs_bmap_trace_delete( |
| 3798 | char *fname, /* function name */ |
| 3799 | char *desc, /* operation description */ |
| 3800 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3801 | xfs_extnum_t idx, /* index of entry(entries) deleted */ |
| 3802 | xfs_extnum_t cnt, /* count of entries deleted, 1 or 2 */ |
| 3803 | int whichfork) /* data or attr fork */ |
| 3804 | { |
| 3805 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3806 | |
| 3807 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3808 | xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_DELETE, fname, desc, ip, idx, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3809 | cnt, xfs_iext_get_ext(ifp, idx), |
| 3810 | cnt == 2 ? xfs_iext_get_ext(ifp, idx + 1) : NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | whichfork); |
| 3812 | } |
| 3813 | |
| 3814 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3815 | * 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] | 3816 | * reading in the extents list from the disk (in the btree). |
| 3817 | */ |
| 3818 | STATIC void |
| 3819 | xfs_bmap_trace_insert( |
| 3820 | char *fname, /* function name */ |
| 3821 | char *desc, /* operation description */ |
| 3822 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3823 | xfs_extnum_t idx, /* index of entry(entries) inserted */ |
| 3824 | xfs_extnum_t cnt, /* count of entries inserted, 1 or 2 */ |
| 3825 | xfs_bmbt_irec_t *r1, /* inserted record 1 */ |
| 3826 | xfs_bmbt_irec_t *r2, /* inserted record 2 or null */ |
| 3827 | int whichfork) /* data or attr fork */ |
| 3828 | { |
| 3829 | xfs_bmbt_rec_t tr1; /* compressed record 1 */ |
| 3830 | xfs_bmbt_rec_t tr2; /* compressed record 2 if needed */ |
| 3831 | |
| 3832 | xfs_bmbt_set_all(&tr1, r1); |
| 3833 | if (cnt == 2) { |
| 3834 | ASSERT(r2 != NULL); |
| 3835 | xfs_bmbt_set_all(&tr2, r2); |
| 3836 | } else { |
| 3837 | ASSERT(cnt == 1); |
| 3838 | ASSERT(r2 == NULL); |
| 3839 | } |
| 3840 | xfs_bmap_trace_addentry(XFS_BMAP_KTRACE_INSERT, fname, desc, ip, idx, |
| 3841 | cnt, &tr1, cnt == 2 ? &tr2 : NULL, whichfork); |
| 3842 | } |
| 3843 | |
| 3844 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3845 | * Add bmap trace entry after updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | */ |
| 3847 | STATIC void |
| 3848 | xfs_bmap_trace_post_update( |
| 3849 | char *fname, /* function name */ |
| 3850 | char *desc, /* operation description */ |
| 3851 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3852 | xfs_extnum_t idx, /* index of entry updated */ |
| 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_POST_UP, fname, desc, ip, idx, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3859 | 1, xfs_iext_get_ext(ifp, idx), NULL, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3863 | * Add bmap trace entry prior to updating an extent record in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | */ |
| 3865 | STATIC void |
| 3866 | xfs_bmap_trace_pre_update( |
| 3867 | char *fname, /* function name */ |
| 3868 | char *desc, /* operation description */ |
| 3869 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3870 | xfs_extnum_t idx, /* index of entry to be updated */ |
| 3871 | int whichfork) /* data or attr fork */ |
| 3872 | { |
| 3873 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3874 | |
| 3875 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3876 | 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] | 3877 | xfs_iext_get_ext(ifp, idx), NULL, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | } |
| 3879 | #endif /* XFS_BMAP_TRACE */ |
| 3880 | |
| 3881 | /* |
| 3882 | * Compute the worst-case number of indirect blocks that will be used |
| 3883 | * for ip's delayed extent of length "len". |
| 3884 | */ |
| 3885 | STATIC xfs_filblks_t |
| 3886 | xfs_bmap_worst_indlen( |
| 3887 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3888 | xfs_filblks_t len) /* delayed extent length */ |
| 3889 | { |
| 3890 | int level; /* btree level number */ |
| 3891 | int maxrecs; /* maximum record count at this level */ |
| 3892 | xfs_mount_t *mp; /* mount structure */ |
| 3893 | xfs_filblks_t rval; /* return value */ |
| 3894 | |
| 3895 | mp = ip->i_mount; |
| 3896 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3897 | for (level = 0, rval = 0; |
| 3898 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3899 | level++) { |
| 3900 | len += maxrecs - 1; |
| 3901 | do_div(len, maxrecs); |
| 3902 | rval += len; |
| 3903 | if (len == 1) |
| 3904 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3905 | level - 1; |
| 3906 | if (level == 0) |
| 3907 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3908 | } |
| 3909 | return rval; |
| 3910 | } |
| 3911 | |
| 3912 | #if defined(XFS_RW_TRACE) |
| 3913 | STATIC void |
| 3914 | xfs_bunmap_trace( |
| 3915 | xfs_inode_t *ip, |
| 3916 | xfs_fileoff_t bno, |
| 3917 | xfs_filblks_t len, |
| 3918 | int flags, |
| 3919 | inst_t *ra) |
| 3920 | { |
| 3921 | if (ip->i_rwtrace == NULL) |
| 3922 | return; |
| 3923 | ktrace_enter(ip->i_rwtrace, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3924 | (void *)(__psint_t)XFS_BUNMAP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | (void *)ip, |
| 3926 | (void *)(__psint_t)((ip->i_d.di_size >> 32) & 0xffffffff), |
| 3927 | (void *)(__psint_t)(ip->i_d.di_size & 0xffffffff), |
| 3928 | (void *)(__psint_t)(((xfs_dfiloff_t)bno >> 32) & 0xffffffff), |
| 3929 | (void *)(__psint_t)((xfs_dfiloff_t)bno & 0xffffffff), |
| 3930 | (void *)(__psint_t)len, |
| 3931 | (void *)(__psint_t)flags, |
| 3932 | (void *)(unsigned long)current_cpu(), |
| 3933 | (void *)ra, |
| 3934 | (void *)0, |
| 3935 | (void *)0, |
| 3936 | (void *)0, |
| 3937 | (void *)0, |
| 3938 | (void *)0, |
| 3939 | (void *)0); |
| 3940 | } |
| 3941 | #endif |
| 3942 | |
| 3943 | /* |
| 3944 | * Convert inode from non-attributed to attributed. |
| 3945 | * Must not be in a transaction, ip must not be locked. |
| 3946 | */ |
| 3947 | int /* error code */ |
| 3948 | xfs_bmap_add_attrfork( |
| 3949 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3950 | int size, /* space new attribute needs */ |
| 3951 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3954 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3957 | unsigned long s; /* spinlock spl value */ |
| 3958 | int blks; /* space reservation */ |
| 3959 | int version = 1; /* superblock attr version */ |
| 3960 | int committed; /* xaction was committed */ |
| 3961 | int logflags; /* logging flags */ |
| 3962 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3964 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | ASSERT(ip->i_df.if_ext_max == |
| 3966 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | mp = ip->i_mount; |
| 3969 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3970 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3971 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3972 | if (rsvd) |
| 3973 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3974 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3975 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3976 | goto error0; |
| 3977 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 3978 | error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip, blks, 0, rsvd ? |
| 3979 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3980 | XFS_QMOPT_RES_REGBLKS); |
| 3981 | if (error) { |
| 3982 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3983 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3984 | return error; |
| 3985 | } |
| 3986 | if (XFS_IFORK_Q(ip)) |
| 3987 | goto error1; |
| 3988 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3989 | /* |
| 3990 | * For inodes coming from pre-6.2 filesystems. |
| 3991 | */ |
| 3992 | ASSERT(ip->i_d.di_aformat == 0); |
| 3993 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3994 | } |
| 3995 | ASSERT(ip->i_d.di_anextents == 0); |
| 3996 | VN_HOLD(XFS_ITOV(ip)); |
| 3997 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3998 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3999 | switch (ip->i_d.di_format) { |
| 4000 | case XFS_DINODE_FMT_DEV: |
| 4001 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 4002 | break; |
| 4003 | case XFS_DINODE_FMT_UUID: |
| 4004 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 4005 | break; |
| 4006 | case XFS_DINODE_FMT_LOCAL: |
| 4007 | case XFS_DINODE_FMT_EXTENTS: |
| 4008 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4009 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 4010 | if (!ip->i_d.di_forkoff) |
| 4011 | ip->i_d.di_forkoff = mp->m_attroffset >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 4012 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4013 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | break; |
| 4015 | default: |
| 4016 | ASSERT(0); |
| 4017 | error = XFS_ERROR(EINVAL); |
| 4018 | goto error1; |
| 4019 | } |
| 4020 | ip->i_df.if_ext_max = |
| 4021 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 4022 | ASSERT(ip->i_afp == NULL); |
| 4023 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 4024 | ip->i_afp->if_ext_max = |
| 4025 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 4026 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 4027 | logflags = 0; |
| 4028 | XFS_BMAP_INIT(&flist, &firstblock); |
| 4029 | switch (ip->i_d.di_format) { |
| 4030 | case XFS_DINODE_FMT_LOCAL: |
| 4031 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 4032 | &logflags); |
| 4033 | break; |
| 4034 | case XFS_DINODE_FMT_EXTENTS: |
| 4035 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 4036 | &flist, &logflags); |
| 4037 | break; |
| 4038 | case XFS_DINODE_FMT_BTREE: |
| 4039 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 4040 | &logflags); |
| 4041 | break; |
| 4042 | default: |
| 4043 | error = 0; |
| 4044 | break; |
| 4045 | } |
| 4046 | if (logflags) |
| 4047 | xfs_trans_log_inode(tp, ip, logflags); |
| 4048 | if (error) |
| 4049 | goto error2; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4050 | if (!XFS_SB_VERSION_HASATTR(&mp->m_sb) || |
| 4051 | (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4052 | __int64_t sbfields = 0; |
| 4053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | s = XFS_SB_LOCK(mp); |
| 4055 | if (!XFS_SB_VERSION_HASATTR(&mp->m_sb)) { |
| 4056 | XFS_SB_VERSION_ADDATTR(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4057 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4058 | } |
| 4059 | if (!XFS_SB_VERSION_HASATTR2(&mp->m_sb) && version == 2) { |
| 4060 | XFS_SB_VERSION_ADDATTR2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4061 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4062 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4063 | if (sbfields) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | XFS_SB_UNLOCK(mp, s); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 4065 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | } else |
| 4067 | XFS_SB_UNLOCK(mp, s); |
| 4068 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 4069 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | goto error2; |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4071 | error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | ASSERT(ip->i_df.if_ext_max == |
| 4073 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4074 | return error; |
| 4075 | error2: |
| 4076 | xfs_bmap_cancel(&flist); |
| 4077 | error1: |
| 4078 | ASSERT(ismrlocked(&ip->i_lock,MR_UPDATE)); |
| 4079 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 4080 | error0: |
| 4081 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 4082 | ASSERT(ip->i_df.if_ext_max == |
| 4083 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4084 | return error; |
| 4085 | } |
| 4086 | |
| 4087 | /* |
| 4088 | * Add the extent to the list of extents to be free at transaction end. |
| 4089 | * The list is maintained sorted (by block number). |
| 4090 | */ |
| 4091 | /* ARGSUSED */ |
| 4092 | void |
| 4093 | xfs_bmap_add_free( |
| 4094 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 4095 | xfs_filblks_t len, /* length of extent */ |
| 4096 | xfs_bmap_free_t *flist, /* list of extents */ |
| 4097 | xfs_mount_t *mp) /* mount point structure */ |
| 4098 | { |
| 4099 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 4100 | xfs_bmap_free_item_t *new; /* new element */ |
| 4101 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 4102 | #ifdef DEBUG |
| 4103 | xfs_agnumber_t agno; |
| 4104 | xfs_agblock_t agbno; |
| 4105 | |
| 4106 | ASSERT(bno != NULLFSBLOCK); |
| 4107 | ASSERT(len > 0); |
| 4108 | ASSERT(len <= MAXEXTLEN); |
| 4109 | ASSERT(!ISNULLSTARTBLOCK(bno)); |
| 4110 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 4111 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 4112 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 4113 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 4114 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 4115 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 4116 | #endif |
| 4117 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 4118 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 4119 | new->xbfi_startblock = bno; |
| 4120 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 4121 | for (prev = NULL, cur = flist->xbf_first; |
| 4122 | cur != NULL; |
| 4123 | prev = cur, cur = cur->xbfi_next) { |
| 4124 | if (cur->xbfi_startblock >= bno) |
| 4125 | break; |
| 4126 | } |
| 4127 | if (prev) |
| 4128 | prev->xbfi_next = new; |
| 4129 | else |
| 4130 | flist->xbf_first = new; |
| 4131 | new->xbfi_next = cur; |
| 4132 | flist->xbf_count++; |
| 4133 | } |
| 4134 | |
| 4135 | /* |
| 4136 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 4137 | * in this filesystem. Done once, during mount. |
| 4138 | */ |
| 4139 | void |
| 4140 | xfs_bmap_compute_maxlevels( |
| 4141 | xfs_mount_t *mp, /* file system mount structure */ |
| 4142 | int whichfork) /* data or attr fork */ |
| 4143 | { |
| 4144 | int level; /* btree level */ |
| 4145 | uint maxblocks; /* max blocks at this level */ |
| 4146 | uint maxleafents; /* max leaf entries possible */ |
| 4147 | int maxrootrecs; /* max records in root block */ |
| 4148 | int minleafrecs; /* min records in leaf block */ |
| 4149 | int minnoderecs; /* min records in node block */ |
| 4150 | int sz; /* root block size */ |
| 4151 | |
| 4152 | /* |
| 4153 | * The maximum number of extents in a file, hence the maximum |
| 4154 | * number of leaf entries, is controlled by the type of di_nextents |
| 4155 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 4156 | * (a signed 16-bit number, xfs_aextnum_t). |
| 4157 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4158 | if (whichfork == XFS_DATA_FORK) { |
| 4159 | maxleafents = MAXEXTNUM; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 4160 | sz = (mp->m_flags & XFS_MOUNT_ATTR2) ? |
| 4161 | XFS_BMDR_SPACE_CALC(MINDBTPTRS) : mp->m_attroffset; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4162 | } else { |
| 4163 | maxleafents = MAXAEXTNUM; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 4164 | sz = (mp->m_flags & XFS_MOUNT_ATTR2) ? |
| 4165 | XFS_BMDR_SPACE_CALC(MINABTPTRS) : |
| 4166 | mp->m_sb.sb_inodesize - mp->m_attroffset; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 4167 | } |
| 4168 | maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 4170 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 4172 | for (level = 1; maxblocks > 1; level++) { |
| 4173 | if (maxblocks <= maxrootrecs) |
| 4174 | maxblocks = 1; |
| 4175 | else |
| 4176 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 4177 | } |
| 4178 | mp->m_bm_maxlevels[whichfork] = level; |
| 4179 | } |
| 4180 | |
| 4181 | /* |
| 4182 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 4183 | * caller. Frees all the extents that need freeing, which must be done |
| 4184 | * last due to locking considerations. We never free any extents in |
| 4185 | * the first transaction. This is to allow the caller to make the first |
| 4186 | * transaction a synchronous one so that the pointers to the data being |
| 4187 | * broken in this transaction will be permanent before the data is actually |
| 4188 | * freed. This is necessary to prevent blocks from being reallocated |
| 4189 | * and written to before the free and reallocation are actually permanent. |
| 4190 | * We do not just make the first transaction synchronous here, because |
| 4191 | * there are more efficient ways to gain the same protection in some cases |
| 4192 | * (see the file truncation code). |
| 4193 | * |
| 4194 | * Return 1 if the given transaction was committed and a new one |
| 4195 | * started, and 0 otherwise in the committed parameter. |
| 4196 | */ |
| 4197 | /*ARGSUSED*/ |
| 4198 | int /* error */ |
| 4199 | xfs_bmap_finish( |
| 4200 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 4201 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | int *committed) /* xact committed or not */ |
| 4203 | { |
| 4204 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 4205 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 4206 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4207 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | unsigned int logres; /* new log reservation */ |
| 4209 | unsigned int logcount; /* new log count */ |
| 4210 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 4211 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 4212 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 4213 | |
| 4214 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 4215 | if (flist->xbf_count == 0) { |
| 4216 | *committed = 0; |
| 4217 | return 0; |
| 4218 | } |
| 4219 | ntp = *tp; |
| 4220 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 4221 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 4222 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 4223 | free->xbfi_blockcount); |
| 4224 | logres = ntp->t_log_res; |
| 4225 | logcount = ntp->t_log_count; |
| 4226 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 4227 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | *tp = ntp; |
| 4229 | *committed = 1; |
| 4230 | /* |
| 4231 | * We have a new transaction, so we should return committed=1, |
| 4232 | * even though we're returning an error. |
| 4233 | */ |
| 4234 | if (error) { |
| 4235 | return error; |
| 4236 | } |
| 4237 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 4238 | logcount))) |
| 4239 | return error; |
| 4240 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 4241 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 4242 | next = free->xbfi_next; |
| 4243 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 4244 | free->xbfi_blockcount))) { |
| 4245 | /* |
| 4246 | * The bmap free list will be cleaned up at a |
| 4247 | * higher level. The EFI will be canceled when |
| 4248 | * this transaction is aborted. |
| 4249 | * Need to force shutdown here to make sure it |
| 4250 | * happens, since this transaction may not be |
| 4251 | * dirty yet. |
| 4252 | */ |
| 4253 | mp = ntp->t_mountp; |
| 4254 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 4255 | xfs_force_shutdown(mp, |
| 4256 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 4257 | SHUTDOWN_CORRUPT_INCORE : |
| 4258 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | return error; |
| 4260 | } |
| 4261 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 4262 | free->xbfi_blockcount); |
| 4263 | xfs_bmap_del_free(flist, NULL, free); |
| 4264 | } |
| 4265 | return 0; |
| 4266 | } |
| 4267 | |
| 4268 | /* |
| 4269 | * Free up any items left in the list. |
| 4270 | */ |
| 4271 | void |
| 4272 | xfs_bmap_cancel( |
| 4273 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 4274 | { |
| 4275 | xfs_bmap_free_item_t *free; /* free list item */ |
| 4276 | xfs_bmap_free_item_t *next; |
| 4277 | |
| 4278 | if (flist->xbf_count == 0) |
| 4279 | return; |
| 4280 | ASSERT(flist->xbf_first != NULL); |
| 4281 | for (free = flist->xbf_first; free; free = next) { |
| 4282 | next = free->xbfi_next; |
| 4283 | xfs_bmap_del_free(flist, NULL, free); |
| 4284 | } |
| 4285 | ASSERT(flist->xbf_count == 0); |
| 4286 | } |
| 4287 | |
| 4288 | /* |
| 4289 | * Returns the file-relative block number of the first unused block(s) |
| 4290 | * in the file with at least "len" logically contiguous blocks free. |
| 4291 | * This is the lowest-address hole if the file has holes, else the first block |
| 4292 | * past the end of file. |
| 4293 | * Return 0 if the file is currently local (in-inode). |
| 4294 | */ |
| 4295 | int /* error */ |
| 4296 | xfs_bmap_first_unused( |
| 4297 | xfs_trans_t *tp, /* transaction pointer */ |
| 4298 | xfs_inode_t *ip, /* incore inode */ |
| 4299 | xfs_extlen_t len, /* size of hole to find */ |
| 4300 | xfs_fileoff_t *first_unused, /* unused block */ |
| 4301 | int whichfork) /* data or attr fork */ |
| 4302 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | xfs_bmbt_rec_t *ep; /* pointer to an extent entry */ |
| 4304 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4305 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4307 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 4308 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 4309 | xfs_fileoff_t max; /* starting useful block */ |
| 4310 | xfs_fileoff_t off; /* offset for this block */ |
| 4311 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4312 | |
| 4313 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 4314 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 4315 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 4316 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4317 | *first_unused = 0; |
| 4318 | return 0; |
| 4319 | } |
| 4320 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4321 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4322 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4323 | return error; |
| 4324 | lowest = *first_unused; |
| 4325 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4326 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
| 4327 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | off = xfs_bmbt_get_startoff(ep); |
| 4329 | /* |
| 4330 | * See if the hole before this extent will work. |
| 4331 | */ |
| 4332 | if (off >= lowest + len && off - max >= len) { |
| 4333 | *first_unused = max; |
| 4334 | return 0; |
| 4335 | } |
| 4336 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 4337 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 4338 | } |
| 4339 | *first_unused = max; |
| 4340 | return 0; |
| 4341 | } |
| 4342 | |
| 4343 | /* |
| 4344 | * Returns the file-relative block number of the last block + 1 before |
| 4345 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4346 | * This is not based on i_size, it is based on the extent records. |
| 4347 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | */ |
| 4349 | int /* error */ |
| 4350 | xfs_bmap_last_before( |
| 4351 | xfs_trans_t *tp, /* transaction pointer */ |
| 4352 | xfs_inode_t *ip, /* incore inode */ |
| 4353 | xfs_fileoff_t *last_block, /* last block */ |
| 4354 | int whichfork) /* data or attr fork */ |
| 4355 | { |
| 4356 | xfs_fileoff_t bno; /* input file offset */ |
| 4357 | int eof; /* hit end of file */ |
| 4358 | xfs_bmbt_rec_t *ep; /* pointer to last extent */ |
| 4359 | int error; /* error return value */ |
| 4360 | xfs_bmbt_irec_t got; /* current extent value */ |
| 4361 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4362 | xfs_extnum_t lastx; /* last extent used */ |
| 4363 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 4364 | |
| 4365 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4366 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4367 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4368 | return XFS_ERROR(EIO); |
| 4369 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4370 | *last_block = 0; |
| 4371 | return 0; |
| 4372 | } |
| 4373 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4374 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4375 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4376 | return error; |
| 4377 | bno = *last_block - 1; |
| 4378 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4379 | &prev); |
| 4380 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 4381 | if (prev.br_startoff == NULLFILEOFF) |
| 4382 | *last_block = 0; |
| 4383 | else |
| 4384 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 4385 | } |
| 4386 | /* |
| 4387 | * Otherwise *last_block is already the right answer. |
| 4388 | */ |
| 4389 | return 0; |
| 4390 | } |
| 4391 | |
| 4392 | /* |
| 4393 | * 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] | 4394 | * the file. This is not based on i_size, it is based on the extent records. |
| 4395 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | */ |
| 4397 | int /* error */ |
| 4398 | xfs_bmap_last_offset( |
| 4399 | xfs_trans_t *tp, /* transaction pointer */ |
| 4400 | xfs_inode_t *ip, /* incore inode */ |
| 4401 | xfs_fileoff_t *last_block, /* last block */ |
| 4402 | int whichfork) /* data or attr fork */ |
| 4403 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | xfs_bmbt_rec_t *ep; /* pointer to last extent */ |
| 4405 | int error; /* error return value */ |
| 4406 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4407 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4408 | |
| 4409 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4410 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4411 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4412 | return XFS_ERROR(EIO); |
| 4413 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4414 | *last_block = 0; |
| 4415 | return 0; |
| 4416 | } |
| 4417 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4418 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4419 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4420 | return error; |
| 4421 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4422 | if (!nextents) { |
| 4423 | *last_block = 0; |
| 4424 | return 0; |
| 4425 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4426 | ep = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); |
| 4428 | return 0; |
| 4429 | } |
| 4430 | |
| 4431 | /* |
| 4432 | * Returns whether the selected fork of the inode has exactly one |
| 4433 | * block or not. For the data fork we check this matches di_size, |
| 4434 | * implying the file's range is 0..bsize-1. |
| 4435 | */ |
| 4436 | int /* 1=>1 block, 0=>otherwise */ |
| 4437 | xfs_bmap_one_block( |
| 4438 | xfs_inode_t *ip, /* incore inode */ |
| 4439 | int whichfork) /* data or attr fork */ |
| 4440 | { |
| 4441 | xfs_bmbt_rec_t *ep; /* ptr to fork's extent */ |
| 4442 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4443 | int rval; /* return value */ |
| 4444 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 4445 | |
| 4446 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame^] | 4447 | if (whichfork == XFS_DATA_FORK) { |
| 4448 | return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ? |
| 4449 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 4450 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 4451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | #endif /* !DEBUG */ |
| 4453 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4454 | return 0; |
| 4455 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4456 | return 0; |
| 4457 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4458 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4459 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | xfs_bmbt_get_all(ep, &s); |
| 4461 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4462 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame^] | 4463 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | return rval; |
| 4465 | } |
| 4466 | |
| 4467 | /* |
| 4468 | * Read in the extents to if_extents. |
| 4469 | * All inode fields are set up by caller, we just traverse the btree |
| 4470 | * and copy the records in. If the file system cannot contain unwritten |
| 4471 | * extents, the records are checked for no "state" flags. |
| 4472 | */ |
| 4473 | int /* error */ |
| 4474 | xfs_bmap_read_extents( |
| 4475 | xfs_trans_t *tp, /* transaction pointer */ |
| 4476 | xfs_inode_t *ip, /* incore inode */ |
| 4477 | int whichfork) /* data or attr fork */ |
| 4478 | { |
| 4479 | xfs_bmbt_block_t *block; /* current btree block */ |
| 4480 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4481 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4482 | int error; /* error return value */ |
| 4483 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
| 4484 | #ifdef XFS_BMAP_TRACE |
| 4485 | static char fname[] = "xfs_bmap_read_extents"; |
| 4486 | #endif |
| 4487 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4488 | xfs_ifork_t *ifp; /* fork structure */ |
| 4489 | int level; /* btree level, for checking */ |
| 4490 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4491 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | /* REFERENCED */ |
| 4493 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | |
| 4495 | bno = NULLFSBLOCK; |
| 4496 | mp = ip->i_mount; |
| 4497 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4498 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4499 | XFS_EXTFMT_INODE(ip); |
| 4500 | block = ifp->if_broot; |
| 4501 | /* |
| 4502 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4503 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4504 | level = be16_to_cpu(block->bb_level); |
| 4505 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4507 | bno = be64_to_cpu(*pp); |
| 4508 | ASSERT(bno != NULLDFSBNO); |
| 4509 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4510 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | /* |
| 4512 | * Go down the tree until leaf level is reached, following the first |
| 4513 | * pointer (leftmost) at each level. |
| 4514 | */ |
| 4515 | while (level-- > 0) { |
| 4516 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4517 | XFS_BMAP_BTREE_REF))) |
| 4518 | return error; |
| 4519 | block = XFS_BUF_TO_BMBT_BLOCK(bp); |
| 4520 | XFS_WANT_CORRUPTED_GOTO( |
| 4521 | XFS_BMAP_SANITY_CHECK(mp, block, level), |
| 4522 | error0); |
| 4523 | if (level == 0) |
| 4524 | break; |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 4525 | pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4526 | bno = be64_to_cpu(*pp); |
| 4527 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | xfs_trans_brelse(tp, bp); |
| 4529 | } |
| 4530 | /* |
| 4531 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4532 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4533 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | i = 0; |
| 4535 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4536 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | */ |
| 4538 | for (;;) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4539 | xfs_bmbt_rec_t *frp, *trp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | xfs_fsblock_t nextbno; |
| 4541 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4542 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
| 4544 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4545 | num_recs = be16_to_cpu(block->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | if (unlikely(i + num_recs > room)) { |
| 4547 | ASSERT(i + num_recs <= room); |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4548 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 4549 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | (unsigned long long) ip->i_ino); |
| 4551 | XFS_ERROR_REPORT("xfs_bmap_read_extents(1)", |
| 4552 | XFS_ERRLEVEL_LOW, |
| 4553 | ip->i_mount); |
| 4554 | goto error0; |
| 4555 | } |
| 4556 | XFS_WANT_CORRUPTED_GOTO( |
| 4557 | XFS_BMAP_SANITY_CHECK(mp, block, 0), |
| 4558 | error0); |
| 4559 | /* |
| 4560 | * Read-ahead the next leaf block, if any. |
| 4561 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4562 | nextbno = be64_to_cpu(block->bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | if (nextbno != NULLFSBLOCK) |
| 4564 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4565 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4566 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | */ |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 4568 | frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4569 | start = i; |
| 4570 | for (j = 0; j < num_recs; j++, i++, frp++) { |
| 4571 | trp = xfs_iext_get_ext(ifp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | trp->l0 = INT_GET(frp->l0, ARCH_CONVERT); |
| 4573 | trp->l1 = INT_GET(frp->l1, ARCH_CONVERT); |
| 4574 | } |
| 4575 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4576 | /* |
| 4577 | * Check all attribute bmap btree records and |
| 4578 | * any "older" data bmap btree records for a |
| 4579 | * set bit in the "extent flag" position. |
| 4580 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4581 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4582 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4584 | XFS_ERRLEVEL_LOW, |
| 4585 | ip->i_mount); |
| 4586 | goto error0; |
| 4587 | } |
| 4588 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | xfs_trans_brelse(tp, bp); |
| 4590 | bno = nextbno; |
| 4591 | /* |
| 4592 | * If we've reached the end, stop. |
| 4593 | */ |
| 4594 | if (bno == NULLFSBLOCK) |
| 4595 | break; |
| 4596 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4597 | XFS_BMAP_BTREE_REF))) |
| 4598 | return error; |
| 4599 | block = XFS_BUF_TO_BMBT_BLOCK(bp); |
| 4600 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4601 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
| 4603 | xfs_bmap_trace_exlist(fname, ip, i, whichfork); |
| 4604 | return 0; |
| 4605 | error0: |
| 4606 | xfs_trans_brelse(tp, bp); |
| 4607 | return XFS_ERROR(EFSCORRUPTED); |
| 4608 | } |
| 4609 | |
| 4610 | #ifdef XFS_BMAP_TRACE |
| 4611 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4612 | * 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] | 4613 | */ |
| 4614 | void |
| 4615 | xfs_bmap_trace_exlist( |
| 4616 | char *fname, /* function name */ |
| 4617 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4618 | xfs_extnum_t cnt, /* count of entries in the list */ |
| 4619 | int whichfork) /* data or attr fork */ |
| 4620 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4621 | xfs_bmbt_rec_t *ep; /* current extent record */ |
| 4622 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4624 | xfs_bmbt_irec_t s; /* file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | |
| 4626 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4627 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
| 4628 | for (idx = 0; idx < cnt; idx++) { |
| 4629 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | xfs_bmbt_get_all(ep, &s); |
| 4631 | xfs_bmap_trace_insert(fname, "exlist", ip, idx, 1, &s, NULL, |
| 4632 | whichfork); |
| 4633 | } |
| 4634 | } |
| 4635 | #endif |
| 4636 | |
| 4637 | #ifdef DEBUG |
| 4638 | /* |
| 4639 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4640 | * given the callers original parameters. Specifically check the |
| 4641 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4642 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4643 | */ |
| 4644 | STATIC void |
| 4645 | xfs_bmap_validate_ret( |
| 4646 | xfs_fileoff_t bno, |
| 4647 | xfs_filblks_t len, |
| 4648 | int flags, |
| 4649 | xfs_bmbt_irec_t *mval, |
| 4650 | int nmap, |
| 4651 | int ret_nmap) |
| 4652 | { |
| 4653 | int i; /* index to map values */ |
| 4654 | |
| 4655 | ASSERT(ret_nmap <= nmap); |
| 4656 | |
| 4657 | for (i = 0; i < ret_nmap; i++) { |
| 4658 | ASSERT(mval[i].br_blockcount > 0); |
| 4659 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4660 | ASSERT(mval[i].br_startoff >= bno); |
| 4661 | ASSERT(mval[i].br_blockcount <= len); |
| 4662 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4663 | bno + len); |
| 4664 | } else { |
| 4665 | ASSERT(mval[i].br_startoff < bno + len); |
| 4666 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4667 | bno); |
| 4668 | } |
| 4669 | ASSERT(i == 0 || |
| 4670 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4671 | mval[i].br_startoff); |
| 4672 | if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY)) |
| 4673 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4674 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 4675 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4676 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4677 | } |
| 4678 | } |
| 4679 | #endif /* DEBUG */ |
| 4680 | |
| 4681 | |
| 4682 | /* |
| 4683 | * Map file blocks to filesystem blocks. |
| 4684 | * File range is given by the bno/len pair. |
| 4685 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 4686 | * into a hole or past eof. |
| 4687 | * Only allocates blocks from a single allocation group, |
| 4688 | * to avoid locking problems. |
| 4689 | * The returned value in "firstblock" from the first call in a transaction |
| 4690 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4691 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4692 | * the first call in "total"; if no allocation group has that many free |
| 4693 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4694 | */ |
| 4695 | int /* error */ |
| 4696 | xfs_bmapi( |
| 4697 | xfs_trans_t *tp, /* transaction pointer */ |
| 4698 | xfs_inode_t *ip, /* incore inode */ |
| 4699 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4700 | xfs_filblks_t len, /* length to map in file */ |
| 4701 | int flags, /* XFS_BMAPI_... */ |
| 4702 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4703 | controls a.g. for allocs */ |
| 4704 | xfs_extlen_t total, /* total blocks needed */ |
| 4705 | xfs_bmbt_irec_t *mval, /* output: map values */ |
| 4706 | int *nmap, /* i/o: mval size/count */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4707 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 4708 | xfs_extdelta_t *delta) /* o: change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | { |
| 4710 | xfs_fsblock_t abno; /* allocated block number */ |
| 4711 | xfs_extlen_t alen; /* allocated extent length */ |
| 4712 | xfs_fileoff_t aoff; /* allocated file offset */ |
| 4713 | xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | xfs_fileoff_t end; /* end of mapped file region */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4716 | int eof; /* we've hit the end of extents */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4717 | xfs_bmbt_rec_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4719 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4721 | xfs_extlen_t indlen; /* indirect blocks length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4723 | int logflags; /* flags for transaction logging */ |
| 4724 | xfs_extlen_t minleft; /* min blocks left after allocation */ |
| 4725 | xfs_extlen_t minlen; /* min allocation size */ |
| 4726 | xfs_mount_t *mp; /* xfs mount structure */ |
| 4727 | int n; /* current extent index */ |
| 4728 | int nallocs; /* number of extents alloc\'d */ |
| 4729 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4730 | xfs_fileoff_t obno; /* old block number (offset) */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4731 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | int tmp_logflags; /* temp flags holder */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4733 | int whichfork; /* data or attr fork */ |
| 4734 | char inhole; /* current location is hole in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | char wasdelay; /* old extent was delayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | char wr; /* this is a write request */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4737 | char rt; /* this is a realtime file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | #ifdef DEBUG |
| 4739 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4740 | int orig_flags; /* original flags arg value */ |
| 4741 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4742 | xfs_bmbt_irec_t *orig_mval; /* original value of mval */ |
| 4743 | int orig_nmap; /* original value of *nmap */ |
| 4744 | |
| 4745 | orig_bno = bno; |
| 4746 | orig_len = len; |
| 4747 | orig_flags = flags; |
| 4748 | orig_mval = mval; |
| 4749 | orig_nmap = *nmap; |
| 4750 | #endif |
| 4751 | ASSERT(*nmap >= 1); |
| 4752 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); |
| 4753 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4754 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4755 | mp = ip->i_mount; |
| 4756 | if (unlikely(XFS_TEST_ERROR( |
| 4757 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4758 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4759 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4760 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4761 | XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); |
| 4762 | return XFS_ERROR(EFSCORRUPTED); |
| 4763 | } |
| 4764 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4765 | return XFS_ERROR(EIO); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4766 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4768 | ASSERT(ifp->if_ext_max == |
| 4769 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4770 | if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) |
| 4771 | XFS_STATS_INC(xs_blk_mapw); |
| 4772 | else |
| 4773 | XFS_STATS_INC(xs_blk_mapr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | /* |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4775 | * IGSTATE flag is used to combine extents which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | * differ only due to the state of the extents. |
| 4777 | * This technique is used from xfs_getbmap() |
| 4778 | * when the caller does not wish to see the |
| 4779 | * separation (which is the default). |
| 4780 | * |
| 4781 | * This technique is also used when writing a |
| 4782 | * buffer which has been partially written, |
| 4783 | * (usually by being flushed during a chunkread), |
| 4784 | * to ensure one write takes place. This also |
| 4785 | * prevents a change in the xfs inode extents at |
| 4786 | * this time, intentionally. This change occurs |
| 4787 | * on completion of the write operation, in |
| 4788 | * xfs_strat_comp(), where the xfs_bmapi() call |
| 4789 | * is transactioned, and the extents combined. |
| 4790 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4791 | if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */ |
| 4792 | wr = 0; /* no allocations are allowed */ |
| 4793 | ASSERT(wr || !(flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | logflags = 0; |
| 4795 | nallocs = 0; |
| 4796 | cur = NULL; |
| 4797 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4798 | ASSERT(wr && tp); |
| 4799 | if ((error = xfs_bmap_local_to_extents(tp, ip, |
| 4800 | firstblock, total, &logflags, whichfork))) |
| 4801 | goto error0; |
| 4802 | } |
| 4803 | if (wr && *firstblock == NULLFSBLOCK) { |
| 4804 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4805 | minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | else |
| 4807 | minleft = 1; |
| 4808 | } else |
| 4809 | minleft = 0; |
| 4810 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4811 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4812 | goto error0; |
| 4813 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4814 | &prev); |
| 4815 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4816 | n = 0; |
| 4817 | end = bno + len; |
| 4818 | obno = bno; |
| 4819 | bma.ip = NULL; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4820 | if (delta) { |
| 4821 | delta->xed_startoff = NULLFILEOFF; |
| 4822 | delta->xed_blockcount = 0; |
| 4823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | while (bno < end && n < *nmap) { |
| 4825 | /* |
| 4826 | * Reading past eof, act as though there's a hole |
| 4827 | * up to end. |
| 4828 | */ |
| 4829 | if (eof && !wr) |
| 4830 | got.br_startoff = end; |
| 4831 | inhole = eof || got.br_startoff > bno; |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4832 | wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | ISNULLSTARTBLOCK(got.br_startblock); |
| 4834 | /* |
| 4835 | * First, deal with the hole before the allocated space |
| 4836 | * that we found, if any. |
| 4837 | */ |
| 4838 | if (wr && (inhole || wasdelay)) { |
| 4839 | /* |
| 4840 | * For the wasdelay case, we could also just |
| 4841 | * allocate the stuff asked for in this bmap call |
| 4842 | * but that wouldn't be as good. |
| 4843 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4844 | if (wasdelay && !(flags & XFS_BMAPI_EXACT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | alen = (xfs_extlen_t)got.br_blockcount; |
| 4846 | aoff = got.br_startoff; |
| 4847 | if (lastx != NULLEXTNUM && lastx) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4848 | ep = xfs_iext_get_ext(ifp, lastx - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | xfs_bmbt_get_all(ep, &prev); |
| 4850 | } |
| 4851 | } else if (wasdelay) { |
| 4852 | alen = (xfs_extlen_t) |
| 4853 | XFS_FILBLKS_MIN(len, |
| 4854 | (got.br_startoff + |
| 4855 | got.br_blockcount) - bno); |
| 4856 | aoff = bno; |
| 4857 | } else { |
| 4858 | alen = (xfs_extlen_t) |
| 4859 | XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4860 | if (!eof) |
| 4861 | alen = (xfs_extlen_t) |
| 4862 | XFS_FILBLKS_MIN(alen, |
| 4863 | got.br_startoff - bno); |
| 4864 | aoff = bno; |
| 4865 | } |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4866 | minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; |
| 4867 | if (flags & XFS_BMAPI_DELAY) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4868 | xfs_extlen_t extsz; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4869 | |
| 4870 | /* Figure out the extent size, adjust alen */ |
| 4871 | if (rt) { |
| 4872 | if (!(extsz = ip->i_d.di_extsize)) |
| 4873 | extsz = mp->m_sb.sb_rextsize; |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4874 | } else { |
| 4875 | extsz = ip->i_d.di_extsize; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4876 | } |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4877 | if (extsz) { |
| 4878 | error = xfs_bmap_extsize_align(mp, |
| 4879 | &got, &prev, extsz, |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4880 | rt, eof, |
| 4881 | flags&XFS_BMAPI_DELAY, |
| 4882 | flags&XFS_BMAPI_CONVERT, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4883 | &aoff, &alen); |
| 4884 | ASSERT(!error); |
| 4885 | } |
| 4886 | |
| 4887 | if (rt) |
| 4888 | extsz = alen / mp->m_sb.sb_rextsize; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | /* |
| 4891 | * Make a transaction-less quota reservation for |
| 4892 | * delayed allocation blocks. This number gets |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4893 | * adjusted later. We return if we haven't |
| 4894 | * allocated blocks already inside this loop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | */ |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4896 | if ((error = XFS_TRANS_RESERVE_QUOTA_NBLKS( |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4897 | mp, NULL, ip, (long)alen, 0, |
| 4898 | rt ? XFS_QMOPT_RES_RTBLKS : |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4899 | XFS_QMOPT_RES_REGBLKS))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | if (n == 0) { |
| 4901 | *nmap = 0; |
| 4902 | ASSERT(cur == NULL); |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4903 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | } |
| 4905 | break; |
| 4906 | } |
| 4907 | |
| 4908 | /* |
| 4909 | * Split changing sb for alen and indlen since |
| 4910 | * they could be coming from different places. |
| 4911 | */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4912 | indlen = (xfs_extlen_t) |
| 4913 | xfs_bmap_worst_indlen(ip, alen); |
| 4914 | ASSERT(indlen > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4916 | if (rt) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4917 | error = xfs_mod_incore_sb(mp, |
| 4918 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4919 | -((int64_t)extsz), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4920 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4921 | } else { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4922 | error = xfs_mod_incore_sb(mp, |
| 4923 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4924 | -((int64_t)alen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4925 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4926 | } |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4927 | if (!error) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4928 | error = xfs_mod_incore_sb(mp, |
| 4929 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4930 | -((int64_t)indlen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4931 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4932 | if (error && rt) |
| 4933 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4934 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4935 | (int64_t)extsz, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4936 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4937 | else if (error) |
| 4938 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4939 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4940 | (int64_t)alen, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4941 | XFS_BMAPI_RSVBLOCKS)); |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4942 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4943 | |
| 4944 | if (error) { |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4945 | if (XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4946 | /* unreserve the blocks now */ |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4947 | (void) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4948 | XFS_TRANS_UNRESERVE_QUOTA_NBLKS( |
| 4949 | mp, NULL, ip, |
| 4950 | (long)alen, 0, rt ? |
| 4951 | XFS_QMOPT_RES_RTBLKS : |
| 4952 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4953 | break; |
| 4954 | } |
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 | ip->i_delayed_blks += alen; |
| 4957 | abno = NULLSTARTBLOCK(indlen); |
| 4958 | } else { |
| 4959 | /* |
| 4960 | * If first time, allocate and fill in |
| 4961 | * once-only bma fields. |
| 4962 | */ |
| 4963 | if (bma.ip == NULL) { |
| 4964 | bma.tp = tp; |
| 4965 | bma.ip = ip; |
| 4966 | bma.prevp = &prev; |
| 4967 | bma.gotp = &got; |
| 4968 | bma.total = total; |
| 4969 | bma.userdata = 0; |
| 4970 | } |
| 4971 | /* Indicate if this is the first user data |
| 4972 | * in the file, or just any user data. |
| 4973 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4974 | if (!(flags & XFS_BMAPI_METADATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | bma.userdata = (aoff == 0) ? |
| 4976 | XFS_ALLOC_INITIAL_USER_DATA : |
| 4977 | XFS_ALLOC_USERDATA; |
| 4978 | } |
| 4979 | /* |
| 4980 | * Fill in changeable bma fields. |
| 4981 | */ |
| 4982 | bma.eof = eof; |
| 4983 | bma.firstblock = *firstblock; |
| 4984 | bma.alen = alen; |
| 4985 | bma.off = aoff; |
Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 4986 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | bma.wasdel = wasdelay; |
| 4988 | bma.minlen = minlen; |
| 4989 | bma.low = flist->xbf_low; |
| 4990 | bma.minleft = minleft; |
| 4991 | /* |
| 4992 | * Only want to do the alignment at the |
| 4993 | * eof if it is userdata and allocation length |
| 4994 | * is larger than a stripe unit. |
| 4995 | */ |
| 4996 | if (mp->m_dalign && alen >= mp->m_dalign && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4997 | (!(flags & XFS_BMAPI_METADATA)) && |
| 4998 | (whichfork == XFS_DATA_FORK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | if ((error = xfs_bmap_isaeof(ip, aoff, |
| 5000 | whichfork, &bma.aeof))) |
| 5001 | goto error0; |
| 5002 | } else |
| 5003 | bma.aeof = 0; |
| 5004 | /* |
| 5005 | * Call allocator. |
| 5006 | */ |
| 5007 | if ((error = xfs_bmap_alloc(&bma))) |
| 5008 | goto error0; |
| 5009 | /* |
| 5010 | * Copy out result fields. |
| 5011 | */ |
| 5012 | abno = bma.rval; |
| 5013 | if ((flist->xbf_low = bma.low)) |
| 5014 | minleft = 0; |
| 5015 | alen = bma.alen; |
| 5016 | aoff = bma.off; |
| 5017 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5018 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5019 | XFS_FSB_TO_AGNO(mp, bma.firstblock) || |
| 5020 | (flist->xbf_low && |
| 5021 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5022 | XFS_FSB_TO_AGNO(mp, bma.firstblock))); |
| 5023 | *firstblock = bma.firstblock; |
| 5024 | if (cur) |
| 5025 | cur->bc_private.b.firstblock = |
| 5026 | *firstblock; |
| 5027 | if (abno == NULLFSBLOCK) |
| 5028 | break; |
| 5029 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
| 5030 | cur = xfs_btree_init_cursor(mp, |
| 5031 | tp, NULL, 0, XFS_BTNUM_BMAP, |
| 5032 | ip, whichfork); |
| 5033 | cur->bc_private.b.firstblock = |
| 5034 | *firstblock; |
| 5035 | cur->bc_private.b.flist = flist; |
| 5036 | } |
| 5037 | /* |
| 5038 | * Bump the number of extents we've allocated |
| 5039 | * in this call. |
| 5040 | */ |
| 5041 | nallocs++; |
| 5042 | } |
| 5043 | if (cur) |
| 5044 | cur->bc_private.b.flags = |
| 5045 | wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 5046 | got.br_startoff = aoff; |
| 5047 | got.br_startblock = abno; |
| 5048 | got.br_blockcount = alen; |
| 5049 | got.br_state = XFS_EXT_NORM; /* assume normal */ |
| 5050 | /* |
| 5051 | * Determine state of extent, and the filesystem. |
| 5052 | * A wasdelay extent has been initialized, so |
| 5053 | * shouldn't be flagged as unwritten. |
| 5054 | */ |
| 5055 | if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) { |
| 5056 | if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) |
| 5057 | got.br_state = XFS_EXT_UNWRITTEN; |
| 5058 | } |
| 5059 | error = xfs_bmap_add_extent(ip, lastx, &cur, &got, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5060 | firstblock, flist, &tmp_logflags, delta, |
| 5061 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | logflags |= tmp_logflags; |
| 5063 | if (error) |
| 5064 | goto error0; |
| 5065 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5066 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5068 | xfs_bmbt_get_all(ep, &got); |
| 5069 | ASSERT(got.br_startoff <= aoff); |
| 5070 | ASSERT(got.br_startoff + got.br_blockcount >= |
| 5071 | aoff + alen); |
| 5072 | #ifdef DEBUG |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5073 | if (flags & XFS_BMAPI_DELAY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | ASSERT(ISNULLSTARTBLOCK(got.br_startblock)); |
| 5075 | ASSERT(STARTBLOCKVAL(got.br_startblock) > 0); |
| 5076 | } |
| 5077 | ASSERT(got.br_state == XFS_EXT_NORM || |
| 5078 | got.br_state == XFS_EXT_UNWRITTEN); |
| 5079 | #endif |
| 5080 | /* |
| 5081 | * Fall down into the found allocated space case. |
| 5082 | */ |
| 5083 | } else if (inhole) { |
| 5084 | /* |
| 5085 | * Reading in a hole. |
| 5086 | */ |
| 5087 | mval->br_startoff = bno; |
| 5088 | mval->br_startblock = HOLESTARTBLOCK; |
| 5089 | mval->br_blockcount = |
| 5090 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 5091 | mval->br_state = XFS_EXT_NORM; |
| 5092 | bno += mval->br_blockcount; |
| 5093 | len -= mval->br_blockcount; |
| 5094 | mval++; |
| 5095 | n++; |
| 5096 | continue; |
| 5097 | } |
| 5098 | /* |
| 5099 | * Then deal with the allocated space we found. |
| 5100 | */ |
| 5101 | ASSERT(ep != NULL); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5102 | if (!(flags & XFS_BMAPI_ENTIRE) && |
| 5103 | (got.br_startoff + got.br_blockcount > obno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | if (obno > bno) |
| 5105 | bno = obno; |
| 5106 | ASSERT((bno >= obno) || (n == 0)); |
| 5107 | ASSERT(bno < end); |
| 5108 | mval->br_startoff = bno; |
| 5109 | if (ISNULLSTARTBLOCK(got.br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5110 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | mval->br_startblock = DELAYSTARTBLOCK; |
| 5112 | } else |
| 5113 | mval->br_startblock = |
| 5114 | got.br_startblock + |
| 5115 | (bno - got.br_startoff); |
| 5116 | /* |
| 5117 | * Return the minimum of what we got and what we |
| 5118 | * asked for for the length. We can use the len |
| 5119 | * variable here because it is modified below |
| 5120 | * and we could have been there before coming |
| 5121 | * here if the first part of the allocation |
| 5122 | * didn't overlap what was asked for. |
| 5123 | */ |
| 5124 | mval->br_blockcount = |
| 5125 | XFS_FILBLKS_MIN(end - bno, got.br_blockcount - |
| 5126 | (bno - got.br_startoff)); |
| 5127 | mval->br_state = got.br_state; |
| 5128 | ASSERT(mval->br_blockcount <= len); |
| 5129 | } else { |
| 5130 | *mval = got; |
| 5131 | if (ISNULLSTARTBLOCK(mval->br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5132 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | mval->br_startblock = DELAYSTARTBLOCK; |
| 5134 | } |
| 5135 | } |
| 5136 | |
| 5137 | /* |
| 5138 | * Check if writing previously allocated but |
| 5139 | * unwritten extents. |
| 5140 | */ |
| 5141 | if (wr && mval->br_state == XFS_EXT_UNWRITTEN && |
| 5142 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) { |
| 5143 | /* |
| 5144 | * Modify (by adding) the state flag, if writing. |
| 5145 | */ |
| 5146 | ASSERT(mval->br_blockcount <= len); |
| 5147 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
| 5148 | cur = xfs_btree_init_cursor(mp, |
| 5149 | tp, NULL, 0, XFS_BTNUM_BMAP, |
| 5150 | ip, whichfork); |
| 5151 | cur->bc_private.b.firstblock = |
| 5152 | *firstblock; |
| 5153 | cur->bc_private.b.flist = flist; |
| 5154 | } |
| 5155 | mval->br_state = XFS_EXT_NORM; |
| 5156 | error = xfs_bmap_add_extent(ip, lastx, &cur, mval, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5157 | firstblock, flist, &tmp_logflags, delta, |
| 5158 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | logflags |= tmp_logflags; |
| 5160 | if (error) |
| 5161 | goto error0; |
| 5162 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5163 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5165 | xfs_bmbt_get_all(ep, &got); |
| 5166 | /* |
| 5167 | * We may have combined previously unwritten |
| 5168 | * space with written space, so generate |
| 5169 | * another request. |
| 5170 | */ |
| 5171 | if (mval->br_blockcount < len) |
| 5172 | continue; |
| 5173 | } |
| 5174 | |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5175 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5177 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 5178 | (mval->br_blockcount <= len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | (mval->br_startoff < obno)); |
| 5180 | bno = mval->br_startoff + mval->br_blockcount; |
| 5181 | len = end - bno; |
| 5182 | if (n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 5183 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 5184 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 5185 | ASSERT(mval->br_state == mval[-1].br_state); |
| 5186 | mval[-1].br_blockcount = mval->br_blockcount; |
| 5187 | mval[-1].br_state = mval->br_state; |
| 5188 | } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 5189 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 5190 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 5191 | mval->br_startblock == |
| 5192 | mval[-1].br_startblock + mval[-1].br_blockcount && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 5193 | ((flags & XFS_BMAPI_IGSTATE) || |
| 5194 | mval[-1].br_state == mval->br_state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | ASSERT(mval->br_startoff == |
| 5196 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 5197 | mval[-1].br_blockcount += mval->br_blockcount; |
| 5198 | } else if (n > 0 && |
| 5199 | mval->br_startblock == DELAYSTARTBLOCK && |
| 5200 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 5201 | mval->br_startoff == |
| 5202 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 5203 | mval[-1].br_blockcount += mval->br_blockcount; |
| 5204 | mval[-1].br_state = mval->br_state; |
| 5205 | } else if (!((n == 0) && |
| 5206 | ((mval->br_startoff + mval->br_blockcount) <= |
| 5207 | obno))) { |
| 5208 | mval++; |
| 5209 | n++; |
| 5210 | } |
| 5211 | /* |
| 5212 | * If we're done, stop now. Stop when we've allocated |
| 5213 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 5214 | * the transaction may get too big. |
| 5215 | */ |
| 5216 | if (bno >= end || n >= *nmap || nallocs >= *nmap) |
| 5217 | break; |
| 5218 | /* |
| 5219 | * Else go on to the next record. |
| 5220 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5221 | ep = xfs_iext_get_ext(ifp, ++lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | if (lastx >= nextents) { |
| 5223 | eof = 1; |
| 5224 | prev = got; |
| 5225 | } else |
| 5226 | xfs_bmbt_get_all(ep, &got); |
| 5227 | } |
| 5228 | ifp->if_lastex = lastx; |
| 5229 | *nmap = n; |
| 5230 | /* |
| 5231 | * Transform from btree to extents, give it cur. |
| 5232 | */ |
| 5233 | if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5234 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5235 | ASSERT(wr && cur); |
| 5236 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 5237 | &tmp_logflags, whichfork); |
| 5238 | logflags |= tmp_logflags; |
| 5239 | if (error) |
| 5240 | goto error0; |
| 5241 | } |
| 5242 | ASSERT(ifp->if_ext_max == |
| 5243 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5244 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 5245 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 5246 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5247 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5248 | /* A change was actually made. |
| 5249 | * Note that delta->xed_blockount is an offset at this |
| 5250 | * point and needs to be converted to a block count. |
| 5251 | */ |
| 5252 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5253 | delta->xed_blockcount -= delta->xed_startoff; |
| 5254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | error0: |
| 5256 | /* |
| 5257 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5258 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | */ |
| 5260 | if ((logflags & XFS_ILOG_FEXT(whichfork)) && |
| 5261 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 5262 | logflags &= ~XFS_ILOG_FEXT(whichfork); |
| 5263 | else if ((logflags & XFS_ILOG_FBROOT(whichfork)) && |
| 5264 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
| 5265 | logflags &= ~XFS_ILOG_FBROOT(whichfork); |
| 5266 | /* |
| 5267 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5268 | * detecting a case where the data is changed, there's an error, |
| 5269 | * and it's not logged so we don't shutdown when we should. |
| 5270 | */ |
| 5271 | if (logflags) { |
| 5272 | ASSERT(tp && wr); |
| 5273 | xfs_trans_log_inode(tp, ip, logflags); |
| 5274 | } |
| 5275 | if (cur) { |
| 5276 | if (!error) { |
| 5277 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5278 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5279 | XFS_FSB_TO_AGNO(mp, |
| 5280 | cur->bc_private.b.firstblock) || |
| 5281 | (flist->xbf_low && |
| 5282 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5283 | XFS_FSB_TO_AGNO(mp, |
| 5284 | cur->bc_private.b.firstblock))); |
| 5285 | *firstblock = cur->bc_private.b.firstblock; |
| 5286 | } |
| 5287 | xfs_btree_del_cursor(cur, |
| 5288 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5289 | } |
| 5290 | if (!error) |
| 5291 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5292 | orig_nmap, *nmap); |
| 5293 | return error; |
| 5294 | } |
| 5295 | |
| 5296 | /* |
| 5297 | * Map file blocks to filesystem blocks, simple version. |
| 5298 | * One block (extent) only, read-only. |
| 5299 | * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. |
| 5300 | * For the other flag values, the effect is as if XFS_BMAPI_METADATA |
| 5301 | * was set and all the others were clear. |
| 5302 | */ |
| 5303 | int /* error */ |
| 5304 | xfs_bmapi_single( |
| 5305 | xfs_trans_t *tp, /* transaction pointer */ |
| 5306 | xfs_inode_t *ip, /* incore inode */ |
| 5307 | int whichfork, /* data or attr fork */ |
| 5308 | xfs_fsblock_t *fsb, /* output: mapped block */ |
| 5309 | xfs_fileoff_t bno) /* starting file offs. mapped */ |
| 5310 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5311 | int eof; /* we've hit the end of extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5313 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5315 | xfs_extnum_t lastx; /* last useful extent number */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5316 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | |
| 5318 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5319 | if (unlikely( |
| 5320 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 5321 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) { |
| 5322 | XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW, |
| 5323 | ip->i_mount); |
| 5324 | return XFS_ERROR(EFSCORRUPTED); |
| 5325 | } |
| 5326 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5327 | return XFS_ERROR(EIO); |
| 5328 | XFS_STATS_INC(xs_blk_mapr); |
| 5329 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5330 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5331 | return error; |
| 5332 | (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5333 | &prev); |
| 5334 | /* |
| 5335 | * Reading past eof, act as though there's a hole |
| 5336 | * up to end. |
| 5337 | */ |
| 5338 | if (eof || got.br_startoff > bno) { |
| 5339 | *fsb = NULLFSBLOCK; |
| 5340 | return 0; |
| 5341 | } |
| 5342 | ASSERT(!ISNULLSTARTBLOCK(got.br_startblock)); |
| 5343 | ASSERT(bno < got.br_startoff + got.br_blockcount); |
| 5344 | *fsb = got.br_startblock + (bno - got.br_startoff); |
| 5345 | ifp->if_lastex = lastx; |
| 5346 | return 0; |
| 5347 | } |
| 5348 | |
| 5349 | /* |
| 5350 | * Unmap (remove) blocks from a file. |
| 5351 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5352 | * that value. If not all extents in the block range can be removed then |
| 5353 | * *done is set. |
| 5354 | */ |
| 5355 | int /* error */ |
| 5356 | xfs_bunmapi( |
| 5357 | xfs_trans_t *tp, /* transaction pointer */ |
| 5358 | struct xfs_inode *ip, /* incore inode */ |
| 5359 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5360 | xfs_filblks_t len, /* length to unmap in file */ |
| 5361 | int flags, /* misc flags */ |
| 5362 | xfs_extnum_t nexts, /* number of extents max */ |
| 5363 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5364 | controls a.g. for allocs */ |
| 5365 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5366 | xfs_extdelta_t *delta, /* o: change made to incore |
| 5367 | extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | int *done) /* set if not done yet */ |
| 5369 | { |
| 5370 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5371 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5372 | int eof; /* is deleting at eof */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5373 | xfs_bmbt_rec_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | int error; /* error return value */ |
| 5375 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5376 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5378 | int isrt; /* freeing in rt area */ |
| 5379 | xfs_extnum_t lastx; /* last extent index used */ |
| 5380 | int logflags; /* transaction logging flags */ |
| 5381 | xfs_extlen_t mod; /* rt extent offset */ |
| 5382 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5383 | xfs_extnum_t nextents; /* number of file extents */ |
| 5384 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5386 | int tmp_logflags; /* partial logging flags */ |
| 5387 | int wasdel; /* was a delayed alloc extent */ |
| 5388 | int whichfork; /* data or attribute fork */ |
| 5389 | int rsvd; /* OK to allocate reserved blocks */ |
| 5390 | xfs_fsblock_t sum; |
| 5391 | |
| 5392 | xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address); |
| 5393 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5394 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5395 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5396 | if (unlikely( |
| 5397 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5398 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5399 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5400 | ip->i_mount); |
| 5401 | return XFS_ERROR(EFSCORRUPTED); |
| 5402 | } |
| 5403 | mp = ip->i_mount; |
| 5404 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5405 | return XFS_ERROR(EIO); |
| 5406 | rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0; |
| 5407 | ASSERT(len > 0); |
| 5408 | ASSERT(nexts >= 0); |
| 5409 | ASSERT(ifp->if_ext_max == |
| 5410 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5411 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5412 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5413 | return error; |
| 5414 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5415 | if (nextents == 0) { |
| 5416 | *done = 1; |
| 5417 | return 0; |
| 5418 | } |
| 5419 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5420 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | start = bno; |
| 5422 | bno = start + len - 1; |
| 5423 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5424 | &prev); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5425 | if (delta) { |
| 5426 | delta->xed_startoff = NULLFILEOFF; |
| 5427 | delta->xed_blockcount = 0; |
| 5428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | /* |
| 5430 | * Check to see if the given block number is past the end of the |
| 5431 | * file, back up to the last block if so... |
| 5432 | */ |
| 5433 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5434 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | xfs_bmbt_get_all(ep, &got); |
| 5436 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5437 | } |
| 5438 | logflags = 0; |
| 5439 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5440 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 5441 | cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip, |
| 5442 | whichfork); |
| 5443 | cur->bc_private.b.firstblock = *firstblock; |
| 5444 | cur->bc_private.b.flist = flist; |
| 5445 | cur->bc_private.b.flags = 0; |
| 5446 | } else |
| 5447 | cur = NULL; |
| 5448 | extno = 0; |
| 5449 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5450 | (nexts == 0 || extno < nexts)) { |
| 5451 | /* |
| 5452 | * Is the found extent after a hole in which bno lives? |
| 5453 | * Just back up to the previous extent, if so. |
| 5454 | */ |
| 5455 | if (got.br_startoff > bno) { |
| 5456 | if (--lastx < 0) |
| 5457 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5458 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | xfs_bmbt_get_all(ep, &got); |
| 5460 | } |
| 5461 | /* |
| 5462 | * Is the last block of this extent before the range |
| 5463 | * we're supposed to delete? If so, we're done. |
| 5464 | */ |
| 5465 | bno = XFS_FILEOFF_MIN(bno, |
| 5466 | got.br_startoff + got.br_blockcount - 1); |
| 5467 | if (bno < start) |
| 5468 | break; |
| 5469 | /* |
| 5470 | * Then deal with the (possibly delayed) allocated space |
| 5471 | * we found. |
| 5472 | */ |
| 5473 | ASSERT(ep != NULL); |
| 5474 | del = got; |
| 5475 | wasdel = ISNULLSTARTBLOCK(del.br_startblock); |
| 5476 | if (got.br_startoff < start) { |
| 5477 | del.br_startoff = start; |
| 5478 | del.br_blockcount -= start - got.br_startoff; |
| 5479 | if (!wasdel) |
| 5480 | del.br_startblock += start - got.br_startoff; |
| 5481 | } |
| 5482 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5483 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5484 | sum = del.br_startblock + del.br_blockcount; |
| 5485 | if (isrt && |
| 5486 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5487 | /* |
| 5488 | * Realtime extent not lined up at the end. |
| 5489 | * The extent could have been split into written |
| 5490 | * and unwritten pieces, or we could just be |
| 5491 | * unmapping part of it. But we can't really |
| 5492 | * get rid of part of a realtime extent. |
| 5493 | */ |
| 5494 | if (del.br_state == XFS_EXT_UNWRITTEN || |
| 5495 | !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) { |
| 5496 | /* |
| 5497 | * This piece is unwritten, or we're not |
| 5498 | * using unwritten extents. Skip over it. |
| 5499 | */ |
| 5500 | ASSERT(bno >= mod); |
| 5501 | bno -= mod > del.br_blockcount ? |
| 5502 | del.br_blockcount : mod; |
| 5503 | if (bno < got.br_startoff) { |
| 5504 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5505 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5506 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | } |
| 5508 | continue; |
| 5509 | } |
| 5510 | /* |
| 5511 | * It's written, turn it unwritten. |
| 5512 | * This is better than zeroing it. |
| 5513 | */ |
| 5514 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5515 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5516 | /* |
| 5517 | * If this spans a realtime extent boundary, |
| 5518 | * chop it back to the start of the one we end at. |
| 5519 | */ |
| 5520 | if (del.br_blockcount > mod) { |
| 5521 | del.br_startoff += del.br_blockcount - mod; |
| 5522 | del.br_startblock += del.br_blockcount - mod; |
| 5523 | del.br_blockcount = mod; |
| 5524 | } |
| 5525 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5526 | error = xfs_bmap_add_extent(ip, lastx, &cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5527 | firstblock, flist, &logflags, delta, |
| 5528 | XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | if (error) |
| 5530 | goto error0; |
| 5531 | goto nodelete; |
| 5532 | } |
| 5533 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5534 | /* |
| 5535 | * Realtime extent is lined up at the end but not |
| 5536 | * at the front. We'll get rid of full extents if |
| 5537 | * we can. |
| 5538 | */ |
| 5539 | mod = mp->m_sb.sb_rextsize - mod; |
| 5540 | if (del.br_blockcount > mod) { |
| 5541 | del.br_blockcount -= mod; |
| 5542 | del.br_startoff += mod; |
| 5543 | del.br_startblock += mod; |
| 5544 | } else if ((del.br_startoff == start && |
| 5545 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5546 | xfs_trans_get_block_res(tp) == 0)) || |
| 5547 | !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) { |
| 5548 | /* |
| 5549 | * Can't make it unwritten. There isn't |
| 5550 | * a full extent here so just skip it. |
| 5551 | */ |
| 5552 | ASSERT(bno >= del.br_blockcount); |
| 5553 | bno -= del.br_blockcount; |
| 5554 | if (bno < got.br_startoff) { |
| 5555 | if (--lastx >= 0) |
| 5556 | xfs_bmbt_get_all(--ep, &got); |
| 5557 | } |
| 5558 | continue; |
| 5559 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5560 | /* |
| 5561 | * This one is already unwritten. |
| 5562 | * It must have a written left neighbor. |
| 5563 | * Unwrite the killed part of that one and |
| 5564 | * try again. |
| 5565 | */ |
| 5566 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5567 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5568 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | ASSERT(prev.br_state == XFS_EXT_NORM); |
| 5570 | ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock)); |
| 5571 | ASSERT(del.br_startblock == |
| 5572 | prev.br_startblock + prev.br_blockcount); |
| 5573 | if (prev.br_startoff < start) { |
| 5574 | mod = start - prev.br_startoff; |
| 5575 | prev.br_blockcount -= mod; |
| 5576 | prev.br_startblock += mod; |
| 5577 | prev.br_startoff = start; |
| 5578 | } |
| 5579 | prev.br_state = XFS_EXT_UNWRITTEN; |
| 5580 | error = xfs_bmap_add_extent(ip, lastx - 1, &cur, |
| 5581 | &prev, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5582 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | if (error) |
| 5584 | goto error0; |
| 5585 | goto nodelete; |
| 5586 | } else { |
| 5587 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5588 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5589 | error = xfs_bmap_add_extent(ip, lastx, &cur, |
| 5590 | &del, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5591 | delta, 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 | } |
| 5597 | if (wasdel) { |
| 5598 | ASSERT(STARTBLOCKVAL(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5599 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5600 | if (isrt) { |
| 5601 | xfs_filblks_t rtexts; |
| 5602 | |
| 5603 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5604 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5605 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5606 | (int64_t)rtexts, rsvd); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5607 | (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, |
| 5608 | NULL, ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5609 | XFS_QMOPT_RES_RTBLKS); |
| 5610 | } else { |
| 5611 | xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5612 | (int64_t)del.br_blockcount, rsvd); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5613 | (void)XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, |
| 5614 | NULL, ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | ip->i_delayed_blks -= del.br_blockcount; |
| 5618 | if (cur) |
| 5619 | cur->bc_private.b.flags |= |
| 5620 | XFS_BTCUR_BPRV_WASDEL; |
| 5621 | } else if (cur) |
| 5622 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5623 | /* |
| 5624 | * If it's the case where the directory code is running |
| 5625 | * with no block reservation, and the deleted block is in |
| 5626 | * the middle of its extent, and the resulting insert |
| 5627 | * of an extent would cause transformation to btree format, |
| 5628 | * then reject it. The calling code will then swap |
| 5629 | * blocks around instead. |
| 5630 | * We have to do this now, rather than waiting for the |
| 5631 | * conversion to btree format, since the transaction |
| 5632 | * will be dirty. |
| 5633 | */ |
| 5634 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5635 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5636 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5637 | del.br_startoff > got.br_startoff && |
| 5638 | del.br_startoff + del.br_blockcount < |
| 5639 | got.br_startoff + got.br_blockcount) { |
| 5640 | error = XFS_ERROR(ENOSPC); |
| 5641 | goto error0; |
| 5642 | } |
| 5643 | error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5644 | &tmp_logflags, delta, whichfork, rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | logflags |= tmp_logflags; |
| 5646 | if (error) |
| 5647 | goto error0; |
| 5648 | bno = del.br_startoff - 1; |
| 5649 | nodelete: |
| 5650 | lastx = ifp->if_lastex; |
| 5651 | /* |
| 5652 | * If not done go on to the next (previous) record. |
| 5653 | * Reset ep in case the extents array was re-alloced. |
| 5654 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5655 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
| 5657 | if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) || |
| 5658 | xfs_bmbt_get_startoff(ep) > bno) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5659 | if (--lastx >= 0) |
| 5660 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | } |
| 5662 | if (lastx >= 0) |
| 5663 | xfs_bmbt_get_all(ep, &got); |
| 5664 | extno++; |
| 5665 | } |
| 5666 | } |
| 5667 | ifp->if_lastex = lastx; |
| 5668 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5669 | ASSERT(ifp->if_ext_max == |
| 5670 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5671 | /* |
| 5672 | * Convert to a btree if necessary. |
| 5673 | */ |
| 5674 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5675 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5676 | ASSERT(cur == NULL); |
| 5677 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5678 | &cur, 0, &tmp_logflags, whichfork); |
| 5679 | logflags |= tmp_logflags; |
| 5680 | if (error) |
| 5681 | goto error0; |
| 5682 | } |
| 5683 | /* |
| 5684 | * transform from btree to extents, give it cur |
| 5685 | */ |
| 5686 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5687 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5688 | ASSERT(cur != NULL); |
| 5689 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5690 | whichfork); |
| 5691 | logflags |= tmp_logflags; |
| 5692 | if (error) |
| 5693 | goto error0; |
| 5694 | } |
| 5695 | /* |
| 5696 | * transform from extents to local? |
| 5697 | */ |
| 5698 | ASSERT(ifp->if_ext_max == |
| 5699 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5700 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5701 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5702 | /* A change was actually made. |
| 5703 | * Note that delta->xed_blockount is an offset at this |
| 5704 | * point and needs to be converted to a block count. |
| 5705 | */ |
| 5706 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5707 | delta->xed_blockcount -= delta->xed_startoff; |
| 5708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | error0: |
| 5710 | /* |
| 5711 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5712 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | */ |
| 5714 | if ((logflags & XFS_ILOG_FEXT(whichfork)) && |
| 5715 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 5716 | logflags &= ~XFS_ILOG_FEXT(whichfork); |
| 5717 | else if ((logflags & XFS_ILOG_FBROOT(whichfork)) && |
| 5718 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
| 5719 | logflags &= ~XFS_ILOG_FBROOT(whichfork); |
| 5720 | /* |
| 5721 | * Log inode even in the error case, if the transaction |
| 5722 | * is dirty we'll need to shut down the filesystem. |
| 5723 | */ |
| 5724 | if (logflags) |
| 5725 | xfs_trans_log_inode(tp, ip, logflags); |
| 5726 | if (cur) { |
| 5727 | if (!error) { |
| 5728 | *firstblock = cur->bc_private.b.firstblock; |
| 5729 | cur->bc_private.b.allocated = 0; |
| 5730 | } |
| 5731 | xfs_btree_del_cursor(cur, |
| 5732 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5733 | } |
| 5734 | return error; |
| 5735 | } |
| 5736 | |
| 5737 | /* |
| 5738 | * Fcntl interface to xfs_bmapi. |
| 5739 | */ |
| 5740 | int /* error code */ |
| 5741 | xfs_getbmap( |
| 5742 | bhv_desc_t *bdp, /* XFS behavior descriptor*/ |
| 5743 | struct getbmap *bmv, /* user bmap structure */ |
| 5744 | void __user *ap, /* pointer to user's array */ |
| 5745 | int interface) /* interface flags */ |
| 5746 | { |
| 5747 | __int64_t bmvend; /* last block requested */ |
| 5748 | int error; /* return value */ |
| 5749 | __int64_t fixlen; /* length for -1 case */ |
| 5750 | int i; /* extent number */ |
| 5751 | xfs_inode_t *ip; /* xfs incore inode pointer */ |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 5752 | bhv_vnode_t *vp; /* corresponding vnode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | int lock; /* lock state */ |
| 5754 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5755 | xfs_mount_t *mp; /* file system mount point */ |
| 5756 | int nex; /* # of user extents can do */ |
| 5757 | int nexleft; /* # of user extents left */ |
| 5758 | int subnex; /* # of bmapi's can do */ |
| 5759 | int nmap; /* number of map entries */ |
| 5760 | struct getbmap out; /* output structure */ |
| 5761 | int whichfork; /* data or attr fork */ |
| 5762 | int prealloced; /* this is a file with |
| 5763 | * preallocated data space */ |
| 5764 | int sh_unwritten; /* true, if unwritten */ |
| 5765 | /* extents listed separately */ |
| 5766 | int bmapi_flags; /* flags for xfs_bmapi */ |
| 5767 | __int32_t oflags; /* getbmapx bmv_oflags field */ |
| 5768 | |
| 5769 | vp = BHV_TO_VNODE(bdp); |
| 5770 | ip = XFS_BHVTOI(bdp); |
| 5771 | mp = ip->i_mount; |
| 5772 | |
| 5773 | whichfork = interface & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5774 | sh_unwritten = (interface & BMV_IF_PREALLOC) != 0; |
| 5775 | |
| 5776 | /* If the BMV_IF_NO_DMAPI_READ interface bit specified, do not |
| 5777 | * generate a DMAPI read event. Otherwise, if the DM_EVENT_READ |
| 5778 | * bit is set for the file, generate a read event in order |
| 5779 | * that the DMAPI application may do its thing before we return |
| 5780 | * the extents. Usually this means restoring user file data to |
| 5781 | * regions of the file that look like holes. |
| 5782 | * |
| 5783 | * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify |
| 5784 | * BMV_IF_NO_DMAPI_READ so that read events are generated. |
| 5785 | * If this were not true, callers of ioctl( XFS_IOC_GETBMAP ) |
| 5786 | * could misinterpret holes in a DMAPI file as true holes, |
| 5787 | * when in fact they may represent offline user data. |
| 5788 | */ |
| 5789 | if ( (interface & BMV_IF_NO_DMAPI_READ) == 0 |
| 5790 | && DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_READ) |
| 5791 | && whichfork == XFS_DATA_FORK) { |
| 5792 | |
| 5793 | error = XFS_SEND_DATA(mp, DM_EVENT_READ, vp, 0, 0, 0, NULL); |
| 5794 | if (error) |
| 5795 | return XFS_ERROR(error); |
| 5796 | } |
| 5797 | |
| 5798 | if (whichfork == XFS_ATTR_FORK) { |
| 5799 | if (XFS_IFORK_Q(ip)) { |
| 5800 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5801 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5802 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5803 | return XFS_ERROR(EINVAL); |
| 5804 | } else if (unlikely( |
| 5805 | ip->i_d.di_aformat != 0 && |
| 5806 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5807 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5808 | ip->i_mount); |
| 5809 | return XFS_ERROR(EFSCORRUPTED); |
| 5810 | } |
| 5811 | } else if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5812 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5813 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5814 | return XFS_ERROR(EINVAL); |
| 5815 | if (whichfork == XFS_DATA_FORK) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5816 | if ((ip->i_d.di_extsize && (ip->i_d.di_flags & |
| 5817 | (XFS_DIFLAG_REALTIME|XFS_DIFLAG_EXTSIZE))) || |
| 5818 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | prealloced = 1; |
| 5820 | fixlen = XFS_MAXIOFFSET(mp); |
| 5821 | } else { |
| 5822 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame^] | 5823 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | } |
| 5825 | } else { |
| 5826 | prealloced = 0; |
| 5827 | fixlen = 1LL << 32; |
| 5828 | } |
| 5829 | |
| 5830 | if (bmv->bmv_length == -1) { |
| 5831 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
| 5832 | bmv->bmv_length = MAX( (__int64_t)(fixlen - bmv->bmv_offset), |
| 5833 | (__int64_t)0); |
| 5834 | } else if (bmv->bmv_length < 0) |
| 5835 | return XFS_ERROR(EINVAL); |
| 5836 | if (bmv->bmv_length == 0) { |
| 5837 | bmv->bmv_entries = 0; |
| 5838 | return 0; |
| 5839 | } |
| 5840 | nex = bmv->bmv_count - 1; |
| 5841 | if (nex <= 0) |
| 5842 | return XFS_ERROR(EINVAL); |
| 5843 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5844 | |
| 5845 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
| 5846 | |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame^] | 5847 | if (whichfork == XFS_DATA_FORK && |
| 5848 | (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5849 | /* xfs_fsize_t last_byte = xfs_file_last_byte(ip); */ |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 5850 | error = bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | ASSERT(whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0); |
| 5854 | |
| 5855 | lock = xfs_ilock_map_shared(ip); |
| 5856 | |
| 5857 | /* |
| 5858 | * Don't let nex be bigger than the number of extents |
| 5859 | * we can have assuming alternating holes and real extents. |
| 5860 | */ |
| 5861 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5862 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5863 | |
| 5864 | bmapi_flags = XFS_BMAPI_AFLAG(whichfork) | |
| 5865 | ((sh_unwritten) ? 0 : XFS_BMAPI_IGSTATE); |
| 5866 | |
| 5867 | /* |
| 5868 | * Allocate enough space to handle "subnex" maps at a time. |
| 5869 | */ |
| 5870 | subnex = 16; |
| 5871 | map = kmem_alloc(subnex * sizeof(*map), KM_SLEEP); |
| 5872 | |
| 5873 | bmv->bmv_entries = 0; |
| 5874 | |
| 5875 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0) { |
| 5876 | error = 0; |
| 5877 | goto unlock_and_return; |
| 5878 | } |
| 5879 | |
| 5880 | nexleft = nex; |
| 5881 | |
| 5882 | do { |
| 5883 | nmap = (nexleft > subnex) ? subnex : nexleft; |
| 5884 | error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5885 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5886 | bmapi_flags, NULL, 0, map, &nmap, |
| 5887 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | if (error) |
| 5889 | goto unlock_and_return; |
| 5890 | ASSERT(nmap <= subnex); |
| 5891 | |
| 5892 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
| 5893 | nexleft--; |
| 5894 | oflags = (map[i].br_state == XFS_EXT_UNWRITTEN) ? |
| 5895 | BMV_OF_PREALLOC : 0; |
| 5896 | out.bmv_offset = XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5897 | out.bmv_length = XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5898 | ASSERT(map[i].br_startblock != DELAYSTARTBLOCK); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5899 | if (map[i].br_startblock == HOLESTARTBLOCK && |
| 5900 | ((prealloced && out.bmv_offset + out.bmv_length == bmvend) || |
| 5901 | whichfork == XFS_ATTR_FORK )) { |
| 5902 | /* |
| 5903 | * came to hole at end of file or the end of |
| 5904 | attribute fork |
| 5905 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | goto unlock_and_return; |
| 5907 | } else { |
| 5908 | out.bmv_block = |
| 5909 | (map[i].br_startblock == HOLESTARTBLOCK) ? |
| 5910 | -1 : |
| 5911 | XFS_FSB_TO_DB(ip, map[i].br_startblock); |
| 5912 | |
| 5913 | /* return either getbmap/getbmapx structure. */ |
| 5914 | if (interface & BMV_IF_EXTENDED) { |
| 5915 | struct getbmapx outx; |
| 5916 | |
| 5917 | GETBMAP_CONVERT(out,outx); |
| 5918 | outx.bmv_oflags = oflags; |
| 5919 | outx.bmv_unused1 = outx.bmv_unused2 = 0; |
| 5920 | if (copy_to_user(ap, &outx, |
| 5921 | sizeof(outx))) { |
| 5922 | error = XFS_ERROR(EFAULT); |
| 5923 | goto unlock_and_return; |
| 5924 | } |
| 5925 | } else { |
| 5926 | if (copy_to_user(ap, &out, |
| 5927 | sizeof(out))) { |
| 5928 | error = XFS_ERROR(EFAULT); |
| 5929 | goto unlock_and_return; |
| 5930 | } |
| 5931 | } |
| 5932 | bmv->bmv_offset = |
| 5933 | out.bmv_offset + out.bmv_length; |
| 5934 | bmv->bmv_length = MAX((__int64_t)0, |
| 5935 | (__int64_t)(bmvend - bmv->bmv_offset)); |
| 5936 | bmv->bmv_entries++; |
| 5937 | ap = (interface & BMV_IF_EXTENDED) ? |
| 5938 | (void __user *) |
| 5939 | ((struct getbmapx __user *)ap + 1) : |
| 5940 | (void __user *) |
| 5941 | ((struct getbmap __user *)ap + 1); |
| 5942 | } |
| 5943 | } |
| 5944 | } while (nmap && nexleft && bmv->bmv_length); |
| 5945 | |
| 5946 | unlock_and_return: |
| 5947 | xfs_iunlock_map_shared(ip, lock); |
| 5948 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
| 5949 | |
| 5950 | kmem_free(map, subnex * sizeof(*map)); |
| 5951 | |
| 5952 | return error; |
| 5953 | } |
| 5954 | |
| 5955 | /* |
| 5956 | * Check the last inode extent to determine whether this allocation will result |
| 5957 | * in blocks being allocated at the end of the file. When we allocate new data |
| 5958 | * blocks at the end of the file which do not start at the previous data block, |
| 5959 | * we will try to align the new blocks at stripe unit boundaries. |
| 5960 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 5961 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5962 | xfs_bmap_isaeof( |
| 5963 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5964 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 5965 | int whichfork, /* data or attribute fork */ |
| 5966 | char *aeof) /* return value */ |
| 5967 | { |
| 5968 | int error; /* error return value */ |
| 5969 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5970 | xfs_bmbt_rec_t *lastrec; /* extent record pointer */ |
| 5971 | xfs_extnum_t nextents; /* number of file extents */ |
| 5972 | xfs_bmbt_irec_t s; /* expanded extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | |
| 5974 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5975 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5976 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5977 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5978 | return error; |
| 5979 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5980 | if (nextents == 0) { |
| 5981 | *aeof = 1; |
| 5982 | return 0; |
| 5983 | } |
| 5984 | /* |
| 5985 | * Go to the last extent |
| 5986 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5987 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | xfs_bmbt_get_all(lastrec, &s); |
| 5989 | /* |
| 5990 | * Check we are allocating in the last extent (for delayed allocations) |
| 5991 | * or past the last extent for non-delayed allocations. |
| 5992 | */ |
| 5993 | *aeof = (off >= s.br_startoff && |
| 5994 | off < s.br_startoff + s.br_blockcount && |
| 5995 | ISNULLSTARTBLOCK(s.br_startblock)) || |
| 5996 | off >= s.br_startoff + s.br_blockcount; |
| 5997 | return 0; |
| 5998 | } |
| 5999 | |
| 6000 | /* |
| 6001 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 6002 | * the allocation to a stripe unit boundary. |
| 6003 | */ |
| 6004 | int /* error */ |
| 6005 | xfs_bmap_eof( |
| 6006 | xfs_inode_t *ip, /* incore inode pointer */ |
| 6007 | xfs_fileoff_t endoff, /* file offset in fsblocks */ |
| 6008 | int whichfork, /* data or attribute fork */ |
| 6009 | int *eof) /* result value */ |
| 6010 | { |
| 6011 | xfs_fsblock_t blockcount; /* extent block count */ |
| 6012 | int error; /* error return value */ |
| 6013 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6014 | xfs_bmbt_rec_t *lastrec; /* extent record pointer */ |
| 6015 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | xfs_fileoff_t startoff; /* extent starting file offset */ |
| 6017 | |
| 6018 | ASSERT(whichfork == XFS_DATA_FORK); |
| 6019 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6020 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 6021 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 6022 | return error; |
| 6023 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 6024 | if (nextents == 0) { |
| 6025 | *eof = 1; |
| 6026 | return 0; |
| 6027 | } |
| 6028 | /* |
| 6029 | * Go to the last extent |
| 6030 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6031 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | startoff = xfs_bmbt_get_startoff(lastrec); |
| 6033 | blockcount = xfs_bmbt_get_blockcount(lastrec); |
| 6034 | *eof = endoff >= startoff + blockcount; |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
| 6038 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | STATIC |
| 6040 | xfs_buf_t * |
| 6041 | xfs_bmap_get_bp( |
| 6042 | xfs_btree_cur_t *cur, |
| 6043 | xfs_fsblock_t bno) |
| 6044 | { |
| 6045 | int i; |
| 6046 | xfs_buf_t *bp; |
| 6047 | |
| 6048 | if (!cur) |
| 6049 | return(NULL); |
| 6050 | |
| 6051 | bp = NULL; |
| 6052 | for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 6053 | bp = cur->bc_bufs[i]; |
| 6054 | if (!bp) break; |
| 6055 | if (XFS_BUF_ADDR(bp) == bno) |
| 6056 | break; /* Found it */ |
| 6057 | } |
| 6058 | if (i == XFS_BTREE_MAXLEVELS) |
| 6059 | bp = NULL; |
| 6060 | |
| 6061 | if (!bp) { /* Chase down all the log items to see if the bp is there */ |
| 6062 | xfs_log_item_chunk_t *licp; |
| 6063 | xfs_trans_t *tp; |
| 6064 | |
| 6065 | tp = cur->bc_tp; |
| 6066 | licp = &tp->t_items; |
| 6067 | while (!bp && licp != NULL) { |
| 6068 | if (XFS_LIC_ARE_ALL_FREE(licp)) { |
| 6069 | licp = licp->lic_next; |
| 6070 | continue; |
| 6071 | } |
| 6072 | for (i = 0; i < licp->lic_unused; i++) { |
| 6073 | xfs_log_item_desc_t *lidp; |
| 6074 | xfs_log_item_t *lip; |
| 6075 | xfs_buf_log_item_t *bip; |
| 6076 | xfs_buf_t *lbp; |
| 6077 | |
| 6078 | if (XFS_LIC_ISFREE(licp, i)) { |
| 6079 | continue; |
| 6080 | } |
| 6081 | |
| 6082 | lidp = XFS_LIC_SLOT(licp, i); |
| 6083 | lip = lidp->lid_item; |
| 6084 | if (lip->li_type != XFS_LI_BUF) |
| 6085 | continue; |
| 6086 | |
| 6087 | bip = (xfs_buf_log_item_t *)lip; |
| 6088 | lbp = bip->bli_buf; |
| 6089 | |
| 6090 | if (XFS_BUF_ADDR(lbp) == bno) { |
| 6091 | bp = lbp; |
| 6092 | break; /* Found it */ |
| 6093 | } |
| 6094 | } |
| 6095 | licp = licp->lic_next; |
| 6096 | } |
| 6097 | } |
| 6098 | return(bp); |
| 6099 | } |
| 6100 | |
| 6101 | void |
| 6102 | xfs_check_block( |
| 6103 | xfs_bmbt_block_t *block, |
| 6104 | xfs_mount_t *mp, |
| 6105 | int root, |
| 6106 | short sz) |
| 6107 | { |
| 6108 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6109 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | xfs_bmbt_key_t *prevp, *keyp; |
| 6111 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6112 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | |
| 6114 | prevp = NULL; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6115 | for( i = 1; i <= be16_to_cpu(block->bb_numrecs); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | dmxr = mp->m_bmap_dmxr[0]; |
| 6117 | |
| 6118 | if (root) { |
| 6119 | keyp = XFS_BMAP_BROOT_KEY_ADDR(block, i, sz); |
| 6120 | } else { |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6121 | keyp = XFS_BTREE_KEY_ADDR(xfs_bmbt, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | } |
| 6123 | |
| 6124 | if (prevp) { |
| 6125 | xfs_btree_check_key(XFS_BTNUM_BMAP, prevp, keyp); |
| 6126 | } |
| 6127 | prevp = keyp; |
| 6128 | |
| 6129 | /* |
| 6130 | * Compare the block numbers to see if there are dups. |
| 6131 | */ |
| 6132 | |
| 6133 | if (root) { |
| 6134 | pp = XFS_BMAP_BROOT_PTR_ADDR(block, i, sz); |
| 6135 | } else { |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6136 | pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, i, dmxr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6138 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | if (root) { |
| 6140 | thispa = XFS_BMAP_BROOT_PTR_ADDR(block, j, sz); |
| 6141 | } else { |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6142 | thispa = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, j, |
| 6143 | dmxr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | } |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6145 | if (*thispa == *pp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld", |
| 6147 | __FUNCTION__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6148 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6149 | panic("%s: ptrs are equal in node\n", |
| 6150 | __FUNCTION__); |
| 6151 | } |
| 6152 | } |
| 6153 | } |
| 6154 | } |
| 6155 | |
| 6156 | /* |
| 6157 | * Check that the extents for the inode ip are in the right order in all |
| 6158 | * btree leaves. |
| 6159 | */ |
| 6160 | |
| 6161 | STATIC void |
| 6162 | xfs_bmap_check_leaf_extents( |
| 6163 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 6164 | xfs_inode_t *ip, /* incore inode pointer */ |
| 6165 | int whichfork) /* data or attr fork */ |
| 6166 | { |
| 6167 | xfs_bmbt_block_t *block; /* current btree block */ |
| 6168 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6169 | xfs_buf_t *bp; /* buffer for "block" */ |
| 6170 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6171 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | xfs_ifork_t *ifp; /* fork structure */ |
| 6173 | int level; /* btree level, for checking */ |
| 6174 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6175 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6176 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
| 6177 | xfs_bmbt_rec_t *lastp; /* pointer to previous extent */ |
| 6178 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | int bp_release = 0; |
| 6180 | |
| 6181 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 6182 | return; |
| 6183 | } |
| 6184 | |
| 6185 | bno = NULLFSBLOCK; |
| 6186 | mp = ip->i_mount; |
| 6187 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6188 | block = ifp->if_broot; |
| 6189 | /* |
| 6190 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6191 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6192 | level = be16_to_cpu(block->bb_level); |
| 6193 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6194 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
| 6195 | pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6196 | bno = be64_to_cpu(*pp); |
| 6197 | |
| 6198 | ASSERT(bno != NULLDFSBNO); |
| 6199 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6200 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 6201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | /* |
| 6203 | * Go down the tree until leaf level is reached, following the first |
| 6204 | * pointer (leftmost) at each level. |
| 6205 | */ |
| 6206 | while (level-- > 0) { |
| 6207 | /* See if buf is in cur first */ |
| 6208 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6209 | if (bp) { |
| 6210 | bp_release = 0; |
| 6211 | } else { |
| 6212 | bp_release = 1; |
| 6213 | } |
| 6214 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6215 | XFS_BMAP_BTREE_REF))) |
| 6216 | goto error_norelse; |
| 6217 | block = XFS_BUF_TO_BMBT_BLOCK(bp); |
| 6218 | XFS_WANT_CORRUPTED_GOTO( |
| 6219 | XFS_BMAP_SANITY_CHECK(mp, block, level), |
| 6220 | error0); |
| 6221 | if (level == 0) |
| 6222 | break; |
| 6223 | |
| 6224 | /* |
| 6225 | * Check this block for basic sanity (increasing keys and |
| 6226 | * no duplicate blocks). |
| 6227 | */ |
| 6228 | |
| 6229 | xfs_check_block(block, mp, 0, 0); |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6230 | pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6231 | bno = be64_to_cpu(*pp); |
| 6232 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | if (bp_release) { |
| 6234 | bp_release = 0; |
| 6235 | xfs_trans_brelse(NULL, bp); |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | /* |
| 6240 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 6241 | */ |
| 6242 | i = 0; |
| 6243 | |
| 6244 | /* |
| 6245 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 6246 | */ |
| 6247 | lastp = NULL; |
| 6248 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | xfs_fsblock_t nextbno; |
| 6250 | xfs_extnum_t num_recs; |
| 6251 | |
| 6252 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6253 | num_recs = be16_to_cpu(block->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | |
| 6255 | /* |
| 6256 | * Read-ahead the next leaf block, if any. |
| 6257 | */ |
| 6258 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6259 | nextbno = be64_to_cpu(block->bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | |
| 6261 | /* |
| 6262 | * Check all the extents to make sure they are OK. |
| 6263 | * If we had a previous block, the last entry should |
| 6264 | * conform with the first entry in this one. |
| 6265 | */ |
| 6266 | |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6267 | ep = XFS_BTREE_REC_ADDR(xfs_bmbt, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6268 | for (j = 1; j < num_recs; j++) { |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6269 | nextp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, j + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | if (lastp) { |
| 6271 | xfs_btree_check_rec(XFS_BTNUM_BMAP, |
| 6272 | (void *)lastp, (void *)ep); |
| 6273 | } |
| 6274 | xfs_btree_check_rec(XFS_BTNUM_BMAP, (void *)ep, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6275 | (void *)(nextp)); |
| 6276 | lastp = ep; |
| 6277 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | |
| 6280 | i += num_recs; |
| 6281 | if (bp_release) { |
| 6282 | bp_release = 0; |
| 6283 | xfs_trans_brelse(NULL, bp); |
| 6284 | } |
| 6285 | bno = nextbno; |
| 6286 | /* |
| 6287 | * If we've reached the end, stop. |
| 6288 | */ |
| 6289 | if (bno == NULLFSBLOCK) |
| 6290 | break; |
| 6291 | |
| 6292 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6293 | if (bp) { |
| 6294 | bp_release = 0; |
| 6295 | } else { |
| 6296 | bp_release = 1; |
| 6297 | } |
| 6298 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6299 | XFS_BMAP_BTREE_REF))) |
| 6300 | goto error_norelse; |
| 6301 | block = XFS_BUF_TO_BMBT_BLOCK(bp); |
| 6302 | } |
| 6303 | if (bp_release) { |
| 6304 | bp_release = 0; |
| 6305 | xfs_trans_brelse(NULL, bp); |
| 6306 | } |
| 6307 | return; |
| 6308 | |
| 6309 | error0: |
| 6310 | cmn_err(CE_WARN, "%s: at error0", __FUNCTION__); |
| 6311 | if (bp_release) |
| 6312 | xfs_trans_brelse(NULL, bp); |
| 6313 | error_norelse: |
| 6314 | cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents", |
Christoph Hellwig | da1650a | 2005-11-02 10:21:35 +1100 | [diff] [blame] | 6315 | __FUNCTION__, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | panic("%s: CORRUPTED BTREE OR SOMETHING", __FUNCTION__); |
| 6317 | return; |
| 6318 | } |
| 6319 | #endif |
| 6320 | |
| 6321 | /* |
| 6322 | * Count fsblocks of the given fork. |
| 6323 | */ |
| 6324 | int /* error */ |
| 6325 | xfs_bmap_count_blocks( |
| 6326 | xfs_trans_t *tp, /* transaction pointer */ |
| 6327 | xfs_inode_t *ip, /* incore inode */ |
| 6328 | int whichfork, /* data or attr fork */ |
| 6329 | int *count) /* out: count of blocks */ |
| 6330 | { |
| 6331 | xfs_bmbt_block_t *block; /* current btree block */ |
| 6332 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6333 | xfs_ifork_t *ifp; /* fork structure */ |
| 6334 | int level; /* btree level, for checking */ |
| 6335 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6336 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6337 | |
| 6338 | bno = NULLFSBLOCK; |
| 6339 | mp = ip->i_mount; |
| 6340 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6341 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6342 | if (unlikely(xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
| 6344 | count) < 0)) { |
| 6345 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(1)", |
| 6346 | XFS_ERRLEVEL_LOW, mp); |
| 6347 | return XFS_ERROR(EFSCORRUPTED); |
| 6348 | } |
| 6349 | return 0; |
| 6350 | } |
| 6351 | |
| 6352 | /* |
| 6353 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6354 | */ |
| 6355 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6356 | level = be16_to_cpu(block->bb_level); |
| 6357 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6359 | bno = be64_to_cpu(*pp); |
| 6360 | ASSERT(bno != NULLDFSBNO); |
| 6361 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6362 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6364 | 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] | 6365 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 6366 | mp); |
| 6367 | return XFS_ERROR(EFSCORRUPTED); |
| 6368 | } |
| 6369 | |
| 6370 | return 0; |
| 6371 | } |
| 6372 | |
| 6373 | /* |
| 6374 | * Recursively walks each level of a btree |
| 6375 | * to count total fsblocks is use. |
| 6376 | */ |
| 6377 | int /* error */ |
| 6378 | xfs_bmap_count_tree( |
| 6379 | xfs_mount_t *mp, /* file system mount point */ |
| 6380 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6381 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | xfs_fsblock_t blockno, /* file system block number */ |
| 6383 | int levelin, /* level in btree */ |
| 6384 | int *count) /* Count of blocks */ |
| 6385 | { |
| 6386 | int error; |
| 6387 | xfs_buf_t *bp, *nbp; |
| 6388 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6389 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | xfs_fsblock_t bno = blockno; |
| 6391 | xfs_fsblock_t nextbno; |
| 6392 | xfs_bmbt_block_t *block, *nextblock; |
| 6393 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | |
| 6395 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 6396 | return error; |
| 6397 | *count += 1; |
| 6398 | block = XFS_BUF_TO_BMBT_BLOCK(bp); |
| 6399 | |
| 6400 | if (--level) { |
| 6401 | /* Not at node above leafs, count this level of nodes */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6402 | nextbno = be64_to_cpu(block->bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | while (nextbno != NULLFSBLOCK) { |
| 6404 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6405 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6406 | return error; |
| 6407 | *count += 1; |
| 6408 | nextblock = XFS_BUF_TO_BMBT_BLOCK(nbp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6409 | nextbno = be64_to_cpu(nextblock->bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | xfs_trans_brelse(tp, nbp); |
| 6411 | } |
| 6412 | |
| 6413 | /* Dive to the next level */ |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6414 | pp = XFS_BTREE_PTR_ADDR(xfs_bmbt, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6415 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6417 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | xfs_trans_brelse(tp, bp); |
| 6419 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6420 | XFS_ERRLEVEL_LOW, mp); |
| 6421 | return XFS_ERROR(EFSCORRUPTED); |
| 6422 | } |
| 6423 | xfs_trans_brelse(tp, bp); |
| 6424 | } else { |
| 6425 | /* count all level 1 nodes and their leaves */ |
| 6426 | for (;;) { |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6427 | nextbno = be64_to_cpu(block->bb_rightsib); |
| 6428 | numrecs = be16_to_cpu(block->bb_numrecs); |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 6429 | if (unlikely(xfs_bmap_disk_count_leaves(0, |
| 6430 | block, numrecs, count) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6431 | xfs_trans_brelse(tp, bp); |
| 6432 | XFS_ERROR_REPORT("xfs_bmap_count_tree(2)", |
| 6433 | XFS_ERRLEVEL_LOW, mp); |
| 6434 | return XFS_ERROR(EFSCORRUPTED); |
| 6435 | } |
| 6436 | xfs_trans_brelse(tp, bp); |
| 6437 | if (nextbno == NULLFSBLOCK) |
| 6438 | break; |
| 6439 | bno = nextbno; |
| 6440 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6441 | XFS_BMAP_BTREE_REF))) |
| 6442 | return error; |
| 6443 | *count += 1; |
| 6444 | block = XFS_BUF_TO_BMBT_BLOCK(bp); |
| 6445 | } |
| 6446 | } |
| 6447 | return 0; |
| 6448 | } |
| 6449 | |
| 6450 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6451 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | */ |
| 6453 | int |
| 6454 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6455 | xfs_ifork_t *ifp, |
| 6456 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | int numrecs, |
| 6458 | int *count) |
| 6459 | { |
| 6460 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6461 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6463 | for (b = 0; b < numrecs; b++) { |
| 6464 | frp = xfs_iext_get_ext(ifp, idx + b); |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6465 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6466 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6467 | return 0; |
| 6468 | } |
| 6469 | |
| 6470 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6471 | * Count leaf blocks given a range of extent records originally |
| 6472 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6473 | */ |
| 6474 | int |
| 6475 | xfs_bmap_disk_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6476 | xfs_extnum_t idx, |
| 6477 | xfs_bmbt_block_t *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6478 | int numrecs, |
| 6479 | int *count) |
| 6480 | { |
| 6481 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6482 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6483 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6484 | for (b = 1; b <= numrecs; b++) { |
Eric Sandeen | 2c36dde | 2007-02-10 18:37:33 +1100 | [diff] [blame] | 6485 | frp = XFS_BTREE_REC_ADDR(xfs_bmbt, block, idx + b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | return 0; |
| 6489 | } |