blob: fe5f27e4cdc54321d8fba9d82750b7281a7c3952 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100026#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Christoph Hellwig38bb7422008-10-30 16:56:22 +110029#include "xfs_inode_item.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050030#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000033#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050034#include "xfs_cksum.h"
Dave Chinnercf11da92014-07-15 07:08:24 +100035#include "xfs_alloc.h"
Brian Fostera45086e2015-10-12 15:59:25 +110036#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Cursor allocation zone.
40 */
41kmem_zone_t *xfs_btree_cur_zone;
42
43/*
44 * Btree magic numbers.
45 */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050046static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
Darrick J. Wongb8704942016-08-03 11:30:32 +100047 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
Brian Fosteraafc3c22014-04-24 16:00:52 +100048 XFS_FIBT_MAGIC },
Darrick J. Wongb8704942016-08-03 11:30:32 +100049 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
Brian Fosteraafc3c22014-04-24 16:00:52 +100050 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050052#define xfs_btree_magic(cur) \
53 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110055STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110056xfs_btree_check_lblock(
57 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110058 struct xfs_btree_block *block, /* btree long form block pointer */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110059 int level, /* level of the btree block */
60 struct xfs_buf *bp) /* buffer for block, if any */
61{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050062 int lblock_ok = 1; /* block passes checks */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110063 struct xfs_mount *mp; /* file system mount point */
64
65 mp = cur->bc_mp;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050066
67 if (xfs_sb_version_hascrc(&mp->m_sb)) {
68 lblock_ok = lblock_ok &&
Eric Sandeence748ea2015-07-29 11:53:31 +100069 uuid_equal(&block->bb_u.l.bb_uuid,
70 &mp->m_sb.sb_meta_uuid) &&
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050071 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 Hellwiga23f6ef2008-10-30 16:54:53 +110077 be16_to_cpu(block->bb_level) == level &&
78 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +110079 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110080 block->bb_u.l.bb_leftsib &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100081 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110082 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050083 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110084 block->bb_u.l.bb_rightsib &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100085 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110086 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050087 be64_to_cpu(block->bb_u.l.bb_rightsib)));
88
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110089 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
90 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
91 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
92 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000093 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050094 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +100095 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110096 }
97 return 0;
98}
99
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100100STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101xfs_btree_check_sblock(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100102 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100103 struct xfs_btree_block *block, /* btree short form block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int level, /* level of the btree block */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100105 struct xfs_buf *bp) /* buffer containing block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500107 struct xfs_mount *mp; /* file system mount point */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100108 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
109 struct xfs_agf *agf; /* ag. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 xfs_agblock_t agflen; /* native ag. freespace length */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500111 int sblock_ok = 1; /* block passes checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500113 mp = cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 agbp = cur->bc_private.a.agbp;
115 agf = XFS_BUF_TO_AGF(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100116 agflen = be32_to_cpu(agf->agf_length);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500117
118 if (xfs_sb_version_hascrc(&mp->m_sb)) {
119 sblock_ok = sblock_ok &&
Eric Sandeence748ea2015-07-29 11:53:31 +1000120 uuid_equal(&block->bb_u.s.bb_uuid,
121 &mp->m_sb.sb_meta_uuid) &&
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500122 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 Hellwig16259e72005-11-02 15:11:25 +1100128 be16_to_cpu(block->bb_level) == level &&
129 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100130 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200131 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100132 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
133 block->bb_u.s.bb_leftsib &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200134 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100135 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
136 block->bb_u.s.bb_rightsib;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500137
138 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
140 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
141 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000142 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500143 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000144 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 return 0;
147}
148
149/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100150 * Debug routine: check that block header is ok.
151 */
152int
153xfs_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 Hellwig7cc95a82008-10-30 17:14:34 +1100159 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 Hellwiga23f6ef2008-10-30 16:54:53 +1100163}
164
165/*
166 * Check that (long) pointer is ok.
167 */
168int /* error (0 or EFSCORRUPTED) */
169xfs_btree_check_lptr(
170 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000171 xfs_fsblock_t bno, /* btree block disk address */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100172 int level) /* btree block level */
173{
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100174 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100175 level > 0 &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000176 bno != NULLFSBLOCK &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100177 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
178 return 0;
179}
180
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100181#ifdef DEBUG
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100182/*
183 * Check that (short) pointer is ok.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100185STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186xfs_btree_check_sptr(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100187 struct xfs_btree_cur *cur, /* btree cursor */
188 xfs_agblock_t bno, /* btree block disk address */
189 int level) /* btree block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100191 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100193 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 level > 0 &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100195 bno != NULLAGBLOCK &&
196 bno != 0 &&
197 bno < agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
201/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100202 * Check that block ptr is ok.
203 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100204STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100205xfs_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 McIlroy24ee0e42008-10-30 17:05:26 +1100219#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100220
221/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500222 * 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 Tangfef4ded2015-10-12 16:02:32 +1100226 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500227 * it to disk.
228 */
229void
230xfs_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 Sandeenf1dbcd72014-02-27 15:18:23 +1100240 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500241}
242
243bool
244xfs_btree_lblock_verify_crc(
245 struct xfs_buf *bp)
246{
Brian Fostera45086e2015-10-12 15:59:25 +1100247 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 Sandeen51582172014-02-27 15:17:27 +1100253 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100254 }
Eric Sandeen51582172014-02-27 15:17:27 +1100255
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500256 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 Tangfef4ded2015-10-12 16:02:32 +1100264 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500265 * it to disk.
266 */
267void
268xfs_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 Sandeenf1dbcd72014-02-27 15:18:23 +1100278 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500279}
280
281bool
282xfs_btree_sblock_verify_crc(
283 struct xfs_buf *bp)
284{
Brian Fostera45086e2015-10-12 15:59:25 +1100285 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 Sandeen51582172014-02-27 15:17:27 +1100291 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100292 }
Eric Sandeen51582172014-02-27 15:17:27 +1100293
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500294 return true;
295}
296
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100297static int
298xfs_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 Hellwigedfd9dd2016-02-08 14:58:07 +1100305 if (!error) {
306 xfs_trans_binval(cur->bc_tp, bp);
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100307 XFS_BTREE_STATS_INC(cur, free);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100308 }
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100309 return error;
310}
311
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500312/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * Delete the btree cursor.
314 */
315void
316xfs_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 Hellwigc0e59e12010-09-07 23:34:07 +0000334 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 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 */
354int /* error */
355xfs_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 Hellwig561f7d12008-10-30 16:53:59 +1100368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
370 * Allocate a new cursor like the old one.
371 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100372 new = cur->bc_ops->dup_cursor(cur);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * Copy the record currently in the cursor.
376 */
377 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
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 Chinnerc3f8fc72012-11-12 22:54:01 +1100385 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 Chinner3d3e6f62012-11-12 22:54:08 +1100389 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100390 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100391 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 xfs_btree_del_cursor(new, error);
393 *ncur = NULL;
394 return error;
395 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500396 }
397 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 *ncur = new;
400 return 0;
401}
402
403/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100404 * 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. Wong2c813ad2016-08-03 11:08:36 +1000431 *
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 Hellwig65f1eae2008-10-30 16:55:34 +1100475 */
476
477/*
478 * Return size of the btree block header for this btree instance.
479 */
480static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
481{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500482 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 Hellwig65f1eae2008-10-30 16:55:34 +1100490}
491
492/*
493 * Return size of btree block pointers for this btree instance.
494 */
495static 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 */
504STATIC size_t
505xfs_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 */
516STATIC size_t
517xfs_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. Wong2c813ad2016-08-03 11:08:36 +1000526 * Calculate offset of the n-th high key in a btree block.
527 */
528STATIC size_t
529xfs_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 Hellwig65f1eae2008-10-30 16:55:34 +1100538 * Calculate offset of the n-th block pointer in a btree block.
539 */
540STATIC size_t
541xfs_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 */
554STATIC union xfs_btree_rec *
555xfs_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 */
567STATIC union xfs_btree_key *
568xfs_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. Wong2c813ad2016-08-03 11:08:36 +1000578 * Return a pointer to the n-th high key in the btree block.
579 */
580STATIC union xfs_btree_key *
581xfs_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 Hellwig65f1eae2008-10-30 16:55:34 +1100591 * Return a pointer to the n-th block pointer in the btree block.
592 */
593STATIC union xfs_btree_ptr *
594xfs_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 Wu1cb93862013-08-07 10:11:05 +0000608 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100609 *
610 * For now this btree implementation assumes the btree root is always
611 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100613STATIC struct xfs_btree_block *
614xfs_btree_get_iroot(
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000615 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000617 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000619 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
620 return (struct xfs_btree_block *)ifp->if_broot;
Christoph Hellwig8186e512008-10-30 16:54:22 +1100621}
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 */
627STATIC struct xfs_btree_block * /* generic btree block pointer */
628xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100638
639 *bpp = cur->bc_bufs[level];
640 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/*
644 * Get a buffer for the block, return it with no data read.
645 * Long-form addressing.
646 */
647xfs_buf_t * /* buffer for fsbno */
648xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700654 xfs_daddr_t d; /* real disk block address */
655
656 ASSERT(fsbno != NULLFSBLOCK);
657 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000658 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/*
662 * Get a buffer for the block, return it with no data read.
663 * Short-form addressing.
664 */
665xfs_buf_t * /* buffer for agno/agbno */
666xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700673 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 Chinner36de9552014-06-06 16:02:12 +1000678 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * Check for the cursor referring to the last block at the given level.
683 */
684int /* 1=is last block, 0=not last block */
685xfs_btree_islastblock(
686 xfs_btree_cur_t *cur, /* btree cursor */
687 int level) /* level to check */
688{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100689 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 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 Hellwige99ab902008-10-30 16:54:33 +1100694 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000695 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200697 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000701 * Change the cursor to point to the first record at the given level.
702 * Other levels are unaffected.
703 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100704STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000705xfs_btree_firstrec(
706 xfs_btree_cur_t *cur, /* btree cursor */
707 int level) /* level to change */
708{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100709 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000710 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 Hellwigf2277f02008-10-30 16:53:47 +1100720 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000721 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 Torvalds1da177e2005-04-16 15:20:36 -0700730 * Change the cursor to point to the last record in the current block
731 * at the given level. Other levels are unaffected.
732 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100733STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734xfs_btree_lastrec(
735 xfs_btree_cur_t *cur, /* btree cursor */
736 int level) /* level to change */
737{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100738 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 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 Hellwigf2277f02008-10-30 16:53:47 +1100749 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751 /*
752 * Set the ptr value to numrecs, that's the last record/key.
753 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100754 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 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 */
762void
763xfs_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 Chinner3d3e6f62012-11-12 22:54:08 +1100798int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100800 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 Chinner1813dd62012-11-14 17:54:40 +1100806 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100808 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100810 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 ASSERT(fsbno != NULLFSBLOCK);
813 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100814 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Dave Chinner1813dd62012-11-14 17:54:40 +1100815 mp->m_bsize, lock, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100816 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return error;
Dave Chinner821eb212010-12-02 16:31:13 +1100818 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000819 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *bpp = bp;
821 return 0;
822}
823
824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * Read-ahead the block, don't wait for it, don't return a buffer.
826 * Long-form addressing.
827 */
828/* ARGSUSED */
829void
830xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100831 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 Chinner1813dd62012-11-14 17:54:40 +1100834 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 xfs_daddr_t d;
837
838 ASSERT(fsbno != NULLFSBLOCK);
839 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100840 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/*
844 * Read-ahead the block, don't wait for it, don't return a buffer.
845 * Short-form addressing.
846 */
847/* ARGSUSED */
848void
849xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100850 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 Chinner1813dd62012-11-14 17:54:40 +1100854 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 xfs_daddr_t d;
857
858 ASSERT(agno != NULLAGNUMBER);
859 ASSERT(agbno != NULLAGBLOCK);
860 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100861 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100864STATIC int
865xfs_btree_readahead_lblock(
866 struct xfs_btree_cur *cur,
867 int lr,
868 struct xfs_btree_block *block)
869{
870 int rval = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000871 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 Hellwigb524bfe2008-10-30 16:54:43 +1100873
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000874 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100875 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100876 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100877 rval++;
878 }
879
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000880 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100881 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100882 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100883 rval++;
884 }
885
886 return rval;
887}
888
889STATIC int
890xfs_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 Chinner1813dd62012-11-14 17:54:40 +1100902 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100903 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 Chinner1813dd62012-11-14 17:54:40 +1100908 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100909 rval++;
910 }
911
912 return rval;
913}
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915/*
916 * Read-ahead btree blocks, at the given level.
917 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
918 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100919STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100920xfs_btree_readahead(
921 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int lev, /* level in btree */
923 int lr) /* left/right bits */
924{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100925 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100927 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700938 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100939 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 Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Dave Chinner21b5c972013-08-30 10:23:44 +1000946STATIC xfs_daddr_t
947xfs_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 Hellwigd5cf09b2014-07-30 09:12:05 +1000952 ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK));
Dave Chinner21b5c972013-08-30 10:23:44 +1000953
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 */
970STATIC void
971xfs_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 Torvalds1da177e2005-04-16 15:20:36 -0700981/*
982 * Set the buffer for level "lev" in the cursor to bp, releasing
983 * any previous buffer.
984 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000985STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986xfs_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 Hellwig7cc95a82008-10-30 17:14:34 +1100991 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000993 if (cur->bc_bufs[lev])
994 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 cur->bc_bufs[lev] = bp;
996 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100999 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001000 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001002 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1004 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001005 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001007 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1009 }
1010}
Christoph Hellwig637aa502008-10-30 16:55:45 +11001011
1012STATIC int
1013xfs_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 Hellwigd5cf09b2014-07-30 09:12:05 +10001018 return ptr->l == cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001019 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001020 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001021}
1022
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001023STATIC void
1024xfs_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 Hellwigd5cf09b2014-07-30 09:12:05 +10001029 ptr->l = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001030 else
1031 ptr->s = cpu_to_be32(NULLAGBLOCK);
1032}
1033
Christoph Hellwig637aa502008-10-30 16:55:45 +11001034/*
1035 * Get/set/init sibling pointers
1036 */
1037STATIC void
1038xfs_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 Hellwigf5eb8e72008-10-30 16:57:03 +11001059STATIC void
1060xfs_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 Chinnerb64f3a32012-11-13 16:40:27 -06001081void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001082xfs_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 Hellwigd5cf09b2014-07-30 09:12:05 +10001097 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1098 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001099 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 Sandeence748ea2015-07-29 11:53:31 +10001102 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001103 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001104 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001105 }
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 Sandeence748ea2015-07-29 11:53:31 +10001115 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001116 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001117 }
1118 }
1119}
1120
1121void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001122xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001123 struct xfs_mount *mp,
1124 struct xfs_buf *bp,
1125 __u32 magic,
1126 __u16 level,
1127 __u16 numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001128 __u64 owner,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001129 unsigned int flags)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001130{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001131 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1132 magic, level, numrecs, owner, flags);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001133}
1134
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001135STATIC void
1136xfs_btree_init_block_cur(
1137 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001138 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001139 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001140 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001141{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001142 __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 Chinnerb64f3a32012-11-13 16:40:27 -06001158}
1159
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001160/*
1161 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001162 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001163 * will be further refined in the update_lastrec method.
1164 */
1165STATIC int
1166xfs_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 Hellwigf5eb8e72008-10-30 16:57:03 +11001184STATIC void
1185xfs_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 Sandeen9d87c312009-01-14 23:22:07 -06001194 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001195 XFS_BUF_ADDR(bp)));
1196 }
1197}
1198
Christoph Hellwig637aa502008-10-30 16:55:45 +11001199STATIC void
1200xfs_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 Hellwig38f23232011-10-10 16:52:45 +00001207 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001208 break;
1209 case XFS_BTNUM_INO:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001210 case XFS_BTNUM_FINO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001211 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001212 break;
1213 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001214 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001215 break;
Darrick J. Wong035e00a2016-08-03 11:36:07 +10001216 case XFS_BTNUM_RMAP:
1217 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1218 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001219 default:
1220 ASSERT(0);
1221 }
1222}
1223
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001224STATIC int
1225xfs_btree_get_buf_block(
1226 struct xfs_btree_cur *cur,
1227 union xfs_btree_ptr *ptr,
1228 int flags,
1229 struct xfs_btree_block **block,
1230 struct xfs_buf **bpp)
1231{
1232 struct xfs_mount *mp = cur->bc_mp;
1233 xfs_daddr_t d;
1234
1235 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001236 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001237
1238 d = xfs_btree_ptr_to_daddr(cur, ptr);
1239 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1240 mp->m_bsize, flags);
1241
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001242 if (!*bpp)
Dave Chinner24513372014-06-25 14:58:08 +10001243 return -ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001244
Dave Chinner1813dd62012-11-14 17:54:40 +11001245 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001246 *block = XFS_BUF_TO_BLOCK(*bpp);
1247 return 0;
1248}
1249
Christoph Hellwig637aa502008-10-30 16:55:45 +11001250/*
1251 * Read in the buffer at the given ptr and return the buffer and
1252 * the block pointer within the buffer.
1253 */
1254STATIC int
1255xfs_btree_read_buf_block(
1256 struct xfs_btree_cur *cur,
1257 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001258 int flags,
1259 struct xfs_btree_block **block,
1260 struct xfs_buf **bpp)
1261{
1262 struct xfs_mount *mp = cur->bc_mp;
1263 xfs_daddr_t d;
1264 int error;
1265
1266 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001267 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001268
1269 d = xfs_btree_ptr_to_daddr(cur, ptr);
1270 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001271 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001272 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001273 if (error)
1274 return error;
1275
Christoph Hellwig637aa502008-10-30 16:55:45 +11001276 xfs_btree_set_refs(cur, *bpp);
1277 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001278 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001279}
1280
1281/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001282 * Copy keys from one btree block to another.
1283 */
1284STATIC void
1285xfs_btree_copy_keys(
1286 struct xfs_btree_cur *cur,
1287 union xfs_btree_key *dst_key,
1288 union xfs_btree_key *src_key,
1289 int numkeys)
1290{
1291 ASSERT(numkeys >= 0);
1292 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1293}
1294
1295/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001296 * Copy records from one btree block to another.
1297 */
1298STATIC void
1299xfs_btree_copy_recs(
1300 struct xfs_btree_cur *cur,
1301 union xfs_btree_rec *dst_rec,
1302 union xfs_btree_rec *src_rec,
1303 int numrecs)
1304{
1305 ASSERT(numrecs >= 0);
1306 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1307}
1308
1309/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001310 * Copy block pointers from one btree block to another.
1311 */
1312STATIC void
1313xfs_btree_copy_ptrs(
1314 struct xfs_btree_cur *cur,
1315 union xfs_btree_ptr *dst_ptr,
1316 union xfs_btree_ptr *src_ptr,
1317 int numptrs)
1318{
1319 ASSERT(numptrs >= 0);
1320 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1321}
1322
1323/*
1324 * Shift keys one index left/right inside a single btree block.
1325 */
1326STATIC void
1327xfs_btree_shift_keys(
1328 struct xfs_btree_cur *cur,
1329 union xfs_btree_key *key,
1330 int dir,
1331 int numkeys)
1332{
1333 char *dst_key;
1334
1335 ASSERT(numkeys >= 0);
1336 ASSERT(dir == 1 || dir == -1);
1337
1338 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1339 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1340}
1341
1342/*
1343 * Shift records one index left/right inside a single btree block.
1344 */
1345STATIC void
1346xfs_btree_shift_recs(
1347 struct xfs_btree_cur *cur,
1348 union xfs_btree_rec *rec,
1349 int dir,
1350 int numrecs)
1351{
1352 char *dst_rec;
1353
1354 ASSERT(numrecs >= 0);
1355 ASSERT(dir == 1 || dir == -1);
1356
1357 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1358 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1359}
1360
1361/*
1362 * Shift block pointers one index left/right inside a single btree block.
1363 */
1364STATIC void
1365xfs_btree_shift_ptrs(
1366 struct xfs_btree_cur *cur,
1367 union xfs_btree_ptr *ptr,
1368 int dir,
1369 int numptrs)
1370{
1371 char *dst_ptr;
1372
1373 ASSERT(numptrs >= 0);
1374 ASSERT(dir == 1 || dir == -1);
1375
1376 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1377 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1378}
1379
1380/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001381 * Log key values from the btree block.
1382 */
1383STATIC void
1384xfs_btree_log_keys(
1385 struct xfs_btree_cur *cur,
1386 struct xfs_buf *bp,
1387 int first,
1388 int last)
1389{
1390 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1391 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1392
1393 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001394 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001395 xfs_trans_log_buf(cur->bc_tp, bp,
1396 xfs_btree_key_offset(cur, first),
1397 xfs_btree_key_offset(cur, last + 1) - 1);
1398 } else {
1399 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1400 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1401 }
1402
1403 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1404}
1405
1406/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001407 * Log record values from the btree block.
1408 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001409void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001410xfs_btree_log_recs(
1411 struct xfs_btree_cur *cur,
1412 struct xfs_buf *bp,
1413 int first,
1414 int last)
1415{
1416 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1417 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1418
Dave Chinner61fe1352013-04-03 16:11:30 +11001419 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001420 xfs_trans_log_buf(cur->bc_tp, bp,
1421 xfs_btree_rec_offset(cur, first),
1422 xfs_btree_rec_offset(cur, last + 1) - 1);
1423
1424 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1425}
1426
1427/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001428 * Log block pointer fields from a btree block (nonleaf).
1429 */
1430STATIC void
1431xfs_btree_log_ptrs(
1432 struct xfs_btree_cur *cur, /* btree cursor */
1433 struct xfs_buf *bp, /* buffer containing btree block */
1434 int first, /* index of first pointer to log */
1435 int last) /* index of last pointer to log */
1436{
1437 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1438 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1439
1440 if (bp) {
1441 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1442 int level = xfs_btree_get_level(block);
1443
Dave Chinner61fe1352013-04-03 16:11:30 +11001444 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001445 xfs_trans_log_buf(cur->bc_tp, bp,
1446 xfs_btree_ptr_offset(cur, first, level),
1447 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1448 } else {
1449 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1450 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1451 }
1452
1453 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1454}
1455
1456/*
1457 * Log fields from a btree block header.
1458 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001459void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001460xfs_btree_log_block(
1461 struct xfs_btree_cur *cur, /* btree cursor */
1462 struct xfs_buf *bp, /* buffer containing btree block */
1463 int fields) /* mask of fields: XFS_BB_... */
1464{
1465 int first; /* first byte offset logged */
1466 int last; /* last byte offset logged */
1467 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001468 offsetof(struct xfs_btree_block, bb_magic),
1469 offsetof(struct xfs_btree_block, bb_level),
1470 offsetof(struct xfs_btree_block, bb_numrecs),
1471 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1472 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001473 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1474 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1475 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1476 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1477 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1478 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001479 };
1480 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001481 offsetof(struct xfs_btree_block, bb_magic),
1482 offsetof(struct xfs_btree_block, bb_level),
1483 offsetof(struct xfs_btree_block, bb_numrecs),
1484 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1485 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001486 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1487 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1488 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1489 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1490 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1491 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1492 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001493 };
1494
1495 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1496 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1497
1498 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001499 int nbits;
1500
1501 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1502 /*
1503 * We don't log the CRC when updating a btree
1504 * block but instead recreate it during log
1505 * recovery. As the log buffers have checksums
1506 * of their own this is safe and avoids logging a crc
1507 * update in a lot of places.
1508 */
1509 if (fields == XFS_BB_ALL_BITS)
1510 fields = XFS_BB_ALL_BITS_CRC;
1511 nbits = XFS_BB_NUM_BITS_CRC;
1512 } else {
1513 nbits = XFS_BB_NUM_BITS;
1514 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001515 xfs_btree_offsets(fields,
1516 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1517 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001518 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001519 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001520 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1521 } else {
1522 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1523 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1524 }
1525
1526 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1527}
1528
1529/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001530 * Increment cursor by one record at the level.
1531 * For nonzero levels the leaf-ward information is untouched.
1532 */
1533int /* error */
1534xfs_btree_increment(
1535 struct xfs_btree_cur *cur,
1536 int level,
1537 int *stat) /* success/failure */
1538{
1539 struct xfs_btree_block *block;
1540 union xfs_btree_ptr ptr;
1541 struct xfs_buf *bp;
1542 int error; /* error return value */
1543 int lev;
1544
1545 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1546 XFS_BTREE_TRACE_ARGI(cur, level);
1547
1548 ASSERT(level < cur->bc_nlevels);
1549
1550 /* Read-ahead to the right at this level. */
1551 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1552
1553 /* Get a pointer to the btree block. */
1554 block = xfs_btree_get_block(cur, level, &bp);
1555
1556#ifdef DEBUG
1557 error = xfs_btree_check_block(cur, block, level, bp);
1558 if (error)
1559 goto error0;
1560#endif
1561
1562 /* We're done if we remain in the block after the increment. */
1563 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1564 goto out1;
1565
1566 /* Fail if we just went off the right edge of the tree. */
1567 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1568 if (xfs_btree_ptr_is_null(cur, &ptr))
1569 goto out0;
1570
1571 XFS_BTREE_STATS_INC(cur, increment);
1572
1573 /*
1574 * March up the tree incrementing pointers.
1575 * Stop when we don't go off the right edge of a block.
1576 */
1577 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1578 block = xfs_btree_get_block(cur, lev, &bp);
1579
1580#ifdef DEBUG
1581 error = xfs_btree_check_block(cur, block, lev, bp);
1582 if (error)
1583 goto error0;
1584#endif
1585
1586 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1587 break;
1588
1589 /* Read-ahead the right block for the next loop. */
1590 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1591 }
1592
1593 /*
1594 * If we went off the root then we are either seriously
1595 * confused or have the tree root in an inode.
1596 */
1597 if (lev == cur->bc_nlevels) {
1598 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1599 goto out0;
1600 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001601 error = -EFSCORRUPTED;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001602 goto error0;
1603 }
1604 ASSERT(lev < cur->bc_nlevels);
1605
1606 /*
1607 * Now walk back down the tree, fixing up the cursor's buffer
1608 * pointers and key numbers.
1609 */
1610 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1611 union xfs_btree_ptr *ptrp;
1612
1613 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001614 --lev;
1615 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001616 if (error)
1617 goto error0;
1618
1619 xfs_btree_setbuf(cur, lev, bp);
1620 cur->bc_ptrs[lev] = 1;
1621 }
1622out1:
1623 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1624 *stat = 1;
1625 return 0;
1626
1627out0:
1628 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1629 *stat = 0;
1630 return 0;
1631
1632error0:
1633 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1634 return error;
1635}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001636
1637/*
1638 * Decrement cursor by one record at the level.
1639 * For nonzero levels the leaf-ward information is untouched.
1640 */
1641int /* error */
1642xfs_btree_decrement(
1643 struct xfs_btree_cur *cur,
1644 int level,
1645 int *stat) /* success/failure */
1646{
1647 struct xfs_btree_block *block;
1648 xfs_buf_t *bp;
1649 int error; /* error return value */
1650 int lev;
1651 union xfs_btree_ptr ptr;
1652
1653 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1654 XFS_BTREE_TRACE_ARGI(cur, level);
1655
1656 ASSERT(level < cur->bc_nlevels);
1657
1658 /* Read-ahead to the left at this level. */
1659 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1660
1661 /* We're done if we remain in the block after the decrement. */
1662 if (--cur->bc_ptrs[level] > 0)
1663 goto out1;
1664
1665 /* Get a pointer to the btree block. */
1666 block = xfs_btree_get_block(cur, level, &bp);
1667
1668#ifdef DEBUG
1669 error = xfs_btree_check_block(cur, block, level, bp);
1670 if (error)
1671 goto error0;
1672#endif
1673
1674 /* Fail if we just went off the left edge of the tree. */
1675 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1676 if (xfs_btree_ptr_is_null(cur, &ptr))
1677 goto out0;
1678
1679 XFS_BTREE_STATS_INC(cur, decrement);
1680
1681 /*
1682 * March up the tree decrementing pointers.
1683 * Stop when we don't go off the left edge of a block.
1684 */
1685 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1686 if (--cur->bc_ptrs[lev] > 0)
1687 break;
1688 /* Read-ahead the left block for the next loop. */
1689 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1690 }
1691
1692 /*
1693 * If we went off the root then we are seriously confused.
1694 * or the root of the tree is in an inode.
1695 */
1696 if (lev == cur->bc_nlevels) {
1697 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1698 goto out0;
1699 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001700 error = -EFSCORRUPTED;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001701 goto error0;
1702 }
1703 ASSERT(lev < cur->bc_nlevels);
1704
1705 /*
1706 * Now walk back down the tree, fixing up the cursor's buffer
1707 * pointers and key numbers.
1708 */
1709 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1710 union xfs_btree_ptr *ptrp;
1711
1712 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001713 --lev;
1714 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001715 if (error)
1716 goto error0;
1717 xfs_btree_setbuf(cur, lev, bp);
1718 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1719 }
1720out1:
1721 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1722 *stat = 1;
1723 return 0;
1724
1725out0:
1726 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1727 *stat = 0;
1728 return 0;
1729
1730error0:
1731 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1732 return error;
1733}
1734
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001735STATIC int
1736xfs_btree_lookup_get_block(
1737 struct xfs_btree_cur *cur, /* btree cursor */
1738 int level, /* level in the btree */
1739 union xfs_btree_ptr *pp, /* ptr to btree block */
1740 struct xfs_btree_block **blkp) /* return btree block */
1741{
1742 struct xfs_buf *bp; /* buffer pointer for btree block */
1743 int error = 0;
1744
1745 /* special case the root block if in an inode */
1746 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1747 (level == cur->bc_nlevels - 1)) {
1748 *blkp = xfs_btree_get_iroot(cur);
1749 return 0;
1750 }
1751
1752 /*
1753 * If the old buffer at this level for the disk address we are
1754 * looking for re-use it.
1755 *
1756 * Otherwise throw it away and get a new one.
1757 */
1758 bp = cur->bc_bufs[level];
1759 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1760 *blkp = XFS_BUF_TO_BLOCK(bp);
1761 return 0;
1762 }
1763
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001764 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001765 if (error)
1766 return error;
1767
1768 xfs_btree_setbuf(cur, level, bp);
1769 return 0;
1770}
1771
1772/*
1773 * Get current search key. For level 0 we don't actually have a key
1774 * structure so we make one up from the record. For all other levels
1775 * we just return the right key.
1776 */
1777STATIC union xfs_btree_key *
1778xfs_lookup_get_search_key(
1779 struct xfs_btree_cur *cur,
1780 int level,
1781 int keyno,
1782 struct xfs_btree_block *block,
1783 union xfs_btree_key *kp)
1784{
1785 if (level == 0) {
1786 cur->bc_ops->init_key_from_rec(kp,
1787 xfs_btree_rec_addr(cur, keyno, block));
1788 return kp;
1789 }
1790
1791 return xfs_btree_key_addr(cur, keyno, block);
1792}
1793
1794/*
1795 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001796 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001797 */
1798int /* error */
1799xfs_btree_lookup(
1800 struct xfs_btree_cur *cur, /* btree cursor */
1801 xfs_lookup_t dir, /* <=, ==, or >= */
1802 int *stat) /* success/failure */
1803{
1804 struct xfs_btree_block *block; /* current btree block */
1805 __int64_t diff; /* difference for the current key */
1806 int error; /* error return value */
1807 int keyno; /* current key number */
1808 int level; /* level in the btree */
1809 union xfs_btree_ptr *pp; /* ptr to btree block */
1810 union xfs_btree_ptr ptr; /* ptr to btree block */
1811
1812 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1813 XFS_BTREE_TRACE_ARGI(cur, dir);
1814
1815 XFS_BTREE_STATS_INC(cur, lookup);
1816
1817 block = NULL;
1818 keyno = 0;
1819
1820 /* initialise start pointer from cursor */
1821 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1822 pp = &ptr;
1823
1824 /*
1825 * Iterate over each level in the btree, starting at the root.
1826 * For each level above the leaves, find the key we need, based
1827 * on the lookup record, then follow the corresponding block
1828 * pointer down to the next level.
1829 */
1830 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1831 /* Get the block we need to do the lookup on. */
1832 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1833 if (error)
1834 goto error0;
1835
1836 if (diff == 0) {
1837 /*
1838 * If we already had a key match at a higher level, we
1839 * know we need to use the first entry in this block.
1840 */
1841 keyno = 1;
1842 } else {
1843 /* Otherwise search this block. Do a binary search. */
1844
1845 int high; /* high entry number */
1846 int low; /* low entry number */
1847
1848 /* Set low and high entry numbers, 1-based. */
1849 low = 1;
1850 high = xfs_btree_get_numrecs(block);
1851 if (!high) {
1852 /* Block is empty, must be an empty leaf. */
1853 ASSERT(level == 0 && cur->bc_nlevels == 1);
1854
1855 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1856 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1857 *stat = 0;
1858 return 0;
1859 }
1860
1861 /* Binary search the block. */
1862 while (low <= high) {
1863 union xfs_btree_key key;
1864 union xfs_btree_key *kp;
1865
1866 XFS_BTREE_STATS_INC(cur, compare);
1867
1868 /* keyno is average of low and high. */
1869 keyno = (low + high) >> 1;
1870
1871 /* Get current search key */
1872 kp = xfs_lookup_get_search_key(cur, level,
1873 keyno, block, &key);
1874
1875 /*
1876 * Compute difference to get next direction:
1877 * - less than, move right
1878 * - greater than, move left
1879 * - equal, we're done
1880 */
1881 diff = cur->bc_ops->key_diff(cur, kp);
1882 if (diff < 0)
1883 low = keyno + 1;
1884 else if (diff > 0)
1885 high = keyno - 1;
1886 else
1887 break;
1888 }
1889 }
1890
1891 /*
1892 * If there are more levels, set up for the next level
1893 * by getting the block number and filling in the cursor.
1894 */
1895 if (level > 0) {
1896 /*
1897 * If we moved left, need the previous key number,
1898 * unless there isn't one.
1899 */
1900 if (diff > 0 && --keyno < 1)
1901 keyno = 1;
1902 pp = xfs_btree_ptr_addr(cur, keyno, block);
1903
1904#ifdef DEBUG
1905 error = xfs_btree_check_ptr(cur, pp, 0, level);
1906 if (error)
1907 goto error0;
1908#endif
1909 cur->bc_ptrs[level] = keyno;
1910 }
1911 }
1912
1913 /* Done with the search. See if we need to adjust the results. */
1914 if (dir != XFS_LOOKUP_LE && diff < 0) {
1915 keyno++;
1916 /*
1917 * If ge search and we went off the end of the block, but it's
1918 * not the last block, we're in the wrong block.
1919 */
1920 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1921 if (dir == XFS_LOOKUP_GE &&
1922 keyno > xfs_btree_get_numrecs(block) &&
1923 !xfs_btree_ptr_is_null(cur, &ptr)) {
1924 int i;
1925
1926 cur->bc_ptrs[0] = keyno;
1927 error = xfs_btree_increment(cur, 0, &i);
1928 if (error)
1929 goto error0;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001930 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001931 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1932 *stat = 1;
1933 return 0;
1934 }
1935 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1936 keyno--;
1937 cur->bc_ptrs[0] = keyno;
1938
1939 /* Return if we succeeded or not. */
1940 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1941 *stat = 0;
1942 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1943 *stat = 1;
1944 else
1945 *stat = 0;
1946 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1947 return 0;
1948
1949error0:
1950 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1951 return error;
1952}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001953
Darrick J. Wong70b22652016-08-03 11:03:38 +10001954/* Determine the low key of a leaf block (simple) */
1955void
1956xfs_btree_get_leaf_keys(
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001957 struct xfs_btree_cur *cur,
Darrick J. Wong70b22652016-08-03 11:03:38 +10001958 struct xfs_btree_block *block,
1959 union xfs_btree_key *key)
1960{
1961 union xfs_btree_rec *rec;
1962
1963 rec = xfs_btree_rec_addr(cur, 1, block);
1964 cur->bc_ops->init_key_from_rec(key, rec);
1965}
1966
1967/* Determine the low key of a node block (simple) */
1968void
1969xfs_btree_get_node_keys(
1970 struct xfs_btree_cur *cur,
1971 struct xfs_btree_block *block,
1972 union xfs_btree_key *key)
1973{
1974 memcpy(key, xfs_btree_key_addr(cur, 1, block), cur->bc_ops->key_len);
1975}
1976
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10001977/* Find the high key storage area from a regular key. */
1978STATIC union xfs_btree_key *
1979xfs_btree_high_key_from_key(
1980 struct xfs_btree_cur *cur,
1981 union xfs_btree_key *key)
1982{
1983 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
1984 return (union xfs_btree_key *)((char *)key +
1985 (cur->bc_ops->key_len / 2));
1986}
1987
1988/* Determine the low and high keys of a leaf block (overlapped) */
1989void
1990xfs_btree_get_leaf_keys_overlapped(
1991 struct xfs_btree_cur *cur,
1992 struct xfs_btree_block *block,
1993 union xfs_btree_key *key)
1994{
1995 int n;
1996 union xfs_btree_rec *rec;
1997 union xfs_btree_key max_hkey;
1998 union xfs_btree_key hkey;
1999 union xfs_btree_key *high;
2000
2001 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2002 rec = xfs_btree_rec_addr(cur, 1, block);
2003 cur->bc_ops->init_key_from_rec(key, rec);
2004
2005 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2006 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2007 rec = xfs_btree_rec_addr(cur, n, block);
2008 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2009 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey) > 0)
2010 max_hkey = hkey;
2011 }
2012
2013 high = xfs_btree_high_key_from_key(cur, key);
2014 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2015}
2016
2017/* Determine the low and high keys of a node block (overlapped) */
2018void
2019xfs_btree_get_node_keys_overlapped(
2020 struct xfs_btree_cur *cur,
2021 struct xfs_btree_block *block,
2022 union xfs_btree_key *key)
2023{
2024 int n;
2025 union xfs_btree_key *hkey;
2026 union xfs_btree_key *max_hkey;
2027 union xfs_btree_key *high;
2028
2029 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2030 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2031 cur->bc_ops->key_len / 2);
2032
2033 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2034 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2035 hkey = xfs_btree_high_key_addr(cur, n, block);
2036 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2037 max_hkey = hkey;
2038 }
2039
2040 high = xfs_btree_high_key_from_key(cur, key);
2041 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2042}
2043
Darrick J. Wong70b22652016-08-03 11:03:38 +10002044/* Derive the keys for any btree block. */
2045STATIC void
2046xfs_btree_get_keys(
2047 struct xfs_btree_cur *cur,
2048 struct xfs_btree_block *block,
2049 union xfs_btree_key *key)
2050{
2051 if (be16_to_cpu(block->bb_level) == 0)
2052 cur->bc_ops->get_leaf_keys(cur, block, key);
2053 else
2054 cur->bc_ops->get_node_keys(cur, block, key);
2055}
2056
2057/*
2058 * Decide if we need to update the parent keys of a btree block. For
2059 * a standard btree this is only necessary if we're updating the first
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002060 * record/key. For an overlapping btree, we must always update the
2061 * keys because the highest key can be in any of the records or keys
2062 * in the block.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002063 */
2064static inline bool
2065xfs_btree_needs_key_update(
2066 struct xfs_btree_cur *cur,
2067 int ptr)
2068{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002069 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2070}
2071
2072/*
2073 * Update the low and high parent keys of the given level, progressing
2074 * towards the root. If force_all is false, stop if the keys for a given
2075 * level do not need updating.
2076 */
2077STATIC int
2078__xfs_btree_updkeys(
2079 struct xfs_btree_cur *cur,
2080 int level,
2081 struct xfs_btree_block *block,
2082 struct xfs_buf *bp0,
2083 bool force_all)
2084{
2085 union xfs_btree_bigkey key; /* keys from current level */
2086 union xfs_btree_key *lkey; /* keys from the next level up */
2087 union xfs_btree_key *hkey;
2088 union xfs_btree_key *nlkey; /* keys from the next level up */
2089 union xfs_btree_key *nhkey;
2090 struct xfs_buf *bp;
2091 int ptr;
2092
2093 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2094
2095 /* Exit if there aren't any parent levels to update. */
2096 if (level + 1 >= cur->bc_nlevels)
2097 return 0;
2098
2099 trace_xfs_btree_updkeys(cur, level, bp0);
2100
2101 lkey = (union xfs_btree_key *)&key;
2102 hkey = xfs_btree_high_key_from_key(cur, lkey);
2103 xfs_btree_get_keys(cur, block, lkey);
2104 for (level++; level < cur->bc_nlevels; level++) {
2105#ifdef DEBUG
2106 int error;
2107#endif
2108 block = xfs_btree_get_block(cur, level, &bp);
2109 trace_xfs_btree_updkeys(cur, level, bp);
2110#ifdef DEBUG
2111 error = xfs_btree_check_block(cur, block, level, bp);
2112 if (error) {
2113 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2114 return error;
2115 }
2116#endif
2117 ptr = cur->bc_ptrs[level];
2118 nlkey = xfs_btree_key_addr(cur, ptr, block);
2119 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2120 if (!force_all &&
2121 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2122 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2123 break;
2124 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2125 xfs_btree_log_keys(cur, bp, ptr, ptr);
2126 if (level + 1 >= cur->bc_nlevels)
2127 break;
2128 cur->bc_ops->get_node_keys(cur, block, lkey);
2129 }
2130
2131 return 0;
2132}
2133
2134/*
2135 * Update all the keys from some level in cursor back to the root, stopping
2136 * when we find a key pair that don't need updating.
2137 */
2138int
2139xfs_btree_update_keys_overlapped(
2140 struct xfs_btree_cur *cur,
2141 int level)
2142{
2143 struct xfs_buf *bp;
2144 struct xfs_btree_block *block;
2145
2146 block = xfs_btree_get_block(cur, level, &bp);
2147 return __xfs_btree_updkeys(cur, level, block, bp, false);
2148}
2149
2150/* Update all the keys from some level in cursor back to the root. */
2151STATIC int
2152xfs_btree_updkeys_force(
2153 struct xfs_btree_cur *cur,
2154 int level)
2155{
2156 struct xfs_buf *bp;
2157 struct xfs_btree_block *block;
2158
2159 block = xfs_btree_get_block(cur, level, &bp);
2160 return __xfs_btree_updkeys(cur, level, block, bp, true);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002161}
2162
2163/*
2164 * Update the parent keys of the given level, progressing towards the root.
2165 */
2166int
2167xfs_btree_update_keys(
2168 struct xfs_btree_cur *cur,
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002169 int level)
2170{
2171 struct xfs_btree_block *block;
2172 struct xfs_buf *bp;
2173 union xfs_btree_key *kp;
Darrick J. Wong70b22652016-08-03 11:03:38 +10002174 union xfs_btree_key key;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002175 int ptr;
2176
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002177 ASSERT(!(cur->bc_flags & XFS_BTREE_OVERLAPPING));
2178
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002179 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2180 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
2181
Darrick J. Wong70b22652016-08-03 11:03:38 +10002182 ASSERT(level >= 0);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002183
2184 /*
2185 * Go up the tree from this level toward the root.
2186 * At each level, update the key value to the value input.
2187 * Stop when we reach a level where the cursor isn't pointing
2188 * at the first entry in the block.
2189 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002190 block = xfs_btree_get_block(cur, level, &bp);
2191 xfs_btree_get_keys(cur, block, &key);
2192 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002193#ifdef DEBUG
2194 int error;
2195#endif
2196 block = xfs_btree_get_block(cur, level, &bp);
2197#ifdef DEBUG
2198 error = xfs_btree_check_block(cur, block, level, bp);
2199 if (error) {
2200 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2201 return error;
2202 }
2203#endif
2204 ptr = cur->bc_ptrs[level];
2205 kp = xfs_btree_key_addr(cur, ptr, block);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002206 xfs_btree_copy_keys(cur, kp, &key, 1);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002207 xfs_btree_log_keys(cur, bp, ptr, ptr);
2208 }
2209
2210 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2211 return 0;
2212}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002213
2214/*
2215 * Update the record referred to by cur to the value in the
2216 * given record. This either works (return 0) or gets an
2217 * EFSCORRUPTED error.
2218 */
2219int
2220xfs_btree_update(
2221 struct xfs_btree_cur *cur,
2222 union xfs_btree_rec *rec)
2223{
2224 struct xfs_btree_block *block;
2225 struct xfs_buf *bp;
2226 int error;
2227 int ptr;
2228 union xfs_btree_rec *rp;
2229
2230 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2231 XFS_BTREE_TRACE_ARGR(cur, rec);
2232
2233 /* Pick up the current block. */
2234 block = xfs_btree_get_block(cur, 0, &bp);
2235
2236#ifdef DEBUG
2237 error = xfs_btree_check_block(cur, block, 0, bp);
2238 if (error)
2239 goto error0;
2240#endif
2241 /* Get the address of the rec to be updated. */
2242 ptr = cur->bc_ptrs[0];
2243 rp = xfs_btree_rec_addr(cur, ptr, block);
2244
2245 /* Fill in the new contents and log them. */
2246 xfs_btree_copy_recs(cur, rp, rec, 1);
2247 xfs_btree_log_recs(cur, bp, ptr, ptr);
2248
2249 /*
2250 * If we are tracking the last record in the tree and
2251 * we are at the far right edge of the tree, update it.
2252 */
2253 if (xfs_btree_is_lastrec(cur, block, 0)) {
2254 cur->bc_ops->update_lastrec(cur, block, rec,
2255 ptr, LASTREC_UPDATE);
2256 }
2257
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002258 /* Pass new key value up to our parent. */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002259 if (xfs_btree_needs_key_update(cur, ptr)) {
2260 error = cur->bc_ops->update_keys(cur, 0);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002261 if (error)
2262 goto error0;
2263 }
2264
2265 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2266 return 0;
2267
2268error0:
2269 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2270 return error;
2271}
2272
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002273/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11002274 * Move 1 record left from cur/level if possible.
2275 * Update cur to reflect the new path.
2276 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002277STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002278xfs_btree_lshift(
2279 struct xfs_btree_cur *cur,
2280 int level,
2281 int *stat) /* success/failure */
2282{
2283 union xfs_btree_key key; /* btree key */
2284 struct xfs_buf *lbp; /* left buffer pointer */
2285 struct xfs_btree_block *left; /* left btree block */
2286 int lrecs; /* left record count */
2287 struct xfs_buf *rbp; /* right buffer pointer */
2288 struct xfs_btree_block *right; /* right btree block */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002289 struct xfs_btree_cur *tcur; /* temporary btree cursor */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002290 int rrecs; /* right record count */
2291 union xfs_btree_ptr lptr; /* left btree pointer */
2292 union xfs_btree_key *rkp = NULL; /* right btree key */
2293 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2294 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2295 int error; /* error return value */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002296 int i;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002297
2298 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2299 XFS_BTREE_TRACE_ARGI(cur, level);
2300
2301 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2302 level == cur->bc_nlevels - 1)
2303 goto out0;
2304
2305 /* Set up variables for this block as "right". */
2306 right = xfs_btree_get_block(cur, level, &rbp);
2307
2308#ifdef DEBUG
2309 error = xfs_btree_check_block(cur, right, level, rbp);
2310 if (error)
2311 goto error0;
2312#endif
2313
2314 /* If we've got no left sibling then we can't shift an entry left. */
2315 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2316 if (xfs_btree_ptr_is_null(cur, &lptr))
2317 goto out0;
2318
2319 /*
2320 * If the cursor entry is the one that would be moved, don't
2321 * do it... it's too complicated.
2322 */
2323 if (cur->bc_ptrs[level] <= 1)
2324 goto out0;
2325
2326 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002327 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002328 if (error)
2329 goto error0;
2330
2331 /* If it's full, it can't take another entry. */
2332 lrecs = xfs_btree_get_numrecs(left);
2333 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2334 goto out0;
2335
2336 rrecs = xfs_btree_get_numrecs(right);
2337
2338 /*
2339 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002340 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002341 * later.
2342 */
2343 lrecs++;
2344 rrecs--;
2345
2346 XFS_BTREE_STATS_INC(cur, lshift);
2347 XFS_BTREE_STATS_ADD(cur, moves, 1);
2348
2349 /*
2350 * If non-leaf, copy a key and a ptr to the left block.
2351 * Log the changes to the left block.
2352 */
2353 if (level > 0) {
2354 /* It's a non-leaf. Move keys and pointers. */
2355 union xfs_btree_key *lkp; /* left btree key */
2356 union xfs_btree_ptr *lpp; /* left address pointer */
2357
2358 lkp = xfs_btree_key_addr(cur, lrecs, left);
2359 rkp = xfs_btree_key_addr(cur, 1, right);
2360
2361 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2362 rpp = xfs_btree_ptr_addr(cur, 1, right);
2363#ifdef DEBUG
2364 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2365 if (error)
2366 goto error0;
2367#endif
2368 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2369 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2370
2371 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2372 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2373
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002374 ASSERT(cur->bc_ops->keys_inorder(cur,
2375 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002376 } else {
2377 /* It's a leaf. Move records. */
2378 union xfs_btree_rec *lrp; /* left record pointer */
2379
2380 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2381 rrp = xfs_btree_rec_addr(cur, 1, right);
2382
2383 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2384 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2385
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002386 ASSERT(cur->bc_ops->recs_inorder(cur,
2387 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002388 }
2389
2390 xfs_btree_set_numrecs(left, lrecs);
2391 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2392
2393 xfs_btree_set_numrecs(right, rrecs);
2394 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2395
2396 /*
2397 * Slide the contents of right down one entry.
2398 */
2399 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2400 if (level > 0) {
2401 /* It's a nonleaf. operate on keys and ptrs */
2402#ifdef DEBUG
2403 int i; /* loop index */
2404
2405 for (i = 0; i < rrecs; i++) {
2406 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2407 if (error)
2408 goto error0;
2409 }
2410#endif
2411 xfs_btree_shift_keys(cur,
2412 xfs_btree_key_addr(cur, 2, right),
2413 -1, rrecs);
2414 xfs_btree_shift_ptrs(cur,
2415 xfs_btree_ptr_addr(cur, 2, right),
2416 -1, rrecs);
2417
2418 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2419 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2420 } else {
2421 /* It's a leaf. operate on records */
2422 xfs_btree_shift_recs(cur,
2423 xfs_btree_rec_addr(cur, 2, right),
2424 -1, rrecs);
2425 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2426
2427 /*
2428 * If it's the first record in the block, we'll need a key
2429 * structure to pass up to the next level (updkey).
2430 */
2431 cur->bc_ops->init_key_from_rec(&key,
2432 xfs_btree_rec_addr(cur, 1, right));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002433 }
2434
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002435 /*
2436 * Using a temporary cursor, update the parent key values of the
2437 * block on the left.
2438 */
2439 error = xfs_btree_dup_cursor(cur, &tcur);
2440 if (error)
2441 goto error0;
2442 i = xfs_btree_firstrec(tcur, level);
2443 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
2444
2445 error = xfs_btree_decrement(tcur, level, &i);
2446 if (error)
2447 goto error1;
2448
Darrick J. Wong70b22652016-08-03 11:03:38 +10002449 /* Update the parent keys of the right block. */
2450 error = cur->bc_ops->update_keys(cur, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002451 if (error)
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002452 goto error1;
2453
2454 /* Update the parent high keys of the left block, if needed. */
2455 if (tcur->bc_flags & XFS_BTREE_OVERLAPPING) {
2456 error = tcur->bc_ops->update_keys(tcur, level);
2457 if (error)
2458 goto error1;
2459 }
2460
2461 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002462
2463 /* Slide the cursor value left one. */
2464 cur->bc_ptrs[level]--;
2465
2466 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2467 *stat = 1;
2468 return 0;
2469
2470out0:
2471 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2472 *stat = 0;
2473 return 0;
2474
2475error0:
2476 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2477 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002478
2479error1:
2480 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2481 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2482 return error;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002483}
2484
2485/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002486 * Move 1 record right from cur/level if possible.
2487 * Update cur to reflect the new path.
2488 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002489STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002490xfs_btree_rshift(
2491 struct xfs_btree_cur *cur,
2492 int level,
2493 int *stat) /* success/failure */
2494{
2495 union xfs_btree_key key; /* btree key */
2496 struct xfs_buf *lbp; /* left buffer pointer */
2497 struct xfs_btree_block *left; /* left btree block */
2498 struct xfs_buf *rbp; /* right buffer pointer */
2499 struct xfs_btree_block *right; /* right btree block */
2500 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2501 union xfs_btree_ptr rptr; /* right block pointer */
2502 union xfs_btree_key *rkp; /* right btree key */
2503 int rrecs; /* right record count */
2504 int lrecs; /* left record count */
2505 int error; /* error return value */
2506 int i; /* loop counter */
2507
2508 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2509 XFS_BTREE_TRACE_ARGI(cur, level);
2510
2511 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2512 (level == cur->bc_nlevels - 1))
2513 goto out0;
2514
2515 /* Set up variables for this block as "left". */
2516 left = xfs_btree_get_block(cur, level, &lbp);
2517
2518#ifdef DEBUG
2519 error = xfs_btree_check_block(cur, left, level, lbp);
2520 if (error)
2521 goto error0;
2522#endif
2523
2524 /* If we've got no right sibling then we can't shift an entry right. */
2525 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2526 if (xfs_btree_ptr_is_null(cur, &rptr))
2527 goto out0;
2528
2529 /*
2530 * If the cursor entry is the one that would be moved, don't
2531 * do it... it's too complicated.
2532 */
2533 lrecs = xfs_btree_get_numrecs(left);
2534 if (cur->bc_ptrs[level] >= lrecs)
2535 goto out0;
2536
2537 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002538 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002539 if (error)
2540 goto error0;
2541
2542 /* If it's full, it can't take another entry. */
2543 rrecs = xfs_btree_get_numrecs(right);
2544 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2545 goto out0;
2546
2547 XFS_BTREE_STATS_INC(cur, rshift);
2548 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2549
2550 /*
2551 * Make a hole at the start of the right neighbor block, then
2552 * copy the last left block entry to the hole.
2553 */
2554 if (level > 0) {
2555 /* It's a nonleaf. make a hole in the keys and ptrs */
2556 union xfs_btree_key *lkp;
2557 union xfs_btree_ptr *lpp;
2558 union xfs_btree_ptr *rpp;
2559
2560 lkp = xfs_btree_key_addr(cur, lrecs, left);
2561 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2562 rkp = xfs_btree_key_addr(cur, 1, right);
2563 rpp = xfs_btree_ptr_addr(cur, 1, right);
2564
2565#ifdef DEBUG
2566 for (i = rrecs - 1; i >= 0; i--) {
2567 error = xfs_btree_check_ptr(cur, rpp, i, level);
2568 if (error)
2569 goto error0;
2570 }
2571#endif
2572
2573 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2574 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2575
2576#ifdef DEBUG
2577 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2578 if (error)
2579 goto error0;
2580#endif
2581
2582 /* Now put the new data in, and log it. */
2583 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2584 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2585
2586 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2587 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2588
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002589 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2590 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002591 } else {
2592 /* It's a leaf. make a hole in the records */
2593 union xfs_btree_rec *lrp;
2594 union xfs_btree_rec *rrp;
2595
2596 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2597 rrp = xfs_btree_rec_addr(cur, 1, right);
2598
2599 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2600
2601 /* Now put the new data in, and log it. */
2602 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2603 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2604
2605 cur->bc_ops->init_key_from_rec(&key, rrp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002606
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002607 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2608 xfs_btree_rec_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002609 }
2610
2611 /*
2612 * Decrement and log left's numrecs, bump and log right's numrecs.
2613 */
2614 xfs_btree_set_numrecs(left, --lrecs);
2615 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2616
2617 xfs_btree_set_numrecs(right, ++rrecs);
2618 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2619
2620 /*
2621 * Using a temporary cursor, update the parent key values of the
2622 * block on the right.
2623 */
2624 error = xfs_btree_dup_cursor(cur, &tcur);
2625 if (error)
2626 goto error0;
2627 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11002628 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002629
2630 error = xfs_btree_increment(tcur, level, &i);
2631 if (error)
2632 goto error1;
2633
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002634 /* Update the parent high keys of the left block, if needed. */
2635 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2636 error = cur->bc_ops->update_keys(cur, level);
2637 if (error)
2638 goto error1;
2639 }
2640
Darrick J. Wong70b22652016-08-03 11:03:38 +10002641 /* Update the parent keys of the right block. */
2642 error = cur->bc_ops->update_keys(tcur, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002643 if (error)
2644 goto error1;
2645
2646 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2647
2648 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2649 *stat = 1;
2650 return 0;
2651
2652out0:
2653 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2654 *stat = 0;
2655 return 0;
2656
2657error0:
2658 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2659 return error;
2660
2661error1:
2662 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2663 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2664 return error;
2665}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002666
2667/*
2668 * Split cur/level block in half.
2669 * Return new block number and the key to its first
2670 * record (to be inserted into parent).
2671 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002672STATIC int /* error */
Dave Chinnercf11da92014-07-15 07:08:24 +10002673__xfs_btree_split(
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002674 struct xfs_btree_cur *cur,
2675 int level,
2676 union xfs_btree_ptr *ptrp,
2677 union xfs_btree_key *key,
2678 struct xfs_btree_cur **curp,
2679 int *stat) /* success/failure */
2680{
2681 union xfs_btree_ptr lptr; /* left sibling block ptr */
2682 struct xfs_buf *lbp; /* left buffer pointer */
2683 struct xfs_btree_block *left; /* left btree block */
2684 union xfs_btree_ptr rptr; /* right sibling block ptr */
2685 struct xfs_buf *rbp; /* right buffer pointer */
2686 struct xfs_btree_block *right; /* right btree block */
2687 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2688 struct xfs_buf *rrbp; /* right-right buffer pointer */
2689 struct xfs_btree_block *rrblock; /* right-right btree block */
2690 int lrecs;
2691 int rrecs;
2692 int src_index;
2693 int error; /* error return value */
2694#ifdef DEBUG
2695 int i;
2696#endif
2697
2698 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2699 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2700
2701 XFS_BTREE_STATS_INC(cur, split);
2702
2703 /* Set up left block (current one). */
2704 left = xfs_btree_get_block(cur, level, &lbp);
2705
2706#ifdef DEBUG
2707 error = xfs_btree_check_block(cur, left, level, lbp);
2708 if (error)
2709 goto error0;
2710#endif
2711
2712 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2713
2714 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002715 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002716 if (error)
2717 goto error0;
2718 if (*stat == 0)
2719 goto out0;
2720 XFS_BTREE_STATS_INC(cur, alloc);
2721
2722 /* Set up the new block as "right". */
2723 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2724 if (error)
2725 goto error0;
2726
2727 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002728 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002729
2730 /*
2731 * Split the entries between the old and the new block evenly.
2732 * Make sure that if there's an odd number of entries now, that
2733 * each new block will have the same number of entries.
2734 */
2735 lrecs = xfs_btree_get_numrecs(left);
2736 rrecs = lrecs / 2;
2737 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2738 rrecs++;
2739 src_index = (lrecs - rrecs + 1);
2740
2741 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2742
Darrick J. Wong70b22652016-08-03 11:03:38 +10002743 /* Adjust numrecs for the later get_*_keys() calls. */
2744 lrecs -= rrecs;
2745 xfs_btree_set_numrecs(left, lrecs);
2746 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2747
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002748 /*
2749 * Copy btree block entries from the left block over to the
2750 * new block, the right. Update the right block and log the
2751 * changes.
2752 */
2753 if (level > 0) {
2754 /* It's a non-leaf. Move keys and pointers. */
2755 union xfs_btree_key *lkp; /* left btree key */
2756 union xfs_btree_ptr *lpp; /* left address pointer */
2757 union xfs_btree_key *rkp; /* right btree key */
2758 union xfs_btree_ptr *rpp; /* right address pointer */
2759
2760 lkp = xfs_btree_key_addr(cur, src_index, left);
2761 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2762 rkp = xfs_btree_key_addr(cur, 1, right);
2763 rpp = xfs_btree_ptr_addr(cur, 1, right);
2764
2765#ifdef DEBUG
2766 for (i = src_index; i < rrecs; i++) {
2767 error = xfs_btree_check_ptr(cur, lpp, i, level);
2768 if (error)
2769 goto error0;
2770 }
2771#endif
2772
Darrick J. Wong70b22652016-08-03 11:03:38 +10002773 /* Copy the keys & pointers to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002774 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2775 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2776
2777 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2778 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2779
Darrick J. Wong70b22652016-08-03 11:03:38 +10002780 /* Stash the keys of the new block for later insertion. */
2781 cur->bc_ops->get_node_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002782 } else {
2783 /* It's a leaf. Move records. */
2784 union xfs_btree_rec *lrp; /* left record pointer */
2785 union xfs_btree_rec *rrp; /* right record pointer */
2786
2787 lrp = xfs_btree_rec_addr(cur, src_index, left);
2788 rrp = xfs_btree_rec_addr(cur, 1, right);
2789
Darrick J. Wong70b22652016-08-03 11:03:38 +10002790 /* Copy records to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002791 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2792 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2793
Darrick J. Wong70b22652016-08-03 11:03:38 +10002794 /* Stash the keys of the new block for later insertion. */
2795 cur->bc_ops->get_leaf_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002796 }
2797
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002798 /*
2799 * Find the left block number by looking in the buffer.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002800 * Adjust sibling pointers.
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002801 */
2802 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2803 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2804 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2805 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2806
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002807 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2808 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2809
2810 /*
2811 * If there's a block to the new block's right, make that block
2812 * point back to right instead of to left.
2813 */
2814 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002815 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002816 0, &rrblock, &rrbp);
2817 if (error)
2818 goto error0;
2819 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2820 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2821 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002822
2823 /* Update the parent high keys of the left block, if needed. */
2824 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2825 error = cur->bc_ops->update_keys(cur, level);
2826 if (error)
2827 goto error0;
2828 }
2829
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002830 /*
2831 * If the cursor is really in the right block, move it there.
2832 * If it's just pointing past the last entry in left, then we'll
2833 * insert there, so don't change anything in that case.
2834 */
2835 if (cur->bc_ptrs[level] > lrecs + 1) {
2836 xfs_btree_setbuf(cur, level, rbp);
2837 cur->bc_ptrs[level] -= lrecs;
2838 }
2839 /*
2840 * If there are more levels, we'll need another cursor which refers
2841 * the right block, no matter where this cursor was.
2842 */
2843 if (level + 1 < cur->bc_nlevels) {
2844 error = xfs_btree_dup_cursor(cur, curp);
2845 if (error)
2846 goto error0;
2847 (*curp)->bc_ptrs[level + 1]++;
2848 }
2849 *ptrp = rptr;
2850 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2851 *stat = 1;
2852 return 0;
2853out0:
2854 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2855 *stat = 0;
2856 return 0;
2857
2858error0:
2859 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2860 return error;
2861}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002862
Dave Chinnercf11da92014-07-15 07:08:24 +10002863struct xfs_btree_split_args {
2864 struct xfs_btree_cur *cur;
2865 int level;
2866 union xfs_btree_ptr *ptrp;
2867 union xfs_btree_key *key;
2868 struct xfs_btree_cur **curp;
2869 int *stat; /* success/failure */
2870 int result;
2871 bool kswapd; /* allocation in kswapd context */
2872 struct completion *done;
2873 struct work_struct work;
2874};
2875
2876/*
2877 * Stack switching interfaces for allocation
2878 */
2879static void
2880xfs_btree_split_worker(
2881 struct work_struct *work)
2882{
2883 struct xfs_btree_split_args *args = container_of(work,
2884 struct xfs_btree_split_args, work);
2885 unsigned long pflags;
2886 unsigned long new_pflags = PF_FSTRANS;
2887
2888 /*
2889 * we are in a transaction context here, but may also be doing work
2890 * in kswapd context, and hence we may need to inherit that state
2891 * temporarily to ensure that we don't block waiting for memory reclaim
2892 * in any way.
2893 */
2894 if (args->kswapd)
2895 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2896
2897 current_set_flags_nested(&pflags, new_pflags);
2898
2899 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2900 args->key, args->curp, args->stat);
2901 complete(args->done);
2902
2903 current_restore_flags_nested(&pflags, new_pflags);
2904}
2905
2906/*
2907 * BMBT split requests often come in with little stack to work on. Push
2908 * them off to a worker thread so there is lots of stack to use. For the other
2909 * btree types, just call directly to avoid the context switch overhead here.
2910 */
2911STATIC int /* error */
2912xfs_btree_split(
2913 struct xfs_btree_cur *cur,
2914 int level,
2915 union xfs_btree_ptr *ptrp,
2916 union xfs_btree_key *key,
2917 struct xfs_btree_cur **curp,
2918 int *stat) /* success/failure */
2919{
2920 struct xfs_btree_split_args args;
2921 DECLARE_COMPLETION_ONSTACK(done);
2922
2923 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2924 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2925
2926 args.cur = cur;
2927 args.level = level;
2928 args.ptrp = ptrp;
2929 args.key = key;
2930 args.curp = curp;
2931 args.stat = stat;
2932 args.done = &done;
2933 args.kswapd = current_is_kswapd();
2934 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2935 queue_work(xfs_alloc_wq, &args.work);
2936 wait_for_completion(&done);
2937 destroy_work_on_stack(&args.work);
2938 return args.result;
2939}
2940
2941
Christoph Hellwig344207c2008-10-30 16:57:16 +11002942/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002943 * Copy the old inode root contents into a real block and make the
2944 * broot point to it.
2945 */
2946int /* error */
2947xfs_btree_new_iroot(
2948 struct xfs_btree_cur *cur, /* btree cursor */
2949 int *logflags, /* logging flags for inode */
2950 int *stat) /* return status - 0 fail */
2951{
2952 struct xfs_buf *cbp; /* buffer for cblock */
2953 struct xfs_btree_block *block; /* btree block */
2954 struct xfs_btree_block *cblock; /* child btree block */
2955 union xfs_btree_key *ckp; /* child key pointer */
2956 union xfs_btree_ptr *cpp; /* child ptr pointer */
2957 union xfs_btree_key *kp; /* pointer to btree key */
2958 union xfs_btree_ptr *pp; /* pointer to block addr */
2959 union xfs_btree_ptr nptr; /* new block addr */
2960 int level; /* btree level */
2961 int error; /* error return code */
2962#ifdef DEBUG
2963 int i; /* loop counter */
2964#endif
2965
2966 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2967 XFS_BTREE_STATS_INC(cur, newroot);
2968
2969 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2970
2971 level = cur->bc_nlevels - 1;
2972
2973 block = xfs_btree_get_iroot(cur);
2974 pp = xfs_btree_ptr_addr(cur, 1, block);
2975
2976 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002977 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002978 if (error)
2979 goto error0;
2980 if (*stat == 0) {
2981 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2982 return 0;
2983 }
2984 XFS_BTREE_STATS_INC(cur, alloc);
2985
2986 /* Copy the root into a real block. */
2987 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2988 if (error)
2989 goto error0;
2990
Dave Chinner088c9f62013-06-12 12:19:08 +10002991 /*
2992 * we can't just memcpy() the root in for CRC enabled btree blocks.
2993 * In that case have to also ensure the blkno remains correct
2994 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002995 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002996 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2997 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2998 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2999 else
3000 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
3001 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003002
3003 be16_add_cpu(&block->bb_level, 1);
3004 xfs_btree_set_numrecs(block, 1);
3005 cur->bc_nlevels++;
3006 cur->bc_ptrs[level + 1] = 1;
3007
3008 kp = xfs_btree_key_addr(cur, 1, block);
3009 ckp = xfs_btree_key_addr(cur, 1, cblock);
3010 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
3011
3012 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3013#ifdef DEBUG
3014 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
3015 error = xfs_btree_check_ptr(cur, pp, i, level);
3016 if (error)
3017 goto error0;
3018 }
3019#endif
3020 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
3021
3022#ifdef DEBUG
3023 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
3024 if (error)
3025 goto error0;
3026#endif
3027 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3028
3029 xfs_iroot_realloc(cur->bc_private.b.ip,
3030 1 - xfs_btree_get_numrecs(cblock),
3031 cur->bc_private.b.whichfork);
3032
3033 xfs_btree_setbuf(cur, level, cbp);
3034
3035 /*
3036 * Do all this logging at the end so that
3037 * the root is at the right level.
3038 */
3039 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3040 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3041 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3042
3043 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06003044 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003045 *stat = 1;
3046 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3047 return 0;
3048error0:
3049 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3050 return error;
3051}
3052
3053/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11003054 * Allocate a new root block, fill it in.
3055 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11003056STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11003057xfs_btree_new_root(
3058 struct xfs_btree_cur *cur, /* btree cursor */
3059 int *stat) /* success/failure */
3060{
3061 struct xfs_btree_block *block; /* one half of the old root block */
3062 struct xfs_buf *bp; /* buffer containing block */
3063 int error; /* error return value */
3064 struct xfs_buf *lbp; /* left buffer pointer */
3065 struct xfs_btree_block *left; /* left btree block */
3066 struct xfs_buf *nbp; /* new (root) buffer */
3067 struct xfs_btree_block *new; /* new (root) btree block */
3068 int nptr; /* new value for key index, 1 or 2 */
3069 struct xfs_buf *rbp; /* right buffer pointer */
3070 struct xfs_btree_block *right; /* right btree block */
3071 union xfs_btree_ptr rptr;
3072 union xfs_btree_ptr lptr;
3073
3074 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3075 XFS_BTREE_STATS_INC(cur, newroot);
3076
3077 /* initialise our start point from the cursor */
3078 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3079
3080 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10003081 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003082 if (error)
3083 goto error0;
3084 if (*stat == 0)
3085 goto out0;
3086 XFS_BTREE_STATS_INC(cur, alloc);
3087
3088 /* Set up the new block. */
3089 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3090 if (error)
3091 goto error0;
3092
3093 /* Set the root in the holding structure increasing the level by 1. */
3094 cur->bc_ops->set_root(cur, &lptr, 1);
3095
3096 /*
3097 * At the previous root level there are now two blocks: the old root,
3098 * and the new block generated when it was split. We don't know which
3099 * one the cursor is pointing at, so we set up variables "left" and
3100 * "right" for each case.
3101 */
3102 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3103
3104#ifdef DEBUG
3105 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3106 if (error)
3107 goto error0;
3108#endif
3109
3110 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3111 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3112 /* Our block is left, pick up the right block. */
3113 lbp = bp;
3114 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3115 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003116 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003117 if (error)
3118 goto error0;
3119 bp = rbp;
3120 nptr = 1;
3121 } else {
3122 /* Our block is right, pick up the left block. */
3123 rbp = bp;
3124 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3125 right = block;
3126 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003127 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003128 if (error)
3129 goto error0;
3130 bp = lbp;
3131 nptr = 2;
3132 }
Darrick J. Wong70b22652016-08-03 11:03:38 +10003133
Christoph Hellwig344207c2008-10-30 16:57:16 +11003134 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05003135 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003136 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3137 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3138 !xfs_btree_ptr_is_null(cur, &rptr));
3139
3140 /* Fill in the key data in the new root. */
3141 if (xfs_btree_get_level(left) > 0) {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003142 /*
3143 * Get the keys for the left block's keys and put them directly
3144 * in the parent block. Do the same for the right block.
3145 */
3146 cur->bc_ops->get_node_keys(cur, left,
3147 xfs_btree_key_addr(cur, 1, new));
3148 cur->bc_ops->get_node_keys(cur, right,
3149 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003150 } else {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003151 /*
3152 * Get the keys for the left block's records and put them
3153 * directly in the parent block. Do the same for the right
3154 * block.
3155 */
3156 cur->bc_ops->get_leaf_keys(cur, left,
3157 xfs_btree_key_addr(cur, 1, new));
3158 cur->bc_ops->get_leaf_keys(cur, right,
3159 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003160 }
3161 xfs_btree_log_keys(cur, nbp, 1, 2);
3162
3163 /* Fill in the pointer data in the new root. */
3164 xfs_btree_copy_ptrs(cur,
3165 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3166 xfs_btree_copy_ptrs(cur,
3167 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3168 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3169
3170 /* Fix up the cursor. */
3171 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3172 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3173 cur->bc_nlevels++;
3174 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3175 *stat = 1;
3176 return 0;
3177error0:
3178 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3179 return error;
3180out0:
3181 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3182 *stat = 0;
3183 return 0;
3184}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003185
3186STATIC int
3187xfs_btree_make_block_unfull(
3188 struct xfs_btree_cur *cur, /* btree cursor */
3189 int level, /* btree level */
3190 int numrecs,/* # of recs in block */
3191 int *oindex,/* old tree index */
3192 int *index, /* new tree index */
3193 union xfs_btree_ptr *nptr, /* new btree ptr */
3194 struct xfs_btree_cur **ncur, /* new btree cursor */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003195 union xfs_btree_key *key, /* key of new block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003196 int *stat)
3197{
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003198 int error = 0;
3199
3200 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3201 level == cur->bc_nlevels - 1) {
3202 struct xfs_inode *ip = cur->bc_private.b.ip;
3203
3204 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3205 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003206 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
Darrick J. Wong0d309792016-08-03 11:01:25 +10003207 *stat = 1;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003208 } else {
3209 /* A root block that needs replacing */
3210 int logflags = 0;
3211
3212 error = xfs_btree_new_iroot(cur, &logflags, stat);
3213 if (error || *stat == 0)
3214 return error;
3215
3216 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3217 }
3218
3219 return 0;
3220 }
3221
3222 /* First, try shifting an entry to the right neighbor. */
3223 error = xfs_btree_rshift(cur, level, stat);
3224 if (error || *stat)
3225 return error;
3226
3227 /* Next, try shifting an entry to the left neighbor. */
3228 error = xfs_btree_lshift(cur, level, stat);
3229 if (error)
3230 return error;
3231
3232 if (*stat) {
3233 *oindex = *index = cur->bc_ptrs[level];
3234 return 0;
3235 }
3236
3237 /*
3238 * Next, try splitting the current block in half.
3239 *
3240 * If this works we have to re-set our variables because we
3241 * could be in a different block now.
3242 */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003243 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003244 if (error || *stat == 0)
3245 return error;
3246
3247
3248 *index = cur->bc_ptrs[level];
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003249 return 0;
3250}
3251
3252/*
3253 * Insert one record/level. Return information to the caller
3254 * allowing the next level up to proceed if necessary.
3255 */
3256STATIC int
3257xfs_btree_insrec(
3258 struct xfs_btree_cur *cur, /* btree cursor */
3259 int level, /* level to insert record at */
3260 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003261 union xfs_btree_rec *rec, /* record to insert */
3262 union xfs_btree_key *key, /* i/o: block key for ptrp */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003263 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3264 int *stat) /* success/failure */
3265{
3266 struct xfs_btree_block *block; /* btree block */
3267 struct xfs_buf *bp; /* buffer for block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003268 union xfs_btree_ptr nptr; /* new block ptr */
3269 struct xfs_btree_cur *ncur; /* new btree cursor */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003270 union xfs_btree_bigkey nkey; /* new block key */
3271 union xfs_btree_key *lkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003272 int optr; /* old key/record index */
3273 int ptr; /* key/record index */
3274 int numrecs;/* number of records */
3275 int error; /* error return value */
3276#ifdef DEBUG
3277 int i;
3278#endif
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003279 xfs_daddr_t old_bn;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003280
3281 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003282 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003283
3284 ncur = NULL;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003285 lkey = (union xfs_btree_key *)&nkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003286
3287 /*
3288 * If we have an external root pointer, and we've made it to the
3289 * root level, allocate a new root block and we're done.
3290 */
3291 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3292 (level >= cur->bc_nlevels)) {
3293 error = xfs_btree_new_root(cur, stat);
3294 xfs_btree_set_ptr_null(cur, ptrp);
3295
3296 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3297 return error;
3298 }
3299
3300 /* If we're off the left edge, return failure. */
3301 ptr = cur->bc_ptrs[level];
3302 if (ptr == 0) {
3303 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3304 *stat = 0;
3305 return 0;
3306 }
3307
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003308 optr = ptr;
3309
3310 XFS_BTREE_STATS_INC(cur, insrec);
3311
3312 /* Get pointers to the btree buffer and block. */
3313 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003314 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003315 numrecs = xfs_btree_get_numrecs(block);
3316
3317#ifdef DEBUG
3318 error = xfs_btree_check_block(cur, block, level, bp);
3319 if (error)
3320 goto error0;
3321
3322 /* Check that the new entry is being inserted in the right place. */
3323 if (ptr <= numrecs) {
3324 if (level == 0) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003325 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003326 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003327 } else {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003328 ASSERT(cur->bc_ops->keys_inorder(cur, key,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003329 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003330 }
3331 }
3332#endif
3333
3334 /*
3335 * If the block is full, we can't insert the new entry until we
3336 * make the block un-full.
3337 */
3338 xfs_btree_set_ptr_null(cur, &nptr);
3339 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3340 error = xfs_btree_make_block_unfull(cur, level, numrecs,
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003341 &optr, &ptr, &nptr, &ncur, lkey, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003342 if (error || *stat == 0)
3343 goto error0;
3344 }
3345
3346 /*
3347 * The current block may have changed if the block was
3348 * previously full and we have just made space in it.
3349 */
3350 block = xfs_btree_get_block(cur, level, &bp);
3351 numrecs = xfs_btree_get_numrecs(block);
3352
3353#ifdef DEBUG
3354 error = xfs_btree_check_block(cur, block, level, bp);
3355 if (error)
3356 return error;
3357#endif
3358
3359 /*
3360 * At this point we know there's room for our new entry in the block
3361 * we're pointing at.
3362 */
3363 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3364
3365 if (level > 0) {
3366 /* It's a nonleaf. make a hole in the keys and ptrs */
3367 union xfs_btree_key *kp;
3368 union xfs_btree_ptr *pp;
3369
3370 kp = xfs_btree_key_addr(cur, ptr, block);
3371 pp = xfs_btree_ptr_addr(cur, ptr, block);
3372
3373#ifdef DEBUG
3374 for (i = numrecs - ptr; i >= 0; i--) {
3375 error = xfs_btree_check_ptr(cur, pp, i, level);
3376 if (error)
3377 return error;
3378 }
3379#endif
3380
3381 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3382 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3383
3384#ifdef DEBUG
3385 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
3386 if (error)
3387 goto error0;
3388#endif
3389
3390 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003391 xfs_btree_copy_keys(cur, kp, key, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003392 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3393 numrecs++;
3394 xfs_btree_set_numrecs(block, numrecs);
3395 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3396 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3397#ifdef DEBUG
3398 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003399 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3400 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003401 }
3402#endif
3403 } else {
3404 /* It's a leaf. make a hole in the records */
3405 union xfs_btree_rec *rp;
3406
3407 rp = xfs_btree_rec_addr(cur, ptr, block);
3408
3409 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3410
3411 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003412 xfs_btree_copy_recs(cur, rp, rec, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003413 xfs_btree_set_numrecs(block, ++numrecs);
3414 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3415#ifdef DEBUG
3416 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003417 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3418 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003419 }
3420#endif
3421 }
3422
3423 /* Log the new number of records in the btree header. */
3424 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3425
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003426 /*
3427 * If we just inserted into a new tree block, we have to
3428 * recalculate nkey here because nkey is out of date.
3429 *
3430 * Otherwise we're just updating an existing block (having shoved
3431 * some records into the new tree block), so use the regular key
3432 * update mechanism.
3433 */
3434 if (bp && bp->b_bn != old_bn) {
3435 xfs_btree_get_keys(cur, block, lkey);
3436 } else if (xfs_btree_needs_key_update(cur, optr)) {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003437 error = cur->bc_ops->update_keys(cur, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003438 if (error)
3439 goto error0;
3440 }
3441
3442 /*
3443 * If we are tracking the last record in the tree and
3444 * we are at the far right edge of the tree, update it.
3445 */
3446 if (xfs_btree_is_lastrec(cur, block, level)) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003447 cur->bc_ops->update_lastrec(cur, block, rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003448 ptr, LASTREC_INSREC);
3449 }
3450
3451 /*
3452 * Return the new block number, if any.
3453 * If there is one, give back a record value and a cursor too.
3454 */
3455 *ptrp = nptr;
3456 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003457 xfs_btree_copy_keys(cur, key, lkey, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003458 *curp = ncur;
3459 }
3460
3461 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3462 *stat = 1;
3463 return 0;
3464
3465error0:
3466 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3467 return error;
3468}
3469
3470/*
3471 * Insert the record at the point referenced by cur.
3472 *
3473 * A multi-level split of the tree on insert will invalidate the original
3474 * cursor. All callers of this function should assume that the cursor is
3475 * no longer valid and revalidate it.
3476 */
3477int
3478xfs_btree_insert(
3479 struct xfs_btree_cur *cur,
3480 int *stat)
3481{
3482 int error; /* error return value */
3483 int i; /* result value, 0 for failure */
3484 int level; /* current level number in btree */
3485 union xfs_btree_ptr nptr; /* new block number (split result) */
3486 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3487 struct xfs_btree_cur *pcur; /* previous level's cursor */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003488 union xfs_btree_bigkey bkey; /* key of block to insert */
3489 union xfs_btree_key *key;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003490 union xfs_btree_rec rec; /* record to insert */
3491
3492 level = 0;
3493 ncur = NULL;
3494 pcur = cur;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003495 key = (union xfs_btree_key *)&bkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003496
3497 xfs_btree_set_ptr_null(cur, &nptr);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003498
3499 /* Make a key out of the record data to be inserted, and save it. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003500 cur->bc_ops->init_rec_from_cur(cur, &rec);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003501 cur->bc_ops->init_key_from_rec(key, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003502
3503 /*
3504 * Loop going up the tree, starting at the leaf level.
3505 * Stop when we don't get a split block, that must mean that
3506 * the insert is finished with this level.
3507 */
3508 do {
3509 /*
3510 * Insert nrec/nptr into this level of the tree.
3511 * Note if we fail, nptr will be null.
3512 */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003513 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003514 &ncur, &i);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003515 if (error) {
3516 if (pcur != cur)
3517 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3518 goto error0;
3519 }
3520
Eric Sandeenc29aad42015-02-23 22:39:08 +11003521 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003522 level++;
3523
3524 /*
3525 * See if the cursor we just used is trash.
3526 * Can't trash the caller's cursor, but otherwise we should
3527 * if ncur is a new cursor or we're about to be done.
3528 */
3529 if (pcur != cur &&
3530 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3531 /* Save the state from the cursor before we trash it */
3532 if (cur->bc_ops->update_cursor)
3533 cur->bc_ops->update_cursor(pcur, cur);
3534 cur->bc_nlevels = pcur->bc_nlevels;
3535 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3536 }
3537 /* If we got a new cursor, switch to it. */
3538 if (ncur) {
3539 pcur = ncur;
3540 ncur = NULL;
3541 }
3542 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3543
3544 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3545 *stat = i;
3546 return 0;
3547error0:
3548 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3549 return error;
3550}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003551
3552/*
3553 * Try to merge a non-leaf block back into the inode root.
3554 *
3555 * Note: the killroot names comes from the fact that we're effectively
3556 * killing the old root block. But because we can't just delete the
3557 * inode we have to copy the single block it was pointing to into the
3558 * inode.
3559 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003560STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003561xfs_btree_kill_iroot(
3562 struct xfs_btree_cur *cur)
3563{
3564 int whichfork = cur->bc_private.b.whichfork;
3565 struct xfs_inode *ip = cur->bc_private.b.ip;
3566 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3567 struct xfs_btree_block *block;
3568 struct xfs_btree_block *cblock;
3569 union xfs_btree_key *kp;
3570 union xfs_btree_key *ckp;
3571 union xfs_btree_ptr *pp;
3572 union xfs_btree_ptr *cpp;
3573 struct xfs_buf *cbp;
3574 int level;
3575 int index;
3576 int numrecs;
Christoph Hellwig196328e2016-02-08 14:58:07 +11003577 int error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003578#ifdef DEBUG
3579 union xfs_btree_ptr ptr;
3580 int i;
3581#endif
3582
3583 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3584
3585 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3586 ASSERT(cur->bc_nlevels > 1);
3587
3588 /*
3589 * Don't deal with the root block needs to be a leaf case.
3590 * We're just going to turn the thing back into extents anyway.
3591 */
3592 level = cur->bc_nlevels - 1;
3593 if (level == 1)
3594 goto out0;
3595
3596 /*
3597 * Give up if the root has multiple children.
3598 */
3599 block = xfs_btree_get_iroot(cur);
3600 if (xfs_btree_get_numrecs(block) != 1)
3601 goto out0;
3602
3603 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3604 numrecs = xfs_btree_get_numrecs(cblock);
3605
3606 /*
3607 * Only do this if the next level will fit.
3608 * Then the data must be copied up to the inode,
3609 * instead of freeing the root you free the next level.
3610 */
3611 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3612 goto out0;
3613
3614 XFS_BTREE_STATS_INC(cur, killroot);
3615
3616#ifdef DEBUG
3617 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3618 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3619 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3620 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3621#endif
3622
3623 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3624 if (index) {
3625 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3626 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003627 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003628 }
3629
3630 be16_add_cpu(&block->bb_numrecs, index);
3631 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3632
3633 kp = xfs_btree_key_addr(cur, 1, block);
3634 ckp = xfs_btree_key_addr(cur, 1, cblock);
3635 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3636
3637 pp = xfs_btree_ptr_addr(cur, 1, block);
3638 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3639#ifdef DEBUG
3640 for (i = 0; i < numrecs; i++) {
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003641 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3642 if (error) {
3643 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3644 return error;
3645 }
3646 }
3647#endif
3648 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3649
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003650 error = xfs_btree_free_block(cur, cbp);
Christoph Hellwig196328e2016-02-08 14:58:07 +11003651 if (error) {
3652 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3653 return error;
3654 }
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003655
3656 cur->bc_bufs[level - 1] = NULL;
3657 be16_add_cpu(&block->bb_level, -1);
3658 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003659 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003660 cur->bc_nlevels--;
3661out0:
3662 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3663 return 0;
3664}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003665
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003666/*
3667 * Kill the current root node, and replace it with it's only child node.
3668 */
3669STATIC int
3670xfs_btree_kill_root(
3671 struct xfs_btree_cur *cur,
3672 struct xfs_buf *bp,
3673 int level,
3674 union xfs_btree_ptr *newroot)
3675{
3676 int error;
3677
3678 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3679 XFS_BTREE_STATS_INC(cur, killroot);
3680
3681 /*
3682 * Update the root pointer, decreasing the level by 1 and then
3683 * free the old root.
3684 */
3685 cur->bc_ops->set_root(cur, newroot, -1);
3686
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003687 error = xfs_btree_free_block(cur, bp);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003688 if (error) {
3689 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3690 return error;
3691 }
3692
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003693 cur->bc_bufs[level] = NULL;
3694 cur->bc_ra[level] = 0;
3695 cur->bc_nlevels--;
3696
3697 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3698 return 0;
3699}
3700
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003701STATIC int
3702xfs_btree_dec_cursor(
3703 struct xfs_btree_cur *cur,
3704 int level,
3705 int *stat)
3706{
3707 int error;
3708 int i;
3709
3710 if (level > 0) {
3711 error = xfs_btree_decrement(cur, level, &i);
3712 if (error)
3713 return error;
3714 }
3715
3716 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3717 *stat = 1;
3718 return 0;
3719}
3720
3721/*
3722 * Single level of the btree record deletion routine.
3723 * Delete record pointed to by cur/level.
3724 * Remove the record from its block then rebalance the tree.
3725 * Return 0 for error, 1 for done, 2 to go on to the next level.
3726 */
3727STATIC int /* error */
3728xfs_btree_delrec(
3729 struct xfs_btree_cur *cur, /* btree cursor */
3730 int level, /* level removing record from */
3731 int *stat) /* fail/done/go-on */
3732{
3733 struct xfs_btree_block *block; /* btree block */
3734 union xfs_btree_ptr cptr; /* current block ptr */
3735 struct xfs_buf *bp; /* buffer for block */
3736 int error; /* error return value */
3737 int i; /* loop counter */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003738 union xfs_btree_ptr lptr; /* left sibling block ptr */
3739 struct xfs_buf *lbp; /* left buffer pointer */
3740 struct xfs_btree_block *left; /* left btree block */
3741 int lrecs = 0; /* left record count */
3742 int ptr; /* key/record index */
3743 union xfs_btree_ptr rptr; /* right sibling block ptr */
3744 struct xfs_buf *rbp; /* right buffer pointer */
3745 struct xfs_btree_block *right; /* right btree block */
3746 struct xfs_btree_block *rrblock; /* right-right btree block */
3747 struct xfs_buf *rrbp; /* right-right buffer pointer */
3748 int rrecs = 0; /* right record count */
3749 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3750 int numrecs; /* temporary numrec count */
3751
3752 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3753 XFS_BTREE_TRACE_ARGI(cur, level);
3754
3755 tcur = NULL;
3756
3757 /* Get the index of the entry being deleted, check for nothing there. */
3758 ptr = cur->bc_ptrs[level];
3759 if (ptr == 0) {
3760 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3761 *stat = 0;
3762 return 0;
3763 }
3764
3765 /* Get the buffer & block containing the record or key/ptr. */
3766 block = xfs_btree_get_block(cur, level, &bp);
3767 numrecs = xfs_btree_get_numrecs(block);
3768
3769#ifdef DEBUG
3770 error = xfs_btree_check_block(cur, block, level, bp);
3771 if (error)
3772 goto error0;
3773#endif
3774
3775 /* Fail if we're off the end of the block. */
3776 if (ptr > numrecs) {
3777 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3778 *stat = 0;
3779 return 0;
3780 }
3781
3782 XFS_BTREE_STATS_INC(cur, delrec);
3783 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3784
3785 /* Excise the entries being deleted. */
3786 if (level > 0) {
3787 /* It's a nonleaf. operate on keys and ptrs */
3788 union xfs_btree_key *lkp;
3789 union xfs_btree_ptr *lpp;
3790
3791 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3792 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3793
3794#ifdef DEBUG
3795 for (i = 0; i < numrecs - ptr; i++) {
3796 error = xfs_btree_check_ptr(cur, lpp, i, level);
3797 if (error)
3798 goto error0;
3799 }
3800#endif
3801
3802 if (ptr < numrecs) {
3803 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3804 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3805 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3806 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3807 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003808 } else {
3809 /* It's a leaf. operate on records */
3810 if (ptr < numrecs) {
3811 xfs_btree_shift_recs(cur,
3812 xfs_btree_rec_addr(cur, ptr + 1, block),
3813 -1, numrecs - ptr);
3814 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3815 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003816 }
3817
3818 /*
3819 * Decrement and log the number of entries in the block.
3820 */
3821 xfs_btree_set_numrecs(block, --numrecs);
3822 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3823
3824 /*
3825 * If we are tracking the last record in the tree and
3826 * we are at the far right edge of the tree, update it.
3827 */
3828 if (xfs_btree_is_lastrec(cur, block, level)) {
3829 cur->bc_ops->update_lastrec(cur, block, NULL,
3830 ptr, LASTREC_DELREC);
3831 }
3832
3833 /*
3834 * We're at the root level. First, shrink the root block in-memory.
3835 * Try to get rid of the next level down. If we can't then there's
3836 * nothing left to do.
3837 */
3838 if (level == cur->bc_nlevels - 1) {
3839 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3840 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3841 cur->bc_private.b.whichfork);
3842
3843 error = xfs_btree_kill_iroot(cur);
3844 if (error)
3845 goto error0;
3846
3847 error = xfs_btree_dec_cursor(cur, level, stat);
3848 if (error)
3849 goto error0;
3850 *stat = 1;
3851 return 0;
3852 }
3853
3854 /*
3855 * If this is the root level, and there's only one entry left,
3856 * and it's NOT the leaf level, then we can get rid of this
3857 * level.
3858 */
3859 if (numrecs == 1 && level > 0) {
3860 union xfs_btree_ptr *pp;
3861 /*
3862 * pp is still set to the first pointer in the block.
3863 * Make it the new root of the btree.
3864 */
3865 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003866 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003867 if (error)
3868 goto error0;
3869 } else if (level > 0) {
3870 error = xfs_btree_dec_cursor(cur, level, stat);
3871 if (error)
3872 goto error0;
3873 }
3874 *stat = 1;
3875 return 0;
3876 }
3877
3878 /*
3879 * If we deleted the leftmost entry in the block, update the
3880 * key values above us in the tree.
3881 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003882 if (xfs_btree_needs_key_update(cur, ptr)) {
3883 error = cur->bc_ops->update_keys(cur, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003884 if (error)
3885 goto error0;
3886 }
3887
3888 /*
3889 * If the number of records remaining in the block is at least
3890 * the minimum, we're done.
3891 */
3892 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3893 error = xfs_btree_dec_cursor(cur, level, stat);
3894 if (error)
3895 goto error0;
3896 return 0;
3897 }
3898
3899 /*
3900 * Otherwise, we have to move some records around to keep the
3901 * tree balanced. Look at the left and right sibling blocks to
3902 * see if we can re-balance by moving only one record.
3903 */
3904 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3905 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3906
3907 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3908 /*
3909 * One child of root, need to get a chance to copy its contents
3910 * into the root and delete it. Can't go up to next level,
3911 * there's nothing to delete there.
3912 */
3913 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3914 xfs_btree_ptr_is_null(cur, &lptr) &&
3915 level == cur->bc_nlevels - 2) {
3916 error = xfs_btree_kill_iroot(cur);
3917 if (!error)
3918 error = xfs_btree_dec_cursor(cur, level, stat);
3919 if (error)
3920 goto error0;
3921 return 0;
3922 }
3923 }
3924
3925 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3926 !xfs_btree_ptr_is_null(cur, &lptr));
3927
3928 /*
3929 * Duplicate the cursor so our btree manipulations here won't
3930 * disrupt the next level up.
3931 */
3932 error = xfs_btree_dup_cursor(cur, &tcur);
3933 if (error)
3934 goto error0;
3935
3936 /*
3937 * If there's a right sibling, see if it's ok to shift an entry
3938 * out of it.
3939 */
3940 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3941 /*
3942 * Move the temp cursor to the last entry in the next block.
3943 * Actually any entry but the first would suffice.
3944 */
3945 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003946 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003947
3948 error = xfs_btree_increment(tcur, level, &i);
3949 if (error)
3950 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003951 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003952
3953 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003954 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003955
3956 /* Grab a pointer to the block. */
3957 right = xfs_btree_get_block(tcur, level, &rbp);
3958#ifdef DEBUG
3959 error = xfs_btree_check_block(tcur, right, level, rbp);
3960 if (error)
3961 goto error0;
3962#endif
3963 /* Grab the current block number, for future use. */
3964 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3965
3966 /*
3967 * If right block is full enough so that removing one entry
3968 * won't make it too empty, and left-shifting an entry out
3969 * of right to us works, we're done.
3970 */
3971 if (xfs_btree_get_numrecs(right) - 1 >=
3972 cur->bc_ops->get_minrecs(tcur, level)) {
3973 error = xfs_btree_lshift(tcur, level, &i);
3974 if (error)
3975 goto error0;
3976 if (i) {
3977 ASSERT(xfs_btree_get_numrecs(block) >=
3978 cur->bc_ops->get_minrecs(tcur, level));
3979
3980 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3981 tcur = NULL;
3982
3983 error = xfs_btree_dec_cursor(cur, level, stat);
3984 if (error)
3985 goto error0;
3986 return 0;
3987 }
3988 }
3989
3990 /*
3991 * Otherwise, grab the number of records in right for
3992 * future reference, and fix up the temp cursor to point
3993 * to our block again (last record).
3994 */
3995 rrecs = xfs_btree_get_numrecs(right);
3996 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3997 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003998 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003999
4000 error = xfs_btree_decrement(tcur, level, &i);
4001 if (error)
4002 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004003 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004004 }
4005 }
4006
4007 /*
4008 * If there's a left sibling, see if it's ok to shift an entry
4009 * out of it.
4010 */
4011 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
4012 /*
4013 * Move the temp cursor to the first entry in the
4014 * previous block.
4015 */
4016 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11004017 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004018
4019 error = xfs_btree_decrement(tcur, level, &i);
4020 if (error)
4021 goto error0;
4022 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11004023 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004024
4025 /* Grab a pointer to the block. */
4026 left = xfs_btree_get_block(tcur, level, &lbp);
4027#ifdef DEBUG
4028 error = xfs_btree_check_block(cur, left, level, lbp);
4029 if (error)
4030 goto error0;
4031#endif
4032 /* Grab the current block number, for future use. */
4033 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
4034
4035 /*
4036 * If left block is full enough so that removing one entry
4037 * won't make it too empty, and right-shifting an entry out
4038 * of left to us works, we're done.
4039 */
4040 if (xfs_btree_get_numrecs(left) - 1 >=
4041 cur->bc_ops->get_minrecs(tcur, level)) {
4042 error = xfs_btree_rshift(tcur, level, &i);
4043 if (error)
4044 goto error0;
4045 if (i) {
4046 ASSERT(xfs_btree_get_numrecs(block) >=
4047 cur->bc_ops->get_minrecs(tcur, level));
4048 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4049 tcur = NULL;
4050 if (level == 0)
4051 cur->bc_ptrs[0]++;
4052 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4053 *stat = 1;
4054 return 0;
4055 }
4056 }
4057
4058 /*
4059 * Otherwise, grab the number of records in right for
4060 * future reference.
4061 */
4062 lrecs = xfs_btree_get_numrecs(left);
4063 }
4064
4065 /* Delete the temp cursor, we're done with it. */
4066 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4067 tcur = NULL;
4068
4069 /* If here, we need to do a join to keep the tree balanced. */
4070 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4071
4072 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4073 lrecs + xfs_btree_get_numrecs(block) <=
4074 cur->bc_ops->get_maxrecs(cur, level)) {
4075 /*
4076 * Set "right" to be the starting block,
4077 * "left" to be the left neighbor.
4078 */
4079 rptr = cptr;
4080 right = block;
4081 rbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004082 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004083 if (error)
4084 goto error0;
4085
4086 /*
4087 * If that won't work, see if we can join with the right neighbor block.
4088 */
4089 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4090 rrecs + xfs_btree_get_numrecs(block) <=
4091 cur->bc_ops->get_maxrecs(cur, level)) {
4092 /*
4093 * Set "left" to be the starting block,
4094 * "right" to be the right neighbor.
4095 */
4096 lptr = cptr;
4097 left = block;
4098 lbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004099 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004100 if (error)
4101 goto error0;
4102
4103 /*
4104 * Otherwise, we can't fix the imbalance.
4105 * Just return. This is probably a logic error, but it's not fatal.
4106 */
4107 } else {
4108 error = xfs_btree_dec_cursor(cur, level, stat);
4109 if (error)
4110 goto error0;
4111 return 0;
4112 }
4113
4114 rrecs = xfs_btree_get_numrecs(right);
4115 lrecs = xfs_btree_get_numrecs(left);
4116
4117 /*
4118 * We're now going to join "left" and "right" by moving all the stuff
4119 * in "right" to "left" and deleting "right".
4120 */
4121 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4122 if (level > 0) {
4123 /* It's a non-leaf. Move keys and pointers. */
4124 union xfs_btree_key *lkp; /* left btree key */
4125 union xfs_btree_ptr *lpp; /* left address pointer */
4126 union xfs_btree_key *rkp; /* right btree key */
4127 union xfs_btree_ptr *rpp; /* right address pointer */
4128
4129 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4130 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4131 rkp = xfs_btree_key_addr(cur, 1, right);
4132 rpp = xfs_btree_ptr_addr(cur, 1, right);
4133#ifdef DEBUG
4134 for (i = 1; i < rrecs; i++) {
4135 error = xfs_btree_check_ptr(cur, rpp, i, level);
4136 if (error)
4137 goto error0;
4138 }
4139#endif
4140 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4141 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4142
4143 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4144 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4145 } else {
4146 /* It's a leaf. Move records. */
4147 union xfs_btree_rec *lrp; /* left record pointer */
4148 union xfs_btree_rec *rrp; /* right record pointer */
4149
4150 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4151 rrp = xfs_btree_rec_addr(cur, 1, right);
4152
4153 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4154 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4155 }
4156
4157 XFS_BTREE_STATS_INC(cur, join);
4158
4159 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004160 * Fix up the number of records and right block pointer in the
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004161 * surviving block, and log it.
4162 */
4163 xfs_btree_set_numrecs(left, lrecs + rrecs);
4164 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4165 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4166 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4167
4168 /* If there is a right sibling, point it to the remaining block. */
4169 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4170 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004171 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004172 if (error)
4173 goto error0;
4174 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4175 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4176 }
4177
4178 /* Free the deleted block. */
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11004179 error = xfs_btree_free_block(cur, rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004180 if (error)
4181 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004182
4183 /*
4184 * If we joined with the left neighbor, set the buffer in the
4185 * cursor to the left block, and fix up the index.
4186 */
4187 if (bp != lbp) {
4188 cur->bc_bufs[level] = lbp;
4189 cur->bc_ptrs[level] += lrecs;
4190 cur->bc_ra[level] = 0;
4191 }
4192 /*
4193 * If we joined with the right neighbor and there's a level above
4194 * us, increment the cursor at that level.
4195 */
4196 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4197 (level + 1 < cur->bc_nlevels)) {
4198 error = xfs_btree_increment(cur, level + 1, &i);
4199 if (error)
4200 goto error0;
4201 }
4202
4203 /*
4204 * Readjust the ptr at this level if it's not a leaf, since it's
4205 * still pointing at the deletion point, which makes the cursor
4206 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4207 * We can't use decrement because it would change the next level up.
4208 */
4209 if (level > 0)
4210 cur->bc_ptrs[level]--;
4211
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004212 /*
4213 * We combined blocks, so we have to update the parent keys if the
4214 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4215 * points to the old block so that the caller knows which record to
4216 * delete. Therefore, the caller must be savvy enough to call updkeys
4217 * for us if we return stat == 2. The other exit points from this
4218 * function don't require deletions further up the tree, so they can
4219 * call updkeys directly.
4220 */
4221
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004222 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4223 /* Return value means the next level up has something to do. */
4224 *stat = 2;
4225 return 0;
4226
4227error0:
4228 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4229 if (tcur)
4230 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4231 return error;
4232}
4233
4234/*
4235 * Delete the record pointed to by cur.
4236 * The cursor refers to the place where the record was (could be inserted)
4237 * when the operation returns.
4238 */
4239int /* error */
4240xfs_btree_delete(
4241 struct xfs_btree_cur *cur,
4242 int *stat) /* success/failure */
4243{
4244 int error; /* error return value */
4245 int level;
4246 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004247 bool joined = false;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004248
4249 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
4250
4251 /*
4252 * Go up the tree, starting at leaf level.
4253 *
4254 * If 2 is returned then a join was done; go to the next level.
4255 * Otherwise we are done.
4256 */
4257 for (level = 0, i = 2; i == 2; level++) {
4258 error = xfs_btree_delrec(cur, level, &i);
4259 if (error)
4260 goto error0;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004261 if (i == 2)
4262 joined = true;
4263 }
4264
4265 /*
4266 * If we combined blocks as part of deleting the record, delrec won't
4267 * have updated the parent high keys so we have to do that here.
4268 */
4269 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4270 error = xfs_btree_updkeys_force(cur, 0);
4271 if (error)
4272 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004273 }
4274
4275 if (i == 0) {
4276 for (level = 1; level < cur->bc_nlevels; level++) {
4277 if (cur->bc_ptrs[level] == 0) {
4278 error = xfs_btree_decrement(cur, level, &i);
4279 if (error)
4280 goto error0;
4281 break;
4282 }
4283 }
4284 }
4285
4286 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4287 *stat = i;
4288 return 0;
4289error0:
4290 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4291 return error;
4292}
Christoph Hellwig8cc938f2008-10-30 16:58:11 +11004293
4294/*
4295 * Get the data from the pointed-to record.
4296 */
4297int /* error */
4298xfs_btree_get_rec(
4299 struct xfs_btree_cur *cur, /* btree cursor */
4300 union xfs_btree_rec **recp, /* output: btree record */
4301 int *stat) /* output: success/failure */
4302{
4303 struct xfs_btree_block *block; /* btree block */
4304 struct xfs_buf *bp; /* buffer pointer */
4305 int ptr; /* record number */
4306#ifdef DEBUG
4307 int error; /* error return value */
4308#endif
4309
4310 ptr = cur->bc_ptrs[0];
4311 block = xfs_btree_get_block(cur, 0, &bp);
4312
4313#ifdef DEBUG
4314 error = xfs_btree_check_block(cur, block, 0, bp);
4315 if (error)
4316 return error;
4317#endif
4318
4319 /*
4320 * Off the right end or left end, return failure.
4321 */
4322 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4323 *stat = 0;
4324 return 0;
4325 }
4326
4327 /*
4328 * Point to the record and extract its data.
4329 */
4330 *recp = xfs_btree_rec_addr(cur, ptr, block);
4331 *stat = 1;
4332 return 0;
4333}
Dave Chinner21b5c972013-08-30 10:23:44 +10004334
Darrick J. Wong28a89562016-08-03 11:10:55 +10004335/* Visit a block in a btree. */
4336STATIC int
4337xfs_btree_visit_block(
4338 struct xfs_btree_cur *cur,
4339 int level,
4340 xfs_btree_visit_blocks_fn fn,
4341 void *data)
4342{
4343 struct xfs_btree_block *block;
4344 struct xfs_buf *bp;
4345 union xfs_btree_ptr rptr;
4346 int error;
4347
4348 /* do right sibling readahead */
4349 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4350 block = xfs_btree_get_block(cur, level, &bp);
4351
4352 /* process the block */
4353 error = fn(cur, level, data);
4354 if (error)
4355 return error;
4356
4357 /* now read rh sibling block for next iteration */
4358 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4359 if (xfs_btree_ptr_is_null(cur, &rptr))
4360 return -ENOENT;
4361
4362 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4363}
4364
4365
4366/* Visit every block in a btree. */
4367int
4368xfs_btree_visit_blocks(
4369 struct xfs_btree_cur *cur,
4370 xfs_btree_visit_blocks_fn fn,
4371 void *data)
4372{
4373 union xfs_btree_ptr lptr;
4374 int level;
4375 struct xfs_btree_block *block = NULL;
4376 int error = 0;
4377
4378 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4379
4380 /* for each level */
4381 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4382 /* grab the left hand block */
4383 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4384 if (error)
4385 return error;
4386
4387 /* readahead the left most block for the next level down */
4388 if (level > 0) {
4389 union xfs_btree_ptr *ptr;
4390
4391 ptr = xfs_btree_ptr_addr(cur, 1, block);
4392 xfs_btree_readahead_ptr(cur, ptr, 1);
4393
4394 /* save for the next iteration of the loop */
4395 lptr = *ptr;
4396 }
4397
4398 /* for each buffer in the level */
4399 do {
4400 error = xfs_btree_visit_block(cur, level, fn, data);
4401 } while (!error);
4402
4403 if (error != -ENOENT)
4404 return error;
4405 }
4406
4407 return 0;
4408}
4409
Dave Chinner21b5c972013-08-30 10:23:44 +10004410/*
4411 * Change the owner of a btree.
4412 *
4413 * The mechanism we use here is ordered buffer logging. Because we don't know
4414 * how many buffers were are going to need to modify, we don't really want to
4415 * have to make transaction reservations for the worst case of every buffer in a
4416 * full size btree as that may be more space that we can fit in the log....
4417 *
4418 * We do the btree walk in the most optimal manner possible - we have sibling
4419 * pointers so we can just walk all the blocks on each level from left to right
4420 * in a single pass, and then move to the next level and do the same. We can
4421 * also do readahead on the sibling pointers to get IO moving more quickly,
4422 * though for slow disks this is unlikely to make much difference to performance
4423 * as the amount of CPU work we have to do before moving to the next block is
4424 * relatively small.
4425 *
4426 * For each btree block that we load, modify the owner appropriately, set the
4427 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4428 * we mark the region we change dirty so that if the buffer is relogged in
4429 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10004430 * correctly relogged in that transaction. If we are in recovery context, then
4431 * just queue the modified buffer as delayed write buffer so the transaction
4432 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10004433 */
Darrick J. Wong28a89562016-08-03 11:10:55 +10004434struct xfs_btree_block_change_owner_info {
4435 __uint64_t new_owner;
4436 struct list_head *buffer_list;
4437};
4438
Dave Chinner21b5c972013-08-30 10:23:44 +10004439static int
4440xfs_btree_block_change_owner(
4441 struct xfs_btree_cur *cur,
4442 int level,
Darrick J. Wong28a89562016-08-03 11:10:55 +10004443 void *data)
Dave Chinner21b5c972013-08-30 10:23:44 +10004444{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004445 struct xfs_btree_block_change_owner_info *bbcoi = data;
Dave Chinner21b5c972013-08-30 10:23:44 +10004446 struct xfs_btree_block *block;
4447 struct xfs_buf *bp;
Dave Chinner21b5c972013-08-30 10:23:44 +10004448
4449 /* modify the owner */
4450 block = xfs_btree_get_block(cur, level, &bp);
4451 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Darrick J. Wong28a89562016-08-03 11:10:55 +10004452 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
Dave Chinner21b5c972013-08-30 10:23:44 +10004453 else
Darrick J. Wong28a89562016-08-03 11:10:55 +10004454 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
Dave Chinner21b5c972013-08-30 10:23:44 +10004455
4456 /*
Dave Chinner638f44162013-08-30 10:23:45 +10004457 * If the block is a root block hosted in an inode, we might not have a
4458 * buffer pointer here and we shouldn't attempt to log the change as the
4459 * information is already held in the inode and discarded when the root
4460 * block is formatted into the on-disk inode fork. We still change it,
4461 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10004462 */
4463 if (bp) {
Dave Chinner638f44162013-08-30 10:23:45 +10004464 if (cur->bc_tp) {
4465 xfs_trans_ordered_buf(cur->bc_tp, bp);
4466 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4467 } else {
Darrick J. Wong28a89562016-08-03 11:10:55 +10004468 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
Dave Chinner638f44162013-08-30 10:23:45 +10004469 }
Dave Chinner21b5c972013-08-30 10:23:44 +10004470 } else {
4471 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4472 ASSERT(level == cur->bc_nlevels - 1);
4473 }
4474
Darrick J. Wong28a89562016-08-03 11:10:55 +10004475 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10004476}
4477
4478int
4479xfs_btree_change_owner(
4480 struct xfs_btree_cur *cur,
Dave Chinner638f44162013-08-30 10:23:45 +10004481 __uint64_t new_owner,
4482 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004483{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004484 struct xfs_btree_block_change_owner_info bbcoi;
Dave Chinner21b5c972013-08-30 10:23:44 +10004485
Darrick J. Wong28a89562016-08-03 11:10:55 +10004486 bbcoi.new_owner = new_owner;
4487 bbcoi.buffer_list = buffer_list;
Dave Chinner21b5c972013-08-30 10:23:44 +10004488
Darrick J. Wong28a89562016-08-03 11:10:55 +10004489 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4490 &bbcoi);
Dave Chinner21b5c972013-08-30 10:23:44 +10004491}
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004492
4493/**
4494 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4495 * btree block
4496 *
4497 * @bp: buffer containing the btree block
4498 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4499 * @pag_max_level: pointer to the per-ag max level field
4500 */
4501bool
4502xfs_btree_sblock_v5hdr_verify(
4503 struct xfs_buf *bp)
4504{
4505 struct xfs_mount *mp = bp->b_target->bt_mount;
4506 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4507 struct xfs_perag *pag = bp->b_pag;
4508
4509 if (!xfs_sb_version_hascrc(&mp->m_sb))
4510 return false;
4511 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4512 return false;
4513 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4514 return false;
4515 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4516 return false;
4517 return true;
4518}
4519
4520/**
4521 * xfs_btree_sblock_verify() -- verify a short-format btree block
4522 *
4523 * @bp: buffer containing the btree block
4524 * @max_recs: maximum records allowed in this btree node
4525 */
4526bool
4527xfs_btree_sblock_verify(
4528 struct xfs_buf *bp,
4529 unsigned int max_recs)
4530{
4531 struct xfs_mount *mp = bp->b_target->bt_mount;
4532 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4533
4534 /* numrecs verification */
4535 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4536 return false;
4537
4538 /* sibling pointer verification */
4539 if (!block->bb_u.s.bb_leftsib ||
4540 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
4541 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
4542 return false;
4543 if (!block->bb_u.s.bb_rightsib ||
4544 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
4545 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
4546 return false;
4547
4548 return true;
4549}
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004550
4551/*
4552 * Calculate the number of btree levels needed to store a given number of
4553 * records in a short-format btree.
4554 */
4555uint
4556xfs_btree_compute_maxlevels(
4557 struct xfs_mount *mp,
4558 uint *limits,
4559 unsigned long len)
4560{
4561 uint level;
4562 unsigned long maxblocks;
4563
4564 maxblocks = (len + limits[0] - 1) / limits[0];
4565 for (level = 1; maxblocks > 1; level++)
4566 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4567 return level;
4568}
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004569
4570/*
4571 * Query a regular btree for all records overlapping a given interval.
4572 * Start with a LE lookup of the key of low_rec and return all records
4573 * until we find a record with a key greater than the key of high_rec.
4574 */
4575STATIC int
4576xfs_btree_simple_query_range(
4577 struct xfs_btree_cur *cur,
4578 union xfs_btree_key *low_key,
4579 union xfs_btree_key *high_key,
4580 xfs_btree_query_range_fn fn,
4581 void *priv)
4582{
4583 union xfs_btree_rec *recp;
4584 union xfs_btree_key rec_key;
4585 __int64_t diff;
4586 int stat;
4587 bool firstrec = true;
4588 int error;
4589
4590 ASSERT(cur->bc_ops->init_high_key_from_rec);
4591 ASSERT(cur->bc_ops->diff_two_keys);
4592
4593 /*
4594 * Find the leftmost record. The btree cursor must be set
4595 * to the low record used to generate low_key.
4596 */
4597 stat = 0;
4598 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4599 if (error)
4600 goto out;
4601
4602 while (stat) {
4603 /* Find the record. */
4604 error = xfs_btree_get_rec(cur, &recp, &stat);
4605 if (error || !stat)
4606 break;
4607 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
4608
4609 /* Skip if high_key(rec) < low_key. */
4610 if (firstrec) {
4611 firstrec = false;
4612 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4613 &rec_key);
4614 if (diff > 0)
4615 goto advloop;
4616 }
4617
4618 /* Stop if high_key < low_key(rec). */
4619 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4620 if (diff > 0)
4621 break;
4622
4623 /* Callback */
4624 error = fn(cur, recp, priv);
4625 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4626 break;
4627
4628advloop:
4629 /* Move on to the next record. */
4630 error = xfs_btree_increment(cur, 0, &stat);
4631 if (error)
4632 break;
4633 }
4634
4635out:
4636 return error;
4637}
4638
4639/*
4640 * Query an overlapped interval btree for all records overlapping a given
4641 * interval. This function roughly follows the algorithm given in
4642 * "Interval Trees" of _Introduction to Algorithms_, which is section
4643 * 14.3 in the 2nd and 3rd editions.
4644 *
4645 * First, generate keys for the low and high records passed in.
4646 *
4647 * For any leaf node, generate the high and low keys for the record.
4648 * If the record keys overlap with the query low/high keys, pass the
4649 * record to the function iterator.
4650 *
4651 * For any internal node, compare the low and high keys of each
4652 * pointer against the query low/high keys. If there's an overlap,
4653 * follow the pointer.
4654 *
4655 * As an optimization, we stop scanning a block when we find a low key
4656 * that is greater than the query's high key.
4657 */
4658STATIC int
4659xfs_btree_overlapped_query_range(
4660 struct xfs_btree_cur *cur,
4661 union xfs_btree_key *low_key,
4662 union xfs_btree_key *high_key,
4663 xfs_btree_query_range_fn fn,
4664 void *priv)
4665{
4666 union xfs_btree_ptr ptr;
4667 union xfs_btree_ptr *pp;
4668 union xfs_btree_key rec_key;
4669 union xfs_btree_key rec_hkey;
4670 union xfs_btree_key *lkp;
4671 union xfs_btree_key *hkp;
4672 union xfs_btree_rec *recp;
4673 struct xfs_btree_block *block;
4674 __int64_t ldiff;
4675 __int64_t hdiff;
4676 int level;
4677 struct xfs_buf *bp;
4678 int i;
4679 int error;
4680
4681 /* Load the root of the btree. */
4682 level = cur->bc_nlevels - 1;
4683 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4684 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4685 if (error)
4686 return error;
4687 xfs_btree_get_block(cur, level, &bp);
4688 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4689#ifdef DEBUG
4690 error = xfs_btree_check_block(cur, block, level, bp);
4691 if (error)
4692 goto out;
4693#endif
4694 cur->bc_ptrs[level] = 1;
4695
4696 while (level < cur->bc_nlevels) {
4697 block = xfs_btree_get_block(cur, level, &bp);
4698
4699 /* End of node, pop back towards the root. */
4700 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4701pop_up:
4702 if (level < cur->bc_nlevels - 1)
4703 cur->bc_ptrs[level + 1]++;
4704 level++;
4705 continue;
4706 }
4707
4708 if (level == 0) {
4709 /* Handle a leaf node. */
4710 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4711
4712 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4713 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4714 low_key);
4715
4716 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4717 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4718 &rec_key);
4719
4720 /*
4721 * If (record's high key >= query's low key) and
4722 * (query's high key >= record's low key), then
4723 * this record overlaps the query range; callback.
4724 */
4725 if (ldiff >= 0 && hdiff >= 0) {
4726 error = fn(cur, recp, priv);
4727 if (error < 0 ||
4728 error == XFS_BTREE_QUERY_RANGE_ABORT)
4729 break;
4730 } else if (hdiff < 0) {
4731 /* Record is larger than high key; pop. */
4732 goto pop_up;
4733 }
4734 cur->bc_ptrs[level]++;
4735 continue;
4736 }
4737
4738 /* Handle an internal node. */
4739 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4740 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4741 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4742
4743 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4744 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4745
4746 /*
4747 * If (pointer's high key >= query's low key) and
4748 * (query's high key >= pointer's low key), then
4749 * this record overlaps the query range; follow pointer.
4750 */
4751 if (ldiff >= 0 && hdiff >= 0) {
4752 level--;
4753 error = xfs_btree_lookup_get_block(cur, level, pp,
4754 &block);
4755 if (error)
4756 goto out;
4757 xfs_btree_get_block(cur, level, &bp);
4758 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4759#ifdef DEBUG
4760 error = xfs_btree_check_block(cur, block, level, bp);
4761 if (error)
4762 goto out;
4763#endif
4764 cur->bc_ptrs[level] = 1;
4765 continue;
4766 } else if (hdiff < 0) {
4767 /* The low key is larger than the upper range; pop. */
4768 goto pop_up;
4769 }
4770 cur->bc_ptrs[level]++;
4771 }
4772
4773out:
4774 /*
4775 * If we don't end this function with the cursor pointing at a record
4776 * block, a subsequent non-error cursor deletion will not release
4777 * node-level buffers, causing a buffer leak. This is quite possible
4778 * with a zero-results range query, so release the buffers if we
4779 * failed to return any results.
4780 */
4781 if (cur->bc_bufs[0] == NULL) {
4782 for (i = 0; i < cur->bc_nlevels; i++) {
4783 if (cur->bc_bufs[i]) {
4784 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4785 cur->bc_bufs[i] = NULL;
4786 cur->bc_ptrs[i] = 0;
4787 cur->bc_ra[i] = 0;
4788 }
4789 }
4790 }
4791
4792 return error;
4793}
4794
4795/*
4796 * Query a btree for all records overlapping a given interval of keys. The
4797 * supplied function will be called with each record found; return one of the
4798 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4799 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4800 * negative error code.
4801 */
4802int
4803xfs_btree_query_range(
4804 struct xfs_btree_cur *cur,
4805 union xfs_btree_irec *low_rec,
4806 union xfs_btree_irec *high_rec,
4807 xfs_btree_query_range_fn fn,
4808 void *priv)
4809{
4810 union xfs_btree_rec rec;
4811 union xfs_btree_key low_key;
4812 union xfs_btree_key high_key;
4813
4814 /* Find the keys of both ends of the interval. */
4815 cur->bc_rec = *high_rec;
4816 cur->bc_ops->init_rec_from_cur(cur, &rec);
4817 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4818
4819 cur->bc_rec = *low_rec;
4820 cur->bc_ops->init_rec_from_cur(cur, &rec);
4821 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4822
4823 /* Enforce low key < high key. */
4824 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4825 return -EINVAL;
4826
4827 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4828 return xfs_btree_simple_query_range(cur, &low_key,
4829 &high_key, fn, priv);
4830 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4831 fn, priv);
4832}