blob: e2cc98931552949bc708095ce24f4e259070c462 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050016#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110017#include "xfs_btree.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070018#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000020#include "xfs_trace.h"
Dave Chinnercf11da92014-07-15 07:08:24 +100021#include "xfs_alloc.h"
Brian Fostera45086e2015-10-12 15:59:25 +110022#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Cursor allocation zone.
26 */
27kmem_zone_t *xfs_btree_cur_zone;
28
29/*
30 * Btree magic numbers.
31 */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070032static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
Darrick J. Wongb8704942016-08-03 11:30:32 +100033 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070034 XFS_FIBT_MAGIC, 0 },
Darrick J. Wongb8704942016-08-03 11:30:32 +100035 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070036 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
37 XFS_REFC_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080039
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070040uint32_t
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080041xfs_btree_magic(
42 int crc,
43 xfs_btnum_t btnum)
44{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070045 uint32_t magic = xfs_magics[crc][btnum];
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080046
47 /* Ensure we asked for crc for crc-only magics. */
48 ASSERT(magic != 0);
49 return magic;
50}
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Darrick J. Wong52c732e2017-10-17 21:37:33 -070052/*
53 * Check a long btree block header. Return the address of the failing check,
54 * or NULL if everything is ok.
55 */
56xfs_failaddr_t
57__xfs_btree_check_lblock(
58 struct xfs_btree_cur *cur,
59 struct xfs_btree_block *block,
60 int level,
61 struct xfs_buf *bp)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110062{
Darrick J. Wong52c732e2017-10-17 21:37:33 -070063 struct xfs_mount *mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080064 xfs_btnum_t btnum = cur->bc_btnum;
Darrick J. Wong52c732e2017-10-17 21:37:33 -070065 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050066
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080067 if (crc) {
Darrick J. Wong52c732e2017-10-17 21:37:33 -070068 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
69 return __this_address;
70 if (block->bb_u.l.bb_blkno !=
71 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
72 return __this_address;
73 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
74 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050075 }
76
Darrick J. Wong52c732e2017-10-17 21:37:33 -070077 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
78 return __this_address;
79 if (be16_to_cpu(block->bb_level) != level)
80 return __this_address;
81 if (be16_to_cpu(block->bb_numrecs) >
82 cur->bc_ops->get_maxrecs(cur, level))
83 return __this_address;
84 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
85 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib),
86 level + 1))
87 return __this_address;
88 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
89 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib),
90 level + 1))
91 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050092
Darrick J. Wong52c732e2017-10-17 21:37:33 -070093 return NULL;
94}
95
96/* Check a long btree block header. */
Christoph Hellwig4483eb52017-11-06 11:54:01 -080097static int
Darrick J. Wong52c732e2017-10-17 21:37:33 -070098xfs_btree_check_lblock(
99 struct xfs_btree_cur *cur,
100 struct xfs_btree_block *block,
101 int level,
102 struct xfs_buf *bp)
103{
104 struct xfs_mount *mp = cur->bc_mp;
105 xfs_failaddr_t fa;
106
107 fa = __xfs_btree_check_lblock(cur, block, level, bp);
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800108 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
109 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BTREE_CHECK_LBLOCK)) {
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100110 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000111 trace_xfs_btree_corrupt(bp, _RET_IP_);
Dave Chinner24513372014-06-25 14:58:08 +1000112 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100113 }
114 return 0;
115}
116
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700117/*
118 * Check a short btree block header. Return the address of the failing check,
119 * or NULL if everything is ok.
120 */
121xfs_failaddr_t
122__xfs_btree_check_sblock(
123 struct xfs_btree_cur *cur,
124 struct xfs_btree_block *block,
125 int level,
126 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700128 struct xfs_mount *mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800129 xfs_btnum_t btnum = cur->bc_btnum;
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700130 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500131
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800132 if (crc) {
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700133 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
134 return __this_address;
135 if (block->bb_u.s.bb_blkno !=
136 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
137 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500138 }
139
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700140 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
141 return __this_address;
142 if (be16_to_cpu(block->bb_level) != level)
143 return __this_address;
144 if (be16_to_cpu(block->bb_numrecs) >
145 cur->bc_ops->get_maxrecs(cur, level))
146 return __this_address;
147 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
148 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib),
149 level + 1))
150 return __this_address;
151 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
152 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib),
153 level + 1))
154 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500155
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700156 return NULL;
157}
158
159/* Check a short btree block header. */
160STATIC int
161xfs_btree_check_sblock(
162 struct xfs_btree_cur *cur,
163 struct xfs_btree_block *block,
164 int level,
165 struct xfs_buf *bp)
166{
167 struct xfs_mount *mp = cur->bc_mp;
168 xfs_failaddr_t fa;
169
170 fa = __xfs_btree_check_sblock(cur, block, level, bp);
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800171 if (XFS_IS_CORRUPT(mp, fa != NULL) ||
172 XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BTREE_CHECK_SBLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000174 trace_xfs_btree_corrupt(bp, _RET_IP_);
Dave Chinner24513372014-06-25 14:58:08 +1000175 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 return 0;
178}
179
180/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100181 * Debug routine: check that block header is ok.
182 */
183int
184xfs_btree_check_block(
185 struct xfs_btree_cur *cur, /* btree cursor */
186 struct xfs_btree_block *block, /* generic btree block pointer */
187 int level, /* level of the btree block */
188 struct xfs_buf *bp) /* buffer containing block, if any */
189{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100190 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
191 return xfs_btree_check_lblock(cur, block, level, bp);
192 else
193 return xfs_btree_check_sblock(cur, block, level, bp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100194}
195
Darrick J. Wongf1357612017-10-17 21:37:33 -0700196/* Check that this long pointer is valid and points within the fs. */
197bool
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100198xfs_btree_check_lptr(
Darrick J. Wongf1357612017-10-17 21:37:33 -0700199 struct xfs_btree_cur *cur,
200 xfs_fsblock_t fsbno,
201 int level)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100202{
Darrick J. Wongf1357612017-10-17 21:37:33 -0700203 if (level <= 0)
204 return false;
205 return xfs_verify_fsbno(cur->bc_mp, fsbno);
206}
207
208/* Check that this short pointer is valid and points within the AG. */
209bool
210xfs_btree_check_sptr(
211 struct xfs_btree_cur *cur,
212 xfs_agblock_t agbno,
213 int level)
214{
215 if (level <= 0)
216 return false;
217 return xfs_verify_agbno(cur->bc_mp, cur->bc_private.a.agno, agbno);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100218}
219
220/*
Darrick J. Wongf1357612017-10-17 21:37:33 -0700221 * Check that a given (indexed) btree pointer at a certain level of a
222 * btree is valid and doesn't point past where it should.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Christoph Hellwig4483eb52017-11-06 11:54:01 -0800224static int
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100225xfs_btree_check_ptr(
Darrick J. Wongf1357612017-10-17 21:37:33 -0700226 struct xfs_btree_cur *cur,
227 union xfs_btree_ptr *ptr,
228 int index,
229 int level)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100230{
231 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700232 if (xfs_btree_check_lptr(cur, be64_to_cpu((&ptr->l)[index]),
233 level))
234 return 0;
235 xfs_err(cur->bc_mp,
236"Inode %llu fork %d: Corrupt btree %d pointer at level %d index %d.",
237 cur->bc_private.b.ip->i_ino,
238 cur->bc_private.b.whichfork, cur->bc_btnum,
239 level, index);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100240 } else {
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700241 if (xfs_btree_check_sptr(cur, be32_to_cpu((&ptr->s)[index]),
242 level))
243 return 0;
244 xfs_err(cur->bc_mp,
245"AG %u: Corrupt btree %d pointer at level %d index %d.",
246 cur->bc_private.a.agno, cur->bc_btnum,
247 level, index);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100248 }
Darrick J. Wongf1357612017-10-17 21:37:33 -0700249
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700250 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100251}
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -0700252
253#ifdef DEBUG
254# define xfs_btree_debug_check_ptr xfs_btree_check_ptr
255#else
256# define xfs_btree_debug_check_ptr(...) (0)
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100257#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100258
259/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500260 * Calculate CRC on the whole btree block and stuff it into the
261 * long-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_lblock_calc_crc(
269 struct xfs_buf *bp)
270{
271 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800272 struct xfs_buf_log_item *bip = bp->b_log_item;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500273
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700274 if (!xfs_sb_version_hascrc(&bp->b_mount->m_sb))
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500275 return;
276 if (bip)
277 block->bb_u.l.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_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500279}
280
281bool
282xfs_btree_lblock_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);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700286 struct xfs_mount *mp = bp->b_mount;
Brian Fostera45086e2015-10-12 15:59:25 +1100287
288 if (xfs_sb_version_hascrc(&mp->m_sb)) {
289 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
290 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100291 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_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
297/*
298 * Calculate CRC on the whole btree block and stuff it into the
299 * short-form btree header.
300 *
301 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100302 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500303 * it to disk.
304 */
305void
306xfs_btree_sblock_calc_crc(
307 struct xfs_buf *bp)
308{
309 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800310 struct xfs_buf_log_item *bip = bp->b_log_item;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500311
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700312 if (!xfs_sb_version_hascrc(&bp->b_mount->m_sb))
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500313 return;
314 if (bip)
315 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100316 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500317}
318
319bool
320xfs_btree_sblock_verify_crc(
321 struct xfs_buf *bp)
322{
Brian Fostera45086e2015-10-12 15:59:25 +1100323 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700324 struct xfs_mount *mp = bp->b_mount;
Brian Fostera45086e2015-10-12 15:59:25 +1100325
326 if (xfs_sb_version_hascrc(&mp->m_sb)) {
327 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
Eric Sandeen7d048df2018-11-30 07:55:57 -0800328 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100329 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100330 }
Eric Sandeen51582172014-02-27 15:17:27 +1100331
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500332 return true;
333}
334
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100335static int
336xfs_btree_free_block(
337 struct xfs_btree_cur *cur,
338 struct xfs_buf *bp)
339{
340 int error;
341
342 error = cur->bc_ops->free_block(cur, bp);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100343 if (!error) {
344 xfs_trans_binval(cur->bc_tp, bp);
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100345 XFS_BTREE_STATS_INC(cur, free);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100346 }
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100347 return error;
348}
349
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * Delete the btree cursor.
352 */
353void
354xfs_btree_del_cursor(
355 xfs_btree_cur_t *cur, /* btree cursor */
356 int error) /* del because of error */
357{
358 int i; /* btree level */
359
360 /*
361 * Clear the buffer pointers, and release the buffers.
362 * If we're doing this in the face of an error, we
363 * need to make sure to inspect all of the entries
364 * in the bc_bufs array for buffers to be unlocked.
365 * This is because some of the btree code works from
366 * level n down to 0, and if we get an error along
367 * the way we won't have initialized all the entries
368 * down to 0.
369 */
370 for (i = 0; i < cur->bc_nlevels; i++) {
371 if (cur->bc_bufs[i])
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000372 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 else if (!error)
374 break;
375 }
376 /*
377 * Can't free a bmap cursor without having dealt with the
378 * allocated indirect blocks' accounting.
379 */
380 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
381 cur->bc_private.b.allocated == 0);
382 /*
383 * Free the cursor.
384 */
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800385 kmem_cache_free(xfs_btree_cur_zone, cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388/*
389 * Duplicate the btree cursor.
390 * Allocate a new one, copy the record, re-get the buffers.
391 */
392int /* error */
393xfs_btree_dup_cursor(
394 xfs_btree_cur_t *cur, /* input cursor */
395 xfs_btree_cur_t **ncur) /* output cursor */
396{
397 xfs_buf_t *bp; /* btree block's buffer pointer */
398 int error; /* error return value */
399 int i; /* level number of btree block */
400 xfs_mount_t *mp; /* mount structure for filesystem */
401 xfs_btree_cur_t *new; /* new cursor value */
402 xfs_trans_t *tp; /* transaction pointer, can be NULL */
403
404 tp = cur->bc_tp;
405 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
408 * Allocate a new cursor like the old one.
409 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100410 new = cur->bc_ops->dup_cursor(cur);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * Copy the record currently in the cursor.
414 */
415 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * For each level current, re-get the buffer and copy the ptr value.
419 */
420 for (i = 0; i < new->bc_nlevels; i++) {
421 new->bc_ptrs[i] = cur->bc_ptrs[i];
422 new->bc_ra[i] = cur->bc_ra[i];
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100423 bp = cur->bc_bufs[i];
424 if (bp) {
425 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
426 XFS_BUF_ADDR(bp), mp->m_bsize,
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100427 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100428 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100429 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 xfs_btree_del_cursor(new, error);
431 *ncur = NULL;
432 return error;
433 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500434 }
435 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *ncur = new;
438 return 0;
439}
440
441/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100442 * XFS btree block layout and addressing:
443 *
444 * There are two types of blocks in the btree: leaf and non-leaf blocks.
445 *
446 * The leaf record start with a header then followed by records containing
447 * the values. A non-leaf block also starts with the same header, and
448 * then first contains lookup keys followed by an equal number of pointers
449 * to the btree blocks at the previous level.
450 *
451 * +--------+-------+-------+-------+-------+-------+-------+
452 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
453 * +--------+-------+-------+-------+-------+-------+-------+
454 *
455 * +--------+-------+-------+-------+-------+-------+-------+
456 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
457 * +--------+-------+-------+-------+-------+-------+-------+
458 *
459 * The header is called struct xfs_btree_block for reasons better left unknown
460 * and comes in different versions for short (32bit) and long (64bit) block
461 * pointers. The record and key structures are defined by the btree instances
462 * and opaque to the btree core. The block pointers are simple disk endian
463 * integers, available in a short (32bit) and long (64bit) variant.
464 *
465 * The helpers below calculate the offset of a given record, key or pointer
466 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
467 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
468 * inside the btree block is done using indices starting at one, not zero!
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000469 *
470 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
471 * overlapping intervals. In such a tree, records are still sorted lowest to
472 * highest and indexed by the smallest key value that refers to the record.
473 * However, nodes are different: each pointer has two associated keys -- one
474 * indexing the lowest key available in the block(s) below (the same behavior
475 * as the key in a regular btree) and another indexing the highest key
476 * available in the block(s) below. Because records are /not/ sorted by the
477 * highest key, all leaf block updates require us to compute the highest key
478 * that matches any record in the leaf and to recursively update the high keys
479 * in the nodes going further up in the tree, if necessary. Nodes look like
480 * this:
481 *
482 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
483 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
484 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
485 *
486 * To perform an interval query on an overlapped tree, perform the usual
487 * depth-first search and use the low and high keys to decide if we can skip
488 * that particular node. If a leaf node is reached, return the records that
489 * intersect the interval. Note that an interval query may return numerous
490 * entries. For a non-overlapped tree, simply search for the record associated
491 * with the lowest key and iterate forward until a non-matching record is
492 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
493 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
494 * more detail.
495 *
496 * Why do we care about overlapping intervals? Let's say you have a bunch of
497 * reverse mapping records on a reflink filesystem:
498 *
499 * 1: +- file A startblock B offset C length D -----------+
500 * 2: +- file E startblock F offset G length H --------------+
501 * 3: +- file I startblock F offset J length K --+
502 * 4: +- file L... --+
503 *
504 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
505 * we'd simply increment the length of record 1. But how do we find the record
506 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
507 * record 3 because the keys are ordered first by startblock. An interval
508 * query would return records 1 and 2 because they both overlap (B+D-1), and
509 * from that we can pick out record 1 as the appropriate left neighbor.
510 *
511 * In the non-overlapped case you can do a LE lookup and decrement the cursor
512 * because a record's interval must end before the next record.
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100513 */
514
515/*
516 * Return size of the btree block header for this btree instance.
517 */
518static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
519{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500520 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
521 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
522 return XFS_BTREE_LBLOCK_CRC_LEN;
523 return XFS_BTREE_LBLOCK_LEN;
524 }
525 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
526 return XFS_BTREE_SBLOCK_CRC_LEN;
527 return XFS_BTREE_SBLOCK_LEN;
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100528}
529
530/*
531 * Return size of btree block pointers for this btree instance.
532 */
533static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
534{
535 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
536 sizeof(__be64) : sizeof(__be32);
537}
538
539/*
540 * Calculate offset of the n-th record in a btree block.
541 */
542STATIC size_t
543xfs_btree_rec_offset(
544 struct xfs_btree_cur *cur,
545 int n)
546{
547 return xfs_btree_block_len(cur) +
548 (n - 1) * cur->bc_ops->rec_len;
549}
550
551/*
552 * Calculate offset of the n-th key in a btree block.
553 */
554STATIC size_t
555xfs_btree_key_offset(
556 struct xfs_btree_cur *cur,
557 int n)
558{
559 return xfs_btree_block_len(cur) +
560 (n - 1) * cur->bc_ops->key_len;
561}
562
563/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000564 * Calculate offset of the n-th high key in a btree block.
565 */
566STATIC size_t
567xfs_btree_high_key_offset(
568 struct xfs_btree_cur *cur,
569 int n)
570{
571 return xfs_btree_block_len(cur) +
572 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
573}
574
575/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100576 * Calculate offset of the n-th block pointer in a btree block.
577 */
578STATIC size_t
579xfs_btree_ptr_offset(
580 struct xfs_btree_cur *cur,
581 int n,
582 int level)
583{
584 return xfs_btree_block_len(cur) +
585 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
586 (n - 1) * xfs_btree_ptr_len(cur);
587}
588
589/*
590 * Return a pointer to the n-th record in the btree block.
591 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700592union xfs_btree_rec *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100593xfs_btree_rec_addr(
594 struct xfs_btree_cur *cur,
595 int n,
596 struct xfs_btree_block *block)
597{
598 return (union xfs_btree_rec *)
599 ((char *)block + xfs_btree_rec_offset(cur, n));
600}
601
602/*
603 * Return a pointer to the n-th key in the btree block.
604 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700605union xfs_btree_key *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100606xfs_btree_key_addr(
607 struct xfs_btree_cur *cur,
608 int n,
609 struct xfs_btree_block *block)
610{
611 return (union xfs_btree_key *)
612 ((char *)block + xfs_btree_key_offset(cur, n));
613}
614
615/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000616 * Return a pointer to the n-th high key in the btree block.
617 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700618union xfs_btree_key *
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000619xfs_btree_high_key_addr(
620 struct xfs_btree_cur *cur,
621 int n,
622 struct xfs_btree_block *block)
623{
624 return (union xfs_btree_key *)
625 ((char *)block + xfs_btree_high_key_offset(cur, n));
626}
627
628/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100629 * Return a pointer to the n-th block pointer in the btree block.
630 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700631union xfs_btree_ptr *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100632xfs_btree_ptr_addr(
633 struct xfs_btree_cur *cur,
634 int n,
635 struct xfs_btree_block *block)
636{
637 int level = xfs_btree_get_level(block);
638
639 ASSERT(block->bb_level != 0);
640
641 return (union xfs_btree_ptr *)
642 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
643}
644
645/*
Zhi Yong Wu1cb93862013-08-07 10:11:05 +0000646 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100647 *
648 * For now this btree implementation assumes the btree root is always
649 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100651STATIC struct xfs_btree_block *
652xfs_btree_get_iroot(
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000653 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000655 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000657 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
658 return (struct xfs_btree_block *)ifp->if_broot;
Christoph Hellwig8186e512008-10-30 16:54:22 +1100659}
660
661/*
662 * Retrieve the block pointer from the cursor at the given level.
663 * This may be an inode btree root or from a buffer.
664 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700665struct xfs_btree_block * /* generic btree block pointer */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100666xfs_btree_get_block(
667 struct xfs_btree_cur *cur, /* btree cursor */
668 int level, /* level in btree */
669 struct xfs_buf **bpp) /* buffer containing the block */
670{
671 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
672 (level == cur->bc_nlevels - 1)) {
673 *bpp = NULL;
674 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100676
677 *bpp = cur->bc_bufs[level];
678 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/*
682 * Get a buffer for the block, return it with no data read.
683 * Long-form addressing.
684 */
685xfs_buf_t * /* buffer for fsbno */
686xfs_btree_get_bufl(
687 xfs_mount_t *mp, /* file system mount point */
688 xfs_trans_t *tp, /* transaction pointer */
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700689 xfs_fsblock_t fsbno) /* file system block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfs_daddr_t d; /* real disk block address */
692
693 ASSERT(fsbno != NULLFSBLOCK);
694 d = XFS_FSB_TO_DADDR(mp, fsbno);
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700695 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698/*
699 * Get a buffer for the block, return it with no data read.
700 * Short-form addressing.
701 */
702xfs_buf_t * /* buffer for agno/agbno */
703xfs_btree_get_bufs(
704 xfs_mount_t *mp, /* file system mount point */
705 xfs_trans_t *tp, /* transaction pointer */
706 xfs_agnumber_t agno, /* allocation group number */
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700707 xfs_agblock_t agbno) /* allocation group block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfs_daddr_t d; /* real disk block address */
710
711 ASSERT(agno != NULLAGNUMBER);
712 ASSERT(agbno != NULLAGBLOCK);
713 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700714 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000718 * Change the cursor to point to the first record at the given level.
719 * Other levels are unaffected.
720 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100721STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000722xfs_btree_firstrec(
723 xfs_btree_cur_t *cur, /* btree cursor */
724 int level) /* level to change */
725{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100726 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000727 xfs_buf_t *bp; /* buffer containing block */
728
729 /*
730 * Get the block pointer for this level.
731 */
732 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong1e86eab2017-07-17 14:30:45 -0700733 if (xfs_btree_check_block(cur, block, level, bp))
734 return 0;
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000735 /*
736 * It's empty, there is no such record.
737 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100738 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000739 return 0;
740 /*
741 * Set the ptr value to 1, that's the first record/key.
742 */
743 cur->bc_ptrs[level] = 1;
744 return 1;
745}
746
747/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * Change the cursor to point to the last record in the current block
749 * at the given level. Other levels are unaffected.
750 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100751STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752xfs_btree_lastrec(
753 xfs_btree_cur_t *cur, /* btree cursor */
754 int level) /* level to change */
755{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100756 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 xfs_buf_t *bp; /* buffer containing block */
758
759 /*
760 * Get the block pointer for this level.
761 */
762 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong1e86eab2017-07-17 14:30:45 -0700763 if (xfs_btree_check_block(cur, block, level, bp))
764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * It's empty, there is no such record.
767 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100768 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 0;
770 /*
771 * Set the ptr value to numrecs, that's the last record/key.
772 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100773 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return 1;
775}
776
777/*
778 * Compute first and last byte offsets for the fields given.
779 * Interprets the offsets table, which contains struct field offsets.
780 */
781void
782xfs_btree_offsets(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700783 int64_t fields, /* bitmask of fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 const short *offsets, /* table of field offsets */
785 int nbits, /* number of bits to inspect */
786 int *first, /* output: first byte offset */
787 int *last) /* output: last byte offset */
788{
789 int i; /* current bit number */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700790 int64_t imask; /* mask for current bit number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 ASSERT(fields != 0);
793 /*
794 * Find the lowest bit, so the first byte offset.
795 */
796 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
797 if (imask & fields) {
798 *first = offsets[i];
799 break;
800 }
801 }
802 /*
803 * Find the highest bit, so the last byte offset.
804 */
805 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
806 if (imask & fields) {
807 *last = offsets[i + 1] - 1;
808 break;
809 }
810 }
811}
812
813/*
814 * Get a buffer for the block, return it read in.
815 * Long-form addressing.
816 */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100817int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100819 struct xfs_mount *mp, /* file system mount point */
820 struct xfs_trans *tp, /* transaction pointer */
821 xfs_fsblock_t fsbno, /* file system block number */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100822 struct xfs_buf **bpp, /* buffer for fsbno */
823 int refval, /* ref count value for buffer */
Dave Chinner1813dd62012-11-14 17:54:40 +1100824 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100826 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100828 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800830 if (!xfs_verify_fsbno(mp, fsbno))
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -0800831 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100833 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Eric Sandeenf5b999c2019-06-12 09:00:00 -0700834 mp->m_bsize, 0, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100835 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return error;
Dave Chinner821eb212010-12-02 16:31:13 +1100837 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000838 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 *bpp = bp;
840 return 0;
841}
842
843/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * Read-ahead the block, don't wait for it, don't return a buffer.
845 * Long-form addressing.
846 */
847/* ARGSUSED */
848void
849xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100850 struct xfs_mount *mp, /* file system mount point */
851 xfs_fsblock_t fsbno, /* file system block number */
852 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100853 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 xfs_daddr_t d;
856
857 ASSERT(fsbno != NULLFSBLOCK);
858 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100859 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/*
863 * Read-ahead the block, don't wait for it, don't return a buffer.
864 * Short-form addressing.
865 */
866/* ARGSUSED */
867void
868xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100869 struct xfs_mount *mp, /* file system mount point */
870 xfs_agnumber_t agno, /* allocation group number */
871 xfs_agblock_t agbno, /* allocation group block number */
872 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100873 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 xfs_daddr_t d;
876
877 ASSERT(agno != NULLAGNUMBER);
878 ASSERT(agbno != NULLAGBLOCK);
879 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100880 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100883STATIC int
884xfs_btree_readahead_lblock(
885 struct xfs_btree_cur *cur,
886 int lr,
887 struct xfs_btree_block *block)
888{
889 int rval = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000890 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
891 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100892
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000893 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100894 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100895 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100896 rval++;
897 }
898
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000899 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100900 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100901 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100902 rval++;
903 }
904
905 return rval;
906}
907
908STATIC int
909xfs_btree_readahead_sblock(
910 struct xfs_btree_cur *cur,
911 int lr,
912 struct xfs_btree_block *block)
913{
914 int rval = 0;
915 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
916 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
917
918
919 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
920 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100921 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100922 rval++;
923 }
924
925 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
926 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100927 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100928 rval++;
929 }
930
931 return rval;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934/*
935 * Read-ahead btree blocks, at the given level.
936 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
937 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100938STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100939xfs_btree_readahead(
940 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int lev, /* level in btree */
942 int lr) /* left/right bits */
943{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100944 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100946 /*
947 * No readahead needed if we are at the root level and the
948 * btree root is stored in the inode.
949 */
950 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
951 (lev == cur->bc_nlevels - 1))
952 return 0;
953
954 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
955 return 0;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100958 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
959
960 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
961 return xfs_btree_readahead_lblock(cur, lr, block);
962 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700965STATIC int
Dave Chinner21b5c972013-08-30 10:23:44 +1000966xfs_btree_ptr_to_daddr(
967 struct xfs_btree_cur *cur,
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700968 union xfs_btree_ptr *ptr,
969 xfs_daddr_t *daddr)
Dave Chinner21b5c972013-08-30 10:23:44 +1000970{
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700971 xfs_fsblock_t fsbno;
972 xfs_agblock_t agbno;
973 int error;
974
975 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
976 if (error)
977 return error;
978
Dave Chinner21b5c972013-08-30 10:23:44 +1000979 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700980 fsbno = be64_to_cpu(ptr->l);
981 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, fsbno);
Dave Chinner21b5c972013-08-30 10:23:44 +1000982 } else {
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700983 agbno = be32_to_cpu(ptr->s);
984 *daddr = XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
985 agbno);
Dave Chinner21b5c972013-08-30 10:23:44 +1000986 }
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700987
988 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +1000989}
990
991/*
992 * Readahead @count btree blocks at the given @ptr location.
993 *
994 * We don't need to care about long or short form btrees here as we have a
995 * method of converting the ptr directly to a daddr available to us.
996 */
997STATIC void
998xfs_btree_readahead_ptr(
999 struct xfs_btree_cur *cur,
1000 union xfs_btree_ptr *ptr,
1001 xfs_extlen_t count)
1002{
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001003 xfs_daddr_t daddr;
1004
1005 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
1006 return;
1007 xfs_buf_readahead(cur->bc_mp->m_ddev_targp, daddr,
Dave Chinner21b5c972013-08-30 10:23:44 +10001008 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
1012 * Set the buffer for level "lev" in the cursor to bp, releasing
1013 * any previous buffer.
1014 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001015STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016xfs_btree_setbuf(
1017 xfs_btree_cur_t *cur, /* btree cursor */
1018 int lev, /* level in btree */
1019 xfs_buf_t *bp) /* new buffer to set */
1020{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001021 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001023 if (cur->bc_bufs[lev])
1024 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 cur->bc_bufs[lev] = bp;
1026 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +11001029 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001030 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001032 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1034 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001035 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001037 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1039 }
1040}
Christoph Hellwig637aa502008-10-30 16:55:45 +11001041
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07001042bool
Christoph Hellwig637aa502008-10-30 16:55:45 +11001043xfs_btree_ptr_is_null(
1044 struct xfs_btree_cur *cur,
1045 union xfs_btree_ptr *ptr)
1046{
1047 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001048 return ptr->l == cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001049 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001050 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001051}
1052
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001053STATIC void
1054xfs_btree_set_ptr_null(
1055 struct xfs_btree_cur *cur,
1056 union xfs_btree_ptr *ptr)
1057{
1058 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001059 ptr->l = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001060 else
1061 ptr->s = cpu_to_be32(NULLAGBLOCK);
1062}
1063
Christoph Hellwig637aa502008-10-30 16:55:45 +11001064/*
1065 * Get/set/init sibling pointers
1066 */
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07001067void
Christoph Hellwig637aa502008-10-30 16:55:45 +11001068xfs_btree_get_sibling(
1069 struct xfs_btree_cur *cur,
1070 struct xfs_btree_block *block,
1071 union xfs_btree_ptr *ptr,
1072 int lr)
1073{
1074 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1075
1076 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1077 if (lr == XFS_BB_RIGHTSIB)
1078 ptr->l = block->bb_u.l.bb_rightsib;
1079 else
1080 ptr->l = block->bb_u.l.bb_leftsib;
1081 } else {
1082 if (lr == XFS_BB_RIGHTSIB)
1083 ptr->s = block->bb_u.s.bb_rightsib;
1084 else
1085 ptr->s = block->bb_u.s.bb_leftsib;
1086 }
1087}
1088
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001089STATIC void
1090xfs_btree_set_sibling(
1091 struct xfs_btree_cur *cur,
1092 struct xfs_btree_block *block,
1093 union xfs_btree_ptr *ptr,
1094 int lr)
1095{
1096 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1097
1098 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1099 if (lr == XFS_BB_RIGHTSIB)
1100 block->bb_u.l.bb_rightsib = ptr->l;
1101 else
1102 block->bb_u.l.bb_leftsib = ptr->l;
1103 } else {
1104 if (lr == XFS_BB_RIGHTSIB)
1105 block->bb_u.s.bb_rightsib = ptr->s;
1106 else
1107 block->bb_u.s.bb_leftsib = ptr->s;
1108 }
1109}
1110
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001111void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001112xfs_btree_init_block_int(
1113 struct xfs_mount *mp,
1114 struct xfs_btree_block *buf,
1115 xfs_daddr_t blkno,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001116 xfs_btnum_t btnum,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001117 __u16 level,
1118 __u16 numrecs,
1119 __u64 owner,
1120 unsigned int flags)
1121{
Eric Sandeenf88ae462017-01-27 23:16:37 -08001122 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001123 __u32 magic = xfs_btree_magic(crc, btnum);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001124
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001125 buf->bb_magic = cpu_to_be32(magic);
1126 buf->bb_level = cpu_to_be16(level);
1127 buf->bb_numrecs = cpu_to_be16(numrecs);
1128
1129 if (flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001130 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1131 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001132 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001133 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1134 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001135 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001136 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001137 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001138 }
1139 } else {
1140 /* owner is a 32 bit value on short blocks */
1141 __u32 __owner = (__u32)owner;
1142
1143 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1144 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001145 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001146 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1147 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001148 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001149 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001150 }
1151 }
1152}
1153
1154void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001155xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001156 struct xfs_mount *mp,
1157 struct xfs_buf *bp,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001158 xfs_btnum_t btnum,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001159 __u16 level,
1160 __u16 numrecs,
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001161 __u64 owner)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001162{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001163 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001164 btnum, level, numrecs, owner, 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001165}
1166
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001167STATIC void
1168xfs_btree_init_block_cur(
1169 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001170 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001171 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001172 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001173{
Eric Sandeenaf7d20f2017-01-27 23:16:38 -08001174 __u64 owner;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001175
1176 /*
1177 * we can pull the owner from the cursor right now as the different
1178 * owners align directly with the pointer size of the btree. This may
1179 * change in future, but is safe for current users of the generic btree
1180 * code.
1181 */
1182 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1183 owner = cur->bc_private.b.ip->i_ino;
1184 else
1185 owner = cur->bc_private.a.agno;
1186
1187 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001188 cur->bc_btnum, level, numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001189 owner, cur->bc_flags);
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001190}
1191
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001192/*
1193 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001194 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001195 * will be further refined in the update_lastrec method.
1196 */
1197STATIC int
1198xfs_btree_is_lastrec(
1199 struct xfs_btree_cur *cur,
1200 struct xfs_btree_block *block,
1201 int level)
1202{
1203 union xfs_btree_ptr ptr;
1204
1205 if (level > 0)
1206 return 0;
1207 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1208 return 0;
1209
1210 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1211 if (!xfs_btree_ptr_is_null(cur, &ptr))
1212 return 0;
1213 return 1;
1214}
1215
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001216STATIC void
1217xfs_btree_buf_to_ptr(
1218 struct xfs_btree_cur *cur,
1219 struct xfs_buf *bp,
1220 union xfs_btree_ptr *ptr)
1221{
1222 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1223 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1224 XFS_BUF_ADDR(bp)));
1225 else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001226 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001227 XFS_BUF_ADDR(bp)));
1228 }
1229}
1230
Christoph Hellwig637aa502008-10-30 16:55:45 +11001231STATIC void
1232xfs_btree_set_refs(
1233 struct xfs_btree_cur *cur,
1234 struct xfs_buf *bp)
1235{
1236 switch (cur->bc_btnum) {
1237 case XFS_BTNUM_BNO:
1238 case XFS_BTNUM_CNT:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001239 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001240 break;
1241 case XFS_BTNUM_INO:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001242 case XFS_BTNUM_FINO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001243 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001244 break;
1245 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001246 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001247 break;
Darrick J. Wong035e00a2016-08-03 11:36:07 +10001248 case XFS_BTNUM_RMAP:
1249 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1250 break;
Darrick J. Wong1946b912016-10-03 09:11:18 -07001251 case XFS_BTNUM_REFC:
1252 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1253 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001254 default:
1255 ASSERT(0);
1256 }
1257}
1258
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001259STATIC int
1260xfs_btree_get_buf_block(
1261 struct xfs_btree_cur *cur,
1262 union xfs_btree_ptr *ptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001263 struct xfs_btree_block **block,
1264 struct xfs_buf **bpp)
1265{
1266 struct xfs_mount *mp = cur->bc_mp;
1267 xfs_daddr_t d;
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001268 int error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001269
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001270 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1271 if (error)
1272 return error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001273 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
Eric Sandeenf5b999c2019-06-12 09:00:00 -07001274 mp->m_bsize, 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001275
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001276 if (!*bpp)
Dave Chinner24513372014-06-25 14:58:08 +10001277 return -ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001278
Dave Chinner1813dd62012-11-14 17:54:40 +11001279 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001280 *block = XFS_BUF_TO_BLOCK(*bpp);
1281 return 0;
1282}
1283
Christoph Hellwig637aa502008-10-30 16:55:45 +11001284/*
1285 * Read in the buffer at the given ptr and return the buffer and
1286 * the block pointer within the buffer.
1287 */
1288STATIC int
1289xfs_btree_read_buf_block(
1290 struct xfs_btree_cur *cur,
1291 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001292 int flags,
1293 struct xfs_btree_block **block,
1294 struct xfs_buf **bpp)
1295{
1296 struct xfs_mount *mp = cur->bc_mp;
1297 xfs_daddr_t d;
1298 int error;
1299
1300 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001301 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001302
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001303 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1304 if (error)
1305 return error;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001306 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001307 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001308 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001309 if (error)
1310 return error;
1311
Christoph Hellwig637aa502008-10-30 16:55:45 +11001312 xfs_btree_set_refs(cur, *bpp);
1313 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001314 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001315}
1316
1317/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001318 * Copy keys from one btree block to another.
1319 */
1320STATIC void
1321xfs_btree_copy_keys(
1322 struct xfs_btree_cur *cur,
1323 union xfs_btree_key *dst_key,
1324 union xfs_btree_key *src_key,
1325 int numkeys)
1326{
1327 ASSERT(numkeys >= 0);
1328 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1329}
1330
1331/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001332 * Copy records from one btree block to another.
1333 */
1334STATIC void
1335xfs_btree_copy_recs(
1336 struct xfs_btree_cur *cur,
1337 union xfs_btree_rec *dst_rec,
1338 union xfs_btree_rec *src_rec,
1339 int numrecs)
1340{
1341 ASSERT(numrecs >= 0);
1342 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1343}
1344
1345/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001346 * Copy block pointers from one btree block to another.
1347 */
1348STATIC void
1349xfs_btree_copy_ptrs(
1350 struct xfs_btree_cur *cur,
1351 union xfs_btree_ptr *dst_ptr,
1352 union xfs_btree_ptr *src_ptr,
1353 int numptrs)
1354{
1355 ASSERT(numptrs >= 0);
1356 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1357}
1358
1359/*
1360 * Shift keys one index left/right inside a single btree block.
1361 */
1362STATIC void
1363xfs_btree_shift_keys(
1364 struct xfs_btree_cur *cur,
1365 union xfs_btree_key *key,
1366 int dir,
1367 int numkeys)
1368{
1369 char *dst_key;
1370
1371 ASSERT(numkeys >= 0);
1372 ASSERT(dir == 1 || dir == -1);
1373
1374 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1375 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1376}
1377
1378/*
1379 * Shift records one index left/right inside a single btree block.
1380 */
1381STATIC void
1382xfs_btree_shift_recs(
1383 struct xfs_btree_cur *cur,
1384 union xfs_btree_rec *rec,
1385 int dir,
1386 int numrecs)
1387{
1388 char *dst_rec;
1389
1390 ASSERT(numrecs >= 0);
1391 ASSERT(dir == 1 || dir == -1);
1392
1393 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1394 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1395}
1396
1397/*
1398 * Shift block pointers one index left/right inside a single btree block.
1399 */
1400STATIC void
1401xfs_btree_shift_ptrs(
1402 struct xfs_btree_cur *cur,
1403 union xfs_btree_ptr *ptr,
1404 int dir,
1405 int numptrs)
1406{
1407 char *dst_ptr;
1408
1409 ASSERT(numptrs >= 0);
1410 ASSERT(dir == 1 || dir == -1);
1411
1412 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1413 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1414}
1415
1416/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001417 * Log key values from the btree block.
1418 */
1419STATIC void
1420xfs_btree_log_keys(
1421 struct xfs_btree_cur *cur,
1422 struct xfs_buf *bp,
1423 int first,
1424 int last)
1425{
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001426
1427 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001428 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001429 xfs_trans_log_buf(cur->bc_tp, bp,
1430 xfs_btree_key_offset(cur, first),
1431 xfs_btree_key_offset(cur, last + 1) - 1);
1432 } else {
1433 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1434 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1435 }
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001436}
1437
1438/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001439 * Log record values from the btree block.
1440 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001441void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001442xfs_btree_log_recs(
1443 struct xfs_btree_cur *cur,
1444 struct xfs_buf *bp,
1445 int first,
1446 int last)
1447{
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001448
Dave Chinner61fe1352013-04-03 16:11:30 +11001449 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001450 xfs_trans_log_buf(cur->bc_tp, bp,
1451 xfs_btree_rec_offset(cur, first),
1452 xfs_btree_rec_offset(cur, last + 1) - 1);
1453
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001454}
1455
1456/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001457 * Log block pointer fields from a btree block (nonleaf).
1458 */
1459STATIC void
1460xfs_btree_log_ptrs(
1461 struct xfs_btree_cur *cur, /* btree cursor */
1462 struct xfs_buf *bp, /* buffer containing btree block */
1463 int first, /* index of first pointer to log */
1464 int last) /* index of last pointer to log */
1465{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001466
1467 if (bp) {
1468 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1469 int level = xfs_btree_get_level(block);
1470
Dave Chinner61fe1352013-04-03 16:11:30 +11001471 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001472 xfs_trans_log_buf(cur->bc_tp, bp,
1473 xfs_btree_ptr_offset(cur, first, level),
1474 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1475 } else {
1476 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1477 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1478 }
1479
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001480}
1481
1482/*
1483 * Log fields from a btree block header.
1484 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001485void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001486xfs_btree_log_block(
1487 struct xfs_btree_cur *cur, /* btree cursor */
1488 struct xfs_buf *bp, /* buffer containing btree block */
1489 int fields) /* mask of fields: XFS_BB_... */
1490{
1491 int first; /* first byte offset logged */
1492 int last; /* last byte offset logged */
1493 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001494 offsetof(struct xfs_btree_block, bb_magic),
1495 offsetof(struct xfs_btree_block, bb_level),
1496 offsetof(struct xfs_btree_block, bb_numrecs),
1497 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1498 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001499 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1500 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1501 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1502 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1503 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1504 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001505 };
1506 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001507 offsetof(struct xfs_btree_block, bb_magic),
1508 offsetof(struct xfs_btree_block, bb_level),
1509 offsetof(struct xfs_btree_block, bb_numrecs),
1510 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1511 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001512 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1513 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1514 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1515 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1516 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1517 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1518 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001519 };
1520
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001521 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001522 int nbits;
1523
1524 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1525 /*
1526 * We don't log the CRC when updating a btree
1527 * block but instead recreate it during log
1528 * recovery. As the log buffers have checksums
1529 * of their own this is safe and avoids logging a crc
1530 * update in a lot of places.
1531 */
1532 if (fields == XFS_BB_ALL_BITS)
1533 fields = XFS_BB_ALL_BITS_CRC;
1534 nbits = XFS_BB_NUM_BITS_CRC;
1535 } else {
1536 nbits = XFS_BB_NUM_BITS;
1537 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001538 xfs_btree_offsets(fields,
1539 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1540 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001541 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001542 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001543 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1544 } else {
1545 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1546 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1547 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001548}
1549
1550/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001551 * Increment cursor by one record at the level.
1552 * For nonzero levels the leaf-ward information is untouched.
1553 */
1554int /* error */
1555xfs_btree_increment(
1556 struct xfs_btree_cur *cur,
1557 int level,
1558 int *stat) /* success/failure */
1559{
1560 struct xfs_btree_block *block;
1561 union xfs_btree_ptr ptr;
1562 struct xfs_buf *bp;
1563 int error; /* error return value */
1564 int lev;
1565
Christoph Hellwig637aa502008-10-30 16:55:45 +11001566 ASSERT(level < cur->bc_nlevels);
1567
1568 /* Read-ahead to the right at this level. */
1569 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1570
1571 /* Get a pointer to the btree block. */
1572 block = xfs_btree_get_block(cur, level, &bp);
1573
1574#ifdef DEBUG
1575 error = xfs_btree_check_block(cur, block, level, bp);
1576 if (error)
1577 goto error0;
1578#endif
1579
1580 /* We're done if we remain in the block after the increment. */
1581 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1582 goto out1;
1583
1584 /* Fail if we just went off the right edge of the tree. */
1585 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1586 if (xfs_btree_ptr_is_null(cur, &ptr))
1587 goto out0;
1588
1589 XFS_BTREE_STATS_INC(cur, increment);
1590
1591 /*
1592 * March up the tree incrementing pointers.
1593 * Stop when we don't go off the right edge of a block.
1594 */
1595 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1596 block = xfs_btree_get_block(cur, lev, &bp);
1597
1598#ifdef DEBUG
1599 error = xfs_btree_check_block(cur, block, lev, bp);
1600 if (error)
1601 goto error0;
1602#endif
1603
1604 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1605 break;
1606
1607 /* Read-ahead the right block for the next loop. */
1608 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1609 }
1610
1611 /*
1612 * If we went off the root then we are either seriously
1613 * confused or have the tree root in an inode.
1614 */
1615 if (lev == cur->bc_nlevels) {
1616 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1617 goto out0;
1618 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001619 error = -EFSCORRUPTED;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001620 goto error0;
1621 }
1622 ASSERT(lev < cur->bc_nlevels);
1623
1624 /*
1625 * Now walk back down the tree, fixing up the cursor's buffer
1626 * pointers and key numbers.
1627 */
1628 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1629 union xfs_btree_ptr *ptrp;
1630
1631 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001632 --lev;
1633 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001634 if (error)
1635 goto error0;
1636
1637 xfs_btree_setbuf(cur, lev, bp);
1638 cur->bc_ptrs[lev] = 1;
1639 }
1640out1:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001641 *stat = 1;
1642 return 0;
1643
1644out0:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001645 *stat = 0;
1646 return 0;
1647
1648error0:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001649 return error;
1650}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001651
1652/*
1653 * Decrement cursor by one record at the level.
1654 * For nonzero levels the leaf-ward information is untouched.
1655 */
1656int /* error */
1657xfs_btree_decrement(
1658 struct xfs_btree_cur *cur,
1659 int level,
1660 int *stat) /* success/failure */
1661{
1662 struct xfs_btree_block *block;
1663 xfs_buf_t *bp;
1664 int error; /* error return value */
1665 int lev;
1666 union xfs_btree_ptr ptr;
1667
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001668 ASSERT(level < cur->bc_nlevels);
1669
1670 /* Read-ahead to the left at this level. */
1671 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1672
1673 /* We're done if we remain in the block after the decrement. */
1674 if (--cur->bc_ptrs[level] > 0)
1675 goto out1;
1676
1677 /* Get a pointer to the btree block. */
1678 block = xfs_btree_get_block(cur, level, &bp);
1679
1680#ifdef DEBUG
1681 error = xfs_btree_check_block(cur, block, level, bp);
1682 if (error)
1683 goto error0;
1684#endif
1685
1686 /* Fail if we just went off the left edge of the tree. */
1687 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1688 if (xfs_btree_ptr_is_null(cur, &ptr))
1689 goto out0;
1690
1691 XFS_BTREE_STATS_INC(cur, decrement);
1692
1693 /*
1694 * March up the tree decrementing pointers.
1695 * Stop when we don't go off the left edge of a block.
1696 */
1697 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1698 if (--cur->bc_ptrs[lev] > 0)
1699 break;
1700 /* Read-ahead the left block for the next loop. */
1701 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1702 }
1703
1704 /*
1705 * If we went off the root then we are seriously confused.
1706 * or the root of the tree is in an inode.
1707 */
1708 if (lev == cur->bc_nlevels) {
1709 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1710 goto out0;
1711 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001712 error = -EFSCORRUPTED;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001713 goto error0;
1714 }
1715 ASSERT(lev < cur->bc_nlevels);
1716
1717 /*
1718 * Now walk back down the tree, fixing up the cursor's buffer
1719 * pointers and key numbers.
1720 */
1721 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1722 union xfs_btree_ptr *ptrp;
1723
1724 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001725 --lev;
1726 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001727 if (error)
1728 goto error0;
1729 xfs_btree_setbuf(cur, lev, bp);
1730 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1731 }
1732out1:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001733 *stat = 1;
1734 return 0;
1735
1736out0:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001737 *stat = 0;
1738 return 0;
1739
1740error0:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001741 return error;
1742}
1743
Darrick J. Wong26788092017-06-16 11:00:07 -07001744int
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001745xfs_btree_lookup_get_block(
1746 struct xfs_btree_cur *cur, /* btree cursor */
1747 int level, /* level in the btree */
1748 union xfs_btree_ptr *pp, /* ptr to btree block */
1749 struct xfs_btree_block **blkp) /* return btree block */
1750{
1751 struct xfs_buf *bp; /* buffer pointer for btree block */
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001752 xfs_daddr_t daddr;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001753 int error = 0;
1754
1755 /* special case the root block if in an inode */
1756 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1757 (level == cur->bc_nlevels - 1)) {
1758 *blkp = xfs_btree_get_iroot(cur);
1759 return 0;
1760 }
1761
1762 /*
1763 * If the old buffer at this level for the disk address we are
1764 * looking for re-use it.
1765 *
1766 * Otherwise throw it away and get a new one.
1767 */
1768 bp = cur->bc_bufs[level];
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001769 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1770 if (error)
1771 return error;
1772 if (bp && XFS_BUF_ADDR(bp) == daddr) {
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001773 *blkp = XFS_BUF_TO_BLOCK(bp);
1774 return 0;
1775 }
1776
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001777 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001778 if (error)
1779 return error;
1780
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001781 /* Check the inode owner since the verifiers don't. */
1782 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
Brian Foster99c794c2017-08-29 10:08:39 -07001783 !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) &&
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001784 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1785 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1786 cur->bc_private.b.ip->i_ino)
1787 goto out_bad;
1788
1789 /* Did we get the level we were looking for? */
1790 if (be16_to_cpu((*blkp)->bb_level) != level)
1791 goto out_bad;
1792
1793 /* Check that internal nodes have at least one record. */
1794 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1795 goto out_bad;
1796
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001797 xfs_btree_setbuf(cur, level, bp);
1798 return 0;
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001799
1800out_bad:
1801 *blkp = NULL;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001802 xfs_buf_corruption_error(bp);
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001803 xfs_trans_brelse(cur->bc_tp, bp);
1804 return -EFSCORRUPTED;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001805}
1806
1807/*
1808 * Get current search key. For level 0 we don't actually have a key
1809 * structure so we make one up from the record. For all other levels
1810 * we just return the right key.
1811 */
1812STATIC union xfs_btree_key *
1813xfs_lookup_get_search_key(
1814 struct xfs_btree_cur *cur,
1815 int level,
1816 int keyno,
1817 struct xfs_btree_block *block,
1818 union xfs_btree_key *kp)
1819{
1820 if (level == 0) {
1821 cur->bc_ops->init_key_from_rec(kp,
1822 xfs_btree_rec_addr(cur, keyno, block));
1823 return kp;
1824 }
1825
1826 return xfs_btree_key_addr(cur, keyno, block);
1827}
1828
1829/*
1830 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001831 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001832 */
1833int /* error */
1834xfs_btree_lookup(
1835 struct xfs_btree_cur *cur, /* btree cursor */
1836 xfs_lookup_t dir, /* <=, ==, or >= */
1837 int *stat) /* success/failure */
1838{
1839 struct xfs_btree_block *block; /* current btree block */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001840 int64_t diff; /* difference for the current key */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001841 int error; /* error return value */
1842 int keyno; /* current key number */
1843 int level; /* level in the btree */
1844 union xfs_btree_ptr *pp; /* ptr to btree block */
1845 union xfs_btree_ptr ptr; /* ptr to btree block */
1846
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001847 XFS_BTREE_STATS_INC(cur, lookup);
1848
Darrick J. Wonged150e12016-08-26 15:58:40 +10001849 /* No such thing as a zero-level tree. */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001850 if (XFS_IS_CORRUPT(cur->bc_mp, cur->bc_nlevels == 0))
Darrick J. Wonged150e12016-08-26 15:58:40 +10001851 return -EFSCORRUPTED;
1852
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001853 block = NULL;
1854 keyno = 0;
1855
1856 /* initialise start pointer from cursor */
1857 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1858 pp = &ptr;
1859
1860 /*
1861 * Iterate over each level in the btree, starting at the root.
1862 * For each level above the leaves, find the key we need, based
1863 * on the lookup record, then follow the corresponding block
1864 * pointer down to the next level.
1865 */
1866 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1867 /* Get the block we need to do the lookup on. */
1868 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1869 if (error)
1870 goto error0;
1871
1872 if (diff == 0) {
1873 /*
1874 * If we already had a key match at a higher level, we
1875 * know we need to use the first entry in this block.
1876 */
1877 keyno = 1;
1878 } else {
1879 /* Otherwise search this block. Do a binary search. */
1880
1881 int high; /* high entry number */
1882 int low; /* low entry number */
1883
1884 /* Set low and high entry numbers, 1-based. */
1885 low = 1;
1886 high = xfs_btree_get_numrecs(block);
1887 if (!high) {
1888 /* Block is empty, must be an empty leaf. */
Darrick J. Wongeeee0d62018-06-03 16:10:14 -07001889 if (level != 0 || cur->bc_nlevels != 1) {
1890 XFS_CORRUPTION_ERROR(__func__,
1891 XFS_ERRLEVEL_LOW,
Darrick J. Wong2551a532018-06-04 10:23:54 -07001892 cur->bc_mp, block,
1893 sizeof(*block));
Darrick J. Wongeeee0d62018-06-03 16:10:14 -07001894 return -EFSCORRUPTED;
1895 }
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001896
1897 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001898 *stat = 0;
1899 return 0;
1900 }
1901
1902 /* Binary search the block. */
1903 while (low <= high) {
1904 union xfs_btree_key key;
1905 union xfs_btree_key *kp;
1906
1907 XFS_BTREE_STATS_INC(cur, compare);
1908
1909 /* keyno is average of low and high. */
1910 keyno = (low + high) >> 1;
1911
1912 /* Get current search key */
1913 kp = xfs_lookup_get_search_key(cur, level,
1914 keyno, block, &key);
1915
1916 /*
1917 * Compute difference to get next direction:
1918 * - less than, move right
1919 * - greater than, move left
1920 * - equal, we're done
1921 */
1922 diff = cur->bc_ops->key_diff(cur, kp);
1923 if (diff < 0)
1924 low = keyno + 1;
1925 else if (diff > 0)
1926 high = keyno - 1;
1927 else
1928 break;
1929 }
1930 }
1931
1932 /*
1933 * If there are more levels, set up for the next level
1934 * by getting the block number and filling in the cursor.
1935 */
1936 if (level > 0) {
1937 /*
1938 * If we moved left, need the previous key number,
1939 * unless there isn't one.
1940 */
1941 if (diff > 0 && --keyno < 1)
1942 keyno = 1;
1943 pp = xfs_btree_ptr_addr(cur, keyno, block);
1944
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07001945 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001946 if (error)
1947 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07001948
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001949 cur->bc_ptrs[level] = keyno;
1950 }
1951 }
1952
1953 /* Done with the search. See if we need to adjust the results. */
1954 if (dir != XFS_LOOKUP_LE && diff < 0) {
1955 keyno++;
1956 /*
1957 * If ge search and we went off the end of the block, but it's
1958 * not the last block, we're in the wrong block.
1959 */
1960 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1961 if (dir == XFS_LOOKUP_GE &&
1962 keyno > xfs_btree_get_numrecs(block) &&
1963 !xfs_btree_ptr_is_null(cur, &ptr)) {
1964 int i;
1965
1966 cur->bc_ptrs[0] = keyno;
1967 error = xfs_btree_increment(cur, 0, &i);
1968 if (error)
1969 goto error0;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08001970 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1971 return -EFSCORRUPTED;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001972 *stat = 1;
1973 return 0;
1974 }
1975 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1976 keyno--;
1977 cur->bc_ptrs[0] = keyno;
1978
1979 /* Return if we succeeded or not. */
1980 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1981 *stat = 0;
1982 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1983 *stat = 1;
1984 else
1985 *stat = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001986 return 0;
1987
1988error0:
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001989 return error;
1990}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001991
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10001992/* Find the high key storage area from a regular key. */
Darrick J. Wong2fdbec52017-10-25 15:03:46 -07001993union xfs_btree_key *
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10001994xfs_btree_high_key_from_key(
1995 struct xfs_btree_cur *cur,
1996 union xfs_btree_key *key)
1997{
1998 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
1999 return (union xfs_btree_key *)((char *)key +
2000 (cur->bc_ops->key_len / 2));
2001}
2002
Darrick J. Wong973b8312016-08-03 12:22:12 +10002003/* Determine the low (and high if overlapped) keys of a leaf block */
2004STATIC void
2005xfs_btree_get_leaf_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002006 struct xfs_btree_cur *cur,
2007 struct xfs_btree_block *block,
2008 union xfs_btree_key *key)
2009{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002010 union xfs_btree_key max_hkey;
2011 union xfs_btree_key hkey;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002012 union xfs_btree_rec *rec;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002013 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002014 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002015
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002016 rec = xfs_btree_rec_addr(cur, 1, block);
2017 cur->bc_ops->init_key_from_rec(key, rec);
2018
Darrick J. Wong973b8312016-08-03 12:22:12 +10002019 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002020
Darrick J. Wong973b8312016-08-03 12:22:12 +10002021 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2022 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2023 rec = xfs_btree_rec_addr(cur, n, block);
2024 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2025 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2026 > 0)
2027 max_hkey = hkey;
2028 }
2029
2030 high = xfs_btree_high_key_from_key(cur, key);
2031 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2032 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002033}
2034
Darrick J. Wong973b8312016-08-03 12:22:12 +10002035/* Determine the low (and high if overlapped) keys of a node block */
2036STATIC void
2037xfs_btree_get_node_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002038 struct xfs_btree_cur *cur,
2039 struct xfs_btree_block *block,
2040 union xfs_btree_key *key)
2041{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002042 union xfs_btree_key *hkey;
2043 union xfs_btree_key *max_hkey;
2044 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002045 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002046
Darrick J. Wong973b8312016-08-03 12:22:12 +10002047 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2048 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2049 cur->bc_ops->key_len / 2);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002050
Darrick J. Wong973b8312016-08-03 12:22:12 +10002051 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2052 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2053 hkey = xfs_btree_high_key_addr(cur, n, block);
2054 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2055 max_hkey = hkey;
2056 }
2057
2058 high = xfs_btree_high_key_from_key(cur, key);
2059 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2060 } else {
2061 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2062 cur->bc_ops->key_len);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002063 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002064}
2065
Darrick J. Wong70b22652016-08-03 11:03:38 +10002066/* Derive the keys for any btree block. */
Darrick J. Wong2fdbec52017-10-25 15:03:46 -07002067void
Darrick J. Wong70b22652016-08-03 11:03:38 +10002068xfs_btree_get_keys(
2069 struct xfs_btree_cur *cur,
2070 struct xfs_btree_block *block,
2071 union xfs_btree_key *key)
2072{
2073 if (be16_to_cpu(block->bb_level) == 0)
Darrick J. Wong973b8312016-08-03 12:22:12 +10002074 xfs_btree_get_leaf_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002075 else
Darrick J. Wong973b8312016-08-03 12:22:12 +10002076 xfs_btree_get_node_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002077}
2078
2079/*
2080 * Decide if we need to update the parent keys of a btree block. For
2081 * a standard btree this is only necessary if we're updating the first
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002082 * record/key. For an overlapping btree, we must always update the
2083 * keys because the highest key can be in any of the records or keys
2084 * in the block.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002085 */
2086static inline bool
2087xfs_btree_needs_key_update(
2088 struct xfs_btree_cur *cur,
2089 int ptr)
2090{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002091 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2092}
2093
2094/*
2095 * Update the low and high parent keys of the given level, progressing
2096 * towards the root. If force_all is false, stop if the keys for a given
2097 * level do not need updating.
2098 */
2099STATIC int
2100__xfs_btree_updkeys(
2101 struct xfs_btree_cur *cur,
2102 int level,
2103 struct xfs_btree_block *block,
2104 struct xfs_buf *bp0,
2105 bool force_all)
2106{
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002107 union xfs_btree_key key; /* keys from current level */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002108 union xfs_btree_key *lkey; /* keys from the next level up */
2109 union xfs_btree_key *hkey;
2110 union xfs_btree_key *nlkey; /* keys from the next level up */
2111 union xfs_btree_key *nhkey;
2112 struct xfs_buf *bp;
2113 int ptr;
2114
2115 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2116
2117 /* Exit if there aren't any parent levels to update. */
2118 if (level + 1 >= cur->bc_nlevels)
2119 return 0;
2120
2121 trace_xfs_btree_updkeys(cur, level, bp0);
2122
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002123 lkey = &key;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002124 hkey = xfs_btree_high_key_from_key(cur, lkey);
2125 xfs_btree_get_keys(cur, block, lkey);
2126 for (level++; level < cur->bc_nlevels; level++) {
2127#ifdef DEBUG
2128 int error;
2129#endif
2130 block = xfs_btree_get_block(cur, level, &bp);
2131 trace_xfs_btree_updkeys(cur, level, bp);
2132#ifdef DEBUG
2133 error = xfs_btree_check_block(cur, block, level, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002134 if (error)
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002135 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002136#endif
2137 ptr = cur->bc_ptrs[level];
2138 nlkey = xfs_btree_key_addr(cur, ptr, block);
2139 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2140 if (!force_all &&
2141 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2142 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2143 break;
2144 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2145 xfs_btree_log_keys(cur, bp, ptr, ptr);
2146 if (level + 1 >= cur->bc_nlevels)
2147 break;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002148 xfs_btree_get_node_keys(cur, block, lkey);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002149 }
2150
2151 return 0;
2152}
2153
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002154/* Update all the keys from some level in cursor back to the root. */
2155STATIC int
2156xfs_btree_updkeys_force(
2157 struct xfs_btree_cur *cur,
2158 int level)
2159{
2160 struct xfs_buf *bp;
2161 struct xfs_btree_block *block;
2162
2163 block = xfs_btree_get_block(cur, level, &bp);
2164 return __xfs_btree_updkeys(cur, level, block, bp, true);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002165}
2166
2167/*
2168 * Update the parent keys of the given level, progressing towards the root.
2169 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002170STATIC int
Darrick J. Wong70b22652016-08-03 11:03:38 +10002171xfs_btree_update_keys(
2172 struct xfs_btree_cur *cur,
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002173 int level)
2174{
2175 struct xfs_btree_block *block;
2176 struct xfs_buf *bp;
2177 union xfs_btree_key *kp;
Darrick J. Wong70b22652016-08-03 11:03:38 +10002178 union xfs_btree_key key;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002179 int ptr;
2180
Darrick J. Wong973b8312016-08-03 12:22:12 +10002181 ASSERT(level >= 0);
2182
2183 block = xfs_btree_get_block(cur, level, &bp);
2184 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2185 return __xfs_btree_updkeys(cur, level, block, bp, false);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002186
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002187 /*
2188 * Go up the tree from this level toward the root.
2189 * At each level, update the key value to the value input.
2190 * Stop when we reach a level where the cursor isn't pointing
2191 * at the first entry in the block.
2192 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002193 xfs_btree_get_keys(cur, block, &key);
2194 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002195#ifdef DEBUG
2196 int error;
2197#endif
2198 block = xfs_btree_get_block(cur, level, &bp);
2199#ifdef DEBUG
2200 error = xfs_btree_check_block(cur, block, level, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002201 if (error)
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002202 return error;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002203#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
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002210 return 0;
2211}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002212
2213/*
2214 * Update the record referred to by cur to the value in the
2215 * given record. This either works (return 0) or gets an
2216 * EFSCORRUPTED error.
2217 */
2218int
2219xfs_btree_update(
2220 struct xfs_btree_cur *cur,
2221 union xfs_btree_rec *rec)
2222{
2223 struct xfs_btree_block *block;
2224 struct xfs_buf *bp;
2225 int error;
2226 int ptr;
2227 union xfs_btree_rec *rp;
2228
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002229 /* Pick up the current block. */
2230 block = xfs_btree_get_block(cur, 0, &bp);
2231
2232#ifdef DEBUG
2233 error = xfs_btree_check_block(cur, block, 0, bp);
2234 if (error)
2235 goto error0;
2236#endif
2237 /* Get the address of the rec to be updated. */
2238 ptr = cur->bc_ptrs[0];
2239 rp = xfs_btree_rec_addr(cur, ptr, block);
2240
2241 /* Fill in the new contents and log them. */
2242 xfs_btree_copy_recs(cur, rp, rec, 1);
2243 xfs_btree_log_recs(cur, bp, ptr, ptr);
2244
2245 /*
2246 * If we are tracking the last record in the tree and
2247 * we are at the far right edge of the tree, update it.
2248 */
2249 if (xfs_btree_is_lastrec(cur, block, 0)) {
2250 cur->bc_ops->update_lastrec(cur, block, rec,
2251 ptr, LASTREC_UPDATE);
2252 }
2253
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002254 /* Pass new key value up to our parent. */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002255 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002256 error = xfs_btree_update_keys(cur, 0);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002257 if (error)
2258 goto error0;
2259 }
2260
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002261 return 0;
2262
2263error0:
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002264 return error;
2265}
2266
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002267/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11002268 * Move 1 record left from cur/level if possible.
2269 * Update cur to reflect the new path.
2270 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002271STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002272xfs_btree_lshift(
2273 struct xfs_btree_cur *cur,
2274 int level,
2275 int *stat) /* success/failure */
2276{
Christoph Hellwig687b8902008-10-30 16:56:53 +11002277 struct xfs_buf *lbp; /* left buffer pointer */
2278 struct xfs_btree_block *left; /* left btree block */
2279 int lrecs; /* left record count */
2280 struct xfs_buf *rbp; /* right buffer pointer */
2281 struct xfs_btree_block *right; /* right btree block */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002282 struct xfs_btree_cur *tcur; /* temporary btree cursor */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002283 int rrecs; /* right record count */
2284 union xfs_btree_ptr lptr; /* left btree pointer */
2285 union xfs_btree_key *rkp = NULL; /* right btree key */
2286 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2287 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2288 int error; /* error return value */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002289 int i;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002290
Christoph Hellwig687b8902008-10-30 16:56:53 +11002291 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2292 level == cur->bc_nlevels - 1)
2293 goto out0;
2294
2295 /* Set up variables for this block as "right". */
2296 right = xfs_btree_get_block(cur, level, &rbp);
2297
2298#ifdef DEBUG
2299 error = xfs_btree_check_block(cur, right, level, rbp);
2300 if (error)
2301 goto error0;
2302#endif
2303
2304 /* If we've got no left sibling then we can't shift an entry left. */
2305 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2306 if (xfs_btree_ptr_is_null(cur, &lptr))
2307 goto out0;
2308
2309 /*
2310 * If the cursor entry is the one that would be moved, don't
2311 * do it... it's too complicated.
2312 */
2313 if (cur->bc_ptrs[level] <= 1)
2314 goto out0;
2315
2316 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002317 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002318 if (error)
2319 goto error0;
2320
2321 /* If it's full, it can't take another entry. */
2322 lrecs = xfs_btree_get_numrecs(left);
2323 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2324 goto out0;
2325
2326 rrecs = xfs_btree_get_numrecs(right);
2327
2328 /*
2329 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002330 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002331 * later.
2332 */
2333 lrecs++;
2334 rrecs--;
2335
2336 XFS_BTREE_STATS_INC(cur, lshift);
2337 XFS_BTREE_STATS_ADD(cur, moves, 1);
2338
2339 /*
2340 * If non-leaf, copy a key and a ptr to the left block.
2341 * Log the changes to the left block.
2342 */
2343 if (level > 0) {
2344 /* It's a non-leaf. Move keys and pointers. */
2345 union xfs_btree_key *lkp; /* left btree key */
2346 union xfs_btree_ptr *lpp; /* left address pointer */
2347
2348 lkp = xfs_btree_key_addr(cur, lrecs, left);
2349 rkp = xfs_btree_key_addr(cur, 1, right);
2350
2351 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2352 rpp = xfs_btree_ptr_addr(cur, 1, right);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002353
2354 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002355 if (error)
2356 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002357
Christoph Hellwig687b8902008-10-30 16:56:53 +11002358 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2359 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2360
2361 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2362 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2363
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002364 ASSERT(cur->bc_ops->keys_inorder(cur,
2365 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002366 } else {
2367 /* It's a leaf. Move records. */
2368 union xfs_btree_rec *lrp; /* left record pointer */
2369
2370 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2371 rrp = xfs_btree_rec_addr(cur, 1, right);
2372
2373 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2374 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2375
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002376 ASSERT(cur->bc_ops->recs_inorder(cur,
2377 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002378 }
2379
2380 xfs_btree_set_numrecs(left, lrecs);
2381 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2382
2383 xfs_btree_set_numrecs(right, rrecs);
2384 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2385
2386 /*
2387 * Slide the contents of right down one entry.
2388 */
2389 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2390 if (level > 0) {
2391 /* It's a nonleaf. operate on keys and ptrs */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002392 int i; /* loop index */
2393
2394 for (i = 0; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002395 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002396 if (error)
2397 goto error0;
2398 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002399
Christoph Hellwig687b8902008-10-30 16:56:53 +11002400 xfs_btree_shift_keys(cur,
2401 xfs_btree_key_addr(cur, 2, right),
2402 -1, rrecs);
2403 xfs_btree_shift_ptrs(cur,
2404 xfs_btree_ptr_addr(cur, 2, right),
2405 -1, rrecs);
2406
2407 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2408 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2409 } else {
2410 /* It's a leaf. operate on records */
2411 xfs_btree_shift_recs(cur,
2412 xfs_btree_rec_addr(cur, 2, right),
2413 -1, rrecs);
2414 xfs_btree_log_recs(cur, rbp, 1, rrecs);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002415 }
2416
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002417 /*
2418 * Using a temporary cursor, update the parent key values of the
2419 * block on the left.
2420 */
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002421 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2422 error = xfs_btree_dup_cursor(cur, &tcur);
2423 if (error)
2424 goto error0;
2425 i = xfs_btree_firstrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002426 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2427 error = -EFSCORRUPTED;
2428 goto error0;
2429 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002430
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002431 error = xfs_btree_decrement(tcur, level, &i);
2432 if (error)
2433 goto error1;
2434
2435 /* Update the parent high keys of the left block, if needed. */
2436 error = xfs_btree_update_keys(tcur, level);
2437 if (error)
2438 goto error1;
2439
2440 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2441 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002442
Darrick J. Wong70b22652016-08-03 11:03:38 +10002443 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002444 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002445 if (error)
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002446 goto error0;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002447
2448 /* Slide the cursor value left one. */
2449 cur->bc_ptrs[level]--;
2450
Christoph Hellwig687b8902008-10-30 16:56:53 +11002451 *stat = 1;
2452 return 0;
2453
2454out0:
Christoph Hellwig687b8902008-10-30 16:56:53 +11002455 *stat = 0;
2456 return 0;
2457
2458error0:
Christoph Hellwig687b8902008-10-30 16:56:53 +11002459 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002460
2461error1:
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002462 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2463 return error;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002464}
2465
2466/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002467 * Move 1 record right from cur/level if possible.
2468 * Update cur to reflect the new path.
2469 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002470STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002471xfs_btree_rshift(
2472 struct xfs_btree_cur *cur,
2473 int level,
2474 int *stat) /* success/failure */
2475{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002476 struct xfs_buf *lbp; /* left buffer pointer */
2477 struct xfs_btree_block *left; /* left btree block */
2478 struct xfs_buf *rbp; /* right buffer pointer */
2479 struct xfs_btree_block *right; /* right btree block */
2480 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2481 union xfs_btree_ptr rptr; /* right block pointer */
2482 union xfs_btree_key *rkp; /* right btree key */
2483 int rrecs; /* right record count */
2484 int lrecs; /* left record count */
2485 int error; /* error return value */
2486 int i; /* loop counter */
2487
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002488 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2489 (level == cur->bc_nlevels - 1))
2490 goto out0;
2491
2492 /* Set up variables for this block as "left". */
2493 left = xfs_btree_get_block(cur, level, &lbp);
2494
2495#ifdef DEBUG
2496 error = xfs_btree_check_block(cur, left, level, lbp);
2497 if (error)
2498 goto error0;
2499#endif
2500
2501 /* If we've got no right sibling then we can't shift an entry right. */
2502 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2503 if (xfs_btree_ptr_is_null(cur, &rptr))
2504 goto out0;
2505
2506 /*
2507 * If the cursor entry is the one that would be moved, don't
2508 * do it... it's too complicated.
2509 */
2510 lrecs = xfs_btree_get_numrecs(left);
2511 if (cur->bc_ptrs[level] >= lrecs)
2512 goto out0;
2513
2514 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002515 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002516 if (error)
2517 goto error0;
2518
2519 /* If it's full, it can't take another entry. */
2520 rrecs = xfs_btree_get_numrecs(right);
2521 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2522 goto out0;
2523
2524 XFS_BTREE_STATS_INC(cur, rshift);
2525 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2526
2527 /*
2528 * Make a hole at the start of the right neighbor block, then
2529 * copy the last left block entry to the hole.
2530 */
2531 if (level > 0) {
2532 /* It's a nonleaf. make a hole in the keys and ptrs */
2533 union xfs_btree_key *lkp;
2534 union xfs_btree_ptr *lpp;
2535 union xfs_btree_ptr *rpp;
2536
2537 lkp = xfs_btree_key_addr(cur, lrecs, left);
2538 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2539 rkp = xfs_btree_key_addr(cur, 1, right);
2540 rpp = xfs_btree_ptr_addr(cur, 1, right);
2541
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002542 for (i = rrecs - 1; i >= 0; i--) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002543 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002544 if (error)
2545 goto error0;
2546 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002547
2548 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2549 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2550
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002551 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002552 if (error)
2553 goto error0;
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002554
2555 /* Now put the new data in, and log it. */
2556 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2557 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2558
2559 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2560 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2561
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002562 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2563 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002564 } else {
2565 /* It's a leaf. make a hole in the records */
2566 union xfs_btree_rec *lrp;
2567 union xfs_btree_rec *rrp;
2568
2569 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2570 rrp = xfs_btree_rec_addr(cur, 1, right);
2571
2572 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2573
2574 /* Now put the new data in, and log it. */
2575 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2576 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002577 }
2578
2579 /*
2580 * Decrement and log left's numrecs, bump and log right's numrecs.
2581 */
2582 xfs_btree_set_numrecs(left, --lrecs);
2583 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2584
2585 xfs_btree_set_numrecs(right, ++rrecs);
2586 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2587
2588 /*
2589 * Using a temporary cursor, update the parent key values of the
2590 * block on the right.
2591 */
2592 error = xfs_btree_dup_cursor(cur, &tcur);
2593 if (error)
2594 goto error0;
2595 i = xfs_btree_lastrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08002596 if (XFS_IS_CORRUPT(tcur->bc_mp, i != 1)) {
2597 error = -EFSCORRUPTED;
2598 goto error0;
2599 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002600
2601 error = xfs_btree_increment(tcur, level, &i);
2602 if (error)
2603 goto error1;
2604
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002605 /* Update the parent high keys of the left block, if needed. */
2606 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002607 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002608 if (error)
2609 goto error1;
2610 }
2611
Darrick J. Wong70b22652016-08-03 11:03:38 +10002612 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002613 error = xfs_btree_update_keys(tcur, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002614 if (error)
2615 goto error1;
2616
2617 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2618
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002619 *stat = 1;
2620 return 0;
2621
2622out0:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002623 *stat = 0;
2624 return 0;
2625
2626error0:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002627 return error;
2628
2629error1:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002630 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2631 return error;
2632}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002633
2634/*
2635 * Split cur/level block in half.
2636 * Return new block number and the key to its first
2637 * record (to be inserted into parent).
2638 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002639STATIC int /* error */
Dave Chinnercf11da92014-07-15 07:08:24 +10002640__xfs_btree_split(
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002641 struct xfs_btree_cur *cur,
2642 int level,
2643 union xfs_btree_ptr *ptrp,
2644 union xfs_btree_key *key,
2645 struct xfs_btree_cur **curp,
2646 int *stat) /* success/failure */
2647{
2648 union xfs_btree_ptr lptr; /* left sibling block ptr */
2649 struct xfs_buf *lbp; /* left buffer pointer */
2650 struct xfs_btree_block *left; /* left btree block */
2651 union xfs_btree_ptr rptr; /* right sibling block ptr */
2652 struct xfs_buf *rbp; /* right buffer pointer */
2653 struct xfs_btree_block *right; /* right btree block */
2654 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2655 struct xfs_buf *rrbp; /* right-right buffer pointer */
2656 struct xfs_btree_block *rrblock; /* right-right btree block */
2657 int lrecs;
2658 int rrecs;
2659 int src_index;
2660 int error; /* error return value */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002661 int i;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002662
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002663 XFS_BTREE_STATS_INC(cur, split);
2664
2665 /* Set up left block (current one). */
2666 left = xfs_btree_get_block(cur, level, &lbp);
2667
2668#ifdef DEBUG
2669 error = xfs_btree_check_block(cur, left, level, lbp);
2670 if (error)
2671 goto error0;
2672#endif
2673
2674 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2675
2676 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002677 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002678 if (error)
2679 goto error0;
2680 if (*stat == 0)
2681 goto out0;
2682 XFS_BTREE_STATS_INC(cur, alloc);
2683
2684 /* Set up the new block as "right". */
Eric Sandeenf5b999c2019-06-12 09:00:00 -07002685 error = xfs_btree_get_buf_block(cur, &rptr, &right, &rbp);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002686 if (error)
2687 goto error0;
2688
2689 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002690 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002691
2692 /*
2693 * Split the entries between the old and the new block evenly.
2694 * Make sure that if there's an odd number of entries now, that
2695 * each new block will have the same number of entries.
2696 */
2697 lrecs = xfs_btree_get_numrecs(left);
2698 rrecs = lrecs / 2;
2699 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2700 rrecs++;
2701 src_index = (lrecs - rrecs + 1);
2702
2703 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2704
Darrick J. Wong70b22652016-08-03 11:03:38 +10002705 /* Adjust numrecs for the later get_*_keys() calls. */
2706 lrecs -= rrecs;
2707 xfs_btree_set_numrecs(left, lrecs);
2708 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2709
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002710 /*
2711 * Copy btree block entries from the left block over to the
2712 * new block, the right. Update the right block and log the
2713 * changes.
2714 */
2715 if (level > 0) {
2716 /* It's a non-leaf. Move keys and pointers. */
2717 union xfs_btree_key *lkp; /* left btree key */
2718 union xfs_btree_ptr *lpp; /* left address pointer */
2719 union xfs_btree_key *rkp; /* right btree key */
2720 union xfs_btree_ptr *rpp; /* right address pointer */
2721
2722 lkp = xfs_btree_key_addr(cur, src_index, left);
2723 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2724 rkp = xfs_btree_key_addr(cur, 1, right);
2725 rpp = xfs_btree_ptr_addr(cur, 1, right);
2726
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002727 for (i = src_index; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002728 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002729 if (error)
2730 goto error0;
2731 }
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002732
Darrick J. Wong70b22652016-08-03 11:03:38 +10002733 /* Copy the keys & pointers to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002734 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2735 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2736
2737 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2738 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2739
Darrick J. Wong70b22652016-08-03 11:03:38 +10002740 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002741 xfs_btree_get_node_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002742 } else {
2743 /* It's a leaf. Move records. */
2744 union xfs_btree_rec *lrp; /* left record pointer */
2745 union xfs_btree_rec *rrp; /* right record pointer */
2746
2747 lrp = xfs_btree_rec_addr(cur, src_index, left);
2748 rrp = xfs_btree_rec_addr(cur, 1, right);
2749
Darrick J. Wong70b22652016-08-03 11:03:38 +10002750 /* Copy records to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002751 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2752 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2753
Darrick J. Wong70b22652016-08-03 11:03:38 +10002754 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002755 xfs_btree_get_leaf_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002756 }
2757
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002758 /*
2759 * Find the left block number by looking in the buffer.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002760 * Adjust sibling pointers.
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002761 */
2762 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2763 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2764 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2765 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2766
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002767 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2768 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2769
2770 /*
2771 * If there's a block to the new block's right, make that block
2772 * point back to right instead of to left.
2773 */
2774 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002775 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002776 0, &rrblock, &rrbp);
2777 if (error)
2778 goto error0;
2779 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2780 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2781 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002782
2783 /* Update the parent high keys of the left block, if needed. */
2784 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002785 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002786 if (error)
2787 goto error0;
2788 }
2789
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002790 /*
2791 * If the cursor is really in the right block, move it there.
2792 * If it's just pointing past the last entry in left, then we'll
2793 * insert there, so don't change anything in that case.
2794 */
2795 if (cur->bc_ptrs[level] > lrecs + 1) {
2796 xfs_btree_setbuf(cur, level, rbp);
2797 cur->bc_ptrs[level] -= lrecs;
2798 }
2799 /*
2800 * If there are more levels, we'll need another cursor which refers
2801 * the right block, no matter where this cursor was.
2802 */
2803 if (level + 1 < cur->bc_nlevels) {
2804 error = xfs_btree_dup_cursor(cur, curp);
2805 if (error)
2806 goto error0;
2807 (*curp)->bc_ptrs[level + 1]++;
2808 }
2809 *ptrp = rptr;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002810 *stat = 1;
2811 return 0;
2812out0:
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002813 *stat = 0;
2814 return 0;
2815
2816error0:
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002817 return error;
2818}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002819
Dave Chinnercf11da92014-07-15 07:08:24 +10002820struct xfs_btree_split_args {
2821 struct xfs_btree_cur *cur;
2822 int level;
2823 union xfs_btree_ptr *ptrp;
2824 union xfs_btree_key *key;
2825 struct xfs_btree_cur **curp;
2826 int *stat; /* success/failure */
2827 int result;
2828 bool kswapd; /* allocation in kswapd context */
2829 struct completion *done;
2830 struct work_struct work;
2831};
2832
2833/*
2834 * Stack switching interfaces for allocation
2835 */
2836static void
2837xfs_btree_split_worker(
2838 struct work_struct *work)
2839{
2840 struct xfs_btree_split_args *args = container_of(work,
2841 struct xfs_btree_split_args, work);
2842 unsigned long pflags;
Michal Hocko90707332017-05-03 14:53:12 -07002843 unsigned long new_pflags = PF_MEMALLOC_NOFS;
Dave Chinnercf11da92014-07-15 07:08:24 +10002844
2845 /*
2846 * we are in a transaction context here, but may also be doing work
2847 * in kswapd context, and hence we may need to inherit that state
2848 * temporarily to ensure that we don't block waiting for memory reclaim
2849 * in any way.
2850 */
2851 if (args->kswapd)
2852 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2853
2854 current_set_flags_nested(&pflags, new_pflags);
2855
2856 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2857 args->key, args->curp, args->stat);
2858 complete(args->done);
2859
2860 current_restore_flags_nested(&pflags, new_pflags);
2861}
2862
2863/*
2864 * BMBT split requests often come in with little stack to work on. Push
2865 * them off to a worker thread so there is lots of stack to use. For the other
2866 * btree types, just call directly to avoid the context switch overhead here.
2867 */
2868STATIC int /* error */
2869xfs_btree_split(
2870 struct xfs_btree_cur *cur,
2871 int level,
2872 union xfs_btree_ptr *ptrp,
2873 union xfs_btree_key *key,
2874 struct xfs_btree_cur **curp,
2875 int *stat) /* success/failure */
2876{
2877 struct xfs_btree_split_args args;
2878 DECLARE_COMPLETION_ONSTACK(done);
2879
2880 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2881 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2882
2883 args.cur = cur;
2884 args.level = level;
2885 args.ptrp = ptrp;
2886 args.key = key;
2887 args.curp = curp;
2888 args.stat = stat;
2889 args.done = &done;
2890 args.kswapd = current_is_kswapd();
2891 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2892 queue_work(xfs_alloc_wq, &args.work);
2893 wait_for_completion(&done);
2894 destroy_work_on_stack(&args.work);
2895 return args.result;
2896}
2897
2898
Christoph Hellwig344207c2008-10-30 16:57:16 +11002899/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002900 * Copy the old inode root contents into a real block and make the
2901 * broot point to it.
2902 */
2903int /* error */
2904xfs_btree_new_iroot(
2905 struct xfs_btree_cur *cur, /* btree cursor */
2906 int *logflags, /* logging flags for inode */
2907 int *stat) /* return status - 0 fail */
2908{
2909 struct xfs_buf *cbp; /* buffer for cblock */
2910 struct xfs_btree_block *block; /* btree block */
2911 struct xfs_btree_block *cblock; /* child btree block */
2912 union xfs_btree_key *ckp; /* child key pointer */
2913 union xfs_btree_ptr *cpp; /* child ptr pointer */
2914 union xfs_btree_key *kp; /* pointer to btree key */
2915 union xfs_btree_ptr *pp; /* pointer to block addr */
2916 union xfs_btree_ptr nptr; /* new block addr */
2917 int level; /* btree level */
2918 int error; /* error return code */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002919 int i; /* loop counter */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002920
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002921 XFS_BTREE_STATS_INC(cur, newroot);
2922
2923 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2924
2925 level = cur->bc_nlevels - 1;
2926
2927 block = xfs_btree_get_iroot(cur);
2928 pp = xfs_btree_ptr_addr(cur, 1, block);
2929
2930 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002931 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002932 if (error)
2933 goto error0;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002934 if (*stat == 0)
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002935 return 0;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002936
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002937 XFS_BTREE_STATS_INC(cur, alloc);
2938
2939 /* Copy the root into a real block. */
Eric Sandeenf5b999c2019-06-12 09:00:00 -07002940 error = xfs_btree_get_buf_block(cur, &nptr, &cblock, &cbp);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002941 if (error)
2942 goto error0;
2943
Dave Chinner088c9f62013-06-12 12:19:08 +10002944 /*
2945 * we can't just memcpy() the root in for CRC enabled btree blocks.
2946 * In that case have to also ensure the blkno remains correct
2947 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002948 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002949 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2950 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2951 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2952 else
2953 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2954 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002955
2956 be16_add_cpu(&block->bb_level, 1);
2957 xfs_btree_set_numrecs(block, 1);
2958 cur->bc_nlevels++;
2959 cur->bc_ptrs[level + 1] = 1;
2960
2961 kp = xfs_btree_key_addr(cur, 1, block);
2962 ckp = xfs_btree_key_addr(cur, 1, cblock);
2963 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2964
2965 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002966 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002967 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002968 if (error)
2969 goto error0;
2970 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002971
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002972 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2973
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002974 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002975 if (error)
2976 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002977
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002978 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2979
2980 xfs_iroot_realloc(cur->bc_private.b.ip,
2981 1 - xfs_btree_get_numrecs(cblock),
2982 cur->bc_private.b.whichfork);
2983
2984 xfs_btree_setbuf(cur, level, cbp);
2985
2986 /*
2987 * Do all this logging at the end so that
2988 * the root is at the right level.
2989 */
2990 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2991 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2992 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2993
2994 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06002995 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002996 *stat = 1;
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002997 return 0;
2998error0:
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002999 return error;
3000}
3001
3002/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11003003 * Allocate a new root block, fill it in.
3004 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11003005STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11003006xfs_btree_new_root(
3007 struct xfs_btree_cur *cur, /* btree cursor */
3008 int *stat) /* success/failure */
3009{
3010 struct xfs_btree_block *block; /* one half of the old root block */
3011 struct xfs_buf *bp; /* buffer containing block */
3012 int error; /* error return value */
3013 struct xfs_buf *lbp; /* left buffer pointer */
3014 struct xfs_btree_block *left; /* left btree block */
3015 struct xfs_buf *nbp; /* new (root) buffer */
3016 struct xfs_btree_block *new; /* new (root) btree block */
3017 int nptr; /* new value for key index, 1 or 2 */
3018 struct xfs_buf *rbp; /* right buffer pointer */
3019 struct xfs_btree_block *right; /* right btree block */
3020 union xfs_btree_ptr rptr;
3021 union xfs_btree_ptr lptr;
3022
Christoph Hellwig344207c2008-10-30 16:57:16 +11003023 XFS_BTREE_STATS_INC(cur, newroot);
3024
3025 /* initialise our start point from the cursor */
3026 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3027
3028 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10003029 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003030 if (error)
3031 goto error0;
3032 if (*stat == 0)
3033 goto out0;
3034 XFS_BTREE_STATS_INC(cur, alloc);
3035
3036 /* Set up the new block. */
Eric Sandeenf5b999c2019-06-12 09:00:00 -07003037 error = xfs_btree_get_buf_block(cur, &lptr, &new, &nbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003038 if (error)
3039 goto error0;
3040
3041 /* Set the root in the holding structure increasing the level by 1. */
3042 cur->bc_ops->set_root(cur, &lptr, 1);
3043
3044 /*
3045 * At the previous root level there are now two blocks: the old root,
3046 * and the new block generated when it was split. We don't know which
3047 * one the cursor is pointing at, so we set up variables "left" and
3048 * "right" for each case.
3049 */
3050 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3051
3052#ifdef DEBUG
3053 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3054 if (error)
3055 goto error0;
3056#endif
3057
3058 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3059 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3060 /* Our block is left, pick up the right block. */
3061 lbp = bp;
3062 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3063 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003064 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003065 if (error)
3066 goto error0;
3067 bp = rbp;
3068 nptr = 1;
3069 } else {
3070 /* Our block is right, pick up the left block. */
3071 rbp = bp;
3072 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3073 right = block;
3074 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003075 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003076 if (error)
3077 goto error0;
3078 bp = lbp;
3079 nptr = 2;
3080 }
Darrick J. Wong70b22652016-08-03 11:03:38 +10003081
Christoph Hellwig344207c2008-10-30 16:57:16 +11003082 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05003083 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003084 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3085 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3086 !xfs_btree_ptr_is_null(cur, &rptr));
3087
3088 /* Fill in the key data in the new root. */
3089 if (xfs_btree_get_level(left) > 0) {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003090 /*
3091 * Get the keys for the left block's keys and put them directly
3092 * in the parent block. Do the same for the right block.
3093 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003094 xfs_btree_get_node_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003095 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003096 xfs_btree_get_node_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003097 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003098 } else {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003099 /*
3100 * Get the keys for the left block's records and put them
3101 * directly in the parent block. Do the same for the right
3102 * block.
3103 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003104 xfs_btree_get_leaf_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003105 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003106 xfs_btree_get_leaf_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003107 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003108 }
3109 xfs_btree_log_keys(cur, nbp, 1, 2);
3110
3111 /* Fill in the pointer data in the new root. */
3112 xfs_btree_copy_ptrs(cur,
3113 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3114 xfs_btree_copy_ptrs(cur,
3115 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3116 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3117
3118 /* Fix up the cursor. */
3119 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3120 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3121 cur->bc_nlevels++;
Christoph Hellwig344207c2008-10-30 16:57:16 +11003122 *stat = 1;
3123 return 0;
3124error0:
Christoph Hellwig344207c2008-10-30 16:57:16 +11003125 return error;
3126out0:
Christoph Hellwig344207c2008-10-30 16:57:16 +11003127 *stat = 0;
3128 return 0;
3129}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003130
3131STATIC int
3132xfs_btree_make_block_unfull(
3133 struct xfs_btree_cur *cur, /* btree cursor */
3134 int level, /* btree level */
3135 int numrecs,/* # of recs in block */
3136 int *oindex,/* old tree index */
3137 int *index, /* new tree index */
3138 union xfs_btree_ptr *nptr, /* new btree ptr */
3139 struct xfs_btree_cur **ncur, /* new btree cursor */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003140 union xfs_btree_key *key, /* key of new block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003141 int *stat)
3142{
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003143 int error = 0;
3144
3145 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3146 level == cur->bc_nlevels - 1) {
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003147 struct xfs_inode *ip = cur->bc_private.b.ip;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003148
3149 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3150 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003151 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
Darrick J. Wong0d309792016-08-03 11:01:25 +10003152 *stat = 1;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003153 } else {
3154 /* A root block that needs replacing */
3155 int logflags = 0;
3156
3157 error = xfs_btree_new_iroot(cur, &logflags, stat);
3158 if (error || *stat == 0)
3159 return error;
3160
3161 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3162 }
3163
3164 return 0;
3165 }
3166
3167 /* First, try shifting an entry to the right neighbor. */
3168 error = xfs_btree_rshift(cur, level, stat);
3169 if (error || *stat)
3170 return error;
3171
3172 /* Next, try shifting an entry to the left neighbor. */
3173 error = xfs_btree_lshift(cur, level, stat);
3174 if (error)
3175 return error;
3176
3177 if (*stat) {
3178 *oindex = *index = cur->bc_ptrs[level];
3179 return 0;
3180 }
3181
3182 /*
3183 * Next, try splitting the current block in half.
3184 *
3185 * If this works we have to re-set our variables because we
3186 * could be in a different block now.
3187 */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003188 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003189 if (error || *stat == 0)
3190 return error;
3191
3192
3193 *index = cur->bc_ptrs[level];
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003194 return 0;
3195}
3196
3197/*
3198 * Insert one record/level. Return information to the caller
3199 * allowing the next level up to proceed if necessary.
3200 */
3201STATIC int
3202xfs_btree_insrec(
3203 struct xfs_btree_cur *cur, /* btree cursor */
3204 int level, /* level to insert record at */
3205 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003206 union xfs_btree_rec *rec, /* record to insert */
3207 union xfs_btree_key *key, /* i/o: block key for ptrp */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003208 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3209 int *stat) /* success/failure */
3210{
3211 struct xfs_btree_block *block; /* btree block */
3212 struct xfs_buf *bp; /* buffer for block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003213 union xfs_btree_ptr nptr; /* new block ptr */
3214 struct xfs_btree_cur *ncur; /* new btree cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003215 union xfs_btree_key nkey; /* new block key */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003216 union xfs_btree_key *lkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003217 int optr; /* old key/record index */
3218 int ptr; /* key/record index */
3219 int numrecs;/* number of records */
3220 int error; /* error return value */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003221 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003222 xfs_daddr_t old_bn;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003223
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003224 ncur = NULL;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003225 lkey = &nkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003226
3227 /*
3228 * If we have an external root pointer, and we've made it to the
3229 * root level, allocate a new root block and we're done.
3230 */
3231 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3232 (level >= cur->bc_nlevels)) {
3233 error = xfs_btree_new_root(cur, stat);
3234 xfs_btree_set_ptr_null(cur, ptrp);
3235
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003236 return error;
3237 }
3238
3239 /* If we're off the left edge, return failure. */
3240 ptr = cur->bc_ptrs[level];
3241 if (ptr == 0) {
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003242 *stat = 0;
3243 return 0;
3244 }
3245
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003246 optr = ptr;
3247
3248 XFS_BTREE_STATS_INC(cur, insrec);
3249
3250 /* Get pointers to the btree buffer and block. */
3251 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003252 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003253 numrecs = xfs_btree_get_numrecs(block);
3254
3255#ifdef DEBUG
3256 error = xfs_btree_check_block(cur, block, level, bp);
3257 if (error)
3258 goto error0;
3259
3260 /* Check that the new entry is being inserted in the right place. */
3261 if (ptr <= numrecs) {
3262 if (level == 0) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003263 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003264 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003265 } else {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003266 ASSERT(cur->bc_ops->keys_inorder(cur, key,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003267 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003268 }
3269 }
3270#endif
3271
3272 /*
3273 * If the block is full, we can't insert the new entry until we
3274 * make the block un-full.
3275 */
3276 xfs_btree_set_ptr_null(cur, &nptr);
3277 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3278 error = xfs_btree_make_block_unfull(cur, level, numrecs,
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003279 &optr, &ptr, &nptr, &ncur, lkey, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003280 if (error || *stat == 0)
3281 goto error0;
3282 }
3283
3284 /*
3285 * The current block may have changed if the block was
3286 * previously full and we have just made space in it.
3287 */
3288 block = xfs_btree_get_block(cur, level, &bp);
3289 numrecs = xfs_btree_get_numrecs(block);
3290
3291#ifdef DEBUG
3292 error = xfs_btree_check_block(cur, block, level, bp);
3293 if (error)
3294 return error;
3295#endif
3296
3297 /*
3298 * At this point we know there's room for our new entry in the block
3299 * we're pointing at.
3300 */
3301 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3302
3303 if (level > 0) {
3304 /* It's a nonleaf. make a hole in the keys and ptrs */
3305 union xfs_btree_key *kp;
3306 union xfs_btree_ptr *pp;
3307
3308 kp = xfs_btree_key_addr(cur, ptr, block);
3309 pp = xfs_btree_ptr_addr(cur, ptr, block);
3310
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003311 for (i = numrecs - ptr; i >= 0; i--) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003312 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003313 if (error)
3314 return error;
3315 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003316
3317 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3318 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3319
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003320 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003321 if (error)
3322 goto error0;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003323
3324 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003325 xfs_btree_copy_keys(cur, kp, key, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003326 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3327 numrecs++;
3328 xfs_btree_set_numrecs(block, numrecs);
3329 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3330 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3331#ifdef DEBUG
3332 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003333 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3334 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003335 }
3336#endif
3337 } else {
3338 /* It's a leaf. make a hole in the records */
3339 union xfs_btree_rec *rp;
3340
3341 rp = xfs_btree_rec_addr(cur, ptr, block);
3342
3343 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3344
3345 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003346 xfs_btree_copy_recs(cur, rp, rec, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003347 xfs_btree_set_numrecs(block, ++numrecs);
3348 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3349#ifdef DEBUG
3350 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003351 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3352 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003353 }
3354#endif
3355 }
3356
3357 /* Log the new number of records in the btree header. */
3358 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3359
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003360 /*
3361 * If we just inserted into a new tree block, we have to
3362 * recalculate nkey here because nkey is out of date.
3363 *
3364 * Otherwise we're just updating an existing block (having shoved
3365 * some records into the new tree block), so use the regular key
3366 * update mechanism.
3367 */
3368 if (bp && bp->b_bn != old_bn) {
3369 xfs_btree_get_keys(cur, block, lkey);
3370 } else if (xfs_btree_needs_key_update(cur, optr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003371 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003372 if (error)
3373 goto error0;
3374 }
3375
3376 /*
3377 * If we are tracking the last record in the tree and
3378 * we are at the far right edge of the tree, update it.
3379 */
3380 if (xfs_btree_is_lastrec(cur, block, level)) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003381 cur->bc_ops->update_lastrec(cur, block, rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003382 ptr, LASTREC_INSREC);
3383 }
3384
3385 /*
3386 * Return the new block number, if any.
3387 * If there is one, give back a record value and a cursor too.
3388 */
3389 *ptrp = nptr;
3390 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003391 xfs_btree_copy_keys(cur, key, lkey, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003392 *curp = ncur;
3393 }
3394
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003395 *stat = 1;
3396 return 0;
3397
3398error0:
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003399 return error;
3400}
3401
3402/*
3403 * Insert the record at the point referenced by cur.
3404 *
3405 * A multi-level split of the tree on insert will invalidate the original
3406 * cursor. All callers of this function should assume that the cursor is
3407 * no longer valid and revalidate it.
3408 */
3409int
3410xfs_btree_insert(
3411 struct xfs_btree_cur *cur,
3412 int *stat)
3413{
3414 int error; /* error return value */
3415 int i; /* result value, 0 for failure */
3416 int level; /* current level number in btree */
3417 union xfs_btree_ptr nptr; /* new block number (split result) */
3418 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3419 struct xfs_btree_cur *pcur; /* previous level's cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003420 union xfs_btree_key bkey; /* key of block to insert */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003421 union xfs_btree_key *key;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003422 union xfs_btree_rec rec; /* record to insert */
3423
3424 level = 0;
3425 ncur = NULL;
3426 pcur = cur;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003427 key = &bkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003428
3429 xfs_btree_set_ptr_null(cur, &nptr);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003430
3431 /* Make a key out of the record data to be inserted, and save it. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003432 cur->bc_ops->init_rec_from_cur(cur, &rec);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003433 cur->bc_ops->init_key_from_rec(key, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003434
3435 /*
3436 * Loop going up the tree, starting at the leaf level.
3437 * Stop when we don't get a split block, that must mean that
3438 * the insert is finished with this level.
3439 */
3440 do {
3441 /*
3442 * Insert nrec/nptr into this level of the tree.
3443 * Note if we fail, nptr will be null.
3444 */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003445 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003446 &ncur, &i);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003447 if (error) {
3448 if (pcur != cur)
3449 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3450 goto error0;
3451 }
3452
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003453 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3454 error = -EFSCORRUPTED;
3455 goto error0;
3456 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003457 level++;
3458
3459 /*
3460 * See if the cursor we just used is trash.
3461 * Can't trash the caller's cursor, but otherwise we should
3462 * if ncur is a new cursor or we're about to be done.
3463 */
3464 if (pcur != cur &&
3465 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3466 /* Save the state from the cursor before we trash it */
3467 if (cur->bc_ops->update_cursor)
3468 cur->bc_ops->update_cursor(pcur, cur);
3469 cur->bc_nlevels = pcur->bc_nlevels;
3470 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3471 }
3472 /* If we got a new cursor, switch to it. */
3473 if (ncur) {
3474 pcur = ncur;
3475 ncur = NULL;
3476 }
3477 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3478
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003479 *stat = i;
3480 return 0;
3481error0:
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003482 return error;
3483}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003484
3485/*
3486 * Try to merge a non-leaf block back into the inode root.
3487 *
3488 * Note: the killroot names comes from the fact that we're effectively
3489 * killing the old root block. But because we can't just delete the
3490 * inode we have to copy the single block it was pointing to into the
3491 * inode.
3492 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003493STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003494xfs_btree_kill_iroot(
3495 struct xfs_btree_cur *cur)
3496{
3497 int whichfork = cur->bc_private.b.whichfork;
3498 struct xfs_inode *ip = cur->bc_private.b.ip;
3499 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3500 struct xfs_btree_block *block;
3501 struct xfs_btree_block *cblock;
3502 union xfs_btree_key *kp;
3503 union xfs_btree_key *ckp;
3504 union xfs_btree_ptr *pp;
3505 union xfs_btree_ptr *cpp;
3506 struct xfs_buf *cbp;
3507 int level;
3508 int index;
3509 int numrecs;
Christoph Hellwig196328e2016-02-08 14:58:07 +11003510 int error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003511#ifdef DEBUG
3512 union xfs_btree_ptr ptr;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003513#endif
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003514 int i;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003515
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003516 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3517 ASSERT(cur->bc_nlevels > 1);
3518
3519 /*
3520 * Don't deal with the root block needs to be a leaf case.
3521 * We're just going to turn the thing back into extents anyway.
3522 */
3523 level = cur->bc_nlevels - 1;
3524 if (level == 1)
3525 goto out0;
3526
3527 /*
3528 * Give up if the root has multiple children.
3529 */
3530 block = xfs_btree_get_iroot(cur);
3531 if (xfs_btree_get_numrecs(block) != 1)
3532 goto out0;
3533
3534 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3535 numrecs = xfs_btree_get_numrecs(cblock);
3536
3537 /*
3538 * Only do this if the next level will fit.
3539 * Then the data must be copied up to the inode,
3540 * instead of freeing the root you free the next level.
3541 */
3542 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3543 goto out0;
3544
3545 XFS_BTREE_STATS_INC(cur, killroot);
3546
3547#ifdef DEBUG
3548 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3549 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3550 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3551 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3552#endif
3553
3554 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3555 if (index) {
3556 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3557 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003558 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003559 }
3560
3561 be16_add_cpu(&block->bb_numrecs, index);
3562 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3563
3564 kp = xfs_btree_key_addr(cur, 1, block);
3565 ckp = xfs_btree_key_addr(cur, 1, cblock);
3566 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3567
3568 pp = xfs_btree_ptr_addr(cur, 1, block);
3569 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003570
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003571 for (i = 0; i < numrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003572 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003573 if (error)
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003574 return error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003575 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003576
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003577 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3578
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003579 error = xfs_btree_free_block(cur, cbp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003580 if (error)
Christoph Hellwig196328e2016-02-08 14:58:07 +11003581 return error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003582
3583 cur->bc_bufs[level - 1] = NULL;
3584 be16_add_cpu(&block->bb_level, -1);
3585 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003586 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003587 cur->bc_nlevels--;
3588out0:
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003589 return 0;
3590}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003591
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003592/*
3593 * Kill the current root node, and replace it with it's only child node.
3594 */
3595STATIC int
3596xfs_btree_kill_root(
3597 struct xfs_btree_cur *cur,
3598 struct xfs_buf *bp,
3599 int level,
3600 union xfs_btree_ptr *newroot)
3601{
3602 int error;
3603
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003604 XFS_BTREE_STATS_INC(cur, killroot);
3605
3606 /*
3607 * Update the root pointer, decreasing the level by 1 and then
3608 * free the old root.
3609 */
3610 cur->bc_ops->set_root(cur, newroot, -1);
3611
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003612 error = xfs_btree_free_block(cur, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003613 if (error)
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003614 return error;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003615
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003616 cur->bc_bufs[level] = NULL;
3617 cur->bc_ra[level] = 0;
3618 cur->bc_nlevels--;
3619
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003620 return 0;
3621}
3622
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003623STATIC int
3624xfs_btree_dec_cursor(
3625 struct xfs_btree_cur *cur,
3626 int level,
3627 int *stat)
3628{
3629 int error;
3630 int i;
3631
3632 if (level > 0) {
3633 error = xfs_btree_decrement(cur, level, &i);
3634 if (error)
3635 return error;
3636 }
3637
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003638 *stat = 1;
3639 return 0;
3640}
3641
3642/*
3643 * Single level of the btree record deletion routine.
3644 * Delete record pointed to by cur/level.
3645 * Remove the record from its block then rebalance the tree.
3646 * Return 0 for error, 1 for done, 2 to go on to the next level.
3647 */
3648STATIC int /* error */
3649xfs_btree_delrec(
3650 struct xfs_btree_cur *cur, /* btree cursor */
3651 int level, /* level removing record from */
3652 int *stat) /* fail/done/go-on */
3653{
3654 struct xfs_btree_block *block; /* btree block */
3655 union xfs_btree_ptr cptr; /* current block ptr */
3656 struct xfs_buf *bp; /* buffer for block */
3657 int error; /* error return value */
3658 int i; /* loop counter */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003659 union xfs_btree_ptr lptr; /* left sibling block ptr */
3660 struct xfs_buf *lbp; /* left buffer pointer */
3661 struct xfs_btree_block *left; /* left btree block */
3662 int lrecs = 0; /* left record count */
3663 int ptr; /* key/record index */
3664 union xfs_btree_ptr rptr; /* right sibling block ptr */
3665 struct xfs_buf *rbp; /* right buffer pointer */
3666 struct xfs_btree_block *right; /* right btree block */
3667 struct xfs_btree_block *rrblock; /* right-right btree block */
3668 struct xfs_buf *rrbp; /* right-right buffer pointer */
3669 int rrecs = 0; /* right record count */
3670 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3671 int numrecs; /* temporary numrec count */
3672
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003673 tcur = NULL;
3674
3675 /* Get the index of the entry being deleted, check for nothing there. */
3676 ptr = cur->bc_ptrs[level];
3677 if (ptr == 0) {
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003678 *stat = 0;
3679 return 0;
3680 }
3681
3682 /* Get the buffer & block containing the record or key/ptr. */
3683 block = xfs_btree_get_block(cur, level, &bp);
3684 numrecs = xfs_btree_get_numrecs(block);
3685
3686#ifdef DEBUG
3687 error = xfs_btree_check_block(cur, block, level, bp);
3688 if (error)
3689 goto error0;
3690#endif
3691
3692 /* Fail if we're off the end of the block. */
3693 if (ptr > numrecs) {
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003694 *stat = 0;
3695 return 0;
3696 }
3697
3698 XFS_BTREE_STATS_INC(cur, delrec);
3699 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3700
3701 /* Excise the entries being deleted. */
3702 if (level > 0) {
3703 /* It's a nonleaf. operate on keys and ptrs */
3704 union xfs_btree_key *lkp;
3705 union xfs_btree_ptr *lpp;
3706
3707 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3708 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3709
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003710 for (i = 0; i < numrecs - ptr; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003711 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003712 if (error)
3713 goto error0;
3714 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003715
3716 if (ptr < numrecs) {
3717 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3718 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3719 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3720 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3721 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003722 } else {
3723 /* It's a leaf. operate on records */
3724 if (ptr < numrecs) {
3725 xfs_btree_shift_recs(cur,
3726 xfs_btree_rec_addr(cur, ptr + 1, block),
3727 -1, numrecs - ptr);
3728 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3729 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003730 }
3731
3732 /*
3733 * Decrement and log the number of entries in the block.
3734 */
3735 xfs_btree_set_numrecs(block, --numrecs);
3736 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3737
3738 /*
3739 * If we are tracking the last record in the tree and
3740 * we are at the far right edge of the tree, update it.
3741 */
3742 if (xfs_btree_is_lastrec(cur, block, level)) {
3743 cur->bc_ops->update_lastrec(cur, block, NULL,
3744 ptr, LASTREC_DELREC);
3745 }
3746
3747 /*
3748 * We're at the root level. First, shrink the root block in-memory.
3749 * Try to get rid of the next level down. If we can't then there's
3750 * nothing left to do.
3751 */
3752 if (level == cur->bc_nlevels - 1) {
3753 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3754 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3755 cur->bc_private.b.whichfork);
3756
3757 error = xfs_btree_kill_iroot(cur);
3758 if (error)
3759 goto error0;
3760
3761 error = xfs_btree_dec_cursor(cur, level, stat);
3762 if (error)
3763 goto error0;
3764 *stat = 1;
3765 return 0;
3766 }
3767
3768 /*
3769 * If this is the root level, and there's only one entry left,
3770 * and it's NOT the leaf level, then we can get rid of this
3771 * level.
3772 */
3773 if (numrecs == 1 && level > 0) {
3774 union xfs_btree_ptr *pp;
3775 /*
3776 * pp is still set to the first pointer in the block.
3777 * Make it the new root of the btree.
3778 */
3779 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003780 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003781 if (error)
3782 goto error0;
3783 } else if (level > 0) {
3784 error = xfs_btree_dec_cursor(cur, level, stat);
3785 if (error)
3786 goto error0;
3787 }
3788 *stat = 1;
3789 return 0;
3790 }
3791
3792 /*
3793 * If we deleted the leftmost entry in the block, update the
3794 * key values above us in the tree.
3795 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003796 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003797 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003798 if (error)
3799 goto error0;
3800 }
3801
3802 /*
3803 * If the number of records remaining in the block is at least
3804 * the minimum, we're done.
3805 */
3806 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3807 error = xfs_btree_dec_cursor(cur, level, stat);
3808 if (error)
3809 goto error0;
3810 return 0;
3811 }
3812
3813 /*
3814 * Otherwise, we have to move some records around to keep the
3815 * tree balanced. Look at the left and right sibling blocks to
3816 * see if we can re-balance by moving only one record.
3817 */
3818 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3819 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3820
3821 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3822 /*
3823 * One child of root, need to get a chance to copy its contents
3824 * into the root and delete it. Can't go up to next level,
3825 * there's nothing to delete there.
3826 */
3827 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3828 xfs_btree_ptr_is_null(cur, &lptr) &&
3829 level == cur->bc_nlevels - 2) {
3830 error = xfs_btree_kill_iroot(cur);
3831 if (!error)
3832 error = xfs_btree_dec_cursor(cur, level, stat);
3833 if (error)
3834 goto error0;
3835 return 0;
3836 }
3837 }
3838
3839 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3840 !xfs_btree_ptr_is_null(cur, &lptr));
3841
3842 /*
3843 * Duplicate the cursor so our btree manipulations here won't
3844 * disrupt the next level up.
3845 */
3846 error = xfs_btree_dup_cursor(cur, &tcur);
3847 if (error)
3848 goto error0;
3849
3850 /*
3851 * If there's a right sibling, see if it's ok to shift an entry
3852 * out of it.
3853 */
3854 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3855 /*
3856 * Move the temp cursor to the last entry in the next block.
3857 * Actually any entry but the first would suffice.
3858 */
3859 i = xfs_btree_lastrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003860 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3861 error = -EFSCORRUPTED;
3862 goto error0;
3863 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003864
3865 error = xfs_btree_increment(tcur, level, &i);
3866 if (error)
3867 goto error0;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003868 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3869 error = -EFSCORRUPTED;
3870 goto error0;
3871 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003872
3873 i = xfs_btree_lastrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003874 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3875 error = -EFSCORRUPTED;
3876 goto error0;
3877 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003878
3879 /* Grab a pointer to the block. */
3880 right = xfs_btree_get_block(tcur, level, &rbp);
3881#ifdef DEBUG
3882 error = xfs_btree_check_block(tcur, right, level, rbp);
3883 if (error)
3884 goto error0;
3885#endif
3886 /* Grab the current block number, for future use. */
3887 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3888
3889 /*
3890 * If right block is full enough so that removing one entry
3891 * won't make it too empty, and left-shifting an entry out
3892 * of right to us works, we're done.
3893 */
3894 if (xfs_btree_get_numrecs(right) - 1 >=
3895 cur->bc_ops->get_minrecs(tcur, level)) {
3896 error = xfs_btree_lshift(tcur, level, &i);
3897 if (error)
3898 goto error0;
3899 if (i) {
3900 ASSERT(xfs_btree_get_numrecs(block) >=
3901 cur->bc_ops->get_minrecs(tcur, level));
3902
3903 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3904 tcur = NULL;
3905
3906 error = xfs_btree_dec_cursor(cur, level, stat);
3907 if (error)
3908 goto error0;
3909 return 0;
3910 }
3911 }
3912
3913 /*
3914 * Otherwise, grab the number of records in right for
3915 * future reference, and fix up the temp cursor to point
3916 * to our block again (last record).
3917 */
3918 rrecs = xfs_btree_get_numrecs(right);
3919 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3920 i = xfs_btree_firstrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003921 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3922 error = -EFSCORRUPTED;
3923 goto error0;
3924 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003925
3926 error = xfs_btree_decrement(tcur, level, &i);
3927 if (error)
3928 goto error0;
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003929 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3930 error = -EFSCORRUPTED;
3931 goto error0;
3932 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003933 }
3934 }
3935
3936 /*
3937 * If there's a left sibling, see if it's ok to shift an entry
3938 * out of it.
3939 */
3940 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3941 /*
3942 * Move the temp cursor to the first entry in the
3943 * previous block.
3944 */
3945 i = xfs_btree_firstrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003946 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3947 error = -EFSCORRUPTED;
3948 goto error0;
3949 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003950
3951 error = xfs_btree_decrement(tcur, level, &i);
3952 if (error)
3953 goto error0;
3954 i = xfs_btree_firstrec(tcur, level);
Darrick J. Wongf9e03702019-11-11 12:52:18 -08003955 if (XFS_IS_CORRUPT(cur->bc_mp, i != 1)) {
3956 error = -EFSCORRUPTED;
3957 goto error0;
3958 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003959
3960 /* Grab a pointer to the block. */
3961 left = xfs_btree_get_block(tcur, level, &lbp);
3962#ifdef DEBUG
3963 error = xfs_btree_check_block(cur, left, level, lbp);
3964 if (error)
3965 goto error0;
3966#endif
3967 /* Grab the current block number, for future use. */
3968 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3969
3970 /*
3971 * If left block is full enough so that removing one entry
3972 * won't make it too empty, and right-shifting an entry out
3973 * of left to us works, we're done.
3974 */
3975 if (xfs_btree_get_numrecs(left) - 1 >=
3976 cur->bc_ops->get_minrecs(tcur, level)) {
3977 error = xfs_btree_rshift(tcur, level, &i);
3978 if (error)
3979 goto error0;
3980 if (i) {
3981 ASSERT(xfs_btree_get_numrecs(block) >=
3982 cur->bc_ops->get_minrecs(tcur, level));
3983 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3984 tcur = NULL;
3985 if (level == 0)
3986 cur->bc_ptrs[0]++;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003987
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003988 *stat = 1;
3989 return 0;
3990 }
3991 }
3992
3993 /*
3994 * Otherwise, grab the number of records in right for
3995 * future reference.
3996 */
3997 lrecs = xfs_btree_get_numrecs(left);
3998 }
3999
4000 /* Delete the temp cursor, we're done with it. */
4001 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4002 tcur = NULL;
4003
4004 /* If here, we need to do a join to keep the tree balanced. */
4005 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4006
4007 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4008 lrecs + xfs_btree_get_numrecs(block) <=
4009 cur->bc_ops->get_maxrecs(cur, level)) {
4010 /*
4011 * Set "right" to be the starting block,
4012 * "left" to be the left neighbor.
4013 */
4014 rptr = cptr;
4015 right = block;
4016 rbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004017 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004018 if (error)
4019 goto error0;
4020
4021 /*
4022 * If that won't work, see if we can join with the right neighbor block.
4023 */
4024 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4025 rrecs + xfs_btree_get_numrecs(block) <=
4026 cur->bc_ops->get_maxrecs(cur, level)) {
4027 /*
4028 * Set "left" to be the starting block,
4029 * "right" to be the right neighbor.
4030 */
4031 lptr = cptr;
4032 left = block;
4033 lbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004034 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004035 if (error)
4036 goto error0;
4037
4038 /*
4039 * Otherwise, we can't fix the imbalance.
4040 * Just return. This is probably a logic error, but it's not fatal.
4041 */
4042 } else {
4043 error = xfs_btree_dec_cursor(cur, level, stat);
4044 if (error)
4045 goto error0;
4046 return 0;
4047 }
4048
4049 rrecs = xfs_btree_get_numrecs(right);
4050 lrecs = xfs_btree_get_numrecs(left);
4051
4052 /*
4053 * We're now going to join "left" and "right" by moving all the stuff
4054 * in "right" to "left" and deleting "right".
4055 */
4056 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4057 if (level > 0) {
4058 /* It's a non-leaf. Move keys and pointers. */
4059 union xfs_btree_key *lkp; /* left btree key */
4060 union xfs_btree_ptr *lpp; /* left address pointer */
4061 union xfs_btree_key *rkp; /* right btree key */
4062 union xfs_btree_ptr *rpp; /* right address pointer */
4063
4064 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4065 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4066 rkp = xfs_btree_key_addr(cur, 1, right);
4067 rpp = xfs_btree_ptr_addr(cur, 1, right);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07004068
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004069 for (i = 1; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07004070 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004071 if (error)
4072 goto error0;
4073 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07004074
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004075 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4076 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4077
4078 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4079 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4080 } else {
4081 /* It's a leaf. Move records. */
4082 union xfs_btree_rec *lrp; /* left record pointer */
4083 union xfs_btree_rec *rrp; /* right record pointer */
4084
4085 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4086 rrp = xfs_btree_rec_addr(cur, 1, right);
4087
4088 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4089 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4090 }
4091
4092 XFS_BTREE_STATS_INC(cur, join);
4093
4094 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004095 * Fix up the number of records and right block pointer in the
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004096 * surviving block, and log it.
4097 */
4098 xfs_btree_set_numrecs(left, lrecs + rrecs);
4099 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4100 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4101 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4102
4103 /* If there is a right sibling, point it to the remaining block. */
4104 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4105 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004106 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004107 if (error)
4108 goto error0;
4109 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4110 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4111 }
4112
4113 /* Free the deleted block. */
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11004114 error = xfs_btree_free_block(cur, rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004115 if (error)
4116 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004117
4118 /*
4119 * If we joined with the left neighbor, set the buffer in the
4120 * cursor to the left block, and fix up the index.
4121 */
4122 if (bp != lbp) {
4123 cur->bc_bufs[level] = lbp;
4124 cur->bc_ptrs[level] += lrecs;
4125 cur->bc_ra[level] = 0;
4126 }
4127 /*
4128 * If we joined with the right neighbor and there's a level above
4129 * us, increment the cursor at that level.
4130 */
4131 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4132 (level + 1 < cur->bc_nlevels)) {
4133 error = xfs_btree_increment(cur, level + 1, &i);
4134 if (error)
4135 goto error0;
4136 }
4137
4138 /*
4139 * Readjust the ptr at this level if it's not a leaf, since it's
4140 * still pointing at the deletion point, which makes the cursor
4141 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4142 * We can't use decrement because it would change the next level up.
4143 */
4144 if (level > 0)
4145 cur->bc_ptrs[level]--;
4146
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004147 /*
4148 * We combined blocks, so we have to update the parent keys if the
4149 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4150 * points to the old block so that the caller knows which record to
4151 * delete. Therefore, the caller must be savvy enough to call updkeys
4152 * for us if we return stat == 2. The other exit points from this
4153 * function don't require deletions further up the tree, so they can
4154 * call updkeys directly.
4155 */
4156
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004157 /* Return value means the next level up has something to do. */
4158 *stat = 2;
4159 return 0;
4160
4161error0:
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004162 if (tcur)
4163 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4164 return error;
4165}
4166
4167/*
4168 * Delete the record pointed to by cur.
4169 * The cursor refers to the place where the record was (could be inserted)
4170 * when the operation returns.
4171 */
4172int /* error */
4173xfs_btree_delete(
4174 struct xfs_btree_cur *cur,
4175 int *stat) /* success/failure */
4176{
4177 int error; /* error return value */
4178 int level;
4179 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004180 bool joined = false;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004181
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004182 /*
4183 * Go up the tree, starting at leaf level.
4184 *
4185 * If 2 is returned then a join was done; go to the next level.
4186 * Otherwise we are done.
4187 */
4188 for (level = 0, i = 2; i == 2; level++) {
4189 error = xfs_btree_delrec(cur, level, &i);
4190 if (error)
4191 goto error0;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004192 if (i == 2)
4193 joined = true;
4194 }
4195
4196 /*
4197 * If we combined blocks as part of deleting the record, delrec won't
4198 * have updated the parent high keys so we have to do that here.
4199 */
4200 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4201 error = xfs_btree_updkeys_force(cur, 0);
4202 if (error)
4203 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004204 }
4205
4206 if (i == 0) {
4207 for (level = 1; level < cur->bc_nlevels; level++) {
4208 if (cur->bc_ptrs[level] == 0) {
4209 error = xfs_btree_decrement(cur, level, &i);
4210 if (error)
4211 goto error0;
4212 break;
4213 }
4214 }
4215 }
4216
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004217 *stat = i;
4218 return 0;
4219error0:
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004220 return error;
4221}
Christoph Hellwig8cc938f2008-10-30 16:58:11 +11004222
4223/*
4224 * Get the data from the pointed-to record.
4225 */
4226int /* error */
4227xfs_btree_get_rec(
4228 struct xfs_btree_cur *cur, /* btree cursor */
4229 union xfs_btree_rec **recp, /* output: btree record */
4230 int *stat) /* output: success/failure */
4231{
4232 struct xfs_btree_block *block; /* btree block */
4233 struct xfs_buf *bp; /* buffer pointer */
4234 int ptr; /* record number */
4235#ifdef DEBUG
4236 int error; /* error return value */
4237#endif
4238
4239 ptr = cur->bc_ptrs[0];
4240 block = xfs_btree_get_block(cur, 0, &bp);
4241
4242#ifdef DEBUG
4243 error = xfs_btree_check_block(cur, block, 0, bp);
4244 if (error)
4245 return error;
4246#endif
4247
4248 /*
4249 * Off the right end or left end, return failure.
4250 */
4251 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4252 *stat = 0;
4253 return 0;
4254 }
4255
4256 /*
4257 * Point to the record and extract its data.
4258 */
4259 *recp = xfs_btree_rec_addr(cur, ptr, block);
4260 *stat = 1;
4261 return 0;
4262}
Dave Chinner21b5c972013-08-30 10:23:44 +10004263
Darrick J. Wong28a89562016-08-03 11:10:55 +10004264/* Visit a block in a btree. */
4265STATIC int
4266xfs_btree_visit_block(
4267 struct xfs_btree_cur *cur,
4268 int level,
4269 xfs_btree_visit_blocks_fn fn,
4270 void *data)
4271{
4272 struct xfs_btree_block *block;
4273 struct xfs_buf *bp;
4274 union xfs_btree_ptr rptr;
4275 int error;
4276
4277 /* do right sibling readahead */
4278 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4279 block = xfs_btree_get_block(cur, level, &bp);
4280
4281 /* process the block */
4282 error = fn(cur, level, data);
4283 if (error)
4284 return error;
4285
4286 /* now read rh sibling block for next iteration */
4287 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4288 if (xfs_btree_ptr_is_null(cur, &rptr))
4289 return -ENOENT;
4290
4291 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4292}
4293
4294
4295/* Visit every block in a btree. */
4296int
4297xfs_btree_visit_blocks(
4298 struct xfs_btree_cur *cur,
4299 xfs_btree_visit_blocks_fn fn,
Darrick J. Wonge992ae82019-10-28 16:12:35 -07004300 unsigned int flags,
Darrick J. Wong28a89562016-08-03 11:10:55 +10004301 void *data)
4302{
4303 union xfs_btree_ptr lptr;
4304 int level;
4305 struct xfs_btree_block *block = NULL;
4306 int error = 0;
4307
4308 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4309
4310 /* for each level */
4311 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4312 /* grab the left hand block */
4313 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4314 if (error)
4315 return error;
4316
4317 /* readahead the left most block for the next level down */
4318 if (level > 0) {
4319 union xfs_btree_ptr *ptr;
4320
4321 ptr = xfs_btree_ptr_addr(cur, 1, block);
4322 xfs_btree_readahead_ptr(cur, ptr, 1);
4323
4324 /* save for the next iteration of the loop */
Eric Sandeena4d768e2017-05-22 19:54:10 -07004325 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
Darrick J. Wonge992ae82019-10-28 16:12:35 -07004326
4327 if (!(flags & XFS_BTREE_VISIT_LEAVES))
4328 continue;
4329 } else if (!(flags & XFS_BTREE_VISIT_RECORDS)) {
4330 continue;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004331 }
4332
4333 /* for each buffer in the level */
4334 do {
4335 error = xfs_btree_visit_block(cur, level, fn, data);
4336 } while (!error);
4337
4338 if (error != -ENOENT)
4339 return error;
4340 }
4341
4342 return 0;
4343}
4344
Dave Chinner21b5c972013-08-30 10:23:44 +10004345/*
4346 * Change the owner of a btree.
4347 *
4348 * The mechanism we use here is ordered buffer logging. Because we don't know
4349 * how many buffers were are going to need to modify, we don't really want to
4350 * have to make transaction reservations for the worst case of every buffer in a
4351 * full size btree as that may be more space that we can fit in the log....
4352 *
4353 * We do the btree walk in the most optimal manner possible - we have sibling
4354 * pointers so we can just walk all the blocks on each level from left to right
4355 * in a single pass, and then move to the next level and do the same. We can
4356 * also do readahead on the sibling pointers to get IO moving more quickly,
4357 * though for slow disks this is unlikely to make much difference to performance
4358 * as the amount of CPU work we have to do before moving to the next block is
4359 * relatively small.
4360 *
4361 * For each btree block that we load, modify the owner appropriately, set the
4362 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4363 * we mark the region we change dirty so that if the buffer is relogged in
4364 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10004365 * correctly relogged in that transaction. If we are in recovery context, then
4366 * just queue the modified buffer as delayed write buffer so the transaction
4367 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10004368 */
Darrick J. Wong28a89562016-08-03 11:10:55 +10004369struct xfs_btree_block_change_owner_info {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004370 uint64_t new_owner;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004371 struct list_head *buffer_list;
4372};
4373
Dave Chinner21b5c972013-08-30 10:23:44 +10004374static int
4375xfs_btree_block_change_owner(
4376 struct xfs_btree_cur *cur,
4377 int level,
Darrick J. Wong28a89562016-08-03 11:10:55 +10004378 void *data)
Dave Chinner21b5c972013-08-30 10:23:44 +10004379{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004380 struct xfs_btree_block_change_owner_info *bbcoi = data;
Dave Chinner21b5c972013-08-30 10:23:44 +10004381 struct xfs_btree_block *block;
4382 struct xfs_buf *bp;
Dave Chinner21b5c972013-08-30 10:23:44 +10004383
4384 /* modify the owner */
4385 block = xfs_btree_get_block(cur, level, &bp);
Brian Foster2dd3d702017-08-29 10:08:40 -07004386 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4387 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4388 return 0;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004389 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
Brian Foster2dd3d702017-08-29 10:08:40 -07004390 } else {
4391 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4392 return 0;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004393 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
Brian Foster2dd3d702017-08-29 10:08:40 -07004394 }
Dave Chinner21b5c972013-08-30 10:23:44 +10004395
4396 /*
Dave Chinner638f44162013-08-30 10:23:45 +10004397 * If the block is a root block hosted in an inode, we might not have a
4398 * buffer pointer here and we shouldn't attempt to log the change as the
4399 * information is already held in the inode and discarded when the root
4400 * block is formatted into the on-disk inode fork. We still change it,
4401 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10004402 */
Brian Foster2dd3d702017-08-29 10:08:40 -07004403 if (!bp) {
Dave Chinner21b5c972013-08-30 10:23:44 +10004404 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4405 ASSERT(level == cur->bc_nlevels - 1);
Brian Foster2dd3d702017-08-29 10:08:40 -07004406 return 0;
4407 }
4408
4409 if (cur->bc_tp) {
4410 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4411 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4412 return -EAGAIN;
4413 }
4414 } else {
4415 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
Dave Chinner21b5c972013-08-30 10:23:44 +10004416 }
4417
Darrick J. Wong28a89562016-08-03 11:10:55 +10004418 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10004419}
4420
4421int
4422xfs_btree_change_owner(
4423 struct xfs_btree_cur *cur,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004424 uint64_t new_owner,
Dave Chinner638f44162013-08-30 10:23:45 +10004425 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004426{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004427 struct xfs_btree_block_change_owner_info bbcoi;
Dave Chinner21b5c972013-08-30 10:23:44 +10004428
Darrick J. Wong28a89562016-08-03 11:10:55 +10004429 bbcoi.new_owner = new_owner;
4430 bbcoi.buffer_list = buffer_list;
Dave Chinner21b5c972013-08-30 10:23:44 +10004431
Darrick J. Wong28a89562016-08-03 11:10:55 +10004432 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
Darrick J. Wonge992ae82019-10-28 16:12:35 -07004433 XFS_BTREE_VISIT_ALL, &bbcoi);
Dave Chinner21b5c972013-08-30 10:23:44 +10004434}
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004435
Darrick J. Wong8368a602018-01-08 10:51:00 -08004436/* Verify the v5 fields of a long-format btree block. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004437xfs_failaddr_t
Darrick J. Wong8368a602018-01-08 10:51:00 -08004438xfs_btree_lblock_v5hdr_verify(
4439 struct xfs_buf *bp,
4440 uint64_t owner)
4441{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07004442 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004443 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4444
4445 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004446 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004447 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004448 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004449 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004450 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004451 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4452 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004453 return __this_address;
4454 return NULL;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004455}
4456
4457/* Verify a long-format btree block. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004458xfs_failaddr_t
Darrick J. Wong8368a602018-01-08 10:51:00 -08004459xfs_btree_lblock_verify(
4460 struct xfs_buf *bp,
4461 unsigned int max_recs)
4462{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07004463 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004464 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4465
4466 /* numrecs verification */
4467 if (be16_to_cpu(block->bb_numrecs) > max_recs)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004468 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004469
4470 /* sibling pointer verification */
4471 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4472 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004473 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004474 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4475 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004476 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004477
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004478 return NULL;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004479}
4480
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004481/**
4482 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4483 * btree block
4484 *
4485 * @bp: buffer containing the btree block
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004486 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004487xfs_failaddr_t
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004488xfs_btree_sblock_v5hdr_verify(
4489 struct xfs_buf *bp)
4490{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07004491 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004492 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4493 struct xfs_perag *pag = bp->b_pag;
4494
4495 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004496 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004497 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004498 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004499 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004500 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004501 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004502 return __this_address;
4503 return NULL;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004504}
4505
4506/**
4507 * xfs_btree_sblock_verify() -- verify a short-format btree block
4508 *
4509 * @bp: buffer containing the btree block
4510 * @max_recs: maximum records allowed in this btree node
4511 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004512xfs_failaddr_t
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004513xfs_btree_sblock_verify(
4514 struct xfs_buf *bp,
4515 unsigned int max_recs)
4516{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07004517 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004518 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004519 xfs_agblock_t agno;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004520
4521 /* numrecs verification */
4522 if (be16_to_cpu(block->bb_numrecs) > max_recs)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004523 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004524
4525 /* sibling pointer verification */
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004526 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4527 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4528 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004529 return __this_address;
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004530 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4531 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004532 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004533
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004534 return NULL;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004535}
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004536
4537/*
4538 * Calculate the number of btree levels needed to store a given number of
4539 * records in a short-format btree.
4540 */
4541uint
4542xfs_btree_compute_maxlevels(
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004543 uint *limits,
4544 unsigned long len)
4545{
4546 uint level;
4547 unsigned long maxblocks;
4548
4549 maxblocks = (len + limits[0] - 1) / limits[0];
4550 for (level = 1; maxblocks > 1; level++)
4551 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4552 return level;
4553}
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004554
4555/*
4556 * Query a regular btree for all records overlapping a given interval.
4557 * Start with a LE lookup of the key of low_rec and return all records
4558 * until we find a record with a key greater than the key of high_rec.
4559 */
4560STATIC int
4561xfs_btree_simple_query_range(
4562 struct xfs_btree_cur *cur,
4563 union xfs_btree_key *low_key,
4564 union xfs_btree_key *high_key,
4565 xfs_btree_query_range_fn fn,
4566 void *priv)
4567{
4568 union xfs_btree_rec *recp;
4569 union xfs_btree_key rec_key;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004570 int64_t diff;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004571 int stat;
4572 bool firstrec = true;
4573 int error;
4574
4575 ASSERT(cur->bc_ops->init_high_key_from_rec);
4576 ASSERT(cur->bc_ops->diff_two_keys);
4577
4578 /*
4579 * Find the leftmost record. The btree cursor must be set
4580 * to the low record used to generate low_key.
4581 */
4582 stat = 0;
4583 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4584 if (error)
4585 goto out;
4586
Darrick J. Wong5b5c2db2016-08-26 16:00:10 +10004587 /* Nothing? See if there's anything to the right. */
4588 if (!stat) {
4589 error = xfs_btree_increment(cur, 0, &stat);
4590 if (error)
4591 goto out;
4592 }
4593
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004594 while (stat) {
4595 /* Find the record. */
4596 error = xfs_btree_get_rec(cur, &recp, &stat);
4597 if (error || !stat)
4598 break;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004599
4600 /* Skip if high_key(rec) < low_key. */
4601 if (firstrec) {
Darrick J. Wong72227892016-08-26 15:59:50 +10004602 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004603 firstrec = false;
4604 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4605 &rec_key);
4606 if (diff > 0)
4607 goto advloop;
4608 }
4609
4610 /* Stop if high_key < low_key(rec). */
Darrick J. Wong72227892016-08-26 15:59:50 +10004611 cur->bc_ops->init_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004612 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4613 if (diff > 0)
4614 break;
4615
4616 /* Callback */
4617 error = fn(cur, recp, priv);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07004618 if (error)
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004619 break;
4620
4621advloop:
4622 /* Move on to the next record. */
4623 error = xfs_btree_increment(cur, 0, &stat);
4624 if (error)
4625 break;
4626 }
4627
4628out:
4629 return error;
4630}
4631
4632/*
4633 * Query an overlapped interval btree for all records overlapping a given
4634 * interval. This function roughly follows the algorithm given in
4635 * "Interval Trees" of _Introduction to Algorithms_, which is section
4636 * 14.3 in the 2nd and 3rd editions.
4637 *
4638 * First, generate keys for the low and high records passed in.
4639 *
4640 * For any leaf node, generate the high and low keys for the record.
4641 * If the record keys overlap with the query low/high keys, pass the
4642 * record to the function iterator.
4643 *
4644 * For any internal node, compare the low and high keys of each
4645 * pointer against the query low/high keys. If there's an overlap,
4646 * follow the pointer.
4647 *
4648 * As an optimization, we stop scanning a block when we find a low key
4649 * that is greater than the query's high key.
4650 */
4651STATIC int
4652xfs_btree_overlapped_query_range(
4653 struct xfs_btree_cur *cur,
4654 union xfs_btree_key *low_key,
4655 union xfs_btree_key *high_key,
4656 xfs_btree_query_range_fn fn,
4657 void *priv)
4658{
4659 union xfs_btree_ptr ptr;
4660 union xfs_btree_ptr *pp;
4661 union xfs_btree_key rec_key;
4662 union xfs_btree_key rec_hkey;
4663 union xfs_btree_key *lkp;
4664 union xfs_btree_key *hkp;
4665 union xfs_btree_rec *recp;
4666 struct xfs_btree_block *block;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004667 int64_t ldiff;
4668 int64_t hdiff;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004669 int level;
4670 struct xfs_buf *bp;
4671 int i;
4672 int error;
4673
4674 /* Load the root of the btree. */
4675 level = cur->bc_nlevels - 1;
4676 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4677 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4678 if (error)
4679 return error;
4680 xfs_btree_get_block(cur, level, &bp);
4681 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4682#ifdef DEBUG
4683 error = xfs_btree_check_block(cur, block, level, bp);
4684 if (error)
4685 goto out;
4686#endif
4687 cur->bc_ptrs[level] = 1;
4688
4689 while (level < cur->bc_nlevels) {
4690 block = xfs_btree_get_block(cur, level, &bp);
4691
4692 /* End of node, pop back towards the root. */
4693 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4694pop_up:
4695 if (level < cur->bc_nlevels - 1)
4696 cur->bc_ptrs[level + 1]++;
4697 level++;
4698 continue;
4699 }
4700
4701 if (level == 0) {
4702 /* Handle a leaf node. */
4703 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4704
4705 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4706 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4707 low_key);
4708
4709 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4710 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4711 &rec_key);
4712
4713 /*
4714 * If (record's high key >= query's low key) and
4715 * (query's high key >= record's low key), then
4716 * this record overlaps the query range; callback.
4717 */
4718 if (ldiff >= 0 && hdiff >= 0) {
4719 error = fn(cur, recp, priv);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07004720 if (error)
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004721 break;
4722 } else if (hdiff < 0) {
4723 /* Record is larger than high key; pop. */
4724 goto pop_up;
4725 }
4726 cur->bc_ptrs[level]++;
4727 continue;
4728 }
4729
4730 /* Handle an internal node. */
4731 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4732 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4733 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4734
4735 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4736 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4737
4738 /*
4739 * If (pointer's high key >= query's low key) and
4740 * (query's high key >= pointer's low key), then
4741 * this record overlaps the query range; follow pointer.
4742 */
4743 if (ldiff >= 0 && hdiff >= 0) {
4744 level--;
4745 error = xfs_btree_lookup_get_block(cur, level, pp,
4746 &block);
4747 if (error)
4748 goto out;
4749 xfs_btree_get_block(cur, level, &bp);
4750 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4751#ifdef DEBUG
4752 error = xfs_btree_check_block(cur, block, level, bp);
4753 if (error)
4754 goto out;
4755#endif
4756 cur->bc_ptrs[level] = 1;
4757 continue;
4758 } else if (hdiff < 0) {
4759 /* The low key is larger than the upper range; pop. */
4760 goto pop_up;
4761 }
4762 cur->bc_ptrs[level]++;
4763 }
4764
4765out:
4766 /*
4767 * If we don't end this function with the cursor pointing at a record
4768 * block, a subsequent non-error cursor deletion will not release
4769 * node-level buffers, causing a buffer leak. This is quite possible
4770 * with a zero-results range query, so release the buffers if we
4771 * failed to return any results.
4772 */
4773 if (cur->bc_bufs[0] == NULL) {
4774 for (i = 0; i < cur->bc_nlevels; i++) {
4775 if (cur->bc_bufs[i]) {
4776 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4777 cur->bc_bufs[i] = NULL;
4778 cur->bc_ptrs[i] = 0;
4779 cur->bc_ra[i] = 0;
4780 }
4781 }
4782 }
4783
4784 return error;
4785}
4786
4787/*
4788 * Query a btree for all records overlapping a given interval of keys. The
4789 * supplied function will be called with each record found; return one of the
4790 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07004791 * code. This function returns -ECANCELED, zero, or a negative error code.
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004792 */
4793int
4794xfs_btree_query_range(
4795 struct xfs_btree_cur *cur,
4796 union xfs_btree_irec *low_rec,
4797 union xfs_btree_irec *high_rec,
4798 xfs_btree_query_range_fn fn,
4799 void *priv)
4800{
4801 union xfs_btree_rec rec;
4802 union xfs_btree_key low_key;
4803 union xfs_btree_key high_key;
4804
4805 /* Find the keys of both ends of the interval. */
4806 cur->bc_rec = *high_rec;
4807 cur->bc_ops->init_rec_from_cur(cur, &rec);
4808 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4809
4810 cur->bc_rec = *low_rec;
4811 cur->bc_ops->init_rec_from_cur(cur, &rec);
4812 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4813
4814 /* Enforce low key < high key. */
4815 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4816 return -EINVAL;
4817
4818 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4819 return xfs_btree_simple_query_range(cur, &low_key,
4820 &high_key, fn, priv);
4821 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4822 fn, priv);
4823}
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004824
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004825/* Query a btree for all records. */
4826int
4827xfs_btree_query_all(
4828 struct xfs_btree_cur *cur,
4829 xfs_btree_query_range_fn fn,
4830 void *priv)
4831{
Darrick J. Wong5a4c7332017-06-16 11:00:04 -07004832 union xfs_btree_key low_key;
4833 union xfs_btree_key high_key;
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004834
Darrick J. Wong5a4c7332017-06-16 11:00:04 -07004835 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4836 memset(&low_key, 0, sizeof(low_key));
4837 memset(&high_key, 0xFF, sizeof(high_key));
4838
4839 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004840}
4841
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004842/*
4843 * Calculate the number of blocks needed to store a given number of records
4844 * in a short-format (per-AG metadata) btree.
4845 */
Darrick J. Wong14861c42018-05-09 10:02:01 -07004846unsigned long long
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004847xfs_btree_calc_size(
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004848 uint *limits,
4849 unsigned long long len)
4850{
4851 int level;
4852 int maxrecs;
Darrick J. Wong14861c42018-05-09 10:02:01 -07004853 unsigned long long rval;
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004854
4855 maxrecs = limits[0];
4856 for (level = 0, rval = 0; len > 1; level++) {
4857 len += maxrecs - 1;
4858 do_div(len, maxrecs);
4859 maxrecs = limits[1];
4860 rval += len;
4861 }
4862 return rval;
4863}
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004864
Eric Biggersf1b82432016-10-20 15:42:30 +11004865static int
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004866xfs_btree_count_blocks_helper(
4867 struct xfs_btree_cur *cur,
4868 int level,
4869 void *data)
4870{
4871 xfs_extlen_t *blocks = data;
4872 (*blocks)++;
4873
4874 return 0;
4875}
4876
4877/* Count the blocks in a btree and return the result in *blocks. */
4878int
4879xfs_btree_count_blocks(
4880 struct xfs_btree_cur *cur,
4881 xfs_extlen_t *blocks)
4882{
4883 *blocks = 0;
4884 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
Darrick J. Wonge992ae82019-10-28 16:12:35 -07004885 XFS_BTREE_VISIT_ALL, blocks);
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004886}
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07004887
4888/* Compare two btree pointers. */
4889int64_t
4890xfs_btree_diff_two_ptrs(
4891 struct xfs_btree_cur *cur,
4892 const union xfs_btree_ptr *a,
4893 const union xfs_btree_ptr *b)
4894{
4895 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4896 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4897 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4898}
Darrick J. Wongce1d8022018-01-16 18:52:12 -08004899
4900/* If there's an extent, we're done. */
4901STATIC int
4902xfs_btree_has_record_helper(
4903 struct xfs_btree_cur *cur,
4904 union xfs_btree_rec *rec,
4905 void *priv)
4906{
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07004907 return -ECANCELED;
Darrick J. Wongce1d8022018-01-16 18:52:12 -08004908}
4909
4910/* Is there a record covering a given range of keys? */
4911int
4912xfs_btree_has_record(
4913 struct xfs_btree_cur *cur,
4914 union xfs_btree_irec *low,
4915 union xfs_btree_irec *high,
4916 bool *exists)
4917{
4918 int error;
4919
4920 error = xfs_btree_query_range(cur, low, high,
4921 &xfs_btree_has_record_helper, NULL);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -07004922 if (error == -ECANCELED) {
Darrick J. Wongce1d8022018-01-16 18:52:12 -08004923 *exists = true;
4924 return 0;
4925 }
4926 *exists = false;
4927 return error;
4928}
Darrick J. Wong08daa3c2018-05-09 10:02:03 -07004929
4930/* Are there more records in this btree? */
4931bool
4932xfs_btree_has_more_records(
4933 struct xfs_btree_cur *cur)
4934{
4935 struct xfs_btree_block *block;
4936 struct xfs_buf *bp;
4937
4938 block = xfs_btree_get_block(cur, 0, &bp);
4939
4940 /* There are still records in this block. */
4941 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4942 return true;
4943
4944 /* There are more record blocks. */
4945 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4946 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4947 else
4948 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);
4949}