Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
| 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" |
| 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
| 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_bmap_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 28 | #include "xfs_alloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_ialloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_dinode.h" |
| 31 | #include "xfs_inode.h" |
| 32 | #include "xfs_inode_item.h" |
| 33 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_itable.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_bmap.h" |
| 37 | #include "xfs_error.h" |
| 38 | #include "xfs_quota.h" |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * Determine the extent state. |
| 42 | */ |
| 43 | /* ARGSUSED */ |
| 44 | STATIC xfs_exntst_t |
| 45 | xfs_extent_state( |
| 46 | xfs_filblks_t blks, |
| 47 | int extent_flag) |
| 48 | { |
| 49 | if (extent_flag) { |
| 50 | ASSERT(blks != 0); /* saved for DMIG */ |
| 51 | return XFS_EXT_UNWRITTEN; |
| 52 | } |
| 53 | return XFS_EXT_NORM; |
| 54 | } |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Convert on-disk form of btree root to in-memory form. |
| 58 | */ |
| 59 | void |
| 60 | xfs_bmdr_to_bmbt( |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 61 | struct xfs_mount *mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | xfs_bmdr_block_t *dblock, |
| 63 | int dblocklen, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 64 | struct xfs_btree_block *rblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int rblocklen) |
| 66 | { |
| 67 | int dmxr; |
| 68 | xfs_bmbt_key_t *fkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 69 | __be64 *fpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | xfs_bmbt_key_t *tkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 71 | __be64 *tpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 73 | rblock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC); |
| 74 | rblock->bb_level = dblock->bb_level; |
| 75 | ASSERT(be16_to_cpu(rblock->bb_level) > 0); |
| 76 | rblock->bb_numrecs = dblock->bb_numrecs; |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 77 | rblock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO); |
| 78 | rblock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 79 | dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 80 | fkp = XFS_BMDR_KEY_ADDR(dblock, 1); |
| 81 | tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
| 82 | fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 83 | tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 84 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 86 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Convert a compressed bmap extent record to an uncompressed form. |
| 91 | * This code must be in sync with the routines xfs_bmbt_get_startoff, |
| 92 | * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state. |
| 93 | */ |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 94 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | __xfs_bmbt_get_all( |
| 96 | __uint64_t l0, |
| 97 | __uint64_t l1, |
| 98 | xfs_bmbt_irec_t *s) |
| 99 | { |
| 100 | int ext_flag; |
| 101 | xfs_exntst_t st; |
| 102 | |
| 103 | ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN)); |
| 104 | s->br_startoff = ((xfs_fileoff_t)l0 & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 105 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 107 | s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | (((xfs_fsblock_t)l1) >> 21); |
| 109 | #else |
| 110 | #ifdef DEBUG |
| 111 | { |
| 112 | xfs_dfsbno_t b; |
| 113 | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 114 | b = (((xfs_dfsbno_t)l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | (((xfs_dfsbno_t)l1) >> 21); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 116 | ASSERT((b >> 32) == 0 || isnulldstartblock(b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | s->br_startblock = (xfs_fsblock_t)b; |
| 118 | } |
| 119 | #else /* !DEBUG */ |
| 120 | s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21); |
| 121 | #endif /* DEBUG */ |
| 122 | #endif /* XFS_BIG_BLKNOS */ |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 123 | s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* This is xfs_extent_state() in-line */ |
| 125 | if (ext_flag) { |
| 126 | ASSERT(s->br_blockcount != 0); /* saved for DMIG */ |
| 127 | st = XFS_EXT_UNWRITTEN; |
| 128 | } else |
| 129 | st = XFS_EXT_NORM; |
| 130 | s->br_state = st; |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | xfs_bmbt_get_all( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 135 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | xfs_bmbt_irec_t *s) |
| 137 | { |
| 138 | __xfs_bmbt_get_all(r->l0, r->l1, s); |
| 139 | } |
| 140 | |
| 141 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | * Extract the blockcount field from an in memory bmap extent record. |
| 143 | */ |
| 144 | xfs_filblks_t |
| 145 | xfs_bmbt_get_blockcount( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 146 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 148 | return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Extract the startblock field from an in memory bmap extent record. |
| 153 | */ |
| 154 | xfs_fsblock_t |
| 155 | xfs_bmbt_get_startblock( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 156 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 159 | return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | (((xfs_fsblock_t)r->l1) >> 21); |
| 161 | #else |
| 162 | #ifdef DEBUG |
| 163 | xfs_dfsbno_t b; |
| 164 | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 165 | b = (((xfs_dfsbno_t)r->l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | (((xfs_dfsbno_t)r->l1) >> 21); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 167 | ASSERT((b >> 32) == 0 || isnulldstartblock(b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return (xfs_fsblock_t)b; |
| 169 | #else /* !DEBUG */ |
| 170 | return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21); |
| 171 | #endif /* DEBUG */ |
| 172 | #endif /* XFS_BIG_BLKNOS */ |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Extract the startoff field from an in memory bmap extent record. |
| 177 | */ |
| 178 | xfs_fileoff_t |
| 179 | xfs_bmbt_get_startoff( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 180 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | return ((xfs_fileoff_t)r->l0 & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 183 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | xfs_exntst_t |
| 187 | xfs_bmbt_get_state( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 188 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
| 190 | int ext_flag; |
| 191 | |
| 192 | ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN)); |
| 193 | return xfs_extent_state(xfs_bmbt_get_blockcount(r), |
| 194 | ext_flag); |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* |
| 198 | * Extract the blockcount field from an on disk bmap extent record. |
| 199 | */ |
| 200 | xfs_filblks_t |
| 201 | xfs_bmbt_disk_get_blockcount( |
| 202 | xfs_bmbt_rec_t *r) |
| 203 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 204 | return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | * Extract the startoff field from a disk format bmap extent record. |
| 209 | */ |
| 210 | xfs_fileoff_t |
| 211 | xfs_bmbt_disk_get_startoff( |
| 212 | xfs_bmbt_rec_t *r) |
| 213 | { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 214 | return ((xfs_fileoff_t)be64_to_cpu(r->l0) & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 215 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 220 | * Set all the fields in a bmap extent record from the arguments. |
| 221 | */ |
| 222 | void |
| 223 | xfs_bmbt_set_allf( |
| 224 | xfs_bmbt_rec_host_t *r, |
| 225 | xfs_fileoff_t startoff, |
| 226 | xfs_fsblock_t startblock, |
| 227 | xfs_filblks_t blockcount, |
| 228 | xfs_exntst_t state) |
| 229 | { |
| 230 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
| 231 | |
| 232 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 233 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
| 234 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 235 | |
| 236 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 237 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 238 | |
| 239 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 240 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
| 241 | ((xfs_bmbt_rec_base_t)startblock >> 43); |
| 242 | r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 243 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 244 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 245 | #else /* !XFS_BIG_BLKNOS */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 246 | if (isnullstartblock(startblock)) { |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 247 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 248 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 249 | (xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
| 250 | r->l1 = xfs_mask64hi(11) | |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 251 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 252 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 253 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 254 | } else { |
| 255 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 256 | ((xfs_bmbt_rec_base_t)startoff << 9); |
| 257 | r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 258 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 259 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 260 | } |
| 261 | #endif /* XFS_BIG_BLKNOS */ |
| 262 | } |
| 263 | |
| 264 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | * Set all the fields in a bmap extent record from the uncompressed form. |
| 266 | */ |
| 267 | void |
| 268 | xfs_bmbt_set_all( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 269 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | xfs_bmbt_irec_t *s) |
| 271 | { |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 272 | xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock, |
| 273 | s->br_blockcount, s->br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 278 | * Set all the fields in a disk format bmap extent record from the arguments. |
| 279 | */ |
| 280 | void |
| 281 | xfs_bmbt_disk_set_allf( |
| 282 | xfs_bmbt_rec_t *r, |
| 283 | xfs_fileoff_t startoff, |
| 284 | xfs_fsblock_t startblock, |
| 285 | xfs_filblks_t blockcount, |
| 286 | xfs_exntst_t state) |
| 287 | { |
| 288 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
| 289 | |
| 290 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 291 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
| 292 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 293 | |
| 294 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 295 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 296 | |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 297 | r->l0 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 298 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 299 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
| 300 | ((xfs_bmbt_rec_base_t)startblock >> 43)); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 301 | r->l1 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 302 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 303 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 304 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 305 | #else /* !XFS_BIG_BLKNOS */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 306 | if (isnullstartblock(startblock)) { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 307 | r->l0 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 308 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 309 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 310 | (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
| 311 | r->l1 = cpu_to_be64(xfs_mask64hi(11) | |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 312 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 313 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 314 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 315 | } else { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 316 | r->l0 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 317 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 318 | ((xfs_bmbt_rec_base_t)startoff << 9)); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 319 | r->l1 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 320 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 321 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 322 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 323 | } |
| 324 | #endif /* XFS_BIG_BLKNOS */ |
| 325 | } |
| 326 | |
| 327 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | * Set all the fields in a bmap extent record from the uncompressed form. |
| 329 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 330 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | xfs_bmbt_disk_set_all( |
| 332 | xfs_bmbt_rec_t *r, |
| 333 | xfs_bmbt_irec_t *s) |
| 334 | { |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 335 | xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock, |
| 336 | s->br_blockcount, s->br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Set the blockcount field in a bmap extent record. |
| 341 | */ |
| 342 | void |
| 343 | xfs_bmbt_set_blockcount( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 344 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | xfs_filblks_t v) |
| 346 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 347 | ASSERT((v & xfs_mask64hi(43)) == 0); |
| 348 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64hi(43)) | |
| 349 | (xfs_bmbt_rec_base_t)(v & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Set the startblock field in a bmap extent record. |
| 354 | */ |
| 355 | void |
| 356 | xfs_bmbt_set_startblock( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 357 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | xfs_fsblock_t v) |
| 359 | { |
| 360 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 361 | ASSERT((v & xfs_mask64hi(12)) == 0); |
| 362 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | (xfs_bmbt_rec_base_t)(v >> 43); |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 364 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | (xfs_bmbt_rec_base_t)(v << 21); |
| 366 | #else /* !XFS_BIG_BLKNOS */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 367 | if (isnullstartblock(v)) { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 368 | r->l0 |= (xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
| 369 | r->l1 = (xfs_bmbt_rec_base_t)xfs_mask64hi(11) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | ((xfs_bmbt_rec_base_t)v << 21) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 371 | (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } else { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 373 | r->l0 &= ~(xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | r->l1 = ((xfs_bmbt_rec_base_t)v << 21) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 375 | (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | #endif /* XFS_BIG_BLKNOS */ |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Set the startoff field in a bmap extent record. |
| 382 | */ |
| 383 | void |
| 384 | xfs_bmbt_set_startoff( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 385 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | xfs_fileoff_t v) |
| 387 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 388 | ASSERT((v & xfs_mask64hi(9)) == 0); |
| 389 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | ((xfs_bmbt_rec_base_t)v << 9) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 391 | (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Set the extent state field in a bmap extent record. |
| 396 | */ |
| 397 | void |
| 398 | xfs_bmbt_set_state( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 399 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | xfs_exntst_t v) |
| 401 | { |
| 402 | ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN); |
| 403 | if (v == XFS_EXT_NORM) |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 404 | r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | else |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 406 | r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Convert in-memory form of btree root to on-disk form. |
| 411 | */ |
| 412 | void |
| 413 | xfs_bmbt_to_bmdr( |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 414 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 415 | struct xfs_btree_block *rblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | int rblocklen, |
| 417 | xfs_bmdr_block_t *dblock, |
| 418 | int dblocklen) |
| 419 | { |
| 420 | int dmxr; |
| 421 | xfs_bmbt_key_t *fkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 422 | __be64 *fpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | xfs_bmbt_key_t *tkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 424 | __be64 *tpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 426 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC)); |
| 427 | ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO)); |
| 428 | ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO)); |
| 429 | ASSERT(rblock->bb_level != 0); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 430 | dblock->bb_level = rblock->bb_level; |
| 431 | dblock->bb_numrecs = rblock->bb_numrecs; |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 432 | dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 433 | fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
| 434 | tkp = XFS_BMDR_KEY_ADDR(dblock, 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 435 | fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 436 | tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 437 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 439 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 443 | * Check extent records, which have just been read, for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | * any bit in the extent flag field. ASSERT on debug |
| 445 | * kernels, as this condition should not occur. |
| 446 | * Return an error condition (1) if any flags found, |
| 447 | * otherwise return 0. |
| 448 | */ |
| 449 | |
| 450 | int |
| 451 | xfs_check_nostate_extents( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 452 | xfs_ifork_t *ifp, |
| 453 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | xfs_extnum_t num) |
| 455 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 456 | for (; num > 0; num--, idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 457 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | if ((ep->l0 >> |
| 459 | (64 - BMBT_EXNTFLAG_BITLEN)) != 0) { |
| 460 | ASSERT(0); |
| 461 | return 1; |
| 462 | } |
| 463 | } |
| 464 | return 0; |
| 465 | } |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 466 | |
| 467 | |
| 468 | STATIC struct xfs_btree_cur * |
| 469 | xfs_bmbt_dup_cursor( |
| 470 | struct xfs_btree_cur *cur) |
| 471 | { |
| 472 | struct xfs_btree_cur *new; |
| 473 | |
| 474 | new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp, |
| 475 | cur->bc_private.b.ip, cur->bc_private.b.whichfork); |
| 476 | |
| 477 | /* |
| 478 | * Copy the firstblock, flist, and flags values, |
| 479 | * since init cursor doesn't get them. |
| 480 | */ |
| 481 | new->bc_private.b.firstblock = cur->bc_private.b.firstblock; |
| 482 | new->bc_private.b.flist = cur->bc_private.b.flist; |
| 483 | new->bc_private.b.flags = cur->bc_private.b.flags; |
| 484 | |
| 485 | return new; |
| 486 | } |
| 487 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 488 | STATIC void |
| 489 | xfs_bmbt_update_cursor( |
| 490 | struct xfs_btree_cur *src, |
| 491 | struct xfs_btree_cur *dst) |
| 492 | { |
| 493 | ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) || |
| 494 | (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME)); |
| 495 | ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist); |
| 496 | |
| 497 | dst->bc_private.b.allocated += src->bc_private.b.allocated; |
| 498 | dst->bc_private.b.firstblock = src->bc_private.b.firstblock; |
| 499 | |
| 500 | src->bc_private.b.allocated = 0; |
| 501 | } |
| 502 | |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 503 | STATIC int |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 504 | xfs_bmbt_alloc_block( |
| 505 | struct xfs_btree_cur *cur, |
| 506 | union xfs_btree_ptr *start, |
| 507 | union xfs_btree_ptr *new, |
| 508 | int length, |
| 509 | int *stat) |
| 510 | { |
| 511 | xfs_alloc_arg_t args; /* block allocation args */ |
| 512 | int error; /* error return value */ |
| 513 | |
| 514 | memset(&args, 0, sizeof(args)); |
| 515 | args.tp = cur->bc_tp; |
| 516 | args.mp = cur->bc_mp; |
| 517 | args.fsbno = cur->bc_private.b.firstblock; |
| 518 | args.firstblock = args.fsbno; |
| 519 | |
| 520 | if (args.fsbno == NULLFSBLOCK) { |
| 521 | args.fsbno = be64_to_cpu(start->l); |
| 522 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 523 | /* |
| 524 | * Make sure there is sufficient room left in the AG to |
| 525 | * complete a full tree split for an extent insert. If |
| 526 | * we are converting the middle part of an extent then |
| 527 | * we may need space for two tree splits. |
| 528 | * |
| 529 | * We are relying on the caller to make the correct block |
| 530 | * reservation for this operation to succeed. If the |
| 531 | * reservation amount is insufficient then we may fail a |
| 532 | * block allocation here and corrupt the filesystem. |
| 533 | */ |
| 534 | args.minleft = xfs_trans_get_block_res(args.tp); |
| 535 | } else if (cur->bc_private.b.flist->xbf_low) { |
| 536 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 537 | } else { |
| 538 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 539 | } |
| 540 | |
| 541 | args.minlen = args.maxlen = args.prod = 1; |
| 542 | args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL; |
| 543 | if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) { |
| 544 | error = XFS_ERROR(ENOSPC); |
| 545 | goto error0; |
| 546 | } |
| 547 | error = xfs_alloc_vextent(&args); |
| 548 | if (error) |
| 549 | goto error0; |
| 550 | |
| 551 | if (args.fsbno == NULLFSBLOCK && args.minleft) { |
| 552 | /* |
| 553 | * Could not find an AG with enough free space to satisfy |
| 554 | * a full btree split. Try again without minleft and if |
| 555 | * successful activate the lowspace algorithm. |
| 556 | */ |
| 557 | args.fsbno = 0; |
| 558 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 559 | args.minleft = 0; |
| 560 | error = xfs_alloc_vextent(&args); |
| 561 | if (error) |
| 562 | goto error0; |
| 563 | cur->bc_private.b.flist->xbf_low = 1; |
| 564 | } |
| 565 | if (args.fsbno == NULLFSBLOCK) { |
| 566 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 567 | *stat = 0; |
| 568 | return 0; |
| 569 | } |
| 570 | ASSERT(args.len == 1); |
| 571 | cur->bc_private.b.firstblock = args.fsbno; |
| 572 | cur->bc_private.b.allocated++; |
| 573 | cur->bc_private.b.ip->i_d.di_nblocks++; |
| 574 | xfs_trans_log_inode(args.tp, cur->bc_private.b.ip, XFS_ILOG_CORE); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 575 | xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 576 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 577 | |
| 578 | new->l = cpu_to_be64(args.fsbno); |
| 579 | |
| 580 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 581 | *stat = 1; |
| 582 | return 0; |
| 583 | |
| 584 | error0: |
| 585 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 586 | return error; |
| 587 | } |
| 588 | |
| 589 | STATIC int |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 590 | xfs_bmbt_free_block( |
| 591 | struct xfs_btree_cur *cur, |
| 592 | struct xfs_buf *bp) |
| 593 | { |
| 594 | struct xfs_mount *mp = cur->bc_mp; |
| 595 | struct xfs_inode *ip = cur->bc_private.b.ip; |
| 596 | struct xfs_trans *tp = cur->bc_tp; |
| 597 | xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp)); |
| 598 | |
| 599 | xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp); |
| 600 | ip->i_d.di_nblocks--; |
| 601 | |
| 602 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 603 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 604 | xfs_trans_binval(tp, bp); |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | STATIC int |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 609 | xfs_bmbt_get_minrecs( |
| 610 | struct xfs_btree_cur *cur, |
| 611 | int level) |
| 612 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 613 | if (level == cur->bc_nlevels - 1) { |
| 614 | struct xfs_ifork *ifp; |
| 615 | |
| 616 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, |
| 617 | cur->bc_private.b.whichfork); |
| 618 | |
| 619 | return xfs_bmbt_maxrecs(cur->bc_mp, |
| 620 | ifp->if_broot_bytes, level == 0) / 2; |
| 621 | } |
| 622 | |
| 623 | return cur->bc_mp->m_bmap_dmnr[level != 0]; |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 624 | } |
| 625 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 626 | int |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 627 | xfs_bmbt_get_maxrecs( |
| 628 | struct xfs_btree_cur *cur, |
| 629 | int level) |
| 630 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 631 | if (level == cur->bc_nlevels - 1) { |
| 632 | struct xfs_ifork *ifp; |
| 633 | |
| 634 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, |
| 635 | cur->bc_private.b.whichfork); |
| 636 | |
| 637 | return xfs_bmbt_maxrecs(cur->bc_mp, |
| 638 | ifp->if_broot_bytes, level == 0); |
| 639 | } |
| 640 | |
| 641 | return cur->bc_mp->m_bmap_dmxr[level != 0]; |
| 642 | |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 643 | } |
| 644 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 645 | /* |
| 646 | * Get the maximum records we could store in the on-disk format. |
| 647 | * |
| 648 | * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but |
| 649 | * for the root node this checks the available space in the dinode fork |
| 650 | * so that we can resize the in-memory buffer to match it. After a |
| 651 | * resize to the maximum size this function returns the same value |
| 652 | * as xfs_bmbt_get_maxrecs for the root node, too. |
| 653 | */ |
| 654 | STATIC int |
| 655 | xfs_bmbt_get_dmaxrecs( |
| 656 | struct xfs_btree_cur *cur, |
| 657 | int level) |
| 658 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 659 | if (level != cur->bc_nlevels - 1) |
| 660 | return cur->bc_mp->m_bmap_dmxr[level != 0]; |
| 661 | return xfs_bmdr_maxrecs(cur->bc_mp, cur->bc_private.b.forksize, |
| 662 | level == 0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 663 | } |
| 664 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 665 | STATIC void |
| 666 | xfs_bmbt_init_key_from_rec( |
| 667 | union xfs_btree_key *key, |
| 668 | union xfs_btree_rec *rec) |
| 669 | { |
| 670 | key->bmbt.br_startoff = |
| 671 | cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt)); |
| 672 | } |
| 673 | |
| 674 | STATIC void |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 675 | xfs_bmbt_init_rec_from_key( |
| 676 | union xfs_btree_key *key, |
| 677 | union xfs_btree_rec *rec) |
| 678 | { |
| 679 | ASSERT(key->bmbt.br_startoff != 0); |
| 680 | |
| 681 | xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff), |
| 682 | 0, 0, XFS_EXT_NORM); |
| 683 | } |
| 684 | |
| 685 | STATIC void |
| 686 | xfs_bmbt_init_rec_from_cur( |
| 687 | struct xfs_btree_cur *cur, |
| 688 | union xfs_btree_rec *rec) |
| 689 | { |
| 690 | xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b); |
| 691 | } |
| 692 | |
| 693 | STATIC void |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 694 | xfs_bmbt_init_ptr_from_cur( |
| 695 | struct xfs_btree_cur *cur, |
| 696 | union xfs_btree_ptr *ptr) |
| 697 | { |
| 698 | ptr->l = 0; |
| 699 | } |
| 700 | |
| 701 | STATIC __int64_t |
| 702 | xfs_bmbt_key_diff( |
| 703 | struct xfs_btree_cur *cur, |
| 704 | union xfs_btree_key *key) |
| 705 | { |
| 706 | return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) - |
| 707 | cur->bc_rec.b.br_startoff; |
| 708 | } |
| 709 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 710 | #ifdef DEBUG |
| 711 | STATIC int |
| 712 | xfs_bmbt_keys_inorder( |
| 713 | struct xfs_btree_cur *cur, |
| 714 | union xfs_btree_key *k1, |
| 715 | union xfs_btree_key *k2) |
| 716 | { |
| 717 | return be64_to_cpu(k1->bmbt.br_startoff) < |
| 718 | be64_to_cpu(k2->bmbt.br_startoff); |
| 719 | } |
| 720 | |
| 721 | STATIC int |
| 722 | xfs_bmbt_recs_inorder( |
| 723 | struct xfs_btree_cur *cur, |
| 724 | union xfs_btree_rec *r1, |
| 725 | union xfs_btree_rec *r2) |
| 726 | { |
| 727 | return xfs_bmbt_disk_get_startoff(&r1->bmbt) + |
| 728 | xfs_bmbt_disk_get_blockcount(&r1->bmbt) <= |
| 729 | xfs_bmbt_disk_get_startoff(&r2->bmbt); |
| 730 | } |
| 731 | #endif /* DEBUG */ |
| 732 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 733 | static const struct xfs_btree_ops xfs_bmbt_ops = { |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 734 | .rec_len = sizeof(xfs_bmbt_rec_t), |
| 735 | .key_len = sizeof(xfs_bmbt_key_t), |
| 736 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 737 | .dup_cursor = xfs_bmbt_dup_cursor, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 738 | .update_cursor = xfs_bmbt_update_cursor, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 739 | .alloc_block = xfs_bmbt_alloc_block, |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 740 | .free_block = xfs_bmbt_free_block, |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 741 | .get_maxrecs = xfs_bmbt_get_maxrecs, |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 742 | .get_minrecs = xfs_bmbt_get_minrecs, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 743 | .get_dmaxrecs = xfs_bmbt_get_dmaxrecs, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 744 | .init_key_from_rec = xfs_bmbt_init_key_from_rec, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 745 | .init_rec_from_key = xfs_bmbt_init_rec_from_key, |
| 746 | .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 747 | .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur, |
| 748 | .key_diff = xfs_bmbt_key_diff, |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 749 | #ifdef DEBUG |
| 750 | .keys_inorder = xfs_bmbt_keys_inorder, |
| 751 | .recs_inorder = xfs_bmbt_recs_inorder, |
| 752 | #endif |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | /* |
| 756 | * Allocate a new bmap btree cursor. |
| 757 | */ |
| 758 | struct xfs_btree_cur * /* new bmap btree cursor */ |
| 759 | xfs_bmbt_init_cursor( |
| 760 | struct xfs_mount *mp, /* file system mount point */ |
| 761 | struct xfs_trans *tp, /* transaction pointer */ |
| 762 | struct xfs_inode *ip, /* inode owning the btree */ |
| 763 | int whichfork) /* data or attr fork */ |
| 764 | { |
| 765 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 766 | struct xfs_btree_cur *cur; |
| 767 | |
| 768 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP); |
| 769 | |
| 770 | cur->bc_tp = tp; |
| 771 | cur->bc_mp = mp; |
| 772 | cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
| 773 | cur->bc_btnum = XFS_BTNUM_BMAP; |
| 774 | cur->bc_blocklog = mp->m_sb.sb_blocklog; |
| 775 | |
| 776 | cur->bc_ops = &xfs_bmbt_ops; |
Christoph Hellwig | e99ab90 | 2008-10-30 16:54:33 +1100 | [diff] [blame] | 777 | cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 778 | |
| 779 | cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork); |
| 780 | cur->bc_private.b.ip = ip; |
| 781 | cur->bc_private.b.firstblock = NULLFSBLOCK; |
| 782 | cur->bc_private.b.flist = NULL; |
| 783 | cur->bc_private.b.allocated = 0; |
| 784 | cur->bc_private.b.flags = 0; |
| 785 | cur->bc_private.b.whichfork = whichfork; |
| 786 | |
| 787 | return cur; |
| 788 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * Calculate number of records in a bmap btree block. |
| 792 | */ |
| 793 | int |
| 794 | xfs_bmbt_maxrecs( |
| 795 | struct xfs_mount *mp, |
| 796 | int blocklen, |
| 797 | int leaf) |
| 798 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 799 | blocklen -= XFS_BMBT_BLOCK_LEN(mp); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 800 | |
| 801 | if (leaf) |
| 802 | return blocklen / sizeof(xfs_bmbt_rec_t); |
| 803 | return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t)); |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Calculate number of records in a bmap btree inode root. |
| 808 | */ |
| 809 | int |
| 810 | xfs_bmdr_maxrecs( |
| 811 | struct xfs_mount *mp, |
| 812 | int blocklen, |
| 813 | int leaf) |
| 814 | { |
| 815 | blocklen -= sizeof(xfs_bmdr_block_t); |
| 816 | |
| 817 | if (leaf) |
| 818 | return blocklen / sizeof(xfs_bmdr_rec_t); |
| 819 | return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t)); |
| 820 | } |