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" |
| 33 | #include "xfs_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "xfs_itable.h" |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 38 | #include "xfs_dir2_data.h" |
| 39 | #include "xfs_dir2_leaf.h" |
| 40 | #include "xfs_dir2_block.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 41 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "xfs_extfree_item.h" |
| 43 | #include "xfs_alloc.h" |
| 44 | #include "xfs_bmap.h" |
| 45 | #include "xfs_rtalloc.h" |
| 46 | #include "xfs_error.h" |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 47 | #include "xfs_attr_leaf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include "xfs_rw.h" |
| 49 | #include "xfs_quota.h" |
| 50 | #include "xfs_trans_space.h" |
| 51 | #include "xfs_buf_item.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 52 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 53 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 54 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | STATIC void |
| 59 | xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork); |
| 60 | #endif |
| 61 | |
| 62 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 63 | |
| 64 | /* |
| 65 | * Prototypes for internal bmap routines. |
| 66 | */ |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 71 | */ |
| 72 | STATIC int /* error */ |
| 73 | xfs_bmap_add_attrfork_extents( |
| 74 | xfs_trans_t *tp, /* transaction pointer */ |
| 75 | xfs_inode_t *ip, /* incore inode pointer */ |
| 76 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 77 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 78 | int *flags); /* inode logging flags */ |
| 79 | |
| 80 | /* |
| 81 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 82 | */ |
| 83 | STATIC int /* error */ |
| 84 | xfs_bmap_add_attrfork_local( |
| 85 | xfs_trans_t *tp, /* transaction pointer */ |
| 86 | xfs_inode_t *ip, /* incore inode pointer */ |
| 87 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 88 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 89 | int *flags); /* inode logging flags */ |
| 90 | |
| 91 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 92 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * after allocating space (or doing a delayed allocation). |
| 94 | */ |
| 95 | STATIC int /* error */ |
| 96 | xfs_bmap_add_extent( |
| 97 | xfs_inode_t *ip, /* incore inode pointer */ |
| 98 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 99 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 100 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 102 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 103 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 104 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | int whichfork, /* data or attr fork */ |
| 106 | int rsvd); /* OK to allocate reserved blocks */ |
| 107 | |
| 108 | /* |
| 109 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 110 | * allocation to a real allocation. |
| 111 | */ |
| 112 | STATIC int /* error */ |
| 113 | xfs_bmap_add_extent_delay_real( |
| 114 | xfs_inode_t *ip, /* incore inode pointer */ |
| 115 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 116 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 117 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 119 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 120 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 121 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 122 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | int rsvd); /* OK to allocate reserved blocks */ |
| 124 | |
| 125 | /* |
| 126 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 127 | * to a delayed allocation. |
| 128 | */ |
| 129 | STATIC int /* error */ |
| 130 | xfs_bmap_add_extent_hole_delay( |
| 131 | xfs_inode_t *ip, /* incore inode pointer */ |
| 132 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 133 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 135 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int rsvd); /* OK to allocate reserved blocks */ |
| 137 | |
| 138 | /* |
| 139 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 140 | * to a real allocation. |
| 141 | */ |
| 142 | STATIC int /* error */ |
| 143 | xfs_bmap_add_extent_hole_real( |
| 144 | xfs_inode_t *ip, /* incore inode pointer */ |
| 145 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 146 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 147 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 149 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | int whichfork); /* data or attr fork */ |
| 151 | |
| 152 | /* |
| 153 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 154 | * allocation to a real allocation or vice versa. |
| 155 | */ |
| 156 | STATIC int /* error */ |
| 157 | xfs_bmap_add_extent_unwritten_real( |
| 158 | xfs_inode_t *ip, /* incore inode pointer */ |
| 159 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 160 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 161 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 162 | int *logflagsp, /* inode logging flags */ |
| 163 | xfs_extdelta_t *delta); /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 167 | * It figures out where to ask the underlying allocator to put the new extent. |
| 168 | */ |
| 169 | STATIC int /* error */ |
| 170 | xfs_bmap_alloc( |
| 171 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 172 | |
| 173 | /* |
| 174 | * Transform a btree format file with only one leaf node, where the |
| 175 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 176 | * Since the file extents are already in-core, all we have to do is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | * give up the space for the btree root and pitch the leaf block. |
| 178 | */ |
| 179 | STATIC int /* error */ |
| 180 | xfs_bmap_btree_to_extents( |
| 181 | xfs_trans_t *tp, /* transaction pointer */ |
| 182 | xfs_inode_t *ip, /* incore inode pointer */ |
| 183 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 184 | int *logflagsp, /* inode logging flags */ |
| 185 | int whichfork); /* data or attr fork */ |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 188 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * after removing space (or undoing a delayed allocation). |
| 190 | */ |
| 191 | STATIC int /* error */ |
| 192 | xfs_bmap_del_extent( |
| 193 | xfs_inode_t *ip, /* incore inode pointer */ |
| 194 | xfs_trans_t *tp, /* current trans pointer */ |
| 195 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 196 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 197 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 198 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | int *logflagsp,/* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 200 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | int whichfork, /* data or attr fork */ |
| 202 | int rsvd); /* OK to allocate reserved blocks */ |
| 203 | |
| 204 | /* |
| 205 | * Remove the entry "free" from the free item list. Prev points to the |
| 206 | * previous entry, unless "free" is the head of the list. |
| 207 | */ |
| 208 | STATIC void |
| 209 | xfs_bmap_del_free( |
| 210 | xfs_bmap_free_t *flist, /* free item list header */ |
| 211 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 212 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 213 | |
| 214 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * Convert an extents-format file into a btree-format file. |
| 216 | * The new file will have a root block (in the inode) and a single child block. |
| 217 | */ |
| 218 | STATIC int /* error */ |
| 219 | xfs_bmap_extents_to_btree( |
| 220 | xfs_trans_t *tp, /* transaction pointer */ |
| 221 | xfs_inode_t *ip, /* incore inode pointer */ |
| 222 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 223 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 224 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 225 | int wasdel, /* converting a delayed alloc */ |
| 226 | int *logflagsp, /* inode logging flags */ |
| 227 | int whichfork); /* data or attr fork */ |
| 228 | |
| 229 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * Convert a local file to an extents file. |
| 231 | * This code is sort of bogus, since the file data needs to get |
| 232 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 233 | */ |
| 234 | STATIC int /* error */ |
| 235 | xfs_bmap_local_to_extents( |
| 236 | xfs_trans_t *tp, /* transaction pointer */ |
| 237 | xfs_inode_t *ip, /* incore inode pointer */ |
| 238 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 239 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 240 | int *logflagsp, /* inode logging flags */ |
| 241 | int whichfork); /* data or attr fork */ |
| 242 | |
| 243 | /* |
| 244 | * Search the extents list for the inode, for the extent containing bno. |
| 245 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 246 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 247 | * Else, *lastxp will be set to the index of the found |
| 248 | * entry; *gotp will contain the entry. |
| 249 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 250 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | xfs_bmap_search_extents( |
| 252 | xfs_inode_t *ip, /* incore inode pointer */ |
| 253 | xfs_fileoff_t bno, /* block number searched for */ |
| 254 | int whichfork, /* data or attr fork */ |
| 255 | int *eofp, /* out: end of file found */ |
| 256 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 257 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 258 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 259 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 260 | /* |
| 261 | * Check the last inode extent to determine whether this allocation will result |
| 262 | * in blocks being allocated at the end of the file. When we allocate new data |
| 263 | * blocks at the end of the file which do not start at the previous data block, |
| 264 | * we will try to align the new blocks at stripe unit boundaries. |
| 265 | */ |
| 266 | STATIC int /* error */ |
| 267 | xfs_bmap_isaeof( |
| 268 | xfs_inode_t *ip, /* incore inode pointer */ |
| 269 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 270 | int whichfork, /* data or attribute fork */ |
| 271 | char *aeof); /* return value */ |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /* |
| 274 | * Compute the worst-case number of indirect blocks that will be used |
| 275 | * for ip's delayed extent of length "len". |
| 276 | */ |
| 277 | STATIC xfs_filblks_t |
| 278 | xfs_bmap_worst_indlen( |
| 279 | xfs_inode_t *ip, /* incore inode pointer */ |
| 280 | xfs_filblks_t len); /* delayed extent length */ |
| 281 | |
| 282 | #ifdef DEBUG |
| 283 | /* |
| 284 | * Perform various validation checks on the values being returned |
| 285 | * from xfs_bmapi(). |
| 286 | */ |
| 287 | STATIC void |
| 288 | xfs_bmap_validate_ret( |
| 289 | xfs_fileoff_t bno, |
| 290 | xfs_filblks_t len, |
| 291 | int flags, |
| 292 | xfs_bmbt_irec_t *mval, |
| 293 | int nmap, |
| 294 | int ret_nmap); |
| 295 | #else |
| 296 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 297 | #endif /* DEBUG */ |
| 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | STATIC int |
| 300 | xfs_bmap_count_tree( |
| 301 | xfs_mount_t *mp, |
| 302 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 303 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | xfs_fsblock_t blockno, |
| 305 | int levelin, |
| 306 | int *count); |
| 307 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 308 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 310 | xfs_ifork_t *ifp, |
| 311 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | int numrecs, |
| 313 | int *count); |
| 314 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 315 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 316 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 317 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 318 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 319 | int numrecs, |
| 320 | int *count); |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Bmap internal routines. |
| 324 | */ |
| 325 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 326 | STATIC int /* error */ |
| 327 | xfs_bmbt_lookup_eq( |
| 328 | struct xfs_btree_cur *cur, |
| 329 | xfs_fileoff_t off, |
| 330 | xfs_fsblock_t bno, |
| 331 | xfs_filblks_t len, |
| 332 | int *stat) /* success/failure */ |
| 333 | { |
| 334 | cur->bc_rec.b.br_startoff = off; |
| 335 | cur->bc_rec.b.br_startblock = bno; |
| 336 | cur->bc_rec.b.br_blockcount = len; |
| 337 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 338 | } |
| 339 | |
| 340 | STATIC int /* error */ |
| 341 | xfs_bmbt_lookup_ge( |
| 342 | struct xfs_btree_cur *cur, |
| 343 | xfs_fileoff_t off, |
| 344 | xfs_fsblock_t bno, |
| 345 | xfs_filblks_t len, |
| 346 | int *stat) /* success/failure */ |
| 347 | { |
| 348 | cur->bc_rec.b.br_startoff = off; |
| 349 | cur->bc_rec.b.br_startblock = bno; |
| 350 | cur->bc_rec.b.br_blockcount = len; |
| 351 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 352 | } |
| 353 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 354 | /* |
| 355 | * Update the record referred to by cur to the value given |
| 356 | * by [off, bno, len, state]. |
| 357 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 358 | */ |
| 359 | STATIC int |
| 360 | xfs_bmbt_update( |
| 361 | struct xfs_btree_cur *cur, |
| 362 | xfs_fileoff_t off, |
| 363 | xfs_fsblock_t bno, |
| 364 | xfs_filblks_t len, |
| 365 | xfs_exntst_t state) |
| 366 | { |
| 367 | union xfs_btree_rec rec; |
| 368 | |
| 369 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 370 | return xfs_btree_update(cur, &rec); |
| 371 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* |
| 374 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 375 | */ |
| 376 | STATIC int /* error */ |
| 377 | xfs_bmap_add_attrfork_btree( |
| 378 | xfs_trans_t *tp, /* transaction pointer */ |
| 379 | xfs_inode_t *ip, /* incore inode pointer */ |
| 380 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 381 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 382 | int *flags) /* inode logging flags */ |
| 383 | { |
| 384 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 385 | int error; /* error return value */ |
| 386 | xfs_mount_t *mp; /* file system mount struct */ |
| 387 | int stat; /* newroot status */ |
| 388 | |
| 389 | mp = ip->i_mount; |
| 390 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 391 | *flags |= XFS_ILOG_DBROOT; |
| 392 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 393 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | cur->bc_private.b.flist = flist; |
| 395 | cur->bc_private.b.firstblock = *firstblock; |
| 396 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 397 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 398 | /* must be at least one entry */ |
| 399 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 400 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | goto error0; |
| 402 | if (stat == 0) { |
| 403 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 404 | return XFS_ERROR(ENOSPC); |
| 405 | } |
| 406 | *firstblock = cur->bc_private.b.firstblock; |
| 407 | cur->bc_private.b.allocated = 0; |
| 408 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 409 | } |
| 410 | return 0; |
| 411 | error0: |
| 412 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 413 | return error; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 418 | */ |
| 419 | STATIC int /* error */ |
| 420 | xfs_bmap_add_attrfork_extents( |
| 421 | xfs_trans_t *tp, /* transaction pointer */ |
| 422 | xfs_inode_t *ip, /* incore inode pointer */ |
| 423 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 424 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 425 | int *flags) /* inode logging flags */ |
| 426 | { |
| 427 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 428 | int error; /* error return value */ |
| 429 | |
| 430 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 431 | return 0; |
| 432 | cur = NULL; |
| 433 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 434 | flags, XFS_DATA_FORK); |
| 435 | if (cur) { |
| 436 | cur->bc_private.b.allocated = 0; |
| 437 | xfs_btree_del_cursor(cur, |
| 438 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 439 | } |
| 440 | return error; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 445 | */ |
| 446 | STATIC int /* error */ |
| 447 | xfs_bmap_add_attrfork_local( |
| 448 | xfs_trans_t *tp, /* transaction pointer */ |
| 449 | xfs_inode_t *ip, /* incore inode pointer */ |
| 450 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 451 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 452 | int *flags) /* inode logging flags */ |
| 453 | { |
| 454 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 455 | int error; /* error return value */ |
| 456 | xfs_mount_t *mp; /* mount structure pointer */ |
| 457 | |
| 458 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 459 | return 0; |
| 460 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 461 | mp = ip->i_mount; |
| 462 | memset(&dargs, 0, sizeof(dargs)); |
| 463 | dargs.dp = ip; |
| 464 | dargs.firstblock = firstblock; |
| 465 | dargs.flist = flist; |
| 466 | dargs.total = mp->m_dirblkfsbs; |
| 467 | dargs.whichfork = XFS_DATA_FORK; |
| 468 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 469 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } else |
| 471 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 472 | XFS_DATA_FORK); |
| 473 | return error; |
| 474 | } |
| 475 | |
| 476 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 477 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | * after allocating space (or doing a delayed allocation). |
| 479 | */ |
| 480 | STATIC int /* error */ |
| 481 | xfs_bmap_add_extent( |
| 482 | xfs_inode_t *ip, /* incore inode pointer */ |
| 483 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 484 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 485 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 487 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 488 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 489 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | int whichfork, /* data or attr fork */ |
| 491 | int rsvd) /* OK to use reserved data blocks */ |
| 492 | { |
| 493 | xfs_btree_cur_t *cur; /* btree cursor or null */ |
| 494 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 495 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 496 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | xfs_ifork_t *ifp; /* inode fork ptr */ |
| 498 | int logflags; /* returned value */ |
| 499 | xfs_extnum_t nextents; /* number of extents in file now */ |
| 500 | |
| 501 | XFS_STATS_INC(xs_add_exlist); |
| 502 | cur = *curp; |
| 503 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 504 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 505 | ASSERT(idx <= nextents); |
| 506 | da_old = da_new = 0; |
| 507 | error = 0; |
| 508 | /* |
| 509 | * This is the first extent added to a new/empty file. |
| 510 | * Special case this one, so other routines get to assume there are |
| 511 | * already extents in the list. |
| 512 | */ |
| 513 | if (nextents == 0) { |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 514 | xfs_iext_insert(ip, 0, 1, new, |
| 515 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | ASSERT(cur == NULL); |
| 518 | ifp->if_lastex = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 519 | if (!isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 521 | logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } else |
| 523 | logflags = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 524 | /* DELTA: single new extent */ |
| 525 | if (delta) { |
| 526 | if (delta->xed_startoff > new->br_startoff) |
| 527 | delta->xed_startoff = new->br_startoff; |
| 528 | if (delta->xed_blockcount < |
| 529 | new->br_startoff + new->br_blockcount) |
| 530 | delta->xed_blockcount = new->br_startoff + |
| 531 | new->br_blockcount; |
| 532 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | /* |
| 535 | * Any kind of new delayed allocation goes here. |
| 536 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 537 | else if (isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | if (cur) |
| 539 | ASSERT((cur->bc_private.b.flags & |
| 540 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Eric Sandeen | e9ed9d2 | 2007-05-08 13:48:56 +1000 | [diff] [blame] | 541 | if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 542 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | goto done; |
| 544 | } |
| 545 | /* |
| 546 | * Real allocation off the end of the file. |
| 547 | */ |
| 548 | else if (idx == nextents) { |
| 549 | if (cur) |
| 550 | ASSERT((cur->bc_private.b.flags & |
| 551 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 552 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 553 | &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | goto done; |
| 555 | } else { |
| 556 | xfs_bmbt_irec_t prev; /* old extent at offset idx */ |
| 557 | |
| 558 | /* |
| 559 | * Get the record referred to by idx. |
| 560 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 561 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* |
| 563 | * If it's a real allocation record, and the new allocation ends |
| 564 | * after the start of the referred to record, then we're filling |
| 565 | * in a delayed or unwritten allocation with a real one, or |
| 566 | * converting real back to unwritten. |
| 567 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 568 | if (!isnullstartblock(new->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | new->br_startoff + new->br_blockcount > prev.br_startoff) { |
| 570 | if (prev.br_state != XFS_EXT_UNWRITTEN && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 571 | isnullstartblock(prev.br_startblock)) { |
| 572 | da_old = startblockval(prev.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if (cur) |
| 574 | ASSERT(cur->bc_private.b.flags & |
| 575 | XFS_BTCUR_BPRV_WASDEL); |
| 576 | if ((error = xfs_bmap_add_extent_delay_real(ip, |
| 577 | idx, &cur, new, &da_new, first, flist, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 578 | &logflags, delta, rsvd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | goto done; |
| 580 | } else if (new->br_state == XFS_EXT_NORM) { |
| 581 | ASSERT(new->br_state == XFS_EXT_NORM); |
| 582 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 583 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | goto done; |
| 585 | } else { |
| 586 | ASSERT(new->br_state == XFS_EXT_UNWRITTEN); |
| 587 | if ((error = xfs_bmap_add_extent_unwritten_real( |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 588 | ip, idx, &cur, new, &logflags, delta))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | goto done; |
| 590 | } |
| 591 | ASSERT(*curp == cur || *curp == NULL); |
| 592 | } |
| 593 | /* |
| 594 | * Otherwise we're filling in a hole with an allocation. |
| 595 | */ |
| 596 | else { |
| 597 | if (cur) |
| 598 | ASSERT((cur->bc_private.b.flags & |
| 599 | XFS_BTCUR_BPRV_WASDEL) == 0); |
| 600 | if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 601 | new, &logflags, delta, whichfork))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | goto done; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | ASSERT(*curp == cur || *curp == NULL); |
| 607 | /* |
| 608 | * Convert to a btree if necessary. |
| 609 | */ |
| 610 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 611 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 612 | int tmp_logflags; /* partial log flag return val */ |
| 613 | |
| 614 | ASSERT(cur == NULL); |
| 615 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first, |
| 616 | flist, &cur, da_old > 0, &tmp_logflags, whichfork); |
| 617 | logflags |= tmp_logflags; |
| 618 | if (error) |
| 619 | goto done; |
| 620 | } |
| 621 | /* |
| 622 | * Adjust for changes in reserved delayed indirect blocks. |
| 623 | * Nothing to do for disk quotas here. |
| 624 | */ |
| 625 | if (da_old || da_new) { |
| 626 | xfs_filblks_t nblks; |
| 627 | |
| 628 | nblks = da_new; |
| 629 | if (cur) |
| 630 | nblks += cur->bc_private.b.allocated; |
| 631 | ASSERT(nblks <= da_old); |
| 632 | if (nblks < da_old) |
| 633 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 634 | (int64_t)(da_old - nblks), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | /* |
| 637 | * Clear out the allocated field, done with it now in any case. |
| 638 | */ |
| 639 | if (cur) { |
| 640 | cur->bc_private.b.allocated = 0; |
| 641 | *curp = cur; |
| 642 | } |
| 643 | done: |
| 644 | #ifdef DEBUG |
| 645 | if (!error) |
| 646 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
| 647 | #endif |
| 648 | *logflagsp = logflags; |
| 649 | return error; |
| 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 654 | * allocation to a real allocation. |
| 655 | */ |
| 656 | STATIC int /* error */ |
| 657 | xfs_bmap_add_extent_delay_real( |
| 658 | xfs_inode_t *ip, /* incore inode pointer */ |
| 659 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 660 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 661 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 663 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 664 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 665 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 666 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | int rsvd) /* OK to use reserved data block allocation */ |
| 668 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 670 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 671 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 674 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 676 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 677 | /* left is 0, right is 1, prev is 2 */ |
| 678 | int rval=0; /* return value (logging flags) */ |
| 679 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 680 | xfs_filblks_t temp=0; /* value for dnew calculations */ |
| 681 | xfs_filblks_t temp2=0;/* value for dnew calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | #define LEFT r[0] |
| 685 | #define RIGHT r[1] |
| 686 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | /* |
| 689 | * Set up a bunch of variables to make the tests simpler. |
| 690 | */ |
| 691 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 692 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 693 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | xfs_bmbt_get_all(ep, &PREV); |
| 695 | new_endoff = new->br_startoff + new->br_blockcount; |
| 696 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 697 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | /* |
| 700 | * Set flags determining what part of the previous delayed allocation |
| 701 | * extent is being replaced by a real allocation. |
| 702 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 703 | if (PREV.br_startoff == new->br_startoff) |
| 704 | state |= BMAP_LEFT_FILLING; |
| 705 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 706 | state |= BMAP_RIGHT_FILLING; |
| 707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* |
| 709 | * Check and set flags if this segment has a left neighbor. |
| 710 | * Don't set contiguous if the combined extent would be too large. |
| 711 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 712 | if (idx > 0) { |
| 713 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 714 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 715 | |
| 716 | if (isnullstartblock(LEFT.br_startblock)) |
| 717 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 719 | |
| 720 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 721 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 722 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 723 | LEFT.br_state == new->br_state && |
| 724 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 725 | state |= BMAP_LEFT_CONTIG; |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | /* |
| 728 | * Check and set flags if this segment has a right neighbor. |
| 729 | * Don't set contiguous if the combined extent would be too large. |
| 730 | * Also check for all-three-contiguous being too large. |
| 731 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 732 | if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
| 733 | state |= BMAP_RIGHT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 734 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 735 | |
| 736 | if (isnullstartblock(RIGHT.br_startblock)) |
| 737 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 739 | |
| 740 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 741 | new_endoff == RIGHT.br_startoff && |
| 742 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 743 | new->br_state == RIGHT.br_state && |
| 744 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 745 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 746 | BMAP_RIGHT_FILLING)) != |
| 747 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 748 | BMAP_RIGHT_FILLING) || |
| 749 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 750 | <= MAXEXTLEN)) |
| 751 | state |= BMAP_RIGHT_CONTIG; |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | error = 0; |
| 754 | /* |
| 755 | * Switch out based on the FILLING and CONTIG state bits. |
| 756 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 757 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 758 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 759 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 760 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* |
| 762 | * Filling in all of a previously delayed allocation extent. |
| 763 | * The left and right neighbors are both contiguous with new. |
| 764 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 765 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 766 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | LEFT.br_blockcount + PREV.br_blockcount + |
| 768 | RIGHT.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 769 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 770 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 771 | xfs_iext_remove(ip, idx, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | ip->i_df.if_lastex = idx - 1; |
| 773 | ip->i_d.di_nextents--; |
| 774 | if (cur == NULL) |
| 775 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 776 | else { |
| 777 | rval = XFS_ILOG_CORE; |
| 778 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 779 | RIGHT.br_startblock, |
| 780 | RIGHT.br_blockcount, &i))) |
| 781 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 782 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 783 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 786 | if ((error = xfs_btree_decrement(cur, 0, &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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 790 | LEFT.br_startblock, |
| 791 | LEFT.br_blockcount + |
| 792 | PREV.br_blockcount + |
| 793 | RIGHT.br_blockcount, LEFT.br_state))) |
| 794 | goto done; |
| 795 | } |
| 796 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 797 | /* DELTA: Three in-core extents are replaced by one. */ |
| 798 | temp = LEFT.br_startoff; |
| 799 | temp2 = LEFT.br_blockcount + |
| 800 | PREV.br_blockcount + |
| 801 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | break; |
| 803 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 804 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /* |
| 806 | * Filling in all of a previously delayed allocation extent. |
| 807 | * The left neighbor is contiguous, the right is not. |
| 808 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 809 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 810 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 812 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | ip->i_df.if_lastex = idx - 1; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 815 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | if (cur == NULL) |
| 817 | rval = XFS_ILOG_DEXT; |
| 818 | else { |
| 819 | rval = 0; |
| 820 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 821 | LEFT.br_startblock, LEFT.br_blockcount, |
| 822 | &i))) |
| 823 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 824 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 826 | LEFT.br_startblock, |
| 827 | LEFT.br_blockcount + |
| 828 | PREV.br_blockcount, LEFT.br_state))) |
| 829 | goto done; |
| 830 | } |
| 831 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 832 | /* DELTA: Two in-core extents are replaced by one. */ |
| 833 | temp = LEFT.br_startoff; |
| 834 | temp2 = LEFT.br_blockcount + |
| 835 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | break; |
| 837 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 838 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | /* |
| 840 | * Filling in all of a previously delayed allocation extent. |
| 841 | * The right neighbor is contiguous, the left is not. |
| 842 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 843 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 845 | xfs_bmbt_set_blockcount(ep, |
| 846 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 847 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | ip->i_df.if_lastex = idx; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 850 | xfs_iext_remove(ip, idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | if (cur == NULL) |
| 852 | rval = XFS_ILOG_DEXT; |
| 853 | else { |
| 854 | rval = 0; |
| 855 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 856 | RIGHT.br_startblock, |
| 857 | RIGHT.br_blockcount, &i))) |
| 858 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 859 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 861 | new->br_startblock, |
| 862 | PREV.br_blockcount + |
| 863 | RIGHT.br_blockcount, PREV.br_state))) |
| 864 | goto done; |
| 865 | } |
| 866 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 867 | /* DELTA: Two in-core extents are replaced by one. */ |
| 868 | temp = PREV.br_startoff; |
| 869 | temp2 = PREV.br_blockcount + |
| 870 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | break; |
| 872 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 873 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | /* |
| 875 | * Filling in all of a previously delayed allocation extent. |
| 876 | * Neither the left nor right neighbors are contiguous with |
| 877 | * the new one. |
| 878 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 879 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 881 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | ip->i_df.if_lastex = idx; |
| 884 | ip->i_d.di_nextents++; |
| 885 | if (cur == NULL) |
| 886 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 887 | else { |
| 888 | rval = XFS_ILOG_CORE; |
| 889 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 890 | new->br_startblock, new->br_blockcount, |
| 891 | &i))) |
| 892 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 893 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 895 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 897 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | *dnew = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 900 | /* DELTA: The in-core extent described by new changed type. */ |
| 901 | temp = new->br_startoff; |
| 902 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | break; |
| 904 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 905 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | /* |
| 907 | * Filling in the first part of a previous delayed allocation. |
| 908 | * The left neighbor is contiguous. |
| 909 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 910 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 911 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | LEFT.br_blockcount + new->br_blockcount); |
| 913 | xfs_bmbt_set_startoff(ep, |
| 914 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 915 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 918 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | xfs_bmbt_set_blockcount(ep, temp); |
| 920 | ip->i_df.if_lastex = idx - 1; |
| 921 | if (cur == NULL) |
| 922 | rval = XFS_ILOG_DEXT; |
| 923 | else { |
| 924 | rval = 0; |
| 925 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 926 | LEFT.br_startblock, LEFT.br_blockcount, |
| 927 | &i))) |
| 928 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 929 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 931 | LEFT.br_startblock, |
| 932 | LEFT.br_blockcount + |
| 933 | new->br_blockcount, |
| 934 | LEFT.br_state))) |
| 935 | goto done; |
| 936 | } |
| 937 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 938 | startblockval(PREV.br_startblock)); |
| 939 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 940 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 942 | /* DELTA: The boundary between two in-core extents moved. */ |
| 943 | temp = LEFT.br_startoff; |
| 944 | temp2 = LEFT.br_blockcount + |
| 945 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | break; |
| 947 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 948 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | /* |
| 950 | * Filling in the first part of a previous delayed allocation. |
| 951 | * The left neighbor is not contiguous. |
| 952 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 953 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 955 | temp = PREV.br_blockcount - new->br_blockcount; |
| 956 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 957 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | ip->i_df.if_lastex = idx; |
| 959 | ip->i_d.di_nextents++; |
| 960 | if (cur == NULL) |
| 961 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 962 | else { |
| 963 | rval = XFS_ILOG_CORE; |
| 964 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 965 | new->br_startblock, new->br_blockcount, |
| 966 | &i))) |
| 967 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 968 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 970 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 972 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 975 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 976 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 977 | first, flist, &cur, 1, &tmp_rval, |
| 978 | XFS_DATA_FORK); |
| 979 | rval |= tmp_rval; |
| 980 | if (error) |
| 981 | goto done; |
| 982 | } |
| 983 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 984 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 986 | ep = xfs_iext_get_ext(ifp, idx + 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 987 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 988 | trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 990 | /* DELTA: One in-core extent is split in two. */ |
| 991 | temp = PREV.br_startoff; |
| 992 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | break; |
| 994 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 995 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | /* |
| 997 | * Filling in the last part of a previous delayed allocation. |
| 998 | * The right neighbor is contiguous with the new allocation. |
| 999 | */ |
| 1000 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1001 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
| 1002 | trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | xfs_bmbt_set_blockcount(ep, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1004 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1005 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | new->br_blockcount + RIGHT.br_blockcount, |
| 1007 | RIGHT.br_state); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1008 | trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | ip->i_df.if_lastex = idx + 1; |
| 1010 | if (cur == NULL) |
| 1011 | rval = XFS_ILOG_DEXT; |
| 1012 | else { |
| 1013 | rval = 0; |
| 1014 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1015 | RIGHT.br_startblock, |
| 1016 | RIGHT.br_blockcount, &i))) |
| 1017 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1018 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1020 | new->br_startblock, |
| 1021 | new->br_blockcount + |
| 1022 | RIGHT.br_blockcount, |
| 1023 | RIGHT.br_state))) |
| 1024 | goto done; |
| 1025 | } |
| 1026 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1027 | startblockval(PREV.br_startblock)); |
| 1028 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1029 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1031 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1032 | temp = PREV.br_startoff; |
| 1033 | temp2 = PREV.br_blockcount + |
| 1034 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | break; |
| 1036 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1037 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | /* |
| 1039 | * Filling in the last part of a previous delayed allocation. |
| 1040 | * The right neighbor is not contiguous. |
| 1041 | */ |
| 1042 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1043 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1045 | xfs_iext_insert(ip, idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | ip->i_df.if_lastex = idx + 1; |
| 1047 | ip->i_d.di_nextents++; |
| 1048 | if (cur == NULL) |
| 1049 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1050 | else { |
| 1051 | rval = XFS_ILOG_CORE; |
| 1052 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1053 | new->br_startblock, new->br_blockcount, |
| 1054 | &i))) |
| 1055 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1056 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1058 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1060 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
| 1062 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1063 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1064 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1065 | first, flist, &cur, 1, &tmp_rval, |
| 1066 | XFS_DATA_FORK); |
| 1067 | rval |= tmp_rval; |
| 1068 | if (error) |
| 1069 | goto done; |
| 1070 | } |
| 1071 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1072 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | (cur ? cur->bc_private.b.allocated : 0)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1074 | ep = xfs_iext_get_ext(ifp, idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1075 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1076 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | *dnew = temp; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1078 | /* DELTA: One in-core extent is split in two. */ |
| 1079 | temp = PREV.br_startoff; |
| 1080 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | break; |
| 1082 | |
| 1083 | case 0: |
| 1084 | /* |
| 1085 | * Filling in the middle part of a previous delayed allocation. |
| 1086 | * Contiguity is impossible here. |
| 1087 | * This case is avoided almost all the time. |
| 1088 | */ |
| 1089 | temp = new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1090 | trace_xfs_bmap_pre_update(ip, idx, 0, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | xfs_bmbt_set_blockcount(ep, temp); |
| 1092 | r[0] = *new; |
Lachlan McIlroy | d213371 | 2006-11-21 18:55:16 +1100 | [diff] [blame] | 1093 | r[1].br_state = PREV.br_state; |
| 1094 | r[1].br_startblock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | r[1].br_startoff = new_endoff; |
| 1096 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1097 | r[1].br_blockcount = temp2; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1098 | xfs_iext_insert(ip, idx + 1, 2, &r[0], state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | ip->i_df.if_lastex = idx + 1; |
| 1100 | ip->i_d.di_nextents++; |
| 1101 | if (cur == NULL) |
| 1102 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1103 | else { |
| 1104 | rval = XFS_ILOG_CORE; |
| 1105 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1106 | new->br_startblock, new->br_blockcount, |
| 1107 | &i))) |
| 1108 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1109 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1111 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1113 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | } |
| 1115 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1116 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
| 1117 | error = xfs_bmap_extents_to_btree(ip->i_transp, ip, |
| 1118 | first, flist, &cur, 1, &tmp_rval, |
| 1119 | XFS_DATA_FORK); |
| 1120 | rval |= tmp_rval; |
| 1121 | if (error) |
| 1122 | goto done; |
| 1123 | } |
| 1124 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 1125 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1126 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | (cur ? cur->bc_private.b.allocated : 0)); |
| 1128 | if (diff > 0 && |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1129 | 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] | 1130 | /* |
| 1131 | * Ick gross gag me with a spoon. |
| 1132 | */ |
| 1133 | ASSERT(0); /* want to see if this ever happens! */ |
| 1134 | while (diff > 0) { |
| 1135 | if (temp) { |
| 1136 | temp--; |
| 1137 | diff--; |
| 1138 | if (!diff || |
| 1139 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1140 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | if (temp2) { |
| 1144 | temp2--; |
| 1145 | diff--; |
| 1146 | if (!diff || |
| 1147 | !xfs_mod_incore_sb(ip->i_mount, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1148 | XFS_SBS_FDBLOCKS, -((int64_t)diff), rsvd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1153 | ep = xfs_iext_get_ext(ifp, idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1154 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1155 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1156 | trace_xfs_bmap_pre_update(ip, idx + 2, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1157 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1158 | nullstartblock((int)temp2)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1159 | trace_xfs_bmap_post_update(ip, idx + 2, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | *dnew = temp + temp2; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1161 | /* DELTA: One in-core extent is split in three. */ |
| 1162 | temp = PREV.br_startoff; |
| 1163 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | break; |
| 1165 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1166 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1167 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1168 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1169 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1170 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1171 | case BMAP_LEFT_CONTIG: |
| 1172 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * These cases are all impossible. |
| 1175 | */ |
| 1176 | ASSERT(0); |
| 1177 | } |
| 1178 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1179 | if (delta) { |
| 1180 | temp2 += temp; |
| 1181 | if (delta->xed_startoff > temp) |
| 1182 | delta->xed_startoff = temp; |
| 1183 | if (delta->xed_blockcount < temp2) |
| 1184 | delta->xed_blockcount = temp2; |
| 1185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | done: |
| 1187 | *logflagsp = rval; |
| 1188 | return error; |
| 1189 | #undef LEFT |
| 1190 | #undef RIGHT |
| 1191 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 1196 | * allocation to a real allocation or vice versa. |
| 1197 | */ |
| 1198 | STATIC int /* error */ |
| 1199 | xfs_bmap_add_extent_unwritten_real( |
| 1200 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1201 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 1202 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1203 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1204 | int *logflagsp, /* inode logging flags */ |
| 1205 | xfs_extdelta_t *delta) /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1208 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1211 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1213 | xfs_exntst_t newext; /* new extent state */ |
| 1214 | xfs_exntst_t oldext; /* old extent state */ |
| 1215 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1216 | /* left is 0, right is 1, prev is 2 */ |
| 1217 | int rval=0; /* return value (logging flags) */ |
| 1218 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1219 | xfs_filblks_t temp=0; |
| 1220 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | |
| 1222 | #define LEFT r[0] |
| 1223 | #define RIGHT r[1] |
| 1224 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | /* |
| 1226 | * Set up a bunch of variables to make the tests simpler. |
| 1227 | */ |
| 1228 | error = 0; |
| 1229 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1230 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1231 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | xfs_bmbt_get_all(ep, &PREV); |
| 1233 | newext = new->br_state; |
| 1234 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 1235 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 1236 | ASSERT(PREV.br_state == oldext); |
| 1237 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1238 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1239 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | /* |
| 1242 | * Set flags determining what part of the previous oldext allocation |
| 1243 | * extent is being replaced by a newext allocation. |
| 1244 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1245 | if (PREV.br_startoff == new->br_startoff) |
| 1246 | state |= BMAP_LEFT_FILLING; |
| 1247 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1248 | state |= BMAP_RIGHT_FILLING; |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | /* |
| 1251 | * Check and set flags if this segment has a left neighbor. |
| 1252 | * Don't set contiguous if the combined extent would be too large. |
| 1253 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1254 | if (idx > 0) { |
| 1255 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1256 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1257 | |
| 1258 | if (isnullstartblock(LEFT.br_startblock)) |
| 1259 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1261 | |
| 1262 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1263 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1264 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1265 | LEFT.br_state == newext && |
| 1266 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1267 | state |= BMAP_LEFT_CONTIG; |
| 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | /* |
| 1270 | * Check and set flags if this segment has a right neighbor. |
| 1271 | * Don't set contiguous if the combined extent would be too large. |
| 1272 | * Also check for all-three-contiguous being too large. |
| 1273 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1274 | if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
| 1275 | state |= BMAP_RIGHT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1276 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1277 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1278 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1280 | |
| 1281 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1282 | new_endoff == RIGHT.br_startoff && |
| 1283 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1284 | newext == RIGHT.br_state && |
| 1285 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1286 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1287 | BMAP_RIGHT_FILLING)) != |
| 1288 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1289 | BMAP_RIGHT_FILLING) || |
| 1290 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1291 | <= MAXEXTLEN)) |
| 1292 | state |= BMAP_RIGHT_CONTIG; |
| 1293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | /* |
| 1295 | * Switch out based on the FILLING and CONTIG state bits. |
| 1296 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1297 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1298 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1299 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1300 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | /* |
| 1302 | * Setting all of a previous oldext extent to newext. |
| 1303 | * The left and right neighbors are both contiguous with new. |
| 1304 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1305 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1306 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1308 | RIGHT.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1309 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1310 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1311 | xfs_iext_remove(ip, idx, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | ip->i_df.if_lastex = idx - 1; |
| 1313 | ip->i_d.di_nextents -= 2; |
| 1314 | if (cur == NULL) |
| 1315 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1316 | else { |
| 1317 | rval = XFS_ILOG_CORE; |
| 1318 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1319 | RIGHT.br_startblock, |
| 1320 | RIGHT.br_blockcount, &i))) |
| 1321 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1322 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1323 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1326 | if ((error = xfs_btree_decrement(cur, 0, &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 | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1329 | if ((error = xfs_btree_delete(cur, &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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1332 | if ((error = xfs_btree_decrement(cur, 0, &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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1336 | LEFT.br_startblock, |
| 1337 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1338 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1339 | goto done; |
| 1340 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1341 | /* DELTA: Three in-core extents are replaced by one. */ |
| 1342 | temp = LEFT.br_startoff; |
| 1343 | temp2 = LEFT.br_blockcount + |
| 1344 | PREV.br_blockcount + |
| 1345 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | break; |
| 1347 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1348 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | /* |
| 1350 | * Setting all of a previous oldext extent to newext. |
| 1351 | * The left neighbor is contiguous, the right is not. |
| 1352 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1353 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1354 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1356 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | ip->i_df.if_lastex = idx - 1; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1359 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | ip->i_d.di_nextents--; |
| 1361 | if (cur == NULL) |
| 1362 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1363 | else { |
| 1364 | rval = XFS_ILOG_CORE; |
| 1365 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1366 | PREV.br_startblock, PREV.br_blockcount, |
| 1367 | &i))) |
| 1368 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1369 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1370 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1373 | if ((error = xfs_btree_decrement(cur, 0, &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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1377 | LEFT.br_startblock, |
| 1378 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1379 | LEFT.br_state))) |
| 1380 | goto done; |
| 1381 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1382 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1383 | temp = LEFT.br_startoff; |
| 1384 | temp2 = LEFT.br_blockcount + |
| 1385 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | break; |
| 1387 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1388 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | /* |
| 1390 | * Setting all of a previous oldext extent to newext. |
| 1391 | * The right neighbor is contiguous, the left is not. |
| 1392 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1393 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | xfs_bmbt_set_blockcount(ep, |
| 1395 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1396 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1397 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | ip->i_df.if_lastex = idx; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1399 | xfs_iext_remove(ip, idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | ip->i_d.di_nextents--; |
| 1401 | if (cur == NULL) |
| 1402 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1403 | else { |
| 1404 | rval = XFS_ILOG_CORE; |
| 1405 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1406 | RIGHT.br_startblock, |
| 1407 | RIGHT.br_blockcount, &i))) |
| 1408 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1409 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1410 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1413 | if ((error = xfs_btree_decrement(cur, 0, &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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1417 | new->br_startblock, |
| 1418 | new->br_blockcount + RIGHT.br_blockcount, |
| 1419 | newext))) |
| 1420 | goto done; |
| 1421 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1422 | /* DELTA: Two in-core extents are replaced by one. */ |
| 1423 | temp = PREV.br_startoff; |
| 1424 | temp2 = PREV.br_blockcount + |
| 1425 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1428 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | /* |
| 1430 | * Setting all of a previous oldext extent to newext. |
| 1431 | * Neither the left nor right neighbors are contiguous with |
| 1432 | * the new one. |
| 1433 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1434 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1436 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | ip->i_df.if_lastex = idx; |
| 1439 | if (cur == NULL) |
| 1440 | rval = XFS_ILOG_DEXT; |
| 1441 | else { |
| 1442 | rval = 0; |
| 1443 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1444 | new->br_startblock, new->br_blockcount, |
| 1445 | &i))) |
| 1446 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1447 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1449 | new->br_startblock, new->br_blockcount, |
| 1450 | newext))) |
| 1451 | goto done; |
| 1452 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1453 | /* DELTA: The in-core extent described by new changed type. */ |
| 1454 | temp = new->br_startoff; |
| 1455 | temp2 = new->br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | break; |
| 1457 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1458 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | /* |
| 1460 | * Setting the first part of a previous oldext extent to newext. |
| 1461 | * The left neighbor is contiguous. |
| 1462 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1463 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1464 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | LEFT.br_blockcount + new->br_blockcount); |
| 1466 | xfs_bmbt_set_startoff(ep, |
| 1467 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1468 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1469 | |
| 1470 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | xfs_bmbt_set_startblock(ep, |
| 1472 | new->br_startblock + new->br_blockcount); |
| 1473 | xfs_bmbt_set_blockcount(ep, |
| 1474 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1475 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | ip->i_df.if_lastex = idx - 1; |
| 1478 | if (cur == NULL) |
| 1479 | rval = XFS_ILOG_DEXT; |
| 1480 | else { |
| 1481 | rval = 0; |
| 1482 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1483 | PREV.br_startblock, PREV.br_blockcount, |
| 1484 | &i))) |
| 1485 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1486 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | if ((error = xfs_bmbt_update(cur, |
| 1488 | PREV.br_startoff + new->br_blockcount, |
| 1489 | PREV.br_startblock + new->br_blockcount, |
| 1490 | PREV.br_blockcount - new->br_blockcount, |
| 1491 | oldext))) |
| 1492 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1493 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | goto done; |
| 1495 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1496 | LEFT.br_startblock, |
| 1497 | LEFT.br_blockcount + new->br_blockcount, |
| 1498 | LEFT.br_state)) |
| 1499 | goto done; |
| 1500 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1501 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1502 | temp = LEFT.br_startoff; |
| 1503 | temp2 = LEFT.br_blockcount + |
| 1504 | PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | break; |
| 1506 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1507 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * Setting the first part of a previous oldext extent to newext. |
| 1510 | * The left neighbor is not contiguous. |
| 1511 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1512 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1514 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1515 | xfs_bmbt_set_blockcount(ep, |
| 1516 | PREV.br_blockcount - new->br_blockcount); |
| 1517 | xfs_bmbt_set_startblock(ep, |
| 1518 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1519 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1520 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1521 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | ip->i_df.if_lastex = idx; |
| 1523 | ip->i_d.di_nextents++; |
| 1524 | if (cur == NULL) |
| 1525 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1526 | else { |
| 1527 | rval = XFS_ILOG_CORE; |
| 1528 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1529 | PREV.br_startblock, PREV.br_blockcount, |
| 1530 | &i))) |
| 1531 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1532 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | if ((error = xfs_bmbt_update(cur, |
| 1534 | PREV.br_startoff + new->br_blockcount, |
| 1535 | PREV.br_startblock + new->br_blockcount, |
| 1536 | PREV.br_blockcount - new->br_blockcount, |
| 1537 | oldext))) |
| 1538 | goto done; |
| 1539 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1540 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1542 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1544 | /* DELTA: One in-core extent is split in two. */ |
| 1545 | temp = PREV.br_startoff; |
| 1546 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | break; |
| 1548 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1549 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | /* |
| 1551 | * Setting the last part of a previous oldext extent to newext. |
| 1552 | * The right neighbor is contiguous with the new allocation. |
| 1553 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1554 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
| 1555 | trace_xfs_bmap_pre_update(ip, idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | xfs_bmbt_set_blockcount(ep, |
| 1557 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1558 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1559 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, idx + 1), |
| 1560 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1562 | trace_xfs_bmap_post_update(ip, idx + 1, state, _THIS_IP_); |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | ip->i_df.if_lastex = idx + 1; |
| 1565 | if (cur == NULL) |
| 1566 | rval = XFS_ILOG_DEXT; |
| 1567 | else { |
| 1568 | rval = 0; |
| 1569 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1570 | PREV.br_startblock, |
| 1571 | PREV.br_blockcount, &i))) |
| 1572 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1573 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1575 | PREV.br_startblock, |
| 1576 | PREV.br_blockcount - new->br_blockcount, |
| 1577 | oldext))) |
| 1578 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1579 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | goto done; |
| 1581 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1582 | new->br_startblock, |
| 1583 | new->br_blockcount + RIGHT.br_blockcount, |
| 1584 | newext))) |
| 1585 | goto done; |
| 1586 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1587 | /* DELTA: The boundary between two in-core extents moved. */ |
| 1588 | temp = PREV.br_startoff; |
| 1589 | temp2 = PREV.br_blockcount + |
| 1590 | RIGHT.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | break; |
| 1592 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1593 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | /* |
| 1595 | * Setting the last part of a previous oldext extent to newext. |
| 1596 | * The right neighbor is not contiguous. |
| 1597 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1598 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | xfs_bmbt_set_blockcount(ep, |
| 1600 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1601 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1602 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1603 | xfs_iext_insert(ip, idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | ip->i_df.if_lastex = idx + 1; |
| 1605 | ip->i_d.di_nextents++; |
| 1606 | if (cur == NULL) |
| 1607 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1608 | else { |
| 1609 | rval = XFS_ILOG_CORE; |
| 1610 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1611 | PREV.br_startblock, PREV.br_blockcount, |
| 1612 | &i))) |
| 1613 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1614 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1616 | PREV.br_startblock, |
| 1617 | PREV.br_blockcount - new->br_blockcount, |
| 1618 | oldext))) |
| 1619 | goto done; |
| 1620 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1621 | new->br_startblock, new->br_blockcount, |
| 1622 | &i))) |
| 1623 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1624 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1626 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1628 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1630 | /* DELTA: One in-core extent is split in two. */ |
| 1631 | temp = PREV.br_startoff; |
| 1632 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | break; |
| 1634 | |
| 1635 | case 0: |
| 1636 | /* |
| 1637 | * Setting the middle part of a previous oldext extent to |
| 1638 | * newext. Contiguity is impossible here. |
| 1639 | * One extent becomes three extents. |
| 1640 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1641 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | xfs_bmbt_set_blockcount(ep, |
| 1643 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1644 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | r[0] = *new; |
| 1647 | r[1].br_startoff = new_endoff; |
| 1648 | r[1].br_blockcount = |
| 1649 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1650 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1651 | r[1].br_state = oldext; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1652 | xfs_iext_insert(ip, idx + 1, 2, &r[0], state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | ip->i_df.if_lastex = idx + 1; |
| 1654 | ip->i_d.di_nextents += 2; |
| 1655 | if (cur == NULL) |
| 1656 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1657 | else { |
| 1658 | rval = XFS_ILOG_CORE; |
| 1659 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1660 | PREV.br_startblock, PREV.br_blockcount, |
| 1661 | &i))) |
| 1662 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1663 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | /* new right extent - oldext */ |
| 1665 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1666 | r[1].br_startblock, r[1].br_blockcount, |
| 1667 | r[1].br_state))) |
| 1668 | goto done; |
| 1669 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1671 | cur->bc_rec.b.br_blockcount = |
| 1672 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1673 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1675 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1676 | /* |
| 1677 | * Reset the cursor to the position of the new extent |
| 1678 | * we are about to insert as we can't trust it after |
| 1679 | * the previous insert. |
| 1680 | */ |
| 1681 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1682 | new->br_startblock, new->br_blockcount, |
| 1683 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1685 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1687 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1688 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1690 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1692 | /* DELTA: One in-core extent is split in three. */ |
| 1693 | temp = PREV.br_startoff; |
| 1694 | temp2 = PREV.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | break; |
| 1696 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1697 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1698 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1699 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1700 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1701 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1702 | case BMAP_LEFT_CONTIG: |
| 1703 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | /* |
| 1705 | * These cases are all impossible. |
| 1706 | */ |
| 1707 | ASSERT(0); |
| 1708 | } |
| 1709 | *curp = cur; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1710 | if (delta) { |
| 1711 | temp2 += temp; |
| 1712 | if (delta->xed_startoff > temp) |
| 1713 | delta->xed_startoff = temp; |
| 1714 | if (delta->xed_blockcount < temp2) |
| 1715 | delta->xed_blockcount = temp2; |
| 1716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | done: |
| 1718 | *logflagsp = rval; |
| 1719 | return error; |
| 1720 | #undef LEFT |
| 1721 | #undef RIGHT |
| 1722 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1727 | * to a delayed allocation. |
| 1728 | */ |
| 1729 | /*ARGSUSED*/ |
| 1730 | STATIC int /* error */ |
| 1731 | xfs_bmap_add_extent_hole_delay( |
| 1732 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1733 | xfs_extnum_t idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1734 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1736 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | int rsvd) /* OK to allocate reserved blocks */ |
| 1738 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1739 | xfs_bmbt_rec_host_t *ep; /* extent record for idx */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1740 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1742 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1743 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1744 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1745 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1746 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
| 1747 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1749 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 1750 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1752 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | /* |
| 1755 | * Check and set flags if this segment has a left neighbor |
| 1756 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1757 | if (idx > 0) { |
| 1758 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1759 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1760 | |
| 1761 | if (isnullstartblock(left.br_startblock)) |
| 1762 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | /* |
| 1766 | * Check and set flags if the current (right) segment exists. |
| 1767 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1768 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1769 | if (idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
| 1770 | state |= BMAP_RIGHT_VALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | xfs_bmbt_get_all(ep, &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1772 | |
| 1773 | if (isnullstartblock(right.br_startblock)) |
| 1774 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | /* |
| 1778 | * Set contiguity flags on the left and right neighbors. |
| 1779 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1780 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1781 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1782 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1783 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1784 | state |= BMAP_LEFT_CONTIG; |
| 1785 | |
| 1786 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1787 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1788 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1789 | (!(state & BMAP_LEFT_CONTIG) || |
| 1790 | (left.br_blockcount + new->br_blockcount + |
| 1791 | right.br_blockcount <= MAXEXTLEN))) |
| 1792 | state |= BMAP_RIGHT_CONTIG; |
| 1793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | /* |
| 1795 | * Switch out based on the contiguity flags. |
| 1796 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1797 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1798 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | /* |
| 1800 | * New allocation is contiguous with delayed allocations |
| 1801 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1802 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | */ |
| 1804 | temp = left.br_blockcount + new->br_blockcount + |
| 1805 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1806 | |
| 1807 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1808 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1809 | oldlen = startblockval(left.br_startblock) + |
| 1810 | startblockval(new->br_startblock) + |
| 1811 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1813 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1814 | nullstartblock((int)newlen)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1815 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1816 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1817 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1819 | /* DELTA: Two in-core extents were replaced by one. */ |
| 1820 | temp2 = temp; |
| 1821 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | break; |
| 1823 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1824 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | /* |
| 1826 | * New allocation is contiguous with a delayed allocation |
| 1827 | * on the left. |
| 1828 | * Merge the new allocation with the left neighbor. |
| 1829 | */ |
| 1830 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1831 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1832 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1833 | oldlen = startblockval(left.br_startblock) + |
| 1834 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1836 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, idx - 1), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1837 | nullstartblock((int)newlen)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1838 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | ip->i_df.if_lastex = idx - 1; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1841 | /* DELTA: One in-core extent grew into a hole. */ |
| 1842 | temp2 = temp; |
| 1843 | temp = left.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | break; |
| 1845 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1846 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | /* |
| 1848 | * New allocation is contiguous with a delayed allocation |
| 1849 | * on the right. |
| 1850 | * Merge the new allocation with the right neighbor. |
| 1851 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1852 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1854 | oldlen = startblockval(new->br_startblock) + |
| 1855 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | newlen = xfs_bmap_worst_indlen(ip, temp); |
| 1857 | xfs_bmbt_set_allf(ep, new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1858 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1859 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 1860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1862 | /* DELTA: One in-core extent grew into a hole. */ |
| 1863 | temp2 = temp; |
| 1864 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | break; |
| 1866 | |
| 1867 | case 0: |
| 1868 | /* |
| 1869 | * New allocation is not contiguous with another |
| 1870 | * delayed allocation. |
| 1871 | * Insert a new entry. |
| 1872 | */ |
| 1873 | oldlen = newlen = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1874 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | ip->i_df.if_lastex = idx; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1876 | /* DELTA: A new in-core extent was added in a hole. */ |
| 1877 | temp2 = new->br_blockcount; |
| 1878 | temp = new->br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | break; |
| 1880 | } |
| 1881 | if (oldlen != newlen) { |
| 1882 | ASSERT(oldlen > newlen); |
| 1883 | xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 1884 | (int64_t)(oldlen - newlen), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | /* |
| 1886 | * Nothing to do for disk quota accounting here. |
| 1887 | */ |
| 1888 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1889 | if (delta) { |
| 1890 | temp2 += temp; |
| 1891 | if (delta->xed_startoff > temp) |
| 1892 | delta->xed_startoff = temp; |
| 1893 | if (delta->xed_blockcount < temp2) |
| 1894 | delta->xed_blockcount = temp2; |
| 1895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | *logflagsp = 0; |
| 1897 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /* |
| 1901 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1902 | * to a real allocation. |
| 1903 | */ |
| 1904 | STATIC int /* error */ |
| 1905 | xfs_bmap_add_extent_hole_real( |
| 1906 | xfs_inode_t *ip, /* incore inode pointer */ |
| 1907 | xfs_extnum_t idx, /* extent number to update/insert */ |
| 1908 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1909 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1911 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | int whichfork) /* data or attr fork */ |
| 1913 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1914 | xfs_bmbt_rec_host_t *ep; /* pointer to extent entry ins. point */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | int i; /* temp state */ |
| 1917 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1918 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1919 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1920 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1922 | xfs_filblks_t temp=0; |
| 1923 | xfs_filblks_t temp2=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | |
| 1925 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 1926 | ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1927 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | state = 0; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1929 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1930 | if (whichfork == XFS_ATTR_FORK) |
| 1931 | state |= BMAP_ATTRFORK; |
| 1932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | /* |
| 1934 | * Check and set flags if this segment has a left neighbor. |
| 1935 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1936 | if (idx > 0) { |
| 1937 | state |= BMAP_LEFT_VALID; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1938 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1939 | if (isnullstartblock(left.br_startblock)) |
| 1940 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | /* |
| 1944 | * Check and set flags if this segment has a current value. |
| 1945 | * Not true if we're inserting into the "hole" at eof. |
| 1946 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1947 | if (idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
| 1948 | state |= BMAP_RIGHT_VALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | xfs_bmbt_get_all(ep, &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1950 | if (isnullstartblock(right.br_startblock)) |
| 1951 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | /* |
| 1955 | * We're inserting a real allocation between "left" and "right". |
| 1956 | * Set the contiguity flags. Don't let extents get too large. |
| 1957 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1958 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1959 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1960 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1961 | left.br_state == new->br_state && |
| 1962 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1963 | state |= BMAP_LEFT_CONTIG; |
| 1964 | |
| 1965 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1966 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1967 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1968 | new->br_state == right.br_state && |
| 1969 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1970 | (!(state & BMAP_LEFT_CONTIG) || |
| 1971 | left.br_blockcount + new->br_blockcount + |
| 1972 | right.br_blockcount <= MAXEXTLEN)) |
| 1973 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1975 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | /* |
| 1977 | * Select which case we're in here, and implement it. |
| 1978 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1979 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1980 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | /* |
| 1982 | * New allocation is contiguous with real allocations on the |
| 1983 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1984 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1986 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1987 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | left.br_blockcount + new->br_blockcount + |
| 1989 | right.br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1990 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 1991 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1992 | xfs_iext_remove(ip, idx, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | ifp->if_lastex = idx - 1; |
| 1994 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1995 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 1996 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1997 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1998 | } else { |
| 1999 | rval = XFS_ILOG_CORE; |
| 2000 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2001 | right.br_startoff, |
| 2002 | right.br_startblock, |
| 2003 | right.br_blockcount, &i))) |
| 2004 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2005 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2006 | if ((error = xfs_btree_delete(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 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 | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 2009 | if ((error = xfs_btree_decrement(cur, 0, &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); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2012 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2013 | left.br_startblock, |
| 2014 | left.br_blockcount + |
| 2015 | new->br_blockcount + |
| 2016 | right.br_blockcount, |
| 2017 | left.br_state))) |
| 2018 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2020 | /* DELTA: Two in-core extents were replaced by one. */ |
| 2021 | temp = left.br_startoff; |
| 2022 | temp2 = left.br_blockcount + |
| 2023 | new->br_blockcount + |
| 2024 | right.br_blockcount; |
| 2025 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2027 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | /* |
| 2029 | * New allocation is contiguous with a real allocation |
| 2030 | * on the left. |
| 2031 | * Merge the new allocation with the left neighbor. |
| 2032 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2033 | trace_xfs_bmap_pre_update(ip, idx - 1, state, _THIS_IP_); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2034 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2036 | trace_xfs_bmap_post_update(ip, idx - 1, state, _THIS_IP_); |
| 2037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | ifp->if_lastex = idx - 1; |
| 2039 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2040 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2041 | } else { |
| 2042 | rval = 0; |
| 2043 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2044 | left.br_startoff, |
| 2045 | left.br_startblock, |
| 2046 | left.br_blockcount, &i))) |
| 2047 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2048 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2049 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 2050 | left.br_startblock, |
| 2051 | left.br_blockcount + |
| 2052 | new->br_blockcount, |
| 2053 | left.br_state))) |
| 2054 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2056 | /* DELTA: One in-core extent grew. */ |
| 2057 | temp = left.br_startoff; |
| 2058 | temp2 = left.br_blockcount + |
| 2059 | new->br_blockcount; |
| 2060 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 2062 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | /* |
| 2064 | * New allocation is contiguous with a real allocation |
| 2065 | * on the right. |
| 2066 | * Merge the new allocation with the right neighbor. |
| 2067 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2068 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock, |
| 2070 | new->br_blockcount + right.br_blockcount, |
| 2071 | right.br_state); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2072 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 2073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | ifp->if_lastex = idx; |
| 2075 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2076 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2077 | } else { |
| 2078 | rval = 0; |
| 2079 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2080 | right.br_startoff, |
| 2081 | right.br_startblock, |
| 2082 | right.br_blockcount, &i))) |
| 2083 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2084 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2085 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 2086 | new->br_startblock, |
| 2087 | new->br_blockcount + |
| 2088 | right.br_blockcount, |
| 2089 | right.br_state))) |
| 2090 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2092 | /* DELTA: One in-core extent grew. */ |
| 2093 | temp = new->br_startoff; |
| 2094 | temp2 = new->br_blockcount + |
| 2095 | right.br_blockcount; |
| 2096 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
| 2098 | case 0: |
| 2099 | /* |
| 2100 | * New allocation is not contiguous with another |
| 2101 | * real allocation. |
| 2102 | * Insert a new entry. |
| 2103 | */ |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2104 | xfs_iext_insert(ip, idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | ifp->if_lastex = idx; |
| 2106 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2107 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2108 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2109 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2110 | } else { |
| 2111 | rval = XFS_ILOG_CORE; |
| 2112 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2113 | new->br_startoff, |
| 2114 | new->br_startblock, |
| 2115 | new->br_blockcount, &i))) |
| 2116 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2117 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2118 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2119 | if ((error = xfs_btree_insert(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2120 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2121 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2123 | /* DELTA: A new extent was added in a hole. */ |
| 2124 | temp = new->br_startoff; |
| 2125 | temp2 = new->br_blockcount; |
| 2126 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2128 | if (delta) { |
| 2129 | temp2 += temp; |
| 2130 | if (delta->xed_startoff > temp) |
| 2131 | delta->xed_startoff = temp; |
| 2132 | if (delta->xed_blockcount < temp2) |
| 2133 | delta->xed_blockcount = temp2; |
| 2134 | } |
| 2135 | done: |
| 2136 | *logflagsp = rval; |
| 2137 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 2140 | /* |
| 2141 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 2142 | */ |
| 2143 | STATIC int |
| 2144 | xfs_bmap_extsize_align( |
| 2145 | xfs_mount_t *mp, |
| 2146 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 2147 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 2148 | xfs_extlen_t extsz, /* align to this extent size */ |
| 2149 | int rt, /* is this a realtime inode? */ |
| 2150 | int eof, /* is extent at end-of-file? */ |
| 2151 | int delay, /* creating delalloc extent? */ |
| 2152 | int convert, /* overwriting unwritten extent? */ |
| 2153 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 2154 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 2155 | { |
| 2156 | xfs_fileoff_t orig_off; /* original offset */ |
| 2157 | xfs_extlen_t orig_alen; /* original length */ |
| 2158 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2159 | xfs_fileoff_t nexto; /* next file offset */ |
| 2160 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2161 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2162 | xfs_extlen_t align_alen; /* temp for length */ |
| 2163 | xfs_extlen_t temp; /* temp for calculations */ |
| 2164 | |
| 2165 | if (convert) |
| 2166 | return 0; |
| 2167 | |
| 2168 | orig_off = align_off = *offp; |
| 2169 | orig_alen = align_alen = *lenp; |
| 2170 | orig_end = orig_off + orig_alen; |
| 2171 | |
| 2172 | /* |
| 2173 | * If this request overlaps an existing extent, then don't |
| 2174 | * attempt to perform any additional alignment. |
| 2175 | */ |
| 2176 | if (!delay && !eof && |
| 2177 | (orig_off >= gotp->br_startoff) && |
| 2178 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2179 | return 0; |
| 2180 | } |
| 2181 | |
| 2182 | /* |
| 2183 | * If the file offset is unaligned vs. the extent size |
| 2184 | * we need to align it. This will be possible unless |
| 2185 | * the file was previously written with a kernel that didn't |
| 2186 | * perform this alignment, or if a truncate shot us in the |
| 2187 | * foot. |
| 2188 | */ |
| 2189 | temp = do_mod(orig_off, extsz); |
| 2190 | if (temp) { |
| 2191 | align_alen += temp; |
| 2192 | align_off -= temp; |
| 2193 | } |
| 2194 | /* |
| 2195 | * Same adjustment for the end of the requested area. |
| 2196 | */ |
| 2197 | if ((temp = (align_alen % extsz))) { |
| 2198 | align_alen += extsz - temp; |
| 2199 | } |
| 2200 | /* |
| 2201 | * If the previous block overlaps with this proposed allocation |
| 2202 | * then move the start forward without adjusting the length. |
| 2203 | */ |
| 2204 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2205 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2206 | prevo = prevp->br_startoff; |
| 2207 | else |
| 2208 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2209 | } else |
| 2210 | prevo = 0; |
| 2211 | if (align_off != orig_off && align_off < prevo) |
| 2212 | align_off = prevo; |
| 2213 | /* |
| 2214 | * If the next block overlaps with this proposed allocation |
| 2215 | * then move the start back without adjusting the length, |
| 2216 | * but not before offset 0. |
| 2217 | * This may of course make the start overlap previous block, |
| 2218 | * and if we hit the offset 0 limit then the next block |
| 2219 | * can still overlap too. |
| 2220 | */ |
| 2221 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2222 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2223 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2224 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2225 | else |
| 2226 | nexto = gotp->br_startoff; |
| 2227 | } else |
| 2228 | nexto = NULLFILEOFF; |
| 2229 | if (!eof && |
| 2230 | align_off + align_alen != orig_end && |
| 2231 | align_off + align_alen > nexto) |
| 2232 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2233 | /* |
| 2234 | * If we're now overlapping the next or previous extent that |
| 2235 | * means we can't fit an extsz piece in this hole. Just move |
| 2236 | * the start forward to the first valid spot and set |
| 2237 | * the length so we hit the end. |
| 2238 | */ |
| 2239 | if (align_off != orig_off && align_off < prevo) |
| 2240 | align_off = prevo; |
| 2241 | if (align_off + align_alen != orig_end && |
| 2242 | align_off + align_alen > nexto && |
| 2243 | nexto != NULLFILEOFF) { |
| 2244 | ASSERT(nexto > prevo); |
| 2245 | align_alen = nexto - align_off; |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * If realtime, and the result isn't a multiple of the realtime |
| 2250 | * extent size we need to remove blocks until it is. |
| 2251 | */ |
| 2252 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2253 | /* |
| 2254 | * We're not covering the original request, or |
| 2255 | * we won't be able to once we fix the length. |
| 2256 | */ |
| 2257 | if (orig_off < align_off || |
| 2258 | orig_end > align_off + align_alen || |
| 2259 | align_alen - temp < orig_alen) |
| 2260 | return XFS_ERROR(EINVAL); |
| 2261 | /* |
| 2262 | * Try to fix it by moving the start up. |
| 2263 | */ |
| 2264 | if (align_off + temp <= orig_off) { |
| 2265 | align_alen -= temp; |
| 2266 | align_off += temp; |
| 2267 | } |
| 2268 | /* |
| 2269 | * Try to fix it by moving the end in. |
| 2270 | */ |
| 2271 | else if (align_off + align_alen - temp >= orig_end) |
| 2272 | align_alen -= temp; |
| 2273 | /* |
| 2274 | * Set the start to the minimum then trim the length. |
| 2275 | */ |
| 2276 | else { |
| 2277 | align_alen -= orig_off - align_off; |
| 2278 | align_off = orig_off; |
| 2279 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 2280 | } |
| 2281 | /* |
| 2282 | * Result doesn't cover the request, fail it. |
| 2283 | */ |
| 2284 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2285 | return XFS_ERROR(EINVAL); |
| 2286 | } else { |
| 2287 | ASSERT(orig_off >= align_off); |
| 2288 | ASSERT(orig_end <= align_off + align_alen); |
| 2289 | } |
| 2290 | |
| 2291 | #ifdef DEBUG |
| 2292 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2293 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2294 | if (prevp->br_startoff != NULLFILEOFF) |
| 2295 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2296 | #endif |
| 2297 | |
| 2298 | *lenp = align_alen; |
| 2299 | *offp = align_off; |
| 2300 | return 0; |
| 2301 | } |
| 2302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2304 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2305 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2306 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2307 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2308 | { |
| 2309 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2311 | xfs_mount_t *mp; /* mount point structure */ |
| 2312 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2313 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
| 2315 | #define ISVALID(x,y) \ |
| 2316 | (rt ? \ |
| 2317 | (x) < mp->m_sb.sb_rblocks : \ |
| 2318 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2319 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2320 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | mp = ap->ip->i_mount; |
| 2323 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2324 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
| 2325 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | /* |
| 2327 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2328 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | */ |
| 2330 | if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2331 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, |
| 2333 | ap->prevp->br_startblock)) { |
| 2334 | ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; |
| 2335 | /* |
| 2336 | * Adjust for the gap between prevp and us. |
| 2337 | */ |
| 2338 | adjust = ap->off - |
| 2339 | (ap->prevp->br_startoff + ap->prevp->br_blockcount); |
| 2340 | if (adjust && |
| 2341 | ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) |
| 2342 | ap->rval += adjust; |
| 2343 | } |
| 2344 | /* |
| 2345 | * If not at eof, then compare the two neighbor blocks. |
| 2346 | * Figure out whether either one gives us a good starting point, |
| 2347 | * and pick the better one. |
| 2348 | */ |
| 2349 | else if (!ap->eof) { |
| 2350 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2351 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2352 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2353 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2354 | |
| 2355 | /* |
| 2356 | * If there's a previous (left) block, select a requested |
| 2357 | * start block based on it. |
| 2358 | */ |
| 2359 | if (ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2360 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | (prevbno = ap->prevp->br_startblock + |
| 2362 | ap->prevp->br_blockcount) && |
| 2363 | ISVALID(prevbno, ap->prevp->br_startblock)) { |
| 2364 | /* |
| 2365 | * Calculate gap to end of previous block. |
| 2366 | */ |
| 2367 | adjust = prevdiff = ap->off - |
| 2368 | (ap->prevp->br_startoff + |
| 2369 | ap->prevp->br_blockcount); |
| 2370 | /* |
| 2371 | * Figure the startblock based on the previous block's |
| 2372 | * end and the gap size. |
| 2373 | * Heuristic! |
| 2374 | * If the gap is large relative to the piece we're |
| 2375 | * allocating, or using it gives us an invalid block |
| 2376 | * number, then just use the end of the previous block. |
| 2377 | */ |
| 2378 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2379 | ISVALID(prevbno + prevdiff, |
| 2380 | ap->prevp->br_startblock)) |
| 2381 | prevbno += adjust; |
| 2382 | else |
| 2383 | prevdiff += adjust; |
| 2384 | /* |
| 2385 | * If the firstblock forbids it, can't use it, |
| 2386 | * must use default. |
| 2387 | */ |
| 2388 | if (!rt && !nullfb && |
| 2389 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2390 | prevbno = NULLFSBLOCK; |
| 2391 | } |
| 2392 | /* |
| 2393 | * No previous block or can't follow it, just default. |
| 2394 | */ |
| 2395 | else |
| 2396 | prevbno = NULLFSBLOCK; |
| 2397 | /* |
| 2398 | * If there's a following (right) block, select a requested |
| 2399 | * start block based on it. |
| 2400 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2401 | if (!isnullstartblock(ap->gotp->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | /* |
| 2403 | * Calculate gap to start of next block. |
| 2404 | */ |
| 2405 | adjust = gotdiff = ap->gotp->br_startoff - ap->off; |
| 2406 | /* |
| 2407 | * Figure the startblock based on the next block's |
| 2408 | * start and the gap size. |
| 2409 | */ |
| 2410 | gotbno = ap->gotp->br_startblock; |
| 2411 | /* |
| 2412 | * Heuristic! |
| 2413 | * If the gap is large relative to the piece we're |
| 2414 | * allocating, or using it gives us an invalid block |
| 2415 | * number, then just use the start of the next block |
| 2416 | * offset by our length. |
| 2417 | */ |
| 2418 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2419 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2420 | gotbno -= adjust; |
| 2421 | else if (ISVALID(gotbno - ap->alen, gotbno)) { |
| 2422 | gotbno -= ap->alen; |
| 2423 | gotdiff += adjust - ap->alen; |
| 2424 | } else |
| 2425 | gotdiff += adjust; |
| 2426 | /* |
| 2427 | * If the firstblock forbids it, can't use it, |
| 2428 | * must use default. |
| 2429 | */ |
| 2430 | if (!rt && !nullfb && |
| 2431 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2432 | gotbno = NULLFSBLOCK; |
| 2433 | } |
| 2434 | /* |
| 2435 | * No next block, just default. |
| 2436 | */ |
| 2437 | else |
| 2438 | gotbno = NULLFSBLOCK; |
| 2439 | /* |
| 2440 | * If both valid, pick the better one, else the only good |
| 2441 | * one, else ap->rval is already set (to 0 or the inode block). |
| 2442 | */ |
| 2443 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
| 2444 | ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; |
| 2445 | else if (prevbno != NULLFSBLOCK) |
| 2446 | ap->rval = prevbno; |
| 2447 | else if (gotbno != NULLFSBLOCK) |
| 2448 | ap->rval = gotbno; |
| 2449 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2450 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | STATIC int |
| 2454 | xfs_bmap_rtalloc( |
| 2455 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2456 | { |
| 2457 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2458 | int error; /* error return value */ |
| 2459 | xfs_mount_t *mp; /* mount point structure */ |
| 2460 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2461 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2462 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2463 | xfs_rtblock_t rtb; |
| 2464 | |
| 2465 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2466 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2467 | prod = align / mp->m_sb.sb_rextsize; |
| 2468 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2469 | align, 1, ap->eof, 0, |
| 2470 | ap->conv, &ap->off, &ap->alen); |
| 2471 | if (error) |
| 2472 | return error; |
| 2473 | ASSERT(ap->alen); |
| 2474 | ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); |
| 2475 | |
| 2476 | /* |
| 2477 | * If the offset & length are not perfectly aligned |
| 2478 | * then kill prod, it will just get us in trouble. |
| 2479 | */ |
| 2480 | if (do_mod(ap->off, align) || ap->alen % align) |
| 2481 | prod = 1; |
| 2482 | /* |
| 2483 | * Set ralen to be the actual requested length in rtextents. |
| 2484 | */ |
| 2485 | ralen = ap->alen / mp->m_sb.sb_rextsize; |
| 2486 | /* |
| 2487 | * If the old value was close enough to MAXEXTLEN that |
| 2488 | * we rounded up to it, cut it back so it's valid again. |
| 2489 | * Note that if it's a really large request (bigger than |
| 2490 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2491 | * adjust the starting point to match it. |
| 2492 | */ |
| 2493 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2494 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
| 2495 | /* |
| 2496 | * If it's an allocation to an empty file at offset 0, |
| 2497 | * pick an extent that will space things out in the rt area. |
| 2498 | */ |
| 2499 | if (ap->eof && ap->off == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2500 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2501 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2502 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2503 | if (error) |
| 2504 | return error; |
| 2505 | ap->rval = rtx * mp->m_sb.sb_rextsize; |
| 2506 | } else { |
| 2507 | ap->rval = 0; |
| 2508 | } |
| 2509 | |
| 2510 | xfs_bmap_adjacent(ap); |
| 2511 | |
| 2512 | /* |
| 2513 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2514 | */ |
| 2515 | atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2516 | do_div(ap->rval, mp->m_sb.sb_rextsize); |
| 2517 | rtb = ap->rval; |
| 2518 | ap->alen = ralen; |
| 2519 | if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, |
| 2520 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2521 | return error; |
| 2522 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 2523 | (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, |
| 2524 | ap->alen, &ralen, atype, |
| 2525 | ap->wasdel, 1, &rtb))) |
| 2526 | return error; |
| 2527 | ap->rval = rtb; |
| 2528 | if (ap->rval != NULLFSBLOCK) { |
| 2529 | ap->rval *= mp->m_sb.sb_rextsize; |
| 2530 | ralen *= mp->m_sb.sb_rextsize; |
| 2531 | ap->alen = ralen; |
| 2532 | ap->ip->i_d.di_nblocks += ralen; |
| 2533 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2534 | if (ap->wasdel) |
| 2535 | ap->ip->i_delayed_blks -= ralen; |
| 2536 | /* |
| 2537 | * Adjust the disk quota also. This was reserved |
| 2538 | * earlier. |
| 2539 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2540 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2541 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2542 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2543 | } else { |
| 2544 | ap->alen = 0; |
| 2545 | } |
| 2546 | return 0; |
| 2547 | } |
| 2548 | |
| 2549 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2550 | xfs_bmap_btalloc_nullfb( |
| 2551 | struct xfs_bmalloca *ap, |
| 2552 | struct xfs_alloc_arg *args, |
| 2553 | xfs_extlen_t *blen) |
| 2554 | { |
| 2555 | struct xfs_mount *mp = ap->ip->i_mount; |
| 2556 | struct xfs_perag *pag; |
| 2557 | xfs_agnumber_t ag, startag; |
| 2558 | int notinit = 0; |
| 2559 | int error; |
| 2560 | |
| 2561 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2562 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2563 | else |
| 2564 | args->type = XFS_ALLOCTYPE_START_BNO; |
| 2565 | args->total = ap->total; |
| 2566 | |
| 2567 | /* |
| 2568 | * Search for an allocation group with a single extent large enough |
| 2569 | * for the request. If one isn't found, then adjust the minimum |
| 2570 | * allocation size to the largest space found. |
| 2571 | */ |
| 2572 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2573 | if (startag == NULLAGNUMBER) |
| 2574 | startag = ag = 0; |
| 2575 | |
| 2576 | pag = xfs_perag_get(mp, ag); |
| 2577 | while (*blen < ap->alen) { |
| 2578 | if (!pag->pagf_init) { |
| 2579 | error = xfs_alloc_pagf_init(mp, args->tp, ag, |
| 2580 | XFS_ALLOC_FLAG_TRYLOCK); |
| 2581 | if (error) { |
| 2582 | xfs_perag_put(pag); |
| 2583 | return error; |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | /* |
| 2588 | * See xfs_alloc_fix_freelist... |
| 2589 | */ |
| 2590 | if (pag->pagf_init) { |
| 2591 | xfs_extlen_t longest; |
| 2592 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 2593 | if (*blen < longest) |
| 2594 | *blen = longest; |
| 2595 | } else |
| 2596 | notinit = 1; |
| 2597 | |
| 2598 | if (xfs_inode_is_filestream(ap->ip)) { |
| 2599 | if (*blen >= ap->alen) |
| 2600 | break; |
| 2601 | |
| 2602 | if (ap->userdata) { |
| 2603 | /* |
| 2604 | * If startag is an invalid AG, we've |
| 2605 | * come here once before and |
| 2606 | * xfs_filestream_new_ag picked the |
| 2607 | * best currently available. |
| 2608 | * |
| 2609 | * Don't continue looping, since we |
| 2610 | * could loop forever. |
| 2611 | */ |
| 2612 | if (startag == NULLAGNUMBER) |
| 2613 | break; |
| 2614 | |
| 2615 | error = xfs_filestream_new_ag(ap, &ag); |
| 2616 | xfs_perag_put(pag); |
| 2617 | if (error) |
| 2618 | return error; |
| 2619 | |
| 2620 | /* loop again to set 'blen'*/ |
| 2621 | startag = NULLAGNUMBER; |
| 2622 | pag = xfs_perag_get(mp, ag); |
| 2623 | continue; |
| 2624 | } |
| 2625 | } |
| 2626 | if (++ag == mp->m_sb.sb_agcount) |
| 2627 | ag = 0; |
| 2628 | if (ag == startag) |
| 2629 | break; |
| 2630 | xfs_perag_put(pag); |
| 2631 | pag = xfs_perag_get(mp, ag); |
| 2632 | } |
| 2633 | xfs_perag_put(pag); |
| 2634 | |
| 2635 | /* |
| 2636 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2637 | * possible that there is space for this request. |
| 2638 | */ |
| 2639 | if (notinit || *blen < ap->minlen) |
| 2640 | args->minlen = ap->minlen; |
| 2641 | /* |
| 2642 | * If the best seen length is less than the request |
| 2643 | * length, use the best as the minimum. |
| 2644 | */ |
| 2645 | else if (*blen < ap->alen) |
| 2646 | args->minlen = *blen; |
| 2647 | /* |
| 2648 | * Otherwise we've seen an extent as big as alen, |
| 2649 | * use that as the minimum. |
| 2650 | */ |
| 2651 | else |
| 2652 | args->minlen = ap->alen; |
| 2653 | |
| 2654 | /* |
| 2655 | * set the failure fallback case to look in the selected |
| 2656 | * AG as the stream may have moved. |
| 2657 | */ |
| 2658 | if (xfs_inode_is_filestream(ap->ip)) |
| 2659 | ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2660 | |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2665 | xfs_bmap_btalloc( |
| 2666 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2667 | { |
| 2668 | xfs_mount_t *mp; /* mount point structure */ |
| 2669 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2670 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2671 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2672 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2673 | xfs_alloc_arg_t args; |
| 2674 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2675 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2676 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2677 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2678 | int tryagain; |
| 2679 | int error; |
| 2680 | |
| 2681 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2682 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2683 | if (unlikely(align)) { |
| 2684 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2685 | align, 0, ap->eof, 0, ap->conv, |
| 2686 | &ap->off, &ap->alen); |
| 2687 | ASSERT(!error); |
| 2688 | ASSERT(ap->alen); |
| 2689 | } |
| 2690 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2691 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2692 | if (nullfb) { |
| 2693 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2694 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2695 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
| 2696 | ap->rval = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2697 | } else { |
| 2698 | ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
| 2699 | } |
| 2700 | } else |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2701 | ap->rval = ap->firstblock; |
| 2702 | |
| 2703 | xfs_bmap_adjacent(ap); |
| 2704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | /* |
| 2706 | * If allowed, use ap->rval; otherwise must use firstblock since |
| 2707 | * it's in the right allocation group. |
| 2708 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2709 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | ; |
| 2711 | else |
| 2712 | ap->rval = ap->firstblock; |
| 2713 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | * Normal allocation, done through xfs_alloc_vextent. |
| 2715 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2716 | tryagain = isaligned = 0; |
| 2717 | args.tp = ap->tp; |
| 2718 | args.mp = mp; |
| 2719 | args.fsbno = ap->rval; |
| 2720 | args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks); |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2721 | args.firstblock = ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2722 | blen = 0; |
| 2723 | if (nullfb) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2724 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
| 2725 | if (error) |
| 2726 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2727 | } else if (ap->low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2728 | if (xfs_inode_is_filestream(ap->ip)) |
| 2729 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2730 | else |
| 2731 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2732 | args.total = args.minlen = ap->minlen; |
| 2733 | } else { |
| 2734 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2735 | args.total = ap->total; |
| 2736 | args.minlen = ap->minlen; |
| 2737 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2738 | /* apply extent size hints if obtained earlier */ |
| 2739 | if (unlikely(align)) { |
| 2740 | args.prod = align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2741 | if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) |
| 2742 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2743 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2744 | args.prod = 1; |
| 2745 | args.mod = 0; |
| 2746 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2747 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2748 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) |
| 2749 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2750 | } |
| 2751 | /* |
| 2752 | * If we are not low on available data blocks, and the |
| 2753 | * underlying logical volume manager is a stripe, and |
| 2754 | * the file offset is zero then try to allocate data |
| 2755 | * blocks on stripe unit boundary. |
| 2756 | * NOTE: ap->aeof is only set if the allocation length |
| 2757 | * is >= the stripe unit and the allocation offset is |
| 2758 | * at the end of file. |
| 2759 | */ |
| 2760 | if (!ap->low && ap->aeof) { |
| 2761 | if (!ap->off) { |
| 2762 | args.alignment = mp->m_dalign; |
| 2763 | atype = args.type; |
| 2764 | isaligned = 1; |
| 2765 | /* |
| 2766 | * Adjust for alignment |
| 2767 | */ |
| 2768 | if (blen > args.alignment && blen <= ap->alen) |
| 2769 | args.minlen = blen - args.alignment; |
| 2770 | args.minalignslop = 0; |
| 2771 | } else { |
| 2772 | /* |
| 2773 | * First try an exact bno allocation. |
| 2774 | * If it fails then do a near or start bno |
| 2775 | * allocation with alignment turned on. |
| 2776 | */ |
| 2777 | atype = args.type; |
| 2778 | tryagain = 1; |
| 2779 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2780 | args.alignment = 1; |
| 2781 | /* |
| 2782 | * Compute the minlen+alignment for the |
| 2783 | * next case. Set slop so that the value |
| 2784 | * of minlen+alignment+slop doesn't go up |
| 2785 | * between the calls. |
| 2786 | */ |
| 2787 | if (blen > mp->m_dalign && blen <= ap->alen) |
| 2788 | nextminlen = blen - mp->m_dalign; |
| 2789 | else |
| 2790 | nextminlen = args.minlen; |
| 2791 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2792 | args.minalignslop = |
| 2793 | nextminlen + mp->m_dalign - |
| 2794 | args.minlen - 1; |
| 2795 | else |
| 2796 | args.minalignslop = 0; |
| 2797 | } |
| 2798 | } else { |
| 2799 | args.alignment = 1; |
| 2800 | args.minalignslop = 0; |
| 2801 | } |
| 2802 | args.minleft = ap->minleft; |
| 2803 | args.wasdel = ap->wasdel; |
| 2804 | args.isfl = 0; |
| 2805 | args.userdata = ap->userdata; |
| 2806 | if ((error = xfs_alloc_vextent(&args))) |
| 2807 | return error; |
| 2808 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2809 | /* |
| 2810 | * Exact allocation failed. Now try with alignment |
| 2811 | * turned on. |
| 2812 | */ |
| 2813 | args.type = atype; |
| 2814 | args.fsbno = ap->rval; |
| 2815 | args.alignment = mp->m_dalign; |
| 2816 | args.minlen = nextminlen; |
| 2817 | args.minalignslop = 0; |
| 2818 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | if ((error = xfs_alloc_vextent(&args))) |
| 2820 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2821 | } |
| 2822 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2823 | /* |
| 2824 | * allocation failed, so turn off alignment and |
| 2825 | * try again. |
| 2826 | */ |
| 2827 | args.type = atype; |
| 2828 | args.fsbno = ap->rval; |
| 2829 | args.alignment = 0; |
| 2830 | if ((error = xfs_alloc_vextent(&args))) |
| 2831 | return error; |
| 2832 | } |
| 2833 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2834 | args.minlen > ap->minlen) { |
| 2835 | args.minlen = ap->minlen; |
| 2836 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2837 | args.fsbno = ap->rval; |
| 2838 | if ((error = xfs_alloc_vextent(&args))) |
| 2839 | return error; |
| 2840 | } |
| 2841 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2842 | args.fsbno = 0; |
| 2843 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2844 | args.total = ap->minlen; |
| 2845 | args.minleft = 0; |
| 2846 | if ((error = xfs_alloc_vextent(&args))) |
| 2847 | return error; |
| 2848 | ap->low = 1; |
| 2849 | } |
| 2850 | if (args.fsbno != NULLFSBLOCK) { |
| 2851 | ap->firstblock = ap->rval = args.fsbno; |
| 2852 | ASSERT(nullfb || fb_agno == args.agno || |
| 2853 | (ap->low && fb_agno < args.agno)); |
| 2854 | ap->alen = args.len; |
| 2855 | ap->ip->i_d.di_nblocks += args.len; |
| 2856 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2857 | if (ap->wasdel) |
| 2858 | ap->ip->i_delayed_blks -= args.len; |
| 2859 | /* |
| 2860 | * Adjust the disk quota also. This was reserved |
| 2861 | * earlier. |
| 2862 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2863 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2864 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2865 | XFS_TRANS_DQ_BCOUNT, |
| 2866 | (long) args.len); |
| 2867 | } else { |
| 2868 | ap->rval = NULLFSBLOCK; |
| 2869 | ap->alen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | } |
| 2871 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | /* |
| 2875 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2876 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2877 | */ |
| 2878 | STATIC int |
| 2879 | xfs_bmap_alloc( |
| 2880 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2881 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2882 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2883 | return xfs_bmap_rtalloc(ap); |
| 2884 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * Transform a btree format file with only one leaf node, where the |
| 2889 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2890 | * 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] | 2891 | * give up the space for the btree root and pitch the leaf block. |
| 2892 | */ |
| 2893 | STATIC int /* error */ |
| 2894 | xfs_bmap_btree_to_extents( |
| 2895 | xfs_trans_t *tp, /* transaction pointer */ |
| 2896 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2897 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2898 | int *logflagsp, /* inode logging flags */ |
| 2899 | int whichfork) /* data or attr fork */ |
| 2900 | { |
| 2901 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2902 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | xfs_fsblock_t cbno; /* child block number */ |
| 2904 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2905 | int error; /* error return value */ |
| 2906 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2907 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2908 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2909 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2911 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2913 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2914 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2915 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2916 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2917 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2918 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2919 | 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] | 2920 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | *logflagsp = 0; |
| 2922 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2923 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | return error; |
| 2925 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2927 | XFS_BMAP_BTREE_REF))) |
| 2928 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2929 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2930 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | return error; |
| 2932 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2933 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2934 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | xfs_trans_binval(tp, cbp); |
| 2936 | if (cur->bc_bufs[0] == cbp) |
| 2937 | cur->bc_bufs[0] = NULL; |
| 2938 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2939 | ASSERT(ifp->if_broot == NULL); |
| 2940 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2941 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2942 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | return 0; |
| 2944 | } |
| 2945 | |
| 2946 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2947 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | * after removing space (or undoing a delayed allocation). |
| 2949 | */ |
| 2950 | STATIC int /* error */ |
| 2951 | xfs_bmap_del_extent( |
| 2952 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2953 | xfs_trans_t *tp, /* current transaction pointer */ |
| 2954 | xfs_extnum_t idx, /* extent number to update/delete */ |
| 2955 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2956 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2957 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | int *logflagsp, /* inode logging flags */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 2959 | xfs_extdelta_t *delta, /* Change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | int whichfork, /* data or attr fork */ |
| 2961 | int rsvd) /* OK to allocate reserved blocks */ |
| 2962 | { |
| 2963 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2964 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2965 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2966 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2967 | int delay; /* current block is delayed allocated */ |
| 2968 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2969 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | int error; /* error return value */ |
| 2971 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2973 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2974 | int i; /* temp state */ |
| 2975 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2976 | xfs_mount_t *mp; /* mount structure */ |
| 2977 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2978 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2979 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | uint qfield; /* quota field to update */ |
| 2981 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2982 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2983 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | |
| 2985 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2986 | |
| 2987 | if (whichfork == XFS_ATTR_FORK) |
| 2988 | state |= BMAP_ATTRFORK; |
| 2989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | mp = ip->i_mount; |
| 2991 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2992 | ASSERT((idx >= 0) && (idx < ifp->if_bytes / |
| 2993 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | ASSERT(del->br_blockcount > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2995 | ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | xfs_bmbt_get_all(ep, &got); |
| 2997 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2998 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2999 | got_endoff = got.br_startoff + got.br_blockcount; |
| 3000 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3001 | delay = isnullstartblock(got.br_startblock); |
| 3002 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | flags = 0; |
| 3004 | qfield = 0; |
| 3005 | error = 0; |
| 3006 | /* |
| 3007 | * If deleting a real allocation, must free up the disk space. |
| 3008 | */ |
| 3009 | if (!delay) { |
| 3010 | flags = XFS_ILOG_CORE; |
| 3011 | /* |
| 3012 | * Realtime allocation. Free it and record di_nblocks update. |
| 3013 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 3014 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | xfs_fsblock_t bno; |
| 3016 | xfs_filblks_t len; |
| 3017 | |
| 3018 | ASSERT(do_mod(del->br_blockcount, |
| 3019 | mp->m_sb.sb_rextsize) == 0); |
| 3020 | ASSERT(do_mod(del->br_startblock, |
| 3021 | mp->m_sb.sb_rextsize) == 0); |
| 3022 | bno = del->br_startblock; |
| 3023 | len = del->br_blockcount; |
| 3024 | do_div(bno, mp->m_sb.sb_rextsize); |
| 3025 | do_div(len, mp->m_sb.sb_rextsize); |
| 3026 | if ((error = xfs_rtfree_extent(ip->i_transp, bno, |
| 3027 | (xfs_extlen_t)len))) |
| 3028 | goto done; |
| 3029 | do_fx = 0; |
| 3030 | nblks = len * mp->m_sb.sb_rextsize; |
| 3031 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 3032 | } |
| 3033 | /* |
| 3034 | * Ordinary allocation. |
| 3035 | */ |
| 3036 | else { |
| 3037 | do_fx = 1; |
| 3038 | nblks = del->br_blockcount; |
| 3039 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 3040 | } |
| 3041 | /* |
| 3042 | * Set up del_endblock and cur for later. |
| 3043 | */ |
| 3044 | del_endblock = del->br_startblock + del->br_blockcount; |
| 3045 | if (cur) { |
| 3046 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 3047 | got.br_startblock, got.br_blockcount, |
| 3048 | &i))) |
| 3049 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3050 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | } |
| 3052 | da_old = da_new = 0; |
| 3053 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3054 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | da_new = 0; |
| 3056 | nblks = 0; |
| 3057 | do_fx = 0; |
| 3058 | } |
| 3059 | /* |
| 3060 | * Set flag value to use in switch statement. |
| 3061 | * Left-contig is 2, right-contig is 1. |
| 3062 | */ |
| 3063 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 3064 | (got_endoff == del_endoff)) { |
| 3065 | case 3: |
| 3066 | /* |
| 3067 | * Matches the whole extent. Delete the entry. |
| 3068 | */ |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 3069 | xfs_iext_remove(ip, idx, 1, |
| 3070 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | ifp->if_lastex = idx; |
| 3072 | if (delay) |
| 3073 | break; |
| 3074 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3075 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 3076 | flags |= XFS_ILOG_CORE; |
| 3077 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3078 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | break; |
| 3080 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3081 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3083 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | break; |
| 3085 | |
| 3086 | case 2: |
| 3087 | /* |
| 3088 | * Deleting the first part of the extent. |
| 3089 | */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3090 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 3092 | temp = got.br_blockcount - del->br_blockcount; |
| 3093 | xfs_bmbt_set_blockcount(ep, temp); |
| 3094 | ifp->if_lastex = idx; |
| 3095 | if (delay) { |
| 3096 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3097 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3098 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3099 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | da_new = temp; |
| 3101 | break; |
| 3102 | } |
| 3103 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3104 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3106 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | break; |
| 3108 | } |
| 3109 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 3110 | got.br_blockcount - del->br_blockcount, |
| 3111 | got.br_state))) |
| 3112 | goto done; |
| 3113 | break; |
| 3114 | |
| 3115 | case 1: |
| 3116 | /* |
| 3117 | * Deleting the last part of the extent. |
| 3118 | */ |
| 3119 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3120 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | xfs_bmbt_set_blockcount(ep, temp); |
| 3122 | ifp->if_lastex = idx; |
| 3123 | if (delay) { |
| 3124 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 3125 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3126 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3127 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | da_new = temp; |
| 3129 | break; |
| 3130 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3131 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3133 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | break; |
| 3135 | } |
| 3136 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 3137 | got.br_startblock, |
| 3138 | got.br_blockcount - del->br_blockcount, |
| 3139 | got.br_state))) |
| 3140 | goto done; |
| 3141 | break; |
| 3142 | |
| 3143 | case 0: |
| 3144 | /* |
| 3145 | * Deleting the middle of the extent. |
| 3146 | */ |
| 3147 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3148 | trace_xfs_bmap_pre_update(ip, idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | xfs_bmbt_set_blockcount(ep, temp); |
| 3150 | new.br_startoff = del_endoff; |
| 3151 | temp2 = got_endoff - del_endoff; |
| 3152 | new.br_blockcount = temp2; |
| 3153 | new.br_state = got.br_state; |
| 3154 | if (!delay) { |
| 3155 | new.br_startblock = del_endblock; |
| 3156 | flags |= XFS_ILOG_CORE; |
| 3157 | if (cur) { |
| 3158 | if ((error = xfs_bmbt_update(cur, |
| 3159 | got.br_startoff, |
| 3160 | got.br_startblock, temp, |
| 3161 | got.br_state))) |
| 3162 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 3163 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | goto done; |
| 3165 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3166 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | if (error && error != ENOSPC) |
| 3168 | goto done; |
| 3169 | /* |
| 3170 | * If get no-space back from btree insert, |
| 3171 | * it tried a split, and we have a zero |
| 3172 | * block reservation. |
| 3173 | * Fix up our state and return the error. |
| 3174 | */ |
| 3175 | if (error == ENOSPC) { |
| 3176 | /* |
| 3177 | * Reset the cursor, don't trust |
| 3178 | * it after any insert operation. |
| 3179 | */ |
| 3180 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 3181 | got.br_startoff, |
| 3182 | got.br_startblock, |
| 3183 | temp, &i))) |
| 3184 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3185 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | /* |
| 3187 | * Update the btree record back |
| 3188 | * to the original value. |
| 3189 | */ |
| 3190 | if ((error = xfs_bmbt_update(cur, |
| 3191 | got.br_startoff, |
| 3192 | got.br_startblock, |
| 3193 | got.br_blockcount, |
| 3194 | got.br_state))) |
| 3195 | goto done; |
| 3196 | /* |
| 3197 | * Reset the extent record back |
| 3198 | * to the original value. |
| 3199 | */ |
| 3200 | xfs_bmbt_set_blockcount(ep, |
| 3201 | got.br_blockcount); |
| 3202 | flags = 0; |
| 3203 | error = XFS_ERROR(ENOSPC); |
| 3204 | goto done; |
| 3205 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3206 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3208 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3210 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3211 | } else { |
| 3212 | ASSERT(whichfork == XFS_DATA_FORK); |
| 3213 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3214 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3216 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | da_new = temp + temp2; |
| 3218 | while (da_new > da_old) { |
| 3219 | if (temp) { |
| 3220 | temp--; |
| 3221 | da_new--; |
| 3222 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3223 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3224 | } |
| 3225 | if (da_new == da_old) |
| 3226 | break; |
| 3227 | if (temp2) { |
| 3228 | temp2--; |
| 3229 | da_new--; |
| 3230 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3231 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | } |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3235 | trace_xfs_bmap_post_update(ip, idx, state, _THIS_IP_); |
| 3236 | xfs_iext_insert(ip, idx + 1, 1, &new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | ifp->if_lastex = idx + 1; |
| 3238 | break; |
| 3239 | } |
| 3240 | /* |
| 3241 | * If we need to, add to list of extents to delete. |
| 3242 | */ |
| 3243 | if (do_fx) |
| 3244 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 3245 | mp); |
| 3246 | /* |
| 3247 | * Adjust inode # blocks in the file. |
| 3248 | */ |
| 3249 | if (nblks) |
| 3250 | ip->i_d.di_nblocks -= nblks; |
| 3251 | /* |
| 3252 | * Adjust quota data. |
| 3253 | */ |
| 3254 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3255 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | |
| 3257 | /* |
| 3258 | * Account for change in delayed indirect blocks. |
| 3259 | * Nothing to do for disk quota accounting here. |
| 3260 | */ |
| 3261 | ASSERT(da_old >= da_new); |
| 3262 | if (da_old > da_new) |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 3263 | 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] | 3264 | rsvd); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 3265 | if (delta) { |
| 3266 | /* DELTA: report the original extent. */ |
| 3267 | if (delta->xed_startoff > got.br_startoff) |
| 3268 | delta->xed_startoff = got.br_startoff; |
| 3269 | if (delta->xed_blockcount < got.br_startoff+got.br_blockcount) |
| 3270 | delta->xed_blockcount = got.br_startoff + |
| 3271 | got.br_blockcount; |
| 3272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | done: |
| 3274 | *logflagsp = flags; |
| 3275 | return error; |
| 3276 | } |
| 3277 | |
| 3278 | /* |
| 3279 | * Remove the entry "free" from the free item list. Prev points to the |
| 3280 | * previous entry, unless "free" is the head of the list. |
| 3281 | */ |
| 3282 | STATIC void |
| 3283 | xfs_bmap_del_free( |
| 3284 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3285 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3286 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3287 | { |
| 3288 | if (prev) |
| 3289 | prev->xbfi_next = free->xbfi_next; |
| 3290 | else |
| 3291 | flist->xbf_first = free->xbfi_next; |
| 3292 | flist->xbf_count--; |
| 3293 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3294 | } |
| 3295 | |
| 3296 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | * Convert an extents-format file into a btree-format file. |
| 3298 | * The new file will have a root block (in the inode) and a single child block. |
| 3299 | */ |
| 3300 | STATIC int /* error */ |
| 3301 | xfs_bmap_extents_to_btree( |
| 3302 | xfs_trans_t *tp, /* transaction pointer */ |
| 3303 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3304 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3305 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3306 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3307 | int wasdel, /* converting a delayed alloc */ |
| 3308 | int *logflagsp, /* inode logging flags */ |
| 3309 | int whichfork) /* data or attr fork */ |
| 3310 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3311 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3313 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3314 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3315 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3317 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3319 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3321 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3322 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3323 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3325 | |
| 3326 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3327 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3328 | ASSERT(ifp->if_ext_max == |
| 3329 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3330 | /* |
| 3331 | * Make space in the inode incore. |
| 3332 | */ |
| 3333 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3334 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3336 | /* |
| 3337 | * Fill in the root. |
| 3338 | */ |
| 3339 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3340 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3341 | block->bb_level = cpu_to_be16(1); |
| 3342 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3343 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3344 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3346 | /* |
| 3347 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3348 | */ |
| 3349 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3350 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | cur->bc_private.b.firstblock = *firstblock; |
| 3352 | cur->bc_private.b.flist = flist; |
| 3353 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3354 | /* |
| 3355 | * Convert to a btree with two levels, one record in root. |
| 3356 | */ |
| 3357 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3358 | args.tp = tp; |
| 3359 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3360 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | if (*firstblock == NULLFSBLOCK) { |
| 3362 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3363 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3364 | } else if (flist->xbf_low) { |
| 3365 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3366 | args.fsbno = *firstblock; |
| 3367 | } else { |
| 3368 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3369 | args.fsbno = *firstblock; |
| 3370 | } |
| 3371 | args.minlen = args.maxlen = args.prod = 1; |
| 3372 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3373 | args.minalignslop = 0; |
| 3374 | args.wasdel = wasdel; |
| 3375 | *logflagsp = 0; |
| 3376 | if ((error = xfs_alloc_vextent(&args))) { |
| 3377 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3378 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3379 | return error; |
| 3380 | } |
| 3381 | /* |
| 3382 | * Allocation can't fail, the space was reserved. |
| 3383 | */ |
| 3384 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3385 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3386 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3387 | (flist->xbf_low && |
| 3388 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3389 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3390 | cur->bc_private.b.allocated++; |
| 3391 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3392 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3394 | /* |
| 3395 | * Fill in the child block. |
| 3396 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3397 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3398 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3400 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3401 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3402 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3404 | for (cnt = i = 0; i < nextents; i++) { |
| 3405 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3406 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3407 | arp->l0 = cpu_to_be64(ep->l0); |
| 3408 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | arp++; cnt++; |
| 3410 | } |
| 3411 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3412 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3413 | xfs_btree_set_numrecs(ablock, cnt); |
| 3414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | /* |
| 3416 | * Fill in the root key and pointer. |
| 3417 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3418 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3419 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3420 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3421 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3422 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3423 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | /* |
| 3426 | * Do all this logging at the end so that |
| 3427 | * the root is at the right level. |
| 3428 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3429 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3430 | 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] | 3431 | ASSERT(*curp == NULL); |
| 3432 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3433 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | return 0; |
| 3435 | } |
| 3436 | |
| 3437 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3438 | * Calculate the default attribute fork offset for newly created inodes. |
| 3439 | */ |
| 3440 | uint |
| 3441 | xfs_default_attroffset( |
| 3442 | struct xfs_inode *ip) |
| 3443 | { |
| 3444 | struct xfs_mount *mp = ip->i_mount; |
| 3445 | uint offset; |
| 3446 | |
| 3447 | if (mp->m_sb.sb_inodesize == 256) { |
| 3448 | offset = XFS_LITINO(mp) - |
| 3449 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3450 | } else { |
| 3451 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3452 | } |
| 3453 | |
| 3454 | ASSERT(offset < XFS_LITINO(mp)); |
| 3455 | return offset; |
| 3456 | } |
| 3457 | |
| 3458 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3459 | * Helper routine to reset inode di_forkoff field when switching |
| 3460 | * attribute fork from local to extent format - we reset it where |
| 3461 | * possible to make space available for inline data fork extents. |
| 3462 | */ |
| 3463 | STATIC void |
| 3464 | xfs_bmap_forkoff_reset( |
| 3465 | xfs_mount_t *mp, |
| 3466 | xfs_inode_t *ip, |
| 3467 | int whichfork) |
| 3468 | { |
| 3469 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3470 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3471 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3472 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3473 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3474 | |
| 3475 | if (dfl_forkoff > ip->i_d.di_forkoff) { |
| 3476 | ip->i_d.di_forkoff = dfl_forkoff; |
| 3477 | ip->i_df.if_ext_max = |
| 3478 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3479 | ip->i_afp->if_ext_max = |
| 3480 | XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3481 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | * Convert a local file to an extents file. |
| 3487 | * This code is out of bounds for data forks of regular files, |
| 3488 | * since the file data needs to get logged so things will stay consistent. |
| 3489 | * (The bmap-level manipulations are ok, though). |
| 3490 | */ |
| 3491 | STATIC int /* error */ |
| 3492 | xfs_bmap_local_to_extents( |
| 3493 | xfs_trans_t *tp, /* transaction pointer */ |
| 3494 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3495 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3496 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3497 | int *logflagsp, /* inode logging flags */ |
| 3498 | int whichfork) /* data or attr fork */ |
| 3499 | { |
| 3500 | int error; /* error return value */ |
| 3501 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3503 | |
| 3504 | /* |
| 3505 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3506 | * So sending the data fork of a regular inode is invalid. |
| 3507 | */ |
| 3508 | ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG && |
| 3509 | whichfork == XFS_DATA_FORK)); |
| 3510 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3511 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3512 | flags = 0; |
| 3513 | error = 0; |
| 3514 | if (ifp->if_bytes) { |
| 3515 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3516 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3517 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | |
| 3519 | args.tp = tp; |
| 3520 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3521 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3522 | ASSERT((ifp->if_flags & |
| 3523 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | /* |
| 3525 | * Allocate a block. We know we need only one, since the |
| 3526 | * file currently fits in an inode. |
| 3527 | */ |
| 3528 | if (*firstblock == NULLFSBLOCK) { |
| 3529 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3530 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3531 | } else { |
| 3532 | args.fsbno = *firstblock; |
| 3533 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3534 | } |
| 3535 | args.total = total; |
| 3536 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3537 | args.isfl = args.minalignslop = 0; |
| 3538 | args.minlen = args.maxlen = args.prod = 1; |
| 3539 | if ((error = xfs_alloc_vextent(&args))) |
| 3540 | goto done; |
| 3541 | /* |
| 3542 | * Can't fail, the space was reserved. |
| 3543 | */ |
| 3544 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3545 | ASSERT(args.len == 1); |
| 3546 | *firstblock = args.fsbno; |
| 3547 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 3548 | memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data, |
| 3549 | ifp->if_bytes); |
| 3550 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3551 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3553 | xfs_iext_add(ifp, 0, 1); |
| 3554 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3556 | trace_xfs_bmap_post_update(ip, 0, |
| 3557 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3558 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3560 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3561 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3563 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3564 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3565 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3566 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3569 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3570 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3571 | flags |= XFS_ILOG_CORE; |
| 3572 | done: |
| 3573 | *logflagsp = flags; |
| 3574 | return error; |
| 3575 | } |
| 3576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3578 | * Search the extent records for the entry containing block bno. |
| 3579 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3580 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3581 | * entry (null if none). Else, *lastxp will be set to the index |
| 3582 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3583 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3584 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3585 | xfs_bmap_search_multi_extents( |
| 3586 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3587 | xfs_fileoff_t bno, /* block number searched for */ |
| 3588 | int *eofp, /* out: end of file found */ |
| 3589 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3590 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3591 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3592 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3593 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3594 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3595 | |
| 3596 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3597 | * Initialize the extent entry structure to catch access to |
| 3598 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3599 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3600 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3601 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3602 | gotp->br_state = XFS_EXT_INVALID; |
| 3603 | #if XFS_BIG_BLKNOS |
| 3604 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3605 | #else |
| 3606 | gotp->br_startblock = 0xffffa5a5; |
| 3607 | #endif |
| 3608 | prevp->br_startoff = NULLFILEOFF; |
| 3609 | |
| 3610 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3611 | if (lastx > 0) { |
| 3612 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3613 | } |
| 3614 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3615 | xfs_bmbt_get_all(ep, gotp); |
| 3616 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3617 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3618 | if (lastx > 0) { |
| 3619 | *gotp = *prevp; |
| 3620 | } |
| 3621 | *eofp = 1; |
| 3622 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3623 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3624 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3625 | return ep; |
| 3626 | } |
| 3627 | |
| 3628 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | * Search the extents list for the inode, for the extent containing bno. |
| 3630 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3631 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3632 | * Else, *lastxp will be set to the index of the found |
| 3633 | * entry; *gotp will contain the entry. |
| 3634 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3635 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | xfs_bmap_search_extents( |
| 3637 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3638 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3639 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | int *eofp, /* out: end of file found */ |
| 3641 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3642 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3643 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3644 | { |
| 3645 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3646 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | |
| 3648 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3649 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3651 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3652 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3653 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3654 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
| 3655 | xfs_cmn_err(XFS_PTAG_FSBLOCK_ZERO, CE_ALERT, ip->i_mount, |
| 3656 | "Access to block zero in inode %llu " |
| 3657 | "start_block: %llx start_off: %llx " |
| 3658 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3659 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3660 | (unsigned long long)gotp->br_startblock, |
| 3661 | (unsigned long long)gotp->br_startoff, |
| 3662 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3663 | gotp->br_state, *lastxp); |
| 3664 | *lastxp = NULLEXTNUM; |
| 3665 | *eofp = 1; |
| 3666 | return NULL; |
| 3667 | } |
| 3668 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | /* |
| 3672 | * Compute the worst-case number of indirect blocks that will be used |
| 3673 | * for ip's delayed extent of length "len". |
| 3674 | */ |
| 3675 | STATIC xfs_filblks_t |
| 3676 | xfs_bmap_worst_indlen( |
| 3677 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3678 | xfs_filblks_t len) /* delayed extent length */ |
| 3679 | { |
| 3680 | int level; /* btree level number */ |
| 3681 | int maxrecs; /* maximum record count at this level */ |
| 3682 | xfs_mount_t *mp; /* mount structure */ |
| 3683 | xfs_filblks_t rval; /* return value */ |
| 3684 | |
| 3685 | mp = ip->i_mount; |
| 3686 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3687 | for (level = 0, rval = 0; |
| 3688 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3689 | level++) { |
| 3690 | len += maxrecs - 1; |
| 3691 | do_div(len, maxrecs); |
| 3692 | rval += len; |
| 3693 | if (len == 1) |
| 3694 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3695 | level - 1; |
| 3696 | if (level == 0) |
| 3697 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3698 | } |
| 3699 | return rval; |
| 3700 | } |
| 3701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | /* |
| 3703 | * Convert inode from non-attributed to attributed. |
| 3704 | * Must not be in a transaction, ip must not be locked. |
| 3705 | */ |
| 3706 | int /* error code */ |
| 3707 | xfs_bmap_add_attrfork( |
| 3708 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3709 | int size, /* space new attribute needs */ |
| 3710 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3712 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3713 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3716 | int blks; /* space reservation */ |
| 3717 | int version = 1; /* superblock attr version */ |
| 3718 | int committed; /* xaction was committed */ |
| 3719 | int logflags; /* logging flags */ |
| 3720 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3722 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | ASSERT(ip->i_df.if_ext_max == |
| 3724 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | mp = ip->i_mount; |
| 3727 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3728 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3729 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3730 | if (rsvd) |
| 3731 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3732 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3733 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3734 | goto error0; |
| 3735 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3736 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3738 | XFS_QMOPT_RES_REGBLKS); |
| 3739 | if (error) { |
| 3740 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3741 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3742 | return error; |
| 3743 | } |
| 3744 | if (XFS_IFORK_Q(ip)) |
| 3745 | goto error1; |
| 3746 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3747 | /* |
| 3748 | * For inodes coming from pre-6.2 filesystems. |
| 3749 | */ |
| 3750 | ASSERT(ip->i_d.di_aformat == 0); |
| 3751 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3752 | } |
| 3753 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 0b1f9177 | 2008-08-13 16:13:09 +1000 | [diff] [blame] | 3754 | IHOLD(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 3756 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
| 3757 | switch (ip->i_d.di_format) { |
| 3758 | case XFS_DINODE_FMT_DEV: |
| 3759 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3760 | break; |
| 3761 | case XFS_DINODE_FMT_UUID: |
| 3762 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3763 | break; |
| 3764 | case XFS_DINODE_FMT_LOCAL: |
| 3765 | case XFS_DINODE_FMT_EXTENTS: |
| 3766 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3767 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3768 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3769 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3770 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3771 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | break; |
| 3773 | default: |
| 3774 | ASSERT(0); |
| 3775 | error = XFS_ERROR(EINVAL); |
| 3776 | goto error1; |
| 3777 | } |
| 3778 | ip->i_df.if_ext_max = |
| 3779 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3780 | ASSERT(ip->i_afp == NULL); |
| 3781 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 3782 | ip->i_afp->if_ext_max = |
| 3783 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3784 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3785 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3786 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | switch (ip->i_d.di_format) { |
| 3788 | case XFS_DINODE_FMT_LOCAL: |
| 3789 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3790 | &logflags); |
| 3791 | break; |
| 3792 | case XFS_DINODE_FMT_EXTENTS: |
| 3793 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3794 | &flist, &logflags); |
| 3795 | break; |
| 3796 | case XFS_DINODE_FMT_BTREE: |
| 3797 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3798 | &logflags); |
| 3799 | break; |
| 3800 | default: |
| 3801 | error = 0; |
| 3802 | break; |
| 3803 | } |
| 3804 | if (logflags) |
| 3805 | xfs_trans_log_inode(tp, ip, logflags); |
| 3806 | if (error) |
| 3807 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3808 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3809 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3810 | __int64_t sbfields = 0; |
| 3811 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3812 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3813 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3814 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3815 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3816 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3817 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3818 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3819 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3820 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3821 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3822 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3823 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3825 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3827 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | goto error2; |
Dave Chinner | 713bf88 | 2010-03-08 11:26:23 +1100 | [diff] [blame] | 3829 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | ASSERT(ip->i_df.if_ext_max == |
| 3831 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3832 | return error; |
| 3833 | error2: |
| 3834 | xfs_bmap_cancel(&flist); |
| 3835 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3837 | error0: |
| 3838 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 3839 | ASSERT(ip->i_df.if_ext_max == |
| 3840 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3841 | return error; |
| 3842 | } |
| 3843 | |
| 3844 | /* |
| 3845 | * Add the extent to the list of extents to be free at transaction end. |
| 3846 | * The list is maintained sorted (by block number). |
| 3847 | */ |
| 3848 | /* ARGSUSED */ |
| 3849 | void |
| 3850 | xfs_bmap_add_free( |
| 3851 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3852 | xfs_filblks_t len, /* length of extent */ |
| 3853 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3854 | xfs_mount_t *mp) /* mount point structure */ |
| 3855 | { |
| 3856 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3857 | xfs_bmap_free_item_t *new; /* new element */ |
| 3858 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3859 | #ifdef DEBUG |
| 3860 | xfs_agnumber_t agno; |
| 3861 | xfs_agblock_t agbno; |
| 3862 | |
| 3863 | ASSERT(bno != NULLFSBLOCK); |
| 3864 | ASSERT(len > 0); |
| 3865 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3866 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3868 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3869 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3870 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3871 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3872 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3873 | #endif |
| 3874 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3875 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3876 | new->xbfi_startblock = bno; |
| 3877 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3878 | for (prev = NULL, cur = flist->xbf_first; |
| 3879 | cur != NULL; |
| 3880 | prev = cur, cur = cur->xbfi_next) { |
| 3881 | if (cur->xbfi_startblock >= bno) |
| 3882 | break; |
| 3883 | } |
| 3884 | if (prev) |
| 3885 | prev->xbfi_next = new; |
| 3886 | else |
| 3887 | flist->xbf_first = new; |
| 3888 | new->xbfi_next = cur; |
| 3889 | flist->xbf_count++; |
| 3890 | } |
| 3891 | |
| 3892 | /* |
| 3893 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3894 | * in this filesystem. Done once, during mount. |
| 3895 | */ |
| 3896 | void |
| 3897 | xfs_bmap_compute_maxlevels( |
| 3898 | xfs_mount_t *mp, /* file system mount structure */ |
| 3899 | int whichfork) /* data or attr fork */ |
| 3900 | { |
| 3901 | int level; /* btree level */ |
| 3902 | uint maxblocks; /* max blocks at this level */ |
| 3903 | uint maxleafents; /* max leaf entries possible */ |
| 3904 | int maxrootrecs; /* max records in root block */ |
| 3905 | int minleafrecs; /* min records in leaf block */ |
| 3906 | int minnoderecs; /* min records in node block */ |
| 3907 | int sz; /* root block size */ |
| 3908 | |
| 3909 | /* |
| 3910 | * The maximum number of extents in a file, hence the maximum |
| 3911 | * number of leaf entries, is controlled by the type of di_nextents |
| 3912 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3913 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3914 | * |
| 3915 | * 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] | 3916 | * the fork offset of all the inodes will be |
| 3917 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3918 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3919 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3920 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3921 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3922 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3923 | if (whichfork == XFS_DATA_FORK) { |
| 3924 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3925 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3926 | } else { |
| 3927 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3928 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3929 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3930 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3932 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3934 | for (level = 1; maxblocks > 1; level++) { |
| 3935 | if (maxblocks <= maxrootrecs) |
| 3936 | maxblocks = 1; |
| 3937 | else |
| 3938 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3939 | } |
| 3940 | mp->m_bm_maxlevels[whichfork] = level; |
| 3941 | } |
| 3942 | |
| 3943 | /* |
| 3944 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3945 | * caller. Frees all the extents that need freeing, which must be done |
| 3946 | * last due to locking considerations. We never free any extents in |
| 3947 | * the first transaction. This is to allow the caller to make the first |
| 3948 | * transaction a synchronous one so that the pointers to the data being |
| 3949 | * broken in this transaction will be permanent before the data is actually |
| 3950 | * freed. This is necessary to prevent blocks from being reallocated |
| 3951 | * and written to before the free and reallocation are actually permanent. |
| 3952 | * We do not just make the first transaction synchronous here, because |
| 3953 | * there are more efficient ways to gain the same protection in some cases |
| 3954 | * (see the file truncation code). |
| 3955 | * |
| 3956 | * Return 1 if the given transaction was committed and a new one |
| 3957 | * started, and 0 otherwise in the committed parameter. |
| 3958 | */ |
| 3959 | /*ARGSUSED*/ |
| 3960 | int /* error */ |
| 3961 | xfs_bmap_finish( |
| 3962 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3963 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | int *committed) /* xact committed or not */ |
| 3965 | { |
| 3966 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3967 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3968 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3969 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | unsigned int logres; /* new log reservation */ |
| 3971 | unsigned int logcount; /* new log count */ |
| 3972 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3973 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3974 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3975 | |
| 3976 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3977 | if (flist->xbf_count == 0) { |
| 3978 | *committed = 0; |
| 3979 | return 0; |
| 3980 | } |
| 3981 | ntp = *tp; |
| 3982 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3983 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3984 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3985 | free->xbfi_blockcount); |
| 3986 | logres = ntp->t_log_res; |
| 3987 | logcount = ntp->t_log_count; |
| 3988 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3989 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | *tp = ntp; |
| 3991 | *committed = 1; |
| 3992 | /* |
| 3993 | * We have a new transaction, so we should return committed=1, |
| 3994 | * even though we're returning an error. |
| 3995 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3996 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3998 | |
| 3999 | /* |
| 4000 | * transaction commit worked ok so we can drop the extra ticket |
| 4001 | * reference that we gained in xfs_trans_dup() |
| 4002 | */ |
| 4003 | xfs_log_ticket_put(ntp->t_ticket); |
| 4004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 4006 | logcount))) |
| 4007 | return error; |
| 4008 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 4009 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 4010 | next = free->xbfi_next; |
| 4011 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 4012 | free->xbfi_blockcount))) { |
| 4013 | /* |
| 4014 | * The bmap free list will be cleaned up at a |
| 4015 | * higher level. The EFI will be canceled when |
| 4016 | * this transaction is aborted. |
| 4017 | * Need to force shutdown here to make sure it |
| 4018 | * happens, since this transaction may not be |
| 4019 | * dirty yet. |
| 4020 | */ |
| 4021 | mp = ntp->t_mountp; |
| 4022 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 4023 | xfs_force_shutdown(mp, |
| 4024 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 4025 | SHUTDOWN_CORRUPT_INCORE : |
| 4026 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | return error; |
| 4028 | } |
| 4029 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 4030 | free->xbfi_blockcount); |
| 4031 | xfs_bmap_del_free(flist, NULL, free); |
| 4032 | } |
| 4033 | return 0; |
| 4034 | } |
| 4035 | |
| 4036 | /* |
| 4037 | * Free up any items left in the list. |
| 4038 | */ |
| 4039 | void |
| 4040 | xfs_bmap_cancel( |
| 4041 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 4042 | { |
| 4043 | xfs_bmap_free_item_t *free; /* free list item */ |
| 4044 | xfs_bmap_free_item_t *next; |
| 4045 | |
| 4046 | if (flist->xbf_count == 0) |
| 4047 | return; |
| 4048 | ASSERT(flist->xbf_first != NULL); |
| 4049 | for (free = flist->xbf_first; free; free = next) { |
| 4050 | next = free->xbfi_next; |
| 4051 | xfs_bmap_del_free(flist, NULL, free); |
| 4052 | } |
| 4053 | ASSERT(flist->xbf_count == 0); |
| 4054 | } |
| 4055 | |
| 4056 | /* |
| 4057 | * Returns the file-relative block number of the first unused block(s) |
| 4058 | * in the file with at least "len" logically contiguous blocks free. |
| 4059 | * This is the lowest-address hole if the file has holes, else the first block |
| 4060 | * past the end of file. |
| 4061 | * Return 0 if the file is currently local (in-inode). |
| 4062 | */ |
| 4063 | int /* error */ |
| 4064 | xfs_bmap_first_unused( |
| 4065 | xfs_trans_t *tp, /* transaction pointer */ |
| 4066 | xfs_inode_t *ip, /* incore inode */ |
| 4067 | xfs_extlen_t len, /* size of hole to find */ |
| 4068 | xfs_fileoff_t *first_unused, /* unused block */ |
| 4069 | int whichfork) /* data or attr fork */ |
| 4070 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4072 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4074 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 4075 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 4076 | xfs_fileoff_t max; /* starting useful block */ |
| 4077 | xfs_fileoff_t off; /* offset for this block */ |
| 4078 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4079 | |
| 4080 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 4081 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 4082 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 4083 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4084 | *first_unused = 0; |
| 4085 | return 0; |
| 4086 | } |
| 4087 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4088 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4089 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4090 | return error; |
| 4091 | lowest = *first_unused; |
| 4092 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4093 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4094 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | off = xfs_bmbt_get_startoff(ep); |
| 4096 | /* |
| 4097 | * See if the hole before this extent will work. |
| 4098 | */ |
| 4099 | if (off >= lowest + len && off - max >= len) { |
| 4100 | *first_unused = max; |
| 4101 | return 0; |
| 4102 | } |
| 4103 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 4104 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 4105 | } |
| 4106 | *first_unused = max; |
| 4107 | return 0; |
| 4108 | } |
| 4109 | |
| 4110 | /* |
| 4111 | * Returns the file-relative block number of the last block + 1 before |
| 4112 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4113 | * This is not based on i_size, it is based on the extent records. |
| 4114 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | */ |
| 4116 | int /* error */ |
| 4117 | xfs_bmap_last_before( |
| 4118 | xfs_trans_t *tp, /* transaction pointer */ |
| 4119 | xfs_inode_t *ip, /* incore inode */ |
| 4120 | xfs_fileoff_t *last_block, /* last block */ |
| 4121 | int whichfork) /* data or attr fork */ |
| 4122 | { |
| 4123 | xfs_fileoff_t bno; /* input file offset */ |
| 4124 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4125 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | int error; /* error return value */ |
| 4127 | xfs_bmbt_irec_t got; /* current extent value */ |
| 4128 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4129 | xfs_extnum_t lastx; /* last extent used */ |
| 4130 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 4131 | |
| 4132 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4133 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4134 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4135 | return XFS_ERROR(EIO); |
| 4136 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4137 | *last_block = 0; |
| 4138 | return 0; |
| 4139 | } |
| 4140 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4141 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4142 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4143 | return error; |
| 4144 | bno = *last_block - 1; |
| 4145 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4146 | &prev); |
| 4147 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 4148 | if (prev.br_startoff == NULLFILEOFF) |
| 4149 | *last_block = 0; |
| 4150 | else |
| 4151 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 4152 | } |
| 4153 | /* |
| 4154 | * Otherwise *last_block is already the right answer. |
| 4155 | */ |
| 4156 | return 0; |
| 4157 | } |
| 4158 | |
| 4159 | /* |
| 4160 | * 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] | 4161 | * the file. This is not based on i_size, it is based on the extent records. |
| 4162 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | */ |
| 4164 | int /* error */ |
| 4165 | xfs_bmap_last_offset( |
| 4166 | xfs_trans_t *tp, /* transaction pointer */ |
| 4167 | xfs_inode_t *ip, /* incore inode */ |
| 4168 | xfs_fileoff_t *last_block, /* last block */ |
| 4169 | int whichfork) /* data or attr fork */ |
| 4170 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4171 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | int error; /* error return value */ |
| 4173 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4174 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4175 | |
| 4176 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4177 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4178 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4179 | return XFS_ERROR(EIO); |
| 4180 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4181 | *last_block = 0; |
| 4182 | return 0; |
| 4183 | } |
| 4184 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4185 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4186 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4187 | return error; |
| 4188 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4189 | if (!nextents) { |
| 4190 | *last_block = 0; |
| 4191 | return 0; |
| 4192 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4193 | ep = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); |
| 4195 | return 0; |
| 4196 | } |
| 4197 | |
| 4198 | /* |
| 4199 | * Returns whether the selected fork of the inode has exactly one |
| 4200 | * block or not. For the data fork we check this matches di_size, |
| 4201 | * implying the file's range is 0..bsize-1. |
| 4202 | */ |
| 4203 | int /* 1=>1 block, 0=>otherwise */ |
| 4204 | xfs_bmap_one_block( |
| 4205 | xfs_inode_t *ip, /* incore inode */ |
| 4206 | int whichfork) /* data or attr fork */ |
| 4207 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4208 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4210 | int rval; /* return value */ |
| 4211 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 4212 | |
| 4213 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4214 | if (whichfork == XFS_DATA_FORK) { |
| 4215 | return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ? |
| 4216 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 4217 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 4218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | #endif /* !DEBUG */ |
| 4220 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4221 | return 0; |
| 4222 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4223 | return 0; |
| 4224 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4225 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4226 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | xfs_bmbt_get_all(ep, &s); |
| 4228 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4229 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4230 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | return rval; |
| 4232 | } |
| 4233 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4234 | STATIC int |
| 4235 | xfs_bmap_sanity_check( |
| 4236 | struct xfs_mount *mp, |
| 4237 | struct xfs_buf *bp, |
| 4238 | int level) |
| 4239 | { |
| 4240 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4241 | |
| 4242 | if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC || |
| 4243 | be16_to_cpu(block->bb_level) != level || |
| 4244 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4245 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4246 | return 0; |
| 4247 | return 1; |
| 4248 | } |
| 4249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | /* |
| 4251 | * Read in the extents to if_extents. |
| 4252 | * All inode fields are set up by caller, we just traverse the btree |
| 4253 | * and copy the records in. If the file system cannot contain unwritten |
| 4254 | * extents, the records are checked for no "state" flags. |
| 4255 | */ |
| 4256 | int /* error */ |
| 4257 | xfs_bmap_read_extents( |
| 4258 | xfs_trans_t *tp, /* transaction pointer */ |
| 4259 | xfs_inode_t *ip, /* incore inode */ |
| 4260 | int whichfork) /* data or attr fork */ |
| 4261 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4262 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4264 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4265 | int error; /* error return value */ |
| 4266 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4268 | xfs_ifork_t *ifp; /* fork structure */ |
| 4269 | int level; /* btree level, for checking */ |
| 4270 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4271 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | /* REFERENCED */ |
| 4273 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | |
| 4275 | bno = NULLFSBLOCK; |
| 4276 | mp = ip->i_mount; |
| 4277 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4278 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4279 | XFS_EXTFMT_INODE(ip); |
| 4280 | block = ifp->if_broot; |
| 4281 | /* |
| 4282 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4283 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4284 | level = be16_to_cpu(block->bb_level); |
| 4285 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4286 | 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] | 4287 | bno = be64_to_cpu(*pp); |
| 4288 | ASSERT(bno != NULLDFSBNO); |
| 4289 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4290 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | /* |
| 4292 | * Go down the tree until leaf level is reached, following the first |
| 4293 | * pointer (leftmost) at each level. |
| 4294 | */ |
| 4295 | while (level-- > 0) { |
| 4296 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4297 | XFS_BMAP_BTREE_REF))) |
| 4298 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4299 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4301 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | error0); |
| 4303 | if (level == 0) |
| 4304 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4305 | 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] | 4306 | bno = be64_to_cpu(*pp); |
| 4307 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | xfs_trans_brelse(tp, bp); |
| 4309 | } |
| 4310 | /* |
| 4311 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4312 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4313 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | i = 0; |
| 4315 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4316 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | */ |
| 4318 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4319 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | xfs_fsblock_t nextbno; |
| 4321 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4322 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
| 4324 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4325 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | if (unlikely(i + num_recs > room)) { |
| 4327 | ASSERT(i + num_recs <= room); |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4328 | xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount, |
| 4329 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | (unsigned long long) ip->i_ino); |
| 4331 | XFS_ERROR_REPORT("xfs_bmap_read_extents(1)", |
| 4332 | XFS_ERRLEVEL_LOW, |
| 4333 | ip->i_mount); |
| 4334 | goto error0; |
| 4335 | } |
| 4336 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4337 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | error0); |
| 4339 | /* |
| 4340 | * Read-ahead the next leaf block, if any. |
| 4341 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4342 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | if (nextbno != NULLFSBLOCK) |
| 4344 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4345 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4346 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4348 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4349 | start = i; |
| 4350 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4351 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4352 | trp->l0 = be64_to_cpu(frp->l0); |
| 4353 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | } |
| 4355 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4356 | /* |
| 4357 | * Check all attribute bmap btree records and |
| 4358 | * any "older" data bmap btree records for a |
| 4359 | * set bit in the "extent flag" position. |
| 4360 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4361 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4362 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4364 | XFS_ERRLEVEL_LOW, |
| 4365 | ip->i_mount); |
| 4366 | goto error0; |
| 4367 | } |
| 4368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | xfs_trans_brelse(tp, bp); |
| 4370 | bno = nextbno; |
| 4371 | /* |
| 4372 | * If we've reached the end, stop. |
| 4373 | */ |
| 4374 | if (bno == NULLFSBLOCK) |
| 4375 | break; |
| 4376 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4377 | XFS_BMAP_BTREE_REF))) |
| 4378 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4379 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4381 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4383 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | return 0; |
| 4385 | error0: |
| 4386 | xfs_trans_brelse(tp, bp); |
| 4387 | return XFS_ERROR(EFSCORRUPTED); |
| 4388 | } |
| 4389 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4390 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4392 | * 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] | 4393 | */ |
| 4394 | void |
| 4395 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4397 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4398 | int whichfork, /* data or attr fork */ |
| 4399 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4401 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4403 | int state = 0; |
| 4404 | |
| 4405 | if (whichfork == XFS_ATTR_FORK) |
| 4406 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | |
| 4408 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4409 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4410 | for (idx = 0; idx < cnt; idx++) |
| 4411 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | /* |
| 4415 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4416 | * given the callers original parameters. Specifically check the |
| 4417 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4418 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4419 | */ |
| 4420 | STATIC void |
| 4421 | xfs_bmap_validate_ret( |
| 4422 | xfs_fileoff_t bno, |
| 4423 | xfs_filblks_t len, |
| 4424 | int flags, |
| 4425 | xfs_bmbt_irec_t *mval, |
| 4426 | int nmap, |
| 4427 | int ret_nmap) |
| 4428 | { |
| 4429 | int i; /* index to map values */ |
| 4430 | |
| 4431 | ASSERT(ret_nmap <= nmap); |
| 4432 | |
| 4433 | for (i = 0; i < ret_nmap; i++) { |
| 4434 | ASSERT(mval[i].br_blockcount > 0); |
| 4435 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4436 | ASSERT(mval[i].br_startoff >= bno); |
| 4437 | ASSERT(mval[i].br_blockcount <= len); |
| 4438 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4439 | bno + len); |
| 4440 | } else { |
| 4441 | ASSERT(mval[i].br_startoff < bno + len); |
| 4442 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4443 | bno); |
| 4444 | } |
| 4445 | ASSERT(i == 0 || |
| 4446 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4447 | mval[i].br_startoff); |
| 4448 | if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY)) |
| 4449 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4450 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 4451 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4452 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4453 | } |
| 4454 | } |
| 4455 | #endif /* DEBUG */ |
| 4456 | |
| 4457 | |
| 4458 | /* |
| 4459 | * Map file blocks to filesystem blocks. |
| 4460 | * File range is given by the bno/len pair. |
| 4461 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 4462 | * into a hole or past eof. |
| 4463 | * Only allocates blocks from a single allocation group, |
| 4464 | * to avoid locking problems. |
| 4465 | * The returned value in "firstblock" from the first call in a transaction |
| 4466 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4467 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4468 | * the first call in "total"; if no allocation group has that many free |
| 4469 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4470 | */ |
| 4471 | int /* error */ |
| 4472 | xfs_bmapi( |
| 4473 | xfs_trans_t *tp, /* transaction pointer */ |
| 4474 | xfs_inode_t *ip, /* incore inode */ |
| 4475 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4476 | xfs_filblks_t len, /* length to map in file */ |
| 4477 | int flags, /* XFS_BMAPI_... */ |
| 4478 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4479 | controls a.g. for allocs */ |
| 4480 | xfs_extlen_t total, /* total blocks needed */ |
| 4481 | xfs_bmbt_irec_t *mval, /* output: map values */ |
| 4482 | int *nmap, /* i/o: mval size/count */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4483 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 4484 | xfs_extdelta_t *delta) /* o: change made to incore extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | { |
| 4486 | xfs_fsblock_t abno; /* allocated block number */ |
| 4487 | xfs_extlen_t alen; /* allocated extent length */ |
| 4488 | xfs_fileoff_t aoff; /* allocated file offset */ |
Dave Chinner | f0a0eaa | 2010-01-20 10:50:06 +1100 | [diff] [blame] | 4489 | xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | xfs_fileoff_t end; /* end of mapped file region */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4492 | int eof; /* we've hit the end of extents */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4493 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4495 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4497 | xfs_extlen_t indlen; /* indirect blocks length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4499 | int logflags; /* flags for transaction logging */ |
| 4500 | xfs_extlen_t minleft; /* min blocks left after allocation */ |
| 4501 | xfs_extlen_t minlen; /* min allocation size */ |
| 4502 | xfs_mount_t *mp; /* xfs mount structure */ |
| 4503 | int n; /* current extent index */ |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 4504 | int nallocs; /* number of extents alloc'd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4506 | xfs_fileoff_t obno; /* old block number (offset) */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4507 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | int tmp_logflags; /* temp flags holder */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4509 | int whichfork; /* data or attr fork */ |
| 4510 | char inhole; /* current location is hole in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | char wasdelay; /* old extent was delayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | char wr; /* this is a write request */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4513 | char rt; /* this is a realtime file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | #ifdef DEBUG |
| 4515 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4516 | int orig_flags; /* original flags arg value */ |
| 4517 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4518 | xfs_bmbt_irec_t *orig_mval; /* original value of mval */ |
| 4519 | int orig_nmap; /* original value of *nmap */ |
| 4520 | |
| 4521 | orig_bno = bno; |
| 4522 | orig_len = len; |
| 4523 | orig_flags = flags; |
| 4524 | orig_mval = mval; |
| 4525 | orig_nmap = *nmap; |
| 4526 | #endif |
| 4527 | ASSERT(*nmap >= 1); |
| 4528 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); |
| 4529 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4530 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4531 | mp = ip->i_mount; |
| 4532 | if (unlikely(XFS_TEST_ERROR( |
| 4533 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4534 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4535 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4536 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4537 | XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); |
| 4538 | return XFS_ERROR(EFSCORRUPTED); |
| 4539 | } |
| 4540 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4541 | return XFS_ERROR(EIO); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4542 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4544 | ASSERT(ifp->if_ext_max == |
| 4545 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4546 | if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) |
| 4547 | XFS_STATS_INC(xs_blk_mapw); |
| 4548 | else |
| 4549 | XFS_STATS_INC(xs_blk_mapr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | /* |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4551 | * IGSTATE flag is used to combine extents which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | * differ only due to the state of the extents. |
| 4553 | * This technique is used from xfs_getbmap() |
| 4554 | * when the caller does not wish to see the |
| 4555 | * separation (which is the default). |
| 4556 | * |
| 4557 | * This technique is also used when writing a |
| 4558 | * buffer which has been partially written, |
| 4559 | * (usually by being flushed during a chunkread), |
| 4560 | * to ensure one write takes place. This also |
| 4561 | * prevents a change in the xfs inode extents at |
| 4562 | * this time, intentionally. This change occurs |
| 4563 | * on completion of the write operation, in |
| 4564 | * xfs_strat_comp(), where the xfs_bmapi() call |
| 4565 | * is transactioned, and the extents combined. |
| 4566 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4567 | if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */ |
| 4568 | wr = 0; /* no allocations are allowed */ |
| 4569 | ASSERT(wr || !(flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | logflags = 0; |
| 4571 | nallocs = 0; |
| 4572 | cur = NULL; |
| 4573 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4574 | ASSERT(wr && tp); |
| 4575 | if ((error = xfs_bmap_local_to_extents(tp, ip, |
| 4576 | firstblock, total, &logflags, whichfork))) |
| 4577 | goto error0; |
| 4578 | } |
| 4579 | if (wr && *firstblock == NULLFSBLOCK) { |
| 4580 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4581 | minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | else |
| 4583 | minleft = 1; |
| 4584 | } else |
| 4585 | minleft = 0; |
| 4586 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4587 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4588 | goto error0; |
| 4589 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4590 | &prev); |
| 4591 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4592 | n = 0; |
| 4593 | end = bno + len; |
| 4594 | obno = bno; |
| 4595 | bma.ip = NULL; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4596 | if (delta) { |
| 4597 | delta->xed_startoff = NULLFILEOFF; |
| 4598 | delta->xed_blockcount = 0; |
| 4599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | while (bno < end && n < *nmap) { |
| 4601 | /* |
| 4602 | * Reading past eof, act as though there's a hole |
| 4603 | * up to end. |
| 4604 | */ |
| 4605 | if (eof && !wr) |
| 4606 | got.br_startoff = end; |
| 4607 | inhole = eof || got.br_startoff > bno; |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4608 | wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4609 | isnullstartblock(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | /* |
| 4611 | * First, deal with the hole before the allocated space |
| 4612 | * that we found, if any. |
| 4613 | */ |
| 4614 | if (wr && (inhole || wasdelay)) { |
| 4615 | /* |
| 4616 | * For the wasdelay case, we could also just |
| 4617 | * allocate the stuff asked for in this bmap call |
| 4618 | * but that wouldn't be as good. |
| 4619 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4620 | if (wasdelay && !(flags & XFS_BMAPI_EXACT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | alen = (xfs_extlen_t)got.br_blockcount; |
| 4622 | aoff = got.br_startoff; |
| 4623 | if (lastx != NULLEXTNUM && lastx) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4624 | ep = xfs_iext_get_ext(ifp, lastx - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | xfs_bmbt_get_all(ep, &prev); |
| 4626 | } |
| 4627 | } else if (wasdelay) { |
| 4628 | alen = (xfs_extlen_t) |
| 4629 | XFS_FILBLKS_MIN(len, |
| 4630 | (got.br_startoff + |
| 4631 | got.br_blockcount) - bno); |
| 4632 | aoff = bno; |
| 4633 | } else { |
| 4634 | alen = (xfs_extlen_t) |
| 4635 | XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4636 | if (!eof) |
| 4637 | alen = (xfs_extlen_t) |
| 4638 | XFS_FILBLKS_MIN(alen, |
| 4639 | got.br_startoff - bno); |
| 4640 | aoff = bno; |
| 4641 | } |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4642 | minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; |
| 4643 | if (flags & XFS_BMAPI_DELAY) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4644 | xfs_extlen_t extsz; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4645 | |
| 4646 | /* Figure out the extent size, adjust alen */ |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 4647 | extsz = xfs_get_extsz_hint(ip); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4648 | if (extsz) { |
| 4649 | error = xfs_bmap_extsize_align(mp, |
| 4650 | &got, &prev, extsz, |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4651 | rt, eof, |
| 4652 | flags&XFS_BMAPI_DELAY, |
| 4653 | flags&XFS_BMAPI_CONVERT, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4654 | &aoff, &alen); |
| 4655 | ASSERT(!error); |
| 4656 | } |
| 4657 | |
| 4658 | if (rt) |
| 4659 | extsz = alen / mp->m_sb.sb_rextsize; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | /* |
| 4662 | * Make a transaction-less quota reservation for |
| 4663 | * delayed allocation blocks. This number gets |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4664 | * adjusted later. We return if we haven't |
| 4665 | * allocated blocks already inside this loop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4667 | error = xfs_trans_reserve_quota_nblks( |
| 4668 | NULL, ip, (long)alen, 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4669 | rt ? XFS_QMOPT_RES_RTBLKS : |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4670 | XFS_QMOPT_RES_REGBLKS); |
| 4671 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | if (n == 0) { |
| 4673 | *nmap = 0; |
| 4674 | ASSERT(cur == NULL); |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4675 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | } |
| 4677 | break; |
| 4678 | } |
| 4679 | |
| 4680 | /* |
| 4681 | * Split changing sb for alen and indlen since |
| 4682 | * they could be coming from different places. |
| 4683 | */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4684 | indlen = (xfs_extlen_t) |
| 4685 | xfs_bmap_worst_indlen(ip, alen); |
| 4686 | ASSERT(indlen > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4688 | if (rt) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4689 | error = xfs_mod_incore_sb(mp, |
| 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 | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4693 | } else { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4694 | error = xfs_mod_incore_sb(mp, |
| 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)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4698 | } |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4699 | if (!error) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4700 | error = xfs_mod_incore_sb(mp, |
| 4701 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4702 | -((int64_t)indlen), (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4703 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4704 | if (error && rt) |
| 4705 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4706 | XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4707 | (int64_t)extsz, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4708 | XFS_BMAPI_RSVBLOCKS)); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4709 | else if (error) |
| 4710 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4711 | XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 4712 | (int64_t)alen, (flags & |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4713 | XFS_BMAPI_RSVBLOCKS)); |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4714 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4715 | |
| 4716 | if (error) { |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4717 | if (XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4718 | /* unreserve the blocks now */ |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4719 | (void) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4720 | xfs_trans_unreserve_quota_nblks( |
| 4721 | NULL, ip, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4722 | (long)alen, 0, rt ? |
| 4723 | XFS_QMOPT_RES_RTBLKS : |
| 4724 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | break; |
| 4726 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | ip->i_delayed_blks += alen; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4729 | abno = nullstartblock(indlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | } else { |
| 4731 | /* |
| 4732 | * If first time, allocate and fill in |
| 4733 | * once-only bma fields. |
| 4734 | */ |
| 4735 | if (bma.ip == NULL) { |
| 4736 | bma.tp = tp; |
| 4737 | bma.ip = ip; |
| 4738 | bma.prevp = &prev; |
| 4739 | bma.gotp = &got; |
| 4740 | bma.total = total; |
| 4741 | bma.userdata = 0; |
| 4742 | } |
| 4743 | /* Indicate if this is the first user data |
| 4744 | * in the file, or just any user data. |
| 4745 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4746 | if (!(flags & XFS_BMAPI_METADATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | bma.userdata = (aoff == 0) ? |
| 4748 | XFS_ALLOC_INITIAL_USER_DATA : |
| 4749 | XFS_ALLOC_USERDATA; |
| 4750 | } |
| 4751 | /* |
| 4752 | * Fill in changeable bma fields. |
| 4753 | */ |
| 4754 | bma.eof = eof; |
| 4755 | bma.firstblock = *firstblock; |
| 4756 | bma.alen = alen; |
| 4757 | bma.off = aoff; |
Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 4758 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | bma.wasdel = wasdelay; |
| 4760 | bma.minlen = minlen; |
| 4761 | bma.low = flist->xbf_low; |
| 4762 | bma.minleft = minleft; |
| 4763 | /* |
| 4764 | * Only want to do the alignment at the |
| 4765 | * eof if it is userdata and allocation length |
| 4766 | * is larger than a stripe unit. |
| 4767 | */ |
| 4768 | if (mp->m_dalign && alen >= mp->m_dalign && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4769 | (!(flags & XFS_BMAPI_METADATA)) && |
| 4770 | (whichfork == XFS_DATA_FORK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | if ((error = xfs_bmap_isaeof(ip, aoff, |
| 4772 | whichfork, &bma.aeof))) |
| 4773 | goto error0; |
| 4774 | } else |
| 4775 | bma.aeof = 0; |
| 4776 | /* |
| 4777 | * Call allocator. |
| 4778 | */ |
| 4779 | if ((error = xfs_bmap_alloc(&bma))) |
| 4780 | goto error0; |
| 4781 | /* |
| 4782 | * Copy out result fields. |
| 4783 | */ |
| 4784 | abno = bma.rval; |
| 4785 | if ((flist->xbf_low = bma.low)) |
| 4786 | minleft = 0; |
| 4787 | alen = bma.alen; |
| 4788 | aoff = bma.off; |
| 4789 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4790 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4791 | XFS_FSB_TO_AGNO(mp, bma.firstblock) || |
| 4792 | (flist->xbf_low && |
| 4793 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4794 | XFS_FSB_TO_AGNO(mp, bma.firstblock))); |
| 4795 | *firstblock = bma.firstblock; |
| 4796 | if (cur) |
| 4797 | cur->bc_private.b.firstblock = |
| 4798 | *firstblock; |
| 4799 | if (abno == NULLFSBLOCK) |
| 4800 | break; |
| 4801 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4802 | cur = xfs_bmbt_init_cursor(mp, tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | ip, whichfork); |
| 4804 | cur->bc_private.b.firstblock = |
| 4805 | *firstblock; |
| 4806 | cur->bc_private.b.flist = flist; |
| 4807 | } |
| 4808 | /* |
| 4809 | * Bump the number of extents we've allocated |
| 4810 | * in this call. |
| 4811 | */ |
| 4812 | nallocs++; |
| 4813 | } |
| 4814 | if (cur) |
| 4815 | cur->bc_private.b.flags = |
| 4816 | wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4817 | got.br_startoff = aoff; |
| 4818 | got.br_startblock = abno; |
| 4819 | got.br_blockcount = alen; |
| 4820 | got.br_state = XFS_EXT_NORM; /* assume normal */ |
| 4821 | /* |
| 4822 | * Determine state of extent, and the filesystem. |
| 4823 | * A wasdelay extent has been initialized, so |
| 4824 | * shouldn't be flagged as unwritten. |
| 4825 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4826 | if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) |
| 4828 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4829 | } |
| 4830 | error = xfs_bmap_add_extent(ip, lastx, &cur, &got, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4831 | firstblock, flist, &tmp_logflags, delta, |
| 4832 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | logflags |= tmp_logflags; |
| 4834 | if (error) |
| 4835 | goto error0; |
| 4836 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4837 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4839 | xfs_bmbt_get_all(ep, &got); |
| 4840 | ASSERT(got.br_startoff <= aoff); |
| 4841 | ASSERT(got.br_startoff + got.br_blockcount >= |
| 4842 | aoff + alen); |
| 4843 | #ifdef DEBUG |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4844 | if (flags & XFS_BMAPI_DELAY) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4845 | ASSERT(isnullstartblock(got.br_startblock)); |
| 4846 | ASSERT(startblockval(got.br_startblock) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | } |
| 4848 | ASSERT(got.br_state == XFS_EXT_NORM || |
| 4849 | got.br_state == XFS_EXT_UNWRITTEN); |
| 4850 | #endif |
| 4851 | /* |
| 4852 | * Fall down into the found allocated space case. |
| 4853 | */ |
| 4854 | } else if (inhole) { |
| 4855 | /* |
| 4856 | * Reading in a hole. |
| 4857 | */ |
| 4858 | mval->br_startoff = bno; |
| 4859 | mval->br_startblock = HOLESTARTBLOCK; |
| 4860 | mval->br_blockcount = |
| 4861 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4862 | mval->br_state = XFS_EXT_NORM; |
| 4863 | bno += mval->br_blockcount; |
| 4864 | len -= mval->br_blockcount; |
| 4865 | mval++; |
| 4866 | n++; |
| 4867 | continue; |
| 4868 | } |
| 4869 | /* |
| 4870 | * Then deal with the allocated space we found. |
| 4871 | */ |
| 4872 | ASSERT(ep != NULL); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4873 | if (!(flags & XFS_BMAPI_ENTIRE) && |
| 4874 | (got.br_startoff + got.br_blockcount > obno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | if (obno > bno) |
| 4876 | bno = obno; |
| 4877 | ASSERT((bno >= obno) || (n == 0)); |
| 4878 | ASSERT(bno < end); |
| 4879 | mval->br_startoff = bno; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4880 | if (isnullstartblock(got.br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4881 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4883 | } else |
| 4884 | mval->br_startblock = |
| 4885 | got.br_startblock + |
| 4886 | (bno - got.br_startoff); |
| 4887 | /* |
| 4888 | * Return the minimum of what we got and what we |
| 4889 | * asked for for the length. We can use the len |
| 4890 | * variable here because it is modified below |
| 4891 | * and we could have been there before coming |
| 4892 | * here if the first part of the allocation |
| 4893 | * didn't overlap what was asked for. |
| 4894 | */ |
| 4895 | mval->br_blockcount = |
| 4896 | XFS_FILBLKS_MIN(end - bno, got.br_blockcount - |
| 4897 | (bno - got.br_startoff)); |
| 4898 | mval->br_state = got.br_state; |
| 4899 | ASSERT(mval->br_blockcount <= len); |
| 4900 | } else { |
| 4901 | *mval = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4902 | if (isnullstartblock(mval->br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4903 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4905 | } |
| 4906 | } |
| 4907 | |
| 4908 | /* |
| 4909 | * Check if writing previously allocated but |
| 4910 | * unwritten extents. |
| 4911 | */ |
| 4912 | if (wr && mval->br_state == XFS_EXT_UNWRITTEN && |
| 4913 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) { |
| 4914 | /* |
| 4915 | * Modify (by adding) the state flag, if writing. |
| 4916 | */ |
| 4917 | ASSERT(mval->br_blockcount <= len); |
| 4918 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4919 | cur = xfs_bmbt_init_cursor(mp, |
| 4920 | tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | cur->bc_private.b.firstblock = |
| 4922 | *firstblock; |
| 4923 | cur->bc_private.b.flist = flist; |
| 4924 | } |
| 4925 | mval->br_state = XFS_EXT_NORM; |
| 4926 | error = xfs_bmap_add_extent(ip, lastx, &cur, mval, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 4927 | firstblock, flist, &tmp_logflags, delta, |
| 4928 | whichfork, (flags & XFS_BMAPI_RSVBLOCKS)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | logflags |= tmp_logflags; |
| 4930 | if (error) |
| 4931 | goto error0; |
| 4932 | lastx = ifp->if_lastex; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4933 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4935 | xfs_bmbt_get_all(ep, &got); |
| 4936 | /* |
| 4937 | * We may have combined previously unwritten |
| 4938 | * space with written space, so generate |
| 4939 | * another request. |
| 4940 | */ |
| 4941 | if (mval->br_blockcount < len) |
| 4942 | continue; |
| 4943 | } |
| 4944 | |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4945 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4947 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4948 | (mval->br_blockcount <= len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | (mval->br_startoff < obno)); |
| 4950 | bno = mval->br_startoff + mval->br_blockcount; |
| 4951 | len = end - bno; |
| 4952 | if (n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4953 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4954 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4955 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4956 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4957 | mval[-1].br_state = mval->br_state; |
| 4958 | } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4959 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4960 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4961 | mval->br_startblock == |
| 4962 | mval[-1].br_startblock + mval[-1].br_blockcount && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4963 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4964 | mval[-1].br_state == mval->br_state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | ASSERT(mval->br_startoff == |
| 4966 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4967 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4968 | } else if (n > 0 && |
| 4969 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4970 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4971 | mval->br_startoff == |
| 4972 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4973 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4974 | mval[-1].br_state = mval->br_state; |
| 4975 | } else if (!((n == 0) && |
| 4976 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4977 | obno))) { |
| 4978 | mval++; |
| 4979 | n++; |
| 4980 | } |
| 4981 | /* |
| 4982 | * If we're done, stop now. Stop when we've allocated |
| 4983 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4984 | * the transaction may get too big. |
| 4985 | */ |
| 4986 | if (bno >= end || n >= *nmap || nallocs >= *nmap) |
| 4987 | break; |
| 4988 | /* |
| 4989 | * Else go on to the next record. |
| 4990 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4991 | ep = xfs_iext_get_ext(ifp, ++lastx); |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4992 | prev = got; |
| 4993 | if (lastx >= nextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | eof = 1; |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4995 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | xfs_bmbt_get_all(ep, &got); |
| 4997 | } |
| 4998 | ifp->if_lastex = lastx; |
| 4999 | *nmap = n; |
| 5000 | /* |
| 5001 | * Transform from btree to extents, give it cur. |
| 5002 | */ |
| 5003 | if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5004 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5005 | ASSERT(wr && cur); |
| 5006 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 5007 | &tmp_logflags, whichfork); |
| 5008 | logflags |= tmp_logflags; |
| 5009 | if (error) |
| 5010 | goto error0; |
| 5011 | } |
| 5012 | ASSERT(ifp->if_ext_max == |
| 5013 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5014 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 5015 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 5016 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5017 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5018 | /* A change was actually made. |
| 5019 | * Note that delta->xed_blockount is an offset at this |
| 5020 | * point and needs to be converted to a block count. |
| 5021 | */ |
| 5022 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5023 | delta->xed_blockcount -= delta->xed_startoff; |
| 5024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | error0: |
| 5026 | /* |
| 5027 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5028 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5030 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5032 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5033 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5035 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | /* |
| 5037 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5038 | * detecting a case where the data is changed, there's an error, |
| 5039 | * and it's not logged so we don't shutdown when we should. |
| 5040 | */ |
| 5041 | if (logflags) { |
| 5042 | ASSERT(tp && wr); |
| 5043 | xfs_trans_log_inode(tp, ip, logflags); |
| 5044 | } |
| 5045 | if (cur) { |
| 5046 | if (!error) { |
| 5047 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5048 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5049 | XFS_FSB_TO_AGNO(mp, |
| 5050 | cur->bc_private.b.firstblock) || |
| 5051 | (flist->xbf_low && |
| 5052 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5053 | XFS_FSB_TO_AGNO(mp, |
| 5054 | cur->bc_private.b.firstblock))); |
| 5055 | *firstblock = cur->bc_private.b.firstblock; |
| 5056 | } |
| 5057 | xfs_btree_del_cursor(cur, |
| 5058 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5059 | } |
| 5060 | if (!error) |
| 5061 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5062 | orig_nmap, *nmap); |
| 5063 | return error; |
| 5064 | } |
| 5065 | |
| 5066 | /* |
| 5067 | * Map file blocks to filesystem blocks, simple version. |
| 5068 | * One block (extent) only, read-only. |
| 5069 | * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. |
| 5070 | * For the other flag values, the effect is as if XFS_BMAPI_METADATA |
| 5071 | * was set and all the others were clear. |
| 5072 | */ |
| 5073 | int /* error */ |
| 5074 | xfs_bmapi_single( |
| 5075 | xfs_trans_t *tp, /* transaction pointer */ |
| 5076 | xfs_inode_t *ip, /* incore inode */ |
| 5077 | int whichfork, /* data or attr fork */ |
| 5078 | xfs_fsblock_t *fsb, /* output: mapped block */ |
| 5079 | xfs_fileoff_t bno) /* starting file offs. mapped */ |
| 5080 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5081 | int eof; /* we've hit the end of extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5083 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5085 | xfs_extnum_t lastx; /* last useful extent number */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5086 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | |
| 5088 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5089 | if (unlikely( |
| 5090 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 5091 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) { |
| 5092 | XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW, |
| 5093 | ip->i_mount); |
| 5094 | return XFS_ERROR(EFSCORRUPTED); |
| 5095 | } |
| 5096 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 5097 | return XFS_ERROR(EIO); |
| 5098 | XFS_STATS_INC(xs_blk_mapr); |
| 5099 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5100 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5101 | return error; |
| 5102 | (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5103 | &prev); |
| 5104 | /* |
| 5105 | * Reading past eof, act as though there's a hole |
| 5106 | * up to end. |
| 5107 | */ |
| 5108 | if (eof || got.br_startoff > bno) { |
| 5109 | *fsb = NULLFSBLOCK; |
| 5110 | return 0; |
| 5111 | } |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5112 | ASSERT(!isnullstartblock(got.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | ASSERT(bno < got.br_startoff + got.br_blockcount); |
| 5114 | *fsb = got.br_startblock + (bno - got.br_startoff); |
| 5115 | ifp->if_lastex = lastx; |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
| 5119 | /* |
| 5120 | * Unmap (remove) blocks from a file. |
| 5121 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5122 | * that value. If not all extents in the block range can be removed then |
| 5123 | * *done is set. |
| 5124 | */ |
| 5125 | int /* error */ |
| 5126 | xfs_bunmapi( |
| 5127 | xfs_trans_t *tp, /* transaction pointer */ |
| 5128 | struct xfs_inode *ip, /* incore inode */ |
| 5129 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5130 | xfs_filblks_t len, /* length to unmap in file */ |
| 5131 | int flags, /* misc flags */ |
| 5132 | xfs_extnum_t nexts, /* number of extents max */ |
| 5133 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5134 | controls a.g. for allocs */ |
| 5135 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5136 | xfs_extdelta_t *delta, /* o: change made to incore |
| 5137 | extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | int *done) /* set if not done yet */ |
| 5139 | { |
| 5140 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5141 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5142 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5143 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | int error; /* error return value */ |
| 5145 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5146 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5148 | int isrt; /* freeing in rt area */ |
| 5149 | xfs_extnum_t lastx; /* last extent index used */ |
| 5150 | int logflags; /* transaction logging flags */ |
| 5151 | xfs_extlen_t mod; /* rt extent offset */ |
| 5152 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5153 | xfs_extnum_t nextents; /* number of file extents */ |
| 5154 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5156 | int tmp_logflags; /* partial logging flags */ |
| 5157 | int wasdel; /* was a delayed alloc extent */ |
| 5158 | int whichfork; /* data or attribute fork */ |
| 5159 | int rsvd; /* OK to allocate reserved blocks */ |
| 5160 | xfs_fsblock_t sum; |
| 5161 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5162 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5165 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5166 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5167 | if (unlikely( |
| 5168 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5169 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5170 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5171 | ip->i_mount); |
| 5172 | return XFS_ERROR(EFSCORRUPTED); |
| 5173 | } |
| 5174 | mp = ip->i_mount; |
| 5175 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5176 | return XFS_ERROR(EIO); |
| 5177 | rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0; |
| 5178 | ASSERT(len > 0); |
| 5179 | ASSERT(nexts >= 0); |
| 5180 | ASSERT(ifp->if_ext_max == |
| 5181 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5182 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5183 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5184 | return error; |
| 5185 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5186 | if (nextents == 0) { |
| 5187 | *done = 1; |
| 5188 | return 0; |
| 5189 | } |
| 5190 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5191 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | start = bno; |
| 5193 | bno = start + len - 1; |
| 5194 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5195 | &prev); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5196 | if (delta) { |
| 5197 | delta->xed_startoff = NULLFILEOFF; |
| 5198 | delta->xed_blockcount = 0; |
| 5199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | /* |
| 5201 | * Check to see if the given block number is past the end of the |
| 5202 | * file, back up to the last block if so... |
| 5203 | */ |
| 5204 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5205 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5206 | xfs_bmbt_get_all(ep, &got); |
| 5207 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5208 | } |
| 5209 | logflags = 0; |
| 5210 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5211 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5212 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | cur->bc_private.b.firstblock = *firstblock; |
| 5214 | cur->bc_private.b.flist = flist; |
| 5215 | cur->bc_private.b.flags = 0; |
| 5216 | } else |
| 5217 | cur = NULL; |
| 5218 | extno = 0; |
| 5219 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5220 | (nexts == 0 || extno < nexts)) { |
| 5221 | /* |
| 5222 | * Is the found extent after a hole in which bno lives? |
| 5223 | * Just back up to the previous extent, if so. |
| 5224 | */ |
| 5225 | if (got.br_startoff > bno) { |
| 5226 | if (--lastx < 0) |
| 5227 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5228 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | xfs_bmbt_get_all(ep, &got); |
| 5230 | } |
| 5231 | /* |
| 5232 | * Is the last block of this extent before the range |
| 5233 | * we're supposed to delete? If so, we're done. |
| 5234 | */ |
| 5235 | bno = XFS_FILEOFF_MIN(bno, |
| 5236 | got.br_startoff + got.br_blockcount - 1); |
| 5237 | if (bno < start) |
| 5238 | break; |
| 5239 | /* |
| 5240 | * Then deal with the (possibly delayed) allocated space |
| 5241 | * we found. |
| 5242 | */ |
| 5243 | ASSERT(ep != NULL); |
| 5244 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5245 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | if (got.br_startoff < start) { |
| 5247 | del.br_startoff = start; |
| 5248 | del.br_blockcount -= start - got.br_startoff; |
| 5249 | if (!wasdel) |
| 5250 | del.br_startblock += start - got.br_startoff; |
| 5251 | } |
| 5252 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5253 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5254 | sum = del.br_startblock + del.br_blockcount; |
| 5255 | if (isrt && |
| 5256 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5257 | /* |
| 5258 | * Realtime extent not lined up at the end. |
| 5259 | * The extent could have been split into written |
| 5260 | * and unwritten pieces, or we could just be |
| 5261 | * unmapping part of it. But we can't really |
| 5262 | * get rid of part of a realtime extent. |
| 5263 | */ |
| 5264 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5265 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | /* |
| 5267 | * This piece is unwritten, or we're not |
| 5268 | * using unwritten extents. Skip over it. |
| 5269 | */ |
| 5270 | ASSERT(bno >= mod); |
| 5271 | bno -= mod > del.br_blockcount ? |
| 5272 | del.br_blockcount : mod; |
| 5273 | if (bno < got.br_startoff) { |
| 5274 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5275 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5276 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | } |
| 5278 | continue; |
| 5279 | } |
| 5280 | /* |
| 5281 | * It's written, turn it unwritten. |
| 5282 | * This is better than zeroing it. |
| 5283 | */ |
| 5284 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5285 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5286 | /* |
| 5287 | * If this spans a realtime extent boundary, |
| 5288 | * chop it back to the start of the one we end at. |
| 5289 | */ |
| 5290 | if (del.br_blockcount > mod) { |
| 5291 | del.br_startoff += del.br_blockcount - mod; |
| 5292 | del.br_startblock += del.br_blockcount - mod; |
| 5293 | del.br_blockcount = mod; |
| 5294 | } |
| 5295 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5296 | error = xfs_bmap_add_extent(ip, lastx, &cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5297 | firstblock, flist, &logflags, delta, |
| 5298 | XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | if (error) |
| 5300 | goto error0; |
| 5301 | goto nodelete; |
| 5302 | } |
| 5303 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5304 | /* |
| 5305 | * Realtime extent is lined up at the end but not |
| 5306 | * at the front. We'll get rid of full extents if |
| 5307 | * we can. |
| 5308 | */ |
| 5309 | mod = mp->m_sb.sb_rextsize - mod; |
| 5310 | if (del.br_blockcount > mod) { |
| 5311 | del.br_blockcount -= mod; |
| 5312 | del.br_startoff += mod; |
| 5313 | del.br_startblock += mod; |
| 5314 | } else if ((del.br_startoff == start && |
| 5315 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5316 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5317 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | /* |
| 5319 | * Can't make it unwritten. There isn't |
| 5320 | * a full extent here so just skip it. |
| 5321 | */ |
| 5322 | ASSERT(bno >= del.br_blockcount); |
| 5323 | bno -= del.br_blockcount; |
| 5324 | if (bno < got.br_startoff) { |
| 5325 | if (--lastx >= 0) |
| 5326 | xfs_bmbt_get_all(--ep, &got); |
| 5327 | } |
| 5328 | continue; |
| 5329 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5330 | /* |
| 5331 | * This one is already unwritten. |
| 5332 | * It must have a written left neighbor. |
| 5333 | * Unwrite the killed part of that one and |
| 5334 | * try again. |
| 5335 | */ |
| 5336 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5337 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5338 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5340 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | ASSERT(del.br_startblock == |
| 5342 | prev.br_startblock + prev.br_blockcount); |
| 5343 | if (prev.br_startoff < start) { |
| 5344 | mod = start - prev.br_startoff; |
| 5345 | prev.br_blockcount -= mod; |
| 5346 | prev.br_startblock += mod; |
| 5347 | prev.br_startoff = start; |
| 5348 | } |
| 5349 | prev.br_state = XFS_EXT_UNWRITTEN; |
| 5350 | error = xfs_bmap_add_extent(ip, lastx - 1, &cur, |
| 5351 | &prev, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5352 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | if (error) |
| 5354 | goto error0; |
| 5355 | goto nodelete; |
| 5356 | } else { |
| 5357 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5358 | del.br_state = XFS_EXT_UNWRITTEN; |
| 5359 | error = xfs_bmap_add_extent(ip, lastx, &cur, |
| 5360 | &del, firstblock, flist, &logflags, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5361 | delta, XFS_DATA_FORK, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | if (error) |
| 5363 | goto error0; |
| 5364 | goto nodelete; |
| 5365 | } |
| 5366 | } |
| 5367 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5368 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5369 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5370 | if (isrt) { |
| 5371 | xfs_filblks_t rtexts; |
| 5372 | |
| 5373 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5374 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5375 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5376 | (int64_t)rtexts, rsvd); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5377 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5378 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5379 | XFS_QMOPT_RES_RTBLKS); |
| 5380 | } else { |
| 5381 | xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 5382 | (int64_t)del.br_blockcount, rsvd); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5383 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5384 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | ip->i_delayed_blks -= del.br_blockcount; |
| 5388 | if (cur) |
| 5389 | cur->bc_private.b.flags |= |
| 5390 | XFS_BTCUR_BPRV_WASDEL; |
| 5391 | } else if (cur) |
| 5392 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5393 | /* |
| 5394 | * If it's the case where the directory code is running |
| 5395 | * with no block reservation, and the deleted block is in |
| 5396 | * the middle of its extent, and the resulting insert |
| 5397 | * of an extent would cause transformation to btree format, |
| 5398 | * then reject it. The calling code will then swap |
| 5399 | * blocks around instead. |
| 5400 | * We have to do this now, rather than waiting for the |
| 5401 | * conversion to btree format, since the transaction |
| 5402 | * will be dirty. |
| 5403 | */ |
| 5404 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5405 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5406 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5407 | del.br_startoff > got.br_startoff && |
| 5408 | del.br_startoff + del.br_blockcount < |
| 5409 | got.br_startoff + got.br_blockcount) { |
| 5410 | error = XFS_ERROR(ENOSPC); |
| 5411 | goto error0; |
| 5412 | } |
| 5413 | error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5414 | &tmp_logflags, delta, whichfork, rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | logflags |= tmp_logflags; |
| 5416 | if (error) |
| 5417 | goto error0; |
| 5418 | bno = del.br_startoff - 1; |
| 5419 | nodelete: |
| 5420 | lastx = ifp->if_lastex; |
| 5421 | /* |
| 5422 | * If not done go on to the next (previous) record. |
| 5423 | * Reset ep in case the extents array was re-alloced. |
| 5424 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5425 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
| 5427 | if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) || |
| 5428 | xfs_bmbt_get_startoff(ep) > bno) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5429 | if (--lastx >= 0) |
| 5430 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | } |
| 5432 | if (lastx >= 0) |
| 5433 | xfs_bmbt_get_all(ep, &got); |
| 5434 | extno++; |
| 5435 | } |
| 5436 | } |
| 5437 | ifp->if_lastex = lastx; |
| 5438 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5439 | ASSERT(ifp->if_ext_max == |
| 5440 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5441 | /* |
| 5442 | * Convert to a btree if necessary. |
| 5443 | */ |
| 5444 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5445 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5446 | ASSERT(cur == NULL); |
| 5447 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5448 | &cur, 0, &tmp_logflags, whichfork); |
| 5449 | logflags |= tmp_logflags; |
| 5450 | if (error) |
| 5451 | goto error0; |
| 5452 | } |
| 5453 | /* |
| 5454 | * transform from btree to extents, give it cur |
| 5455 | */ |
| 5456 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5457 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5458 | ASSERT(cur != NULL); |
| 5459 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5460 | whichfork); |
| 5461 | logflags |= tmp_logflags; |
| 5462 | if (error) |
| 5463 | goto error0; |
| 5464 | } |
| 5465 | /* |
| 5466 | * transform from extents to local? |
| 5467 | */ |
| 5468 | ASSERT(ifp->if_ext_max == |
| 5469 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5470 | error = 0; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5471 | if (delta && delta->xed_startoff != NULLFILEOFF) { |
| 5472 | /* A change was actually made. |
| 5473 | * Note that delta->xed_blockount is an offset at this |
| 5474 | * point and needs to be converted to a block count. |
| 5475 | */ |
| 5476 | ASSERT(delta->xed_blockcount > delta->xed_startoff); |
| 5477 | delta->xed_blockcount -= delta->xed_startoff; |
| 5478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | error0: |
| 5480 | /* |
| 5481 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5482 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5484 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5486 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5487 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5489 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | /* |
| 5491 | * Log inode even in the error case, if the transaction |
| 5492 | * is dirty we'll need to shut down the filesystem. |
| 5493 | */ |
| 5494 | if (logflags) |
| 5495 | xfs_trans_log_inode(tp, ip, logflags); |
| 5496 | if (cur) { |
| 5497 | if (!error) { |
| 5498 | *firstblock = cur->bc_private.b.firstblock; |
| 5499 | cur->bc_private.b.allocated = 0; |
| 5500 | } |
| 5501 | xfs_btree_del_cursor(cur, |
| 5502 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5503 | } |
| 5504 | return error; |
| 5505 | } |
| 5506 | |
| 5507 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5508 | * returns 1 for success, 0 if we failed to map the extent. |
| 5509 | */ |
| 5510 | STATIC int |
| 5511 | xfs_getbmapx_fix_eof_hole( |
| 5512 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5513 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5514 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5515 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5516 | __int64_t end, /* last block requested */ |
| 5517 | xfs_fsblock_t startblock) |
| 5518 | { |
| 5519 | __int64_t fixlen; |
| 5520 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5521 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5522 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5523 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5524 | |
| 5525 | if (startblock == HOLESTARTBLOCK) { |
| 5526 | mp = ip->i_mount; |
| 5527 | out->bmv_block = -1; |
| 5528 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5529 | fixlen -= out->bmv_offset; |
| 5530 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5531 | /* Came to hole at EOF. Trim it. */ |
| 5532 | if (fixlen <= 0) |
| 5533 | return 0; |
| 5534 | out->bmv_length = fixlen; |
| 5535 | } |
| 5536 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5537 | if (startblock == DELAYSTARTBLOCK) |
| 5538 | out->bmv_block = -2; |
| 5539 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5540 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5541 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5542 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5543 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5544 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5545 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | return 1; |
| 5549 | } |
| 5550 | |
| 5551 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5552 | * Get inode's extents as described in bmv, and format for output. |
| 5553 | * Calls formatter to fill the user's buffer until all extents |
| 5554 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5555 | * been filled, or until the formatter short-circuits the loop, |
| 5556 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | */ |
| 5558 | int /* error code */ |
| 5559 | xfs_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5560 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5561 | struct getbmapx *bmv, /* user bmap structure */ |
| 5562 | xfs_bmap_format_t formatter, /* format to user */ |
| 5563 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | { |
| 5565 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5566 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | __int64_t fixlen; /* length for -1 case */ |
| 5568 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | int lock; /* lock state */ |
| 5570 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5571 | xfs_mount_t *mp; /* file system mount point */ |
| 5572 | int nex; /* # of user extents can do */ |
| 5573 | int nexleft; /* # of user extents left */ |
| 5574 | int subnex; /* # of bmapi's can do */ |
| 5575 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5576 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5577 | int whichfork; /* data or attr fork */ |
| 5578 | int prealloced; /* this is a file with |
| 5579 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5580 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5582 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5585 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5586 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | if (whichfork == XFS_ATTR_FORK) { |
| 5589 | if (XFS_IFORK_Q(ip)) { |
| 5590 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5591 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5592 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5593 | return XFS_ERROR(EINVAL); |
| 5594 | } else if (unlikely( |
| 5595 | ip->i_d.di_aformat != 0 && |
| 5596 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5597 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5598 | ip->i_mount); |
| 5599 | return XFS_ERROR(EFSCORRUPTED); |
| 5600 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5601 | |
| 5602 | prealloced = 0; |
| 5603 | fixlen = 1LL << 32; |
| 5604 | } else { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5605 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5606 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5607 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5608 | return XFS_ERROR(EINVAL); |
| 5609 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5610 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5611 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | prealloced = 1; |
| 5613 | fixlen = XFS_MAXIOFFSET(mp); |
| 5614 | } else { |
| 5615 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5616 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | if (bmv->bmv_length == -1) { |
| 5621 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5622 | bmv->bmv_length = |
| 5623 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5624 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | bmv->bmv_entries = 0; |
| 5626 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5627 | } else if (bmv->bmv_length < 0) { |
| 5628 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | nex = bmv->bmv_count - 1; |
| 5632 | if (nex <= 0) |
| 5633 | return XFS_ERROR(EINVAL); |
| 5634 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5635 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5636 | |
| 5637 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5638 | return XFS_ERROR(ENOMEM); |
| 5639 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 5640 | if (!out) |
| 5641 | return XFS_ERROR(ENOMEM); |
| 5642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5644 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 5645 | if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { |
| 5646 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5647 | if (error) |
| 5648 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5651 | ASSERT(ip->i_delayed_blks == 0); |
| 5652 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | |
| 5654 | lock = xfs_ilock_map_shared(ip); |
| 5655 | |
| 5656 | /* |
| 5657 | * Don't let nex be bigger than the number of extents |
| 5658 | * we can have assuming alternating holes and real extents. |
| 5659 | */ |
| 5660 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5661 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5662 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5663 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5664 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5665 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | |
| 5667 | /* |
| 5668 | * Allocate enough space to handle "subnex" maps at a time. |
| 5669 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5670 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5672 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5673 | if (!map) |
| 5674 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | |
| 5676 | bmv->bmv_entries = 0; |
| 5677 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5678 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5679 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5680 | error = 0; |
| 5681 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | nexleft = nex; |
| 5685 | |
| 5686 | do { |
| 5687 | nmap = (nexleft > subnex) ? subnex : nexleft; |
| 5688 | error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5689 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5690 | bmapi_flags, NULL, 0, map, &nmap, |
| 5691 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5693 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | ASSERT(nmap <= subnex); |
| 5695 | |
| 5696 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5697 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5698 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5699 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5700 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5701 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5702 | out[cur_ext].bmv_offset = |
| 5703 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5704 | out[cur_ext].bmv_length = |
| 5705 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5706 | out[cur_ext].bmv_unused1 = 0; |
| 5707 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5708 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5709 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5710 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5711 | whichfork == XFS_ATTR_FORK) { |
| 5712 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5713 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5714 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5716 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5717 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5718 | prealloced, bmvend, |
| 5719 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5720 | goto out_free_map; |
| 5721 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5722 | nexleft--; |
| 5723 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5724 | out[cur_ext].bmv_offset + |
| 5725 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5726 | bmv->bmv_length = |
| 5727 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
| 5728 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5729 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | } |
| 5731 | } while (nmap && nexleft && bmv->bmv_length); |
| 5732 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5733 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5734 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5735 | out_unlock_ilock: |
| 5736 | xfs_iunlock_map_shared(ip, lock); |
| 5737 | out_unlock_iolock: |
| 5738 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5739 | |
| 5740 | for (i = 0; i < cur_ext; i++) { |
| 5741 | int full = 0; /* user array is full */ |
| 5742 | |
| 5743 | /* format results & advance arg */ |
| 5744 | error = formatter(&arg, &out[i], &full); |
| 5745 | if (error || full) |
| 5746 | break; |
| 5747 | } |
| 5748 | |
Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5749 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | return error; |
| 5751 | } |
| 5752 | |
| 5753 | /* |
| 5754 | * Check the last inode extent to determine whether this allocation will result |
| 5755 | * in blocks being allocated at the end of the file. When we allocate new data |
| 5756 | * blocks at the end of the file which do not start at the previous data block, |
| 5757 | * we will try to align the new blocks at stripe unit boundaries. |
| 5758 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 5759 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | xfs_bmap_isaeof( |
| 5761 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5762 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 5763 | int whichfork, /* data or attribute fork */ |
| 5764 | char *aeof) /* return value */ |
| 5765 | { |
| 5766 | int error; /* error return value */ |
| 5767 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5768 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5769 | xfs_extnum_t nextents; /* number of file extents */ |
| 5770 | xfs_bmbt_irec_t s; /* expanded extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | |
| 5772 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5773 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5774 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5775 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5776 | return error; |
| 5777 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5778 | if (nextents == 0) { |
| 5779 | *aeof = 1; |
| 5780 | return 0; |
| 5781 | } |
| 5782 | /* |
| 5783 | * Go to the last extent |
| 5784 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5785 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | xfs_bmbt_get_all(lastrec, &s); |
| 5787 | /* |
| 5788 | * Check we are allocating in the last extent (for delayed allocations) |
| 5789 | * or past the last extent for non-delayed allocations. |
| 5790 | */ |
| 5791 | *aeof = (off >= s.br_startoff && |
| 5792 | off < s.br_startoff + s.br_blockcount && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5793 | isnullstartblock(s.br_startblock)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | off >= s.br_startoff + s.br_blockcount; |
| 5795 | return 0; |
| 5796 | } |
| 5797 | |
| 5798 | /* |
| 5799 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 5800 | * the allocation to a stripe unit boundary. |
| 5801 | */ |
| 5802 | int /* error */ |
| 5803 | xfs_bmap_eof( |
| 5804 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5805 | xfs_fileoff_t endoff, /* file offset in fsblocks */ |
| 5806 | int whichfork, /* data or attribute fork */ |
| 5807 | int *eof) /* result value */ |
| 5808 | { |
| 5809 | xfs_fsblock_t blockcount; /* extent block count */ |
| 5810 | int error; /* error return value */ |
| 5811 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5812 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5813 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | xfs_fileoff_t startoff; /* extent starting file offset */ |
| 5815 | |
| 5816 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5817 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5818 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5819 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5820 | return error; |
| 5821 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5822 | if (nextents == 0) { |
| 5823 | *eof = 1; |
| 5824 | return 0; |
| 5825 | } |
| 5826 | /* |
| 5827 | * Go to the last extent |
| 5828 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5829 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | startoff = xfs_bmbt_get_startoff(lastrec); |
| 5831 | blockcount = xfs_bmbt_get_blockcount(lastrec); |
| 5832 | *eof = endoff >= startoff + blockcount; |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
| 5836 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | STATIC |
| 5838 | xfs_buf_t * |
| 5839 | xfs_bmap_get_bp( |
| 5840 | xfs_btree_cur_t *cur, |
| 5841 | xfs_fsblock_t bno) |
| 5842 | { |
| 5843 | int i; |
| 5844 | xfs_buf_t *bp; |
| 5845 | |
| 5846 | if (!cur) |
| 5847 | return(NULL); |
| 5848 | |
| 5849 | bp = NULL; |
| 5850 | for(i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5851 | bp = cur->bc_bufs[i]; |
| 5852 | if (!bp) break; |
| 5853 | if (XFS_BUF_ADDR(bp) == bno) |
| 5854 | break; /* Found it */ |
| 5855 | } |
| 5856 | if (i == XFS_BTREE_MAXLEVELS) |
| 5857 | bp = NULL; |
| 5858 | |
| 5859 | if (!bp) { /* Chase down all the log items to see if the bp is there */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame^] | 5860 | struct xfs_log_item_desc *lidp; |
| 5861 | struct xfs_buf_log_item *bip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame^] | 5863 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
| 5864 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 5865 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 5866 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 5867 | return bip->bli_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | } |
| 5869 | } |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame^] | 5870 | |
| 5871 | return bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | } |
| 5873 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5874 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5876 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | xfs_mount_t *mp, |
| 5878 | int root, |
| 5879 | short sz) |
| 5880 | { |
| 5881 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5882 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | xfs_bmbt_key_t *prevp, *keyp; |
| 5884 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5885 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | |
| 5887 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5888 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5890 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
| 5892 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5893 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5894 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | } |
| 5896 | prevp = keyp; |
| 5897 | |
| 5898 | /* |
| 5899 | * Compare the block numbers to see if there are dups. |
| 5900 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5901 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5902 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5903 | else |
| 5904 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5905 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5906 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5907 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5908 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5909 | else |
| 5910 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5911 | if (*thispa == *pp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | cmn_err(CE_WARN, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5913 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5914 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5916 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | } |
| 5918 | } |
| 5919 | } |
| 5920 | } |
| 5921 | |
| 5922 | /* |
| 5923 | * Check that the extents for the inode ip are in the right order in all |
| 5924 | * btree leaves. |
| 5925 | */ |
| 5926 | |
| 5927 | STATIC void |
| 5928 | xfs_bmap_check_leaf_extents( |
| 5929 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5930 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5931 | int whichfork) /* data or attr fork */ |
| 5932 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5933 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5935 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5936 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5937 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | xfs_ifork_t *ifp; /* fork structure */ |
| 5939 | int level; /* btree level, for checking */ |
| 5940 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5941 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5942 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5943 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5944 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | int bp_release = 0; |
| 5946 | |
| 5947 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5948 | return; |
| 5949 | } |
| 5950 | |
| 5951 | bno = NULLFSBLOCK; |
| 5952 | mp = ip->i_mount; |
| 5953 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5954 | block = ifp->if_broot; |
| 5955 | /* |
| 5956 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5957 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5958 | level = be16_to_cpu(block->bb_level); |
| 5959 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5961 | 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] | 5962 | bno = be64_to_cpu(*pp); |
| 5963 | |
| 5964 | ASSERT(bno != NULLDFSBNO); |
| 5965 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5966 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | /* |
| 5969 | * Go down the tree until leaf level is reached, following the first |
| 5970 | * pointer (leftmost) at each level. |
| 5971 | */ |
| 5972 | while (level-- > 0) { |
| 5973 | /* See if buf is in cur first */ |
| 5974 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5975 | if (bp) { |
| 5976 | bp_release = 0; |
| 5977 | } else { |
| 5978 | bp_release = 1; |
| 5979 | } |
| 5980 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5981 | XFS_BMAP_BTREE_REF))) |
| 5982 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5983 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5985 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | error0); |
| 5987 | if (level == 0) |
| 5988 | break; |
| 5989 | |
| 5990 | /* |
| 5991 | * Check this block for basic sanity (increasing keys and |
| 5992 | * no duplicate blocks). |
| 5993 | */ |
| 5994 | |
| 5995 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5996 | 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] | 5997 | bno = be64_to_cpu(*pp); |
| 5998 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | if (bp_release) { |
| 6000 | bp_release = 0; |
| 6001 | xfs_trans_brelse(NULL, bp); |
| 6002 | } |
| 6003 | } |
| 6004 | |
| 6005 | /* |
| 6006 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 6007 | */ |
| 6008 | i = 0; |
| 6009 | |
| 6010 | /* |
| 6011 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 6012 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | xfs_fsblock_t nextbno; |
| 6015 | xfs_extnum_t num_recs; |
| 6016 | |
| 6017 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6018 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | |
| 6020 | /* |
| 6021 | * Read-ahead the next leaf block, if any. |
| 6022 | */ |
| 6023 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6024 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | |
| 6026 | /* |
| 6027 | * Check all the extents to make sure they are OK. |
| 6028 | * If we had a previous block, the last entry should |
| 6029 | * conform with the first entry in this one. |
| 6030 | */ |
| 6031 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6032 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6033 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6034 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 6035 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 6036 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6037 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6038 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6039 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 6040 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 6041 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 6042 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6043 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 6046 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | i += num_recs; |
| 6048 | if (bp_release) { |
| 6049 | bp_release = 0; |
| 6050 | xfs_trans_brelse(NULL, bp); |
| 6051 | } |
| 6052 | bno = nextbno; |
| 6053 | /* |
| 6054 | * If we've reached the end, stop. |
| 6055 | */ |
| 6056 | if (bno == NULLFSBLOCK) |
| 6057 | break; |
| 6058 | |
| 6059 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 6060 | if (bp) { |
| 6061 | bp_release = 0; |
| 6062 | } else { |
| 6063 | bp_release = 1; |
| 6064 | } |
| 6065 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 6066 | XFS_BMAP_BTREE_REF))) |
| 6067 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6068 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | } |
| 6070 | if (bp_release) { |
| 6071 | bp_release = 0; |
| 6072 | xfs_trans_brelse(NULL, bp); |
| 6073 | } |
| 6074 | return; |
| 6075 | |
| 6076 | error0: |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6077 | cmn_err(CE_WARN, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | if (bp_release) |
| 6079 | xfs_trans_brelse(NULL, bp); |
| 6080 | error_norelse: |
| 6081 | cmn_err(CE_WARN, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 6082 | __func__, i); |
| 6083 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | return; |
| 6085 | } |
| 6086 | #endif |
| 6087 | |
| 6088 | /* |
| 6089 | * Count fsblocks of the given fork. |
| 6090 | */ |
| 6091 | int /* error */ |
| 6092 | xfs_bmap_count_blocks( |
| 6093 | xfs_trans_t *tp, /* transaction pointer */ |
| 6094 | xfs_inode_t *ip, /* incore inode */ |
| 6095 | int whichfork, /* data or attr fork */ |
| 6096 | int *count) /* out: count of blocks */ |
| 6097 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6098 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6100 | xfs_ifork_t *ifp; /* fork structure */ |
| 6101 | int level; /* btree level, for checking */ |
| 6102 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6103 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | |
| 6105 | bno = NULLFSBLOCK; |
| 6106 | mp = ip->i_mount; |
| 6107 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6108 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6109 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6111 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | return 0; |
| 6113 | } |
| 6114 | |
| 6115 | /* |
| 6116 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6117 | */ |
| 6118 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6119 | level = be16_to_cpu(block->bb_level); |
| 6120 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6121 | 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] | 6122 | bno = be64_to_cpu(*pp); |
| 6123 | ASSERT(bno != NULLDFSBNO); |
| 6124 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6125 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6127 | 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] | 6128 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 6129 | mp); |
| 6130 | return XFS_ERROR(EFSCORRUPTED); |
| 6131 | } |
| 6132 | |
| 6133 | return 0; |
| 6134 | } |
| 6135 | |
| 6136 | /* |
| 6137 | * Recursively walks each level of a btree |
| 6138 | * to count total fsblocks is use. |
| 6139 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 6140 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | xfs_bmap_count_tree( |
| 6142 | xfs_mount_t *mp, /* file system mount point */ |
| 6143 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6144 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | xfs_fsblock_t blockno, /* file system block number */ |
| 6146 | int levelin, /* level in btree */ |
| 6147 | int *count) /* Count of blocks */ |
| 6148 | { |
| 6149 | int error; |
| 6150 | xfs_buf_t *bp, *nbp; |
| 6151 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6152 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | xfs_fsblock_t bno = blockno; |
| 6154 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6155 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6157 | |
| 6158 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 6159 | return error; |
| 6160 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6161 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | |
| 6163 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 6164 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6165 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | while (nextbno != NULLFSBLOCK) { |
| 6167 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6168 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6169 | return error; |
| 6170 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6171 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6172 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | xfs_trans_brelse(tp, nbp); |
| 6174 | } |
| 6175 | |
| 6176 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6177 | 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] | 6178 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6180 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | xfs_trans_brelse(tp, bp); |
| 6182 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6183 | XFS_ERRLEVEL_LOW, mp); |
| 6184 | return XFS_ERROR(EFSCORRUPTED); |
| 6185 | } |
| 6186 | xfs_trans_brelse(tp, bp); |
| 6187 | } else { |
| 6188 | /* count all level 1 nodes and their leaves */ |
| 6189 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6190 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6191 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6192 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | xfs_trans_brelse(tp, bp); |
| 6194 | if (nextbno == NULLFSBLOCK) |
| 6195 | break; |
| 6196 | bno = nextbno; |
| 6197 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6198 | XFS_BMAP_BTREE_REF))) |
| 6199 | return error; |
| 6200 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6201 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | } |
| 6203 | } |
| 6204 | return 0; |
| 6205 | } |
| 6206 | |
| 6207 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6208 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6210 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6212 | xfs_ifork_t *ifp, |
| 6213 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | int numrecs, |
| 6215 | int *count) |
| 6216 | { |
| 6217 | int b; |
| 6218 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6219 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6220 | 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] | 6221 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6222 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6223 | } |
| 6224 | |
| 6225 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6226 | * Count leaf blocks given a range of extent records originally |
| 6227 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6228 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6229 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6230 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6231 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6232 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6233 | int numrecs, |
| 6234 | int *count) |
| 6235 | { |
| 6236 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6237 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6238 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6239 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6240 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | } |