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