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 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 93 | * allocation to a real allocation. |
| 94 | */ |
| 95 | STATIC int /* error */ |
| 96 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 97 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 99 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 101 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 103 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 104 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 105 | int *logflagsp); /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 109 | * to a delayed allocation. |
| 110 | */ |
| 111 | STATIC int /* error */ |
| 112 | xfs_bmap_add_extent_hole_delay( |
| 113 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 114 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 115 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 116 | int *logflagsp); /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 120 | * to a real allocation. |
| 121 | */ |
| 122 | STATIC int /* error */ |
| 123 | xfs_bmap_add_extent_hole_real( |
| 124 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 125 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 127 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int *logflagsp, /* inode logging flags */ |
| 129 | int whichfork); /* data or attr fork */ |
| 130 | |
| 131 | /* |
| 132 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 133 | * allocation to a real allocation or vice versa. |
| 134 | */ |
| 135 | STATIC int /* error */ |
| 136 | xfs_bmap_add_extent_unwritten_real( |
| 137 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 138 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 140 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 141 | int *logflagsp); /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 145 | * It figures out where to ask the underlying allocator to put the new extent. |
| 146 | */ |
| 147 | STATIC int /* error */ |
| 148 | xfs_bmap_alloc( |
| 149 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 150 | |
| 151 | /* |
| 152 | * Transform a btree format file with only one leaf node, where the |
| 153 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 154 | * 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] | 155 | * give up the space for the btree root and pitch the leaf block. |
| 156 | */ |
| 157 | STATIC int /* error */ |
| 158 | xfs_bmap_btree_to_extents( |
| 159 | xfs_trans_t *tp, /* transaction pointer */ |
| 160 | xfs_inode_t *ip, /* incore inode pointer */ |
| 161 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 162 | int *logflagsp, /* inode logging flags */ |
| 163 | int whichfork); /* data or attr fork */ |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | * Remove the entry "free" from the free item list. Prev points to the |
| 167 | * previous entry, unless "free" is the head of the list. |
| 168 | */ |
| 169 | STATIC void |
| 170 | xfs_bmap_del_free( |
| 171 | xfs_bmap_free_t *flist, /* free item list header */ |
| 172 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 173 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 174 | |
| 175 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * Convert an extents-format file into a btree-format file. |
| 177 | * The new file will have a root block (in the inode) and a single child block. |
| 178 | */ |
| 179 | STATIC int /* error */ |
| 180 | xfs_bmap_extents_to_btree( |
| 181 | xfs_trans_t *tp, /* transaction pointer */ |
| 182 | xfs_inode_t *ip, /* incore inode pointer */ |
| 183 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 184 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 185 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 186 | int wasdel, /* converting a delayed alloc */ |
| 187 | int *logflagsp, /* inode logging flags */ |
| 188 | int whichfork); /* data or attr fork */ |
| 189 | |
| 190 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * Convert a local file to an extents file. |
| 192 | * This code is sort of bogus, since the file data needs to get |
| 193 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 194 | */ |
| 195 | STATIC int /* error */ |
| 196 | xfs_bmap_local_to_extents( |
| 197 | xfs_trans_t *tp, /* transaction pointer */ |
| 198 | xfs_inode_t *ip, /* incore inode pointer */ |
| 199 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 200 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 201 | int *logflagsp, /* inode logging flags */ |
| 202 | int whichfork); /* data or attr fork */ |
| 203 | |
| 204 | /* |
| 205 | * Search the extents list for the inode, for the extent containing bno. |
| 206 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 207 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 208 | * Else, *lastxp will be set to the index of the found |
| 209 | * entry; *gotp will contain the entry. |
| 210 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 211 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | xfs_bmap_search_extents( |
| 213 | xfs_inode_t *ip, /* incore inode pointer */ |
| 214 | xfs_fileoff_t bno, /* block number searched for */ |
| 215 | int whichfork, /* data or attr fork */ |
| 216 | int *eofp, /* out: end of file found */ |
| 217 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 218 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 219 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 220 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 221 | /* |
| 222 | * Check the last inode extent to determine whether this allocation will result |
| 223 | * in blocks being allocated at the end of the file. When we allocate new data |
| 224 | * blocks at the end of the file which do not start at the previous data block, |
| 225 | * we will try to align the new blocks at stripe unit boundaries. |
| 226 | */ |
| 227 | STATIC int /* error */ |
| 228 | xfs_bmap_isaeof( |
| 229 | xfs_inode_t *ip, /* incore inode pointer */ |
| 230 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 231 | int whichfork, /* data or attribute fork */ |
| 232 | char *aeof); /* return value */ |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* |
| 235 | * Compute the worst-case number of indirect blocks that will be used |
| 236 | * for ip's delayed extent of length "len". |
| 237 | */ |
| 238 | STATIC xfs_filblks_t |
| 239 | xfs_bmap_worst_indlen( |
| 240 | xfs_inode_t *ip, /* incore inode pointer */ |
| 241 | xfs_filblks_t len); /* delayed extent length */ |
| 242 | |
| 243 | #ifdef DEBUG |
| 244 | /* |
| 245 | * Perform various validation checks on the values being returned |
| 246 | * from xfs_bmapi(). |
| 247 | */ |
| 248 | STATIC void |
| 249 | xfs_bmap_validate_ret( |
| 250 | xfs_fileoff_t bno, |
| 251 | xfs_filblks_t len, |
| 252 | int flags, |
| 253 | xfs_bmbt_irec_t *mval, |
| 254 | int nmap, |
| 255 | int ret_nmap); |
| 256 | #else |
| 257 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 258 | #endif /* DEBUG */ |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | STATIC int |
| 261 | xfs_bmap_count_tree( |
| 262 | xfs_mount_t *mp, |
| 263 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 264 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | xfs_fsblock_t blockno, |
| 266 | int levelin, |
| 267 | int *count); |
| 268 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 269 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 271 | xfs_ifork_t *ifp, |
| 272 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | int numrecs, |
| 274 | int *count); |
| 275 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 276 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 277 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 278 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 279 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 280 | int numrecs, |
| 281 | int *count); |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* |
| 284 | * Bmap internal routines. |
| 285 | */ |
| 286 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 287 | STATIC int /* error */ |
| 288 | xfs_bmbt_lookup_eq( |
| 289 | struct xfs_btree_cur *cur, |
| 290 | xfs_fileoff_t off, |
| 291 | xfs_fsblock_t bno, |
| 292 | xfs_filblks_t len, |
| 293 | int *stat) /* success/failure */ |
| 294 | { |
| 295 | cur->bc_rec.b.br_startoff = off; |
| 296 | cur->bc_rec.b.br_startblock = bno; |
| 297 | cur->bc_rec.b.br_blockcount = len; |
| 298 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 299 | } |
| 300 | |
| 301 | STATIC int /* error */ |
| 302 | xfs_bmbt_lookup_ge( |
| 303 | struct xfs_btree_cur *cur, |
| 304 | xfs_fileoff_t off, |
| 305 | xfs_fsblock_t bno, |
| 306 | xfs_filblks_t len, |
| 307 | int *stat) /* success/failure */ |
| 308 | { |
| 309 | cur->bc_rec.b.br_startoff = off; |
| 310 | cur->bc_rec.b.br_startblock = bno; |
| 311 | cur->bc_rec.b.br_blockcount = len; |
| 312 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 313 | } |
| 314 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 315 | /* |
| 316 | * Update the record referred to by cur to the value given |
| 317 | * by [off, bno, len, state]. |
| 318 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 319 | */ |
| 320 | STATIC int |
| 321 | xfs_bmbt_update( |
| 322 | struct xfs_btree_cur *cur, |
| 323 | xfs_fileoff_t off, |
| 324 | xfs_fsblock_t bno, |
| 325 | xfs_filblks_t len, |
| 326 | xfs_exntst_t state) |
| 327 | { |
| 328 | union xfs_btree_rec rec; |
| 329 | |
| 330 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 331 | return xfs_btree_update(cur, &rec); |
| 332 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* |
| 335 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 336 | */ |
| 337 | STATIC int /* error */ |
| 338 | xfs_bmap_add_attrfork_btree( |
| 339 | xfs_trans_t *tp, /* transaction pointer */ |
| 340 | xfs_inode_t *ip, /* incore inode pointer */ |
| 341 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 342 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 343 | int *flags) /* inode logging flags */ |
| 344 | { |
| 345 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 346 | int error; /* error return value */ |
| 347 | xfs_mount_t *mp; /* file system mount struct */ |
| 348 | int stat; /* newroot status */ |
| 349 | |
| 350 | mp = ip->i_mount; |
| 351 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 352 | *flags |= XFS_ILOG_DBROOT; |
| 353 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 354 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | cur->bc_private.b.flist = flist; |
| 356 | cur->bc_private.b.firstblock = *firstblock; |
| 357 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 358 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 359 | /* must be at least one entry */ |
| 360 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 361 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | goto error0; |
| 363 | if (stat == 0) { |
| 364 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 365 | return XFS_ERROR(ENOSPC); |
| 366 | } |
| 367 | *firstblock = cur->bc_private.b.firstblock; |
| 368 | cur->bc_private.b.allocated = 0; |
| 369 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 370 | } |
| 371 | return 0; |
| 372 | error0: |
| 373 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 374 | return error; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 379 | */ |
| 380 | STATIC int /* error */ |
| 381 | xfs_bmap_add_attrfork_extents( |
| 382 | xfs_trans_t *tp, /* transaction pointer */ |
| 383 | xfs_inode_t *ip, /* incore inode pointer */ |
| 384 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 385 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 386 | int *flags) /* inode logging flags */ |
| 387 | { |
| 388 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 389 | int error; /* error return value */ |
| 390 | |
| 391 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 392 | return 0; |
| 393 | cur = NULL; |
| 394 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 395 | flags, XFS_DATA_FORK); |
| 396 | if (cur) { |
| 397 | cur->bc_private.b.allocated = 0; |
| 398 | xfs_btree_del_cursor(cur, |
| 399 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 400 | } |
| 401 | return error; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 406 | */ |
| 407 | STATIC int /* error */ |
| 408 | xfs_bmap_add_attrfork_local( |
| 409 | xfs_trans_t *tp, /* transaction pointer */ |
| 410 | xfs_inode_t *ip, /* incore inode pointer */ |
| 411 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 412 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 413 | int *flags) /* inode logging flags */ |
| 414 | { |
| 415 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 416 | int error; /* error return value */ |
| 417 | xfs_mount_t *mp; /* mount structure pointer */ |
| 418 | |
| 419 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 420 | return 0; |
| 421 | if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) { |
| 422 | mp = ip->i_mount; |
| 423 | memset(&dargs, 0, sizeof(dargs)); |
| 424 | dargs.dp = ip; |
| 425 | dargs.firstblock = firstblock; |
| 426 | dargs.flist = flist; |
| 427 | dargs.total = mp->m_dirblkfsbs; |
| 428 | dargs.whichfork = XFS_DATA_FORK; |
| 429 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 430 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } else |
| 432 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 433 | XFS_DATA_FORK); |
| 434 | return error; |
| 435 | } |
| 436 | |
| 437 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 438 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | * after allocating space (or doing a delayed allocation). |
| 440 | */ |
| 441 | STATIC int /* error */ |
| 442 | xfs_bmap_add_extent( |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 443 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 445 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 447 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 449 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 450 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 451 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
| 453 | xfs_btree_cur_t *cur; /* btree cursor or null */ |
| 454 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 455 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 456 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | xfs_ifork_t *ifp; /* inode fork ptr */ |
| 458 | int logflags; /* returned value */ |
| 459 | xfs_extnum_t nextents; /* number of extents in file now */ |
| 460 | |
| 461 | XFS_STATS_INC(xs_add_exlist); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | cur = *curp; |
| 464 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 465 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | da_old = da_new = 0; |
| 467 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 468 | |
| 469 | ASSERT(*idx >= 0); |
| 470 | ASSERT(*idx <= nextents); |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* |
| 473 | * This is the first extent added to a new/empty file. |
| 474 | * Special case this one, so other routines get to assume there are |
| 475 | * already extents in the list. |
| 476 | */ |
| 477 | if (nextents == 0) { |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 478 | xfs_iext_insert(ip, *idx, 1, new, |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 479 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | ASSERT(cur == NULL); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 482 | |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 483 | if (!isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 485 | logflags = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } else |
| 487 | logflags = 0; |
| 488 | } |
| 489 | /* |
| 490 | * Any kind of new delayed allocation goes here. |
| 491 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 492 | else if (isnullstartblock(new->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (cur) |
| 494 | ASSERT((cur->bc_private.b.flags & |
| 495 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 496 | error = xfs_bmap_add_extent_hole_delay(ip, idx, new, |
| 497 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | /* |
| 500 | * Real allocation off the end of the file. |
| 501 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 502 | else if (*idx == nextents) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | if (cur) |
| 504 | ASSERT((cur->bc_private.b.flags & |
| 505 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 506 | error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, |
| 507 | &logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } else { |
| 509 | xfs_bmbt_irec_t prev; /* old extent at offset idx */ |
| 510 | |
| 511 | /* |
| 512 | * Get the record referred to by idx. |
| 513 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 514 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | /* |
| 516 | * If it's a real allocation record, and the new allocation ends |
| 517 | * after the start of the referred to record, then we're filling |
| 518 | * in a delayed or unwritten allocation with a real one, or |
| 519 | * converting real back to unwritten. |
| 520 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 521 | if (!isnullstartblock(new->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | new->br_startoff + new->br_blockcount > prev.br_startoff) { |
| 523 | if (prev.br_state != XFS_EXT_UNWRITTEN && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 524 | isnullstartblock(prev.br_startblock)) { |
| 525 | da_old = startblockval(prev.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | if (cur) |
| 527 | ASSERT(cur->bc_private.b.flags & |
| 528 | XFS_BTCUR_BPRV_WASDEL); |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 529 | error = xfs_bmap_add_extent_delay_real(tp, ip, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 530 | idx, &cur, new, &da_new, |
| 531 | first, flist, &logflags); |
| 532 | } else { |
| 533 | ASSERT(new->br_state == XFS_EXT_NORM || |
| 534 | new->br_state == XFS_EXT_UNWRITTEN); |
| 535 | |
| 536 | error = xfs_bmap_add_extent_unwritten_real(ip, |
| 537 | idx, &cur, new, &logflags); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 538 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | /* |
| 543 | * Otherwise we're filling in a hole with an allocation. |
| 544 | */ |
| 545 | else { |
| 546 | if (cur) |
| 547 | ASSERT((cur->bc_private.b.flags & |
| 548 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 549 | error = xfs_bmap_add_extent_hole_real(ip, idx, cur, |
| 550 | new, &logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 554 | if (error) |
| 555 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | ASSERT(*curp == cur || *curp == NULL); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* |
| 559 | * Convert to a btree if necessary. |
| 560 | */ |
| 561 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 562 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 563 | int tmp_logflags; /* partial log flag return val */ |
| 564 | |
| 565 | ASSERT(cur == NULL); |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 566 | error = xfs_bmap_extents_to_btree(tp, ip, first, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | flist, &cur, da_old > 0, &tmp_logflags, whichfork); |
| 568 | logflags |= tmp_logflags; |
| 569 | if (error) |
| 570 | goto done; |
| 571 | } |
| 572 | /* |
| 573 | * Adjust for changes in reserved delayed indirect blocks. |
| 574 | * Nothing to do for disk quotas here. |
| 575 | */ |
| 576 | if (da_old || da_new) { |
| 577 | xfs_filblks_t nblks; |
| 578 | |
| 579 | nblks = da_new; |
| 580 | if (cur) |
| 581 | nblks += cur->bc_private.b.allocated; |
| 582 | ASSERT(nblks <= da_old); |
| 583 | if (nblks < da_old) |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 584 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 585 | (int64_t)(da_old - nblks), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | /* |
| 588 | * Clear out the allocated field, done with it now in any case. |
| 589 | */ |
| 590 | if (cur) { |
| 591 | cur->bc_private.b.allocated = 0; |
| 592 | *curp = cur; |
| 593 | } |
| 594 | done: |
| 595 | #ifdef DEBUG |
| 596 | if (!error) |
| 597 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
| 598 | #endif |
| 599 | *logflagsp = logflags; |
| 600 | return error; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 605 | * allocation to a real allocation. |
| 606 | */ |
| 607 | STATIC int /* error */ |
| 608 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 609 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 611 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 613 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 615 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 616 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 617 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 620 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 621 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 624 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 626 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 627 | /* left is 0, right is 1, prev is 2 */ |
| 628 | int rval=0; /* return value (logging flags) */ |
| 629 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 630 | xfs_filblks_t temp=0; /* value for dnew calculations */ |
| 631 | xfs_filblks_t temp2=0;/* value for dnew calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
| 634 | #define LEFT r[0] |
| 635 | #define RIGHT r[1] |
| 636 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * Set up a bunch of variables to make the tests simpler. |
| 640 | */ |
| 641 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 642 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 643 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | xfs_bmbt_get_all(ep, &PREV); |
| 645 | new_endoff = new->br_startoff + new->br_blockcount; |
| 646 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 647 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | /* |
| 650 | * Set flags determining what part of the previous delayed allocation |
| 651 | * extent is being replaced by a real allocation. |
| 652 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 653 | if (PREV.br_startoff == new->br_startoff) |
| 654 | state |= BMAP_LEFT_FILLING; |
| 655 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 656 | state |= BMAP_RIGHT_FILLING; |
| 657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | /* |
| 659 | * Check and set flags if this segment has a left neighbor. |
| 660 | * Don't set contiguous if the combined extent would be too large. |
| 661 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 662 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 663 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 664 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 665 | |
| 666 | if (isnullstartblock(LEFT.br_startblock)) |
| 667 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 669 | |
| 670 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 671 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 672 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 673 | LEFT.br_state == new->br_state && |
| 674 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 675 | state |= BMAP_LEFT_CONTIG; |
| 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | /* |
| 678 | * Check and set flags if this segment has a right neighbor. |
| 679 | * Don't set contiguous if the combined extent would be too large. |
| 680 | * Also check for all-three-contiguous being too large. |
| 681 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 682 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 683 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 684 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 685 | |
| 686 | if (isnullstartblock(RIGHT.br_startblock)) |
| 687 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 689 | |
| 690 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 691 | new_endoff == RIGHT.br_startoff && |
| 692 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 693 | new->br_state == RIGHT.br_state && |
| 694 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 695 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 696 | BMAP_RIGHT_FILLING)) != |
| 697 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 698 | BMAP_RIGHT_FILLING) || |
| 699 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 700 | <= MAXEXTLEN)) |
| 701 | state |= BMAP_RIGHT_CONTIG; |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | error = 0; |
| 704 | /* |
| 705 | * Switch out based on the FILLING and CONTIG state bits. |
| 706 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 707 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 708 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 709 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 710 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* |
| 712 | * Filling in all of a previously delayed allocation extent. |
| 713 | * The left and right neighbors are both contiguous with new. |
| 714 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 715 | --*idx; |
| 716 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 717 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | LEFT.br_blockcount + PREV.br_blockcount + |
| 719 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 720 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 721 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 722 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | ip->i_d.di_nextents--; |
| 724 | if (cur == NULL) |
| 725 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 726 | else { |
| 727 | rval = XFS_ILOG_CORE; |
| 728 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 729 | RIGHT.br_startblock, |
| 730 | RIGHT.br_blockcount, &i))) |
| 731 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 732 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 733 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 735 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 736 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 738 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 740 | LEFT.br_startblock, |
| 741 | LEFT.br_blockcount + |
| 742 | PREV.br_blockcount + |
| 743 | RIGHT.br_blockcount, LEFT.br_state))) |
| 744 | goto done; |
| 745 | } |
| 746 | *dnew = 0; |
| 747 | break; |
| 748 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 749 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | /* |
| 751 | * Filling in all of a previously delayed allocation extent. |
| 752 | * The left neighbor is contiguous, the right is not. |
| 753 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 754 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 755 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 756 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 757 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 758 | LEFT.br_blockcount + PREV.br_blockcount); |
| 759 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 760 | |
| 761 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | if (cur == NULL) |
| 763 | rval = XFS_ILOG_DEXT; |
| 764 | else { |
| 765 | rval = 0; |
| 766 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 767 | LEFT.br_startblock, LEFT.br_blockcount, |
| 768 | &i))) |
| 769 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 770 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 772 | LEFT.br_startblock, |
| 773 | LEFT.br_blockcount + |
| 774 | PREV.br_blockcount, LEFT.br_state))) |
| 775 | goto done; |
| 776 | } |
| 777 | *dnew = 0; |
| 778 | break; |
| 779 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 780 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | /* |
| 782 | * Filling in all of a previously delayed allocation extent. |
| 783 | * The right neighbor is contiguous, the left is not. |
| 784 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 785 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 787 | xfs_bmbt_set_blockcount(ep, |
| 788 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 789 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 790 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 791 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (cur == NULL) |
| 793 | rval = XFS_ILOG_DEXT; |
| 794 | else { |
| 795 | rval = 0; |
| 796 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 797 | RIGHT.br_startblock, |
| 798 | RIGHT.br_blockcount, &i))) |
| 799 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 800 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 802 | new->br_startblock, |
| 803 | PREV.br_blockcount + |
| 804 | RIGHT.br_blockcount, PREV.br_state))) |
| 805 | goto done; |
| 806 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | *dnew = 0; |
| 809 | break; |
| 810 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 811 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* |
| 813 | * Filling in all of a previously delayed allocation extent. |
| 814 | * Neither the left nor right neighbors are contiguous with |
| 815 | * the new one. |
| 816 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 817 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 819 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | ip->i_d.di_nextents++; |
| 822 | if (cur == NULL) |
| 823 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 824 | else { |
| 825 | rval = XFS_ILOG_CORE; |
| 826 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 827 | new->br_startblock, new->br_blockcount, |
| 828 | &i))) |
| 829 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 830 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 832 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 834 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | *dnew = 0; |
| 838 | break; |
| 839 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 840 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | /* |
| 842 | * Filling in the first part of a previous delayed allocation. |
| 843 | * The left neighbor is contiguous. |
| 844 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 845 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 846 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | LEFT.br_blockcount + new->br_blockcount); |
| 848 | xfs_bmbt_set_startoff(ep, |
| 849 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 850 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 853 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | if (cur == NULL) |
| 856 | rval = XFS_ILOG_DEXT; |
| 857 | else { |
| 858 | rval = 0; |
| 859 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 860 | LEFT.br_startblock, LEFT.br_blockcount, |
| 861 | &i))) |
| 862 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 863 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 865 | LEFT.br_startblock, |
| 866 | LEFT.br_blockcount + |
| 867 | new->br_blockcount, |
| 868 | LEFT.br_state))) |
| 869 | goto done; |
| 870 | } |
| 871 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 872 | startblockval(PREV.br_startblock)); |
| 873 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 874 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 875 | |
| 876 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | *dnew = temp; |
| 878 | break; |
| 879 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 880 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* |
| 882 | * Filling in the first part of a previous delayed allocation. |
| 883 | * The left neighbor is not contiguous. |
| 884 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 885 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 887 | temp = PREV.br_blockcount - new->br_blockcount; |
| 888 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 889 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | ip->i_d.di_nextents++; |
| 891 | if (cur == NULL) |
| 892 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 893 | else { |
| 894 | rval = XFS_ILOG_CORE; |
| 895 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 896 | new->br_startblock, new->br_blockcount, |
| 897 | &i))) |
| 898 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 899 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 901 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 903 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | } |
| 905 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 906 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 907 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | first, flist, &cur, 1, &tmp_rval, |
| 909 | XFS_DATA_FORK); |
| 910 | rval |= tmp_rval; |
| 911 | if (error) |
| 912 | goto done; |
| 913 | } |
| 914 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 915 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 917 | ep = xfs_iext_get_ext(ifp, *idx + 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 918 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 919 | trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_); |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | *dnew = temp; |
| 922 | break; |
| 923 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 924 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | /* |
| 926 | * Filling in the last part of a previous delayed allocation. |
| 927 | * The right neighbor is contiguous with the new allocation. |
| 928 | */ |
| 929 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 930 | trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 932 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 933 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | new->br_blockcount + RIGHT.br_blockcount, |
| 935 | RIGHT.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 936 | trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | if (cur == NULL) |
| 938 | rval = XFS_ILOG_DEXT; |
| 939 | else { |
| 940 | rval = 0; |
| 941 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 942 | RIGHT.br_startblock, |
| 943 | RIGHT.br_blockcount, &i))) |
| 944 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 945 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 947 | new->br_startblock, |
| 948 | new->br_blockcount + |
| 949 | RIGHT.br_blockcount, |
| 950 | RIGHT.br_state))) |
| 951 | goto done; |
| 952 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 955 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 956 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 957 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 958 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 959 | |
| 960 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | *dnew = temp; |
| 962 | break; |
| 963 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 964 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | /* |
| 966 | * Filling in the last part of a previous delayed allocation. |
| 967 | * The right neighbor is not contiguous. |
| 968 | */ |
| 969 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 970 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 972 | xfs_iext_insert(ip, *idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | ip->i_d.di_nextents++; |
| 974 | if (cur == NULL) |
| 975 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 976 | else { |
| 977 | rval = XFS_ILOG_CORE; |
| 978 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 979 | new->br_startblock, new->br_blockcount, |
| 980 | &i))) |
| 981 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 982 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 984 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 986 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 989 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 990 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | first, flist, &cur, 1, &tmp_rval, |
| 992 | XFS_DATA_FORK); |
| 993 | rval |= tmp_rval; |
| 994 | if (error) |
| 995 | goto done; |
| 996 | } |
| 997 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 998 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1000 | ep = xfs_iext_get_ext(ifp, *idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1001 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1002 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1003 | |
| 1004 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | *dnew = temp; |
| 1006 | break; |
| 1007 | |
| 1008 | case 0: |
| 1009 | /* |
| 1010 | * Filling in the middle part of a previous delayed allocation. |
| 1011 | * Contiguity is impossible here. |
| 1012 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1013 | * |
| 1014 | * We start with a delayed allocation: |
| 1015 | * |
| 1016 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 1017 | * PREV @ idx |
| 1018 | * |
| 1019 | * and we are allocating: |
| 1020 | * +rrrrrrrrrrrrrrrrr+ |
| 1021 | * new |
| 1022 | * |
| 1023 | * and we set it up for insertion as: |
| 1024 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 1025 | * new |
| 1026 | * PREV @ idx LEFT RIGHT |
| 1027 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | */ |
| 1029 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1031 | trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 1032 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 1033 | LEFT = *new; |
| 1034 | RIGHT.br_state = PREV.br_state; |
| 1035 | RIGHT.br_startblock = nullstartblock( |
| 1036 | (int)xfs_bmap_worst_indlen(ip, temp2)); |
| 1037 | RIGHT.br_startoff = new_endoff; |
| 1038 | RIGHT.br_blockcount = temp2; |
| 1039 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1040 | xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | ip->i_d.di_nextents++; |
| 1042 | if (cur == NULL) |
| 1043 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1044 | else { |
| 1045 | rval = XFS_ILOG_CORE; |
| 1046 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1047 | new->br_startblock, new->br_blockcount, |
| 1048 | &i))) |
| 1049 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1050 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1052 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1054 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1057 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 1058 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | first, flist, &cur, 1, &tmp_rval, |
| 1060 | XFS_DATA_FORK); |
| 1061 | rval |= tmp_rval; |
| 1062 | if (error) |
| 1063 | goto done; |
| 1064 | } |
| 1065 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 1066 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1067 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | (cur ? cur->bc_private.b.allocated : 0)); |
| 1069 | if (diff > 0 && |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1070 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1071 | -((int64_t)diff), 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | /* |
| 1073 | * Ick gross gag me with a spoon. |
| 1074 | */ |
| 1075 | ASSERT(0); /* want to see if this ever happens! */ |
| 1076 | while (diff > 0) { |
| 1077 | if (temp) { |
| 1078 | temp--; |
| 1079 | diff--; |
| 1080 | if (!diff || |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1081 | !xfs_icsb_modify_counters(ip->i_mount, |
| 1082 | XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1083 | -((int64_t)diff), 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | if (temp2) { |
| 1087 | temp2--; |
| 1088 | diff--; |
| 1089 | if (!diff || |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1090 | !xfs_icsb_modify_counters(ip->i_mount, |
| 1091 | XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1092 | -((int64_t)diff), 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1097 | ep = xfs_iext_get_ext(ifp, *idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1098 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1099 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1100 | trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_); |
| 1101 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1102 | nullstartblock((int)temp2)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1103 | trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_); |
| 1104 | |
| 1105 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | *dnew = temp + temp2; |
| 1107 | break; |
| 1108 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1109 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1110 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1111 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1112 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1113 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1114 | case BMAP_LEFT_CONTIG: |
| 1115 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | /* |
| 1117 | * These cases are all impossible. |
| 1118 | */ |
| 1119 | ASSERT(0); |
| 1120 | } |
| 1121 | *curp = cur; |
| 1122 | done: |
| 1123 | *logflagsp = rval; |
| 1124 | return error; |
| 1125 | #undef LEFT |
| 1126 | #undef RIGHT |
| 1127 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 1132 | * allocation to a real allocation or vice versa. |
| 1133 | */ |
| 1134 | STATIC int /* error */ |
| 1135 | xfs_bmap_add_extent_unwritten_real( |
| 1136 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1137 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1139 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1140 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1143 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1146 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1148 | xfs_exntst_t newext; /* new extent state */ |
| 1149 | xfs_exntst_t oldext; /* old extent state */ |
| 1150 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1151 | /* left is 0, right is 1, prev is 2 */ |
| 1152 | int rval=0; /* return value (logging flags) */ |
| 1153 | int state = 0;/* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | #define LEFT r[0] |
| 1156 | #define RIGHT r[1] |
| 1157 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | /* |
| 1159 | * Set up a bunch of variables to make the tests simpler. |
| 1160 | */ |
| 1161 | error = 0; |
| 1162 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1163 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1164 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | xfs_bmbt_get_all(ep, &PREV); |
| 1166 | newext = new->br_state; |
| 1167 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 1168 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 1169 | ASSERT(PREV.br_state == oldext); |
| 1170 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1171 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1172 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | /* |
| 1175 | * Set flags determining what part of the previous oldext allocation |
| 1176 | * extent is being replaced by a newext allocation. |
| 1177 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1178 | if (PREV.br_startoff == new->br_startoff) |
| 1179 | state |= BMAP_LEFT_FILLING; |
| 1180 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1181 | state |= BMAP_RIGHT_FILLING; |
| 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | /* |
| 1184 | * Check and set flags if this segment has a left neighbor. |
| 1185 | * Don't set contiguous if the combined extent would be too large. |
| 1186 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1187 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1188 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1189 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1190 | |
| 1191 | if (isnullstartblock(LEFT.br_startblock)) |
| 1192 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1194 | |
| 1195 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1196 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1197 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1198 | LEFT.br_state == newext && |
| 1199 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1200 | state |= BMAP_LEFT_CONTIG; |
| 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* |
| 1203 | * Check and set flags if this segment has a right neighbor. |
| 1204 | * Don't set contiguous if the combined extent would be too large. |
| 1205 | * Also check for all-three-contiguous being too large. |
| 1206 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1207 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1208 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1209 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1210 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1211 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1213 | |
| 1214 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1215 | new_endoff == RIGHT.br_startoff && |
| 1216 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1217 | newext == RIGHT.br_state && |
| 1218 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1219 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1220 | BMAP_RIGHT_FILLING)) != |
| 1221 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1222 | BMAP_RIGHT_FILLING) || |
| 1223 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1224 | <= MAXEXTLEN)) |
| 1225 | state |= BMAP_RIGHT_CONTIG; |
| 1226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | /* |
| 1228 | * Switch out based on the FILLING and CONTIG state bits. |
| 1229 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1230 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1231 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1232 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1233 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | /* |
| 1235 | * Setting all of a previous oldext extent to newext. |
| 1236 | * The left and right neighbors are both contiguous with new. |
| 1237 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1238 | --*idx; |
| 1239 | |
| 1240 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1241 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1243 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1244 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1245 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1246 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | ip->i_d.di_nextents -= 2; |
| 1248 | if (cur == NULL) |
| 1249 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1250 | else { |
| 1251 | rval = XFS_ILOG_CORE; |
| 1252 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1253 | RIGHT.br_startblock, |
| 1254 | RIGHT.br_blockcount, &i))) |
| 1255 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1256 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1257 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1259 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1260 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1262 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1263 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1265 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1266 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1268 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1270 | LEFT.br_startblock, |
| 1271 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1272 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1273 | goto done; |
| 1274 | } |
| 1275 | break; |
| 1276 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1277 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | /* |
| 1279 | * Setting all of a previous oldext extent to newext. |
| 1280 | * The left neighbor is contiguous, the right is not. |
| 1281 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1282 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1283 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1284 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1285 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 1286 | LEFT.br_blockcount + PREV.br_blockcount); |
| 1287 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1288 | |
| 1289 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | ip->i_d.di_nextents--; |
| 1291 | if (cur == NULL) |
| 1292 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1293 | else { |
| 1294 | rval = XFS_ILOG_CORE; |
| 1295 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1296 | PREV.br_startblock, PREV.br_blockcount, |
| 1297 | &i))) |
| 1298 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1299 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1300 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1302 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1303 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1305 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1307 | LEFT.br_startblock, |
| 1308 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1309 | LEFT.br_state))) |
| 1310 | goto done; |
| 1311 | } |
| 1312 | break; |
| 1313 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1314 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | /* |
| 1316 | * Setting all of a previous oldext extent to newext. |
| 1317 | * The right neighbor is contiguous, the left is not. |
| 1318 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1319 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | xfs_bmbt_set_blockcount(ep, |
| 1321 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1322 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1323 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1324 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | ip->i_d.di_nextents--; |
| 1326 | if (cur == NULL) |
| 1327 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1328 | else { |
| 1329 | rval = XFS_ILOG_CORE; |
| 1330 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1331 | RIGHT.br_startblock, |
| 1332 | RIGHT.br_blockcount, &i))) |
| 1333 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1334 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1335 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1337 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1338 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1340 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1342 | new->br_startblock, |
| 1343 | new->br_blockcount + RIGHT.br_blockcount, |
| 1344 | newext))) |
| 1345 | goto done; |
| 1346 | } |
| 1347 | break; |
| 1348 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1349 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | /* |
| 1351 | * Setting all of a previous oldext extent to newext. |
| 1352 | * Neither the left nor right neighbors are contiguous with |
| 1353 | * the new one. |
| 1354 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1355 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1357 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | if (cur == NULL) |
| 1360 | rval = XFS_ILOG_DEXT; |
| 1361 | else { |
| 1362 | rval = 0; |
| 1363 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1364 | new->br_startblock, new->br_blockcount, |
| 1365 | &i))) |
| 1366 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1367 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1369 | new->br_startblock, new->br_blockcount, |
| 1370 | newext))) |
| 1371 | goto done; |
| 1372 | } |
| 1373 | break; |
| 1374 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1375 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | /* |
| 1377 | * Setting the first part of a previous oldext extent to newext. |
| 1378 | * The left neighbor is contiguous. |
| 1379 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1380 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 1381 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | LEFT.br_blockcount + new->br_blockcount); |
| 1383 | xfs_bmbt_set_startoff(ep, |
| 1384 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1385 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1386 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1387 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | xfs_bmbt_set_startblock(ep, |
| 1389 | new->br_startblock + new->br_blockcount); |
| 1390 | xfs_bmbt_set_blockcount(ep, |
| 1391 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1392 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1393 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1394 | --*idx; |
| 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (cur == NULL) |
| 1397 | rval = XFS_ILOG_DEXT; |
| 1398 | else { |
| 1399 | rval = 0; |
| 1400 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1401 | PREV.br_startblock, PREV.br_blockcount, |
| 1402 | &i))) |
| 1403 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1404 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | if ((error = xfs_bmbt_update(cur, |
| 1406 | PREV.br_startoff + new->br_blockcount, |
| 1407 | PREV.br_startblock + new->br_blockcount, |
| 1408 | PREV.br_blockcount - new->br_blockcount, |
| 1409 | oldext))) |
| 1410 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1411 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | goto done; |
| 1413 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1414 | LEFT.br_startblock, |
| 1415 | LEFT.br_blockcount + new->br_blockcount, |
| 1416 | LEFT.br_state)) |
| 1417 | goto done; |
| 1418 | } |
| 1419 | break; |
| 1420 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1421 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * Setting the first part of a previous oldext extent to newext. |
| 1424 | * The left neighbor is not contiguous. |
| 1425 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1426 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1428 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1429 | xfs_bmbt_set_blockcount(ep, |
| 1430 | PREV.br_blockcount - new->br_blockcount); |
| 1431 | xfs_bmbt_set_startblock(ep, |
| 1432 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1433 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1434 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1435 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | ip->i_d.di_nextents++; |
| 1437 | if (cur == NULL) |
| 1438 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1439 | else { |
| 1440 | rval = XFS_ILOG_CORE; |
| 1441 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1442 | PREV.br_startblock, PREV.br_blockcount, |
| 1443 | &i))) |
| 1444 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1445 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | if ((error = xfs_bmbt_update(cur, |
| 1447 | PREV.br_startoff + new->br_blockcount, |
| 1448 | PREV.br_startblock + new->br_blockcount, |
| 1449 | PREV.br_blockcount - new->br_blockcount, |
| 1450 | oldext))) |
| 1451 | goto done; |
| 1452 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1453 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1455 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | break; |
| 1458 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1459 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | /* |
| 1461 | * Setting the last part of a previous oldext extent to newext. |
| 1462 | * The right neighbor is contiguous with the new allocation. |
| 1463 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1464 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | xfs_bmbt_set_blockcount(ep, |
| 1466 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1467 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1468 | |
| 1469 | ++*idx; |
| 1470 | |
| 1471 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1472 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1473 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1475 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (cur == NULL) |
| 1478 | rval = XFS_ILOG_DEXT; |
| 1479 | else { |
| 1480 | rval = 0; |
| 1481 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1482 | PREV.br_startblock, |
| 1483 | PREV.br_blockcount, &i))) |
| 1484 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1485 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1487 | PREV.br_startblock, |
| 1488 | PREV.br_blockcount - new->br_blockcount, |
| 1489 | oldext))) |
| 1490 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1491 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | goto done; |
| 1493 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1494 | new->br_startblock, |
| 1495 | new->br_blockcount + RIGHT.br_blockcount, |
| 1496 | newext))) |
| 1497 | goto done; |
| 1498 | } |
| 1499 | break; |
| 1500 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1501 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | /* |
| 1503 | * Setting the last part of a previous oldext extent to newext. |
| 1504 | * The right neighbor is not contiguous. |
| 1505 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1506 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | xfs_bmbt_set_blockcount(ep, |
| 1508 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1509 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1510 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1511 | ++*idx; |
| 1512 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | ip->i_d.di_nextents++; |
| 1515 | if (cur == NULL) |
| 1516 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1517 | else { |
| 1518 | rval = XFS_ILOG_CORE; |
| 1519 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1520 | PREV.br_startblock, PREV.br_blockcount, |
| 1521 | &i))) |
| 1522 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1523 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1525 | PREV.br_startblock, |
| 1526 | PREV.br_blockcount - new->br_blockcount, |
| 1527 | oldext))) |
| 1528 | goto done; |
| 1529 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1530 | new->br_startblock, new->br_blockcount, |
| 1531 | &i))) |
| 1532 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1533 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1535 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1537 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
| 1539 | break; |
| 1540 | |
| 1541 | case 0: |
| 1542 | /* |
| 1543 | * Setting the middle part of a previous oldext extent to |
| 1544 | * newext. Contiguity is impossible here. |
| 1545 | * One extent becomes three extents. |
| 1546 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1547 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | xfs_bmbt_set_blockcount(ep, |
| 1549 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1550 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | r[0] = *new; |
| 1553 | r[1].br_startoff = new_endoff; |
| 1554 | r[1].br_blockcount = |
| 1555 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1556 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1557 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1558 | |
| 1559 | ++*idx; |
| 1560 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | ip->i_d.di_nextents += 2; |
| 1563 | if (cur == NULL) |
| 1564 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1565 | else { |
| 1566 | rval = XFS_ILOG_CORE; |
| 1567 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1568 | PREV.br_startblock, PREV.br_blockcount, |
| 1569 | &i))) |
| 1570 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1571 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | /* new right extent - oldext */ |
| 1573 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1574 | r[1].br_startblock, r[1].br_blockcount, |
| 1575 | r[1].br_state))) |
| 1576 | goto done; |
| 1577 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1579 | cur->bc_rec.b.br_blockcount = |
| 1580 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1581 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1583 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1584 | /* |
| 1585 | * Reset the cursor to the position of the new extent |
| 1586 | * we are about to insert as we can't trust it after |
| 1587 | * the previous insert. |
| 1588 | */ |
| 1589 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1590 | new->br_startblock, new->br_blockcount, |
| 1591 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1593 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1595 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1596 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1598 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | } |
| 1600 | break; |
| 1601 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1602 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1603 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1604 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1605 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1606 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1607 | case BMAP_LEFT_CONTIG: |
| 1608 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | /* |
| 1610 | * These cases are all impossible. |
| 1611 | */ |
| 1612 | ASSERT(0); |
| 1613 | } |
| 1614 | *curp = cur; |
| 1615 | done: |
| 1616 | *logflagsp = rval; |
| 1617 | return error; |
| 1618 | #undef LEFT |
| 1619 | #undef RIGHT |
| 1620 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1625 | * to a delayed allocation. |
| 1626 | */ |
| 1627 | /*ARGSUSED*/ |
| 1628 | STATIC int /* error */ |
| 1629 | xfs_bmap_add_extent_hole_delay( |
| 1630 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1631 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1632 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1633 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1635 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1637 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1638 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1639 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1640 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1641 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1643 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1645 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | /* |
| 1648 | * Check and set flags if this segment has a left neighbor |
| 1649 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1650 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1651 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1652 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1653 | |
| 1654 | if (isnullstartblock(left.br_startblock)) |
| 1655 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | /* |
| 1659 | * Check and set flags if the current (right) segment exists. |
| 1660 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1661 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1662 | if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1663 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1664 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1665 | |
| 1666 | if (isnullstartblock(right.br_startblock)) |
| 1667 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * Set contiguity flags on the left and right neighbors. |
| 1672 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1673 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1674 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1675 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1676 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1677 | state |= BMAP_LEFT_CONTIG; |
| 1678 | |
| 1679 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1680 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1681 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1682 | (!(state & BMAP_LEFT_CONTIG) || |
| 1683 | (left.br_blockcount + new->br_blockcount + |
| 1684 | right.br_blockcount <= MAXEXTLEN))) |
| 1685 | state |= BMAP_RIGHT_CONTIG; |
| 1686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | /* |
| 1688 | * Switch out based on the contiguity flags. |
| 1689 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1690 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1691 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | /* |
| 1693 | * New allocation is contiguous with delayed allocations |
| 1694 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1695 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1697 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | temp = left.br_blockcount + new->br_blockcount + |
| 1699 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1700 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1701 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1702 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1703 | oldlen = startblockval(left.br_startblock) + |
| 1704 | startblockval(new->br_startblock) + |
| 1705 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1707 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1708 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1709 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1710 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1711 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | break; |
| 1713 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1714 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | /* |
| 1716 | * New allocation is contiguous with a delayed allocation |
| 1717 | * on the left. |
| 1718 | * Merge the new allocation with the left neighbor. |
| 1719 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1720 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1722 | |
| 1723 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1724 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1725 | oldlen = startblockval(left.br_startblock) + |
| 1726 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1728 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1729 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1730 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | break; |
| 1732 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1733 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | /* |
| 1735 | * New allocation is contiguous with a delayed allocation |
| 1736 | * on the right. |
| 1737 | * Merge the new allocation with the right neighbor. |
| 1738 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1739 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1741 | oldlen = startblockval(new->br_startblock) + |
| 1742 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1744 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1745 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1746 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1747 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | break; |
| 1749 | |
| 1750 | case 0: |
| 1751 | /* |
| 1752 | * New allocation is not contiguous with another |
| 1753 | * delayed allocation. |
| 1754 | * Insert a new entry. |
| 1755 | */ |
| 1756 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1757 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | if (oldlen != newlen) { |
| 1761 | ASSERT(oldlen > newlen); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1762 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1763 | (int64_t)(oldlen - newlen), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | /* |
| 1765 | * Nothing to do for disk quota accounting here. |
| 1766 | */ |
| 1767 | } |
| 1768 | *logflagsp = 0; |
| 1769 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | /* |
| 1773 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1774 | * to a real allocation. |
| 1775 | */ |
| 1776 | STATIC int /* error */ |
| 1777 | xfs_bmap_add_extent_hole_real( |
| 1778 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1779 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1781 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | int *logflagsp, /* inode logging flags */ |
| 1783 | int whichfork) /* data or attr fork */ |
| 1784 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | int i; /* temp state */ |
| 1787 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1788 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1789 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1790 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
| 1793 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1794 | ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | state = 0; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1796 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1797 | if (whichfork == XFS_ATTR_FORK) |
| 1798 | state |= BMAP_ATTRFORK; |
| 1799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | /* |
| 1801 | * Check and set flags if this segment has a left neighbor. |
| 1802 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1803 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1804 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1805 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1806 | if (isnullstartblock(left.br_startblock)) |
| 1807 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | /* |
| 1811 | * Check and set flags if this segment has a current value. |
| 1812 | * Not true if we're inserting into the "hole" at eof. |
| 1813 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1814 | if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1815 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1816 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1817 | if (isnullstartblock(right.br_startblock)) |
| 1818 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | /* |
| 1822 | * We're inserting a real allocation between "left" and "right". |
| 1823 | * Set the contiguity flags. Don't let extents get too large. |
| 1824 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1825 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1826 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1827 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1828 | left.br_state == new->br_state && |
| 1829 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1830 | state |= BMAP_LEFT_CONTIG; |
| 1831 | |
| 1832 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1833 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1834 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1835 | new->br_state == right.br_state && |
| 1836 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1837 | (!(state & BMAP_LEFT_CONTIG) || |
| 1838 | left.br_blockcount + new->br_blockcount + |
| 1839 | right.br_blockcount <= MAXEXTLEN)) |
| 1840 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1842 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | /* |
| 1844 | * Select which case we're in here, and implement it. |
| 1845 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1846 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1847 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | /* |
| 1849 | * New allocation is contiguous with real allocations on the |
| 1850 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1851 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1853 | --*idx; |
| 1854 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1855 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | left.br_blockcount + new->br_blockcount + |
| 1857 | right.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1858 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1859 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1860 | xfs_iext_remove(ip, *idx + 1, 1, state); |
| 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1863 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 1864 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1865 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1866 | } else { |
| 1867 | rval = XFS_ILOG_CORE; |
| 1868 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1869 | right.br_startoff, |
| 1870 | right.br_startblock, |
| 1871 | right.br_blockcount, &i))) |
| 1872 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1873 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1874 | if ((error = xfs_btree_delete(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1875 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1876 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1877 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1878 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1879 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1880 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 1881 | left.br_startblock, |
| 1882 | left.br_blockcount + |
| 1883 | new->br_blockcount + |
| 1884 | right.br_blockcount, |
| 1885 | left.br_state))) |
| 1886 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1888 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1890 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | /* |
| 1892 | * New allocation is contiguous with a real allocation |
| 1893 | * on the left. |
| 1894 | * Merge the new allocation with the left neighbor. |
| 1895 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1896 | --*idx; |
| 1897 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1898 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1900 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1903 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1904 | } else { |
| 1905 | rval = 0; |
| 1906 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1907 | left.br_startoff, |
| 1908 | left.br_startblock, |
| 1909 | left.br_blockcount, &i))) |
| 1910 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1911 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1912 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 1913 | left.br_startblock, |
| 1914 | left.br_blockcount + |
| 1915 | new->br_blockcount, |
| 1916 | left.br_state))) |
| 1917 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1919 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1921 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | /* |
| 1923 | * New allocation is contiguous with a real allocation |
| 1924 | * on the right. |
| 1925 | * Merge the new allocation with the right neighbor. |
| 1926 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1927 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1928 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1929 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | new->br_blockcount + right.br_blockcount, |
| 1931 | right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1932 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1935 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1936 | } else { |
| 1937 | rval = 0; |
| 1938 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1939 | right.br_startoff, |
| 1940 | right.br_startblock, |
| 1941 | right.br_blockcount, &i))) |
| 1942 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1943 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1944 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1945 | new->br_startblock, |
| 1946 | new->br_blockcount + |
| 1947 | right.br_blockcount, |
| 1948 | right.br_state))) |
| 1949 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1951 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | |
| 1953 | case 0: |
| 1954 | /* |
| 1955 | * New allocation is not contiguous with another |
| 1956 | * real allocation. |
| 1957 | * Insert a new entry. |
| 1958 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1959 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1961 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 1962 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1963 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1964 | } else { |
| 1965 | rval = XFS_ILOG_CORE; |
| 1966 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1967 | new->br_startoff, |
| 1968 | new->br_startblock, |
| 1969 | new->br_blockcount, &i))) |
| 1970 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1971 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1972 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1973 | if ((error = xfs_btree_insert(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1974 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1975 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1977 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1979 | done: |
| 1980 | *logflagsp = rval; |
| 1981 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1984 | /* |
| 1985 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 1986 | */ |
| 1987 | STATIC int |
| 1988 | xfs_bmap_extsize_align( |
| 1989 | xfs_mount_t *mp, |
| 1990 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 1991 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 1992 | xfs_extlen_t extsz, /* align to this extent size */ |
| 1993 | int rt, /* is this a realtime inode? */ |
| 1994 | int eof, /* is extent at end-of-file? */ |
| 1995 | int delay, /* creating delalloc extent? */ |
| 1996 | int convert, /* overwriting unwritten extent? */ |
| 1997 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 1998 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 1999 | { |
| 2000 | xfs_fileoff_t orig_off; /* original offset */ |
| 2001 | xfs_extlen_t orig_alen; /* original length */ |
| 2002 | xfs_fileoff_t orig_end; /* original off+len */ |
| 2003 | xfs_fileoff_t nexto; /* next file offset */ |
| 2004 | xfs_fileoff_t prevo; /* previous file offset */ |
| 2005 | xfs_fileoff_t align_off; /* temp for offset */ |
| 2006 | xfs_extlen_t align_alen; /* temp for length */ |
| 2007 | xfs_extlen_t temp; /* temp for calculations */ |
| 2008 | |
| 2009 | if (convert) |
| 2010 | return 0; |
| 2011 | |
| 2012 | orig_off = align_off = *offp; |
| 2013 | orig_alen = align_alen = *lenp; |
| 2014 | orig_end = orig_off + orig_alen; |
| 2015 | |
| 2016 | /* |
| 2017 | * If this request overlaps an existing extent, then don't |
| 2018 | * attempt to perform any additional alignment. |
| 2019 | */ |
| 2020 | if (!delay && !eof && |
| 2021 | (orig_off >= gotp->br_startoff) && |
| 2022 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * If the file offset is unaligned vs. the extent size |
| 2028 | * we need to align it. This will be possible unless |
| 2029 | * the file was previously written with a kernel that didn't |
| 2030 | * perform this alignment, or if a truncate shot us in the |
| 2031 | * foot. |
| 2032 | */ |
| 2033 | temp = do_mod(orig_off, extsz); |
| 2034 | if (temp) { |
| 2035 | align_alen += temp; |
| 2036 | align_off -= temp; |
| 2037 | } |
| 2038 | /* |
| 2039 | * Same adjustment for the end of the requested area. |
| 2040 | */ |
| 2041 | if ((temp = (align_alen % extsz))) { |
| 2042 | align_alen += extsz - temp; |
| 2043 | } |
| 2044 | /* |
| 2045 | * If the previous block overlaps with this proposed allocation |
| 2046 | * then move the start forward without adjusting the length. |
| 2047 | */ |
| 2048 | if (prevp->br_startoff != NULLFILEOFF) { |
| 2049 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 2050 | prevo = prevp->br_startoff; |
| 2051 | else |
| 2052 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 2053 | } else |
| 2054 | prevo = 0; |
| 2055 | if (align_off != orig_off && align_off < prevo) |
| 2056 | align_off = prevo; |
| 2057 | /* |
| 2058 | * If the next block overlaps with this proposed allocation |
| 2059 | * then move the start back without adjusting the length, |
| 2060 | * but not before offset 0. |
| 2061 | * This may of course make the start overlap previous block, |
| 2062 | * and if we hit the offset 0 limit then the next block |
| 2063 | * can still overlap too. |
| 2064 | */ |
| 2065 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2066 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2067 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2068 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2069 | else |
| 2070 | nexto = gotp->br_startoff; |
| 2071 | } else |
| 2072 | nexto = NULLFILEOFF; |
| 2073 | if (!eof && |
| 2074 | align_off + align_alen != orig_end && |
| 2075 | align_off + align_alen > nexto) |
| 2076 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2077 | /* |
| 2078 | * If we're now overlapping the next or previous extent that |
| 2079 | * means we can't fit an extsz piece in this hole. Just move |
| 2080 | * the start forward to the first valid spot and set |
| 2081 | * the length so we hit the end. |
| 2082 | */ |
| 2083 | if (align_off != orig_off && align_off < prevo) |
| 2084 | align_off = prevo; |
| 2085 | if (align_off + align_alen != orig_end && |
| 2086 | align_off + align_alen > nexto && |
| 2087 | nexto != NULLFILEOFF) { |
| 2088 | ASSERT(nexto > prevo); |
| 2089 | align_alen = nexto - align_off; |
| 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * If realtime, and the result isn't a multiple of the realtime |
| 2094 | * extent size we need to remove blocks until it is. |
| 2095 | */ |
| 2096 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2097 | /* |
| 2098 | * We're not covering the original request, or |
| 2099 | * we won't be able to once we fix the length. |
| 2100 | */ |
| 2101 | if (orig_off < align_off || |
| 2102 | orig_end > align_off + align_alen || |
| 2103 | align_alen - temp < orig_alen) |
| 2104 | return XFS_ERROR(EINVAL); |
| 2105 | /* |
| 2106 | * Try to fix it by moving the start up. |
| 2107 | */ |
| 2108 | if (align_off + temp <= orig_off) { |
| 2109 | align_alen -= temp; |
| 2110 | align_off += temp; |
| 2111 | } |
| 2112 | /* |
| 2113 | * Try to fix it by moving the end in. |
| 2114 | */ |
| 2115 | else if (align_off + align_alen - temp >= orig_end) |
| 2116 | align_alen -= temp; |
| 2117 | /* |
| 2118 | * Set the start to the minimum then trim the length. |
| 2119 | */ |
| 2120 | else { |
| 2121 | align_alen -= orig_off - align_off; |
| 2122 | align_off = orig_off; |
| 2123 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 2124 | } |
| 2125 | /* |
| 2126 | * Result doesn't cover the request, fail it. |
| 2127 | */ |
| 2128 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2129 | return XFS_ERROR(EINVAL); |
| 2130 | } else { |
| 2131 | ASSERT(orig_off >= align_off); |
| 2132 | ASSERT(orig_end <= align_off + align_alen); |
| 2133 | } |
| 2134 | |
| 2135 | #ifdef DEBUG |
| 2136 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2137 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2138 | if (prevp->br_startoff != NULLFILEOFF) |
| 2139 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2140 | #endif |
| 2141 | |
| 2142 | *lenp = align_alen; |
| 2143 | *offp = align_off; |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2148 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2149 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2150 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2152 | { |
| 2153 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2155 | xfs_mount_t *mp; /* mount point structure */ |
| 2156 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2157 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
| 2159 | #define ISVALID(x,y) \ |
| 2160 | (rt ? \ |
| 2161 | (x) < mp->m_sb.sb_rblocks : \ |
| 2162 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2163 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2164 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | mp = ap->ip->i_mount; |
| 2167 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2168 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
| 2169 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | /* |
| 2171 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2172 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | */ |
| 2174 | if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2175 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, |
| 2177 | ap->prevp->br_startblock)) { |
| 2178 | ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; |
| 2179 | /* |
| 2180 | * Adjust for the gap between prevp and us. |
| 2181 | */ |
| 2182 | adjust = ap->off - |
| 2183 | (ap->prevp->br_startoff + ap->prevp->br_blockcount); |
| 2184 | if (adjust && |
| 2185 | ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) |
| 2186 | ap->rval += adjust; |
| 2187 | } |
| 2188 | /* |
| 2189 | * If not at eof, then compare the two neighbor blocks. |
| 2190 | * Figure out whether either one gives us a good starting point, |
| 2191 | * and pick the better one. |
| 2192 | */ |
| 2193 | else if (!ap->eof) { |
| 2194 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2195 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2196 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2197 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2198 | |
| 2199 | /* |
| 2200 | * If there's a previous (left) block, select a requested |
| 2201 | * start block based on it. |
| 2202 | */ |
| 2203 | if (ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2204 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | (prevbno = ap->prevp->br_startblock + |
| 2206 | ap->prevp->br_blockcount) && |
| 2207 | ISVALID(prevbno, ap->prevp->br_startblock)) { |
| 2208 | /* |
| 2209 | * Calculate gap to end of previous block. |
| 2210 | */ |
| 2211 | adjust = prevdiff = ap->off - |
| 2212 | (ap->prevp->br_startoff + |
| 2213 | ap->prevp->br_blockcount); |
| 2214 | /* |
| 2215 | * Figure the startblock based on the previous block's |
| 2216 | * end and the gap size. |
| 2217 | * Heuristic! |
| 2218 | * If the gap is large relative to the piece we're |
| 2219 | * allocating, or using it gives us an invalid block |
| 2220 | * number, then just use the end of the previous block. |
| 2221 | */ |
| 2222 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2223 | ISVALID(prevbno + prevdiff, |
| 2224 | ap->prevp->br_startblock)) |
| 2225 | prevbno += adjust; |
| 2226 | else |
| 2227 | prevdiff += adjust; |
| 2228 | /* |
| 2229 | * If the firstblock forbids it, can't use it, |
| 2230 | * must use default. |
| 2231 | */ |
| 2232 | if (!rt && !nullfb && |
| 2233 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2234 | prevbno = NULLFSBLOCK; |
| 2235 | } |
| 2236 | /* |
| 2237 | * No previous block or can't follow it, just default. |
| 2238 | */ |
| 2239 | else |
| 2240 | prevbno = NULLFSBLOCK; |
| 2241 | /* |
| 2242 | * If there's a following (right) block, select a requested |
| 2243 | * start block based on it. |
| 2244 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2245 | if (!isnullstartblock(ap->gotp->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | /* |
| 2247 | * Calculate gap to start of next block. |
| 2248 | */ |
| 2249 | adjust = gotdiff = ap->gotp->br_startoff - ap->off; |
| 2250 | /* |
| 2251 | * Figure the startblock based on the next block's |
| 2252 | * start and the gap size. |
| 2253 | */ |
| 2254 | gotbno = ap->gotp->br_startblock; |
| 2255 | /* |
| 2256 | * Heuristic! |
| 2257 | * If the gap is large relative to the piece we're |
| 2258 | * allocating, or using it gives us an invalid block |
| 2259 | * number, then just use the start of the next block |
| 2260 | * offset by our length. |
| 2261 | */ |
| 2262 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2263 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2264 | gotbno -= adjust; |
| 2265 | else if (ISVALID(gotbno - ap->alen, gotbno)) { |
| 2266 | gotbno -= ap->alen; |
| 2267 | gotdiff += adjust - ap->alen; |
| 2268 | } else |
| 2269 | gotdiff += adjust; |
| 2270 | /* |
| 2271 | * If the firstblock forbids it, can't use it, |
| 2272 | * must use default. |
| 2273 | */ |
| 2274 | if (!rt && !nullfb && |
| 2275 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2276 | gotbno = NULLFSBLOCK; |
| 2277 | } |
| 2278 | /* |
| 2279 | * No next block, just default. |
| 2280 | */ |
| 2281 | else |
| 2282 | gotbno = NULLFSBLOCK; |
| 2283 | /* |
| 2284 | * If both valid, pick the better one, else the only good |
| 2285 | * one, else ap->rval is already set (to 0 or the inode block). |
| 2286 | */ |
| 2287 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
| 2288 | ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; |
| 2289 | else if (prevbno != NULLFSBLOCK) |
| 2290 | ap->rval = prevbno; |
| 2291 | else if (gotbno != NULLFSBLOCK) |
| 2292 | ap->rval = gotbno; |
| 2293 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2294 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | STATIC int |
| 2298 | xfs_bmap_rtalloc( |
| 2299 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2300 | { |
| 2301 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2302 | int error; /* error return value */ |
| 2303 | xfs_mount_t *mp; /* mount point structure */ |
| 2304 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2305 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2306 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2307 | xfs_rtblock_t rtb; |
| 2308 | |
| 2309 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2310 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2311 | prod = align / mp->m_sb.sb_rextsize; |
| 2312 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2313 | align, 1, ap->eof, 0, |
| 2314 | ap->conv, &ap->off, &ap->alen); |
| 2315 | if (error) |
| 2316 | return error; |
| 2317 | ASSERT(ap->alen); |
| 2318 | ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); |
| 2319 | |
| 2320 | /* |
| 2321 | * If the offset & length are not perfectly aligned |
| 2322 | * then kill prod, it will just get us in trouble. |
| 2323 | */ |
| 2324 | if (do_mod(ap->off, align) || ap->alen % align) |
| 2325 | prod = 1; |
| 2326 | /* |
| 2327 | * Set ralen to be the actual requested length in rtextents. |
| 2328 | */ |
| 2329 | ralen = ap->alen / mp->m_sb.sb_rextsize; |
| 2330 | /* |
| 2331 | * If the old value was close enough to MAXEXTLEN that |
| 2332 | * we rounded up to it, cut it back so it's valid again. |
| 2333 | * Note that if it's a really large request (bigger than |
| 2334 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2335 | * adjust the starting point to match it. |
| 2336 | */ |
| 2337 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2338 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2339 | |
| 2340 | /* |
| 2341 | * Lock out other modifications to the RT bitmap inode. |
| 2342 | */ |
Christoph Hellwig | 1050c71 | 2011-02-13 13:25:31 +0000 | [diff] [blame] | 2343 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 2344 | xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2345 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2346 | /* |
| 2347 | * If it's an allocation to an empty file at offset 0, |
| 2348 | * pick an extent that will space things out in the rt area. |
| 2349 | */ |
| 2350 | if (ap->eof && ap->off == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2351 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2352 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2353 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2354 | if (error) |
| 2355 | return error; |
| 2356 | ap->rval = rtx * mp->m_sb.sb_rextsize; |
| 2357 | } else { |
| 2358 | ap->rval = 0; |
| 2359 | } |
| 2360 | |
| 2361 | xfs_bmap_adjacent(ap); |
| 2362 | |
| 2363 | /* |
| 2364 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2365 | */ |
| 2366 | atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2367 | do_div(ap->rval, mp->m_sb.sb_rextsize); |
| 2368 | rtb = ap->rval; |
| 2369 | ap->alen = ralen; |
| 2370 | if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, |
| 2371 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2372 | return error; |
| 2373 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 2374 | (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, |
| 2375 | ap->alen, &ralen, atype, |
| 2376 | ap->wasdel, 1, &rtb))) |
| 2377 | return error; |
| 2378 | ap->rval = rtb; |
| 2379 | if (ap->rval != NULLFSBLOCK) { |
| 2380 | ap->rval *= mp->m_sb.sb_rextsize; |
| 2381 | ralen *= mp->m_sb.sb_rextsize; |
| 2382 | ap->alen = ralen; |
| 2383 | ap->ip->i_d.di_nblocks += ralen; |
| 2384 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2385 | if (ap->wasdel) |
| 2386 | ap->ip->i_delayed_blks -= ralen; |
| 2387 | /* |
| 2388 | * Adjust the disk quota also. This was reserved |
| 2389 | * earlier. |
| 2390 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2391 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2392 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2393 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2394 | } else { |
| 2395 | ap->alen = 0; |
| 2396 | } |
| 2397 | return 0; |
| 2398 | } |
| 2399 | |
| 2400 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2401 | xfs_bmap_btalloc_nullfb( |
| 2402 | struct xfs_bmalloca *ap, |
| 2403 | struct xfs_alloc_arg *args, |
| 2404 | xfs_extlen_t *blen) |
| 2405 | { |
| 2406 | struct xfs_mount *mp = ap->ip->i_mount; |
| 2407 | struct xfs_perag *pag; |
| 2408 | xfs_agnumber_t ag, startag; |
| 2409 | int notinit = 0; |
| 2410 | int error; |
| 2411 | |
| 2412 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2413 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2414 | else |
| 2415 | args->type = XFS_ALLOCTYPE_START_BNO; |
| 2416 | args->total = ap->total; |
| 2417 | |
| 2418 | /* |
| 2419 | * Search for an allocation group with a single extent large enough |
| 2420 | * for the request. If one isn't found, then adjust the minimum |
| 2421 | * allocation size to the largest space found. |
| 2422 | */ |
| 2423 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2424 | if (startag == NULLAGNUMBER) |
| 2425 | startag = ag = 0; |
| 2426 | |
| 2427 | pag = xfs_perag_get(mp, ag); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2428 | while (*blen < args->maxlen) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2429 | if (!pag->pagf_init) { |
| 2430 | error = xfs_alloc_pagf_init(mp, args->tp, ag, |
| 2431 | XFS_ALLOC_FLAG_TRYLOCK); |
| 2432 | if (error) { |
| 2433 | xfs_perag_put(pag); |
| 2434 | return error; |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | /* |
| 2439 | * See xfs_alloc_fix_freelist... |
| 2440 | */ |
| 2441 | if (pag->pagf_init) { |
| 2442 | xfs_extlen_t longest; |
| 2443 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 2444 | if (*blen < longest) |
| 2445 | *blen = longest; |
| 2446 | } else |
| 2447 | notinit = 1; |
| 2448 | |
| 2449 | if (xfs_inode_is_filestream(ap->ip)) { |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2450 | if (*blen >= args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2451 | break; |
| 2452 | |
| 2453 | if (ap->userdata) { |
| 2454 | /* |
| 2455 | * If startag is an invalid AG, we've |
| 2456 | * come here once before and |
| 2457 | * xfs_filestream_new_ag picked the |
| 2458 | * best currently available. |
| 2459 | * |
| 2460 | * Don't continue looping, since we |
| 2461 | * could loop forever. |
| 2462 | */ |
| 2463 | if (startag == NULLAGNUMBER) |
| 2464 | break; |
| 2465 | |
| 2466 | error = xfs_filestream_new_ag(ap, &ag); |
| 2467 | xfs_perag_put(pag); |
| 2468 | if (error) |
| 2469 | return error; |
| 2470 | |
| 2471 | /* loop again to set 'blen'*/ |
| 2472 | startag = NULLAGNUMBER; |
| 2473 | pag = xfs_perag_get(mp, ag); |
| 2474 | continue; |
| 2475 | } |
| 2476 | } |
| 2477 | if (++ag == mp->m_sb.sb_agcount) |
| 2478 | ag = 0; |
| 2479 | if (ag == startag) |
| 2480 | break; |
| 2481 | xfs_perag_put(pag); |
| 2482 | pag = xfs_perag_get(mp, ag); |
| 2483 | } |
| 2484 | xfs_perag_put(pag); |
| 2485 | |
| 2486 | /* |
| 2487 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2488 | * possible that there is space for this request. |
| 2489 | */ |
| 2490 | if (notinit || *blen < ap->minlen) |
| 2491 | args->minlen = ap->minlen; |
| 2492 | /* |
| 2493 | * If the best seen length is less than the request |
| 2494 | * length, use the best as the minimum. |
| 2495 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2496 | else if (*blen < args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2497 | args->minlen = *blen; |
| 2498 | /* |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2499 | * Otherwise we've seen an extent as big as maxlen, |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2500 | * use that as the minimum. |
| 2501 | */ |
| 2502 | else |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2503 | args->minlen = args->maxlen; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2504 | |
| 2505 | /* |
| 2506 | * set the failure fallback case to look in the selected |
| 2507 | * AG as the stream may have moved. |
| 2508 | */ |
| 2509 | if (xfs_inode_is_filestream(ap->ip)) |
| 2510 | ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2511 | |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
| 2515 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2516 | xfs_bmap_btalloc( |
| 2517 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2518 | { |
| 2519 | xfs_mount_t *mp; /* mount point structure */ |
| 2520 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2521 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2522 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2523 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2524 | xfs_alloc_arg_t args; |
| 2525 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2526 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2527 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2528 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2529 | int tryagain; |
| 2530 | int error; |
| 2531 | |
| 2532 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2533 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2534 | if (unlikely(align)) { |
| 2535 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2536 | align, 0, ap->eof, 0, ap->conv, |
| 2537 | &ap->off, &ap->alen); |
| 2538 | ASSERT(!error); |
| 2539 | ASSERT(ap->alen); |
| 2540 | } |
| 2541 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2542 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2543 | if (nullfb) { |
| 2544 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2545 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2546 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
| 2547 | ap->rval = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2548 | } else { |
| 2549 | ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
| 2550 | } |
| 2551 | } else |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2552 | ap->rval = ap->firstblock; |
| 2553 | |
| 2554 | xfs_bmap_adjacent(ap); |
| 2555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | /* |
| 2557 | * If allowed, use ap->rval; otherwise must use firstblock since |
| 2558 | * it's in the right allocation group. |
| 2559 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2560 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | ; |
| 2562 | else |
| 2563 | ap->rval = ap->firstblock; |
| 2564 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | * Normal allocation, done through xfs_alloc_vextent. |
| 2566 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2567 | tryagain = isaligned = 0; |
| 2568 | args.tp = ap->tp; |
| 2569 | args.mp = mp; |
| 2570 | args.fsbno = ap->rval; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2571 | |
| 2572 | /* Trim the allocation back to the maximum an AG can fit. */ |
| 2573 | args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2574 | args.firstblock = ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2575 | blen = 0; |
| 2576 | if (nullfb) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2577 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
| 2578 | if (error) |
| 2579 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2580 | } else if (ap->low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2581 | if (xfs_inode_is_filestream(ap->ip)) |
| 2582 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2583 | else |
| 2584 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2585 | args.total = args.minlen = ap->minlen; |
| 2586 | } else { |
| 2587 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2588 | args.total = ap->total; |
| 2589 | args.minlen = ap->minlen; |
| 2590 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2591 | /* apply extent size hints if obtained earlier */ |
| 2592 | if (unlikely(align)) { |
| 2593 | args.prod = align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2594 | if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) |
| 2595 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2596 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2597 | args.prod = 1; |
| 2598 | args.mod = 0; |
| 2599 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2600 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2601 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) |
| 2602 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2603 | } |
| 2604 | /* |
| 2605 | * If we are not low on available data blocks, and the |
| 2606 | * underlying logical volume manager is a stripe, and |
| 2607 | * the file offset is zero then try to allocate data |
| 2608 | * blocks on stripe unit boundary. |
| 2609 | * NOTE: ap->aeof is only set if the allocation length |
| 2610 | * is >= the stripe unit and the allocation offset is |
| 2611 | * at the end of file. |
| 2612 | */ |
| 2613 | if (!ap->low && ap->aeof) { |
| 2614 | if (!ap->off) { |
| 2615 | args.alignment = mp->m_dalign; |
| 2616 | atype = args.type; |
| 2617 | isaligned = 1; |
| 2618 | /* |
| 2619 | * Adjust for alignment |
| 2620 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2621 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2622 | args.minlen = blen - args.alignment; |
| 2623 | args.minalignslop = 0; |
| 2624 | } else { |
| 2625 | /* |
| 2626 | * First try an exact bno allocation. |
| 2627 | * If it fails then do a near or start bno |
| 2628 | * allocation with alignment turned on. |
| 2629 | */ |
| 2630 | atype = args.type; |
| 2631 | tryagain = 1; |
| 2632 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2633 | args.alignment = 1; |
| 2634 | /* |
| 2635 | * Compute the minlen+alignment for the |
| 2636 | * next case. Set slop so that the value |
| 2637 | * of minlen+alignment+slop doesn't go up |
| 2638 | * between the calls. |
| 2639 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2640 | if (blen > mp->m_dalign && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2641 | nextminlen = blen - mp->m_dalign; |
| 2642 | else |
| 2643 | nextminlen = args.minlen; |
| 2644 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2645 | args.minalignslop = |
| 2646 | nextminlen + mp->m_dalign - |
| 2647 | args.minlen - 1; |
| 2648 | else |
| 2649 | args.minalignslop = 0; |
| 2650 | } |
| 2651 | } else { |
| 2652 | args.alignment = 1; |
| 2653 | args.minalignslop = 0; |
| 2654 | } |
| 2655 | args.minleft = ap->minleft; |
| 2656 | args.wasdel = ap->wasdel; |
| 2657 | args.isfl = 0; |
| 2658 | args.userdata = ap->userdata; |
| 2659 | if ((error = xfs_alloc_vextent(&args))) |
| 2660 | return error; |
| 2661 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2662 | /* |
| 2663 | * Exact allocation failed. Now try with alignment |
| 2664 | * turned on. |
| 2665 | */ |
| 2666 | args.type = atype; |
| 2667 | args.fsbno = ap->rval; |
| 2668 | args.alignment = mp->m_dalign; |
| 2669 | args.minlen = nextminlen; |
| 2670 | args.minalignslop = 0; |
| 2671 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | if ((error = xfs_alloc_vextent(&args))) |
| 2673 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2674 | } |
| 2675 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2676 | /* |
| 2677 | * allocation failed, so turn off alignment and |
| 2678 | * try again. |
| 2679 | */ |
| 2680 | args.type = atype; |
| 2681 | args.fsbno = ap->rval; |
| 2682 | args.alignment = 0; |
| 2683 | if ((error = xfs_alloc_vextent(&args))) |
| 2684 | return error; |
| 2685 | } |
| 2686 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2687 | args.minlen > ap->minlen) { |
| 2688 | args.minlen = ap->minlen; |
| 2689 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2690 | args.fsbno = ap->rval; |
| 2691 | if ((error = xfs_alloc_vextent(&args))) |
| 2692 | return error; |
| 2693 | } |
| 2694 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2695 | args.fsbno = 0; |
| 2696 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2697 | args.total = ap->minlen; |
| 2698 | args.minleft = 0; |
| 2699 | if ((error = xfs_alloc_vextent(&args))) |
| 2700 | return error; |
| 2701 | ap->low = 1; |
| 2702 | } |
| 2703 | if (args.fsbno != NULLFSBLOCK) { |
| 2704 | ap->firstblock = ap->rval = args.fsbno; |
| 2705 | ASSERT(nullfb || fb_agno == args.agno || |
| 2706 | (ap->low && fb_agno < args.agno)); |
| 2707 | ap->alen = args.len; |
| 2708 | ap->ip->i_d.di_nblocks += args.len; |
| 2709 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2710 | if (ap->wasdel) |
| 2711 | ap->ip->i_delayed_blks -= args.len; |
| 2712 | /* |
| 2713 | * Adjust the disk quota also. This was reserved |
| 2714 | * earlier. |
| 2715 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2716 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2717 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2718 | XFS_TRANS_DQ_BCOUNT, |
| 2719 | (long) args.len); |
| 2720 | } else { |
| 2721 | ap->rval = NULLFSBLOCK; |
| 2722 | ap->alen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | } |
| 2724 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* |
| 2728 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2729 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2730 | */ |
| 2731 | STATIC int |
| 2732 | xfs_bmap_alloc( |
| 2733 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2734 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2735 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2736 | return xfs_bmap_rtalloc(ap); |
| 2737 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | /* |
| 2741 | * Transform a btree format file with only one leaf node, where the |
| 2742 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2743 | * 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] | 2744 | * give up the space for the btree root and pitch the leaf block. |
| 2745 | */ |
| 2746 | STATIC int /* error */ |
| 2747 | xfs_bmap_btree_to_extents( |
| 2748 | xfs_trans_t *tp, /* transaction pointer */ |
| 2749 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2750 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2751 | int *logflagsp, /* inode logging flags */ |
| 2752 | int whichfork) /* data or attr fork */ |
| 2753 | { |
| 2754 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2755 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | xfs_fsblock_t cbno; /* child block number */ |
| 2757 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2758 | int error; /* error return value */ |
| 2759 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2760 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2761 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2762 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2764 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2766 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2767 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2768 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2769 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2770 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2771 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2772 | 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] | 2773 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | *logflagsp = 0; |
| 2775 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2776 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | return error; |
| 2778 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2780 | XFS_BMAP_BTREE_REF))) |
| 2781 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2782 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2783 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | return error; |
| 2785 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2786 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2787 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | xfs_trans_binval(tp, cbp); |
| 2789 | if (cur->bc_bufs[0] == cbp) |
| 2790 | cur->bc_bufs[0] = NULL; |
| 2791 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2792 | ASSERT(ifp->if_broot == NULL); |
| 2793 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2794 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2795 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2800 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | * after removing space (or undoing a delayed allocation). |
| 2802 | */ |
| 2803 | STATIC int /* error */ |
| 2804 | xfs_bmap_del_extent( |
| 2805 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2806 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2807 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2809 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2810 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2812 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | { |
| 2814 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2815 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2816 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2817 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2818 | int delay; /* current block is delayed allocated */ |
| 2819 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2820 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | int error; /* error return value */ |
| 2822 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2824 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2825 | int i; /* temp state */ |
| 2826 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2827 | xfs_mount_t *mp; /* mount structure */ |
| 2828 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2829 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2830 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | uint qfield; /* quota field to update */ |
| 2832 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2833 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2834 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | |
| 2836 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2837 | |
| 2838 | if (whichfork == XFS_ATTR_FORK) |
| 2839 | state |= BMAP_ATTRFORK; |
| 2840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | mp = ip->i_mount; |
| 2842 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2843 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2844 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2846 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | xfs_bmbt_get_all(ep, &got); |
| 2848 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2849 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2850 | got_endoff = got.br_startoff + got.br_blockcount; |
| 2851 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2852 | delay = isnullstartblock(got.br_startblock); |
| 2853 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | flags = 0; |
| 2855 | qfield = 0; |
| 2856 | error = 0; |
| 2857 | /* |
| 2858 | * If deleting a real allocation, must free up the disk space. |
| 2859 | */ |
| 2860 | if (!delay) { |
| 2861 | flags = XFS_ILOG_CORE; |
| 2862 | /* |
| 2863 | * Realtime allocation. Free it and record di_nblocks update. |
| 2864 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2865 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | xfs_fsblock_t bno; |
| 2867 | xfs_filblks_t len; |
| 2868 | |
| 2869 | ASSERT(do_mod(del->br_blockcount, |
| 2870 | mp->m_sb.sb_rextsize) == 0); |
| 2871 | ASSERT(do_mod(del->br_startblock, |
| 2872 | mp->m_sb.sb_rextsize) == 0); |
| 2873 | bno = del->br_startblock; |
| 2874 | len = del->br_blockcount; |
| 2875 | do_div(bno, mp->m_sb.sb_rextsize); |
| 2876 | do_div(len, mp->m_sb.sb_rextsize); |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 2877 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 2878 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | goto done; |
| 2880 | do_fx = 0; |
| 2881 | nblks = len * mp->m_sb.sb_rextsize; |
| 2882 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 2883 | } |
| 2884 | /* |
| 2885 | * Ordinary allocation. |
| 2886 | */ |
| 2887 | else { |
| 2888 | do_fx = 1; |
| 2889 | nblks = del->br_blockcount; |
| 2890 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 2891 | } |
| 2892 | /* |
| 2893 | * Set up del_endblock and cur for later. |
| 2894 | */ |
| 2895 | del_endblock = del->br_startblock + del->br_blockcount; |
| 2896 | if (cur) { |
| 2897 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 2898 | got.br_startblock, got.br_blockcount, |
| 2899 | &i))) |
| 2900 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2901 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | } |
| 2903 | da_old = da_new = 0; |
| 2904 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2905 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | da_new = 0; |
| 2907 | nblks = 0; |
| 2908 | do_fx = 0; |
| 2909 | } |
| 2910 | /* |
| 2911 | * Set flag value to use in switch statement. |
| 2912 | * Left-contig is 2, right-contig is 1. |
| 2913 | */ |
| 2914 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 2915 | (got_endoff == del_endoff)) { |
| 2916 | case 3: |
| 2917 | /* |
| 2918 | * Matches the whole extent. Delete the entry. |
| 2919 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2920 | xfs_iext_remove(ip, *idx, 1, |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2921 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2922 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | if (delay) |
| 2924 | break; |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2927 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2928 | flags |= XFS_ILOG_CORE; |
| 2929 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2930 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | break; |
| 2932 | } |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2933 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2935 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | break; |
| 2937 | |
| 2938 | case 2: |
| 2939 | /* |
| 2940 | * Deleting the first part of the extent. |
| 2941 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2942 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 2944 | temp = got.br_blockcount - del->br_blockcount; |
| 2945 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | if (delay) { |
| 2947 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2948 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2949 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2950 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | da_new = temp; |
| 2952 | break; |
| 2953 | } |
| 2954 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2955 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2957 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | break; |
| 2959 | } |
| 2960 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 2961 | got.br_blockcount - del->br_blockcount, |
| 2962 | got.br_state))) |
| 2963 | goto done; |
| 2964 | break; |
| 2965 | |
| 2966 | case 1: |
| 2967 | /* |
| 2968 | * Deleting the last part of the extent. |
| 2969 | */ |
| 2970 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2971 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | if (delay) { |
| 2974 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2975 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2976 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2977 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | da_new = temp; |
| 2979 | break; |
| 2980 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2981 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2983 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | break; |
| 2985 | } |
| 2986 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 2987 | got.br_startblock, |
| 2988 | got.br_blockcount - del->br_blockcount, |
| 2989 | got.br_state))) |
| 2990 | goto done; |
| 2991 | break; |
| 2992 | |
| 2993 | case 0: |
| 2994 | /* |
| 2995 | * Deleting the middle of the extent. |
| 2996 | */ |
| 2997 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2998 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | xfs_bmbt_set_blockcount(ep, temp); |
| 3000 | new.br_startoff = del_endoff; |
| 3001 | temp2 = got_endoff - del_endoff; |
| 3002 | new.br_blockcount = temp2; |
| 3003 | new.br_state = got.br_state; |
| 3004 | if (!delay) { |
| 3005 | new.br_startblock = del_endblock; |
| 3006 | flags |= XFS_ILOG_CORE; |
| 3007 | if (cur) { |
| 3008 | if ((error = xfs_bmbt_update(cur, |
| 3009 | got.br_startoff, |
| 3010 | got.br_startblock, temp, |
| 3011 | got.br_state))) |
| 3012 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 3013 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | goto done; |
| 3015 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3016 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | if (error && error != ENOSPC) |
| 3018 | goto done; |
| 3019 | /* |
| 3020 | * If get no-space back from btree insert, |
| 3021 | * it tried a split, and we have a zero |
| 3022 | * block reservation. |
| 3023 | * Fix up our state and return the error. |
| 3024 | */ |
| 3025 | if (error == ENOSPC) { |
| 3026 | /* |
| 3027 | * Reset the cursor, don't trust |
| 3028 | * it after any insert operation. |
| 3029 | */ |
| 3030 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 3031 | got.br_startoff, |
| 3032 | got.br_startblock, |
| 3033 | temp, &i))) |
| 3034 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3035 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | /* |
| 3037 | * Update the btree record back |
| 3038 | * to the original value. |
| 3039 | */ |
| 3040 | if ((error = xfs_bmbt_update(cur, |
| 3041 | got.br_startoff, |
| 3042 | got.br_startblock, |
| 3043 | got.br_blockcount, |
| 3044 | got.br_state))) |
| 3045 | goto done; |
| 3046 | /* |
| 3047 | * Reset the extent record back |
| 3048 | * to the original value. |
| 3049 | */ |
| 3050 | xfs_bmbt_set_blockcount(ep, |
| 3051 | got.br_blockcount); |
| 3052 | flags = 0; |
| 3053 | error = XFS_ERROR(ENOSPC); |
| 3054 | goto done; |
| 3055 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 3056 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3058 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 3060 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 3061 | } else { |
| 3062 | ASSERT(whichfork == XFS_DATA_FORK); |
| 3063 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3064 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3066 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | da_new = temp + temp2; |
| 3068 | while (da_new > da_old) { |
| 3069 | if (temp) { |
| 3070 | temp--; |
| 3071 | da_new--; |
| 3072 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3073 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | } |
| 3075 | if (da_new == da_old) |
| 3076 | break; |
| 3077 | if (temp2) { |
| 3078 | temp2--; |
| 3079 | da_new--; |
| 3080 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3081 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 3085 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 3086 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 3087 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | break; |
| 3089 | } |
| 3090 | /* |
| 3091 | * If we need to, add to list of extents to delete. |
| 3092 | */ |
| 3093 | if (do_fx) |
| 3094 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 3095 | mp); |
| 3096 | /* |
| 3097 | * Adjust inode # blocks in the file. |
| 3098 | */ |
| 3099 | if (nblks) |
| 3100 | ip->i_d.di_nblocks -= nblks; |
| 3101 | /* |
| 3102 | * Adjust quota data. |
| 3103 | */ |
| 3104 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3105 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
| 3107 | /* |
| 3108 | * Account for change in delayed indirect blocks. |
| 3109 | * Nothing to do for disk quota accounting here. |
| 3110 | */ |
| 3111 | ASSERT(da_old >= da_new); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 3112 | if (da_old > da_new) { |
| 3113 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 3114 | (int64_t)(da_old - da_new), 0); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 3115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | done: |
| 3117 | *logflagsp = flags; |
| 3118 | return error; |
| 3119 | } |
| 3120 | |
| 3121 | /* |
| 3122 | * Remove the entry "free" from the free item list. Prev points to the |
| 3123 | * previous entry, unless "free" is the head of the list. |
| 3124 | */ |
| 3125 | STATIC void |
| 3126 | xfs_bmap_del_free( |
| 3127 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3128 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3129 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3130 | { |
| 3131 | if (prev) |
| 3132 | prev->xbfi_next = free->xbfi_next; |
| 3133 | else |
| 3134 | flist->xbf_first = free->xbfi_next; |
| 3135 | flist->xbf_count--; |
| 3136 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3137 | } |
| 3138 | |
| 3139 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | * Convert an extents-format file into a btree-format file. |
| 3141 | * The new file will have a root block (in the inode) and a single child block. |
| 3142 | */ |
| 3143 | STATIC int /* error */ |
| 3144 | xfs_bmap_extents_to_btree( |
| 3145 | xfs_trans_t *tp, /* transaction pointer */ |
| 3146 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3147 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3148 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3149 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3150 | int wasdel, /* converting a delayed alloc */ |
| 3151 | int *logflagsp, /* inode logging flags */ |
| 3152 | int whichfork) /* data or attr fork */ |
| 3153 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3154 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3156 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3157 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3158 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3160 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3162 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3164 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3165 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3166 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3168 | |
| 3169 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3170 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3171 | ASSERT(ifp->if_ext_max == |
| 3172 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3173 | /* |
| 3174 | * Make space in the inode incore. |
| 3175 | */ |
| 3176 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3177 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | /* |
| 3180 | * Fill in the root. |
| 3181 | */ |
| 3182 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3183 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3184 | block->bb_level = cpu_to_be16(1); |
| 3185 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3186 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3187 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | /* |
| 3190 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3191 | */ |
| 3192 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3193 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | cur->bc_private.b.firstblock = *firstblock; |
| 3195 | cur->bc_private.b.flist = flist; |
| 3196 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3197 | /* |
| 3198 | * Convert to a btree with two levels, one record in root. |
| 3199 | */ |
| 3200 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3201 | args.tp = tp; |
| 3202 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3203 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | if (*firstblock == NULLFSBLOCK) { |
| 3205 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3206 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3207 | } else if (flist->xbf_low) { |
| 3208 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3209 | args.fsbno = *firstblock; |
| 3210 | } else { |
| 3211 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3212 | args.fsbno = *firstblock; |
| 3213 | } |
| 3214 | args.minlen = args.maxlen = args.prod = 1; |
| 3215 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3216 | args.minalignslop = 0; |
| 3217 | args.wasdel = wasdel; |
| 3218 | *logflagsp = 0; |
| 3219 | if ((error = xfs_alloc_vextent(&args))) { |
| 3220 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3221 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3222 | return error; |
| 3223 | } |
| 3224 | /* |
| 3225 | * Allocation can't fail, the space was reserved. |
| 3226 | */ |
| 3227 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3228 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3229 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3230 | (flist->xbf_low && |
| 3231 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3232 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3233 | cur->bc_private.b.allocated++; |
| 3234 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3235 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3237 | /* |
| 3238 | * Fill in the child block. |
| 3239 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3240 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3241 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3243 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3244 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3245 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3247 | for (cnt = i = 0; i < nextents; i++) { |
| 3248 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3249 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3250 | arp->l0 = cpu_to_be64(ep->l0); |
| 3251 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | arp++; cnt++; |
| 3253 | } |
| 3254 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3255 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3256 | xfs_btree_set_numrecs(ablock, cnt); |
| 3257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | /* |
| 3259 | * Fill in the root key and pointer. |
| 3260 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3261 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3262 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3263 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3264 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3265 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3266 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | /* |
| 3269 | * Do all this logging at the end so that |
| 3270 | * the root is at the right level. |
| 3271 | */ |
Christoph Hellwig | fd6bcc5 | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3272 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3273 | 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] | 3274 | ASSERT(*curp == NULL); |
| 3275 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3276 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | return 0; |
| 3278 | } |
| 3279 | |
| 3280 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3281 | * Calculate the default attribute fork offset for newly created inodes. |
| 3282 | */ |
| 3283 | uint |
| 3284 | xfs_default_attroffset( |
| 3285 | struct xfs_inode *ip) |
| 3286 | { |
| 3287 | struct xfs_mount *mp = ip->i_mount; |
| 3288 | uint offset; |
| 3289 | |
| 3290 | if (mp->m_sb.sb_inodesize == 256) { |
| 3291 | offset = XFS_LITINO(mp) - |
| 3292 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3293 | } else { |
| 3294 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3295 | } |
| 3296 | |
| 3297 | ASSERT(offset < XFS_LITINO(mp)); |
| 3298 | return offset; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3302 | * Helper routine to reset inode di_forkoff field when switching |
| 3303 | * attribute fork from local to extent format - we reset it where |
| 3304 | * possible to make space available for inline data fork extents. |
| 3305 | */ |
| 3306 | STATIC void |
| 3307 | xfs_bmap_forkoff_reset( |
| 3308 | xfs_mount_t *mp, |
| 3309 | xfs_inode_t *ip, |
| 3310 | int whichfork) |
| 3311 | { |
| 3312 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3313 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3314 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3315 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3316 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3317 | |
| 3318 | if (dfl_forkoff > ip->i_d.di_forkoff) { |
| 3319 | ip->i_d.di_forkoff = dfl_forkoff; |
| 3320 | ip->i_df.if_ext_max = |
| 3321 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3322 | ip->i_afp->if_ext_max = |
| 3323 | XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3324 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | * Convert a local file to an extents file. |
| 3330 | * This code is out of bounds for data forks of regular files, |
| 3331 | * since the file data needs to get logged so things will stay consistent. |
| 3332 | * (The bmap-level manipulations are ok, though). |
| 3333 | */ |
| 3334 | STATIC int /* error */ |
| 3335 | xfs_bmap_local_to_extents( |
| 3336 | xfs_trans_t *tp, /* transaction pointer */ |
| 3337 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3338 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3339 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3340 | int *logflagsp, /* inode logging flags */ |
| 3341 | int whichfork) /* data or attr fork */ |
| 3342 | { |
| 3343 | int error; /* error return value */ |
| 3344 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3346 | |
| 3347 | /* |
| 3348 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3349 | * So sending the data fork of a regular inode is invalid. |
| 3350 | */ |
| 3351 | ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG && |
| 3352 | whichfork == XFS_DATA_FORK)); |
| 3353 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3354 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3355 | flags = 0; |
| 3356 | error = 0; |
| 3357 | if (ifp->if_bytes) { |
| 3358 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3359 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3360 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | |
| 3362 | args.tp = tp; |
| 3363 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3364 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3365 | ASSERT((ifp->if_flags & |
| 3366 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3367 | /* |
| 3368 | * Allocate a block. We know we need only one, since the |
| 3369 | * file currently fits in an inode. |
| 3370 | */ |
| 3371 | if (*firstblock == NULLFSBLOCK) { |
| 3372 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3373 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3374 | } else { |
| 3375 | args.fsbno = *firstblock; |
| 3376 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3377 | } |
| 3378 | args.total = total; |
| 3379 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3380 | args.isfl = args.minalignslop = 0; |
| 3381 | args.minlen = args.maxlen = args.prod = 1; |
| 3382 | if ((error = xfs_alloc_vextent(&args))) |
| 3383 | goto done; |
| 3384 | /* |
| 3385 | * Can't fail, the space was reserved. |
| 3386 | */ |
| 3387 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3388 | ASSERT(args.len == 1); |
| 3389 | *firstblock = args.fsbno; |
| 3390 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
| 3391 | memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data, |
| 3392 | ifp->if_bytes); |
| 3393 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3394 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3396 | xfs_iext_add(ifp, 0, 1); |
| 3397 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3399 | trace_xfs_bmap_post_update(ip, 0, |
| 3400 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3401 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3403 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3404 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3406 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3407 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3409 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3412 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3413 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3414 | flags |= XFS_ILOG_CORE; |
| 3415 | done: |
| 3416 | *logflagsp = flags; |
| 3417 | return error; |
| 3418 | } |
| 3419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3421 | * Search the extent records for the entry containing block bno. |
| 3422 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3423 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3424 | * entry (null if none). Else, *lastxp will be set to the index |
| 3425 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3426 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3427 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3428 | xfs_bmap_search_multi_extents( |
| 3429 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3430 | xfs_fileoff_t bno, /* block number searched for */ |
| 3431 | int *eofp, /* out: end of file found */ |
| 3432 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3433 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3434 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3435 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3436 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3437 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3438 | |
| 3439 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3440 | * Initialize the extent entry structure to catch access to |
| 3441 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3442 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3443 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3444 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3445 | gotp->br_state = XFS_EXT_INVALID; |
| 3446 | #if XFS_BIG_BLKNOS |
| 3447 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3448 | #else |
| 3449 | gotp->br_startblock = 0xffffa5a5; |
| 3450 | #endif |
| 3451 | prevp->br_startoff = NULLFILEOFF; |
| 3452 | |
| 3453 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3454 | if (lastx > 0) { |
| 3455 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3456 | } |
| 3457 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3458 | xfs_bmbt_get_all(ep, gotp); |
| 3459 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3460 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3461 | if (lastx > 0) { |
| 3462 | *gotp = *prevp; |
| 3463 | } |
| 3464 | *eofp = 1; |
| 3465 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3466 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3467 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3468 | return ep; |
| 3469 | } |
| 3470 | |
| 3471 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | * Search the extents list for the inode, for the extent containing bno. |
| 3473 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3474 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3475 | * Else, *lastxp will be set to the index of the found |
| 3476 | * entry; *gotp will contain the entry. |
| 3477 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3478 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | xfs_bmap_search_extents( |
| 3480 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3481 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3482 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3483 | int *eofp, /* out: end of file found */ |
| 3484 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3485 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3486 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3487 | { |
| 3488 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3489 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | |
| 3491 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3492 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3494 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3495 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3496 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3497 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3498 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3499 | "Access to block zero in inode %llu " |
| 3500 | "start_block: %llx start_off: %llx " |
| 3501 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3502 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3503 | (unsigned long long)gotp->br_startblock, |
| 3504 | (unsigned long long)gotp->br_startoff, |
| 3505 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3506 | gotp->br_state, *lastxp); |
| 3507 | *lastxp = NULLEXTNUM; |
| 3508 | *eofp = 1; |
| 3509 | return NULL; |
| 3510 | } |
| 3511 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | } |
| 3513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | /* |
| 3515 | * Compute the worst-case number of indirect blocks that will be used |
| 3516 | * for ip's delayed extent of length "len". |
| 3517 | */ |
| 3518 | STATIC xfs_filblks_t |
| 3519 | xfs_bmap_worst_indlen( |
| 3520 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3521 | xfs_filblks_t len) /* delayed extent length */ |
| 3522 | { |
| 3523 | int level; /* btree level number */ |
| 3524 | int maxrecs; /* maximum record count at this level */ |
| 3525 | xfs_mount_t *mp; /* mount structure */ |
| 3526 | xfs_filblks_t rval; /* return value */ |
| 3527 | |
| 3528 | mp = ip->i_mount; |
| 3529 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3530 | for (level = 0, rval = 0; |
| 3531 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3532 | level++) { |
| 3533 | len += maxrecs - 1; |
| 3534 | do_div(len, maxrecs); |
| 3535 | rval += len; |
| 3536 | if (len == 1) |
| 3537 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3538 | level - 1; |
| 3539 | if (level == 0) |
| 3540 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3541 | } |
| 3542 | return rval; |
| 3543 | } |
| 3544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | /* |
| 3546 | * Convert inode from non-attributed to attributed. |
| 3547 | * Must not be in a transaction, ip must not be locked. |
| 3548 | */ |
| 3549 | int /* error code */ |
| 3550 | xfs_bmap_add_attrfork( |
| 3551 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3552 | int size, /* space new attribute needs */ |
| 3553 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3556 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3559 | int blks; /* space reservation */ |
| 3560 | int version = 1; /* superblock attr version */ |
| 3561 | int committed; /* xaction was committed */ |
| 3562 | int logflags; /* logging flags */ |
| 3563 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3565 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | ASSERT(ip->i_df.if_ext_max == |
| 3567 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3569 | mp = ip->i_mount; |
| 3570 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3571 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3572 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3573 | if (rsvd) |
| 3574 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3575 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3576 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3577 | goto error0; |
| 3578 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3579 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3581 | XFS_QMOPT_RES_REGBLKS); |
| 3582 | if (error) { |
| 3583 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3584 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3585 | return error; |
| 3586 | } |
| 3587 | if (XFS_IFORK_Q(ip)) |
| 3588 | goto error1; |
| 3589 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3590 | /* |
| 3591 | * For inodes coming from pre-6.2 filesystems. |
| 3592 | */ |
| 3593 | ASSERT(ip->i_d.di_aformat == 0); |
| 3594 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3595 | } |
| 3596 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3597 | |
| 3598 | xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | switch (ip->i_d.di_format) { |
| 3602 | case XFS_DINODE_FMT_DEV: |
| 3603 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3604 | break; |
| 3605 | case XFS_DINODE_FMT_UUID: |
| 3606 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3607 | break; |
| 3608 | case XFS_DINODE_FMT_LOCAL: |
| 3609 | case XFS_DINODE_FMT_EXTENTS: |
| 3610 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3611 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3612 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3613 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3614 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3615 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | break; |
| 3617 | default: |
| 3618 | ASSERT(0); |
| 3619 | error = XFS_ERROR(EINVAL); |
| 3620 | goto error1; |
| 3621 | } |
| 3622 | ip->i_df.if_ext_max = |
| 3623 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3624 | ASSERT(ip->i_afp == NULL); |
| 3625 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 3626 | ip->i_afp->if_ext_max = |
| 3627 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3628 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3629 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3630 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | switch (ip->i_d.di_format) { |
| 3632 | case XFS_DINODE_FMT_LOCAL: |
| 3633 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3634 | &logflags); |
| 3635 | break; |
| 3636 | case XFS_DINODE_FMT_EXTENTS: |
| 3637 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3638 | &flist, &logflags); |
| 3639 | break; |
| 3640 | case XFS_DINODE_FMT_BTREE: |
| 3641 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3642 | &logflags); |
| 3643 | break; |
| 3644 | default: |
| 3645 | error = 0; |
| 3646 | break; |
| 3647 | } |
| 3648 | if (logflags) |
| 3649 | xfs_trans_log_inode(tp, ip, logflags); |
| 3650 | if (error) |
| 3651 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3652 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3653 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3654 | __int64_t sbfields = 0; |
| 3655 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3656 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3657 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3658 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3659 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3660 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3661 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3662 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3663 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3664 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3665 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3666 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3667 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3669 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3671 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | goto error2; |
Dave Chinner | 713bf88 | 2010-03-08 11:26:23 +1100 | [diff] [blame] | 3673 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | ASSERT(ip->i_df.if_ext_max == |
| 3675 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3676 | return error; |
| 3677 | error2: |
| 3678 | xfs_bmap_cancel(&flist); |
| 3679 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3681 | error0: |
| 3682 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 3683 | ASSERT(ip->i_df.if_ext_max == |
| 3684 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3685 | return error; |
| 3686 | } |
| 3687 | |
| 3688 | /* |
| 3689 | * Add the extent to the list of extents to be free at transaction end. |
| 3690 | * The list is maintained sorted (by block number). |
| 3691 | */ |
| 3692 | /* ARGSUSED */ |
| 3693 | void |
| 3694 | xfs_bmap_add_free( |
| 3695 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3696 | xfs_filblks_t len, /* length of extent */ |
| 3697 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3698 | xfs_mount_t *mp) /* mount point structure */ |
| 3699 | { |
| 3700 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3701 | xfs_bmap_free_item_t *new; /* new element */ |
| 3702 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3703 | #ifdef DEBUG |
| 3704 | xfs_agnumber_t agno; |
| 3705 | xfs_agblock_t agbno; |
| 3706 | |
| 3707 | ASSERT(bno != NULLFSBLOCK); |
| 3708 | ASSERT(len > 0); |
| 3709 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3710 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3712 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3713 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3714 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3715 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3716 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3717 | #endif |
| 3718 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3719 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3720 | new->xbfi_startblock = bno; |
| 3721 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3722 | for (prev = NULL, cur = flist->xbf_first; |
| 3723 | cur != NULL; |
| 3724 | prev = cur, cur = cur->xbfi_next) { |
| 3725 | if (cur->xbfi_startblock >= bno) |
| 3726 | break; |
| 3727 | } |
| 3728 | if (prev) |
| 3729 | prev->xbfi_next = new; |
| 3730 | else |
| 3731 | flist->xbf_first = new; |
| 3732 | new->xbfi_next = cur; |
| 3733 | flist->xbf_count++; |
| 3734 | } |
| 3735 | |
| 3736 | /* |
| 3737 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3738 | * in this filesystem. Done once, during mount. |
| 3739 | */ |
| 3740 | void |
| 3741 | xfs_bmap_compute_maxlevels( |
| 3742 | xfs_mount_t *mp, /* file system mount structure */ |
| 3743 | int whichfork) /* data or attr fork */ |
| 3744 | { |
| 3745 | int level; /* btree level */ |
| 3746 | uint maxblocks; /* max blocks at this level */ |
| 3747 | uint maxleafents; /* max leaf entries possible */ |
| 3748 | int maxrootrecs; /* max records in root block */ |
| 3749 | int minleafrecs; /* min records in leaf block */ |
| 3750 | int minnoderecs; /* min records in node block */ |
| 3751 | int sz; /* root block size */ |
| 3752 | |
| 3753 | /* |
| 3754 | * The maximum number of extents in a file, hence the maximum |
| 3755 | * number of leaf entries, is controlled by the type of di_nextents |
| 3756 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3757 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3758 | * |
| 3759 | * 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] | 3760 | * the fork offset of all the inodes will be |
| 3761 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3762 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3763 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3764 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3765 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3766 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3767 | if (whichfork == XFS_DATA_FORK) { |
| 3768 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3769 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3770 | } else { |
| 3771 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3772 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3773 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3774 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3776 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3778 | for (level = 1; maxblocks > 1; level++) { |
| 3779 | if (maxblocks <= maxrootrecs) |
| 3780 | maxblocks = 1; |
| 3781 | else |
| 3782 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3783 | } |
| 3784 | mp->m_bm_maxlevels[whichfork] = level; |
| 3785 | } |
| 3786 | |
| 3787 | /* |
| 3788 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3789 | * caller. Frees all the extents that need freeing, which must be done |
| 3790 | * last due to locking considerations. We never free any extents in |
| 3791 | * the first transaction. This is to allow the caller to make the first |
| 3792 | * transaction a synchronous one so that the pointers to the data being |
| 3793 | * broken in this transaction will be permanent before the data is actually |
| 3794 | * freed. This is necessary to prevent blocks from being reallocated |
| 3795 | * and written to before the free and reallocation are actually permanent. |
| 3796 | * We do not just make the first transaction synchronous here, because |
| 3797 | * there are more efficient ways to gain the same protection in some cases |
| 3798 | * (see the file truncation code). |
| 3799 | * |
| 3800 | * Return 1 if the given transaction was committed and a new one |
| 3801 | * started, and 0 otherwise in the committed parameter. |
| 3802 | */ |
| 3803 | /*ARGSUSED*/ |
| 3804 | int /* error */ |
| 3805 | xfs_bmap_finish( |
| 3806 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3807 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | int *committed) /* xact committed or not */ |
| 3809 | { |
| 3810 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3811 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3812 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3813 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | unsigned int logres; /* new log reservation */ |
| 3815 | unsigned int logcount; /* new log count */ |
| 3816 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3817 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3818 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3819 | |
| 3820 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3821 | if (flist->xbf_count == 0) { |
| 3822 | *committed = 0; |
| 3823 | return 0; |
| 3824 | } |
| 3825 | ntp = *tp; |
| 3826 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3827 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3828 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3829 | free->xbfi_blockcount); |
| 3830 | logres = ntp->t_log_res; |
| 3831 | logcount = ntp->t_log_count; |
| 3832 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3833 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | *tp = ntp; |
| 3835 | *committed = 1; |
| 3836 | /* |
| 3837 | * We have a new transaction, so we should return committed=1, |
| 3838 | * even though we're returning an error. |
| 3839 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3840 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3842 | |
| 3843 | /* |
| 3844 | * transaction commit worked ok so we can drop the extra ticket |
| 3845 | * reference that we gained in xfs_trans_dup() |
| 3846 | */ |
| 3847 | xfs_log_ticket_put(ntp->t_ticket); |
| 3848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 3850 | logcount))) |
| 3851 | return error; |
| 3852 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 3853 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 3854 | next = free->xbfi_next; |
| 3855 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 3856 | free->xbfi_blockcount))) { |
| 3857 | /* |
| 3858 | * The bmap free list will be cleaned up at a |
| 3859 | * higher level. The EFI will be canceled when |
| 3860 | * this transaction is aborted. |
| 3861 | * Need to force shutdown here to make sure it |
| 3862 | * happens, since this transaction may not be |
| 3863 | * dirty yet. |
| 3864 | */ |
| 3865 | mp = ntp->t_mountp; |
| 3866 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 3867 | xfs_force_shutdown(mp, |
| 3868 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3869 | SHUTDOWN_CORRUPT_INCORE : |
| 3870 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | return error; |
| 3872 | } |
| 3873 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 3874 | free->xbfi_blockcount); |
| 3875 | xfs_bmap_del_free(flist, NULL, free); |
| 3876 | } |
| 3877 | return 0; |
| 3878 | } |
| 3879 | |
| 3880 | /* |
| 3881 | * Free up any items left in the list. |
| 3882 | */ |
| 3883 | void |
| 3884 | xfs_bmap_cancel( |
| 3885 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 3886 | { |
| 3887 | xfs_bmap_free_item_t *free; /* free list item */ |
| 3888 | xfs_bmap_free_item_t *next; |
| 3889 | |
| 3890 | if (flist->xbf_count == 0) |
| 3891 | return; |
| 3892 | ASSERT(flist->xbf_first != NULL); |
| 3893 | for (free = flist->xbf_first; free; free = next) { |
| 3894 | next = free->xbfi_next; |
| 3895 | xfs_bmap_del_free(flist, NULL, free); |
| 3896 | } |
| 3897 | ASSERT(flist->xbf_count == 0); |
| 3898 | } |
| 3899 | |
| 3900 | /* |
| 3901 | * Returns the file-relative block number of the first unused block(s) |
| 3902 | * in the file with at least "len" logically contiguous blocks free. |
| 3903 | * This is the lowest-address hole if the file has holes, else the first block |
| 3904 | * past the end of file. |
| 3905 | * Return 0 if the file is currently local (in-inode). |
| 3906 | */ |
| 3907 | int /* error */ |
| 3908 | xfs_bmap_first_unused( |
| 3909 | xfs_trans_t *tp, /* transaction pointer */ |
| 3910 | xfs_inode_t *ip, /* incore inode */ |
| 3911 | xfs_extlen_t len, /* size of hole to find */ |
| 3912 | xfs_fileoff_t *first_unused, /* unused block */ |
| 3913 | int whichfork) /* data or attr fork */ |
| 3914 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3916 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3918 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 3919 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 3920 | xfs_fileoff_t max; /* starting useful block */ |
| 3921 | xfs_fileoff_t off; /* offset for this block */ |
| 3922 | xfs_extnum_t nextents; /* number of extent entries */ |
| 3923 | |
| 3924 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 3925 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 3926 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3927 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3928 | *first_unused = 0; |
| 3929 | return 0; |
| 3930 | } |
| 3931 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3932 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3933 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3934 | return error; |
| 3935 | lowest = *first_unused; |
| 3936 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3937 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3938 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | off = xfs_bmbt_get_startoff(ep); |
| 3940 | /* |
| 3941 | * See if the hole before this extent will work. |
| 3942 | */ |
| 3943 | if (off >= lowest + len && off - max >= len) { |
| 3944 | *first_unused = max; |
| 3945 | return 0; |
| 3946 | } |
| 3947 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 3948 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 3949 | } |
| 3950 | *first_unused = max; |
| 3951 | return 0; |
| 3952 | } |
| 3953 | |
| 3954 | /* |
| 3955 | * Returns the file-relative block number of the last block + 1 before |
| 3956 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3957 | * This is not based on i_size, it is based on the extent records. |
| 3958 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | */ |
| 3960 | int /* error */ |
| 3961 | xfs_bmap_last_before( |
| 3962 | xfs_trans_t *tp, /* transaction pointer */ |
| 3963 | xfs_inode_t *ip, /* incore inode */ |
| 3964 | xfs_fileoff_t *last_block, /* last block */ |
| 3965 | int whichfork) /* data or attr fork */ |
| 3966 | { |
| 3967 | xfs_fileoff_t bno; /* input file offset */ |
| 3968 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3969 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | int error; /* error return value */ |
| 3971 | xfs_bmbt_irec_t got; /* current extent value */ |
| 3972 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3973 | xfs_extnum_t lastx; /* last extent used */ |
| 3974 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 3975 | |
| 3976 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 3977 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3978 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 3979 | return XFS_ERROR(EIO); |
| 3980 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3981 | *last_block = 0; |
| 3982 | return 0; |
| 3983 | } |
| 3984 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3985 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3986 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3987 | return error; |
| 3988 | bno = *last_block - 1; |
| 3989 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 3990 | &prev); |
| 3991 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 3992 | if (prev.br_startoff == NULLFILEOFF) |
| 3993 | *last_block = 0; |
| 3994 | else |
| 3995 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 3996 | } |
| 3997 | /* |
| 3998 | * Otherwise *last_block is already the right answer. |
| 3999 | */ |
| 4000 | return 0; |
| 4001 | } |
| 4002 | |
| 4003 | /* |
| 4004 | * 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] | 4005 | * the file. This is not based on i_size, it is based on the extent records. |
| 4006 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | */ |
| 4008 | int /* error */ |
| 4009 | xfs_bmap_last_offset( |
| 4010 | xfs_trans_t *tp, /* transaction pointer */ |
| 4011 | xfs_inode_t *ip, /* incore inode */ |
| 4012 | xfs_fileoff_t *last_block, /* last block */ |
| 4013 | int whichfork) /* data or attr fork */ |
| 4014 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4015 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | int error; /* error return value */ |
| 4017 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4018 | xfs_extnum_t nextents; /* number of extent entries */ |
| 4019 | |
| 4020 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4021 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4022 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 4023 | return XFS_ERROR(EIO); |
| 4024 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4025 | *last_block = 0; |
| 4026 | return 0; |
| 4027 | } |
| 4028 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4029 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4030 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4031 | return error; |
| 4032 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4033 | if (!nextents) { |
| 4034 | *last_block = 0; |
| 4035 | return 0; |
| 4036 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4037 | ep = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); |
| 4039 | return 0; |
| 4040 | } |
| 4041 | |
| 4042 | /* |
| 4043 | * Returns whether the selected fork of the inode has exactly one |
| 4044 | * block or not. For the data fork we check this matches di_size, |
| 4045 | * implying the file's range is 0..bsize-1. |
| 4046 | */ |
| 4047 | int /* 1=>1 block, 0=>otherwise */ |
| 4048 | xfs_bmap_one_block( |
| 4049 | xfs_inode_t *ip, /* incore inode */ |
| 4050 | int whichfork) /* data or attr fork */ |
| 4051 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4052 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4054 | int rval; /* return value */ |
| 4055 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 4056 | |
| 4057 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4058 | if (whichfork == XFS_DATA_FORK) { |
| 4059 | return ((ip->i_d.di_mode & S_IFMT) == S_IFREG) ? |
| 4060 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 4061 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 4062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | #endif /* !DEBUG */ |
| 4064 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4065 | return 0; |
| 4066 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4067 | return 0; |
| 4068 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4069 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4070 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | xfs_bmbt_get_all(ep, &s); |
| 4072 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4073 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 4074 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | return rval; |
| 4076 | } |
| 4077 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4078 | STATIC int |
| 4079 | xfs_bmap_sanity_check( |
| 4080 | struct xfs_mount *mp, |
| 4081 | struct xfs_buf *bp, |
| 4082 | int level) |
| 4083 | { |
| 4084 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4085 | |
| 4086 | if (be32_to_cpu(block->bb_magic) != XFS_BMAP_MAGIC || |
| 4087 | be16_to_cpu(block->bb_level) != level || |
| 4088 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4089 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4090 | return 0; |
| 4091 | return 1; |
| 4092 | } |
| 4093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | /* |
| 4095 | * Read in the extents to if_extents. |
| 4096 | * All inode fields are set up by caller, we just traverse the btree |
| 4097 | * and copy the records in. If the file system cannot contain unwritten |
| 4098 | * extents, the records are checked for no "state" flags. |
| 4099 | */ |
| 4100 | int /* error */ |
| 4101 | xfs_bmap_read_extents( |
| 4102 | xfs_trans_t *tp, /* transaction pointer */ |
| 4103 | xfs_inode_t *ip, /* incore inode */ |
| 4104 | int whichfork) /* data or attr fork */ |
| 4105 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4106 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4108 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4109 | int error; /* error return value */ |
| 4110 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4112 | xfs_ifork_t *ifp; /* fork structure */ |
| 4113 | int level; /* btree level, for checking */ |
| 4114 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4115 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | /* REFERENCED */ |
| 4117 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | |
| 4119 | bno = NULLFSBLOCK; |
| 4120 | mp = ip->i_mount; |
| 4121 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4122 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4123 | XFS_EXTFMT_INODE(ip); |
| 4124 | block = ifp->if_broot; |
| 4125 | /* |
| 4126 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4127 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4128 | level = be16_to_cpu(block->bb_level); |
| 4129 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4130 | 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] | 4131 | bno = be64_to_cpu(*pp); |
| 4132 | ASSERT(bno != NULLDFSBNO); |
| 4133 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4134 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | /* |
| 4136 | * Go down the tree until leaf level is reached, following the first |
| 4137 | * pointer (leftmost) at each level. |
| 4138 | */ |
| 4139 | while (level-- > 0) { |
| 4140 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4141 | XFS_BMAP_BTREE_REF))) |
| 4142 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4143 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4145 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | error0); |
| 4147 | if (level == 0) |
| 4148 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4149 | 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] | 4150 | bno = be64_to_cpu(*pp); |
| 4151 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | xfs_trans_brelse(tp, bp); |
| 4153 | } |
| 4154 | /* |
| 4155 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4156 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4157 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | i = 0; |
| 4159 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4160 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | */ |
| 4162 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4163 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | xfs_fsblock_t nextbno; |
| 4165 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4166 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | |
| 4168 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4169 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | if (unlikely(i + num_recs > room)) { |
| 4171 | ASSERT(i + num_recs <= room); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4172 | xfs_warn(ip->i_mount, |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4173 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | (unsigned long long) ip->i_ino); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4175 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 4176 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | goto error0; |
| 4178 | } |
| 4179 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4180 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | error0); |
| 4182 | /* |
| 4183 | * Read-ahead the next leaf block, if any. |
| 4184 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4185 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | if (nextbno != NULLFSBLOCK) |
| 4187 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4188 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4189 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4191 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4192 | start = i; |
| 4193 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4194 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4195 | trp->l0 = be64_to_cpu(frp->l0); |
| 4196 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | } |
| 4198 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4199 | /* |
| 4200 | * Check all attribute bmap btree records and |
| 4201 | * any "older" data bmap btree records for a |
| 4202 | * set bit in the "extent flag" position. |
| 4203 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4204 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4205 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4207 | XFS_ERRLEVEL_LOW, |
| 4208 | ip->i_mount); |
| 4209 | goto error0; |
| 4210 | } |
| 4211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | xfs_trans_brelse(tp, bp); |
| 4213 | bno = nextbno; |
| 4214 | /* |
| 4215 | * If we've reached the end, stop. |
| 4216 | */ |
| 4217 | if (bno == NULLFSBLOCK) |
| 4218 | break; |
| 4219 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4220 | XFS_BMAP_BTREE_REF))) |
| 4221 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4222 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4224 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4226 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | return 0; |
| 4228 | error0: |
| 4229 | xfs_trans_brelse(tp, bp); |
| 4230 | return XFS_ERROR(EFSCORRUPTED); |
| 4231 | } |
| 4232 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4233 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4235 | * 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] | 4236 | */ |
| 4237 | void |
| 4238 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4240 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4241 | int whichfork, /* data or attr fork */ |
| 4242 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4244 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4246 | int state = 0; |
| 4247 | |
| 4248 | if (whichfork == XFS_ATTR_FORK) |
| 4249 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
| 4251 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4252 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4253 | for (idx = 0; idx < cnt; idx++) |
| 4254 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | /* |
| 4258 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4259 | * given the callers original parameters. Specifically check the |
| 4260 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4261 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4262 | */ |
| 4263 | STATIC void |
| 4264 | xfs_bmap_validate_ret( |
| 4265 | xfs_fileoff_t bno, |
| 4266 | xfs_filblks_t len, |
| 4267 | int flags, |
| 4268 | xfs_bmbt_irec_t *mval, |
| 4269 | int nmap, |
| 4270 | int ret_nmap) |
| 4271 | { |
| 4272 | int i; /* index to map values */ |
| 4273 | |
| 4274 | ASSERT(ret_nmap <= nmap); |
| 4275 | |
| 4276 | for (i = 0; i < ret_nmap; i++) { |
| 4277 | ASSERT(mval[i].br_blockcount > 0); |
| 4278 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4279 | ASSERT(mval[i].br_startoff >= bno); |
| 4280 | ASSERT(mval[i].br_blockcount <= len); |
| 4281 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4282 | bno + len); |
| 4283 | } else { |
| 4284 | ASSERT(mval[i].br_startoff < bno + len); |
| 4285 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4286 | bno); |
| 4287 | } |
| 4288 | ASSERT(i == 0 || |
| 4289 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4290 | mval[i].br_startoff); |
| 4291 | if ((flags & XFS_BMAPI_WRITE) && !(flags & XFS_BMAPI_DELAY)) |
| 4292 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4293 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 4294 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4295 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4296 | } |
| 4297 | } |
| 4298 | #endif /* DEBUG */ |
| 4299 | |
| 4300 | |
| 4301 | /* |
| 4302 | * Map file blocks to filesystem blocks. |
| 4303 | * File range is given by the bno/len pair. |
| 4304 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 4305 | * into a hole or past eof. |
| 4306 | * Only allocates blocks from a single allocation group, |
| 4307 | * to avoid locking problems. |
| 4308 | * The returned value in "firstblock" from the first call in a transaction |
| 4309 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4310 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4311 | * the first call in "total"; if no allocation group has that many free |
| 4312 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4313 | */ |
| 4314 | int /* error */ |
| 4315 | xfs_bmapi( |
| 4316 | xfs_trans_t *tp, /* transaction pointer */ |
| 4317 | xfs_inode_t *ip, /* incore inode */ |
| 4318 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4319 | xfs_filblks_t len, /* length to map in file */ |
| 4320 | int flags, /* XFS_BMAPI_... */ |
| 4321 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4322 | controls a.g. for allocs */ |
| 4323 | xfs_extlen_t total, /* total blocks needed */ |
| 4324 | xfs_bmbt_irec_t *mval, /* output: map values */ |
| 4325 | int *nmap, /* i/o: mval size/count */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4326 | xfs_bmap_free_t *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | { |
| 4328 | xfs_fsblock_t abno; /* allocated block number */ |
| 4329 | xfs_extlen_t alen; /* allocated extent length */ |
| 4330 | xfs_fileoff_t aoff; /* allocated file offset */ |
Dave Chinner | f0a0eaa | 2010-01-20 10:50:06 +1100 | [diff] [blame] | 4331 | xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | xfs_fileoff_t end; /* end of mapped file region */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4334 | int eof; /* we've hit the end of extents */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4335 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4337 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4339 | xfs_extlen_t indlen; /* indirect blocks length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4341 | int logflags; /* flags for transaction logging */ |
| 4342 | xfs_extlen_t minleft; /* min blocks left after allocation */ |
| 4343 | xfs_extlen_t minlen; /* min allocation size */ |
| 4344 | xfs_mount_t *mp; /* xfs mount structure */ |
| 4345 | int n; /* current extent index */ |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 4346 | int nallocs; /* number of extents alloc'd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | xfs_extnum_t nextents; /* number of extents in file */ |
| 4348 | xfs_fileoff_t obno; /* old block number (offset) */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4349 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | int tmp_logflags; /* temp flags holder */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4351 | int whichfork; /* data or attr fork */ |
| 4352 | char inhole; /* current location is hole in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | char wasdelay; /* old extent was delayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | char wr; /* this is a write request */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4355 | char rt; /* this is a realtime file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | #ifdef DEBUG |
| 4357 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4358 | int orig_flags; /* original flags arg value */ |
| 4359 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4360 | xfs_bmbt_irec_t *orig_mval; /* original value of mval */ |
| 4361 | int orig_nmap; /* original value of *nmap */ |
| 4362 | |
| 4363 | orig_bno = bno; |
| 4364 | orig_len = len; |
| 4365 | orig_flags = flags; |
| 4366 | orig_mval = mval; |
| 4367 | orig_nmap = *nmap; |
| 4368 | #endif |
| 4369 | ASSERT(*nmap >= 1); |
| 4370 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); |
| 4371 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4372 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4373 | mp = ip->i_mount; |
| 4374 | if (unlikely(XFS_TEST_ERROR( |
| 4375 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4376 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4377 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4378 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4379 | XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); |
| 4380 | return XFS_ERROR(EFSCORRUPTED); |
| 4381 | } |
| 4382 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4383 | return XFS_ERROR(EIO); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4384 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4386 | ASSERT(ifp->if_ext_max == |
| 4387 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4388 | if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) |
| 4389 | XFS_STATS_INC(xs_blk_mapw); |
| 4390 | else |
| 4391 | XFS_STATS_INC(xs_blk_mapr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | /* |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4393 | * IGSTATE flag is used to combine extents which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | * differ only due to the state of the extents. |
| 4395 | * This technique is used from xfs_getbmap() |
| 4396 | * when the caller does not wish to see the |
| 4397 | * separation (which is the default). |
| 4398 | * |
| 4399 | * This technique is also used when writing a |
| 4400 | * buffer which has been partially written, |
| 4401 | * (usually by being flushed during a chunkread), |
| 4402 | * to ensure one write takes place. This also |
| 4403 | * prevents a change in the xfs inode extents at |
| 4404 | * this time, intentionally. This change occurs |
| 4405 | * on completion of the write operation, in |
| 4406 | * xfs_strat_comp(), where the xfs_bmapi() call |
| 4407 | * is transactioned, and the extents combined. |
| 4408 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4409 | if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */ |
| 4410 | wr = 0; /* no allocations are allowed */ |
| 4411 | ASSERT(wr || !(flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | logflags = 0; |
| 4413 | nallocs = 0; |
| 4414 | cur = NULL; |
| 4415 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4416 | ASSERT(wr && tp); |
| 4417 | if ((error = xfs_bmap_local_to_extents(tp, ip, |
| 4418 | firstblock, total, &logflags, whichfork))) |
| 4419 | goto error0; |
| 4420 | } |
| 4421 | if (wr && *firstblock == NULLFSBLOCK) { |
| 4422 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4423 | minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | else |
| 4425 | minleft = 1; |
| 4426 | } else |
| 4427 | minleft = 0; |
| 4428 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4429 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4430 | goto error0; |
| 4431 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4432 | &prev); |
| 4433 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4434 | n = 0; |
| 4435 | end = bno + len; |
| 4436 | obno = bno; |
| 4437 | bma.ip = NULL; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | while (bno < end && n < *nmap) { |
| 4440 | /* |
| 4441 | * Reading past eof, act as though there's a hole |
| 4442 | * up to end. |
| 4443 | */ |
| 4444 | if (eof && !wr) |
| 4445 | got.br_startoff = end; |
| 4446 | inhole = eof || got.br_startoff > bno; |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4447 | wasdelay = wr && !inhole && !(flags & XFS_BMAPI_DELAY) && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4448 | isnullstartblock(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | /* |
| 4450 | * First, deal with the hole before the allocated space |
| 4451 | * that we found, if any. |
| 4452 | */ |
| 4453 | if (wr && (inhole || wasdelay)) { |
| 4454 | /* |
| 4455 | * For the wasdelay case, we could also just |
| 4456 | * allocate the stuff asked for in this bmap call |
| 4457 | * but that wouldn't be as good. |
| 4458 | */ |
Christoph Hellwig | cd8b0bb | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4459 | if (wasdelay) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | alen = (xfs_extlen_t)got.br_blockcount; |
| 4461 | aoff = got.br_startoff; |
| 4462 | if (lastx != NULLEXTNUM && lastx) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4463 | ep = xfs_iext_get_ext(ifp, lastx - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | xfs_bmbt_get_all(ep, &prev); |
| 4465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | } else { |
| 4467 | alen = (xfs_extlen_t) |
| 4468 | XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4469 | if (!eof) |
| 4470 | alen = (xfs_extlen_t) |
| 4471 | XFS_FILBLKS_MIN(alen, |
| 4472 | got.br_startoff - bno); |
| 4473 | aoff = bno; |
| 4474 | } |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4475 | minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; |
| 4476 | if (flags & XFS_BMAPI_DELAY) { |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4477 | xfs_extlen_t extsz; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4478 | |
| 4479 | /* Figure out the extent size, adjust alen */ |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 4480 | extsz = xfs_get_extsz_hint(ip); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4481 | if (extsz) { |
Dave Chinner | 4ce1598 | 2011-01-27 12:17:58 +1100 | [diff] [blame] | 4482 | /* |
| 4483 | * make sure we don't exceed a single |
| 4484 | * extent length when we align the |
| 4485 | * extent by reducing length we are |
| 4486 | * going to allocate by the maximum |
| 4487 | * amount extent size aligment may |
| 4488 | * require. |
| 4489 | */ |
| 4490 | alen = XFS_FILBLKS_MIN(len, |
| 4491 | MAXEXTLEN - (2 * extsz - 1)); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4492 | error = xfs_bmap_extsize_align(mp, |
| 4493 | &got, &prev, extsz, |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4494 | rt, eof, |
| 4495 | flags&XFS_BMAPI_DELAY, |
| 4496 | flags&XFS_BMAPI_CONVERT, |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4497 | &aoff, &alen); |
| 4498 | ASSERT(!error); |
| 4499 | } |
| 4500 | |
| 4501 | if (rt) |
| 4502 | extsz = alen / mp->m_sb.sb_rextsize; |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | /* |
| 4505 | * Make a transaction-less quota reservation for |
| 4506 | * delayed allocation blocks. This number gets |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4507 | * adjusted later. We return if we haven't |
| 4508 | * allocated blocks already inside this loop. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4510 | error = xfs_trans_reserve_quota_nblks( |
| 4511 | NULL, ip, (long)alen, 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4512 | rt ? XFS_QMOPT_RES_RTBLKS : |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4513 | XFS_QMOPT_RES_REGBLKS); |
| 4514 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | if (n == 0) { |
| 4516 | *nmap = 0; |
| 4517 | ASSERT(cur == NULL); |
Nathan Scott | 9a2a7de | 2006-03-31 13:04:49 +1000 | [diff] [blame] | 4518 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | } |
| 4520 | break; |
| 4521 | } |
| 4522 | |
| 4523 | /* |
| 4524 | * Split changing sb for alen and indlen since |
| 4525 | * they could be coming from different places. |
| 4526 | */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4527 | indlen = (xfs_extlen_t) |
| 4528 | xfs_bmap_worst_indlen(ip, alen); |
| 4529 | ASSERT(indlen > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4531 | if (rt) { |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4532 | error = xfs_mod_incore_sb(mp, |
| 4533 | XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4534 | -((int64_t)extsz), 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4535 | } else { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 4536 | error = xfs_icsb_modify_counters(mp, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4537 | XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4538 | -((int64_t)alen), 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4539 | } |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4540 | if (!error) { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 4541 | error = xfs_icsb_modify_counters(mp, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4542 | XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4543 | -((int64_t)indlen), 0); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4544 | if (error && rt) |
| 4545 | xfs_mod_incore_sb(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4546 | XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4547 | (int64_t)extsz, 0); |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4548 | else if (error) |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 4549 | xfs_icsb_modify_counters(mp, |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4550 | XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4551 | (int64_t)alen, 0); |
David Chinner | 3bdbfb1 | 2005-09-02 16:40:47 +1000 | [diff] [blame] | 4552 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4553 | |
| 4554 | if (error) { |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 4555 | if (XFS_IS_QUOTA_ON(mp)) |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4556 | /* unreserve the blocks now */ |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4557 | (void) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 4558 | xfs_trans_unreserve_quota_nblks( |
| 4559 | NULL, ip, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4560 | (long)alen, 0, rt ? |
| 4561 | XFS_QMOPT_RES_RTBLKS : |
| 4562 | XFS_QMOPT_RES_REGBLKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | break; |
| 4564 | } |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | ip->i_delayed_blks += alen; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4567 | abno = nullstartblock(indlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | } else { |
| 4569 | /* |
| 4570 | * If first time, allocate and fill in |
| 4571 | * once-only bma fields. |
| 4572 | */ |
| 4573 | if (bma.ip == NULL) { |
| 4574 | bma.tp = tp; |
| 4575 | bma.ip = ip; |
| 4576 | bma.prevp = &prev; |
| 4577 | bma.gotp = &got; |
| 4578 | bma.total = total; |
| 4579 | bma.userdata = 0; |
| 4580 | } |
| 4581 | /* Indicate if this is the first user data |
| 4582 | * in the file, or just any user data. |
| 4583 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4584 | if (!(flags & XFS_BMAPI_METADATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | bma.userdata = (aoff == 0) ? |
| 4586 | XFS_ALLOC_INITIAL_USER_DATA : |
| 4587 | XFS_ALLOC_USERDATA; |
| 4588 | } |
| 4589 | /* |
| 4590 | * Fill in changeable bma fields. |
| 4591 | */ |
| 4592 | bma.eof = eof; |
| 4593 | bma.firstblock = *firstblock; |
| 4594 | bma.alen = alen; |
| 4595 | bma.off = aoff; |
Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 4596 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | bma.wasdel = wasdelay; |
| 4598 | bma.minlen = minlen; |
| 4599 | bma.low = flist->xbf_low; |
| 4600 | bma.minleft = minleft; |
| 4601 | /* |
| 4602 | * Only want to do the alignment at the |
| 4603 | * eof if it is userdata and allocation length |
| 4604 | * is larger than a stripe unit. |
| 4605 | */ |
| 4606 | if (mp->m_dalign && alen >= mp->m_dalign && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4607 | (!(flags & XFS_BMAPI_METADATA)) && |
| 4608 | (whichfork == XFS_DATA_FORK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | if ((error = xfs_bmap_isaeof(ip, aoff, |
| 4610 | whichfork, &bma.aeof))) |
| 4611 | goto error0; |
| 4612 | } else |
| 4613 | bma.aeof = 0; |
| 4614 | /* |
| 4615 | * Call allocator. |
| 4616 | */ |
| 4617 | if ((error = xfs_bmap_alloc(&bma))) |
| 4618 | goto error0; |
| 4619 | /* |
| 4620 | * Copy out result fields. |
| 4621 | */ |
| 4622 | abno = bma.rval; |
| 4623 | if ((flist->xbf_low = bma.low)) |
| 4624 | minleft = 0; |
| 4625 | alen = bma.alen; |
| 4626 | aoff = bma.off; |
| 4627 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4628 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4629 | XFS_FSB_TO_AGNO(mp, bma.firstblock) || |
| 4630 | (flist->xbf_low && |
| 4631 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4632 | XFS_FSB_TO_AGNO(mp, bma.firstblock))); |
| 4633 | *firstblock = bma.firstblock; |
| 4634 | if (cur) |
| 4635 | cur->bc_private.b.firstblock = |
| 4636 | *firstblock; |
| 4637 | if (abno == NULLFSBLOCK) |
| 4638 | break; |
| 4639 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4640 | cur = xfs_bmbt_init_cursor(mp, tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | ip, whichfork); |
| 4642 | cur->bc_private.b.firstblock = |
| 4643 | *firstblock; |
| 4644 | cur->bc_private.b.flist = flist; |
| 4645 | } |
| 4646 | /* |
| 4647 | * Bump the number of extents we've allocated |
| 4648 | * in this call. |
| 4649 | */ |
| 4650 | nallocs++; |
| 4651 | } |
| 4652 | if (cur) |
| 4653 | cur->bc_private.b.flags = |
| 4654 | wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4655 | got.br_startoff = aoff; |
| 4656 | got.br_startblock = abno; |
| 4657 | got.br_blockcount = alen; |
| 4658 | got.br_state = XFS_EXT_NORM; /* assume normal */ |
| 4659 | /* |
| 4660 | * Determine state of extent, and the filesystem. |
| 4661 | * A wasdelay extent has been initialized, so |
| 4662 | * shouldn't be flagged as unwritten. |
| 4663 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4664 | if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) |
| 4666 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4667 | } |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 4668 | error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &got, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4669 | firstblock, flist, &tmp_logflags, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4670 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | logflags |= tmp_logflags; |
| 4672 | if (error) |
| 4673 | goto error0; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4674 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4676 | xfs_bmbt_get_all(ep, &got); |
| 4677 | ASSERT(got.br_startoff <= aoff); |
| 4678 | ASSERT(got.br_startoff + got.br_blockcount >= |
| 4679 | aoff + alen); |
| 4680 | #ifdef DEBUG |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4681 | if (flags & XFS_BMAPI_DELAY) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4682 | ASSERT(isnullstartblock(got.br_startblock)); |
| 4683 | ASSERT(startblockval(got.br_startblock) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | } |
| 4685 | ASSERT(got.br_state == XFS_EXT_NORM || |
| 4686 | got.br_state == XFS_EXT_UNWRITTEN); |
| 4687 | #endif |
| 4688 | /* |
| 4689 | * Fall down into the found allocated space case. |
| 4690 | */ |
| 4691 | } else if (inhole) { |
| 4692 | /* |
| 4693 | * Reading in a hole. |
| 4694 | */ |
| 4695 | mval->br_startoff = bno; |
| 4696 | mval->br_startblock = HOLESTARTBLOCK; |
| 4697 | mval->br_blockcount = |
| 4698 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4699 | mval->br_state = XFS_EXT_NORM; |
| 4700 | bno += mval->br_blockcount; |
| 4701 | len -= mval->br_blockcount; |
| 4702 | mval++; |
| 4703 | n++; |
| 4704 | continue; |
| 4705 | } |
| 4706 | /* |
| 4707 | * Then deal with the allocated space we found. |
| 4708 | */ |
| 4709 | ASSERT(ep != NULL); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4710 | if (!(flags & XFS_BMAPI_ENTIRE) && |
| 4711 | (got.br_startoff + got.br_blockcount > obno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4712 | if (obno > bno) |
| 4713 | bno = obno; |
| 4714 | ASSERT((bno >= obno) || (n == 0)); |
| 4715 | ASSERT(bno < end); |
| 4716 | mval->br_startoff = bno; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4717 | if (isnullstartblock(got.br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4718 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4720 | } else |
| 4721 | mval->br_startblock = |
| 4722 | got.br_startblock + |
| 4723 | (bno - got.br_startoff); |
| 4724 | /* |
| 4725 | * Return the minimum of what we got and what we |
| 4726 | * asked for for the length. We can use the len |
| 4727 | * variable here because it is modified below |
| 4728 | * and we could have been there before coming |
| 4729 | * here if the first part of the allocation |
| 4730 | * didn't overlap what was asked for. |
| 4731 | */ |
| 4732 | mval->br_blockcount = |
| 4733 | XFS_FILBLKS_MIN(end - bno, got.br_blockcount - |
| 4734 | (bno - got.br_startoff)); |
| 4735 | mval->br_state = got.br_state; |
| 4736 | ASSERT(mval->br_blockcount <= len); |
| 4737 | } else { |
| 4738 | *mval = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4739 | if (isnullstartblock(mval->br_startblock)) { |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4740 | ASSERT(!wr || (flags & XFS_BMAPI_DELAY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4742 | } |
| 4743 | } |
| 4744 | |
| 4745 | /* |
| 4746 | * Check if writing previously allocated but |
| 4747 | * unwritten extents. |
| 4748 | */ |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 4749 | if (wr && |
| 4750 | ((mval->br_state == XFS_EXT_UNWRITTEN && |
| 4751 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) || |
| 4752 | (mval->br_state == XFS_EXT_NORM && |
| 4753 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) == |
| 4754 | (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | /* |
| 4756 | * Modify (by adding) the state flag, if writing. |
| 4757 | */ |
| 4758 | ASSERT(mval->br_blockcount <= len); |
| 4759 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4760 | cur = xfs_bmbt_init_cursor(mp, |
| 4761 | tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | cur->bc_private.b.firstblock = |
| 4763 | *firstblock; |
| 4764 | cur->bc_private.b.flist = flist; |
| 4765 | } |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 4766 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4767 | ? XFS_EXT_NORM |
| 4768 | : XFS_EXT_UNWRITTEN; |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 4769 | error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, mval, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4770 | firstblock, flist, &tmp_logflags, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4771 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | logflags |= tmp_logflags; |
| 4773 | if (error) |
| 4774 | goto error0; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4775 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 4777 | xfs_bmbt_get_all(ep, &got); |
| 4778 | /* |
| 4779 | * We may have combined previously unwritten |
| 4780 | * space with written space, so generate |
| 4781 | * another request. |
| 4782 | */ |
| 4783 | if (mval->br_blockcount < len) |
| 4784 | continue; |
| 4785 | } |
| 4786 | |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4787 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4789 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4790 | (mval->br_blockcount <= len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | (mval->br_startoff < obno)); |
| 4792 | bno = mval->br_startoff + mval->br_blockcount; |
| 4793 | len = end - bno; |
| 4794 | if (n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4795 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4796 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4797 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4798 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4799 | mval[-1].br_state = mval->br_state; |
| 4800 | } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4801 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4802 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4803 | mval->br_startblock == |
| 4804 | mval[-1].br_startblock + mval[-1].br_blockcount && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4805 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4806 | mval[-1].br_state == mval->br_state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | ASSERT(mval->br_startoff == |
| 4808 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4809 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4810 | } else if (n > 0 && |
| 4811 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4812 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4813 | mval->br_startoff == |
| 4814 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4815 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4816 | mval[-1].br_state = mval->br_state; |
| 4817 | } else if (!((n == 0) && |
| 4818 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4819 | obno))) { |
| 4820 | mval++; |
| 4821 | n++; |
| 4822 | } |
| 4823 | /* |
| 4824 | * If we're done, stop now. Stop when we've allocated |
| 4825 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4826 | * the transaction may get too big. |
| 4827 | */ |
| 4828 | if (bno >= end || n >= *nmap || nallocs >= *nmap) |
| 4829 | break; |
| 4830 | /* |
| 4831 | * Else go on to the next record. |
| 4832 | */ |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4833 | prev = got; |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4834 | if (++lastx < nextents) { |
| 4835 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4837 | } else { |
| 4838 | eof = 1; |
| 4839 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | *nmap = n; |
| 4842 | /* |
| 4843 | * Transform from btree to extents, give it cur. |
| 4844 | */ |
| 4845 | if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 4846 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 4847 | ASSERT(wr && cur); |
| 4848 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4849 | &tmp_logflags, whichfork); |
| 4850 | logflags |= tmp_logflags; |
| 4851 | if (error) |
| 4852 | goto error0; |
| 4853 | } |
| 4854 | ASSERT(ifp->if_ext_max == |
| 4855 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4856 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 4857 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 4858 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | error0: |
| 4860 | /* |
| 4861 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4862 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4864 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4866 | logflags &= ~xfs_ilog_fext(whichfork); |
| 4867 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4869 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | /* |
| 4871 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 4872 | * detecting a case where the data is changed, there's an error, |
| 4873 | * and it's not logged so we don't shutdown when we should. |
| 4874 | */ |
| 4875 | if (logflags) { |
| 4876 | ASSERT(tp && wr); |
| 4877 | xfs_trans_log_inode(tp, ip, logflags); |
| 4878 | } |
| 4879 | if (cur) { |
| 4880 | if (!error) { |
| 4881 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4882 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4883 | XFS_FSB_TO_AGNO(mp, |
| 4884 | cur->bc_private.b.firstblock) || |
| 4885 | (flist->xbf_low && |
| 4886 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4887 | XFS_FSB_TO_AGNO(mp, |
| 4888 | cur->bc_private.b.firstblock))); |
| 4889 | *firstblock = cur->bc_private.b.firstblock; |
| 4890 | } |
| 4891 | xfs_btree_del_cursor(cur, |
| 4892 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 4893 | } |
| 4894 | if (!error) |
| 4895 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 4896 | orig_nmap, *nmap); |
| 4897 | return error; |
| 4898 | } |
| 4899 | |
| 4900 | /* |
| 4901 | * Map file blocks to filesystem blocks, simple version. |
| 4902 | * One block (extent) only, read-only. |
| 4903 | * For flags, only the XFS_BMAPI_ATTRFORK flag is examined. |
| 4904 | * For the other flag values, the effect is as if XFS_BMAPI_METADATA |
| 4905 | * was set and all the others were clear. |
| 4906 | */ |
| 4907 | int /* error */ |
| 4908 | xfs_bmapi_single( |
| 4909 | xfs_trans_t *tp, /* transaction pointer */ |
| 4910 | xfs_inode_t *ip, /* incore inode */ |
| 4911 | int whichfork, /* data or attr fork */ |
| 4912 | xfs_fsblock_t *fsb, /* output: mapped block */ |
| 4913 | xfs_fileoff_t bno) /* starting file offs. mapped */ |
| 4914 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4915 | int eof; /* we've hit the end of extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4916 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4917 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4918 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4919 | xfs_extnum_t lastx; /* last useful extent number */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4920 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | |
| 4922 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4923 | if (unlikely( |
| 4924 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4925 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) { |
| 4926 | XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW, |
| 4927 | ip->i_mount); |
| 4928 | return XFS_ERROR(EFSCORRUPTED); |
| 4929 | } |
| 4930 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
| 4931 | return XFS_ERROR(EIO); |
| 4932 | XFS_STATS_INC(xs_blk_mapr); |
| 4933 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4934 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4935 | return error; |
| 4936 | (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4937 | &prev); |
| 4938 | /* |
| 4939 | * Reading past eof, act as though there's a hole |
| 4940 | * up to end. |
| 4941 | */ |
| 4942 | if (eof || got.br_startoff > bno) { |
| 4943 | *fsb = NULLFSBLOCK; |
| 4944 | return 0; |
| 4945 | } |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4946 | ASSERT(!isnullstartblock(got.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | ASSERT(bno < got.br_startoff + got.br_blockcount); |
| 4948 | *fsb = got.br_startblock + (bno - got.br_startoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | return 0; |
| 4950 | } |
| 4951 | |
| 4952 | /* |
| 4953 | * Unmap (remove) blocks from a file. |
| 4954 | * If nexts is nonzero then the number of extents to remove is limited to |
| 4955 | * that value. If not all extents in the block range can be removed then |
| 4956 | * *done is set. |
| 4957 | */ |
| 4958 | int /* error */ |
| 4959 | xfs_bunmapi( |
| 4960 | xfs_trans_t *tp, /* transaction pointer */ |
| 4961 | struct xfs_inode *ip, /* incore inode */ |
| 4962 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 4963 | xfs_filblks_t len, /* length to unmap in file */ |
| 4964 | int flags, /* misc flags */ |
| 4965 | xfs_extnum_t nexts, /* number of extents max */ |
| 4966 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4967 | controls a.g. for allocs */ |
| 4968 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 4969 | int *done) /* set if not done yet */ |
| 4970 | { |
| 4971 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 4972 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 4973 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4974 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | int error; /* error return value */ |
| 4976 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4977 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 4979 | int isrt; /* freeing in rt area */ |
| 4980 | xfs_extnum_t lastx; /* last extent index used */ |
| 4981 | int logflags; /* transaction logging flags */ |
| 4982 | xfs_extlen_t mod; /* rt extent offset */ |
| 4983 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4984 | xfs_extnum_t nextents; /* number of file extents */ |
| 4985 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | xfs_fileoff_t start; /* first file offset deleted */ |
| 4987 | int tmp_logflags; /* partial logging flags */ |
| 4988 | int wasdel; /* was a delayed alloc extent */ |
| 4989 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | xfs_fsblock_t sum; |
| 4991 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4992 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 4993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4995 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4996 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4997 | if (unlikely( |
| 4998 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4999 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5000 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5001 | ip->i_mount); |
| 5002 | return XFS_ERROR(EFSCORRUPTED); |
| 5003 | } |
| 5004 | mp = ip->i_mount; |
| 5005 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5006 | return XFS_ERROR(EIO); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | ASSERT(len > 0); |
| 5009 | ASSERT(nexts >= 0); |
| 5010 | ASSERT(ifp->if_ext_max == |
| 5011 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5012 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5013 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5014 | return error; |
| 5015 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5016 | if (nextents == 0) { |
| 5017 | *done = 1; |
| 5018 | return 0; |
| 5019 | } |
| 5020 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5021 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5022 | start = bno; |
| 5023 | bno = start + len - 1; |
| 5024 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5025 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | /* |
| 5028 | * Check to see if the given block number is past the end of the |
| 5029 | * file, back up to the last block if so... |
| 5030 | */ |
| 5031 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5032 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | xfs_bmbt_get_all(ep, &got); |
| 5034 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5035 | } |
| 5036 | logflags = 0; |
| 5037 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5038 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5039 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | cur->bc_private.b.firstblock = *firstblock; |
| 5041 | cur->bc_private.b.flist = flist; |
| 5042 | cur->bc_private.b.flags = 0; |
| 5043 | } else |
| 5044 | cur = NULL; |
| 5045 | extno = 0; |
| 5046 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5047 | (nexts == 0 || extno < nexts)) { |
| 5048 | /* |
| 5049 | * Is the found extent after a hole in which bno lives? |
| 5050 | * Just back up to the previous extent, if so. |
| 5051 | */ |
| 5052 | if (got.br_startoff > bno) { |
| 5053 | if (--lastx < 0) |
| 5054 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5055 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | xfs_bmbt_get_all(ep, &got); |
| 5057 | } |
| 5058 | /* |
| 5059 | * Is the last block of this extent before the range |
| 5060 | * we're supposed to delete? If so, we're done. |
| 5061 | */ |
| 5062 | bno = XFS_FILEOFF_MIN(bno, |
| 5063 | got.br_startoff + got.br_blockcount - 1); |
| 5064 | if (bno < start) |
| 5065 | break; |
| 5066 | /* |
| 5067 | * Then deal with the (possibly delayed) allocated space |
| 5068 | * we found. |
| 5069 | */ |
| 5070 | ASSERT(ep != NULL); |
| 5071 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5072 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | if (got.br_startoff < start) { |
| 5074 | del.br_startoff = start; |
| 5075 | del.br_blockcount -= start - got.br_startoff; |
| 5076 | if (!wasdel) |
| 5077 | del.br_startblock += start - got.br_startoff; |
| 5078 | } |
| 5079 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5080 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5081 | sum = del.br_startblock + del.br_blockcount; |
| 5082 | if (isrt && |
| 5083 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5084 | /* |
| 5085 | * Realtime extent not lined up at the end. |
| 5086 | * The extent could have been split into written |
| 5087 | * and unwritten pieces, or we could just be |
| 5088 | * unmapping part of it. But we can't really |
| 5089 | * get rid of part of a realtime extent. |
| 5090 | */ |
| 5091 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5092 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | /* |
| 5094 | * This piece is unwritten, or we're not |
| 5095 | * using unwritten extents. Skip over it. |
| 5096 | */ |
| 5097 | ASSERT(bno >= mod); |
| 5098 | bno -= mod > del.br_blockcount ? |
| 5099 | del.br_blockcount : mod; |
| 5100 | if (bno < got.br_startoff) { |
| 5101 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5102 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5103 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | } |
| 5105 | continue; |
| 5106 | } |
| 5107 | /* |
| 5108 | * It's written, turn it unwritten. |
| 5109 | * This is better than zeroing it. |
| 5110 | */ |
| 5111 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5112 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5113 | /* |
| 5114 | * If this spans a realtime extent boundary, |
| 5115 | * chop it back to the start of the one we end at. |
| 5116 | */ |
| 5117 | if (del.br_blockcount > mod) { |
| 5118 | del.br_startoff += del.br_blockcount - mod; |
| 5119 | del.br_startblock += del.br_blockcount - mod; |
| 5120 | del.br_blockcount = mod; |
| 5121 | } |
| 5122 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 5123 | error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5124 | firstblock, flist, &logflags, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5125 | XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | if (error) |
| 5127 | goto error0; |
| 5128 | goto nodelete; |
| 5129 | } |
| 5130 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5131 | /* |
| 5132 | * Realtime extent is lined up at the end but not |
| 5133 | * at the front. We'll get rid of full extents if |
| 5134 | * we can. |
| 5135 | */ |
| 5136 | mod = mp->m_sb.sb_rextsize - mod; |
| 5137 | if (del.br_blockcount > mod) { |
| 5138 | del.br_blockcount -= mod; |
| 5139 | del.br_startoff += mod; |
| 5140 | del.br_startblock += mod; |
| 5141 | } else if ((del.br_startoff == start && |
| 5142 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5143 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5144 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | /* |
| 5146 | * Can't make it unwritten. There isn't |
| 5147 | * a full extent here so just skip it. |
| 5148 | */ |
| 5149 | ASSERT(bno >= del.br_blockcount); |
| 5150 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5151 | if (got.br_startoff > bno) { |
| 5152 | if (--lastx >= 0) { |
| 5153 | ep = xfs_iext_get_ext(ifp, |
| 5154 | lastx); |
| 5155 | xfs_bmbt_get_all(ep, &got); |
| 5156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | } |
| 5158 | continue; |
| 5159 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5160 | /* |
| 5161 | * This one is already unwritten. |
| 5162 | * It must have a written left neighbor. |
| 5163 | * Unwrite the killed part of that one and |
| 5164 | * try again. |
| 5165 | */ |
| 5166 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5167 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5168 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5170 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | ASSERT(del.br_startblock == |
| 5172 | prev.br_startblock + prev.br_blockcount); |
| 5173 | if (prev.br_startoff < start) { |
| 5174 | mod = start - prev.br_startoff; |
| 5175 | prev.br_blockcount -= mod; |
| 5176 | prev.br_startblock += mod; |
| 5177 | prev.br_startoff = start; |
| 5178 | } |
| 5179 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5180 | lastx--; |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 5181 | error = xfs_bmap_add_extent(tp, ip, &lastx, |
| 5182 | &cur, &prev, firstblock, flist, |
| 5183 | &logflags, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | if (error) |
| 5185 | goto error0; |
| 5186 | goto nodelete; |
| 5187 | } else { |
| 5188 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5189 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame^] | 5190 | error = xfs_bmap_add_extent(tp, ip, &lastx, |
| 5191 | &cur, &del, firstblock, flist, |
| 5192 | &logflags, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | if (error) |
| 5194 | goto error0; |
| 5195 | goto nodelete; |
| 5196 | } |
| 5197 | } |
| 5198 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5199 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5200 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5201 | if (isrt) { |
| 5202 | xfs_filblks_t rtexts; |
| 5203 | |
| 5204 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5205 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5206 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5207 | (int64_t)rtexts, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5208 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5209 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5210 | XFS_QMOPT_RES_RTBLKS); |
| 5211 | } else { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 5212 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5213 | (int64_t)del.br_blockcount, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5214 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5215 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5216 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | ip->i_delayed_blks -= del.br_blockcount; |
| 5219 | if (cur) |
| 5220 | cur->bc_private.b.flags |= |
| 5221 | XFS_BTCUR_BPRV_WASDEL; |
| 5222 | } else if (cur) |
| 5223 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5224 | /* |
| 5225 | * If it's the case where the directory code is running |
| 5226 | * with no block reservation, and the deleted block is in |
| 5227 | * the middle of its extent, and the resulting insert |
| 5228 | * of an extent would cause transformation to btree format, |
| 5229 | * then reject it. The calling code will then swap |
| 5230 | * blocks around instead. |
| 5231 | * We have to do this now, rather than waiting for the |
| 5232 | * conversion to btree format, since the transaction |
| 5233 | * will be dirty. |
| 5234 | */ |
| 5235 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5236 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5237 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5238 | del.br_startoff > got.br_startoff && |
| 5239 | del.br_startoff + del.br_blockcount < |
| 5240 | got.br_startoff + got.br_blockcount) { |
| 5241 | error = XFS_ERROR(ENOSPC); |
| 5242 | goto error0; |
| 5243 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5244 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5245 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | logflags |= tmp_logflags; |
| 5247 | if (error) |
| 5248 | goto error0; |
| 5249 | bno = del.br_startoff - 1; |
| 5250 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | /* |
| 5252 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5254 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5255 | if (lastx >= 0) { |
| 5256 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5257 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5258 | if (--lastx >= 0) |
| 5259 | ep = xfs_iext_get_ext(ifp, |
| 5260 | lastx); |
| 5261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5263 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | extno++; |
| 5265 | } |
| 5266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5268 | ASSERT(ifp->if_ext_max == |
| 5269 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5270 | /* |
| 5271 | * Convert to a btree if necessary. |
| 5272 | */ |
| 5273 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5274 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5275 | ASSERT(cur == NULL); |
| 5276 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5277 | &cur, 0, &tmp_logflags, whichfork); |
| 5278 | logflags |= tmp_logflags; |
| 5279 | if (error) |
| 5280 | goto error0; |
| 5281 | } |
| 5282 | /* |
| 5283 | * transform from btree to extents, give it cur |
| 5284 | */ |
| 5285 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5286 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5287 | ASSERT(cur != NULL); |
| 5288 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5289 | whichfork); |
| 5290 | logflags |= tmp_logflags; |
| 5291 | if (error) |
| 5292 | goto error0; |
| 5293 | } |
| 5294 | /* |
| 5295 | * transform from extents to local? |
| 5296 | */ |
| 5297 | ASSERT(ifp->if_ext_max == |
| 5298 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5299 | error = 0; |
| 5300 | error0: |
| 5301 | /* |
| 5302 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5303 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5305 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5306 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5307 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5308 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5310 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | /* |
| 5312 | * Log inode even in the error case, if the transaction |
| 5313 | * is dirty we'll need to shut down the filesystem. |
| 5314 | */ |
| 5315 | if (logflags) |
| 5316 | xfs_trans_log_inode(tp, ip, logflags); |
| 5317 | if (cur) { |
| 5318 | if (!error) { |
| 5319 | *firstblock = cur->bc_private.b.firstblock; |
| 5320 | cur->bc_private.b.allocated = 0; |
| 5321 | } |
| 5322 | xfs_btree_del_cursor(cur, |
| 5323 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5324 | } |
| 5325 | return error; |
| 5326 | } |
| 5327 | |
| 5328 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5329 | * returns 1 for success, 0 if we failed to map the extent. |
| 5330 | */ |
| 5331 | STATIC int |
| 5332 | xfs_getbmapx_fix_eof_hole( |
| 5333 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5334 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5335 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5336 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5337 | __int64_t end, /* last block requested */ |
| 5338 | xfs_fsblock_t startblock) |
| 5339 | { |
| 5340 | __int64_t fixlen; |
| 5341 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5342 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5343 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5344 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5345 | |
| 5346 | if (startblock == HOLESTARTBLOCK) { |
| 5347 | mp = ip->i_mount; |
| 5348 | out->bmv_block = -1; |
| 5349 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5350 | fixlen -= out->bmv_offset; |
| 5351 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5352 | /* Came to hole at EOF. Trim it. */ |
| 5353 | if (fixlen <= 0) |
| 5354 | return 0; |
| 5355 | out->bmv_length = fixlen; |
| 5356 | } |
| 5357 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5358 | if (startblock == DELAYSTARTBLOCK) |
| 5359 | out->bmv_block = -2; |
| 5360 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5361 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5362 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5363 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5364 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5365 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5366 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | return 1; |
| 5370 | } |
| 5371 | |
| 5372 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5373 | * Get inode's extents as described in bmv, and format for output. |
| 5374 | * Calls formatter to fill the user's buffer until all extents |
| 5375 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5376 | * been filled, or until the formatter short-circuits the loop, |
| 5377 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | */ |
| 5379 | int /* error code */ |
| 5380 | xfs_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5381 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5382 | struct getbmapx *bmv, /* user bmap structure */ |
| 5383 | xfs_bmap_format_t formatter, /* format to user */ |
| 5384 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5385 | { |
| 5386 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5387 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | __int64_t fixlen; /* length for -1 case */ |
| 5389 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | int lock; /* lock state */ |
| 5391 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5392 | xfs_mount_t *mp; /* file system mount point */ |
| 5393 | int nex; /* # of user extents can do */ |
| 5394 | int nexleft; /* # of user extents left */ |
| 5395 | int subnex; /* # of bmapi's can do */ |
| 5396 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5397 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | int whichfork; /* data or attr fork */ |
| 5399 | int prealloced; /* this is a file with |
| 5400 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5401 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5403 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5406 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5407 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | if (whichfork == XFS_ATTR_FORK) { |
| 5410 | if (XFS_IFORK_Q(ip)) { |
| 5411 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5412 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5413 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5414 | return XFS_ERROR(EINVAL); |
| 5415 | } else if (unlikely( |
| 5416 | ip->i_d.di_aformat != 0 && |
| 5417 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5418 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5419 | ip->i_mount); |
| 5420 | return XFS_ERROR(EFSCORRUPTED); |
| 5421 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5422 | |
| 5423 | prealloced = 0; |
| 5424 | fixlen = 1LL << 32; |
| 5425 | } else { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5426 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5427 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5428 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5429 | return XFS_ERROR(EINVAL); |
| 5430 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5431 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5432 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | prealloced = 1; |
| 5434 | fixlen = XFS_MAXIOFFSET(mp); |
| 5435 | } else { |
| 5436 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5437 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | if (bmv->bmv_length == -1) { |
| 5442 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5443 | bmv->bmv_length = |
| 5444 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5445 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | bmv->bmv_entries = 0; |
| 5447 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5448 | } else if (bmv->bmv_length < 0) { |
| 5449 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5450 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | nex = bmv->bmv_count - 1; |
| 5453 | if (nex <= 0) |
| 5454 | return XFS_ERROR(EINVAL); |
| 5455 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5456 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5457 | |
| 5458 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5459 | return XFS_ERROR(ENOMEM); |
| 5460 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 5461 | if (!out) |
| 5462 | return XFS_ERROR(ENOMEM); |
| 5463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5465 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 5466 | if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { |
| 5467 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5468 | if (error) |
| 5469 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5470 | } |
Dave Chinner | 309c848 | 2010-11-30 15:16:02 +1100 | [diff] [blame] | 5471 | /* |
| 5472 | * even after flushing the inode, there can still be delalloc |
| 5473 | * blocks on the inode beyond EOF due to speculative |
| 5474 | * preallocation. These are not removed until the release |
| 5475 | * function is called or the inode is inactivated. Hence we |
| 5476 | * cannot assert here that ip->i_delayed_blks == 0. |
| 5477 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | |
| 5480 | lock = xfs_ilock_map_shared(ip); |
| 5481 | |
| 5482 | /* |
| 5483 | * Don't let nex be bigger than the number of extents |
| 5484 | * we can have assuming alternating holes and real extents. |
| 5485 | */ |
| 5486 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5487 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5488 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5489 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5490 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5491 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | |
| 5493 | /* |
| 5494 | * Allocate enough space to handle "subnex" maps at a time. |
| 5495 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5496 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5498 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5499 | if (!map) |
| 5500 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | |
| 5502 | bmv->bmv_entries = 0; |
| 5503 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5504 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5505 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5506 | error = 0; |
| 5507 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | nexleft = nex; |
| 5511 | |
| 5512 | do { |
| 5513 | nmap = (nexleft > subnex) ? subnex : nexleft; |
| 5514 | error = xfs_bmapi(NULL, ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5515 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 5516 | bmapi_flags, NULL, 0, map, &nmap, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5517 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5519 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | ASSERT(nmap <= subnex); |
| 5521 | |
| 5522 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5523 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5524 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5525 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5526 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5527 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5528 | out[cur_ext].bmv_offset = |
| 5529 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5530 | out[cur_ext].bmv_length = |
| 5531 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5532 | out[cur_ext].bmv_unused1 = 0; |
| 5533 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5534 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5535 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5536 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5537 | whichfork == XFS_ATTR_FORK) { |
| 5538 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5539 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5540 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5542 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5543 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5544 | prealloced, bmvend, |
| 5545 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5546 | goto out_free_map; |
| 5547 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5548 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5549 | out[cur_ext].bmv_offset + |
| 5550 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5551 | bmv->bmv_length = |
| 5552 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 5553 | |
| 5554 | /* |
| 5555 | * In case we don't want to return the hole, |
| 5556 | * don't increase cur_ext so that we can reuse |
| 5557 | * it in the next loop. |
| 5558 | */ |
| 5559 | if ((iflags & BMV_IF_NO_HOLES) && |
| 5560 | map[i].br_startblock == HOLESTARTBLOCK) { |
| 5561 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); |
| 5562 | continue; |
| 5563 | } |
| 5564 | |
| 5565 | nexleft--; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5566 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5567 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | } |
| 5569 | } while (nmap && nexleft && bmv->bmv_length); |
| 5570 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5571 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5572 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5573 | out_unlock_ilock: |
| 5574 | xfs_iunlock_map_shared(ip, lock); |
| 5575 | out_unlock_iolock: |
| 5576 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5577 | |
| 5578 | for (i = 0; i < cur_ext; i++) { |
| 5579 | int full = 0; /* user array is full */ |
| 5580 | |
| 5581 | /* format results & advance arg */ |
| 5582 | error = formatter(&arg, &out[i], &full); |
| 5583 | if (error || full) |
| 5584 | break; |
| 5585 | } |
| 5586 | |
Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5587 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | return error; |
| 5589 | } |
| 5590 | |
| 5591 | /* |
| 5592 | * Check the last inode extent to determine whether this allocation will result |
| 5593 | * in blocks being allocated at the end of the file. When we allocate new data |
| 5594 | * blocks at the end of the file which do not start at the previous data block, |
| 5595 | * we will try to align the new blocks at stripe unit boundaries. |
| 5596 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 5597 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | xfs_bmap_isaeof( |
| 5599 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5600 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 5601 | int whichfork, /* data or attribute fork */ |
| 5602 | char *aeof) /* return value */ |
| 5603 | { |
| 5604 | int error; /* error return value */ |
| 5605 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5606 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5607 | xfs_extnum_t nextents; /* number of file extents */ |
| 5608 | xfs_bmbt_irec_t s; /* expanded extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | |
| 5610 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5611 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5612 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5613 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5614 | return error; |
| 5615 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5616 | if (nextents == 0) { |
| 5617 | *aeof = 1; |
| 5618 | return 0; |
| 5619 | } |
| 5620 | /* |
| 5621 | * Go to the last extent |
| 5622 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5623 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5624 | xfs_bmbt_get_all(lastrec, &s); |
| 5625 | /* |
| 5626 | * Check we are allocating in the last extent (for delayed allocations) |
| 5627 | * or past the last extent for non-delayed allocations. |
| 5628 | */ |
| 5629 | *aeof = (off >= s.br_startoff && |
| 5630 | off < s.br_startoff + s.br_blockcount && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5631 | isnullstartblock(s.br_startblock)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5632 | off >= s.br_startoff + s.br_blockcount; |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | /* |
| 5637 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 5638 | * the allocation to a stripe unit boundary. |
| 5639 | */ |
| 5640 | int /* error */ |
| 5641 | xfs_bmap_eof( |
| 5642 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5643 | xfs_fileoff_t endoff, /* file offset in fsblocks */ |
| 5644 | int whichfork, /* data or attribute fork */ |
| 5645 | int *eof) /* result value */ |
| 5646 | { |
| 5647 | xfs_fsblock_t blockcount; /* extent block count */ |
| 5648 | int error; /* error return value */ |
| 5649 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5650 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5651 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | xfs_fileoff_t startoff; /* extent starting file offset */ |
| 5653 | |
| 5654 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5655 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5656 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5657 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5658 | return error; |
| 5659 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5660 | if (nextents == 0) { |
| 5661 | *eof = 1; |
| 5662 | return 0; |
| 5663 | } |
| 5664 | /* |
| 5665 | * Go to the last extent |
| 5666 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5667 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | startoff = xfs_bmbt_get_startoff(lastrec); |
| 5669 | blockcount = xfs_bmbt_get_blockcount(lastrec); |
| 5670 | *eof = endoff >= startoff + blockcount; |
| 5671 | return 0; |
| 5672 | } |
| 5673 | |
| 5674 | #ifdef DEBUG |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5675 | STATIC struct xfs_buf * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | xfs_bmap_get_bp( |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5677 | struct xfs_btree_cur *cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | xfs_fsblock_t bno) |
| 5679 | { |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5680 | struct xfs_log_item_desc *lidp; |
| 5681 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | |
| 5683 | if (!cur) |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5684 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5686 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5687 | if (!cur->bc_bufs[i]) |
| 5688 | break; |
| 5689 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 5690 | return cur->bc_bufs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5693 | /* Chase down all the log items to see if the bp is there */ |
| 5694 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5695 | struct xfs_buf_log_item *bip; |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5696 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 5697 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 5698 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 5699 | return bip->bli_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | } |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5701 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5702 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5703 | } |
| 5704 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5705 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5707 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | xfs_mount_t *mp, |
| 5709 | int root, |
| 5710 | short sz) |
| 5711 | { |
| 5712 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5713 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | xfs_bmbt_key_t *prevp, *keyp; |
| 5715 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5716 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | |
| 5718 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5719 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5721 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | |
| 5723 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5724 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5725 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | } |
| 5727 | prevp = keyp; |
| 5728 | |
| 5729 | /* |
| 5730 | * Compare the block numbers to see if there are dups. |
| 5731 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5732 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5733 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5734 | else |
| 5735 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5736 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5737 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5738 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5739 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5740 | else |
| 5741 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5742 | if (*thispa == *pp) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5743 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5744 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5745 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5747 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | } |
| 5749 | } |
| 5750 | } |
| 5751 | } |
| 5752 | |
| 5753 | /* |
| 5754 | * Check that the extents for the inode ip are in the right order in all |
| 5755 | * btree leaves. |
| 5756 | */ |
| 5757 | |
| 5758 | STATIC void |
| 5759 | xfs_bmap_check_leaf_extents( |
| 5760 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5761 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5762 | int whichfork) /* data or attr fork */ |
| 5763 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5764 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5766 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5767 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5768 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | xfs_ifork_t *ifp; /* fork structure */ |
| 5770 | int level; /* btree level, for checking */ |
| 5771 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5772 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5773 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5774 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5775 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | int bp_release = 0; |
| 5777 | |
| 5778 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5779 | return; |
| 5780 | } |
| 5781 | |
| 5782 | bno = NULLFSBLOCK; |
| 5783 | mp = ip->i_mount; |
| 5784 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5785 | block = ifp->if_broot; |
| 5786 | /* |
| 5787 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5788 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5789 | level = be16_to_cpu(block->bb_level); |
| 5790 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5792 | 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] | 5793 | bno = be64_to_cpu(*pp); |
| 5794 | |
| 5795 | ASSERT(bno != NULLDFSBNO); |
| 5796 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5797 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | /* |
| 5800 | * Go down the tree until leaf level is reached, following the first |
| 5801 | * pointer (leftmost) at each level. |
| 5802 | */ |
| 5803 | while (level-- > 0) { |
| 5804 | /* See if buf is in cur first */ |
| 5805 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5806 | if (bp) { |
| 5807 | bp_release = 0; |
| 5808 | } else { |
| 5809 | bp_release = 1; |
| 5810 | } |
| 5811 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5812 | XFS_BMAP_BTREE_REF))) |
| 5813 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5814 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5815 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5816 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | error0); |
| 5818 | if (level == 0) |
| 5819 | break; |
| 5820 | |
| 5821 | /* |
| 5822 | * Check this block for basic sanity (increasing keys and |
| 5823 | * no duplicate blocks). |
| 5824 | */ |
| 5825 | |
| 5826 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5827 | 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] | 5828 | bno = be64_to_cpu(*pp); |
| 5829 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | if (bp_release) { |
| 5831 | bp_release = 0; |
| 5832 | xfs_trans_brelse(NULL, bp); |
| 5833 | } |
| 5834 | } |
| 5835 | |
| 5836 | /* |
| 5837 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 5838 | */ |
| 5839 | i = 0; |
| 5840 | |
| 5841 | /* |
| 5842 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 5843 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | xfs_fsblock_t nextbno; |
| 5846 | xfs_extnum_t num_recs; |
| 5847 | |
| 5848 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5849 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | |
| 5851 | /* |
| 5852 | * Read-ahead the next leaf block, if any. |
| 5853 | */ |
| 5854 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5855 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | |
| 5857 | /* |
| 5858 | * Check all the extents to make sure they are OK. |
| 5859 | * If we had a previous block, the last entry should |
| 5860 | * conform with the first entry in this one. |
| 5861 | */ |
| 5862 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5863 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5864 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5865 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 5866 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 5867 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5868 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5869 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5870 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5871 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 5872 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 5873 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5874 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5877 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5878 | i += num_recs; |
| 5879 | if (bp_release) { |
| 5880 | bp_release = 0; |
| 5881 | xfs_trans_brelse(NULL, bp); |
| 5882 | } |
| 5883 | bno = nextbno; |
| 5884 | /* |
| 5885 | * If we've reached the end, stop. |
| 5886 | */ |
| 5887 | if (bno == NULLFSBLOCK) |
| 5888 | break; |
| 5889 | |
| 5890 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5891 | if (bp) { |
| 5892 | bp_release = 0; |
| 5893 | } else { |
| 5894 | bp_release = 1; |
| 5895 | } |
| 5896 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5897 | XFS_BMAP_BTREE_REF))) |
| 5898 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5899 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | } |
| 5901 | if (bp_release) { |
| 5902 | bp_release = 0; |
| 5903 | xfs_trans_brelse(NULL, bp); |
| 5904 | } |
| 5905 | return; |
| 5906 | |
| 5907 | error0: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5908 | xfs_warn(mp, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | if (bp_release) |
| 5910 | xfs_trans_brelse(NULL, bp); |
| 5911 | error_norelse: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5912 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5913 | __func__, i); |
| 5914 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5915 | return; |
| 5916 | } |
| 5917 | #endif |
| 5918 | |
| 5919 | /* |
| 5920 | * Count fsblocks of the given fork. |
| 5921 | */ |
| 5922 | int /* error */ |
| 5923 | xfs_bmap_count_blocks( |
| 5924 | xfs_trans_t *tp, /* transaction pointer */ |
| 5925 | xfs_inode_t *ip, /* incore inode */ |
| 5926 | int whichfork, /* data or attr fork */ |
| 5927 | int *count) /* out: count of blocks */ |
| 5928 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5929 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5931 | xfs_ifork_t *ifp; /* fork structure */ |
| 5932 | int level; /* btree level, for checking */ |
| 5933 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5934 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | |
| 5936 | bno = NULLFSBLOCK; |
| 5937 | mp = ip->i_mount; |
| 5938 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5939 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5940 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5942 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | return 0; |
| 5944 | } |
| 5945 | |
| 5946 | /* |
| 5947 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5948 | */ |
| 5949 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5950 | level = be16_to_cpu(block->bb_level); |
| 5951 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5952 | 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] | 5953 | bno = be64_to_cpu(*pp); |
| 5954 | ASSERT(bno != NULLDFSBNO); |
| 5955 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5956 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5958 | 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] | 5959 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 5960 | mp); |
| 5961 | return XFS_ERROR(EFSCORRUPTED); |
| 5962 | } |
| 5963 | |
| 5964 | return 0; |
| 5965 | } |
| 5966 | |
| 5967 | /* |
| 5968 | * Recursively walks each level of a btree |
| 5969 | * to count total fsblocks is use. |
| 5970 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 5971 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | xfs_bmap_count_tree( |
| 5973 | xfs_mount_t *mp, /* file system mount point */ |
| 5974 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5975 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | xfs_fsblock_t blockno, /* file system block number */ |
| 5977 | int levelin, /* level in btree */ |
| 5978 | int *count) /* Count of blocks */ |
| 5979 | { |
| 5980 | int error; |
| 5981 | xfs_buf_t *bp, *nbp; |
| 5982 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5983 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | xfs_fsblock_t bno = blockno; |
| 5985 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5986 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5987 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | |
| 5989 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 5990 | return error; |
| 5991 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5992 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | |
| 5994 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 5995 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5996 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | while (nextbno != NULLFSBLOCK) { |
| 5998 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 5999 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6000 | return error; |
| 6001 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6002 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6003 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | xfs_trans_brelse(tp, nbp); |
| 6005 | } |
| 6006 | |
| 6007 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6008 | 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] | 6009 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6011 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | xfs_trans_brelse(tp, bp); |
| 6013 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6014 | XFS_ERRLEVEL_LOW, mp); |
| 6015 | return XFS_ERROR(EFSCORRUPTED); |
| 6016 | } |
| 6017 | xfs_trans_brelse(tp, bp); |
| 6018 | } else { |
| 6019 | /* count all level 1 nodes and their leaves */ |
| 6020 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6021 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6022 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6023 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | xfs_trans_brelse(tp, bp); |
| 6025 | if (nextbno == NULLFSBLOCK) |
| 6026 | break; |
| 6027 | bno = nextbno; |
| 6028 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6029 | XFS_BMAP_BTREE_REF))) |
| 6030 | return error; |
| 6031 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6032 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | } |
| 6034 | } |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
| 6038 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6039 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6041 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6042 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6043 | xfs_ifork_t *ifp, |
| 6044 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | int numrecs, |
| 6046 | int *count) |
| 6047 | { |
| 6048 | int b; |
| 6049 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6050 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6051 | 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] | 6052 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6053 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6057 | * Count leaf blocks given a range of extent records originally |
| 6058 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6059 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6060 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6061 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6062 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6063 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6064 | int numrecs, |
| 6065 | int *count) |
| 6066 | { |
| 6067 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6068 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6069 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6070 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6071 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | } |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6075 | |
| 6076 | /* |
| 6077 | * dead simple method of punching delalyed allocation blocks from a range in |
| 6078 | * the inode. Walks a block at a time so will be slow, but is only executed in |
| 6079 | * rare error cases so the overhead is not critical. This will alays punch out |
| 6080 | * both the start and end blocks, even if the ranges only partially overlap |
| 6081 | * them, so it is up to the caller to ensure that partial blocks are not |
| 6082 | * passed in. |
| 6083 | */ |
| 6084 | int |
| 6085 | xfs_bmap_punch_delalloc_range( |
| 6086 | struct xfs_inode *ip, |
| 6087 | xfs_fileoff_t start_fsb, |
| 6088 | xfs_fileoff_t length) |
| 6089 | { |
| 6090 | xfs_fileoff_t remaining = length; |
| 6091 | int error = 0; |
| 6092 | |
| 6093 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 6094 | |
| 6095 | do { |
| 6096 | int done; |
| 6097 | xfs_bmbt_irec_t imap; |
| 6098 | int nimaps = 1; |
| 6099 | xfs_fsblock_t firstblock; |
| 6100 | xfs_bmap_free_t flist; |
| 6101 | |
| 6102 | /* |
| 6103 | * Map the range first and check that it is a delalloc extent |
| 6104 | * before trying to unmap the range. Otherwise we will be |
| 6105 | * trying to remove a real extent (which requires a |
| 6106 | * transaction) or a hole, which is probably a bad idea... |
| 6107 | */ |
| 6108 | error = xfs_bmapi(NULL, ip, start_fsb, 1, |
| 6109 | XFS_BMAPI_ENTIRE, NULL, 0, &imap, |
| 6110 | &nimaps, NULL); |
| 6111 | |
| 6112 | if (error) { |
| 6113 | /* something screwed, just bail */ |
| 6114 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 6115 | xfs_alert(ip->i_mount, |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6116 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
| 6117 | ip->i_ino, start_fsb); |
| 6118 | } |
| 6119 | break; |
| 6120 | } |
| 6121 | if (!nimaps) { |
| 6122 | /* nothing there */ |
| 6123 | goto next_block; |
| 6124 | } |
| 6125 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 6126 | /* been converted, ignore */ |
| 6127 | goto next_block; |
| 6128 | } |
| 6129 | WARN_ON(imap.br_blockcount == 0); |
| 6130 | |
| 6131 | /* |
| 6132 | * Note: while we initialise the firstblock/flist pair, they |
| 6133 | * should never be used because blocks should never be |
| 6134 | * allocated or freed for a delalloc extent and hence we need |
| 6135 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 6136 | */ |
| 6137 | xfs_bmap_init(&flist, &firstblock); |
| 6138 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, |
| 6139 | &flist, &done); |
| 6140 | if (error) |
| 6141 | break; |
| 6142 | |
| 6143 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 6144 | next_block: |
| 6145 | start_fsb++; |
| 6146 | remaining--; |
| 6147 | } while(remaining > 0); |
| 6148 | |
| 6149 | return error; |
| 6150 | } |