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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | kmem_zone_t *xfs_bmap_free_item_zone; |
| 54 | |
| 55 | /* |
| 56 | * Prototypes for internal bmap routines. |
| 57 | */ |
| 58 | |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 59 | #ifdef DEBUG |
| 60 | STATIC void |
| 61 | xfs_bmap_check_leaf_extents( |
| 62 | struct xfs_btree_cur *cur, |
| 63 | struct xfs_inode *ip, |
| 64 | int whichfork); |
| 65 | #else |
| 66 | #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0) |
| 67 | #endif |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 72 | */ |
| 73 | STATIC int /* error */ |
| 74 | xfs_bmap_add_attrfork_extents( |
| 75 | xfs_trans_t *tp, /* transaction pointer */ |
| 76 | xfs_inode_t *ip, /* incore inode pointer */ |
| 77 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 78 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 79 | int *flags); /* inode logging flags */ |
| 80 | |
| 81 | /* |
| 82 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 83 | */ |
| 84 | STATIC int /* error */ |
| 85 | xfs_bmap_add_attrfork_local( |
| 86 | xfs_trans_t *tp, /* transaction pointer */ |
| 87 | xfs_inode_t *ip, /* incore inode pointer */ |
| 88 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 89 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 90 | int *flags); /* inode logging flags */ |
| 91 | |
| 92 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 94 | * It figures out where to ask the underlying allocator to put the new extent. |
| 95 | */ |
| 96 | STATIC int /* error */ |
| 97 | xfs_bmap_alloc( |
| 98 | xfs_bmalloca_t *ap); /* bmap alloc argument struct */ |
| 99 | |
| 100 | /* |
| 101 | * Transform a btree format file with only one leaf node, where the |
| 102 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 103 | * 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] | 104 | * give up the space for the btree root and pitch the leaf block. |
| 105 | */ |
| 106 | STATIC int /* error */ |
| 107 | xfs_bmap_btree_to_extents( |
| 108 | xfs_trans_t *tp, /* transaction pointer */ |
| 109 | xfs_inode_t *ip, /* incore inode pointer */ |
| 110 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 111 | int *logflagsp, /* inode logging flags */ |
| 112 | int whichfork); /* data or attr fork */ |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | * Remove the entry "free" from the free item list. Prev points to the |
| 116 | * previous entry, unless "free" is the head of the list. |
| 117 | */ |
| 118 | STATIC void |
| 119 | xfs_bmap_del_free( |
| 120 | xfs_bmap_free_t *flist, /* free item list header */ |
| 121 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 122 | xfs_bmap_free_item_t *free); /* list item to be freed */ |
| 123 | |
| 124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * Convert an extents-format file into a btree-format file. |
| 126 | * The new file will have a root block (in the inode) and a single child block. |
| 127 | */ |
| 128 | STATIC int /* error */ |
| 129 | xfs_bmap_extents_to_btree( |
| 130 | xfs_trans_t *tp, /* transaction pointer */ |
| 131 | xfs_inode_t *ip, /* incore inode pointer */ |
| 132 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 133 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 134 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 135 | int wasdel, /* converting a delayed alloc */ |
| 136 | int *logflagsp, /* inode logging flags */ |
| 137 | int whichfork); /* data or attr fork */ |
| 138 | |
| 139 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * Convert a local file to an extents file. |
| 141 | * This code is sort of bogus, since the file data needs to get |
| 142 | * logged so it won't be lost. The bmap-level manipulations are ok, though. |
| 143 | */ |
| 144 | STATIC int /* error */ |
| 145 | xfs_bmap_local_to_extents( |
| 146 | xfs_trans_t *tp, /* transaction pointer */ |
| 147 | xfs_inode_t *ip, /* incore inode pointer */ |
| 148 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 149 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 150 | int *logflagsp, /* inode logging flags */ |
| 151 | int whichfork); /* data or attr fork */ |
| 152 | |
| 153 | /* |
| 154 | * Search the extents list for the inode, for the extent containing bno. |
| 155 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 156 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 157 | * Else, *lastxp will be set to the index of the found |
| 158 | * entry; *gotp will contain the entry. |
| 159 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 160 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | xfs_bmap_search_extents( |
| 162 | xfs_inode_t *ip, /* incore inode pointer */ |
| 163 | xfs_fileoff_t bno, /* block number searched for */ |
| 164 | int whichfork, /* data or attr fork */ |
| 165 | int *eofp, /* out: end of file found */ |
| 166 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 167 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 168 | xfs_bmbt_irec_t *prevp); /* out: previous extent entry found */ |
| 169 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 170 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | * Compute the worst-case number of indirect blocks that will be used |
| 172 | * for ip's delayed extent of length "len". |
| 173 | */ |
| 174 | STATIC xfs_filblks_t |
| 175 | xfs_bmap_worst_indlen( |
| 176 | xfs_inode_t *ip, /* incore inode pointer */ |
| 177 | xfs_filblks_t len); /* delayed extent length */ |
| 178 | |
| 179 | #ifdef DEBUG |
| 180 | /* |
| 181 | * Perform various validation checks on the values being returned |
| 182 | * from xfs_bmapi(). |
| 183 | */ |
| 184 | STATIC void |
| 185 | xfs_bmap_validate_ret( |
| 186 | xfs_fileoff_t bno, |
| 187 | xfs_filblks_t len, |
| 188 | int flags, |
| 189 | xfs_bmbt_irec_t *mval, |
| 190 | int nmap, |
| 191 | int ret_nmap); |
| 192 | #else |
| 193 | #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) |
| 194 | #endif /* DEBUG */ |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | STATIC int |
| 197 | xfs_bmap_count_tree( |
| 198 | xfs_mount_t *mp, |
| 199 | xfs_trans_t *tp, |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 200 | xfs_ifork_t *ifp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | xfs_fsblock_t blockno, |
| 202 | int levelin, |
| 203 | int *count); |
| 204 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 205 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 207 | xfs_ifork_t *ifp, |
| 208 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | int numrecs, |
| 210 | int *count); |
| 211 | |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 212 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 213 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 214 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 215 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 216 | int numrecs, |
| 217 | int *count); |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* |
| 220 | * Bmap internal routines. |
| 221 | */ |
| 222 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 223 | STATIC int /* error */ |
| 224 | xfs_bmbt_lookup_eq( |
| 225 | struct xfs_btree_cur *cur, |
| 226 | xfs_fileoff_t off, |
| 227 | xfs_fsblock_t bno, |
| 228 | xfs_filblks_t len, |
| 229 | int *stat) /* success/failure */ |
| 230 | { |
| 231 | cur->bc_rec.b.br_startoff = off; |
| 232 | cur->bc_rec.b.br_startblock = bno; |
| 233 | cur->bc_rec.b.br_blockcount = len; |
| 234 | return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat); |
| 235 | } |
| 236 | |
| 237 | STATIC int /* error */ |
| 238 | xfs_bmbt_lookup_ge( |
| 239 | struct xfs_btree_cur *cur, |
| 240 | xfs_fileoff_t off, |
| 241 | xfs_fsblock_t bno, |
| 242 | xfs_filblks_t len, |
| 243 | int *stat) /* success/failure */ |
| 244 | { |
| 245 | cur->bc_rec.b.br_startoff = off; |
| 246 | cur->bc_rec.b.br_startblock = bno; |
| 247 | cur->bc_rec.b.br_blockcount = len; |
| 248 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 249 | } |
| 250 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 251 | /* |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 252 | * Check if the inode needs to be converted to btree format. |
| 253 | */ |
| 254 | static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork) |
| 255 | { |
| 256 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
| 257 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 258 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Check if the inode should be converted to extent format. |
| 263 | */ |
| 264 | static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork) |
| 265 | { |
| 266 | return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE && |
| 267 | XFS_IFORK_NEXTENTS(ip, whichfork) <= |
| 268 | XFS_IFORK_MAXEXT(ip, whichfork); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Update the record referred to by cur to the value given |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 273 | * by [off, bno, len, state]. |
| 274 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 275 | */ |
| 276 | STATIC int |
| 277 | xfs_bmbt_update( |
| 278 | struct xfs_btree_cur *cur, |
| 279 | xfs_fileoff_t off, |
| 280 | xfs_fsblock_t bno, |
| 281 | xfs_filblks_t len, |
| 282 | xfs_exntst_t state) |
| 283 | { |
| 284 | union xfs_btree_rec rec; |
| 285 | |
| 286 | xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state); |
| 287 | return xfs_btree_update(cur, &rec); |
| 288 | } |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | /* |
| 291 | * Called from xfs_bmap_add_attrfork to handle btree format files. |
| 292 | */ |
| 293 | STATIC int /* error */ |
| 294 | xfs_bmap_add_attrfork_btree( |
| 295 | xfs_trans_t *tp, /* transaction pointer */ |
| 296 | xfs_inode_t *ip, /* incore inode pointer */ |
| 297 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 298 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 299 | int *flags) /* inode logging flags */ |
| 300 | { |
| 301 | xfs_btree_cur_t *cur; /* btree cursor */ |
| 302 | int error; /* error return value */ |
| 303 | xfs_mount_t *mp; /* file system mount struct */ |
| 304 | int stat; /* newroot status */ |
| 305 | |
| 306 | mp = ip->i_mount; |
| 307 | if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip)) |
| 308 | *flags |= XFS_ILOG_DBROOT; |
| 309 | else { |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 310 | cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | cur->bc_private.b.flist = flist; |
| 312 | cur->bc_private.b.firstblock = *firstblock; |
| 313 | if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat))) |
| 314 | goto error0; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 315 | /* must be at least one entry */ |
| 316 | XFS_WANT_CORRUPTED_GOTO(stat == 1, error0); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 317 | if ((error = xfs_btree_new_iroot(cur, flags, &stat))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | goto error0; |
| 319 | if (stat == 0) { |
| 320 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 321 | return XFS_ERROR(ENOSPC); |
| 322 | } |
| 323 | *firstblock = cur->bc_private.b.firstblock; |
| 324 | cur->bc_private.b.allocated = 0; |
| 325 | xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR); |
| 326 | } |
| 327 | return 0; |
| 328 | error0: |
| 329 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 330 | return error; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Called from xfs_bmap_add_attrfork to handle extents format files. |
| 335 | */ |
| 336 | STATIC int /* error */ |
| 337 | xfs_bmap_add_attrfork_extents( |
| 338 | xfs_trans_t *tp, /* transaction pointer */ |
| 339 | xfs_inode_t *ip, /* incore inode pointer */ |
| 340 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 341 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 342 | int *flags) /* inode logging flags */ |
| 343 | { |
| 344 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 345 | int error; /* error return value */ |
| 346 | |
| 347 | if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip)) |
| 348 | return 0; |
| 349 | cur = NULL; |
| 350 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0, |
| 351 | flags, XFS_DATA_FORK); |
| 352 | if (cur) { |
| 353 | cur->bc_private.b.allocated = 0; |
| 354 | xfs_btree_del_cursor(cur, |
| 355 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 356 | } |
| 357 | return error; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Called from xfs_bmap_add_attrfork to handle local format files. |
| 362 | */ |
| 363 | STATIC int /* error */ |
| 364 | xfs_bmap_add_attrfork_local( |
| 365 | xfs_trans_t *tp, /* transaction pointer */ |
| 366 | xfs_inode_t *ip, /* incore inode pointer */ |
| 367 | xfs_fsblock_t *firstblock, /* first block allocated */ |
| 368 | xfs_bmap_free_t *flist, /* blocks to free at commit */ |
| 369 | int *flags) /* inode logging flags */ |
| 370 | { |
| 371 | xfs_da_args_t dargs; /* args for dir/attr code */ |
| 372 | int error; /* error return value */ |
| 373 | xfs_mount_t *mp; /* mount structure pointer */ |
| 374 | |
| 375 | if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip)) |
| 376 | return 0; |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 377 | if (S_ISDIR(ip->i_d.di_mode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | mp = ip->i_mount; |
| 379 | memset(&dargs, 0, sizeof(dargs)); |
| 380 | dargs.dp = ip; |
| 381 | dargs.firstblock = firstblock; |
| 382 | dargs.flist = flist; |
| 383 | dargs.total = mp->m_dirblkfsbs; |
| 384 | dargs.whichfork = XFS_DATA_FORK; |
| 385 | dargs.trans = tp; |
Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 386 | error = xfs_dir2_sf_to_block(&dargs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } else |
| 388 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags, |
| 389 | XFS_DATA_FORK); |
| 390 | return error; |
| 391 | } |
| 392 | |
| 393 | /* |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 394 | * Convert a delayed allocation to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | */ |
| 396 | STATIC int /* error */ |
| 397 | xfs_bmap_add_extent_delay_real( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 398 | struct xfs_bmalloca *bma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 400 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | int diff; /* temp value */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 402 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 405 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 407 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 408 | /* left is 0, right is 1, prev is 2 */ |
| 409 | int rval=0; /* return value (logging flags) */ |
| 410 | int state = 0;/* state bits, accessed thru macros */ |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 411 | xfs_filblks_t da_new; /* new count del alloc blocks used */ |
| 412 | xfs_filblks_t da_old; /* old count del alloc blocks used */ |
| 413 | xfs_filblks_t temp=0; /* value for da_new calculations */ |
| 414 | xfs_filblks_t temp2=0;/* value for da_new calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | int tmp_rval; /* partial logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 417 | ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 418 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 419 | ASSERT(bma->idx >= 0); |
| 420 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 421 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 422 | ASSERT(!bma->cur || |
| 423 | (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 424 | |
| 425 | XFS_STATS_INC(xs_add_exlist); |
| 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | #define LEFT r[0] |
| 428 | #define RIGHT r[1] |
| 429 | #define PREV r[2] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Set up a bunch of variables to make the tests simpler. |
| 433 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 434 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | xfs_bmbt_get_all(ep, &PREV); |
| 436 | new_endoff = new->br_startoff + new->br_blockcount; |
| 437 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 438 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 439 | |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 440 | da_old = startblockval(PREV.br_startblock); |
| 441 | da_new = 0; |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /* |
| 444 | * Set flags determining what part of the previous delayed allocation |
| 445 | * extent is being replaced by a real allocation. |
| 446 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 447 | if (PREV.br_startoff == new->br_startoff) |
| 448 | state |= BMAP_LEFT_FILLING; |
| 449 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 450 | state |= BMAP_RIGHT_FILLING; |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* |
| 453 | * Check and set flags if this segment has a left neighbor. |
| 454 | * Don't set contiguous if the combined extent would be too large. |
| 455 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 456 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 457 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 458 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 459 | |
| 460 | if (isnullstartblock(LEFT.br_startblock)) |
| 461 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 463 | |
| 464 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 465 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 466 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 467 | LEFT.br_state == new->br_state && |
| 468 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 469 | state |= BMAP_LEFT_CONTIG; |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* |
| 472 | * Check and set flags if this segment has a right neighbor. |
| 473 | * Don't set contiguous if the combined extent would be too large. |
| 474 | * Also check for all-three-contiguous being too large. |
| 475 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 476 | if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 477 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 478 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 479 | |
| 480 | if (isnullstartblock(RIGHT.br_startblock)) |
| 481 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 483 | |
| 484 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 485 | new_endoff == RIGHT.br_startoff && |
| 486 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 487 | new->br_state == RIGHT.br_state && |
| 488 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 489 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 490 | BMAP_RIGHT_FILLING)) != |
| 491 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 492 | BMAP_RIGHT_FILLING) || |
| 493 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 494 | <= MAXEXTLEN)) |
| 495 | state |= BMAP_RIGHT_CONTIG; |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | error = 0; |
| 498 | /* |
| 499 | * Switch out based on the FILLING and CONTIG state bits. |
| 500 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 501 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 502 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 503 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 504 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | /* |
| 506 | * Filling in all of a previously delayed allocation extent. |
| 507 | * The left and right neighbors are both contiguous with new. |
| 508 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 509 | bma->idx--; |
| 510 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 511 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | LEFT.br_blockcount + PREV.br_blockcount + |
| 513 | RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 514 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 515 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 516 | xfs_iext_remove(bma->ip, bma->idx + 1, 2, state); |
| 517 | bma->ip->i_d.di_nextents--; |
| 518 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 520 | else { |
| 521 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 522 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 524 | RIGHT.br_blockcount, &i); |
| 525 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 527 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 528 | error = xfs_btree_delete(bma->cur, &i); |
| 529 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 531 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 532 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 533 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 535 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 536 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | LEFT.br_startblock, |
| 538 | LEFT.br_blockcount + |
| 539 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 540 | RIGHT.br_blockcount, LEFT.br_state); |
| 541 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | goto done; |
| 543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | break; |
| 545 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 546 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | /* |
| 548 | * Filling in all of a previously delayed allocation extent. |
| 549 | * The left neighbor is contiguous, the right is not. |
| 550 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 551 | bma->idx--; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 552 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 553 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 554 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 555 | LEFT.br_blockcount + PREV.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 556 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 557 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 558 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 559 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | rval = XFS_ILOG_DEXT; |
| 561 | else { |
| 562 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 563 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 565 | &i); |
| 566 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 568 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 569 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | LEFT.br_startblock, |
| 571 | LEFT.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 572 | PREV.br_blockcount, LEFT.br_state); |
| 573 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | goto done; |
| 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | break; |
| 577 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 578 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* |
| 580 | * Filling in all of a previously delayed allocation extent. |
| 581 | * The right neighbor is contiguous, the left is not. |
| 582 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 583 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
| 585 | xfs_bmbt_set_blockcount(ep, |
| 586 | PREV.br_blockcount + RIGHT.br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 587 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 588 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 589 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
| 590 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | rval = XFS_ILOG_DEXT; |
| 592 | else { |
| 593 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 594 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 596 | RIGHT.br_blockcount, &i); |
| 597 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 599 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 600 | error = xfs_bmbt_update(bma->cur, PREV.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | new->br_startblock, |
| 602 | PREV.br_blockcount + |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 603 | RIGHT.br_blockcount, PREV.br_state); |
| 604 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | goto done; |
| 606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | break; |
| 608 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 609 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | /* |
| 611 | * Filling in all of a previously delayed allocation extent. |
| 612 | * Neither the left nor right neighbors are contiguous with |
| 613 | * the new one. |
| 614 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 615 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | xfs_bmbt_set_startblock(ep, new->br_startblock); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 617 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 618 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 619 | bma->ip->i_d.di_nextents++; |
| 620 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 622 | else { |
| 623 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 624 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 626 | &i); |
| 627 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 629 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 630 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 631 | error = xfs_btree_insert(bma->cur, &i); |
| 632 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 634 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | break; |
| 637 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 638 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* |
| 640 | * Filling in the first part of a previous delayed allocation. |
| 641 | * The left neighbor is contiguous. |
| 642 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 643 | trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
| 644 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | LEFT.br_blockcount + new->br_blockcount); |
| 646 | xfs_bmbt_set_startoff(ep, |
| 647 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 648 | trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 651 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 653 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | rval = XFS_ILOG_DEXT; |
| 655 | else { |
| 656 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 657 | error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | LEFT.br_startblock, LEFT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 659 | &i); |
| 660 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 662 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 663 | error = xfs_bmbt_update(bma->cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | LEFT.br_startblock, |
| 665 | LEFT.br_blockcount + |
| 666 | new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 667 | LEFT.br_state); |
| 668 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | goto done; |
| 670 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 671 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 672 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 673 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 674 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 675 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 676 | bma->idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | break; |
| 678 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 679 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* |
| 681 | * Filling in the first part of a previous delayed allocation. |
| 682 | * The left neighbor is not contiguous. |
| 683 | */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 684 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 686 | temp = PREV.br_blockcount - new->br_blockcount; |
| 687 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 688 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 689 | bma->ip->i_d.di_nextents++; |
| 690 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 692 | else { |
| 693 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 694 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 696 | &i); |
| 697 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 699 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 700 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 701 | error = xfs_btree_insert(bma->cur, &i); |
| 702 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 704 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 706 | |
| 707 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 708 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 709 | bma->firstblock, bma->flist, |
| 710 | &bma->cur, 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | rval |= tmp_rval; |
| 712 | if (error) |
| 713 | goto done; |
| 714 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 715 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 716 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 717 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 718 | ep = xfs_iext_get_ext(ifp, bma->idx + 1); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 719 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 720 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | break; |
| 722 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 723 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | /* |
| 725 | * Filling in the last part of a previous delayed allocation. |
| 726 | * The right neighbor is contiguous with the new allocation. |
| 727 | */ |
| 728 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 729 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 731 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 732 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | new->br_blockcount + RIGHT.br_blockcount, |
| 734 | RIGHT.br_state); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 735 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_); |
| 736 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | rval = XFS_ILOG_DEXT; |
| 738 | else { |
| 739 | rval = 0; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 740 | error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | RIGHT.br_startblock, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 742 | RIGHT.br_blockcount, &i); |
| 743 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 745 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 746 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | new->br_startblock, |
| 748 | new->br_blockcount + |
| 749 | RIGHT.br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 750 | RIGHT.br_state); |
| 751 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | goto done; |
| 753 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 754 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 755 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 756 | startblockval(PREV.br_startblock)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 757 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 758 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 759 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 760 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 761 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | break; |
| 763 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 764 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | /* |
| 766 | * Filling in the last part of a previous delayed allocation. |
| 767 | * The right neighbor is not contiguous. |
| 768 | */ |
| 769 | temp = PREV.br_blockcount - new->br_blockcount; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 770 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | xfs_bmbt_set_blockcount(ep, temp); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 772 | xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state); |
| 773 | bma->ip->i_d.di_nextents++; |
| 774 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 776 | else { |
| 777 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 778 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 780 | &i); |
| 781 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 783 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 784 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 785 | error = xfs_btree_insert(bma->cur, &i); |
| 786 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 788 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 790 | |
| 791 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 792 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 793 | bma->firstblock, bma->flist, &bma->cur, 1, |
| 794 | &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | rval |= tmp_rval; |
| 796 | if (error) |
| 797 | goto done; |
| 798 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 799 | da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 800 | startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 801 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
| 802 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 803 | xfs_bmbt_set_startblock(ep, nullstartblock(da_new)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 804 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 805 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 806 | bma->idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | break; |
| 808 | |
| 809 | case 0: |
| 810 | /* |
| 811 | * Filling in the middle part of a previous delayed allocation. |
| 812 | * Contiguity is impossible here. |
| 813 | * This case is avoided almost all the time. |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 814 | * |
| 815 | * We start with a delayed allocation: |
| 816 | * |
| 817 | * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+ |
| 818 | * PREV @ idx |
| 819 | * |
| 820 | * and we are allocating: |
| 821 | * +rrrrrrrrrrrrrrrrr+ |
| 822 | * new |
| 823 | * |
| 824 | * and we set it up for insertion as: |
| 825 | * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+ |
| 826 | * new |
| 827 | * PREV @ idx LEFT RIGHT |
| 828 | * inserted at idx + 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | */ |
| 830 | temp = new->br_startoff - PREV.br_startoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 832 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 833 | xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */ |
| 834 | LEFT = *new; |
| 835 | RIGHT.br_state = PREV.br_state; |
| 836 | RIGHT.br_startblock = nullstartblock( |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 837 | (int)xfs_bmap_worst_indlen(bma->ip, temp2)); |
bpm@sgi.com | 24446fc | 2011-01-19 17:41:58 +0000 | [diff] [blame] | 838 | RIGHT.br_startoff = new_endoff; |
| 839 | RIGHT.br_blockcount = temp2; |
| 840 | /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 841 | xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state); |
| 842 | bma->ip->i_d.di_nextents++; |
| 843 | if (bma->cur == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 845 | else { |
| 846 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 847 | error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | new->br_startblock, new->br_blockcount, |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 849 | &i); |
| 850 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 852 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 853 | bma->cur->bc_rec.b.br_state = XFS_EXT_NORM; |
| 854 | error = xfs_btree_insert(bma->cur, &i); |
| 855 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 857 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 859 | |
| 860 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 861 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 862 | bma->firstblock, bma->flist, &bma->cur, |
| 863 | 1, &tmp_rval, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | rval |= tmp_rval; |
| 865 | if (error) |
| 866 | goto done; |
| 867 | } |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 868 | temp = xfs_bmap_worst_indlen(bma->ip, temp); |
| 869 | temp2 = xfs_bmap_worst_indlen(bma->ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 870 | diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) - |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 871 | (bma->cur ? bma->cur->bc_private.b.allocated : 0)); |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 872 | if (diff > 0) { |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 873 | error = xfs_icsb_modify_counters(bma->ip->i_mount, |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 874 | XFS_SBS_FDBLOCKS, |
| 875 | -((int64_t)diff), 0); |
| 876 | ASSERT(!error); |
| 877 | if (error) |
| 878 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
Christoph Hellwig | b9b984d | 2011-09-18 20:40:42 +0000 | [diff] [blame] | 880 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 881 | ep = xfs_iext_get_ext(ifp, bma->idx); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 882 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 883 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 884 | trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
| 885 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 886 | nullstartblock((int)temp2)); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 887 | trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 888 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 889 | bma->idx++; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 890 | da_new = temp + temp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | break; |
| 892 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 893 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 894 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 895 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 896 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 897 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 898 | case BMAP_LEFT_CONTIG: |
| 899 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | /* |
| 901 | * These cases are all impossible. |
| 902 | */ |
| 903 | ASSERT(0); |
| 904 | } |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 905 | |
| 906 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 907 | if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 908 | int tmp_logflags; /* partial log flag return val */ |
| 909 | |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 910 | ASSERT(bma->cur == NULL); |
| 911 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 912 | bma->firstblock, bma->flist, &bma->cur, |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 913 | da_old > 0, &tmp_logflags, XFS_DATA_FORK); |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 914 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 915 | if (error) |
| 916 | goto done; |
| 917 | } |
| 918 | |
| 919 | /* adjust for changes in reserved delayed indirect blocks */ |
| 920 | if (da_old || da_new) { |
| 921 | temp = da_new; |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 922 | if (bma->cur) |
| 923 | temp += bma->cur->bc_private.b.allocated; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 924 | ASSERT(temp <= da_old); |
| 925 | if (temp < da_old) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 926 | xfs_icsb_modify_counters(bma->ip->i_mount, |
| 927 | XFS_SBS_FDBLOCKS, |
| 928 | (int64_t)(da_old - temp), 0); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 932 | if (bma->cur) |
| 933 | bma->cur->bc_private.b.allocated = 0; |
| 934 | |
| 935 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | done: |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 937 | bma->logflags |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | return error; |
| 939 | #undef LEFT |
| 940 | #undef RIGHT |
| 941 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 945 | * Convert an unwritten allocation to a real allocation or vice versa. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | */ |
| 947 | STATIC int /* error */ |
| 948 | xfs_bmap_add_extent_unwritten_real( |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 949 | struct xfs_trans *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 951 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | xfs_btree_cur_t **curp, /* if *curp is null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 953 | xfs_bmbt_irec_t *new, /* new data to add to file extents */ |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 954 | xfs_fsblock_t *first, /* pointer to firstblock variable */ |
| 955 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 956 | int *logflagsp) /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | xfs_btree_cur_t *cur; /* btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 959 | xfs_bmbt_rec_host_t *ep; /* extent entry for idx */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | int i; /* temp state */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 962 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | xfs_fileoff_t new_endoff; /* end offset of new entry */ |
| 964 | xfs_exntst_t newext; /* new extent state */ |
| 965 | xfs_exntst_t oldext; /* old extent state */ |
| 966 | xfs_bmbt_irec_t r[3]; /* neighbor extent entries */ |
| 967 | /* left is 0, right is 1, prev is 2 */ |
| 968 | int rval=0; /* return value (logging flags) */ |
| 969 | int state = 0;/* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 971 | *logflagsp = 0; |
| 972 | |
| 973 | cur = *curp; |
| 974 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 975 | |
| 976 | ASSERT(*idx >= 0); |
| 977 | ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
| 978 | ASSERT(!isnullstartblock(new->br_startblock)); |
| 979 | |
| 980 | XFS_STATS_INC(xs_add_exlist); |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | #define LEFT r[0] |
| 983 | #define RIGHT r[1] |
| 984 | #define PREV r[2] |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | /* |
| 987 | * Set up a bunch of variables to make the tests simpler. |
| 988 | */ |
| 989 | error = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 990 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | xfs_bmbt_get_all(ep, &PREV); |
| 992 | newext = new->br_state; |
| 993 | oldext = (newext == XFS_EXT_UNWRITTEN) ? |
| 994 | XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 995 | ASSERT(PREV.br_state == oldext); |
| 996 | new_endoff = new->br_startoff + new->br_blockcount; |
| 997 | ASSERT(PREV.br_startoff <= new->br_startoff); |
| 998 | ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | /* |
| 1001 | * Set flags determining what part of the previous oldext allocation |
| 1002 | * extent is being replaced by a newext allocation. |
| 1003 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1004 | if (PREV.br_startoff == new->br_startoff) |
| 1005 | state |= BMAP_LEFT_FILLING; |
| 1006 | if (PREV.br_startoff + PREV.br_blockcount == new_endoff) |
| 1007 | state |= BMAP_RIGHT_FILLING; |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | /* |
| 1010 | * Check and set flags if this segment has a left neighbor. |
| 1011 | * Don't set contiguous if the combined extent would be too large. |
| 1012 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1013 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1014 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1015 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1016 | |
| 1017 | if (isnullstartblock(LEFT.br_startblock)) |
| 1018 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1020 | |
| 1021 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1022 | LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff && |
| 1023 | LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock && |
| 1024 | LEFT.br_state == newext && |
| 1025 | LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1026 | state |= BMAP_LEFT_CONTIG; |
| 1027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | /* |
| 1029 | * Check and set flags if this segment has a right neighbor. |
| 1030 | * Don't set contiguous if the combined extent would be too large. |
| 1031 | * Also check for all-three-contiguous being too large. |
| 1032 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1033 | 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] | 1034 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1035 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1036 | if (isnullstartblock(RIGHT.br_startblock)) |
| 1037 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1039 | |
| 1040 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1041 | new_endoff == RIGHT.br_startoff && |
| 1042 | new->br_startblock + new->br_blockcount == RIGHT.br_startblock && |
| 1043 | newext == RIGHT.br_state && |
| 1044 | new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN && |
| 1045 | ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1046 | BMAP_RIGHT_FILLING)) != |
| 1047 | (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING | |
| 1048 | BMAP_RIGHT_FILLING) || |
| 1049 | LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount |
| 1050 | <= MAXEXTLEN)) |
| 1051 | state |= BMAP_RIGHT_CONTIG; |
| 1052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * Switch out based on the FILLING and CONTIG state bits. |
| 1055 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1056 | switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1057 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) { |
| 1058 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | |
| 1059 | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | /* |
| 1061 | * Setting all of a previous oldext extent to newext. |
| 1062 | * The left and right neighbors are both contiguous with new. |
| 1063 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1064 | --*idx; |
| 1065 | |
| 1066 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1067 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1069 | RIGHT.br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1070 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1071 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1072 | xfs_iext_remove(ip, *idx + 1, 2, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | ip->i_d.di_nextents -= 2; |
| 1074 | if (cur == NULL) |
| 1075 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1076 | else { |
| 1077 | rval = XFS_ILOG_CORE; |
| 1078 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1079 | RIGHT.br_startblock, |
| 1080 | RIGHT.br_blockcount, &i))) |
| 1081 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1082 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1083 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1085 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1086 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1088 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1089 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1091 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1092 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1094 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1096 | LEFT.br_startblock, |
| 1097 | LEFT.br_blockcount + PREV.br_blockcount + |
| 1098 | RIGHT.br_blockcount, LEFT.br_state))) |
| 1099 | goto done; |
| 1100 | } |
| 1101 | break; |
| 1102 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1103 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | /* |
| 1105 | * Setting all of a previous oldext extent to newext. |
| 1106 | * The left neighbor is contiguous, the right is not. |
| 1107 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1108 | --*idx; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1109 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1110 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1111 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), |
| 1112 | LEFT.br_blockcount + PREV.br_blockcount); |
| 1113 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1114 | |
| 1115 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | ip->i_d.di_nextents--; |
| 1117 | if (cur == NULL) |
| 1118 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1119 | else { |
| 1120 | rval = XFS_ILOG_CORE; |
| 1121 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1122 | PREV.br_startblock, PREV.br_blockcount, |
| 1123 | &i))) |
| 1124 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1125 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1126 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1128 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1129 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1131 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | if ((error = xfs_bmbt_update(cur, LEFT.br_startoff, |
| 1133 | LEFT.br_startblock, |
| 1134 | LEFT.br_blockcount + PREV.br_blockcount, |
| 1135 | LEFT.br_state))) |
| 1136 | goto done; |
| 1137 | } |
| 1138 | break; |
| 1139 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1140 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * Setting all of a previous oldext extent to newext. |
| 1143 | * The right neighbor is contiguous, the left is not. |
| 1144 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1145 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | xfs_bmbt_set_blockcount(ep, |
| 1147 | PREV.br_blockcount + RIGHT.br_blockcount); |
| 1148 | xfs_bmbt_set_state(ep, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1149 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1150 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | ip->i_d.di_nextents--; |
| 1152 | if (cur == NULL) |
| 1153 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1154 | else { |
| 1155 | rval = XFS_ILOG_CORE; |
| 1156 | if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff, |
| 1157 | RIGHT.br_startblock, |
| 1158 | RIGHT.br_blockcount, &i))) |
| 1159 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1160 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 1161 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1163 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1164 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1166 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1168 | new->br_startblock, |
| 1169 | new->br_blockcount + RIGHT.br_blockcount, |
| 1170 | newext))) |
| 1171 | goto done; |
| 1172 | } |
| 1173 | break; |
| 1174 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1175 | case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | /* |
| 1177 | * Setting all of a previous oldext extent to newext. |
| 1178 | * Neither the left nor right neighbors are contiguous with |
| 1179 | * the new one. |
| 1180 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1181 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | xfs_bmbt_set_state(ep, newext); |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | if (cur == NULL) |
| 1186 | rval = XFS_ILOG_DEXT; |
| 1187 | else { |
| 1188 | rval = 0; |
| 1189 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1190 | new->br_startblock, new->br_blockcount, |
| 1191 | &i))) |
| 1192 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1193 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1195 | new->br_startblock, new->br_blockcount, |
| 1196 | newext))) |
| 1197 | goto done; |
| 1198 | } |
| 1199 | break; |
| 1200 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1201 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* |
| 1203 | * Setting the first part of a previous oldext extent to newext. |
| 1204 | * The left neighbor is contiguous. |
| 1205 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1206 | trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_); |
| 1207 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | LEFT.br_blockcount + new->br_blockcount); |
| 1209 | xfs_bmbt_set_startoff(ep, |
| 1210 | PREV.br_startoff + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1211 | trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1212 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1213 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | xfs_bmbt_set_startblock(ep, |
| 1215 | new->br_startblock + new->br_blockcount); |
| 1216 | xfs_bmbt_set_blockcount(ep, |
| 1217 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1218 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1219 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1220 | --*idx; |
| 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | if (cur == NULL) |
| 1223 | rval = XFS_ILOG_DEXT; |
| 1224 | else { |
| 1225 | rval = 0; |
| 1226 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1227 | PREV.br_startblock, PREV.br_blockcount, |
| 1228 | &i))) |
| 1229 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1230 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | if ((error = xfs_bmbt_update(cur, |
| 1232 | PREV.br_startoff + new->br_blockcount, |
| 1233 | PREV.br_startblock + new->br_blockcount, |
| 1234 | PREV.br_blockcount - new->br_blockcount, |
| 1235 | oldext))) |
| 1236 | goto done; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1237 | if ((error = xfs_btree_decrement(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | goto done; |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 1239 | error = xfs_bmbt_update(cur, LEFT.br_startoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | LEFT.br_startblock, |
| 1241 | LEFT.br_blockcount + new->br_blockcount, |
Christoph Hellwig | b0eab14 | 2011-09-18 20:41:06 +0000 | [diff] [blame] | 1242 | LEFT.br_state); |
| 1243 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | goto done; |
| 1245 | } |
| 1246 | break; |
| 1247 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1248 | case BMAP_LEFT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | /* |
| 1250 | * Setting the first part of a previous oldext extent to newext. |
| 1251 | * The left neighbor is not contiguous. |
| 1252 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1253 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | ASSERT(ep && xfs_bmbt_get_state(ep) == oldext); |
| 1255 | xfs_bmbt_set_startoff(ep, new_endoff); |
| 1256 | xfs_bmbt_set_blockcount(ep, |
| 1257 | PREV.br_blockcount - new->br_blockcount); |
| 1258 | xfs_bmbt_set_startblock(ep, |
| 1259 | new->br_startblock + new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1260 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1261 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1262 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | ip->i_d.di_nextents++; |
| 1264 | if (cur == NULL) |
| 1265 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1266 | else { |
| 1267 | rval = XFS_ILOG_CORE; |
| 1268 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1269 | PREV.br_startblock, PREV.br_blockcount, |
| 1270 | &i))) |
| 1271 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1272 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | if ((error = xfs_bmbt_update(cur, |
| 1274 | PREV.br_startoff + new->br_blockcount, |
| 1275 | PREV.br_startblock + new->br_blockcount, |
| 1276 | PREV.br_blockcount - new->br_blockcount, |
| 1277 | oldext))) |
| 1278 | goto done; |
| 1279 | cur->bc_rec.b = *new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1280 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1282 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | break; |
| 1285 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1286 | case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | /* |
| 1288 | * Setting the last part of a previous oldext extent to newext. |
| 1289 | * The right neighbor is contiguous with the new allocation. |
| 1290 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1291 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | xfs_bmbt_set_blockcount(ep, |
| 1293 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1294 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 1295 | |
| 1296 | ++*idx; |
| 1297 | |
| 1298 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1299 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1300 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | new->br_blockcount + RIGHT.br_blockcount, newext); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1302 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | if (cur == NULL) |
| 1305 | rval = XFS_ILOG_DEXT; |
| 1306 | else { |
| 1307 | rval = 0; |
| 1308 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1309 | PREV.br_startblock, |
| 1310 | PREV.br_blockcount, &i))) |
| 1311 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1312 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1314 | PREV.br_startblock, |
| 1315 | PREV.br_blockcount - new->br_blockcount, |
| 1316 | oldext))) |
| 1317 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1318 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | goto done; |
| 1320 | if ((error = xfs_bmbt_update(cur, new->br_startoff, |
| 1321 | new->br_startblock, |
| 1322 | new->br_blockcount + RIGHT.br_blockcount, |
| 1323 | newext))) |
| 1324 | goto done; |
| 1325 | } |
| 1326 | break; |
| 1327 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1328 | case BMAP_RIGHT_FILLING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | /* |
| 1330 | * Setting the last part of a previous oldext extent to newext. |
| 1331 | * The right neighbor is not contiguous. |
| 1332 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1333 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | xfs_bmbt_set_blockcount(ep, |
| 1335 | PREV.br_blockcount - new->br_blockcount); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1336 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1337 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1338 | ++*idx; |
| 1339 | xfs_iext_insert(ip, *idx, 1, new, state); |
| 1340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | ip->i_d.di_nextents++; |
| 1342 | if (cur == NULL) |
| 1343 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1344 | else { |
| 1345 | rval = XFS_ILOG_CORE; |
| 1346 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1347 | PREV.br_startblock, PREV.br_blockcount, |
| 1348 | &i))) |
| 1349 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1350 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | if ((error = xfs_bmbt_update(cur, PREV.br_startoff, |
| 1352 | PREV.br_startblock, |
| 1353 | PREV.br_blockcount - new->br_blockcount, |
| 1354 | oldext))) |
| 1355 | goto done; |
| 1356 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1357 | new->br_startblock, new->br_blockcount, |
| 1358 | &i))) |
| 1359 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1360 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | cur->bc_rec.b.br_state = XFS_EXT_NORM; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1362 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1364 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | break; |
| 1367 | |
| 1368 | case 0: |
| 1369 | /* |
| 1370 | * Setting the middle part of a previous oldext extent to |
| 1371 | * newext. Contiguity is impossible here. |
| 1372 | * One extent becomes three extents. |
| 1373 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1374 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | xfs_bmbt_set_blockcount(ep, |
| 1376 | new->br_startoff - PREV.br_startoff); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1377 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | r[0] = *new; |
| 1380 | r[1].br_startoff = new_endoff; |
| 1381 | r[1].br_blockcount = |
| 1382 | PREV.br_startoff + PREV.br_blockcount - new_endoff; |
| 1383 | r[1].br_startblock = new->br_startblock + new->br_blockcount; |
| 1384 | r[1].br_state = oldext; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1385 | |
| 1386 | ++*idx; |
| 1387 | xfs_iext_insert(ip, *idx, 2, &r[0], state); |
| 1388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | ip->i_d.di_nextents += 2; |
| 1390 | if (cur == NULL) |
| 1391 | rval = XFS_ILOG_CORE | XFS_ILOG_DEXT; |
| 1392 | else { |
| 1393 | rval = XFS_ILOG_CORE; |
| 1394 | if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff, |
| 1395 | PREV.br_startblock, PREV.br_blockcount, |
| 1396 | &i))) |
| 1397 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1398 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | /* new right extent - oldext */ |
| 1400 | if ((error = xfs_bmbt_update(cur, r[1].br_startoff, |
| 1401 | r[1].br_startblock, r[1].br_blockcount, |
| 1402 | r[1].br_state))) |
| 1403 | goto done; |
| 1404 | /* new left extent - oldext */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | cur->bc_rec.b = PREV; |
Tim Shimmin | 6a617dd | 2008-07-18 17:13:04 +1000 | [diff] [blame] | 1406 | cur->bc_rec.b.br_blockcount = |
| 1407 | new->br_startoff - PREV.br_startoff; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1408 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1410 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1411 | /* |
| 1412 | * Reset the cursor to the position of the new extent |
| 1413 | * we are about to insert as we can't trust it after |
| 1414 | * the previous insert. |
| 1415 | */ |
| 1416 | if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff, |
| 1417 | new->br_startblock, new->br_blockcount, |
| 1418 | &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | goto done; |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1420 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | /* new middle extent - newext */ |
Lachlan McIlroy | ddea2d5 | 2008-06-23 13:25:53 +1000 | [diff] [blame] | 1422 | cur->bc_rec.b.br_state = new->br_state; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1423 | if ((error = xfs_btree_insert(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1425 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } |
| 1427 | break; |
| 1428 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1429 | case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1430 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1431 | case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG: |
| 1432 | case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG: |
| 1433 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
| 1434 | case BMAP_LEFT_CONTIG: |
| 1435 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | /* |
| 1437 | * These cases are all impossible. |
| 1438 | */ |
| 1439 | ASSERT(0); |
| 1440 | } |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1441 | |
| 1442 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1443 | if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) { |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1444 | int tmp_logflags; /* partial log flag return val */ |
| 1445 | |
| 1446 | ASSERT(cur == NULL); |
| 1447 | error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur, |
| 1448 | 0, &tmp_logflags, XFS_DATA_FORK); |
| 1449 | *logflagsp |= tmp_logflags; |
| 1450 | if (error) |
| 1451 | goto done; |
| 1452 | } |
| 1453 | |
| 1454 | /* clear out the allocated field, done with it now in any case. */ |
| 1455 | if (cur) { |
| 1456 | cur->bc_private.b.allocated = 0; |
| 1457 | *curp = cur; |
| 1458 | } |
| 1459 | |
| 1460 | xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | done: |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1462 | *logflagsp |= rval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | return error; |
| 1464 | #undef LEFT |
| 1465 | #undef RIGHT |
| 1466 | #undef PREV |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1470 | * Convert a hole to a delayed allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1472 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | xfs_bmap_add_extent_hole_delay( |
| 1474 | xfs_inode_t *ip, /* incore inode pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1475 | xfs_extnum_t *idx, /* extent number to update/insert */ |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 1476 | xfs_bmbt_irec_t *new) /* new data to add to file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1478 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1480 | xfs_filblks_t newlen=0; /* new indirect size */ |
| 1481 | xfs_filblks_t oldlen=0; /* old indirect size */ |
| 1482 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
| 1483 | int state; /* state bits, accessed thru macros */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1484 | xfs_filblks_t temp=0; /* temp for indirect calculations */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1486 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | state = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1488 | ASSERT(isnullstartblock(new->br_startblock)); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | /* |
| 1491 | * Check and set flags if this segment has a left neighbor |
| 1492 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1493 | if (*idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1494 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1495 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (isnullstartblock(left.br_startblock)) |
| 1498 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | /* |
| 1502 | * Check and set flags if the current (right) segment exists. |
| 1503 | * If it doesn't exist, we're converting the hole at end-of-file. |
| 1504 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1505 | 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] | 1506 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1507 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1508 | |
| 1509 | if (isnullstartblock(right.br_startblock)) |
| 1510 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | /* |
| 1514 | * Set contiguity flags on the left and right neighbors. |
| 1515 | * Don't let extents get too large, even if the pieces are contiguous. |
| 1516 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1517 | if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) && |
| 1518 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1519 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1520 | state |= BMAP_LEFT_CONTIG; |
| 1521 | |
| 1522 | if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) && |
| 1523 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1524 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1525 | (!(state & BMAP_LEFT_CONTIG) || |
| 1526 | (left.br_blockcount + new->br_blockcount + |
| 1527 | right.br_blockcount <= MAXEXTLEN))) |
| 1528 | state |= BMAP_RIGHT_CONTIG; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | /* |
| 1531 | * Switch out based on the contiguity flags. |
| 1532 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1533 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1534 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | /* |
| 1536 | * New allocation is contiguous with delayed allocations |
| 1537 | * on the left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1538 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1540 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | temp = left.br_blockcount + new->br_blockcount + |
| 1542 | right.br_blockcount; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1543 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1544 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1545 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1546 | oldlen = startblockval(left.br_startblock) + |
| 1547 | startblockval(new->br_startblock) + |
| 1548 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1550 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1551 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1552 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1553 | |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1554 | xfs_iext_remove(ip, *idx + 1, 1, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | break; |
| 1556 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1557 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | /* |
| 1559 | * New allocation is contiguous with a delayed allocation |
| 1560 | * on the left. |
| 1561 | * Merge the new allocation with the left neighbor. |
| 1562 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1563 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | temp = left.br_blockcount + new->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1565 | |
| 1566 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
| 1567 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1568 | oldlen = startblockval(left.br_startblock) + |
| 1569 | startblockval(new->br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1571 | xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx), |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1572 | nullstartblock((int)newlen)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1573 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | break; |
| 1575 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1576 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | /* |
| 1578 | * New allocation is contiguous with a delayed allocation |
| 1579 | * on the right. |
| 1580 | * Merge the new allocation with the right neighbor. |
| 1581 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1582 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | temp = new->br_blockcount + right.br_blockcount; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1584 | oldlen = startblockval(new->br_startblock) + |
| 1585 | startblockval(right.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | newlen = xfs_bmap_worst_indlen(ip, temp); |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1587 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx), |
| 1588 | new->br_startoff, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1589 | nullstartblock((int)newlen), temp, right.br_state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1590 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | break; |
| 1592 | |
| 1593 | case 0: |
| 1594 | /* |
| 1595 | * New allocation is not contiguous with another |
| 1596 | * delayed allocation. |
| 1597 | * Insert a new entry. |
| 1598 | */ |
| 1599 | oldlen = newlen = 0; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1600 | xfs_iext_insert(ip, *idx, 1, new, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | break; |
| 1602 | } |
| 1603 | if (oldlen != newlen) { |
| 1604 | ASSERT(oldlen > newlen); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 1605 | xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 1606 | (int64_t)(oldlen - newlen), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | /* |
| 1608 | * Nothing to do for disk quota accounting here. |
| 1609 | */ |
| 1610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1614 | * Convert a hole to a real allocation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | */ |
| 1616 | STATIC int /* error */ |
| 1617 | xfs_bmap_add_extent_hole_real( |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1618 | struct xfs_bmalloca *bma, |
| 1619 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | { |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1621 | struct xfs_bmbt_irec *new = &bma->got; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | int i; /* temp state */ |
| 1624 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 1625 | xfs_bmbt_irec_t left; /* left neighbor extent entry */ |
| 1626 | xfs_bmbt_irec_t right; /* right neighbor extent entry */ |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1627 | int rval=0; /* return value (logging flags) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | int state; /* state bits, accessed thru macros */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1630 | ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1631 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1632 | ASSERT(bma->idx >= 0); |
| 1633 | ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec)); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1634 | ASSERT(!isnullstartblock(new->br_startblock)); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1635 | ASSERT(!bma->cur || |
| 1636 | !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL)); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1637 | |
| 1638 | XFS_STATS_INC(xs_add_exlist); |
| 1639 | |
| 1640 | state = 0; |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 1641 | if (whichfork == XFS_ATTR_FORK) |
| 1642 | state |= BMAP_ATTRFORK; |
| 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | /* |
| 1645 | * Check and set flags if this segment has a left neighbor. |
| 1646 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1647 | if (bma->idx > 0) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1648 | state |= BMAP_LEFT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1649 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1650 | if (isnullstartblock(left.br_startblock)) |
| 1651 | state |= BMAP_LEFT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | /* |
| 1655 | * Check and set flags if this segment has a current value. |
| 1656 | * Not true if we're inserting into the "hole" at eof. |
| 1657 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1658 | if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) { |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1659 | state |= BMAP_RIGHT_VALID; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1660 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right); |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1661 | if (isnullstartblock(right.br_startblock)) |
| 1662 | state |= BMAP_RIGHT_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | } |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | /* |
| 1666 | * We're inserting a real allocation between "left" and "right". |
| 1667 | * Set the contiguity flags. Don't let extents get too large. |
| 1668 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1669 | if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) && |
| 1670 | left.br_startoff + left.br_blockcount == new->br_startoff && |
| 1671 | left.br_startblock + left.br_blockcount == new->br_startblock && |
| 1672 | left.br_state == new->br_state && |
| 1673 | left.br_blockcount + new->br_blockcount <= MAXEXTLEN) |
| 1674 | state |= BMAP_LEFT_CONTIG; |
| 1675 | |
| 1676 | if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) && |
| 1677 | new->br_startoff + new->br_blockcount == right.br_startoff && |
| 1678 | new->br_startblock + new->br_blockcount == right.br_startblock && |
| 1679 | new->br_state == right.br_state && |
| 1680 | new->br_blockcount + right.br_blockcount <= MAXEXTLEN && |
| 1681 | (!(state & BMAP_LEFT_CONTIG) || |
| 1682 | left.br_blockcount + new->br_blockcount + |
| 1683 | right.br_blockcount <= MAXEXTLEN)) |
| 1684 | state |= BMAP_RIGHT_CONTIG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1686 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | /* |
| 1688 | * Select which case we're in here, and implement it. |
| 1689 | */ |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1690 | switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) { |
| 1691 | case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | /* |
| 1693 | * New allocation is contiguous with real allocations on the |
| 1694 | * left and on the right. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 1695 | * Merge all three into a single extent record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1697 | --bma->idx; |
| 1698 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1699 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | left.br_blockcount + new->br_blockcount + |
| 1701 | right.br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1702 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1703 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1704 | xfs_iext_remove(bma->ip, bma->idx + 1, 1, state); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 1705 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1706 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 1707 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1); |
| 1708 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1709 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1710 | } else { |
| 1711 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1712 | error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff, |
| 1713 | right.br_startblock, right.br_blockcount, |
| 1714 | &i); |
| 1715 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1716 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1717 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1718 | error = xfs_btree_delete(bma->cur, &i); |
| 1719 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1720 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1721 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1722 | error = xfs_btree_decrement(bma->cur, 0, &i); |
| 1723 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1724 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1725 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1726 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1727 | left.br_startblock, |
| 1728 | left.br_blockcount + |
| 1729 | new->br_blockcount + |
| 1730 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1731 | left.br_state); |
| 1732 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1733 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1735 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1737 | case BMAP_LEFT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | /* |
| 1739 | * New allocation is contiguous with a real allocation |
| 1740 | * on the left. |
| 1741 | * Merge the new allocation with the left neighbor. |
| 1742 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1743 | --bma->idx; |
| 1744 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1745 | xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | left.br_blockcount + new->br_blockcount); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1747 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1748 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1749 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1750 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1751 | } else { |
| 1752 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1753 | error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff, |
| 1754 | left.br_startblock, left.br_blockcount, |
| 1755 | &i); |
| 1756 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1757 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1758 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1759 | error = xfs_bmbt_update(bma->cur, left.br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1760 | left.br_startblock, |
| 1761 | left.br_blockcount + |
| 1762 | new->br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1763 | left.br_state); |
| 1764 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1765 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1767 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | |
Christoph Hellwig | 7574aa9 | 2009-11-25 00:00:19 +0000 | [diff] [blame] | 1769 | case BMAP_RIGHT_CONTIG: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | /* |
| 1771 | * New allocation is contiguous with a real allocation |
| 1772 | * on the right. |
| 1773 | * Merge the new allocation with the right neighbor. |
| 1774 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1775 | trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_); |
| 1776 | xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx), |
Christoph Hellwig | 2f2b322 | 2011-05-11 15:04:06 +0000 | [diff] [blame] | 1777 | new->br_startoff, new->br_startblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | new->br_blockcount + right.br_blockcount, |
| 1779 | right.br_state); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1780 | trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1781 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1782 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1783 | rval = xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1784 | } else { |
| 1785 | rval = 0; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1786 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1787 | right.br_startoff, |
| 1788 | right.br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1789 | right.br_blockcount, &i); |
| 1790 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1791 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1792 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1793 | error = xfs_bmbt_update(bma->cur, new->br_startoff, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1794 | new->br_startblock, |
| 1795 | new->br_blockcount + |
| 1796 | right.br_blockcount, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1797 | right.br_state); |
| 1798 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1799 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1801 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | |
| 1803 | case 0: |
| 1804 | /* |
| 1805 | * New allocation is not contiguous with another |
| 1806 | * real allocation. |
| 1807 | * Insert a new entry. |
| 1808 | */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1809 | xfs_iext_insert(bma->ip, bma->idx, 1, new, state); |
| 1810 | XFS_IFORK_NEXT_SET(bma->ip, whichfork, |
| 1811 | XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1); |
| 1812 | if (bma->cur == NULL) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1813 | rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1814 | } else { |
| 1815 | rval = XFS_ILOG_CORE; |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1816 | error = xfs_bmbt_lookup_eq(bma->cur, |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1817 | new->br_startoff, |
| 1818 | new->br_startblock, |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1819 | new->br_blockcount, &i); |
| 1820 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1821 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1822 | XFS_WANT_CORRUPTED_GOTO(i == 0, done); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1823 | bma->cur->bc_rec.b.br_state = new->br_state; |
| 1824 | error = xfs_btree_insert(bma->cur, &i); |
| 1825 | if (error) |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1826 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 1827 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1829 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1831 | |
| 1832 | /* convert to a btree if necessary */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 1833 | if (xfs_bmap_needs_btree(bma->ip, whichfork)) { |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1834 | int tmp_logflags; /* partial log flag return val */ |
| 1835 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1836 | ASSERT(bma->cur == NULL); |
| 1837 | error = xfs_bmap_extents_to_btree(bma->tp, bma->ip, |
| 1838 | bma->firstblock, bma->flist, &bma->cur, |
| 1839 | 0, &tmp_logflags, whichfork); |
| 1840 | bma->logflags |= tmp_logflags; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 1841 | if (error) |
| 1842 | goto done; |
| 1843 | } |
| 1844 | |
| 1845 | /* clear out the allocated field, done with it now in any case. */ |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1846 | if (bma->cur) |
| 1847 | bma->cur->bc_private.b.allocated = 0; |
| 1848 | |
| 1849 | xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork); |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1850 | done: |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 1851 | bma->logflags |= rval; |
Olaf Weber | 3e57ecf | 2006-06-09 14:48:12 +1000 | [diff] [blame] | 1852 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 1855 | /* |
| 1856 | * Adjust the size of the new extent based on di_extsize and rt extsize. |
| 1857 | */ |
| 1858 | STATIC int |
| 1859 | xfs_bmap_extsize_align( |
| 1860 | xfs_mount_t *mp, |
| 1861 | xfs_bmbt_irec_t *gotp, /* next extent pointer */ |
| 1862 | xfs_bmbt_irec_t *prevp, /* previous extent pointer */ |
| 1863 | xfs_extlen_t extsz, /* align to this extent size */ |
| 1864 | int rt, /* is this a realtime inode? */ |
| 1865 | int eof, /* is extent at end-of-file? */ |
| 1866 | int delay, /* creating delalloc extent? */ |
| 1867 | int convert, /* overwriting unwritten extent? */ |
| 1868 | xfs_fileoff_t *offp, /* in/out: aligned offset */ |
| 1869 | xfs_extlen_t *lenp) /* in/out: aligned length */ |
| 1870 | { |
| 1871 | xfs_fileoff_t orig_off; /* original offset */ |
| 1872 | xfs_extlen_t orig_alen; /* original length */ |
| 1873 | xfs_fileoff_t orig_end; /* original off+len */ |
| 1874 | xfs_fileoff_t nexto; /* next file offset */ |
| 1875 | xfs_fileoff_t prevo; /* previous file offset */ |
| 1876 | xfs_fileoff_t align_off; /* temp for offset */ |
| 1877 | xfs_extlen_t align_alen; /* temp for length */ |
| 1878 | xfs_extlen_t temp; /* temp for calculations */ |
| 1879 | |
| 1880 | if (convert) |
| 1881 | return 0; |
| 1882 | |
| 1883 | orig_off = align_off = *offp; |
| 1884 | orig_alen = align_alen = *lenp; |
| 1885 | orig_end = orig_off + orig_alen; |
| 1886 | |
| 1887 | /* |
| 1888 | * If this request overlaps an existing extent, then don't |
| 1889 | * attempt to perform any additional alignment. |
| 1890 | */ |
| 1891 | if (!delay && !eof && |
| 1892 | (orig_off >= gotp->br_startoff) && |
| 1893 | (orig_end <= gotp->br_startoff + gotp->br_blockcount)) { |
| 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * If the file offset is unaligned vs. the extent size |
| 1899 | * we need to align it. This will be possible unless |
| 1900 | * the file was previously written with a kernel that didn't |
| 1901 | * perform this alignment, or if a truncate shot us in the |
| 1902 | * foot. |
| 1903 | */ |
| 1904 | temp = do_mod(orig_off, extsz); |
| 1905 | if (temp) { |
| 1906 | align_alen += temp; |
| 1907 | align_off -= temp; |
| 1908 | } |
| 1909 | /* |
| 1910 | * Same adjustment for the end of the requested area. |
| 1911 | */ |
| 1912 | if ((temp = (align_alen % extsz))) { |
| 1913 | align_alen += extsz - temp; |
| 1914 | } |
| 1915 | /* |
| 1916 | * If the previous block overlaps with this proposed allocation |
| 1917 | * then move the start forward without adjusting the length. |
| 1918 | */ |
| 1919 | if (prevp->br_startoff != NULLFILEOFF) { |
| 1920 | if (prevp->br_startblock == HOLESTARTBLOCK) |
| 1921 | prevo = prevp->br_startoff; |
| 1922 | else |
| 1923 | prevo = prevp->br_startoff + prevp->br_blockcount; |
| 1924 | } else |
| 1925 | prevo = 0; |
| 1926 | if (align_off != orig_off && align_off < prevo) |
| 1927 | align_off = prevo; |
| 1928 | /* |
| 1929 | * If the next block overlaps with this proposed allocation |
| 1930 | * then move the start back without adjusting the length, |
| 1931 | * but not before offset 0. |
| 1932 | * This may of course make the start overlap previous block, |
| 1933 | * and if we hit the offset 0 limit then the next block |
| 1934 | * can still overlap too. |
| 1935 | */ |
| 1936 | if (!eof && gotp->br_startoff != NULLFILEOFF) { |
| 1937 | if ((delay && gotp->br_startblock == HOLESTARTBLOCK) || |
| 1938 | (!delay && gotp->br_startblock == DELAYSTARTBLOCK)) |
| 1939 | nexto = gotp->br_startoff + gotp->br_blockcount; |
| 1940 | else |
| 1941 | nexto = gotp->br_startoff; |
| 1942 | } else |
| 1943 | nexto = NULLFILEOFF; |
| 1944 | if (!eof && |
| 1945 | align_off + align_alen != orig_end && |
| 1946 | align_off + align_alen > nexto) |
| 1947 | align_off = nexto > align_alen ? nexto - align_alen : 0; |
| 1948 | /* |
| 1949 | * If we're now overlapping the next or previous extent that |
| 1950 | * means we can't fit an extsz piece in this hole. Just move |
| 1951 | * the start forward to the first valid spot and set |
| 1952 | * the length so we hit the end. |
| 1953 | */ |
| 1954 | if (align_off != orig_off && align_off < prevo) |
| 1955 | align_off = prevo; |
| 1956 | if (align_off + align_alen != orig_end && |
| 1957 | align_off + align_alen > nexto && |
| 1958 | nexto != NULLFILEOFF) { |
| 1959 | ASSERT(nexto > prevo); |
| 1960 | align_alen = nexto - align_off; |
| 1961 | } |
| 1962 | |
| 1963 | /* |
| 1964 | * If realtime, and the result isn't a multiple of the realtime |
| 1965 | * extent size we need to remove blocks until it is. |
| 1966 | */ |
| 1967 | if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) { |
| 1968 | /* |
| 1969 | * We're not covering the original request, or |
| 1970 | * we won't be able to once we fix the length. |
| 1971 | */ |
| 1972 | if (orig_off < align_off || |
| 1973 | orig_end > align_off + align_alen || |
| 1974 | align_alen - temp < orig_alen) |
| 1975 | return XFS_ERROR(EINVAL); |
| 1976 | /* |
| 1977 | * Try to fix it by moving the start up. |
| 1978 | */ |
| 1979 | if (align_off + temp <= orig_off) { |
| 1980 | align_alen -= temp; |
| 1981 | align_off += temp; |
| 1982 | } |
| 1983 | /* |
| 1984 | * Try to fix it by moving the end in. |
| 1985 | */ |
| 1986 | else if (align_off + align_alen - temp >= orig_end) |
| 1987 | align_alen -= temp; |
| 1988 | /* |
| 1989 | * Set the start to the minimum then trim the length. |
| 1990 | */ |
| 1991 | else { |
| 1992 | align_alen -= orig_off - align_off; |
| 1993 | align_off = orig_off; |
| 1994 | align_alen -= align_alen % mp->m_sb.sb_rextsize; |
| 1995 | } |
| 1996 | /* |
| 1997 | * Result doesn't cover the request, fail it. |
| 1998 | */ |
| 1999 | if (orig_off < align_off || orig_end > align_off + align_alen) |
| 2000 | return XFS_ERROR(EINVAL); |
| 2001 | } else { |
| 2002 | ASSERT(orig_off >= align_off); |
| 2003 | ASSERT(orig_end <= align_off + align_alen); |
| 2004 | } |
| 2005 | |
| 2006 | #ifdef DEBUG |
| 2007 | if (!eof && gotp->br_startoff != NULLFILEOFF) |
| 2008 | ASSERT(align_off + align_alen <= gotp->br_startoff); |
| 2009 | if (prevp->br_startoff != NULLFILEOFF) |
| 2010 | ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount); |
| 2011 | #endif |
| 2012 | |
| 2013 | *lenp = align_alen; |
| 2014 | *offp = align_off; |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | #define XFS_ALLOC_GAP_UNITS 4 |
| 2019 | |
David Chinner | c2b1cba | 2008-04-10 12:21:40 +1000 | [diff] [blame] | 2020 | STATIC void |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2021 | xfs_bmap_adjacent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2023 | { |
| 2024 | xfs_fsblock_t adjust; /* adjustment to block numbers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
| 2026 | xfs_mount_t *mp; /* mount point structure */ |
| 2027 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2028 | int rt; /* true if inode is realtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
| 2030 | #define ISVALID(x,y) \ |
| 2031 | (rt ? \ |
| 2032 | (x) < mp->m_sb.sb_rblocks : \ |
| 2033 | XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \ |
| 2034 | XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \ |
| 2035 | XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks) |
| 2036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | mp = ap->ip->i_mount; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2038 | nullfb = *ap->firstblock == NULLFSBLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2040 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | /* |
| 2042 | * If allocating at eof, and there's a previous real block, |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2043 | * try to use its last block as our starting point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2045 | if (ap->eof && ap->prev.br_startoff != NULLFILEOFF && |
| 2046 | !isnullstartblock(ap->prev.br_startblock) && |
| 2047 | ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount, |
| 2048 | ap->prev.br_startblock)) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2049 | ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | /* |
| 2051 | * Adjust for the gap between prevp and us. |
| 2052 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2053 | adjust = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2054 | (ap->prev.br_startoff + ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | if (adjust && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2056 | ISVALID(ap->blkno + adjust, ap->prev.br_startblock)) |
| 2057 | ap->blkno += adjust; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | /* |
| 2060 | * If not at eof, then compare the two neighbor blocks. |
| 2061 | * Figure out whether either one gives us a good starting point, |
| 2062 | * and pick the better one. |
| 2063 | */ |
| 2064 | else if (!ap->eof) { |
| 2065 | xfs_fsblock_t gotbno; /* right side block number */ |
| 2066 | xfs_fsblock_t gotdiff=0; /* right side difference */ |
| 2067 | xfs_fsblock_t prevbno; /* left side block number */ |
| 2068 | xfs_fsblock_t prevdiff=0; /* left side difference */ |
| 2069 | |
| 2070 | /* |
| 2071 | * If there's a previous (left) block, select a requested |
| 2072 | * start block based on it. |
| 2073 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2074 | if (ap->prev.br_startoff != NULLFILEOFF && |
| 2075 | !isnullstartblock(ap->prev.br_startblock) && |
| 2076 | (prevbno = ap->prev.br_startblock + |
| 2077 | ap->prev.br_blockcount) && |
| 2078 | ISVALID(prevbno, ap->prev.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | /* |
| 2080 | * Calculate gap to end of previous block. |
| 2081 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2082 | adjust = prevdiff = ap->offset - |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2083 | (ap->prev.br_startoff + |
| 2084 | ap->prev.br_blockcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | /* |
| 2086 | * Figure the startblock based on the previous block's |
| 2087 | * end and the gap size. |
| 2088 | * Heuristic! |
| 2089 | * If the gap is large relative to the piece we're |
| 2090 | * allocating, or using it gives us an invalid block |
| 2091 | * number, then just use the end of the previous block. |
| 2092 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2093 | if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | ISVALID(prevbno + prevdiff, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2095 | ap->prev.br_startblock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | prevbno += adjust; |
| 2097 | else |
| 2098 | prevdiff += adjust; |
| 2099 | /* |
| 2100 | * If the firstblock forbids it, can't use it, |
| 2101 | * must use default. |
| 2102 | */ |
| 2103 | if (!rt && !nullfb && |
| 2104 | XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno) |
| 2105 | prevbno = NULLFSBLOCK; |
| 2106 | } |
| 2107 | /* |
| 2108 | * No previous block or can't follow it, just default. |
| 2109 | */ |
| 2110 | else |
| 2111 | prevbno = NULLFSBLOCK; |
| 2112 | /* |
| 2113 | * If there's a following (right) block, select a requested |
| 2114 | * start block based on it. |
| 2115 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2116 | if (!isnullstartblock(ap->got.br_startblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | /* |
| 2118 | * Calculate gap to start of next block. |
| 2119 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2120 | adjust = gotdiff = ap->got.br_startoff - ap->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | /* |
| 2122 | * Figure the startblock based on the next block's |
| 2123 | * start and the gap size. |
| 2124 | */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2125 | gotbno = ap->got.br_startblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | /* |
| 2127 | * Heuristic! |
| 2128 | * If the gap is large relative to the piece we're |
| 2129 | * allocating, or using it gives us an invalid block |
| 2130 | * number, then just use the start of the next block |
| 2131 | * offset by our length. |
| 2132 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2133 | if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | ISVALID(gotbno - gotdiff, gotbno)) |
| 2135 | gotbno -= adjust; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2136 | else if (ISVALID(gotbno - ap->length, gotbno)) { |
| 2137 | gotbno -= ap->length; |
| 2138 | gotdiff += adjust - ap->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | } else |
| 2140 | gotdiff += adjust; |
| 2141 | /* |
| 2142 | * If the firstblock forbids it, can't use it, |
| 2143 | * must use default. |
| 2144 | */ |
| 2145 | if (!rt && !nullfb && |
| 2146 | XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno) |
| 2147 | gotbno = NULLFSBLOCK; |
| 2148 | } |
| 2149 | /* |
| 2150 | * No next block, just default. |
| 2151 | */ |
| 2152 | else |
| 2153 | gotbno = NULLFSBLOCK; |
| 2154 | /* |
| 2155 | * If both valid, pick the better one, else the only good |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2156 | * one, else ap->blkno is already set (to 0 or the inode block). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | */ |
| 2158 | if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2159 | ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | else if (prevbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2161 | ap->blkno = prevbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | else if (gotbno != NULLFSBLOCK) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2163 | ap->blkno = gotbno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2165 | #undef ISVALID |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | STATIC int |
| 2169 | xfs_bmap_rtalloc( |
| 2170 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2171 | { |
| 2172 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2173 | int error; /* error return value */ |
| 2174 | xfs_mount_t *mp; /* mount point structure */ |
| 2175 | xfs_extlen_t prod = 0; /* product factor for allocators */ |
| 2176 | xfs_extlen_t ralen = 0; /* realtime allocation length */ |
| 2177 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2178 | xfs_rtblock_t rtb; |
| 2179 | |
| 2180 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2181 | align = xfs_get_extsz_hint(ap->ip); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2182 | prod = align / mp->m_sb.sb_rextsize; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2183 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2184 | align, 1, ap->eof, 0, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2185 | ap->conv, &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2186 | if (error) |
| 2187 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2188 | ASSERT(ap->length); |
| 2189 | ASSERT(ap->length % mp->m_sb.sb_rextsize == 0); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2190 | |
| 2191 | /* |
| 2192 | * If the offset & length are not perfectly aligned |
| 2193 | * then kill prod, it will just get us in trouble. |
| 2194 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2195 | if (do_mod(ap->offset, align) || ap->length % align) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2196 | prod = 1; |
| 2197 | /* |
| 2198 | * Set ralen to be the actual requested length in rtextents. |
| 2199 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2200 | ralen = ap->length / mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2201 | /* |
| 2202 | * If the old value was close enough to MAXEXTLEN that |
| 2203 | * we rounded up to it, cut it back so it's valid again. |
| 2204 | * Note that if it's a really large request (bigger than |
| 2205 | * MAXEXTLEN), we don't hear about that number, and can't |
| 2206 | * adjust the starting point to match it. |
| 2207 | */ |
| 2208 | if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN) |
| 2209 | ralen = MAXEXTLEN / mp->m_sb.sb_rextsize; |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* |
| 2212 | * Lock out other modifications to the RT bitmap inode. |
| 2213 | */ |
Christoph Hellwig | 1050c71 | 2011-02-13 13:25:31 +0000 | [diff] [blame] | 2214 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 2215 | xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 04e9945 | 2011-01-25 09:06:19 +0000 | [diff] [blame] | 2216 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2217 | /* |
| 2218 | * If it's an allocation to an empty file at offset 0, |
| 2219 | * pick an extent that will space things out in the rt area. |
| 2220 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2221 | if (ap->eof && ap->offset == 0) { |
Andrew Morton | 0892ccd | 2007-06-28 16:46:56 +1000 | [diff] [blame] | 2222 | xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */ |
| 2223 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2224 | error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx); |
| 2225 | if (error) |
| 2226 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2227 | ap->blkno = rtx * mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2228 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2229 | ap->blkno = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | xfs_bmap_adjacent(ap); |
| 2233 | |
| 2234 | /* |
| 2235 | * Realtime allocation, done through xfs_rtallocate_extent. |
| 2236 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2237 | atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO; |
| 2238 | do_div(ap->blkno, mp->m_sb.sb_rextsize); |
| 2239 | rtb = ap->blkno; |
| 2240 | ap->length = ralen; |
| 2241 | if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2242 | &ralen, atype, ap->wasdel, prod, &rtb))) |
| 2243 | return error; |
| 2244 | if (rtb == NULLFSBLOCK && prod > 1 && |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2245 | (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, |
| 2246 | ap->length, &ralen, atype, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2247 | ap->wasdel, 1, &rtb))) |
| 2248 | return error; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2249 | ap->blkno = rtb; |
| 2250 | if (ap->blkno != NULLFSBLOCK) { |
| 2251 | ap->blkno *= mp->m_sb.sb_rextsize; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2252 | ralen *= mp->m_sb.sb_rextsize; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2253 | ap->length = ralen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2254 | ap->ip->i_d.di_nblocks += ralen; |
| 2255 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2256 | if (ap->wasdel) |
| 2257 | ap->ip->i_delayed_blks -= ralen; |
| 2258 | /* |
| 2259 | * Adjust the disk quota also. This was reserved |
| 2260 | * earlier. |
| 2261 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2262 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2263 | ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT : |
| 2264 | XFS_TRANS_DQ_RTBCOUNT, (long) ralen); |
| 2265 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2266 | ap->length = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2267 | } |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
| 2271 | STATIC int |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2272 | xfs_bmap_btalloc_nullfb( |
| 2273 | struct xfs_bmalloca *ap, |
| 2274 | struct xfs_alloc_arg *args, |
| 2275 | xfs_extlen_t *blen) |
| 2276 | { |
| 2277 | struct xfs_mount *mp = ap->ip->i_mount; |
| 2278 | struct xfs_perag *pag; |
| 2279 | xfs_agnumber_t ag, startag; |
| 2280 | int notinit = 0; |
| 2281 | int error; |
| 2282 | |
| 2283 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) |
| 2284 | args->type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2285 | else |
| 2286 | args->type = XFS_ALLOCTYPE_START_BNO; |
| 2287 | args->total = ap->total; |
| 2288 | |
| 2289 | /* |
| 2290 | * Search for an allocation group with a single extent large enough |
| 2291 | * for the request. If one isn't found, then adjust the minimum |
| 2292 | * allocation size to the largest space found. |
| 2293 | */ |
| 2294 | startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno); |
| 2295 | if (startag == NULLAGNUMBER) |
| 2296 | startag = ag = 0; |
| 2297 | |
| 2298 | pag = xfs_perag_get(mp, ag); |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2299 | while (*blen < args->maxlen) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2300 | if (!pag->pagf_init) { |
| 2301 | error = xfs_alloc_pagf_init(mp, args->tp, ag, |
| 2302 | XFS_ALLOC_FLAG_TRYLOCK); |
| 2303 | if (error) { |
| 2304 | xfs_perag_put(pag); |
| 2305 | return error; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * See xfs_alloc_fix_freelist... |
| 2311 | */ |
| 2312 | if (pag->pagf_init) { |
| 2313 | xfs_extlen_t longest; |
| 2314 | longest = xfs_alloc_longest_free_extent(mp, pag); |
| 2315 | if (*blen < longest) |
| 2316 | *blen = longest; |
| 2317 | } else |
| 2318 | notinit = 1; |
| 2319 | |
| 2320 | if (xfs_inode_is_filestream(ap->ip)) { |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2321 | if (*blen >= args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2322 | break; |
| 2323 | |
| 2324 | if (ap->userdata) { |
| 2325 | /* |
| 2326 | * If startag is an invalid AG, we've |
| 2327 | * come here once before and |
| 2328 | * xfs_filestream_new_ag picked the |
| 2329 | * best currently available. |
| 2330 | * |
| 2331 | * Don't continue looping, since we |
| 2332 | * could loop forever. |
| 2333 | */ |
| 2334 | if (startag == NULLAGNUMBER) |
| 2335 | break; |
| 2336 | |
| 2337 | error = xfs_filestream_new_ag(ap, &ag); |
| 2338 | xfs_perag_put(pag); |
| 2339 | if (error) |
| 2340 | return error; |
| 2341 | |
| 2342 | /* loop again to set 'blen'*/ |
| 2343 | startag = NULLAGNUMBER; |
| 2344 | pag = xfs_perag_get(mp, ag); |
| 2345 | continue; |
| 2346 | } |
| 2347 | } |
| 2348 | if (++ag == mp->m_sb.sb_agcount) |
| 2349 | ag = 0; |
| 2350 | if (ag == startag) |
| 2351 | break; |
| 2352 | xfs_perag_put(pag); |
| 2353 | pag = xfs_perag_get(mp, ag); |
| 2354 | } |
| 2355 | xfs_perag_put(pag); |
| 2356 | |
| 2357 | /* |
| 2358 | * Since the above loop did a BUF_TRYLOCK, it is |
| 2359 | * possible that there is space for this request. |
| 2360 | */ |
| 2361 | if (notinit || *blen < ap->minlen) |
| 2362 | args->minlen = ap->minlen; |
| 2363 | /* |
| 2364 | * If the best seen length is less than the request |
| 2365 | * length, use the best as the minimum. |
| 2366 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2367 | else if (*blen < args->maxlen) |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2368 | args->minlen = *blen; |
| 2369 | /* |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2370 | * Otherwise we've seen an extent as big as maxlen, |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2371 | * use that as the minimum. |
| 2372 | */ |
| 2373 | else |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2374 | args->minlen = args->maxlen; |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2375 | |
| 2376 | /* |
| 2377 | * set the failure fallback case to look in the selected |
| 2378 | * AG as the stream may have moved. |
| 2379 | */ |
| 2380 | if (xfs_inode_is_filestream(ap->ip)) |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2381 | ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0); |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2382 | |
| 2383 | return 0; |
| 2384 | } |
| 2385 | |
| 2386 | STATIC int |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2387 | xfs_bmap_btalloc( |
| 2388 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2389 | { |
| 2390 | xfs_mount_t *mp; /* mount point structure */ |
| 2391 | xfs_alloctype_t atype = 0; /* type for allocation routines */ |
| 2392 | xfs_extlen_t align; /* minimum allocation alignment */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2393 | xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2394 | xfs_agnumber_t ag; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2395 | xfs_alloc_arg_t args; |
| 2396 | xfs_extlen_t blen; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2397 | xfs_extlen_t nextminlen = 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2398 | int nullfb; /* true if ap->firstblock isn't set */ |
| 2399 | int isaligned; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2400 | int tryagain; |
| 2401 | int error; |
| 2402 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 2403 | ASSERT(ap->length); |
| 2404 | |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2405 | mp = ap->ip->i_mount; |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2406 | align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2407 | if (unlikely(align)) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 2408 | error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2409 | align, 0, ap->eof, 0, ap->conv, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2410 | &ap->offset, &ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2411 | ASSERT(!error); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2412 | ASSERT(ap->length); |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2413 | } |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2414 | nullfb = *ap->firstblock == NULLFSBLOCK; |
| 2415 | fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2416 | if (nullfb) { |
| 2417 | if (ap->userdata && xfs_inode_is_filestream(ap->ip)) { |
| 2418 | ag = xfs_filestream_lookup_ag(ap->ip); |
| 2419 | ag = (ag != NULLAGNUMBER) ? ag : 0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2420 | ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2421 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2422 | ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino); |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2423 | } |
| 2424 | } else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2425 | ap->blkno = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2426 | |
| 2427 | xfs_bmap_adjacent(ap); |
| 2428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | /* |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2430 | * If allowed, use ap->blkno; otherwise must use firstblock since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | * it's in the right allocation group. |
| 2432 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2433 | if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | ; |
| 2435 | else |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2436 | ap->blkno = *ap->firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | * Normal allocation, done through xfs_alloc_vextent. |
| 2439 | */ |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2440 | tryagain = isaligned = 0; |
| 2441 | args.tp = ap->tp; |
| 2442 | args.mp = mp; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2443 | args.fsbno = ap->blkno; |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2444 | |
| 2445 | /* Trim the allocation back to the maximum an AG can fit. */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2446 | args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp)); |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2447 | args.firstblock = *ap->firstblock; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2448 | blen = 0; |
| 2449 | if (nullfb) { |
Christoph Hellwig | c467c04 | 2010-02-15 23:34:42 +0000 | [diff] [blame] | 2450 | error = xfs_bmap_btalloc_nullfb(ap, &args, &blen); |
| 2451 | if (error) |
| 2452 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2453 | } else if (ap->flist->xbf_low) { |
David Chinner | 2a82b8b | 2007-07-11 11:09:12 +1000 | [diff] [blame] | 2454 | if (xfs_inode_is_filestream(ap->ip)) |
| 2455 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2456 | else |
| 2457 | args.type = XFS_ALLOCTYPE_START_BNO; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2458 | args.total = args.minlen = ap->minlen; |
| 2459 | } else { |
| 2460 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 2461 | args.total = ap->total; |
| 2462 | args.minlen = ap->minlen; |
| 2463 | } |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 2464 | /* apply extent size hints if obtained earlier */ |
| 2465 | if (unlikely(align)) { |
| 2466 | args.prod = align; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2467 | if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2468 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2469 | } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2470 | args.prod = 1; |
| 2471 | args.mod = 0; |
| 2472 | } else { |
Tim Shimmin | e6a4b37 | 2007-11-23 16:30:42 +1100 | [diff] [blame] | 2473 | args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2474 | if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod)))) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2475 | args.mod = (xfs_extlen_t)(args.prod - args.mod); |
| 2476 | } |
| 2477 | /* |
| 2478 | * If we are not low on available data blocks, and the |
| 2479 | * underlying logical volume manager is a stripe, and |
| 2480 | * the file offset is zero then try to allocate data |
| 2481 | * blocks on stripe unit boundary. |
| 2482 | * NOTE: ap->aeof is only set if the allocation length |
| 2483 | * is >= the stripe unit and the allocation offset is |
| 2484 | * at the end of file. |
| 2485 | */ |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2486 | if (!ap->flist->xbf_low && ap->aeof) { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2487 | if (!ap->offset) { |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2488 | args.alignment = mp->m_dalign; |
| 2489 | atype = args.type; |
| 2490 | isaligned = 1; |
| 2491 | /* |
| 2492 | * Adjust for alignment |
| 2493 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2494 | if (blen > args.alignment && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2495 | args.minlen = blen - args.alignment; |
| 2496 | args.minalignslop = 0; |
| 2497 | } else { |
| 2498 | /* |
| 2499 | * First try an exact bno allocation. |
| 2500 | * If it fails then do a near or start bno |
| 2501 | * allocation with alignment turned on. |
| 2502 | */ |
| 2503 | atype = args.type; |
| 2504 | tryagain = 1; |
| 2505 | args.type = XFS_ALLOCTYPE_THIS_BNO; |
| 2506 | args.alignment = 1; |
| 2507 | /* |
| 2508 | * Compute the minlen+alignment for the |
| 2509 | * next case. Set slop so that the value |
| 2510 | * of minlen+alignment+slop doesn't go up |
| 2511 | * between the calls. |
| 2512 | */ |
Dave Chinner | 14b064c | 2011-01-27 12:16:28 +1100 | [diff] [blame] | 2513 | if (blen > mp->m_dalign && blen <= args.maxlen) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2514 | nextminlen = blen - mp->m_dalign; |
| 2515 | else |
| 2516 | nextminlen = args.minlen; |
| 2517 | if (nextminlen + mp->m_dalign > args.minlen + 1) |
| 2518 | args.minalignslop = |
| 2519 | nextminlen + mp->m_dalign - |
| 2520 | args.minlen - 1; |
| 2521 | else |
| 2522 | args.minalignslop = 0; |
| 2523 | } |
| 2524 | } else { |
| 2525 | args.alignment = 1; |
| 2526 | args.minalignslop = 0; |
| 2527 | } |
| 2528 | args.minleft = ap->minleft; |
| 2529 | args.wasdel = ap->wasdel; |
| 2530 | args.isfl = 0; |
| 2531 | args.userdata = ap->userdata; |
| 2532 | if ((error = xfs_alloc_vextent(&args))) |
| 2533 | return error; |
| 2534 | if (tryagain && args.fsbno == NULLFSBLOCK) { |
| 2535 | /* |
| 2536 | * Exact allocation failed. Now try with alignment |
| 2537 | * turned on. |
| 2538 | */ |
| 2539 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2540 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2541 | args.alignment = mp->m_dalign; |
| 2542 | args.minlen = nextminlen; |
| 2543 | args.minalignslop = 0; |
| 2544 | isaligned = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | if ((error = xfs_alloc_vextent(&args))) |
| 2546 | return error; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2547 | } |
| 2548 | if (isaligned && args.fsbno == NULLFSBLOCK) { |
| 2549 | /* |
| 2550 | * allocation failed, so turn off alignment and |
| 2551 | * try again. |
| 2552 | */ |
| 2553 | args.type = atype; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2554 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2555 | args.alignment = 0; |
| 2556 | if ((error = xfs_alloc_vextent(&args))) |
| 2557 | return error; |
| 2558 | } |
| 2559 | if (args.fsbno == NULLFSBLOCK && nullfb && |
| 2560 | args.minlen > ap->minlen) { |
| 2561 | args.minlen = ap->minlen; |
| 2562 | args.type = XFS_ALLOCTYPE_START_BNO; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2563 | args.fsbno = ap->blkno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2564 | if ((error = xfs_alloc_vextent(&args))) |
| 2565 | return error; |
| 2566 | } |
| 2567 | if (args.fsbno == NULLFSBLOCK && nullfb) { |
| 2568 | args.fsbno = 0; |
| 2569 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 2570 | args.total = ap->minlen; |
| 2571 | args.minleft = 0; |
| 2572 | if ((error = xfs_alloc_vextent(&args))) |
| 2573 | return error; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2574 | ap->flist->xbf_low = 1; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2575 | } |
| 2576 | if (args.fsbno != NULLFSBLOCK) { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2577 | /* |
| 2578 | * check the allocation happened at the same or higher AG than |
| 2579 | * the first block that was allocated. |
| 2580 | */ |
| 2581 | ASSERT(*ap->firstblock == NULLFSBLOCK || |
| 2582 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) == |
| 2583 | XFS_FSB_TO_AGNO(mp, args.fsbno) || |
| 2584 | (ap->flist->xbf_low && |
| 2585 | XFS_FSB_TO_AGNO(mp, *ap->firstblock) < |
| 2586 | XFS_FSB_TO_AGNO(mp, args.fsbno))); |
| 2587 | |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2588 | ap->blkno = args.fsbno; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2589 | if (*ap->firstblock == NULLFSBLOCK) |
| 2590 | *ap->firstblock = args.fsbno; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2591 | ASSERT(nullfb || fb_agno == args.agno || |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 2592 | (ap->flist->xbf_low && fb_agno < args.agno)); |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2593 | ap->length = args.len; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2594 | ap->ip->i_d.di_nblocks += args.len; |
| 2595 | xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE); |
| 2596 | if (ap->wasdel) |
| 2597 | ap->ip->i_delayed_blks -= args.len; |
| 2598 | /* |
| 2599 | * Adjust the disk quota also. This was reserved |
| 2600 | * earlier. |
| 2601 | */ |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2602 | xfs_trans_mod_dquot_byino(ap->tp, ap->ip, |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2603 | ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : |
| 2604 | XFS_TRANS_DQ_BCOUNT, |
| 2605 | (long) args.len); |
| 2606 | } else { |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 2607 | ap->blkno = NULLFSBLOCK; |
| 2608 | ap->length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | } |
| 2610 | return 0; |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | /* |
| 2614 | * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file. |
| 2615 | * It figures out where to ask the underlying allocator to put the new extent. |
| 2616 | */ |
| 2617 | STATIC int |
| 2618 | xfs_bmap_alloc( |
| 2619 | xfs_bmalloca_t *ap) /* bmap alloc argument struct */ |
| 2620 | { |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2621 | if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) |
Nathan Scott | a365bdd | 2006-03-14 13:34:16 +1100 | [diff] [blame] | 2622 | return xfs_bmap_rtalloc(ap); |
| 2623 | return xfs_bmap_btalloc(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | /* |
| 2627 | * Transform a btree format file with only one leaf node, where the |
| 2628 | * extents list will fit in the inode, into an extents format file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2629 | * 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] | 2630 | * give up the space for the btree root and pitch the leaf block. |
| 2631 | */ |
| 2632 | STATIC int /* error */ |
| 2633 | xfs_bmap_btree_to_extents( |
| 2634 | xfs_trans_t *tp, /* transaction pointer */ |
| 2635 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2636 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 2637 | int *logflagsp, /* inode logging flags */ |
| 2638 | int whichfork) /* data or attr fork */ |
| 2639 | { |
| 2640 | /* REFERENCED */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2641 | struct xfs_btree_block *cblock;/* child btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | xfs_fsblock_t cbno; /* child block number */ |
| 2643 | xfs_buf_t *cbp; /* child block's buffer */ |
| 2644 | int error; /* error return value */ |
| 2645 | xfs_ifork_t *ifp; /* inode fork data */ |
| 2646 | xfs_mount_t *mp; /* mount point structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2647 | __be64 *pp; /* ptr to block address */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2648 | struct xfs_btree_block *rblock;/* root btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2650 | mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 2652 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
| 2653 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE); |
| 2654 | rblock = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 2655 | ASSERT(be16_to_cpu(rblock->bb_level) == 1); |
| 2656 | ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 2657 | ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1); |
| 2658 | 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] | 2659 | cbno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | *logflagsp = 0; |
| 2661 | #ifdef DEBUG |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 2662 | if ((error = xfs_btree_check_lptr(cur, cbno, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | return error; |
| 2664 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, |
| 2666 | XFS_BMAP_BTREE_REF))) |
| 2667 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 2668 | cblock = XFS_BUF_TO_BLOCK(cbp); |
| 2669 | if ((error = xfs_btree_check_block(cur, cblock, 0, cbp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | return error; |
| 2671 | xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp); |
| 2672 | ip->i_d.di_nblocks--; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2673 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | xfs_trans_binval(tp, cbp); |
| 2675 | if (cur->bc_bufs[0] == cbp) |
| 2676 | cur->bc_bufs[0] = NULL; |
| 2677 | xfs_iroot_realloc(ip, -1, whichfork); |
| 2678 | ASSERT(ifp->if_broot == NULL); |
| 2679 | ASSERT((ifp->if_flags & XFS_IFBROOT) == 0); |
| 2680 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2681 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | return 0; |
| 2683 | } |
| 2684 | |
| 2685 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2686 | * Called by xfs_bmapi to update file extent records and the btree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | * after removing space (or undoing a delayed allocation). |
| 2688 | */ |
| 2689 | STATIC int /* error */ |
| 2690 | xfs_bmap_del_extent( |
| 2691 | xfs_inode_t *ip, /* incore inode pointer */ |
| 2692 | xfs_trans_t *tp, /* current transaction pointer */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2693 | xfs_extnum_t *idx, /* extent number to update/delete */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | xfs_bmap_free_t *flist, /* list of extents to be freed */ |
| 2695 | xfs_btree_cur_t *cur, /* if null, not a btree */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2696 | xfs_bmbt_irec_t *del, /* data to remove from extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | int *logflagsp, /* inode logging flags */ |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 2698 | int whichfork) /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | { |
| 2700 | xfs_filblks_t da_new; /* new delay-alloc indirect blocks */ |
| 2701 | xfs_filblks_t da_old; /* old delay-alloc indirect blocks */ |
| 2702 | xfs_fsblock_t del_endblock=0; /* first block past del */ |
| 2703 | xfs_fileoff_t del_endoff; /* first offset past del */ |
| 2704 | int delay; /* current block is delayed allocated */ |
| 2705 | int do_fx; /* free extent at end of routine */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 2706 | xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | int error; /* error return value */ |
| 2708 | int flags; /* inode logging flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | xfs_bmbt_irec_t got; /* current extent entry */ |
| 2710 | xfs_fileoff_t got_endoff; /* first offset past got */ |
| 2711 | int i; /* temp state */ |
| 2712 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 2713 | xfs_mount_t *mp; /* mount structure */ |
| 2714 | xfs_filblks_t nblks; /* quota/sb block count */ |
| 2715 | xfs_bmbt_irec_t new; /* new record to be inserted */ |
| 2716 | /* REFERENCED */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | uint qfield; /* quota field to update */ |
| 2718 | xfs_filblks_t temp; /* for indirect length calculations */ |
| 2719 | xfs_filblks_t temp2; /* for indirect length calculations */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2720 | int state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | |
| 2722 | XFS_STATS_INC(xs_del_exlist); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2723 | |
| 2724 | if (whichfork == XFS_ATTR_FORK) |
| 2725 | state |= BMAP_ATTRFORK; |
| 2726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | mp = ip->i_mount; |
| 2728 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2729 | ASSERT((*idx >= 0) && (*idx < ifp->if_bytes / |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 2730 | (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | ASSERT(del->br_blockcount > 0); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2732 | ep = xfs_iext_get_ext(ifp, *idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | xfs_bmbt_get_all(ep, &got); |
| 2734 | ASSERT(got.br_startoff <= del->br_startoff); |
| 2735 | del_endoff = del->br_startoff + del->br_blockcount; |
| 2736 | got_endoff = got.br_startoff + got.br_blockcount; |
| 2737 | ASSERT(got_endoff >= del_endoff); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2738 | delay = isnullstartblock(got.br_startblock); |
| 2739 | ASSERT(isnullstartblock(del->br_startblock) == delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | flags = 0; |
| 2741 | qfield = 0; |
| 2742 | error = 0; |
| 2743 | /* |
| 2744 | * If deleting a real allocation, must free up the disk space. |
| 2745 | */ |
| 2746 | if (!delay) { |
| 2747 | flags = XFS_ILOG_CORE; |
| 2748 | /* |
| 2749 | * Realtime allocation. Free it and record di_nblocks update. |
| 2750 | */ |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 2751 | if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | xfs_fsblock_t bno; |
| 2753 | xfs_filblks_t len; |
| 2754 | |
| 2755 | ASSERT(do_mod(del->br_blockcount, |
| 2756 | mp->m_sb.sb_rextsize) == 0); |
| 2757 | ASSERT(do_mod(del->br_startblock, |
| 2758 | mp->m_sb.sb_rextsize) == 0); |
| 2759 | bno = del->br_startblock; |
| 2760 | len = del->br_blockcount; |
| 2761 | do_div(bno, mp->m_sb.sb_rextsize); |
| 2762 | do_div(len, mp->m_sb.sb_rextsize); |
Christoph Hellwig | f3ca873 | 2011-07-08 14:34:47 +0200 | [diff] [blame] | 2763 | error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len); |
| 2764 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | goto done; |
| 2766 | do_fx = 0; |
| 2767 | nblks = len * mp->m_sb.sb_rextsize; |
| 2768 | qfield = XFS_TRANS_DQ_RTBCOUNT; |
| 2769 | } |
| 2770 | /* |
| 2771 | * Ordinary allocation. |
| 2772 | */ |
| 2773 | else { |
| 2774 | do_fx = 1; |
| 2775 | nblks = del->br_blockcount; |
| 2776 | qfield = XFS_TRANS_DQ_BCOUNT; |
| 2777 | } |
| 2778 | /* |
| 2779 | * Set up del_endblock and cur for later. |
| 2780 | */ |
| 2781 | del_endblock = del->br_startblock + del->br_blockcount; |
| 2782 | if (cur) { |
| 2783 | if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff, |
| 2784 | got.br_startblock, got.br_blockcount, |
| 2785 | &i))) |
| 2786 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2787 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | } |
| 2789 | da_old = da_new = 0; |
| 2790 | } else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2791 | da_old = startblockval(got.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | da_new = 0; |
| 2793 | nblks = 0; |
| 2794 | do_fx = 0; |
| 2795 | } |
| 2796 | /* |
| 2797 | * Set flag value to use in switch statement. |
| 2798 | * Left-contig is 2, right-contig is 1. |
| 2799 | */ |
| 2800 | switch (((got.br_startoff == del->br_startoff) << 1) | |
| 2801 | (got_endoff == del_endoff)) { |
| 2802 | case 3: |
| 2803 | /* |
| 2804 | * Matches the whole extent. Delete the entry. |
| 2805 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2806 | xfs_iext_remove(ip, *idx, 1, |
Christoph Hellwig | 6ef3554 | 2009-11-25 00:00:21 +0000 | [diff] [blame] | 2807 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0); |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2808 | --*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | if (delay) |
| 2810 | break; |
Christoph Hellwig | 233eebb | 2011-05-11 15:04:05 +0000 | [diff] [blame] | 2811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2813 | XFS_IFORK_NEXTENTS(ip, whichfork) - 1); |
| 2814 | flags |= XFS_ILOG_CORE; |
| 2815 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2816 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | break; |
| 2818 | } |
Christoph Hellwig | 91cca5d | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 2819 | if ((error = xfs_btree_delete(cur, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2821 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | break; |
| 2823 | |
| 2824 | case 2: |
| 2825 | /* |
| 2826 | * Deleting the first part of the extent. |
| 2827 | */ |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2828 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | xfs_bmbt_set_startoff(ep, del_endoff); |
| 2830 | temp = got.br_blockcount - del->br_blockcount; |
| 2831 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | if (delay) { |
| 2833 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2834 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2835 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2836 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | da_new = temp; |
| 2838 | break; |
| 2839 | } |
| 2840 | xfs_bmbt_set_startblock(ep, del_endblock); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2841 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2843 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | break; |
| 2845 | } |
| 2846 | if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock, |
| 2847 | got.br_blockcount - del->br_blockcount, |
| 2848 | got.br_state))) |
| 2849 | goto done; |
| 2850 | break; |
| 2851 | |
| 2852 | case 1: |
| 2853 | /* |
| 2854 | * Deleting the last part of the extent. |
| 2855 | */ |
| 2856 | temp = got.br_blockcount - del->br_blockcount; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2857 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | xfs_bmbt_set_blockcount(ep, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | if (delay) { |
| 2860 | temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp), |
| 2861 | da_old); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2862 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2863 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | da_new = temp; |
| 2865 | break; |
| 2866 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2867 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | if (!cur) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2869 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | break; |
| 2871 | } |
| 2872 | if ((error = xfs_bmbt_update(cur, got.br_startoff, |
| 2873 | got.br_startblock, |
| 2874 | got.br_blockcount - del->br_blockcount, |
| 2875 | got.br_state))) |
| 2876 | goto done; |
| 2877 | break; |
| 2878 | |
| 2879 | case 0: |
| 2880 | /* |
| 2881 | * Deleting the middle of the extent. |
| 2882 | */ |
| 2883 | temp = del->br_startoff - got.br_startoff; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2884 | trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | xfs_bmbt_set_blockcount(ep, temp); |
| 2886 | new.br_startoff = del_endoff; |
| 2887 | temp2 = got_endoff - del_endoff; |
| 2888 | new.br_blockcount = temp2; |
| 2889 | new.br_state = got.br_state; |
| 2890 | if (!delay) { |
| 2891 | new.br_startblock = del_endblock; |
| 2892 | flags |= XFS_ILOG_CORE; |
| 2893 | if (cur) { |
| 2894 | if ((error = xfs_bmbt_update(cur, |
| 2895 | got.br_startoff, |
| 2896 | got.br_startblock, temp, |
| 2897 | got.br_state))) |
| 2898 | goto done; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 2899 | if ((error = xfs_btree_increment(cur, 0, &i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | goto done; |
| 2901 | cur->bc_rec.b = new; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 2902 | error = xfs_btree_insert(cur, &i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | if (error && error != ENOSPC) |
| 2904 | goto done; |
| 2905 | /* |
| 2906 | * If get no-space back from btree insert, |
| 2907 | * it tried a split, and we have a zero |
| 2908 | * block reservation. |
| 2909 | * Fix up our state and return the error. |
| 2910 | */ |
| 2911 | if (error == ENOSPC) { |
| 2912 | /* |
| 2913 | * Reset the cursor, don't trust |
| 2914 | * it after any insert operation. |
| 2915 | */ |
| 2916 | if ((error = xfs_bmbt_lookup_eq(cur, |
| 2917 | got.br_startoff, |
| 2918 | got.br_startblock, |
| 2919 | temp, &i))) |
| 2920 | goto done; |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2921 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | /* |
| 2923 | * Update the btree record back |
| 2924 | * to the original value. |
| 2925 | */ |
| 2926 | if ((error = xfs_bmbt_update(cur, |
| 2927 | got.br_startoff, |
| 2928 | got.br_startblock, |
| 2929 | got.br_blockcount, |
| 2930 | got.br_state))) |
| 2931 | goto done; |
| 2932 | /* |
| 2933 | * Reset the extent record back |
| 2934 | * to the original value. |
| 2935 | */ |
| 2936 | xfs_bmbt_set_blockcount(ep, |
| 2937 | got.br_blockcount); |
| 2938 | flags = 0; |
| 2939 | error = XFS_ERROR(ENOSPC); |
| 2940 | goto done; |
| 2941 | } |
Lachlan McIlroy | 6bd8fc8 | 2008-06-23 13:25:46 +1000 | [diff] [blame] | 2942 | XFS_WANT_CORRUPTED_GOTO(i == 1, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | } else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2944 | flags |= xfs_ilog_fext(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | XFS_IFORK_NEXT_SET(ip, whichfork, |
| 2946 | XFS_IFORK_NEXTENTS(ip, whichfork) + 1); |
| 2947 | } else { |
| 2948 | ASSERT(whichfork == XFS_DATA_FORK); |
| 2949 | temp = xfs_bmap_worst_indlen(ip, temp); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2950 | xfs_bmbt_set_startblock(ep, nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | temp2 = xfs_bmap_worst_indlen(ip, temp2); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2952 | new.br_startblock = nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | da_new = temp + temp2; |
| 2954 | while (da_new > da_old) { |
| 2955 | if (temp) { |
| 2956 | temp--; |
| 2957 | da_new--; |
| 2958 | xfs_bmbt_set_startblock(ep, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2959 | nullstartblock((int)temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | } |
| 2961 | if (da_new == da_old) |
| 2962 | break; |
| 2963 | if (temp2) { |
| 2964 | temp2--; |
| 2965 | da_new--; |
| 2966 | new.br_startblock = |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 2967 | nullstartblock((int)temp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 2971 | trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_); |
| 2972 | xfs_iext_insert(ip, *idx + 1, 1, &new, state); |
| 2973 | ++*idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | break; |
| 2975 | } |
| 2976 | /* |
| 2977 | * If we need to, add to list of extents to delete. |
| 2978 | */ |
| 2979 | if (do_fx) |
| 2980 | xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist, |
| 2981 | mp); |
| 2982 | /* |
| 2983 | * Adjust inode # blocks in the file. |
| 2984 | */ |
| 2985 | if (nblks) |
| 2986 | ip->i_d.di_nblocks -= nblks; |
| 2987 | /* |
| 2988 | * Adjust quota data. |
| 2989 | */ |
| 2990 | if (qfield) |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 2991 | xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | |
| 2993 | /* |
| 2994 | * Account for change in delayed indirect blocks. |
| 2995 | * Nothing to do for disk quota accounting here. |
| 2996 | */ |
| 2997 | ASSERT(da_old >= da_new); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 2998 | if (da_old > da_new) { |
| 2999 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 3000 | (int64_t)(da_old - da_new), 0); |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 3001 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | done: |
| 3003 | *logflagsp = flags; |
| 3004 | return error; |
| 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * Remove the entry "free" from the free item list. Prev points to the |
| 3009 | * previous entry, unless "free" is the head of the list. |
| 3010 | */ |
| 3011 | STATIC void |
| 3012 | xfs_bmap_del_free( |
| 3013 | xfs_bmap_free_t *flist, /* free item list header */ |
| 3014 | xfs_bmap_free_item_t *prev, /* previous item on list, if any */ |
| 3015 | xfs_bmap_free_item_t *free) /* list item to be freed */ |
| 3016 | { |
| 3017 | if (prev) |
| 3018 | prev->xbfi_next = free->xbfi_next; |
| 3019 | else |
| 3020 | flist->xbf_first = free->xbfi_next; |
| 3021 | flist->xbf_count--; |
| 3022 | kmem_zone_free(xfs_bmap_free_item_zone, free); |
| 3023 | } |
| 3024 | |
| 3025 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | * Convert an extents-format file into a btree-format file. |
| 3027 | * The new file will have a root block (in the inode) and a single child block. |
| 3028 | */ |
| 3029 | STATIC int /* error */ |
| 3030 | xfs_bmap_extents_to_btree( |
| 3031 | xfs_trans_t *tp, /* transaction pointer */ |
| 3032 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3033 | xfs_fsblock_t *firstblock, /* first-block-allocated */ |
| 3034 | xfs_bmap_free_t *flist, /* blocks freed in xaction */ |
| 3035 | xfs_btree_cur_t **curp, /* cursor returned to caller */ |
| 3036 | int wasdel, /* converting a delayed alloc */ |
| 3037 | int *logflagsp, /* inode logging flags */ |
| 3038 | int whichfork) /* data or attr fork */ |
| 3039 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3040 | struct xfs_btree_block *ablock; /* allocated (child) bt block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | xfs_buf_t *abp; /* buffer for ablock */ |
| 3042 | xfs_alloc_arg_t args; /* allocation arguments */ |
| 3043 | xfs_bmbt_rec_t *arp; /* child record pointer */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3044 | struct xfs_btree_block *block; /* btree root block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3046 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3048 | xfs_extnum_t i, cnt; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3050 | xfs_bmbt_key_t *kp; /* root block key pointer */ |
| 3051 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3052 | xfs_extnum_t nextents; /* number of file extents */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | xfs_bmbt_ptr_t *pp; /* root block address pointer */ |
| 3054 | |
| 3055 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3056 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | /* |
| 3059 | * Make space in the inode incore. |
| 3060 | */ |
| 3061 | xfs_iroot_realloc(ip, 1, whichfork); |
| 3062 | ifp->if_flags |= XFS_IFBROOT; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | /* |
| 3065 | * Fill in the root. |
| 3066 | */ |
| 3067 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3068 | block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 3069 | block->bb_level = cpu_to_be16(1); |
| 3070 | block->bb_numrecs = cpu_to_be16(1); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3071 | block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3072 | block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
| 3073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | /* |
| 3075 | * Need a cursor. Can't allocate until bb_level is filled in. |
| 3076 | */ |
| 3077 | mp = ip->i_mount; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 3078 | cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | cur->bc_private.b.firstblock = *firstblock; |
| 3080 | cur->bc_private.b.flist = flist; |
| 3081 | cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 3082 | /* |
| 3083 | * Convert to a btree with two levels, one record in root. |
| 3084 | */ |
| 3085 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE); |
| 3086 | args.tp = tp; |
| 3087 | args.mp = mp; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3088 | args.firstblock = *firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | if (*firstblock == NULLFSBLOCK) { |
| 3090 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3091 | args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino); |
| 3092 | } else if (flist->xbf_low) { |
| 3093 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3094 | args.fsbno = *firstblock; |
| 3095 | } else { |
| 3096 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3097 | args.fsbno = *firstblock; |
| 3098 | } |
| 3099 | args.minlen = args.maxlen = args.prod = 1; |
| 3100 | args.total = args.minleft = args.alignment = args.mod = args.isfl = |
| 3101 | args.minalignslop = 0; |
| 3102 | args.wasdel = wasdel; |
| 3103 | *logflagsp = 0; |
| 3104 | if ((error = xfs_alloc_vextent(&args))) { |
| 3105 | xfs_iroot_realloc(ip, -1, whichfork); |
| 3106 | xfs_btree_del_cursor(cur, XFS_BTREE_ERROR); |
| 3107 | return error; |
| 3108 | } |
| 3109 | /* |
| 3110 | * Allocation can't fail, the space was reserved. |
| 3111 | */ |
| 3112 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3113 | ASSERT(*firstblock == NULLFSBLOCK || |
| 3114 | args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) || |
| 3115 | (flist->xbf_low && |
| 3116 | args.agno > XFS_FSB_TO_AGNO(mp, *firstblock))); |
| 3117 | *firstblock = cur->bc_private.b.firstblock = args.fsbno; |
| 3118 | cur->bc_private.b.allocated++; |
| 3119 | ip->i_d.di_nblocks++; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3120 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0); |
| 3122 | /* |
| 3123 | * Fill in the child block. |
| 3124 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3125 | ablock = XFS_BUF_TO_BLOCK(abp); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3126 | ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | ablock->bb_level = 0; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3128 | ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 3129 | ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3130 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3132 | for (cnt = i = 0; i < nextents; i++) { |
| 3133 | ep = xfs_iext_get_ext(ifp, i); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3134 | if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 3135 | arp->l0 = cpu_to_be64(ep->l0); |
| 3136 | arp->l1 = cpu_to_be64(ep->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3137 | arp++; cnt++; |
| 3138 | } |
| 3139 | } |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 3140 | ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3141 | xfs_btree_set_numrecs(ablock, cnt); |
| 3142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | /* |
| 3144 | * Fill in the root key and pointer. |
| 3145 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3146 | kp = XFS_BMBT_KEY_ADDR(mp, block, 1); |
| 3147 | arp = XFS_BMBT_REC_ADDR(mp, ablock, 1); |
Christoph Hellwig | 8801bb9 | 2006-09-28 10:58:17 +1000 | [diff] [blame] | 3148 | kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp)); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3149 | pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur, |
| 3150 | be16_to_cpu(block->bb_level))); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 3151 | *pp = cpu_to_be64(args.fsbno); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 3152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | /* |
| 3154 | * Do all this logging at the end so that |
| 3155 | * the root is at the right level. |
| 3156 | */ |
Christoph Hellwig | fd6bcc5 | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 3157 | xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS); |
| 3158 | 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] | 3159 | ASSERT(*curp == NULL); |
| 3160 | *curp = cur; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3161 | *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | return 0; |
| 3163 | } |
| 3164 | |
| 3165 | /* |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3166 | * Calculate the default attribute fork offset for newly created inodes. |
| 3167 | */ |
| 3168 | uint |
| 3169 | xfs_default_attroffset( |
| 3170 | struct xfs_inode *ip) |
| 3171 | { |
| 3172 | struct xfs_mount *mp = ip->i_mount; |
| 3173 | uint offset; |
| 3174 | |
| 3175 | if (mp->m_sb.sb_inodesize == 256) { |
| 3176 | offset = XFS_LITINO(mp) - |
| 3177 | XFS_BMDR_SPACE_CALC(MINABTPTRS); |
| 3178 | } else { |
| 3179 | offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS); |
| 3180 | } |
| 3181 | |
| 3182 | ASSERT(offset < XFS_LITINO(mp)); |
| 3183 | return offset; |
| 3184 | } |
| 3185 | |
| 3186 | /* |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3187 | * Helper routine to reset inode di_forkoff field when switching |
| 3188 | * attribute fork from local to extent format - we reset it where |
| 3189 | * possible to make space available for inline data fork extents. |
| 3190 | */ |
| 3191 | STATIC void |
| 3192 | xfs_bmap_forkoff_reset( |
| 3193 | xfs_mount_t *mp, |
| 3194 | xfs_inode_t *ip, |
| 3195 | int whichfork) |
| 3196 | { |
| 3197 | if (whichfork == XFS_ATTR_FORK && |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3198 | ip->i_d.di_format != XFS_DINODE_FMT_DEV && |
| 3199 | ip->i_d.di_format != XFS_DINODE_FMT_UUID && |
| 3200 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE) { |
| 3201 | uint dfl_forkoff = xfs_default_attroffset(ip) >> 3; |
| 3202 | |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3203 | if (dfl_forkoff > ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3204 | ip->i_d.di_forkoff = dfl_forkoff; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | * Convert a local file to an extents file. |
| 3210 | * This code is out of bounds for data forks of regular files, |
| 3211 | * since the file data needs to get logged so things will stay consistent. |
| 3212 | * (The bmap-level manipulations are ok, though). |
| 3213 | */ |
| 3214 | STATIC int /* error */ |
| 3215 | xfs_bmap_local_to_extents( |
| 3216 | xfs_trans_t *tp, /* transaction pointer */ |
| 3217 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3218 | xfs_fsblock_t *firstblock, /* first block allocated in xaction */ |
| 3219 | xfs_extlen_t total, /* total blocks needed by transaction */ |
| 3220 | int *logflagsp, /* inode logging flags */ |
| 3221 | int whichfork) /* data or attr fork */ |
| 3222 | { |
| 3223 | int error; /* error return value */ |
| 3224 | int flags; /* logging flags returned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3226 | |
| 3227 | /* |
| 3228 | * We don't want to deal with the case of keeping inode data inline yet. |
| 3229 | * So sending the data fork of a regular inode is invalid. |
| 3230 | */ |
Al Viro | abbede1 | 2011-07-26 02:31:30 -0400 | [diff] [blame] | 3231 | ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3233 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3234 | flags = 0; |
| 3235 | error = 0; |
| 3236 | if (ifp->if_bytes) { |
| 3237 | xfs_alloc_arg_t args; /* allocation arguments */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3238 | xfs_buf_t *bp; /* buffer for extent block */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3239 | xfs_bmbt_rec_host_t *ep;/* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3240 | |
| 3241 | args.tp = tp; |
| 3242 | args.mp = ip->i_mount; |
Yingping Lu | d210a28 | 2006-06-09 14:55:18 +1000 | [diff] [blame] | 3243 | args.firstblock = *firstblock; |
Mandy Kirkconnell | f020b67 | 2006-03-14 14:07:24 +1100 | [diff] [blame] | 3244 | ASSERT((ifp->if_flags & |
| 3245 | (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3246 | /* |
| 3247 | * Allocate a block. We know we need only one, since the |
| 3248 | * file currently fits in an inode. |
| 3249 | */ |
| 3250 | if (*firstblock == NULLFSBLOCK) { |
| 3251 | args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino); |
| 3252 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 3253 | } else { |
| 3254 | args.fsbno = *firstblock; |
| 3255 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 3256 | } |
| 3257 | args.total = total; |
| 3258 | args.mod = args.minleft = args.alignment = args.wasdel = |
| 3259 | args.isfl = args.minalignslop = 0; |
| 3260 | args.minlen = args.maxlen = args.prod = 1; |
| 3261 | if ((error = xfs_alloc_vextent(&args))) |
| 3262 | goto done; |
| 3263 | /* |
| 3264 | * Can't fail, the space was reserved. |
| 3265 | */ |
| 3266 | ASSERT(args.fsbno != NULLFSBLOCK); |
| 3267 | ASSERT(args.len == 1); |
| 3268 | *firstblock = args.fsbno; |
| 3269 | bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0); |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 3270 | memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3272 | xfs_bmap_forkoff_reset(args.mp, ip, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | xfs_idata_realloc(ip, -ifp->if_bytes, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3274 | xfs_iext_add(ifp, 0, 1); |
| 3275 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 3277 | trace_xfs_bmap_post_update(ip, 0, |
| 3278 | whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0, |
| 3279 | _THIS_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | XFS_IFORK_NEXT_SET(ip, whichfork, 1); |
| 3281 | ip->i_d.di_nblocks = 1; |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3282 | xfs_trans_mod_dquot_byino(tp, ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | XFS_TRANS_DQ_BCOUNT, 1L); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3284 | flags |= xfs_ilog_fext(whichfork); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3285 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3287 | xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork); |
| 3288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3289 | ifp->if_flags &= ~XFS_IFINLINE; |
| 3290 | ifp->if_flags |= XFS_IFEXTENTS; |
| 3291 | XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS); |
| 3292 | flags |= XFS_ILOG_CORE; |
| 3293 | done: |
| 3294 | *logflagsp = flags; |
| 3295 | return error; |
| 3296 | } |
| 3297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3299 | * Search the extent records for the entry containing block bno. |
| 3300 | * If bno lies in a hole, point to the next entry. If bno lies |
| 3301 | * past eof, *eofp will be set, and *prevp will contain the last |
| 3302 | * entry (null if none). Else, *lastxp will be set to the index |
| 3303 | * of the found entry; *gotp will contain the entry. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3304 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3305 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3306 | xfs_bmap_search_multi_extents( |
| 3307 | xfs_ifork_t *ifp, /* inode fork pointer */ |
| 3308 | xfs_fileoff_t bno, /* block number searched for */ |
| 3309 | int *eofp, /* out: end of file found */ |
| 3310 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3311 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3312 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3313 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3314 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3315 | xfs_extnum_t lastx; /* last extent index */ |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3316 | |
| 3317 | /* |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3318 | * Initialize the extent entry structure to catch access to |
| 3319 | * uninitialized br_startblock field. |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3320 | */ |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3321 | gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL; |
| 3322 | gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL; |
| 3323 | gotp->br_state = XFS_EXT_INVALID; |
| 3324 | #if XFS_BIG_BLKNOS |
| 3325 | gotp->br_startblock = 0xffffa5a5a5a5a5a5LL; |
| 3326 | #else |
| 3327 | gotp->br_startblock = 0xffffa5a5; |
| 3328 | #endif |
| 3329 | prevp->br_startoff = NULLFILEOFF; |
| 3330 | |
| 3331 | ep = xfs_iext_bno_to_ext(ifp, bno, &lastx); |
| 3332 | if (lastx > 0) { |
| 3333 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp); |
| 3334 | } |
| 3335 | if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) { |
| 3336 | xfs_bmbt_get_all(ep, gotp); |
| 3337 | *eofp = 0; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3338 | } else { |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3339 | if (lastx > 0) { |
| 3340 | *gotp = *prevp; |
| 3341 | } |
| 3342 | *eofp = 1; |
| 3343 | ep = NULL; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3344 | } |
Mandy Kirkconnell | 8867bc9 | 2006-03-17 17:25:04 +1100 | [diff] [blame] | 3345 | *lastxp = lastx; |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3346 | return ep; |
| 3347 | } |
| 3348 | |
| 3349 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | * Search the extents list for the inode, for the extent containing bno. |
| 3351 | * If bno lies in a hole, point to the next entry. If bno lies past eof, |
| 3352 | * *eofp will be set, and *prevp will contain the last entry (null if none). |
| 3353 | * Else, *lastxp will be set to the index of the found |
| 3354 | * entry; *gotp will contain the entry. |
| 3355 | */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3356 | STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | xfs_bmap_search_extents( |
| 3358 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3359 | xfs_fileoff_t bno, /* block number searched for */ |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3360 | int fork, /* data or attr fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | int *eofp, /* out: end of file found */ |
| 3362 | xfs_extnum_t *lastxp, /* out: last extent index */ |
| 3363 | xfs_bmbt_irec_t *gotp, /* out: extent entry found */ |
| 3364 | xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */ |
| 3365 | { |
| 3366 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3367 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | |
| 3369 | XFS_STATS_INC(xs_look_exlist); |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3370 | ifp = XFS_IFORK_PTR(ip, fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | |
Mandy Kirkconnell | 0293ce3 | 2006-03-14 13:30:23 +1100 | [diff] [blame] | 3372 | ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp); |
| 3373 | |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3374 | if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) && |
| 3375 | !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) { |
Dave Chinner | 6a19d93 | 2011-03-07 10:02:35 +1100 | [diff] [blame] | 3376 | xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3377 | "Access to block zero in inode %llu " |
| 3378 | "start_block: %llx start_off: %llx " |
| 3379 | "blkcnt: %llx extent-state: %x lastx: %x\n", |
| 3380 | (unsigned long long)ip->i_ino, |
Nathan Scott | 3ddb8fa | 2006-01-11 15:33:02 +1100 | [diff] [blame] | 3381 | (unsigned long long)gotp->br_startblock, |
| 3382 | (unsigned long long)gotp->br_startoff, |
| 3383 | (unsigned long long)gotp->br_blockcount, |
Nathan Scott | 572d95f | 2006-09-28 11:03:20 +1000 | [diff] [blame] | 3384 | gotp->br_state, *lastxp); |
| 3385 | *lastxp = NULLEXTNUM; |
| 3386 | *eofp = 1; |
| 3387 | return NULL; |
| 3388 | } |
| 3389 | return ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | /* |
| 3393 | * Compute the worst-case number of indirect blocks that will be used |
| 3394 | * for ip's delayed extent of length "len". |
| 3395 | */ |
| 3396 | STATIC xfs_filblks_t |
| 3397 | xfs_bmap_worst_indlen( |
| 3398 | xfs_inode_t *ip, /* incore inode pointer */ |
| 3399 | xfs_filblks_t len) /* delayed extent length */ |
| 3400 | { |
| 3401 | int level; /* btree level number */ |
| 3402 | int maxrecs; /* maximum record count at this level */ |
| 3403 | xfs_mount_t *mp; /* mount structure */ |
| 3404 | xfs_filblks_t rval; /* return value */ |
| 3405 | |
| 3406 | mp = ip->i_mount; |
| 3407 | maxrecs = mp->m_bmap_dmxr[0]; |
| 3408 | for (level = 0, rval = 0; |
| 3409 | level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK); |
| 3410 | level++) { |
| 3411 | len += maxrecs - 1; |
| 3412 | do_div(len, maxrecs); |
| 3413 | rval += len; |
| 3414 | if (len == 1) |
| 3415 | return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) - |
| 3416 | level - 1; |
| 3417 | if (level == 0) |
| 3418 | maxrecs = mp->m_bmap_dmxr[1]; |
| 3419 | } |
| 3420 | return rval; |
| 3421 | } |
| 3422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | /* |
| 3424 | * Convert inode from non-attributed to attributed. |
| 3425 | * Must not be in a transaction, ip must not be locked. |
| 3426 | */ |
| 3427 | int /* error code */ |
| 3428 | xfs_bmap_add_attrfork( |
| 3429 | xfs_inode_t *ip, /* incore inode pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3430 | int size, /* space new attribute needs */ |
| 3431 | int rsvd) /* xact may use reserved blks */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | xfs_fsblock_t firstblock; /* 1st block/ag allocated */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3434 | xfs_bmap_free_t flist; /* freed extent records */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | xfs_mount_t *mp; /* mount structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | xfs_trans_t *tp; /* transaction pointer */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3437 | int blks; /* space reservation */ |
| 3438 | int version = 1; /* superblock attr version */ |
| 3439 | int committed; /* xaction was committed */ |
| 3440 | int logflags; /* logging flags */ |
| 3441 | int error; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3443 | ASSERT(XFS_IFORK_Q(ip) == 0); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | mp = ip->i_mount; |
| 3446 | ASSERT(!XFS_NOT_DQATTACHED(mp, ip)); |
| 3447 | tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK); |
| 3448 | blks = XFS_ADDAFORK_SPACE_RES(mp); |
| 3449 | if (rsvd) |
| 3450 | tp->t_flags |= XFS_TRANS_RESERVE; |
| 3451 | if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0, |
| 3452 | XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT))) |
| 3453 | goto error0; |
| 3454 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 3455 | error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES : |
| 3457 | XFS_QMOPT_RES_REGBLKS); |
| 3458 | if (error) { |
| 3459 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3460 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES); |
| 3461 | return error; |
| 3462 | } |
| 3463 | if (XFS_IFORK_Q(ip)) |
| 3464 | goto error1; |
| 3465 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) { |
| 3466 | /* |
| 3467 | * For inodes coming from pre-6.2 filesystems. |
| 3468 | */ |
| 3469 | ASSERT(ip->i_d.di_aformat == 0); |
| 3470 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; |
| 3471 | } |
| 3472 | ASSERT(ip->i_d.di_anextents == 0); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3473 | |
Christoph Hellwig | ddc3415 | 2011-09-19 15:00:54 +0000 | [diff] [blame] | 3474 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 898621d | 2010-06-24 11:36:58 +1000 | [diff] [blame] | 3476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | switch (ip->i_d.di_format) { |
| 3478 | case XFS_DINODE_FMT_DEV: |
| 3479 | ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
| 3480 | break; |
| 3481 | case XFS_DINODE_FMT_UUID: |
| 3482 | ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3; |
| 3483 | break; |
| 3484 | case XFS_DINODE_FMT_LOCAL: |
| 3485 | case XFS_DINODE_FMT_EXTENTS: |
| 3486 | case XFS_DINODE_FMT_BTREE: |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3487 | ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size); |
| 3488 | if (!ip->i_d.di_forkoff) |
Christoph Hellwig | 1a5902c | 2009-03-29 19:26:46 +0200 | [diff] [blame] | 3489 | ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3; |
Nathan Scott | 13059ff | 2006-01-11 15:32:01 +1100 | [diff] [blame] | 3490 | else if (mp->m_flags & XFS_MOUNT_ATTR2) |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3491 | version = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | break; |
| 3493 | default: |
| 3494 | ASSERT(0); |
| 3495 | error = XFS_ERROR(EINVAL); |
| 3496 | goto error1; |
| 3497 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | ASSERT(ip->i_afp == NULL); |
| 3500 | ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | ip->i_afp->if_flags = XFS_IFEXTENTS; |
| 3502 | logflags = 0; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3503 | xfs_bmap_init(&flist, &firstblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | switch (ip->i_d.di_format) { |
| 3505 | case XFS_DINODE_FMT_LOCAL: |
| 3506 | error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist, |
| 3507 | &logflags); |
| 3508 | break; |
| 3509 | case XFS_DINODE_FMT_EXTENTS: |
| 3510 | error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock, |
| 3511 | &flist, &logflags); |
| 3512 | break; |
| 3513 | case XFS_DINODE_FMT_BTREE: |
| 3514 | error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist, |
| 3515 | &logflags); |
| 3516 | break; |
| 3517 | default: |
| 3518 | error = 0; |
| 3519 | break; |
| 3520 | } |
| 3521 | if (logflags) |
| 3522 | xfs_trans_log_inode(tp, ip, logflags); |
| 3523 | if (error) |
| 3524 | goto error2; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3525 | if (!xfs_sb_version_hasattr(&mp->m_sb) || |
| 3526 | (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) { |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3527 | __int64_t sbfields = 0; |
| 3528 | |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3529 | spin_lock(&mp->m_sb_lock); |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3530 | if (!xfs_sb_version_hasattr(&mp->m_sb)) { |
| 3531 | xfs_sb_version_addattr(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3532 | sbfields |= XFS_SB_VERSIONNUM; |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3533 | } |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3534 | if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) { |
| 3535 | xfs_sb_version_addattr2(&mp->m_sb); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3536 | sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3537 | } |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3538 | if (sbfields) { |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3539 | spin_unlock(&mp->m_sb_lock); |
Nathan Scott | da087ba | 2005-11-02 15:00:20 +1100 | [diff] [blame] | 3540 | xfs_mod_sb(tp, sbfields); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | } else |
Eric Sandeen | 3685c2a | 2007-10-11 17:42:32 +1000 | [diff] [blame] | 3542 | spin_unlock(&mp->m_sb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3544 | |
| 3545 | error = xfs_bmap_finish(&tp, &flist, &committed); |
| 3546 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | goto error2; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 3548 | return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | error2: |
| 3550 | xfs_bmap_cancel(&flist); |
| 3551 | error1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 3553 | error0: |
| 3554 | xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | return error; |
| 3556 | } |
| 3557 | |
| 3558 | /* |
| 3559 | * Add the extent to the list of extents to be free at transaction end. |
| 3560 | * The list is maintained sorted (by block number). |
| 3561 | */ |
| 3562 | /* ARGSUSED */ |
| 3563 | void |
| 3564 | xfs_bmap_add_free( |
| 3565 | xfs_fsblock_t bno, /* fs block number of extent */ |
| 3566 | xfs_filblks_t len, /* length of extent */ |
| 3567 | xfs_bmap_free_t *flist, /* list of extents */ |
| 3568 | xfs_mount_t *mp) /* mount point structure */ |
| 3569 | { |
| 3570 | xfs_bmap_free_item_t *cur; /* current (next) element */ |
| 3571 | xfs_bmap_free_item_t *new; /* new element */ |
| 3572 | xfs_bmap_free_item_t *prev; /* previous element */ |
| 3573 | #ifdef DEBUG |
| 3574 | xfs_agnumber_t agno; |
| 3575 | xfs_agblock_t agbno; |
| 3576 | |
| 3577 | ASSERT(bno != NULLFSBLOCK); |
| 3578 | ASSERT(len > 0); |
| 3579 | ASSERT(len <= MAXEXTLEN); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3580 | ASSERT(!isnullstartblock(bno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | agno = XFS_FSB_TO_AGNO(mp, bno); |
| 3582 | agbno = XFS_FSB_TO_AGBNO(mp, bno); |
| 3583 | ASSERT(agno < mp->m_sb.sb_agcount); |
| 3584 | ASSERT(agbno < mp->m_sb.sb_agblocks); |
| 3585 | ASSERT(len < mp->m_sb.sb_agblocks); |
| 3586 | ASSERT(agbno + len <= mp->m_sb.sb_agblocks); |
| 3587 | #endif |
| 3588 | ASSERT(xfs_bmap_free_item_zone != NULL); |
| 3589 | new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP); |
| 3590 | new->xbfi_startblock = bno; |
| 3591 | new->xbfi_blockcount = (xfs_extlen_t)len; |
| 3592 | for (prev = NULL, cur = flist->xbf_first; |
| 3593 | cur != NULL; |
| 3594 | prev = cur, cur = cur->xbfi_next) { |
| 3595 | if (cur->xbfi_startblock >= bno) |
| 3596 | break; |
| 3597 | } |
| 3598 | if (prev) |
| 3599 | prev->xbfi_next = new; |
| 3600 | else |
| 3601 | flist->xbf_first = new; |
| 3602 | new->xbfi_next = cur; |
| 3603 | flist->xbf_count++; |
| 3604 | } |
| 3605 | |
| 3606 | /* |
| 3607 | * Compute and fill in the value of the maximum depth of a bmap btree |
| 3608 | * in this filesystem. Done once, during mount. |
| 3609 | */ |
| 3610 | void |
| 3611 | xfs_bmap_compute_maxlevels( |
| 3612 | xfs_mount_t *mp, /* file system mount structure */ |
| 3613 | int whichfork) /* data or attr fork */ |
| 3614 | { |
| 3615 | int level; /* btree level */ |
| 3616 | uint maxblocks; /* max blocks at this level */ |
| 3617 | uint maxleafents; /* max leaf entries possible */ |
| 3618 | int maxrootrecs; /* max records in root block */ |
| 3619 | int minleafrecs; /* min records in leaf block */ |
| 3620 | int minnoderecs; /* min records in node block */ |
| 3621 | int sz; /* root block size */ |
| 3622 | |
| 3623 | /* |
| 3624 | * The maximum number of extents in a file, hence the maximum |
| 3625 | * number of leaf entries, is controlled by the type of di_nextents |
| 3626 | * (a signed 32-bit number, xfs_extnum_t), or by di_anextents |
| 3627 | * (a signed 16-bit number, xfs_aextnum_t). |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3628 | * |
| 3629 | * 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] | 3630 | * the fork offset of all the inodes will be |
| 3631 | * (xfs_default_attroffset(ip) >> 3) because we could have mounted |
| 3632 | * with ATTR2 and then mounted back with ATTR1, keeping the |
| 3633 | * di_forkoff's fixed but probably at various positions. Therefore, |
| 3634 | * for both ATTR1 and ATTR2 we have to assume the worst case scenario |
| 3635 | * of a minimum size available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | */ |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3637 | if (whichfork == XFS_DATA_FORK) { |
| 3638 | maxleafents = MAXEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3639 | sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3640 | } else { |
| 3641 | maxleafents = MAXAEXTNUM; |
Tim Shimmin | 6d1337b | 2008-04-17 16:50:16 +1000 | [diff] [blame] | 3642 | sz = XFS_BMDR_SPACE_CALC(MINABTPTRS); |
Nathan Scott | d8cc890 | 2005-11-02 10:34:53 +1100 | [diff] [blame] | 3643 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 3644 | maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | minleafrecs = mp->m_bmap_dmnr[0]; |
| 3646 | minnoderecs = mp->m_bmap_dmnr[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs; |
| 3648 | for (level = 1; maxblocks > 1; level++) { |
| 3649 | if (maxblocks <= maxrootrecs) |
| 3650 | maxblocks = 1; |
| 3651 | else |
| 3652 | maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs; |
| 3653 | } |
| 3654 | mp->m_bm_maxlevels[whichfork] = level; |
| 3655 | } |
| 3656 | |
| 3657 | /* |
| 3658 | * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi |
| 3659 | * caller. Frees all the extents that need freeing, which must be done |
| 3660 | * last due to locking considerations. We never free any extents in |
Christoph Hellwig | 859f57c | 2011-08-27 14:45:11 +0000 | [diff] [blame] | 3661 | * the first transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | * |
| 3663 | * Return 1 if the given transaction was committed and a new one |
| 3664 | * started, and 0 otherwise in the committed parameter. |
| 3665 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | int /* error */ |
| 3667 | xfs_bmap_finish( |
| 3668 | xfs_trans_t **tp, /* transaction pointer addr */ |
| 3669 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | int *committed) /* xact committed or not */ |
| 3671 | { |
| 3672 | xfs_efd_log_item_t *efd; /* extent free data */ |
| 3673 | xfs_efi_log_item_t *efi; /* extent free intention */ |
| 3674 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3675 | xfs_bmap_free_item_t *free; /* free extent item */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3676 | unsigned int logres; /* new log reservation */ |
| 3677 | unsigned int logcount; /* new log count */ |
| 3678 | xfs_mount_t *mp; /* filesystem mount structure */ |
| 3679 | xfs_bmap_free_item_t *next; /* next item on free list */ |
| 3680 | xfs_trans_t *ntp; /* new transaction pointer */ |
| 3681 | |
| 3682 | ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 3683 | if (flist->xbf_count == 0) { |
| 3684 | *committed = 0; |
| 3685 | return 0; |
| 3686 | } |
| 3687 | ntp = *tp; |
| 3688 | efi = xfs_trans_get_efi(ntp, flist->xbf_count); |
| 3689 | for (free = flist->xbf_first; free; free = free->xbfi_next) |
| 3690 | xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock, |
| 3691 | free->xbfi_blockcount); |
| 3692 | logres = ntp->t_log_res; |
| 3693 | logcount = ntp->t_log_count; |
| 3694 | ntp = xfs_trans_dup(*tp); |
Eric Sandeen | 1c72bf9 | 2007-05-08 13:48:42 +1000 | [diff] [blame] | 3695 | error = xfs_trans_commit(*tp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | *tp = ntp; |
| 3697 | *committed = 1; |
| 3698 | /* |
| 3699 | * We have a new transaction, so we should return committed=1, |
| 3700 | * even though we're returning an error. |
| 3701 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3702 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | return error; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3704 | |
| 3705 | /* |
| 3706 | * transaction commit worked ok so we can drop the extra ticket |
| 3707 | * reference that we gained in xfs_trans_dup() |
| 3708 | */ |
| 3709 | xfs_log_ticket_put(ntp->t_ticket); |
| 3710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES, |
| 3712 | logcount))) |
| 3713 | return error; |
| 3714 | efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count); |
| 3715 | for (free = flist->xbf_first; free != NULL; free = next) { |
| 3716 | next = free->xbfi_next; |
| 3717 | if ((error = xfs_free_extent(ntp, free->xbfi_startblock, |
| 3718 | free->xbfi_blockcount))) { |
| 3719 | /* |
| 3720 | * The bmap free list will be cleaned up at a |
| 3721 | * higher level. The EFI will be canceled when |
| 3722 | * this transaction is aborted. |
| 3723 | * Need to force shutdown here to make sure it |
| 3724 | * happens, since this transaction may not be |
| 3725 | * dirty yet. |
| 3726 | */ |
| 3727 | mp = ntp->t_mountp; |
| 3728 | if (!XFS_FORCED_SHUTDOWN(mp)) |
| 3729 | xfs_force_shutdown(mp, |
| 3730 | (error == EFSCORRUPTED) ? |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 3731 | SHUTDOWN_CORRUPT_INCORE : |
| 3732 | SHUTDOWN_META_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | return error; |
| 3734 | } |
| 3735 | xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock, |
| 3736 | free->xbfi_blockcount); |
| 3737 | xfs_bmap_del_free(flist, NULL, free); |
| 3738 | } |
| 3739 | return 0; |
| 3740 | } |
| 3741 | |
| 3742 | /* |
| 3743 | * Free up any items left in the list. |
| 3744 | */ |
| 3745 | void |
| 3746 | xfs_bmap_cancel( |
| 3747 | xfs_bmap_free_t *flist) /* list of bmap_free_items */ |
| 3748 | { |
| 3749 | xfs_bmap_free_item_t *free; /* free list item */ |
| 3750 | xfs_bmap_free_item_t *next; |
| 3751 | |
| 3752 | if (flist->xbf_count == 0) |
| 3753 | return; |
| 3754 | ASSERT(flist->xbf_first != NULL); |
| 3755 | for (free = flist->xbf_first; free; free = next) { |
| 3756 | next = free->xbfi_next; |
| 3757 | xfs_bmap_del_free(flist, NULL, free); |
| 3758 | } |
| 3759 | ASSERT(flist->xbf_count == 0); |
| 3760 | } |
| 3761 | |
| 3762 | /* |
| 3763 | * Returns the file-relative block number of the first unused block(s) |
| 3764 | * in the file with at least "len" logically contiguous blocks free. |
| 3765 | * This is the lowest-address hole if the file has holes, else the first block |
| 3766 | * past the end of file. |
| 3767 | * Return 0 if the file is currently local (in-inode). |
| 3768 | */ |
| 3769 | int /* error */ |
| 3770 | xfs_bmap_first_unused( |
| 3771 | xfs_trans_t *tp, /* transaction pointer */ |
| 3772 | xfs_inode_t *ip, /* incore inode */ |
| 3773 | xfs_extlen_t len, /* size of hole to find */ |
| 3774 | xfs_fileoff_t *first_unused, /* unused block */ |
| 3775 | int whichfork) /* data or attr fork */ |
| 3776 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3777 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3778 | int idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3780 | xfs_fileoff_t lastaddr; /* last block number seen */ |
| 3781 | xfs_fileoff_t lowest; /* lowest useful block */ |
| 3782 | xfs_fileoff_t max; /* starting useful block */ |
| 3783 | xfs_fileoff_t off; /* offset for this block */ |
| 3784 | xfs_extnum_t nextents; /* number of extent entries */ |
| 3785 | |
| 3786 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE || |
| 3787 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS || |
| 3788 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL); |
| 3789 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3790 | *first_unused = 0; |
| 3791 | return 0; |
| 3792 | } |
| 3793 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3794 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3795 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3796 | return error; |
| 3797 | lowest = *first_unused; |
| 3798 | nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3799 | for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3800 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | off = xfs_bmbt_get_startoff(ep); |
| 3802 | /* |
| 3803 | * See if the hole before this extent will work. |
| 3804 | */ |
| 3805 | if (off >= lowest + len && off - max >= len) { |
| 3806 | *first_unused = max; |
| 3807 | return 0; |
| 3808 | } |
| 3809 | lastaddr = off + xfs_bmbt_get_blockcount(ep); |
| 3810 | max = XFS_FILEOFF_MAX(lastaddr, lowest); |
| 3811 | } |
| 3812 | *first_unused = max; |
| 3813 | return 0; |
| 3814 | } |
| 3815 | |
| 3816 | /* |
| 3817 | * Returns the file-relative block number of the last block + 1 before |
| 3818 | * last_block (input value) in the file. |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 3819 | * This is not based on i_size, it is based on the extent records. |
| 3820 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | */ |
| 3822 | int /* error */ |
| 3823 | xfs_bmap_last_before( |
| 3824 | xfs_trans_t *tp, /* transaction pointer */ |
| 3825 | xfs_inode_t *ip, /* incore inode */ |
| 3826 | xfs_fileoff_t *last_block, /* last block */ |
| 3827 | int whichfork) /* data or attr fork */ |
| 3828 | { |
| 3829 | xfs_fileoff_t bno; /* input file offset */ |
| 3830 | int eof; /* hit end of file */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3831 | xfs_bmbt_rec_host_t *ep; /* pointer to last extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | int error; /* error return value */ |
| 3833 | xfs_bmbt_irec_t got; /* current extent value */ |
| 3834 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3835 | xfs_extnum_t lastx; /* last extent used */ |
| 3836 | xfs_bmbt_irec_t prev; /* previous extent value */ |
| 3837 | |
| 3838 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 3839 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 3840 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL) |
| 3841 | return XFS_ERROR(EIO); |
| 3842 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
| 3843 | *last_block = 0; |
| 3844 | return 0; |
| 3845 | } |
| 3846 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3847 | if (!(ifp->if_flags & XFS_IFEXTENTS) && |
| 3848 | (error = xfs_iread_extents(tp, ip, whichfork))) |
| 3849 | return error; |
| 3850 | bno = *last_block - 1; |
| 3851 | ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, |
| 3852 | &prev); |
| 3853 | if (eof || xfs_bmbt_get_startoff(ep) > bno) { |
| 3854 | if (prev.br_startoff == NULLFILEOFF) |
| 3855 | *last_block = 0; |
| 3856 | else |
| 3857 | *last_block = prev.br_startoff + prev.br_blockcount; |
| 3858 | } |
| 3859 | /* |
| 3860 | * Otherwise *last_block is already the right answer. |
| 3861 | */ |
| 3862 | return 0; |
| 3863 | } |
| 3864 | |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3865 | STATIC int |
| 3866 | xfs_bmap_last_extent( |
| 3867 | struct xfs_trans *tp, |
| 3868 | struct xfs_inode *ip, |
| 3869 | int whichfork, |
| 3870 | struct xfs_bmbt_irec *rec, |
| 3871 | int *is_empty) |
| 3872 | { |
| 3873 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3874 | int error; |
| 3875 | int nextents; |
| 3876 | |
| 3877 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 3878 | error = xfs_iread_extents(tp, ip, whichfork); |
| 3879 | if (error) |
| 3880 | return error; |
| 3881 | } |
| 3882 | |
| 3883 | nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t); |
| 3884 | if (nextents == 0) { |
| 3885 | *is_empty = 1; |
| 3886 | return 0; |
| 3887 | } |
| 3888 | |
| 3889 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec); |
| 3890 | *is_empty = 0; |
| 3891 | return 0; |
| 3892 | } |
| 3893 | |
| 3894 | /* |
| 3895 | * Check the last inode extent to determine whether this allocation will result |
| 3896 | * in blocks being allocated at the end of the file. When we allocate new data |
| 3897 | * blocks at the end of the file which do not start at the previous data block, |
| 3898 | * we will try to align the new blocks at stripe unit boundaries. |
| 3899 | * |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3900 | * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be |
| 3901 | * at, or past the EOF. |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3902 | */ |
| 3903 | STATIC int |
| 3904 | xfs_bmap_isaeof( |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3905 | struct xfs_bmalloca *bma, |
| 3906 | int whichfork) |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3907 | { |
| 3908 | struct xfs_bmbt_irec rec; |
| 3909 | int is_empty; |
| 3910 | int error; |
| 3911 | |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3912 | bma->aeof = 0; |
| 3913 | error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec, |
| 3914 | &is_empty); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3915 | if (error || is_empty) |
| 3916 | return error; |
| 3917 | |
| 3918 | /* |
| 3919 | * Check if we are allocation or past the last extent, or at least into |
| 3920 | * the last delayed allocated extent. |
| 3921 | */ |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 3922 | bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount || |
| 3923 | (bma->offset >= rec.br_startoff && |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 3924 | isnullstartblock(rec.br_startblock)); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3925 | return 0; |
| 3926 | } |
| 3927 | |
| 3928 | /* |
| 3929 | * Check if the endoff is outside the last extent. If so the caller will grow |
| 3930 | * the allocation to a stripe unit boundary. All offsets are considered outside |
| 3931 | * the end of file for an empty fork, so 1 is returned in *eof in that case. |
| 3932 | */ |
| 3933 | int |
| 3934 | xfs_bmap_eof( |
| 3935 | struct xfs_inode *ip, |
| 3936 | xfs_fileoff_t endoff, |
| 3937 | int whichfork, |
| 3938 | int *eof) |
| 3939 | { |
| 3940 | struct xfs_bmbt_irec rec; |
| 3941 | int error; |
| 3942 | |
| 3943 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof); |
| 3944 | if (error || *eof) |
| 3945 | return error; |
| 3946 | |
| 3947 | *eof = endoff >= rec.br_startoff + rec.br_blockcount; |
| 3948 | return 0; |
| 3949 | } |
| 3950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | /* |
| 3952 | * 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] | 3953 | * the file. This is not based on i_size, it is based on the extent records. |
| 3954 | * Returns 0 for local files, as they do not have extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3955 | */ |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3956 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | xfs_bmap_last_offset( |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3958 | struct xfs_trans *tp, |
| 3959 | struct xfs_inode *ip, |
| 3960 | xfs_fileoff_t *last_block, |
| 3961 | int whichfork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | { |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3963 | struct xfs_bmbt_irec rec; |
| 3964 | int is_empty; |
| 3965 | int error; |
| 3966 | |
| 3967 | *last_block = 0; |
| 3968 | |
| 3969 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) |
| 3970 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | |
| 3972 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3973 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | return XFS_ERROR(EIO); |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3975 | |
| 3976 | error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty); |
| 3977 | if (error || is_empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | return error; |
Christoph Hellwig | 27a3f8f | 2011-09-18 20:40:53 +0000 | [diff] [blame] | 3979 | |
| 3980 | *last_block = rec.br_startoff + rec.br_blockcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | return 0; |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * Returns whether the selected fork of the inode has exactly one |
| 3986 | * block or not. For the data fork we check this matches di_size, |
| 3987 | * implying the file's range is 0..bsize-1. |
| 3988 | */ |
| 3989 | int /* 1=>1 block, 0=>otherwise */ |
| 3990 | xfs_bmap_one_block( |
| 3991 | xfs_inode_t *ip, /* incore inode */ |
| 3992 | int whichfork) /* data or attr fork */ |
| 3993 | { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 3994 | xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3995 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 3996 | int rval; /* return value */ |
| 3997 | xfs_bmbt_irec_t s; /* internal version of extent */ |
| 3998 | |
| 3999 | #ifndef DEBUG |
Christoph Hellwig | ce7ae15 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 4000 | if (whichfork == XFS_DATA_FORK) |
| 4001 | return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | #endif /* !DEBUG */ |
| 4003 | if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1) |
| 4004 | return 0; |
| 4005 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
| 4006 | return 0; |
| 4007 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4008 | ASSERT(ifp->if_flags & XFS_IFEXTENTS); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4009 | ep = xfs_iext_get_ext(ifp, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | xfs_bmbt_get_all(ep, &s); |
| 4011 | rval = s.br_startoff == 0 && s.br_blockcount == 1; |
| 4012 | if (rval && whichfork == XFS_DATA_FORK) |
Christoph Hellwig | ce7ae15 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 4013 | ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | return rval; |
| 4015 | } |
| 4016 | |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4017 | STATIC int |
| 4018 | xfs_bmap_sanity_check( |
| 4019 | struct xfs_mount *mp, |
| 4020 | struct xfs_buf *bp, |
| 4021 | int level) |
| 4022 | { |
| 4023 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4024 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 4025 | if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) || |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4026 | be16_to_cpu(block->bb_level) != level || |
| 4027 | be16_to_cpu(block->bb_numrecs) == 0 || |
| 4028 | be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 4029 | return 0; |
| 4030 | return 1; |
| 4031 | } |
| 4032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | /* |
| 4034 | * Read in the extents to if_extents. |
| 4035 | * All inode fields are set up by caller, we just traverse the btree |
| 4036 | * and copy the records in. If the file system cannot contain unwritten |
| 4037 | * extents, the records are checked for no "state" flags. |
| 4038 | */ |
| 4039 | int /* error */ |
| 4040 | xfs_bmap_read_extents( |
| 4041 | xfs_trans_t *tp, /* transaction pointer */ |
| 4042 | xfs_inode_t *ip, /* incore inode */ |
| 4043 | int whichfork) /* data or attr fork */ |
| 4044 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4045 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | xfs_fsblock_t bno; /* block # of "block" */ |
| 4047 | xfs_buf_t *bp; /* buffer for "block" */ |
| 4048 | int error; /* error return value */ |
| 4049 | xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | xfs_extnum_t i, j; /* index into the extents list */ |
| 4051 | xfs_ifork_t *ifp; /* fork structure */ |
| 4052 | int level; /* btree level, for checking */ |
| 4053 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 4054 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | /* REFERENCED */ |
| 4056 | xfs_extnum_t room; /* number of entries there's room for */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | |
| 4058 | bno = NULLFSBLOCK; |
| 4059 | mp = ip->i_mount; |
| 4060 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 4061 | exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE : |
| 4062 | XFS_EXTFMT_INODE(ip); |
| 4063 | block = ifp->if_broot; |
| 4064 | /* |
| 4065 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 4066 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 4067 | level = be16_to_cpu(block->bb_level); |
| 4068 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 4069 | 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] | 4070 | bno = be64_to_cpu(*pp); |
| 4071 | ASSERT(bno != NULLDFSBNO); |
| 4072 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 4073 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | /* |
| 4075 | * Go down the tree until leaf level is reached, following the first |
| 4076 | * pointer (leftmost) at each level. |
| 4077 | */ |
| 4078 | while (level-- > 0) { |
| 4079 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4080 | XFS_BMAP_BTREE_REF))) |
| 4081 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4082 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4084 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | error0); |
| 4086 | if (level == 0) |
| 4087 | break; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4088 | 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] | 4089 | bno = be64_to_cpu(*pp); |
| 4090 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | xfs_trans_brelse(tp, bp); |
| 4092 | } |
| 4093 | /* |
| 4094 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 4095 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4096 | room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | i = 0; |
| 4098 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4099 | * Loop over all leaf nodes. Copy information to the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | */ |
| 4101 | for (;;) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4102 | xfs_bmbt_rec_t *frp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | xfs_fsblock_t nextbno; |
| 4104 | xfs_extnum_t num_recs; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4105 | xfs_extnum_t start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4107 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | if (unlikely(i + num_recs > room)) { |
| 4109 | ASSERT(i + num_recs <= room); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4110 | xfs_warn(ip->i_mount, |
Nathan Scott | 3762ec6 | 2006-01-12 10:29:53 +1100 | [diff] [blame] | 4111 | "corrupt dinode %Lu, (btree extents).", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | (unsigned long long) ip->i_ino); |
Dave Chinner | 65333b4 | 2011-03-07 10:03:35 +1100 | [diff] [blame] | 4113 | XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)", |
| 4114 | XFS_ERRLEVEL_LOW, ip->i_mount, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | goto error0; |
| 4116 | } |
| 4117 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 4118 | xfs_bmap_sanity_check(mp, bp, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | error0); |
| 4120 | /* |
| 4121 | * Read-ahead the next leaf block, if any. |
| 4122 | */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4123 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | if (nextbno != NULLFSBLOCK) |
| 4125 | xfs_btree_reada_bufl(mp, nextbno, 1); |
| 4126 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4127 | * Copy records into the extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 4129 | frp = XFS_BMBT_REC_ADDR(mp, block, 1); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4130 | start = i; |
| 4131 | for (j = 0; j < num_recs; j++, i++, frp++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 4132 | xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 4133 | trp->l0 = be64_to_cpu(frp->l0); |
| 4134 | trp->l1 = be64_to_cpu(frp->l1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | } |
| 4136 | if (exntf == XFS_EXTFMT_NOSTATE) { |
| 4137 | /* |
| 4138 | * Check all attribute bmap btree records and |
| 4139 | * any "older" data bmap btree records for a |
| 4140 | * set bit in the "extent flag" position. |
| 4141 | */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4142 | if (unlikely(xfs_check_nostate_extents(ifp, |
| 4143 | start, num_recs))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | XFS_ERROR_REPORT("xfs_bmap_read_extents(2)", |
| 4145 | XFS_ERRLEVEL_LOW, |
| 4146 | ip->i_mount); |
| 4147 | goto error0; |
| 4148 | } |
| 4149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | xfs_trans_brelse(tp, bp); |
| 4151 | bno = nextbno; |
| 4152 | /* |
| 4153 | * If we've reached the end, stop. |
| 4154 | */ |
| 4155 | if (bno == NULLFSBLOCK) |
| 4156 | break; |
| 4157 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 4158 | XFS_BMAP_BTREE_REF))) |
| 4159 | return error; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 4160 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4162 | ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork)); |
Eric Sandeen | 3a59c94 | 2007-07-11 11:09:47 +1000 | [diff] [blame] | 4164 | XFS_BMAP_TRACE_EXLIST(ip, i, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | return 0; |
| 4166 | error0: |
| 4167 | xfs_trans_brelse(tp, bp); |
| 4168 | return XFS_ERROR(EFSCORRUPTED); |
| 4169 | } |
| 4170 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4171 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4173 | * 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] | 4174 | */ |
| 4175 | void |
| 4176 | xfs_bmap_trace_exlist( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | xfs_inode_t *ip, /* incore inode pointer */ |
| 4178 | xfs_extnum_t cnt, /* count of entries in the list */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4179 | int whichfork, /* data or attr fork */ |
| 4180 | unsigned long caller_ip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4182 | xfs_extnum_t idx; /* extent record index */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | xfs_ifork_t *ifp; /* inode fork pointer */ |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4184 | int state = 0; |
| 4185 | |
| 4186 | if (whichfork == XFS_ATTR_FORK) |
| 4187 | state |= BMAP_ATTRFORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | |
| 4189 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4190 | ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 4191 | for (idx = 0; idx < cnt; idx++) |
| 4192 | trace_xfs_extlist(ip, idx, whichfork, caller_ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | /* |
| 4196 | * Validate that the bmbt_irecs being returned from bmapi are valid |
| 4197 | * given the callers original parameters. Specifically check the |
| 4198 | * ranges of the returned irecs to ensure that they only extent beyond |
| 4199 | * the given parameters if the XFS_BMAPI_ENTIRE flag was set. |
| 4200 | */ |
| 4201 | STATIC void |
| 4202 | xfs_bmap_validate_ret( |
| 4203 | xfs_fileoff_t bno, |
| 4204 | xfs_filblks_t len, |
| 4205 | int flags, |
| 4206 | xfs_bmbt_irec_t *mval, |
| 4207 | int nmap, |
| 4208 | int ret_nmap) |
| 4209 | { |
| 4210 | int i; /* index to map values */ |
| 4211 | |
| 4212 | ASSERT(ret_nmap <= nmap); |
| 4213 | |
| 4214 | for (i = 0; i < ret_nmap; i++) { |
| 4215 | ASSERT(mval[i].br_blockcount > 0); |
| 4216 | if (!(flags & XFS_BMAPI_ENTIRE)) { |
| 4217 | ASSERT(mval[i].br_startoff >= bno); |
| 4218 | ASSERT(mval[i].br_blockcount <= len); |
| 4219 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount <= |
| 4220 | bno + len); |
| 4221 | } else { |
| 4222 | ASSERT(mval[i].br_startoff < bno + len); |
| 4223 | ASSERT(mval[i].br_startoff + mval[i].br_blockcount > |
| 4224 | bno); |
| 4225 | } |
| 4226 | ASSERT(i == 0 || |
| 4227 | mval[i - 1].br_startoff + mval[i - 1].br_blockcount == |
| 4228 | mval[i].br_startoff); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4229 | ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK && |
| 4230 | mval[i].br_startblock != HOLESTARTBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | ASSERT(mval[i].br_state == XFS_EXT_NORM || |
| 4232 | mval[i].br_state == XFS_EXT_UNWRITTEN); |
| 4233 | } |
| 4234 | } |
| 4235 | #endif /* DEBUG */ |
| 4236 | |
| 4237 | |
| 4238 | /* |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4239 | * Trim the returned map to the required bounds |
| 4240 | */ |
| 4241 | STATIC void |
| 4242 | xfs_bmapi_trim_map( |
| 4243 | struct xfs_bmbt_irec *mval, |
| 4244 | struct xfs_bmbt_irec *got, |
| 4245 | xfs_fileoff_t *bno, |
| 4246 | xfs_filblks_t len, |
| 4247 | xfs_fileoff_t obno, |
| 4248 | xfs_fileoff_t end, |
| 4249 | int n, |
| 4250 | int flags) |
| 4251 | { |
| 4252 | if ((flags & XFS_BMAPI_ENTIRE) || |
| 4253 | got->br_startoff + got->br_blockcount <= obno) { |
| 4254 | *mval = *got; |
| 4255 | if (isnullstartblock(got->br_startblock)) |
| 4256 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4257 | return; |
| 4258 | } |
| 4259 | |
| 4260 | if (obno > *bno) |
| 4261 | *bno = obno; |
| 4262 | ASSERT((*bno >= obno) || (n == 0)); |
| 4263 | ASSERT(*bno < end); |
| 4264 | mval->br_startoff = *bno; |
| 4265 | if (isnullstartblock(got->br_startblock)) |
| 4266 | mval->br_startblock = DELAYSTARTBLOCK; |
| 4267 | else |
| 4268 | mval->br_startblock = got->br_startblock + |
| 4269 | (*bno - got->br_startoff); |
| 4270 | /* |
| 4271 | * Return the minimum of what we got and what we asked for for |
| 4272 | * the length. We can use the len variable here because it is |
| 4273 | * modified below and we could have been there before coming |
| 4274 | * here if the first part of the allocation didn't overlap what |
| 4275 | * was asked for. |
| 4276 | */ |
| 4277 | mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno, |
| 4278 | got->br_blockcount - (*bno - got->br_startoff)); |
| 4279 | mval->br_state = got->br_state; |
| 4280 | ASSERT(mval->br_blockcount <= len); |
| 4281 | return; |
| 4282 | } |
| 4283 | |
| 4284 | /* |
| 4285 | * Update and validate the extent map to return |
| 4286 | */ |
| 4287 | STATIC void |
| 4288 | xfs_bmapi_update_map( |
| 4289 | struct xfs_bmbt_irec **map, |
| 4290 | xfs_fileoff_t *bno, |
| 4291 | xfs_filblks_t *len, |
| 4292 | xfs_fileoff_t obno, |
| 4293 | xfs_fileoff_t end, |
| 4294 | int *n, |
| 4295 | int flags) |
| 4296 | { |
| 4297 | xfs_bmbt_irec_t *mval = *map; |
| 4298 | |
| 4299 | ASSERT((flags & XFS_BMAPI_ENTIRE) || |
| 4300 | ((mval->br_startoff + mval->br_blockcount) <= end)); |
| 4301 | ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) || |
| 4302 | (mval->br_startoff < obno)); |
| 4303 | |
| 4304 | *bno = mval->br_startoff + mval->br_blockcount; |
| 4305 | *len = end - *bno; |
| 4306 | if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) { |
| 4307 | /* update previous map with new information */ |
| 4308 | ASSERT(mval->br_startblock == mval[-1].br_startblock); |
| 4309 | ASSERT(mval->br_blockcount > mval[-1].br_blockcount); |
| 4310 | ASSERT(mval->br_state == mval[-1].br_state); |
| 4311 | mval[-1].br_blockcount = mval->br_blockcount; |
| 4312 | mval[-1].br_state = mval->br_state; |
| 4313 | } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK && |
| 4314 | mval[-1].br_startblock != DELAYSTARTBLOCK && |
| 4315 | mval[-1].br_startblock != HOLESTARTBLOCK && |
| 4316 | mval->br_startblock == mval[-1].br_startblock + |
| 4317 | mval[-1].br_blockcount && |
| 4318 | ((flags & XFS_BMAPI_IGSTATE) || |
| 4319 | mval[-1].br_state == mval->br_state)) { |
| 4320 | ASSERT(mval->br_startoff == |
| 4321 | mval[-1].br_startoff + mval[-1].br_blockcount); |
| 4322 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4323 | } else if (*n > 0 && |
| 4324 | mval->br_startblock == DELAYSTARTBLOCK && |
| 4325 | mval[-1].br_startblock == DELAYSTARTBLOCK && |
| 4326 | mval->br_startoff == |
| 4327 | mval[-1].br_startoff + mval[-1].br_blockcount) { |
| 4328 | mval[-1].br_blockcount += mval->br_blockcount; |
| 4329 | mval[-1].br_state = mval->br_state; |
| 4330 | } else if (!((*n == 0) && |
| 4331 | ((mval->br_startoff + mval->br_blockcount) <= |
| 4332 | obno))) { |
| 4333 | mval++; |
| 4334 | (*n)++; |
| 4335 | } |
| 4336 | *map = mval; |
| 4337 | } |
| 4338 | |
| 4339 | /* |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4340 | * Map file blocks to filesystem blocks without allocation. |
| 4341 | */ |
| 4342 | int |
| 4343 | xfs_bmapi_read( |
| 4344 | struct xfs_inode *ip, |
| 4345 | xfs_fileoff_t bno, |
| 4346 | xfs_filblks_t len, |
| 4347 | struct xfs_bmbt_irec *mval, |
| 4348 | int *nmap, |
| 4349 | int flags) |
| 4350 | { |
| 4351 | struct xfs_mount *mp = ip->i_mount; |
| 4352 | struct xfs_ifork *ifp; |
| 4353 | struct xfs_bmbt_irec got; |
| 4354 | struct xfs_bmbt_irec prev; |
| 4355 | xfs_fileoff_t obno; |
| 4356 | xfs_fileoff_t end; |
| 4357 | xfs_extnum_t lastx; |
| 4358 | int error; |
| 4359 | int eof; |
| 4360 | int n = 0; |
| 4361 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4362 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4363 | |
| 4364 | ASSERT(*nmap >= 1); |
| 4365 | ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE| |
| 4366 | XFS_BMAPI_IGSTATE))); |
| 4367 | |
| 4368 | if (unlikely(XFS_TEST_ERROR( |
| 4369 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4370 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE), |
| 4371 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4372 | XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp); |
| 4373 | return XFS_ERROR(EFSCORRUPTED); |
| 4374 | } |
| 4375 | |
| 4376 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4377 | return XFS_ERROR(EIO); |
| 4378 | |
| 4379 | XFS_STATS_INC(xs_blk_mapr); |
| 4380 | |
| 4381 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4382 | |
| 4383 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4384 | error = xfs_iread_extents(NULL, ip, whichfork); |
| 4385 | if (error) |
| 4386 | return error; |
| 4387 | } |
| 4388 | |
| 4389 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev); |
| 4390 | end = bno + len; |
| 4391 | obno = bno; |
| 4392 | |
| 4393 | while (bno < end && n < *nmap) { |
| 4394 | /* Reading past eof, act as though there's a hole up to end. */ |
| 4395 | if (eof) |
| 4396 | got.br_startoff = end; |
| 4397 | if (got.br_startoff > bno) { |
| 4398 | /* Reading in a hole. */ |
| 4399 | mval->br_startoff = bno; |
| 4400 | mval->br_startblock = HOLESTARTBLOCK; |
| 4401 | mval->br_blockcount = |
| 4402 | XFS_FILBLKS_MIN(len, got.br_startoff - bno); |
| 4403 | mval->br_state = XFS_EXT_NORM; |
| 4404 | bno += mval->br_blockcount; |
| 4405 | len -= mval->br_blockcount; |
| 4406 | mval++; |
| 4407 | n++; |
| 4408 | continue; |
| 4409 | } |
| 4410 | |
| 4411 | /* set up the extent map to return. */ |
| 4412 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4413 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4414 | |
| 4415 | /* If we're done, stop now. */ |
| 4416 | if (bno >= end || n >= *nmap) |
| 4417 | break; |
| 4418 | |
| 4419 | /* Else go on to the next record. */ |
| 4420 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4421 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4422 | else |
| 4423 | eof = 1; |
| 4424 | } |
| 4425 | *nmap = n; |
| 4426 | return 0; |
| 4427 | } |
| 4428 | |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4429 | STATIC int |
| 4430 | xfs_bmapi_reserve_delalloc( |
| 4431 | struct xfs_inode *ip, |
| 4432 | xfs_fileoff_t aoff, |
| 4433 | xfs_filblks_t len, |
| 4434 | struct xfs_bmbt_irec *got, |
| 4435 | struct xfs_bmbt_irec *prev, |
| 4436 | xfs_extnum_t *lastx, |
| 4437 | int eof) |
| 4438 | { |
| 4439 | struct xfs_mount *mp = ip->i_mount; |
| 4440 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4441 | xfs_extlen_t alen; |
| 4442 | xfs_extlen_t indlen; |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4443 | char rt = XFS_IS_REALTIME_INODE(ip); |
| 4444 | xfs_extlen_t extsz; |
| 4445 | int error; |
| 4446 | |
| 4447 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN); |
| 4448 | if (!eof) |
| 4449 | alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff); |
| 4450 | |
| 4451 | /* Figure out the extent size, adjust alen */ |
| 4452 | extsz = xfs_get_extsz_hint(ip); |
| 4453 | if (extsz) { |
| 4454 | /* |
| 4455 | * Make sure we don't exceed a single extent length when we |
| 4456 | * align the extent by reducing length we are going to |
| 4457 | * allocate by the maximum amount extent size aligment may |
| 4458 | * require. |
| 4459 | */ |
| 4460 | alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1)); |
| 4461 | error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof, |
| 4462 | 1, 0, &aoff, &alen); |
| 4463 | ASSERT(!error); |
| 4464 | } |
| 4465 | |
| 4466 | if (rt) |
| 4467 | extsz = alen / mp->m_sb.sb_rextsize; |
| 4468 | |
| 4469 | /* |
| 4470 | * Make a transaction-less quota reservation for delayed allocation |
| 4471 | * blocks. This number gets adjusted later. We return if we haven't |
| 4472 | * allocated blocks already inside this loop. |
| 4473 | */ |
| 4474 | error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0, |
| 4475 | rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4476 | if (error) |
| 4477 | return error; |
| 4478 | |
| 4479 | /* |
| 4480 | * Split changing sb for alen and indlen since they could be coming |
| 4481 | * from different places. |
| 4482 | */ |
| 4483 | indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen); |
| 4484 | ASSERT(indlen > 0); |
| 4485 | |
| 4486 | if (rt) { |
| 4487 | error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
| 4488 | -((int64_t)extsz), 0); |
| 4489 | } else { |
| 4490 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4491 | -((int64_t)alen), 0); |
| 4492 | } |
| 4493 | |
| 4494 | if (error) |
| 4495 | goto out_unreserve_quota; |
| 4496 | |
| 4497 | error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
| 4498 | -((int64_t)indlen), 0); |
| 4499 | if (error) |
| 4500 | goto out_unreserve_blocks; |
| 4501 | |
| 4502 | |
| 4503 | ip->i_delayed_blks += alen; |
| 4504 | |
| 4505 | got->br_startoff = aoff; |
| 4506 | got->br_startblock = nullstartblock(indlen); |
| 4507 | got->br_blockcount = alen; |
| 4508 | got->br_state = XFS_EXT_NORM; |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4509 | xfs_bmap_add_extent_hole_delay(ip, lastx, got); |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4510 | |
| 4511 | /* |
Christoph Hellwig | 1fd044d | 2011-09-18 20:40:49 +0000 | [diff] [blame] | 4512 | * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay |
| 4513 | * might have merged it into one of the neighbouring ones. |
Christoph Hellwig | b64dfe4 | 2011-09-18 20:40:47 +0000 | [diff] [blame] | 4514 | */ |
| 4515 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got); |
| 4516 | |
| 4517 | ASSERT(got->br_startoff <= aoff); |
| 4518 | ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen); |
| 4519 | ASSERT(isnullstartblock(got->br_startblock)); |
| 4520 | ASSERT(got->br_state == XFS_EXT_NORM); |
| 4521 | return 0; |
| 4522 | |
| 4523 | out_unreserve_blocks: |
| 4524 | if (rt) |
| 4525 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0); |
| 4526 | else |
| 4527 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0); |
| 4528 | out_unreserve_quota: |
| 4529 | if (XFS_IS_QUOTA_ON(mp)) |
| 4530 | xfs_trans_unreserve_quota_nblks(NULL, ip, alen, 0, rt ? |
| 4531 | XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS); |
| 4532 | return error; |
| 4533 | } |
| 4534 | |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 4535 | /* |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4536 | * Map file blocks to filesystem blocks, adding delayed allocations as needed. |
| 4537 | */ |
| 4538 | int |
| 4539 | xfs_bmapi_delay( |
| 4540 | struct xfs_inode *ip, /* incore inode */ |
| 4541 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4542 | xfs_filblks_t len, /* length to map in file */ |
| 4543 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4544 | int *nmap, /* i/o: mval size/count */ |
| 4545 | int flags) /* XFS_BMAPI_... */ |
| 4546 | { |
| 4547 | struct xfs_mount *mp = ip->i_mount; |
| 4548 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 4549 | struct xfs_bmbt_irec got; /* current file extent record */ |
| 4550 | struct xfs_bmbt_irec prev; /* previous file extent record */ |
| 4551 | xfs_fileoff_t obno; /* old block number (offset) */ |
| 4552 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4553 | xfs_extnum_t lastx; /* last useful extent number */ |
| 4554 | int eof; /* we've hit the end of extents */ |
| 4555 | int n = 0; /* current extent index */ |
| 4556 | int error = 0; |
| 4557 | |
| 4558 | ASSERT(*nmap >= 1); |
| 4559 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4560 | ASSERT(!(flags & ~XFS_BMAPI_ENTIRE)); |
| 4561 | |
| 4562 | if (unlikely(XFS_TEST_ERROR( |
| 4563 | (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS && |
| 4564 | XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE), |
| 4565 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
| 4566 | XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp); |
| 4567 | return XFS_ERROR(EFSCORRUPTED); |
| 4568 | } |
| 4569 | |
| 4570 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4571 | return XFS_ERROR(EIO); |
| 4572 | |
| 4573 | XFS_STATS_INC(xs_blk_mapw); |
| 4574 | |
| 4575 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4576 | error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK); |
| 4577 | if (error) |
| 4578 | return error; |
| 4579 | } |
| 4580 | |
| 4581 | xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev); |
| 4582 | end = bno + len; |
| 4583 | obno = bno; |
| 4584 | |
| 4585 | while (bno < end && n < *nmap) { |
| 4586 | if (eof || got.br_startoff > bno) { |
| 4587 | error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got, |
| 4588 | &prev, &lastx, eof); |
| 4589 | if (error) { |
| 4590 | if (n == 0) { |
| 4591 | *nmap = 0; |
| 4592 | return error; |
| 4593 | } |
| 4594 | break; |
| 4595 | } |
| 4596 | } |
| 4597 | |
| 4598 | /* set up the extent map to return. */ |
| 4599 | xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags); |
| 4600 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4601 | |
| 4602 | /* If we're done, stop now. */ |
| 4603 | if (bno >= end || n >= *nmap) |
| 4604 | break; |
| 4605 | |
| 4606 | /* Else go on to the next record. */ |
| 4607 | prev = got; |
| 4608 | if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) |
| 4609 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got); |
| 4610 | else |
| 4611 | eof = 1; |
| 4612 | } |
| 4613 | |
| 4614 | *nmap = n; |
| 4615 | return 0; |
| 4616 | } |
| 4617 | |
| 4618 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4619 | STATIC int |
| 4620 | xfs_bmapi_allocate( |
| 4621 | struct xfs_bmalloca *bma, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4622 | int flags) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4623 | { |
| 4624 | struct xfs_mount *mp = bma->ip->i_mount; |
| 4625 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4626 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4627 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4628 | int tmp_logflags = 0; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4629 | int error; |
| 4630 | int rt; |
| 4631 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4632 | ASSERT(bma->length > 0); |
| 4633 | |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4634 | rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip); |
| 4635 | |
| 4636 | /* |
| 4637 | * For the wasdelay case, we could also just allocate the stuff asked |
| 4638 | * for in this bmap call but that wouldn't be as good. |
| 4639 | */ |
| 4640 | if (bma->wasdel) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4641 | bma->length = (xfs_extlen_t)bma->got.br_blockcount; |
| 4642 | bma->offset = bma->got.br_startoff; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4643 | if (bma->idx != NULLEXTNUM && bma->idx) { |
| 4644 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4645 | &bma->prev); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4646 | } |
| 4647 | } else { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4648 | bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4649 | if (!bma->eof) |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4650 | bma->length = XFS_FILBLKS_MIN(bma->length, |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4651 | bma->got.br_startoff - bma->offset); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | /* |
| 4655 | * Indicate if this is the first user data in the file, or just any |
| 4656 | * user data. |
| 4657 | */ |
| 4658 | if (!(flags & XFS_BMAPI_METADATA)) { |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4659 | bma->userdata = (bma->offset == 0) ? |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4660 | XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA; |
| 4661 | } |
| 4662 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4663 | bma->minlen = (flags & XFS_BMAPI_CONTIG) ? bma->length : 1; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4664 | |
| 4665 | /* |
| 4666 | * Only want to do the alignment at the eof if it is userdata and |
| 4667 | * allocation length is larger than a stripe unit. |
| 4668 | */ |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4669 | if (mp->m_dalign && bma->length >= mp->m_dalign && |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4670 | !(flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) { |
Dave Chinner | 1b16447 | 2011-09-18 20:40:55 +0000 | [diff] [blame] | 4671 | error = xfs_bmap_isaeof(bma, whichfork); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4672 | if (error) |
| 4673 | return error; |
| 4674 | } |
| 4675 | |
| 4676 | error = xfs_bmap_alloc(bma); |
| 4677 | if (error) |
| 4678 | return error; |
| 4679 | |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4680 | if (bma->flist->xbf_low) |
| 4681 | bma->minleft = 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4682 | if (bma->cur) |
| 4683 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4684 | if (bma->blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4685 | return 0; |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4686 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4687 | bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork); |
| 4688 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4689 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4690 | } |
| 4691 | /* |
| 4692 | * Bump the number of extents we've allocated |
| 4693 | * in this call. |
| 4694 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4695 | bma->nallocs++; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4696 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4697 | if (bma->cur) |
| 4698 | bma->cur->bc_private.b.flags = |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4699 | bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0; |
| 4700 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4701 | bma->got.br_startoff = bma->offset; |
| 4702 | bma->got.br_startblock = bma->blkno; |
| 4703 | bma->got.br_blockcount = bma->length; |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4704 | bma->got.br_state = XFS_EXT_NORM; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4705 | |
| 4706 | /* |
| 4707 | * A wasdelay extent has been initialized, so shouldn't be flagged |
| 4708 | * as unwritten. |
| 4709 | */ |
| 4710 | if (!bma->wasdel && (flags & XFS_BMAPI_PREALLOC) && |
| 4711 | xfs_sb_version_hasextflgbit(&mp->m_sb)) |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4712 | bma->got.br_state = XFS_EXT_UNWRITTEN; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4713 | |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4714 | if (bma->wasdel) |
Christoph Hellwig | 572a4cf | 2011-09-18 20:41:04 +0000 | [diff] [blame] | 4715 | error = xfs_bmap_add_extent_delay_real(bma); |
Christoph Hellwig | c653424 | 2011-09-18 20:41:05 +0000 | [diff] [blame] | 4716 | else |
| 4717 | error = xfs_bmap_add_extent_hole_real(bma, whichfork); |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4718 | |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4719 | bma->logflags |= tmp_logflags; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4720 | if (error) |
| 4721 | return error; |
| 4722 | |
| 4723 | /* |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4724 | * Update our extent pointer, given that xfs_bmap_add_extent_delay_real |
| 4725 | * or xfs_bmap_add_extent_hole_real might have merged it into one of |
| 4726 | * the neighbouring ones. |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4727 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4728 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4729 | |
Dave Chinner | 963c30c | 2011-09-18 20:40:59 +0000 | [diff] [blame] | 4730 | ASSERT(bma->got.br_startoff <= bma->offset); |
| 4731 | ASSERT(bma->got.br_startoff + bma->got.br_blockcount >= |
| 4732 | bma->offset + bma->length); |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4733 | ASSERT(bma->got.br_state == XFS_EXT_NORM || |
| 4734 | bma->got.br_state == XFS_EXT_UNWRITTEN); |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4735 | return 0; |
| 4736 | } |
| 4737 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4738 | STATIC int |
| 4739 | xfs_bmapi_convert_unwritten( |
| 4740 | struct xfs_bmalloca *bma, |
| 4741 | struct xfs_bmbt_irec *mval, |
| 4742 | xfs_filblks_t len, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4743 | int flags) |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4744 | { |
| 4745 | int whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4746 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 4747 | struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4748 | int tmp_logflags = 0; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4749 | int error; |
| 4750 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4751 | /* check if we need to do unwritten->real conversion */ |
| 4752 | if (mval->br_state == XFS_EXT_UNWRITTEN && |
| 4753 | (flags & XFS_BMAPI_PREALLOC)) |
| 4754 | return 0; |
| 4755 | |
| 4756 | /* check if we need to do real->unwritten conversion */ |
| 4757 | if (mval->br_state == XFS_EXT_NORM && |
| 4758 | (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) != |
| 4759 | (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) |
| 4760 | return 0; |
| 4761 | |
| 4762 | /* |
| 4763 | * Modify (by adding) the state flag, if writing. |
| 4764 | */ |
| 4765 | ASSERT(mval->br_blockcount <= len); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4766 | if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) { |
| 4767 | bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp, |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4768 | bma->ip, whichfork); |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4769 | bma->cur->bc_private.b.firstblock = *bma->firstblock; |
| 4770 | bma->cur->bc_private.b.flist = bma->flist; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4771 | } |
| 4772 | mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN) |
| 4773 | ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN; |
| 4774 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4775 | error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4776 | &bma->cur, mval, bma->firstblock, bma->flist, |
| 4777 | &tmp_logflags); |
| 4778 | bma->logflags |= tmp_logflags; |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4779 | if (error) |
| 4780 | return error; |
| 4781 | |
| 4782 | /* |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 4783 | * Update our extent pointer, given that |
| 4784 | * xfs_bmap_add_extent_unwritten_real might have merged it into one |
| 4785 | * of the neighbouring ones. |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4786 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4787 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got); |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4788 | |
| 4789 | /* |
| 4790 | * We may have combined previously unwritten space with written space, |
| 4791 | * so generate another request. |
| 4792 | */ |
| 4793 | if (mval->br_blockcount < len) |
| 4794 | return EAGAIN; |
| 4795 | return 0; |
| 4796 | } |
| 4797 | |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4798 | /* |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4799 | * Map file blocks to filesystem blocks, and allocate blocks or convert the |
| 4800 | * extent state if necessary. Details behaviour is controlled by the flags |
| 4801 | * parameter. Only allocates blocks from a single allocation group, to avoid |
| 4802 | * locking problems. |
| 4803 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | * The returned value in "firstblock" from the first call in a transaction |
| 4805 | * must be remembered and presented to subsequent calls in "firstblock". |
| 4806 | * An upper bound for the number of blocks to be allocated is supplied to |
| 4807 | * the first call in "total"; if no allocation group has that many free |
| 4808 | * blocks then the call will fail (return NULLFSBLOCK in "firstblock"). |
| 4809 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4810 | int |
| 4811 | xfs_bmapi_write( |
| 4812 | struct xfs_trans *tp, /* transaction pointer */ |
| 4813 | struct xfs_inode *ip, /* incore inode */ |
| 4814 | xfs_fileoff_t bno, /* starting file offs. mapped */ |
| 4815 | xfs_filblks_t len, /* length to map in file */ |
| 4816 | int flags, /* XFS_BMAPI_... */ |
| 4817 | xfs_fsblock_t *firstblock, /* first allocated block |
| 4818 | controls a.g. for allocs */ |
| 4819 | xfs_extlen_t total, /* total blocks needed */ |
| 4820 | struct xfs_bmbt_irec *mval, /* output: map values */ |
| 4821 | int *nmap, /* i/o: mval size/count */ |
| 4822 | struct xfs_bmap_free *flist) /* i/o: list extents to free */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4824 | struct xfs_mount *mp = ip->i_mount; |
| 4825 | struct xfs_ifork *ifp; |
| 4826 | struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4827 | xfs_fileoff_t end; /* end of mapped file region */ |
| 4828 | int eof; /* after the end of extents */ |
| 4829 | int error; /* error return */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4830 | int n; /* current extent index */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4831 | xfs_fileoff_t obno; /* old block number (offset) */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4832 | int whichfork; /* data or attr fork */ |
| 4833 | char inhole; /* current location is hole in file */ |
| 4834 | char wasdelay; /* old extent was delayed */ |
| 4835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | #ifdef DEBUG |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4837 | xfs_fileoff_t orig_bno; /* original block number value */ |
| 4838 | int orig_flags; /* original flags arg value */ |
| 4839 | xfs_filblks_t orig_len; /* original value of len arg */ |
| 4840 | struct xfs_bmbt_irec *orig_mval; /* original value of mval */ |
| 4841 | int orig_nmap; /* original value of *nmap */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | |
| 4843 | orig_bno = bno; |
| 4844 | orig_len = len; |
| 4845 | orig_flags = flags; |
| 4846 | orig_mval = mval; |
| 4847 | orig_nmap = *nmap; |
| 4848 | #endif |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | ASSERT(*nmap >= 1); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4851 | ASSERT(*nmap <= XFS_BMAP_MAX_NMAP); |
| 4852 | ASSERT(!(flags & XFS_BMAPI_IGSTATE)); |
| 4853 | ASSERT(tp != NULL); |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4854 | ASSERT(len > 0); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 4857 | XFS_ATTR_FORK : XFS_DATA_FORK; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | if (unlikely(XFS_TEST_ERROR( |
| 4860 | (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 4861 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE && |
| 4862 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL), |
| 4863 | mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4864 | XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | return XFS_ERROR(EFSCORRUPTED); |
| 4866 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 4869 | return XFS_ERROR(EIO); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | ifp = XFS_IFORK_PTR(ip, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4872 | |
| 4873 | XFS_STATS_INC(xs_blk_mapw); |
| 4874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4876 | error = xfs_bmap_local_to_extents(tp, ip, firstblock, total, |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4877 | &bma.logflags, whichfork); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4878 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | goto error0; |
| 4880 | } |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4881 | |
| 4882 | if (*firstblock == NULLFSBLOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE) |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4884 | bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | else |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4886 | bma.minleft = 1; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4887 | } else { |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4888 | bma.minleft = 0; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
| 4891 | if (!(ifp->if_flags & XFS_IFEXTENTS)) { |
| 4892 | error = xfs_iread_extents(tp, ip, whichfork); |
| 4893 | if (error) |
| 4894 | goto error0; |
| 4895 | } |
| 4896 | |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4897 | xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got, |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4898 | &bma.prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | n = 0; |
| 4900 | end = bno + len; |
| 4901 | obno = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4902 | |
| 4903 | bma.tp = tp; |
| 4904 | bma.ip = ip; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4905 | bma.total = total; |
| 4906 | bma.userdata = 0; |
Dave Chinner | 0937e0f | 2011-09-18 20:40:57 +0000 | [diff] [blame] | 4907 | bma.flist = flist; |
| 4908 | bma.firstblock = firstblock; |
Christoph Hellwig | b4e9181 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 4909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | while (bno < end && n < *nmap) { |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4911 | inhole = eof || bma.got.br_startoff > bno; |
| 4912 | wasdelay = !inhole && isnullstartblock(bma.got.br_startblock); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | /* |
| 4915 | * First, deal with the hole before the allocated space |
| 4916 | * that we found, if any. |
| 4917 | */ |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4918 | if (inhole || wasdelay) { |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4919 | bma.eof = eof; |
| 4920 | bma.conv = !!(flags & XFS_BMAPI_CONVERT); |
| 4921 | bma.wasdel = wasdelay; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4922 | bma.offset = bno; |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4923 | |
Dave Chinner | a99ebf4 | 2011-12-01 11:24:20 +0000 | [diff] [blame] | 4924 | /* |
| 4925 | * There's a 32/64 bit type mismatch between the |
| 4926 | * allocation length request (which can be 64 bits in |
| 4927 | * length) and the bma length request, which is |
| 4928 | * xfs_extlen_t and therefore 32 bits. Hence we have to |
| 4929 | * check for 32-bit overflows and handle them here. |
| 4930 | */ |
| 4931 | if (len > (xfs_filblks_t)MAXEXTLEN) |
| 4932 | bma.length = MAXEXTLEN; |
| 4933 | else |
| 4934 | bma.length = len; |
| 4935 | |
| 4936 | ASSERT(len > 0); |
| 4937 | ASSERT(bma.length > 0); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4938 | error = xfs_bmapi_allocate(&bma, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | if (error) |
| 4940 | goto error0; |
Dave Chinner | 3a75667 | 2011-09-18 20:40:58 +0000 | [diff] [blame] | 4941 | if (bma.blkno == NULLFSBLOCK) |
Dave Chinner | 7e47a4e | 2011-09-18 20:40:50 +0000 | [diff] [blame] | 4942 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | } |
Christoph Hellwig | 4403280 | 2011-09-18 20:40:48 +0000 | [diff] [blame] | 4944 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4945 | /* Deal with the allocated space we found. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4946 | xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno, |
| 4947 | end, n, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | |
Dave Chinner | b447fe5 | 2011-09-18 20:40:51 +0000 | [diff] [blame] | 4949 | /* Execute unwritten extent conversion if necessary */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4950 | error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4951 | if (error == EAGAIN) |
| 4952 | continue; |
| 4953 | if (error) |
| 4954 | goto error0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | |
Dave Chinner | aef9a89 | 2011-09-18 20:40:44 +0000 | [diff] [blame] | 4956 | /* update the extent map to return */ |
| 4957 | xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags); |
| 4958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | /* |
| 4960 | * If we're done, stop now. Stop when we've allocated |
| 4961 | * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise |
| 4962 | * the transaction may get too big. |
| 4963 | */ |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4964 | if (bno >= end || n >= *nmap || bma.nallocs >= *nmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | break; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4966 | |
| 4967 | /* Else go on to the next record. */ |
Dave Chinner | baf41a5 | 2011-09-18 20:40:56 +0000 | [diff] [blame] | 4968 | bma.prev = bma.got; |
Dave Chinner | e0c3da5 | 2011-09-18 20:41:01 +0000 | [diff] [blame] | 4969 | if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) { |
| 4970 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx), |
| 4971 | &bma.got); |
| 4972 | } else |
Christoph Hellwig | 5690f92 | 2011-05-11 15:04:07 +0000 | [diff] [blame] | 4973 | eof = 1; |
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; |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 4976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | /* |
| 4978 | * Transform from btree to extents, give it cur. |
| 4979 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4980 | if (xfs_bmap_wants_extents(ip, whichfork)) { |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4981 | int tmp_logflags = 0; |
| 4982 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 4983 | ASSERT(bma.cur); |
| 4984 | error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | &tmp_logflags, whichfork); |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 4986 | bma.logflags |= tmp_logflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | if (error) |
| 4988 | goto error0; |
| 4989 | } |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE || |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 4992 | XFS_IFORK_NEXTENTS(ip, whichfork) > |
| 4993 | XFS_IFORK_MAXEXT(ip, whichfork)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | error0: |
| 4996 | /* |
| 4997 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 4998 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5000 | if ((bma.logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5002 | bma.logflags &= ~xfs_ilog_fext(whichfork); |
| 5003 | else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5005 | bma.logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | /* |
| 5007 | * Log whatever the flags say, even if error. Otherwise we might miss |
| 5008 | * detecting a case where the data is changed, there's an error, |
| 5009 | * and it's not logged so we don't shutdown when we should. |
| 5010 | */ |
Christoph Hellwig | c315c90 | 2011-09-18 20:41:02 +0000 | [diff] [blame] | 5011 | if (bma.logflags) |
| 5012 | xfs_trans_log_inode(tp, ip, bma.logflags); |
Dave Chinner | c0dc782 | 2011-09-18 20:40:52 +0000 | [diff] [blame] | 5013 | |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5014 | if (bma.cur) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5015 | if (!error) { |
| 5016 | ASSERT(*firstblock == NULLFSBLOCK || |
| 5017 | XFS_FSB_TO_AGNO(mp, *firstblock) == |
| 5018 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5019 | bma.cur->bc_private.b.firstblock) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | (flist->xbf_low && |
| 5021 | XFS_FSB_TO_AGNO(mp, *firstblock) < |
| 5022 | XFS_FSB_TO_AGNO(mp, |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5023 | bma.cur->bc_private.b.firstblock))); |
| 5024 | *firstblock = bma.cur->bc_private.b.firstblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | } |
Dave Chinner | 29c8d17 | 2011-09-18 20:41:00 +0000 | [diff] [blame] | 5026 | xfs_btree_del_cursor(bma.cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5028 | } |
| 5029 | if (!error) |
| 5030 | xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval, |
| 5031 | orig_nmap, *nmap); |
| 5032 | return error; |
| 5033 | } |
| 5034 | |
| 5035 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | * Unmap (remove) blocks from a file. |
| 5037 | * If nexts is nonzero then the number of extents to remove is limited to |
| 5038 | * that value. If not all extents in the block range can be removed then |
| 5039 | * *done is set. |
| 5040 | */ |
| 5041 | int /* error */ |
| 5042 | xfs_bunmapi( |
| 5043 | xfs_trans_t *tp, /* transaction pointer */ |
| 5044 | struct xfs_inode *ip, /* incore inode */ |
| 5045 | xfs_fileoff_t bno, /* starting offset to unmap */ |
| 5046 | xfs_filblks_t len, /* length to unmap in file */ |
| 5047 | int flags, /* misc flags */ |
| 5048 | xfs_extnum_t nexts, /* number of extents max */ |
| 5049 | xfs_fsblock_t *firstblock, /* first allocated block |
| 5050 | controls a.g. for allocs */ |
| 5051 | xfs_bmap_free_t *flist, /* i/o: list extents to free */ |
| 5052 | int *done) /* set if not done yet */ |
| 5053 | { |
| 5054 | xfs_btree_cur_t *cur; /* bmap btree cursor */ |
| 5055 | xfs_bmbt_irec_t del; /* extent being deleted */ |
| 5056 | int eof; /* is deleting at eof */ |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 5057 | xfs_bmbt_rec_host_t *ep; /* extent record pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | int error; /* error return value */ |
| 5059 | xfs_extnum_t extno; /* extent number in list */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5060 | xfs_bmbt_irec_t got; /* current extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5062 | int isrt; /* freeing in rt area */ |
| 5063 | xfs_extnum_t lastx; /* last extent index used */ |
| 5064 | int logflags; /* transaction logging flags */ |
| 5065 | xfs_extlen_t mod; /* rt extent offset */ |
| 5066 | xfs_mount_t *mp; /* mount structure */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5067 | xfs_extnum_t nextents; /* number of file extents */ |
| 5068 | xfs_bmbt_irec_t prev; /* previous extent record */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | xfs_fileoff_t start; /* first file offset deleted */ |
| 5070 | int tmp_logflags; /* partial logging flags */ |
| 5071 | int wasdel; /* was a delayed alloc extent */ |
| 5072 | int whichfork; /* data or attribute fork */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | xfs_fsblock_t sum; |
| 5074 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 5075 | trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_); |
| 5076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | whichfork = (flags & XFS_BMAPI_ATTRFORK) ? |
| 5078 | XFS_ATTR_FORK : XFS_DATA_FORK; |
| 5079 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5080 | if (unlikely( |
| 5081 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS && |
| 5082 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { |
| 5083 | XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW, |
| 5084 | ip->i_mount); |
| 5085 | return XFS_ERROR(EFSCORRUPTED); |
| 5086 | } |
| 5087 | mp = ip->i_mount; |
| 5088 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 5089 | return XFS_ERROR(EIO); |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | ASSERT(len > 0); |
| 5092 | ASSERT(nexts >= 0); |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Kamal Dasu | 5575acc | 2012-02-23 00:41:39 +0000 | [diff] [blame] | 5127 | |
| 5128 | if (isrt) { |
| 5129 | /* |
| 5130 | * Synchronize by locking the bitmap inode. |
| 5131 | */ |
| 5132 | xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL); |
| 5133 | xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL); |
| 5134 | } |
| 5135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | extno = 0; |
| 5137 | while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 && |
| 5138 | (nexts == 0 || extno < nexts)) { |
| 5139 | /* |
| 5140 | * Is the found extent after a hole in which bno lives? |
| 5141 | * Just back up to the previous extent, if so. |
| 5142 | */ |
| 5143 | if (got.br_startoff > bno) { |
| 5144 | if (--lastx < 0) |
| 5145 | break; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5146 | ep = xfs_iext_get_ext(ifp, lastx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | xfs_bmbt_get_all(ep, &got); |
| 5148 | } |
| 5149 | /* |
| 5150 | * Is the last block of this extent before the range |
| 5151 | * we're supposed to delete? If so, we're done. |
| 5152 | */ |
| 5153 | bno = XFS_FILEOFF_MIN(bno, |
| 5154 | got.br_startoff + got.br_blockcount - 1); |
| 5155 | if (bno < start) |
| 5156 | break; |
| 5157 | /* |
| 5158 | * Then deal with the (possibly delayed) allocated space |
| 5159 | * we found. |
| 5160 | */ |
| 5161 | ASSERT(ep != NULL); |
| 5162 | del = got; |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5163 | wasdel = isnullstartblock(del.br_startblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | if (got.br_startoff < start) { |
| 5165 | del.br_startoff = start; |
| 5166 | del.br_blockcount -= start - got.br_startoff; |
| 5167 | if (!wasdel) |
| 5168 | del.br_startblock += start - got.br_startoff; |
| 5169 | } |
| 5170 | if (del.br_startoff + del.br_blockcount > bno + 1) |
| 5171 | del.br_blockcount = bno + 1 - del.br_startoff; |
| 5172 | sum = del.br_startblock + del.br_blockcount; |
| 5173 | if (isrt && |
| 5174 | (mod = do_mod(sum, mp->m_sb.sb_rextsize))) { |
| 5175 | /* |
| 5176 | * Realtime extent not lined up at the end. |
| 5177 | * The extent could have been split into written |
| 5178 | * and unwritten pieces, or we could just be |
| 5179 | * unmapping part of it. But we can't really |
| 5180 | * get rid of part of a realtime extent. |
| 5181 | */ |
| 5182 | if (del.br_state == XFS_EXT_UNWRITTEN || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5183 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | /* |
| 5185 | * This piece is unwritten, or we're not |
| 5186 | * using unwritten extents. Skip over it. |
| 5187 | */ |
| 5188 | ASSERT(bno >= mod); |
| 5189 | bno -= mod > del.br_blockcount ? |
| 5190 | del.br_blockcount : mod; |
| 5191 | if (bno < got.br_startoff) { |
| 5192 | if (--lastx >= 0) |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5193 | xfs_bmbt_get_all(xfs_iext_get_ext( |
| 5194 | ifp, lastx), &got); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | } |
| 5196 | continue; |
| 5197 | } |
| 5198 | /* |
| 5199 | * It's written, turn it unwritten. |
| 5200 | * This is better than zeroing it. |
| 5201 | */ |
| 5202 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5203 | ASSERT(xfs_trans_get_block_res(tp) > 0); |
| 5204 | /* |
| 5205 | * If this spans a realtime extent boundary, |
| 5206 | * chop it back to the start of the one we end at. |
| 5207 | */ |
| 5208 | if (del.br_blockcount > mod) { |
| 5209 | del.br_startoff += del.br_blockcount - mod; |
| 5210 | del.br_startblock += del.br_blockcount - mod; |
| 5211 | del.br_blockcount = mod; |
| 5212 | } |
| 5213 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5214 | error = xfs_bmap_add_extent_unwritten_real(tp, ip, |
| 5215 | &lastx, &cur, &del, firstblock, flist, |
| 5216 | &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | if (error) |
| 5218 | goto error0; |
| 5219 | goto nodelete; |
| 5220 | } |
| 5221 | if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) { |
| 5222 | /* |
| 5223 | * Realtime extent is lined up at the end but not |
| 5224 | * at the front. We'll get rid of full extents if |
| 5225 | * we can. |
| 5226 | */ |
| 5227 | mod = mp->m_sb.sb_rextsize - mod; |
| 5228 | if (del.br_blockcount > mod) { |
| 5229 | del.br_blockcount -= mod; |
| 5230 | del.br_startoff += mod; |
| 5231 | del.br_startblock += mod; |
| 5232 | } else if ((del.br_startoff == start && |
| 5233 | (del.br_state == XFS_EXT_UNWRITTEN || |
| 5234 | xfs_trans_get_block_res(tp) == 0)) || |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 5235 | !xfs_sb_version_hasextflgbit(&mp->m_sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | /* |
| 5237 | * Can't make it unwritten. There isn't |
| 5238 | * a full extent here so just skip it. |
| 5239 | */ |
| 5240 | ASSERT(bno >= del.br_blockcount); |
| 5241 | bno -= del.br_blockcount; |
Christoph Hellwig | f1c63b7 | 2011-05-11 15:04:09 +0000 | [diff] [blame] | 5242 | if (got.br_startoff > bno) { |
| 5243 | if (--lastx >= 0) { |
| 5244 | ep = xfs_iext_get_ext(ifp, |
| 5245 | lastx); |
| 5246 | xfs_bmbt_get_all(ep, &got); |
| 5247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | } |
| 5249 | continue; |
| 5250 | } else if (del.br_state == XFS_EXT_UNWRITTEN) { |
| 5251 | /* |
| 5252 | * This one is already unwritten. |
| 5253 | * It must have a written left neighbor. |
| 5254 | * Unwrite the killed part of that one and |
| 5255 | * try again. |
| 5256 | */ |
| 5257 | ASSERT(lastx > 0); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5258 | xfs_bmbt_get_all(xfs_iext_get_ext(ifp, |
| 5259 | lastx - 1), &prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | ASSERT(prev.br_state == XFS_EXT_NORM); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5261 | ASSERT(!isnullstartblock(prev.br_startblock)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5262 | ASSERT(del.br_startblock == |
| 5263 | prev.br_startblock + prev.br_blockcount); |
| 5264 | if (prev.br_startoff < start) { |
| 5265 | mod = start - prev.br_startoff; |
| 5266 | prev.br_blockcount -= mod; |
| 5267 | prev.br_startblock += mod; |
| 5268 | prev.br_startoff = start; |
| 5269 | } |
| 5270 | prev.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5271 | lastx--; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5272 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5273 | ip, &lastx, &cur, &prev, |
| 5274 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | if (error) |
| 5276 | goto error0; |
| 5277 | goto nodelete; |
| 5278 | } else { |
| 5279 | ASSERT(del.br_state == XFS_EXT_NORM); |
| 5280 | del.br_state = XFS_EXT_UNWRITTEN; |
Christoph Hellwig | a5bd606 | 2011-09-18 20:40:54 +0000 | [diff] [blame] | 5281 | error = xfs_bmap_add_extent_unwritten_real(tp, |
| 5282 | ip, &lastx, &cur, &del, |
| 5283 | firstblock, flist, &logflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | if (error) |
| 5285 | goto error0; |
| 5286 | goto nodelete; |
| 5287 | } |
| 5288 | } |
| 5289 | if (wasdel) { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5290 | ASSERT(startblockval(del.br_startblock) > 0); |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5291 | /* Update realtime/data freespace, unreserve quota */ |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5292 | if (isrt) { |
| 5293 | xfs_filblks_t rtexts; |
| 5294 | |
| 5295 | rtexts = XFS_FSB_TO_B(mp, del.br_blockcount); |
| 5296 | do_div(rtexts, mp->m_sb.sb_rextsize); |
| 5297 | xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5298 | (int64_t)rtexts, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5299 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5300 | ip, -((long)del.br_blockcount), 0, |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5301 | XFS_QMOPT_RES_RTBLKS); |
| 5302 | } else { |
Christoph Hellwig | 96540c7 | 2010-09-30 02:25:55 +0000 | [diff] [blame] | 5303 | xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5304 | (int64_t)del.br_blockcount, 0); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 5305 | (void)xfs_trans_reserve_quota_nblks(NULL, |
| 5306 | ip, -((long)del.br_blockcount), 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | XFS_QMOPT_RES_REGBLKS); |
Nathan Scott | 06d10dd | 2005-06-21 15:48:47 +1000 | [diff] [blame] | 5308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | ip->i_delayed_blks -= del.br_blockcount; |
| 5310 | if (cur) |
| 5311 | cur->bc_private.b.flags |= |
| 5312 | XFS_BTCUR_BPRV_WASDEL; |
| 5313 | } else if (cur) |
| 5314 | cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL; |
| 5315 | /* |
| 5316 | * If it's the case where the directory code is running |
| 5317 | * with no block reservation, and the deleted block is in |
| 5318 | * the middle of its extent, and the resulting insert |
| 5319 | * of an extent would cause transformation to btree format, |
| 5320 | * then reject it. The calling code will then swap |
| 5321 | * blocks around instead. |
| 5322 | * We have to do this now, rather than waiting for the |
| 5323 | * conversion to btree format, since the transaction |
| 5324 | * will be dirty. |
| 5325 | */ |
| 5326 | if (!wasdel && xfs_trans_get_block_res(tp) == 0 && |
| 5327 | XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS && |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5328 | XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */ |
| 5329 | XFS_IFORK_MAXEXT(ip, whichfork) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | del.br_startoff > got.br_startoff && |
| 5331 | del.br_startoff + del.br_blockcount < |
| 5332 | got.br_startoff + got.br_blockcount) { |
| 5333 | error = XFS_ERROR(ENOSPC); |
| 5334 | goto error0; |
| 5335 | } |
Christoph Hellwig | ec90c55 | 2011-05-23 08:52:53 +0000 | [diff] [blame] | 5336 | error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del, |
Christoph Hellwig | 5489327 | 2011-05-11 15:04:03 +0000 | [diff] [blame] | 5337 | &tmp_logflags, whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | logflags |= tmp_logflags; |
| 5339 | if (error) |
| 5340 | goto error0; |
| 5341 | bno = del.br_startoff - 1; |
| 5342 | nodelete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | /* |
| 5344 | * If not done go on to the next (previous) record. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5345 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | if (bno != (xfs_fileoff_t)-1 && bno >= start) { |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5347 | if (lastx >= 0) { |
| 5348 | ep = xfs_iext_get_ext(ifp, lastx); |
| 5349 | if (xfs_bmbt_get_startoff(ep) > bno) { |
| 5350 | if (--lastx >= 0) |
| 5351 | ep = xfs_iext_get_ext(ifp, |
| 5352 | lastx); |
| 5353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | xfs_bmbt_get_all(ep, &got); |
Christoph Hellwig | 00239ac | 2011-05-11 15:04:08 +0000 | [diff] [blame] | 5355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | extno++; |
| 5357 | } |
| 5358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5359 | *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0; |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | /* |
| 5362 | * Convert to a btree if necessary. |
| 5363 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5364 | if (xfs_bmap_needs_btree(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | ASSERT(cur == NULL); |
| 5366 | error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, |
| 5367 | &cur, 0, &tmp_logflags, whichfork); |
| 5368 | logflags |= tmp_logflags; |
| 5369 | if (error) |
| 5370 | goto error0; |
| 5371 | } |
| 5372 | /* |
| 5373 | * transform from btree to extents, give it cur |
| 5374 | */ |
Christoph Hellwig | 8096b1e | 2011-12-18 20:00:07 +0000 | [diff] [blame] | 5375 | else if (xfs_bmap_wants_extents(ip, whichfork)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | ASSERT(cur != NULL); |
| 5377 | error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags, |
| 5378 | whichfork); |
| 5379 | logflags |= tmp_logflags; |
| 5380 | if (error) |
| 5381 | goto error0; |
| 5382 | } |
| 5383 | /* |
| 5384 | * transform from extents to local? |
| 5385 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | error = 0; |
| 5387 | error0: |
| 5388 | /* |
| 5389 | * Log everything. Do this after conversion, there's no point in |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5390 | * logging the extent records if we've converted to btree format. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5392 | if ((logflags & xfs_ilog_fext(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5394 | logflags &= ~xfs_ilog_fext(whichfork); |
| 5395 | else if ((logflags & xfs_ilog_fbroot(whichfork)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5397 | logflags &= ~xfs_ilog_fbroot(whichfork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5398 | /* |
| 5399 | * Log inode even in the error case, if the transaction |
| 5400 | * is dirty we'll need to shut down the filesystem. |
| 5401 | */ |
| 5402 | if (logflags) |
| 5403 | xfs_trans_log_inode(tp, ip, logflags); |
| 5404 | if (cur) { |
| 5405 | if (!error) { |
| 5406 | *firstblock = cur->bc_private.b.firstblock; |
| 5407 | cur->bc_private.b.allocated = 0; |
| 5408 | } |
| 5409 | xfs_btree_del_cursor(cur, |
| 5410 | error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 5411 | } |
| 5412 | return error; |
| 5413 | } |
| 5414 | |
| 5415 | /* |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5416 | * returns 1 for success, 0 if we failed to map the extent. |
| 5417 | */ |
| 5418 | STATIC int |
| 5419 | xfs_getbmapx_fix_eof_hole( |
| 5420 | xfs_inode_t *ip, /* xfs incore inode pointer */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5421 | struct getbmapx *out, /* output structure */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5422 | int prealloced, /* this is a file with |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5423 | * preallocated data space */ |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5424 | __int64_t end, /* last block requested */ |
| 5425 | xfs_fsblock_t startblock) |
| 5426 | { |
| 5427 | __int64_t fixlen; |
| 5428 | xfs_mount_t *mp; /* file system mount point */ |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5429 | xfs_ifork_t *ifp; /* inode fork pointer */ |
| 5430 | xfs_extnum_t lastx; /* last extent pointer */ |
| 5431 | xfs_fileoff_t fileblock; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5432 | |
| 5433 | if (startblock == HOLESTARTBLOCK) { |
| 5434 | mp = ip->i_mount; |
| 5435 | out->bmv_block = -1; |
Christoph Hellwig | ce7ae15 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 5436 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip))); |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5437 | fixlen -= out->bmv_offset; |
| 5438 | if (prealloced && out->bmv_offset + out->bmv_length == end) { |
| 5439 | /* Came to hole at EOF. Trim it. */ |
| 5440 | if (fixlen <= 0) |
| 5441 | return 0; |
| 5442 | out->bmv_length = fixlen; |
| 5443 | } |
| 5444 | } else { |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5445 | if (startblock == DELAYSTARTBLOCK) |
| 5446 | out->bmv_block = -2; |
| 5447 | else |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 5448 | out->bmv_block = xfs_fsb_to_db(ip, startblock); |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5449 | fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset); |
| 5450 | ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK); |
| 5451 | if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) && |
| 5452 | (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1)) |
| 5453 | out->bmv_oflags |= BMV_OF_LAST; |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | return 1; |
| 5457 | } |
| 5458 | |
| 5459 | /* |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5460 | * Get inode's extents as described in bmv, and format for output. |
| 5461 | * Calls formatter to fill the user's buffer until all extents |
| 5462 | * are mapped, until the passed-in bmv->bmv_count slots have |
| 5463 | * been filled, or until the formatter short-circuits the loop, |
| 5464 | * if it is tracking filled-in extents on its own. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | */ |
| 5466 | int /* error code */ |
| 5467 | xfs_getbmap( |
Christoph Hellwig | 993386c | 2007-08-28 16:12:30 +1000 | [diff] [blame] | 5468 | xfs_inode_t *ip, |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5469 | struct getbmapx *bmv, /* user bmap structure */ |
| 5470 | xfs_bmap_format_t formatter, /* format to user */ |
| 5471 | void *arg) /* formatter arg */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | { |
| 5473 | __int64_t bmvend; /* last block requested */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5474 | int error = 0; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | __int64_t fixlen; /* length for -1 case */ |
| 5476 | int i; /* extent number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | int lock; /* lock state */ |
| 5478 | xfs_bmbt_irec_t *map; /* buffer for user's data */ |
| 5479 | xfs_mount_t *mp; /* file system mount point */ |
| 5480 | int nex; /* # of user extents can do */ |
| 5481 | int nexleft; /* # of user extents left */ |
| 5482 | int subnex; /* # of bmapi's can do */ |
| 5483 | int nmap; /* number of map entries */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5484 | struct getbmapx *out; /* output structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | int whichfork; /* data or attr fork */ |
| 5486 | int prealloced; /* this is a file with |
| 5487 | * preallocated data space */ |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5488 | int iflags; /* interface flags */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5489 | int bmapi_flags; /* flags for xfs_bmapi */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5490 | int cur_ext = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | mp = ip->i_mount; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5493 | iflags = bmv->bmv_iflags; |
Eric Sandeen | 8a7141a | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5494 | whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5496 | if (whichfork == XFS_ATTR_FORK) { |
| 5497 | if (XFS_IFORK_Q(ip)) { |
| 5498 | if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS && |
| 5499 | ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE && |
| 5500 | ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL) |
| 5501 | return XFS_ERROR(EINVAL); |
| 5502 | } else if (unlikely( |
| 5503 | ip->i_d.di_aformat != 0 && |
| 5504 | ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) { |
| 5505 | XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW, |
| 5506 | ip->i_mount); |
| 5507 | return XFS_ERROR(EFSCORRUPTED); |
| 5508 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5509 | |
| 5510 | prealloced = 0; |
| 5511 | fixlen = 1LL << 32; |
| 5512 | } else { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5513 | if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS && |
| 5514 | ip->i_d.di_format != XFS_DINODE_FMT_BTREE && |
| 5515 | ip->i_d.di_format != XFS_DINODE_FMT_LOCAL) |
| 5516 | return XFS_ERROR(EINVAL); |
| 5517 | |
David Chinner | 957d0eb | 2007-06-18 16:50:37 +1000 | [diff] [blame] | 5518 | if (xfs_get_extsz_hint(ip) || |
Nathan Scott | dd9f438 | 2006-01-11 15:28:28 +1100 | [diff] [blame] | 5519 | ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | prealloced = 1; |
| 5521 | fixlen = XFS_MAXIOFFSET(mp); |
| 5522 | } else { |
| 5523 | prealloced = 0; |
Christoph Hellwig | ce7ae15 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 5524 | fixlen = XFS_ISIZE(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | if (bmv->bmv_length == -1) { |
| 5529 | fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen)); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5530 | bmv->bmv_length = |
| 5531 | max_t(__int64_t, fixlen - bmv->bmv_offset, 0); |
| 5532 | } else if (bmv->bmv_length == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | bmv->bmv_entries = 0; |
| 5534 | return 0; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5535 | } else if (bmv->bmv_length < 0) { |
| 5536 | return XFS_ERROR(EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | nex = bmv->bmv_count - 1; |
| 5540 | if (nex <= 0) |
| 5541 | return XFS_ERROR(EINVAL); |
| 5542 | bmvend = bmv->bmv_offset + bmv->bmv_length; |
| 5543 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5544 | |
| 5545 | if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx)) |
| 5546 | return XFS_ERROR(ENOMEM); |
| 5547 | out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL); |
Dave Chinner | f074211 | 2012-03-07 04:50:22 +0000 | [diff] [blame] | 5548 | if (!out) { |
| 5549 | out = kmem_zalloc_large(bmv->bmv_count * |
| 5550 | sizeof(struct getbmapx)); |
| 5551 | if (!out) |
| 5552 | return XFS_ERROR(ENOMEM); |
| 5553 | } |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5555 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5556 | if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) { |
Christoph Hellwig | ce7ae15 | 2011-12-18 20:00:11 +0000 | [diff] [blame] | 5557 | if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) { |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5558 | error = xfs_flush_pages(ip, 0, -1, 0, FI_REMAPF); |
| 5559 | if (error) |
| 5560 | goto out_unlock_iolock; |
Niv Sardi | e12070a | 2008-03-06 13:43:03 +1100 | [diff] [blame] | 5561 | } |
Dave Chinner | 309c848 | 2010-11-30 15:16:02 +1100 | [diff] [blame] | 5562 | /* |
| 5563 | * even after flushing the inode, there can still be delalloc |
| 5564 | * blocks on the inode beyond EOF due to speculative |
| 5565 | * preallocation. These are not removed until the release |
| 5566 | * function is called or the inode is inactivated. Hence we |
| 5567 | * cannot assert here that ip->i_delayed_blks == 0. |
| 5568 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | |
| 5571 | lock = xfs_ilock_map_shared(ip); |
| 5572 | |
| 5573 | /* |
| 5574 | * Don't let nex be bigger than the number of extents |
| 5575 | * we can have assuming alternating holes and real extents. |
| 5576 | */ |
| 5577 | if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1) |
| 5578 | nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1; |
| 5579 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5580 | bmapi_flags = xfs_bmapi_aflag(whichfork); |
| 5581 | if (!(iflags & BMV_IF_PREALLOC)) |
| 5582 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | |
| 5584 | /* |
| 5585 | * Allocate enough space to handle "subnex" maps at a time. |
| 5586 | */ |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5587 | error = ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | subnex = 16; |
Christoph Hellwig | ca35dcd | 2009-07-18 18:14:54 -0400 | [diff] [blame] | 5589 | map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5590 | if (!map) |
| 5591 | goto out_unlock_ilock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | |
| 5593 | bmv->bmv_entries = 0; |
| 5594 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5595 | if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 && |
| 5596 | (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) { |
| 5597 | error = 0; |
| 5598 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | } |
| 5600 | |
| 5601 | nexleft = nex; |
| 5602 | |
| 5603 | do { |
| 5604 | nmap = (nexleft > subnex) ? subnex : nexleft; |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 5605 | error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset), |
| 5606 | XFS_BB_TO_FSB(mp, bmv->bmv_length), |
| 5607 | map, &nmap, bmapi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | if (error) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5609 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | ASSERT(nmap <= subnex); |
| 5611 | |
| 5612 | for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) { |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5613 | out[cur_ext].bmv_oflags = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5614 | if (map[i].br_state == XFS_EXT_UNWRITTEN) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5615 | out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5616 | else if (map[i].br_startblock == DELAYSTARTBLOCK) |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5617 | out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC; |
| 5618 | out[cur_ext].bmv_offset = |
| 5619 | XFS_FSB_TO_BB(mp, map[i].br_startoff); |
| 5620 | out[cur_ext].bmv_length = |
| 5621 | XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
| 5622 | out[cur_ext].bmv_unused1 = 0; |
| 5623 | out[cur_ext].bmv_unused2 = 0; |
Eric Sandeen | 5af317c | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 5624 | ASSERT(((iflags & BMV_IF_DELALLOC) != 0) || |
| 5625 | (map[i].br_startblock != DELAYSTARTBLOCK)); |
Yingping Lu | 9af0a70 | 2005-11-02 15:09:54 +1100 | [diff] [blame] | 5626 | if (map[i].br_startblock == HOLESTARTBLOCK && |
Vlad Apostolov | 3bacbcd | 2007-08-16 15:20:25 +1000 | [diff] [blame] | 5627 | whichfork == XFS_ATTR_FORK) { |
| 5628 | /* came to the end of attribute fork */ |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5629 | out[cur_ext].bmv_oflags |= BMV_OF_LAST; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5630 | goto out_free_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | } |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5632 | |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5633 | if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext], |
| 5634 | prealloced, bmvend, |
| 5635 | map[i].br_startblock)) |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5636 | goto out_free_map; |
| 5637 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5638 | bmv->bmv_offset = |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5639 | out[cur_ext].bmv_offset + |
| 5640 | out[cur_ext].bmv_length; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5641 | bmv->bmv_length = |
| 5642 | max_t(__int64_t, 0, bmvend - bmv->bmv_offset); |
Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 5643 | |
| 5644 | /* |
| 5645 | * In case we don't want to return the hole, |
| 5646 | * don't increase cur_ext so that we can reuse |
| 5647 | * it in the next loop. |
| 5648 | */ |
| 5649 | if ((iflags & BMV_IF_NO_HOLES) && |
| 5650 | map[i].br_startblock == HOLESTARTBLOCK) { |
| 5651 | memset(&out[cur_ext], 0, sizeof(out[cur_ext])); |
| 5652 | continue; |
| 5653 | } |
| 5654 | |
| 5655 | nexleft--; |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5656 | bmv->bmv_entries++; |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5657 | cur_ext++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | } |
| 5659 | } while (nmap && nexleft && bmv->bmv_length); |
| 5660 | |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5661 | out_free_map: |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 5662 | kmem_free(map); |
Christoph Hellwig | 4be4a00 | 2009-04-29 10:50:48 -0400 | [diff] [blame] | 5663 | out_unlock_ilock: |
| 5664 | xfs_iunlock_map_shared(ip, lock); |
| 5665 | out_unlock_iolock: |
| 5666 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
Christoph Hellwig | 6321e3e | 2009-02-24 08:39:02 -0500 | [diff] [blame] | 5667 | |
| 5668 | for (i = 0; i < cur_ext; i++) { |
| 5669 | int full = 0; /* user array is full */ |
| 5670 | |
| 5671 | /* format results & advance arg */ |
| 5672 | error = formatter(&arg, &out[i], &full); |
| 5673 | if (error || full) |
| 5674 | break; |
| 5675 | } |
| 5676 | |
Dave Chinner | f074211 | 2012-03-07 04:50:22 +0000 | [diff] [blame] | 5677 | if (is_vmalloc_addr(out)) |
| 5678 | kmem_free_large(out); |
| 5679 | else |
| 5680 | kmem_free(out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | return error; |
| 5682 | } |
| 5683 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | #ifdef DEBUG |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5685 | STATIC struct xfs_buf * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | xfs_bmap_get_bp( |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5687 | struct xfs_btree_cur *cur, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | xfs_fsblock_t bno) |
| 5689 | { |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5690 | struct xfs_log_item_desc *lidp; |
| 5691 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | |
| 5693 | if (!cur) |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5694 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5696 | for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) { |
| 5697 | if (!cur->bc_bufs[i]) |
| 5698 | break; |
| 5699 | if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno) |
| 5700 | return cur->bc_bufs[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5703 | /* Chase down all the log items to see if the bp is there */ |
| 5704 | list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) { |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5705 | struct xfs_buf_log_item *bip; |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5706 | bip = (struct xfs_buf_log_item *)lidp->lid_item; |
| 5707 | if (bip->bli_item.li_type == XFS_LI_BUF && |
| 5708 | XFS_BUF_ADDR(bip->bli_buf) == bno) |
| 5709 | return bip->bli_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | } |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 5711 | |
Christoph Hellwig | ecd7f08 | 2010-07-22 12:52:08 +1000 | [diff] [blame] | 5712 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
Hannes Eder | 3180e66 | 2009-03-04 19:34:10 +0100 | [diff] [blame] | 5715 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | xfs_check_block( |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5717 | struct xfs_btree_block *block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | xfs_mount_t *mp, |
| 5719 | int root, |
| 5720 | short sz) |
| 5721 | { |
| 5722 | int i, j, dmxr; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5723 | __be64 *pp, *thispa; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | xfs_bmbt_key_t *prevp, *keyp; |
| 5725 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5726 | ASSERT(be16_to_cpu(block->bb_level) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | |
| 5728 | prevp = NULL; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5729 | for( i = 1; i <= xfs_btree_get_numrecs(block); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | dmxr = mp->m_bmap_dmxr[0]; |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5731 | keyp = XFS_BMBT_KEY_ADDR(mp, block, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | |
| 5733 | if (prevp) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5734 | ASSERT(be64_to_cpu(prevp->br_startoff) < |
| 5735 | be64_to_cpu(keyp->br_startoff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | } |
| 5737 | prevp = keyp; |
| 5738 | |
| 5739 | /* |
| 5740 | * Compare the block numbers to see if there are dups. |
| 5741 | */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5742 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5743 | pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5744 | else |
| 5745 | pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr); |
| 5746 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5747 | for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5748 | if (root) |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5749 | thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5750 | else |
| 5751 | thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5752 | if (*thispa == *pp) { |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5753 | xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5754 | __func__, j, i, |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5755 | (unsigned long long)be64_to_cpu(*thispa)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | panic("%s: ptrs are equal in node\n", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5757 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | } |
| 5759 | } |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | /* |
| 5764 | * Check that the extents for the inode ip are in the right order in all |
| 5765 | * btree leaves. |
| 5766 | */ |
| 5767 | |
| 5768 | STATIC void |
| 5769 | xfs_bmap_check_leaf_extents( |
| 5770 | xfs_btree_cur_t *cur, /* btree cursor or null */ |
| 5771 | xfs_inode_t *ip, /* incore inode pointer */ |
| 5772 | int whichfork) /* data or attr fork */ |
| 5773 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5774 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5776 | xfs_buf_t *bp; /* buffer for "block" */ |
| 5777 | int error; /* error return value */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5778 | xfs_extnum_t i=0, j; /* index into the extents list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | xfs_ifork_t *ifp; /* fork structure */ |
| 5780 | int level; /* btree level, for checking */ |
| 5781 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5782 | __be64 *pp; /* pointer to block address */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5783 | xfs_bmbt_rec_t *ep; /* pointer to current extent */ |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5784 | xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5785 | xfs_bmbt_rec_t *nextp; /* pointer to next extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | int bp_release = 0; |
| 5787 | |
| 5788 | if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) { |
| 5789 | return; |
| 5790 | } |
| 5791 | |
| 5792 | bno = NULLFSBLOCK; |
| 5793 | mp = ip->i_mount; |
| 5794 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5795 | block = ifp->if_broot; |
| 5796 | /* |
| 5797 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5798 | */ |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5799 | level = be16_to_cpu(block->bb_level); |
| 5800 | ASSERT(level > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5801 | xfs_check_block(block, mp, 1, ifp->if_broot_bytes); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5802 | 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] | 5803 | bno = be64_to_cpu(*pp); |
| 5804 | |
| 5805 | ASSERT(bno != NULLDFSBNO); |
| 5806 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5807 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
| 5808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | /* |
| 5810 | * Go down the tree until leaf level is reached, following the first |
| 5811 | * pointer (leftmost) at each level. |
| 5812 | */ |
| 5813 | while (level-- > 0) { |
| 5814 | /* See if buf is in cur first */ |
| 5815 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5816 | if (bp) { |
| 5817 | bp_release = 0; |
| 5818 | } else { |
| 5819 | bp_release = 1; |
| 5820 | } |
| 5821 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5822 | XFS_BMAP_BTREE_REF))) |
| 5823 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5824 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | XFS_WANT_CORRUPTED_GOTO( |
Christoph Hellwig | 4e8938f | 2008-10-30 17:14:43 +1100 | [diff] [blame] | 5826 | xfs_bmap_sanity_check(mp, bp, level), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | error0); |
| 5828 | if (level == 0) |
| 5829 | break; |
| 5830 | |
| 5831 | /* |
| 5832 | * Check this block for basic sanity (increasing keys and |
| 5833 | * no duplicate blocks). |
| 5834 | */ |
| 5835 | |
| 5836 | xfs_check_block(block, mp, 0, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5837 | 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] | 5838 | bno = be64_to_cpu(*pp); |
| 5839 | XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | if (bp_release) { |
| 5841 | bp_release = 0; |
| 5842 | xfs_trans_brelse(NULL, bp); |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | /* |
| 5847 | * Here with bp and block set to the leftmost leaf node in the tree. |
| 5848 | */ |
| 5849 | i = 0; |
| 5850 | |
| 5851 | /* |
| 5852 | * Loop over all leaf nodes checking that all extents are in the right order. |
| 5853 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5854 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5855 | xfs_fsblock_t nextbno; |
| 5856 | xfs_extnum_t num_recs; |
| 5857 | |
| 5858 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5859 | num_recs = xfs_btree_get_numrecs(block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | |
| 5861 | /* |
| 5862 | * Read-ahead the next leaf block, if any. |
| 5863 | */ |
| 5864 | |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5865 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | |
| 5867 | /* |
| 5868 | * Check all the extents to make sure they are OK. |
| 5869 | * If we had a previous block, the last entry should |
| 5870 | * conform with the first entry in this one. |
| 5871 | */ |
| 5872 | |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5873 | ep = XFS_BMBT_REC_ADDR(mp, block, 1); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5874 | if (i) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5875 | ASSERT(xfs_bmbt_disk_get_startoff(&last) + |
| 5876 | xfs_bmbt_disk_get_blockcount(&last) <= |
| 5877 | xfs_bmbt_disk_get_startoff(ep)); |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5878 | } |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5879 | for (j = 1; j < num_recs; j++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 5880 | nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1); |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 5881 | ASSERT(xfs_bmbt_disk_get_startoff(ep) + |
| 5882 | xfs_bmbt_disk_get_blockcount(ep) <= |
| 5883 | xfs_bmbt_disk_get_startoff(nextp)); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5884 | ep = nextp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | |
Lachlan McIlroy | 2abdb8c | 2008-03-27 18:01:14 +1100 | [diff] [blame] | 5887 | last = *ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | i += num_recs; |
| 5889 | if (bp_release) { |
| 5890 | bp_release = 0; |
| 5891 | xfs_trans_brelse(NULL, bp); |
| 5892 | } |
| 5893 | bno = nextbno; |
| 5894 | /* |
| 5895 | * If we've reached the end, stop. |
| 5896 | */ |
| 5897 | if (bno == NULLFSBLOCK) |
| 5898 | break; |
| 5899 | |
| 5900 | bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno)); |
| 5901 | if (bp) { |
| 5902 | bp_release = 0; |
| 5903 | } else { |
| 5904 | bp_release = 1; |
| 5905 | } |
| 5906 | if (!bp && (error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp, |
| 5907 | XFS_BMAP_BTREE_REF))) |
| 5908 | goto error_norelse; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5909 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | } |
| 5911 | if (bp_release) { |
| 5912 | bp_release = 0; |
| 5913 | xfs_trans_brelse(NULL, bp); |
| 5914 | } |
| 5915 | return; |
| 5916 | |
| 5917 | error0: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5918 | xfs_warn(mp, "%s: at error0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | if (bp_release) |
| 5920 | xfs_trans_brelse(NULL, bp); |
| 5921 | error_norelse: |
Dave Chinner | 0b932cc | 2011-03-07 10:08:35 +1100 | [diff] [blame] | 5922 | xfs_warn(mp, "%s: BAD after btree leaves for %d extents", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 5923 | __func__, i); |
| 5924 | panic("%s: CORRUPTED BTREE OR SOMETHING", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5925 | return; |
| 5926 | } |
| 5927 | #endif |
| 5928 | |
| 5929 | /* |
| 5930 | * Count fsblocks of the given fork. |
| 5931 | */ |
| 5932 | int /* error */ |
| 5933 | xfs_bmap_count_blocks( |
| 5934 | xfs_trans_t *tp, /* transaction pointer */ |
| 5935 | xfs_inode_t *ip, /* incore inode */ |
| 5936 | int whichfork, /* data or attr fork */ |
| 5937 | int *count) /* out: count of blocks */ |
| 5938 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5939 | struct xfs_btree_block *block; /* current btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | xfs_fsblock_t bno; /* block # of "block" */ |
| 5941 | xfs_ifork_t *ifp; /* fork structure */ |
| 5942 | int level; /* btree level, for checking */ |
| 5943 | xfs_mount_t *mp; /* file system mount structure */ |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5944 | __be64 *pp; /* pointer to block address */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | |
| 5946 | bno = NULLFSBLOCK; |
| 5947 | mp = ip->i_mount; |
| 5948 | ifp = XFS_IFORK_PTR(ip, whichfork); |
| 5949 | if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) { |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5950 | xfs_bmap_count_leaves(ifp, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t), |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 5952 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | return 0; |
| 5954 | } |
| 5955 | |
| 5956 | /* |
| 5957 | * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out. |
| 5958 | */ |
| 5959 | block = ifp->if_broot; |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 5960 | level = be16_to_cpu(block->bb_level); |
| 5961 | ASSERT(level > 0); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 5962 | 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] | 5963 | bno = be64_to_cpu(*pp); |
| 5964 | ASSERT(bno != NULLDFSBNO); |
| 5965 | ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount); |
| 5966 | ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5968 | 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] | 5969 | XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW, |
| 5970 | mp); |
| 5971 | return XFS_ERROR(EFSCORRUPTED); |
| 5972 | } |
| 5973 | |
| 5974 | return 0; |
| 5975 | } |
| 5976 | |
| 5977 | /* |
| 5978 | * Recursively walks each level of a btree |
| 5979 | * to count total fsblocks is use. |
| 5980 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 5981 | STATIC int /* error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | xfs_bmap_count_tree( |
| 5983 | xfs_mount_t *mp, /* file system mount point */ |
| 5984 | xfs_trans_t *tp, /* transaction pointer */ |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 5985 | xfs_ifork_t *ifp, /* inode fork pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | xfs_fsblock_t blockno, /* file system block number */ |
| 5987 | int levelin, /* level in btree */ |
| 5988 | int *count) /* Count of blocks */ |
| 5989 | { |
| 5990 | int error; |
| 5991 | xfs_buf_t *bp, *nbp; |
| 5992 | int level = levelin; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 5993 | __be64 *pp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | xfs_fsblock_t bno = blockno; |
| 5995 | xfs_fsblock_t nextbno; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 5996 | struct xfs_btree_block *block, *nextblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | int numrecs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | |
| 5999 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF))) |
| 6000 | return error; |
| 6001 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6002 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | |
| 6004 | if (--level) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 6005 | /* Not at node above leaves, count this level of nodes */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6006 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | while (nextbno != NULLFSBLOCK) { |
| 6008 | if ((error = xfs_btree_read_bufl(mp, tp, nextbno, |
| 6009 | 0, &nbp, XFS_BMAP_BTREE_REF))) |
| 6010 | return error; |
| 6011 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6012 | nextblock = XFS_BUF_TO_BLOCK(nbp); |
| 6013 | nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | xfs_trans_brelse(tp, nbp); |
| 6015 | } |
| 6016 | |
| 6017 | /* Dive to the next level */ |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6018 | 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] | 6019 | bno = be64_to_cpu(*pp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | if (unlikely((error = |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6021 | xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | xfs_trans_brelse(tp, bp); |
| 6023 | XFS_ERROR_REPORT("xfs_bmap_count_tree(1)", |
| 6024 | XFS_ERRLEVEL_LOW, mp); |
| 6025 | return XFS_ERROR(EFSCORRUPTED); |
| 6026 | } |
| 6027 | xfs_trans_brelse(tp, bp); |
| 6028 | } else { |
| 6029 | /* count all level 1 nodes and their leaves */ |
| 6030 | for (;;) { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6031 | nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 6032 | numrecs = be16_to_cpu(block->bb_numrecs); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6033 | xfs_bmap_disk_count_leaves(mp, block, numrecs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6034 | xfs_trans_brelse(tp, bp); |
| 6035 | if (nextbno == NULLFSBLOCK) |
| 6036 | break; |
| 6037 | bno = nextbno; |
| 6038 | if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, |
| 6039 | XFS_BMAP_BTREE_REF))) |
| 6040 | return error; |
| 6041 | *count += 1; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6042 | block = XFS_BUF_TO_BLOCK(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | } |
| 6044 | } |
| 6045 | return 0; |
| 6046 | } |
| 6047 | |
| 6048 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6049 | * Count leaf blocks given a range of extent records. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6051 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | xfs_bmap_count_leaves( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6053 | xfs_ifork_t *ifp, |
| 6054 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | int numrecs, |
| 6056 | int *count) |
| 6057 | { |
| 6058 | int b; |
| 6059 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6060 | for (b = 0; b < numrecs; b++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 6061 | 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] | 6062 | *count += xfs_bmbt_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6063 | } |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6067 | * Count leaf blocks given a range of extent records originally |
| 6068 | * in btree format. |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6069 | */ |
Ruben Porras | c94312d | 2008-08-13 16:52:25 +1000 | [diff] [blame] | 6070 | STATIC void |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6071 | xfs_bmap_disk_count_leaves( |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6072 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 6073 | struct xfs_btree_block *block, |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6074 | int numrecs, |
| 6075 | int *count) |
| 6076 | { |
| 6077 | int b; |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6078 | xfs_bmbt_rec_t *frp; |
Yingping Lu | 91e1108 | 2005-11-02 15:10:24 +1100 | [diff] [blame] | 6079 | |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6080 | for (b = 1; b <= numrecs; b++) { |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 6081 | frp = XFS_BMBT_REC_ADDR(mp, block, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | *count += xfs_bmbt_disk_get_blockcount(frp); |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 6083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | } |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6085 | |
| 6086 | /* |
| 6087 | * dead simple method of punching delalyed allocation blocks from a range in |
| 6088 | * the inode. Walks a block at a time so will be slow, but is only executed in |
| 6089 | * rare error cases so the overhead is not critical. This will alays punch out |
| 6090 | * both the start and end blocks, even if the ranges only partially overlap |
| 6091 | * them, so it is up to the caller to ensure that partial blocks are not |
| 6092 | * passed in. |
| 6093 | */ |
| 6094 | int |
| 6095 | xfs_bmap_punch_delalloc_range( |
| 6096 | struct xfs_inode *ip, |
| 6097 | xfs_fileoff_t start_fsb, |
| 6098 | xfs_fileoff_t length) |
| 6099 | { |
| 6100 | xfs_fileoff_t remaining = length; |
| 6101 | int error = 0; |
| 6102 | |
| 6103 | ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); |
| 6104 | |
| 6105 | do { |
| 6106 | int done; |
| 6107 | xfs_bmbt_irec_t imap; |
| 6108 | int nimaps = 1; |
| 6109 | xfs_fsblock_t firstblock; |
| 6110 | xfs_bmap_free_t flist; |
| 6111 | |
| 6112 | /* |
| 6113 | * Map the range first and check that it is a delalloc extent |
| 6114 | * before trying to unmap the range. Otherwise we will be |
| 6115 | * trying to remove a real extent (which requires a |
| 6116 | * transaction) or a hole, which is probably a bad idea... |
| 6117 | */ |
Dave Chinner | 5c8ed20 | 2011-09-18 20:40:45 +0000 | [diff] [blame] | 6118 | error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps, |
| 6119 | XFS_BMAPI_ENTIRE); |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6120 | |
| 6121 | if (error) { |
| 6122 | /* something screwed, just bail */ |
| 6123 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
Dave Chinner | 5348778 | 2011-03-07 10:05:35 +1100 | [diff] [blame] | 6124 | xfs_alert(ip->i_mount, |
Dave Chinner | c726de4 | 2010-11-30 15:14:39 +1100 | [diff] [blame] | 6125 | "Failed delalloc mapping lookup ino %lld fsb %lld.", |
| 6126 | ip->i_ino, start_fsb); |
| 6127 | } |
| 6128 | break; |
| 6129 | } |
| 6130 | if (!nimaps) { |
| 6131 | /* nothing there */ |
| 6132 | goto next_block; |
| 6133 | } |
| 6134 | if (imap.br_startblock != DELAYSTARTBLOCK) { |
| 6135 | /* been converted, ignore */ |
| 6136 | goto next_block; |
| 6137 | } |
| 6138 | WARN_ON(imap.br_blockcount == 0); |
| 6139 | |
| 6140 | /* |
| 6141 | * Note: while we initialise the firstblock/flist pair, they |
| 6142 | * should never be used because blocks should never be |
| 6143 | * allocated or freed for a delalloc extent and hence we need |
| 6144 | * don't cancel or finish them after the xfs_bunmapi() call. |
| 6145 | */ |
| 6146 | xfs_bmap_init(&flist, &firstblock); |
| 6147 | error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock, |
| 6148 | &flist, &done); |
| 6149 | if (error) |
| 6150 | break; |
| 6151 | |
| 6152 | ASSERT(!flist.xbf_count && !flist.xbf_first); |
| 6153 | next_block: |
| 6154 | start_fsb++; |
| 6155 | remaining--; |
| 6156 | } while(remaining > 0); |
| 6157 | |
| 6158 | return error; |
| 6159 | } |