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_dinode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_inode.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 34 | #include "xfs_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_itable.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 37 | #include "xfs_inode_item.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_extfree_item.h" |
| 39 | #include "xfs_alloc.h" |
| 40 | #include "xfs_bmap.h" |
| 41 | #include "xfs_rtalloc.h" |
| 42 | #include "xfs_error.h" |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 43 | #include "xfs_attr_leaf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "xfs_rw.h" |
| 45 | #include "xfs_quota.h" |
| 46 | #include "xfs_trans_space.h" |
| 47 | #include "xfs_buf_item.h" |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 48 | #include "xfs_filestream.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 49 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 50 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | #ifdef DEBUG |
| 54 | STATIC void |
| 55 | xfs_bmap_check_leaf_extents(xfs_btree_cur_t *cur, xfs_inode_t *ip, int whichfork); |
| 56 | #endif |
| 57 | |
| 58 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 59 | |
| 60 | /* |
| 61 | * Prototypes for internal bmap routines. |
| 62 | */ |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 67 | */ |
| 68 | STATIC int /* error */ |
| 69 | xfs_bmap_add_attrfork_extents( |
| 70 | xfs_trans_t *tp, /* transaction pointer */ |
| 71 | xfs_inode_t *ip, /* incore inode pointer */ |
| 72 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 73 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 74 | int *flags); /* inode logging flags */ |
| 75 | |
| 76 | /* |
| 77 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 78 | */ |
| 79 | STATIC int /* error */ |
| 80 | xfs_bmap_add_attrfork_local( |
| 81 | xfs_trans_t *tp, /* transaction pointer */ |
| 82 | xfs_inode_t *ip, /* incore inode pointer */ |
| 83 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 84 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 85 | int *flags); /* inode logging flags */ |
| 86 | |
| 87 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 89 | * allocation to a real allocation. |
| 90 | */ |
| 91 | STATIC int /* error */ |
| 92 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 93 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 95 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 97 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 99 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 100 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 101 | int *logflagsp); /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * to a real allocation. |
| 106 | */ |
| 107 | STATIC int /* error */ |
| 108 | xfs_bmap_add_extent_hole_real( |
| 109 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 110 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 112 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | int *logflagsp, /* inode logging flags */ |
| 114 | int whichfork); /* data or attr fork */ |
| 115 | |
| 116 | /* |
| 117 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 118 | * allocation to a real allocation or vice versa. |
| 119 | */ |
| 120 | STATIC int /* error */ |
| 121 | xfs_bmap_add_extent_unwritten_real( |
| 122 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 123 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 125 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 126 | int *logflagsp); /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 130 | * It figures out where to ask the underlying allocator to put the new extent. |
| 131 | */ |
| 132 | STATIC int /* error */ |
| 133 | xfs_bmap_alloc( |
| 134 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 135 | |
| 136 | /* |
| 137 | * Transform a btree format file with only one leaf node, where the |
| 138 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 139 | * 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] | 140 | * give up the space for the btree root and pitch the leaf block. |
| 141 | */ |
| 142 | STATIC int /* error */ |
| 143 | xfs_bmap_btree_to_extents( |
| 144 | xfs_trans_t *tp, /* transaction pointer */ |
| 145 | xfs_inode_t *ip, /* incore inode pointer */ |
| 146 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 147 | int *logflagsp, /* inode logging flags */ |
| 148 | int whichfork); /* data or attr fork */ |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * Remove the entry "free" from the free item list. Prev points to the |
| 152 | * previous entry, unless "free" is the head of the list. |
| 153 | */ |
| 154 | STATIC void |
| 155 | xfs_bmap_del_free( |
| 156 | xfs_bmap_free_t *flist, /* free item list header */ |
| 157 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 158 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 159 | |
| 160 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | * Convert an extents-format file into a btree-format file. |
| 162 | * The new file will have a root block (in the inode) and a single child block. |
| 163 | */ |
| 164 | STATIC int /* error */ |
| 165 | xfs_bmap_extents_to_btree( |
| 166 | xfs_trans_t *tp, /* transaction pointer */ |
| 167 | xfs_inode_t *ip, /* incore inode pointer */ |
| 168 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 169 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 170 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 171 | int wasdel, /* converting a delayed alloc */ |
| 172 | int *logflagsp, /* inode logging flags */ |
| 173 | int whichfork); /* data or attr fork */ |
| 174 | |
| 175 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * Convert a local file to an extents file. |
| 177 | * This code is sort of bogus, since the file data needs to get |
| 178 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 179 | */ |
| 180 | STATIC int /* error */ |
| 181 | xfs_bmap_local_to_extents( |
| 182 | xfs_trans_t *tp, /* transaction pointer */ |
| 183 | xfs_inode_t *ip, /* incore inode pointer */ |
| 184 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 185 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 186 | int *logflagsp, /* inode logging flags */ |
| 187 | int whichfork); /* data or attr fork */ |
| 188 | |
| 189 | /* |
| 190 | * Search the extents list for the inode, for the extent containing bno. |
| 191 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 192 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 193 | * Else, *lastxp will be set to the index of the found |
| 194 | * entry; *gotp will contain the entry. |
| 195 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 196 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | xfs_bmap_search_extents( |
| 198 | xfs_inode_t *ip, /* incore inode pointer */ |
| 199 | xfs_fileoff_t bno, /* block number searched for */ |
| 200 | int whichfork, /* data or attr fork */ |
| 201 | int *eofp, /* out: end of file found */ |
| 202 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 203 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 204 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 205 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 206 | /* |
| 207 | * Check the last inode extent to determine whether this allocation will result |
| 208 | * in blocks being allocated at the end of the file. When we allocate new data |
| 209 | * blocks at the end of the file which do not start at the previous data block, |
| 210 | * we will try to align the new blocks at stripe unit boundaries. |
| 211 | */ |
| 212 | STATIC int /* error */ |
| 213 | xfs_bmap_isaeof( |
| 214 | xfs_inode_t *ip, /* incore inode pointer */ |
| 215 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 216 | int whichfork, /* data or attribute fork */ |
| 217 | char *aeof); /* return value */ |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* |
| 220 | * Compute the worst-case number of indirect blocks that will be used |
| 221 | * for ip's delayed extent of length "len". |
| 222 | */ |
| 223 | STATIC xfs_filblks_t |
| 224 | xfs_bmap_worst_indlen( |
| 225 | xfs_inode_t *ip, /* incore inode pointer */ |
| 226 | xfs_filblks_t len); /* delayed extent length */ |
| 227 | |
| 228 | #ifdef DEBUG |
| 229 | /* |
| 230 | * Perform various validation checks on the values being returned |
| 231 | * from xfs_bmapi(). |
| 232 | */ |
| 233 | STATIC void |
| 234 | xfs_bmap_validate_ret( |
| 235 | xfs_fileoff_t bno, |
| 236 | xfs_filblks_t len, |
| 237 | int flags, |
| 238 | xfs_bmbt_irec_t *mval, |
| 239 | int nmap, |
| 240 | int ret_nmap); |
| 241 | #else |
| 242 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 243 | #endif /* DEBUG */ |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | STATIC int |
| 246 | xfs_bmap_count_tree( |
| 247 | xfs_mount_t *mp, |
| 248 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 249 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | xfs_fsblock_t blockno, |
| 251 | int levelin, |
| 252 | int *count); |
| 253 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 254 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 256 | xfs_ifork_t *ifp, |
| 257 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | int numrecs, |
| 259 | int *count); |
| 260 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 261 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 262 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 263 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 264 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 265 | int numrecs, |
| 266 | int *count); |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | /* |
| 269 | * Bmap internal routines. |
| 270 | */ |
| 271 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 272 | STATIC int /* error */ |
| 273 | xfs_bmbt_lookup_eq( |
| 274 | struct xfs_btree_cur *cur, |
| 275 | xfs_fileoff_t off, |
| 276 | xfs_fsblock_t bno, |
| 277 | xfs_filblks_t len, |
| 278 | int *stat) /* success/failure */ |
| 279 | { |
| 280 | cur->bc_rec.b.br_startoff = off; |
| 281 | cur->bc_rec.b.br_startblock = bno; |
| 282 | cur->bc_rec.b.br_blockcount = len; |
| 283 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 284 | } |
| 285 | |
| 286 | STATIC int /* error */ |
| 287 | xfs_bmbt_lookup_ge( |
| 288 | struct xfs_btree_cur *cur, |
| 289 | xfs_fileoff_t off, |
| 290 | xfs_fsblock_t bno, |
| 291 | xfs_filblks_t len, |
| 292 | int *stat) /* success/failure */ |
| 293 | { |
| 294 | cur->bc_rec.b.br_startoff = off; |
| 295 | cur->bc_rec.b.br_startblock = bno; |
| 296 | cur->bc_rec.b.br_blockcount = len; |
| 297 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 298 | } |
| 299 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 300 | /* |
| 301 | * Update the record referred to by cur to the value given |
| 302 | * by [off, bno, len, state]. |
| 303 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 304 | */ |
| 305 | STATIC int |
| 306 | xfs_bmbt_update( |
| 307 | struct xfs_btree_cur *cur, |
| 308 | xfs_fileoff_t off, |
| 309 | xfs_fsblock_t bno, |
| 310 | xfs_filblks_t len, |
| 311 | xfs_exntst_t state) |
| 312 | { |
| 313 | union xfs_btree_rec rec; |
| 314 | |
| 315 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 316 | return xfs_btree_update(cur, &rec); |
| 317 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /* |
| 320 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 321 | */ |
| 322 | STATIC int /* error */ |
| 323 | xfs_bmap_add_attrfork_btree( |
| 324 | xfs_trans_t *tp, /* transaction pointer */ |
| 325 | xfs_inode_t *ip, /* incore inode pointer */ |
| 326 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 327 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 328 | int *flags) /* inode logging flags */ |
| 329 | { |
| 330 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 331 | int error; /* error return value */ |
| 332 | xfs_mount_t *mp; /* file system mount struct */ |
| 333 | int stat; /* newroot status */ |
| 334 | |
| 335 | mp = ip->i_mount; |
| 336 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 337 | *flags |= XFS_ILOG_DBROOT; |
| 338 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 339 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | cur->bc_private.b.flist = flist; |
| 341 | cur->bc_private.b.firstblock = *firstblock; |
| 342 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 343 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 344 | /* must be at least one entry */ |
| 345 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 346 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | goto error0; |
| 348 | if (stat == 0) { |
| 349 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 350 | return XFS_ERROR(ENOSPC); |
| 351 | } |
| 352 | *firstblock = cur->bc_private.b.firstblock; |
| 353 | cur->bc_private.b.allocated = 0; |
| 354 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 355 | } |
| 356 | return 0; |
| 357 | error0: |
| 358 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 359 | return error; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 364 | */ |
| 365 | STATIC int /* error */ |
| 366 | xfs_bmap_add_attrfork_extents( |
| 367 | xfs_trans_t *tp, /* transaction pointer */ |
| 368 | xfs_inode_t *ip, /* incore inode pointer */ |
| 369 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 370 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 371 | int *flags) /* inode logging flags */ |
| 372 | { |
| 373 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 374 | int error; /* error return value */ |
| 375 | |
| 376 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 377 | return 0; |
| 378 | cur = NULL; |
| 379 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 380 | flags, XFS_DATA_FORK); |
| 381 | if (cur) { |
| 382 | cur->bc_private.b.allocated = 0; |
| 383 | xfs_btree_del_cursor(cur, |
| 384 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 385 | } |
| 386 | return error; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 391 | */ |
| 392 | STATIC int /* error */ |
| 393 | xfs_bmap_add_attrfork_local( |
| 394 | xfs_trans_t *tp, /* transaction pointer */ |
| 395 | xfs_inode_t *ip, /* incore inode pointer */ |
| 396 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 397 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 398 | int *flags) /* inode logging flags */ |
| 399 | { |
| 400 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 401 | int error; /* error return value */ |
| 402 | xfs_mount_t *mp; /* mount structure pointer */ |
| 403 | |
| 404 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 405 | return 0; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 406 | if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | mp = ip->i_mount; |
| 408 | memset(&dargs, 0, sizeof(dargs)); |
| 409 | dargs.dp = ip; |
| 410 | dargs.firstblock = firstblock; |
| 411 | dargs.flist = flist; |
| 412 | dargs.total = mp->m_dirblkfsbs; |
| 413 | dargs.whichfork = XFS_DATA_FORK; |
| 414 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 415 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } else |
| 417 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 418 | XFS_DATA_FORK); |
| 419 | return error; |
| 420 | } |
| 421 | |
| 422 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 423 | * Update file extent records and the btree after allocating space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | */ |
| 425 | STATIC int /* error */ |
| 426 | xfs_bmap_add_extent( |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 427 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 429 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 431 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 433 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 434 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 435 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | { |
| 437 | xfs_btree_cur_t *cur; /* btree cursor or null */ |
| 438 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 439 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 440 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | xfs_ifork_t *ifp; /* inode fork ptr */ |
| 442 | int logflags; /* returned value */ |
| 443 | xfs_extnum_t nextents; /* number of extents in file now */ |
| 444 | |
| 445 | XFS_STATS_INC(xs_add_exlist); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | cur = *curp; |
| 448 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 449 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | da_old = da_new = 0; |
| 451 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 452 | |
| 453 | ASSERT(*idx >= 0); |
| 454 | ASSERT(*idx <= nextents); |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 455 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | * Real allocation off the end of the file. |
| 459 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 460 | if (*idx == nextents) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (cur) |
| 462 | ASSERT((cur->bc_private.b.flags & |
| 463 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 464 | error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new, |
| 465 | &logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } else { |
| 467 | xfs_bmbt_irec_t prev; /* old extent at offset idx */ |
| 468 | |
| 469 | /* |
| 470 | * Get the record referred to by idx. |
| 471 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 472 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | /* |
| 474 | * If it's a real allocation record, and the new allocation ends |
| 475 | * after the start of the referred to record, then we're filling |
| 476 | * in a delayed or unwritten allocation with a real one, or |
| 477 | * converting real back to unwritten. |
| 478 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 479 | if (!isnullstartblock(new->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | new->br_startoff + new->br_blockcount > prev.br_startoff) { |
| 481 | if (prev.br_state != XFS_EXT_UNWRITTEN && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 482 | isnullstartblock(prev.br_startblock)) { |
| 483 | da_old = startblockval(prev.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (cur) |
| 485 | ASSERT(cur->bc_private.b.flags & |
| 486 | XFS_BTCUR_BPRV_WASDEL); |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 487 | error = xfs_bmap_add_extent_delay_real(tp, ip, |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 488 | idx, &cur, new, &da_new, |
| 489 | first, flist, &logflags); |
| 490 | } else { |
| 491 | ASSERT(new->br_state == XFS_EXT_NORM || |
| 492 | new->br_state == XFS_EXT_UNWRITTEN); |
| 493 | |
| 494 | error = xfs_bmap_add_extent_unwritten_real(ip, |
| 495 | idx, &cur, new, &logflags); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 496 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | /* |
| 501 | * Otherwise we're filling in a hole with an allocation. |
| 502 | */ |
| 503 | else { |
| 504 | if (cur) |
| 505 | ASSERT((cur->bc_private.b.flags & |
| 506 | XFS_BTCUR_BPRV_WASDEL) == 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 507 | error = xfs_bmap_add_extent_hole_real(ip, idx, cur, |
| 508 | new, &logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 512 | if (error) |
| 513 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | ASSERT(*curp == cur || *curp == NULL); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* |
| 517 | * Convert to a btree if necessary. |
| 518 | */ |
| 519 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 520 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 521 | int tmp_logflags; /* partial log flag return val */ |
| 522 | |
| 523 | ASSERT(cur == NULL); |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 524 | error = xfs_bmap_extents_to_btree(tp, ip, first, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | flist, &cur, da_old > 0, &tmp_logflags, whichfork); |
| 526 | logflags |= tmp_logflags; |
| 527 | if (error) |
| 528 | goto done; |
| 529 | } |
| 530 | /* |
| 531 | * Adjust for changes in reserved delayed indirect blocks. |
| 532 | * Nothing to do for disk quotas here. |
| 533 | */ |
| 534 | if (da_old || da_new) { |
| 535 | xfs_filblks_t nblks; |
| 536 | |
| 537 | nblks = da_new; |
| 538 | if (cur) |
| 539 | nblks += cur->bc_private.b.allocated; |
| 540 | ASSERT(nblks <= da_old); |
| 541 | if (nblks < da_old) |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 542 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 543 | (int64_t)(da_old - nblks), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | /* |
| 546 | * Clear out the allocated field, done with it now in any case. |
| 547 | */ |
| 548 | if (cur) { |
| 549 | cur->bc_private.b.allocated = 0; |
| 550 | *curp = cur; |
| 551 | } |
| 552 | done: |
| 553 | #ifdef DEBUG |
| 554 | if (!error) |
| 555 | xfs_bmap_check_leaf_extents(*curp, ip, whichfork); |
| 556 | #endif |
| 557 | *logflagsp = logflags; |
| 558 | return error; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Called by xfs_bmap_add_extent to handle cases converting a delayed |
| 563 | * allocation to a real allocation. |
| 564 | */ |
| 565 | STATIC int /* error */ |
| 566 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 567 | struct xfs_trans *tp, /* transaction pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 569 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 571 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */ |
| 573 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 574 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 575 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 578 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 579 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 582 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 584 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 585 | /* left is 0, right is 1, prev is 2 */ |
| 586 | int rval=0; /* return value (logging flags) */ |
| 587 | int state = 0;/* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 588 | xfs_filblks_t temp=0; /* value for dnew calculations */ |
| 589 | xfs_filblks_t temp2=0;/* value for dnew calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | #define LEFT r[0] |
| 593 | #define RIGHT r[1] |
| 594 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * Set up a bunch of variables to make the tests simpler. |
| 598 | */ |
| 599 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 600 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 601 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | xfs_bmbt_get_all(ep, &PREV); |
| 603 | new_endoff = new->br_startoff + new->br_blockcount; |
| 604 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 605 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* |
| 608 | * Set flags determining what part of the previous delayed allocation |
| 609 | * extent is being replaced by a real allocation. |
| 610 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 611 | if (PREV.br_startoff == new->br_startoff) |
| 612 | state |= BMAP_LEFT_FILLING; |
| 613 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 614 | state |= BMAP_RIGHT_FILLING; |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | /* |
| 617 | * Check and set flags if this segment has a left neighbor. |
| 618 | * Don't set contiguous if the combined extent would be too large. |
| 619 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 620 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 621 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 622 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 623 | |
| 624 | if (isnullstartblock(LEFT.br_startblock)) |
| 625 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 627 | |
| 628 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 629 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 630 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 631 | LEFT.br_state == new->br_state && |
| 632 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 633 | state |= BMAP_LEFT_CONTIG; |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | /* |
| 636 | * Check and set flags if this segment has a right neighbor. |
| 637 | * Don't set contiguous if the combined extent would be too large. |
| 638 | * Also check for all-three-contiguous being too large. |
| 639 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 640 | 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] | 641 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 642 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 643 | |
| 644 | if (isnullstartblock(RIGHT.br_startblock)) |
| 645 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 647 | |
| 648 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 649 | new_endoff == RIGHT.br_startoff && |
| 650 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 651 | new->br_state == RIGHT.br_state && |
| 652 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 653 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 654 | BMAP_RIGHT_FILLING)) != |
| 655 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 656 | BMAP_RIGHT_FILLING) || |
| 657 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 658 | <= MAXEXTLEN)) |
| 659 | state |= BMAP_RIGHT_CONTIG; |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | error = 0; |
| 662 | /* |
| 663 | * Switch out based on the FILLING and CONTIG state bits. |
| 664 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 665 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 666 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 667 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 668 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | /* |
| 670 | * Filling in all of a previously delayed allocation extent. |
| 671 | * The left and right neighbors are both contiguous with new. |
| 672 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 673 | --*idx; |
| 674 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 675 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | LEFT.br_blockcount + PREV.br_blockcount + |
| 677 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 678 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 679 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 680 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | ip->i_d.di_nextents--; |
| 682 | if (cur == NULL) |
| 683 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 684 | else { |
| 685 | rval = XFS_ILOG_CORE; |
| 686 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 687 | RIGHT.br_startblock, |
| 688 | RIGHT.br_blockcount, &i))) |
| 689 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 690 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 691 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 693 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 694 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 696 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 698 | LEFT.br_startblock, |
| 699 | LEFT.br_blockcount + |
| 700 | PREV.br_blockcount + |
| 701 | RIGHT.br_blockcount, LEFT.br_state))) |
| 702 | goto done; |
| 703 | } |
| 704 | *dnew = 0; |
| 705 | break; |
| 706 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 707 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* |
| 709 | * Filling in all of a previously delayed allocation extent. |
| 710 | * The left neighbor is contiguous, the right is not. |
| 711 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 712 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 713 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 714 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 715 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 716 | LEFT.br_blockcount + PREV.br_blockcount); |
| 717 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 718 | |
| 719 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if (cur == NULL) |
| 721 | rval = XFS_ILOG_DEXT; |
| 722 | else { |
| 723 | rval = 0; |
| 724 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 725 | LEFT.br_startblock, LEFT.br_blockcount, |
| 726 | &i))) |
| 727 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 728 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 730 | LEFT.br_startblock, |
| 731 | LEFT.br_blockcount + |
| 732 | PREV.br_blockcount, LEFT.br_state))) |
| 733 | goto done; |
| 734 | } |
| 735 | *dnew = 0; |
| 736 | break; |
| 737 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 738 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | /* |
| 740 | * Filling in all of a previously delayed allocation extent. |
| 741 | * The right neighbor is contiguous, the left is not. |
| 742 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 743 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 745 | xfs_bmbt_set_blockcount(ep, |
| 746 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 747 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 748 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 749 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | if (cur == NULL) |
| 751 | rval = XFS_ILOG_DEXT; |
| 752 | else { |
| 753 | rval = 0; |
| 754 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 755 | RIGHT.br_startblock, |
| 756 | RIGHT.br_blockcount, &i))) |
| 757 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 758 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 760 | new->br_startblock, |
| 761 | PREV.br_blockcount + |
| 762 | RIGHT.br_blockcount, PREV.br_state))) |
| 763 | goto done; |
| 764 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | *dnew = 0; |
| 767 | break; |
| 768 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 769 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | /* |
| 771 | * Filling in all of a previously delayed allocation extent. |
| 772 | * Neither the left nor right neighbors are contiguous with |
| 773 | * the new one. |
| 774 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 775 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 777 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | ip->i_d.di_nextents++; |
| 780 | if (cur == NULL) |
| 781 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 782 | else { |
| 783 | rval = XFS_ILOG_CORE; |
| 784 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 785 | new->br_startblock, new->br_blockcount, |
| 786 | &i))) |
| 787 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 788 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 790 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 792 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | *dnew = 0; |
| 796 | break; |
| 797 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 798 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /* |
| 800 | * Filling in the first part of a previous delayed allocation. |
| 801 | * The left neighbor is contiguous. |
| 802 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 803 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 804 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | LEFT.br_blockcount + new->br_blockcount); |
| 806 | xfs_bmbt_set_startoff(ep, |
| 807 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 808 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 811 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | if (cur == NULL) |
| 814 | rval = XFS_ILOG_DEXT; |
| 815 | else { |
| 816 | rval = 0; |
| 817 | if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff, |
| 818 | LEFT.br_startblock, LEFT.br_blockcount, |
| 819 | &i))) |
| 820 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 821 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 823 | LEFT.br_startblock, |
| 824 | LEFT.br_blockcount + |
| 825 | new->br_blockcount, |
| 826 | LEFT.br_state))) |
| 827 | goto done; |
| 828 | } |
| 829 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 830 | startblockval(PREV.br_startblock)); |
| 831 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 832 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 833 | |
| 834 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | *dnew = temp; |
| 836 | break; |
| 837 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 838 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | /* |
| 840 | * Filling in the first part of a previous delayed allocation. |
| 841 | * The left neighbor is not contiguous. |
| 842 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 843 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 845 | temp = PREV.br_blockcount - new->br_blockcount; |
| 846 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 847 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | ip->i_d.di_nextents++; |
| 849 | if (cur == NULL) |
| 850 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 851 | else { |
| 852 | rval = XFS_ILOG_CORE; |
| 853 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 854 | new->br_startblock, new->br_blockcount, |
| 855 | &i))) |
| 856 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 857 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 859 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 861 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | } |
| 863 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 864 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 865 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | first, flist, &cur, 1, &tmp_rval, |
| 867 | XFS_DATA_FORK); |
| 868 | rval |= tmp_rval; |
| 869 | if (error) |
| 870 | goto done; |
| 871 | } |
| 872 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 873 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 875 | ep = xfs_iext_get_ext(ifp, *idx + 1); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 876 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 877 | trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_); |
| 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | *dnew = temp; |
| 880 | break; |
| 881 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 882 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | /* |
| 884 | * Filling in the last part of a previous delayed allocation. |
| 885 | * The right neighbor is contiguous with the new allocation. |
| 886 | */ |
| 887 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 888 | trace_xfs_bmap_pre_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 890 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 891 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | new->br_blockcount + RIGHT.br_blockcount, |
| 893 | RIGHT.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 894 | trace_xfs_bmap_post_update(ip, *idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | if (cur == NULL) |
| 896 | rval = XFS_ILOG_DEXT; |
| 897 | else { |
| 898 | rval = 0; |
| 899 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 900 | RIGHT.br_startblock, |
| 901 | RIGHT.br_blockcount, &i))) |
| 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 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 905 | new->br_startblock, |
| 906 | new->br_blockcount + |
| 907 | RIGHT.br_blockcount, |
| 908 | RIGHT.br_state))) |
| 909 | goto done; |
| 910 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 913 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 914 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 915 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 916 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 917 | |
| 918 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | *dnew = temp; |
| 920 | break; |
| 921 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 922 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | /* |
| 924 | * Filling in the last part of a previous delayed allocation. |
| 925 | * The right neighbor is not contiguous. |
| 926 | */ |
| 927 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 928 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 930 | xfs_iext_insert(ip, *idx + 1, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | ip->i_d.di_nextents++; |
| 932 | if (cur == NULL) |
| 933 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 934 | else { |
| 935 | rval = XFS_ILOG_CORE; |
| 936 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 937 | new->br_startblock, new->br_blockcount, |
| 938 | &i))) |
| 939 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 940 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 942 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 944 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
| 946 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 947 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 948 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | first, flist, &cur, 1, &tmp_rval, |
| 950 | XFS_DATA_FORK); |
| 951 | rval |= tmp_rval; |
| 952 | if (error) |
| 953 | goto done; |
| 954 | } |
| 955 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 956 | startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 958 | ep = xfs_iext_get_ext(ifp, *idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 959 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 960 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 961 | |
| 962 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | *dnew = temp; |
| 964 | break; |
| 965 | |
| 966 | case 0: |
| 967 | /* |
| 968 | * Filling in the middle part of a previous delayed allocation. |
| 969 | * Contiguity is impossible here. |
| 970 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 971 | * |
| 972 | * We start with a delayed allocation: |
| 973 | * |
| 974 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 975 | * PREV @ idx |
| 976 | * |
| 977 | * and we are allocating: |
| 978 | * +rrrrrrrrrrrrrrrrr+ |
| 979 | * new |
| 980 | * |
| 981 | * and we set it up for insertion as: |
| 982 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 983 | * new |
| 984 | * PREV @ idx LEFT RIGHT |
| 985 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | */ |
| 987 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 989 | trace_xfs_bmap_pre_update(ip, *idx, 0, _THIS_IP_); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 990 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 991 | LEFT = *new; |
| 992 | RIGHT.br_state = PREV.br_state; |
| 993 | RIGHT.br_startblock = nullstartblock( |
| 994 | (int)xfs_bmap_worst_indlen(ip, temp2)); |
| 995 | RIGHT.br_startoff = new_endoff; |
| 996 | RIGHT.br_blockcount = temp2; |
| 997 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 998 | xfs_iext_insert(ip, *idx + 1, 2, &LEFT, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | ip->i_d.di_nextents++; |
| 1000 | if (cur == NULL) |
| 1001 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1002 | else { |
| 1003 | rval = XFS_ILOG_CORE; |
| 1004 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1005 | new->br_startblock, new->br_blockcount, |
| 1006 | &i))) |
| 1007 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1008 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1010 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1012 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS && |
| 1015 | ip->i_d.di_nextents > ip->i_df.if_ext_max) { |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 1016 | error = xfs_bmap_extents_to_btree(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | first, flist, &cur, 1, &tmp_rval, |
| 1018 | XFS_DATA_FORK); |
| 1019 | rval |= tmp_rval; |
| 1020 | if (error) |
| 1021 | goto done; |
| 1022 | } |
| 1023 | temp = xfs_bmap_worst_indlen(ip, temp); |
| 1024 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1025 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | (cur ? cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 1027 | if (diff > 0) { |
| 1028 | error = xfs_icsb_modify_counters(ip->i_mount, |
| 1029 | XFS_SBS_FDBLOCKS, |
| 1030 | -((int64_t)diff), 0); |
| 1031 | ASSERT(!error); |
| 1032 | if (error) |
| 1033 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 1035 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1036 | ep = xfs_iext_get_ext(ifp, *idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1037 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1038 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1039 | trace_xfs_bmap_pre_update(ip, *idx + 2, state, _THIS_IP_); |
| 1040 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1041 | nullstartblock((int)temp2)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1042 | trace_xfs_bmap_post_update(ip, *idx + 2, state, _THIS_IP_); |
| 1043 | |
| 1044 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | *dnew = temp + temp2; |
| 1046 | break; |
| 1047 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1048 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1049 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1050 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1051 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1052 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1053 | case BMAP_LEFT_CONTIG: |
| 1054 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | /* |
| 1056 | * These cases are all impossible. |
| 1057 | */ |
| 1058 | ASSERT(0); |
| 1059 | } |
| 1060 | *curp = cur; |
| 1061 | done: |
| 1062 | *logflagsp = rval; |
| 1063 | return error; |
| 1064 | #undef LEFT |
| 1065 | #undef RIGHT |
| 1066 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * Called by xfs_bmap_add_extent to handle cases converting an unwritten |
| 1071 | * allocation to a real allocation or vice versa. |
| 1072 | */ |
| 1073 | STATIC int /* error */ |
| 1074 | xfs_bmap_add_extent_unwritten_real( |
| 1075 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1076 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1078 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 1079 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 1082 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1085 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 1087 | xfs_exntst_t newext; /* new extent state */ |
| 1088 | xfs_exntst_t oldext; /* old extent state */ |
| 1089 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 1090 | /* left is 0, right is 1, prev is 2 */ |
| 1091 | int rval=0; /* return value (logging flags) */ |
| 1092 | int state = 0;/* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
| 1094 | #define LEFT r[0] |
| 1095 | #define RIGHT r[1] |
| 1096 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | /* |
| 1098 | * Set up a bunch of variables to make the tests simpler. |
| 1099 | */ |
| 1100 | error = 0; |
| 1101 | cur = *curp; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1102 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1103 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | xfs_bmbt_get_all(ep, &PREV); |
| 1105 | newext = new->br_state; |
| 1106 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 1107 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 1108 | ASSERT(PREV.br_state == oldext); |
| 1109 | new_endoff = new->br_startoff + new->br_blockcount; |
| 1110 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 1111 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * Set flags determining what part of the previous oldext allocation |
| 1115 | * extent is being replaced by a newext allocation. |
| 1116 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1117 | if (PREV.br_startoff == new->br_startoff) |
| 1118 | state |= BMAP_LEFT_FILLING; |
| 1119 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1120 | state |= BMAP_RIGHT_FILLING; |
| 1121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | /* |
| 1123 | * Check and set flags if this segment has a left neighbor. |
| 1124 | * Don't set contiguous if the combined extent would be too large. |
| 1125 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1126 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1127 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1128 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (isnullstartblock(LEFT.br_startblock)) |
| 1131 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1133 | |
| 1134 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1135 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1136 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1137 | LEFT.br_state == newext && |
| 1138 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1139 | state |= BMAP_LEFT_CONTIG; |
| 1140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * Check and set flags if this segment has a right neighbor. |
| 1143 | * Don't set contiguous if the combined extent would be too large. |
| 1144 | * Also check for all-three-contiguous being too large. |
| 1145 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1146 | 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] | 1147 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1148 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1149 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1150 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1152 | |
| 1153 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1154 | new_endoff == RIGHT.br_startoff && |
| 1155 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1156 | newext == RIGHT.br_state && |
| 1157 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1158 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1159 | BMAP_RIGHT_FILLING)) != |
| 1160 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1161 | BMAP_RIGHT_FILLING) || |
| 1162 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1163 | <= MAXEXTLEN)) |
| 1164 | state |= BMAP_RIGHT_CONTIG; |
| 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | /* |
| 1167 | * Switch out based on the FILLING and CONTIG state bits. |
| 1168 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1169 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1170 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1171 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1172 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * Setting all of a previous oldext extent to newext. |
| 1175 | * The left and right neighbors are both contiguous with new. |
| 1176 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1177 | --*idx; |
| 1178 | |
| 1179 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1180 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1182 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1183 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1184 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1185 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | ip->i_d.di_nextents -= 2; |
| 1187 | if (cur == NULL) |
| 1188 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1189 | else { |
| 1190 | rval = XFS_ILOG_CORE; |
| 1191 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1192 | RIGHT.br_startblock, |
| 1193 | RIGHT.br_blockcount, &i))) |
| 1194 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1195 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1196 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1198 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1199 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1201 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1202 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1204 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1205 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1207 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1209 | LEFT.br_startblock, |
| 1210 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1211 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1212 | goto done; |
| 1213 | } |
| 1214 | break; |
| 1215 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1216 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | /* |
| 1218 | * Setting all of a previous oldext extent to newext. |
| 1219 | * The left neighbor is contiguous, the right is not. |
| 1220 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1221 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1222 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1223 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1224 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 1225 | LEFT.br_blockcount + PREV.br_blockcount); |
| 1226 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1227 | |
| 1228 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | ip->i_d.di_nextents--; |
| 1230 | if (cur == NULL) |
| 1231 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1232 | else { |
| 1233 | rval = XFS_ILOG_CORE; |
| 1234 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1235 | PREV.br_startblock, PREV.br_blockcount, |
| 1236 | &i))) |
| 1237 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1238 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1239 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1241 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1242 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1244 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1246 | LEFT.br_startblock, |
| 1247 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1248 | LEFT.br_state))) |
| 1249 | goto done; |
| 1250 | } |
| 1251 | break; |
| 1252 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1253 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | /* |
| 1255 | * Setting all of a previous oldext extent to newext. |
| 1256 | * The right neighbor is contiguous, the left is not. |
| 1257 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1258 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | xfs_bmbt_set_blockcount(ep, |
| 1260 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1261 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1262 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1263 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | ip->i_d.di_nextents--; |
| 1265 | if (cur == NULL) |
| 1266 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1267 | else { |
| 1268 | rval = XFS_ILOG_CORE; |
| 1269 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1270 | RIGHT.br_startblock, |
| 1271 | RIGHT.br_blockcount, &i))) |
| 1272 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1273 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1274 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1276 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1277 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1279 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1281 | new->br_startblock, |
| 1282 | new->br_blockcount + RIGHT.br_blockcount, |
| 1283 | newext))) |
| 1284 | goto done; |
| 1285 | } |
| 1286 | break; |
| 1287 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1288 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | /* |
| 1290 | * Setting all of a previous oldext extent to newext. |
| 1291 | * Neither the left nor right neighbors are contiguous with |
| 1292 | * the new one. |
| 1293 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1294 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1296 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | if (cur == NULL) |
| 1299 | rval = XFS_ILOG_DEXT; |
| 1300 | else { |
| 1301 | rval = 0; |
| 1302 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1303 | new->br_startblock, new->br_blockcount, |
| 1304 | &i))) |
| 1305 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1306 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1308 | new->br_startblock, new->br_blockcount, |
| 1309 | newext))) |
| 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_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | /* |
| 1316 | * Setting the first part of a previous oldext extent to newext. |
| 1317 | * The left neighbor is contiguous. |
| 1318 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1319 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 1320 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | LEFT.br_blockcount + new->br_blockcount); |
| 1322 | xfs_bmbt_set_startoff(ep, |
| 1323 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1324 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1325 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1326 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | xfs_bmbt_set_startblock(ep, |
| 1328 | new->br_startblock + new->br_blockcount); |
| 1329 | xfs_bmbt_set_blockcount(ep, |
| 1330 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1331 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1332 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1333 | --*idx; |
| 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | if (cur == NULL) |
| 1336 | rval = XFS_ILOG_DEXT; |
| 1337 | else { |
| 1338 | rval = 0; |
| 1339 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1340 | PREV.br_startblock, PREV.br_blockcount, |
| 1341 | &i))) |
| 1342 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1343 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | if ((error = xfs_bmbt_update(cur, |
| 1345 | PREV.br_startoff + new->br_blockcount, |
| 1346 | PREV.br_startblock + new->br_blockcount, |
| 1347 | PREV.br_blockcount - new->br_blockcount, |
| 1348 | oldext))) |
| 1349 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1350 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | goto done; |
| 1352 | if (xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1353 | LEFT.br_startblock, |
| 1354 | LEFT.br_blockcount + new->br_blockcount, |
| 1355 | LEFT.br_state)) |
| 1356 | goto done; |
| 1357 | } |
| 1358 | break; |
| 1359 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1360 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | /* |
| 1362 | * Setting the first part of a previous oldext extent to newext. |
| 1363 | * The left neighbor is not contiguous. |
| 1364 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1365 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1367 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1368 | xfs_bmbt_set_blockcount(ep, |
| 1369 | PREV.br_blockcount - new->br_blockcount); |
| 1370 | xfs_bmbt_set_startblock(ep, |
| 1371 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1372 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1373 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1374 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | ip->i_d.di_nextents++; |
| 1376 | if (cur == NULL) |
| 1377 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1378 | else { |
| 1379 | rval = XFS_ILOG_CORE; |
| 1380 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1381 | PREV.br_startblock, PREV.br_blockcount, |
| 1382 | &i))) |
| 1383 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1384 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | if ((error = xfs_bmbt_update(cur, |
| 1386 | PREV.br_startoff + new->br_blockcount, |
| 1387 | PREV.br_startblock + new->br_blockcount, |
| 1388 | PREV.br_blockcount - new->br_blockcount, |
| 1389 | oldext))) |
| 1390 | goto done; |
| 1391 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1392 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1394 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } |
| 1396 | break; |
| 1397 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1398 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | /* |
| 1400 | * Setting the last part of a previous oldext extent to newext. |
| 1401 | * The right neighbor is contiguous with the new allocation. |
| 1402 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1403 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | xfs_bmbt_set_blockcount(ep, |
| 1405 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1406 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1407 | |
| 1408 | ++*idx; |
| 1409 | |
| 1410 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1411 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1412 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1414 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | if (cur == NULL) |
| 1417 | rval = XFS_ILOG_DEXT; |
| 1418 | else { |
| 1419 | rval = 0; |
| 1420 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1421 | PREV.br_startblock, |
| 1422 | PREV.br_blockcount, &i))) |
| 1423 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1424 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1426 | PREV.br_startblock, |
| 1427 | PREV.br_blockcount - new->br_blockcount, |
| 1428 | oldext))) |
| 1429 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1430 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | goto done; |
| 1432 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1433 | new->br_startblock, |
| 1434 | new->br_blockcount + RIGHT.br_blockcount, |
| 1435 | newext))) |
| 1436 | goto done; |
| 1437 | } |
| 1438 | break; |
| 1439 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1440 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | /* |
| 1442 | * Setting the last part of a previous oldext extent to newext. |
| 1443 | * The right neighbor is not contiguous. |
| 1444 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1445 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | xfs_bmbt_set_blockcount(ep, |
| 1447 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1448 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1449 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1450 | ++*idx; |
| 1451 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 1452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | ip->i_d.di_nextents++; |
| 1454 | if (cur == NULL) |
| 1455 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1456 | else { |
| 1457 | rval = XFS_ILOG_CORE; |
| 1458 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1459 | PREV.br_startblock, PREV.br_blockcount, |
| 1460 | &i))) |
| 1461 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1462 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1464 | PREV.br_startblock, |
| 1465 | PREV.br_blockcount - new->br_blockcount, |
| 1466 | oldext))) |
| 1467 | goto done; |
| 1468 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1469 | new->br_startblock, new->br_blockcount, |
| 1470 | &i))) |
| 1471 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1472 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1474 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1476 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } |
| 1478 | break; |
| 1479 | |
| 1480 | case 0: |
| 1481 | /* |
| 1482 | * Setting the middle part of a previous oldext extent to |
| 1483 | * newext. Contiguity is impossible here. |
| 1484 | * One extent becomes three extents. |
| 1485 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1486 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | xfs_bmbt_set_blockcount(ep, |
| 1488 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1489 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | r[0] = *new; |
| 1492 | r[1].br_startoff = new_endoff; |
| 1493 | r[1].br_blockcount = |
| 1494 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1495 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1496 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1497 | |
| 1498 | ++*idx; |
| 1499 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 1500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | ip->i_d.di_nextents += 2; |
| 1502 | if (cur == NULL) |
| 1503 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1504 | else { |
| 1505 | rval = XFS_ILOG_CORE; |
| 1506 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1507 | PREV.br_startblock, PREV.br_blockcount, |
| 1508 | &i))) |
| 1509 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1510 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | /* new right extent - oldext */ |
| 1512 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1513 | r[1].br_startblock, r[1].br_blockcount, |
| 1514 | r[1].br_state))) |
| 1515 | goto done; |
| 1516 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1518 | cur->bc_rec.b.br_blockcount = |
| 1519 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1520 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1522 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1523 | /* |
| 1524 | * Reset the cursor to the position of the new extent |
| 1525 | * we are about to insert as we can't trust it after |
| 1526 | * the previous insert. |
| 1527 | */ |
| 1528 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1529 | new->br_startblock, new->br_blockcount, |
| 1530 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1532 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1534 | cur->bc_rec.b.br_state = new->br_state; |
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 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1541 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1542 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1543 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1544 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1545 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1546 | case BMAP_LEFT_CONTIG: |
| 1547 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | /* |
| 1549 | * These cases are all impossible. |
| 1550 | */ |
| 1551 | ASSERT(0); |
| 1552 | } |
| 1553 | *curp = cur; |
| 1554 | done: |
| 1555 | *logflagsp = rval; |
| 1556 | return error; |
| 1557 | #undef LEFT |
| 1558 | #undef RIGHT |
| 1559 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 1563 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 1565 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | xfs_bmap_add_extent_hole_delay( |
| 1567 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1568 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 1569 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1571 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1573 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1574 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1575 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1576 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1577 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1579 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1581 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | /* |
| 1584 | * Check and set flags if this segment has a left neighbor |
| 1585 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1586 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1587 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1588 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1589 | |
| 1590 | if (isnullstartblock(left.br_startblock)) |
| 1591 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | /* |
| 1595 | * Check and set flags if the current (right) segment exists. |
| 1596 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1597 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1598 | 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] | 1599 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1600 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1601 | |
| 1602 | if (isnullstartblock(right.br_startblock)) |
| 1603 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | /* |
| 1607 | * Set contiguity flags on the left and right neighbors. |
| 1608 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1609 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1610 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1611 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1612 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1613 | state |= BMAP_LEFT_CONTIG; |
| 1614 | |
| 1615 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1616 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1617 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1618 | (!(state & BMAP_LEFT_CONTIG) || |
| 1619 | (left.br_blockcount + new->br_blockcount + |
| 1620 | right.br_blockcount <= MAXEXTLEN))) |
| 1621 | state |= BMAP_RIGHT_CONTIG; |
| 1622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | /* |
| 1624 | * Switch out based on the contiguity flags. |
| 1625 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1626 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1627 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | /* |
| 1629 | * New allocation is contiguous with delayed allocations |
| 1630 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1631 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1633 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | temp = left.br_blockcount + new->br_blockcount + |
| 1635 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1636 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1637 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1638 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1639 | oldlen = startblockval(left.br_startblock) + |
| 1640 | startblockval(new->br_startblock) + |
| 1641 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1643 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1644 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1645 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1646 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1647 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | break; |
| 1649 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1650 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | /* |
| 1652 | * New allocation is contiguous with a delayed allocation |
| 1653 | * on the left. |
| 1654 | * Merge the new allocation with the left neighbor. |
| 1655 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1656 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1658 | |
| 1659 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1660 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1661 | oldlen = startblockval(left.br_startblock) + |
| 1662 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1664 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1665 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1666 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | break; |
| 1668 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1669 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * New allocation is contiguous with a delayed allocation |
| 1672 | * on the right. |
| 1673 | * Merge the new allocation with the right neighbor. |
| 1674 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1675 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1677 | oldlen = startblockval(new->br_startblock) + |
| 1678 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1680 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1681 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1682 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1683 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | break; |
| 1685 | |
| 1686 | case 0: |
| 1687 | /* |
| 1688 | * New allocation is not contiguous with another |
| 1689 | * delayed allocation. |
| 1690 | * Insert a new entry. |
| 1691 | */ |
| 1692 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1693 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | break; |
| 1695 | } |
| 1696 | if (oldlen != newlen) { |
| 1697 | ASSERT(oldlen > newlen); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1698 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1699 | (int64_t)(oldlen - newlen), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | /* |
| 1701 | * Nothing to do for disk quota accounting here. |
| 1702 | */ |
| 1703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * Called by xfs_bmap_add_extent to handle cases converting a hole |
| 1708 | * to a real allocation. |
| 1709 | */ |
| 1710 | STATIC int /* error */ |
| 1711 | xfs_bmap_add_extent_hole_real( |
| 1712 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1713 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1715 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | int *logflagsp, /* inode logging flags */ |
| 1717 | int whichfork) /* data or attr fork */ |
| 1718 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | int i; /* temp state */ |
| 1721 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1722 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1723 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1724 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
| 1727 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1728 | ASSERT(*idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | state = 0; |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1730 | |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1731 | if (whichfork == XFS_ATTR_FORK) |
| 1732 | state |= BMAP_ATTRFORK; |
| 1733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | /* |
| 1735 | * Check and set flags if this segment has a left neighbor. |
| 1736 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1737 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1738 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1739 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1740 | if (isnullstartblock(left.br_startblock)) |
| 1741 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | /* |
| 1745 | * Check and set flags if this segment has a current value. |
| 1746 | * Not true if we're inserting into the "hole" at eof. |
| 1747 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1748 | if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1749 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1750 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1751 | if (isnullstartblock(right.br_startblock)) |
| 1752 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | /* |
| 1756 | * We're inserting a real allocation between "left" and "right". |
| 1757 | * Set the contiguity flags. Don't let extents get too large. |
| 1758 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1759 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1760 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1761 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1762 | left.br_state == new->br_state && |
| 1763 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1764 | state |= BMAP_LEFT_CONTIG; |
| 1765 | |
| 1766 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1767 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1768 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1769 | new->br_state == right.br_state && |
| 1770 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1771 | (!(state & BMAP_LEFT_CONTIG) || |
| 1772 | left.br_blockcount + new->br_blockcount + |
| 1773 | right.br_blockcount <= MAXEXTLEN)) |
| 1774 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1776 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | /* |
| 1778 | * Select which case we're in here, and implement it. |
| 1779 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1780 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1781 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | /* |
| 1783 | * New allocation is contiguous with real allocations on the |
| 1784 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1785 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1787 | --*idx; |
| 1788 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1789 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | left.br_blockcount + new->br_blockcount + |
| 1791 | right.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1792 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1793 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1794 | xfs_iext_remove(ip, *idx + 1, 1, state); |
| 1795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1797 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 1798 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1799 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1800 | } else { |
| 1801 | rval = XFS_ILOG_CORE; |
| 1802 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1803 | right.br_startoff, |
| 1804 | right.br_startblock, |
| 1805 | right.br_blockcount, &i))) |
| 1806 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1807 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1808 | if ((error = xfs_btree_delete(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1809 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1810 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1811 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1812 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1813 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1814 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 1815 | left.br_startblock, |
| 1816 | left.br_blockcount + |
| 1817 | new->br_blockcount + |
| 1818 | right.br_blockcount, |
| 1819 | left.br_state))) |
| 1820 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1822 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1824 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | /* |
| 1826 | * New allocation is contiguous with a real allocation |
| 1827 | * on the left. |
| 1828 | * Merge the new allocation with the left neighbor. |
| 1829 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1830 | --*idx; |
| 1831 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1832 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1834 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1837 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1838 | } else { |
| 1839 | rval = 0; |
| 1840 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1841 | left.br_startoff, |
| 1842 | left.br_startblock, |
| 1843 | left.br_blockcount, &i))) |
| 1844 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1845 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1846 | if ((error = xfs_bmbt_update(cur, left.br_startoff, |
| 1847 | left.br_startblock, |
| 1848 | left.br_blockcount + |
| 1849 | new->br_blockcount, |
| 1850 | left.br_state))) |
| 1851 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1853 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1855 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | /* |
| 1857 | * New allocation is contiguous with a real allocation |
| 1858 | * on the right. |
| 1859 | * Merge the new allocation with the right neighbor. |
| 1860 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1861 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1862 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1863 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | new->br_blockcount + right.br_blockcount, |
| 1865 | right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1866 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1869 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1870 | } else { |
| 1871 | rval = 0; |
| 1872 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1873 | right.br_startoff, |
| 1874 | right.br_startblock, |
| 1875 | right.br_blockcount, &i))) |
| 1876 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1877 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1878 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1879 | new->br_startblock, |
| 1880 | new->br_blockcount + |
| 1881 | right.br_blockcount, |
| 1882 | right.br_state))) |
| 1883 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1885 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
| 1887 | case 0: |
| 1888 | /* |
| 1889 | * New allocation is not contiguous with another |
| 1890 | * real allocation. |
| 1891 | * Insert a new entry. |
| 1892 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1893 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 1895 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 1896 | if (cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1897 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1898 | } else { |
| 1899 | rval = XFS_ILOG_CORE; |
| 1900 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 1901 | new->br_startoff, |
| 1902 | new->br_startblock, |
| 1903 | new->br_blockcount, &i))) |
| 1904 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1905 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1906 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1907 | if ((error = xfs_btree_insert(cur, &i))) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1908 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1909 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1911 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1913 | done: |
| 1914 | *logflagsp = rval; |
| 1915 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1918 | /* |
| 1919 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 1920 | */ |
| 1921 | STATIC int |
| 1922 | xfs_bmap_extsize_align( |
| 1923 | xfs_mount_t *mp, |
| 1924 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 1925 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 1926 | xfs_extlen_t extsz, /* align to this extent size */ |
| 1927 | int rt, /* is this a realtime inode? */ |
| 1928 | int eof, /* is extent at end-of-file? */ |
| 1929 | int delay, /* creating delalloc extent? */ |
| 1930 | int convert, /* overwriting unwritten extent? */ |
| 1931 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 1932 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 1933 | { |
| 1934 | xfs_fileoff_t orig_off; /* original offset */ |
| 1935 | xfs_extlen_t orig_alen; /* original length */ |
| 1936 | xfs_fileoff_t orig_end; /* original off+len */ |
| 1937 | xfs_fileoff_t nexto; /* next file offset */ |
| 1938 | xfs_fileoff_t prevo; /* previous file offset */ |
| 1939 | xfs_fileoff_t align_off; /* temp for offset */ |
| 1940 | xfs_extlen_t align_alen; /* temp for length */ |
| 1941 | xfs_extlen_t temp; /* temp for calculations */ |
| 1942 | |
| 1943 | if (convert) |
| 1944 | return 0; |
| 1945 | |
| 1946 | orig_off = align_off = *offp; |
| 1947 | orig_alen = align_alen = *lenp; |
| 1948 | orig_end = orig_off + orig_alen; |
| 1949 | |
| 1950 | /* |
| 1951 | * If this request overlaps an existing extent, then don't |
| 1952 | * attempt to perform any additional alignment. |
| 1953 | */ |
| 1954 | if (!delay && !eof && |
| 1955 | (orig_off >= gotp->br_startoff) && |
| 1956 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 1957 | return 0; |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * If the file offset is unaligned vs. the extent size |
| 1962 | * we need to align it. This will be possible unless |
| 1963 | * the file was previously written with a kernel that didn't |
| 1964 | * perform this alignment, or if a truncate shot us in the |
| 1965 | * foot. |
| 1966 | */ |
| 1967 | temp = do_mod(orig_off, extsz); |
| 1968 | if (temp) { |
| 1969 | align_alen += temp; |
| 1970 | align_off -= temp; |
| 1971 | } |
| 1972 | /* |
| 1973 | * Same adjustment for the end of the requested area. |
| 1974 | */ |
| 1975 | if ((temp = (align_alen % extsz))) { |
| 1976 | align_alen += extsz - temp; |
| 1977 | } |
| 1978 | /* |
| 1979 | * If the previous block overlaps with this proposed allocation |
| 1980 | * then move the start forward without adjusting the length. |
| 1981 | */ |
| 1982 | if (prevp->br_startoff != NULLFILEOFF) { |
| 1983 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 1984 | prevo = prevp->br_startoff; |
| 1985 | else |
| 1986 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 1987 | } else |
| 1988 | prevo = 0; |
| 1989 | if (align_off != orig_off && align_off < prevo) |
| 1990 | align_off = prevo; |
| 1991 | /* |
| 1992 | * If the next block overlaps with this proposed allocation |
| 1993 | * then move the start back without adjusting the length, |
| 1994 | * but not before offset 0. |
| 1995 | * This may of course make the start overlap previous block, |
| 1996 | * and if we hit the offset 0 limit then the next block |
| 1997 | * can still overlap too. |
| 1998 | */ |
| 1999 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 2000 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 2001 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 2002 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 2003 | else |
| 2004 | nexto = gotp->br_startoff; |
| 2005 | } else |
| 2006 | nexto = NULLFILEOFF; |
| 2007 | if (!eof && |
| 2008 | align_off + align_alen != orig_end && |
| 2009 | align_off + align_alen > nexto) |
| 2010 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 2011 | /* |
| 2012 | * If we're now overlapping the next or previous extent that |
| 2013 | * means we can't fit an extsz piece in this hole. Just move |
| 2014 | * the start forward to the first valid spot and set |
| 2015 | * the length so we hit the end. |
| 2016 | */ |
| 2017 | if (align_off != orig_off && align_off < prevo) |
| 2018 | align_off = prevo; |
| 2019 | if (align_off + align_alen != orig_end && |
| 2020 | align_off + align_alen > nexto && |
| 2021 | nexto != NULLFILEOFF) { |
| 2022 | ASSERT(nexto > prevo); |
| 2023 | align_alen = nexto - align_off; |
| 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * If realtime, and the result isn't a multiple of the realtime |
| 2028 | * extent size we need to remove blocks until it is. |
| 2029 | */ |
| 2030 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 2031 | /* |
| 2032 | * We're not covering the original request, or |
| 2033 | * we won't be able to once we fix the length. |
| 2034 | */ |
| 2035 | if (orig_off < align_off || |
| 2036 | orig_end > align_off + align_alen || |
| 2037 | align_alen - temp < orig_alen) |
| 2038 | return XFS_ERROR(EINVAL); |
| 2039 | /* |
| 2040 | * Try to fix it by moving the start up. |
| 2041 | */ |
| 2042 | if (align_off + temp <= orig_off) { |
| 2043 | align_alen -= temp; |
| 2044 | align_off += temp; |
| 2045 | } |
| 2046 | /* |
| 2047 | * Try to fix it by moving the end in. |
| 2048 | */ |
| 2049 | else if (align_off + align_alen - temp >= orig_end) |
| 2050 | align_alen -= temp; |
| 2051 | /* |
| 2052 | * Set the start to the minimum then trim the length. |
| 2053 | */ |
| 2054 | else { |
| 2055 | align_alen -= orig_off - align_off; |
| 2056 | align_off = orig_off; |
| 2057 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 2058 | } |
| 2059 | /* |
| 2060 | * Result doesn't cover the request, fail it. |
| 2061 | */ |
| 2062 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2063 | return XFS_ERROR(EINVAL); |
| 2064 | } else { |
| 2065 | ASSERT(orig_off >= align_off); |
| 2066 | ASSERT(orig_end <= align_off + align_alen); |
| 2067 | } |
| 2068 | |
| 2069 | #ifdef DEBUG |
| 2070 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2071 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2072 | if (prevp->br_startoff != NULLFILEOFF) |
| 2073 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2074 | #endif |
| 2075 | |
| 2076 | *lenp = align_alen; |
| 2077 | *offp = align_off; |
| 2078 | return 0; |
| 2079 | } |
| 2080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2082 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2083 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2084 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2086 | { |
| 2087 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2089 | xfs_mount_t *mp; /* mount point structure */ |
| 2090 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2091 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | |
| 2093 | #define ISVALID(x,y) \ |
| 2094 | (rt ? \ |
| 2095 | (x) < mp->m_sb.sb_rblocks : \ |
| 2096 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2097 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2098 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | mp = ap->ip->i_mount; |
| 2101 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2102 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
| 2103 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | /* |
| 2105 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2106 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | */ |
| 2108 | if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2109 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount, |
| 2111 | ap->prevp->br_startblock)) { |
| 2112 | ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount; |
| 2113 | /* |
| 2114 | * Adjust for the gap between prevp and us. |
| 2115 | */ |
| 2116 | adjust = ap->off - |
| 2117 | (ap->prevp->br_startoff + ap->prevp->br_blockcount); |
| 2118 | if (adjust && |
| 2119 | ISVALID(ap->rval + adjust, ap->prevp->br_startblock)) |
| 2120 | ap->rval += adjust; |
| 2121 | } |
| 2122 | /* |
| 2123 | * If not at eof, then compare the two neighbor blocks. |
| 2124 | * Figure out whether either one gives us a good starting point, |
| 2125 | * and pick the better one. |
| 2126 | */ |
| 2127 | else if (!ap->eof) { |
| 2128 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2129 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2130 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2131 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2132 | |
| 2133 | /* |
| 2134 | * If there's a previous (left) block, select a requested |
| 2135 | * start block based on it. |
| 2136 | */ |
| 2137 | if (ap->prevp->br_startoff != NULLFILEOFF && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2138 | !isnullstartblock(ap->prevp->br_startblock) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | (prevbno = ap->prevp->br_startblock + |
| 2140 | ap->prevp->br_blockcount) && |
| 2141 | ISVALID(prevbno, ap->prevp->br_startblock)) { |
| 2142 | /* |
| 2143 | * Calculate gap to end of previous block. |
| 2144 | */ |
| 2145 | adjust = prevdiff = ap->off - |
| 2146 | (ap->prevp->br_startoff + |
| 2147 | ap->prevp->br_blockcount); |
| 2148 | /* |
| 2149 | * Figure the startblock based on the previous block's |
| 2150 | * end and the gap size. |
| 2151 | * Heuristic! |
| 2152 | * If the gap is large relative to the piece we're |
| 2153 | * allocating, or using it gives us an invalid block |
| 2154 | * number, then just use the end of the previous block. |
| 2155 | */ |
| 2156 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2157 | ISVALID(prevbno + prevdiff, |
| 2158 | ap->prevp->br_startblock)) |
| 2159 | prevbno += adjust; |
| 2160 | else |
| 2161 | prevdiff += adjust; |
| 2162 | /* |
| 2163 | * If the firstblock forbids it, can't use it, |
| 2164 | * must use default. |
| 2165 | */ |
| 2166 | if (!rt && !nullfb && |
| 2167 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2168 | prevbno = NULLFSBLOCK; |
| 2169 | } |
| 2170 | /* |
| 2171 | * No previous block or can't follow it, just default. |
| 2172 | */ |
| 2173 | else |
| 2174 | prevbno = NULLFSBLOCK; |
| 2175 | /* |
| 2176 | * If there's a following (right) block, select a requested |
| 2177 | * start block based on it. |
| 2178 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2179 | if (!isnullstartblock(ap->gotp->br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | /* |
| 2181 | * Calculate gap to start of next block. |
| 2182 | */ |
| 2183 | adjust = gotdiff = ap->gotp->br_startoff - ap->off; |
| 2184 | /* |
| 2185 | * Figure the startblock based on the next block's |
| 2186 | * start and the gap size. |
| 2187 | */ |
| 2188 | gotbno = ap->gotp->br_startblock; |
| 2189 | /* |
| 2190 | * Heuristic! |
| 2191 | * If the gap is large relative to the piece we're |
| 2192 | * allocating, or using it gives us an invalid block |
| 2193 | * number, then just use the start of the next block |
| 2194 | * offset by our length. |
| 2195 | */ |
| 2196 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen && |
| 2197 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2198 | gotbno -= adjust; |
| 2199 | else if (ISVALID(gotbno - ap->alen, gotbno)) { |
| 2200 | gotbno -= ap->alen; |
| 2201 | gotdiff += adjust - ap->alen; |
| 2202 | } else |
| 2203 | gotdiff += adjust; |
| 2204 | /* |
| 2205 | * If the firstblock forbids it, can't use it, |
| 2206 | * must use default. |
| 2207 | */ |
| 2208 | if (!rt && !nullfb && |
| 2209 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2210 | gotbno = NULLFSBLOCK; |
| 2211 | } |
| 2212 | /* |
| 2213 | * No next block, just default. |
| 2214 | */ |
| 2215 | else |
| 2216 | gotbno = NULLFSBLOCK; |
| 2217 | /* |
| 2218 | * If both valid, pick the better one, else the only good |
| 2219 | * one, else ap->rval is already set (to 0 or the inode block). |
| 2220 | */ |
| 2221 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
| 2222 | ap->rval = prevdiff <= gotdiff ? prevbno : gotbno; |
| 2223 | else if (prevbno != NULLFSBLOCK) |
| 2224 | ap->rval = prevbno; |
| 2225 | else if (gotbno != NULLFSBLOCK) |
| 2226 | ap->rval = gotbno; |
| 2227 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2228 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | STATIC int |
| 2232 | xfs_bmap_rtalloc( |
| 2233 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2234 | { |
| 2235 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2236 | int error; /* error return value */ |
| 2237 | xfs_mount_t *mp; /* mount point structure */ |
| 2238 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2239 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2240 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2241 | xfs_rtblock_t rtb; |
| 2242 | |
| 2243 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2244 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2245 | prod = align / mp->m_sb.sb_rextsize; |
| 2246 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2247 | align, 1, ap->eof, 0, |
| 2248 | ap->conv, &ap->off, &ap->alen); |
| 2249 | if (error) |
| 2250 | return error; |
| 2251 | ASSERT(ap->alen); |
| 2252 | ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0); |
| 2253 | |
| 2254 | /* |
| 2255 | * If the offset & length are not perfectly aligned |
| 2256 | * then kill prod, it will just get us in trouble. |
| 2257 | */ |
| 2258 | if (do_mod(ap->off, align) || ap->alen % align) |
| 2259 | prod = 1; |
| 2260 | /* |
| 2261 | * Set ralen to be the actual requested length in rtextents. |
| 2262 | */ |
| 2263 | ralen = ap->alen / mp->m_sb.sb_rextsize; |
| 2264 | /* |
| 2265 | * If the old value was close enough to MAXEXTLEN that |
| 2266 | * we rounded up to it, cut it back so it's valid again. |
| 2267 | * Note that if it's a really large request (bigger than |
| 2268 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2269 | * adjust the starting point to match it. |
| 2270 | */ |
| 2271 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2272 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* |
| 2275 | * Lock out other modifications to the RT bitmap inode. |
| 2276 | */ |
Christoph Hellwig | 1050c71 | 2011-02-13 13:25:31 +0000 | [diff] [blame] | 2277 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 2278 | xfs_trans_ijoin_ref(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2279 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2280 | /* |
| 2281 | * If it's an allocation to an empty file at offset 0, |
| 2282 | * pick an extent that will space things out in the rt area. |
| 2283 | */ |
| 2284 | if (ap->eof && ap->off == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2285 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2286 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2287 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2288 | if (error) |
| 2289 | return error; |
| 2290 | ap->rval = rtx * mp->m_sb.sb_rextsize; |
| 2291 | } else { |
| 2292 | ap->rval = 0; |
| 2293 | } |
| 2294 | |
| 2295 | xfs_bmap_adjacent(ap); |
| 2296 | |
| 2297 | /* |
| 2298 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2299 | */ |
| 2300 | atype = ap->rval == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2301 | do_div(ap->rval, mp->m_sb.sb_rextsize); |
| 2302 | rtb = ap->rval; |
| 2303 | ap->alen = ralen; |
| 2304 | if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen, |
| 2305 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2306 | return error; |
| 2307 | if (rtb == NULLFSBLOCK && prod > 1 && |
| 2308 | (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, |
| 2309 | ap->alen, &ralen, atype, |
| 2310 | ap->wasdel, 1, &rtb))) |
| 2311 | return error; |
| 2312 | ap->rval = rtb; |
| 2313 | if (ap->rval != NULLFSBLOCK) { |
| 2314 | ap->rval *= mp->m_sb.sb_rextsize; |
| 2315 | ralen *= mp->m_sb.sb_rextsize; |
| 2316 | ap->alen = ralen; |
| 2317 | ap->ip->i_d.di_nblocks += ralen; |
| 2318 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2319 | if (ap->wasdel) |
| 2320 | ap->ip->i_delayed_blks -= ralen; |
| 2321 | /* |
| 2322 | * Adjust the disk quota also. This was reserved |
| 2323 | * earlier. |
| 2324 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2325 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2326 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2327 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2328 | } else { |
| 2329 | ap->alen = 0; |
| 2330 | } |
| 2331 | return 0; |
| 2332 | } |
| 2333 | |
| 2334 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2335 | xfs_bmap_btalloc_nullfb( |
| 2336 | struct xfs_bmalloca *ap, |
| 2337 | struct xfs_alloc_arg *args, |
| 2338 | xfs_extlen_t *blen) |
| 2339 | { |
| 2340 | struct xfs_mount *mp = ap->ip->i_mount; |
| 2341 | struct xfs_perag *pag; |
| 2342 | xfs_agnumber_t ag, startag; |
| 2343 | int notinit = 0; |
| 2344 | int error; |
| 2345 | |
| 2346 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2347 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2348 | else |
| 2349 | args->type = XFS_ALLOCTYPE_START_BNO; |
| 2350 | args->total = ap->total; |
| 2351 | |
| 2352 | /* |
| 2353 | * Search for an allocation group with a single extent large enough |
| 2354 | * for the request. If one isn't found, then adjust the minimum |
| 2355 | * allocation size to the largest space found. |
| 2356 | */ |
| 2357 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2358 | if (startag == NULLAGNUMBER) |
| 2359 | startag = ag = 0; |
| 2360 | |
| 2361 | pag = xfs_perag_get(mp, ag); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2362 | while (*blen < args->maxlen) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2363 | if (!pag->pagf_init) { |
| 2364 | error = xfs_alloc_pagf_init(mp, args->tp, ag, |
| 2365 | XFS_ALLOC_FLAG_TRYLOCK); |
| 2366 | if (error) { |
| 2367 | xfs_perag_put(pag); |
| 2368 | return error; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * See xfs_alloc_fix_freelist... |
| 2374 | */ |
| 2375 | if (pag->pagf_init) { |
| 2376 | xfs_extlen_t longest; |
| 2377 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 2378 | if (*blen < longest) |
| 2379 | *blen = longest; |
| 2380 | } else |
| 2381 | notinit = 1; |
| 2382 | |
| 2383 | if (xfs_inode_is_filestream(ap->ip)) { |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2384 | if (*blen >= args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | |
| 2387 | if (ap->userdata) { |
| 2388 | /* |
| 2389 | * If startag is an invalid AG, we've |
| 2390 | * come here once before and |
| 2391 | * xfs_filestream_new_ag picked the |
| 2392 | * best currently available. |
| 2393 | * |
| 2394 | * Don't continue looping, since we |
| 2395 | * could loop forever. |
| 2396 | */ |
| 2397 | if (startag == NULLAGNUMBER) |
| 2398 | break; |
| 2399 | |
| 2400 | error = xfs_filestream_new_ag(ap, &ag); |
| 2401 | xfs_perag_put(pag); |
| 2402 | if (error) |
| 2403 | return error; |
| 2404 | |
| 2405 | /* loop again to set 'blen'*/ |
| 2406 | startag = NULLAGNUMBER; |
| 2407 | pag = xfs_perag_get(mp, ag); |
| 2408 | continue; |
| 2409 | } |
| 2410 | } |
| 2411 | if (++ag == mp->m_sb.sb_agcount) |
| 2412 | ag = 0; |
| 2413 | if (ag == startag) |
| 2414 | break; |
| 2415 | xfs_perag_put(pag); |
| 2416 | pag = xfs_perag_get(mp, ag); |
| 2417 | } |
| 2418 | xfs_perag_put(pag); |
| 2419 | |
| 2420 | /* |
| 2421 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2422 | * possible that there is space for this request. |
| 2423 | */ |
| 2424 | if (notinit || *blen < ap->minlen) |
| 2425 | args->minlen = ap->minlen; |
| 2426 | /* |
| 2427 | * If the best seen length is less than the request |
| 2428 | * length, use the best as the minimum. |
| 2429 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2430 | else if (*blen < args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2431 | args->minlen = *blen; |
| 2432 | /* |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2433 | * Otherwise we've seen an extent as big as maxlen, |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2434 | * use that as the minimum. |
| 2435 | */ |
| 2436 | else |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2437 | args->minlen = args->maxlen; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2438 | |
| 2439 | /* |
| 2440 | * set the failure fallback case to look in the selected |
| 2441 | * AG as the stream may have moved. |
| 2442 | */ |
| 2443 | if (xfs_inode_is_filestream(ap->ip)) |
| 2444 | ap->rval = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2445 | |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2450 | xfs_bmap_btalloc( |
| 2451 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2452 | { |
| 2453 | xfs_mount_t *mp; /* mount point structure */ |
| 2454 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2455 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2456 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2457 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2458 | xfs_alloc_arg_t args; |
| 2459 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2460 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2461 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2462 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2463 | int tryagain; |
| 2464 | int error; |
| 2465 | |
| 2466 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2467 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2468 | if (unlikely(align)) { |
| 2469 | error = xfs_bmap_extsize_align(mp, ap->gotp, ap->prevp, |
| 2470 | align, 0, ap->eof, 0, ap->conv, |
| 2471 | &ap->off, &ap->alen); |
| 2472 | ASSERT(!error); |
| 2473 | ASSERT(ap->alen); |
| 2474 | } |
| 2475 | nullfb = ap->firstblock == NULLFSBLOCK; |
| 2476 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2477 | if (nullfb) { |
| 2478 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2479 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2480 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
| 2481 | ap->rval = XFS_AGB_TO_FSB(mp, ag, 0); |
| 2482 | } else { |
| 2483 | ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
| 2484 | } |
| 2485 | } else |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2486 | ap->rval = ap->firstblock; |
| 2487 | |
| 2488 | xfs_bmap_adjacent(ap); |
| 2489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | /* |
| 2491 | * If allowed, use ap->rval; otherwise must use firstblock since |
| 2492 | * it's in the right allocation group. |
| 2493 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2494 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | ; |
| 2496 | else |
| 2497 | ap->rval = ap->firstblock; |
| 2498 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | * Normal allocation, done through xfs_alloc_vextent. |
| 2500 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2501 | tryagain = isaligned = 0; |
| 2502 | args.tp = ap->tp; |
| 2503 | args.mp = mp; |
| 2504 | args.fsbno = ap->rval; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2505 | |
| 2506 | /* Trim the allocation back to the maximum an AG can fit. */ |
| 2507 | args.maxlen = MIN(ap->alen, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 2508 | args.firstblock = ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2509 | blen = 0; |
| 2510 | if (nullfb) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2511 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
| 2512 | if (error) |
| 2513 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2514 | } else if (ap->low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2515 | if (xfs_inode_is_filestream(ap->ip)) |
| 2516 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2517 | else |
| 2518 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2519 | args.total = args.minlen = ap->minlen; |
| 2520 | } else { |
| 2521 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2522 | args.total = ap->total; |
| 2523 | args.minlen = ap->minlen; |
| 2524 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2525 | /* apply extent size hints if obtained earlier */ |
| 2526 | if (unlikely(align)) { |
| 2527 | args.prod = align; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2528 | if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))) |
| 2529 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2530 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2531 | args.prod = 1; |
| 2532 | args.mod = 0; |
| 2533 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2534 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2535 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))) |
| 2536 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2537 | } |
| 2538 | /* |
| 2539 | * If we are not low on available data blocks, and the |
| 2540 | * underlying logical volume manager is a stripe, and |
| 2541 | * the file offset is zero then try to allocate data |
| 2542 | * blocks on stripe unit boundary. |
| 2543 | * NOTE: ap->aeof is only set if the allocation length |
| 2544 | * is >= the stripe unit and the allocation offset is |
| 2545 | * at the end of file. |
| 2546 | */ |
| 2547 | if (!ap->low && ap->aeof) { |
| 2548 | if (!ap->off) { |
| 2549 | args.alignment = mp->m_dalign; |
| 2550 | atype = args.type; |
| 2551 | isaligned = 1; |
| 2552 | /* |
| 2553 | * Adjust for alignment |
| 2554 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2555 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2556 | args.minlen = blen - args.alignment; |
| 2557 | args.minalignslop = 0; |
| 2558 | } else { |
| 2559 | /* |
| 2560 | * First try an exact bno allocation. |
| 2561 | * If it fails then do a near or start bno |
| 2562 | * allocation with alignment turned on. |
| 2563 | */ |
| 2564 | atype = args.type; |
| 2565 | tryagain = 1; |
| 2566 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2567 | args.alignment = 1; |
| 2568 | /* |
| 2569 | * Compute the minlen+alignment for the |
| 2570 | * next case. Set slop so that the value |
| 2571 | * of minlen+alignment+slop doesn't go up |
| 2572 | * between the calls. |
| 2573 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2574 | if (blen > mp->m_dalign && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2575 | nextminlen = blen - mp->m_dalign; |
| 2576 | else |
| 2577 | nextminlen = args.minlen; |
| 2578 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2579 | args.minalignslop = |
| 2580 | nextminlen + mp->m_dalign - |
| 2581 | args.minlen - 1; |
| 2582 | else |
| 2583 | args.minalignslop = 0; |
| 2584 | } |
| 2585 | } else { |
| 2586 | args.alignment = 1; |
| 2587 | args.minalignslop = 0; |
| 2588 | } |
| 2589 | args.minleft = ap->minleft; |
| 2590 | args.wasdel = ap->wasdel; |
| 2591 | args.isfl = 0; |
| 2592 | args.userdata = ap->userdata; |
| 2593 | if ((error = xfs_alloc_vextent(&args))) |
| 2594 | return error; |
| 2595 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2596 | /* |
| 2597 | * Exact allocation failed. Now try with alignment |
| 2598 | * turned on. |
| 2599 | */ |
| 2600 | args.type = atype; |
| 2601 | args.fsbno = ap->rval; |
| 2602 | args.alignment = mp->m_dalign; |
| 2603 | args.minlen = nextminlen; |
| 2604 | args.minalignslop = 0; |
| 2605 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | if ((error = xfs_alloc_vextent(&args))) |
| 2607 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2608 | } |
| 2609 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2610 | /* |
| 2611 | * allocation failed, so turn off alignment and |
| 2612 | * try again. |
| 2613 | */ |
| 2614 | args.type = atype; |
| 2615 | args.fsbno = ap->rval; |
| 2616 | args.alignment = 0; |
| 2617 | if ((error = xfs_alloc_vextent(&args))) |
| 2618 | return error; |
| 2619 | } |
| 2620 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2621 | args.minlen > ap->minlen) { |
| 2622 | args.minlen = ap->minlen; |
| 2623 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 2624 | args.fsbno = ap->rval; |
| 2625 | if ((error = xfs_alloc_vextent(&args))) |
| 2626 | return error; |
| 2627 | } |
| 2628 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2629 | args.fsbno = 0; |
| 2630 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2631 | args.total = ap->minlen; |
| 2632 | args.minleft = 0; |
| 2633 | if ((error = xfs_alloc_vextent(&args))) |
| 2634 | return error; |
| 2635 | ap->low = 1; |
| 2636 | } |
| 2637 | if (args.fsbno != NULLFSBLOCK) { |
| 2638 | ap->firstblock = ap->rval = args.fsbno; |
| 2639 | ASSERT(nullfb || fb_agno == args.agno || |
| 2640 | (ap->low && fb_agno < args.agno)); |
| 2641 | ap->alen = args.len; |
| 2642 | ap->ip->i_d.di_nblocks += args.len; |
| 2643 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2644 | if (ap->wasdel) |
| 2645 | ap->ip->i_delayed_blks -= args.len; |
| 2646 | /* |
| 2647 | * Adjust the disk quota also. This was reserved |
| 2648 | * earlier. |
| 2649 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2650 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2651 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2652 | XFS_TRANS_DQ_BCOUNT, |
| 2653 | (long) args.len); |
| 2654 | } else { |
| 2655 | ap->rval = NULLFSBLOCK; |
| 2656 | ap->alen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | } |
| 2658 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | /* |
| 2662 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2663 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2664 | */ |
| 2665 | STATIC int |
| 2666 | xfs_bmap_alloc( |
| 2667 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2668 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2669 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2670 | return xfs_bmap_rtalloc(ap); |
| 2671 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * Transform a btree format file with only one leaf node, where the |
| 2676 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2677 | * 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] | 2678 | * give up the space for the btree root and pitch the leaf block. |
| 2679 | */ |
| 2680 | STATIC int /* error */ |
| 2681 | xfs_bmap_btree_to_extents( |
| 2682 | xfs_trans_t *tp, /* transaction pointer */ |
| 2683 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2684 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2685 | int *logflagsp, /* inode logging flags */ |
| 2686 | int whichfork) /* data or attr fork */ |
| 2687 | { |
| 2688 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2689 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | xfs_fsblock_t cbno; /* child block number */ |
| 2691 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2692 | int error; /* error return value */ |
| 2693 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2694 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2695 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2696 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2698 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2700 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2701 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2702 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2703 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2704 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2705 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2706 | 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] | 2707 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | *logflagsp = 0; |
| 2709 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2710 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | return error; |
| 2712 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2714 | XFS_BMAP_BTREE_REF))) |
| 2715 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2716 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2717 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | return error; |
| 2719 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2720 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2721 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | xfs_trans_binval(tp, cbp); |
| 2723 | if (cur->bc_bufs[0] == cbp) |
| 2724 | cur->bc_bufs[0] = NULL; |
| 2725 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2726 | ASSERT(ifp->if_broot == NULL); |
| 2727 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2728 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2729 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2734 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | * after removing space (or undoing a delayed allocation). |
| 2736 | */ |
| 2737 | STATIC int /* error */ |
| 2738 | xfs_bmap_del_extent( |
| 2739 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2740 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2741 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2743 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2744 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2746 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | { |
| 2748 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2749 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2750 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2751 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2752 | int delay; /* current block is delayed allocated */ |
| 2753 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2754 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | int error; /* error return value */ |
| 2756 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2758 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2759 | int i; /* temp state */ |
| 2760 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2761 | xfs_mount_t *mp; /* mount structure */ |
| 2762 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2763 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2764 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | uint qfield; /* quota field to update */ |
| 2766 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2767 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2768 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | |
| 2770 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2771 | |
| 2772 | if (whichfork == XFS_ATTR_FORK) |
| 2773 | state |= BMAP_ATTRFORK; |
| 2774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | mp = ip->i_mount; |
| 2776 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2777 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2778 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2780 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | xfs_bmbt_get_all(ep, &got); |
| 2782 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2783 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2784 | got_endoff = got.br_startoff + got.br_blockcount; |
| 2785 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2786 | delay = isnullstartblock(got.br_startblock); |
| 2787 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | flags = 0; |
| 2789 | qfield = 0; |
| 2790 | error = 0; |
| 2791 | /* |
| 2792 | * If deleting a real allocation, must free up the disk space. |
| 2793 | */ |
| 2794 | if (!delay) { |
| 2795 | flags = XFS_ILOG_CORE; |
| 2796 | /* |
| 2797 | * Realtime allocation. Free it and record di_nblocks update. |
| 2798 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2799 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | xfs_fsblock_t bno; |
| 2801 | xfs_filblks_t len; |
| 2802 | |
| 2803 | ASSERT(do_mod(del->br_blockcount, |
| 2804 | mp->m_sb.sb_rextsize) == 0); |
| 2805 | ASSERT(do_mod(del->br_startblock, |
| 2806 | mp->m_sb.sb_rextsize) == 0); |
| 2807 | bno = del->br_startblock; |
| 2808 | len = del->br_blockcount; |
| 2809 | do_div(bno, mp->m_sb.sb_rextsize); |
| 2810 | do_div(len, mp->m_sb.sb_rextsize); |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 2811 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 2812 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | goto done; |
| 2814 | do_fx = 0; |
| 2815 | nblks = len * mp->m_sb.sb_rextsize; |
| 2816 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 2817 | } |
| 2818 | /* |
| 2819 | * Ordinary allocation. |
| 2820 | */ |
| 2821 | else { |
| 2822 | do_fx = 1; |
| 2823 | nblks = del->br_blockcount; |
| 2824 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 2825 | } |
| 2826 | /* |
| 2827 | * Set up del_endblock and cur for later. |
| 2828 | */ |
| 2829 | del_endblock = del->br_startblock + del->br_blockcount; |
| 2830 | if (cur) { |
| 2831 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 2832 | got.br_startblock, got.br_blockcount, |
| 2833 | &i))) |
| 2834 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2835 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | } |
| 2837 | da_old = da_new = 0; |
| 2838 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2839 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | da_new = 0; |
| 2841 | nblks = 0; |
| 2842 | do_fx = 0; |
| 2843 | } |
| 2844 | /* |
| 2845 | * Set flag value to use in switch statement. |
| 2846 | * Left-contig is 2, right-contig is 1. |
| 2847 | */ |
| 2848 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 2849 | (got_endoff == del_endoff)) { |
| 2850 | case 3: |
| 2851 | /* |
| 2852 | * Matches the whole extent. Delete the entry. |
| 2853 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2854 | xfs_iext_remove(ip, *idx, 1, |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2855 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2856 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | if (delay) |
| 2858 | break; |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2861 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2862 | flags |= XFS_ILOG_CORE; |
| 2863 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2864 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | break; |
| 2866 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2867 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2869 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | break; |
| 2871 | |
| 2872 | case 2: |
| 2873 | /* |
| 2874 | * Deleting the first part of the extent. |
| 2875 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2876 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 2878 | temp = got.br_blockcount - del->br_blockcount; |
| 2879 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | if (delay) { |
| 2881 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2882 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2883 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2884 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | da_new = temp; |
| 2886 | break; |
| 2887 | } |
| 2888 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2889 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2891 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | break; |
| 2893 | } |
| 2894 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 2895 | got.br_blockcount - del->br_blockcount, |
| 2896 | got.br_state))) |
| 2897 | goto done; |
| 2898 | break; |
| 2899 | |
| 2900 | case 1: |
| 2901 | /* |
| 2902 | * Deleting the last part of the extent. |
| 2903 | */ |
| 2904 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2905 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | if (delay) { |
| 2908 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2909 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2910 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2911 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | da_new = temp; |
| 2913 | break; |
| 2914 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2915 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2917 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | break; |
| 2919 | } |
| 2920 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 2921 | got.br_startblock, |
| 2922 | got.br_blockcount - del->br_blockcount, |
| 2923 | got.br_state))) |
| 2924 | goto done; |
| 2925 | break; |
| 2926 | |
| 2927 | case 0: |
| 2928 | /* |
| 2929 | * Deleting the middle of the extent. |
| 2930 | */ |
| 2931 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2932 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | xfs_bmbt_set_blockcount(ep, temp); |
| 2934 | new.br_startoff = del_endoff; |
| 2935 | temp2 = got_endoff - del_endoff; |
| 2936 | new.br_blockcount = temp2; |
| 2937 | new.br_state = got.br_state; |
| 2938 | if (!delay) { |
| 2939 | new.br_startblock = del_endblock; |
| 2940 | flags |= XFS_ILOG_CORE; |
| 2941 | if (cur) { |
| 2942 | if ((error = xfs_bmbt_update(cur, |
| 2943 | got.br_startoff, |
| 2944 | got.br_startblock, temp, |
| 2945 | got.br_state))) |
| 2946 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2947 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | goto done; |
| 2949 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2950 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | if (error && error != ENOSPC) |
| 2952 | goto done; |
| 2953 | /* |
| 2954 | * If get no-space back from btree insert, |
| 2955 | * it tried a split, and we have a zero |
| 2956 | * block reservation. |
| 2957 | * Fix up our state and return the error. |
| 2958 | */ |
| 2959 | if (error == ENOSPC) { |
| 2960 | /* |
| 2961 | * Reset the cursor, don't trust |
| 2962 | * it after any insert operation. |
| 2963 | */ |
| 2964 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2965 | got.br_startoff, |
| 2966 | got.br_startblock, |
| 2967 | temp, &i))) |
| 2968 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2969 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | /* |
| 2971 | * Update the btree record back |
| 2972 | * to the original value. |
| 2973 | */ |
| 2974 | if ((error = xfs_bmbt_update(cur, |
| 2975 | got.br_startoff, |
| 2976 | got.br_startblock, |
| 2977 | got.br_blockcount, |
| 2978 | got.br_state))) |
| 2979 | goto done; |
| 2980 | /* |
| 2981 | * Reset the extent record back |
| 2982 | * to the original value. |
| 2983 | */ |
| 2984 | xfs_bmbt_set_blockcount(ep, |
| 2985 | got.br_blockcount); |
| 2986 | flags = 0; |
| 2987 | error = XFS_ERROR(ENOSPC); |
| 2988 | goto done; |
| 2989 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2990 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2992 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2994 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2995 | } else { |
| 2996 | ASSERT(whichfork == XFS_DATA_FORK); |
| 2997 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2998 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3000 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | da_new = temp + temp2; |
| 3002 | while (da_new > da_old) { |
| 3003 | if (temp) { |
| 3004 | temp--; |
| 3005 | da_new--; |
| 3006 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3007 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | } |
| 3009 | if (da_new == da_old) |
| 3010 | break; |
| 3011 | if (temp2) { |
| 3012 | temp2--; |
| 3013 | da_new--; |
| 3014 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3015 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | } |
| 3017 | } |
| 3018 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 3019 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 3020 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 3021 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | break; |
| 3023 | } |
| 3024 | /* |
| 3025 | * If we need to, add to list of extents to delete. |
| 3026 | */ |
| 3027 | if (do_fx) |
| 3028 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 3029 | mp); |
| 3030 | /* |
| 3031 | * Adjust inode # blocks in the file. |
| 3032 | */ |
| 3033 | if (nblks) |
| 3034 | ip->i_d.di_nblocks -= nblks; |
| 3035 | /* |
| 3036 | * Adjust quota data. |
| 3037 | */ |
| 3038 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3039 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | |
| 3041 | /* |
| 3042 | * Account for change in delayed indirect blocks. |
| 3043 | * Nothing to do for disk quota accounting here. |
| 3044 | */ |
| 3045 | ASSERT(da_old >= da_new); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 3046 | if (da_old > da_new) { |
| 3047 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 3048 | (int64_t)(da_old - da_new), 0); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 3049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3050 | done: |
| 3051 | *logflagsp = flags; |
| 3052 | return error; |
| 3053 | } |
| 3054 | |
| 3055 | /* |
| 3056 | * Remove the entry "free" from the free item list. Prev points to the |
| 3057 | * previous entry, unless "free" is the head of the list. |
| 3058 | */ |
| 3059 | STATIC void |
| 3060 | xfs_bmap_del_free( |
| 3061 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3062 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3063 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3064 | { |
| 3065 | if (prev) |
| 3066 | prev->xbfi_next = free->xbfi_next; |
| 3067 | else |
| 3068 | flist->xbf_first = free->xbfi_next; |
| 3069 | flist->xbf_count--; |
| 3070 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3071 | } |
| 3072 | |
| 3073 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | * Convert an extents-format file into a btree-format file. |
| 3075 | * The new file will have a root block (in the inode) and a single child block. |
| 3076 | */ |
| 3077 | STATIC int /* error */ |
| 3078 | xfs_bmap_extents_to_btree( |
| 3079 | xfs_trans_t *tp, /* transaction pointer */ |
| 3080 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3081 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3082 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3083 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3084 | int wasdel, /* converting a delayed alloc */ |
| 3085 | int *logflagsp, /* inode logging flags */ |
| 3086 | int whichfork) /* data or attr fork */ |
| 3087 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3088 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3090 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3091 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3092 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3094 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3096 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3098 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3099 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3100 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3102 | |
| 3103 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3104 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
| 3105 | ASSERT(ifp->if_ext_max == |
| 3106 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3107 | /* |
| 3108 | * Make space in the inode incore. |
| 3109 | */ |
| 3110 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3111 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | /* |
| 3114 | * Fill in the root. |
| 3115 | */ |
| 3116 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3117 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3118 | block->bb_level = cpu_to_be16(1); |
| 3119 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3120 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3121 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | /* |
| 3124 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3125 | */ |
| 3126 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3127 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | cur->bc_private.b.firstblock = *firstblock; |
| 3129 | cur->bc_private.b.flist = flist; |
| 3130 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3131 | /* |
| 3132 | * Convert to a btree with two levels, one record in root. |
| 3133 | */ |
| 3134 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3135 | args.tp = tp; |
| 3136 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3137 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | if (*firstblock == NULLFSBLOCK) { |
| 3139 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3140 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3141 | } else if (flist->xbf_low) { |
| 3142 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3143 | args.fsbno = *firstblock; |
| 3144 | } else { |
| 3145 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3146 | args.fsbno = *firstblock; |
| 3147 | } |
| 3148 | args.minlen = args.maxlen = args.prod = 1; |
| 3149 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3150 | args.minalignslop = 0; |
| 3151 | args.wasdel = wasdel; |
| 3152 | *logflagsp = 0; |
| 3153 | if ((error = xfs_alloc_vextent(&args))) { |
| 3154 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3155 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3156 | return error; |
| 3157 | } |
| 3158 | /* |
| 3159 | * Allocation can't fail, the space was reserved. |
| 3160 | */ |
| 3161 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3162 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3163 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3164 | (flist->xbf_low && |
| 3165 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3166 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3167 | cur->bc_private.b.allocated++; |
| 3168 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3169 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3171 | /* |
| 3172 | * Fill in the child block. |
| 3173 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3174 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3175 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3177 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3178 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3179 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3181 | for (cnt = i = 0; i < nextents; i++) { |
| 3182 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3183 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3184 | arp->l0 = cpu_to_be64(ep->l0); |
| 3185 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | arp++; cnt++; |
| 3187 | } |
| 3188 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3189 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3190 | xfs_btree_set_numrecs(ablock, cnt); |
| 3191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | /* |
| 3193 | * Fill in the root key and pointer. |
| 3194 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3195 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3196 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3197 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3198 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3199 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3200 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | /* |
| 3203 | * Do all this logging at the end so that |
| 3204 | * the root is at the right level. |
| 3205 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3206 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3207 | 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] | 3208 | ASSERT(*curp == NULL); |
| 3209 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3210 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | return 0; |
| 3212 | } |
| 3213 | |
| 3214 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3215 | * Calculate the default attribute fork offset for newly created inodes. |
| 3216 | */ |
| 3217 | uint |
| 3218 | xfs_default_attroffset( |
| 3219 | struct xfs_inode *ip) |
| 3220 | { |
| 3221 | struct xfs_mount *mp = ip->i_mount; |
| 3222 | uint offset; |
| 3223 | |
| 3224 | if (mp->m_sb.sb_inodesize == 256) { |
| 3225 | offset = XFS_LITINO(mp) - |
| 3226 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3227 | } else { |
| 3228 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3229 | } |
| 3230 | |
| 3231 | ASSERT(offset < XFS_LITINO(mp)); |
| 3232 | return offset; |
| 3233 | } |
| 3234 | |
| 3235 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3236 | * Helper routine to reset inode di_forkoff field when switching |
| 3237 | * attribute fork from local to extent format - we reset it where |
| 3238 | * possible to make space available for inline data fork extents. |
| 3239 | */ |
| 3240 | STATIC void |
| 3241 | xfs_bmap_forkoff_reset( |
| 3242 | xfs_mount_t *mp, |
| 3243 | xfs_inode_t *ip, |
| 3244 | int whichfork) |
| 3245 | { |
| 3246 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3247 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3248 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3249 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3250 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3251 | |
| 3252 | if (dfl_forkoff > ip->i_d.di_forkoff) { |
| 3253 | ip->i_d.di_forkoff = dfl_forkoff; |
| 3254 | ip->i_df.if_ext_max = |
| 3255 | XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3256 | ip->i_afp->if_ext_max = |
| 3257 | XFS_IFORK_ASIZE(ip) / sizeof(xfs_bmbt_rec_t); |
| 3258 | } |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | * Convert a local file to an extents file. |
| 3264 | * This code is out of bounds for data forks of regular files, |
| 3265 | * since the file data needs to get logged so things will stay consistent. |
| 3266 | * (The bmap-level manipulations are ok, though). |
| 3267 | */ |
| 3268 | STATIC int /* error */ |
| 3269 | xfs_bmap_local_to_extents( |
| 3270 | xfs_trans_t *tp, /* transaction pointer */ |
| 3271 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3272 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3273 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3274 | int *logflagsp, /* inode logging flags */ |
| 3275 | int whichfork) /* data or attr fork */ |
| 3276 | { |
| 3277 | int error; /* error return value */ |
| 3278 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3280 | |
| 3281 | /* |
| 3282 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3283 | * So sending the data fork of a regular inode is invalid. |
| 3284 | */ |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3285 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3287 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3288 | flags = 0; |
| 3289 | error = 0; |
| 3290 | if (ifp->if_bytes) { |
| 3291 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3292 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3293 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | |
| 3295 | args.tp = tp; |
| 3296 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3297 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3298 | ASSERT((ifp->if_flags & |
| 3299 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | /* |
| 3301 | * Allocate a block. We know we need only one, since the |
| 3302 | * file currently fits in an inode. |
| 3303 | */ |
| 3304 | if (*firstblock == NULLFSBLOCK) { |
| 3305 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3306 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3307 | } else { |
| 3308 | args.fsbno = *firstblock; |
| 3309 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3310 | } |
| 3311 | args.total = total; |
| 3312 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3313 | args.isfl = args.minalignslop = 0; |
| 3314 | args.minlen = args.maxlen = args.prod = 1; |
| 3315 | if ((error = xfs_alloc_vextent(&args))) |
| 3316 | goto done; |
| 3317 | /* |
| 3318 | * Can't fail, the space was reserved. |
| 3319 | */ |
| 3320 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3321 | ASSERT(args.len == 1); |
| 3322 | *firstblock = args.fsbno; |
| 3323 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 3324 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3325 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3326 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3328 | xfs_iext_add(ifp, 0, 1); |
| 3329 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3331 | trace_xfs_bmap_post_update(ip, 0, |
| 3332 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3333 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3335 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3336 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3337 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3338 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3339 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3341 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3343 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3344 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3345 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3346 | flags |= XFS_ILOG_CORE; |
| 3347 | done: |
| 3348 | *logflagsp = flags; |
| 3349 | return error; |
| 3350 | } |
| 3351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3353 | * Search the extent records for the entry containing block bno. |
| 3354 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3355 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3356 | * entry (null if none). Else, *lastxp will be set to the index |
| 3357 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3358 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3359 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3360 | xfs_bmap_search_multi_extents( |
| 3361 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3362 | xfs_fileoff_t bno, /* block number searched for */ |
| 3363 | int *eofp, /* out: end of file found */ |
| 3364 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3365 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3366 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3367 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3368 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3369 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3370 | |
| 3371 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3372 | * Initialize the extent entry structure to catch access to |
| 3373 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3374 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3375 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3376 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3377 | gotp->br_state = XFS_EXT_INVALID; |
| 3378 | #if XFS_BIG_BLKNOS |
| 3379 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3380 | #else |
| 3381 | gotp->br_startblock = 0xffffa5a5; |
| 3382 | #endif |
| 3383 | prevp->br_startoff = NULLFILEOFF; |
| 3384 | |
| 3385 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3386 | if (lastx > 0) { |
| 3387 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3388 | } |
| 3389 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3390 | xfs_bmbt_get_all(ep, gotp); |
| 3391 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3392 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3393 | if (lastx > 0) { |
| 3394 | *gotp = *prevp; |
| 3395 | } |
| 3396 | *eofp = 1; |
| 3397 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3398 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3399 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3400 | return ep; |
| 3401 | } |
| 3402 | |
| 3403 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | * Search the extents list for the inode, for the extent containing bno. |
| 3405 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3406 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3407 | * Else, *lastxp will be set to the index of the found |
| 3408 | * entry; *gotp will contain the entry. |
| 3409 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3410 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | xfs_bmap_search_extents( |
| 3412 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3413 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3414 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | int *eofp, /* out: end of file found */ |
| 3416 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3417 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3418 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3419 | { |
| 3420 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3421 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | |
| 3423 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3424 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3426 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3427 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3428 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3429 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3430 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3431 | "Access to block zero in inode %llu " |
| 3432 | "start_block: %llx start_off: %llx " |
| 3433 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3434 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3435 | (unsigned long long)gotp->br_startblock, |
| 3436 | (unsigned long long)gotp->br_startoff, |
| 3437 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3438 | gotp->br_state, *lastxp); |
| 3439 | *lastxp = NULLEXTNUM; |
| 3440 | *eofp = 1; |
| 3441 | return NULL; |
| 3442 | } |
| 3443 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | /* |
| 3447 | * Compute the worst-case number of indirect blocks that will be used |
| 3448 | * for ip's delayed extent of length "len". |
| 3449 | */ |
| 3450 | STATIC xfs_filblks_t |
| 3451 | xfs_bmap_worst_indlen( |
| 3452 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3453 | xfs_filblks_t len) /* delayed extent length */ |
| 3454 | { |
| 3455 | int level; /* btree level number */ |
| 3456 | int maxrecs; /* maximum record count at this level */ |
| 3457 | xfs_mount_t *mp; /* mount structure */ |
| 3458 | xfs_filblks_t rval; /* return value */ |
| 3459 | |
| 3460 | mp = ip->i_mount; |
| 3461 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3462 | for (level = 0, rval = 0; |
| 3463 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3464 | level++) { |
| 3465 | len += maxrecs - 1; |
| 3466 | do_div(len, maxrecs); |
| 3467 | rval += len; |
| 3468 | if (len == 1) |
| 3469 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3470 | level - 1; |
| 3471 | if (level == 0) |
| 3472 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3473 | } |
| 3474 | return rval; |
| 3475 | } |
| 3476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | /* |
| 3478 | * Convert inode from non-attributed to attributed. |
| 3479 | * Must not be in a transaction, ip must not be locked. |
| 3480 | */ |
| 3481 | int /* error code */ |
| 3482 | xfs_bmap_add_attrfork( |
| 3483 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3484 | int size, /* space new attribute needs */ |
| 3485 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3488 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3491 | int blks; /* space reservation */ |
| 3492 | int version = 1; /* superblock attr version */ |
| 3493 | int committed; /* xaction was committed */ |
| 3494 | int logflags; /* logging flags */ |
| 3495 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3497 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | ASSERT(ip->i_df.if_ext_max == |
| 3499 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | mp = ip->i_mount; |
| 3502 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3503 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3504 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3505 | if (rsvd) |
| 3506 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3507 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3508 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3509 | goto error0; |
| 3510 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3511 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3513 | XFS_QMOPT_RES_REGBLKS); |
| 3514 | if (error) { |
| 3515 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3516 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3517 | return error; |
| 3518 | } |
| 3519 | if (XFS_IFORK_Q(ip)) |
| 3520 | goto error1; |
| 3521 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3522 | /* |
| 3523 | * For inodes coming from pre-6.2 filesystems. |
| 3524 | */ |
| 3525 | ASSERT(ip->i_d.di_aformat == 0); |
| 3526 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3527 | } |
| 3528 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3529 | |
| 3530 | xfs_trans_ijoin_ref(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | switch (ip->i_d.di_format) { |
| 3534 | case XFS_DINODE_FMT_DEV: |
| 3535 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3536 | break; |
| 3537 | case XFS_DINODE_FMT_UUID: |
| 3538 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3539 | break; |
| 3540 | case XFS_DINODE_FMT_LOCAL: |
| 3541 | case XFS_DINODE_FMT_EXTENTS: |
| 3542 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3543 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3544 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3545 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3546 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3547 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | break; |
| 3549 | default: |
| 3550 | ASSERT(0); |
| 3551 | error = XFS_ERROR(EINVAL); |
| 3552 | goto error1; |
| 3553 | } |
| 3554 | ip->i_df.if_ext_max = |
| 3555 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3556 | ASSERT(ip->i_afp == NULL); |
| 3557 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
| 3558 | ip->i_afp->if_ext_max = |
| 3559 | XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t); |
| 3560 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3561 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3562 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | switch (ip->i_d.di_format) { |
| 3564 | case XFS_DINODE_FMT_LOCAL: |
| 3565 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3566 | &logflags); |
| 3567 | break; |
| 3568 | case XFS_DINODE_FMT_EXTENTS: |
| 3569 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3570 | &flist, &logflags); |
| 3571 | break; |
| 3572 | case XFS_DINODE_FMT_BTREE: |
| 3573 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3574 | &logflags); |
| 3575 | break; |
| 3576 | default: |
| 3577 | error = 0; |
| 3578 | break; |
| 3579 | } |
| 3580 | if (logflags) |
| 3581 | xfs_trans_log_inode(tp, ip, logflags); |
| 3582 | if (error) |
| 3583 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3584 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3585 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3586 | __int64_t sbfields = 0; |
| 3587 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3588 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3589 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3590 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3591 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3592 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3593 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3594 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3595 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3596 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3597 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3598 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3599 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3601 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | } |
Eric Sandeen | f7c99b6 | 2007-02-10 18:37:16 +1100 | [diff] [blame] | 3603 | if ((error = xfs_bmap_finish(&tp, &flist, &committed))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3604 | goto error2; |
Dave Chinner | 713bf88 | 2010-03-08 11:26:23 +1100 | [diff] [blame] | 3605 | error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | ASSERT(ip->i_df.if_ext_max == |
| 3607 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3608 | return error; |
| 3609 | error2: |
| 3610 | xfs_bmap_cancel(&flist); |
| 3611 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3613 | error0: |
| 3614 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
| 3615 | ASSERT(ip->i_df.if_ext_max == |
| 3616 | XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 3617 | return error; |
| 3618 | } |
| 3619 | |
| 3620 | /* |
| 3621 | * Add the extent to the list of extents to be free at transaction end. |
| 3622 | * The list is maintained sorted (by block number). |
| 3623 | */ |
| 3624 | /* ARGSUSED */ |
| 3625 | void |
| 3626 | xfs_bmap_add_free( |
| 3627 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3628 | xfs_filblks_t len, /* length of extent */ |
| 3629 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3630 | xfs_mount_t *mp) /* mount point structure */ |
| 3631 | { |
| 3632 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3633 | xfs_bmap_free_item_t *new; /* new element */ |
| 3634 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3635 | #ifdef DEBUG |
| 3636 | xfs_agnumber_t agno; |
| 3637 | xfs_agblock_t agbno; |
| 3638 | |
| 3639 | ASSERT(bno != NULLFSBLOCK); |
| 3640 | ASSERT(len > 0); |
| 3641 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3642 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3644 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3645 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3646 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3647 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3648 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3649 | #endif |
| 3650 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3651 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3652 | new->xbfi_startblock = bno; |
| 3653 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3654 | for (prev = NULL, cur = flist->xbf_first; |
| 3655 | cur != NULL; |
| 3656 | prev = cur, cur = cur->xbfi_next) { |
| 3657 | if (cur->xbfi_startblock >= bno) |
| 3658 | break; |
| 3659 | } |
| 3660 | if (prev) |
| 3661 | prev->xbfi_next = new; |
| 3662 | else |
| 3663 | flist->xbf_first = new; |
| 3664 | new->xbfi_next = cur; |
| 3665 | flist->xbf_count++; |
| 3666 | } |
| 3667 | |
| 3668 | /* |
| 3669 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3670 | * in this filesystem. Done once, during mount. |
| 3671 | */ |
| 3672 | void |
| 3673 | xfs_bmap_compute_maxlevels( |
| 3674 | xfs_mount_t *mp, /* file system mount structure */ |
| 3675 | int whichfork) /* data or attr fork */ |
| 3676 | { |
| 3677 | int level; /* btree level */ |
| 3678 | uint maxblocks; /* max blocks at this level */ |
| 3679 | uint maxleafents; /* max leaf entries possible */ |
| 3680 | int maxrootrecs; /* max records in root block */ |
| 3681 | int minleafrecs; /* min records in leaf block */ |
| 3682 | int minnoderecs; /* min records in node block */ |
| 3683 | int sz; /* root block size */ |
| 3684 | |
| 3685 | /* |
| 3686 | * The maximum number of extents in a file, hence the maximum |
| 3687 | * number of leaf entries, is controlled by the type of di_nextents |
| 3688 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3689 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3690 | * |
| 3691 | * 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] | 3692 | * the fork offset of all the inodes will be |
| 3693 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3694 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3695 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3696 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3697 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3699 | if (whichfork == XFS_DATA_FORK) { |
| 3700 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3701 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3702 | } else { |
| 3703 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3704 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3705 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3706 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3708 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3710 | for (level = 1; maxblocks > 1; level++) { |
| 3711 | if (maxblocks <= maxrootrecs) |
| 3712 | maxblocks = 1; |
| 3713 | else |
| 3714 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3715 | } |
| 3716 | mp->m_bm_maxlevels[whichfork] = level; |
| 3717 | } |
| 3718 | |
| 3719 | /* |
| 3720 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3721 | * caller. Frees all the extents that need freeing, which must be done |
| 3722 | * last due to locking considerations. We never free any extents in |
Christoph Hellwig | 859f57c | 2011-08-27 14:45:11 +0000 | [diff] [blame] | 3723 | * the first transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | * |
| 3725 | * Return 1 if the given transaction was committed and a new one |
| 3726 | * started, and 0 otherwise in the committed parameter. |
| 3727 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | int /* error */ |
| 3729 | xfs_bmap_finish( |
| 3730 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3731 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | int *committed) /* xact committed or not */ |
| 3733 | { |
| 3734 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3735 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3736 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3737 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | unsigned int logres; /* new log reservation */ |
| 3739 | unsigned int logcount; /* new log count */ |
| 3740 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3741 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3742 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3743 | |
| 3744 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3745 | if (flist->xbf_count == 0) { |
| 3746 | *committed = 0; |
| 3747 | return 0; |
| 3748 | } |
| 3749 | ntp = *tp; |
| 3750 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3751 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3752 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3753 | free->xbfi_blockcount); |
| 3754 | logres = ntp->t_log_res; |
| 3755 | logcount = ntp->t_log_count; |
| 3756 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3757 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | *tp = ntp; |
| 3759 | *committed = 1; |
| 3760 | /* |
| 3761 | * We have a new transaction, so we should return committed=1, |
| 3762 | * even though we're returning an error. |
| 3763 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3764 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3766 | |
| 3767 | /* |
| 3768 | * transaction commit worked ok so we can drop the extra ticket |
| 3769 | * reference that we gained in xfs_trans_dup() |
| 3770 | */ |
| 3771 | xfs_log_ticket_put(ntp->t_ticket); |
| 3772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 3774 | logcount))) |
| 3775 | return error; |
| 3776 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 3777 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 3778 | next = free->xbfi_next; |
| 3779 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 3780 | free->xbfi_blockcount))) { |
| 3781 | /* |
| 3782 | * The bmap free list will be cleaned up at a |
| 3783 | * higher level. The EFI will be canceled when |
| 3784 | * this transaction is aborted. |
| 3785 | * Need to force shutdown here to make sure it |
| 3786 | * happens, since this transaction may not be |
| 3787 | * dirty yet. |
| 3788 | */ |
| 3789 | mp = ntp->t_mountp; |
| 3790 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 3791 | xfs_force_shutdown(mp, |
| 3792 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3793 | SHUTDOWN_CORRUPT_INCORE : |
| 3794 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | return error; |
| 3796 | } |
| 3797 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 3798 | free->xbfi_blockcount); |
| 3799 | xfs_bmap_del_free(flist, NULL, free); |
| 3800 | } |
| 3801 | return 0; |
| 3802 | } |
| 3803 | |
| 3804 | /* |
| 3805 | * Free up any items left in the list. |
| 3806 | */ |
| 3807 | void |
| 3808 | xfs_bmap_cancel( |
| 3809 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 3810 | { |
| 3811 | xfs_bmap_free_item_t *free; /* free list item */ |
| 3812 | xfs_bmap_free_item_t *next; |
| 3813 | |
| 3814 | if (flist->xbf_count == 0) |
| 3815 | return; |
| 3816 | ASSERT(flist->xbf_first != NULL); |
| 3817 | for (free = flist->xbf_first; free; free = next) { |
| 3818 | next = free->xbfi_next; |
| 3819 | xfs_bmap_del_free(flist, NULL, free); |
| 3820 | } |
| 3821 | ASSERT(flist->xbf_count == 0); |
| 3822 | } |
| 3823 | |
| 3824 | /* |
| 3825 | * Returns the file-relative block number of the first unused block(s) |
| 3826 | * in the file with at least "len" logically contiguous blocks free. |
| 3827 | * This is the lowest-address hole if the file has holes, else the first block |
| 3828 | * past the end of file. |
| 3829 | * Return 0 if the file is currently local (in-inode). |
| 3830 | */ |
| 3831 | int /* error */ |
| 3832 | xfs_bmap_first_unused( |
| 3833 | xfs_trans_t *tp, /* transaction pointer */ |
| 3834 | xfs_inode_t *ip, /* incore inode */ |
| 3835 | xfs_extlen_t len, /* size of hole to find */ |
| 3836 | xfs_fileoff_t *first_unused, /* unused block */ |
| 3837 | int whichfork) /* data or attr fork */ |
| 3838 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3840 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3841 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3842 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 3843 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 3844 | xfs_fileoff_t max; /* starting useful block */ |
| 3845 | xfs_fileoff_t off; /* offset for this block */ |
| 3846 | xfs_extnum_t nextents; /* number of extent entries */ |
| 3847 | |
| 3848 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 3849 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 3850 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3851 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3852 | *first_unused = 0; |
| 3853 | return 0; |
| 3854 | } |
| 3855 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3856 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3857 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3858 | return error; |
| 3859 | lowest = *first_unused; |
| 3860 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3861 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3862 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3863 | off = xfs_bmbt_get_startoff(ep); |
| 3864 | /* |
| 3865 | * See if the hole before this extent will work. |
| 3866 | */ |
| 3867 | if (off >= lowest + len && off - max >= len) { |
| 3868 | *first_unused = max; |
| 3869 | return 0; |
| 3870 | } |
| 3871 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 3872 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 3873 | } |
| 3874 | *first_unused = max; |
| 3875 | return 0; |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * Returns the file-relative block number of the last block + 1 before |
| 3880 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3881 | * This is not based on i_size, it is based on the extent records. |
| 3882 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | */ |
| 3884 | int /* error */ |
| 3885 | xfs_bmap_last_before( |
| 3886 | xfs_trans_t *tp, /* transaction pointer */ |
| 3887 | xfs_inode_t *ip, /* incore inode */ |
| 3888 | xfs_fileoff_t *last_block, /* last block */ |
| 3889 | int whichfork) /* data or attr fork */ |
| 3890 | { |
| 3891 | xfs_fileoff_t bno; /* input file offset */ |
| 3892 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3893 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | int error; /* error return value */ |
| 3895 | xfs_bmbt_irec_t got; /* current extent value */ |
| 3896 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3897 | xfs_extnum_t lastx; /* last extent used */ |
| 3898 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 3899 | |
| 3900 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 3901 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3902 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 3903 | return XFS_ERROR(EIO); |
| 3904 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3905 | *last_block = 0; |
| 3906 | return 0; |
| 3907 | } |
| 3908 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3909 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3910 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3911 | return error; |
| 3912 | bno = *last_block - 1; |
| 3913 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 3914 | &prev); |
| 3915 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 3916 | if (prev.br_startoff == NULLFILEOFF) |
| 3917 | *last_block = 0; |
| 3918 | else |
| 3919 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 3920 | } |
| 3921 | /* |
| 3922 | * Otherwise *last_block is already the right answer. |
| 3923 | */ |
| 3924 | return 0; |
| 3925 | } |
| 3926 | |
| 3927 | /* |
| 3928 | * 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] | 3929 | * the file. This is not based on i_size, it is based on the extent records. |
| 3930 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | */ |
| 3932 | int /* error */ |
| 3933 | xfs_bmap_last_offset( |
| 3934 | xfs_trans_t *tp, /* transaction pointer */ |
| 3935 | xfs_inode_t *ip, /* incore inode */ |
| 3936 | xfs_fileoff_t *last_block, /* last block */ |
| 3937 | int whichfork) /* data or attr fork */ |
| 3938 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3939 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | int error; /* error return value */ |
| 3941 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3942 | xfs_extnum_t nextents; /* number of extent entries */ |
| 3943 | |
| 3944 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 3945 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3946 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 3947 | return XFS_ERROR(EIO); |
| 3948 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3949 | *last_block = 0; |
| 3950 | return 0; |
| 3951 | } |
| 3952 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3953 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3954 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3955 | return error; |
| 3956 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 3957 | if (!nextents) { |
| 3958 | *last_block = 0; |
| 3959 | return 0; |
| 3960 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3961 | ep = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep); |
| 3963 | return 0; |
| 3964 | } |
| 3965 | |
| 3966 | /* |
| 3967 | * Returns whether the selected fork of the inode has exactly one |
| 3968 | * block or not. For the data fork we check this matches di_size, |
| 3969 | * implying the file's range is 0..bsize-1. |
| 3970 | */ |
| 3971 | int /* 1=>1 block, 0=>otherwise */ |
| 3972 | xfs_bmap_one_block( |
| 3973 | xfs_inode_t *ip, /* incore inode */ |
| 3974 | int whichfork) /* data or attr fork */ |
| 3975 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3976 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3978 | int rval; /* return value */ |
| 3979 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 3980 | |
| 3981 | #ifndef DEBUG |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3982 | if (whichfork == XFS_DATA_FORK) { |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3983 | return S_ISREG(ip->i_d.di_mode) ? |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3984 | (ip->i_size == ip->i_mount->m_sb.sb_blocksize) : |
| 3985 | (ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize); |
| 3986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | #endif /* !DEBUG */ |
| 3988 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 3989 | return 0; |
| 3990 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 3991 | return 0; |
| 3992 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3993 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3994 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | xfs_bmbt_get_all(ep, &s); |
| 3996 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 3997 | if (rval && whichfork == XFS_DATA_FORK) |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 3998 | ASSERT(ip->i_size == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | return rval; |
| 4000 | } |
| 4001 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4002 | STATIC int |
| 4003 | xfs_bmap_sanity_check( |
| 4004 | struct xfs_mount *mp, |
| 4005 | struct xfs_buf *bp, |
| 4006 | int level) |
| 4007 | { |
| 4008 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4009 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 4010 | if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) || |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4011 | be16_to_cpu(block->bb_level) != level || |
| 4012 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4013 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4014 | return 0; |
| 4015 | return 1; |
| 4016 | } |
| 4017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | /* |
| 4019 | * Read in the extents to if_extents. |
| 4020 | * All inode fields are set up by caller, we just traverse the btree |
| 4021 | * and copy the records in. If the file system cannot contain unwritten |
| 4022 | * extents, the records are checked for no "state" flags. |
| 4023 | */ |
| 4024 | int /* error */ |
| 4025 | xfs_bmap_read_extents( |
| 4026 | xfs_trans_t *tp, /* transaction pointer */ |
| 4027 | xfs_inode_t *ip, /* incore inode */ |
| 4028 | int whichfork) /* data or attr fork */ |
| 4029 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4030 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4032 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4033 | int error; /* error return value */ |
| 4034 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4036 | xfs_ifork_t *ifp; /* fork structure */ |
| 4037 | int level; /* btree level, for checking */ |
| 4038 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4039 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | /* REFERENCED */ |
| 4041 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | |
| 4043 | bno = NULLFSBLOCK; |
| 4044 | mp = ip->i_mount; |
| 4045 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4046 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4047 | XFS_EXTFMT_INODE(ip); |
| 4048 | block = ifp->if_broot; |
| 4049 | /* |
| 4050 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4051 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4052 | level = be16_to_cpu(block->bb_level); |
| 4053 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4054 | 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] | 4055 | bno = be64_to_cpu(*pp); |
| 4056 | ASSERT(bno != NULLDFSBNO); |
| 4057 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4058 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | /* |
| 4060 | * Go down the tree until leaf level is reached, following the first |
| 4061 | * pointer (leftmost) at each level. |
| 4062 | */ |
| 4063 | while (level-- > 0) { |
| 4064 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4065 | XFS_BMAP_BTREE_REF))) |
| 4066 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4067 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4069 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | error0); |
| 4071 | if (level == 0) |
| 4072 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4073 | 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] | 4074 | bno = be64_to_cpu(*pp); |
| 4075 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | xfs_trans_brelse(tp, bp); |
| 4077 | } |
| 4078 | /* |
| 4079 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4080 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4081 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | i = 0; |
| 4083 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4084 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | */ |
| 4086 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4087 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | xfs_fsblock_t nextbno; |
| 4089 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4090 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | |
| 4092 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4093 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | if (unlikely(i + num_recs > room)) { |
| 4095 | ASSERT(i + num_recs <= room); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4096 | xfs_warn(ip->i_mount, |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4097 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | (unsigned long long) ip->i_ino); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4099 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 4100 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | goto error0; |
| 4102 | } |
| 4103 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4104 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | error0); |
| 4106 | /* |
| 4107 | * Read-ahead the next leaf block, if any. |
| 4108 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4109 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | if (nextbno != NULLFSBLOCK) |
| 4111 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4112 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4113 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4115 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4116 | start = i; |
| 4117 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4118 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4119 | trp->l0 = be64_to_cpu(frp->l0); |
| 4120 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | } |
| 4122 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4123 | /* |
| 4124 | * Check all attribute bmap btree records and |
| 4125 | * any "older" data bmap btree records for a |
| 4126 | * set bit in the "extent flag" position. |
| 4127 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4128 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4129 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4131 | XFS_ERRLEVEL_LOW, |
| 4132 | ip->i_mount); |
| 4133 | goto error0; |
| 4134 | } |
| 4135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | xfs_trans_brelse(tp, bp); |
| 4137 | bno = nextbno; |
| 4138 | /* |
| 4139 | * If we've reached the end, stop. |
| 4140 | */ |
| 4141 | if (bno == NULLFSBLOCK) |
| 4142 | break; |
| 4143 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4144 | XFS_BMAP_BTREE_REF))) |
| 4145 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4146 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4148 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4150 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | return 0; |
| 4152 | error0: |
| 4153 | xfs_trans_brelse(tp, bp); |
| 4154 | return XFS_ERROR(EFSCORRUPTED); |
| 4155 | } |
| 4156 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4157 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4159 | * 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] | 4160 | */ |
| 4161 | void |
| 4162 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4164 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4165 | int whichfork, /* data or attr fork */ |
| 4166 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4168 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4170 | int state = 0; |
| 4171 | |
| 4172 | if (whichfork == XFS_ATTR_FORK) |
| 4173 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | |
| 4175 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4176 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4177 | for (idx = 0; idx < cnt; idx++) |
| 4178 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | /* |
| 4182 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4183 | * given the callers original parameters. Specifically check the |
| 4184 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4185 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4186 | */ |
| 4187 | STATIC void |
| 4188 | xfs_bmap_validate_ret( |
| 4189 | xfs_fileoff_t bno, |
| 4190 | xfs_filblks_t len, |
| 4191 | int flags, |
| 4192 | xfs_bmbt_irec_t *mval, |
| 4193 | int nmap, |
| 4194 | int ret_nmap) |
| 4195 | { |
| 4196 | int i; /* index to map values */ |
| 4197 | |
| 4198 | ASSERT(ret_nmap <= nmap); |
| 4199 | |
| 4200 | for (i = 0; i < ret_nmap; i++) { |
| 4201 | ASSERT(mval[i].br_blockcount > 0); |
| 4202 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4203 | ASSERT(mval[i].br_startoff >= bno); |
| 4204 | ASSERT(mval[i].br_blockcount <= len); |
| 4205 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4206 | bno + len); |
| 4207 | } else { |
| 4208 | ASSERT(mval[i].br_startoff < bno + len); |
| 4209 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4210 | bno); |
| 4211 | } |
| 4212 | ASSERT(i == 0 || |
| 4213 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4214 | mval[i].br_startoff); |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4215 | if (flags & XFS_BMAPI_WRITE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4217 | mval[i].br_startblock != HOLESTARTBLOCK); |
| 4218 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4219 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4220 | } |
| 4221 | } |
| 4222 | #endif /* DEBUG */ |
| 4223 | |
| 4224 | |
| 4225 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4226 | * Trim the returned map to the required bounds |
| 4227 | */ |
| 4228 | STATIC void |
| 4229 | xfs_bmapi_trim_map( |
| 4230 | struct xfs_bmbt_irec *mval, |
| 4231 | struct xfs_bmbt_irec *got, |
| 4232 | xfs_fileoff_t *bno, |
| 4233 | xfs_filblks_t len, |
| 4234 | xfs_fileoff_t obno, |
| 4235 | xfs_fileoff_t end, |
| 4236 | int n, |
| 4237 | int flags) |
| 4238 | { |
| 4239 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 4240 | got->br_startoff + got->br_blockcount <= obno) { |
| 4241 | *mval = *got; |
| 4242 | if (isnullstartblock(got->br_startblock)) |
| 4243 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4244 | return; |
| 4245 | } |
| 4246 | |
| 4247 | if (obno > *bno) |
| 4248 | *bno = obno; |
| 4249 | ASSERT((*bno >= obno) || (n == 0)); |
| 4250 | ASSERT(*bno < end); |
| 4251 | mval->br_startoff = *bno; |
| 4252 | if (isnullstartblock(got->br_startblock)) |
| 4253 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4254 | else |
| 4255 | mval->br_startblock = got->br_startblock + |
| 4256 | (*bno - got->br_startoff); |
| 4257 | /* |
| 4258 | * Return the minimum of what we got and what we asked for for |
| 4259 | * the length. We can use the len variable here because it is |
| 4260 | * modified below and we could have been there before coming |
| 4261 | * here if the first part of the allocation didn't overlap what |
| 4262 | * was asked for. |
| 4263 | */ |
| 4264 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 4265 | got->br_blockcount - (*bno - got->br_startoff)); |
| 4266 | mval->br_state = got->br_state; |
| 4267 | ASSERT(mval->br_blockcount <= len); |
| 4268 | return; |
| 4269 | } |
| 4270 | |
| 4271 | /* |
| 4272 | * Update and validate the extent map to return |
| 4273 | */ |
| 4274 | STATIC void |
| 4275 | xfs_bmapi_update_map( |
| 4276 | struct xfs_bmbt_irec **map, |
| 4277 | xfs_fileoff_t *bno, |
| 4278 | xfs_filblks_t *len, |
| 4279 | xfs_fileoff_t obno, |
| 4280 | xfs_fileoff_t end, |
| 4281 | int *n, |
| 4282 | int flags) |
| 4283 | { |
| 4284 | xfs_bmbt_irec_t *mval = *map; |
| 4285 | |
| 4286 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4287 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 4288 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 4289 | (mval->br_startoff < obno)); |
| 4290 | |
| 4291 | *bno = mval->br_startoff + mval->br_blockcount; |
| 4292 | *len = end - *bno; |
| 4293 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4294 | /* update previous map with new information */ |
| 4295 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4296 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4297 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4298 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4299 | mval[-1].br_state = mval->br_state; |
| 4300 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4301 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4302 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4303 | mval->br_startblock == mval[-1].br_startblock + |
| 4304 | mval[-1].br_blockcount && |
| 4305 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4306 | mval[-1].br_state == mval->br_state)) { |
| 4307 | ASSERT(mval->br_startoff == |
| 4308 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4309 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4310 | } else if (*n > 0 && |
| 4311 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4312 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4313 | mval->br_startoff == |
| 4314 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4315 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4316 | mval[-1].br_state = mval->br_state; |
| 4317 | } else if (!((*n == 0) && |
| 4318 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4319 | obno))) { |
| 4320 | mval++; |
| 4321 | (*n)++; |
| 4322 | } |
| 4323 | *map = mval; |
| 4324 | } |
| 4325 | |
| 4326 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4327 | * Map file blocks to filesystem blocks without allocation. |
| 4328 | */ |
| 4329 | int |
| 4330 | xfs_bmapi_read( |
| 4331 | struct xfs_inode *ip, |
| 4332 | xfs_fileoff_t bno, |
| 4333 | xfs_filblks_t len, |
| 4334 | struct xfs_bmbt_irec *mval, |
| 4335 | int *nmap, |
| 4336 | int flags) |
| 4337 | { |
| 4338 | struct xfs_mount *mp = ip->i_mount; |
| 4339 | struct xfs_ifork *ifp; |
| 4340 | struct xfs_bmbt_irec got; |
| 4341 | struct xfs_bmbt_irec prev; |
| 4342 | xfs_fileoff_t obno; |
| 4343 | xfs_fileoff_t end; |
| 4344 | xfs_extnum_t lastx; |
| 4345 | int error; |
| 4346 | int eof; |
| 4347 | int n = 0; |
| 4348 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4349 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4350 | |
| 4351 | ASSERT(*nmap >= 1); |
| 4352 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 4353 | XFS_BMAPI_IGSTATE))); |
| 4354 | |
| 4355 | if (unlikely(XFS_TEST_ERROR( |
| 4356 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4357 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4358 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4359 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
| 4360 | return XFS_ERROR(EFSCORRUPTED); |
| 4361 | } |
| 4362 | |
| 4363 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4364 | return XFS_ERROR(EIO); |
| 4365 | |
| 4366 | XFS_STATS_INC(xs_blk_mapr); |
| 4367 | |
| 4368 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4369 | ASSERT(ifp->if_ext_max == |
| 4370 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4371 | |
| 4372 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4373 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4374 | if (error) |
| 4375 | return error; |
| 4376 | } |
| 4377 | |
| 4378 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4379 | end = bno + len; |
| 4380 | obno = bno; |
| 4381 | |
| 4382 | while (bno < end && n < *nmap) { |
| 4383 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4384 | if (eof) |
| 4385 | got.br_startoff = end; |
| 4386 | if (got.br_startoff > bno) { |
| 4387 | /* Reading in a hole. */ |
| 4388 | mval->br_startoff = bno; |
| 4389 | mval->br_startblock = HOLESTARTBLOCK; |
| 4390 | mval->br_blockcount = |
| 4391 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4392 | mval->br_state = XFS_EXT_NORM; |
| 4393 | bno += mval->br_blockcount; |
| 4394 | len -= mval->br_blockcount; |
| 4395 | mval++; |
| 4396 | n++; |
| 4397 | continue; |
| 4398 | } |
| 4399 | |
| 4400 | /* set up the extent map to return. */ |
| 4401 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4402 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4403 | |
| 4404 | /* If we're done, stop now. */ |
| 4405 | if (bno >= end || n >= *nmap) |
| 4406 | break; |
| 4407 | |
| 4408 | /* Else go on to the next record. */ |
| 4409 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4410 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4411 | else |
| 4412 | eof = 1; |
| 4413 | } |
| 4414 | *nmap = n; |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4418 | STATIC int |
| 4419 | xfs_bmapi_reserve_delalloc( |
| 4420 | struct xfs_inode *ip, |
| 4421 | xfs_fileoff_t aoff, |
| 4422 | xfs_filblks_t len, |
| 4423 | struct xfs_bmbt_irec *got, |
| 4424 | struct xfs_bmbt_irec *prev, |
| 4425 | xfs_extnum_t *lastx, |
| 4426 | int eof) |
| 4427 | { |
| 4428 | struct xfs_mount *mp = ip->i_mount; |
| 4429 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4430 | xfs_extlen_t alen; |
| 4431 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4432 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4433 | xfs_extlen_t extsz; |
| 4434 | int error; |
| 4435 | |
| 4436 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4437 | if (!eof) |
| 4438 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4439 | |
| 4440 | /* Figure out the extent size, adjust alen */ |
| 4441 | extsz = xfs_get_extsz_hint(ip); |
| 4442 | if (extsz) { |
| 4443 | /* |
| 4444 | * Make sure we don't exceed a single extent length when we |
| 4445 | * align the extent by reducing length we are going to |
| 4446 | * allocate by the maximum amount extent size aligment may |
| 4447 | * require. |
| 4448 | */ |
| 4449 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); |
| 4450 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4451 | 1, 0, &aoff, &alen); |
| 4452 | ASSERT(!error); |
| 4453 | } |
| 4454 | |
| 4455 | if (rt) |
| 4456 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4457 | |
| 4458 | /* |
| 4459 | * Make a transaction-less quota reservation for delayed allocation |
| 4460 | * blocks. This number gets adjusted later. We return if we haven't |
| 4461 | * allocated blocks already inside this loop. |
| 4462 | */ |
| 4463 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4464 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4465 | if (error) |
| 4466 | return error; |
| 4467 | |
| 4468 | /* |
| 4469 | * Split changing sb for alen and indlen since they could be coming |
| 4470 | * from different places. |
| 4471 | */ |
| 4472 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4473 | ASSERT(indlen > 0); |
| 4474 | |
| 4475 | if (rt) { |
| 4476 | error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
| 4477 | -((int64_t)extsz), 0); |
| 4478 | } else { |
| 4479 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4480 | -((int64_t)alen), 0); |
| 4481 | } |
| 4482 | |
| 4483 | if (error) |
| 4484 | goto out_unreserve_quota; |
| 4485 | |
| 4486 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4487 | -((int64_t)indlen), 0); |
| 4488 | if (error) |
| 4489 | goto out_unreserve_blocks; |
| 4490 | |
| 4491 | |
| 4492 | ip->i_delayed_blks += alen; |
| 4493 | |
| 4494 | got->br_startoff = aoff; |
| 4495 | got->br_startblock = nullstartblock(indlen); |
| 4496 | got->br_blockcount = alen; |
| 4497 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 4498 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4499 | |
| 4500 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame^] | 4501 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4502 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4503 | */ |
| 4504 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4505 | |
| 4506 | ASSERT(got->br_startoff <= aoff); |
| 4507 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4508 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4509 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4510 | return 0; |
| 4511 | |
| 4512 | out_unreserve_blocks: |
| 4513 | if (rt) |
| 4514 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0); |
| 4515 | else |
| 4516 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0); |
| 4517 | out_unreserve_quota: |
| 4518 | if (XFS_IS_QUOTA_ON(mp)) |
| 4519 | xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ? |
| 4520 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4521 | return error; |
| 4522 | } |
| 4523 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4524 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4525 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4526 | */ |
| 4527 | int |
| 4528 | xfs_bmapi_delay( |
| 4529 | struct xfs_inode *ip, /* incore inode */ |
| 4530 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4531 | xfs_filblks_t len, /* length to map in file */ |
| 4532 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4533 | int *nmap, /* i/o: mval size/count */ |
| 4534 | int flags) /* XFS_BMAPI_... */ |
| 4535 | { |
| 4536 | struct xfs_mount *mp = ip->i_mount; |
| 4537 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4538 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4539 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4540 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4541 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4542 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4543 | int eof; /* we've hit the end of extents */ |
| 4544 | int n = 0; /* current extent index */ |
| 4545 | int error = 0; |
| 4546 | |
| 4547 | ASSERT(*nmap >= 1); |
| 4548 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4549 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
| 4550 | |
| 4551 | if (unlikely(XFS_TEST_ERROR( |
| 4552 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4553 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4554 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4555 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
| 4556 | return XFS_ERROR(EFSCORRUPTED); |
| 4557 | } |
| 4558 | |
| 4559 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4560 | return XFS_ERROR(EIO); |
| 4561 | |
| 4562 | XFS_STATS_INC(xs_blk_mapw); |
| 4563 | |
| 4564 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4565 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4566 | if (error) |
| 4567 | return error; |
| 4568 | } |
| 4569 | |
| 4570 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4571 | end = bno + len; |
| 4572 | obno = bno; |
| 4573 | |
| 4574 | while (bno < end && n < *nmap) { |
| 4575 | if (eof || got.br_startoff > bno) { |
| 4576 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4577 | &prev, &lastx, eof); |
| 4578 | if (error) { |
| 4579 | if (n == 0) { |
| 4580 | *nmap = 0; |
| 4581 | return error; |
| 4582 | } |
| 4583 | break; |
| 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | /* set up the extent map to return. */ |
| 4588 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4589 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4590 | |
| 4591 | /* If we're done, stop now. */ |
| 4592 | if (bno >= end || n >= *nmap) |
| 4593 | break; |
| 4594 | |
| 4595 | /* Else go on to the next record. */ |
| 4596 | prev = got; |
| 4597 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4598 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4599 | else |
| 4600 | eof = 1; |
| 4601 | } |
| 4602 | |
| 4603 | *nmap = n; |
| 4604 | return 0; |
| 4605 | } |
| 4606 | |
| 4607 | |
| 4608 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | * Map file blocks to filesystem blocks. |
| 4610 | * File range is given by the bno/len pair. |
| 4611 | * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set) |
| 4612 | * into a hole or past eof. |
| 4613 | * Only allocates blocks from a single allocation group, |
| 4614 | * to avoid locking problems. |
| 4615 | * The returned value in "firstblock" from the first call in a transaction |
| 4616 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4617 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4618 | * the first call in "total"; if no allocation group has that many free |
| 4619 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4620 | */ |
| 4621 | int /* error */ |
| 4622 | xfs_bmapi( |
| 4623 | xfs_trans_t *tp, /* transaction pointer */ |
| 4624 | xfs_inode_t *ip, /* incore inode */ |
| 4625 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4626 | xfs_filblks_t len, /* length to map in file */ |
| 4627 | int flags, /* XFS_BMAPI_... */ |
| 4628 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4629 | controls a.g. for allocs */ |
| 4630 | xfs_extlen_t total, /* total blocks needed */ |
| 4631 | xfs_bmbt_irec_t *mval, /* output: map values */ |
| 4632 | int *nmap, /* i/o: mval size/count */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4633 | xfs_bmap_free_t *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | { |
| 4635 | xfs_fsblock_t abno; /* allocated block number */ |
| 4636 | xfs_extlen_t alen; /* allocated extent length */ |
| 4637 | xfs_fileoff_t aoff; /* allocated file offset */ |
Dave Chinner | f0a0eaa | 2010-01-20 10:50:06 +1100 | [diff] [blame] | 4638 | xfs_bmalloca_t bma = { 0 }; /* args for xfs_bmap_alloc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | xfs_fileoff_t end; /* end of mapped file region */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4641 | int eof; /* we've hit the end of extents */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4642 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | int error; /* error return */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4644 | xfs_bmbt_irec_t got; /* current file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4647 | int logflags; /* flags for transaction logging */ |
| 4648 | xfs_extlen_t minleft; /* min blocks left after allocation */ |
| 4649 | xfs_extlen_t minlen; /* min allocation size */ |
| 4650 | xfs_mount_t *mp; /* xfs mount structure */ |
| 4651 | int n; /* current extent index */ |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 4652 | int nallocs; /* number of extents alloc'd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | xfs_fileoff_t obno; /* old block number (offset) */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4654 | xfs_bmbt_irec_t prev; /* previous file extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | int tmp_logflags; /* temp flags holder */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4656 | int whichfork; /* data or attr fork */ |
| 4657 | char inhole; /* current location is hole in file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4658 | char wasdelay; /* old extent was delayed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | char wr; /* this is a write request */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 4660 | char rt; /* this is a realtime file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | #ifdef DEBUG |
| 4662 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4663 | int orig_flags; /* original flags arg value */ |
| 4664 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4665 | xfs_bmbt_irec_t *orig_mval; /* original value of mval */ |
| 4666 | int orig_nmap; /* original value of *nmap */ |
| 4667 | |
| 4668 | orig_bno = bno; |
| 4669 | orig_len = len; |
| 4670 | orig_flags = flags; |
| 4671 | orig_mval = mval; |
| 4672 | orig_nmap = *nmap; |
| 4673 | #endif |
| 4674 | ASSERT(*nmap >= 1); |
| 4675 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE)); |
| 4676 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4677 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4678 | mp = ip->i_mount; |
| 4679 | if (unlikely(XFS_TEST_ERROR( |
| 4680 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4681 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4682 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4683 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4684 | XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp); |
| 4685 | return XFS_ERROR(EFSCORRUPTED); |
| 4686 | } |
| 4687 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4688 | return XFS_ERROR(EIO); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 4689 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4691 | ASSERT(ifp->if_ext_max == |
| 4692 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4693 | if ((wr = (flags & XFS_BMAPI_WRITE)) != 0) |
| 4694 | XFS_STATS_INC(xs_blk_mapw); |
| 4695 | else |
| 4696 | XFS_STATS_INC(xs_blk_mapr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | /* |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4698 | * IGSTATE flag is used to combine extents which |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | * differ only due to the state of the extents. |
| 4700 | * This technique is used from xfs_getbmap() |
| 4701 | * when the caller does not wish to see the |
| 4702 | * separation (which is the default). |
| 4703 | * |
| 4704 | * This technique is also used when writing a |
| 4705 | * buffer which has been partially written, |
| 4706 | * (usually by being flushed during a chunkread), |
| 4707 | * to ensure one write takes place. This also |
| 4708 | * prevents a change in the xfs inode extents at |
| 4709 | * this time, intentionally. This change occurs |
| 4710 | * on completion of the write operation, in |
| 4711 | * xfs_strat_comp(), where the xfs_bmapi() call |
| 4712 | * is transactioned, and the extents combined. |
| 4713 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4714 | if ((flags & XFS_BMAPI_IGSTATE) && wr) /* if writing unwritten space */ |
| 4715 | wr = 0; /* no allocations are allowed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | logflags = 0; |
| 4717 | nallocs = 0; |
| 4718 | cur = NULL; |
| 4719 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 4720 | ASSERT(wr && tp); |
| 4721 | if ((error = xfs_bmap_local_to_extents(tp, ip, |
| 4722 | firstblock, total, &logflags, whichfork))) |
| 4723 | goto error0; |
| 4724 | } |
| 4725 | if (wr && *firstblock == NULLFSBLOCK) { |
| 4726 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4727 | minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | else |
| 4729 | minleft = 1; |
| 4730 | } else |
| 4731 | minleft = 0; |
| 4732 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 4733 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 4734 | goto error0; |
| 4735 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 4736 | &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | n = 0; |
| 4738 | end = bno + len; |
| 4739 | obno = bno; |
| 4740 | bma.ip = NULL; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | while (bno < end && n < *nmap) { |
| 4743 | /* |
| 4744 | * Reading past eof, act as though there's a hole |
| 4745 | * up to end. |
| 4746 | */ |
| 4747 | if (eof && !wr) |
| 4748 | got.br_startoff = end; |
| 4749 | inhole = eof || got.br_startoff > bno; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4750 | wasdelay = wr && !inhole && isnullstartblock(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | /* |
| 4752 | * First, deal with the hole before the allocated space |
| 4753 | * that we found, if any. |
| 4754 | */ |
| 4755 | if (wr && (inhole || wasdelay)) { |
| 4756 | /* |
| 4757 | * For the wasdelay case, we could also just |
| 4758 | * allocate the stuff asked for in this bmap call |
| 4759 | * but that wouldn't be as good. |
| 4760 | */ |
Christoph Hellwig | cd8b0bb | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4761 | if (wasdelay) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | alen = (xfs_extlen_t)got.br_blockcount; |
| 4763 | aoff = got.br_startoff; |
| 4764 | if (lastx != NULLEXTNUM && lastx) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4765 | ep = xfs_iext_get_ext(ifp, lastx - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | xfs_bmbt_get_all(ep, &prev); |
| 4767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | } else { |
| 4769 | alen = (xfs_extlen_t) |
| 4770 | XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4771 | if (!eof) |
| 4772 | alen = (xfs_extlen_t) |
| 4773 | XFS_FILBLKS_MIN(alen, |
| 4774 | got.br_startoff - bno); |
| 4775 | aoff = bno; |
| 4776 | } |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4777 | minlen = (flags & XFS_BMAPI_CONTIG) ? alen : 1; |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4778 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | /* |
| 4780 | * If first time, allocate and fill in |
| 4781 | * once-only bma fields. |
| 4782 | */ |
| 4783 | if (bma.ip == NULL) { |
| 4784 | bma.tp = tp; |
| 4785 | bma.ip = ip; |
| 4786 | bma.prevp = &prev; |
| 4787 | bma.gotp = &got; |
| 4788 | bma.total = total; |
| 4789 | bma.userdata = 0; |
| 4790 | } |
| 4791 | /* Indicate if this is the first user data |
| 4792 | * in the file, or just any user data. |
| 4793 | */ |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4794 | if (!(flags & XFS_BMAPI_METADATA)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4795 | bma.userdata = (aoff == 0) ? |
| 4796 | XFS_ALLOC_INITIAL_USER_DATA : |
| 4797 | XFS_ALLOC_USERDATA; |
| 4798 | } |
| 4799 | /* |
| 4800 | * Fill in changeable bma fields. |
| 4801 | */ |
| 4802 | bma.eof = eof; |
| 4803 | bma.firstblock = *firstblock; |
| 4804 | bma.alen = alen; |
| 4805 | bma.off = aoff; |
Adrian Bunk | 7288026 | 2006-08-30 13:41:58 +1000 | [diff] [blame] | 4806 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | bma.wasdel = wasdelay; |
| 4808 | bma.minlen = minlen; |
| 4809 | bma.low = flist->xbf_low; |
| 4810 | bma.minleft = minleft; |
| 4811 | /* |
| 4812 | * Only want to do the alignment at the |
| 4813 | * eof if it is userdata and allocation length |
| 4814 | * is larger than a stripe unit. |
| 4815 | */ |
| 4816 | if (mp->m_dalign && alen >= mp->m_dalign && |
Nathan Scott | 39269e2 | 2006-03-14 13:33:50 +1100 | [diff] [blame] | 4817 | (!(flags & XFS_BMAPI_METADATA)) && |
| 4818 | (whichfork == XFS_DATA_FORK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | if ((error = xfs_bmap_isaeof(ip, aoff, |
| 4820 | whichfork, &bma.aeof))) |
| 4821 | goto error0; |
| 4822 | } else |
| 4823 | bma.aeof = 0; |
| 4824 | /* |
| 4825 | * Call allocator. |
| 4826 | */ |
| 4827 | if ((error = xfs_bmap_alloc(&bma))) |
| 4828 | goto error0; |
| 4829 | /* |
| 4830 | * Copy out result fields. |
| 4831 | */ |
| 4832 | abno = bma.rval; |
| 4833 | if ((flist->xbf_low = bma.low)) |
| 4834 | minleft = 0; |
| 4835 | alen = bma.alen; |
| 4836 | aoff = bma.off; |
| 4837 | ASSERT(*firstblock == NULLFSBLOCK || |
| 4838 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 4839 | XFS_FSB_TO_AGNO(mp, bma.firstblock) || |
| 4840 | (flist->xbf_low && |
| 4841 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 4842 | XFS_FSB_TO_AGNO(mp, bma.firstblock))); |
| 4843 | *firstblock = bma.firstblock; |
| 4844 | if (cur) |
| 4845 | cur->bc_private.b.firstblock = |
| 4846 | *firstblock; |
| 4847 | if (abno == NULLFSBLOCK) |
| 4848 | break; |
| 4849 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4850 | cur = xfs_bmbt_init_cursor(mp, tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | ip, whichfork); |
| 4852 | cur->bc_private.b.firstblock = |
| 4853 | *firstblock; |
| 4854 | cur->bc_private.b.flist = flist; |
| 4855 | } |
| 4856 | /* |
| 4857 | * Bump the number of extents we've allocated |
| 4858 | * in this call. |
| 4859 | */ |
| 4860 | nallocs++; |
| 4861 | } |
| 4862 | if (cur) |
| 4863 | cur->bc_private.b.flags = |
| 4864 | wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4865 | got.br_startoff = aoff; |
| 4866 | got.br_startblock = abno; |
| 4867 | got.br_blockcount = alen; |
| 4868 | got.br_state = XFS_EXT_NORM; /* assume normal */ |
| 4869 | /* |
| 4870 | * Determine state of extent, and the filesystem. |
| 4871 | * A wasdelay extent has been initialized, so |
| 4872 | * shouldn't be flagged as unwritten. |
| 4873 | */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 4874 | if (wr && xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | if (!wasdelay && (flags & XFS_BMAPI_PREALLOC)) |
| 4876 | got.br_state = XFS_EXT_UNWRITTEN; |
| 4877 | } |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 4878 | error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &got, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4879 | firstblock, flist, &tmp_logflags, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4880 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | logflags |= tmp_logflags; |
| 4882 | if (error) |
| 4883 | goto error0; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4884 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | xfs_bmbt_get_all(ep, &got); |
| 4886 | ASSERT(got.br_startoff <= aoff); |
| 4887 | ASSERT(got.br_startoff + got.br_blockcount >= |
| 4888 | aoff + alen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | ASSERT(got.br_state == XFS_EXT_NORM || |
| 4890 | got.br_state == XFS_EXT_UNWRITTEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | /* |
| 4892 | * Fall down into the found allocated space case. |
| 4893 | */ |
| 4894 | } else if (inhole) { |
| 4895 | /* |
| 4896 | * Reading in a hole. |
| 4897 | */ |
| 4898 | mval->br_startoff = bno; |
| 4899 | mval->br_startblock = HOLESTARTBLOCK; |
| 4900 | mval->br_blockcount = |
| 4901 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4902 | mval->br_state = XFS_EXT_NORM; |
| 4903 | bno += mval->br_blockcount; |
| 4904 | len -= mval->br_blockcount; |
| 4905 | mval++; |
| 4906 | n++; |
| 4907 | continue; |
| 4908 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4909 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4910 | /* Deal with the allocated space we found. */ |
| 4911 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | |
| 4913 | /* |
| 4914 | * Check if writing previously allocated but |
| 4915 | * unwritten extents. |
| 4916 | */ |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 4917 | if (wr && |
| 4918 | ((mval->br_state == XFS_EXT_UNWRITTEN && |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4919 | ((flags & XFS_BMAPI_PREALLOC) == 0)) || |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 4920 | (mval->br_state == XFS_EXT_NORM && |
| 4921 | ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT)) == |
| 4922 | (XFS_BMAPI_PREALLOC|XFS_BMAPI_CONVERT))))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | /* |
| 4924 | * Modify (by adding) the state flag, if writing. |
| 4925 | */ |
| 4926 | ASSERT(mval->br_blockcount <= len); |
| 4927 | if ((ifp->if_flags & XFS_IFBROOT) && !cur) { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 4928 | cur = xfs_bmbt_init_cursor(mp, |
| 4929 | tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | cur->bc_private.b.firstblock = |
| 4931 | *firstblock; |
| 4932 | cur->bc_private.b.flist = flist; |
| 4933 | } |
Dave Chinner | 4472235 | 2010-08-24 12:02:11 +1000 | [diff] [blame] | 4934 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4935 | ? XFS_EXT_NORM |
| 4936 | : XFS_EXT_UNWRITTEN; |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 4937 | error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, mval, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4938 | firstblock, flist, &tmp_logflags, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 4939 | whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4940 | logflags |= tmp_logflags; |
| 4941 | if (error) |
| 4942 | goto error0; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4943 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | xfs_bmbt_get_all(ep, &got); |
| 4945 | /* |
| 4946 | * We may have combined previously unwritten |
| 4947 | * space with written space, so generate |
| 4948 | * another request. |
| 4949 | */ |
| 4950 | if (mval->br_blockcount < len) |
| 4951 | continue; |
| 4952 | } |
| 4953 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4954 | /* update the extent map to return */ |
| 4955 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | /* |
| 4958 | * If we're done, stop now. Stop when we've allocated |
| 4959 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4960 | * the transaction may get too big. |
| 4961 | */ |
| 4962 | if (bno >= end || n >= *nmap || nallocs >= *nmap) |
| 4963 | break; |
| 4964 | /* |
| 4965 | * Else go on to the next record. |
| 4966 | */ |
David Chinner | 4e5ae83 | 2007-06-05 16:24:15 +1000 | [diff] [blame] | 4967 | prev = got; |
Christoph Hellwig | ecee76b | 2011-09-18 20:40:43 +0000 | [diff] [blame] | 4968 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4969 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4971 | } else { |
| 4972 | eof = 1; |
| 4973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4975 | *nmap = n; |
| 4976 | /* |
| 4977 | * Transform from btree to extents, give it cur. |
| 4978 | */ |
| 4979 | if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 4980 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 4981 | ASSERT(wr && cur); |
| 4982 | error = xfs_bmap_btree_to_extents(tp, ip, cur, |
| 4983 | &tmp_logflags, whichfork); |
| 4984 | logflags |= tmp_logflags; |
| 4985 | if (error) |
| 4986 | goto error0; |
| 4987 | } |
| 4988 | ASSERT(ifp->if_ext_max == |
| 4989 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 4990 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
| 4991 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max); |
| 4992 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | error0: |
| 4994 | /* |
| 4995 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4996 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 4998 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5000 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5001 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5003 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | /* |
| 5005 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5006 | * detecting a case where the data is changed, there's an error, |
| 5007 | * and it's not logged so we don't shutdown when we should. |
| 5008 | */ |
| 5009 | if (logflags) { |
| 5010 | ASSERT(tp && wr); |
| 5011 | xfs_trans_log_inode(tp, ip, logflags); |
| 5012 | } |
| 5013 | if (cur) { |
| 5014 | if (!error) { |
| 5015 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5016 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5017 | XFS_FSB_TO_AGNO(mp, |
| 5018 | cur->bc_private.b.firstblock) || |
| 5019 | (flist->xbf_low && |
| 5020 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5021 | XFS_FSB_TO_AGNO(mp, |
| 5022 | cur->bc_private.b.firstblock))); |
| 5023 | *firstblock = cur->bc_private.b.firstblock; |
| 5024 | } |
| 5025 | xfs_btree_del_cursor(cur, |
| 5026 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5027 | } |
| 5028 | if (!error) |
| 5029 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5030 | orig_nmap, *nmap); |
| 5031 | return error; |
| 5032 | } |
| 5033 | |
| 5034 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | * Unmap (remove) blocks from a file. |
| 5036 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5037 | * that value. If not all extents in the block range can be removed then |
| 5038 | * *done is set. |
| 5039 | */ |
| 5040 | int /* error */ |
| 5041 | xfs_bunmapi( |
| 5042 | xfs_trans_t *tp, /* transaction pointer */ |
| 5043 | struct xfs_inode *ip, /* incore inode */ |
| 5044 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5045 | xfs_filblks_t len, /* length to unmap in file */ |
| 5046 | int flags, /* misc flags */ |
| 5047 | xfs_extnum_t nexts, /* number of extents max */ |
| 5048 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5049 | controls a.g. for allocs */ |
| 5050 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 5051 | int *done) /* set if not done yet */ |
| 5052 | { |
| 5053 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5054 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5055 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5056 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | int error; /* error return value */ |
| 5058 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5059 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5061 | int isrt; /* freeing in rt area */ |
| 5062 | xfs_extnum_t lastx; /* last extent index used */ |
| 5063 | int logflags; /* transaction logging flags */ |
| 5064 | xfs_extlen_t mod; /* rt extent offset */ |
| 5065 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5066 | xfs_extnum_t nextents; /* number of file extents */ |
| 5067 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5069 | int tmp_logflags; /* partial logging flags */ |
| 5070 | int wasdel; /* was a delayed alloc extent */ |
| 5071 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | xfs_fsblock_t sum; |
| 5073 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5074 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5077 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5078 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5079 | if (unlikely( |
| 5080 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5081 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5082 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5083 | ip->i_mount); |
| 5084 | return XFS_ERROR(EFSCORRUPTED); |
| 5085 | } |
| 5086 | mp = ip->i_mount; |
| 5087 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5088 | return XFS_ERROR(EIO); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | ASSERT(len > 0); |
| 5091 | ASSERT(nexts >= 0); |
| 5092 | ASSERT(ifp->if_ext_max == |
| 5093 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5094 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5095 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 5096 | return error; |
| 5097 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5098 | if (nextents == 0) { |
| 5099 | *done = 1; |
| 5100 | return 0; |
| 5101 | } |
| 5102 | XFS_STATS_INC(xs_blk_unmap); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5103 | isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | start = bno; |
| 5105 | bno = start + len - 1; |
| 5106 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 5107 | &prev); |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | /* |
| 5110 | * Check to see if the given block number is past the end of the |
| 5111 | * file, back up to the last block if so... |
| 5112 | */ |
| 5113 | if (eof) { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5114 | ep = xfs_iext_get_ext(ifp, --lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | xfs_bmbt_get_all(ep, &got); |
| 5116 | bno = got.br_startoff + got.br_blockcount - 1; |
| 5117 | } |
| 5118 | logflags = 0; |
| 5119 | if (ifp->if_flags & XFS_IFBROOT) { |
| 5120 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 5121 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | cur->bc_private.b.firstblock = *firstblock; |
| 5123 | cur->bc_private.b.flist = flist; |
| 5124 | cur->bc_private.b.flags = 0; |
| 5125 | } else |
| 5126 | cur = NULL; |
| 5127 | extno = 0; |
| 5128 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5129 | (nexts == 0 || extno < nexts)) { |
| 5130 | /* |
| 5131 | * Is the found extent after a hole in which bno lives? |
| 5132 | * Just back up to the previous extent, if so. |
| 5133 | */ |
| 5134 | if (got.br_startoff > bno) { |
| 5135 | if (--lastx < 0) |
| 5136 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5137 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | xfs_bmbt_get_all(ep, &got); |
| 5139 | } |
| 5140 | /* |
| 5141 | * Is the last block of this extent before the range |
| 5142 | * we're supposed to delete? If so, we're done. |
| 5143 | */ |
| 5144 | bno = XFS_FILEOFF_MIN(bno, |
| 5145 | got.br_startoff + got.br_blockcount - 1); |
| 5146 | if (bno < start) |
| 5147 | break; |
| 5148 | /* |
| 5149 | * Then deal with the (possibly delayed) allocated space |
| 5150 | * we found. |
| 5151 | */ |
| 5152 | ASSERT(ep != NULL); |
| 5153 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5154 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | if (got.br_startoff < start) { |
| 5156 | del.br_startoff = start; |
| 5157 | del.br_blockcount -= start - got.br_startoff; |
| 5158 | if (!wasdel) |
| 5159 | del.br_startblock += start - got.br_startoff; |
| 5160 | } |
| 5161 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5162 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5163 | sum = del.br_startblock + del.br_blockcount; |
| 5164 | if (isrt && |
| 5165 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5166 | /* |
| 5167 | * Realtime extent not lined up at the end. |
| 5168 | * The extent could have been split into written |
| 5169 | * and unwritten pieces, or we could just be |
| 5170 | * unmapping part of it. But we can't really |
| 5171 | * get rid of part of a realtime extent. |
| 5172 | */ |
| 5173 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5174 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | /* |
| 5176 | * This piece is unwritten, or we're not |
| 5177 | * using unwritten extents. Skip over it. |
| 5178 | */ |
| 5179 | ASSERT(bno >= mod); |
| 5180 | bno -= mod > del.br_blockcount ? |
| 5181 | del.br_blockcount : mod; |
| 5182 | if (bno < got.br_startoff) { |
| 5183 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5184 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5185 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | } |
| 5187 | continue; |
| 5188 | } |
| 5189 | /* |
| 5190 | * It's written, turn it unwritten. |
| 5191 | * This is better than zeroing it. |
| 5192 | */ |
| 5193 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5194 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5195 | /* |
| 5196 | * If this spans a realtime extent boundary, |
| 5197 | * chop it back to the start of the one we end at. |
| 5198 | */ |
| 5199 | if (del.br_blockcount > mod) { |
| 5200 | del.br_startoff += del.br_blockcount - mod; |
| 5201 | del.br_startblock += del.br_blockcount - mod; |
| 5202 | del.br_blockcount = mod; |
| 5203 | } |
| 5204 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 5205 | error = xfs_bmap_add_extent(tp, ip, &lastx, &cur, &del, |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5206 | firstblock, flist, &logflags, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5207 | XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5208 | if (error) |
| 5209 | goto error0; |
| 5210 | goto nodelete; |
| 5211 | } |
| 5212 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5213 | /* |
| 5214 | * Realtime extent is lined up at the end but not |
| 5215 | * at the front. We'll get rid of full extents if |
| 5216 | * we can. |
| 5217 | */ |
| 5218 | mod = mp->m_sb.sb_rextsize - mod; |
| 5219 | if (del.br_blockcount > mod) { |
| 5220 | del.br_blockcount -= mod; |
| 5221 | del.br_startoff += mod; |
| 5222 | del.br_startblock += mod; |
| 5223 | } else if ((del.br_startoff == start && |
| 5224 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5225 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5226 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | /* |
| 5228 | * Can't make it unwritten. There isn't |
| 5229 | * a full extent here so just skip it. |
| 5230 | */ |
| 5231 | ASSERT(bno >= del.br_blockcount); |
| 5232 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5233 | if (got.br_startoff > bno) { |
| 5234 | if (--lastx >= 0) { |
| 5235 | ep = xfs_iext_get_ext(ifp, |
| 5236 | lastx); |
| 5237 | xfs_bmbt_get_all(ep, &got); |
| 5238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | } |
| 5240 | continue; |
| 5241 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5242 | /* |
| 5243 | * This one is already unwritten. |
| 5244 | * It must have a written left neighbor. |
| 5245 | * Unwrite the killed part of that one and |
| 5246 | * try again. |
| 5247 | */ |
| 5248 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5249 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5250 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5252 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | ASSERT(del.br_startblock == |
| 5254 | prev.br_startblock + prev.br_blockcount); |
| 5255 | if (prev.br_startoff < start) { |
| 5256 | mod = start - prev.br_startoff; |
| 5257 | prev.br_blockcount -= mod; |
| 5258 | prev.br_startblock += mod; |
| 5259 | prev.br_startoff = start; |
| 5260 | } |
| 5261 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5262 | lastx--; |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 5263 | error = xfs_bmap_add_extent(tp, ip, &lastx, |
| 5264 | &cur, &prev, firstblock, flist, |
| 5265 | &logflags, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | if (error) |
| 5267 | goto error0; |
| 5268 | goto nodelete; |
| 5269 | } else { |
| 5270 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5271 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | f3ca8738 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 5272 | error = xfs_bmap_add_extent(tp, ip, &lastx, |
| 5273 | &cur, &del, firstblock, flist, |
| 5274 | &logflags, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | if (error) |
| 5276 | goto error0; |
| 5277 | goto nodelete; |
| 5278 | } |
| 5279 | } |
| 5280 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5281 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5282 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5283 | if (isrt) { |
| 5284 | xfs_filblks_t rtexts; |
| 5285 | |
| 5286 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5287 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5288 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5289 | (int64_t)rtexts, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5290 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5291 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5292 | XFS_QMOPT_RES_RTBLKS); |
| 5293 | } else { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 5294 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5295 | (int64_t)del.br_blockcount, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5296 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5297 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | ip->i_delayed_blks -= del.br_blockcount; |
| 5301 | if (cur) |
| 5302 | cur->bc_private.b.flags |= |
| 5303 | XFS_BTCUR_BPRV_WASDEL; |
| 5304 | } else if (cur) |
| 5305 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5306 | /* |
| 5307 | * If it's the case where the directory code is running |
| 5308 | * with no block reservation, and the deleted block is in |
| 5309 | * the middle of its extent, and the resulting insert |
| 5310 | * of an extent would cause transformation to btree format, |
| 5311 | * then reject it. The calling code will then swap |
| 5312 | * blocks around instead. |
| 5313 | * We have to do this now, rather than waiting for the |
| 5314 | * conversion to btree format, since the transaction |
| 5315 | * will be dirty. |
| 5316 | */ |
| 5317 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5318 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5319 | XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max && |
| 5320 | del.br_startoff > got.br_startoff && |
| 5321 | del.br_startoff + del.br_blockcount < |
| 5322 | got.br_startoff + got.br_blockcount) { |
| 5323 | error = XFS_ERROR(ENOSPC); |
| 5324 | goto error0; |
| 5325 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5326 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5327 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | logflags |= tmp_logflags; |
| 5329 | if (error) |
| 5330 | goto error0; |
| 5331 | bno = del.br_startoff - 1; |
| 5332 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | /* |
| 5334 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5337 | if (lastx >= 0) { |
| 5338 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5339 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5340 | if (--lastx >= 0) |
| 5341 | ep = xfs_iext_get_ext(ifp, |
| 5342 | lastx); |
| 5343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | extno++; |
| 5347 | } |
| 5348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
| 5350 | ASSERT(ifp->if_ext_max == |
| 5351 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5352 | /* |
| 5353 | * Convert to a btree if necessary. |
| 5354 | */ |
| 5355 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 5356 | XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) { |
| 5357 | ASSERT(cur == NULL); |
| 5358 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5359 | &cur, 0, &tmp_logflags, whichfork); |
| 5360 | logflags |= tmp_logflags; |
| 5361 | if (error) |
| 5362 | goto error0; |
| 5363 | } |
| 5364 | /* |
| 5365 | * transform from btree to extents, give it cur |
| 5366 | */ |
| 5367 | else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 5368 | XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) { |
| 5369 | ASSERT(cur != NULL); |
| 5370 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5371 | whichfork); |
| 5372 | logflags |= tmp_logflags; |
| 5373 | if (error) |
| 5374 | goto error0; |
| 5375 | } |
| 5376 | /* |
| 5377 | * transform from extents to local? |
| 5378 | */ |
| 5379 | ASSERT(ifp->if_ext_max == |
| 5380 | XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t)); |
| 5381 | error = 0; |
| 5382 | error0: |
| 5383 | /* |
| 5384 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5385 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5387 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5389 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5390 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5392 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | /* |
| 5394 | * Log inode even in the error case, if the transaction |
| 5395 | * is dirty we'll need to shut down the filesystem. |
| 5396 | */ |
| 5397 | if (logflags) |
| 5398 | xfs_trans_log_inode(tp, ip, logflags); |
| 5399 | if (cur) { |
| 5400 | if (!error) { |
| 5401 | *firstblock = cur->bc_private.b.firstblock; |
| 5402 | cur->bc_private.b.allocated = 0; |
| 5403 | } |
| 5404 | xfs_btree_del_cursor(cur, |
| 5405 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5406 | } |
| 5407 | return error; |
| 5408 | } |
| 5409 | |
| 5410 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5411 | * returns 1 for success, 0 if we failed to map the extent. |
| 5412 | */ |
| 5413 | STATIC int |
| 5414 | xfs_getbmapx_fix_eof_hole( |
| 5415 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5416 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5417 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5418 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5419 | __int64_t end, /* last block requested */ |
| 5420 | xfs_fsblock_t startblock) |
| 5421 | { |
| 5422 | __int64_t fixlen; |
| 5423 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5424 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5425 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5426 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5427 | |
| 5428 | if (startblock == HOLESTARTBLOCK) { |
| 5429 | mp = ip->i_mount; |
| 5430 | out->bmv_block = -1; |
| 5431 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, ip->i_size)); |
| 5432 | fixlen -= out->bmv_offset; |
| 5433 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5434 | /* Came to hole at EOF. Trim it. */ |
| 5435 | if (fixlen <= 0) |
| 5436 | return 0; |
| 5437 | out->bmv_length = fixlen; |
| 5438 | } |
| 5439 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5440 | if (startblock == DELAYSTARTBLOCK) |
| 5441 | out->bmv_block = -2; |
| 5442 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5443 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5444 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5445 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5446 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5447 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5448 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | return 1; |
| 5452 | } |
| 5453 | |
| 5454 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5455 | * Get inode's extents as described in bmv, and format for output. |
| 5456 | * Calls formatter to fill the user's buffer until all extents |
| 5457 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5458 | * been filled, or until the formatter short-circuits the loop, |
| 5459 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | */ |
| 5461 | int /* error code */ |
| 5462 | xfs_getbmap( |
Christoph Hellwig | 993386c1 | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5463 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5464 | struct getbmapx *bmv, /* user bmap structure */ |
| 5465 | xfs_bmap_format_t formatter, /* format to user */ |
| 5466 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | { |
| 5468 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5469 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | __int64_t fixlen; /* length for -1 case */ |
| 5471 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | int lock; /* lock state */ |
| 5473 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5474 | xfs_mount_t *mp; /* file system mount point */ |
| 5475 | int nex; /* # of user extents can do */ |
| 5476 | int nexleft; /* # of user extents left */ |
| 5477 | int subnex; /* # of bmapi's can do */ |
| 5478 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5479 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | int whichfork; /* data or attr fork */ |
| 5481 | int prealloced; /* this is a file with |
| 5482 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5483 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5485 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5488 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5489 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | if (whichfork == XFS_ATTR_FORK) { |
| 5492 | if (XFS_IFORK_Q(ip)) { |
| 5493 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5494 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5495 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5496 | return XFS_ERROR(EINVAL); |
| 5497 | } else if (unlikely( |
| 5498 | ip->i_d.di_aformat != 0 && |
| 5499 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5500 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5501 | ip->i_mount); |
| 5502 | return XFS_ERROR(EFSCORRUPTED); |
| 5503 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5504 | |
| 5505 | prealloced = 0; |
| 5506 | fixlen = 1LL << 32; |
| 5507 | } else { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5508 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5509 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5510 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5511 | return XFS_ERROR(EINVAL); |
| 5512 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5513 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5514 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | prealloced = 1; |
| 5516 | fixlen = XFS_MAXIOFFSET(mp); |
| 5517 | } else { |
| 5518 | prealloced = 0; |
Lachlan McIlroy | ba87ea6 | 2007-05-08 13:49:46 +1000 | [diff] [blame] | 5519 | fixlen = ip->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | if (bmv->bmv_length == -1) { |
| 5524 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5525 | bmv->bmv_length = |
| 5526 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5527 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | bmv->bmv_entries = 0; |
| 5529 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5530 | } else if (bmv->bmv_length < 0) { |
| 5531 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | nex = bmv->bmv_count - 1; |
| 5535 | if (nex <= 0) |
| 5536 | return XFS_ERROR(EINVAL); |
| 5537 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5538 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5539 | |
| 5540 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5541 | return XFS_ERROR(ENOMEM); |
| 5542 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
| 5543 | if (!out) |
| 5544 | return XFS_ERROR(ENOMEM); |
| 5545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5547 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
| 5548 | if (ip->i_delayed_blks || ip->i_size > ip->i_d.di_size) { |
| 5549 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5550 | if (error) |
| 5551 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5552 | } |
Dave Chinner | 309c848 | 2010-11-30 15:16:02 +1100 | [diff] [blame] | 5553 | /* |
| 5554 | * even after flushing the inode, there can still be delalloc |
| 5555 | * blocks on the inode beyond EOF due to speculative |
| 5556 | * preallocation. These are not removed until the release |
| 5557 | * function is called or the inode is inactivated. Hence we |
| 5558 | * cannot assert here that ip->i_delayed_blks == 0. |
| 5559 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | |
| 5562 | lock = xfs_ilock_map_shared(ip); |
| 5563 | |
| 5564 | /* |
| 5565 | * Don't let nex be bigger than the number of extents |
| 5566 | * we can have assuming alternating holes and real extents. |
| 5567 | */ |
| 5568 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5569 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5570 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5571 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5572 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5573 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | |
| 5575 | /* |
| 5576 | * Allocate enough space to handle "subnex" maps at a time. |
| 5577 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5578 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5580 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5581 | if (!map) |
| 5582 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | |
| 5584 | bmv->bmv_entries = 0; |
| 5585 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5586 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5587 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5588 | error = 0; |
| 5589 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | nexleft = nex; |
| 5593 | |
| 5594 | do { |
| 5595 | nmap = (nexleft > subnex) ? subnex : nexleft; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 5596 | error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5597 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
| 5598 | map, &nmap, bmapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5600 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | ASSERT(nmap <= subnex); |
| 5602 | |
| 5603 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5604 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5605 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5606 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5607 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5608 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5609 | out[cur_ext].bmv_offset = |
| 5610 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5611 | out[cur_ext].bmv_length = |
| 5612 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5613 | out[cur_ext].bmv_unused1 = 0; |
| 5614 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5615 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5616 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5617 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5618 | whichfork == XFS_ATTR_FORK) { |
| 5619 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5620 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5621 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5622 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5623 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5624 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5625 | prealloced, bmvend, |
| 5626 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5627 | goto out_free_map; |
| 5628 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5629 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5630 | out[cur_ext].bmv_offset + |
| 5631 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5632 | bmv->bmv_length = |
| 5633 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 5634 | |
| 5635 | /* |
| 5636 | * In case we don't want to return the hole, |
| 5637 | * don't increase cur_ext so that we can reuse |
| 5638 | * it in the next loop. |
| 5639 | */ |
| 5640 | if ((iflags & BMV_IF_NO_HOLES) && |
| 5641 | map[i].br_startblock == HOLESTARTBLOCK) { |
| 5642 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); |
| 5643 | continue; |
| 5644 | } |
| 5645 | |
| 5646 | nexleft--; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5647 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5648 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | } |
| 5650 | } while (nmap && nexleft && bmv->bmv_length); |
| 5651 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5652 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5653 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5654 | out_unlock_ilock: |
| 5655 | xfs_iunlock_map_shared(ip, lock); |
| 5656 | out_unlock_iolock: |
| 5657 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5658 | |
| 5659 | for (i = 0; i < cur_ext; i++) { |
| 5660 | int full = 0; /* user array is full */ |
| 5661 | |
| 5662 | /* format results & advance arg */ |
| 5663 | error = formatter(&arg, &out[i], &full); |
| 5664 | if (error || full) |
| 5665 | break; |
| 5666 | } |
| 5667 | |
Felix Blyakher | 7747a0b | 2009-06-11 17:07:28 -0500 | [diff] [blame] | 5668 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5669 | return error; |
| 5670 | } |
| 5671 | |
| 5672 | /* |
| 5673 | * Check the last inode extent to determine whether this allocation will result |
| 5674 | * in blocks being allocated at the end of the file. When we allocate new data |
| 5675 | * blocks at the end of the file which do not start at the previous data block, |
| 5676 | * we will try to align the new blocks at stripe unit boundaries. |
| 5677 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 5678 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | xfs_bmap_isaeof( |
| 5680 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5681 | xfs_fileoff_t off, /* file offset in fsblocks */ |
| 5682 | int whichfork, /* data or attribute fork */ |
| 5683 | char *aeof) /* return value */ |
| 5684 | { |
| 5685 | int error; /* error return value */ |
| 5686 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5687 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5688 | xfs_extnum_t nextents; /* number of file extents */ |
| 5689 | xfs_bmbt_irec_t s; /* expanded extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | |
| 5691 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5692 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5693 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5694 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5695 | return error; |
| 5696 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5697 | if (nextents == 0) { |
| 5698 | *aeof = 1; |
| 5699 | return 0; |
| 5700 | } |
| 5701 | /* |
| 5702 | * Go to the last extent |
| 5703 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5704 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | xfs_bmbt_get_all(lastrec, &s); |
| 5706 | /* |
| 5707 | * Check we are allocating in the last extent (for delayed allocations) |
| 5708 | * or past the last extent for non-delayed allocations. |
| 5709 | */ |
| 5710 | *aeof = (off >= s.br_startoff && |
| 5711 | off < s.br_startoff + s.br_blockcount && |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5712 | isnullstartblock(s.br_startblock)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | off >= s.br_startoff + s.br_blockcount; |
| 5714 | return 0; |
| 5715 | } |
| 5716 | |
| 5717 | /* |
| 5718 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 5719 | * the allocation to a stripe unit boundary. |
| 5720 | */ |
| 5721 | int /* error */ |
| 5722 | xfs_bmap_eof( |
| 5723 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5724 | xfs_fileoff_t endoff, /* file offset in fsblocks */ |
| 5725 | int whichfork, /* data or attribute fork */ |
| 5726 | int *eof) /* result value */ |
| 5727 | { |
| 5728 | xfs_fsblock_t blockcount; /* extent block count */ |
| 5729 | int error; /* error return value */ |
| 5730 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5731 | xfs_bmbt_rec_host_t *lastrec; /* extent record pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5732 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | xfs_fileoff_t startoff; /* extent starting file offset */ |
| 5734 | |
| 5735 | ASSERT(whichfork == XFS_DATA_FORK); |
| 5736 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5737 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 5738 | (error = xfs_iread_extents(NULL, ip, whichfork))) |
| 5739 | return error; |
| 5740 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
| 5741 | if (nextents == 0) { |
| 5742 | *eof = 1; |
| 5743 | return 0; |
| 5744 | } |
| 5745 | /* |
| 5746 | * Go to the last extent |
| 5747 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5748 | lastrec = xfs_iext_get_ext(ifp, nextents - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | startoff = xfs_bmbt_get_startoff(lastrec); |
| 5750 | blockcount = xfs_bmbt_get_blockcount(lastrec); |
| 5751 | *eof = endoff >= startoff + blockcount; |
| 5752 | return 0; |
| 5753 | } |
| 5754 | |
| 5755 | #ifdef DEBUG |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5756 | STATIC struct xfs_buf * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | xfs_bmap_get_bp( |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5758 | struct xfs_btree_cur *cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | xfs_fsblock_t bno) |
| 5760 | { |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5761 | struct xfs_log_item_desc *lidp; |
| 5762 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | |
| 5764 | if (!cur) |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5765 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5767 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5768 | if (!cur->bc_bufs[i]) |
| 5769 | break; |
| 5770 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 5771 | return cur->bc_bufs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5774 | /* Chase down all the log items to see if the bp is there */ |
| 5775 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5776 | struct xfs_buf_log_item *bip; |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5777 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 5778 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 5779 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 5780 | return bip->bli_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | } |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5782 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5783 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | } |
| 5785 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5786 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5788 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | xfs_mount_t *mp, |
| 5790 | int root, |
| 5791 | short sz) |
| 5792 | { |
| 5793 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5794 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5795 | xfs_bmbt_key_t *prevp, *keyp; |
| 5796 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5797 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | |
| 5799 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5800 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5802 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | |
| 5804 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5805 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5806 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | } |
| 5808 | prevp = keyp; |
| 5809 | |
| 5810 | /* |
| 5811 | * Compare the block numbers to see if there are dups. |
| 5812 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5813 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5814 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5815 | else |
| 5816 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5817 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5818 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5819 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5820 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5821 | else |
| 5822 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5823 | if (*thispa == *pp) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5824 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5825 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5826 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5828 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | } |
| 5830 | } |
| 5831 | } |
| 5832 | } |
| 5833 | |
| 5834 | /* |
| 5835 | * Check that the extents for the inode ip are in the right order in all |
| 5836 | * btree leaves. |
| 5837 | */ |
| 5838 | |
| 5839 | STATIC void |
| 5840 | xfs_bmap_check_leaf_extents( |
| 5841 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5842 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5843 | int whichfork) /* data or attr fork */ |
| 5844 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5845 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5846 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5847 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5848 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5849 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | xfs_ifork_t *ifp; /* fork structure */ |
| 5851 | int level; /* btree level, for checking */ |
| 5852 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5853 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5854 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5855 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5856 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | int bp_release = 0; |
| 5858 | |
| 5859 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5860 | return; |
| 5861 | } |
| 5862 | |
| 5863 | bno = NULLFSBLOCK; |
| 5864 | mp = ip->i_mount; |
| 5865 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5866 | block = ifp->if_broot; |
| 5867 | /* |
| 5868 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5869 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5870 | level = be16_to_cpu(block->bb_level); |
| 5871 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5873 | 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] | 5874 | bno = be64_to_cpu(*pp); |
| 5875 | |
| 5876 | ASSERT(bno != NULLDFSBNO); |
| 5877 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5878 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | /* |
| 5881 | * Go down the tree until leaf level is reached, following the first |
| 5882 | * pointer (leftmost) at each level. |
| 5883 | */ |
| 5884 | while (level-- > 0) { |
| 5885 | /* See if buf is in cur first */ |
| 5886 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5887 | if (bp) { |
| 5888 | bp_release = 0; |
| 5889 | } else { |
| 5890 | bp_release = 1; |
| 5891 | } |
| 5892 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5893 | XFS_BMAP_BTREE_REF))) |
| 5894 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5895 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5897 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | error0); |
| 5899 | if (level == 0) |
| 5900 | break; |
| 5901 | |
| 5902 | /* |
| 5903 | * Check this block for basic sanity (increasing keys and |
| 5904 | * no duplicate blocks). |
| 5905 | */ |
| 5906 | |
| 5907 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5908 | 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] | 5909 | bno = be64_to_cpu(*pp); |
| 5910 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | if (bp_release) { |
| 5912 | bp_release = 0; |
| 5913 | xfs_trans_brelse(NULL, bp); |
| 5914 | } |
| 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 5919 | */ |
| 5920 | i = 0; |
| 5921 | |
| 5922 | /* |
| 5923 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 5924 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | xfs_fsblock_t nextbno; |
| 5927 | xfs_extnum_t num_recs; |
| 5928 | |
| 5929 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5930 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | |
| 5932 | /* |
| 5933 | * Read-ahead the next leaf block, if any. |
| 5934 | */ |
| 5935 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5936 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | |
| 5938 | /* |
| 5939 | * Check all the extents to make sure they are OK. |
| 5940 | * If we had a previous block, the last entry should |
| 5941 | * conform with the first entry in this one. |
| 5942 | */ |
| 5943 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5944 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5945 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5946 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 5947 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 5948 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5949 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5950 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5951 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5952 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 5953 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 5954 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5955 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5958 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5959 | i += num_recs; |
| 5960 | if (bp_release) { |
| 5961 | bp_release = 0; |
| 5962 | xfs_trans_brelse(NULL, bp); |
| 5963 | } |
| 5964 | bno = nextbno; |
| 5965 | /* |
| 5966 | * If we've reached the end, stop. |
| 5967 | */ |
| 5968 | if (bno == NULLFSBLOCK) |
| 5969 | break; |
| 5970 | |
| 5971 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5972 | if (bp) { |
| 5973 | bp_release = 0; |
| 5974 | } else { |
| 5975 | bp_release = 1; |
| 5976 | } |
| 5977 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5978 | XFS_BMAP_BTREE_REF))) |
| 5979 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5980 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | } |
| 5982 | if (bp_release) { |
| 5983 | bp_release = 0; |
| 5984 | xfs_trans_brelse(NULL, bp); |
| 5985 | } |
| 5986 | return; |
| 5987 | |
| 5988 | error0: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5989 | xfs_warn(mp, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | if (bp_release) |
| 5991 | xfs_trans_brelse(NULL, bp); |
| 5992 | error_norelse: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5993 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5994 | __func__, i); |
| 5995 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | return; |
| 5997 | } |
| 5998 | #endif |
| 5999 | |
| 6000 | /* |
| 6001 | * Count fsblocks of the given fork. |
| 6002 | */ |
| 6003 | int /* error */ |
| 6004 | xfs_bmap_count_blocks( |
| 6005 | xfs_trans_t *tp, /* transaction pointer */ |
| 6006 | xfs_inode_t *ip, /* incore inode */ |
| 6007 | int whichfork, /* data or attr fork */ |
| 6008 | int *count) /* out: count of blocks */ |
| 6009 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6010 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | xfs_fsblock_t bno; /* block # of "block" */ |
| 6012 | xfs_ifork_t *ifp; /* fork structure */ |
| 6013 | int level; /* btree level, for checking */ |
| 6014 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6015 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | |
| 6017 | bno = NULLFSBLOCK; |
| 6018 | mp = ip->i_mount; |
| 6019 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 6020 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6021 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6023 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6024 | return 0; |
| 6025 | } |
| 6026 | |
| 6027 | /* |
| 6028 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 6029 | */ |
| 6030 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6031 | level = be16_to_cpu(block->bb_level); |
| 6032 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 6033 | 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] | 6034 | bno = be64_to_cpu(*pp); |
| 6035 | ASSERT(bno != NULLDFSBNO); |
| 6036 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 6037 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6039 | 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] | 6040 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 6041 | mp); |
| 6042 | return XFS_ERROR(EFSCORRUPTED); |
| 6043 | } |
| 6044 | |
| 6045 | return 0; |
| 6046 | } |
| 6047 | |
| 6048 | /* |
| 6049 | * Recursively walks each level of a btree |
| 6050 | * to count total fsblocks is use. |
| 6051 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 6052 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | xfs_bmap_count_tree( |
| 6054 | xfs_mount_t *mp, /* file system mount point */ |
| 6055 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6056 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | xfs_fsblock_t blockno, /* file system block number */ |
| 6058 | int levelin, /* level in btree */ |
| 6059 | int *count) /* Count of blocks */ |
| 6060 | { |
| 6061 | int error; |
| 6062 | xfs_buf_t *bp, *nbp; |
| 6063 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 6064 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | xfs_fsblock_t bno = blockno; |
| 6066 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6067 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | |
| 6070 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 6071 | return error; |
| 6072 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6073 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | |
| 6075 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 6076 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6077 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | while (nextbno != NULLFSBLOCK) { |
| 6079 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6080 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6081 | return error; |
| 6082 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6083 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6084 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | xfs_trans_brelse(tp, nbp); |
| 6086 | } |
| 6087 | |
| 6088 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6089 | 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] | 6090 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6092 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | xfs_trans_brelse(tp, bp); |
| 6094 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6095 | XFS_ERRLEVEL_LOW, mp); |
| 6096 | return XFS_ERROR(EFSCORRUPTED); |
| 6097 | } |
| 6098 | xfs_trans_brelse(tp, bp); |
| 6099 | } else { |
| 6100 | /* count all level 1 nodes and their leaves */ |
| 6101 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6102 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6103 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6104 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | xfs_trans_brelse(tp, bp); |
| 6106 | if (nextbno == NULLFSBLOCK) |
| 6107 | break; |
| 6108 | bno = nextbno; |
| 6109 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6110 | XFS_BMAP_BTREE_REF))) |
| 6111 | return error; |
| 6112 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6113 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | } |
| 6115 | } |
| 6116 | return 0; |
| 6117 | } |
| 6118 | |
| 6119 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6120 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6122 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6124 | xfs_ifork_t *ifp, |
| 6125 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | int numrecs, |
| 6127 | int *count) |
| 6128 | { |
| 6129 | int b; |
| 6130 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6131 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6132 | 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] | 6133 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6134 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6135 | } |
| 6136 | |
| 6137 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6138 | * Count leaf blocks given a range of extent records originally |
| 6139 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6140 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6141 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6142 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6143 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6144 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6145 | int numrecs, |
| 6146 | int *count) |
| 6147 | { |
| 6148 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6149 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6150 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6151 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6152 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6154 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6155 | } |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6156 | |
| 6157 | /* |
| 6158 | * dead simple method of punching delalyed allocation blocks from a range in |
| 6159 | * the inode. Walks a block at a time so will be slow, but is only executed in |
| 6160 | * rare error cases so the overhead is not critical. This will alays punch out |
| 6161 | * both the start and end blocks, even if the ranges only partially overlap |
| 6162 | * them, so it is up to the caller to ensure that partial blocks are not |
| 6163 | * passed in. |
| 6164 | */ |
| 6165 | int |
| 6166 | xfs_bmap_punch_delalloc_range( |
| 6167 | struct xfs_inode *ip, |
| 6168 | xfs_fileoff_t start_fsb, |
| 6169 | xfs_fileoff_t length) |
| 6170 | { |
| 6171 | xfs_fileoff_t remaining = length; |
| 6172 | int error = 0; |
| 6173 | |
| 6174 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 6175 | |
| 6176 | do { |
| 6177 | int done; |
| 6178 | xfs_bmbt_irec_t imap; |
| 6179 | int nimaps = 1; |
| 6180 | xfs_fsblock_t firstblock; |
| 6181 | xfs_bmap_free_t flist; |
| 6182 | |
| 6183 | /* |
| 6184 | * Map the range first and check that it is a delalloc extent |
| 6185 | * before trying to unmap the range. Otherwise we will be |
| 6186 | * trying to remove a real extent (which requires a |
| 6187 | * transaction) or a hole, which is probably a bad idea... |
| 6188 | */ |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 6189 | error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, |
| 6190 | XFS_BMAPI_ENTIRE); |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6191 | |
| 6192 | if (error) { |
| 6193 | /* something screwed, just bail */ |
| 6194 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 6195 | xfs_alert(ip->i_mount, |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6196 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
| 6197 | ip->i_ino, start_fsb); |
| 6198 | } |
| 6199 | break; |
| 6200 | } |
| 6201 | if (!nimaps) { |
| 6202 | /* nothing there */ |
| 6203 | goto next_block; |
| 6204 | } |
| 6205 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 6206 | /* been converted, ignore */ |
| 6207 | goto next_block; |
| 6208 | } |
| 6209 | WARN_ON(imap.br_blockcount == 0); |
| 6210 | |
| 6211 | /* |
| 6212 | * Note: while we initialise the firstblock/flist pair, they |
| 6213 | * should never be used because blocks should never be |
| 6214 | * allocated or freed for a delalloc extent and hence we need |
| 6215 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 6216 | */ |
| 6217 | xfs_bmap_init(&flist, &firstblock); |
| 6218 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, |
| 6219 | &flist, &done); |
| 6220 | if (error) |
| 6221 | break; |
| 6222 | |
| 6223 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 6224 | next_block: |
| 6225 | start_fsb++; |
| 6226 | remaining--; |
| 6227 | } while(remaining > 0); |
| 6228 | |
| 6229 | return error; |
| 6230 | } |