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-2002,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 | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 20 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 21 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_log_format.h" |
| 23 | #include "xfs_trans_resv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 24 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 26 | #include "xfs_defer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 28 | #include "xfs_trans.h" |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 29 | #include "xfs_inode_item.h" |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 30 | #include "xfs_buf_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_error.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 33 | #include "xfs_trace.h" |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 34 | #include "xfs_cksum.h" |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 35 | #include "xfs_alloc.h" |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 36 | #include "xfs_log.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Cursor allocation zone. |
| 40 | */ |
| 41 | kmem_zone_t *xfs_btree_cur_zone; |
| 42 | |
| 43 | /* |
| 44 | * Btree magic numbers. |
| 45 | */ |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 46 | static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = { |
Darrick J. Wong | b870494 | 2016-08-03 11:30:32 +1000 | [diff] [blame] | 47 | { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC, |
Darrick J. Wong | 46eeb52 | 2016-10-03 09:11:16 -0700 | [diff] [blame] | 48 | XFS_FIBT_MAGIC, 0 }, |
Darrick J. Wong | b870494 | 2016-08-03 11:30:32 +1000 | [diff] [blame] | 49 | { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC, |
Darrick J. Wong | 46eeb52 | 2016-10-03 09:11:16 -0700 | [diff] [blame] | 50 | XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC, |
| 51 | XFS_REFC_CRC_MAGIC } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 53 | |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 54 | uint32_t |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 55 | xfs_btree_magic( |
| 56 | int crc, |
| 57 | xfs_btnum_t btnum) |
| 58 | { |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 59 | uint32_t magic = xfs_magics[crc][btnum]; |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 60 | |
| 61 | /* Ensure we asked for crc for crc-only magics. */ |
| 62 | ASSERT(magic != 0); |
| 63 | return magic; |
| 64 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 66 | /* |
| 67 | * Check a long btree block header. Return the address of the failing check, |
| 68 | * or NULL if everything is ok. |
| 69 | */ |
| 70 | xfs_failaddr_t |
| 71 | __xfs_btree_check_lblock( |
| 72 | struct xfs_btree_cur *cur, |
| 73 | struct xfs_btree_block *block, |
| 74 | int level, |
| 75 | struct xfs_buf *bp) |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 76 | { |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 77 | struct xfs_mount *mp = cur->bc_mp; |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 78 | xfs_btnum_t btnum = cur->bc_btnum; |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 79 | int crc = xfs_sb_version_hascrc(&mp->m_sb); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 80 | |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 81 | if (crc) { |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 82 | if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid)) |
| 83 | return __this_address; |
| 84 | if (block->bb_u.l.bb_blkno != |
| 85 | cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL)) |
| 86 | return __this_address; |
| 87 | if (block->bb_u.l.bb_pad != cpu_to_be32(0)) |
| 88 | return __this_address; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 91 | if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum)) |
| 92 | return __this_address; |
| 93 | if (be16_to_cpu(block->bb_level) != level) |
| 94 | return __this_address; |
| 95 | if (be16_to_cpu(block->bb_numrecs) > |
| 96 | cur->bc_ops->get_maxrecs(cur, level)) |
| 97 | return __this_address; |
| 98 | if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) && |
| 99 | !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib), |
| 100 | level + 1)) |
| 101 | return __this_address; |
| 102 | if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) && |
| 103 | !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib), |
| 104 | level + 1)) |
| 105 | return __this_address; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 106 | |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | /* Check a long btree block header. */ |
| 111 | int |
| 112 | xfs_btree_check_lblock( |
| 113 | struct xfs_btree_cur *cur, |
| 114 | struct xfs_btree_block *block, |
| 115 | int level, |
| 116 | struct xfs_buf *bp) |
| 117 | { |
| 118 | struct xfs_mount *mp = cur->bc_mp; |
| 119 | xfs_failaddr_t fa; |
| 120 | |
| 121 | fa = __xfs_btree_check_lblock(cur, block, level, bp); |
| 122 | if (unlikely(XFS_TEST_ERROR(fa != NULL, mp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 123 | XFS_ERRTAG_BTREE_CHECK_LBLOCK))) { |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 124 | if (bp) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 125 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 126 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 127 | return -EFSCORRUPTED; |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 132 | /* |
| 133 | * Check a short btree block header. Return the address of the failing check, |
| 134 | * or NULL if everything is ok. |
| 135 | */ |
| 136 | xfs_failaddr_t |
| 137 | __xfs_btree_check_sblock( |
| 138 | struct xfs_btree_cur *cur, |
| 139 | struct xfs_btree_block *block, |
| 140 | int level, |
| 141 | struct xfs_buf *bp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 143 | struct xfs_mount *mp = cur->bc_mp; |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 144 | xfs_btnum_t btnum = cur->bc_btnum; |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 145 | int crc = xfs_sb_version_hascrc(&mp->m_sb); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 146 | |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 147 | if (crc) { |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 148 | if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid)) |
| 149 | return __this_address; |
| 150 | if (block->bb_u.s.bb_blkno != |
| 151 | cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL)) |
| 152 | return __this_address; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 155 | if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum)) |
| 156 | return __this_address; |
| 157 | if (be16_to_cpu(block->bb_level) != level) |
| 158 | return __this_address; |
| 159 | if (be16_to_cpu(block->bb_numrecs) > |
| 160 | cur->bc_ops->get_maxrecs(cur, level)) |
| 161 | return __this_address; |
| 162 | if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) && |
| 163 | !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib), |
| 164 | level + 1)) |
| 165 | return __this_address; |
| 166 | if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) && |
| 167 | !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib), |
| 168 | level + 1)) |
| 169 | return __this_address; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 170 | |
Darrick J. Wong | 52c732e | 2017-10-17 21:37:33 -0700 | [diff] [blame^] | 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | /* Check a short btree block header. */ |
| 175 | STATIC int |
| 176 | xfs_btree_check_sblock( |
| 177 | struct xfs_btree_cur *cur, |
| 178 | struct xfs_btree_block *block, |
| 179 | int level, |
| 180 | struct xfs_buf *bp) |
| 181 | { |
| 182 | struct xfs_mount *mp = cur->bc_mp; |
| 183 | xfs_failaddr_t fa; |
| 184 | |
| 185 | fa = __xfs_btree_check_sblock(cur, block, level, bp); |
| 186 | if (unlikely(XFS_TEST_ERROR(fa != NULL, mp, |
Darrick J. Wong | 9e24cfd | 2017-06-20 17:54:47 -0700 | [diff] [blame] | 187 | XFS_ERRTAG_BTREE_CHECK_SBLOCK))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (bp) |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 189 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 190 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 191 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /* |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 197 | * Debug routine: check that block header is ok. |
| 198 | */ |
| 199 | int |
| 200 | xfs_btree_check_block( |
| 201 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 202 | struct xfs_btree_block *block, /* generic btree block pointer */ |
| 203 | int level, /* level of the btree block */ |
| 204 | struct xfs_buf *bp) /* buffer containing block, if any */ |
| 205 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 206 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 207 | return xfs_btree_check_lblock(cur, block, level, bp); |
| 208 | else |
| 209 | return xfs_btree_check_sblock(cur, block, level, bp); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 210 | } |
| 211 | |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 212 | /* Check that this long pointer is valid and points within the fs. */ |
| 213 | bool |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 214 | xfs_btree_check_lptr( |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 215 | struct xfs_btree_cur *cur, |
| 216 | xfs_fsblock_t fsbno, |
| 217 | int level) |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 218 | { |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 219 | if (level <= 0) |
| 220 | return false; |
| 221 | return xfs_verify_fsbno(cur->bc_mp, fsbno); |
| 222 | } |
| 223 | |
| 224 | /* Check that this short pointer is valid and points within the AG. */ |
| 225 | bool |
| 226 | xfs_btree_check_sptr( |
| 227 | struct xfs_btree_cur *cur, |
| 228 | xfs_agblock_t agbno, |
| 229 | int level) |
| 230 | { |
| 231 | if (level <= 0) |
| 232 | return false; |
| 233 | return xfs_verify_agbno(cur->bc_mp, cur->bc_private.a.agno, agbno); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 234 | } |
| 235 | |
Lachlan McIlroy | 24ee0e4 | 2008-10-30 17:05:26 +1100 | [diff] [blame] | 236 | #ifdef DEBUG |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 237 | /* |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 238 | * Check that a given (indexed) btree pointer at a certain level of a |
| 239 | * btree is valid and doesn't point past where it should. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 241 | int |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 242 | xfs_btree_check_ptr( |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 243 | struct xfs_btree_cur *cur, |
| 244 | union xfs_btree_ptr *ptr, |
| 245 | int index, |
| 246 | int level) |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 247 | { |
| 248 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 249 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 250 | xfs_btree_check_lptr(cur, |
| 251 | be64_to_cpu((&ptr->l)[index]), level)); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 252 | } else { |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 253 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, |
| 254 | xfs_btree_check_sptr(cur, |
| 255 | be32_to_cpu((&ptr->s)[index]), level)); |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 256 | } |
Darrick J. Wong | f135761 | 2017-10-17 21:37:33 -0700 | [diff] [blame] | 257 | |
| 258 | return 0; |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 259 | } |
Lachlan McIlroy | 24ee0e4 | 2008-10-30 17:05:26 +1100 | [diff] [blame] | 260 | #endif |
Christoph Hellwig | a23f6ef | 2008-10-30 16:54:53 +1100 | [diff] [blame] | 261 | |
| 262 | /* |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 263 | * Calculate CRC on the whole btree block and stuff it into the |
| 264 | * long-form btree header. |
| 265 | * |
| 266 | * Prior to calculting the CRC, pull the LSN out of the buffer log item and put |
Geliang Tang | fef4ded | 2015-10-12 16:02:32 +1100 | [diff] [blame] | 267 | * it into the buffer so recovery knows what the last modification was that made |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 268 | * it to disk. |
| 269 | */ |
| 270 | void |
| 271 | xfs_btree_lblock_calc_crc( |
| 272 | struct xfs_buf *bp) |
| 273 | { |
| 274 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 275 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 276 | |
| 277 | if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb)) |
| 278 | return; |
| 279 | if (bip) |
| 280 | block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 281 | xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | bool |
| 285 | xfs_btree_lblock_verify_crc( |
| 286 | struct xfs_buf *bp) |
| 287 | { |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 288 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 289 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 290 | |
| 291 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 292 | if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn))) |
| 293 | return false; |
Eric Sandeen | 5158217 | 2014-02-27 15:17:27 +1100 | [diff] [blame] | 294 | return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF); |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 295 | } |
Eric Sandeen | 5158217 | 2014-02-27 15:17:27 +1100 | [diff] [blame] | 296 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Calculate CRC on the whole btree block and stuff it into the |
| 302 | * short-form btree header. |
| 303 | * |
| 304 | * Prior to calculting the CRC, pull the LSN out of the buffer log item and put |
Geliang Tang | fef4ded | 2015-10-12 16:02:32 +1100 | [diff] [blame] | 305 | * it into the buffer so recovery knows what the last modification was that made |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 306 | * it to disk. |
| 307 | */ |
| 308 | void |
| 309 | xfs_btree_sblock_calc_crc( |
| 310 | struct xfs_buf *bp) |
| 311 | { |
| 312 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 313 | struct xfs_buf_log_item *bip = bp->b_fspriv; |
| 314 | |
| 315 | if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb)) |
| 316 | return; |
| 317 | if (bip) |
| 318 | block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn); |
Eric Sandeen | f1dbcd7 | 2014-02-27 15:18:23 +1100 | [diff] [blame] | 319 | xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | bool |
| 323 | xfs_btree_sblock_verify_crc( |
| 324 | struct xfs_buf *bp) |
| 325 | { |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 326 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 327 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 328 | |
| 329 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 330 | if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn))) |
| 331 | return false; |
Eric Sandeen | 5158217 | 2014-02-27 15:17:27 +1100 | [diff] [blame] | 332 | return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF); |
Brian Foster | a45086e | 2015-10-12 15:59:25 +1100 | [diff] [blame] | 333 | } |
Eric Sandeen | 5158217 | 2014-02-27 15:17:27 +1100 | [diff] [blame] | 334 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 335 | return true; |
| 336 | } |
| 337 | |
Christoph Hellwig | c46ee8a | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 338 | static int |
| 339 | xfs_btree_free_block( |
| 340 | struct xfs_btree_cur *cur, |
| 341 | struct xfs_buf *bp) |
| 342 | { |
| 343 | int error; |
| 344 | |
| 345 | error = cur->bc_ops->free_block(cur, bp); |
Christoph Hellwig | edfd9dd | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 346 | if (!error) { |
| 347 | xfs_trans_binval(cur->bc_tp, bp); |
Christoph Hellwig | c46ee8a | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 348 | XFS_BTREE_STATS_INC(cur, free); |
Christoph Hellwig | edfd9dd | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 349 | } |
Christoph Hellwig | c46ee8a | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 350 | return error; |
| 351 | } |
| 352 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 353 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | * Delete the btree cursor. |
| 355 | */ |
| 356 | void |
| 357 | xfs_btree_del_cursor( |
| 358 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 359 | int error) /* del because of error */ |
| 360 | { |
| 361 | int i; /* btree level */ |
| 362 | |
| 363 | /* |
| 364 | * Clear the buffer pointers, and release the buffers. |
| 365 | * If we're doing this in the face of an error, we |
| 366 | * need to make sure to inspect all of the entries |
| 367 | * in the bc_bufs array for buffers to be unlocked. |
| 368 | * This is because some of the btree code works from |
| 369 | * level n down to 0, and if we get an error along |
| 370 | * the way we won't have initialized all the entries |
| 371 | * down to 0. |
| 372 | */ |
| 373 | for (i = 0; i < cur->bc_nlevels; i++) { |
| 374 | if (cur->bc_bufs[i]) |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 375 | xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | else if (!error) |
| 377 | break; |
| 378 | } |
| 379 | /* |
| 380 | * Can't free a bmap cursor without having dealt with the |
| 381 | * allocated indirect blocks' accounting. |
| 382 | */ |
| 383 | ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP || |
| 384 | cur->bc_private.b.allocated == 0); |
| 385 | /* |
| 386 | * Free the cursor. |
| 387 | */ |
| 388 | kmem_zone_free(xfs_btree_cur_zone, cur); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Duplicate the btree cursor. |
| 393 | * Allocate a new one, copy the record, re-get the buffers. |
| 394 | */ |
| 395 | int /* error */ |
| 396 | xfs_btree_dup_cursor( |
| 397 | xfs_btree_cur_t *cur, /* input cursor */ |
| 398 | xfs_btree_cur_t **ncur) /* output cursor */ |
| 399 | { |
| 400 | xfs_buf_t *bp; /* btree block's buffer pointer */ |
| 401 | int error; /* error return value */ |
| 402 | int i; /* level number of btree block */ |
| 403 | xfs_mount_t *mp; /* mount structure for filesystem */ |
| 404 | xfs_btree_cur_t *new; /* new cursor value */ |
| 405 | xfs_trans_t *tp; /* transaction pointer, can be NULL */ |
| 406 | |
| 407 | tp = cur->bc_tp; |
| 408 | mp = cur->bc_mp; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* |
| 411 | * Allocate a new cursor like the old one. |
| 412 | */ |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 413 | new = cur->bc_ops->dup_cursor(cur); |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* |
| 416 | * Copy the record currently in the cursor. |
| 417 | */ |
| 418 | new->bc_rec = cur->bc_rec; |
Christoph Hellwig | 561f7d1 | 2008-10-30 16:53:59 +1100 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* |
| 421 | * For each level current, re-get the buffer and copy the ptr value. |
| 422 | */ |
| 423 | for (i = 0; i < new->bc_nlevels; i++) { |
| 424 | new->bc_ptrs[i] = cur->bc_ptrs[i]; |
| 425 | new->bc_ra[i] = cur->bc_ra[i]; |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 426 | bp = cur->bc_bufs[i]; |
| 427 | if (bp) { |
| 428 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, |
| 429 | XFS_BUF_ADDR(bp), mp->m_bsize, |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 430 | 0, &bp, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 431 | cur->bc_ops->buf_ops); |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 432 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | xfs_btree_del_cursor(new, error); |
| 434 | *ncur = NULL; |
| 435 | return error; |
| 436 | } |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 437 | } |
| 438 | new->bc_bufs[i] = bp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | *ncur = new; |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /* |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 445 | * XFS btree block layout and addressing: |
| 446 | * |
| 447 | * There are two types of blocks in the btree: leaf and non-leaf blocks. |
| 448 | * |
| 449 | * The leaf record start with a header then followed by records containing |
| 450 | * the values. A non-leaf block also starts with the same header, and |
| 451 | * then first contains lookup keys followed by an equal number of pointers |
| 452 | * to the btree blocks at the previous level. |
| 453 | * |
| 454 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 455 | * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N | |
| 456 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 457 | * |
| 458 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 459 | * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N | |
| 460 | * +--------+-------+-------+-------+-------+-------+-------+ |
| 461 | * |
| 462 | * The header is called struct xfs_btree_block for reasons better left unknown |
| 463 | * and comes in different versions for short (32bit) and long (64bit) block |
| 464 | * pointers. The record and key structures are defined by the btree instances |
| 465 | * and opaque to the btree core. The block pointers are simple disk endian |
| 466 | * integers, available in a short (32bit) and long (64bit) variant. |
| 467 | * |
| 468 | * The helpers below calculate the offset of a given record, key or pointer |
| 469 | * into a btree block (xfs_btree_*_offset) or return a pointer to the given |
| 470 | * record, key or pointer (xfs_btree_*_addr). Note that all addressing |
| 471 | * inside the btree block is done using indices starting at one, not zero! |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 472 | * |
| 473 | * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing |
| 474 | * overlapping intervals. In such a tree, records are still sorted lowest to |
| 475 | * highest and indexed by the smallest key value that refers to the record. |
| 476 | * However, nodes are different: each pointer has two associated keys -- one |
| 477 | * indexing the lowest key available in the block(s) below (the same behavior |
| 478 | * as the key in a regular btree) and another indexing the highest key |
| 479 | * available in the block(s) below. Because records are /not/ sorted by the |
| 480 | * highest key, all leaf block updates require us to compute the highest key |
| 481 | * that matches any record in the leaf and to recursively update the high keys |
| 482 | * in the nodes going further up in the tree, if necessary. Nodes look like |
| 483 | * this: |
| 484 | * |
| 485 | * +--------+-----+-----+-----+-----+-----+-------+-------+-----+ |
| 486 | * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... | |
| 487 | * +--------+-----+-----+-----+-----+-----+-------+-------+-----+ |
| 488 | * |
| 489 | * To perform an interval query on an overlapped tree, perform the usual |
| 490 | * depth-first search and use the low and high keys to decide if we can skip |
| 491 | * that particular node. If a leaf node is reached, return the records that |
| 492 | * intersect the interval. Note that an interval query may return numerous |
| 493 | * entries. For a non-overlapped tree, simply search for the record associated |
| 494 | * with the lowest key and iterate forward until a non-matching record is |
| 495 | * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by |
| 496 | * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in |
| 497 | * more detail. |
| 498 | * |
| 499 | * Why do we care about overlapping intervals? Let's say you have a bunch of |
| 500 | * reverse mapping records on a reflink filesystem: |
| 501 | * |
| 502 | * 1: +- file A startblock B offset C length D -----------+ |
| 503 | * 2: +- file E startblock F offset G length H --------------+ |
| 504 | * 3: +- file I startblock F offset J length K --+ |
| 505 | * 4: +- file L... --+ |
| 506 | * |
| 507 | * Now say we want to map block (B+D) into file A at offset (C+D). Ideally, |
| 508 | * we'd simply increment the length of record 1. But how do we find the record |
| 509 | * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return |
| 510 | * record 3 because the keys are ordered first by startblock. An interval |
| 511 | * query would return records 1 and 2 because they both overlap (B+D-1), and |
| 512 | * from that we can pick out record 1 as the appropriate left neighbor. |
| 513 | * |
| 514 | * In the non-overlapped case you can do a LE lookup and decrement the cursor |
| 515 | * because a record's interval must end before the next record. |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 516 | */ |
| 517 | |
| 518 | /* |
| 519 | * Return size of the btree block header for this btree instance. |
| 520 | */ |
| 521 | static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur) |
| 522 | { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 523 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 524 | if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) |
| 525 | return XFS_BTREE_LBLOCK_CRC_LEN; |
| 526 | return XFS_BTREE_LBLOCK_LEN; |
| 527 | } |
| 528 | if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) |
| 529 | return XFS_BTREE_SBLOCK_CRC_LEN; |
| 530 | return XFS_BTREE_SBLOCK_LEN; |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* |
| 534 | * Return size of btree block pointers for this btree instance. |
| 535 | */ |
| 536 | static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur) |
| 537 | { |
| 538 | return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? |
| 539 | sizeof(__be64) : sizeof(__be32); |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Calculate offset of the n-th record in a btree block. |
| 544 | */ |
| 545 | STATIC size_t |
| 546 | xfs_btree_rec_offset( |
| 547 | struct xfs_btree_cur *cur, |
| 548 | int n) |
| 549 | { |
| 550 | return xfs_btree_block_len(cur) + |
| 551 | (n - 1) * cur->bc_ops->rec_len; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * Calculate offset of the n-th key in a btree block. |
| 556 | */ |
| 557 | STATIC size_t |
| 558 | xfs_btree_key_offset( |
| 559 | struct xfs_btree_cur *cur, |
| 560 | int n) |
| 561 | { |
| 562 | return xfs_btree_block_len(cur) + |
| 563 | (n - 1) * cur->bc_ops->key_len; |
| 564 | } |
| 565 | |
| 566 | /* |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 567 | * Calculate offset of the n-th high key in a btree block. |
| 568 | */ |
| 569 | STATIC size_t |
| 570 | xfs_btree_high_key_offset( |
| 571 | struct xfs_btree_cur *cur, |
| 572 | int n) |
| 573 | { |
| 574 | return xfs_btree_block_len(cur) + |
| 575 | (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2); |
| 576 | } |
| 577 | |
| 578 | /* |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 579 | * Calculate offset of the n-th block pointer in a btree block. |
| 580 | */ |
| 581 | STATIC size_t |
| 582 | xfs_btree_ptr_offset( |
| 583 | struct xfs_btree_cur *cur, |
| 584 | int n, |
| 585 | int level) |
| 586 | { |
| 587 | return xfs_btree_block_len(cur) + |
| 588 | cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len + |
| 589 | (n - 1) * xfs_btree_ptr_len(cur); |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Return a pointer to the n-th record in the btree block. |
| 594 | */ |
Darrick J. Wong | 2678809 | 2017-06-16 11:00:07 -0700 | [diff] [blame] | 595 | union xfs_btree_rec * |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 596 | xfs_btree_rec_addr( |
| 597 | struct xfs_btree_cur *cur, |
| 598 | int n, |
| 599 | struct xfs_btree_block *block) |
| 600 | { |
| 601 | return (union xfs_btree_rec *) |
| 602 | ((char *)block + xfs_btree_rec_offset(cur, n)); |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Return a pointer to the n-th key in the btree block. |
| 607 | */ |
Darrick J. Wong | 2678809 | 2017-06-16 11:00:07 -0700 | [diff] [blame] | 608 | union xfs_btree_key * |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 609 | xfs_btree_key_addr( |
| 610 | struct xfs_btree_cur *cur, |
| 611 | int n, |
| 612 | struct xfs_btree_block *block) |
| 613 | { |
| 614 | return (union xfs_btree_key *) |
| 615 | ((char *)block + xfs_btree_key_offset(cur, n)); |
| 616 | } |
| 617 | |
| 618 | /* |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 619 | * Return a pointer to the n-th high key in the btree block. |
| 620 | */ |
Darrick J. Wong | 2678809 | 2017-06-16 11:00:07 -0700 | [diff] [blame] | 621 | union xfs_btree_key * |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 622 | xfs_btree_high_key_addr( |
| 623 | struct xfs_btree_cur *cur, |
| 624 | int n, |
| 625 | struct xfs_btree_block *block) |
| 626 | { |
| 627 | return (union xfs_btree_key *) |
| 628 | ((char *)block + xfs_btree_high_key_offset(cur, n)); |
| 629 | } |
| 630 | |
| 631 | /* |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 632 | * Return a pointer to the n-th block pointer in the btree block. |
| 633 | */ |
Darrick J. Wong | 2678809 | 2017-06-16 11:00:07 -0700 | [diff] [blame] | 634 | union xfs_btree_ptr * |
Christoph Hellwig | 65f1eae | 2008-10-30 16:55:34 +1100 | [diff] [blame] | 635 | xfs_btree_ptr_addr( |
| 636 | struct xfs_btree_cur *cur, |
| 637 | int n, |
| 638 | struct xfs_btree_block *block) |
| 639 | { |
| 640 | int level = xfs_btree_get_level(block); |
| 641 | |
| 642 | ASSERT(block->bb_level != 0); |
| 643 | |
| 644 | return (union xfs_btree_ptr *) |
| 645 | ((char *)block + xfs_btree_ptr_offset(cur, n, level)); |
| 646 | } |
| 647 | |
| 648 | /* |
Zhi Yong Wu | 1cb9386 | 2013-08-07 10:11:05 +0000 | [diff] [blame] | 649 | * Get the root block which is stored in the inode. |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 650 | * |
| 651 | * For now this btree implementation assumes the btree root is always |
| 652 | * stored in the if_broot field of an inode fork. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | */ |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 654 | STATIC struct xfs_btree_block * |
| 655 | xfs_btree_get_iroot( |
Kaho Ng | fbfb24b | 2016-07-20 10:37:50 +1000 | [diff] [blame] | 656 | struct xfs_btree_cur *cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
Kaho Ng | fbfb24b | 2016-07-20 10:37:50 +1000 | [diff] [blame] | 658 | struct xfs_ifork *ifp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Kaho Ng | fbfb24b | 2016-07-20 10:37:50 +1000 | [diff] [blame] | 660 | ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork); |
| 661 | return (struct xfs_btree_block *)ifp->if_broot; |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Retrieve the block pointer from the cursor at the given level. |
| 666 | * This may be an inode btree root or from a buffer. |
| 667 | */ |
Darrick J. Wong | 2678809 | 2017-06-16 11:00:07 -0700 | [diff] [blame] | 668 | struct xfs_btree_block * /* generic btree block pointer */ |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 669 | xfs_btree_get_block( |
| 670 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 671 | int level, /* level in btree */ |
| 672 | struct xfs_buf **bpp) /* buffer containing the block */ |
| 673 | { |
| 674 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 675 | (level == cur->bc_nlevels - 1)) { |
| 676 | *bpp = NULL; |
| 677 | return xfs_btree_get_iroot(cur); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } |
Christoph Hellwig | 8186e51 | 2008-10-30 16:54:22 +1100 | [diff] [blame] | 679 | |
| 680 | *bpp = cur->bc_bufs[level]; |
| 681 | return XFS_BUF_TO_BLOCK(*bpp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Get a buffer for the block, return it with no data read. |
| 686 | * Long-form addressing. |
| 687 | */ |
| 688 | xfs_buf_t * /* buffer for fsbno */ |
| 689 | xfs_btree_get_bufl( |
| 690 | xfs_mount_t *mp, /* file system mount point */ |
| 691 | xfs_trans_t *tp, /* transaction pointer */ |
| 692 | xfs_fsblock_t fsbno, /* file system block number */ |
| 693 | uint lock) /* lock flags for get_buf */ |
| 694 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | xfs_daddr_t d; /* real disk block address */ |
| 696 | |
| 697 | ASSERT(fsbno != NULLFSBLOCK); |
| 698 | d = XFS_FSB_TO_DADDR(mp, fsbno); |
Dave Chinner | 36de955 | 2014-06-06 16:02:12 +1000 | [diff] [blame] | 699 | return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Get a buffer for the block, return it with no data read. |
| 704 | * Short-form addressing. |
| 705 | */ |
| 706 | xfs_buf_t * /* buffer for agno/agbno */ |
| 707 | xfs_btree_get_bufs( |
| 708 | xfs_mount_t *mp, /* file system mount point */ |
| 709 | xfs_trans_t *tp, /* transaction pointer */ |
| 710 | xfs_agnumber_t agno, /* allocation group number */ |
| 711 | xfs_agblock_t agbno, /* allocation group block number */ |
| 712 | uint lock) /* lock flags for get_buf */ |
| 713 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | xfs_daddr_t d; /* real disk block address */ |
| 715 | |
| 716 | ASSERT(agno != NULLAGNUMBER); |
| 717 | ASSERT(agbno != NULLAGBLOCK); |
| 718 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); |
Dave Chinner | 36de955 | 2014-06-06 16:02:12 +1000 | [diff] [blame] | 719 | return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | * Check for the cursor referring to the last block at the given level. |
| 724 | */ |
| 725 | int /* 1=is last block, 0=not last block */ |
| 726 | xfs_btree_islastblock( |
| 727 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 728 | int level) /* level to check */ |
| 729 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 730 | struct xfs_btree_block *block; /* generic btree block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | xfs_buf_t *bp; /* buffer containing block */ |
| 732 | |
| 733 | block = xfs_btree_get_block(cur, level, &bp); |
| 734 | xfs_btree_check_block(cur, block, level, bp); |
Christoph Hellwig | e99ab90 | 2008-10-30 16:54:33 +1100 | [diff] [blame] | 735 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 736 | return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | else |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 738 | return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | /* |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 742 | * Change the cursor to point to the first record at the given level. |
| 743 | * Other levels are unaffected. |
| 744 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 745 | STATIC int /* success=1, failure=0 */ |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 746 | xfs_btree_firstrec( |
| 747 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 748 | int level) /* level to change */ |
| 749 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 750 | struct xfs_btree_block *block; /* generic btree block pointer */ |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 751 | xfs_buf_t *bp; /* buffer containing block */ |
| 752 | |
| 753 | /* |
| 754 | * Get the block pointer for this level. |
| 755 | */ |
| 756 | block = xfs_btree_get_block(cur, level, &bp); |
Darrick J. Wong | 1e86eab | 2017-07-17 14:30:45 -0700 | [diff] [blame] | 757 | if (xfs_btree_check_block(cur, block, level, bp)) |
| 758 | return 0; |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 759 | /* |
| 760 | * It's empty, there is no such record. |
| 761 | */ |
Christoph Hellwig | f2277f0 | 2008-10-30 16:53:47 +1100 | [diff] [blame] | 762 | if (!block->bb_numrecs) |
Christoph Hellwig | cdcf4333 | 2008-08-13 16:23:50 +1000 | [diff] [blame] | 763 | return 0; |
| 764 | /* |
| 765 | * Set the ptr value to 1, that's the first record/key. |
| 766 | */ |
| 767 | cur->bc_ptrs[level] = 1; |
| 768 | return 1; |
| 769 | } |
| 770 | |
| 771 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | * Change the cursor to point to the last record in the current block |
| 773 | * at the given level. Other levels are unaffected. |
| 774 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 775 | STATIC int /* success=1, failure=0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | xfs_btree_lastrec( |
| 777 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 778 | int level) /* level to change */ |
| 779 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 780 | struct xfs_btree_block *block; /* generic btree block pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | xfs_buf_t *bp; /* buffer containing block */ |
| 782 | |
| 783 | /* |
| 784 | * Get the block pointer for this level. |
| 785 | */ |
| 786 | block = xfs_btree_get_block(cur, level, &bp); |
Darrick J. Wong | 1e86eab | 2017-07-17 14:30:45 -0700 | [diff] [blame] | 787 | if (xfs_btree_check_block(cur, block, level, bp)) |
| 788 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | /* |
| 790 | * It's empty, there is no such record. |
| 791 | */ |
Christoph Hellwig | f2277f0 | 2008-10-30 16:53:47 +1100 | [diff] [blame] | 792 | if (!block->bb_numrecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | return 0; |
| 794 | /* |
| 795 | * Set the ptr value to numrecs, that's the last record/key. |
| 796 | */ |
Christoph Hellwig | f2277f0 | 2008-10-30 16:53:47 +1100 | [diff] [blame] | 797 | cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return 1; |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Compute first and last byte offsets for the fields given. |
| 803 | * Interprets the offsets table, which contains struct field offsets. |
| 804 | */ |
| 805 | void |
| 806 | xfs_btree_offsets( |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 807 | int64_t fields, /* bitmask of fields */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | const short *offsets, /* table of field offsets */ |
| 809 | int nbits, /* number of bits to inspect */ |
| 810 | int *first, /* output: first byte offset */ |
| 811 | int *last) /* output: last byte offset */ |
| 812 | { |
| 813 | int i; /* current bit number */ |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 814 | int64_t imask; /* mask for current bit number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | ASSERT(fields != 0); |
| 817 | /* |
| 818 | * Find the lowest bit, so the first byte offset. |
| 819 | */ |
| 820 | for (i = 0, imask = 1LL; ; i++, imask <<= 1) { |
| 821 | if (imask & fields) { |
| 822 | *first = offsets[i]; |
| 823 | break; |
| 824 | } |
| 825 | } |
| 826 | /* |
| 827 | * Find the highest bit, so the last byte offset. |
| 828 | */ |
| 829 | for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) { |
| 830 | if (imask & fields) { |
| 831 | *last = offsets[i + 1] - 1; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Get a buffer for the block, return it read in. |
| 839 | * Long-form addressing. |
| 840 | */ |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 841 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | xfs_btree_read_bufl( |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 843 | struct xfs_mount *mp, /* file system mount point */ |
| 844 | struct xfs_trans *tp, /* transaction pointer */ |
| 845 | xfs_fsblock_t fsbno, /* file system block number */ |
| 846 | uint lock, /* lock flags for read_buf */ |
| 847 | struct xfs_buf **bpp, /* buffer for fsbno */ |
| 848 | int refval, /* ref count value for buffer */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 849 | const struct xfs_buf_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 851 | struct xfs_buf *bp; /* return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | xfs_daddr_t d; /* real disk block address */ |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 853 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Darrick J. Wong | d5a91ba | 2017-02-02 15:13:58 -0800 | [diff] [blame] | 855 | if (!XFS_FSB_SANITY_CHECK(mp, fsbno)) |
| 856 | return -EFSCORRUPTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | d = XFS_FSB_TO_DADDR(mp, fsbno); |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 858 | error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 859 | mp->m_bsize, lock, &bp, ops); |
Dave Chinner | c3f8fc7 | 2012-11-12 22:54:01 +1100 | [diff] [blame] | 860 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return error; |
Dave Chinner | 821eb21 | 2010-12-02 16:31:13 +1100 | [diff] [blame] | 862 | if (bp) |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 863 | xfs_buf_set_ref(bp, refval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | *bpp = bp; |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | * Read-ahead the block, don't wait for it, don't return a buffer. |
| 870 | * Long-form addressing. |
| 871 | */ |
| 872 | /* ARGSUSED */ |
| 873 | void |
| 874 | xfs_btree_reada_bufl( |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 875 | struct xfs_mount *mp, /* file system mount point */ |
| 876 | xfs_fsblock_t fsbno, /* file system block number */ |
| 877 | xfs_extlen_t count, /* count of filesystem blocks */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 878 | const struct xfs_buf_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { |
| 880 | xfs_daddr_t d; |
| 881 | |
| 882 | ASSERT(fsbno != NULLFSBLOCK); |
| 883 | d = XFS_FSB_TO_DADDR(mp, fsbno); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 884 | xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
| 888 | * Read-ahead the block, don't wait for it, don't return a buffer. |
| 889 | * Short-form addressing. |
| 890 | */ |
| 891 | /* ARGSUSED */ |
| 892 | void |
| 893 | xfs_btree_reada_bufs( |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 894 | struct xfs_mount *mp, /* file system mount point */ |
| 895 | xfs_agnumber_t agno, /* allocation group number */ |
| 896 | xfs_agblock_t agbno, /* allocation group block number */ |
| 897 | xfs_extlen_t count, /* count of filesystem blocks */ |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 898 | const struct xfs_buf_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
| 900 | xfs_daddr_t d; |
| 901 | |
| 902 | ASSERT(agno != NULLAGNUMBER); |
| 903 | ASSERT(agbno != NULLAGBLOCK); |
| 904 | d = XFS_AGB_TO_DADDR(mp, agno, agbno); |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 905 | xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 908 | STATIC int |
| 909 | xfs_btree_readahead_lblock( |
| 910 | struct xfs_btree_cur *cur, |
| 911 | int lr, |
| 912 | struct xfs_btree_block *block) |
| 913 | { |
| 914 | int rval = 0; |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 915 | xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib); |
| 916 | xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib); |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 917 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 918 | if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) { |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 919 | xfs_btree_reada_bufl(cur->bc_mp, left, 1, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 920 | cur->bc_ops->buf_ops); |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 921 | rval++; |
| 922 | } |
| 923 | |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 924 | if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) { |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 925 | xfs_btree_reada_bufl(cur->bc_mp, right, 1, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 926 | cur->bc_ops->buf_ops); |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 927 | rval++; |
| 928 | } |
| 929 | |
| 930 | return rval; |
| 931 | } |
| 932 | |
| 933 | STATIC int |
| 934 | xfs_btree_readahead_sblock( |
| 935 | struct xfs_btree_cur *cur, |
| 936 | int lr, |
| 937 | struct xfs_btree_block *block) |
| 938 | { |
| 939 | int rval = 0; |
| 940 | xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib); |
| 941 | xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib); |
| 942 | |
| 943 | |
| 944 | if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) { |
| 945 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 946 | left, 1, cur->bc_ops->buf_ops); |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 947 | rval++; |
| 948 | } |
| 949 | |
| 950 | if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) { |
| 951 | xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 952 | right, 1, cur->bc_ops->buf_ops); |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 953 | rval++; |
| 954 | } |
| 955 | |
| 956 | return rval; |
| 957 | } |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | /* |
| 960 | * Read-ahead btree blocks, at the given level. |
| 961 | * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA. |
| 962 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 963 | STATIC int |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 964 | xfs_btree_readahead( |
| 965 | struct xfs_btree_cur *cur, /* btree cursor */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | int lev, /* level in btree */ |
| 967 | int lr) /* left/right bits */ |
| 968 | { |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 969 | struct xfs_btree_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 971 | /* |
| 972 | * No readahead needed if we are at the root level and the |
| 973 | * btree root is stored in the inode. |
| 974 | */ |
| 975 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 976 | (lev == cur->bc_nlevels - 1)) |
| 977 | return 0; |
| 978 | |
| 979 | if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev]) |
| 980 | return 0; |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | cur->bc_ra[lev] |= lr; |
Christoph Hellwig | b524bfe | 2008-10-30 16:54:43 +1100 | [diff] [blame] | 983 | block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]); |
| 984 | |
| 985 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 986 | return xfs_btree_readahead_lblock(cur, lr, block); |
| 987 | return xfs_btree_readahead_sblock(cur, lr, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 990 | STATIC xfs_daddr_t |
| 991 | xfs_btree_ptr_to_daddr( |
| 992 | struct xfs_btree_cur *cur, |
| 993 | union xfs_btree_ptr *ptr) |
| 994 | { |
| 995 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 996 | ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK)); |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 997 | |
| 998 | return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l)); |
| 999 | } else { |
| 1000 | ASSERT(cur->bc_private.a.agno != NULLAGNUMBER); |
| 1001 | ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK)); |
| 1002 | |
| 1003 | return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno, |
| 1004 | be32_to_cpu(ptr->s)); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * Readahead @count btree blocks at the given @ptr location. |
| 1010 | * |
| 1011 | * We don't need to care about long or short form btrees here as we have a |
| 1012 | * method of converting the ptr directly to a daddr available to us. |
| 1013 | */ |
| 1014 | STATIC void |
| 1015 | xfs_btree_readahead_ptr( |
| 1016 | struct xfs_btree_cur *cur, |
| 1017 | union xfs_btree_ptr *ptr, |
| 1018 | xfs_extlen_t count) |
| 1019 | { |
| 1020 | xfs_buf_readahead(cur->bc_mp->m_ddev_targp, |
| 1021 | xfs_btree_ptr_to_daddr(cur, ptr), |
| 1022 | cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops); |
| 1023 | } |
| 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | /* |
| 1026 | * Set the buffer for level "lev" in the cursor to bp, releasing |
| 1027 | * any previous buffer. |
| 1028 | */ |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 1029 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | xfs_btree_setbuf( |
| 1031 | xfs_btree_cur_t *cur, /* btree cursor */ |
| 1032 | int lev, /* level in btree */ |
| 1033 | xfs_buf_t *bp) /* new buffer to set */ |
| 1034 | { |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1035 | struct xfs_btree_block *b; /* btree block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 1037 | if (cur->bc_bufs[lev]) |
| 1038 | xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | cur->bc_bufs[lev] = bp; |
| 1040 | cur->bc_ra[lev] = 0; |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 1041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | b = XFS_BUF_TO_BLOCK(bp); |
Christoph Hellwig | e99ab90 | 2008-10-30 16:54:33 +1100 | [diff] [blame] | 1043 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1044 | if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1046 | if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
| 1048 | } else { |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1049 | if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA; |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1051 | if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA; |
| 1053 | } |
| 1054 | } |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1055 | |
| 1056 | STATIC int |
| 1057 | xfs_btree_ptr_is_null( |
| 1058 | struct xfs_btree_cur *cur, |
| 1059 | union xfs_btree_ptr *ptr) |
| 1060 | { |
| 1061 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1062 | return ptr->l == cpu_to_be64(NULLFSBLOCK); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1063 | else |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 1064 | return ptr->s == cpu_to_be32(NULLAGBLOCK); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1065 | } |
| 1066 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1067 | STATIC void |
| 1068 | xfs_btree_set_ptr_null( |
| 1069 | struct xfs_btree_cur *cur, |
| 1070 | union xfs_btree_ptr *ptr) |
| 1071 | { |
| 1072 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1073 | ptr->l = cpu_to_be64(NULLFSBLOCK); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 1074 | else |
| 1075 | ptr->s = cpu_to_be32(NULLAGBLOCK); |
| 1076 | } |
| 1077 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1078 | /* |
| 1079 | * Get/set/init sibling pointers |
| 1080 | */ |
| 1081 | STATIC void |
| 1082 | xfs_btree_get_sibling( |
| 1083 | struct xfs_btree_cur *cur, |
| 1084 | struct xfs_btree_block *block, |
| 1085 | union xfs_btree_ptr *ptr, |
| 1086 | int lr) |
| 1087 | { |
| 1088 | ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB); |
| 1089 | |
| 1090 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 1091 | if (lr == XFS_BB_RIGHTSIB) |
| 1092 | ptr->l = block->bb_u.l.bb_rightsib; |
| 1093 | else |
| 1094 | ptr->l = block->bb_u.l.bb_leftsib; |
| 1095 | } else { |
| 1096 | if (lr == XFS_BB_RIGHTSIB) |
| 1097 | ptr->s = block->bb_u.s.bb_rightsib; |
| 1098 | else |
| 1099 | ptr->s = block->bb_u.s.bb_leftsib; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1103 | STATIC void |
| 1104 | xfs_btree_set_sibling( |
| 1105 | struct xfs_btree_cur *cur, |
| 1106 | struct xfs_btree_block *block, |
| 1107 | union xfs_btree_ptr *ptr, |
| 1108 | int lr) |
| 1109 | { |
| 1110 | ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB); |
| 1111 | |
| 1112 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 1113 | if (lr == XFS_BB_RIGHTSIB) |
| 1114 | block->bb_u.l.bb_rightsib = ptr->l; |
| 1115 | else |
| 1116 | block->bb_u.l.bb_leftsib = ptr->l; |
| 1117 | } else { |
| 1118 | if (lr == XFS_BB_RIGHTSIB) |
| 1119 | block->bb_u.s.bb_rightsib = ptr->s; |
| 1120 | else |
| 1121 | block->bb_u.s.bb_leftsib = ptr->s; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1125 | void |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1126 | xfs_btree_init_block_int( |
| 1127 | struct xfs_mount *mp, |
| 1128 | struct xfs_btree_block *buf, |
| 1129 | xfs_daddr_t blkno, |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 1130 | xfs_btnum_t btnum, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1131 | __u16 level, |
| 1132 | __u16 numrecs, |
| 1133 | __u64 owner, |
| 1134 | unsigned int flags) |
| 1135 | { |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 1136 | int crc = xfs_sb_version_hascrc(&mp->m_sb); |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 1137 | __u32 magic = xfs_btree_magic(crc, btnum); |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 1138 | |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1139 | buf->bb_magic = cpu_to_be32(magic); |
| 1140 | buf->bb_level = cpu_to_be16(level); |
| 1141 | buf->bb_numrecs = cpu_to_be16(numrecs); |
| 1142 | |
| 1143 | if (flags & XFS_BTREE_LONG_PTRS) { |
Christoph Hellwig | d5cf09b | 2014-07-30 09:12:05 +1000 | [diff] [blame] | 1144 | buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK); |
| 1145 | buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK); |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 1146 | if (crc) { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1147 | buf->bb_u.l.bb_blkno = cpu_to_be64(blkno); |
| 1148 | buf->bb_u.l.bb_owner = cpu_to_be64(owner); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 1149 | uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid); |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1150 | buf->bb_u.l.bb_pad = 0; |
Dave Chinner | b58fa55 | 2013-08-28 21:22:46 +1000 | [diff] [blame] | 1151 | buf->bb_u.l.bb_lsn = 0; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1152 | } |
| 1153 | } else { |
| 1154 | /* owner is a 32 bit value on short blocks */ |
| 1155 | __u32 __owner = (__u32)owner; |
| 1156 | |
| 1157 | buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK); |
| 1158 | buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK); |
Eric Sandeen | f88ae46 | 2017-01-27 23:16:37 -0800 | [diff] [blame] | 1159 | if (crc) { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1160 | buf->bb_u.s.bb_blkno = cpu_to_be64(blkno); |
| 1161 | buf->bb_u.s.bb_owner = cpu_to_be32(__owner); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 1162 | uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid); |
Dave Chinner | b58fa55 | 2013-08-28 21:22:46 +1000 | [diff] [blame] | 1163 | buf->bb_u.s.bb_lsn = 0; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | void |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1169 | xfs_btree_init_block( |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1170 | struct xfs_mount *mp, |
| 1171 | struct xfs_buf *bp, |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 1172 | xfs_btnum_t btnum, |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1173 | __u16 level, |
| 1174 | __u16 numrecs, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1175 | __u64 owner, |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1176 | unsigned int flags) |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1177 | { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1178 | xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn, |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 1179 | btnum, level, numrecs, owner, flags); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1180 | } |
| 1181 | |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1182 | STATIC void |
| 1183 | xfs_btree_init_block_cur( |
| 1184 | struct xfs_btree_cur *cur, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1185 | struct xfs_buf *bp, |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1186 | int level, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1187 | int numrecs) |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1188 | { |
Eric Sandeen | af7d20f | 2017-01-27 23:16:38 -0800 | [diff] [blame] | 1189 | __u64 owner; |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1190 | |
| 1191 | /* |
| 1192 | * we can pull the owner from the cursor right now as the different |
| 1193 | * owners align directly with the pointer size of the btree. This may |
| 1194 | * change in future, but is safe for current users of the generic btree |
| 1195 | * code. |
| 1196 | */ |
| 1197 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 1198 | owner = cur->bc_private.b.ip->i_ino; |
| 1199 | else |
| 1200 | owner = cur->bc_private.a.agno; |
| 1201 | |
| 1202 | xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn, |
Eric Sandeen | b6f41e4 | 2017-01-27 23:16:39 -0800 | [diff] [blame] | 1203 | cur->bc_btnum, level, numrecs, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1204 | owner, cur->bc_flags); |
Dave Chinner | b64f3a3 | 2012-11-13 16:40:27 -0600 | [diff] [blame] | 1205 | } |
| 1206 | |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1207 | /* |
| 1208 | * Return true if ptr is the last record in the btree and |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1209 | * we need to track updates to this record. The decision |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1210 | * will be further refined in the update_lastrec method. |
| 1211 | */ |
| 1212 | STATIC int |
| 1213 | xfs_btree_is_lastrec( |
| 1214 | struct xfs_btree_cur *cur, |
| 1215 | struct xfs_btree_block *block, |
| 1216 | int level) |
| 1217 | { |
| 1218 | union xfs_btree_ptr ptr; |
| 1219 | |
| 1220 | if (level > 0) |
| 1221 | return 0; |
| 1222 | if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE)) |
| 1223 | return 0; |
| 1224 | |
| 1225 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 1226 | if (!xfs_btree_ptr_is_null(cur, &ptr)) |
| 1227 | return 0; |
| 1228 | return 1; |
| 1229 | } |
| 1230 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1231 | STATIC void |
| 1232 | xfs_btree_buf_to_ptr( |
| 1233 | struct xfs_btree_cur *cur, |
| 1234 | struct xfs_buf *bp, |
| 1235 | union xfs_btree_ptr *ptr) |
| 1236 | { |
| 1237 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 1238 | ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp, |
| 1239 | XFS_BUF_ADDR(bp))); |
| 1240 | else { |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 1241 | ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1242 | XFS_BUF_ADDR(bp))); |
| 1243 | } |
| 1244 | } |
| 1245 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1246 | STATIC void |
| 1247 | xfs_btree_set_refs( |
| 1248 | struct xfs_btree_cur *cur, |
| 1249 | struct xfs_buf *bp) |
| 1250 | { |
| 1251 | switch (cur->bc_btnum) { |
| 1252 | case XFS_BTNUM_BNO: |
| 1253 | case XFS_BTNUM_CNT: |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 1254 | xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1255 | break; |
| 1256 | case XFS_BTNUM_INO: |
Brian Foster | aafc3c2 | 2014-04-24 16:00:52 +1000 | [diff] [blame] | 1257 | case XFS_BTNUM_FINO: |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 1258 | xfs_buf_set_ref(bp, XFS_INO_BTREE_REF); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1259 | break; |
| 1260 | case XFS_BTNUM_BMAP: |
Christoph Hellwig | 38f2323 | 2011-10-10 16:52:45 +0000 | [diff] [blame] | 1261 | xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1262 | break; |
Darrick J. Wong | 035e00a | 2016-08-03 11:36:07 +1000 | [diff] [blame] | 1263 | case XFS_BTNUM_RMAP: |
| 1264 | xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF); |
| 1265 | break; |
Darrick J. Wong | 1946b91 | 2016-10-03 09:11:18 -0700 | [diff] [blame] | 1266 | case XFS_BTNUM_REFC: |
| 1267 | xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF); |
| 1268 | break; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1269 | default: |
| 1270 | ASSERT(0); |
| 1271 | } |
| 1272 | } |
| 1273 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1274 | STATIC int |
| 1275 | xfs_btree_get_buf_block( |
| 1276 | struct xfs_btree_cur *cur, |
| 1277 | union xfs_btree_ptr *ptr, |
| 1278 | int flags, |
| 1279 | struct xfs_btree_block **block, |
| 1280 | struct xfs_buf **bpp) |
| 1281 | { |
| 1282 | struct xfs_mount *mp = cur->bc_mp; |
| 1283 | xfs_daddr_t d; |
| 1284 | |
| 1285 | /* need to sort out how callers deal with failures first */ |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 1286 | ASSERT(!(flags & XBF_TRYLOCK)); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1287 | |
| 1288 | d = xfs_btree_ptr_to_daddr(cur, ptr); |
| 1289 | *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d, |
| 1290 | mp->m_bsize, flags); |
| 1291 | |
Chandra Seetharaman | 2a30f36d | 2011-09-20 13:56:55 +0000 | [diff] [blame] | 1292 | if (!*bpp) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1293 | return -ENOMEM; |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1294 | |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1295 | (*bpp)->b_ops = cur->bc_ops->buf_ops; |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 1296 | *block = XFS_BUF_TO_BLOCK(*bpp); |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1300 | /* |
| 1301 | * Read in the buffer at the given ptr and return the buffer and |
| 1302 | * the block pointer within the buffer. |
| 1303 | */ |
| 1304 | STATIC int |
| 1305 | xfs_btree_read_buf_block( |
| 1306 | struct xfs_btree_cur *cur, |
| 1307 | union xfs_btree_ptr *ptr, |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1308 | int flags, |
| 1309 | struct xfs_btree_block **block, |
| 1310 | struct xfs_buf **bpp) |
| 1311 | { |
| 1312 | struct xfs_mount *mp = cur->bc_mp; |
| 1313 | xfs_daddr_t d; |
| 1314 | int error; |
| 1315 | |
| 1316 | /* need to sort out how callers deal with failures first */ |
Christoph Hellwig | 0cadda1 | 2010-01-19 09:56:44 +0000 | [diff] [blame] | 1317 | ASSERT(!(flags & XBF_TRYLOCK)); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1318 | |
| 1319 | d = xfs_btree_ptr_to_daddr(cur, ptr); |
| 1320 | error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d, |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 1321 | mp->m_bsize, flags, bpp, |
Dave Chinner | 1813dd6 | 2012-11-14 17:54:40 +1100 | [diff] [blame] | 1322 | cur->bc_ops->buf_ops); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1323 | if (error) |
| 1324 | return error; |
| 1325 | |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1326 | xfs_btree_set_refs(cur, *bpp); |
| 1327 | *block = XFS_BUF_TO_BLOCK(*bpp); |
Dave Chinner | 3d3e6f6 | 2012-11-12 22:54:08 +1100 | [diff] [blame] | 1328 | return 0; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1332 | * Copy keys from one btree block to another. |
| 1333 | */ |
| 1334 | STATIC void |
| 1335 | xfs_btree_copy_keys( |
| 1336 | struct xfs_btree_cur *cur, |
| 1337 | union xfs_btree_key *dst_key, |
| 1338 | union xfs_btree_key *src_key, |
| 1339 | int numkeys) |
| 1340 | { |
| 1341 | ASSERT(numkeys >= 0); |
| 1342 | memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len); |
| 1343 | } |
| 1344 | |
| 1345 | /* |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1346 | * Copy records from one btree block to another. |
| 1347 | */ |
| 1348 | STATIC void |
| 1349 | xfs_btree_copy_recs( |
| 1350 | struct xfs_btree_cur *cur, |
| 1351 | union xfs_btree_rec *dst_rec, |
| 1352 | union xfs_btree_rec *src_rec, |
| 1353 | int numrecs) |
| 1354 | { |
| 1355 | ASSERT(numrecs >= 0); |
| 1356 | memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len); |
| 1357 | } |
| 1358 | |
| 1359 | /* |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1360 | * Copy block pointers from one btree block to another. |
| 1361 | */ |
| 1362 | STATIC void |
| 1363 | xfs_btree_copy_ptrs( |
| 1364 | struct xfs_btree_cur *cur, |
| 1365 | union xfs_btree_ptr *dst_ptr, |
| 1366 | union xfs_btree_ptr *src_ptr, |
| 1367 | int numptrs) |
| 1368 | { |
| 1369 | ASSERT(numptrs >= 0); |
| 1370 | memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur)); |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * Shift keys one index left/right inside a single btree block. |
| 1375 | */ |
| 1376 | STATIC void |
| 1377 | xfs_btree_shift_keys( |
| 1378 | struct xfs_btree_cur *cur, |
| 1379 | union xfs_btree_key *key, |
| 1380 | int dir, |
| 1381 | int numkeys) |
| 1382 | { |
| 1383 | char *dst_key; |
| 1384 | |
| 1385 | ASSERT(numkeys >= 0); |
| 1386 | ASSERT(dir == 1 || dir == -1); |
| 1387 | |
| 1388 | dst_key = (char *)key + (dir * cur->bc_ops->key_len); |
| 1389 | memmove(dst_key, key, numkeys * cur->bc_ops->key_len); |
| 1390 | } |
| 1391 | |
| 1392 | /* |
| 1393 | * Shift records one index left/right inside a single btree block. |
| 1394 | */ |
| 1395 | STATIC void |
| 1396 | xfs_btree_shift_recs( |
| 1397 | struct xfs_btree_cur *cur, |
| 1398 | union xfs_btree_rec *rec, |
| 1399 | int dir, |
| 1400 | int numrecs) |
| 1401 | { |
| 1402 | char *dst_rec; |
| 1403 | |
| 1404 | ASSERT(numrecs >= 0); |
| 1405 | ASSERT(dir == 1 || dir == -1); |
| 1406 | |
| 1407 | dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len); |
| 1408 | memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len); |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * Shift block pointers one index left/right inside a single btree block. |
| 1413 | */ |
| 1414 | STATIC void |
| 1415 | xfs_btree_shift_ptrs( |
| 1416 | struct xfs_btree_cur *cur, |
| 1417 | union xfs_btree_ptr *ptr, |
| 1418 | int dir, |
| 1419 | int numptrs) |
| 1420 | { |
| 1421 | char *dst_ptr; |
| 1422 | |
| 1423 | ASSERT(numptrs >= 0); |
| 1424 | ASSERT(dir == 1 || dir == -1); |
| 1425 | |
| 1426 | dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur)); |
| 1427 | memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur)); |
| 1428 | } |
| 1429 | |
| 1430 | /* |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1431 | * Log key values from the btree block. |
| 1432 | */ |
| 1433 | STATIC void |
| 1434 | xfs_btree_log_keys( |
| 1435 | struct xfs_btree_cur *cur, |
| 1436 | struct xfs_buf *bp, |
| 1437 | int first, |
| 1438 | int last) |
| 1439 | { |
| 1440 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1441 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); |
| 1442 | |
| 1443 | if (bp) { |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1444 | xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF); |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 1445 | xfs_trans_log_buf(cur->bc_tp, bp, |
| 1446 | xfs_btree_key_offset(cur, first), |
| 1447 | xfs_btree_key_offset(cur, last + 1) - 1); |
| 1448 | } else { |
| 1449 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, |
| 1450 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
| 1451 | } |
| 1452 | |
| 1453 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1454 | } |
| 1455 | |
| 1456 | /* |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1457 | * Log record values from the btree block. |
| 1458 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 1459 | void |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1460 | xfs_btree_log_recs( |
| 1461 | struct xfs_btree_cur *cur, |
| 1462 | struct xfs_buf *bp, |
| 1463 | int first, |
| 1464 | int last) |
| 1465 | { |
| 1466 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1467 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); |
| 1468 | |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1469 | xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 1470 | xfs_trans_log_buf(cur->bc_tp, bp, |
| 1471 | xfs_btree_rec_offset(cur, first), |
| 1472 | xfs_btree_rec_offset(cur, last + 1) - 1); |
| 1473 | |
| 1474 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1475 | } |
| 1476 | |
| 1477 | /* |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1478 | * Log block pointer fields from a btree block (nonleaf). |
| 1479 | */ |
| 1480 | STATIC void |
| 1481 | xfs_btree_log_ptrs( |
| 1482 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1483 | struct xfs_buf *bp, /* buffer containing btree block */ |
| 1484 | int first, /* index of first pointer to log */ |
| 1485 | int last) /* index of last pointer to log */ |
| 1486 | { |
| 1487 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1488 | XFS_BTREE_TRACE_ARGBII(cur, bp, first, last); |
| 1489 | |
| 1490 | if (bp) { |
| 1491 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 1492 | int level = xfs_btree_get_level(block); |
| 1493 | |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1494 | xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1495 | xfs_trans_log_buf(cur->bc_tp, bp, |
| 1496 | xfs_btree_ptr_offset(cur, first, level), |
| 1497 | xfs_btree_ptr_offset(cur, last + 1, level) - 1); |
| 1498 | } else { |
| 1499 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, |
| 1500 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
| 1501 | } |
| 1502 | |
| 1503 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Log fields from a btree block header. |
| 1508 | */ |
Christoph Hellwig | fd6bcc5b | 2008-10-30 16:58:21 +1100 | [diff] [blame] | 1509 | void |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1510 | xfs_btree_log_block( |
| 1511 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1512 | struct xfs_buf *bp, /* buffer containing btree block */ |
| 1513 | int fields) /* mask of fields: XFS_BB_... */ |
| 1514 | { |
| 1515 | int first; /* first byte offset logged */ |
| 1516 | int last; /* last byte offset logged */ |
| 1517 | static const short soffsets[] = { /* table of offsets (short) */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1518 | offsetof(struct xfs_btree_block, bb_magic), |
| 1519 | offsetof(struct xfs_btree_block, bb_level), |
| 1520 | offsetof(struct xfs_btree_block, bb_numrecs), |
| 1521 | offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib), |
| 1522 | offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib), |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1523 | offsetof(struct xfs_btree_block, bb_u.s.bb_blkno), |
| 1524 | offsetof(struct xfs_btree_block, bb_u.s.bb_lsn), |
| 1525 | offsetof(struct xfs_btree_block, bb_u.s.bb_uuid), |
| 1526 | offsetof(struct xfs_btree_block, bb_u.s.bb_owner), |
| 1527 | offsetof(struct xfs_btree_block, bb_u.s.bb_crc), |
| 1528 | XFS_BTREE_SBLOCK_CRC_LEN |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1529 | }; |
| 1530 | static const short loffsets[] = { /* table of offsets (long) */ |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 1531 | offsetof(struct xfs_btree_block, bb_magic), |
| 1532 | offsetof(struct xfs_btree_block, bb_level), |
| 1533 | offsetof(struct xfs_btree_block, bb_numrecs), |
| 1534 | offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib), |
| 1535 | offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib), |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1536 | offsetof(struct xfs_btree_block, bb_u.l.bb_blkno), |
| 1537 | offsetof(struct xfs_btree_block, bb_u.l.bb_lsn), |
| 1538 | offsetof(struct xfs_btree_block, bb_u.l.bb_uuid), |
| 1539 | offsetof(struct xfs_btree_block, bb_u.l.bb_owner), |
| 1540 | offsetof(struct xfs_btree_block, bb_u.l.bb_crc), |
| 1541 | offsetof(struct xfs_btree_block, bb_u.l.bb_pad), |
| 1542 | XFS_BTREE_LBLOCK_CRC_LEN |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1543 | }; |
| 1544 | |
| 1545 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1546 | XFS_BTREE_TRACE_ARGBI(cur, bp, fields); |
| 1547 | |
| 1548 | if (bp) { |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1549 | int nbits; |
| 1550 | |
| 1551 | if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) { |
| 1552 | /* |
| 1553 | * We don't log the CRC when updating a btree |
| 1554 | * block but instead recreate it during log |
| 1555 | * recovery. As the log buffers have checksums |
| 1556 | * of their own this is safe and avoids logging a crc |
| 1557 | * update in a lot of places. |
| 1558 | */ |
| 1559 | if (fields == XFS_BB_ALL_BITS) |
| 1560 | fields = XFS_BB_ALL_BITS_CRC; |
| 1561 | nbits = XFS_BB_NUM_BITS_CRC; |
| 1562 | } else { |
| 1563 | nbits = XFS_BB_NUM_BITS; |
| 1564 | } |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1565 | xfs_btree_offsets(fields, |
| 1566 | (cur->bc_flags & XFS_BTREE_LONG_PTRS) ? |
| 1567 | loffsets : soffsets, |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 1568 | nbits, &first, &last); |
Dave Chinner | 61fe135 | 2013-04-03 16:11:30 +1100 | [diff] [blame] | 1569 | xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 1570 | xfs_trans_log_buf(cur->bc_tp, bp, first, last); |
| 1571 | } else { |
| 1572 | xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip, |
| 1573 | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
| 1574 | } |
| 1575 | |
| 1576 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1577 | } |
| 1578 | |
| 1579 | /* |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1580 | * Increment cursor by one record at the level. |
| 1581 | * For nonzero levels the leaf-ward information is untouched. |
| 1582 | */ |
| 1583 | int /* error */ |
| 1584 | xfs_btree_increment( |
| 1585 | struct xfs_btree_cur *cur, |
| 1586 | int level, |
| 1587 | int *stat) /* success/failure */ |
| 1588 | { |
| 1589 | struct xfs_btree_block *block; |
| 1590 | union xfs_btree_ptr ptr; |
| 1591 | struct xfs_buf *bp; |
| 1592 | int error; /* error return value */ |
| 1593 | int lev; |
| 1594 | |
| 1595 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1596 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 1597 | |
| 1598 | ASSERT(level < cur->bc_nlevels); |
| 1599 | |
| 1600 | /* Read-ahead to the right at this level. */ |
| 1601 | xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA); |
| 1602 | |
| 1603 | /* Get a pointer to the btree block. */ |
| 1604 | block = xfs_btree_get_block(cur, level, &bp); |
| 1605 | |
| 1606 | #ifdef DEBUG |
| 1607 | error = xfs_btree_check_block(cur, block, level, bp); |
| 1608 | if (error) |
| 1609 | goto error0; |
| 1610 | #endif |
| 1611 | |
| 1612 | /* We're done if we remain in the block after the increment. */ |
| 1613 | if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block)) |
| 1614 | goto out1; |
| 1615 | |
| 1616 | /* Fail if we just went off the right edge of the tree. */ |
| 1617 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 1618 | if (xfs_btree_ptr_is_null(cur, &ptr)) |
| 1619 | goto out0; |
| 1620 | |
| 1621 | XFS_BTREE_STATS_INC(cur, increment); |
| 1622 | |
| 1623 | /* |
| 1624 | * March up the tree incrementing pointers. |
| 1625 | * Stop when we don't go off the right edge of a block. |
| 1626 | */ |
| 1627 | for (lev = level + 1; lev < cur->bc_nlevels; lev++) { |
| 1628 | block = xfs_btree_get_block(cur, lev, &bp); |
| 1629 | |
| 1630 | #ifdef DEBUG |
| 1631 | error = xfs_btree_check_block(cur, block, lev, bp); |
| 1632 | if (error) |
| 1633 | goto error0; |
| 1634 | #endif |
| 1635 | |
| 1636 | if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block)) |
| 1637 | break; |
| 1638 | |
| 1639 | /* Read-ahead the right block for the next loop. */ |
| 1640 | xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA); |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * If we went off the root then we are either seriously |
| 1645 | * confused or have the tree root in an inode. |
| 1646 | */ |
| 1647 | if (lev == cur->bc_nlevels) { |
| 1648 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) |
| 1649 | goto out0; |
| 1650 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1651 | error = -EFSCORRUPTED; |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1652 | goto error0; |
| 1653 | } |
| 1654 | ASSERT(lev < cur->bc_nlevels); |
| 1655 | |
| 1656 | /* |
| 1657 | * Now walk back down the tree, fixing up the cursor's buffer |
| 1658 | * pointers and key numbers. |
| 1659 | */ |
| 1660 | for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) { |
| 1661 | union xfs_btree_ptr *ptrp; |
| 1662 | |
| 1663 | ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block); |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 1664 | --lev; |
| 1665 | error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp); |
Christoph Hellwig | 637aa50 | 2008-10-30 16:55:45 +1100 | [diff] [blame] | 1666 | if (error) |
| 1667 | goto error0; |
| 1668 | |
| 1669 | xfs_btree_setbuf(cur, lev, bp); |
| 1670 | cur->bc_ptrs[lev] = 1; |
| 1671 | } |
| 1672 | out1: |
| 1673 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1674 | *stat = 1; |
| 1675 | return 0; |
| 1676 | |
| 1677 | out0: |
| 1678 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1679 | *stat = 0; |
| 1680 | return 0; |
| 1681 | |
| 1682 | error0: |
| 1683 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1684 | return error; |
| 1685 | } |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1686 | |
| 1687 | /* |
| 1688 | * Decrement cursor by one record at the level. |
| 1689 | * For nonzero levels the leaf-ward information is untouched. |
| 1690 | */ |
| 1691 | int /* error */ |
| 1692 | xfs_btree_decrement( |
| 1693 | struct xfs_btree_cur *cur, |
| 1694 | int level, |
| 1695 | int *stat) /* success/failure */ |
| 1696 | { |
| 1697 | struct xfs_btree_block *block; |
| 1698 | xfs_buf_t *bp; |
| 1699 | int error; /* error return value */ |
| 1700 | int lev; |
| 1701 | union xfs_btree_ptr ptr; |
| 1702 | |
| 1703 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1704 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 1705 | |
| 1706 | ASSERT(level < cur->bc_nlevels); |
| 1707 | |
| 1708 | /* Read-ahead to the left at this level. */ |
| 1709 | xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA); |
| 1710 | |
| 1711 | /* We're done if we remain in the block after the decrement. */ |
| 1712 | if (--cur->bc_ptrs[level] > 0) |
| 1713 | goto out1; |
| 1714 | |
| 1715 | /* Get a pointer to the btree block. */ |
| 1716 | block = xfs_btree_get_block(cur, level, &bp); |
| 1717 | |
| 1718 | #ifdef DEBUG |
| 1719 | error = xfs_btree_check_block(cur, block, level, bp); |
| 1720 | if (error) |
| 1721 | goto error0; |
| 1722 | #endif |
| 1723 | |
| 1724 | /* Fail if we just went off the left edge of the tree. */ |
| 1725 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB); |
| 1726 | if (xfs_btree_ptr_is_null(cur, &ptr)) |
| 1727 | goto out0; |
| 1728 | |
| 1729 | XFS_BTREE_STATS_INC(cur, decrement); |
| 1730 | |
| 1731 | /* |
| 1732 | * March up the tree decrementing pointers. |
| 1733 | * Stop when we don't go off the left edge of a block. |
| 1734 | */ |
| 1735 | for (lev = level + 1; lev < cur->bc_nlevels; lev++) { |
| 1736 | if (--cur->bc_ptrs[lev] > 0) |
| 1737 | break; |
| 1738 | /* Read-ahead the left block for the next loop. */ |
| 1739 | xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA); |
| 1740 | } |
| 1741 | |
| 1742 | /* |
| 1743 | * If we went off the root then we are seriously confused. |
| 1744 | * or the root of the tree is in an inode. |
| 1745 | */ |
| 1746 | if (lev == cur->bc_nlevels) { |
| 1747 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) |
| 1748 | goto out0; |
| 1749 | ASSERT(0); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 1750 | error = -EFSCORRUPTED; |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1751 | goto error0; |
| 1752 | } |
| 1753 | ASSERT(lev < cur->bc_nlevels); |
| 1754 | |
| 1755 | /* |
| 1756 | * Now walk back down the tree, fixing up the cursor's buffer |
| 1757 | * pointers and key numbers. |
| 1758 | */ |
| 1759 | for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) { |
| 1760 | union xfs_btree_ptr *ptrp; |
| 1761 | |
| 1762 | ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block); |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 1763 | --lev; |
| 1764 | error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp); |
Christoph Hellwig | 8df4da4 | 2008-10-30 16:55:58 +1100 | [diff] [blame] | 1765 | if (error) |
| 1766 | goto error0; |
| 1767 | xfs_btree_setbuf(cur, lev, bp); |
| 1768 | cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block); |
| 1769 | } |
| 1770 | out1: |
| 1771 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1772 | *stat = 1; |
| 1773 | return 0; |
| 1774 | |
| 1775 | out0: |
| 1776 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1777 | *stat = 0; |
| 1778 | return 0; |
| 1779 | |
| 1780 | error0: |
| 1781 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 1782 | return error; |
| 1783 | } |
| 1784 | |
Darrick J. Wong | 2678809 | 2017-06-16 11:00:07 -0700 | [diff] [blame] | 1785 | int |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1786 | xfs_btree_lookup_get_block( |
| 1787 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1788 | int level, /* level in the btree */ |
| 1789 | union xfs_btree_ptr *pp, /* ptr to btree block */ |
| 1790 | struct xfs_btree_block **blkp) /* return btree block */ |
| 1791 | { |
| 1792 | struct xfs_buf *bp; /* buffer pointer for btree block */ |
| 1793 | int error = 0; |
| 1794 | |
| 1795 | /* special case the root block if in an inode */ |
| 1796 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 1797 | (level == cur->bc_nlevels - 1)) { |
| 1798 | *blkp = xfs_btree_get_iroot(cur); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * If the old buffer at this level for the disk address we are |
| 1804 | * looking for re-use it. |
| 1805 | * |
| 1806 | * Otherwise throw it away and get a new one. |
| 1807 | */ |
| 1808 | bp = cur->bc_bufs[level]; |
| 1809 | if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) { |
| 1810 | *blkp = XFS_BUF_TO_BLOCK(bp); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 1814 | error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp); |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1815 | if (error) |
| 1816 | return error; |
| 1817 | |
Darrick J. Wong | bb3be7e | 2016-12-05 12:33:54 +1100 | [diff] [blame] | 1818 | /* Check the inode owner since the verifiers don't. */ |
| 1819 | if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) && |
Brian Foster | 99c794c | 2017-08-29 10:08:39 -0700 | [diff] [blame] | 1820 | !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) && |
Darrick J. Wong | bb3be7e | 2016-12-05 12:33:54 +1100 | [diff] [blame] | 1821 | (cur->bc_flags & XFS_BTREE_LONG_PTRS) && |
| 1822 | be64_to_cpu((*blkp)->bb_u.l.bb_owner) != |
| 1823 | cur->bc_private.b.ip->i_ino) |
| 1824 | goto out_bad; |
| 1825 | |
| 1826 | /* Did we get the level we were looking for? */ |
| 1827 | if (be16_to_cpu((*blkp)->bb_level) != level) |
| 1828 | goto out_bad; |
| 1829 | |
| 1830 | /* Check that internal nodes have at least one record. */ |
| 1831 | if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0) |
| 1832 | goto out_bad; |
| 1833 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1834 | xfs_btree_setbuf(cur, level, bp); |
| 1835 | return 0; |
Darrick J. Wong | bb3be7e | 2016-12-05 12:33:54 +1100 | [diff] [blame] | 1836 | |
| 1837 | out_bad: |
| 1838 | *blkp = NULL; |
| 1839 | xfs_trans_brelse(cur->bc_tp, bp); |
| 1840 | return -EFSCORRUPTED; |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Get current search key. For level 0 we don't actually have a key |
| 1845 | * structure so we make one up from the record. For all other levels |
| 1846 | * we just return the right key. |
| 1847 | */ |
| 1848 | STATIC union xfs_btree_key * |
| 1849 | xfs_lookup_get_search_key( |
| 1850 | struct xfs_btree_cur *cur, |
| 1851 | int level, |
| 1852 | int keyno, |
| 1853 | struct xfs_btree_block *block, |
| 1854 | union xfs_btree_key *kp) |
| 1855 | { |
| 1856 | if (level == 0) { |
| 1857 | cur->bc_ops->init_key_from_rec(kp, |
| 1858 | xfs_btree_rec_addr(cur, keyno, block)); |
| 1859 | return kp; |
| 1860 | } |
| 1861 | |
| 1862 | return xfs_btree_key_addr(cur, keyno, block); |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Lookup the record. The cursor is made to point to it, based on dir. |
Zhi Yong Wu | 49d3da1 | 2013-08-07 10:11:00 +0000 | [diff] [blame] | 1867 | * stat is set to 0 if can't find any such record, 1 for success. |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1868 | */ |
| 1869 | int /* error */ |
| 1870 | xfs_btree_lookup( |
| 1871 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 1872 | xfs_lookup_t dir, /* <=, ==, or >= */ |
| 1873 | int *stat) /* success/failure */ |
| 1874 | { |
| 1875 | struct xfs_btree_block *block; /* current btree block */ |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 1876 | int64_t diff; /* difference for the current key */ |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1877 | int error; /* error return value */ |
| 1878 | int keyno; /* current key number */ |
| 1879 | int level; /* level in the btree */ |
| 1880 | union xfs_btree_ptr *pp; /* ptr to btree block */ |
| 1881 | union xfs_btree_ptr ptr; /* ptr to btree block */ |
| 1882 | |
| 1883 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 1884 | XFS_BTREE_TRACE_ARGI(cur, dir); |
| 1885 | |
| 1886 | XFS_BTREE_STATS_INC(cur, lookup); |
| 1887 | |
Darrick J. Wong | ed150e1 | 2016-08-26 15:58:40 +1000 | [diff] [blame] | 1888 | /* No such thing as a zero-level tree. */ |
| 1889 | if (cur->bc_nlevels == 0) |
| 1890 | return -EFSCORRUPTED; |
| 1891 | |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 1892 | block = NULL; |
| 1893 | keyno = 0; |
| 1894 | |
| 1895 | /* initialise start pointer from cursor */ |
| 1896 | cur->bc_ops->init_ptr_from_cur(cur, &ptr); |
| 1897 | pp = &ptr; |
| 1898 | |
| 1899 | /* |
| 1900 | * Iterate over each level in the btree, starting at the root. |
| 1901 | * For each level above the leaves, find the key we need, based |
| 1902 | * on the lookup record, then follow the corresponding block |
| 1903 | * pointer down to the next level. |
| 1904 | */ |
| 1905 | for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) { |
| 1906 | /* Get the block we need to do the lookup on. */ |
| 1907 | error = xfs_btree_lookup_get_block(cur, level, pp, &block); |
| 1908 | if (error) |
| 1909 | goto error0; |
| 1910 | |
| 1911 | if (diff == 0) { |
| 1912 | /* |
| 1913 | * If we already had a key match at a higher level, we |
| 1914 | * know we need to use the first entry in this block. |
| 1915 | */ |
| 1916 | keyno = 1; |
| 1917 | } else { |
| 1918 | /* Otherwise search this block. Do a binary search. */ |
| 1919 | |
| 1920 | int high; /* high entry number */ |
| 1921 | int low; /* low entry number */ |
| 1922 | |
| 1923 | /* Set low and high entry numbers, 1-based. */ |
| 1924 | low = 1; |
| 1925 | high = xfs_btree_get_numrecs(block); |
| 1926 | if (!high) { |
| 1927 | /* Block is empty, must be an empty leaf. */ |
| 1928 | ASSERT(level == 0 && cur->bc_nlevels == 1); |
| 1929 | |
| 1930 | cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE; |
| 1931 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 1932 | *stat = 0; |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | /* Binary search the block. */ |
| 1937 | while (low <= high) { |
| 1938 | union xfs_btree_key key; |
| 1939 | union xfs_btree_key *kp; |
| 1940 | |
| 1941 | XFS_BTREE_STATS_INC(cur, compare); |
| 1942 | |
| 1943 | /* keyno is average of low and high. */ |
| 1944 | keyno = (low + high) >> 1; |
| 1945 | |
| 1946 | /* Get current search key */ |
| 1947 | kp = xfs_lookup_get_search_key(cur, level, |
| 1948 | keyno, block, &key); |
| 1949 | |
| 1950 | /* |
| 1951 | * Compute difference to get next direction: |
| 1952 | * - less than, move right |
| 1953 | * - greater than, move left |
| 1954 | * - equal, we're done |
| 1955 | */ |
| 1956 | diff = cur->bc_ops->key_diff(cur, kp); |
| 1957 | if (diff < 0) |
| 1958 | low = keyno + 1; |
| 1959 | else if (diff > 0) |
| 1960 | high = keyno - 1; |
| 1961 | else |
| 1962 | break; |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | /* |
| 1967 | * If there are more levels, set up for the next level |
| 1968 | * by getting the block number and filling in the cursor. |
| 1969 | */ |
| 1970 | if (level > 0) { |
| 1971 | /* |
| 1972 | * If we moved left, need the previous key number, |
| 1973 | * unless there isn't one. |
| 1974 | */ |
| 1975 | if (diff > 0 && --keyno < 1) |
| 1976 | keyno = 1; |
| 1977 | pp = xfs_btree_ptr_addr(cur, keyno, block); |
| 1978 | |
| 1979 | #ifdef DEBUG |
| 1980 | error = xfs_btree_check_ptr(cur, pp, 0, level); |
| 1981 | if (error) |
| 1982 | goto error0; |
| 1983 | #endif |
| 1984 | cur->bc_ptrs[level] = keyno; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | /* Done with the search. See if we need to adjust the results. */ |
| 1989 | if (dir != XFS_LOOKUP_LE && diff < 0) { |
| 1990 | keyno++; |
| 1991 | /* |
| 1992 | * If ge search and we went off the end of the block, but it's |
| 1993 | * not the last block, we're in the wrong block. |
| 1994 | */ |
| 1995 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 1996 | if (dir == XFS_LOOKUP_GE && |
| 1997 | keyno > xfs_btree_get_numrecs(block) && |
| 1998 | !xfs_btree_ptr_is_null(cur, &ptr)) { |
| 1999 | int i; |
| 2000 | |
| 2001 | cur->bc_ptrs[0] = keyno; |
| 2002 | error = xfs_btree_increment(cur, 0, &i); |
| 2003 | if (error) |
| 2004 | goto error0; |
Eric Sandeen | 5fb5aee | 2015-02-23 22:39:13 +1100 | [diff] [blame] | 2005 | XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1); |
Christoph Hellwig | fe033cc | 2008-10-30 16:56:09 +1100 | [diff] [blame] | 2006 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2007 | *stat = 1; |
| 2008 | return 0; |
| 2009 | } |
| 2010 | } else if (dir == XFS_LOOKUP_LE && diff > 0) |
| 2011 | keyno--; |
| 2012 | cur->bc_ptrs[0] = keyno; |
| 2013 | |
| 2014 | /* Return if we succeeded or not. */ |
| 2015 | if (keyno == 0 || keyno > xfs_btree_get_numrecs(block)) |
| 2016 | *stat = 0; |
| 2017 | else if (dir != XFS_LOOKUP_EQ || diff == 0) |
| 2018 | *stat = 1; |
| 2019 | else |
| 2020 | *stat = 0; |
| 2021 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2022 | return 0; |
| 2023 | |
| 2024 | error0: |
| 2025 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2026 | return error; |
| 2027 | } |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2028 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2029 | /* Find the high key storage area from a regular key. */ |
| 2030 | STATIC union xfs_btree_key * |
| 2031 | xfs_btree_high_key_from_key( |
| 2032 | struct xfs_btree_cur *cur, |
| 2033 | union xfs_btree_key *key) |
| 2034 | { |
| 2035 | ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING); |
| 2036 | return (union xfs_btree_key *)((char *)key + |
| 2037 | (cur->bc_ops->key_len / 2)); |
| 2038 | } |
| 2039 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2040 | /* Determine the low (and high if overlapped) keys of a leaf block */ |
| 2041 | STATIC void |
| 2042 | xfs_btree_get_leaf_keys( |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2043 | struct xfs_btree_cur *cur, |
| 2044 | struct xfs_btree_block *block, |
| 2045 | union xfs_btree_key *key) |
| 2046 | { |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2047 | union xfs_btree_key max_hkey; |
| 2048 | union xfs_btree_key hkey; |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2049 | union xfs_btree_rec *rec; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2050 | union xfs_btree_key *high; |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2051 | int n; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2052 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2053 | rec = xfs_btree_rec_addr(cur, 1, block); |
| 2054 | cur->bc_ops->init_key_from_rec(key, rec); |
| 2055 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2056 | if (cur->bc_flags & XFS_BTREE_OVERLAPPING) { |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2057 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2058 | cur->bc_ops->init_high_key_from_rec(&max_hkey, rec); |
| 2059 | for (n = 2; n <= xfs_btree_get_numrecs(block); n++) { |
| 2060 | rec = xfs_btree_rec_addr(cur, n, block); |
| 2061 | cur->bc_ops->init_high_key_from_rec(&hkey, rec); |
| 2062 | if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey) |
| 2063 | > 0) |
| 2064 | max_hkey = hkey; |
| 2065 | } |
| 2066 | |
| 2067 | high = xfs_btree_high_key_from_key(cur, key); |
| 2068 | memcpy(high, &max_hkey, cur->bc_ops->key_len / 2); |
| 2069 | } |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2070 | } |
| 2071 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2072 | /* Determine the low (and high if overlapped) keys of a node block */ |
| 2073 | STATIC void |
| 2074 | xfs_btree_get_node_keys( |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2075 | struct xfs_btree_cur *cur, |
| 2076 | struct xfs_btree_block *block, |
| 2077 | union xfs_btree_key *key) |
| 2078 | { |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2079 | union xfs_btree_key *hkey; |
| 2080 | union xfs_btree_key *max_hkey; |
| 2081 | union xfs_btree_key *high; |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2082 | int n; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2083 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2084 | if (cur->bc_flags & XFS_BTREE_OVERLAPPING) { |
| 2085 | memcpy(key, xfs_btree_key_addr(cur, 1, block), |
| 2086 | cur->bc_ops->key_len / 2); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2087 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2088 | max_hkey = xfs_btree_high_key_addr(cur, 1, block); |
| 2089 | for (n = 2; n <= xfs_btree_get_numrecs(block); n++) { |
| 2090 | hkey = xfs_btree_high_key_addr(cur, n, block); |
| 2091 | if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0) |
| 2092 | max_hkey = hkey; |
| 2093 | } |
| 2094 | |
| 2095 | high = xfs_btree_high_key_from_key(cur, key); |
| 2096 | memcpy(high, max_hkey, cur->bc_ops->key_len / 2); |
| 2097 | } else { |
| 2098 | memcpy(key, xfs_btree_key_addr(cur, 1, block), |
| 2099 | cur->bc_ops->key_len); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2100 | } |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2101 | } |
| 2102 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2103 | /* Derive the keys for any btree block. */ |
| 2104 | STATIC void |
| 2105 | xfs_btree_get_keys( |
| 2106 | struct xfs_btree_cur *cur, |
| 2107 | struct xfs_btree_block *block, |
| 2108 | union xfs_btree_key *key) |
| 2109 | { |
| 2110 | if (be16_to_cpu(block->bb_level) == 0) |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2111 | xfs_btree_get_leaf_keys(cur, block, key); |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2112 | else |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2113 | xfs_btree_get_node_keys(cur, block, key); |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * Decide if we need to update the parent keys of a btree block. For |
| 2118 | * a standard btree this is only necessary if we're updating the first |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2119 | * record/key. For an overlapping btree, we must always update the |
| 2120 | * keys because the highest key can be in any of the records or keys |
| 2121 | * in the block. |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2122 | */ |
| 2123 | static inline bool |
| 2124 | xfs_btree_needs_key_update( |
| 2125 | struct xfs_btree_cur *cur, |
| 2126 | int ptr) |
| 2127 | { |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2128 | return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1; |
| 2129 | } |
| 2130 | |
| 2131 | /* |
| 2132 | * Update the low and high parent keys of the given level, progressing |
| 2133 | * towards the root. If force_all is false, stop if the keys for a given |
| 2134 | * level do not need updating. |
| 2135 | */ |
| 2136 | STATIC int |
| 2137 | __xfs_btree_updkeys( |
| 2138 | struct xfs_btree_cur *cur, |
| 2139 | int level, |
| 2140 | struct xfs_btree_block *block, |
| 2141 | struct xfs_buf *bp0, |
| 2142 | bool force_all) |
| 2143 | { |
Darrick J. Wong | a1d46cf | 2016-09-19 10:24:36 +1000 | [diff] [blame] | 2144 | union xfs_btree_key key; /* keys from current level */ |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2145 | union xfs_btree_key *lkey; /* keys from the next level up */ |
| 2146 | union xfs_btree_key *hkey; |
| 2147 | union xfs_btree_key *nlkey; /* keys from the next level up */ |
| 2148 | union xfs_btree_key *nhkey; |
| 2149 | struct xfs_buf *bp; |
| 2150 | int ptr; |
| 2151 | |
| 2152 | ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING); |
| 2153 | |
| 2154 | /* Exit if there aren't any parent levels to update. */ |
| 2155 | if (level + 1 >= cur->bc_nlevels) |
| 2156 | return 0; |
| 2157 | |
| 2158 | trace_xfs_btree_updkeys(cur, level, bp0); |
| 2159 | |
Darrick J. Wong | a1d46cf | 2016-09-19 10:24:36 +1000 | [diff] [blame] | 2160 | lkey = &key; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2161 | hkey = xfs_btree_high_key_from_key(cur, lkey); |
| 2162 | xfs_btree_get_keys(cur, block, lkey); |
| 2163 | for (level++; level < cur->bc_nlevels; level++) { |
| 2164 | #ifdef DEBUG |
| 2165 | int error; |
| 2166 | #endif |
| 2167 | block = xfs_btree_get_block(cur, level, &bp); |
| 2168 | trace_xfs_btree_updkeys(cur, level, bp); |
| 2169 | #ifdef DEBUG |
| 2170 | error = xfs_btree_check_block(cur, block, level, bp); |
| 2171 | if (error) { |
| 2172 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2173 | return error; |
| 2174 | } |
| 2175 | #endif |
| 2176 | ptr = cur->bc_ptrs[level]; |
| 2177 | nlkey = xfs_btree_key_addr(cur, ptr, block); |
| 2178 | nhkey = xfs_btree_high_key_addr(cur, ptr, block); |
| 2179 | if (!force_all && |
| 2180 | !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 || |
| 2181 | cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0)) |
| 2182 | break; |
| 2183 | xfs_btree_copy_keys(cur, nlkey, lkey, 1); |
| 2184 | xfs_btree_log_keys(cur, bp, ptr, ptr); |
| 2185 | if (level + 1 >= cur->bc_nlevels) |
| 2186 | break; |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2187 | xfs_btree_get_node_keys(cur, block, lkey); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2193 | /* Update all the keys from some level in cursor back to the root. */ |
| 2194 | STATIC int |
| 2195 | xfs_btree_updkeys_force( |
| 2196 | struct xfs_btree_cur *cur, |
| 2197 | int level) |
| 2198 | { |
| 2199 | struct xfs_buf *bp; |
| 2200 | struct xfs_btree_block *block; |
| 2201 | |
| 2202 | block = xfs_btree_get_block(cur, level, &bp); |
| 2203 | return __xfs_btree_updkeys(cur, level, block, bp, true); |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* |
| 2207 | * Update the parent keys of the given level, progressing towards the root. |
| 2208 | */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2209 | STATIC int |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2210 | xfs_btree_update_keys( |
| 2211 | struct xfs_btree_cur *cur, |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2212 | int level) |
| 2213 | { |
| 2214 | struct xfs_btree_block *block; |
| 2215 | struct xfs_buf *bp; |
| 2216 | union xfs_btree_key *kp; |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2217 | union xfs_btree_key key; |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2218 | int ptr; |
| 2219 | |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2220 | ASSERT(level >= 0); |
| 2221 | |
| 2222 | block = xfs_btree_get_block(cur, level, &bp); |
| 2223 | if (cur->bc_flags & XFS_BTREE_OVERLAPPING) |
| 2224 | return __xfs_btree_updkeys(cur, level, block, bp, false); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2225 | |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2226 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2227 | XFS_BTREE_TRACE_ARGIK(cur, level, keyp); |
| 2228 | |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2229 | /* |
| 2230 | * Go up the tree from this level toward the root. |
| 2231 | * At each level, update the key value to the value input. |
| 2232 | * Stop when we reach a level where the cursor isn't pointing |
| 2233 | * at the first entry in the block. |
| 2234 | */ |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2235 | xfs_btree_get_keys(cur, block, &key); |
| 2236 | for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) { |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2237 | #ifdef DEBUG |
| 2238 | int error; |
| 2239 | #endif |
| 2240 | block = xfs_btree_get_block(cur, level, &bp); |
| 2241 | #ifdef DEBUG |
| 2242 | error = xfs_btree_check_block(cur, block, level, bp); |
| 2243 | if (error) { |
| 2244 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2245 | return error; |
| 2246 | } |
| 2247 | #endif |
| 2248 | ptr = cur->bc_ptrs[level]; |
| 2249 | kp = xfs_btree_key_addr(cur, ptr, block); |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2250 | xfs_btree_copy_keys(cur, kp, &key, 1); |
Christoph Hellwig | 38bb742 | 2008-10-30 16:56:22 +1100 | [diff] [blame] | 2251 | xfs_btree_log_keys(cur, bp, ptr, ptr); |
| 2252 | } |
| 2253 | |
| 2254 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2255 | return 0; |
| 2256 | } |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 2257 | |
| 2258 | /* |
| 2259 | * Update the record referred to by cur to the value in the |
| 2260 | * given record. This either works (return 0) or gets an |
| 2261 | * EFSCORRUPTED error. |
| 2262 | */ |
| 2263 | int |
| 2264 | xfs_btree_update( |
| 2265 | struct xfs_btree_cur *cur, |
| 2266 | union xfs_btree_rec *rec) |
| 2267 | { |
| 2268 | struct xfs_btree_block *block; |
| 2269 | struct xfs_buf *bp; |
| 2270 | int error; |
| 2271 | int ptr; |
| 2272 | union xfs_btree_rec *rp; |
| 2273 | |
| 2274 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2275 | XFS_BTREE_TRACE_ARGR(cur, rec); |
| 2276 | |
| 2277 | /* Pick up the current block. */ |
| 2278 | block = xfs_btree_get_block(cur, 0, &bp); |
| 2279 | |
| 2280 | #ifdef DEBUG |
| 2281 | error = xfs_btree_check_block(cur, block, 0, bp); |
| 2282 | if (error) |
| 2283 | goto error0; |
| 2284 | #endif |
| 2285 | /* Get the address of the rec to be updated. */ |
| 2286 | ptr = cur->bc_ptrs[0]; |
| 2287 | rp = xfs_btree_rec_addr(cur, ptr, block); |
| 2288 | |
| 2289 | /* Fill in the new contents and log them. */ |
| 2290 | xfs_btree_copy_recs(cur, rp, rec, 1); |
| 2291 | xfs_btree_log_recs(cur, bp, ptr, ptr); |
| 2292 | |
| 2293 | /* |
| 2294 | * If we are tracking the last record in the tree and |
| 2295 | * we are at the far right edge of the tree, update it. |
| 2296 | */ |
| 2297 | if (xfs_btree_is_lastrec(cur, block, 0)) { |
| 2298 | cur->bc_ops->update_lastrec(cur, block, rec, |
| 2299 | ptr, LASTREC_UPDATE); |
| 2300 | } |
| 2301 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2302 | /* Pass new key value up to our parent. */ |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2303 | if (xfs_btree_needs_key_update(cur, ptr)) { |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2304 | error = xfs_btree_update_keys(cur, 0); |
Christoph Hellwig | 278d0ca | 2008-10-30 16:56:32 +1100 | [diff] [blame] | 2305 | if (error) |
| 2306 | goto error0; |
| 2307 | } |
| 2308 | |
| 2309 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2310 | return 0; |
| 2311 | |
| 2312 | error0: |
| 2313 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2314 | return error; |
| 2315 | } |
| 2316 | |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2317 | /* |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2318 | * Move 1 record left from cur/level if possible. |
| 2319 | * Update cur to reflect the new path. |
| 2320 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 2321 | STATIC int /* error */ |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2322 | xfs_btree_lshift( |
| 2323 | struct xfs_btree_cur *cur, |
| 2324 | int level, |
| 2325 | int *stat) /* success/failure */ |
| 2326 | { |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2327 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 2328 | struct xfs_btree_block *left; /* left btree block */ |
| 2329 | int lrecs; /* left record count */ |
| 2330 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 2331 | struct xfs_btree_block *right; /* right btree block */ |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2332 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2333 | int rrecs; /* right record count */ |
| 2334 | union xfs_btree_ptr lptr; /* left btree pointer */ |
| 2335 | union xfs_btree_key *rkp = NULL; /* right btree key */ |
| 2336 | union xfs_btree_ptr *rpp = NULL; /* right address pointer */ |
| 2337 | union xfs_btree_rec *rrp = NULL; /* right record pointer */ |
| 2338 | int error; /* error return value */ |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2339 | int i; |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2340 | |
| 2341 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2342 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 2343 | |
| 2344 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 2345 | level == cur->bc_nlevels - 1) |
| 2346 | goto out0; |
| 2347 | |
| 2348 | /* Set up variables for this block as "right". */ |
| 2349 | right = xfs_btree_get_block(cur, level, &rbp); |
| 2350 | |
| 2351 | #ifdef DEBUG |
| 2352 | error = xfs_btree_check_block(cur, right, level, rbp); |
| 2353 | if (error) |
| 2354 | goto error0; |
| 2355 | #endif |
| 2356 | |
| 2357 | /* If we've got no left sibling then we can't shift an entry left. */ |
| 2358 | xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); |
| 2359 | if (xfs_btree_ptr_is_null(cur, &lptr)) |
| 2360 | goto out0; |
| 2361 | |
| 2362 | /* |
| 2363 | * If the cursor entry is the one that would be moved, don't |
| 2364 | * do it... it's too complicated. |
| 2365 | */ |
| 2366 | if (cur->bc_ptrs[level] <= 1) |
| 2367 | goto out0; |
| 2368 | |
| 2369 | /* Set up the left neighbor as "left". */ |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 2370 | error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2371 | if (error) |
| 2372 | goto error0; |
| 2373 | |
| 2374 | /* If it's full, it can't take another entry. */ |
| 2375 | lrecs = xfs_btree_get_numrecs(left); |
| 2376 | if (lrecs == cur->bc_ops->get_maxrecs(cur, level)) |
| 2377 | goto out0; |
| 2378 | |
| 2379 | rrecs = xfs_btree_get_numrecs(right); |
| 2380 | |
| 2381 | /* |
| 2382 | * We add one entry to the left side and remove one for the right side. |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 2383 | * Account for it here, the changes will be updated on disk and logged |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2384 | * later. |
| 2385 | */ |
| 2386 | lrecs++; |
| 2387 | rrecs--; |
| 2388 | |
| 2389 | XFS_BTREE_STATS_INC(cur, lshift); |
| 2390 | XFS_BTREE_STATS_ADD(cur, moves, 1); |
| 2391 | |
| 2392 | /* |
| 2393 | * If non-leaf, copy a key and a ptr to the left block. |
| 2394 | * Log the changes to the left block. |
| 2395 | */ |
| 2396 | if (level > 0) { |
| 2397 | /* It's a non-leaf. Move keys and pointers. */ |
| 2398 | union xfs_btree_key *lkp; /* left btree key */ |
| 2399 | union xfs_btree_ptr *lpp; /* left address pointer */ |
| 2400 | |
| 2401 | lkp = xfs_btree_key_addr(cur, lrecs, left); |
| 2402 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 2403 | |
| 2404 | lpp = xfs_btree_ptr_addr(cur, lrecs, left); |
| 2405 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 2406 | #ifdef DEBUG |
| 2407 | error = xfs_btree_check_ptr(cur, rpp, 0, level); |
| 2408 | if (error) |
| 2409 | goto error0; |
| 2410 | #endif |
| 2411 | xfs_btree_copy_keys(cur, lkp, rkp, 1); |
| 2412 | xfs_btree_copy_ptrs(cur, lpp, rpp, 1); |
| 2413 | |
| 2414 | xfs_btree_log_keys(cur, lbp, lrecs, lrecs); |
| 2415 | xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs); |
| 2416 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2417 | ASSERT(cur->bc_ops->keys_inorder(cur, |
| 2418 | xfs_btree_key_addr(cur, lrecs - 1, left), lkp)); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2419 | } else { |
| 2420 | /* It's a leaf. Move records. */ |
| 2421 | union xfs_btree_rec *lrp; /* left record pointer */ |
| 2422 | |
| 2423 | lrp = xfs_btree_rec_addr(cur, lrecs, left); |
| 2424 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 2425 | |
| 2426 | xfs_btree_copy_recs(cur, lrp, rrp, 1); |
| 2427 | xfs_btree_log_recs(cur, lbp, lrecs, lrecs); |
| 2428 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2429 | ASSERT(cur->bc_ops->recs_inorder(cur, |
| 2430 | xfs_btree_rec_addr(cur, lrecs - 1, left), lrp)); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | xfs_btree_set_numrecs(left, lrecs); |
| 2434 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS); |
| 2435 | |
| 2436 | xfs_btree_set_numrecs(right, rrecs); |
| 2437 | xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS); |
| 2438 | |
| 2439 | /* |
| 2440 | * Slide the contents of right down one entry. |
| 2441 | */ |
| 2442 | XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1); |
| 2443 | if (level > 0) { |
| 2444 | /* It's a nonleaf. operate on keys and ptrs */ |
| 2445 | #ifdef DEBUG |
| 2446 | int i; /* loop index */ |
| 2447 | |
| 2448 | for (i = 0; i < rrecs; i++) { |
| 2449 | error = xfs_btree_check_ptr(cur, rpp, i + 1, level); |
| 2450 | if (error) |
| 2451 | goto error0; |
| 2452 | } |
| 2453 | #endif |
| 2454 | xfs_btree_shift_keys(cur, |
| 2455 | xfs_btree_key_addr(cur, 2, right), |
| 2456 | -1, rrecs); |
| 2457 | xfs_btree_shift_ptrs(cur, |
| 2458 | xfs_btree_ptr_addr(cur, 2, right), |
| 2459 | -1, rrecs); |
| 2460 | |
| 2461 | xfs_btree_log_keys(cur, rbp, 1, rrecs); |
| 2462 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs); |
| 2463 | } else { |
| 2464 | /* It's a leaf. operate on records */ |
| 2465 | xfs_btree_shift_recs(cur, |
| 2466 | xfs_btree_rec_addr(cur, 2, right), |
| 2467 | -1, rrecs); |
| 2468 | xfs_btree_log_recs(cur, rbp, 1, rrecs); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2469 | } |
| 2470 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2471 | /* |
| 2472 | * Using a temporary cursor, update the parent key values of the |
| 2473 | * block on the left. |
| 2474 | */ |
Darrick J. Wong | c1d22ae | 2016-08-03 12:26:22 +1000 | [diff] [blame] | 2475 | if (cur->bc_flags & XFS_BTREE_OVERLAPPING) { |
| 2476 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 2477 | if (error) |
| 2478 | goto error0; |
| 2479 | i = xfs_btree_firstrec(tcur, level); |
| 2480 | XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2481 | |
Darrick J. Wong | c1d22ae | 2016-08-03 12:26:22 +1000 | [diff] [blame] | 2482 | error = xfs_btree_decrement(tcur, level, &i); |
| 2483 | if (error) |
| 2484 | goto error1; |
| 2485 | |
| 2486 | /* Update the parent high keys of the left block, if needed. */ |
| 2487 | error = xfs_btree_update_keys(tcur, level); |
| 2488 | if (error) |
| 2489 | goto error1; |
| 2490 | |
| 2491 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 2492 | } |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2493 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2494 | /* Update the parent keys of the right block. */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2495 | error = xfs_btree_update_keys(cur, level); |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2496 | if (error) |
Darrick J. Wong | c1d22ae | 2016-08-03 12:26:22 +1000 | [diff] [blame] | 2497 | goto error0; |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2498 | |
| 2499 | /* Slide the cursor value left one. */ |
| 2500 | cur->bc_ptrs[level]--; |
| 2501 | |
| 2502 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2503 | *stat = 1; |
| 2504 | return 0; |
| 2505 | |
| 2506 | out0: |
| 2507 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2508 | *stat = 0; |
| 2509 | return 0; |
| 2510 | |
| 2511 | error0: |
| 2512 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2513 | return error; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2514 | |
| 2515 | error1: |
| 2516 | XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR); |
| 2517 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 2518 | return error; |
Christoph Hellwig | 687b890 | 2008-10-30 16:56:53 +1100 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | /* |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2522 | * Move 1 record right from cur/level if possible. |
| 2523 | * Update cur to reflect the new path. |
| 2524 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 2525 | STATIC int /* error */ |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2526 | xfs_btree_rshift( |
| 2527 | struct xfs_btree_cur *cur, |
| 2528 | int level, |
| 2529 | int *stat) /* success/failure */ |
| 2530 | { |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2531 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 2532 | struct xfs_btree_block *left; /* left btree block */ |
| 2533 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 2534 | struct xfs_btree_block *right; /* right btree block */ |
| 2535 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ |
| 2536 | union xfs_btree_ptr rptr; /* right block pointer */ |
| 2537 | union xfs_btree_key *rkp; /* right btree key */ |
| 2538 | int rrecs; /* right record count */ |
| 2539 | int lrecs; /* left record count */ |
| 2540 | int error; /* error return value */ |
| 2541 | int i; /* loop counter */ |
| 2542 | |
| 2543 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2544 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 2545 | |
| 2546 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 2547 | (level == cur->bc_nlevels - 1)) |
| 2548 | goto out0; |
| 2549 | |
| 2550 | /* Set up variables for this block as "left". */ |
| 2551 | left = xfs_btree_get_block(cur, level, &lbp); |
| 2552 | |
| 2553 | #ifdef DEBUG |
| 2554 | error = xfs_btree_check_block(cur, left, level, lbp); |
| 2555 | if (error) |
| 2556 | goto error0; |
| 2557 | #endif |
| 2558 | |
| 2559 | /* If we've got no right sibling then we can't shift an entry right. */ |
| 2560 | xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB); |
| 2561 | if (xfs_btree_ptr_is_null(cur, &rptr)) |
| 2562 | goto out0; |
| 2563 | |
| 2564 | /* |
| 2565 | * If the cursor entry is the one that would be moved, don't |
| 2566 | * do it... it's too complicated. |
| 2567 | */ |
| 2568 | lrecs = xfs_btree_get_numrecs(left); |
| 2569 | if (cur->bc_ptrs[level] >= lrecs) |
| 2570 | goto out0; |
| 2571 | |
| 2572 | /* Set up the right neighbor as "right". */ |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 2573 | error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2574 | if (error) |
| 2575 | goto error0; |
| 2576 | |
| 2577 | /* If it's full, it can't take another entry. */ |
| 2578 | rrecs = xfs_btree_get_numrecs(right); |
| 2579 | if (rrecs == cur->bc_ops->get_maxrecs(cur, level)) |
| 2580 | goto out0; |
| 2581 | |
| 2582 | XFS_BTREE_STATS_INC(cur, rshift); |
| 2583 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); |
| 2584 | |
| 2585 | /* |
| 2586 | * Make a hole at the start of the right neighbor block, then |
| 2587 | * copy the last left block entry to the hole. |
| 2588 | */ |
| 2589 | if (level > 0) { |
| 2590 | /* It's a nonleaf. make a hole in the keys and ptrs */ |
| 2591 | union xfs_btree_key *lkp; |
| 2592 | union xfs_btree_ptr *lpp; |
| 2593 | union xfs_btree_ptr *rpp; |
| 2594 | |
| 2595 | lkp = xfs_btree_key_addr(cur, lrecs, left); |
| 2596 | lpp = xfs_btree_ptr_addr(cur, lrecs, left); |
| 2597 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 2598 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 2599 | |
| 2600 | #ifdef DEBUG |
| 2601 | for (i = rrecs - 1; i >= 0; i--) { |
| 2602 | error = xfs_btree_check_ptr(cur, rpp, i, level); |
| 2603 | if (error) |
| 2604 | goto error0; |
| 2605 | } |
| 2606 | #endif |
| 2607 | |
| 2608 | xfs_btree_shift_keys(cur, rkp, 1, rrecs); |
| 2609 | xfs_btree_shift_ptrs(cur, rpp, 1, rrecs); |
| 2610 | |
| 2611 | #ifdef DEBUG |
| 2612 | error = xfs_btree_check_ptr(cur, lpp, 0, level); |
| 2613 | if (error) |
| 2614 | goto error0; |
| 2615 | #endif |
| 2616 | |
| 2617 | /* Now put the new data in, and log it. */ |
| 2618 | xfs_btree_copy_keys(cur, rkp, lkp, 1); |
| 2619 | xfs_btree_copy_ptrs(cur, rpp, lpp, 1); |
| 2620 | |
| 2621 | xfs_btree_log_keys(cur, rbp, 1, rrecs + 1); |
| 2622 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1); |
| 2623 | |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 2624 | ASSERT(cur->bc_ops->keys_inorder(cur, rkp, |
| 2625 | xfs_btree_key_addr(cur, 2, right))); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2626 | } else { |
| 2627 | /* It's a leaf. make a hole in the records */ |
| 2628 | union xfs_btree_rec *lrp; |
| 2629 | union xfs_btree_rec *rrp; |
| 2630 | |
| 2631 | lrp = xfs_btree_rec_addr(cur, lrecs, left); |
| 2632 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 2633 | |
| 2634 | xfs_btree_shift_recs(cur, rrp, 1, rrecs); |
| 2635 | |
| 2636 | /* Now put the new data in, and log it. */ |
| 2637 | xfs_btree_copy_recs(cur, rrp, lrp, 1); |
| 2638 | xfs_btree_log_recs(cur, rbp, 1, rrecs + 1); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | /* |
| 2642 | * Decrement and log left's numrecs, bump and log right's numrecs. |
| 2643 | */ |
| 2644 | xfs_btree_set_numrecs(left, --lrecs); |
| 2645 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS); |
| 2646 | |
| 2647 | xfs_btree_set_numrecs(right, ++rrecs); |
| 2648 | xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS); |
| 2649 | |
| 2650 | /* |
| 2651 | * Using a temporary cursor, update the parent key values of the |
| 2652 | * block on the right. |
| 2653 | */ |
| 2654 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 2655 | if (error) |
| 2656 | goto error0; |
| 2657 | i = xfs_btree_lastrec(tcur, level); |
Darrick J. Wong | c1d22ae | 2016-08-03 12:26:22 +1000 | [diff] [blame] | 2658 | XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2659 | |
| 2660 | error = xfs_btree_increment(tcur, level, &i); |
| 2661 | if (error) |
| 2662 | goto error1; |
| 2663 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2664 | /* Update the parent high keys of the left block, if needed. */ |
| 2665 | if (cur->bc_flags & XFS_BTREE_OVERLAPPING) { |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2666 | error = xfs_btree_update_keys(cur, level); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2667 | if (error) |
| 2668 | goto error1; |
| 2669 | } |
| 2670 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2671 | /* Update the parent keys of the right block. */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2672 | error = xfs_btree_update_keys(tcur, level); |
Christoph Hellwig | 9eaead5 | 2008-10-30 16:56:43 +1100 | [diff] [blame] | 2673 | if (error) |
| 2674 | goto error1; |
| 2675 | |
| 2676 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 2677 | |
| 2678 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2679 | *stat = 1; |
| 2680 | return 0; |
| 2681 | |
| 2682 | out0: |
| 2683 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2684 | *stat = 0; |
| 2685 | return 0; |
| 2686 | |
| 2687 | error0: |
| 2688 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2689 | return error; |
| 2690 | |
| 2691 | error1: |
| 2692 | XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR); |
| 2693 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 2694 | return error; |
| 2695 | } |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2696 | |
| 2697 | /* |
| 2698 | * Split cur/level block in half. |
| 2699 | * Return new block number and the key to its first |
| 2700 | * record (to be inserted into parent). |
| 2701 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 2702 | STATIC int /* error */ |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 2703 | __xfs_btree_split( |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2704 | struct xfs_btree_cur *cur, |
| 2705 | int level, |
| 2706 | union xfs_btree_ptr *ptrp, |
| 2707 | union xfs_btree_key *key, |
| 2708 | struct xfs_btree_cur **curp, |
| 2709 | int *stat) /* success/failure */ |
| 2710 | { |
| 2711 | union xfs_btree_ptr lptr; /* left sibling block ptr */ |
| 2712 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 2713 | struct xfs_btree_block *left; /* left btree block */ |
| 2714 | union xfs_btree_ptr rptr; /* right sibling block ptr */ |
| 2715 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 2716 | struct xfs_btree_block *right; /* right btree block */ |
| 2717 | union xfs_btree_ptr rrptr; /* right-right sibling ptr */ |
| 2718 | struct xfs_buf *rrbp; /* right-right buffer pointer */ |
| 2719 | struct xfs_btree_block *rrblock; /* right-right btree block */ |
| 2720 | int lrecs; |
| 2721 | int rrecs; |
| 2722 | int src_index; |
| 2723 | int error; /* error return value */ |
| 2724 | #ifdef DEBUG |
| 2725 | int i; |
| 2726 | #endif |
| 2727 | |
| 2728 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2729 | XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key); |
| 2730 | |
| 2731 | XFS_BTREE_STATS_INC(cur, split); |
| 2732 | |
| 2733 | /* Set up left block (current one). */ |
| 2734 | left = xfs_btree_get_block(cur, level, &lbp); |
| 2735 | |
| 2736 | #ifdef DEBUG |
| 2737 | error = xfs_btree_check_block(cur, left, level, lbp); |
| 2738 | if (error) |
| 2739 | goto error0; |
| 2740 | #endif |
| 2741 | |
| 2742 | xfs_btree_buf_to_ptr(cur, lbp, &lptr); |
| 2743 | |
| 2744 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ |
Eric Sandeen | 6f8950c | 2014-04-14 19:03:53 +1000 | [diff] [blame] | 2745 | error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2746 | if (error) |
| 2747 | goto error0; |
| 2748 | if (*stat == 0) |
| 2749 | goto out0; |
| 2750 | XFS_BTREE_STATS_INC(cur, alloc); |
| 2751 | |
| 2752 | /* Set up the new block as "right". */ |
| 2753 | error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp); |
| 2754 | if (error) |
| 2755 | goto error0; |
| 2756 | |
| 2757 | /* Fill in the btree header for the new right block. */ |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 2758 | xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2759 | |
| 2760 | /* |
| 2761 | * Split the entries between the old and the new block evenly. |
| 2762 | * Make sure that if there's an odd number of entries now, that |
| 2763 | * each new block will have the same number of entries. |
| 2764 | */ |
| 2765 | lrecs = xfs_btree_get_numrecs(left); |
| 2766 | rrecs = lrecs / 2; |
| 2767 | if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1) |
| 2768 | rrecs++; |
| 2769 | src_index = (lrecs - rrecs + 1); |
| 2770 | |
| 2771 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); |
| 2772 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2773 | /* Adjust numrecs for the later get_*_keys() calls. */ |
| 2774 | lrecs -= rrecs; |
| 2775 | xfs_btree_set_numrecs(left, lrecs); |
| 2776 | xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs); |
| 2777 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2778 | /* |
| 2779 | * Copy btree block entries from the left block over to the |
| 2780 | * new block, the right. Update the right block and log the |
| 2781 | * changes. |
| 2782 | */ |
| 2783 | if (level > 0) { |
| 2784 | /* It's a non-leaf. Move keys and pointers. */ |
| 2785 | union xfs_btree_key *lkp; /* left btree key */ |
| 2786 | union xfs_btree_ptr *lpp; /* left address pointer */ |
| 2787 | union xfs_btree_key *rkp; /* right btree key */ |
| 2788 | union xfs_btree_ptr *rpp; /* right address pointer */ |
| 2789 | |
| 2790 | lkp = xfs_btree_key_addr(cur, src_index, left); |
| 2791 | lpp = xfs_btree_ptr_addr(cur, src_index, left); |
| 2792 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 2793 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 2794 | |
| 2795 | #ifdef DEBUG |
| 2796 | for (i = src_index; i < rrecs; i++) { |
| 2797 | error = xfs_btree_check_ptr(cur, lpp, i, level); |
| 2798 | if (error) |
| 2799 | goto error0; |
| 2800 | } |
| 2801 | #endif |
| 2802 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2803 | /* Copy the keys & pointers to the new block. */ |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2804 | xfs_btree_copy_keys(cur, rkp, lkp, rrecs); |
| 2805 | xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs); |
| 2806 | |
| 2807 | xfs_btree_log_keys(cur, rbp, 1, rrecs); |
| 2808 | xfs_btree_log_ptrs(cur, rbp, 1, rrecs); |
| 2809 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2810 | /* Stash the keys of the new block for later insertion. */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2811 | xfs_btree_get_node_keys(cur, right, key); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2812 | } else { |
| 2813 | /* It's a leaf. Move records. */ |
| 2814 | union xfs_btree_rec *lrp; /* left record pointer */ |
| 2815 | union xfs_btree_rec *rrp; /* right record pointer */ |
| 2816 | |
| 2817 | lrp = xfs_btree_rec_addr(cur, src_index, left); |
| 2818 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 2819 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2820 | /* Copy records to the new block. */ |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2821 | xfs_btree_copy_recs(cur, rrp, lrp, rrecs); |
| 2822 | xfs_btree_log_recs(cur, rbp, 1, rrecs); |
| 2823 | |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2824 | /* Stash the keys of the new block for later insertion. */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2825 | xfs_btree_get_leaf_keys(cur, right, key); |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2826 | } |
| 2827 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2828 | /* |
| 2829 | * Find the left block number by looking in the buffer. |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 2830 | * Adjust sibling pointers. |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2831 | */ |
| 2832 | xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB); |
| 2833 | xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB); |
| 2834 | xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); |
| 2835 | xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB); |
| 2836 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2837 | xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS); |
| 2838 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); |
| 2839 | |
| 2840 | /* |
| 2841 | * If there's a block to the new block's right, make that block |
| 2842 | * point back to right instead of to left. |
| 2843 | */ |
| 2844 | if (!xfs_btree_ptr_is_null(cur, &rrptr)) { |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 2845 | error = xfs_btree_read_buf_block(cur, &rrptr, |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2846 | 0, &rrblock, &rrbp); |
| 2847 | if (error) |
| 2848 | goto error0; |
| 2849 | xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB); |
| 2850 | xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB); |
| 2851 | } |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2852 | |
| 2853 | /* Update the parent high keys of the left block, if needed. */ |
| 2854 | if (cur->bc_flags & XFS_BTREE_OVERLAPPING) { |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 2855 | error = xfs_btree_update_keys(cur, level); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 2856 | if (error) |
| 2857 | goto error0; |
| 2858 | } |
| 2859 | |
Christoph Hellwig | f5eb8e7 | 2008-10-30 16:57:03 +1100 | [diff] [blame] | 2860 | /* |
| 2861 | * If the cursor is really in the right block, move it there. |
| 2862 | * If it's just pointing past the last entry in left, then we'll |
| 2863 | * insert there, so don't change anything in that case. |
| 2864 | */ |
| 2865 | if (cur->bc_ptrs[level] > lrecs + 1) { |
| 2866 | xfs_btree_setbuf(cur, level, rbp); |
| 2867 | cur->bc_ptrs[level] -= lrecs; |
| 2868 | } |
| 2869 | /* |
| 2870 | * If there are more levels, we'll need another cursor which refers |
| 2871 | * the right block, no matter where this cursor was. |
| 2872 | */ |
| 2873 | if (level + 1 < cur->bc_nlevels) { |
| 2874 | error = xfs_btree_dup_cursor(cur, curp); |
| 2875 | if (error) |
| 2876 | goto error0; |
| 2877 | (*curp)->bc_ptrs[level + 1]++; |
| 2878 | } |
| 2879 | *ptrp = rptr; |
| 2880 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2881 | *stat = 1; |
| 2882 | return 0; |
| 2883 | out0: |
| 2884 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 2885 | *stat = 0; |
| 2886 | return 0; |
| 2887 | |
| 2888 | error0: |
| 2889 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 2890 | return error; |
| 2891 | } |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 2892 | |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 2893 | struct xfs_btree_split_args { |
| 2894 | struct xfs_btree_cur *cur; |
| 2895 | int level; |
| 2896 | union xfs_btree_ptr *ptrp; |
| 2897 | union xfs_btree_key *key; |
| 2898 | struct xfs_btree_cur **curp; |
| 2899 | int *stat; /* success/failure */ |
| 2900 | int result; |
| 2901 | bool kswapd; /* allocation in kswapd context */ |
| 2902 | struct completion *done; |
| 2903 | struct work_struct work; |
| 2904 | }; |
| 2905 | |
| 2906 | /* |
| 2907 | * Stack switching interfaces for allocation |
| 2908 | */ |
| 2909 | static void |
| 2910 | xfs_btree_split_worker( |
| 2911 | struct work_struct *work) |
| 2912 | { |
| 2913 | struct xfs_btree_split_args *args = container_of(work, |
| 2914 | struct xfs_btree_split_args, work); |
| 2915 | unsigned long pflags; |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 2916 | unsigned long new_pflags = PF_MEMALLOC_NOFS; |
Dave Chinner | cf11da9 | 2014-07-15 07:08:24 +1000 | [diff] [blame] | 2917 | |
| 2918 | /* |
| 2919 | * we are in a transaction context here, but may also be doing work |
| 2920 | * in kswapd context, and hence we may need to inherit that state |
| 2921 | * temporarily to ensure that we don't block waiting for memory reclaim |
| 2922 | * in any way. |
| 2923 | */ |
| 2924 | if (args->kswapd) |
| 2925 | new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
| 2926 | |
| 2927 | current_set_flags_nested(&pflags, new_pflags); |
| 2928 | |
| 2929 | args->result = __xfs_btree_split(args->cur, args->level, args->ptrp, |
| 2930 | args->key, args->curp, args->stat); |
| 2931 | complete(args->done); |
| 2932 | |
| 2933 | current_restore_flags_nested(&pflags, new_pflags); |
| 2934 | } |
| 2935 | |
| 2936 | /* |
| 2937 | * BMBT split requests often come in with little stack to work on. Push |
| 2938 | * them off to a worker thread so there is lots of stack to use. For the other |
| 2939 | * btree types, just call directly to avoid the context switch overhead here. |
| 2940 | */ |
| 2941 | STATIC int /* error */ |
| 2942 | xfs_btree_split( |
| 2943 | struct xfs_btree_cur *cur, |
| 2944 | int level, |
| 2945 | union xfs_btree_ptr *ptrp, |
| 2946 | union xfs_btree_key *key, |
| 2947 | struct xfs_btree_cur **curp, |
| 2948 | int *stat) /* success/failure */ |
| 2949 | { |
| 2950 | struct xfs_btree_split_args args; |
| 2951 | DECLARE_COMPLETION_ONSTACK(done); |
| 2952 | |
| 2953 | if (cur->bc_btnum != XFS_BTNUM_BMAP) |
| 2954 | return __xfs_btree_split(cur, level, ptrp, key, curp, stat); |
| 2955 | |
| 2956 | args.cur = cur; |
| 2957 | args.level = level; |
| 2958 | args.ptrp = ptrp; |
| 2959 | args.key = key; |
| 2960 | args.curp = curp; |
| 2961 | args.stat = stat; |
| 2962 | args.done = &done; |
| 2963 | args.kswapd = current_is_kswapd(); |
| 2964 | INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker); |
| 2965 | queue_work(xfs_alloc_wq, &args.work); |
| 2966 | wait_for_completion(&done); |
| 2967 | destroy_work_on_stack(&args.work); |
| 2968 | return args.result; |
| 2969 | } |
| 2970 | |
| 2971 | |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 2972 | /* |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 2973 | * Copy the old inode root contents into a real block and make the |
| 2974 | * broot point to it. |
| 2975 | */ |
| 2976 | int /* error */ |
| 2977 | xfs_btree_new_iroot( |
| 2978 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 2979 | int *logflags, /* logging flags for inode */ |
| 2980 | int *stat) /* return status - 0 fail */ |
| 2981 | { |
| 2982 | struct xfs_buf *cbp; /* buffer for cblock */ |
| 2983 | struct xfs_btree_block *block; /* btree block */ |
| 2984 | struct xfs_btree_block *cblock; /* child btree block */ |
| 2985 | union xfs_btree_key *ckp; /* child key pointer */ |
| 2986 | union xfs_btree_ptr *cpp; /* child ptr pointer */ |
| 2987 | union xfs_btree_key *kp; /* pointer to btree key */ |
| 2988 | union xfs_btree_ptr *pp; /* pointer to block addr */ |
| 2989 | union xfs_btree_ptr nptr; /* new block addr */ |
| 2990 | int level; /* btree level */ |
| 2991 | int error; /* error return code */ |
| 2992 | #ifdef DEBUG |
| 2993 | int i; /* loop counter */ |
| 2994 | #endif |
| 2995 | |
| 2996 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 2997 | XFS_BTREE_STATS_INC(cur, newroot); |
| 2998 | |
| 2999 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); |
| 3000 | |
| 3001 | level = cur->bc_nlevels - 1; |
| 3002 | |
| 3003 | block = xfs_btree_get_iroot(cur); |
| 3004 | pp = xfs_btree_ptr_addr(cur, 1, block); |
| 3005 | |
| 3006 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ |
Eric Sandeen | 6f8950c | 2014-04-14 19:03:53 +1000 | [diff] [blame] | 3007 | error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 3008 | if (error) |
| 3009 | goto error0; |
| 3010 | if (*stat == 0) { |
| 3011 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3012 | return 0; |
| 3013 | } |
| 3014 | XFS_BTREE_STATS_INC(cur, alloc); |
| 3015 | |
| 3016 | /* Copy the root into a real block. */ |
| 3017 | error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp); |
| 3018 | if (error) |
| 3019 | goto error0; |
| 3020 | |
Dave Chinner | 088c9f6 | 2013-06-12 12:19:08 +1000 | [diff] [blame] | 3021 | /* |
| 3022 | * we can't just memcpy() the root in for CRC enabled btree blocks. |
| 3023 | * In that case have to also ensure the blkno remains correct |
| 3024 | */ |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 3025 | memcpy(cblock, block, xfs_btree_block_len(cur)); |
Dave Chinner | 088c9f6 | 2013-06-12 12:19:08 +1000 | [diff] [blame] | 3026 | if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) { |
| 3027 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) |
| 3028 | cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn); |
| 3029 | else |
| 3030 | cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn); |
| 3031 | } |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 3032 | |
| 3033 | be16_add_cpu(&block->bb_level, 1); |
| 3034 | xfs_btree_set_numrecs(block, 1); |
| 3035 | cur->bc_nlevels++; |
| 3036 | cur->bc_ptrs[level + 1] = 1; |
| 3037 | |
| 3038 | kp = xfs_btree_key_addr(cur, 1, block); |
| 3039 | ckp = xfs_btree_key_addr(cur, 1, cblock); |
| 3040 | xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock)); |
| 3041 | |
| 3042 | cpp = xfs_btree_ptr_addr(cur, 1, cblock); |
| 3043 | #ifdef DEBUG |
| 3044 | for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) { |
| 3045 | error = xfs_btree_check_ptr(cur, pp, i, level); |
| 3046 | if (error) |
| 3047 | goto error0; |
| 3048 | } |
| 3049 | #endif |
| 3050 | xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock)); |
| 3051 | |
| 3052 | #ifdef DEBUG |
| 3053 | error = xfs_btree_check_ptr(cur, &nptr, 0, level); |
| 3054 | if (error) |
| 3055 | goto error0; |
| 3056 | #endif |
| 3057 | xfs_btree_copy_ptrs(cur, pp, &nptr, 1); |
| 3058 | |
| 3059 | xfs_iroot_realloc(cur->bc_private.b.ip, |
| 3060 | 1 - xfs_btree_get_numrecs(cblock), |
| 3061 | cur->bc_private.b.whichfork); |
| 3062 | |
| 3063 | xfs_btree_setbuf(cur, level, cbp); |
| 3064 | |
| 3065 | /* |
| 3066 | * Do all this logging at the end so that |
| 3067 | * the root is at the right level. |
| 3068 | */ |
| 3069 | xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS); |
| 3070 | xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs)); |
| 3071 | xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs)); |
| 3072 | |
| 3073 | *logflags |= |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3074 | XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork); |
Christoph Hellwig | ea77b0a | 2008-10-30 16:57:28 +1100 | [diff] [blame] | 3075 | *stat = 1; |
| 3076 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3077 | return 0; |
| 3078 | error0: |
| 3079 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3080 | return error; |
| 3081 | } |
| 3082 | |
| 3083 | /* |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3084 | * Allocate a new root block, fill it in. |
| 3085 | */ |
Christoph Hellwig | 3cc7524 | 2008-10-30 16:58:41 +1100 | [diff] [blame] | 3086 | STATIC int /* error */ |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3087 | xfs_btree_new_root( |
| 3088 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 3089 | int *stat) /* success/failure */ |
| 3090 | { |
| 3091 | struct xfs_btree_block *block; /* one half of the old root block */ |
| 3092 | struct xfs_buf *bp; /* buffer containing block */ |
| 3093 | int error; /* error return value */ |
| 3094 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 3095 | struct xfs_btree_block *left; /* left btree block */ |
| 3096 | struct xfs_buf *nbp; /* new (root) buffer */ |
| 3097 | struct xfs_btree_block *new; /* new (root) btree block */ |
| 3098 | int nptr; /* new value for key index, 1 or 2 */ |
| 3099 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 3100 | struct xfs_btree_block *right; /* right btree block */ |
| 3101 | union xfs_btree_ptr rptr; |
| 3102 | union xfs_btree_ptr lptr; |
| 3103 | |
| 3104 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 3105 | XFS_BTREE_STATS_INC(cur, newroot); |
| 3106 | |
| 3107 | /* initialise our start point from the cursor */ |
| 3108 | cur->bc_ops->init_ptr_from_cur(cur, &rptr); |
| 3109 | |
| 3110 | /* Allocate the new block. If we can't do it, we're toast. Give up. */ |
Eric Sandeen | 6f8950c | 2014-04-14 19:03:53 +1000 | [diff] [blame] | 3111 | error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat); |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3112 | if (error) |
| 3113 | goto error0; |
| 3114 | if (*stat == 0) |
| 3115 | goto out0; |
| 3116 | XFS_BTREE_STATS_INC(cur, alloc); |
| 3117 | |
| 3118 | /* Set up the new block. */ |
| 3119 | error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp); |
| 3120 | if (error) |
| 3121 | goto error0; |
| 3122 | |
| 3123 | /* Set the root in the holding structure increasing the level by 1. */ |
| 3124 | cur->bc_ops->set_root(cur, &lptr, 1); |
| 3125 | |
| 3126 | /* |
| 3127 | * At the previous root level there are now two blocks: the old root, |
| 3128 | * and the new block generated when it was split. We don't know which |
| 3129 | * one the cursor is pointing at, so we set up variables "left" and |
| 3130 | * "right" for each case. |
| 3131 | */ |
| 3132 | block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp); |
| 3133 | |
| 3134 | #ifdef DEBUG |
| 3135 | error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp); |
| 3136 | if (error) |
| 3137 | goto error0; |
| 3138 | #endif |
| 3139 | |
| 3140 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); |
| 3141 | if (!xfs_btree_ptr_is_null(cur, &rptr)) { |
| 3142 | /* Our block is left, pick up the right block. */ |
| 3143 | lbp = bp; |
| 3144 | xfs_btree_buf_to_ptr(cur, lbp, &lptr); |
| 3145 | left = block; |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 3146 | error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp); |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3147 | if (error) |
| 3148 | goto error0; |
| 3149 | bp = rbp; |
| 3150 | nptr = 1; |
| 3151 | } else { |
| 3152 | /* Our block is right, pick up the left block. */ |
| 3153 | rbp = bp; |
| 3154 | xfs_btree_buf_to_ptr(cur, rbp, &rptr); |
| 3155 | right = block; |
| 3156 | xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB); |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 3157 | error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp); |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3158 | if (error) |
| 3159 | goto error0; |
| 3160 | bp = lbp; |
| 3161 | nptr = 2; |
| 3162 | } |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3163 | |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3164 | /* Fill in the new block's btree header and log it. */ |
Christoph Hellwig | ee1a47a | 2013-04-21 14:53:46 -0500 | [diff] [blame] | 3165 | xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2); |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3166 | xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS); |
| 3167 | ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) && |
| 3168 | !xfs_btree_ptr_is_null(cur, &rptr)); |
| 3169 | |
| 3170 | /* Fill in the key data in the new root. */ |
| 3171 | if (xfs_btree_get_level(left) > 0) { |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3172 | /* |
| 3173 | * Get the keys for the left block's keys and put them directly |
| 3174 | * in the parent block. Do the same for the right block. |
| 3175 | */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 3176 | xfs_btree_get_node_keys(cur, left, |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3177 | xfs_btree_key_addr(cur, 1, new)); |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 3178 | xfs_btree_get_node_keys(cur, right, |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3179 | xfs_btree_key_addr(cur, 2, new)); |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3180 | } else { |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3181 | /* |
| 3182 | * Get the keys for the left block's records and put them |
| 3183 | * directly in the parent block. Do the same for the right |
| 3184 | * block. |
| 3185 | */ |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 3186 | xfs_btree_get_leaf_keys(cur, left, |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3187 | xfs_btree_key_addr(cur, 1, new)); |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 3188 | xfs_btree_get_leaf_keys(cur, right, |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3189 | xfs_btree_key_addr(cur, 2, new)); |
Christoph Hellwig | 344207c | 2008-10-30 16:57:16 +1100 | [diff] [blame] | 3190 | } |
| 3191 | xfs_btree_log_keys(cur, nbp, 1, 2); |
| 3192 | |
| 3193 | /* Fill in the pointer data in the new root. */ |
| 3194 | xfs_btree_copy_ptrs(cur, |
| 3195 | xfs_btree_ptr_addr(cur, 1, new), &lptr, 1); |
| 3196 | xfs_btree_copy_ptrs(cur, |
| 3197 | xfs_btree_ptr_addr(cur, 2, new), &rptr, 1); |
| 3198 | xfs_btree_log_ptrs(cur, nbp, 1, 2); |
| 3199 | |
| 3200 | /* Fix up the cursor. */ |
| 3201 | xfs_btree_setbuf(cur, cur->bc_nlevels, nbp); |
| 3202 | cur->bc_ptrs[cur->bc_nlevels] = nptr; |
| 3203 | cur->bc_nlevels++; |
| 3204 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3205 | *stat = 1; |
| 3206 | return 0; |
| 3207 | error0: |
| 3208 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3209 | return error; |
| 3210 | out0: |
| 3211 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3212 | *stat = 0; |
| 3213 | return 0; |
| 3214 | } |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3215 | |
| 3216 | STATIC int |
| 3217 | xfs_btree_make_block_unfull( |
| 3218 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 3219 | int level, /* btree level */ |
| 3220 | int numrecs,/* # of recs in block */ |
| 3221 | int *oindex,/* old tree index */ |
| 3222 | int *index, /* new tree index */ |
| 3223 | union xfs_btree_ptr *nptr, /* new btree ptr */ |
| 3224 | struct xfs_btree_cur **ncur, /* new btree cursor */ |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3225 | union xfs_btree_key *key, /* key of new block */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3226 | int *stat) |
| 3227 | { |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3228 | int error = 0; |
| 3229 | |
| 3230 | if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 3231 | level == cur->bc_nlevels - 1) { |
| 3232 | struct xfs_inode *ip = cur->bc_private.b.ip; |
| 3233 | |
| 3234 | if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) { |
| 3235 | /* A root block that can be made bigger. */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3236 | xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork); |
Darrick J. Wong | 0d30979 | 2016-08-03 11:01:25 +1000 | [diff] [blame] | 3237 | *stat = 1; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3238 | } else { |
| 3239 | /* A root block that needs replacing */ |
| 3240 | int logflags = 0; |
| 3241 | |
| 3242 | error = xfs_btree_new_iroot(cur, &logflags, stat); |
| 3243 | if (error || *stat == 0) |
| 3244 | return error; |
| 3245 | |
| 3246 | xfs_trans_log_inode(cur->bc_tp, ip, logflags); |
| 3247 | } |
| 3248 | |
| 3249 | return 0; |
| 3250 | } |
| 3251 | |
| 3252 | /* First, try shifting an entry to the right neighbor. */ |
| 3253 | error = xfs_btree_rshift(cur, level, stat); |
| 3254 | if (error || *stat) |
| 3255 | return error; |
| 3256 | |
| 3257 | /* Next, try shifting an entry to the left neighbor. */ |
| 3258 | error = xfs_btree_lshift(cur, level, stat); |
| 3259 | if (error) |
| 3260 | return error; |
| 3261 | |
| 3262 | if (*stat) { |
| 3263 | *oindex = *index = cur->bc_ptrs[level]; |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
| 3267 | /* |
| 3268 | * Next, try splitting the current block in half. |
| 3269 | * |
| 3270 | * If this works we have to re-set our variables because we |
| 3271 | * could be in a different block now. |
| 3272 | */ |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3273 | error = xfs_btree_split(cur, level, nptr, key, ncur, stat); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3274 | if (error || *stat == 0) |
| 3275 | return error; |
| 3276 | |
| 3277 | |
| 3278 | *index = cur->bc_ptrs[level]; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3279 | return 0; |
| 3280 | } |
| 3281 | |
| 3282 | /* |
| 3283 | * Insert one record/level. Return information to the caller |
| 3284 | * allowing the next level up to proceed if necessary. |
| 3285 | */ |
| 3286 | STATIC int |
| 3287 | xfs_btree_insrec( |
| 3288 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 3289 | int level, /* level to insert record at */ |
| 3290 | union xfs_btree_ptr *ptrp, /* i/o: block number inserted */ |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3291 | union xfs_btree_rec *rec, /* record to insert */ |
| 3292 | union xfs_btree_key *key, /* i/o: block key for ptrp */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3293 | struct xfs_btree_cur **curp, /* output: new cursor replacing cur */ |
| 3294 | int *stat) /* success/failure */ |
| 3295 | { |
| 3296 | struct xfs_btree_block *block; /* btree block */ |
| 3297 | struct xfs_buf *bp; /* buffer for block */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3298 | union xfs_btree_ptr nptr; /* new block ptr */ |
| 3299 | struct xfs_btree_cur *ncur; /* new btree cursor */ |
Darrick J. Wong | a1d46cf | 2016-09-19 10:24:36 +1000 | [diff] [blame] | 3300 | union xfs_btree_key nkey; /* new block key */ |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3301 | union xfs_btree_key *lkey; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3302 | int optr; /* old key/record index */ |
| 3303 | int ptr; /* key/record index */ |
| 3304 | int numrecs;/* number of records */ |
| 3305 | int error; /* error return value */ |
| 3306 | #ifdef DEBUG |
| 3307 | int i; |
| 3308 | #endif |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3309 | xfs_daddr_t old_bn; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3310 | |
| 3311 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3312 | XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, &rec); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3313 | |
| 3314 | ncur = NULL; |
Darrick J. Wong | a1d46cf | 2016-09-19 10:24:36 +1000 | [diff] [blame] | 3315 | lkey = &nkey; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3316 | |
| 3317 | /* |
| 3318 | * If we have an external root pointer, and we've made it to the |
| 3319 | * root level, allocate a new root block and we're done. |
| 3320 | */ |
| 3321 | if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) && |
| 3322 | (level >= cur->bc_nlevels)) { |
| 3323 | error = xfs_btree_new_root(cur, stat); |
| 3324 | xfs_btree_set_ptr_null(cur, ptrp); |
| 3325 | |
| 3326 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3327 | return error; |
| 3328 | } |
| 3329 | |
| 3330 | /* If we're off the left edge, return failure. */ |
| 3331 | ptr = cur->bc_ptrs[level]; |
| 3332 | if (ptr == 0) { |
| 3333 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3334 | *stat = 0; |
| 3335 | return 0; |
| 3336 | } |
| 3337 | |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3338 | optr = ptr; |
| 3339 | |
| 3340 | XFS_BTREE_STATS_INC(cur, insrec); |
| 3341 | |
| 3342 | /* Get pointers to the btree buffer and block. */ |
| 3343 | block = xfs_btree_get_block(cur, level, &bp); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3344 | old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3345 | numrecs = xfs_btree_get_numrecs(block); |
| 3346 | |
| 3347 | #ifdef DEBUG |
| 3348 | error = xfs_btree_check_block(cur, block, level, bp); |
| 3349 | if (error) |
| 3350 | goto error0; |
| 3351 | |
| 3352 | /* Check that the new entry is being inserted in the right place. */ |
| 3353 | if (ptr <= numrecs) { |
| 3354 | if (level == 0) { |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3355 | ASSERT(cur->bc_ops->recs_inorder(cur, rec, |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 3356 | xfs_btree_rec_addr(cur, ptr, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3357 | } else { |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3358 | ASSERT(cur->bc_ops->keys_inorder(cur, key, |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 3359 | xfs_btree_key_addr(cur, ptr, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3360 | } |
| 3361 | } |
| 3362 | #endif |
| 3363 | |
| 3364 | /* |
| 3365 | * If the block is full, we can't insert the new entry until we |
| 3366 | * make the block un-full. |
| 3367 | */ |
| 3368 | xfs_btree_set_ptr_null(cur, &nptr); |
| 3369 | if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) { |
| 3370 | error = xfs_btree_make_block_unfull(cur, level, numrecs, |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3371 | &optr, &ptr, &nptr, &ncur, lkey, stat); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3372 | if (error || *stat == 0) |
| 3373 | goto error0; |
| 3374 | } |
| 3375 | |
| 3376 | /* |
| 3377 | * The current block may have changed if the block was |
| 3378 | * previously full and we have just made space in it. |
| 3379 | */ |
| 3380 | block = xfs_btree_get_block(cur, level, &bp); |
| 3381 | numrecs = xfs_btree_get_numrecs(block); |
| 3382 | |
| 3383 | #ifdef DEBUG |
| 3384 | error = xfs_btree_check_block(cur, block, level, bp); |
| 3385 | if (error) |
| 3386 | return error; |
| 3387 | #endif |
| 3388 | |
| 3389 | /* |
| 3390 | * At this point we know there's room for our new entry in the block |
| 3391 | * we're pointing at. |
| 3392 | */ |
| 3393 | XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1); |
| 3394 | |
| 3395 | if (level > 0) { |
| 3396 | /* It's a nonleaf. make a hole in the keys and ptrs */ |
| 3397 | union xfs_btree_key *kp; |
| 3398 | union xfs_btree_ptr *pp; |
| 3399 | |
| 3400 | kp = xfs_btree_key_addr(cur, ptr, block); |
| 3401 | pp = xfs_btree_ptr_addr(cur, ptr, block); |
| 3402 | |
| 3403 | #ifdef DEBUG |
| 3404 | for (i = numrecs - ptr; i >= 0; i--) { |
| 3405 | error = xfs_btree_check_ptr(cur, pp, i, level); |
| 3406 | if (error) |
| 3407 | return error; |
| 3408 | } |
| 3409 | #endif |
| 3410 | |
| 3411 | xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1); |
| 3412 | xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1); |
| 3413 | |
| 3414 | #ifdef DEBUG |
| 3415 | error = xfs_btree_check_ptr(cur, ptrp, 0, level); |
| 3416 | if (error) |
| 3417 | goto error0; |
| 3418 | #endif |
| 3419 | |
| 3420 | /* Now put the new data in, bump numrecs and log it. */ |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3421 | xfs_btree_copy_keys(cur, kp, key, 1); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3422 | xfs_btree_copy_ptrs(cur, pp, ptrp, 1); |
| 3423 | numrecs++; |
| 3424 | xfs_btree_set_numrecs(block, numrecs); |
| 3425 | xfs_btree_log_ptrs(cur, bp, ptr, numrecs); |
| 3426 | xfs_btree_log_keys(cur, bp, ptr, numrecs); |
| 3427 | #ifdef DEBUG |
| 3428 | if (ptr < numrecs) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 3429 | ASSERT(cur->bc_ops->keys_inorder(cur, kp, |
| 3430 | xfs_btree_key_addr(cur, ptr + 1, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3431 | } |
| 3432 | #endif |
| 3433 | } else { |
| 3434 | /* It's a leaf. make a hole in the records */ |
| 3435 | union xfs_btree_rec *rp; |
| 3436 | |
| 3437 | rp = xfs_btree_rec_addr(cur, ptr, block); |
| 3438 | |
| 3439 | xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1); |
| 3440 | |
| 3441 | /* Now put the new data in, bump numrecs and log it. */ |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3442 | xfs_btree_copy_recs(cur, rp, rec, 1); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3443 | xfs_btree_set_numrecs(block, ++numrecs); |
| 3444 | xfs_btree_log_recs(cur, bp, ptr, numrecs); |
| 3445 | #ifdef DEBUG |
| 3446 | if (ptr < numrecs) { |
Christoph Hellwig | 4a26e66 | 2008-10-30 16:58:32 +1100 | [diff] [blame] | 3447 | ASSERT(cur->bc_ops->recs_inorder(cur, rp, |
| 3448 | xfs_btree_rec_addr(cur, ptr + 1, block))); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3449 | } |
| 3450 | #endif |
| 3451 | } |
| 3452 | |
| 3453 | /* Log the new number of records in the btree header. */ |
| 3454 | xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS); |
| 3455 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3456 | /* |
| 3457 | * If we just inserted into a new tree block, we have to |
| 3458 | * recalculate nkey here because nkey is out of date. |
| 3459 | * |
| 3460 | * Otherwise we're just updating an existing block (having shoved |
| 3461 | * some records into the new tree block), so use the regular key |
| 3462 | * update mechanism. |
| 3463 | */ |
| 3464 | if (bp && bp->b_bn != old_bn) { |
| 3465 | xfs_btree_get_keys(cur, block, lkey); |
| 3466 | } else if (xfs_btree_needs_key_update(cur, optr)) { |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 3467 | error = xfs_btree_update_keys(cur, level); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3468 | if (error) |
| 3469 | goto error0; |
| 3470 | } |
| 3471 | |
| 3472 | /* |
| 3473 | * If we are tracking the last record in the tree and |
| 3474 | * we are at the far right edge of the tree, update it. |
| 3475 | */ |
| 3476 | if (xfs_btree_is_lastrec(cur, block, level)) { |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3477 | cur->bc_ops->update_lastrec(cur, block, rec, |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3478 | ptr, LASTREC_INSREC); |
| 3479 | } |
| 3480 | |
| 3481 | /* |
| 3482 | * Return the new block number, if any. |
| 3483 | * If there is one, give back a record value and a cursor too. |
| 3484 | */ |
| 3485 | *ptrp = nptr; |
| 3486 | if (!xfs_btree_ptr_is_null(cur, &nptr)) { |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3487 | xfs_btree_copy_keys(cur, key, lkey, 1); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3488 | *curp = ncur; |
| 3489 | } |
| 3490 | |
| 3491 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3492 | *stat = 1; |
| 3493 | return 0; |
| 3494 | |
| 3495 | error0: |
| 3496 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3497 | return error; |
| 3498 | } |
| 3499 | |
| 3500 | /* |
| 3501 | * Insert the record at the point referenced by cur. |
| 3502 | * |
| 3503 | * A multi-level split of the tree on insert will invalidate the original |
| 3504 | * cursor. All callers of this function should assume that the cursor is |
| 3505 | * no longer valid and revalidate it. |
| 3506 | */ |
| 3507 | int |
| 3508 | xfs_btree_insert( |
| 3509 | struct xfs_btree_cur *cur, |
| 3510 | int *stat) |
| 3511 | { |
| 3512 | int error; /* error return value */ |
| 3513 | int i; /* result value, 0 for failure */ |
| 3514 | int level; /* current level number in btree */ |
| 3515 | union xfs_btree_ptr nptr; /* new block number (split result) */ |
| 3516 | struct xfs_btree_cur *ncur; /* new cursor (split result) */ |
| 3517 | struct xfs_btree_cur *pcur; /* previous level's cursor */ |
Darrick J. Wong | a1d46cf | 2016-09-19 10:24:36 +1000 | [diff] [blame] | 3518 | union xfs_btree_key bkey; /* key of block to insert */ |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3519 | union xfs_btree_key *key; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3520 | union xfs_btree_rec rec; /* record to insert */ |
| 3521 | |
| 3522 | level = 0; |
| 3523 | ncur = NULL; |
| 3524 | pcur = cur; |
Darrick J. Wong | a1d46cf | 2016-09-19 10:24:36 +1000 | [diff] [blame] | 3525 | key = &bkey; |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3526 | |
| 3527 | xfs_btree_set_ptr_null(cur, &nptr); |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3528 | |
| 3529 | /* Make a key out of the record data to be inserted, and save it. */ |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3530 | cur->bc_ops->init_rec_from_cur(cur, &rec); |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3531 | cur->bc_ops->init_key_from_rec(key, &rec); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3532 | |
| 3533 | /* |
| 3534 | * Loop going up the tree, starting at the leaf level. |
| 3535 | * Stop when we don't get a split block, that must mean that |
| 3536 | * the insert is finished with this level. |
| 3537 | */ |
| 3538 | do { |
| 3539 | /* |
| 3540 | * Insert nrec/nptr into this level of the tree. |
| 3541 | * Note if we fail, nptr will be null. |
| 3542 | */ |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 3543 | error = xfs_btree_insrec(pcur, level, &nptr, &rec, key, |
Darrick J. Wong | e5821e5 | 2016-08-03 11:02:39 +1000 | [diff] [blame] | 3544 | &ncur, &i); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3545 | if (error) { |
| 3546 | if (pcur != cur) |
| 3547 | xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR); |
| 3548 | goto error0; |
| 3549 | } |
| 3550 | |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3551 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 4b22a57 | 2008-10-30 16:57:40 +1100 | [diff] [blame] | 3552 | level++; |
| 3553 | |
| 3554 | /* |
| 3555 | * See if the cursor we just used is trash. |
| 3556 | * Can't trash the caller's cursor, but otherwise we should |
| 3557 | * if ncur is a new cursor or we're about to be done. |
| 3558 | */ |
| 3559 | if (pcur != cur && |
| 3560 | (ncur || xfs_btree_ptr_is_null(cur, &nptr))) { |
| 3561 | /* Save the state from the cursor before we trash it */ |
| 3562 | if (cur->bc_ops->update_cursor) |
| 3563 | cur->bc_ops->update_cursor(pcur, cur); |
| 3564 | cur->bc_nlevels = pcur->bc_nlevels; |
| 3565 | xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR); |
| 3566 | } |
| 3567 | /* If we got a new cursor, switch to it. */ |
| 3568 | if (ncur) { |
| 3569 | pcur = ncur; |
| 3570 | ncur = NULL; |
| 3571 | } |
| 3572 | } while (!xfs_btree_ptr_is_null(cur, &nptr)); |
| 3573 | |
| 3574 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3575 | *stat = i; |
| 3576 | return 0; |
| 3577 | error0: |
| 3578 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3579 | return error; |
| 3580 | } |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3581 | |
| 3582 | /* |
| 3583 | * Try to merge a non-leaf block back into the inode root. |
| 3584 | * |
| 3585 | * Note: the killroot names comes from the fact that we're effectively |
| 3586 | * killing the old root block. But because we can't just delete the |
| 3587 | * inode we have to copy the single block it was pointing to into the |
| 3588 | * inode. |
| 3589 | */ |
Eric Sandeen | d96f8f8 | 2009-07-02 00:09:33 -0500 | [diff] [blame] | 3590 | STATIC int |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3591 | xfs_btree_kill_iroot( |
| 3592 | struct xfs_btree_cur *cur) |
| 3593 | { |
| 3594 | int whichfork = cur->bc_private.b.whichfork; |
| 3595 | struct xfs_inode *ip = cur->bc_private.b.ip; |
| 3596 | struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); |
| 3597 | struct xfs_btree_block *block; |
| 3598 | struct xfs_btree_block *cblock; |
| 3599 | union xfs_btree_key *kp; |
| 3600 | union xfs_btree_key *ckp; |
| 3601 | union xfs_btree_ptr *pp; |
| 3602 | union xfs_btree_ptr *cpp; |
| 3603 | struct xfs_buf *cbp; |
| 3604 | int level; |
| 3605 | int index; |
| 3606 | int numrecs; |
Christoph Hellwig | 196328e | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 3607 | int error; |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3608 | #ifdef DEBUG |
| 3609 | union xfs_btree_ptr ptr; |
| 3610 | int i; |
| 3611 | #endif |
| 3612 | |
| 3613 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 3614 | |
| 3615 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); |
| 3616 | ASSERT(cur->bc_nlevels > 1); |
| 3617 | |
| 3618 | /* |
| 3619 | * Don't deal with the root block needs to be a leaf case. |
| 3620 | * We're just going to turn the thing back into extents anyway. |
| 3621 | */ |
| 3622 | level = cur->bc_nlevels - 1; |
| 3623 | if (level == 1) |
| 3624 | goto out0; |
| 3625 | |
| 3626 | /* |
| 3627 | * Give up if the root has multiple children. |
| 3628 | */ |
| 3629 | block = xfs_btree_get_iroot(cur); |
| 3630 | if (xfs_btree_get_numrecs(block) != 1) |
| 3631 | goto out0; |
| 3632 | |
| 3633 | cblock = xfs_btree_get_block(cur, level - 1, &cbp); |
| 3634 | numrecs = xfs_btree_get_numrecs(cblock); |
| 3635 | |
| 3636 | /* |
| 3637 | * Only do this if the next level will fit. |
| 3638 | * Then the data must be copied up to the inode, |
| 3639 | * instead of freeing the root you free the next level. |
| 3640 | */ |
| 3641 | if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level)) |
| 3642 | goto out0; |
| 3643 | |
| 3644 | XFS_BTREE_STATS_INC(cur, killroot); |
| 3645 | |
| 3646 | #ifdef DEBUG |
| 3647 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB); |
| 3648 | ASSERT(xfs_btree_ptr_is_null(cur, &ptr)); |
| 3649 | xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB); |
| 3650 | ASSERT(xfs_btree_ptr_is_null(cur, &ptr)); |
| 3651 | #endif |
| 3652 | |
| 3653 | index = numrecs - cur->bc_ops->get_maxrecs(cur, level); |
| 3654 | if (index) { |
| 3655 | xfs_iroot_realloc(cur->bc_private.b.ip, index, |
| 3656 | cur->bc_private.b.whichfork); |
Christoph Hellwig | 7cc95a8 | 2008-10-30 17:14:34 +1100 | [diff] [blame] | 3657 | block = ifp->if_broot; |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | be16_add_cpu(&block->bb_numrecs, index); |
| 3661 | ASSERT(block->bb_numrecs == cblock->bb_numrecs); |
| 3662 | |
| 3663 | kp = xfs_btree_key_addr(cur, 1, block); |
| 3664 | ckp = xfs_btree_key_addr(cur, 1, cblock); |
| 3665 | xfs_btree_copy_keys(cur, kp, ckp, numrecs); |
| 3666 | |
| 3667 | pp = xfs_btree_ptr_addr(cur, 1, block); |
| 3668 | cpp = xfs_btree_ptr_addr(cur, 1, cblock); |
| 3669 | #ifdef DEBUG |
| 3670 | for (i = 0; i < numrecs; i++) { |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3671 | error = xfs_btree_check_ptr(cur, cpp, i, level - 1); |
| 3672 | if (error) { |
| 3673 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3674 | return error; |
| 3675 | } |
| 3676 | } |
| 3677 | #endif |
| 3678 | xfs_btree_copy_ptrs(cur, pp, cpp, numrecs); |
| 3679 | |
Christoph Hellwig | c46ee8a | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 3680 | error = xfs_btree_free_block(cur, cbp); |
Christoph Hellwig | 196328e | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 3681 | if (error) { |
| 3682 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3683 | return error; |
| 3684 | } |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3685 | |
| 3686 | cur->bc_bufs[level - 1] = NULL; |
| 3687 | be16_add_cpu(&block->bb_level, -1); |
| 3688 | xfs_trans_log_inode(cur->bc_tp, ip, |
Eric Sandeen | 9d87c31 | 2009-01-14 23:22:07 -0600 | [diff] [blame] | 3689 | XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork)); |
Christoph Hellwig | d4b3a4b | 2008-10-30 16:57:51 +1100 | [diff] [blame] | 3690 | cur->bc_nlevels--; |
| 3691 | out0: |
| 3692 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3693 | return 0; |
| 3694 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3695 | |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 3696 | /* |
| 3697 | * Kill the current root node, and replace it with it's only child node. |
| 3698 | */ |
| 3699 | STATIC int |
| 3700 | xfs_btree_kill_root( |
| 3701 | struct xfs_btree_cur *cur, |
| 3702 | struct xfs_buf *bp, |
| 3703 | int level, |
| 3704 | union xfs_btree_ptr *newroot) |
| 3705 | { |
| 3706 | int error; |
| 3707 | |
| 3708 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 3709 | XFS_BTREE_STATS_INC(cur, killroot); |
| 3710 | |
| 3711 | /* |
| 3712 | * Update the root pointer, decreasing the level by 1 and then |
| 3713 | * free the old root. |
| 3714 | */ |
| 3715 | cur->bc_ops->set_root(cur, newroot, -1); |
| 3716 | |
Christoph Hellwig | c46ee8a | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 3717 | error = xfs_btree_free_block(cur, bp); |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 3718 | if (error) { |
| 3719 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 3720 | return error; |
| 3721 | } |
| 3722 | |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 3723 | cur->bc_bufs[level] = NULL; |
| 3724 | cur->bc_ra[level] = 0; |
| 3725 | cur->bc_nlevels--; |
| 3726 | |
| 3727 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3728 | return 0; |
| 3729 | } |
| 3730 | |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3731 | STATIC int |
| 3732 | xfs_btree_dec_cursor( |
| 3733 | struct xfs_btree_cur *cur, |
| 3734 | int level, |
| 3735 | int *stat) |
| 3736 | { |
| 3737 | int error; |
| 3738 | int i; |
| 3739 | |
| 3740 | if (level > 0) { |
| 3741 | error = xfs_btree_decrement(cur, level, &i); |
| 3742 | if (error) |
| 3743 | return error; |
| 3744 | } |
| 3745 | |
| 3746 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3747 | *stat = 1; |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
| 3751 | /* |
| 3752 | * Single level of the btree record deletion routine. |
| 3753 | * Delete record pointed to by cur/level. |
| 3754 | * Remove the record from its block then rebalance the tree. |
| 3755 | * Return 0 for error, 1 for done, 2 to go on to the next level. |
| 3756 | */ |
| 3757 | STATIC int /* error */ |
| 3758 | xfs_btree_delrec( |
| 3759 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 3760 | int level, /* level removing record from */ |
| 3761 | int *stat) /* fail/done/go-on */ |
| 3762 | { |
| 3763 | struct xfs_btree_block *block; /* btree block */ |
| 3764 | union xfs_btree_ptr cptr; /* current block ptr */ |
| 3765 | struct xfs_buf *bp; /* buffer for block */ |
| 3766 | int error; /* error return value */ |
| 3767 | int i; /* loop counter */ |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3768 | union xfs_btree_ptr lptr; /* left sibling block ptr */ |
| 3769 | struct xfs_buf *lbp; /* left buffer pointer */ |
| 3770 | struct xfs_btree_block *left; /* left btree block */ |
| 3771 | int lrecs = 0; /* left record count */ |
| 3772 | int ptr; /* key/record index */ |
| 3773 | union xfs_btree_ptr rptr; /* right sibling block ptr */ |
| 3774 | struct xfs_buf *rbp; /* right buffer pointer */ |
| 3775 | struct xfs_btree_block *right; /* right btree block */ |
| 3776 | struct xfs_btree_block *rrblock; /* right-right btree block */ |
| 3777 | struct xfs_buf *rrbp; /* right-right buffer pointer */ |
| 3778 | int rrecs = 0; /* right record count */ |
| 3779 | struct xfs_btree_cur *tcur; /* temporary btree cursor */ |
| 3780 | int numrecs; /* temporary numrec count */ |
| 3781 | |
| 3782 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 3783 | XFS_BTREE_TRACE_ARGI(cur, level); |
| 3784 | |
| 3785 | tcur = NULL; |
| 3786 | |
| 3787 | /* Get the index of the entry being deleted, check for nothing there. */ |
| 3788 | ptr = cur->bc_ptrs[level]; |
| 3789 | if (ptr == 0) { |
| 3790 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3791 | *stat = 0; |
| 3792 | return 0; |
| 3793 | } |
| 3794 | |
| 3795 | /* Get the buffer & block containing the record or key/ptr. */ |
| 3796 | block = xfs_btree_get_block(cur, level, &bp); |
| 3797 | numrecs = xfs_btree_get_numrecs(block); |
| 3798 | |
| 3799 | #ifdef DEBUG |
| 3800 | error = xfs_btree_check_block(cur, block, level, bp); |
| 3801 | if (error) |
| 3802 | goto error0; |
| 3803 | #endif |
| 3804 | |
| 3805 | /* Fail if we're off the end of the block. */ |
| 3806 | if (ptr > numrecs) { |
| 3807 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 3808 | *stat = 0; |
| 3809 | return 0; |
| 3810 | } |
| 3811 | |
| 3812 | XFS_BTREE_STATS_INC(cur, delrec); |
| 3813 | XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr); |
| 3814 | |
| 3815 | /* Excise the entries being deleted. */ |
| 3816 | if (level > 0) { |
| 3817 | /* It's a nonleaf. operate on keys and ptrs */ |
| 3818 | union xfs_btree_key *lkp; |
| 3819 | union xfs_btree_ptr *lpp; |
| 3820 | |
| 3821 | lkp = xfs_btree_key_addr(cur, ptr + 1, block); |
| 3822 | lpp = xfs_btree_ptr_addr(cur, ptr + 1, block); |
| 3823 | |
| 3824 | #ifdef DEBUG |
| 3825 | for (i = 0; i < numrecs - ptr; i++) { |
| 3826 | error = xfs_btree_check_ptr(cur, lpp, i, level); |
| 3827 | if (error) |
| 3828 | goto error0; |
| 3829 | } |
| 3830 | #endif |
| 3831 | |
| 3832 | if (ptr < numrecs) { |
| 3833 | xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr); |
| 3834 | xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr); |
| 3835 | xfs_btree_log_keys(cur, bp, ptr, numrecs - 1); |
| 3836 | xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1); |
| 3837 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3838 | } else { |
| 3839 | /* It's a leaf. operate on records */ |
| 3840 | if (ptr < numrecs) { |
| 3841 | xfs_btree_shift_recs(cur, |
| 3842 | xfs_btree_rec_addr(cur, ptr + 1, block), |
| 3843 | -1, numrecs - ptr); |
| 3844 | xfs_btree_log_recs(cur, bp, ptr, numrecs - 1); |
| 3845 | } |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | /* |
| 3849 | * Decrement and log the number of entries in the block. |
| 3850 | */ |
| 3851 | xfs_btree_set_numrecs(block, --numrecs); |
| 3852 | xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS); |
| 3853 | |
| 3854 | /* |
| 3855 | * If we are tracking the last record in the tree and |
| 3856 | * we are at the far right edge of the tree, update it. |
| 3857 | */ |
| 3858 | if (xfs_btree_is_lastrec(cur, block, level)) { |
| 3859 | cur->bc_ops->update_lastrec(cur, block, NULL, |
| 3860 | ptr, LASTREC_DELREC); |
| 3861 | } |
| 3862 | |
| 3863 | /* |
| 3864 | * We're at the root level. First, shrink the root block in-memory. |
| 3865 | * Try to get rid of the next level down. If we can't then there's |
| 3866 | * nothing left to do. |
| 3867 | */ |
| 3868 | if (level == cur->bc_nlevels - 1) { |
| 3869 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) { |
| 3870 | xfs_iroot_realloc(cur->bc_private.b.ip, -1, |
| 3871 | cur->bc_private.b.whichfork); |
| 3872 | |
| 3873 | error = xfs_btree_kill_iroot(cur); |
| 3874 | if (error) |
| 3875 | goto error0; |
| 3876 | |
| 3877 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3878 | if (error) |
| 3879 | goto error0; |
| 3880 | *stat = 1; |
| 3881 | return 0; |
| 3882 | } |
| 3883 | |
| 3884 | /* |
| 3885 | * If this is the root level, and there's only one entry left, |
| 3886 | * and it's NOT the leaf level, then we can get rid of this |
| 3887 | * level. |
| 3888 | */ |
| 3889 | if (numrecs == 1 && level > 0) { |
| 3890 | union xfs_btree_ptr *pp; |
| 3891 | /* |
| 3892 | * pp is still set to the first pointer in the block. |
| 3893 | * Make it the new root of the btree. |
| 3894 | */ |
| 3895 | pp = xfs_btree_ptr_addr(cur, 1, block); |
Christoph Hellwig | c0e59e1 | 2010-09-07 23:34:07 +0000 | [diff] [blame] | 3896 | error = xfs_btree_kill_root(cur, bp, level, pp); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3897 | if (error) |
| 3898 | goto error0; |
| 3899 | } else if (level > 0) { |
| 3900 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3901 | if (error) |
| 3902 | goto error0; |
| 3903 | } |
| 3904 | *stat = 1; |
| 3905 | return 0; |
| 3906 | } |
| 3907 | |
| 3908 | /* |
| 3909 | * If we deleted the leftmost entry in the block, update the |
| 3910 | * key values above us in the tree. |
| 3911 | */ |
Darrick J. Wong | 70b2265 | 2016-08-03 11:03:38 +1000 | [diff] [blame] | 3912 | if (xfs_btree_needs_key_update(cur, ptr)) { |
Darrick J. Wong | 973b831 | 2016-08-03 12:22:12 +1000 | [diff] [blame] | 3913 | error = xfs_btree_update_keys(cur, level); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3914 | if (error) |
| 3915 | goto error0; |
| 3916 | } |
| 3917 | |
| 3918 | /* |
| 3919 | * If the number of records remaining in the block is at least |
| 3920 | * the minimum, we're done. |
| 3921 | */ |
| 3922 | if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) { |
| 3923 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3924 | if (error) |
| 3925 | goto error0; |
| 3926 | return 0; |
| 3927 | } |
| 3928 | |
| 3929 | /* |
| 3930 | * Otherwise, we have to move some records around to keep the |
| 3931 | * tree balanced. Look at the left and right sibling blocks to |
| 3932 | * see if we can re-balance by moving only one record. |
| 3933 | */ |
| 3934 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); |
| 3935 | xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB); |
| 3936 | |
| 3937 | if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) { |
| 3938 | /* |
| 3939 | * One child of root, need to get a chance to copy its contents |
| 3940 | * into the root and delete it. Can't go up to next level, |
| 3941 | * there's nothing to delete there. |
| 3942 | */ |
| 3943 | if (xfs_btree_ptr_is_null(cur, &rptr) && |
| 3944 | xfs_btree_ptr_is_null(cur, &lptr) && |
| 3945 | level == cur->bc_nlevels - 2) { |
| 3946 | error = xfs_btree_kill_iroot(cur); |
| 3947 | if (!error) |
| 3948 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 3949 | if (error) |
| 3950 | goto error0; |
| 3951 | return 0; |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) || |
| 3956 | !xfs_btree_ptr_is_null(cur, &lptr)); |
| 3957 | |
| 3958 | /* |
| 3959 | * Duplicate the cursor so our btree manipulations here won't |
| 3960 | * disrupt the next level up. |
| 3961 | */ |
| 3962 | error = xfs_btree_dup_cursor(cur, &tcur); |
| 3963 | if (error) |
| 3964 | goto error0; |
| 3965 | |
| 3966 | /* |
| 3967 | * If there's a right sibling, see if it's ok to shift an entry |
| 3968 | * out of it. |
| 3969 | */ |
| 3970 | if (!xfs_btree_ptr_is_null(cur, &rptr)) { |
| 3971 | /* |
| 3972 | * Move the temp cursor to the last entry in the next block. |
| 3973 | * Actually any entry but the first would suffice. |
| 3974 | */ |
| 3975 | i = xfs_btree_lastrec(tcur, level); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3976 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3977 | |
| 3978 | error = xfs_btree_increment(tcur, level, &i); |
| 3979 | if (error) |
| 3980 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3981 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3982 | |
| 3983 | i = xfs_btree_lastrec(tcur, level); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 3984 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 3985 | |
| 3986 | /* Grab a pointer to the block. */ |
| 3987 | right = xfs_btree_get_block(tcur, level, &rbp); |
| 3988 | #ifdef DEBUG |
| 3989 | error = xfs_btree_check_block(tcur, right, level, rbp); |
| 3990 | if (error) |
| 3991 | goto error0; |
| 3992 | #endif |
| 3993 | /* Grab the current block number, for future use. */ |
| 3994 | xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB); |
| 3995 | |
| 3996 | /* |
| 3997 | * If right block is full enough so that removing one entry |
| 3998 | * won't make it too empty, and left-shifting an entry out |
| 3999 | * of right to us works, we're done. |
| 4000 | */ |
| 4001 | if (xfs_btree_get_numrecs(right) - 1 >= |
| 4002 | cur->bc_ops->get_minrecs(tcur, level)) { |
| 4003 | error = xfs_btree_lshift(tcur, level, &i); |
| 4004 | if (error) |
| 4005 | goto error0; |
| 4006 | if (i) { |
| 4007 | ASSERT(xfs_btree_get_numrecs(block) >= |
| 4008 | cur->bc_ops->get_minrecs(tcur, level)); |
| 4009 | |
| 4010 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 4011 | tcur = NULL; |
| 4012 | |
| 4013 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 4014 | if (error) |
| 4015 | goto error0; |
| 4016 | return 0; |
| 4017 | } |
| 4018 | } |
| 4019 | |
| 4020 | /* |
| 4021 | * Otherwise, grab the number of records in right for |
| 4022 | * future reference, and fix up the temp cursor to point |
| 4023 | * to our block again (last record). |
| 4024 | */ |
| 4025 | rrecs = xfs_btree_get_numrecs(right); |
| 4026 | if (!xfs_btree_ptr_is_null(cur, &lptr)) { |
| 4027 | i = xfs_btree_firstrec(tcur, level); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4028 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4029 | |
| 4030 | error = xfs_btree_decrement(tcur, level, &i); |
| 4031 | if (error) |
| 4032 | goto error0; |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4033 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | /* |
| 4038 | * If there's a left sibling, see if it's ok to shift an entry |
| 4039 | * out of it. |
| 4040 | */ |
| 4041 | if (!xfs_btree_ptr_is_null(cur, &lptr)) { |
| 4042 | /* |
| 4043 | * Move the temp cursor to the first entry in the |
| 4044 | * previous block. |
| 4045 | */ |
| 4046 | i = xfs_btree_firstrec(tcur, level); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4047 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4048 | |
| 4049 | error = xfs_btree_decrement(tcur, level, &i); |
| 4050 | if (error) |
| 4051 | goto error0; |
| 4052 | i = xfs_btree_firstrec(tcur, level); |
Eric Sandeen | c29aad4 | 2015-02-23 22:39:08 +1100 | [diff] [blame] | 4053 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4054 | |
| 4055 | /* Grab a pointer to the block. */ |
| 4056 | left = xfs_btree_get_block(tcur, level, &lbp); |
| 4057 | #ifdef DEBUG |
| 4058 | error = xfs_btree_check_block(cur, left, level, lbp); |
| 4059 | if (error) |
| 4060 | goto error0; |
| 4061 | #endif |
| 4062 | /* Grab the current block number, for future use. */ |
| 4063 | xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB); |
| 4064 | |
| 4065 | /* |
| 4066 | * If left block is full enough so that removing one entry |
| 4067 | * won't make it too empty, and right-shifting an entry out |
| 4068 | * of left to us works, we're done. |
| 4069 | */ |
| 4070 | if (xfs_btree_get_numrecs(left) - 1 >= |
| 4071 | cur->bc_ops->get_minrecs(tcur, level)) { |
| 4072 | error = xfs_btree_rshift(tcur, level, &i); |
| 4073 | if (error) |
| 4074 | goto error0; |
| 4075 | if (i) { |
| 4076 | ASSERT(xfs_btree_get_numrecs(block) >= |
| 4077 | cur->bc_ops->get_minrecs(tcur, level)); |
| 4078 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 4079 | tcur = NULL; |
| 4080 | if (level == 0) |
| 4081 | cur->bc_ptrs[0]++; |
| 4082 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 4083 | *stat = 1; |
| 4084 | return 0; |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | /* |
| 4089 | * Otherwise, grab the number of records in right for |
| 4090 | * future reference. |
| 4091 | */ |
| 4092 | lrecs = xfs_btree_get_numrecs(left); |
| 4093 | } |
| 4094 | |
| 4095 | /* Delete the temp cursor, we're done with it. */ |
| 4096 | xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR); |
| 4097 | tcur = NULL; |
| 4098 | |
| 4099 | /* If here, we need to do a join to keep the tree balanced. */ |
| 4100 | ASSERT(!xfs_btree_ptr_is_null(cur, &cptr)); |
| 4101 | |
| 4102 | if (!xfs_btree_ptr_is_null(cur, &lptr) && |
| 4103 | lrecs + xfs_btree_get_numrecs(block) <= |
| 4104 | cur->bc_ops->get_maxrecs(cur, level)) { |
| 4105 | /* |
| 4106 | * Set "right" to be the starting block, |
| 4107 | * "left" to be the left neighbor. |
| 4108 | */ |
| 4109 | rptr = cptr; |
| 4110 | right = block; |
| 4111 | rbp = bp; |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 4112 | error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4113 | if (error) |
| 4114 | goto error0; |
| 4115 | |
| 4116 | /* |
| 4117 | * If that won't work, see if we can join with the right neighbor block. |
| 4118 | */ |
| 4119 | } else if (!xfs_btree_ptr_is_null(cur, &rptr) && |
| 4120 | rrecs + xfs_btree_get_numrecs(block) <= |
| 4121 | cur->bc_ops->get_maxrecs(cur, level)) { |
| 4122 | /* |
| 4123 | * Set "left" to be the starting block, |
| 4124 | * "right" to be the right neighbor. |
| 4125 | */ |
| 4126 | lptr = cptr; |
| 4127 | left = block; |
| 4128 | lbp = bp; |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 4129 | error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4130 | if (error) |
| 4131 | goto error0; |
| 4132 | |
| 4133 | /* |
| 4134 | * Otherwise, we can't fix the imbalance. |
| 4135 | * Just return. This is probably a logic error, but it's not fatal. |
| 4136 | */ |
| 4137 | } else { |
| 4138 | error = xfs_btree_dec_cursor(cur, level, stat); |
| 4139 | if (error) |
| 4140 | goto error0; |
| 4141 | return 0; |
| 4142 | } |
| 4143 | |
| 4144 | rrecs = xfs_btree_get_numrecs(right); |
| 4145 | lrecs = xfs_btree_get_numrecs(left); |
| 4146 | |
| 4147 | /* |
| 4148 | * We're now going to join "left" and "right" by moving all the stuff |
| 4149 | * in "right" to "left" and deleting "right". |
| 4150 | */ |
| 4151 | XFS_BTREE_STATS_ADD(cur, moves, rrecs); |
| 4152 | if (level > 0) { |
| 4153 | /* It's a non-leaf. Move keys and pointers. */ |
| 4154 | union xfs_btree_key *lkp; /* left btree key */ |
| 4155 | union xfs_btree_ptr *lpp; /* left address pointer */ |
| 4156 | union xfs_btree_key *rkp; /* right btree key */ |
| 4157 | union xfs_btree_ptr *rpp; /* right address pointer */ |
| 4158 | |
| 4159 | lkp = xfs_btree_key_addr(cur, lrecs + 1, left); |
| 4160 | lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left); |
| 4161 | rkp = xfs_btree_key_addr(cur, 1, right); |
| 4162 | rpp = xfs_btree_ptr_addr(cur, 1, right); |
| 4163 | #ifdef DEBUG |
| 4164 | for (i = 1; i < rrecs; i++) { |
| 4165 | error = xfs_btree_check_ptr(cur, rpp, i, level); |
| 4166 | if (error) |
| 4167 | goto error0; |
| 4168 | } |
| 4169 | #endif |
| 4170 | xfs_btree_copy_keys(cur, lkp, rkp, rrecs); |
| 4171 | xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs); |
| 4172 | |
| 4173 | xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 4174 | xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 4175 | } else { |
| 4176 | /* It's a leaf. Move records. */ |
| 4177 | union xfs_btree_rec *lrp; /* left record pointer */ |
| 4178 | union xfs_btree_rec *rrp; /* right record pointer */ |
| 4179 | |
| 4180 | lrp = xfs_btree_rec_addr(cur, lrecs + 1, left); |
| 4181 | rrp = xfs_btree_rec_addr(cur, 1, right); |
| 4182 | |
| 4183 | xfs_btree_copy_recs(cur, lrp, rrp, rrecs); |
| 4184 | xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs); |
| 4185 | } |
| 4186 | |
| 4187 | XFS_BTREE_STATS_INC(cur, join); |
| 4188 | |
| 4189 | /* |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 4190 | * Fix up the number of records and right block pointer in the |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4191 | * surviving block, and log it. |
| 4192 | */ |
| 4193 | xfs_btree_set_numrecs(left, lrecs + rrecs); |
| 4194 | xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB), |
| 4195 | xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB); |
| 4196 | xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB); |
| 4197 | |
| 4198 | /* If there is a right sibling, point it to the remaining block. */ |
| 4199 | xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB); |
| 4200 | if (!xfs_btree_ptr_is_null(cur, &cptr)) { |
Eric Sandeen | 0d7409b | 2014-04-14 18:59:56 +1000 | [diff] [blame] | 4201 | error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4202 | if (error) |
| 4203 | goto error0; |
| 4204 | xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB); |
| 4205 | xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB); |
| 4206 | } |
| 4207 | |
| 4208 | /* Free the deleted block. */ |
Christoph Hellwig | c46ee8a | 2016-02-08 14:58:07 +1100 | [diff] [blame] | 4209 | error = xfs_btree_free_block(cur, rbp); |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4210 | if (error) |
| 4211 | goto error0; |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4212 | |
| 4213 | /* |
| 4214 | * If we joined with the left neighbor, set the buffer in the |
| 4215 | * cursor to the left block, and fix up the index. |
| 4216 | */ |
| 4217 | if (bp != lbp) { |
| 4218 | cur->bc_bufs[level] = lbp; |
| 4219 | cur->bc_ptrs[level] += lrecs; |
| 4220 | cur->bc_ra[level] = 0; |
| 4221 | } |
| 4222 | /* |
| 4223 | * If we joined with the right neighbor and there's a level above |
| 4224 | * us, increment the cursor at that level. |
| 4225 | */ |
| 4226 | else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || |
| 4227 | (level + 1 < cur->bc_nlevels)) { |
| 4228 | error = xfs_btree_increment(cur, level + 1, &i); |
| 4229 | if (error) |
| 4230 | goto error0; |
| 4231 | } |
| 4232 | |
| 4233 | /* |
| 4234 | * Readjust the ptr at this level if it's not a leaf, since it's |
| 4235 | * still pointing at the deletion point, which makes the cursor |
| 4236 | * inconsistent. If this makes the ptr 0, the caller fixes it up. |
| 4237 | * We can't use decrement because it would change the next level up. |
| 4238 | */ |
| 4239 | if (level > 0) |
| 4240 | cur->bc_ptrs[level]--; |
| 4241 | |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 4242 | /* |
| 4243 | * We combined blocks, so we have to update the parent keys if the |
| 4244 | * btree supports overlapped intervals. However, bc_ptrs[level + 1] |
| 4245 | * points to the old block so that the caller knows which record to |
| 4246 | * delete. Therefore, the caller must be savvy enough to call updkeys |
| 4247 | * for us if we return stat == 2. The other exit points from this |
| 4248 | * function don't require deletions further up the tree, so they can |
| 4249 | * call updkeys directly. |
| 4250 | */ |
| 4251 | |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4252 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 4253 | /* Return value means the next level up has something to do. */ |
| 4254 | *stat = 2; |
| 4255 | return 0; |
| 4256 | |
| 4257 | error0: |
| 4258 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 4259 | if (tcur) |
| 4260 | xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR); |
| 4261 | return error; |
| 4262 | } |
| 4263 | |
| 4264 | /* |
| 4265 | * Delete the record pointed to by cur. |
| 4266 | * The cursor refers to the place where the record was (could be inserted) |
| 4267 | * when the operation returns. |
| 4268 | */ |
| 4269 | int /* error */ |
| 4270 | xfs_btree_delete( |
| 4271 | struct xfs_btree_cur *cur, |
| 4272 | int *stat) /* success/failure */ |
| 4273 | { |
| 4274 | int error; /* error return value */ |
| 4275 | int level; |
| 4276 | int i; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 4277 | bool joined = false; |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4278 | |
| 4279 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY); |
| 4280 | |
| 4281 | /* |
| 4282 | * Go up the tree, starting at leaf level. |
| 4283 | * |
| 4284 | * If 2 is returned then a join was done; go to the next level. |
| 4285 | * Otherwise we are done. |
| 4286 | */ |
| 4287 | for (level = 0, i = 2; i == 2; level++) { |
| 4288 | error = xfs_btree_delrec(cur, level, &i); |
| 4289 | if (error) |
| 4290 | goto error0; |
Darrick J. Wong | 2c813ad | 2016-08-03 11:08:36 +1000 | [diff] [blame] | 4291 | if (i == 2) |
| 4292 | joined = true; |
| 4293 | } |
| 4294 | |
| 4295 | /* |
| 4296 | * If we combined blocks as part of deleting the record, delrec won't |
| 4297 | * have updated the parent high keys so we have to do that here. |
| 4298 | */ |
| 4299 | if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) { |
| 4300 | error = xfs_btree_updkeys_force(cur, 0); |
| 4301 | if (error) |
| 4302 | goto error0; |
Christoph Hellwig | 91cca5df | 2008-10-30 16:58:01 +1100 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | if (i == 0) { |
| 4306 | for (level = 1; level < cur->bc_nlevels; level++) { |
| 4307 | if (cur->bc_ptrs[level] == 0) { |
| 4308 | error = xfs_btree_decrement(cur, level, &i); |
| 4309 | if (error) |
| 4310 | goto error0; |
| 4311 | break; |
| 4312 | } |
| 4313 | } |
| 4314 | } |
| 4315 | |
| 4316 | XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT); |
| 4317 | *stat = i; |
| 4318 | return 0; |
| 4319 | error0: |
| 4320 | XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR); |
| 4321 | return error; |
| 4322 | } |
Christoph Hellwig | 8cc938f | 2008-10-30 16:58:11 +1100 | [diff] [blame] | 4323 | |
| 4324 | /* |
| 4325 | * Get the data from the pointed-to record. |
| 4326 | */ |
| 4327 | int /* error */ |
| 4328 | xfs_btree_get_rec( |
| 4329 | struct xfs_btree_cur *cur, /* btree cursor */ |
| 4330 | union xfs_btree_rec **recp, /* output: btree record */ |
| 4331 | int *stat) /* output: success/failure */ |
| 4332 | { |
| 4333 | struct xfs_btree_block *block; /* btree block */ |
| 4334 | struct xfs_buf *bp; /* buffer pointer */ |
| 4335 | int ptr; /* record number */ |
| 4336 | #ifdef DEBUG |
| 4337 | int error; /* error return value */ |
| 4338 | #endif |
| 4339 | |
| 4340 | ptr = cur->bc_ptrs[0]; |
| 4341 | block = xfs_btree_get_block(cur, 0, &bp); |
| 4342 | |
| 4343 | #ifdef DEBUG |
| 4344 | error = xfs_btree_check_block(cur, block, 0, bp); |
| 4345 | if (error) |
| 4346 | return error; |
| 4347 | #endif |
| 4348 | |
| 4349 | /* |
| 4350 | * Off the right end or left end, return failure. |
| 4351 | */ |
| 4352 | if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) { |
| 4353 | *stat = 0; |
| 4354 | return 0; |
| 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | * Point to the record and extract its data. |
| 4359 | */ |
| 4360 | *recp = xfs_btree_rec_addr(cur, ptr, block); |
| 4361 | *stat = 1; |
| 4362 | return 0; |
| 4363 | } |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4364 | |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4365 | /* Visit a block in a btree. */ |
| 4366 | STATIC int |
| 4367 | xfs_btree_visit_block( |
| 4368 | struct xfs_btree_cur *cur, |
| 4369 | int level, |
| 4370 | xfs_btree_visit_blocks_fn fn, |
| 4371 | void *data) |
| 4372 | { |
| 4373 | struct xfs_btree_block *block; |
| 4374 | struct xfs_buf *bp; |
| 4375 | union xfs_btree_ptr rptr; |
| 4376 | int error; |
| 4377 | |
| 4378 | /* do right sibling readahead */ |
| 4379 | xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA); |
| 4380 | block = xfs_btree_get_block(cur, level, &bp); |
| 4381 | |
| 4382 | /* process the block */ |
| 4383 | error = fn(cur, level, data); |
| 4384 | if (error) |
| 4385 | return error; |
| 4386 | |
| 4387 | /* now read rh sibling block for next iteration */ |
| 4388 | xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB); |
| 4389 | if (xfs_btree_ptr_is_null(cur, &rptr)) |
| 4390 | return -ENOENT; |
| 4391 | |
| 4392 | return xfs_btree_lookup_get_block(cur, level, &rptr, &block); |
| 4393 | } |
| 4394 | |
| 4395 | |
| 4396 | /* Visit every block in a btree. */ |
| 4397 | int |
| 4398 | xfs_btree_visit_blocks( |
| 4399 | struct xfs_btree_cur *cur, |
| 4400 | xfs_btree_visit_blocks_fn fn, |
| 4401 | void *data) |
| 4402 | { |
| 4403 | union xfs_btree_ptr lptr; |
| 4404 | int level; |
| 4405 | struct xfs_btree_block *block = NULL; |
| 4406 | int error = 0; |
| 4407 | |
| 4408 | cur->bc_ops->init_ptr_from_cur(cur, &lptr); |
| 4409 | |
| 4410 | /* for each level */ |
| 4411 | for (level = cur->bc_nlevels - 1; level >= 0; level--) { |
| 4412 | /* grab the left hand block */ |
| 4413 | error = xfs_btree_lookup_get_block(cur, level, &lptr, &block); |
| 4414 | if (error) |
| 4415 | return error; |
| 4416 | |
| 4417 | /* readahead the left most block for the next level down */ |
| 4418 | if (level > 0) { |
| 4419 | union xfs_btree_ptr *ptr; |
| 4420 | |
| 4421 | ptr = xfs_btree_ptr_addr(cur, 1, block); |
| 4422 | xfs_btree_readahead_ptr(cur, ptr, 1); |
| 4423 | |
| 4424 | /* save for the next iteration of the loop */ |
Eric Sandeen | a4d768e | 2017-05-22 19:54:10 -0700 | [diff] [blame] | 4425 | xfs_btree_copy_ptrs(cur, &lptr, ptr, 1); |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | /* for each buffer in the level */ |
| 4429 | do { |
| 4430 | error = xfs_btree_visit_block(cur, level, fn, data); |
| 4431 | } while (!error); |
| 4432 | |
| 4433 | if (error != -ENOENT) |
| 4434 | return error; |
| 4435 | } |
| 4436 | |
| 4437 | return 0; |
| 4438 | } |
| 4439 | |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4440 | /* |
| 4441 | * Change the owner of a btree. |
| 4442 | * |
| 4443 | * The mechanism we use here is ordered buffer logging. Because we don't know |
| 4444 | * how many buffers were are going to need to modify, we don't really want to |
| 4445 | * have to make transaction reservations for the worst case of every buffer in a |
| 4446 | * full size btree as that may be more space that we can fit in the log.... |
| 4447 | * |
| 4448 | * We do the btree walk in the most optimal manner possible - we have sibling |
| 4449 | * pointers so we can just walk all the blocks on each level from left to right |
| 4450 | * in a single pass, and then move to the next level and do the same. We can |
| 4451 | * also do readahead on the sibling pointers to get IO moving more quickly, |
| 4452 | * though for slow disks this is unlikely to make much difference to performance |
| 4453 | * as the amount of CPU work we have to do before moving to the next block is |
| 4454 | * relatively small. |
| 4455 | * |
| 4456 | * For each btree block that we load, modify the owner appropriately, set the |
| 4457 | * buffer as an ordered buffer and log it appropriately. We need to ensure that |
| 4458 | * we mark the region we change dirty so that if the buffer is relogged in |
| 4459 | * a subsequent transaction the changes we make here as an ordered buffer are |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame] | 4460 | * correctly relogged in that transaction. If we are in recovery context, then |
| 4461 | * just queue the modified buffer as delayed write buffer so the transaction |
| 4462 | * recovery completion writes the changes to disk. |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4463 | */ |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4464 | struct xfs_btree_block_change_owner_info { |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 4465 | uint64_t new_owner; |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4466 | struct list_head *buffer_list; |
| 4467 | }; |
| 4468 | |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4469 | static int |
| 4470 | xfs_btree_block_change_owner( |
| 4471 | struct xfs_btree_cur *cur, |
| 4472 | int level, |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4473 | void *data) |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4474 | { |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4475 | struct xfs_btree_block_change_owner_info *bbcoi = data; |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4476 | struct xfs_btree_block *block; |
| 4477 | struct xfs_buf *bp; |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4478 | |
| 4479 | /* modify the owner */ |
| 4480 | block = xfs_btree_get_block(cur, level, &bp); |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 4481 | if (cur->bc_flags & XFS_BTREE_LONG_PTRS) { |
| 4482 | if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner)) |
| 4483 | return 0; |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4484 | block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner); |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 4485 | } else { |
| 4486 | if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner)) |
| 4487 | return 0; |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4488 | block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner); |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 4489 | } |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4490 | |
| 4491 | /* |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame] | 4492 | * If the block is a root block hosted in an inode, we might not have a |
| 4493 | * buffer pointer here and we shouldn't attempt to log the change as the |
| 4494 | * information is already held in the inode and discarded when the root |
| 4495 | * block is formatted into the on-disk inode fork. We still change it, |
| 4496 | * though, so everything is consistent in memory. |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4497 | */ |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 4498 | if (!bp) { |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4499 | ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE); |
| 4500 | ASSERT(level == cur->bc_nlevels - 1); |
Brian Foster | 2dd3d70 | 2017-08-29 10:08:40 -0700 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
| 4504 | if (cur->bc_tp) { |
| 4505 | if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) { |
| 4506 | xfs_btree_log_block(cur, bp, XFS_BB_OWNER); |
| 4507 | return -EAGAIN; |
| 4508 | } |
| 4509 | } else { |
| 4510 | xfs_buf_delwri_queue(bp, bbcoi->buffer_list); |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4511 | } |
| 4512 | |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4513 | return 0; |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | int |
| 4517 | xfs_btree_change_owner( |
| 4518 | struct xfs_btree_cur *cur, |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 4519 | uint64_t new_owner, |
Dave Chinner | 638f4416 | 2013-08-30 10:23:45 +1000 | [diff] [blame] | 4520 | struct list_head *buffer_list) |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4521 | { |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4522 | struct xfs_btree_block_change_owner_info bbcoi; |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4523 | |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4524 | bbcoi.new_owner = new_owner; |
| 4525 | bbcoi.buffer_list = buffer_list; |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4526 | |
Darrick J. Wong | 28a8956 | 2016-08-03 11:10:55 +1000 | [diff] [blame] | 4527 | return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner, |
| 4528 | &bbcoi); |
Dave Chinner | 21b5c97 | 2013-08-30 10:23:44 +1000 | [diff] [blame] | 4529 | } |
Darrick J. Wong | c5ab131 | 2016-01-04 16:13:21 +1100 | [diff] [blame] | 4530 | |
| 4531 | /** |
| 4532 | * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format |
| 4533 | * btree block |
| 4534 | * |
| 4535 | * @bp: buffer containing the btree block |
| 4536 | * @max_recs: pointer to the m_*_mxr max records field in the xfs mount |
| 4537 | * @pag_max_level: pointer to the per-ag max level field |
| 4538 | */ |
| 4539 | bool |
| 4540 | xfs_btree_sblock_v5hdr_verify( |
| 4541 | struct xfs_buf *bp) |
| 4542 | { |
| 4543 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 4544 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4545 | struct xfs_perag *pag = bp->b_pag; |
| 4546 | |
| 4547 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 4548 | return false; |
| 4549 | if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid)) |
| 4550 | return false; |
| 4551 | if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn)) |
| 4552 | return false; |
| 4553 | if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno) |
| 4554 | return false; |
| 4555 | return true; |
| 4556 | } |
| 4557 | |
| 4558 | /** |
| 4559 | * xfs_btree_sblock_verify() -- verify a short-format btree block |
| 4560 | * |
| 4561 | * @bp: buffer containing the btree block |
| 4562 | * @max_recs: maximum records allowed in this btree node |
| 4563 | */ |
| 4564 | bool |
| 4565 | xfs_btree_sblock_verify( |
| 4566 | struct xfs_buf *bp, |
| 4567 | unsigned int max_recs) |
| 4568 | { |
| 4569 | struct xfs_mount *mp = bp->b_target->bt_mount; |
| 4570 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); |
| 4571 | |
| 4572 | /* numrecs verification */ |
| 4573 | if (be16_to_cpu(block->bb_numrecs) > max_recs) |
| 4574 | return false; |
| 4575 | |
| 4576 | /* sibling pointer verification */ |
| 4577 | if (!block->bb_u.s.bb_leftsib || |
| 4578 | (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks && |
| 4579 | block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK))) |
| 4580 | return false; |
| 4581 | if (!block->bb_u.s.bb_rightsib || |
| 4582 | (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks && |
| 4583 | block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK))) |
| 4584 | return false; |
| 4585 | |
| 4586 | return true; |
| 4587 | } |
Darrick J. Wong | 19b54ee | 2016-06-21 11:53:28 +1000 | [diff] [blame] | 4588 | |
| 4589 | /* |
| 4590 | * Calculate the number of btree levels needed to store a given number of |
| 4591 | * records in a short-format btree. |
| 4592 | */ |
| 4593 | uint |
| 4594 | xfs_btree_compute_maxlevels( |
| 4595 | struct xfs_mount *mp, |
| 4596 | uint *limits, |
| 4597 | unsigned long len) |
| 4598 | { |
| 4599 | uint level; |
| 4600 | unsigned long maxblocks; |
| 4601 | |
| 4602 | maxblocks = (len + limits[0] - 1) / limits[0]; |
| 4603 | for (level = 1; maxblocks > 1; level++) |
| 4604 | maxblocks = (maxblocks + limits[1] - 1) / limits[1]; |
| 4605 | return level; |
| 4606 | } |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4607 | |
| 4608 | /* |
| 4609 | * Query a regular btree for all records overlapping a given interval. |
| 4610 | * Start with a LE lookup of the key of low_rec and return all records |
| 4611 | * until we find a record with a key greater than the key of high_rec. |
| 4612 | */ |
| 4613 | STATIC int |
| 4614 | xfs_btree_simple_query_range( |
| 4615 | struct xfs_btree_cur *cur, |
| 4616 | union xfs_btree_key *low_key, |
| 4617 | union xfs_btree_key *high_key, |
| 4618 | xfs_btree_query_range_fn fn, |
| 4619 | void *priv) |
| 4620 | { |
| 4621 | union xfs_btree_rec *recp; |
| 4622 | union xfs_btree_key rec_key; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 4623 | int64_t diff; |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4624 | int stat; |
| 4625 | bool firstrec = true; |
| 4626 | int error; |
| 4627 | |
| 4628 | ASSERT(cur->bc_ops->init_high_key_from_rec); |
| 4629 | ASSERT(cur->bc_ops->diff_two_keys); |
| 4630 | |
| 4631 | /* |
| 4632 | * Find the leftmost record. The btree cursor must be set |
| 4633 | * to the low record used to generate low_key. |
| 4634 | */ |
| 4635 | stat = 0; |
| 4636 | error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat); |
| 4637 | if (error) |
| 4638 | goto out; |
| 4639 | |
Darrick J. Wong | 5b5c2db | 2016-08-26 16:00:10 +1000 | [diff] [blame] | 4640 | /* Nothing? See if there's anything to the right. */ |
| 4641 | if (!stat) { |
| 4642 | error = xfs_btree_increment(cur, 0, &stat); |
| 4643 | if (error) |
| 4644 | goto out; |
| 4645 | } |
| 4646 | |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4647 | while (stat) { |
| 4648 | /* Find the record. */ |
| 4649 | error = xfs_btree_get_rec(cur, &recp, &stat); |
| 4650 | if (error || !stat) |
| 4651 | break; |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4652 | |
| 4653 | /* Skip if high_key(rec) < low_key. */ |
| 4654 | if (firstrec) { |
Darrick J. Wong | 7222789 | 2016-08-26 15:59:50 +1000 | [diff] [blame] | 4655 | cur->bc_ops->init_high_key_from_rec(&rec_key, recp); |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4656 | firstrec = false; |
| 4657 | diff = cur->bc_ops->diff_two_keys(cur, low_key, |
| 4658 | &rec_key); |
| 4659 | if (diff > 0) |
| 4660 | goto advloop; |
| 4661 | } |
| 4662 | |
| 4663 | /* Stop if high_key < low_key(rec). */ |
Darrick J. Wong | 7222789 | 2016-08-26 15:59:50 +1000 | [diff] [blame] | 4664 | cur->bc_ops->init_key_from_rec(&rec_key, recp); |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4665 | diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key); |
| 4666 | if (diff > 0) |
| 4667 | break; |
| 4668 | |
| 4669 | /* Callback */ |
| 4670 | error = fn(cur, recp, priv); |
| 4671 | if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT) |
| 4672 | break; |
| 4673 | |
| 4674 | advloop: |
| 4675 | /* Move on to the next record. */ |
| 4676 | error = xfs_btree_increment(cur, 0, &stat); |
| 4677 | if (error) |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | out: |
| 4682 | return error; |
| 4683 | } |
| 4684 | |
| 4685 | /* |
| 4686 | * Query an overlapped interval btree for all records overlapping a given |
| 4687 | * interval. This function roughly follows the algorithm given in |
| 4688 | * "Interval Trees" of _Introduction to Algorithms_, which is section |
| 4689 | * 14.3 in the 2nd and 3rd editions. |
| 4690 | * |
| 4691 | * First, generate keys for the low and high records passed in. |
| 4692 | * |
| 4693 | * For any leaf node, generate the high and low keys for the record. |
| 4694 | * If the record keys overlap with the query low/high keys, pass the |
| 4695 | * record to the function iterator. |
| 4696 | * |
| 4697 | * For any internal node, compare the low and high keys of each |
| 4698 | * pointer against the query low/high keys. If there's an overlap, |
| 4699 | * follow the pointer. |
| 4700 | * |
| 4701 | * As an optimization, we stop scanning a block when we find a low key |
| 4702 | * that is greater than the query's high key. |
| 4703 | */ |
| 4704 | STATIC int |
| 4705 | xfs_btree_overlapped_query_range( |
| 4706 | struct xfs_btree_cur *cur, |
| 4707 | union xfs_btree_key *low_key, |
| 4708 | union xfs_btree_key *high_key, |
| 4709 | xfs_btree_query_range_fn fn, |
| 4710 | void *priv) |
| 4711 | { |
| 4712 | union xfs_btree_ptr ptr; |
| 4713 | union xfs_btree_ptr *pp; |
| 4714 | union xfs_btree_key rec_key; |
| 4715 | union xfs_btree_key rec_hkey; |
| 4716 | union xfs_btree_key *lkp; |
| 4717 | union xfs_btree_key *hkp; |
| 4718 | union xfs_btree_rec *recp; |
| 4719 | struct xfs_btree_block *block; |
Darrick J. Wong | c8ce540 | 2017-06-16 11:00:05 -0700 | [diff] [blame] | 4720 | int64_t ldiff; |
| 4721 | int64_t hdiff; |
Darrick J. Wong | 105f7d8 | 2016-08-03 11:10:21 +1000 | [diff] [blame] | 4722 | int level; |
| 4723 | struct xfs_buf *bp; |
| 4724 | int i; |
| 4725 | int error; |
| 4726 | |
| 4727 | /* Load the root of the btree. */ |
| 4728 | level = cur->bc_nlevels - 1; |
| 4729 | cur->bc_ops->init_ptr_from_cur(cur, &ptr); |
| 4730 | error = xfs_btree_lookup_get_block(cur, level, &ptr, &block); |
| 4731 | if (error) |
| 4732 | return error; |
| 4733 | xfs_btree_get_block(cur, level, &bp); |
| 4734 | trace_xfs_btree_overlapped_query_range(cur, level, bp); |
| 4735 | #ifdef DEBUG |
| 4736 | error = xfs_btree_check_block(cur, block, level, bp); |
| 4737 | if (error) |
| 4738 | goto out; |
| 4739 | #endif |
| 4740 | cur->bc_ptrs[level] = 1; |
| 4741 | |
| 4742 | while (level < cur->bc_nlevels) { |
| 4743 | block = xfs_btree_get_block(cur, level, &bp); |
| 4744 | |
| 4745 | /* End of node, pop back towards the root. */ |
| 4746 | if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) { |
| 4747 | pop_up: |
| 4748 | if (level < cur->bc_nlevels - 1) |
| 4749 | cur->bc_ptrs[level + 1]++; |
| 4750 | level++; |
| 4751 | continue; |
| 4752 | } |
| 4753 | |
| 4754 | if (level == 0) { |
| 4755 | /* Handle a leaf node. */ |
| 4756 | recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block); |
| 4757 | |
| 4758 | cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp); |
| 4759 | ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey, |
| 4760 | low_key); |
| 4761 | |
| 4762 | cur->bc_ops->init_key_from_rec(&rec_key, recp); |
| 4763 | hdiff = cur->bc_ops->diff_two_keys(cur, high_key, |
| 4764 | &rec_key); |
| 4765 | |
| 4766 | /* |
| 4767 | * If (record's high key >= query's low key) and |
| 4768 | * (query's high key >= record's low key), then |
| 4769 | * this record overlaps the query range; callback. |
| 4770 | */ |
| 4771 | if (ldiff >= 0 && hdiff >= 0) { |
| 4772 | error = fn(cur, recp, priv); |
| 4773 | if (error < 0 || |
| 4774 | error == XFS_BTREE_QUERY_RANGE_ABORT) |
| 4775 | break; |
| 4776 | } else if (hdiff < 0) { |
| 4777 | /* Record is larger than high key; pop. */ |
| 4778 | goto pop_up; |
| 4779 | } |
| 4780 | cur->bc_ptrs[level]++; |
| 4781 | continue; |
| 4782 | } |
| 4783 | |
| 4784 | /* Handle an internal node. */ |
| 4785 | lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block); |
| 4786 | hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block); |
| 4787 | pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block); |
| 4788 | |
| 4789 | ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key); |
| 4790 | hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp); |
| 4791 | |
| 4792 | /* |
| 4793 | * If (pointer's high key >= query's low key) and |
| 4794 | * (query's high key >= pointer's low key), then |
| 4795 | * this record overlaps the query range; follow pointer. |
| 4796 | */ |
| 4797 | if (ldiff >= 0 && hdiff >= 0) { |
| 4798 | level--; |
| 4799 | error = xfs_btree_lookup_get_block(cur, level, pp, |
| 4800 | &block); |
| 4801 | if (error) |
| 4802 | goto out; |
| 4803 | xfs_btree_get_block(cur, level, &bp); |
| 4804 | trace_xfs_btree_overlapped_query_range(cur, level, bp); |
| 4805 | #ifdef DEBUG |
| 4806 | error = xfs_btree_check_block(cur, block, level, bp); |
| 4807 | if (error) |
| 4808 | goto out; |
| 4809 | #endif |
| 4810 | cur->bc_ptrs[level] = 1; |
| 4811 | continue; |
| 4812 | } else if (hdiff < 0) { |
| 4813 | /* The low key is larger than the upper range; pop. */ |
| 4814 | goto pop_up; |
| 4815 | } |
| 4816 | cur->bc_ptrs[level]++; |
| 4817 | } |
| 4818 | |
| 4819 | out: |
| 4820 | /* |
| 4821 | * If we don't end this function with the cursor pointing at a record |
| 4822 | * block, a subsequent non-error cursor deletion will not release |
| 4823 | * node-level buffers, causing a buffer leak. This is quite possible |
| 4824 | * with a zero-results range query, so release the buffers if we |
| 4825 | * failed to return any results. |
| 4826 | */ |
| 4827 | if (cur->bc_bufs[0] == NULL) { |
| 4828 | for (i = 0; i < cur->bc_nlevels; i++) { |
| 4829 | if (cur->bc_bufs[i]) { |
| 4830 | xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]); |
| 4831 | cur->bc_bufs[i] = NULL; |
| 4832 | cur->bc_ptrs[i] = 0; |
| 4833 | cur->bc_ra[i] = 0; |
| 4834 | } |
| 4835 | } |
| 4836 | } |
| 4837 | |
| 4838 | return error; |
| 4839 | } |
| 4840 | |
| 4841 | /* |
| 4842 | * Query a btree for all records overlapping a given interval of keys. The |
| 4843 | * supplied function will be called with each record found; return one of the |
| 4844 | * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error |
| 4845 | * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a |
| 4846 | * negative error code. |
| 4847 | */ |
| 4848 | int |
| 4849 | xfs_btree_query_range( |
| 4850 | struct xfs_btree_cur *cur, |
| 4851 | union xfs_btree_irec *low_rec, |
| 4852 | union xfs_btree_irec *high_rec, |
| 4853 | xfs_btree_query_range_fn fn, |
| 4854 | void *priv) |
| 4855 | { |
| 4856 | union xfs_btree_rec rec; |
| 4857 | union xfs_btree_key low_key; |
| 4858 | union xfs_btree_key high_key; |
| 4859 | |
| 4860 | /* Find the keys of both ends of the interval. */ |
| 4861 | cur->bc_rec = *high_rec; |
| 4862 | cur->bc_ops->init_rec_from_cur(cur, &rec); |
| 4863 | cur->bc_ops->init_key_from_rec(&high_key, &rec); |
| 4864 | |
| 4865 | cur->bc_rec = *low_rec; |
| 4866 | cur->bc_ops->init_rec_from_cur(cur, &rec); |
| 4867 | cur->bc_ops->init_key_from_rec(&low_key, &rec); |
| 4868 | |
| 4869 | /* Enforce low key < high key. */ |
| 4870 | if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0) |
| 4871 | return -EINVAL; |
| 4872 | |
| 4873 | if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING)) |
| 4874 | return xfs_btree_simple_query_range(cur, &low_key, |
| 4875 | &high_key, fn, priv); |
| 4876 | return xfs_btree_overlapped_query_range(cur, &low_key, &high_key, |
| 4877 | fn, priv); |
| 4878 | } |
Darrick J. Wong | 4ed3f68 | 2016-09-19 10:25:03 +1000 | [diff] [blame] | 4879 | |
Darrick J. Wong | e9a2599 | 2017-03-28 14:56:35 -0700 | [diff] [blame] | 4880 | /* Query a btree for all records. */ |
| 4881 | int |
| 4882 | xfs_btree_query_all( |
| 4883 | struct xfs_btree_cur *cur, |
| 4884 | xfs_btree_query_range_fn fn, |
| 4885 | void *priv) |
| 4886 | { |
Darrick J. Wong | 5a4c733 | 2017-06-16 11:00:04 -0700 | [diff] [blame] | 4887 | union xfs_btree_key low_key; |
| 4888 | union xfs_btree_key high_key; |
Darrick J. Wong | e9a2599 | 2017-03-28 14:56:35 -0700 | [diff] [blame] | 4889 | |
Darrick J. Wong | 5a4c733 | 2017-06-16 11:00:04 -0700 | [diff] [blame] | 4890 | memset(&cur->bc_rec, 0, sizeof(cur->bc_rec)); |
| 4891 | memset(&low_key, 0, sizeof(low_key)); |
| 4892 | memset(&high_key, 0xFF, sizeof(high_key)); |
| 4893 | |
| 4894 | return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv); |
Darrick J. Wong | e9a2599 | 2017-03-28 14:56:35 -0700 | [diff] [blame] | 4895 | } |
| 4896 | |
Darrick J. Wong | 4ed3f68 | 2016-09-19 10:25:03 +1000 | [diff] [blame] | 4897 | /* |
| 4898 | * Calculate the number of blocks needed to store a given number of records |
| 4899 | * in a short-format (per-AG metadata) btree. |
| 4900 | */ |
| 4901 | xfs_extlen_t |
| 4902 | xfs_btree_calc_size( |
| 4903 | struct xfs_mount *mp, |
| 4904 | uint *limits, |
| 4905 | unsigned long long len) |
| 4906 | { |
| 4907 | int level; |
| 4908 | int maxrecs; |
| 4909 | xfs_extlen_t rval; |
| 4910 | |
| 4911 | maxrecs = limits[0]; |
| 4912 | for (level = 0, rval = 0; len > 1; level++) { |
| 4913 | len += maxrecs - 1; |
| 4914 | do_div(len, maxrecs); |
| 4915 | maxrecs = limits[1]; |
| 4916 | rval += len; |
| 4917 | } |
| 4918 | return rval; |
| 4919 | } |
Darrick J. Wong | c611cc0 | 2016-09-19 10:25:20 +1000 | [diff] [blame] | 4920 | |
Eric Biggers | f1b8243 | 2016-10-20 15:42:30 +1100 | [diff] [blame] | 4921 | static int |
Darrick J. Wong | c611cc0 | 2016-09-19 10:25:20 +1000 | [diff] [blame] | 4922 | xfs_btree_count_blocks_helper( |
| 4923 | struct xfs_btree_cur *cur, |
| 4924 | int level, |
| 4925 | void *data) |
| 4926 | { |
| 4927 | xfs_extlen_t *blocks = data; |
| 4928 | (*blocks)++; |
| 4929 | |
| 4930 | return 0; |
| 4931 | } |
| 4932 | |
| 4933 | /* Count the blocks in a btree and return the result in *blocks. */ |
| 4934 | int |
| 4935 | xfs_btree_count_blocks( |
| 4936 | struct xfs_btree_cur *cur, |
| 4937 | xfs_extlen_t *blocks) |
| 4938 | { |
| 4939 | *blocks = 0; |
| 4940 | return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper, |
| 4941 | blocks); |
| 4942 | } |