Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2 | * Copyright (c) 2000-2006 Silicon Graphics, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_dir2.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_btree.h" |
| 37 | #include "xfs_dmapi.h" |
| 38 | #include "xfs_mount.h" |
| 39 | #include "xfs_ialloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include "xfs_itable.h" |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 41 | #include "xfs_dir2_data.h" |
| 42 | #include "xfs_dir2_leaf.h" |
| 43 | #include "xfs_dir2_block.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 44 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include "xfs_extfree_item.h" |
| 46 | #include "xfs_alloc.h" |
| 47 | #include "xfs_bmap.h" |
| 48 | #include "xfs_rtalloc.h" |
| 49 | #include "xfs_error.h" |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 50 | #include "xfs_attr_leaf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "xfs_rw.h" |
| 52 | #include "xfs_quota.h" |
| 53 | #include "xfs_trans_space.h" |
| 54 | #include "xfs_buf_item.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 55 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 56 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 57 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | |
| 60 | #ifdef DEBUG |
| 61 | STATIC void |
| 62 | xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork); |
| 63 | #endif |
| 64 | |
| 65 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 66 | |
| 67 | /* |
| 68 | * Prototypes for internal bmap routines. |
| 69 | */ |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 74 | */ |
| 75 | STATIC int /* error */ |
| 76 | xfs_bmap_add_attrfork_extents( |
| 77 | xfs_trans_t *tp, /* transaction pointer */ |
| 78 | xfs_inode_t *ip, /* incore inode pointer */ |
| 79 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 80 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 81 | int *flags); /* inode logging flags */ |
| 82 | |
| 83 | /* |
| 84 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 85 | */ |
| 86 | STATIC int /* error */ |
| 87 | xfs_bmap_add_attrfork_local( |
| 88 | xfs_trans_t *tp, /* transaction pointer */ |
| 89 | xfs_inode_t *ip, /* incore inode pointer */ |
| 90 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 91 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 92 | int *flags); /* inode logging flags */ |
| 93 | |
| 94 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 95 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | * after allocating space (or doing a delayed allocation). |
| 97 | */ |
| 98 | STATIC int /* error */ |
| 99 | xfs_bmap_add_extent( |
| 100 | xfs_inode_t *ip, /* incore inode pointer */ |
| 101 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 102 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 103 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 105 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 106 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 107 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | int whichfork, /* data or attr fork */ |
| 109 | int rsvd); /* OK to allocate reserved blocks */ |
| 110 | |
| 111 | /* |
| 112 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 113 | * allocation to a real allocation. |
| 114 | */ |
| 115 | STATIC int /* error */ |
| 116 | xfs_bmap_add_extent_delay_real( |
| 117 | xfs_inode_t *ip, /* incore inode pointer */ |
| 118 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 119 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 120 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 122 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 123 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 124 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 125 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | int rsvd); /* OK to allocate reserved blocks */ |
| 127 | |
| 128 | /* |
| 129 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 130 | * to a delayed allocation. |
| 131 | */ |
| 132 | STATIC int /* error */ |
| 133 | xfs_bmap_add_extent_hole_delay( |
| 134 | xfs_inode_t *ip, /* incore inode pointer */ |
| 135 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 136 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 138 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int rsvd); /* OK to allocate reserved blocks */ |
| 140 | |
| 141 | /* |
| 142 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 143 | * to a real allocation. |
| 144 | */ |
| 145 | STATIC int /* error */ |
| 146 | xfs_bmap_add_extent_hole_real( |
| 147 | xfs_inode_t *ip, /* incore inode pointer */ |
| 148 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 149 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 150 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 152 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | int whichfork); /* data or attr fork */ |
| 154 | |
| 155 | /* |
| 156 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 157 | * allocation to a real allocation or vice versa. |
| 158 | */ |
| 159 | STATIC int /* error */ |
| 160 | xfs_bmap_add_extent_unwritten_real( |
| 161 | xfs_inode_t *ip, /* incore inode pointer */ |
| 162 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 163 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 164 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 165 | int *logflagsp, /* inode logging flags */ |
| 166 | xfs_extdelta_t *delta); /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 170 | * It figures out where to ask the underlying allocator to put the new extent. |
| 171 | */ |
| 172 | STATIC int /* error */ |
| 173 | xfs_bmap_alloc( |
| 174 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 175 | |
| 176 | /* |
| 177 | * Transform a btree format file with only one leaf node, where the |
| 178 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 179 | * 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] | 180 | * give up the space for the btree root and pitch the leaf block. |
| 181 | */ |
| 182 | STATIC int /* error */ |
| 183 | xfs_bmap_btree_to_extents( |
| 184 | xfs_trans_t *tp, /* transaction pointer */ |
| 185 | xfs_inode_t *ip, /* incore inode pointer */ |
| 186 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 187 | int *logflagsp, /* inode logging flags */ |
| 188 | int whichfork); /* data or attr fork */ |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 191 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | * after removing space (or undoing a delayed allocation). |
| 193 | */ |
| 194 | STATIC int /* error */ |
| 195 | xfs_bmap_del_extent( |
| 196 | xfs_inode_t *ip, /* incore inode pointer */ |
| 197 | xfs_trans_t *tp, /* current trans pointer */ |
| 198 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 199 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 200 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 201 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 203 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | int whichfork, /* data or attr fork */ |
| 205 | int rsvd); /* OK to allocate reserved blocks */ |
| 206 | |
| 207 | /* |
| 208 | * Remove the entry "free" from the free item list. Prev points to the |
| 209 | * previous entry, unless "free" is the head of the list. |
| 210 | */ |
| 211 | STATIC void |
| 212 | xfs_bmap_del_free( |
| 213 | xfs_bmap_free_t *flist, /* free item list header */ |
| 214 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 215 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 216 | |
| 217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * Convert an extents-format file into a btree-format file. |
| 219 | * The new file will have a root block (in the inode) and a single child block. |
| 220 | */ |
| 221 | STATIC int /* error */ |
| 222 | xfs_bmap_extents_to_btree( |
| 223 | xfs_trans_t *tp, /* transaction pointer */ |
| 224 | xfs_inode_t *ip, /* incore inode pointer */ |
| 225 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 226 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 227 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 228 | int wasdel, /* converting a delayed alloc */ |
| 229 | int *logflagsp, /* inode logging flags */ |
| 230 | int whichfork); /* data or attr fork */ |
| 231 | |
| 232 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | * Convert a local file to an extents file. |
| 234 | * This code is sort of bogus, since the file data needs to get |
| 235 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 236 | */ |
| 237 | STATIC int /* error */ |
| 238 | xfs_bmap_local_to_extents( |
| 239 | xfs_trans_t *tp, /* transaction pointer */ |
| 240 | xfs_inode_t *ip, /* incore inode pointer */ |
| 241 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 242 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 243 | int *logflagsp, /* inode logging flags */ |
| 244 | int whichfork); /* data or attr fork */ |
| 245 | |
| 246 | /* |
| 247 | * Search the extents list for the inode, for the extent containing bno. |
| 248 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 249 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 250 | * Else, *lastxp will be set to the index of the found |
| 251 | * entry; *gotp will contain the entry. |
| 252 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 253 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | xfs_bmap_search_extents( |
| 255 | xfs_inode_t *ip, /* incore inode pointer */ |
| 256 | xfs_fileoff_t bno, /* block number searched for */ |
| 257 | int whichfork, /* data or attr fork */ |
| 258 | int *eofp, /* out: end of file found */ |
| 259 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 260 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 261 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 262 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 263 | /* |
| 264 | * Check the last inode extent to determine whether this allocation will result |
| 265 | * in blocks being allocated at the end of the file. When we allocate new data |
| 266 | * blocks at the end of the file which do not start at the previous data block, |
| 267 | * we will try to align the new blocks at stripe unit boundaries. |
| 268 | */ |
| 269 | STATIC int /* error */ |
| 270 | xfs_bmap_isaeof( |
| 271 | xfs_inode_t *ip, /* incore inode pointer */ |
| 272 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 273 | int whichfork, /* data or attribute fork */ |
| 274 | char *aeof); /* return value */ |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Compute the worst-case number of indirect blocks that will be used |
| 278 | * for ip's delayed extent of length "len". |
| 279 | */ |
| 280 | STATIC xfs_filblks_t |
| 281 | xfs_bmap_worst_indlen( |
| 282 | xfs_inode_t *ip, /* incore inode pointer */ |
| 283 | xfs_filblks_t len); /* delayed extent length */ |
| 284 | |
| 285 | #ifdef DEBUG |
| 286 | /* |
| 287 | * Perform various validation checks on the values being returned |
| 288 | * from xfs_bmapi(). |
| 289 | */ |
| 290 | STATIC void |
| 291 | xfs_bmap_validate_ret( |
| 292 | xfs_fileoff_t bno, |
| 293 | xfs_filblks_t len, |
| 294 | int flags, |
| 295 | xfs_bmbt_irec_t *mval, |
| 296 | int nmap, |
| 297 | int ret_nmap); |
| 298 | #else |
| 299 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 300 | #endif /* DEBUG */ |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | STATIC int |
| 303 | xfs_bmap_count_tree( |
| 304 | xfs_mount_t *mp, |
| 305 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 306 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | xfs_fsblock_t blockno, |
| 308 | int levelin, |
| 309 | int *count); |
| 310 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 311 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 313 | xfs_ifork_t *ifp, |
| 314 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | int numrecs, |
| 316 | int *count); |
| 317 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 318 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 319 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 320 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 321 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 322 | int numrecs, |
| 323 | int *count); |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* |
| 326 | * Bmap internal routines. |
| 327 | */ |
| 328 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 329 | STATIC int /* error */ |
| 330 | xfs_bmbt_lookup_eq( |
| 331 | struct xfs_btree_cur *cur, |
| 332 | xfs_fileoff_t off, |
| 333 | xfs_fsblock_t bno, |
| 334 | xfs_filblks_t len, |
| 335 | int *stat) /* success/failure */ |
| 336 | { |
| 337 | cur->bc_rec.b.br_startoff = off; |
| 338 | cur->bc_rec.b.br_startblock = bno; |
| 339 | cur->bc_rec.b.br_blockcount = len; |
| 340 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 341 | } |
| 342 | |
| 343 | STATIC int /* error */ |
| 344 | xfs_bmbt_lookup_ge( |
| 345 | struct xfs_btree_cur *cur, |
| 346 | xfs_fileoff_t off, |
| 347 | xfs_fsblock_t bno, |
| 348 | xfs_filblks_t len, |
| 349 | int *stat) /* success/failure */ |
| 350 | { |
| 351 | cur->bc_rec.b.br_startoff = off; |
| 352 | cur->bc_rec.b.br_startblock = bno; |
| 353 | cur->bc_rec.b.br_blockcount = len; |
| 354 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 355 | } |
| 356 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 357 | /* |
| 358 | * Update the record referred to by cur to the value given |
| 359 | * by [off, bno, len, state]. |
| 360 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 361 | */ |
| 362 | STATIC int |
| 363 | xfs_bmbt_update( |
| 364 | struct xfs_btree_cur *cur, |
| 365 | xfs_fileoff_t off, |
| 366 | xfs_fsblock_t bno, |
| 367 | xfs_filblks_t len, |
| 368 | xfs_exntst_t state) |
| 369 | { |
| 370 | union xfs_btree_rec rec; |
| 371 | |
| 372 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 373 | return xfs_btree_update(cur, &rec); |
| 374 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /* |
| 377 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 378 | */ |
| 379 | STATIC int /* error */ |
| 380 | xfs_bmap_add_attrfork_btree( |
| 381 | xfs_trans_t *tp, /* transaction pointer */ |
| 382 | xfs_inode_t *ip, /* incore inode pointer */ |
| 383 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 384 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 385 | int *flags) /* inode logging flags */ |
| 386 | { |
| 387 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 388 | int error; /* error return value */ |
| 389 | xfs_mount_t *mp; /* file system mount struct */ |
| 390 | int stat; /* newroot status */ |
| 391 | |
| 392 | mp = ip->i_mount; |
| 393 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 394 | *flags |= XFS_ILOG_DBROOT; |
| 395 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 396 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | cur->bc_private.b.flist = flist; |
| 398 | cur->bc_private.b.firstblock = *firstblock; |
| 399 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 400 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 401 | /* must be at least one entry */ |
| 402 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 403 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | goto error0; |
| 405 | if (stat == 0) { |
| 406 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 407 | return XFS_ERROR(ENOSPC); |
| 408 | } |
| 409 | *firstblock = cur->bc_private.b.firstblock; |
| 410 | cur->bc_private.b.allocated = 0; |
| 411 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 412 | } |
| 413 | return 0; |
| 414 | error0: |
| 415 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 416 | return error; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 421 | */ |
| 422 | STATIC int /* error */ |
| 423 | xfs_bmap_add_attrfork_extents( |
| 424 | xfs_trans_t *tp, /* transaction pointer */ |
| 425 | xfs_inode_t *ip, /* incore inode pointer */ |
| 426 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 427 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 428 | int *flags) /* inode logging flags */ |
| 429 | { |
| 430 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 431 | int error; /* error return value */ |
| 432 | |
| 433 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 434 | return 0; |
| 435 | cur = NULL; |
| 436 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 437 | flags, XFS_DATA_FORK); |
| 438 | if (cur) { |
| 439 | cur->bc_private.b.allocated = 0; |
| 440 | xfs_btree_del_cursor(cur, |
| 441 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 442 | } |
| 443 | return error; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 448 | */ |
| 449 | STATIC int /* error */ |
| 450 | xfs_bmap_add_attrfork_local( |
| 451 | xfs_trans_t *tp, /* transaction pointer */ |
| 452 | xfs_inode_t *ip, /* incore inode pointer */ |
| 453 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 454 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 455 | int *flags) /* inode logging flags */ |
| 456 | { |
| 457 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 458 | int error; /* error return value */ |
| 459 | xfs_mount_t *mp; /* mount structure pointer */ |
| 460 | |
| 461 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 462 | return 0; |
| 463 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 464 | mp = ip->i_mount; |
| 465 | memset(&dargs, 0, sizeof(dargs)); |
| 466 | dargs.dp = ip; |
| 467 | dargs.firstblock = firstblock; |
| 468 | dargs.flist = flist; |
| 469 | dargs.total = mp->m_dirblkfsbs; |
| 470 | dargs.whichfork = XFS_DATA_FORK; |
| 471 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 472 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } else |
| 474 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 475 | XFS_DATA_FORK); |
| 476 | return error; |
| 477 | } |
| 478 | |
| 479 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 480 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | * after allocating space (or doing a delayed allocation). |
| 482 | */ |
| 483 | STATIC int /* error */ |
| 484 | xfs_bmap_add_extent( |
| 485 | xfs_inode_t *ip, /* incore inode pointer */ |
| 486 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 487 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 488 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 490 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 491 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 492 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | int whichfork, /* data or attr fork */ |
| 494 | int rsvd) /* OK to use reserved data blocks */ |
| 495 | { |
| 496 | xfs_btree_cur_t *cur; /* btree cursor or null */ |
| 497 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 498 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 499 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | xfs_ifork_t *ifp; /* inode fork ptr */ |
| 501 | int logflags; /* returned value */ |
| 502 | xfs_extnum_t nextents; /* number of extents in file now */ |
| 503 | |
| 504 | XFS_STATS_INC(xs_add_exlist); |
| 505 | cur = *curp; |
| 506 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 507 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 508 | ASSERT(idx <= nextents); |
| 509 | da_old = da_new = 0; |
| 510 | error = 0; |
| 511 | /* |
| 512 | * This is the first extent added to a new/empty file. |
| 513 | * Special case this one, so other routines get to assume there are |
| 514 | * already extents in the list. |
| 515 | */ |
| 516 | if (nextents == 0) { |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 517 | xfs_iext_insert(ip, 0, 1, new, |
| 518 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | ASSERT(cur == NULL); |
| 521 | ifp->if_lastex = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 522 | if (!isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 524 | logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } else |
| 526 | logflags = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 527 | /* DELTA: single new extent */ |
| 528 | if (delta) { |
| 529 | if (delta->xed_startoff > new->br_startoff) |
| 530 | delta->xed_startoff = new->br_startoff; |
| 531 | if (delta->xed_blockcount < |
| 532 | new->br_startoff + new->br_blockcount) |
| 533 | delta->xed_blockcount = new->br_startoff + |
| 534 | new->br_blockcount; |
| 535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | /* |
| 538 | * Any kind of new delayed allocation goes here. |
| 539 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 540 | else if (isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if (cur) |
| 542 | ASSERT((cur->bc_private.b.flags & |
| 543 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 544 | if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 545 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | goto done; |
| 547 | } |
| 548 | /* |
| 549 | * Real allocation off the end of the file. |
| 550 | */ |
| 551 | else if (idx == nextents) { |
| 552 | if (cur) |
| 553 | ASSERT((cur->bc_private.b.flags & |
| 554 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 555 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 556 | &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto done; |
| 558 | } else { |
| 559 | xfs_bmbt_irec_t prev; /* old extent at offset idx */ |
| 560 | |
| 561 | /* |
| 562 | * Get the record referred to by idx. |
| 563 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 564 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* |
| 566 | * If it's a real allocation record, and the new allocation ends |
| 567 | * after the start of the referred to record, then we're filling |
| 568 | * in a delayed or unwritten allocation with a real one, or |
| 569 | * converting real back to unwritten. |
| 570 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 571 | if (!isnullstartblock(new->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | new->br_startoff + new->br_blockcount > prev.br_startoff) { |
| 573 | if (prev.br_state != XFS_EXT_UNWRITTEN && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 574 | isnullstartblock(prev.br_startblock)) { |
| 575 | da_old = startblockval(prev.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | if (cur) |
| 577 | ASSERT(cur->bc_private.b.flags & |
| 578 | XFS_BTCUR_BPRV_WASDEL); |
| 579 | if ((error = xfs_bmap_add_extent_delay_real(ip, |
| 580 | idx, &cur, new, &da_new, first, flist, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 581 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | goto done; |
| 583 | } else if (new->br_state == XFS_EXT_NORM) { |
| 584 | ASSERT(new->br_state == XFS_EXT_NORM); |
| 585 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 586 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | goto done; |
| 588 | } else { |
| 589 | ASSERT(new->br_state == XFS_EXT_UNWRITTEN); |
| 590 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 591 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | goto done; |
| 593 | } |
| 594 | ASSERT(*curp == cur || *curp == NULL); |
| 595 | } |
| 596 | /* |
| 597 | * Otherwise we're filling in a hole with an allocation. |
| 598 | */ |
| 599 | else { |
| 600 | if (cur) |
| 601 | ASSERT((cur->bc_private.b.flags & |
| 602 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 603 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 604 | new, &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | goto done; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | ASSERT(*curp == cur || *curp == NULL); |
| 610 | /* |
| 611 | * Convert to a btree if necessary. |
| 612 | */ |
| 613 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 614 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 615 | int tmp_logflags; /* partial log flag return val */ |
| 616 | |
| 617 | ASSERT(cur == NULL); |
| 618 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first, |
| 619 | flist, &cur, da_old > 0, &tmp_logflags, whichfork); |
| 620 | logflags |= tmp_logflags; |
| 621 | if (error) |
| 622 | goto done; |
| 623 | } |
| 624 | /* |
| 625 | * Adjust for changes in reserved delayed indirect blocks. |
| 626 | * Nothing to do for disk quotas here. |
| 627 | */ |
| 628 | if (da_old || da_new) { |
| 629 | xfs_filblks_t nblks; |
| 630 | |
| 631 | nblks = da_new; |
| 632 | if (cur) |
| 633 | nblks += cur->bc_private.b.allocated; |
| 634 | ASSERT(nblks <= da_old); |
| 635 | if (nblks < da_old) |
| 636 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 637 | (int64_t)(da_old - nblks), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | /* |
| 640 | * Clear out the allocated field, done with it now in any case. |
| 641 | */ |
| 642 | if (cur) { |
| 643 | cur->bc_private.b.allocated = 0; |
| 644 | *curp = cur; |
| 645 | } |
| 646 | done: |
| 647 | #ifdef DEBUG |
| 648 | if (!error) |
| 649 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
| 650 | #endif |
| 651 | *logflagsp = logflags; |
| 652 | return error; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 657 | * allocation to a real allocation. |
| 658 | */ |
| 659 | STATIC int /* error */ |
| 660 | xfs_bmap_add_extent_delay_real( |
| 661 | xfs_inode_t *ip, /* incore inode pointer */ |
| 662 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 663 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 664 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 666 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 667 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 668 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 669 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | int rsvd) /* OK to use reserved data block allocation */ |
| 671 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 673 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 674 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 677 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 679 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 680 | /* left is 0, right is 1, prev is 2 */ |
| 681 | int rval=0; /* return value (logging flags) */ |
| 682 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 683 | xfs_filblks_t temp=0; /* value for dnew calculations */ |
| 684 | xfs_filblks_t temp2=0;/* value for dnew calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
| 687 | #define LEFT r[0] |
| 688 | #define RIGHT r[1] |
| 689 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
| 691 | /* |
| 692 | * Set up a bunch of variables to make the tests simpler. |
| 693 | */ |
| 694 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 695 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 696 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | xfs_bmbt_get_all(ep, &PREV); |
| 698 | new_endoff = new->br_startoff + new->br_blockcount; |
| 699 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 700 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | /* |
| 703 | * Set flags determining what part of the previous delayed allocation |
| 704 | * extent is being replaced by a real allocation. |
| 705 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 706 | if (PREV.br_startoff == new->br_startoff) |
| 707 | state |= BMAP_LEFT_FILLING; |
| 708 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 709 | state |= BMAP_RIGHT_FILLING; |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* |
| 712 | * Check and set flags if this segment has a left neighbor. |
| 713 | * Don't set contiguous if the combined extent would be too large. |
| 714 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 715 | if (idx > 0) { |
| 716 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 717 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 718 | |
| 719 | if (isnullstartblock(LEFT.br_startblock)) |
| 720 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 722 | |
| 723 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 724 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 725 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 726 | LEFT.br_state == new->br_state && |
| 727 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 728 | state |= BMAP_LEFT_CONTIG; |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | /* |
| 731 | * Check and set flags if this segment has a right neighbor. |
| 732 | * Don't set contiguous if the combined extent would be too large. |
| 733 | * Also check for all-three-contiguous being too large. |
| 734 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 735 | if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
| 736 | state |= BMAP_RIGHT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 737 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 738 | |
| 739 | if (isnullstartblock(RIGHT.br_startblock)) |
| 740 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 742 | |
| 743 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 744 | new_endoff == RIGHT.br_startoff && |
| 745 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 746 | new->br_state == RIGHT.br_state && |
| 747 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 748 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 749 | BMAP_RIGHT_FILLING)) != |
| 750 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 751 | BMAP_RIGHT_FILLING) || |
| 752 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 753 | <= MAXEXTLEN)) |
| 754 | state |= BMAP_RIGHT_CONTIG; |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | error = 0; |
| 757 | /* |
| 758 | * Switch out based on the FILLING and CONTIG state bits. |
| 759 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 760 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 761 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 762 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 763 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | /* |
| 765 | * Filling in all of a previously delayed allocation extent. |
| 766 | * The left and right neighbors are both contiguous with new. |
| 767 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 768 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 769 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | LEFT.br_blockcount + PREV.br_blockcount + |
| 771 | RIGHT.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 772 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 773 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 774 | xfs_iext_remove(ip, idx, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | ip->i_df.if_lastex = idx - 1; |
| 776 | ip->i_d.di_nextents--; |
| 777 | if (cur == NULL) |
| 778 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 779 | else { |
| 780 | rval = XFS_ILOG_CORE; |
| 781 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 782 | RIGHT.br_startblock, |
| 783 | RIGHT.br_blockcount, &i))) |
| 784 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 785 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 786 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 788 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 789 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 791 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 793 | LEFT.br_startblock, |
| 794 | LEFT.br_blockcount + |
| 795 | PREV.br_blockcount + |
| 796 | RIGHT.br_blockcount, LEFT.br_state))) |
| 797 | goto done; |
| 798 | } |
| 799 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 800 | /* DELTA: Three in-core extents are replaced by one. */ |
| 801 | temp = LEFT.br_startoff; |
| 802 | temp2 = LEFT.br_blockcount + |
| 803 | PREV.br_blockcount + |
| 804 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | break; |
| 806 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 807 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | /* |
| 809 | * Filling in all of a previously delayed allocation extent. |
| 810 | * The left neighbor is contiguous, the right is not. |
| 811 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 812 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
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); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 815 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | ip->i_df.if_lastex = idx - 1; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 818 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | if (cur == NULL) |
| 820 | rval = XFS_ILOG_DEXT; |
| 821 | else { |
| 822 | rval = 0; |
| 823 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 824 | LEFT.br_startblock, LEFT.br_blockcount, |
| 825 | &i))) |
| 826 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 827 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 829 | LEFT.br_startblock, |
| 830 | LEFT.br_blockcount + |
| 831 | PREV.br_blockcount, LEFT.br_state))) |
| 832 | goto done; |
| 833 | } |
| 834 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 835 | /* DELTA: Two in-core extents are replaced by one. */ |
| 836 | temp = LEFT.br_startoff; |
| 837 | temp2 = LEFT.br_blockcount + |
| 838 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | break; |
| 840 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 841 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | /* |
| 843 | * Filling in all of a previously delayed allocation extent. |
| 844 | * The right neighbor is contiguous, the left is not. |
| 845 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 846 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 848 | xfs_bmbt_set_blockcount(ep, |
| 849 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 850 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | ip->i_df.if_lastex = idx; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 853 | xfs_iext_remove(ip, idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | if (cur == NULL) |
| 855 | rval = XFS_ILOG_DEXT; |
| 856 | else { |
| 857 | rval = 0; |
| 858 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 859 | RIGHT.br_startblock, |
| 860 | RIGHT.br_blockcount, &i))) |
| 861 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 862 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 864 | new->br_startblock, |
| 865 | PREV.br_blockcount + |
| 866 | RIGHT.br_blockcount, PREV.br_state))) |
| 867 | goto done; |
| 868 | } |
| 869 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 870 | /* DELTA: Two in-core extents are replaced by one. */ |
| 871 | temp = PREV.br_startoff; |
| 872 | temp2 = PREV.br_blockcount + |
| 873 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | break; |
| 875 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 876 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /* |
| 878 | * Filling in all of a previously delayed allocation extent. |
| 879 | * Neither the left nor right neighbors are contiguous with |
| 880 | * the new one. |
| 881 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 882 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 884 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | ip->i_df.if_lastex = idx; |
| 887 | ip->i_d.di_nextents++; |
| 888 | if (cur == NULL) |
| 889 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 890 | else { |
| 891 | rval = XFS_ILOG_CORE; |
| 892 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 893 | new->br_startblock, new->br_blockcount, |
| 894 | &i))) |
| 895 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 896 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 898 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 900 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 903 | /* DELTA: The in-core extent described by new changed type. */ |
| 904 | temp = new->br_startoff; |
| 905 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | break; |
| 907 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 908 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | /* |
| 910 | * Filling in the first part of a previous delayed allocation. |
| 911 | * The left neighbor is contiguous. |
| 912 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 913 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 914 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | LEFT.br_blockcount + new->br_blockcount); |
| 916 | xfs_bmbt_set_startoff(ep, |
| 917 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 918 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 921 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | xfs_bmbt_set_blockcount(ep, temp); |
| 923 | ip->i_df.if_lastex = idx - 1; |
| 924 | if (cur == NULL) |
| 925 | rval = XFS_ILOG_DEXT; |
| 926 | else { |
| 927 | rval = 0; |
| 928 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 929 | LEFT.br_startblock, LEFT.br_blockcount, |
| 930 | &i))) |
| 931 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 932 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 934 | LEFT.br_startblock, |
| 935 | LEFT.br_blockcount + |
| 936 | new->br_blockcount, |
| 937 | LEFT.br_state))) |
| 938 | goto done; |
| 939 | } |
| 940 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 941 | startblockval(PREV.br_startblock)); |
| 942 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 943 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 945 | /* DELTA: The boundary between two in-core extents moved. */ |
| 946 | temp = LEFT.br_startoff; |
| 947 | temp2 = LEFT.br_blockcount + |
| 948 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | break; |
| 950 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 951 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | /* |
| 953 | * Filling in the first part of a previous delayed allocation. |
| 954 | * The left neighbor is not contiguous. |
| 955 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 956 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 958 | temp = PREV.br_blockcount - new->br_blockcount; |
| 959 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 960 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | ip->i_df.if_lastex = idx; |
| 962 | ip->i_d.di_nextents++; |
| 963 | if (cur == NULL) |
| 964 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 965 | else { |
| 966 | rval = XFS_ILOG_CORE; |
| 967 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 968 | new->br_startblock, new->br_blockcount, |
| 969 | &i))) |
| 970 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 971 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 973 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 975 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | } |
| 977 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 978 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 979 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 980 | first, flist, &cur, 1, &tmp_rval, |
| 981 | XFS_DATA_FORK); |
| 982 | rval |= tmp_rval; |
| 983 | if (error) |
| 984 | goto done; |
| 985 | } |
| 986 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 987 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 989 | ep = xfs_iext_get_ext(ifp, idx + 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 990 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 991 | trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 993 | /* DELTA: One in-core extent is split in two. */ |
| 994 | temp = PREV.br_startoff; |
| 995 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | break; |
| 997 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 998 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | /* |
| 1000 | * Filling in the last part of a previous delayed allocation. |
| 1001 | * The right neighbor is contiguous with the new allocation. |
| 1002 | */ |
| 1003 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1004 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
| 1005 | trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | xfs_bmbt_set_blockcount(ep, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1007 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1008 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | new->br_blockcount + RIGHT.br_blockcount, |
| 1010 | RIGHT.br_state); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1011 | trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | ip->i_df.if_lastex = idx + 1; |
| 1013 | if (cur == NULL) |
| 1014 | rval = XFS_ILOG_DEXT; |
| 1015 | else { |
| 1016 | rval = 0; |
| 1017 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1018 | RIGHT.br_startblock, |
| 1019 | RIGHT.br_blockcount, &i))) |
| 1020 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1021 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1023 | new->br_startblock, |
| 1024 | new->br_blockcount + |
| 1025 | RIGHT.br_blockcount, |
| 1026 | RIGHT.br_state))) |
| 1027 | goto done; |
| 1028 | } |
| 1029 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1030 | startblockval(PREV.br_startblock)); |
| 1031 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1032 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1034 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1035 | temp = PREV.br_startoff; |
| 1036 | temp2 = PREV.br_blockcount + |
| 1037 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | break; |
| 1039 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1040 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | * Filling in the last part of a previous delayed allocation. |
| 1043 | * The right neighbor is not contiguous. |
| 1044 | */ |
| 1045 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1046 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1048 | xfs_iext_insert(ip, idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | ip->i_df.if_lastex = idx + 1; |
| 1050 | ip->i_d.di_nextents++; |
| 1051 | if (cur == NULL) |
| 1052 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1053 | else { |
| 1054 | rval = XFS_ILOG_CORE; |
| 1055 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1056 | new->br_startblock, new->br_blockcount, |
| 1057 | &i))) |
| 1058 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1059 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1061 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1063 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
| 1065 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1066 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1067 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1068 | first, flist, &cur, 1, &tmp_rval, |
| 1069 | XFS_DATA_FORK); |
| 1070 | rval |= tmp_rval; |
| 1071 | if (error) |
| 1072 | goto done; |
| 1073 | } |
| 1074 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1075 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1077 | ep = xfs_iext_get_ext(ifp, idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1078 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1079 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1081 | /* DELTA: One in-core extent is split in two. */ |
| 1082 | temp = PREV.br_startoff; |
| 1083 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | break; |
| 1085 | |
| 1086 | case 0: |
| 1087 | /* |
| 1088 | * Filling in the middle part of a previous delayed allocation. |
| 1089 | * Contiguity is impossible here. |
| 1090 | * This case is avoided almost all the time. |
| 1091 | */ |
| 1092 | temp = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1093 | trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | xfs_bmbt_set_blockcount(ep, temp); |
| 1095 | r[0] = *new; |
Lachlan McIlroy | d213371 | 2006-11-21 18:55:16 +1100 | [diff] [blame] | 1096 | r[1].br_state = PREV.br_state; |
| 1097 | r[1].br_startblock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | r[1].br_startoff = new_endoff; |
| 1099 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1100 | r[1].br_blockcount = temp2; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1101 | xfs_iext_insert(ip, idx + 1, 2, &r[0], state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | ip->i_df.if_lastex = idx + 1; |
| 1103 | ip->i_d.di_nextents++; |
| 1104 | if (cur == NULL) |
| 1105 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1106 | else { |
| 1107 | rval = XFS_ILOG_CORE; |
| 1108 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1109 | new->br_startblock, new->br_blockcount, |
| 1110 | &i))) |
| 1111 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1112 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1114 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1116 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | } |
| 1118 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1119 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1120 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1121 | first, flist, &cur, 1, &tmp_rval, |
| 1122 | XFS_DATA_FORK); |
| 1123 | rval |= tmp_rval; |
| 1124 | if (error) |
| 1125 | goto done; |
| 1126 | } |
| 1127 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 1128 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1129 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | (cur ? cur->bc_private.b.allocated : 0)); |
| 1131 | if (diff > 0 && |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1132 | 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] | 1133 | /* |
| 1134 | * Ick gross gag me with a spoon. |
| 1135 | */ |
| 1136 | ASSERT(0); /* want to see if this ever happens! */ |
| 1137 | while (diff > 0) { |
| 1138 | if (temp) { |
| 1139 | temp--; |
| 1140 | diff--; |
| 1141 | if (!diff || |
| 1142 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1143 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | break; |
| 1145 | } |
| 1146 | if (temp2) { |
| 1147 | temp2--; |
| 1148 | diff--; |
| 1149 | if (!diff || |
| 1150 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1151 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1156 | ep = xfs_iext_get_ext(ifp, idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1157 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1158 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1159 | trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1160 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1161 | nullstartblock((int)temp2)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1162 | trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | *dnew = temp + temp2; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1164 | /* DELTA: One in-core extent is split in three. */ |
| 1165 | temp = PREV.br_startoff; |
| 1166 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | break; |
| 1168 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1169 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1170 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1171 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1172 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1173 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1174 | case BMAP_LEFT_CONTIG: |
| 1175 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | /* |
| 1177 | * These cases are all impossible. |
| 1178 | */ |
| 1179 | ASSERT(0); |
| 1180 | } |
| 1181 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1182 | if (delta) { |
| 1183 | temp2 += temp; |
| 1184 | if (delta->xed_startoff > temp) |
| 1185 | delta->xed_startoff = temp; |
| 1186 | if (delta->xed_blockcount < temp2) |
| 1187 | delta->xed_blockcount = temp2; |
| 1188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | done: |
| 1190 | *logflagsp = rval; |
| 1191 | return error; |
| 1192 | #undef LEFT |
| 1193 | #undef RIGHT |
| 1194 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 1199 | * allocation to a real allocation or vice versa. |
| 1200 | */ |
| 1201 | STATIC int /* error */ |
| 1202 | xfs_bmap_add_extent_unwritten_real( |
| 1203 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1204 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 1205 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1206 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1207 | int *logflagsp, /* inode logging flags */ |
| 1208 | xfs_extdelta_t *delta) /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1211 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1214 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1216 | xfs_exntst_t newext; /* new extent state */ |
| 1217 | xfs_exntst_t oldext; /* old extent state */ |
| 1218 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1219 | /* left is 0, right is 1, prev is 2 */ |
| 1220 | int rval=0; /* return value (logging flags) */ |
| 1221 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1222 | xfs_filblks_t temp=0; |
| 1223 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
| 1225 | #define LEFT r[0] |
| 1226 | #define RIGHT r[1] |
| 1227 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | /* |
| 1229 | * Set up a bunch of variables to make the tests simpler. |
| 1230 | */ |
| 1231 | error = 0; |
| 1232 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1233 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1234 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | xfs_bmbt_get_all(ep, &PREV); |
| 1236 | newext = new->br_state; |
| 1237 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 1238 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 1239 | ASSERT(PREV.br_state == oldext); |
| 1240 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1241 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1242 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /* |
| 1245 | * Set flags determining what part of the previous oldext allocation |
| 1246 | * extent is being replaced by a newext allocation. |
| 1247 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1248 | if (PREV.br_startoff == new->br_startoff) |
| 1249 | state |= BMAP_LEFT_FILLING; |
| 1250 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1251 | state |= BMAP_RIGHT_FILLING; |
| 1252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | /* |
| 1254 | * Check and set flags if this segment has a left neighbor. |
| 1255 | * Don't set contiguous if the combined extent would be too large. |
| 1256 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1257 | if (idx > 0) { |
| 1258 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1259 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1260 | |
| 1261 | if (isnullstartblock(LEFT.br_startblock)) |
| 1262 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1264 | |
| 1265 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1266 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1267 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1268 | LEFT.br_state == newext && |
| 1269 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1270 | state |= BMAP_LEFT_CONTIG; |
| 1271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | /* |
| 1273 | * Check and set flags if this segment has a right neighbor. |
| 1274 | * Don't set contiguous if the combined extent would be too large. |
| 1275 | * Also check for all-three-contiguous being too large. |
| 1276 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1277 | if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
| 1278 | state |= BMAP_RIGHT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1279 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1280 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1281 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1283 | |
| 1284 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1285 | new_endoff == RIGHT.br_startoff && |
| 1286 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1287 | newext == RIGHT.br_state && |
| 1288 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1289 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1290 | BMAP_RIGHT_FILLING)) != |
| 1291 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1292 | BMAP_RIGHT_FILLING) || |
| 1293 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1294 | <= MAXEXTLEN)) |
| 1295 | state |= BMAP_RIGHT_CONTIG; |
| 1296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | /* |
| 1298 | * Switch out based on the FILLING and CONTIG state bits. |
| 1299 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1300 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1301 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1302 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1303 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | /* |
| 1305 | * Setting all of a previous oldext extent to newext. |
| 1306 | * The left and right neighbors are both contiguous with new. |
| 1307 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1308 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1309 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1311 | RIGHT.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1312 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1313 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1314 | xfs_iext_remove(ip, idx, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | ip->i_df.if_lastex = idx - 1; |
| 1316 | ip->i_d.di_nextents -= 2; |
| 1317 | if (cur == NULL) |
| 1318 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1319 | else { |
| 1320 | rval = XFS_ILOG_CORE; |
| 1321 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1322 | RIGHT.br_startblock, |
| 1323 | RIGHT.br_blockcount, &i))) |
| 1324 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1325 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1326 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1328 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1329 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1331 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1332 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1334 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1335 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1337 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1339 | LEFT.br_startblock, |
| 1340 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1341 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1342 | goto done; |
| 1343 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1344 | /* DELTA: Three in-core extents are replaced by one. */ |
| 1345 | temp = LEFT.br_startoff; |
| 1346 | temp2 = LEFT.br_blockcount + |
| 1347 | PREV.br_blockcount + |
| 1348 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | break; |
| 1350 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1351 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | /* |
| 1353 | * Setting all of a previous oldext extent to newext. |
| 1354 | * The left neighbor is contiguous, the right is not. |
| 1355 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1356 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1357 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1359 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | ip->i_df.if_lastex = idx - 1; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1362 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | ip->i_d.di_nextents--; |
| 1364 | if (cur == NULL) |
| 1365 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1366 | else { |
| 1367 | rval = XFS_ILOG_CORE; |
| 1368 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1369 | PREV.br_startblock, PREV.br_blockcount, |
| 1370 | &i))) |
| 1371 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1372 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1373 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1375 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1376 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1378 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1380 | LEFT.br_startblock, |
| 1381 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1382 | LEFT.br_state))) |
| 1383 | goto done; |
| 1384 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1385 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1386 | temp = LEFT.br_startoff; |
| 1387 | temp2 = LEFT.br_blockcount + |
| 1388 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | break; |
| 1390 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1391 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * Setting all of a previous oldext extent to newext. |
| 1394 | * The right neighbor is contiguous, the left is not. |
| 1395 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1396 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | xfs_bmbt_set_blockcount(ep, |
| 1398 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1399 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1400 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | ip->i_df.if_lastex = idx; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1402 | xfs_iext_remove(ip, idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | ip->i_d.di_nextents--; |
| 1404 | if (cur == NULL) |
| 1405 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1406 | else { |
| 1407 | rval = XFS_ILOG_CORE; |
| 1408 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1409 | RIGHT.br_startblock, |
| 1410 | RIGHT.br_blockcount, &i))) |
| 1411 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1412 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1413 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1415 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1416 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1418 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1420 | new->br_startblock, |
| 1421 | new->br_blockcount + RIGHT.br_blockcount, |
| 1422 | newext))) |
| 1423 | goto done; |
| 1424 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1425 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1426 | temp = PREV.br_startoff; |
| 1427 | temp2 = PREV.br_blockcount + |
| 1428 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1431 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | /* |
| 1433 | * Setting all of a previous oldext extent to newext. |
| 1434 | * Neither the left nor right neighbors are contiguous with |
| 1435 | * the new one. |
| 1436 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1437 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1439 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | ip->i_df.if_lastex = idx; |
| 1442 | if (cur == NULL) |
| 1443 | rval = XFS_ILOG_DEXT; |
| 1444 | else { |
| 1445 | rval = 0; |
| 1446 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1447 | new->br_startblock, new->br_blockcount, |
| 1448 | &i))) |
| 1449 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1450 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1452 | new->br_startblock, new->br_blockcount, |
| 1453 | newext))) |
| 1454 | goto done; |
| 1455 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1456 | /* DELTA: The in-core extent described by new changed type. */ |
| 1457 | temp = new->br_startoff; |
| 1458 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | break; |
| 1460 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1461 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | /* |
| 1463 | * Setting the first part of a previous oldext extent to newext. |
| 1464 | * The left neighbor is contiguous. |
| 1465 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1466 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1467 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | LEFT.br_blockcount + new->br_blockcount); |
| 1469 | xfs_bmbt_set_startoff(ep, |
| 1470 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1471 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1472 | |
| 1473 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | xfs_bmbt_set_startblock(ep, |
| 1475 | new->br_startblock + new->br_blockcount); |
| 1476 | xfs_bmbt_set_blockcount(ep, |
| 1477 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1478 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | ip->i_df.if_lastex = idx - 1; |
| 1481 | if (cur == NULL) |
| 1482 | rval = XFS_ILOG_DEXT; |
| 1483 | else { |
| 1484 | rval = 0; |
| 1485 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1486 | PREV.br_startblock, PREV.br_blockcount, |
| 1487 | &i))) |
| 1488 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1489 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | if ((error = xfs_bmbt_update(cur, |
| 1491 | PREV.br_startoff + new->br_blockcount, |
| 1492 | PREV.br_startblock + new->br_blockcount, |
| 1493 | PREV.br_blockcount - new->br_blockcount, |
| 1494 | oldext))) |
| 1495 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1496 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | goto done; |
| 1498 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1499 | LEFT.br_startblock, |
| 1500 | LEFT.br_blockcount + new->br_blockcount, |
| 1501 | LEFT.br_state)) |
| 1502 | goto done; |
| 1503 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1504 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1505 | temp = LEFT.br_startoff; |
| 1506 | temp2 = LEFT.br_blockcount + |
| 1507 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | break; |
| 1509 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1510 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | /* |
| 1512 | * Setting the first part of a previous oldext extent to newext. |
| 1513 | * The left neighbor is not contiguous. |
| 1514 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1515 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1517 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1518 | xfs_bmbt_set_blockcount(ep, |
| 1519 | PREV.br_blockcount - new->br_blockcount); |
| 1520 | xfs_bmbt_set_startblock(ep, |
| 1521 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1522 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1523 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1524 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | ip->i_df.if_lastex = idx; |
| 1526 | ip->i_d.di_nextents++; |
| 1527 | if (cur == NULL) |
| 1528 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1529 | else { |
| 1530 | rval = XFS_ILOG_CORE; |
| 1531 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1532 | PREV.br_startblock, PREV.br_blockcount, |
| 1533 | &i))) |
| 1534 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1535 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if ((error = xfs_bmbt_update(cur, |
| 1537 | PREV.br_startoff + new->br_blockcount, |
| 1538 | PREV.br_startblock + new->br_blockcount, |
| 1539 | PREV.br_blockcount - new->br_blockcount, |
| 1540 | oldext))) |
| 1541 | goto done; |
| 1542 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1543 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1545 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1547 | /* DELTA: One in-core extent is split in two. */ |
| 1548 | temp = PREV.br_startoff; |
| 1549 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | break; |
| 1551 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1552 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | /* |
| 1554 | * Setting the last part of a previous oldext extent to newext. |
| 1555 | * The right neighbor is contiguous with the new allocation. |
| 1556 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1557 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
| 1558 | trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | xfs_bmbt_set_blockcount(ep, |
| 1560 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1561 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1562 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1563 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1565 | trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); |
| 1566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | ip->i_df.if_lastex = idx + 1; |
| 1568 | if (cur == NULL) |
| 1569 | rval = XFS_ILOG_DEXT; |
| 1570 | else { |
| 1571 | rval = 0; |
| 1572 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1573 | PREV.br_startblock, |
| 1574 | PREV.br_blockcount, &i))) |
| 1575 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1576 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1578 | PREV.br_startblock, |
| 1579 | PREV.br_blockcount - new->br_blockcount, |
| 1580 | oldext))) |
| 1581 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1582 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | goto done; |
| 1584 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1585 | new->br_startblock, |
| 1586 | new->br_blockcount + RIGHT.br_blockcount, |
| 1587 | newext))) |
| 1588 | goto done; |
| 1589 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1590 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1591 | temp = PREV.br_startoff; |
| 1592 | temp2 = PREV.br_blockcount + |
| 1593 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | break; |
| 1595 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1596 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | /* |
| 1598 | * Setting the last part of a previous oldext extent to newext. |
| 1599 | * The right neighbor is not contiguous. |
| 1600 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1601 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | xfs_bmbt_set_blockcount(ep, |
| 1603 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1604 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1605 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1606 | xfs_iext_insert(ip, idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | ip->i_df.if_lastex = idx + 1; |
| 1608 | ip->i_d.di_nextents++; |
| 1609 | if (cur == NULL) |
| 1610 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1611 | else { |
| 1612 | rval = XFS_ILOG_CORE; |
| 1613 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1614 | PREV.br_startblock, PREV.br_blockcount, |
| 1615 | &i))) |
| 1616 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1617 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1619 | PREV.br_startblock, |
| 1620 | PREV.br_blockcount - new->br_blockcount, |
| 1621 | oldext))) |
| 1622 | goto done; |
| 1623 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1624 | new->br_startblock, new->br_blockcount, |
| 1625 | &i))) |
| 1626 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1627 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1629 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1631 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1633 | /* DELTA: One in-core extent is split in two. */ |
| 1634 | temp = PREV.br_startoff; |
| 1635 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | break; |
| 1637 | |
| 1638 | case 0: |
| 1639 | /* |
| 1640 | * Setting the middle part of a previous oldext extent to |
| 1641 | * newext. Contiguity is impossible here. |
| 1642 | * One extent becomes three extents. |
| 1643 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1644 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | xfs_bmbt_set_blockcount(ep, |
| 1646 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1647 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | r[0] = *new; |
| 1650 | r[1].br_startoff = new_endoff; |
| 1651 | r[1].br_blockcount = |
| 1652 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1653 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1654 | r[1].br_state = oldext; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1655 | xfs_iext_insert(ip, idx + 1, 2, &r[0], state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | ip->i_df.if_lastex = idx + 1; |
| 1657 | ip->i_d.di_nextents += 2; |
| 1658 | if (cur == NULL) |
| 1659 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1660 | else { |
| 1661 | rval = XFS_ILOG_CORE; |
| 1662 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1663 | PREV.br_startblock, PREV.br_blockcount, |
| 1664 | &i))) |
| 1665 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1666 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | /* new right extent - oldext */ |
| 1668 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1669 | r[1].br_startblock, r[1].br_blockcount, |
| 1670 | r[1].br_state))) |
| 1671 | goto done; |
| 1672 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1674 | cur->bc_rec.b.br_blockcount = |
| 1675 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1676 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1678 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1679 | /* |
| 1680 | * Reset the cursor to the position of the new extent |
| 1681 | * we are about to insert as we can't trust it after |
| 1682 | * the previous insert. |
| 1683 | */ |
| 1684 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1685 | new->br_startblock, new->br_blockcount, |
| 1686 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1688 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1690 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1691 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1693 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1695 | /* DELTA: One in-core extent is split in three. */ |
| 1696 | temp = PREV.br_startoff; |
| 1697 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | break; |
| 1699 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1700 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1701 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1702 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1703 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1704 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1705 | case BMAP_LEFT_CONTIG: |
| 1706 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | /* |
| 1708 | * These cases are all impossible. |
| 1709 | */ |
| 1710 | ASSERT(0); |
| 1711 | } |
| 1712 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1713 | if (delta) { |
| 1714 | temp2 += temp; |
| 1715 | if (delta->xed_startoff > temp) |
| 1716 | delta->xed_startoff = temp; |
| 1717 | if (delta->xed_blockcount < temp2) |
| 1718 | delta->xed_blockcount = temp2; |
| 1719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | done: |
| 1721 | *logflagsp = rval; |
| 1722 | return error; |
| 1723 | #undef LEFT |
| 1724 | #undef RIGHT |
| 1725 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1730 | * to a delayed allocation. |
| 1731 | */ |
| 1732 | /*ARGSUSED*/ |
| 1733 | STATIC int /* error */ |
| 1734 | xfs_bmap_add_extent_hole_delay( |
| 1735 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1736 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1737 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1739 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | int rsvd) /* OK to allocate reserved blocks */ |
| 1741 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1742 | xfs_bmbt_rec_host_t *ep; /* extent record for idx */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1743 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1745 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1746 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1747 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1748 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1749 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
| 1750 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1752 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1753 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1755 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | /* |
| 1758 | * Check and set flags if this segment has a left neighbor |
| 1759 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1760 | if (idx > 0) { |
| 1761 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1762 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1763 | |
| 1764 | if (isnullstartblock(left.br_startblock)) |
| 1765 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | /* |
| 1769 | * Check and set flags if the current (right) segment exists. |
| 1770 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1771 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1772 | if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
| 1773 | state |= BMAP_RIGHT_VALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | xfs_bmbt_get_all(ep, &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1775 | |
| 1776 | if (isnullstartblock(right.br_startblock)) |
| 1777 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | /* |
| 1781 | * Set contiguity flags on the left and right neighbors. |
| 1782 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1783 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1784 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1785 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1786 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1787 | state |= BMAP_LEFT_CONTIG; |
| 1788 | |
| 1789 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1790 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1791 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1792 | (!(state & BMAP_LEFT_CONTIG) || |
| 1793 | (left.br_blockcount + new->br_blockcount + |
| 1794 | right.br_blockcount <= MAXEXTLEN))) |
| 1795 | state |= BMAP_RIGHT_CONTIG; |
| 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | /* |
| 1798 | * Switch out based on the contiguity flags. |
| 1799 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1800 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1801 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | /* |
| 1803 | * New allocation is contiguous with delayed allocations |
| 1804 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1805 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | */ |
| 1807 | temp = left.br_blockcount + new->br_blockcount + |
| 1808 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1809 | |
| 1810 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1811 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1812 | oldlen = startblockval(left.br_startblock) + |
| 1813 | startblockval(new->br_startblock) + |
| 1814 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1816 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1817 | nullstartblock((int)newlen)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1818 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1819 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1820 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1822 | /* DELTA: Two in-core extents were replaced by one. */ |
| 1823 | temp2 = temp; |
| 1824 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | break; |
| 1826 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1827 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | /* |
| 1829 | * New allocation is contiguous with a delayed allocation |
| 1830 | * on the left. |
| 1831 | * Merge the new allocation with the left neighbor. |
| 1832 | */ |
| 1833 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1834 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1835 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1836 | oldlen = startblockval(left.br_startblock) + |
| 1837 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1839 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1840 | nullstartblock((int)newlen)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1841 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1844 | /* DELTA: One in-core extent grew into a hole. */ |
| 1845 | temp2 = temp; |
| 1846 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | break; |
| 1848 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1849 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | /* |
| 1851 | * New allocation is contiguous with a delayed allocation |
| 1852 | * on the right. |
| 1853 | * Merge the new allocation with the right neighbor. |
| 1854 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1855 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1857 | oldlen = startblockval(new->br_startblock) + |
| 1858 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | newlen = xfs_bmap_worst_indlen(ip, temp); |
| 1860 | xfs_bmbt_set_allf(ep, new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1861 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1862 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1865 | /* DELTA: One in-core extent grew into a hole. */ |
| 1866 | temp2 = temp; |
| 1867 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | break; |
| 1869 | |
| 1870 | case 0: |
| 1871 | /* |
| 1872 | * New allocation is not contiguous with another |
| 1873 | * delayed allocation. |
| 1874 | * Insert a new entry. |
| 1875 | */ |
| 1876 | oldlen = newlen = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1877 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1879 | /* DELTA: A new in-core extent was added in a hole. */ |
| 1880 | temp2 = new->br_blockcount; |
| 1881 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | break; |
| 1883 | } |
| 1884 | if (oldlen != newlen) { |
| 1885 | ASSERT(oldlen > newlen); |
| 1886 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1887 | (int64_t)(oldlen - newlen), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | /* |
| 1889 | * Nothing to do for disk quota accounting here. |
| 1890 | */ |
| 1891 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1892 | if (delta) { |
| 1893 | temp2 += temp; |
| 1894 | if (delta->xed_startoff > temp) |
| 1895 | delta->xed_startoff = temp; |
| 1896 | if (delta->xed_blockcount < temp2) |
| 1897 | delta->xed_blockcount = temp2; |
| 1898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | *logflagsp = 0; |
| 1900 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | /* |
| 1904 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1905 | * to a real allocation. |
| 1906 | */ |
| 1907 | STATIC int /* error */ |
| 1908 | xfs_bmap_add_extent_hole_real( |
| 1909 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1910 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 1911 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1912 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1914 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | int whichfork) /* data or attr fork */ |
| 1916 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1917 | xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | int i; /* temp state */ |
| 1920 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1921 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1922 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1923 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1925 | xfs_filblks_t temp=0; |
| 1926 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
| 1928 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1929 | ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1930 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | state = 0; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1932 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1933 | if (whichfork == XFS_ATTR_FORK) |
| 1934 | state |= BMAP_ATTRFORK; |
| 1935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | /* |
| 1937 | * Check and set flags if this segment has a left neighbor. |
| 1938 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1939 | if (idx > 0) { |
| 1940 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1941 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1942 | if (isnullstartblock(left.br_startblock)) |
| 1943 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | /* |
| 1947 | * Check and set flags if this segment has a current value. |
| 1948 | * Not true if we're inserting into the "hole" at eof. |
| 1949 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1950 | if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
| 1951 | state |= BMAP_RIGHT_VALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | xfs_bmbt_get_all(ep, &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1953 | if (isnullstartblock(right.br_startblock)) |
| 1954 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | /* |
| 1958 | * We're inserting a real allocation between "left" and "right". |
| 1959 | * Set the contiguity flags. Don't let extents get too large. |
| 1960 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1961 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1962 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1963 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1964 | left.br_state == new->br_state && |
| 1965 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1966 | state |= BMAP_LEFT_CONTIG; |
| 1967 | |
| 1968 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1969 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1970 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1971 | new->br_state == right.br_state && |
| 1972 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1973 | (!(state & BMAP_LEFT_CONTIG) || |
| 1974 | left.br_blockcount + new->br_blockcount + |
| 1975 | right.br_blockcount <= MAXEXTLEN)) |
| 1976 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1978 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | /* |
| 1980 | * Select which case we're in here, and implement it. |
| 1981 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1982 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1983 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | /* |
| 1985 | * New allocation is contiguous with real allocations on the |
| 1986 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1987 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1989 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1990 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | left.br_blockcount + new->br_blockcount + |
| 1992 | right.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1993 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1994 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1995 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | ifp->if_lastex = idx - 1; |
| 1997 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1998 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 1999 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2000 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2001 | } else { |
| 2002 | rval = XFS_ILOG_CORE; |
| 2003 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2004 | right.br_startoff, |
| 2005 | right.br_startblock, |
| 2006 | right.br_blockcount, &i))) |
| 2007 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2008 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2009 | if ((error = xfs_btree_delete(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2010 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2011 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2012 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2013 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2014 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2015 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2016 | left.br_startblock, |
| 2017 | left.br_blockcount + |
| 2018 | new->br_blockcount + |
| 2019 | right.br_blockcount, |
| 2020 | left.br_state))) |
| 2021 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2023 | /* DELTA: Two in-core extents were replaced by one. */ |
| 2024 | temp = left.br_startoff; |
| 2025 | temp2 = left.br_blockcount + |
| 2026 | new->br_blockcount + |
| 2027 | right.br_blockcount; |
| 2028 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2030 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | /* |
| 2032 | * New allocation is contiguous with a real allocation |
| 2033 | * on the left. |
| 2034 | * Merge the new allocation with the left neighbor. |
| 2035 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2036 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2037 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2039 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 2040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | ifp->if_lastex = idx - 1; |
| 2042 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2043 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2044 | } else { |
| 2045 | rval = 0; |
| 2046 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2047 | left.br_startoff, |
| 2048 | left.br_startblock, |
| 2049 | left.br_blockcount, &i))) |
| 2050 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2051 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2052 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2053 | left.br_startblock, |
| 2054 | left.br_blockcount + |
| 2055 | new->br_blockcount, |
| 2056 | left.br_state))) |
| 2057 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2059 | /* DELTA: One in-core extent grew. */ |
| 2060 | temp = left.br_startoff; |
| 2061 | temp2 = left.br_blockcount + |
| 2062 | new->br_blockcount; |
| 2063 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2065 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | /* |
| 2067 | * New allocation is contiguous with a real allocation |
| 2068 | * on the right. |
| 2069 | * Merge the new allocation with the right neighbor. |
| 2070 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2071 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock, |
| 2073 | new->br_blockcount + right.br_blockcount, |
| 2074 | right.br_state); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2075 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | ifp->if_lastex = idx; |
| 2078 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2079 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2080 | } else { |
| 2081 | rval = 0; |
| 2082 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2083 | right.br_startoff, |
| 2084 | right.br_startblock, |
| 2085 | right.br_blockcount, &i))) |
| 2086 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2087 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2088 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2089 | new->br_startblock, |
| 2090 | new->br_blockcount + |
| 2091 | right.br_blockcount, |
| 2092 | right.br_state))) |
| 2093 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2095 | /* DELTA: One in-core extent grew. */ |
| 2096 | temp = new->br_startoff; |
| 2097 | temp2 = new->br_blockcount + |
| 2098 | right.br_blockcount; |
| 2099 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
| 2101 | case 0: |
| 2102 | /* |
| 2103 | * New allocation is not contiguous with another |
| 2104 | * real allocation. |
| 2105 | * Insert a new entry. |
| 2106 | */ |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2107 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | ifp->if_lastex = idx; |
| 2109 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2110 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2111 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2112 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2113 | } else { |
| 2114 | rval = XFS_ILOG_CORE; |
| 2115 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2116 | new->br_startoff, |
| 2117 | new->br_startblock, |
| 2118 | new->br_blockcount, &i))) |
| 2119 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2120 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2121 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2122 | if ((error = xfs_btree_insert(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2123 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2124 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2126 | /* DELTA: A new extent was added in a hole. */ |
| 2127 | temp = new->br_startoff; |
| 2128 | temp2 = new->br_blockcount; |
| 2129 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2131 | if (delta) { |
| 2132 | temp2 += temp; |
| 2133 | if (delta->xed_startoff > temp) |
| 2134 | delta->xed_startoff = temp; |
| 2135 | if (delta->xed_blockcount < temp2) |
| 2136 | delta->xed_blockcount = temp2; |
| 2137 | } |
| 2138 | done: |
| 2139 | *logflagsp = rval; |
| 2140 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2143 | /* |
| 2144 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2145 | */ |
| 2146 | STATIC int |
| 2147 | xfs_bmap_extsize_align( |
| 2148 | xfs_mount_t *mp, |
| 2149 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2150 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2151 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2152 | int rt, /* is this a realtime inode? */ |
| 2153 | int eof, /* is extent at end-of-file? */ |
| 2154 | int delay, /* creating delalloc extent? */ |
| 2155 | int convert, /* overwriting unwritten extent? */ |
| 2156 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2157 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2158 | { |
| 2159 | xfs_fileoff_t orig_off; /* original offset */ |
| 2160 | xfs_extlen_t orig_alen; /* original length */ |
| 2161 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2162 | xfs_fileoff_t nexto; /* next file offset */ |
| 2163 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2164 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2165 | xfs_extlen_t align_alen; /* temp for length */ |
| 2166 | xfs_extlen_t temp; /* temp for calculations */ |
| 2167 | |
| 2168 | if (convert) |
| 2169 | return 0; |
| 2170 | |
| 2171 | orig_off = align_off = *offp; |
| 2172 | orig_alen = align_alen = *lenp; |
| 2173 | orig_end = orig_off + orig_alen; |
| 2174 | |
| 2175 | /* |
| 2176 | * If this request overlaps an existing extent, then don't |
| 2177 | * attempt to perform any additional alignment. |
| 2178 | */ |
| 2179 | if (!delay && !eof && |
| 2180 | (orig_off >= gotp->br_startoff) && |
| 2181 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | /* |
| 2186 | * If the file offset is unaligned vs. the extent size |
| 2187 | * we need to align it. This will be possible unless |
| 2188 | * the file was previously written with a kernel that didn't |
| 2189 | * perform this alignment, or if a truncate shot us in the |
| 2190 | * foot. |
| 2191 | */ |
| 2192 | temp = do_mod(orig_off, extsz); |
| 2193 | if (temp) { |
| 2194 | align_alen += temp; |
| 2195 | align_off -= temp; |
| 2196 | } |
| 2197 | /* |
| 2198 | * Same adjustment for the end of the requested area. |
| 2199 | */ |
| 2200 | if ((temp = (align_alen % extsz))) { |
| 2201 | align_alen += extsz - temp; |
| 2202 | } |
| 2203 | /* |
| 2204 | * If the previous block overlaps with this proposed allocation |
| 2205 | * then move the start forward without adjusting the length. |
| 2206 | */ |
| 2207 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2208 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2209 | prevo = prevp->br_startoff; |
| 2210 | else |
| 2211 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2212 | } else |
| 2213 | prevo = 0; |
| 2214 | if (align_off != orig_off && align_off < prevo) |
| 2215 | align_off = prevo; |
| 2216 | /* |
| 2217 | * If the next block overlaps with this proposed allocation |
| 2218 | * then move the start back without adjusting the length, |
| 2219 | * but not before offset 0. |
| 2220 | * This may of course make the start overlap previous block, |
| 2221 | * and if we hit the offset 0 limit then the next block |
| 2222 | * can still overlap too. |
| 2223 | */ |
| 2224 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2225 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2226 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2227 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2228 | else |
| 2229 | nexto = gotp->br_startoff; |
| 2230 | } else |
| 2231 | nexto = NULLFILEOFF; |
| 2232 | if (!eof && |
| 2233 | align_off + align_alen != orig_end && |
| 2234 | align_off + align_alen > nexto) |
| 2235 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2236 | /* |
| 2237 | * If we're now overlapping the next or previous extent that |
| 2238 | * means we can't fit an extsz piece in this hole. Just move |
| 2239 | * the start forward to the first valid spot and set |
| 2240 | * the length so we hit the end. |
| 2241 | */ |
| 2242 | if (align_off != orig_off && align_off < prevo) |
| 2243 | align_off = prevo; |
| 2244 | if (align_off + align_alen != orig_end && |
| 2245 | align_off + align_alen > nexto && |
| 2246 | nexto != NULLFILEOFF) { |
| 2247 | ASSERT(nexto > prevo); |
| 2248 | align_alen = nexto - align_off; |
| 2249 | } |
| 2250 | |
| 2251 | /* |
| 2252 | * If realtime, and the result isn't a multiple of the realtime |
| 2253 | * extent size we need to remove blocks until it is. |
| 2254 | */ |
| 2255 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2256 | /* |
| 2257 | * We're not covering the original request, or |
| 2258 | * we won't be able to once we fix the length. |
| 2259 | */ |
| 2260 | if (orig_off < align_off || |
| 2261 | orig_end > align_off + align_alen || |
| 2262 | align_alen - temp < orig_alen) |
| 2263 | return XFS_ERROR(EINVAL); |
| 2264 | /* |
| 2265 | * Try to fix it by moving the start up. |
| 2266 | */ |
| 2267 | if (align_off + temp <= orig_off) { |
| 2268 | align_alen -= temp; |
| 2269 | align_off += temp; |
| 2270 | } |
| 2271 | /* |
| 2272 | * Try to fix it by moving the end in. |
| 2273 | */ |
| 2274 | else if (align_off + align_alen - temp >= orig_end) |
| 2275 | align_alen -= temp; |
| 2276 | /* |
| 2277 | * Set the start to the minimum then trim the length. |
| 2278 | */ |
| 2279 | else { |
| 2280 | align_alen -= orig_off - align_off; |
| 2281 | align_off = orig_off; |
| 2282 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 2283 | } |
| 2284 | /* |
| 2285 | * Result doesn't cover the request, fail it. |
| 2286 | */ |
| 2287 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2288 | return XFS_ERROR(EINVAL); |
| 2289 | } else { |
| 2290 | ASSERT(orig_off >= align_off); |
| 2291 | ASSERT(orig_end <= align_off + align_alen); |
| 2292 | } |
| 2293 | |
| 2294 | #ifdef DEBUG |
| 2295 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2296 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2297 | if (prevp->br_startoff != NULLFILEOFF) |
| 2298 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2299 | #endif |
| 2300 | |
| 2301 | *lenp = align_alen; |
| 2302 | *offp = align_off; |
| 2303 | return 0; |
| 2304 | } |
| 2305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2307 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2308 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2309 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2311 | { |
| 2312 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2314 | xfs_mount_t *mp; /* mount point structure */ |
| 2315 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2316 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
| 2318 | #define ISVALID(x,y) \ |
| 2319 | (rt ? \ |
| 2320 | (x) < mp->m_sb.sb_rblocks : \ |
| 2321 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2322 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2323 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | mp = ap->ip->i_mount; |
| 2326 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2327 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
| 2328 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | /* |
| 2330 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2331 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | */ |
| 2333 | if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2334 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, |
| 2336 | ap->prevp->br_startblock)) { |
| 2337 | ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; |
| 2338 | /* |
| 2339 | * Adjust for the gap between prevp and us. |
| 2340 | */ |
| 2341 | adjust = ap->off - |
| 2342 | (ap->prevp->br_startoff + ap->prevp->br_blockcount); |
| 2343 | if (adjust && |
| 2344 | ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) |
| 2345 | ap->rval += adjust; |
| 2346 | } |
| 2347 | /* |
| 2348 | * If not at eof, then compare the two neighbor blocks. |
| 2349 | * Figure out whether either one gives us a good starting point, |
| 2350 | * and pick the better one. |
| 2351 | */ |
| 2352 | else if (!ap->eof) { |
| 2353 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2354 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2355 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2356 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2357 | |
| 2358 | /* |
| 2359 | * If there's a previous (left) block, select a requested |
| 2360 | * start block based on it. |
| 2361 | */ |
| 2362 | if (ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2363 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | (prevbno = ap->prevp->br_startblock + |
| 2365 | ap->prevp->br_blockcount) && |
| 2366 | ISVALID(prevbno, ap->prevp->br_startblock)) { |
| 2367 | /* |
| 2368 | * Calculate gap to end of previous block. |
| 2369 | */ |
| 2370 | adjust = prevdiff = ap->off - |
| 2371 | (ap->prevp->br_startoff + |
| 2372 | ap->prevp->br_blockcount); |
| 2373 | /* |
| 2374 | * Figure the startblock based on the previous block's |
| 2375 | * end and the gap size. |
| 2376 | * Heuristic! |
| 2377 | * If the gap is large relative to the piece we're |
| 2378 | * allocating, or using it gives us an invalid block |
| 2379 | * number, then just use the end of the previous block. |
| 2380 | */ |
| 2381 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2382 | ISVALID(prevbno + prevdiff, |
| 2383 | ap->prevp->br_startblock)) |
| 2384 | prevbno += adjust; |
| 2385 | else |
| 2386 | prevdiff += adjust; |
| 2387 | /* |
| 2388 | * If the firstblock forbids it, can't use it, |
| 2389 | * must use default. |
| 2390 | */ |
| 2391 | if (!rt && !nullfb && |
| 2392 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2393 | prevbno = NULLFSBLOCK; |
| 2394 | } |
| 2395 | /* |
| 2396 | * No previous block or can't follow it, just default. |
| 2397 | */ |
| 2398 | else |
| 2399 | prevbno = NULLFSBLOCK; |
| 2400 | /* |
| 2401 | * If there's a following (right) block, select a requested |
| 2402 | * start block based on it. |
| 2403 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2404 | if (!isnullstartblock(ap->gotp->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | /* |
| 2406 | * Calculate gap to start of next block. |
| 2407 | */ |
| 2408 | adjust = gotdiff = ap->gotp->br_startoff - ap->off; |
| 2409 | /* |
| 2410 | * Figure the startblock based on the next block's |
| 2411 | * start and the gap size. |
| 2412 | */ |
| 2413 | gotbno = ap->gotp->br_startblock; |
| 2414 | /* |
| 2415 | * Heuristic! |
| 2416 | * If the gap is large relative to the piece we're |
| 2417 | * allocating, or using it gives us an invalid block |
| 2418 | * number, then just use the start of the next block |
| 2419 | * offset by our length. |
| 2420 | */ |
| 2421 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2422 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2423 | gotbno -= adjust; |
| 2424 | else if (ISVALID(gotbno - ap->alen, gotbno)) { |
| 2425 | gotbno -= ap->alen; |
| 2426 | gotdiff += adjust - ap->alen; |
| 2427 | } else |
| 2428 | gotdiff += adjust; |
| 2429 | /* |
| 2430 | * If the firstblock forbids it, can't use it, |
| 2431 | * must use default. |
| 2432 | */ |
| 2433 | if (!rt && !nullfb && |
| 2434 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2435 | gotbno = NULLFSBLOCK; |
| 2436 | } |
| 2437 | /* |
| 2438 | * No next block, just default. |
| 2439 | */ |
| 2440 | else |
| 2441 | gotbno = NULLFSBLOCK; |
| 2442 | /* |
| 2443 | * If both valid, pick the better one, else the only good |
| 2444 | * one, else ap->rval is already set (to 0 or the inode block). |
| 2445 | */ |
| 2446 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
| 2447 | ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; |
| 2448 | else if (prevbno != NULLFSBLOCK) |
| 2449 | ap->rval = prevbno; |
| 2450 | else if (gotbno != NULLFSBLOCK) |
| 2451 | ap->rval = gotbno; |
| 2452 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2453 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | STATIC int |
| 2457 | xfs_bmap_rtalloc( |
| 2458 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2459 | { |
| 2460 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2461 | int error; /* error return value */ |
| 2462 | xfs_mount_t *mp; /* mount point structure */ |
| 2463 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2464 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2465 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2466 | xfs_rtblock_t rtb; |
| 2467 | |
| 2468 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2469 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2470 | prod = align / mp->m_sb.sb_rextsize; |
| 2471 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2472 | align, 1, ap->eof, 0, |
| 2473 | ap->conv, &ap->off, &ap->alen); |
| 2474 | if (error) |
| 2475 | return error; |
| 2476 | ASSERT(ap->alen); |
| 2477 | ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); |
| 2478 | |
| 2479 | /* |
| 2480 | * If the offset & length are not perfectly aligned |
| 2481 | * then kill prod, it will just get us in trouble. |
| 2482 | */ |
| 2483 | if (do_mod(ap->off, align) || ap->alen % align) |
| 2484 | prod = 1; |
| 2485 | /* |
| 2486 | * Set ralen to be the actual requested length in rtextents. |
| 2487 | */ |
| 2488 | ralen = ap->alen / mp->m_sb.sb_rextsize; |
| 2489 | /* |
| 2490 | * If the old value was close enough to MAXEXTLEN that |
| 2491 | * we rounded up to it, cut it back so it's valid again. |
| 2492 | * Note that if it's a really large request (bigger than |
| 2493 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2494 | * adjust the starting point to match it. |
| 2495 | */ |
| 2496 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2497 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
| 2498 | /* |
| 2499 | * If it's an allocation to an empty file at offset 0, |
| 2500 | * pick an extent that will space things out in the rt area. |
| 2501 | */ |
| 2502 | if (ap->eof && ap->off == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2503 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2504 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2505 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2506 | if (error) |
| 2507 | return error; |
| 2508 | ap->rval = rtx * mp->m_sb.sb_rextsize; |
| 2509 | } else { |
| 2510 | ap->rval = 0; |
| 2511 | } |
| 2512 | |
| 2513 | xfs_bmap_adjacent(ap); |
| 2514 | |
| 2515 | /* |
| 2516 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2517 | */ |
| 2518 | atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2519 | do_div(ap->rval, mp->m_sb.sb_rextsize); |
| 2520 | rtb = ap->rval; |
| 2521 | ap->alen = ralen; |
| 2522 | if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, |
| 2523 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2524 | return error; |
| 2525 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 2526 | (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, |
| 2527 | ap->alen, &ralen, atype, |
| 2528 | ap->wasdel, 1, &rtb))) |
| 2529 | return error; |
| 2530 | ap->rval = rtb; |
| 2531 | if (ap->rval != NULLFSBLOCK) { |
| 2532 | ap->rval *= mp->m_sb.sb_rextsize; |
| 2533 | ralen *= mp->m_sb.sb_rextsize; |
| 2534 | ap->alen = ralen; |
| 2535 | ap->ip->i_d.di_nblocks += ralen; |
| 2536 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2537 | if (ap->wasdel) |
| 2538 | ap->ip->i_delayed_blks -= ralen; |
| 2539 | /* |
| 2540 | * Adjust the disk quota also. This was reserved |
| 2541 | * earlier. |
| 2542 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2543 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2544 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2545 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2546 | } else { |
| 2547 | ap->alen = 0; |
| 2548 | } |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
| 2552 | STATIC int |
| 2553 | xfs_bmap_btalloc( |
| 2554 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2555 | { |
| 2556 | xfs_mount_t *mp; /* mount point structure */ |
| 2557 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2558 | xfs_extlen_t align; /* minimum allocation alignment */ |
| 2559 | xfs_agnumber_t ag; |
| 2560 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2561 | xfs_agnumber_t startag; |
| 2562 | xfs_alloc_arg_t args; |
| 2563 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2564 | xfs_extlen_t nextminlen = 0; |
| 2565 | xfs_perag_t *pag; |
| 2566 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2567 | int isaligned; |
| 2568 | int notinit; |
| 2569 | int tryagain; |
| 2570 | int error; |
| 2571 | |
| 2572 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2573 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2574 | if (unlikely(align)) { |
| 2575 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2576 | align, 0, ap->eof, 0, ap->conv, |
| 2577 | &ap->off, &ap->alen); |
| 2578 | ASSERT(!error); |
| 2579 | ASSERT(ap->alen); |
| 2580 | } |
| 2581 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2582 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2583 | if (nullfb) { |
| 2584 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2585 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2586 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
| 2587 | ap->rval = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2588 | } else { |
| 2589 | ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
| 2590 | } |
| 2591 | } else |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2592 | ap->rval = ap->firstblock; |
| 2593 | |
| 2594 | xfs_bmap_adjacent(ap); |
| 2595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | /* |
| 2597 | * If allowed, use ap->rval; otherwise must use firstblock since |
| 2598 | * it's in the right allocation group. |
| 2599 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2600 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | ; |
| 2602 | else |
| 2603 | ap->rval = ap->firstblock; |
| 2604 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | * Normal allocation, done through xfs_alloc_vextent. |
| 2606 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2607 | tryagain = isaligned = 0; |
| 2608 | args.tp = ap->tp; |
| 2609 | args.mp = mp; |
| 2610 | args.fsbno = ap->rval; |
| 2611 | args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks); |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2612 | args.firstblock = ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2613 | blen = 0; |
| 2614 | if (nullfb) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2615 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2616 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2617 | else |
| 2618 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2619 | args.total = ap->total; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | /* |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2622 | * Search for an allocation group with a single extent |
| 2623 | * large enough for the request. |
| 2624 | * |
| 2625 | * If one isn't found, then adjust the minimum allocation |
| 2626 | * size to the largest space found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2628 | startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2629 | if (startag == NULLAGNUMBER) |
| 2630 | startag = ag = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2631 | notinit = 0; |
| 2632 | down_read(&mp->m_peraglock); |
| 2633 | while (blen < ap->alen) { |
| 2634 | pag = &mp->m_perag[ag]; |
| 2635 | if (!pag->pagf_init && |
| 2636 | (error = xfs_alloc_pagf_init(mp, args.tp, |
| 2637 | ag, XFS_ALLOC_FLAG_TRYLOCK))) { |
| 2638 | up_read(&mp->m_peraglock); |
| 2639 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2641 | /* |
| 2642 | * See xfs_alloc_fix_freelist... |
| 2643 | */ |
| 2644 | if (pag->pagf_init) { |
Dave Chinner | 6cc8764 | 2009-03-16 08:29:46 +0100 | [diff] [blame] | 2645 | xfs_extlen_t longest; |
| 2646 | longest = xfs_alloc_longest_free_extent(mp, pag); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2647 | if (blen < longest) |
| 2648 | blen = longest; |
| 2649 | } else |
| 2650 | notinit = 1; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2651 | |
| 2652 | if (xfs_inode_is_filestream(ap->ip)) { |
| 2653 | if (blen >= ap->alen) |
| 2654 | break; |
| 2655 | |
| 2656 | if (ap->userdata) { |
| 2657 | /* |
| 2658 | * If startag is an invalid AG, we've |
| 2659 | * come here once before and |
| 2660 | * xfs_filestream_new_ag picked the |
| 2661 | * best currently available. |
| 2662 | * |
| 2663 | * Don't continue looping, since we |
| 2664 | * could loop forever. |
| 2665 | */ |
| 2666 | if (startag == NULLAGNUMBER) |
| 2667 | break; |
| 2668 | |
| 2669 | error = xfs_filestream_new_ag(ap, &ag); |
| 2670 | if (error) { |
| 2671 | up_read(&mp->m_peraglock); |
| 2672 | return error; |
| 2673 | } |
| 2674 | |
| 2675 | /* loop again to set 'blen'*/ |
| 2676 | startag = NULLAGNUMBER; |
| 2677 | continue; |
| 2678 | } |
| 2679 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2680 | if (++ag == mp->m_sb.sb_agcount) |
| 2681 | ag = 0; |
| 2682 | if (ag == startag) |
| 2683 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2685 | up_read(&mp->m_peraglock); |
| 2686 | /* |
| 2687 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2688 | * possible that there is space for this request. |
| 2689 | */ |
| 2690 | if (notinit || blen < ap->minlen) |
| 2691 | args.minlen = ap->minlen; |
| 2692 | /* |
| 2693 | * If the best seen length is less than the request |
| 2694 | * length, use the best as the minimum. |
| 2695 | */ |
| 2696 | else if (blen < ap->alen) |
| 2697 | args.minlen = blen; |
| 2698 | /* |
| 2699 | * Otherwise we've seen an extent as big as alen, |
| 2700 | * use that as the minimum. |
| 2701 | */ |
| 2702 | else |
| 2703 | args.minlen = ap->alen; |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2704 | |
| 2705 | /* |
| 2706 | * set the failure fallback case to look in the selected |
| 2707 | * AG as the stream may have moved. |
| 2708 | */ |
| 2709 | if (xfs_inode_is_filestream(ap->ip)) |
| 2710 | ap->rval = args.fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2711 | } else if (ap->low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2712 | if (xfs_inode_is_filestream(ap->ip)) |
| 2713 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2714 | else |
| 2715 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2716 | args.total = args.minlen = ap->minlen; |
| 2717 | } else { |
| 2718 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2719 | args.total = ap->total; |
| 2720 | args.minlen = ap->minlen; |
| 2721 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2722 | /* apply extent size hints if obtained earlier */ |
| 2723 | if (unlikely(align)) { |
| 2724 | args.prod = align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2725 | if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) |
| 2726 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2727 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2728 | args.prod = 1; |
| 2729 | args.mod = 0; |
| 2730 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2731 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2732 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) |
| 2733 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2734 | } |
| 2735 | /* |
| 2736 | * If we are not low on available data blocks, and the |
| 2737 | * underlying logical volume manager is a stripe, and |
| 2738 | * the file offset is zero then try to allocate data |
| 2739 | * blocks on stripe unit boundary. |
| 2740 | * NOTE: ap->aeof is only set if the allocation length |
| 2741 | * is >= the stripe unit and the allocation offset is |
| 2742 | * at the end of file. |
| 2743 | */ |
| 2744 | if (!ap->low && ap->aeof) { |
| 2745 | if (!ap->off) { |
| 2746 | args.alignment = mp->m_dalign; |
| 2747 | atype = args.type; |
| 2748 | isaligned = 1; |
| 2749 | /* |
| 2750 | * Adjust for alignment |
| 2751 | */ |
| 2752 | if (blen > args.alignment && blen <= ap->alen) |
| 2753 | args.minlen = blen - args.alignment; |
| 2754 | args.minalignslop = 0; |
| 2755 | } else { |
| 2756 | /* |
| 2757 | * First try an exact bno allocation. |
| 2758 | * If it fails then do a near or start bno |
| 2759 | * allocation with alignment turned on. |
| 2760 | */ |
| 2761 | atype = args.type; |
| 2762 | tryagain = 1; |
| 2763 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2764 | args.alignment = 1; |
| 2765 | /* |
| 2766 | * Compute the minlen+alignment for the |
| 2767 | * next case. Set slop so that the value |
| 2768 | * of minlen+alignment+slop doesn't go up |
| 2769 | * between the calls. |
| 2770 | */ |
| 2771 | if (blen > mp->m_dalign && blen <= ap->alen) |
| 2772 | nextminlen = blen - mp->m_dalign; |
| 2773 | else |
| 2774 | nextminlen = args.minlen; |
| 2775 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2776 | args.minalignslop = |
| 2777 | nextminlen + mp->m_dalign - |
| 2778 | args.minlen - 1; |
| 2779 | else |
| 2780 | args.minalignslop = 0; |
| 2781 | } |
| 2782 | } else { |
| 2783 | args.alignment = 1; |
| 2784 | args.minalignslop = 0; |
| 2785 | } |
| 2786 | args.minleft = ap->minleft; |
| 2787 | args.wasdel = ap->wasdel; |
| 2788 | args.isfl = 0; |
| 2789 | args.userdata = ap->userdata; |
| 2790 | if ((error = xfs_alloc_vextent(&args))) |
| 2791 | return error; |
| 2792 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2793 | /* |
| 2794 | * Exact allocation failed. Now try with alignment |
| 2795 | * turned on. |
| 2796 | */ |
| 2797 | args.type = atype; |
| 2798 | args.fsbno = ap->rval; |
| 2799 | args.alignment = mp->m_dalign; |
| 2800 | args.minlen = nextminlen; |
| 2801 | args.minalignslop = 0; |
| 2802 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | if ((error = xfs_alloc_vextent(&args))) |
| 2804 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2805 | } |
| 2806 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2807 | /* |
| 2808 | * allocation failed, so turn off alignment and |
| 2809 | * try again. |
| 2810 | */ |
| 2811 | args.type = atype; |
| 2812 | args.fsbno = ap->rval; |
| 2813 | args.alignment = 0; |
| 2814 | if ((error = xfs_alloc_vextent(&args))) |
| 2815 | return error; |
| 2816 | } |
| 2817 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2818 | args.minlen > ap->minlen) { |
| 2819 | args.minlen = ap->minlen; |
| 2820 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2821 | args.fsbno = ap->rval; |
| 2822 | if ((error = xfs_alloc_vextent(&args))) |
| 2823 | return error; |
| 2824 | } |
| 2825 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2826 | args.fsbno = 0; |
| 2827 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2828 | args.total = ap->minlen; |
| 2829 | args.minleft = 0; |
| 2830 | if ((error = xfs_alloc_vextent(&args))) |
| 2831 | return error; |
| 2832 | ap->low = 1; |
| 2833 | } |
| 2834 | if (args.fsbno != NULLFSBLOCK) { |
| 2835 | ap->firstblock = ap->rval = args.fsbno; |
| 2836 | ASSERT(nullfb || fb_agno == args.agno || |
| 2837 | (ap->low && fb_agno < args.agno)); |
| 2838 | ap->alen = args.len; |
| 2839 | ap->ip->i_d.di_nblocks += args.len; |
| 2840 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2841 | if (ap->wasdel) |
| 2842 | ap->ip->i_delayed_blks -= args.len; |
| 2843 | /* |
| 2844 | * Adjust the disk quota also. This was reserved |
| 2845 | * earlier. |
| 2846 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2847 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2848 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2849 | XFS_TRANS_DQ_BCOUNT, |
| 2850 | (long) args.len); |
| 2851 | } else { |
| 2852 | ap->rval = NULLFSBLOCK; |
| 2853 | ap->alen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | } |
| 2855 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2860 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2861 | */ |
| 2862 | STATIC int |
| 2863 | xfs_bmap_alloc( |
| 2864 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2865 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2866 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2867 | return xfs_bmap_rtalloc(ap); |
| 2868 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | /* |
| 2872 | * Transform a btree format file with only one leaf node, where the |
| 2873 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2874 | * 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] | 2875 | * give up the space for the btree root and pitch the leaf block. |
| 2876 | */ |
| 2877 | STATIC int /* error */ |
| 2878 | xfs_bmap_btree_to_extents( |
| 2879 | xfs_trans_t *tp, /* transaction pointer */ |
| 2880 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2881 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2882 | int *logflagsp, /* inode logging flags */ |
| 2883 | int whichfork) /* data or attr fork */ |
| 2884 | { |
| 2885 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2886 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | xfs_fsblock_t cbno; /* child block number */ |
| 2888 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2889 | int error; /* error return value */ |
| 2890 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2891 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2892 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2893 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2895 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2897 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2898 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2899 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2900 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2901 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2902 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2903 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2904 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | *logflagsp = 0; |
| 2906 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2907 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | return error; |
| 2909 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2911 | XFS_BMAP_BTREE_REF))) |
| 2912 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2913 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2914 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | return error; |
| 2916 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2917 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2918 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | xfs_trans_binval(tp, cbp); |
| 2920 | if (cur->bc_bufs[0] == cbp) |
| 2921 | cur->bc_bufs[0] = NULL; |
| 2922 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2923 | ASSERT(ifp->if_broot == NULL); |
| 2924 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2925 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2926 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | return 0; |
| 2928 | } |
| 2929 | |
| 2930 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2931 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | * after removing space (or undoing a delayed allocation). |
| 2933 | */ |
| 2934 | STATIC int /* error */ |
| 2935 | xfs_bmap_del_extent( |
| 2936 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2937 | xfs_trans_t *tp, /* current transaction pointer */ |
| 2938 | xfs_extnum_t idx, /* extent number to update/delete */ |
| 2939 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2940 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2941 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2943 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | int whichfork, /* data or attr fork */ |
| 2945 | int rsvd) /* OK to allocate reserved blocks */ |
| 2946 | { |
| 2947 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2948 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2949 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2950 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2951 | int delay; /* current block is delayed allocated */ |
| 2952 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2953 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | int error; /* error return value */ |
| 2955 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2957 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2958 | int i; /* temp state */ |
| 2959 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2960 | xfs_mount_t *mp; /* mount structure */ |
| 2961 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2962 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2963 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | uint qfield; /* quota field to update */ |
| 2965 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2966 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2967 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | |
| 2969 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2970 | |
| 2971 | if (whichfork == XFS_ATTR_FORK) |
| 2972 | state |= BMAP_ATTRFORK; |
| 2973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | mp = ip->i_mount; |
| 2975 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2976 | ASSERT((idx >= 0) && (idx < ifp->if_bytes / |
| 2977 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | ASSERT(del->br_blockcount > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2979 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | xfs_bmbt_get_all(ep, &got); |
| 2981 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2982 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2983 | got_endoff = got.br_startoff + got.br_blockcount; |
| 2984 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2985 | delay = isnullstartblock(got.br_startblock); |
| 2986 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | flags = 0; |
| 2988 | qfield = 0; |
| 2989 | error = 0; |
| 2990 | /* |
| 2991 | * If deleting a real allocation, must free up the disk space. |
| 2992 | */ |
| 2993 | if (!delay) { |
| 2994 | flags = XFS_ILOG_CORE; |
| 2995 | /* |
| 2996 | * Realtime allocation. Free it and record di_nblocks update. |
| 2997 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2998 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | xfs_fsblock_t bno; |
| 3000 | xfs_filblks_t len; |
| 3001 | |
| 3002 | ASSERT(do_mod(del->br_blockcount, |
| 3003 | mp->m_sb.sb_rextsize) == 0); |
| 3004 | ASSERT(do_mod(del->br_startblock, |
| 3005 | mp->m_sb.sb_rextsize) == 0); |
| 3006 | bno = del->br_startblock; |
| 3007 | len = del->br_blockcount; |
| 3008 | do_div(bno, mp->m_sb.sb_rextsize); |
| 3009 | do_div(len, mp->m_sb.sb_rextsize); |
| 3010 | if ((error = xfs_rtfree_extent(ip->i_transp, bno, |
| 3011 | (xfs_extlen_t)len))) |
| 3012 | goto done; |
| 3013 | do_fx = 0; |
| 3014 | nblks = len * mp->m_sb.sb_rextsize; |
| 3015 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 3016 | } |
| 3017 | /* |
| 3018 | * Ordinary allocation. |
| 3019 | */ |
| 3020 | else { |
| 3021 | do_fx = 1; |
| 3022 | nblks = del->br_blockcount; |
| 3023 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 3024 | } |
| 3025 | /* |
| 3026 | * Set up del_endblock and cur for later. |
| 3027 | */ |
| 3028 | del_endblock = del->br_startblock + del->br_blockcount; |
| 3029 | if (cur) { |
| 3030 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 3031 | got.br_startblock, got.br_blockcount, |
| 3032 | &i))) |
| 3033 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3034 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | } |
| 3036 | da_old = da_new = 0; |
| 3037 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3038 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | da_new = 0; |
| 3040 | nblks = 0; |
| 3041 | do_fx = 0; |
| 3042 | } |
| 3043 | /* |
| 3044 | * Set flag value to use in switch statement. |
| 3045 | * Left-contig is 2, right-contig is 1. |
| 3046 | */ |
| 3047 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 3048 | (got_endoff == del_endoff)) { |
| 3049 | case 3: |
| 3050 | /* |
| 3051 | * Matches the whole extent. Delete the entry. |
| 3052 | */ |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 3053 | xfs_iext_remove(ip, idx, 1, |
| 3054 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | ifp->if_lastex = idx; |
| 3056 | if (delay) |
| 3057 | break; |
| 3058 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3059 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 3060 | flags |= XFS_ILOG_CORE; |
| 3061 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3062 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | break; |
| 3064 | } |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3065 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3067 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | break; |
| 3069 | |
| 3070 | case 2: |
| 3071 | /* |
| 3072 | * Deleting the first part of the extent. |
| 3073 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3074 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 3076 | temp = got.br_blockcount - del->br_blockcount; |
| 3077 | xfs_bmbt_set_blockcount(ep, temp); |
| 3078 | ifp->if_lastex = idx; |
| 3079 | if (delay) { |
| 3080 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3081 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3082 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3083 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | da_new = temp; |
| 3085 | break; |
| 3086 | } |
| 3087 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3088 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3090 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 3094 | got.br_blockcount - del->br_blockcount, |
| 3095 | got.br_state))) |
| 3096 | goto done; |
| 3097 | break; |
| 3098 | |
| 3099 | case 1: |
| 3100 | /* |
| 3101 | * Deleting the last part of the extent. |
| 3102 | */ |
| 3103 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3104 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | xfs_bmbt_set_blockcount(ep, temp); |
| 3106 | ifp->if_lastex = idx; |
| 3107 | if (delay) { |
| 3108 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3109 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3110 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3111 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | da_new = temp; |
| 3113 | break; |
| 3114 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3115 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3117 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | break; |
| 3119 | } |
| 3120 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 3121 | got.br_startblock, |
| 3122 | got.br_blockcount - del->br_blockcount, |
| 3123 | got.br_state))) |
| 3124 | goto done; |
| 3125 | break; |
| 3126 | |
| 3127 | case 0: |
| 3128 | /* |
| 3129 | * Deleting the middle of the extent. |
| 3130 | */ |
| 3131 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3132 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | xfs_bmbt_set_blockcount(ep, temp); |
| 3134 | new.br_startoff = del_endoff; |
| 3135 | temp2 = got_endoff - del_endoff; |
| 3136 | new.br_blockcount = temp2; |
| 3137 | new.br_state = got.br_state; |
| 3138 | if (!delay) { |
| 3139 | new.br_startblock = del_endblock; |
| 3140 | flags |= XFS_ILOG_CORE; |
| 3141 | if (cur) { |
| 3142 | if ((error = xfs_bmbt_update(cur, |
| 3143 | got.br_startoff, |
| 3144 | got.br_startblock, temp, |
| 3145 | got.br_state))) |
| 3146 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 3147 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | goto done; |
| 3149 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3150 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | if (error && error != ENOSPC) |
| 3152 | goto done; |
| 3153 | /* |
| 3154 | * If get no-space back from btree insert, |
| 3155 | * it tried a split, and we have a zero |
| 3156 | * block reservation. |
| 3157 | * Fix up our state and return the error. |
| 3158 | */ |
| 3159 | if (error == ENOSPC) { |
| 3160 | /* |
| 3161 | * Reset the cursor, don't trust |
| 3162 | * it after any insert operation. |
| 3163 | */ |
| 3164 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 3165 | got.br_startoff, |
| 3166 | got.br_startblock, |
| 3167 | temp, &i))) |
| 3168 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3169 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | /* |
| 3171 | * Update the btree record back |
| 3172 | * to the original value. |
| 3173 | */ |
| 3174 | if ((error = xfs_bmbt_update(cur, |
| 3175 | got.br_startoff, |
| 3176 | got.br_startblock, |
| 3177 | got.br_blockcount, |
| 3178 | got.br_state))) |
| 3179 | goto done; |
| 3180 | /* |
| 3181 | * Reset the extent record back |
| 3182 | * to the original value. |
| 3183 | */ |
| 3184 | xfs_bmbt_set_blockcount(ep, |
| 3185 | got.br_blockcount); |
| 3186 | flags = 0; |
| 3187 | error = XFS_ERROR(ENOSPC); |
| 3188 | goto done; |
| 3189 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3190 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3192 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3194 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3195 | } else { |
| 3196 | ASSERT(whichfork == XFS_DATA_FORK); |
| 3197 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3198 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3200 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | da_new = temp + temp2; |
| 3202 | while (da_new > da_old) { |
| 3203 | if (temp) { |
| 3204 | temp--; |
| 3205 | da_new--; |
| 3206 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3207 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | } |
| 3209 | if (da_new == da_old) |
| 3210 | break; |
| 3211 | if (temp2) { |
| 3212 | temp2--; |
| 3213 | da_new--; |
| 3214 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3215 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3219 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 3220 | xfs_iext_insert(ip, idx + 1, 1, &new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | ifp->if_lastex = idx + 1; |
| 3222 | break; |
| 3223 | } |
| 3224 | /* |
| 3225 | * If we need to, add to list of extents to delete. |
| 3226 | */ |
| 3227 | if (do_fx) |
| 3228 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 3229 | mp); |
| 3230 | /* |
| 3231 | * Adjust inode # blocks in the file. |
| 3232 | */ |
| 3233 | if (nblks) |
| 3234 | ip->i_d.di_nblocks -= nblks; |
| 3235 | /* |
| 3236 | * Adjust quota data. |
| 3237 | */ |
| 3238 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3239 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | |
| 3241 | /* |
| 3242 | * Account for change in delayed indirect blocks. |
| 3243 | * Nothing to do for disk quota accounting here. |
| 3244 | */ |
| 3245 | ASSERT(da_old >= da_new); |
| 3246 | if (da_old > da_new) |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 3247 | 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] | 3248 | rsvd); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3249 | if (delta) { |
| 3250 | /* DELTA: report the original extent. */ |
| 3251 | if (delta->xed_startoff > got.br_startoff) |
| 3252 | delta->xed_startoff = got.br_startoff; |
| 3253 | if (delta->xed_blockcount < got.br_startoff+got.br_blockcount) |
| 3254 | delta->xed_blockcount = got.br_startoff + |
| 3255 | got.br_blockcount; |
| 3256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | done: |
| 3258 | *logflagsp = flags; |
| 3259 | return error; |
| 3260 | } |
| 3261 | |
| 3262 | /* |
| 3263 | * Remove the entry "free" from the free item list. Prev points to the |
| 3264 | * previous entry, unless "free" is the head of the list. |
| 3265 | */ |
| 3266 | STATIC void |
| 3267 | xfs_bmap_del_free( |
| 3268 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3269 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3270 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3271 | { |
| 3272 | if (prev) |
| 3273 | prev->xbfi_next = free->xbfi_next; |
| 3274 | else |
| 3275 | flist->xbf_first = free->xbfi_next; |
| 3276 | flist->xbf_count--; |
| 3277 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3278 | } |
| 3279 | |
| 3280 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | * Convert an extents-format file into a btree-format file. |
| 3282 | * The new file will have a root block (in the inode) and a single child block. |
| 3283 | */ |
| 3284 | STATIC int /* error */ |
| 3285 | xfs_bmap_extents_to_btree( |
| 3286 | xfs_trans_t *tp, /* transaction pointer */ |
| 3287 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3288 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3289 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3290 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3291 | int wasdel, /* converting a delayed alloc */ |
| 3292 | int *logflagsp, /* inode logging flags */ |
| 3293 | int whichfork) /* data or attr fork */ |
| 3294 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3295 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3297 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3298 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3299 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3301 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3303 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3305 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3306 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3307 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3309 | |
| 3310 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3311 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3312 | ASSERT(ifp->if_ext_max == |
| 3313 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3314 | /* |
| 3315 | * Make space in the inode incore. |
| 3316 | */ |
| 3317 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3318 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | /* |
| 3321 | * Fill in the root. |
| 3322 | */ |
| 3323 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3324 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3325 | block->bb_level = cpu_to_be16(1); |
| 3326 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3327 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3328 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | /* |
| 3331 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3332 | */ |
| 3333 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3334 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3335 | cur->bc_private.b.firstblock = *firstblock; |
| 3336 | cur->bc_private.b.flist = flist; |
| 3337 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3338 | /* |
| 3339 | * Convert to a btree with two levels, one record in root. |
| 3340 | */ |
| 3341 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3342 | args.tp = tp; |
| 3343 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3344 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | if (*firstblock == NULLFSBLOCK) { |
| 3346 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3347 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3348 | } else if (flist->xbf_low) { |
| 3349 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3350 | args.fsbno = *firstblock; |
| 3351 | } else { |
| 3352 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3353 | args.fsbno = *firstblock; |
| 3354 | } |
| 3355 | args.minlen = args.maxlen = args.prod = 1; |
| 3356 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3357 | args.minalignslop = 0; |
| 3358 | args.wasdel = wasdel; |
| 3359 | *logflagsp = 0; |
| 3360 | if ((error = xfs_alloc_vextent(&args))) { |
| 3361 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3362 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3363 | return error; |
| 3364 | } |
| 3365 | /* |
| 3366 | * Allocation can't fail, the space was reserved. |
| 3367 | */ |
| 3368 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3369 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3370 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3371 | (flist->xbf_low && |
| 3372 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3373 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3374 | cur->bc_private.b.allocated++; |
| 3375 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3376 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3378 | /* |
| 3379 | * Fill in the child block. |
| 3380 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3381 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3382 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3384 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3385 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3386 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3387 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3388 | for (cnt = i = 0; i < nextents; i++) { |
| 3389 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3390 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3391 | arp->l0 = cpu_to_be64(ep->l0); |
| 3392 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | arp++; cnt++; |
| 3394 | } |
| 3395 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3396 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3397 | xfs_btree_set_numrecs(ablock, cnt); |
| 3398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | /* |
| 3400 | * Fill in the root key and pointer. |
| 3401 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3402 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3403 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3404 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3405 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3406 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3407 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | /* |
| 3410 | * Do all this logging at the end so that |
| 3411 | * the root is at the right level. |
| 3412 | */ |
Christoph Hellwig | fd6bcc5 | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3413 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3414 | xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | ASSERT(*curp == NULL); |
| 3416 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3417 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | return 0; |
| 3419 | } |
| 3420 | |
| 3421 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3422 | * Calculate the default attribute fork offset for newly created inodes. |
| 3423 | */ |
| 3424 | uint |
| 3425 | xfs_default_attroffset( |
| 3426 | struct xfs_inode *ip) |
| 3427 | { |
| 3428 | struct xfs_mount *mp = ip->i_mount; |
| 3429 | uint offset; |
| 3430 | |
| 3431 | if (mp->m_sb.sb_inodesize == 256) { |
| 3432 | offset = XFS_LITINO(mp) - |
| 3433 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3434 | } else { |
| 3435 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3436 | } |
| 3437 | |
| 3438 | ASSERT(offset < XFS_LITINO(mp)); |
| 3439 | return offset; |
| 3440 | } |
| 3441 | |
| 3442 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3443 | * Helper routine to reset inode di_forkoff field when switching |
| 3444 | * attribute fork from local to extent format - we reset it where |
| 3445 | * possible to make space available for inline data fork extents. |
| 3446 | */ |
| 3447 | STATIC void |
| 3448 | xfs_bmap_forkoff_reset( |
| 3449 | xfs_mount_t *mp, |
| 3450 | xfs_inode_t *ip, |
| 3451 | int whichfork) |
| 3452 | { |
| 3453 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3454 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3455 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3456 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3457 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3458 | |
| 3459 | if (dfl_forkoff > ip->i_d.di_forkoff) { |
| 3460 | ip->i_d.di_forkoff = dfl_forkoff; |
| 3461 | ip->i_df.if_ext_max = |
| 3462 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3463 | ip->i_afp->if_ext_max = |
| 3464 | XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3465 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | * Convert a local file to an extents file. |
| 3471 | * This code is out of bounds for data forks of regular files, |
| 3472 | * since the file data needs to get logged so things will stay consistent. |
| 3473 | * (The bmap-level manipulations are ok, though). |
| 3474 | */ |
| 3475 | STATIC int /* error */ |
| 3476 | xfs_bmap_local_to_extents( |
| 3477 | xfs_trans_t *tp, /* transaction pointer */ |
| 3478 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3479 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3480 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3481 | int *logflagsp, /* inode logging flags */ |
| 3482 | int whichfork) /* data or attr fork */ |
| 3483 | { |
| 3484 | int error; /* error return value */ |
| 3485 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3487 | |
| 3488 | /* |
| 3489 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3490 | * So sending the data fork of a regular inode is invalid. |
| 3491 | */ |
| 3492 | ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG && |
| 3493 | whichfork == XFS_DATA_FORK)); |
| 3494 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3495 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3496 | flags = 0; |
| 3497 | error = 0; |
| 3498 | if (ifp->if_bytes) { |
| 3499 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3500 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3501 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | |
| 3503 | args.tp = tp; |
| 3504 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3505 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3506 | ASSERT((ifp->if_flags & |
| 3507 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3508 | /* |
| 3509 | * Allocate a block. We know we need only one, since the |
| 3510 | * file currently fits in an inode. |
| 3511 | */ |
| 3512 | if (*firstblock == NULLFSBLOCK) { |
| 3513 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3514 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3515 | } else { |
| 3516 | args.fsbno = *firstblock; |
| 3517 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3518 | } |
| 3519 | args.total = total; |
| 3520 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3521 | args.isfl = args.minalignslop = 0; |
| 3522 | args.minlen = args.maxlen = args.prod = 1; |
| 3523 | if ((error = xfs_alloc_vextent(&args))) |
| 3524 | goto done; |
| 3525 | /* |
| 3526 | * Can't fail, the space was reserved. |
| 3527 | */ |
| 3528 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3529 | ASSERT(args.len == 1); |
| 3530 | *firstblock = args.fsbno; |
| 3531 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 3532 | memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data, |
| 3533 | ifp->if_bytes); |
| 3534 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3535 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3537 | xfs_iext_add(ifp, 0, 1); |
| 3538 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3540 | trace_xfs_bmap_post_update(ip, 0, |
| 3541 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3542 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3544 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3545 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3547 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3548 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3550 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3551 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3553 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3554 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3555 | flags |= XFS_ILOG_CORE; |
| 3556 | done: |
| 3557 | *logflagsp = flags; |
| 3558 | return error; |
| 3559 | } |
| 3560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3562 | * Search the extent records for the entry containing block bno. |
| 3563 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3564 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3565 | * entry (null if none). Else, *lastxp will be set to the index |
| 3566 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3567 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3568 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3569 | xfs_bmap_search_multi_extents( |
| 3570 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3571 | xfs_fileoff_t bno, /* block number searched for */ |
| 3572 | int *eofp, /* out: end of file found */ |
| 3573 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3574 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3575 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3576 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3577 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3578 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3579 | |
| 3580 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3581 | * Initialize the extent entry structure to catch access to |
| 3582 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3583 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3584 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3585 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3586 | gotp->br_state = XFS_EXT_INVALID; |
| 3587 | #if XFS_BIG_BLKNOS |
| 3588 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3589 | #else |
| 3590 | gotp->br_startblock = 0xffffa5a5; |
| 3591 | #endif |
| 3592 | prevp->br_startoff = NULLFILEOFF; |
| 3593 | |
| 3594 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3595 | if (lastx > 0) { |
| 3596 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3597 | } |
| 3598 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3599 | xfs_bmbt_get_all(ep, gotp); |
| 3600 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3601 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3602 | if (lastx > 0) { |
| 3603 | *gotp = *prevp; |
| 3604 | } |
| 3605 | *eofp = 1; |
| 3606 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3607 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3608 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3609 | return ep; |
| 3610 | } |
| 3611 | |
| 3612 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | * Search the extents list for the inode, for the extent containing bno. |
| 3614 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3615 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3616 | * Else, *lastxp will be set to the index of the found |
| 3617 | * entry; *gotp will contain the entry. |
| 3618 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3619 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | xfs_bmap_search_extents( |
| 3621 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3622 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3623 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3624 | int *eofp, /* out: end of file found */ |
| 3625 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3626 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3627 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3628 | { |
| 3629 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3630 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | |
| 3632 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3633 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3635 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3636 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3637 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3638 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 3639 | xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount, |
| 3640 | "Access to block zero in inode %llu " |
| 3641 | "start_block: %llx start_off: %llx " |
| 3642 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3643 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3644 | (unsigned long long)gotp->br_startblock, |
| 3645 | (unsigned long long)gotp->br_startoff, |
| 3646 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3647 | gotp->br_state, *lastxp); |
| 3648 | *lastxp = NULLEXTNUM; |
| 3649 | *eofp = 1; |
| 3650 | return NULL; |
| 3651 | } |
| 3652 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | } |
| 3654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | /* |
| 3656 | * Compute the worst-case number of indirect blocks that will be used |
| 3657 | * for ip's delayed extent of length "len". |
| 3658 | */ |
| 3659 | STATIC xfs_filblks_t |
| 3660 | xfs_bmap_worst_indlen( |
| 3661 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3662 | xfs_filblks_t len) /* delayed extent length */ |
| 3663 | { |
| 3664 | int level; /* btree level number */ |
| 3665 | int maxrecs; /* maximum record count at this level */ |
| 3666 | xfs_mount_t *mp; /* mount structure */ |
| 3667 | xfs_filblks_t rval; /* return value */ |
| 3668 | |
| 3669 | mp = ip->i_mount; |
| 3670 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3671 | for (level = 0, rval = 0; |
| 3672 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3673 | level++) { |
| 3674 | len += maxrecs - 1; |
| 3675 | do_div(len, maxrecs); |
| 3676 | rval += len; |
| 3677 | if (len == 1) |
| 3678 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3679 | level - 1; |
| 3680 | if (level == 0) |
| 3681 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3682 | } |
| 3683 | return rval; |
| 3684 | } |
| 3685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | /* |
| 3687 | * Convert inode from non-attributed to attributed. |
| 3688 | * Must not be in a transaction, ip must not be locked. |
| 3689 | */ |
| 3690 | int /* error code */ |
| 3691 | xfs_bmap_add_attrfork( |
| 3692 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3693 | int size, /* space new attribute needs */ |
| 3694 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3697 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3700 | int blks; /* space reservation */ |
| 3701 | int version = 1; /* superblock attr version */ |
| 3702 | int committed; /* xaction was committed */ |
| 3703 | int logflags; /* logging flags */ |
| 3704 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3706 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | ASSERT(ip->i_df.if_ext_max == |
| 3708 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | mp = ip->i_mount; |
| 3711 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3712 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3713 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3714 | if (rsvd) |
| 3715 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3716 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3717 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3718 | goto error0; |
| 3719 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3720 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3722 | XFS_QMOPT_RES_REGBLKS); |
| 3723 | if (error) { |
| 3724 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3725 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3726 | return error; |
| 3727 | } |
| 3728 | if (XFS_IFORK_Q(ip)) |
| 3729 | goto error1; |
| 3730 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3731 | /* |
| 3732 | * For inodes coming from pre-6.2 filesystems. |
| 3733 | */ |
| 3734 | ASSERT(ip->i_d.di_aformat == 0); |
| 3735 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3736 | } |
| 3737 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 0b1f9177 | 2008-08-13 16:13:09 +1000 | [diff] [blame] | 3738 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3740 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3741 | switch (ip->i_d.di_format) { |
| 3742 | case XFS_DINODE_FMT_DEV: |
| 3743 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3744 | break; |
| 3745 | case XFS_DINODE_FMT_UUID: |
| 3746 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3747 | break; |
| 3748 | case XFS_DINODE_FMT_LOCAL: |
| 3749 | case XFS_DINODE_FMT_EXTENTS: |
| 3750 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3751 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3752 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3753 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3754 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3755 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | break; |
| 3757 | default: |
| 3758 | ASSERT(0); |
| 3759 | error = XFS_ERROR(EINVAL); |
| 3760 | goto error1; |
| 3761 | } |
| 3762 | ip->i_df.if_ext_max = |
| 3763 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3764 | ASSERT(ip->i_afp == NULL); |
| 3765 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 3766 | ip->i_afp->if_ext_max = |
| 3767 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3768 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3769 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3770 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | switch (ip->i_d.di_format) { |
| 3772 | case XFS_DINODE_FMT_LOCAL: |
| 3773 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3774 | &logflags); |
| 3775 | break; |
| 3776 | case XFS_DINODE_FMT_EXTENTS: |
| 3777 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3778 | &flist, &logflags); |
| 3779 | break; |
| 3780 | case XFS_DINODE_FMT_BTREE: |
| 3781 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3782 | &logflags); |
| 3783 | break; |
| 3784 | default: |
| 3785 | error = 0; |
| 3786 | break; |
| 3787 | } |
| 3788 | if (logflags) |
| 3789 | xfs_trans_log_inode(tp, ip, logflags); |
| 3790 | if (error) |
| 3791 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3792 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3793 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3794 | __int64_t sbfields = 0; |
| 3795 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3796 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3797 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3798 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3799 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3800 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3801 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3802 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3803 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3804 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3805 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3806 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3807 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3809 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3811 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | goto error2; |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3813 | error = xfs_trans_commit(tp, XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | ASSERT(ip->i_df.if_ext_max == |
| 3815 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3816 | return error; |
| 3817 | error2: |
| 3818 | xfs_bmap_cancel(&flist); |
| 3819 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3821 | error0: |
| 3822 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 3823 | ASSERT(ip->i_df.if_ext_max == |
| 3824 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3825 | return error; |
| 3826 | } |
| 3827 | |
| 3828 | /* |
| 3829 | * Add the extent to the list of extents to be free at transaction end. |
| 3830 | * The list is maintained sorted (by block number). |
| 3831 | */ |
| 3832 | /* ARGSUSED */ |
| 3833 | void |
| 3834 | xfs_bmap_add_free( |
| 3835 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3836 | xfs_filblks_t len, /* length of extent */ |
| 3837 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3838 | xfs_mount_t *mp) /* mount point structure */ |
| 3839 | { |
| 3840 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3841 | xfs_bmap_free_item_t *new; /* new element */ |
| 3842 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3843 | #ifdef DEBUG |
| 3844 | xfs_agnumber_t agno; |
| 3845 | xfs_agblock_t agbno; |
| 3846 | |
| 3847 | ASSERT(bno != NULLFSBLOCK); |
| 3848 | ASSERT(len > 0); |
| 3849 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3850 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3852 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3853 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3854 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3855 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3856 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3857 | #endif |
| 3858 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3859 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3860 | new->xbfi_startblock = bno; |
| 3861 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3862 | for (prev = NULL, cur = flist->xbf_first; |
| 3863 | cur != NULL; |
| 3864 | prev = cur, cur = cur->xbfi_next) { |
| 3865 | if (cur->xbfi_startblock >= bno) |
| 3866 | break; |
| 3867 | } |
| 3868 | if (prev) |
| 3869 | prev->xbfi_next = new; |
| 3870 | else |
| 3871 | flist->xbf_first = new; |
| 3872 | new->xbfi_next = cur; |
| 3873 | flist->xbf_count++; |
| 3874 | } |
| 3875 | |
| 3876 | /* |
| 3877 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3878 | * in this filesystem. Done once, during mount. |
| 3879 | */ |
| 3880 | void |
| 3881 | xfs_bmap_compute_maxlevels( |
| 3882 | xfs_mount_t *mp, /* file system mount structure */ |
| 3883 | int whichfork) /* data or attr fork */ |
| 3884 | { |
| 3885 | int level; /* btree level */ |
| 3886 | uint maxblocks; /* max blocks at this level */ |
| 3887 | uint maxleafents; /* max leaf entries possible */ |
| 3888 | int maxrootrecs; /* max records in root block */ |
| 3889 | int minleafrecs; /* min records in leaf block */ |
| 3890 | int minnoderecs; /* min records in node block */ |
| 3891 | int sz; /* root block size */ |
| 3892 | |
| 3893 | /* |
| 3894 | * The maximum number of extents in a file, hence the maximum |
| 3895 | * number of leaf entries, is controlled by the type of di_nextents |
| 3896 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3897 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3898 | * |
| 3899 | * Note that we can no longer assume that if we are in ATTR1 that |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3900 | * the fork offset of all the inodes will be |
| 3901 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3902 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3903 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3904 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3905 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3907 | if (whichfork == XFS_DATA_FORK) { |
| 3908 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3909 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3910 | } else { |
| 3911 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3912 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3913 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3914 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3916 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3918 | for (level = 1; maxblocks > 1; level++) { |
| 3919 | if (maxblocks <= maxrootrecs) |
| 3920 | maxblocks = 1; |
| 3921 | else |
| 3922 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3923 | } |
| 3924 | mp->m_bm_maxlevels[whichfork] = level; |
| 3925 | } |
| 3926 | |
| 3927 | /* |
| 3928 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3929 | * caller. Frees all the extents that need freeing, which must be done |
| 3930 | * last due to locking considerations. We never free any extents in |
| 3931 | * the first transaction. This is to allow the caller to make the first |
| 3932 | * transaction a synchronous one so that the pointers to the data being |
| 3933 | * broken in this transaction will be permanent before the data is actually |
| 3934 | * freed. This is necessary to prevent blocks from being reallocated |
| 3935 | * and written to before the free and reallocation are actually permanent. |
| 3936 | * We do not just make the first transaction synchronous here, because |
| 3937 | * there are more efficient ways to gain the same protection in some cases |
| 3938 | * (see the file truncation code). |
| 3939 | * |
| 3940 | * Return 1 if the given transaction was committed and a new one |
| 3941 | * started, and 0 otherwise in the committed parameter. |
| 3942 | */ |
| 3943 | /*ARGSUSED*/ |
| 3944 | int /* error */ |
| 3945 | xfs_bmap_finish( |
| 3946 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3947 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | int *committed) /* xact committed or not */ |
| 3949 | { |
| 3950 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3951 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3952 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3953 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | unsigned int logres; /* new log reservation */ |
| 3955 | unsigned int logcount; /* new log count */ |
| 3956 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3957 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3958 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3959 | |
| 3960 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3961 | if (flist->xbf_count == 0) { |
| 3962 | *committed = 0; |
| 3963 | return 0; |
| 3964 | } |
| 3965 | ntp = *tp; |
| 3966 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3967 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3968 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3969 | free->xbfi_blockcount); |
| 3970 | logres = ntp->t_log_res; |
| 3971 | logcount = ntp->t_log_count; |
| 3972 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3973 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | *tp = ntp; |
| 3975 | *committed = 1; |
| 3976 | /* |
| 3977 | * We have a new transaction, so we should return committed=1, |
| 3978 | * even though we're returning an error. |
| 3979 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3980 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3982 | |
| 3983 | /* |
| 3984 | * transaction commit worked ok so we can drop the extra ticket |
| 3985 | * reference that we gained in xfs_trans_dup() |
| 3986 | */ |
| 3987 | xfs_log_ticket_put(ntp->t_ticket); |
| 3988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 3990 | logcount))) |
| 3991 | return error; |
| 3992 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 3993 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 3994 | next = free->xbfi_next; |
| 3995 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 3996 | free->xbfi_blockcount))) { |
| 3997 | /* |
| 3998 | * The bmap free list will be cleaned up at a |
| 3999 | * higher level. The EFI will be canceled when |
| 4000 | * this transaction is aborted. |
| 4001 | * Need to force shutdown here to make sure it |
| 4002 | * happens, since this transaction may not be |
| 4003 | * dirty yet. |
| 4004 | */ |
| 4005 | mp = ntp->t_mountp; |
| 4006 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 4007 | xfs_force_shutdown(mp, |
| 4008 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 4009 | SHUTDOWN_CORRUPT_INCORE : |
| 4010 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | return error; |
| 4012 | } |
| 4013 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 4014 | free->xbfi_blockcount); |
| 4015 | xfs_bmap_del_free(flist, NULL, free); |
| 4016 | } |
| 4017 | return 0; |
| 4018 | } |
| 4019 | |
| 4020 | /* |
| 4021 | * Free up any items left in the list. |
| 4022 | */ |
| 4023 | void |
| 4024 | xfs_bmap_cancel( |
| 4025 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 4026 | { |
| 4027 | xfs_bmap_free_item_t *free; /* free list item */ |
| 4028 | xfs_bmap_free_item_t *next; |
| 4029 | |
| 4030 | if (flist->xbf_count == 0) |
| 4031 | return; |
| 4032 | ASSERT(flist->xbf_first != NULL); |
| 4033 | for (free = flist->xbf_first; free; free = next) { |
| 4034 | next = free->xbfi_next; |
| 4035 | xfs_bmap_del_free(flist, NULL, free); |
| 4036 | } |
| 4037 | ASSERT(flist->xbf_count == 0); |
| 4038 | } |
| 4039 | |
| 4040 | /* |
| 4041 | * Returns the file-relative block number of the first unused block(s) |
| 4042 | * in the file with at least "len" logically contiguous blocks free. |
| 4043 | * This is the lowest-address hole if the file has holes, else the first block |
| 4044 | * past the end of file. |
| 4045 | * Return 0 if the file is currently local (in-inode). |
| 4046 | */ |
| 4047 | int /* error */ |
| 4048 | xfs_bmap_first_unused( |
| 4049 | xfs_trans_t *tp, /* transaction pointer */ |
| 4050 | xfs_inode_t *ip, /* incore inode */ |
| 4051 | xfs_extlen_t len, /* size of hole to find */ |
| 4052 | xfs_fileoff_t *first_unused, /* unused block */ |
| 4053 | int whichfork) /* data or attr fork */ |
| 4054 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4056 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4058 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 4059 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 4060 | xfs_fileoff_t max; /* starting useful block */ |
| 4061 | xfs_fileoff_t off; /* offset for this block */ |
| 4062 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4063 | |
| 4064 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 4065 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 4066 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 4067 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4068 | *first_unused = 0; |
| 4069 | return 0; |
| 4070 | } |
| 4071 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4072 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4073 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4074 | return error; |
| 4075 | lowest = *first_unused; |
| 4076 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4077 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4078 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | off = xfs_bmbt_get_startoff(ep); |
| 4080 | /* |
| 4081 | * See if the hole before this extent will work. |
| 4082 | */ |
| 4083 | if (off >= lowest + len && off - max >= len) { |
| 4084 | *first_unused = max; |
| 4085 | return 0; |
| 4086 | } |
| 4087 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 4088 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 4089 | } |
| 4090 | *first_unused = max; |
| 4091 | return 0; |
| 4092 | } |
| 4093 | |
| 4094 | /* |
| 4095 | * Returns the file-relative block number of the last block + 1 before |
| 4096 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4097 | * This is not based on i_size, it is based on the extent records. |
| 4098 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | */ |
| 4100 | int /* error */ |
| 4101 | xfs_bmap_last_before( |
| 4102 | xfs_trans_t *tp, /* transaction pointer */ |
| 4103 | xfs_inode_t *ip, /* incore inode */ |
| 4104 | xfs_fileoff_t *last_block, /* last block */ |
| 4105 | int whichfork) /* data or attr fork */ |
| 4106 | { |
| 4107 | xfs_fileoff_t bno; /* input file offset */ |
| 4108 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4109 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | int error; /* error return value */ |
| 4111 | xfs_bmbt_irec_t got; /* current extent value */ |
| 4112 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4113 | xfs_extnum_t lastx; /* last extent used */ |
| 4114 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 4115 | |
| 4116 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4117 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4118 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4119 | return XFS_ERROR(EIO); |
| 4120 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4121 | *last_block = 0; |
| 4122 | return 0; |
| 4123 | } |
| 4124 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4125 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4126 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4127 | return error; |
| 4128 | bno = *last_block - 1; |
| 4129 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4130 | &prev); |
| 4131 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 4132 | if (prev.br_startoff == NULLFILEOFF) |
| 4133 | *last_block = 0; |
| 4134 | else |
| 4135 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 4136 | } |
| 4137 | /* |
| 4138 | * Otherwise *last_block is already the right answer. |
| 4139 | */ |
| 4140 | return 0; |
| 4141 | } |
| 4142 | |
| 4143 | /* |
| 4144 | * 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] | 4145 | * the file. This is not based on i_size, it is based on the extent records. |
| 4146 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | */ |
| 4148 | int /* error */ |
| 4149 | xfs_bmap_last_offset( |
| 4150 | xfs_trans_t *tp, /* transaction pointer */ |
| 4151 | xfs_inode_t *ip, /* incore inode */ |
| 4152 | xfs_fileoff_t *last_block, /* last block */ |
| 4153 | int whichfork) /* data or attr fork */ |
| 4154 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4155 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | int error; /* error return value */ |
| 4157 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4158 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4159 | |
| 4160 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4161 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4162 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4163 | return XFS_ERROR(EIO); |
| 4164 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4165 | *last_block = 0; |
| 4166 | return 0; |
| 4167 | } |
| 4168 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4169 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4170 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4171 | return error; |
| 4172 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4173 | if (!nextents) { |
| 4174 | *last_block = 0; |
| 4175 | return 0; |
| 4176 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4177 | ep = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); |
| 4179 | return 0; |
| 4180 | } |
| 4181 | |
| 4182 | /* |
| 4183 | * Returns whether the selected fork of the inode has exactly one |
| 4184 | * block or not. For the data fork we check this matches di_size, |
| 4185 | * implying the file's range is 0..bsize-1. |
| 4186 | */ |
| 4187 | int /* 1=>1 block, 0=>otherwise */ |
| 4188 | xfs_bmap_one_block( |
| 4189 | xfs_inode_t *ip, /* incore inode */ |
| 4190 | int whichfork) /* data or attr fork */ |
| 4191 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4192 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4194 | int rval; /* return value */ |
| 4195 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 4196 | |
| 4197 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4198 | if (whichfork == XFS_DATA_FORK) { |
| 4199 | return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ? |
| 4200 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 4201 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 4202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | #endif /* !DEBUG */ |
| 4204 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4205 | return 0; |
| 4206 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4207 | return 0; |
| 4208 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4209 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4210 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | xfs_bmbt_get_all(ep, &s); |
| 4212 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4213 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4214 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | return rval; |
| 4216 | } |
| 4217 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4218 | STATIC int |
| 4219 | xfs_bmap_sanity_check( |
| 4220 | struct xfs_mount *mp, |
| 4221 | struct xfs_buf *bp, |
| 4222 | int level) |
| 4223 | { |
| 4224 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4225 | |
| 4226 | if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC || |
| 4227 | be16_to_cpu(block->bb_level) != level || |
| 4228 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4229 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4230 | return 0; |
| 4231 | return 1; |
| 4232 | } |
| 4233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | /* |
| 4235 | * Read in the extents to if_extents. |
| 4236 | * All inode fields are set up by caller, we just traverse the btree |
| 4237 | * and copy the records in. If the file system cannot contain unwritten |
| 4238 | * extents, the records are checked for no "state" flags. |
| 4239 | */ |
| 4240 | int /* error */ |
| 4241 | xfs_bmap_read_extents( |
| 4242 | xfs_trans_t *tp, /* transaction pointer */ |
| 4243 | xfs_inode_t *ip, /* incore inode */ |
| 4244 | int whichfork) /* data or attr fork */ |
| 4245 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4246 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4248 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4249 | int error; /* error return value */ |
| 4250 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4252 | xfs_ifork_t *ifp; /* fork structure */ |
| 4253 | int level; /* btree level, for checking */ |
| 4254 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4255 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | /* REFERENCED */ |
| 4257 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | |
| 4259 | bno = NULLFSBLOCK; |
| 4260 | mp = ip->i_mount; |
| 4261 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4262 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4263 | XFS_EXTFMT_INODE(ip); |
| 4264 | block = ifp->if_broot; |
| 4265 | /* |
| 4266 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4267 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4268 | level = be16_to_cpu(block->bb_level); |
| 4269 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4270 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4271 | bno = be64_to_cpu(*pp); |
| 4272 | ASSERT(bno != NULLDFSBNO); |
| 4273 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4274 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | /* |
| 4276 | * Go down the tree until leaf level is reached, following the first |
| 4277 | * pointer (leftmost) at each level. |
| 4278 | */ |
| 4279 | while (level-- > 0) { |
| 4280 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4281 | XFS_BMAP_BTREE_REF))) |
| 4282 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4283 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4285 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | error0); |
| 4287 | if (level == 0) |
| 4288 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4289 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4290 | bno = be64_to_cpu(*pp); |
| 4291 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | xfs_trans_brelse(tp, bp); |
| 4293 | } |
| 4294 | /* |
| 4295 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4296 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4297 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | i = 0; |
| 4299 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4300 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | */ |
| 4302 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4303 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | xfs_fsblock_t nextbno; |
| 4305 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4306 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
| 4308 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4309 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | if (unlikely(i + num_recs > room)) { |
| 4311 | ASSERT(i + num_recs <= room); |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4312 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 4313 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | (unsigned long long) ip->i_ino); |
| 4315 | XFS_ERROR_REPORT("xfs_bmap_read_extents(1)", |
| 4316 | XFS_ERRLEVEL_LOW, |
| 4317 | ip->i_mount); |
| 4318 | goto error0; |
| 4319 | } |
| 4320 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4321 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | error0); |
| 4323 | /* |
| 4324 | * Read-ahead the next leaf block, if any. |
| 4325 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4326 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | if (nextbno != NULLFSBLOCK) |
| 4328 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4329 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4330 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4332 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4333 | start = i; |
| 4334 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4335 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4336 | trp->l0 = be64_to_cpu(frp->l0); |
| 4337 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | } |
| 4339 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4340 | /* |
| 4341 | * Check all attribute bmap btree records and |
| 4342 | * any "older" data bmap btree records for a |
| 4343 | * set bit in the "extent flag" position. |
| 4344 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4345 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4346 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4348 | XFS_ERRLEVEL_LOW, |
| 4349 | ip->i_mount); |
| 4350 | goto error0; |
| 4351 | } |
| 4352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | xfs_trans_brelse(tp, bp); |
| 4354 | bno = nextbno; |
| 4355 | /* |
| 4356 | * If we've reached the end, stop. |
| 4357 | */ |
| 4358 | if (bno == NULLFSBLOCK) |
| 4359 | break; |
| 4360 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4361 | XFS_BMAP_BTREE_REF))) |
| 4362 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4363 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4365 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4367 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | return 0; |
| 4369 | error0: |
| 4370 | xfs_trans_brelse(tp, bp); |
| 4371 | return XFS_ERROR(EFSCORRUPTED); |
| 4372 | } |
| 4373 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4374 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4376 | * 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] | 4377 | */ |
| 4378 | void |
| 4379 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4381 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4382 | int whichfork, /* data or attr fork */ |
| 4383 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4385 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4387 | int state = 0; |
| 4388 | |
| 4389 | if (whichfork == XFS_ATTR_FORK) |
| 4390 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | |
| 4392 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4393 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4394 | for (idx = 0; idx < cnt; idx++) |
| 4395 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | /* |
| 4399 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4400 | * given the callers original parameters. Specifically check the |
| 4401 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4402 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4403 | */ |
| 4404 | STATIC void |
| 4405 | xfs_bmap_validate_ret( |
| 4406 | xfs_fileoff_t bno, |
| 4407 | xfs_filblks_t len, |
| 4408 | int flags, |
| 4409 | xfs_bmbt_irec_t *mval, |
| 4410 | int nmap, |
| 4411 | int ret_nmap) |
| 4412 | { |
| 4413 | int i; /* index to map values */ |
| 4414 | |
| 4415 | ASSERT(ret_nmap <= nmap); |
| 4416 | |
| 4417 | for (i = 0; i < ret_nmap; i++) { |
| 4418 | ASSERT(mval[i].br_blockcount > 0); |
| 4419 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4420 | ASSERT(mval[i].br_startoff >= bno); |
| 4421 | ASSERT(mval[i].br_blockcount <= len); |
| 4422 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4423 | bno + len); |
| 4424 | } else { |
| 4425 | ASSERT(mval[i].br_startoff < bno + len); |
| 4426 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4427 | bno); |
| 4428 | } |
| 4429 | ASSERT(i == 0 || |
| 4430 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4431 | mval[i].br_startoff); |
| 4432 | if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY)) |
| 4433 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4434 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 4435 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4436 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4437 | } |
| 4438 | } |
| 4439 | #endif /* DEBUG */ |
| 4440 | |
| 4441 | |
| 4442 | /* |
| 4443 | * Map file blocks to filesystem blocks. |
| 4444 | * File range is given by the bno/len pair. |
| 4445 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 4446 | * into a hole or past eof. |
| 4447 | * Only allocates blocks from a single allocation group, |
| 4448 | * to avoid locking problems. |
| 4449 | * The returned value in "firstblock" from the first call in a transaction |
| 4450 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4451 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4452 | * the first call in "total"; if no allocation group has that many free |
| 4453 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4454 | */ |
| 4455 | int /* error */ |
| 4456 | xfs_bmapi( |
| 4457 | xfs_trans_t *tp, /* transaction pointer */ |
| 4458 | xfs_inode_t *ip, /* incore inode */ |
| 4459 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4460 | xfs_filblks_t len, /* length to map in file */ |
| 4461 | int flags, /* XFS_BMAPI_... */ |
| 4462 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4463 | controls a.g. for allocs */ |
| 4464 | xfs_extlen_t total, /* total blocks needed */ |
| 4465 | xfs_bmbt_irec_t *mval, /* output: map values */ |
| 4466 | int *nmap, /* i/o: mval size/count */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4467 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 4468 | xfs_extdelta_t *delta) /* o: change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | { |
| 4470 | xfs_fsblock_t abno; /* allocated block number */ |
| 4471 | xfs_extlen_t alen; /* allocated extent length */ |
| 4472 | xfs_fileoff_t aoff; /* allocated file offset */ |
| 4473 | xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | xfs_fileoff_t end; /* end of mapped file region */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4476 | int eof; /* we've hit the end of extents */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4477 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4479 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4481 | xfs_extlen_t indlen; /* indirect blocks length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4483 | int logflags; /* flags for transaction logging */ |
| 4484 | xfs_extlen_t minleft; /* min blocks left after allocation */ |
| 4485 | xfs_extlen_t minlen; /* min allocation size */ |
| 4486 | xfs_mount_t *mp; /* xfs mount structure */ |
| 4487 | int n; /* current extent index */ |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 4488 | int nallocs; /* number of extents alloc'd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4490 | xfs_fileoff_t obno; /* old block number (offset) */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4491 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | int tmp_logflags; /* temp flags holder */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4493 | int whichfork; /* data or attr fork */ |
| 4494 | char inhole; /* current location is hole in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | char wasdelay; /* old extent was delayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | char wr; /* this is a write request */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4497 | char rt; /* this is a realtime file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | #ifdef DEBUG |
| 4499 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4500 | int orig_flags; /* original flags arg value */ |
| 4501 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4502 | xfs_bmbt_irec_t *orig_mval; /* original value of mval */ |
| 4503 | int orig_nmap; /* original value of *nmap */ |
| 4504 | |
| 4505 | orig_bno = bno; |
| 4506 | orig_len = len; |
| 4507 | orig_flags = flags; |
| 4508 | orig_mval = mval; |
| 4509 | orig_nmap = *nmap; |
| 4510 | #endif |
| 4511 | ASSERT(*nmap >= 1); |
| 4512 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); |
| 4513 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4514 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4515 | mp = ip->i_mount; |
| 4516 | if (unlikely(XFS_TEST_ERROR( |
| 4517 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4518 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4519 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4520 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4521 | XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); |
| 4522 | return XFS_ERROR(EFSCORRUPTED); |
| 4523 | } |
| 4524 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4525 | return XFS_ERROR(EIO); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4526 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4528 | ASSERT(ifp->if_ext_max == |
| 4529 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4530 | if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) |
| 4531 | XFS_STATS_INC(xs_blk_mapw); |
| 4532 | else |
| 4533 | XFS_STATS_INC(xs_blk_mapr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | /* |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4535 | * IGSTATE flag is used to combine extents which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | * differ only due to the state of the extents. |
| 4537 | * This technique is used from xfs_getbmap() |
| 4538 | * when the caller does not wish to see the |
| 4539 | * separation (which is the default). |
| 4540 | * |
| 4541 | * This technique is also used when writing a |
| 4542 | * buffer which has been partially written, |
| 4543 | * (usually by being flushed during a chunkread), |
| 4544 | * to ensure one write takes place. This also |
| 4545 | * prevents a change in the xfs inode extents at |
| 4546 | * this time, intentionally. This change occurs |
| 4547 | * on completion of the write operation, in |
| 4548 | * xfs_strat_comp(), where the xfs_bmapi() call |
| 4549 | * is transactioned, and the extents combined. |
| 4550 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4551 | if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */ |
| 4552 | wr = 0; /* no allocations are allowed */ |
| 4553 | ASSERT(wr || !(flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | logflags = 0; |
| 4555 | nallocs = 0; |
| 4556 | cur = NULL; |
| 4557 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4558 | ASSERT(wr && tp); |
| 4559 | if ((error = xfs_bmap_local_to_extents(tp, ip, |
| 4560 | firstblock, total, &logflags, whichfork))) |
| 4561 | goto error0; |
| 4562 | } |
| 4563 | if (wr && *firstblock == NULLFSBLOCK) { |
| 4564 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4565 | minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | else |
| 4567 | minleft = 1; |
| 4568 | } else |
| 4569 | minleft = 0; |
| 4570 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4571 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4572 | goto error0; |
| 4573 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4574 | &prev); |
| 4575 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4576 | n = 0; |
| 4577 | end = bno + len; |
| 4578 | obno = bno; |
| 4579 | bma.ip = NULL; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4580 | if (delta) { |
| 4581 | delta->xed_startoff = NULLFILEOFF; |
| 4582 | delta->xed_blockcount = 0; |
| 4583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | while (bno < end && n < *nmap) { |
| 4585 | /* |
| 4586 | * Reading past eof, act as though there's a hole |
| 4587 | * up to end. |
| 4588 | */ |
| 4589 | if (eof && !wr) |
| 4590 | got.br_startoff = end; |
| 4591 | inhole = eof || got.br_startoff > bno; |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4592 | wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4593 | isnullstartblock(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | /* |
| 4595 | * First, deal with the hole before the allocated space |
| 4596 | * that we found, if any. |
| 4597 | */ |
| 4598 | if (wr && (inhole || wasdelay)) { |
| 4599 | /* |
| 4600 | * For the wasdelay case, we could also just |
| 4601 | * allocate the stuff asked for in this bmap call |
| 4602 | * but that wouldn't be as good. |
| 4603 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4604 | if (wasdelay && !(flags & XFS_BMAPI_EXACT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | alen = (xfs_extlen_t)got.br_blockcount; |
| 4606 | aoff = got.br_startoff; |
| 4607 | if (lastx != NULLEXTNUM && lastx) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4608 | ep = xfs_iext_get_ext(ifp, lastx - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | xfs_bmbt_get_all(ep, &prev); |
| 4610 | } |
| 4611 | } else if (wasdelay) { |
| 4612 | alen = (xfs_extlen_t) |
| 4613 | XFS_FILBLKS_MIN(len, |
| 4614 | (got.br_startoff + |
| 4615 | got.br_blockcount) - bno); |
| 4616 | aoff = bno; |
| 4617 | } else { |
| 4618 | alen = (xfs_extlen_t) |
| 4619 | XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4620 | if (!eof) |
| 4621 | alen = (xfs_extlen_t) |
| 4622 | XFS_FILBLKS_MIN(alen, |
| 4623 | got.br_startoff - bno); |
| 4624 | aoff = bno; |
| 4625 | } |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4626 | minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; |
| 4627 | if (flags & XFS_BMAPI_DELAY) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4628 | xfs_extlen_t extsz; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4629 | |
| 4630 | /* Figure out the extent size, adjust alen */ |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 4631 | extsz = xfs_get_extsz_hint(ip); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4632 | if (extsz) { |
| 4633 | error = xfs_bmap_extsize_align(mp, |
| 4634 | &got, &prev, extsz, |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4635 | rt, eof, |
| 4636 | flags&XFS_BMAPI_DELAY, |
| 4637 | flags&XFS_BMAPI_CONVERT, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4638 | &aoff, &alen); |
| 4639 | ASSERT(!error); |
| 4640 | } |
| 4641 | |
| 4642 | if (rt) |
| 4643 | extsz = alen / mp->m_sb.sb_rextsize; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | /* |
| 4646 | * Make a transaction-less quota reservation for |
| 4647 | * delayed allocation blocks. This number gets |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4648 | * adjusted later. We return if we haven't |
| 4649 | * allocated blocks already inside this loop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4651 | error = xfs_trans_reserve_quota_nblks( |
| 4652 | NULL, ip, (long)alen, 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4653 | rt ? XFS_QMOPT_RES_RTBLKS : |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4654 | XFS_QMOPT_RES_REGBLKS); |
| 4655 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | if (n == 0) { |
| 4657 | *nmap = 0; |
| 4658 | ASSERT(cur == NULL); |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4659 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | } |
| 4661 | break; |
| 4662 | } |
| 4663 | |
| 4664 | /* |
| 4665 | * Split changing sb for alen and indlen since |
| 4666 | * they could be coming from different places. |
| 4667 | */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4668 | indlen = (xfs_extlen_t) |
| 4669 | xfs_bmap_worst_indlen(ip, alen); |
| 4670 | ASSERT(indlen > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4672 | if (rt) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4673 | error = xfs_mod_incore_sb(mp, |
| 4674 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4675 | -((int64_t)extsz), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4676 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4677 | } else { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4678 | error = xfs_mod_incore_sb(mp, |
| 4679 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4680 | -((int64_t)alen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4681 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4682 | } |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4683 | if (!error) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4684 | error = xfs_mod_incore_sb(mp, |
| 4685 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4686 | -((int64_t)indlen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4687 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4688 | if (error && rt) |
| 4689 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4690 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4691 | (int64_t)extsz, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4692 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4693 | else if (error) |
| 4694 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4695 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4696 | (int64_t)alen, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4697 | XFS_BMAPI_RSVBLOCKS)); |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4698 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4699 | |
| 4700 | if (error) { |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4701 | if (XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4702 | /* unreserve the blocks now */ |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4703 | (void) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4704 | xfs_trans_unreserve_quota_nblks( |
| 4705 | NULL, ip, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4706 | (long)alen, 0, rt ? |
| 4707 | XFS_QMOPT_RES_RTBLKS : |
| 4708 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | break; |
| 4710 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | ip->i_delayed_blks += alen; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4713 | abno = nullstartblock(indlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | } else { |
| 4715 | /* |
| 4716 | * If first time, allocate and fill in |
| 4717 | * once-only bma fields. |
| 4718 | */ |
| 4719 | if (bma.ip == NULL) { |
| 4720 | bma.tp = tp; |
| 4721 | bma.ip = ip; |
| 4722 | bma.prevp = &prev; |
| 4723 | bma.gotp = &got; |
| 4724 | bma.total = total; |
| 4725 | bma.userdata = 0; |
| 4726 | } |
| 4727 | /* Indicate if this is the first user data |
| 4728 | * in the file, or just any user data. |
| 4729 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4730 | if (!(flags & XFS_BMAPI_METADATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | bma.userdata = (aoff == 0) ? |
| 4732 | XFS_ALLOC_INITIAL_USER_DATA : |
| 4733 | XFS_ALLOC_USERDATA; |
| 4734 | } |
| 4735 | /* |
| 4736 | * Fill in changeable bma fields. |
| 4737 | */ |
| 4738 | bma.eof = eof; |
| 4739 | bma.firstblock = *firstblock; |
| 4740 | bma.alen = alen; |
| 4741 | bma.off = aoff; |
Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 4742 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | bma.wasdel = wasdelay; |
| 4744 | bma.minlen = minlen; |
| 4745 | bma.low = flist->xbf_low; |
| 4746 | bma.minleft = minleft; |
| 4747 | /* |
| 4748 | * Only want to do the alignment at the |
| 4749 | * eof if it is userdata and allocation length |
| 4750 | * is larger than a stripe unit. |
| 4751 | */ |
| 4752 | if (mp->m_dalign && alen >= mp->m_dalign && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4753 | (!(flags & XFS_BMAPI_METADATA)) && |
| 4754 | (whichfork == XFS_DATA_FORK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | if ((error = xfs_bmap_isaeof(ip, aoff, |
| 4756 | whichfork, &bma.aeof))) |
| 4757 | goto error0; |
| 4758 | } else |
| 4759 | bma.aeof = 0; |
| 4760 | /* |
| 4761 | * Call allocator. |
| 4762 | */ |
| 4763 | if ((error = xfs_bmap_alloc(&bma))) |
| 4764 | goto error0; |
| 4765 | /* |
| 4766 | * Copy out result fields. |
| 4767 | */ |
| 4768 | abno = bma.rval; |
| 4769 | if ((flist->xbf_low = bma.low)) |
| 4770 | minleft = 0; |
| 4771 | alen = bma.alen; |
| 4772 | aoff = bma.off; |
| 4773 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4774 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4775 | XFS_FSB_TO_AGNO(mp, bma.firstblock) || |
| 4776 | (flist->xbf_low && |
| 4777 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4778 | XFS_FSB_TO_AGNO(mp, bma.firstblock))); |
| 4779 | *firstblock = bma.firstblock; |
| 4780 | if (cur) |
| 4781 | cur->bc_private.b.firstblock = |
| 4782 | *firstblock; |
| 4783 | if (abno == NULLFSBLOCK) |
| 4784 | break; |
| 4785 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4786 | cur = xfs_bmbt_init_cursor(mp, tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | ip, whichfork); |
| 4788 | cur->bc_private.b.firstblock = |
| 4789 | *firstblock; |
| 4790 | cur->bc_private.b.flist = flist; |
| 4791 | } |
| 4792 | /* |
| 4793 | * Bump the number of extents we've allocated |
| 4794 | * in this call. |
| 4795 | */ |
| 4796 | nallocs++; |
| 4797 | } |
| 4798 | if (cur) |
| 4799 | cur->bc_private.b.flags = |
| 4800 | wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4801 | got.br_startoff = aoff; |
| 4802 | got.br_startblock = abno; |
| 4803 | got.br_blockcount = alen; |
| 4804 | got.br_state = XFS_EXT_NORM; /* assume normal */ |
| 4805 | /* |
| 4806 | * Determine state of extent, and the filesystem. |
| 4807 | * A wasdelay extent has been initialized, so |
| 4808 | * shouldn't be flagged as unwritten. |
| 4809 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4810 | if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4811 | if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) |
| 4812 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4813 | } |
| 4814 | error = xfs_bmap_add_extent(ip, lastx, &cur, &got, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4815 | firstblock, flist, &tmp_logflags, delta, |
| 4816 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | logflags |= tmp_logflags; |
| 4818 | if (error) |
| 4819 | goto error0; |
| 4820 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4821 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4823 | xfs_bmbt_get_all(ep, &got); |
| 4824 | ASSERT(got.br_startoff <= aoff); |
| 4825 | ASSERT(got.br_startoff + got.br_blockcount >= |
| 4826 | aoff + alen); |
| 4827 | #ifdef DEBUG |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4828 | if (flags & XFS_BMAPI_DELAY) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4829 | ASSERT(isnullstartblock(got.br_startblock)); |
| 4830 | ASSERT(startblockval(got.br_startblock) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | } |
| 4832 | ASSERT(got.br_state == XFS_EXT_NORM || |
| 4833 | got.br_state == XFS_EXT_UNWRITTEN); |
| 4834 | #endif |
| 4835 | /* |
| 4836 | * Fall down into the found allocated space case. |
| 4837 | */ |
| 4838 | } else if (inhole) { |
| 4839 | /* |
| 4840 | * Reading in a hole. |
| 4841 | */ |
| 4842 | mval->br_startoff = bno; |
| 4843 | mval->br_startblock = HOLESTARTBLOCK; |
| 4844 | mval->br_blockcount = |
| 4845 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4846 | mval->br_state = XFS_EXT_NORM; |
| 4847 | bno += mval->br_blockcount; |
| 4848 | len -= mval->br_blockcount; |
| 4849 | mval++; |
| 4850 | n++; |
| 4851 | continue; |
| 4852 | } |
| 4853 | /* |
| 4854 | * Then deal with the allocated space we found. |
| 4855 | */ |
| 4856 | ASSERT(ep != NULL); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4857 | if (!(flags & XFS_BMAPI_ENTIRE) && |
| 4858 | (got.br_startoff + got.br_blockcount > obno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | if (obno > bno) |
| 4860 | bno = obno; |
| 4861 | ASSERT((bno >= obno) || (n == 0)); |
| 4862 | ASSERT(bno < end); |
| 4863 | mval->br_startoff = bno; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4864 | if (isnullstartblock(got.br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4865 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4867 | } else |
| 4868 | mval->br_startblock = |
| 4869 | got.br_startblock + |
| 4870 | (bno - got.br_startoff); |
| 4871 | /* |
| 4872 | * Return the minimum of what we got and what we |
| 4873 | * asked for for the length. We can use the len |
| 4874 | * variable here because it is modified below |
| 4875 | * and we could have been there before coming |
| 4876 | * here if the first part of the allocation |
| 4877 | * didn't overlap what was asked for. |
| 4878 | */ |
| 4879 | mval->br_blockcount = |
| 4880 | XFS_FILBLKS_MIN(end - bno, got.br_blockcount - |
| 4881 | (bno - got.br_startoff)); |
| 4882 | mval->br_state = got.br_state; |
| 4883 | ASSERT(mval->br_blockcount <= len); |
| 4884 | } else { |
| 4885 | *mval = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4886 | if (isnullstartblock(mval->br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4887 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | /* |
| 4893 | * Check if writing previously allocated but |
| 4894 | * unwritten extents. |
| 4895 | */ |
| 4896 | if (wr && mval->br_state == XFS_EXT_UNWRITTEN && |
| 4897 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) { |
| 4898 | /* |
| 4899 | * Modify (by adding) the state flag, if writing. |
| 4900 | */ |
| 4901 | ASSERT(mval->br_blockcount <= len); |
| 4902 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4903 | cur = xfs_bmbt_init_cursor(mp, |
| 4904 | tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | cur->bc_private.b.firstblock = |
| 4906 | *firstblock; |
| 4907 | cur->bc_private.b.flist = flist; |
| 4908 | } |
| 4909 | mval->br_state = XFS_EXT_NORM; |
| 4910 | error = xfs_bmap_add_extent(ip, lastx, &cur, mval, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4911 | firstblock, flist, &tmp_logflags, delta, |
| 4912 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4913 | logflags |= tmp_logflags; |
| 4914 | if (error) |
| 4915 | goto error0; |
| 4916 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4917 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4919 | xfs_bmbt_get_all(ep, &got); |
| 4920 | /* |
| 4921 | * We may have combined previously unwritten |
| 4922 | * space with written space, so generate |
| 4923 | * another request. |
| 4924 | */ |
| 4925 | if (mval->br_blockcount < len) |
| 4926 | continue; |
| 4927 | } |
| 4928 | |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4929 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4931 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4932 | (mval->br_blockcount <= len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | (mval->br_startoff < obno)); |
| 4934 | bno = mval->br_startoff + mval->br_blockcount; |
| 4935 | len = end - bno; |
| 4936 | if (n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4937 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4938 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4939 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4940 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4941 | mval[-1].br_state = mval->br_state; |
| 4942 | } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4943 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4944 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4945 | mval->br_startblock == |
| 4946 | mval[-1].br_startblock + mval[-1].br_blockcount && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4947 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4948 | mval[-1].br_state == mval->br_state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | ASSERT(mval->br_startoff == |
| 4950 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4951 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4952 | } else if (n > 0 && |
| 4953 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4954 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4955 | mval->br_startoff == |
| 4956 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4957 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4958 | mval[-1].br_state = mval->br_state; |
| 4959 | } else if (!((n == 0) && |
| 4960 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4961 | obno))) { |
| 4962 | mval++; |
| 4963 | n++; |
| 4964 | } |
| 4965 | /* |
| 4966 | * If we're done, stop now. Stop when we've allocated |
| 4967 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4968 | * the transaction may get too big. |
| 4969 | */ |
| 4970 | if (bno >= end || n >= *nmap || nallocs >= *nmap) |
| 4971 | break; |
| 4972 | /* |
| 4973 | * Else go on to the next record. |
| 4974 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4975 | ep = xfs_iext_get_ext(ifp, ++lastx); |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4976 | prev = got; |
| 4977 | if (lastx >= nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | eof = 1; |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4979 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | xfs_bmbt_get_all(ep, &got); |
| 4981 | } |
| 4982 | ifp->if_lastex = lastx; |
| 4983 | *nmap = n; |
| 4984 | /* |
| 4985 | * Transform from btree to extents, give it cur. |
| 4986 | */ |
| 4987 | if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 4988 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 4989 | ASSERT(wr && cur); |
| 4990 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4991 | &tmp_logflags, whichfork); |
| 4992 | logflags |= tmp_logflags; |
| 4993 | if (error) |
| 4994 | goto error0; |
| 4995 | } |
| 4996 | ASSERT(ifp->if_ext_max == |
| 4997 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4998 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 4999 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 5000 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5001 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5002 | /* A change was actually made. |
| 5003 | * Note that delta->xed_blockount is an offset at this |
| 5004 | * point and needs to be converted to a block count. |
| 5005 | */ |
| 5006 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5007 | delta->xed_blockcount -= delta->xed_startoff; |
| 5008 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | error0: |
| 5010 | /* |
| 5011 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5012 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5014 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5016 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5017 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5019 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | /* |
| 5021 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5022 | * detecting a case where the data is changed, there's an error, |
| 5023 | * and it's not logged so we don't shutdown when we should. |
| 5024 | */ |
| 5025 | if (logflags) { |
| 5026 | ASSERT(tp && wr); |
| 5027 | xfs_trans_log_inode(tp, ip, logflags); |
| 5028 | } |
| 5029 | if (cur) { |
| 5030 | if (!error) { |
| 5031 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5032 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5033 | XFS_FSB_TO_AGNO(mp, |
| 5034 | cur->bc_private.b.firstblock) || |
| 5035 | (flist->xbf_low && |
| 5036 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5037 | XFS_FSB_TO_AGNO(mp, |
| 5038 | cur->bc_private.b.firstblock))); |
| 5039 | *firstblock = cur->bc_private.b.firstblock; |
| 5040 | } |
| 5041 | xfs_btree_del_cursor(cur, |
| 5042 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5043 | } |
| 5044 | if (!error) |
| 5045 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5046 | orig_nmap, *nmap); |
| 5047 | return error; |
| 5048 | } |
| 5049 | |
| 5050 | /* |
| 5051 | * Map file blocks to filesystem blocks, simple version. |
| 5052 | * One block (extent) only, read-only. |
| 5053 | * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. |
| 5054 | * For the other flag values, the effect is as if XFS_BMAPI_METADATA |
| 5055 | * was set and all the others were clear. |
| 5056 | */ |
| 5057 | int /* error */ |
| 5058 | xfs_bmapi_single( |
| 5059 | xfs_trans_t *tp, /* transaction pointer */ |
| 5060 | xfs_inode_t *ip, /* incore inode */ |
| 5061 | int whichfork, /* data or attr fork */ |
| 5062 | xfs_fsblock_t *fsb, /* output: mapped block */ |
| 5063 | xfs_fileoff_t bno) /* starting file offs. mapped */ |
| 5064 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5065 | int eof; /* we've hit the end of extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5067 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5069 | xfs_extnum_t lastx; /* last useful extent number */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5070 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | |
| 5072 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5073 | if (unlikely( |
| 5074 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 5075 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) { |
| 5076 | XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW, |
| 5077 | ip->i_mount); |
| 5078 | return XFS_ERROR(EFSCORRUPTED); |
| 5079 | } |
| 5080 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5081 | return XFS_ERROR(EIO); |
| 5082 | XFS_STATS_INC(xs_blk_mapr); |
| 5083 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5084 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5085 | return error; |
| 5086 | (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5087 | &prev); |
| 5088 | /* |
| 5089 | * Reading past eof, act as though there's a hole |
| 5090 | * up to end. |
| 5091 | */ |
| 5092 | if (eof || got.br_startoff > bno) { |
| 5093 | *fsb = NULLFSBLOCK; |
| 5094 | return 0; |
| 5095 | } |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5096 | ASSERT(!isnullstartblock(got.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5097 | ASSERT(bno < got.br_startoff + got.br_blockcount); |
| 5098 | *fsb = got.br_startblock + (bno - got.br_startoff); |
| 5099 | ifp->if_lastex = lastx; |
| 5100 | return 0; |
| 5101 | } |
| 5102 | |
| 5103 | /* |
| 5104 | * Unmap (remove) blocks from a file. |
| 5105 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5106 | * that value. If not all extents in the block range can be removed then |
| 5107 | * *done is set. |
| 5108 | */ |
| 5109 | int /* error */ |
| 5110 | xfs_bunmapi( |
| 5111 | xfs_trans_t *tp, /* transaction pointer */ |
| 5112 | struct xfs_inode *ip, /* incore inode */ |
| 5113 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5114 | xfs_filblks_t len, /* length to unmap in file */ |
| 5115 | int flags, /* misc flags */ |
| 5116 | xfs_extnum_t nexts, /* number of extents max */ |
| 5117 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5118 | controls a.g. for allocs */ |
| 5119 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5120 | xfs_extdelta_t *delta, /* o: change made to incore |
| 5121 | extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | int *done) /* set if not done yet */ |
| 5123 | { |
| 5124 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5125 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5126 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5127 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | int error; /* error return value */ |
| 5129 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5130 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5132 | int isrt; /* freeing in rt area */ |
| 5133 | xfs_extnum_t lastx; /* last extent index used */ |
| 5134 | int logflags; /* transaction logging flags */ |
| 5135 | xfs_extlen_t mod; /* rt extent offset */ |
| 5136 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5137 | xfs_extnum_t nextents; /* number of file extents */ |
| 5138 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5139 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5140 | int tmp_logflags; /* partial logging flags */ |
| 5141 | int wasdel; /* was a delayed alloc extent */ |
| 5142 | int whichfork; /* data or attribute fork */ |
| 5143 | int rsvd; /* OK to allocate reserved blocks */ |
| 5144 | xfs_fsblock_t sum; |
| 5145 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5146 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5149 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5150 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5151 | if (unlikely( |
| 5152 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5153 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5154 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5155 | ip->i_mount); |
| 5156 | return XFS_ERROR(EFSCORRUPTED); |
| 5157 | } |
| 5158 | mp = ip->i_mount; |
| 5159 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5160 | return XFS_ERROR(EIO); |
| 5161 | rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0; |
| 5162 | ASSERT(len > 0); |
| 5163 | ASSERT(nexts >= 0); |
| 5164 | ASSERT(ifp->if_ext_max == |
| 5165 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5166 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5167 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5168 | return error; |
| 5169 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5170 | if (nextents == 0) { |
| 5171 | *done = 1; |
| 5172 | return 0; |
| 5173 | } |
| 5174 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5175 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | start = bno; |
| 5177 | bno = start + len - 1; |
| 5178 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5179 | &prev); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5180 | if (delta) { |
| 5181 | delta->xed_startoff = NULLFILEOFF; |
| 5182 | delta->xed_blockcount = 0; |
| 5183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | /* |
| 5185 | * Check to see if the given block number is past the end of the |
| 5186 | * file, back up to the last block if so... |
| 5187 | */ |
| 5188 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5189 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5190 | xfs_bmbt_get_all(ep, &got); |
| 5191 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5192 | } |
| 5193 | logflags = 0; |
| 5194 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5195 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5196 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | cur->bc_private.b.firstblock = *firstblock; |
| 5198 | cur->bc_private.b.flist = flist; |
| 5199 | cur->bc_private.b.flags = 0; |
| 5200 | } else |
| 5201 | cur = NULL; |
| 5202 | extno = 0; |
| 5203 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5204 | (nexts == 0 || extno < nexts)) { |
| 5205 | /* |
| 5206 | * Is the found extent after a hole in which bno lives? |
| 5207 | * Just back up to the previous extent, if so. |
| 5208 | */ |
| 5209 | if (got.br_startoff > bno) { |
| 5210 | if (--lastx < 0) |
| 5211 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5212 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | xfs_bmbt_get_all(ep, &got); |
| 5214 | } |
| 5215 | /* |
| 5216 | * Is the last block of this extent before the range |
| 5217 | * we're supposed to delete? If so, we're done. |
| 5218 | */ |
| 5219 | bno = XFS_FILEOFF_MIN(bno, |
| 5220 | got.br_startoff + got.br_blockcount - 1); |
| 5221 | if (bno < start) |
| 5222 | break; |
| 5223 | /* |
| 5224 | * Then deal with the (possibly delayed) allocated space |
| 5225 | * we found. |
| 5226 | */ |
| 5227 | ASSERT(ep != NULL); |
| 5228 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5229 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (got.br_startoff < start) { |
| 5231 | del.br_startoff = start; |
| 5232 | del.br_blockcount -= start - got.br_startoff; |
| 5233 | if (!wasdel) |
| 5234 | del.br_startblock += start - got.br_startoff; |
| 5235 | } |
| 5236 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5237 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5238 | sum = del.br_startblock + del.br_blockcount; |
| 5239 | if (isrt && |
| 5240 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5241 | /* |
| 5242 | * Realtime extent not lined up at the end. |
| 5243 | * The extent could have been split into written |
| 5244 | * and unwritten pieces, or we could just be |
| 5245 | * unmapping part of it. But we can't really |
| 5246 | * get rid of part of a realtime extent. |
| 5247 | */ |
| 5248 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5249 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | /* |
| 5251 | * This piece is unwritten, or we're not |
| 5252 | * using unwritten extents. Skip over it. |
| 5253 | */ |
| 5254 | ASSERT(bno >= mod); |
| 5255 | bno -= mod > del.br_blockcount ? |
| 5256 | del.br_blockcount : mod; |
| 5257 | if (bno < got.br_startoff) { |
| 5258 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5259 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5260 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | } |
| 5262 | continue; |
| 5263 | } |
| 5264 | /* |
| 5265 | * It's written, turn it unwritten. |
| 5266 | * This is better than zeroing it. |
| 5267 | */ |
| 5268 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5269 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5270 | /* |
| 5271 | * If this spans a realtime extent boundary, |
| 5272 | * chop it back to the start of the one we end at. |
| 5273 | */ |
| 5274 | if (del.br_blockcount > mod) { |
| 5275 | del.br_startoff += del.br_blockcount - mod; |
| 5276 | del.br_startblock += del.br_blockcount - mod; |
| 5277 | del.br_blockcount = mod; |
| 5278 | } |
| 5279 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5280 | error = xfs_bmap_add_extent(ip, lastx, &cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5281 | firstblock, flist, &logflags, delta, |
| 5282 | XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5283 | if (error) |
| 5284 | goto error0; |
| 5285 | goto nodelete; |
| 5286 | } |
| 5287 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5288 | /* |
| 5289 | * Realtime extent is lined up at the end but not |
| 5290 | * at the front. We'll get rid of full extents if |
| 5291 | * we can. |
| 5292 | */ |
| 5293 | mod = mp->m_sb.sb_rextsize - mod; |
| 5294 | if (del.br_blockcount > mod) { |
| 5295 | del.br_blockcount -= mod; |
| 5296 | del.br_startoff += mod; |
| 5297 | del.br_startblock += mod; |
| 5298 | } else if ((del.br_startoff == start && |
| 5299 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5300 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5301 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | /* |
| 5303 | * Can't make it unwritten. There isn't |
| 5304 | * a full extent here so just skip it. |
| 5305 | */ |
| 5306 | ASSERT(bno >= del.br_blockcount); |
| 5307 | bno -= del.br_blockcount; |
| 5308 | if (bno < got.br_startoff) { |
| 5309 | if (--lastx >= 0) |
| 5310 | xfs_bmbt_get_all(--ep, &got); |
| 5311 | } |
| 5312 | continue; |
| 5313 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5314 | /* |
| 5315 | * This one is already unwritten. |
| 5316 | * It must have a written left neighbor. |
| 5317 | * Unwrite the killed part of that one and |
| 5318 | * try again. |
| 5319 | */ |
| 5320 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5321 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5322 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5324 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | ASSERT(del.br_startblock == |
| 5326 | prev.br_startblock + prev.br_blockcount); |
| 5327 | if (prev.br_startoff < start) { |
| 5328 | mod = start - prev.br_startoff; |
| 5329 | prev.br_blockcount -= mod; |
| 5330 | prev.br_startblock += mod; |
| 5331 | prev.br_startoff = start; |
| 5332 | } |
| 5333 | prev.br_state = XFS_EXT_UNWRITTEN; |
| 5334 | error = xfs_bmap_add_extent(ip, lastx - 1, &cur, |
| 5335 | &prev, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5336 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | if (error) |
| 5338 | goto error0; |
| 5339 | goto nodelete; |
| 5340 | } else { |
| 5341 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5342 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5343 | error = xfs_bmap_add_extent(ip, lastx, &cur, |
| 5344 | &del, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5345 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | if (error) |
| 5347 | goto error0; |
| 5348 | goto nodelete; |
| 5349 | } |
| 5350 | } |
| 5351 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5352 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5353 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5354 | if (isrt) { |
| 5355 | xfs_filblks_t rtexts; |
| 5356 | |
| 5357 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5358 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5359 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5360 | (int64_t)rtexts, rsvd); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5361 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5362 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5363 | XFS_QMOPT_RES_RTBLKS); |
| 5364 | } else { |
| 5365 | xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5366 | (int64_t)del.br_blockcount, rsvd); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5367 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5368 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5369 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | ip->i_delayed_blks -= del.br_blockcount; |
| 5372 | if (cur) |
| 5373 | cur->bc_private.b.flags |= |
| 5374 | XFS_BTCUR_BPRV_WASDEL; |
| 5375 | } else if (cur) |
| 5376 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5377 | /* |
| 5378 | * If it's the case where the directory code is running |
| 5379 | * with no block reservation, and the deleted block is in |
| 5380 | * the middle of its extent, and the resulting insert |
| 5381 | * of an extent would cause transformation to btree format, |
| 5382 | * then reject it. The calling code will then swap |
| 5383 | * blocks around instead. |
| 5384 | * We have to do this now, rather than waiting for the |
| 5385 | * conversion to btree format, since the transaction |
| 5386 | * will be dirty. |
| 5387 | */ |
| 5388 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5389 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5390 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5391 | del.br_startoff > got.br_startoff && |
| 5392 | del.br_startoff + del.br_blockcount < |
| 5393 | got.br_startoff + got.br_blockcount) { |
| 5394 | error = XFS_ERROR(ENOSPC); |
| 5395 | goto error0; |
| 5396 | } |
| 5397 | error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5398 | &tmp_logflags, delta, whichfork, rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | logflags |= tmp_logflags; |
| 5400 | if (error) |
| 5401 | goto error0; |
| 5402 | bno = del.br_startoff - 1; |
| 5403 | nodelete: |
| 5404 | lastx = ifp->if_lastex; |
| 5405 | /* |
| 5406 | * If not done go on to the next (previous) record. |
| 5407 | * Reset ep in case the extents array was re-alloced. |
| 5408 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5409 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
| 5411 | if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) || |
| 5412 | xfs_bmbt_get_startoff(ep) > bno) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5413 | if (--lastx >= 0) |
| 5414 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | } |
| 5416 | if (lastx >= 0) |
| 5417 | xfs_bmbt_get_all(ep, &got); |
| 5418 | extno++; |
| 5419 | } |
| 5420 | } |
| 5421 | ifp->if_lastex = lastx; |
| 5422 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5423 | ASSERT(ifp->if_ext_max == |
| 5424 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5425 | /* |
| 5426 | * Convert to a btree if necessary. |
| 5427 | */ |
| 5428 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5429 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5430 | ASSERT(cur == NULL); |
| 5431 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5432 | &cur, 0, &tmp_logflags, whichfork); |
| 5433 | logflags |= tmp_logflags; |
| 5434 | if (error) |
| 5435 | goto error0; |
| 5436 | } |
| 5437 | /* |
| 5438 | * transform from btree to extents, give it cur |
| 5439 | */ |
| 5440 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5441 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5442 | ASSERT(cur != NULL); |
| 5443 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5444 | whichfork); |
| 5445 | logflags |= tmp_logflags; |
| 5446 | if (error) |
| 5447 | goto error0; |
| 5448 | } |
| 5449 | /* |
| 5450 | * transform from extents to local? |
| 5451 | */ |
| 5452 | ASSERT(ifp->if_ext_max == |
| 5453 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5454 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5455 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5456 | /* A change was actually made. |
| 5457 | * Note that delta->xed_blockount is an offset at this |
| 5458 | * point and needs to be converted to a block count. |
| 5459 | */ |
| 5460 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5461 | delta->xed_blockcount -= delta->xed_startoff; |
| 5462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | error0: |
| 5464 | /* |
| 5465 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5466 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5468 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5470 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5471 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5473 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | /* |
| 5475 | * Log inode even in the error case, if the transaction |
| 5476 | * is dirty we'll need to shut down the filesystem. |
| 5477 | */ |
| 5478 | if (logflags) |
| 5479 | xfs_trans_log_inode(tp, ip, logflags); |
| 5480 | if (cur) { |
| 5481 | if (!error) { |
| 5482 | *firstblock = cur->bc_private.b.firstblock; |
| 5483 | cur->bc_private.b.allocated = 0; |
| 5484 | } |
| 5485 | xfs_btree_del_cursor(cur, |
| 5486 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5487 | } |
| 5488 | return error; |
| 5489 | } |
| 5490 | |
| 5491 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5492 | * returns 1 for success, 0 if we failed to map the extent. |
| 5493 | */ |
| 5494 | STATIC int |
| 5495 | xfs_getbmapx_fix_eof_hole( |
| 5496 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5497 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5498 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5499 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5500 | __int64_t end, /* last block requested */ |
| 5501 | xfs_fsblock_t startblock) |
| 5502 | { |
| 5503 | __int64_t fixlen; |
| 5504 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5505 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5506 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5507 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5508 | |
| 5509 | if (startblock == HOLESTARTBLOCK) { |
| 5510 | mp = ip->i_mount; |
| 5511 | out->bmv_block = -1; |
| 5512 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5513 | fixlen -= out->bmv_offset; |
| 5514 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5515 | /* Came to hole at EOF. Trim it. */ |
| 5516 | if (fixlen <= 0) |
| 5517 | return 0; |
| 5518 | out->bmv_length = fixlen; |
| 5519 | } |
| 5520 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5521 | if (startblock == DELAYSTARTBLOCK) |
| 5522 | out->bmv_block = -2; |
| 5523 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5524 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5525 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5526 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5527 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5528 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5529 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | return 1; |
| 5533 | } |
| 5534 | |
| 5535 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5536 | * Get inode's extents as described in bmv, and format for output. |
| 5537 | * Calls formatter to fill the user's buffer until all extents |
| 5538 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5539 | * been filled, or until the formatter short-circuits the loop, |
| 5540 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | */ |
| 5542 | int /* error code */ |
| 5543 | xfs_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5544 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5545 | struct getbmapx *bmv, /* user bmap structure */ |
| 5546 | xfs_bmap_format_t formatter, /* format to user */ |
| 5547 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | { |
| 5549 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5550 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | __int64_t fixlen; /* length for -1 case */ |
| 5552 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | int lock; /* lock state */ |
| 5554 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5555 | xfs_mount_t *mp; /* file system mount point */ |
| 5556 | int nex; /* # of user extents can do */ |
| 5557 | int nexleft; /* # of user extents left */ |
| 5558 | int subnex; /* # of bmapi's can do */ |
| 5559 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5560 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | int whichfork; /* data or attr fork */ |
| 5562 | int prealloced; /* this is a file with |
| 5563 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5564 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5566 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5569 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5570 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | if (whichfork == XFS_ATTR_FORK) { |
| 5573 | if (XFS_IFORK_Q(ip)) { |
| 5574 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5575 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5576 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5577 | return XFS_ERROR(EINVAL); |
| 5578 | } else if (unlikely( |
| 5579 | ip->i_d.di_aformat != 0 && |
| 5580 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5581 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5582 | ip->i_mount); |
| 5583 | return XFS_ERROR(EFSCORRUPTED); |
| 5584 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5585 | |
| 5586 | prealloced = 0; |
| 5587 | fixlen = 1LL << 32; |
| 5588 | } else { |
| 5589 | /* |
| 5590 | * If the BMV_IF_NO_DMAPI_READ interface bit specified, do |
| 5591 | * not generate a DMAPI read event. Otherwise, if the |
| 5592 | * DM_EVENT_READ bit is set for the file, generate a read |
| 5593 | * event in order that the DMAPI application may do its thing |
| 5594 | * before we return the extents. Usually this means restoring |
| 5595 | * user file data to regions of the file that look like holes. |
| 5596 | * |
| 5597 | * The "old behavior" (from XFS_IOC_GETBMAP) is to not specify |
| 5598 | * BMV_IF_NO_DMAPI_READ so that read events are generated. |
| 5599 | * If this were not true, callers of ioctl(XFS_IOC_GETBMAP) |
| 5600 | * could misinterpret holes in a DMAPI file as true holes, |
| 5601 | * when in fact they may represent offline user data. |
| 5602 | */ |
| 5603 | if (DM_EVENT_ENABLED(ip, DM_EVENT_READ) && |
| 5604 | !(iflags & BMV_IF_NO_DMAPI_READ)) { |
| 5605 | error = XFS_SEND_DATA(mp, DM_EVENT_READ, ip, |
| 5606 | 0, 0, 0, NULL); |
| 5607 | if (error) |
| 5608 | return XFS_ERROR(error); |
| 5609 | } |
| 5610 | |
| 5611 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5612 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5613 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5614 | return XFS_ERROR(EINVAL); |
| 5615 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5616 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5617 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | prealloced = 1; |
| 5619 | fixlen = XFS_MAXIOFFSET(mp); |
| 5620 | } else { |
| 5621 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5622 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | if (bmv->bmv_length == -1) { |
| 5627 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5628 | bmv->bmv_length = |
| 5629 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5630 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | bmv->bmv_entries = 0; |
| 5632 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5633 | } else if (bmv->bmv_length < 0) { |
| 5634 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | nex = bmv->bmv_count - 1; |
| 5638 | if (nex <= 0) |
| 5639 | return XFS_ERROR(EINVAL); |
| 5640 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5641 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5642 | |
| 5643 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5644 | return XFS_ERROR(ENOMEM); |
| 5645 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 5646 | if (!out) |
| 5647 | return XFS_ERROR(ENOMEM); |
| 5648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5650 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 5651 | if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { |
| 5652 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5653 | if (error) |
| 5654 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5657 | ASSERT(ip->i_delayed_blks == 0); |
| 5658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | |
| 5660 | lock = xfs_ilock_map_shared(ip); |
| 5661 | |
| 5662 | /* |
| 5663 | * Don't let nex be bigger than the number of extents |
| 5664 | * we can have assuming alternating holes and real extents. |
| 5665 | */ |
| 5666 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5667 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5668 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5669 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5670 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5671 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | |
| 5673 | /* |
| 5674 | * Allocate enough space to handle "subnex" maps at a time. |
| 5675 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5676 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5678 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5679 | if (!map) |
| 5680 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | |
| 5682 | bmv->bmv_entries = 0; |
| 5683 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5684 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5685 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5686 | error = 0; |
| 5687 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | nexleft = nex; |
| 5691 | |
| 5692 | do { |
| 5693 | nmap = (nexleft > subnex) ? subnex : nexleft; |
| 5694 | error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5695 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5696 | bmapi_flags, NULL, 0, map, &nmap, |
| 5697 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5699 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | ASSERT(nmap <= subnex); |
| 5701 | |
| 5702 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5703 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5704 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5705 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5706 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5707 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5708 | out[cur_ext].bmv_offset = |
| 5709 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5710 | out[cur_ext].bmv_length = |
| 5711 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5712 | out[cur_ext].bmv_unused1 = 0; |
| 5713 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5714 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5715 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5716 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5717 | whichfork == XFS_ATTR_FORK) { |
| 5718 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5719 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5720 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5722 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5723 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5724 | prealloced, bmvend, |
| 5725 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5726 | goto out_free_map; |
| 5727 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5728 | nexleft--; |
| 5729 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5730 | out[cur_ext].bmv_offset + |
| 5731 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5732 | bmv->bmv_length = |
| 5733 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
| 5734 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5735 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | } |
| 5737 | } while (nmap && nexleft && bmv->bmv_length); |
| 5738 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5739 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5740 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5741 | out_unlock_ilock: |
| 5742 | xfs_iunlock_map_shared(ip, lock); |
| 5743 | out_unlock_iolock: |
| 5744 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5745 | |
| 5746 | for (i = 0; i < cur_ext; i++) { |
| 5747 | int full = 0; /* user array is full */ |
| 5748 | |
| 5749 | /* format results & advance arg */ |
| 5750 | error = formatter(&arg, &out[i], &full); |
| 5751 | if (error || full) |
| 5752 | break; |
| 5753 | } |
| 5754 | |
Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5755 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | return error; |
| 5757 | } |
| 5758 | |
| 5759 | /* |
| 5760 | * Check the last inode extent to determine whether this allocation will result |
| 5761 | * in blocks being allocated at the end of the file. When we allocate new data |
| 5762 | * blocks at the end of the file which do not start at the previous data block, |
| 5763 | * we will try to align the new blocks at stripe unit boundaries. |
| 5764 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 5765 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | xfs_bmap_isaeof( |
| 5767 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5768 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 5769 | int whichfork, /* data or attribute fork */ |
| 5770 | char *aeof) /* return value */ |
| 5771 | { |
| 5772 | int error; /* error return value */ |
| 5773 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5774 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5775 | xfs_extnum_t nextents; /* number of file extents */ |
| 5776 | xfs_bmbt_irec_t s; /* expanded extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | |
| 5778 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5779 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5780 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5781 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5782 | return error; |
| 5783 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5784 | if (nextents == 0) { |
| 5785 | *aeof = 1; |
| 5786 | return 0; |
| 5787 | } |
| 5788 | /* |
| 5789 | * Go to the last extent |
| 5790 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5791 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | xfs_bmbt_get_all(lastrec, &s); |
| 5793 | /* |
| 5794 | * Check we are allocating in the last extent (for delayed allocations) |
| 5795 | * or past the last extent for non-delayed allocations. |
| 5796 | */ |
| 5797 | *aeof = (off >= s.br_startoff && |
| 5798 | off < s.br_startoff + s.br_blockcount && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5799 | isnullstartblock(s.br_startblock)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | off >= s.br_startoff + s.br_blockcount; |
| 5801 | return 0; |
| 5802 | } |
| 5803 | |
| 5804 | /* |
| 5805 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 5806 | * the allocation to a stripe unit boundary. |
| 5807 | */ |
| 5808 | int /* error */ |
| 5809 | xfs_bmap_eof( |
| 5810 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5811 | xfs_fileoff_t endoff, /* file offset in fsblocks */ |
| 5812 | int whichfork, /* data or attribute fork */ |
| 5813 | int *eof) /* result value */ |
| 5814 | { |
| 5815 | xfs_fsblock_t blockcount; /* extent block count */ |
| 5816 | int error; /* error return value */ |
| 5817 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5818 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5819 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | xfs_fileoff_t startoff; /* extent starting file offset */ |
| 5821 | |
| 5822 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5823 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5824 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5825 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5826 | return error; |
| 5827 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5828 | if (nextents == 0) { |
| 5829 | *eof = 1; |
| 5830 | return 0; |
| 5831 | } |
| 5832 | /* |
| 5833 | * Go to the last extent |
| 5834 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5835 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | startoff = xfs_bmbt_get_startoff(lastrec); |
| 5837 | blockcount = xfs_bmbt_get_blockcount(lastrec); |
| 5838 | *eof = endoff >= startoff + blockcount; |
| 5839 | return 0; |
| 5840 | } |
| 5841 | |
| 5842 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | STATIC |
| 5844 | xfs_buf_t * |
| 5845 | xfs_bmap_get_bp( |
| 5846 | xfs_btree_cur_t *cur, |
| 5847 | xfs_fsblock_t bno) |
| 5848 | { |
| 5849 | int i; |
| 5850 | xfs_buf_t *bp; |
| 5851 | |
| 5852 | if (!cur) |
| 5853 | return(NULL); |
| 5854 | |
| 5855 | bp = NULL; |
| 5856 | for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5857 | bp = cur->bc_bufs[i]; |
| 5858 | if (!bp) break; |
| 5859 | if (XFS_BUF_ADDR(bp) == bno) |
| 5860 | break; /* Found it */ |
| 5861 | } |
| 5862 | if (i == XFS_BTREE_MAXLEVELS) |
| 5863 | bp = NULL; |
| 5864 | |
| 5865 | if (!bp) { /* Chase down all the log items to see if the bp is there */ |
| 5866 | xfs_log_item_chunk_t *licp; |
| 5867 | xfs_trans_t *tp; |
| 5868 | |
| 5869 | tp = cur->bc_tp; |
| 5870 | licp = &tp->t_items; |
| 5871 | while (!bp && licp != NULL) { |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 5872 | if (xfs_lic_are_all_free(licp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | licp = licp->lic_next; |
| 5874 | continue; |
| 5875 | } |
| 5876 | for (i = 0; i < licp->lic_unused; i++) { |
| 5877 | xfs_log_item_desc_t *lidp; |
| 5878 | xfs_log_item_t *lip; |
| 5879 | xfs_buf_log_item_t *bip; |
| 5880 | xfs_buf_t *lbp; |
| 5881 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 5882 | if (xfs_lic_isfree(licp, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | continue; |
| 5884 | } |
| 5885 | |
Eric Sandeen | 39dab9d | 2008-08-13 16:10:52 +1000 | [diff] [blame] | 5886 | lidp = xfs_lic_slot(licp, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | lip = lidp->lid_item; |
| 5888 | if (lip->li_type != XFS_LI_BUF) |
| 5889 | continue; |
| 5890 | |
| 5891 | bip = (xfs_buf_log_item_t *)lip; |
| 5892 | lbp = bip->bli_buf; |
| 5893 | |
| 5894 | if (XFS_BUF_ADDR(lbp) == bno) { |
| 5895 | bp = lbp; |
| 5896 | break; /* Found it */ |
| 5897 | } |
| 5898 | } |
| 5899 | licp = licp->lic_next; |
| 5900 | } |
| 5901 | } |
| 5902 | return(bp); |
| 5903 | } |
| 5904 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5905 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5907 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | xfs_mount_t *mp, |
| 5909 | int root, |
| 5910 | short sz) |
| 5911 | { |
| 5912 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5913 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | xfs_bmbt_key_t *prevp, *keyp; |
| 5915 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5916 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | |
| 5918 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5919 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5921 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | |
| 5923 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5924 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5925 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | } |
| 5927 | prevp = keyp; |
| 5928 | |
| 5929 | /* |
| 5930 | * Compare the block numbers to see if there are dups. |
| 5931 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5932 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5933 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5934 | else |
| 5935 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5936 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5937 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5938 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5939 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5940 | else |
| 5941 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5942 | if (*thispa == *pp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5944 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5945 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5947 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | } |
| 5949 | } |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | /* |
| 5954 | * Check that the extents for the inode ip are in the right order in all |
| 5955 | * btree leaves. |
| 5956 | */ |
| 5957 | |
| 5958 | STATIC void |
| 5959 | xfs_bmap_check_leaf_extents( |
| 5960 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5961 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5962 | int whichfork) /* data or attr fork */ |
| 5963 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5964 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5966 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5967 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5968 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | xfs_ifork_t *ifp; /* fork structure */ |
| 5970 | int level; /* btree level, for checking */ |
| 5971 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5972 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5973 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5974 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5975 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | int bp_release = 0; |
| 5977 | |
| 5978 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5979 | return; |
| 5980 | } |
| 5981 | |
| 5982 | bno = NULLFSBLOCK; |
| 5983 | mp = ip->i_mount; |
| 5984 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5985 | block = ifp->if_broot; |
| 5986 | /* |
| 5987 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5988 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5989 | level = be16_to_cpu(block->bb_level); |
| 5990 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5992 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5993 | bno = be64_to_cpu(*pp); |
| 5994 | |
| 5995 | ASSERT(bno != NULLDFSBNO); |
| 5996 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5997 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5998 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | /* |
| 6000 | * Go down the tree until leaf level is reached, following the first |
| 6001 | * pointer (leftmost) at each level. |
| 6002 | */ |
| 6003 | while (level-- > 0) { |
| 6004 | /* See if buf is in cur first */ |
| 6005 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6006 | if (bp) { |
| 6007 | bp_release = 0; |
| 6008 | } else { |
| 6009 | bp_release = 1; |
| 6010 | } |
| 6011 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6012 | XFS_BMAP_BTREE_REF))) |
| 6013 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6014 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 6016 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | error0); |
| 6018 | if (level == 0) |
| 6019 | break; |
| 6020 | |
| 6021 | /* |
| 6022 | * Check this block for basic sanity (increasing keys and |
| 6023 | * no duplicate blocks). |
| 6024 | */ |
| 6025 | |
| 6026 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6027 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6028 | bno = be64_to_cpu(*pp); |
| 6029 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | if (bp_release) { |
| 6031 | bp_release = 0; |
| 6032 | xfs_trans_brelse(NULL, bp); |
| 6033 | } |
| 6034 | } |
| 6035 | |
| 6036 | /* |
| 6037 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 6038 | */ |
| 6039 | i = 0; |
| 6040 | |
| 6041 | /* |
| 6042 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 6043 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | xfs_fsblock_t nextbno; |
| 6046 | xfs_extnum_t num_recs; |
| 6047 | |
| 6048 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6049 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | |
| 6051 | /* |
| 6052 | * Read-ahead the next leaf block, if any. |
| 6053 | */ |
| 6054 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6055 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | |
| 6057 | /* |
| 6058 | * Check all the extents to make sure they are OK. |
| 6059 | * If we had a previous block, the last entry should |
| 6060 | * conform with the first entry in this one. |
| 6061 | */ |
| 6062 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6063 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6064 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6065 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 6066 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 6067 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6068 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6069 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6070 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6071 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 6072 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 6073 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6074 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6077 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | i += num_recs; |
| 6079 | if (bp_release) { |
| 6080 | bp_release = 0; |
| 6081 | xfs_trans_brelse(NULL, bp); |
| 6082 | } |
| 6083 | bno = nextbno; |
| 6084 | /* |
| 6085 | * If we've reached the end, stop. |
| 6086 | */ |
| 6087 | if (bno == NULLFSBLOCK) |
| 6088 | break; |
| 6089 | |
| 6090 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6091 | if (bp) { |
| 6092 | bp_release = 0; |
| 6093 | } else { |
| 6094 | bp_release = 1; |
| 6095 | } |
| 6096 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6097 | XFS_BMAP_BTREE_REF))) |
| 6098 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6099 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | } |
| 6101 | if (bp_release) { |
| 6102 | bp_release = 0; |
| 6103 | xfs_trans_brelse(NULL, bp); |
| 6104 | } |
| 6105 | return; |
| 6106 | |
| 6107 | error0: |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6108 | cmn_err(CE_WARN, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | if (bp_release) |
| 6110 | xfs_trans_brelse(NULL, bp); |
| 6111 | error_norelse: |
| 6112 | cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6113 | __func__, i); |
| 6114 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | return; |
| 6116 | } |
| 6117 | #endif |
| 6118 | |
| 6119 | /* |
| 6120 | * Count fsblocks of the given fork. |
| 6121 | */ |
| 6122 | int /* error */ |
| 6123 | xfs_bmap_count_blocks( |
| 6124 | xfs_trans_t *tp, /* transaction pointer */ |
| 6125 | xfs_inode_t *ip, /* incore inode */ |
| 6126 | int whichfork, /* data or attr fork */ |
| 6127 | int *count) /* out: count of blocks */ |
| 6128 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6129 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6131 | xfs_ifork_t *ifp; /* fork structure */ |
| 6132 | int level; /* btree level, for checking */ |
| 6133 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6134 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | |
| 6136 | bno = NULLFSBLOCK; |
| 6137 | mp = ip->i_mount; |
| 6138 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6139 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6140 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6142 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | return 0; |
| 6144 | } |
| 6145 | |
| 6146 | /* |
| 6147 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6148 | */ |
| 6149 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6150 | level = be16_to_cpu(block->bb_level); |
| 6151 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6152 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6153 | bno = be64_to_cpu(*pp); |
| 6154 | ASSERT(bno != NULLDFSBNO); |
| 6155 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6156 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6158 | 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] | 6159 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 6160 | mp); |
| 6161 | return XFS_ERROR(EFSCORRUPTED); |
| 6162 | } |
| 6163 | |
| 6164 | return 0; |
| 6165 | } |
| 6166 | |
| 6167 | /* |
| 6168 | * Recursively walks each level of a btree |
| 6169 | * to count total fsblocks is use. |
| 6170 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 6171 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | xfs_bmap_count_tree( |
| 6173 | xfs_mount_t *mp, /* file system mount point */ |
| 6174 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6175 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | xfs_fsblock_t blockno, /* file system block number */ |
| 6177 | int levelin, /* level in btree */ |
| 6178 | int *count) /* Count of blocks */ |
| 6179 | { |
| 6180 | int error; |
| 6181 | xfs_buf_t *bp, *nbp; |
| 6182 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6183 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6184 | xfs_fsblock_t bno = blockno; |
| 6185 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6186 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6188 | |
| 6189 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 6190 | return error; |
| 6191 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6192 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | |
| 6194 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 6195 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6196 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | while (nextbno != NULLFSBLOCK) { |
| 6198 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6199 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6200 | return error; |
| 6201 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6202 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6203 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | xfs_trans_brelse(tp, nbp); |
| 6205 | } |
| 6206 | |
| 6207 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6208 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6209 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6211 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | xfs_trans_brelse(tp, bp); |
| 6213 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6214 | XFS_ERRLEVEL_LOW, mp); |
| 6215 | return XFS_ERROR(EFSCORRUPTED); |
| 6216 | } |
| 6217 | xfs_trans_brelse(tp, bp); |
| 6218 | } else { |
| 6219 | /* count all level 1 nodes and their leaves */ |
| 6220 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6221 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6222 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6223 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | xfs_trans_brelse(tp, bp); |
| 6225 | if (nextbno == NULLFSBLOCK) |
| 6226 | break; |
| 6227 | bno = nextbno; |
| 6228 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6229 | XFS_BMAP_BTREE_REF))) |
| 6230 | return error; |
| 6231 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6232 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
| 6238 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6239 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6241 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6243 | xfs_ifork_t *ifp, |
| 6244 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | int numrecs, |
| 6246 | int *count) |
| 6247 | { |
| 6248 | int b; |
| 6249 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6250 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6251 | xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b); |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6252 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6253 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6257 | * Count leaf blocks given a range of extent records originally |
| 6258 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6259 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6260 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6261 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6262 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6263 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6264 | int numrecs, |
| 6265 | int *count) |
| 6266 | { |
| 6267 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6268 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6269 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6270 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6271 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6272 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | } |