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" |
Dave Chinner | 6ca1c90 | 2013-08-12 20:49:26 +1000 | [diff] [blame] | 20 | #include "xfs_format.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" |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 39 | #include "xfs_trace.h" |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 40 | #include "xfs_cksum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * Determine the extent state. |
| 44 | */ |
| 45 | /* ARGSUSED */ |
| 46 | STATIC xfs_exntst_t |
| 47 | xfs_extent_state( |
| 48 | xfs_filblks_t blks, |
| 49 | int extent_flag) |
| 50 | { |
| 51 | if (extent_flag) { |
| 52 | ASSERT(blks != 0); /* saved for DMIG */ |
| 53 | return XFS_EXT_UNWRITTEN; |
| 54 | } |
| 55 | return XFS_EXT_NORM; |
| 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Convert on-disk form of btree root to in-memory form. |
| 60 | */ |
| 61 | void |
| 62 | xfs_bmdr_to_bmbt( |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 63 | struct xfs_inode *ip, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | xfs_bmdr_block_t *dblock, |
| 65 | int dblocklen, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 66 | struct xfs_btree_block *rblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | int rblocklen) |
| 68 | { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 69 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | int dmxr; |
| 71 | xfs_bmbt_key_t *fkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 72 | __be64 *fpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | xfs_bmbt_key_t *tkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 74 | __be64 *tpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 76 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 77 | xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL, |
| 78 | XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino, |
| 79 | XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS); |
| 80 | else |
| 81 | xfs_btree_init_block_int(mp, rblock, XFS_BUF_DADDR_NULL, |
| 82 | XFS_BMAP_MAGIC, 0, 0, ip->i_ino, |
| 83 | XFS_BTREE_LONG_PTRS); |
| 84 | |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 85 | rblock->bb_level = dblock->bb_level; |
| 86 | ASSERT(be16_to_cpu(rblock->bb_level) > 0); |
| 87 | rblock->bb_numrecs = dblock->bb_numrecs; |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 88 | dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 89 | fkp = XFS_BMDR_KEY_ADDR(dblock, 1); |
| 90 | tkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
| 91 | fpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 92 | tpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 93 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 95 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * Convert a compressed bmap extent record to an uncompressed form. |
| 100 | * This code must be in sync with the routines xfs_bmbt_get_startoff, |
| 101 | * xfs_bmbt_get_startblock, xfs_bmbt_get_blockcount and xfs_bmbt_get_state. |
| 102 | */ |
Christoph Hellwig | b8f82a4 | 2009-11-14 16:17:22 +0000 | [diff] [blame] | 103 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | __xfs_bmbt_get_all( |
| 105 | __uint64_t l0, |
| 106 | __uint64_t l1, |
| 107 | xfs_bmbt_irec_t *s) |
| 108 | { |
| 109 | int ext_flag; |
| 110 | xfs_exntst_t st; |
| 111 | |
| 112 | ext_flag = (int)(l0 >> (64 - BMBT_EXNTFLAG_BITLEN)); |
| 113 | s->br_startoff = ((xfs_fileoff_t)l0 & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 114 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 116 | s->br_startblock = (((xfs_fsblock_t)l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | (((xfs_fsblock_t)l1) >> 21); |
| 118 | #else |
| 119 | #ifdef DEBUG |
| 120 | { |
| 121 | xfs_dfsbno_t b; |
| 122 | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 123 | b = (((xfs_dfsbno_t)l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | (((xfs_dfsbno_t)l1) >> 21); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 125 | ASSERT((b >> 32) == 0 || isnulldstartblock(b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | s->br_startblock = (xfs_fsblock_t)b; |
| 127 | } |
| 128 | #else /* !DEBUG */ |
| 129 | s->br_startblock = (xfs_fsblock_t)(((xfs_dfsbno_t)l1) >> 21); |
| 130 | #endif /* DEBUG */ |
| 131 | #endif /* XFS_BIG_BLKNOS */ |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 132 | s->br_blockcount = (xfs_filblks_t)(l1 & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* This is xfs_extent_state() in-line */ |
| 134 | if (ext_flag) { |
| 135 | ASSERT(s->br_blockcount != 0); /* saved for DMIG */ |
| 136 | st = XFS_EXT_UNWRITTEN; |
| 137 | } else |
| 138 | st = XFS_EXT_NORM; |
| 139 | s->br_state = st; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | xfs_bmbt_get_all( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 144 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | xfs_bmbt_irec_t *s) |
| 146 | { |
| 147 | __xfs_bmbt_get_all(r->l0, r->l1, s); |
| 148 | } |
| 149 | |
| 150 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * Extract the blockcount field from an in memory bmap extent record. |
| 152 | */ |
| 153 | xfs_filblks_t |
| 154 | xfs_bmbt_get_blockcount( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 155 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 157 | return (xfs_filblks_t)(r->l1 & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Extract the startblock field from an in memory bmap extent record. |
| 162 | */ |
| 163 | xfs_fsblock_t |
| 164 | xfs_bmbt_get_startblock( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 165 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 168 | return (((xfs_fsblock_t)r->l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | (((xfs_fsblock_t)r->l1) >> 21); |
| 170 | #else |
| 171 | #ifdef DEBUG |
| 172 | xfs_dfsbno_t b; |
| 173 | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 174 | b = (((xfs_dfsbno_t)r->l0 & xfs_mask64lo(9)) << 43) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | (((xfs_dfsbno_t)r->l1) >> 21); |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 176 | ASSERT((b >> 32) == 0 || isnulldstartblock(b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return (xfs_fsblock_t)b; |
| 178 | #else /* !DEBUG */ |
| 179 | return (xfs_fsblock_t)(((xfs_dfsbno_t)r->l1) >> 21); |
| 180 | #endif /* DEBUG */ |
| 181 | #endif /* XFS_BIG_BLKNOS */ |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Extract the startoff field from an in memory bmap extent record. |
| 186 | */ |
| 187 | xfs_fileoff_t |
| 188 | xfs_bmbt_get_startoff( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 189 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | return ((xfs_fileoff_t)r->l0 & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 192 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | xfs_exntst_t |
| 196 | xfs_bmbt_get_state( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 197 | xfs_bmbt_rec_host_t *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | int ext_flag; |
| 200 | |
| 201 | ext_flag = (int)((r->l0) >> (64 - BMBT_EXNTFLAG_BITLEN)); |
| 202 | return xfs_extent_state(xfs_bmbt_get_blockcount(r), |
| 203 | ext_flag); |
| 204 | } |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* |
| 207 | * Extract the blockcount field from an on disk bmap extent record. |
| 208 | */ |
| 209 | xfs_filblks_t |
| 210 | xfs_bmbt_disk_get_blockcount( |
| 211 | xfs_bmbt_rec_t *r) |
| 212 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 213 | return (xfs_filblks_t)(be64_to_cpu(r->l1) & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | * Extract the startoff field from a disk format bmap extent record. |
| 218 | */ |
| 219 | xfs_fileoff_t |
| 220 | xfs_bmbt_disk_get_startoff( |
| 221 | xfs_bmbt_rec_t *r) |
| 222 | { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 223 | return ((xfs_fileoff_t)be64_to_cpu(r->l0) & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 224 | xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN)) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 229 | * Set all the fields in a bmap extent record from the arguments. |
| 230 | */ |
| 231 | void |
| 232 | xfs_bmbt_set_allf( |
| 233 | xfs_bmbt_rec_host_t *r, |
| 234 | xfs_fileoff_t startoff, |
| 235 | xfs_fsblock_t startblock, |
| 236 | xfs_filblks_t blockcount, |
| 237 | xfs_exntst_t state) |
| 238 | { |
| 239 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
| 240 | |
| 241 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 242 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
| 243 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 244 | |
| 245 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 246 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 247 | |
| 248 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 249 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
| 250 | ((xfs_bmbt_rec_base_t)startblock >> 43); |
| 251 | r->l1 = ((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 /* !XFS_BIG_BLKNOS */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 255 | if (isnullstartblock(startblock)) { |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 256 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 257 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 258 | (xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
| 259 | r->l1 = xfs_mask64hi(11) | |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 260 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 261 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 262 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 263 | } else { |
| 264 | r->l0 = ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 265 | ((xfs_bmbt_rec_base_t)startoff << 9); |
| 266 | r->l1 = ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 267 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 268 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 269 | } |
| 270 | #endif /* XFS_BIG_BLKNOS */ |
| 271 | } |
| 272 | |
| 273 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | * Set all the fields in a bmap extent record from the uncompressed form. |
| 275 | */ |
| 276 | void |
| 277 | xfs_bmbt_set_all( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 278 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | xfs_bmbt_irec_t *s) |
| 280 | { |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 281 | xfs_bmbt_set_allf(r, s->br_startoff, s->br_startblock, |
| 282 | s->br_blockcount, s->br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 287 | * Set all the fields in a disk format bmap extent record from the arguments. |
| 288 | */ |
| 289 | void |
| 290 | xfs_bmbt_disk_set_allf( |
| 291 | xfs_bmbt_rec_t *r, |
| 292 | xfs_fileoff_t startoff, |
| 293 | xfs_fsblock_t startblock, |
| 294 | xfs_filblks_t blockcount, |
| 295 | xfs_exntst_t state) |
| 296 | { |
| 297 | int extent_flag = (state == XFS_EXT_NORM) ? 0 : 1; |
| 298 | |
| 299 | ASSERT(state == XFS_EXT_NORM || state == XFS_EXT_UNWRITTEN); |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 300 | ASSERT((startoff & xfs_mask64hi(64-BMBT_STARTOFF_BITLEN)) == 0); |
| 301 | ASSERT((blockcount & xfs_mask64hi(64-BMBT_BLOCKCOUNT_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 302 | |
| 303 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 304 | ASSERT((startblock & xfs_mask64hi(64-BMBT_STARTBLOCK_BITLEN)) == 0); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 305 | |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 306 | r->l0 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 307 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 308 | ((xfs_bmbt_rec_base_t)startoff << 9) | |
| 309 | ((xfs_bmbt_rec_base_t)startblock >> 43)); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 310 | r->l1 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 311 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 312 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 313 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 314 | #else /* !XFS_BIG_BLKNOS */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 315 | if (isnullstartblock(startblock)) { |
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) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 319 | (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
| 320 | r->l1 = cpu_to_be64(xfs_mask64hi(11) | |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 321 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 322 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 323 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 324 | } else { |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 325 | r->l0 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 326 | ((xfs_bmbt_rec_base_t)extent_flag << 63) | |
| 327 | ((xfs_bmbt_rec_base_t)startoff << 9)); |
Christoph Hellwig | cd8b0a9 | 2007-08-16 16:24:15 +1000 | [diff] [blame] | 328 | r->l1 = cpu_to_be64( |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 329 | ((xfs_bmbt_rec_base_t)startblock << 21) | |
| 330 | ((xfs_bmbt_rec_base_t)blockcount & |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 331 | (xfs_bmbt_rec_base_t)xfs_mask64lo(21))); |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 332 | } |
| 333 | #endif /* XFS_BIG_BLKNOS */ |
| 334 | } |
| 335 | |
| 336 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | * Set all the fields in a bmap extent record from the uncompressed form. |
| 338 | */ |
Eric Sandeen | 5d77c0d | 2009-11-19 15:52:00 +0000 | [diff] [blame] | 339 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | xfs_bmbt_disk_set_all( |
| 341 | xfs_bmbt_rec_t *r, |
| 342 | xfs_bmbt_irec_t *s) |
| 343 | { |
Christoph Hellwig | 8cba434 | 2007-08-16 16:23:53 +1000 | [diff] [blame] | 344 | xfs_bmbt_disk_set_allf(r, s->br_startoff, s->br_startblock, |
| 345 | s->br_blockcount, s->br_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * Set the blockcount field in a bmap extent record. |
| 350 | */ |
| 351 | void |
| 352 | xfs_bmbt_set_blockcount( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 353 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | xfs_filblks_t v) |
| 355 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 356 | ASSERT((v & xfs_mask64hi(43)) == 0); |
| 357 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64hi(43)) | |
| 358 | (xfs_bmbt_rec_base_t)(v & xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Set the startblock field in a bmap extent record. |
| 363 | */ |
| 364 | void |
| 365 | xfs_bmbt_set_startblock( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 366 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | xfs_fsblock_t v) |
| 368 | { |
| 369 | #if XFS_BIG_BLKNOS |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 370 | ASSERT((v & xfs_mask64hi(12)) == 0); |
| 371 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64hi(55)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | (xfs_bmbt_rec_base_t)(v >> 43); |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 373 | r->l1 = (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | (xfs_bmbt_rec_base_t)(v << 21); |
| 375 | #else /* !XFS_BIG_BLKNOS */ |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 376 | if (isnullstartblock(v)) { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 377 | r->l0 |= (xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
| 378 | r->l1 = (xfs_bmbt_rec_base_t)xfs_mask64hi(11) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | ((xfs_bmbt_rec_base_t)v << 21) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 380 | (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } else { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 382 | r->l0 &= ~(xfs_bmbt_rec_base_t)xfs_mask64lo(9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | r->l1 = ((xfs_bmbt_rec_base_t)v << 21) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 384 | (r->l1 & (xfs_bmbt_rec_base_t)xfs_mask64lo(21)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | #endif /* XFS_BIG_BLKNOS */ |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Set the startoff field in a bmap extent record. |
| 391 | */ |
| 392 | void |
| 393 | xfs_bmbt_set_startoff( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 394 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | xfs_fileoff_t v) |
| 396 | { |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 397 | ASSERT((v & xfs_mask64hi(9)) == 0); |
| 398 | r->l0 = (r->l0 & (xfs_bmbt_rec_base_t) xfs_mask64hi(1)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | ((xfs_bmbt_rec_base_t)v << 9) | |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 400 | (r->l0 & (xfs_bmbt_rec_base_t)xfs_mask64lo(9)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Set the extent state field in a bmap extent record. |
| 405 | */ |
| 406 | void |
| 407 | xfs_bmbt_set_state( |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 408 | xfs_bmbt_rec_host_t *r, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | xfs_exntst_t v) |
| 410 | { |
| 411 | ASSERT(v == XFS_EXT_NORM || v == XFS_EXT_UNWRITTEN); |
| 412 | if (v == XFS_EXT_NORM) |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 413 | r->l0 &= xfs_mask64lo(64 - BMBT_EXNTFLAG_BITLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | else |
Eric Sandeen | fb82557 | 2009-01-09 15:53:54 +1100 | [diff] [blame] | 415 | r->l0 |= xfs_mask64hi(BMBT_EXNTFLAG_BITLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Convert in-memory form of btree root to on-disk form. |
| 420 | */ |
| 421 | void |
| 422 | xfs_bmbt_to_bmdr( |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 423 | struct xfs_mount *mp, |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 424 | struct xfs_btree_block *rblock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | int rblocklen, |
| 426 | xfs_bmdr_block_t *dblock, |
| 427 | int dblocklen) |
| 428 | { |
| 429 | int dmxr; |
| 430 | xfs_bmbt_key_t *fkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 431 | __be64 *fpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | xfs_bmbt_key_t *tkp; |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 433 | __be64 *tpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 435 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 436 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_CRC_MAGIC)); |
| 437 | ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid)); |
| 438 | ASSERT(rblock->bb_u.l.bb_blkno == |
| 439 | cpu_to_be64(XFS_BUF_DADDR_NULL)); |
| 440 | } else |
| 441 | ASSERT(rblock->bb_magic == cpu_to_be32(XFS_BMAP_MAGIC)); |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 442 | ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO)); |
| 443 | ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO)); |
| 444 | ASSERT(rblock->bb_level != 0); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 445 | dblock->bb_level = rblock->bb_level; |
| 446 | dblock->bb_numrecs = rblock->bb_numrecs; |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 447 | dmxr = xfs_bmdr_maxrecs(mp, dblocklen, 0); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 448 | fkp = XFS_BMBT_KEY_ADDR(mp, rblock, 1); |
| 449 | tkp = XFS_BMDR_KEY_ADDR(dblock, 1); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 450 | fpp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, rblocklen); |
Christoph Hellwig | 136341b | 2008-10-30 17:11:40 +1100 | [diff] [blame] | 451 | tpp = XFS_BMDR_PTR_ADDR(dblock, 1, dmxr); |
Christoph Hellwig | 16259e7 | 2005-11-02 15:11:25 +1100 | [diff] [blame] | 452 | dmxr = be16_to_cpu(dblock->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | memcpy(tkp, fkp, sizeof(*fkp) * dmxr); |
Christoph Hellwig | 576039c | 2006-09-28 10:58:06 +1000 | [diff] [blame] | 454 | memcpy(tpp, fpp, sizeof(*fpp) * dmxr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 458 | * Check extent records, which have just been read, for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | * any bit in the extent flag field. ASSERT on debug |
| 460 | * kernels, as this condition should not occur. |
| 461 | * Return an error condition (1) if any flags found, |
| 462 | * otherwise return 0. |
| 463 | */ |
| 464 | |
| 465 | int |
| 466 | xfs_check_nostate_extents( |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 467 | xfs_ifork_t *ifp, |
| 468 | xfs_extnum_t idx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | xfs_extnum_t num) |
| 470 | { |
Mandy Kirkconnell | 4eea22f | 2006-03-14 13:29:52 +1100 | [diff] [blame] | 471 | for (; num > 0; num--, idx++) { |
Christoph Hellwig | a6f64d4 | 2007-08-16 16:23:40 +1000 | [diff] [blame] | 472 | xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if ((ep->l0 >> |
| 474 | (64 - BMBT_EXNTFLAG_BITLEN)) != 0) { |
| 475 | ASSERT(0); |
| 476 | return 1; |
| 477 | } |
| 478 | } |
| 479 | return 0; |
| 480 | } |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 481 | |
| 482 | |
| 483 | STATIC struct xfs_btree_cur * |
| 484 | xfs_bmbt_dup_cursor( |
| 485 | struct xfs_btree_cur *cur) |
| 486 | { |
| 487 | struct xfs_btree_cur *new; |
| 488 | |
| 489 | new = xfs_bmbt_init_cursor(cur->bc_mp, cur->bc_tp, |
| 490 | cur->bc_private.b.ip, cur->bc_private.b.whichfork); |
| 491 | |
| 492 | /* |
| 493 | * Copy the firstblock, flist, and flags values, |
| 494 | * since init cursor doesn't get them. |
| 495 | */ |
| 496 | new->bc_private.b.firstblock = cur->bc_private.b.firstblock; |
| 497 | new->bc_private.b.flist = cur->bc_private.b.flist; |
| 498 | new->bc_private.b.flags = cur->bc_private.b.flags; |
| 499 | |
| 500 | return new; |
| 501 | } |
| 502 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 503 | STATIC void |
| 504 | xfs_bmbt_update_cursor( |
| 505 | struct xfs_btree_cur *src, |
| 506 | struct xfs_btree_cur *dst) |
| 507 | { |
| 508 | ASSERT((dst->bc_private.b.firstblock != NULLFSBLOCK) || |
| 509 | (dst->bc_private.b.ip->i_d.di_flags & XFS_DIFLAG_REALTIME)); |
| 510 | ASSERT(dst->bc_private.b.flist == src->bc_private.b.flist); |
| 511 | |
| 512 | dst->bc_private.b.allocated += src->bc_private.b.allocated; |
| 513 | dst->bc_private.b.firstblock = src->bc_private.b.firstblock; |
| 514 | |
| 515 | src->bc_private.b.allocated = 0; |
| 516 | } |
| 517 | |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 518 | STATIC int |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 519 | xfs_bmbt_alloc_block( |
| 520 | struct xfs_btree_cur *cur, |
| 521 | union xfs_btree_ptr *start, |
| 522 | union xfs_btree_ptr *new, |
| 523 | int length, |
| 524 | int *stat) |
| 525 | { |
| 526 | xfs_alloc_arg_t args; /* block allocation args */ |
| 527 | int error; /* error return value */ |
| 528 | |
| 529 | memset(&args, 0, sizeof(args)); |
| 530 | args.tp = cur->bc_tp; |
| 531 | args.mp = cur->bc_mp; |
| 532 | args.fsbno = cur->bc_private.b.firstblock; |
| 533 | args.firstblock = args.fsbno; |
| 534 | |
| 535 | if (args.fsbno == NULLFSBLOCK) { |
| 536 | args.fsbno = be64_to_cpu(start->l); |
| 537 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 538 | /* |
| 539 | * Make sure there is sufficient room left in the AG to |
| 540 | * complete a full tree split for an extent insert. If |
| 541 | * we are converting the middle part of an extent then |
| 542 | * we may need space for two tree splits. |
| 543 | * |
| 544 | * We are relying on the caller to make the correct block |
| 545 | * reservation for this operation to succeed. If the |
| 546 | * reservation amount is insufficient then we may fail a |
| 547 | * block allocation here and corrupt the filesystem. |
| 548 | */ |
| 549 | args.minleft = xfs_trans_get_block_res(args.tp); |
| 550 | } else if (cur->bc_private.b.flist->xbf_low) { |
| 551 | args.type = XFS_ALLOCTYPE_START_BNO; |
| 552 | } else { |
| 553 | args.type = XFS_ALLOCTYPE_NEAR_BNO; |
| 554 | } |
| 555 | |
| 556 | args.minlen = args.maxlen = args.prod = 1; |
| 557 | args.wasdel = cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL; |
| 558 | if (!args.wasdel && xfs_trans_get_block_res(args.tp) == 0) { |
| 559 | error = XFS_ERROR(ENOSPC); |
| 560 | goto error0; |
| 561 | } |
| 562 | error = xfs_alloc_vextent(&args); |
| 563 | if (error) |
| 564 | goto error0; |
| 565 | |
| 566 | if (args.fsbno == NULLFSBLOCK && args.minleft) { |
| 567 | /* |
| 568 | * Could not find an AG with enough free space to satisfy |
| 569 | * a full btree split. Try again without minleft and if |
| 570 | * successful activate the lowspace algorithm. |
| 571 | */ |
| 572 | args.fsbno = 0; |
| 573 | args.type = XFS_ALLOCTYPE_FIRST_AG; |
| 574 | args.minleft = 0; |
| 575 | error = xfs_alloc_vextent(&args); |
| 576 | if (error) |
| 577 | goto error0; |
| 578 | cur->bc_private.b.flist->xbf_low = 1; |
| 579 | } |
| 580 | if (args.fsbno == NULLFSBLOCK) { |
| 581 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 582 | *stat = 0; |
| 583 | return 0; |
| 584 | } |
| 585 | ASSERT(args.len == 1); |
| 586 | cur->bc_private.b.firstblock = args.fsbno; |
| 587 | cur->bc_private.b.allocated++; |
| 588 | cur->bc_private.b.ip->i_d.di_nblocks++; |
| 589 | 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] | 590 | xfs_trans_mod_dquot_byino(args.tp, cur->bc_private.b.ip, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 591 | XFS_TRANS_DQ_BCOUNT, 1L); |
| 592 | |
| 593 | new->l = cpu_to_be64(args.fsbno); |
| 594 | |
| 595 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 596 | *stat = 1; |
| 597 | return 0; |
| 598 | |
| 599 | error0: |
| 600 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 601 | return error; |
| 602 | } |
| 603 | |
| 604 | STATIC int |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 605 | xfs_bmbt_free_block( |
| 606 | struct xfs_btree_cur *cur, |
| 607 | struct xfs_buf *bp) |
| 608 | { |
| 609 | struct xfs_mount *mp = cur->bc_mp; |
| 610 | struct xfs_inode *ip = cur->bc_private.b.ip; |
| 611 | struct xfs_trans *tp = cur->bc_tp; |
| 612 | xfs_fsblock_t fsbno = XFS_DADDR_TO_FSB(mp, XFS_BUF_ADDR(bp)); |
| 613 | |
| 614 | xfs_bmap_add_free(fsbno, 1, cur->bc_private.b.flist, mp); |
| 615 | ip->i_d.di_nblocks--; |
| 616 | |
| 617 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 618 | xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L); |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 619 | xfs_trans_binval(tp, bp); |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | STATIC int |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 624 | xfs_bmbt_get_minrecs( |
| 625 | struct xfs_btree_cur *cur, |
| 626 | int level) |
| 627 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 628 | if (level == cur->bc_nlevels - 1) { |
| 629 | struct xfs_ifork *ifp; |
| 630 | |
| 631 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, |
| 632 | cur->bc_private.b.whichfork); |
| 633 | |
| 634 | return xfs_bmbt_maxrecs(cur->bc_mp, |
| 635 | ifp->if_broot_bytes, level == 0) / 2; |
| 636 | } |
| 637 | |
| 638 | return cur->bc_mp->m_bmap_dmnr[level != 0]; |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 639 | } |
| 640 | |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 641 | int |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 642 | xfs_bmbt_get_maxrecs( |
| 643 | struct xfs_btree_cur *cur, |
| 644 | int level) |
| 645 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 646 | if (level == cur->bc_nlevels - 1) { |
| 647 | struct xfs_ifork *ifp; |
| 648 | |
| 649 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, |
| 650 | cur->bc_private.b.whichfork); |
| 651 | |
| 652 | return xfs_bmbt_maxrecs(cur->bc_mp, |
| 653 | ifp->if_broot_bytes, level == 0); |
| 654 | } |
| 655 | |
| 656 | return cur->bc_mp->m_bmap_dmxr[level != 0]; |
| 657 | |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 658 | } |
| 659 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 660 | /* |
| 661 | * Get the maximum records we could store in the on-disk format. |
| 662 | * |
| 663 | * For non-root nodes this is equivalent to xfs_bmbt_get_maxrecs, but |
| 664 | * for the root node this checks the available space in the dinode fork |
| 665 | * so that we can resize the in-memory buffer to match it. After a |
| 666 | * resize to the maximum size this function returns the same value |
| 667 | * as xfs_bmbt_get_maxrecs for the root node, too. |
| 668 | */ |
| 669 | STATIC int |
| 670 | xfs_bmbt_get_dmaxrecs( |
| 671 | struct xfs_btree_cur *cur, |
| 672 | int level) |
| 673 | { |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 674 | if (level != cur->bc_nlevels - 1) |
| 675 | return cur->bc_mp->m_bmap_dmxr[level != 0]; |
| 676 | return xfs_bmdr_maxrecs(cur->bc_mp, cur->bc_private.b.forksize, |
| 677 | level == 0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 678 | } |
| 679 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 680 | STATIC void |
| 681 | xfs_bmbt_init_key_from_rec( |
| 682 | union xfs_btree_key *key, |
| 683 | union xfs_btree_rec *rec) |
| 684 | { |
| 685 | key->bmbt.br_startoff = |
| 686 | cpu_to_be64(xfs_bmbt_disk_get_startoff(&rec->bmbt)); |
| 687 | } |
| 688 | |
| 689 | STATIC void |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 690 | xfs_bmbt_init_rec_from_key( |
| 691 | union xfs_btree_key *key, |
| 692 | union xfs_btree_rec *rec) |
| 693 | { |
| 694 | ASSERT(key->bmbt.br_startoff != 0); |
| 695 | |
| 696 | xfs_bmbt_disk_set_allf(&rec->bmbt, be64_to_cpu(key->bmbt.br_startoff), |
| 697 | 0, 0, XFS_EXT_NORM); |
| 698 | } |
| 699 | |
| 700 | STATIC void |
| 701 | xfs_bmbt_init_rec_from_cur( |
| 702 | struct xfs_btree_cur *cur, |
| 703 | union xfs_btree_rec *rec) |
| 704 | { |
| 705 | xfs_bmbt_disk_set_all(&rec->bmbt, &cur->bc_rec.b); |
| 706 | } |
| 707 | |
| 708 | STATIC void |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 709 | xfs_bmbt_init_ptr_from_cur( |
| 710 | struct xfs_btree_cur *cur, |
| 711 | union xfs_btree_ptr *ptr) |
| 712 | { |
| 713 | ptr->l = 0; |
| 714 | } |
| 715 | |
| 716 | STATIC __int64_t |
| 717 | xfs_bmbt_key_diff( |
| 718 | struct xfs_btree_cur *cur, |
| 719 | union xfs_btree_key *key) |
| 720 | { |
| 721 | return (__int64_t)be64_to_cpu(key->bmbt.br_startoff) - |
| 722 | cur->bc_rec.b.br_startoff; |
| 723 | } |
| 724 | |
Dave Chinner | c5c249b | 2013-08-12 20:49:43 +1000 | [diff] [blame] | 725 | static bool |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 726 | xfs_bmbt_verify( |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 727 | struct xfs_buf *bp) |
| 728 | { |
| 729 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 730 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 731 | unsigned int level; |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 732 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 733 | switch (block->bb_magic) { |
| 734 | case cpu_to_be32(XFS_BMAP_CRC_MAGIC): |
| 735 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 736 | return false; |
| 737 | if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid)) |
| 738 | return false; |
| 739 | if (be64_to_cpu(block->bb_u.l.bb_blkno) != bp->b_bn) |
| 740 | return false; |
| 741 | /* |
| 742 | * XXX: need a better way of verifying the owner here. Right now |
| 743 | * just make sure there has been one set. |
| 744 | */ |
| 745 | if (be64_to_cpu(block->bb_u.l.bb_owner) == 0) |
| 746 | return false; |
| 747 | /* fall through */ |
| 748 | case cpu_to_be32(XFS_BMAP_MAGIC): |
| 749 | break; |
| 750 | default: |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * numrecs and level verification. |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 756 | * |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 757 | * We don't know what fork we belong to, so just verify that the level |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 758 | * is less than the maximum of the two. Later checks will be more |
| 759 | * precise. |
| 760 | */ |
| 761 | level = be16_to_cpu(block->bb_level); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 762 | if (level > max(mp->m_bm_maxlevels[0], mp->m_bm_maxlevels[1])) |
| 763 | return false; |
| 764 | if (be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0]) |
| 765 | return false; |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 766 | |
| 767 | /* sibling pointer verification */ |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 768 | if (!block->bb_u.l.bb_leftsib || |
| 769 | (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLDFSBNO) && |
| 770 | !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))) |
| 771 | return false; |
| 772 | if (!block->bb_u.l.bb_rightsib || |
| 773 | (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLDFSBNO) && |
| 774 | !XFS_FSB_SANITY_CHECK(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))) |
| 775 | return false; |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 776 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 777 | return true; |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 778 | } |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 779 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 780 | static void |
| 781 | xfs_bmbt_read_verify( |
| 782 | struct xfs_buf *bp) |
| 783 | { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 784 | if (!(xfs_btree_lblock_verify_crc(bp) && |
| 785 | xfs_bmbt_verify(bp))) { |
| 786 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
| 787 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, |
| 788 | bp->b_target->bt_mount, bp->b_addr); |
| 789 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 790 | } |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | static void |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 794 | xfs_bmbt_write_verify( |
| 795 | struct xfs_buf *bp) |
| 796 | { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 797 | if (!xfs_bmbt_verify(bp)) { |
| 798 | xfs_warn(bp->b_target->bt_mount, "bmbt daddr 0x%llx failed", bp->b_bn); |
| 799 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
| 800 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, |
| 801 | bp->b_target->bt_mount, bp->b_addr); |
| 802 | xfs_buf_ioerror(bp, EFSCORRUPTED); |
| 803 | return; |
| 804 | } |
| 805 | xfs_btree_lblock_calc_crc(bp); |
Dave Chinner | 612cfbf | 2012-11-14 17:52:32 +1100 | [diff] [blame] | 806 | } |
| 807 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 808 | const struct xfs_buf_ops xfs_bmbt_buf_ops = { |
| 809 | .verify_read = xfs_bmbt_read_verify, |
| 810 | .verify_write = xfs_bmbt_write_verify, |
| 811 | }; |
| 812 | |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 813 | |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 814 | #if defined(DEBUG) || defined(XFS_WARN) |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 815 | STATIC int |
| 816 | xfs_bmbt_keys_inorder( |
| 817 | struct xfs_btree_cur *cur, |
| 818 | union xfs_btree_key *k1, |
| 819 | union xfs_btree_key *k2) |
| 820 | { |
| 821 | return be64_to_cpu(k1->bmbt.br_startoff) < |
| 822 | be64_to_cpu(k2->bmbt.br_startoff); |
| 823 | } |
| 824 | |
| 825 | STATIC int |
| 826 | xfs_bmbt_recs_inorder( |
| 827 | struct xfs_btree_cur *cur, |
| 828 | union xfs_btree_rec *r1, |
| 829 | union xfs_btree_rec *r2) |
| 830 | { |
| 831 | return xfs_bmbt_disk_get_startoff(&r1->bmbt) + |
| 832 | xfs_bmbt_disk_get_blockcount(&r1->bmbt) <= |
| 833 | xfs_bmbt_disk_get_startoff(&r2->bmbt); |
| 834 | } |
| 835 | #endif /* DEBUG */ |
| 836 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 837 | static const struct xfs_btree_ops xfs_bmbt_ops = { |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 838 | .rec_len = sizeof(xfs_bmbt_rec_t), |
| 839 | .key_len = sizeof(xfs_bmbt_key_t), |
| 840 | |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 841 | .dup_cursor = xfs_bmbt_dup_cursor, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 842 | .update_cursor = xfs_bmbt_update_cursor, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 843 | .alloc_block = xfs_bmbt_alloc_block, |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 844 | .free_block = xfs_bmbt_free_block, |
Christoph Hellwig | ce5e42d | 2008-10-30 16:55:23 +1100 | [diff] [blame] | 845 | .get_maxrecs = xfs_bmbt_get_maxrecs, |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 846 | .get_minrecs = xfs_bmbt_get_minrecs, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 847 | .get_dmaxrecs = xfs_bmbt_get_dmaxrecs, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 848 | .init_key_from_rec = xfs_bmbt_init_key_from_rec, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 849 | .init_rec_from_key = xfs_bmbt_init_rec_from_key, |
| 850 | .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 851 | .init_ptr_from_cur = xfs_bmbt_init_ptr_from_cur, |
| 852 | .key_diff = xfs_bmbt_key_diff, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 853 | .buf_ops = &xfs_bmbt_buf_ops, |
Dave Chinner | 742ae1e | 2013-04-30 21:39:34 +1000 | [diff] [blame] | 854 | #if defined(DEBUG) || defined(XFS_WARN) |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 855 | .keys_inorder = xfs_bmbt_keys_inorder, |
| 856 | .recs_inorder = xfs_bmbt_recs_inorder, |
| 857 | #endif |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 858 | }; |
| 859 | |
| 860 | /* |
| 861 | * Allocate a new bmap btree cursor. |
| 862 | */ |
| 863 | struct xfs_btree_cur * /* new bmap btree cursor */ |
| 864 | xfs_bmbt_init_cursor( |
| 865 | struct xfs_mount *mp, /* file system mount point */ |
| 866 | struct xfs_trans *tp, /* transaction pointer */ |
| 867 | struct xfs_inode *ip, /* inode owning the btree */ |
| 868 | int whichfork) /* data or attr fork */ |
| 869 | { |
| 870 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 871 | struct xfs_btree_cur *cur; |
| 872 | |
| 873 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_SLEEP); |
| 874 | |
| 875 | cur->bc_tp = tp; |
| 876 | cur->bc_mp = mp; |
| 877 | cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1; |
| 878 | cur->bc_btnum = XFS_BTNUM_BMAP; |
| 879 | cur->bc_blocklog = mp->m_sb.sb_blocklog; |
| 880 | |
| 881 | cur->bc_ops = &xfs_bmbt_ops; |
Christoph Hellwig | e99ab90 | 2008-10-30 16:54:33 +1100 | [diff] [blame] | 882 | cur->bc_flags = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 883 | if (xfs_sb_version_hascrc(&mp->m_sb)) |
| 884 | cur->bc_flags |= XFS_BTREE_CRC_BLOCKS; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 885 | |
| 886 | cur->bc_private.b.forksize = XFS_IFORK_SIZE(ip, whichfork); |
| 887 | cur->bc_private.b.ip = ip; |
| 888 | cur->bc_private.b.firstblock = NULLFSBLOCK; |
| 889 | cur->bc_private.b.flist = NULL; |
| 890 | cur->bc_private.b.allocated = 0; |
| 891 | cur->bc_private.b.flags = 0; |
| 892 | cur->bc_private.b.whichfork = whichfork; |
| 893 | |
| 894 | return cur; |
| 895 | } |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * Calculate number of records in a bmap btree block. |
| 899 | */ |
| 900 | int |
| 901 | xfs_bmbt_maxrecs( |
| 902 | struct xfs_mount *mp, |
| 903 | int blocklen, |
| 904 | int leaf) |
| 905 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 906 | blocklen -= XFS_BMBT_BLOCK_LEN(mp); |
Christoph Hellwig | 60197e8 | 2008-10-30 17:11:19 +1100 | [diff] [blame] | 907 | |
| 908 | if (leaf) |
| 909 | return blocklen / sizeof(xfs_bmbt_rec_t); |
| 910 | return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t)); |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Calculate number of records in a bmap btree inode root. |
| 915 | */ |
| 916 | int |
| 917 | xfs_bmdr_maxrecs( |
| 918 | struct xfs_mount *mp, |
| 919 | int blocklen, |
| 920 | int leaf) |
| 921 | { |
| 922 | blocklen -= sizeof(xfs_bmdr_block_t); |
| 923 | |
| 924 | if (leaf) |
| 925 | return blocklen / sizeof(xfs_bmdr_rec_t); |
| 926 | return blocklen / (sizeof(xfs_bmdr_key_t) + sizeof(xfs_bmdr_ptr_t)); |
| 927 | } |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * Change the owner of a btree format fork fo the inode passed in. Change it to |
| 931 | * the owner of that is passed in so that we can change owners before or after |
| 932 | * we switch forks between inodes. The operation that the caller is doing will |
| 933 | * determine whether is needs to change owner before or after the switch. |
| 934 | * |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame^] | 935 | * For demand paged transactional modification, the fork switch should be done |
| 936 | * after reading in all the blocks, modifying them and pinning them in the |
| 937 | * transaction. For modification when the buffers are already pinned in memory, |
| 938 | * the fork switch can be done before changing the owner as we won't need to |
| 939 | * validate the owner until the btree buffers are unpinned and writes can occur |
| 940 | * again. |
| 941 | * |
| 942 | * For recovery based ownership change, there is no transactional context and |
| 943 | * so a buffer list must be supplied so that we can record the buffers that we |
| 944 | * modified for the caller to issue IO on. |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 945 | */ |
| 946 | int |
| 947 | xfs_bmbt_change_owner( |
| 948 | struct xfs_trans *tp, |
| 949 | struct xfs_inode *ip, |
| 950 | int whichfork, |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame^] | 951 | xfs_ino_t new_owner, |
| 952 | struct list_head *buffer_list) |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 953 | { |
| 954 | struct xfs_btree_cur *cur; |
| 955 | int error; |
| 956 | |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame^] | 957 | ASSERT(tp || buffer_list); |
| 958 | ASSERT(!(tp && buffer_list)); |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 959 | if (whichfork == XFS_DATA_FORK) |
| 960 | ASSERT(ip->i_d.di_format = XFS_DINODE_FMT_BTREE); |
| 961 | else |
| 962 | ASSERT(ip->i_d.di_aformat = XFS_DINODE_FMT_BTREE); |
| 963 | |
| 964 | cur = xfs_bmbt_init_cursor(ip->i_mount, tp, ip, whichfork); |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame^] | 965 | if (!cur) |
| 966 | return ENOMEM; |
| 967 | |
| 968 | error = xfs_btree_change_owner(cur, new_owner, buffer_list); |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 969 | xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR); |
| 970 | return error; |
| 971 | } |