blob: 34c6d7bd4d180c736d8da7e6c33a413ee177ac5f [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"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100014#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110016#include "xfs_trans.h"
Christoph Hellwig38bb7422008-10-30 16:56:22 +110017#include "xfs_inode_item.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050018#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_btree.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070020#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000022#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050023#include "xfs_cksum.h"
Dave Chinnercf11da92014-07-15 07:08:24 +100024#include "xfs_alloc.h"
Brian Fostera45086e2015-10-12 15:59:25 +110025#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
28 * Cursor allocation zone.
29 */
30kmem_zone_t *xfs_btree_cur_zone;
31
32/*
33 * Btree magic numbers.
34 */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070035static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
Darrick J. Wongb8704942016-08-03 11:30:32 +100036 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070037 XFS_FIBT_MAGIC, 0 },
Darrick J. Wongb8704942016-08-03 11:30:32 +100038 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070039 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
40 XFS_REFC_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080042
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070043uint32_t
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080044xfs_btree_magic(
45 int crc,
46 xfs_btnum_t btnum)
47{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070048 uint32_t magic = xfs_magics[crc][btnum];
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080049
50 /* Ensure we asked for crc for crc-only magics. */
51 ASSERT(magic != 0);
52 return magic;
53}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Darrick J. Wong52c732e2017-10-17 21:37:33 -070055/*
56 * Check a long btree block header. Return the address of the failing check,
57 * or NULL if everything is ok.
58 */
59xfs_failaddr_t
60__xfs_btree_check_lblock(
61 struct xfs_btree_cur *cur,
62 struct xfs_btree_block *block,
63 int level,
64 struct xfs_buf *bp)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110065{
Darrick J. Wong52c732e2017-10-17 21:37:33 -070066 struct xfs_mount *mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080067 xfs_btnum_t btnum = cur->bc_btnum;
Darrick J. Wong52c732e2017-10-17 21:37:33 -070068 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050069
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080070 if (crc) {
Darrick J. Wong52c732e2017-10-17 21:37:33 -070071 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
72 return __this_address;
73 if (block->bb_u.l.bb_blkno !=
74 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
75 return __this_address;
76 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
77 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050078 }
79
Darrick J. Wong52c732e2017-10-17 21:37:33 -070080 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
81 return __this_address;
82 if (be16_to_cpu(block->bb_level) != level)
83 return __this_address;
84 if (be16_to_cpu(block->bb_numrecs) >
85 cur->bc_ops->get_maxrecs(cur, level))
86 return __this_address;
87 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
88 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib),
89 level + 1))
90 return __this_address;
91 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
92 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib),
93 level + 1))
94 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050095
Darrick J. Wong52c732e2017-10-17 21:37:33 -070096 return NULL;
97}
98
99/* Check a long btree block header. */
Christoph Hellwig4483eb52017-11-06 11:54:01 -0800100static int
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700101xfs_btree_check_lblock(
102 struct xfs_btree_cur *cur,
103 struct xfs_btree_block *block,
104 int level,
105 struct xfs_buf *bp)
106{
107 struct xfs_mount *mp = cur->bc_mp;
108 xfs_failaddr_t fa;
109
110 fa = __xfs_btree_check_lblock(cur, block, level, bp);
111 if (unlikely(XFS_TEST_ERROR(fa != NULL, mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700112 XFS_ERRTAG_BTREE_CHECK_LBLOCK))) {
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100113 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000114 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500115 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000116 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100117 }
118 return 0;
119}
120
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700121/*
122 * Check a short btree block header. Return the address of the failing check,
123 * or NULL if everything is ok.
124 */
125xfs_failaddr_t
126__xfs_btree_check_sblock(
127 struct xfs_btree_cur *cur,
128 struct xfs_btree_block *block,
129 int level,
130 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700132 struct xfs_mount *mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800133 xfs_btnum_t btnum = cur->bc_btnum;
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700134 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500135
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800136 if (crc) {
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700137 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
138 return __this_address;
139 if (block->bb_u.s.bb_blkno !=
140 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
141 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500142 }
143
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700144 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
145 return __this_address;
146 if (be16_to_cpu(block->bb_level) != level)
147 return __this_address;
148 if (be16_to_cpu(block->bb_numrecs) >
149 cur->bc_ops->get_maxrecs(cur, level))
150 return __this_address;
151 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
152 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib),
153 level + 1))
154 return __this_address;
155 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
156 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib),
157 level + 1))
158 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500159
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700160 return NULL;
161}
162
163/* Check a short btree block header. */
164STATIC int
165xfs_btree_check_sblock(
166 struct xfs_btree_cur *cur,
167 struct xfs_btree_block *block,
168 int level,
169 struct xfs_buf *bp)
170{
171 struct xfs_mount *mp = cur->bc_mp;
172 xfs_failaddr_t fa;
173
174 fa = __xfs_btree_check_sblock(cur, block, level, bp);
175 if (unlikely(XFS_TEST_ERROR(fa != NULL, mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700176 XFS_ERRTAG_BTREE_CHECK_SBLOCK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000178 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500179 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000180 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 return 0;
183}
184
185/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100186 * Debug routine: check that block header is ok.
187 */
188int
189xfs_btree_check_block(
190 struct xfs_btree_cur *cur, /* btree cursor */
191 struct xfs_btree_block *block, /* generic btree block pointer */
192 int level, /* level of the btree block */
193 struct xfs_buf *bp) /* buffer containing block, if any */
194{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100195 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
196 return xfs_btree_check_lblock(cur, block, level, bp);
197 else
198 return xfs_btree_check_sblock(cur, block, level, bp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100199}
200
Darrick J. Wongf1357612017-10-17 21:37:33 -0700201/* Check that this long pointer is valid and points within the fs. */
202bool
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100203xfs_btree_check_lptr(
Darrick J. Wongf1357612017-10-17 21:37:33 -0700204 struct xfs_btree_cur *cur,
205 xfs_fsblock_t fsbno,
206 int level)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100207{
Darrick J. Wongf1357612017-10-17 21:37:33 -0700208 if (level <= 0)
209 return false;
210 return xfs_verify_fsbno(cur->bc_mp, fsbno);
211}
212
213/* Check that this short pointer is valid and points within the AG. */
214bool
215xfs_btree_check_sptr(
216 struct xfs_btree_cur *cur,
217 xfs_agblock_t agbno,
218 int level)
219{
220 if (level <= 0)
221 return false;
222 return xfs_verify_agbno(cur->bc_mp, cur->bc_private.a.agno, agbno);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100223}
224
225/*
Darrick J. Wongf1357612017-10-17 21:37:33 -0700226 * Check that a given (indexed) btree pointer at a certain level of a
227 * btree is valid and doesn't point past where it should.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Christoph Hellwig4483eb52017-11-06 11:54:01 -0800229static int
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100230xfs_btree_check_ptr(
Darrick J. Wongf1357612017-10-17 21:37:33 -0700231 struct xfs_btree_cur *cur,
232 union xfs_btree_ptr *ptr,
233 int index,
234 int level)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100235{
236 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700237 if (xfs_btree_check_lptr(cur, be64_to_cpu((&ptr->l)[index]),
238 level))
239 return 0;
240 xfs_err(cur->bc_mp,
241"Inode %llu fork %d: Corrupt btree %d pointer at level %d index %d.",
242 cur->bc_private.b.ip->i_ino,
243 cur->bc_private.b.whichfork, cur->bc_btnum,
244 level, index);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100245 } else {
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700246 if (xfs_btree_check_sptr(cur, be32_to_cpu((&ptr->s)[index]),
247 level))
248 return 0;
249 xfs_err(cur->bc_mp,
250"AG %u: Corrupt btree %d pointer at level %d index %d.",
251 cur->bc_private.a.agno, cur->bc_btnum,
252 level, index);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100253 }
Darrick J. Wongf1357612017-10-17 21:37:33 -0700254
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700255 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100256}
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -0700257
258#ifdef DEBUG
259# define xfs_btree_debug_check_ptr xfs_btree_check_ptr
260#else
261# define xfs_btree_debug_check_ptr(...) (0)
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100262#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100263
264/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500265 * Calculate CRC on the whole btree block and stuff it into the
266 * long-form btree header.
267 *
268 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100269 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500270 * it to disk.
271 */
272void
273xfs_btree_lblock_calc_crc(
274 struct xfs_buf *bp)
275{
276 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800277 struct xfs_buf_log_item *bip = bp->b_log_item;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500278
279 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
280 return;
281 if (bip)
282 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100283 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500284}
285
286bool
287xfs_btree_lblock_verify_crc(
288 struct xfs_buf *bp)
289{
Brian Fostera45086e2015-10-12 15:59:25 +1100290 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
291 struct xfs_mount *mp = bp->b_target->bt_mount;
292
293 if (xfs_sb_version_hascrc(&mp->m_sb)) {
294 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
295 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100296 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100297 }
Eric Sandeen51582172014-02-27 15:17:27 +1100298
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500299 return true;
300}
301
302/*
303 * Calculate CRC on the whole btree block and stuff it into the
304 * short-form btree header.
305 *
306 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100307 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500308 * it to disk.
309 */
310void
311xfs_btree_sblock_calc_crc(
312 struct xfs_buf *bp)
313{
314 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800315 struct xfs_buf_log_item *bip = bp->b_log_item;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500316
317 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
318 return;
319 if (bip)
320 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100321 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500322}
323
324bool
325xfs_btree_sblock_verify_crc(
326 struct xfs_buf *bp)
327{
Brian Fostera45086e2015-10-12 15:59:25 +1100328 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
329 struct xfs_mount *mp = bp->b_target->bt_mount;
330
331 if (xfs_sb_version_hascrc(&mp->m_sb)) {
332 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800333 return __this_address;
Eric Sandeen51582172014-02-27 15:17:27 +1100334 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100335 }
Eric Sandeen51582172014-02-27 15:17:27 +1100336
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500337 return true;
338}
339
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100340static int
341xfs_btree_free_block(
342 struct xfs_btree_cur *cur,
343 struct xfs_buf *bp)
344{
345 int error;
346
347 error = cur->bc_ops->free_block(cur, bp);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100348 if (!error) {
349 xfs_trans_binval(cur->bc_tp, bp);
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100350 XFS_BTREE_STATS_INC(cur, free);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100351 }
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100352 return error;
353}
354
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500355/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * Delete the btree cursor.
357 */
358void
359xfs_btree_del_cursor(
360 xfs_btree_cur_t *cur, /* btree cursor */
361 int error) /* del because of error */
362{
363 int i; /* btree level */
364
365 /*
366 * Clear the buffer pointers, and release the buffers.
367 * If we're doing this in the face of an error, we
368 * need to make sure to inspect all of the entries
369 * in the bc_bufs array for buffers to be unlocked.
370 * This is because some of the btree code works from
371 * level n down to 0, and if we get an error along
372 * the way we won't have initialized all the entries
373 * down to 0.
374 */
375 for (i = 0; i < cur->bc_nlevels; i++) {
376 if (cur->bc_bufs[i])
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000377 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 else if (!error)
379 break;
380 }
381 /*
382 * Can't free a bmap cursor without having dealt with the
383 * allocated indirect blocks' accounting.
384 */
385 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
386 cur->bc_private.b.allocated == 0);
387 /*
388 * Free the cursor.
389 */
390 kmem_zone_free(xfs_btree_cur_zone, cur);
391}
392
393/*
394 * Duplicate the btree cursor.
395 * Allocate a new one, copy the record, re-get the buffers.
396 */
397int /* error */
398xfs_btree_dup_cursor(
399 xfs_btree_cur_t *cur, /* input cursor */
400 xfs_btree_cur_t **ncur) /* output cursor */
401{
402 xfs_buf_t *bp; /* btree block's buffer pointer */
403 int error; /* error return value */
404 int i; /* level number of btree block */
405 xfs_mount_t *mp; /* mount structure for filesystem */
406 xfs_btree_cur_t *new; /* new cursor value */
407 xfs_trans_t *tp; /* transaction pointer, can be NULL */
408
409 tp = cur->bc_tp;
410 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * Allocate a new cursor like the old one.
414 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100415 new = cur->bc_ops->dup_cursor(cur);
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Copy the record currently in the cursor.
419 */
420 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
423 * For each level current, re-get the buffer and copy the ptr value.
424 */
425 for (i = 0; i < new->bc_nlevels; i++) {
426 new->bc_ptrs[i] = cur->bc_ptrs[i];
427 new->bc_ra[i] = cur->bc_ra[i];
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100428 bp = cur->bc_bufs[i];
429 if (bp) {
430 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
431 XFS_BUF_ADDR(bp), mp->m_bsize,
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100432 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100433 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100434 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 xfs_btree_del_cursor(new, error);
436 *ncur = NULL;
437 return error;
438 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500439 }
440 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *ncur = new;
443 return 0;
444}
445
446/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100447 * XFS btree block layout and addressing:
448 *
449 * There are two types of blocks in the btree: leaf and non-leaf blocks.
450 *
451 * The leaf record start with a header then followed by records containing
452 * the values. A non-leaf block also starts with the same header, and
453 * then first contains lookup keys followed by an equal number of pointers
454 * to the btree blocks at the previous level.
455 *
456 * +--------+-------+-------+-------+-------+-------+-------+
457 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
458 * +--------+-------+-------+-------+-------+-------+-------+
459 *
460 * +--------+-------+-------+-------+-------+-------+-------+
461 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
462 * +--------+-------+-------+-------+-------+-------+-------+
463 *
464 * The header is called struct xfs_btree_block for reasons better left unknown
465 * and comes in different versions for short (32bit) and long (64bit) block
466 * pointers. The record and key structures are defined by the btree instances
467 * and opaque to the btree core. The block pointers are simple disk endian
468 * integers, available in a short (32bit) and long (64bit) variant.
469 *
470 * The helpers below calculate the offset of a given record, key or pointer
471 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
472 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
473 * inside the btree block is done using indices starting at one, not zero!
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000474 *
475 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
476 * overlapping intervals. In such a tree, records are still sorted lowest to
477 * highest and indexed by the smallest key value that refers to the record.
478 * However, nodes are different: each pointer has two associated keys -- one
479 * indexing the lowest key available in the block(s) below (the same behavior
480 * as the key in a regular btree) and another indexing the highest key
481 * available in the block(s) below. Because records are /not/ sorted by the
482 * highest key, all leaf block updates require us to compute the highest key
483 * that matches any record in the leaf and to recursively update the high keys
484 * in the nodes going further up in the tree, if necessary. Nodes look like
485 * this:
486 *
487 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
488 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
489 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
490 *
491 * To perform an interval query on an overlapped tree, perform the usual
492 * depth-first search and use the low and high keys to decide if we can skip
493 * that particular node. If a leaf node is reached, return the records that
494 * intersect the interval. Note that an interval query may return numerous
495 * entries. For a non-overlapped tree, simply search for the record associated
496 * with the lowest key and iterate forward until a non-matching record is
497 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
498 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
499 * more detail.
500 *
501 * Why do we care about overlapping intervals? Let's say you have a bunch of
502 * reverse mapping records on a reflink filesystem:
503 *
504 * 1: +- file A startblock B offset C length D -----------+
505 * 2: +- file E startblock F offset G length H --------------+
506 * 3: +- file I startblock F offset J length K --+
507 * 4: +- file L... --+
508 *
509 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
510 * we'd simply increment the length of record 1. But how do we find the record
511 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
512 * record 3 because the keys are ordered first by startblock. An interval
513 * query would return records 1 and 2 because they both overlap (B+D-1), and
514 * from that we can pick out record 1 as the appropriate left neighbor.
515 *
516 * In the non-overlapped case you can do a LE lookup and decrement the cursor
517 * because a record's interval must end before the next record.
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100518 */
519
520/*
521 * Return size of the btree block header for this btree instance.
522 */
523static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
524{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500525 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
526 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
527 return XFS_BTREE_LBLOCK_CRC_LEN;
528 return XFS_BTREE_LBLOCK_LEN;
529 }
530 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
531 return XFS_BTREE_SBLOCK_CRC_LEN;
532 return XFS_BTREE_SBLOCK_LEN;
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100533}
534
535/*
536 * Return size of btree block pointers for this btree instance.
537 */
538static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
539{
540 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
541 sizeof(__be64) : sizeof(__be32);
542}
543
544/*
545 * Calculate offset of the n-th record in a btree block.
546 */
547STATIC size_t
548xfs_btree_rec_offset(
549 struct xfs_btree_cur *cur,
550 int n)
551{
552 return xfs_btree_block_len(cur) +
553 (n - 1) * cur->bc_ops->rec_len;
554}
555
556/*
557 * Calculate offset of the n-th key in a btree block.
558 */
559STATIC size_t
560xfs_btree_key_offset(
561 struct xfs_btree_cur *cur,
562 int n)
563{
564 return xfs_btree_block_len(cur) +
565 (n - 1) * cur->bc_ops->key_len;
566}
567
568/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000569 * Calculate offset of the n-th high key in a btree block.
570 */
571STATIC size_t
572xfs_btree_high_key_offset(
573 struct xfs_btree_cur *cur,
574 int n)
575{
576 return xfs_btree_block_len(cur) +
577 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
578}
579
580/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100581 * Calculate offset of the n-th block pointer in a btree block.
582 */
583STATIC size_t
584xfs_btree_ptr_offset(
585 struct xfs_btree_cur *cur,
586 int n,
587 int level)
588{
589 return xfs_btree_block_len(cur) +
590 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
591 (n - 1) * xfs_btree_ptr_len(cur);
592}
593
594/*
595 * Return a pointer to the n-th record in the btree block.
596 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700597union xfs_btree_rec *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100598xfs_btree_rec_addr(
599 struct xfs_btree_cur *cur,
600 int n,
601 struct xfs_btree_block *block)
602{
603 return (union xfs_btree_rec *)
604 ((char *)block + xfs_btree_rec_offset(cur, n));
605}
606
607/*
608 * Return a pointer to the n-th key in the btree block.
609 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700610union xfs_btree_key *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100611xfs_btree_key_addr(
612 struct xfs_btree_cur *cur,
613 int n,
614 struct xfs_btree_block *block)
615{
616 return (union xfs_btree_key *)
617 ((char *)block + xfs_btree_key_offset(cur, n));
618}
619
620/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000621 * Return a pointer to the n-th high key in the btree block.
622 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700623union xfs_btree_key *
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000624xfs_btree_high_key_addr(
625 struct xfs_btree_cur *cur,
626 int n,
627 struct xfs_btree_block *block)
628{
629 return (union xfs_btree_key *)
630 ((char *)block + xfs_btree_high_key_offset(cur, n));
631}
632
633/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100634 * Return a pointer to the n-th block pointer in the btree block.
635 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700636union xfs_btree_ptr *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100637xfs_btree_ptr_addr(
638 struct xfs_btree_cur *cur,
639 int n,
640 struct xfs_btree_block *block)
641{
642 int level = xfs_btree_get_level(block);
643
644 ASSERT(block->bb_level != 0);
645
646 return (union xfs_btree_ptr *)
647 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
648}
649
650/*
Zhi Yong Wu1cb93862013-08-07 10:11:05 +0000651 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100652 *
653 * For now this btree implementation assumes the btree root is always
654 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100656STATIC struct xfs_btree_block *
657xfs_btree_get_iroot(
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000658 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000660 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000662 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
663 return (struct xfs_btree_block *)ifp->if_broot;
Christoph Hellwig8186e512008-10-30 16:54:22 +1100664}
665
666/*
667 * Retrieve the block pointer from the cursor at the given level.
668 * This may be an inode btree root or from a buffer.
669 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700670struct xfs_btree_block * /* generic btree block pointer */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100671xfs_btree_get_block(
672 struct xfs_btree_cur *cur, /* btree cursor */
673 int level, /* level in btree */
674 struct xfs_buf **bpp) /* buffer containing the block */
675{
676 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
677 (level == cur->bc_nlevels - 1)) {
678 *bpp = NULL;
679 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100681
682 *bpp = cur->bc_bufs[level];
683 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686/*
687 * Get a buffer for the block, return it with no data read.
688 * Long-form addressing.
689 */
690xfs_buf_t * /* buffer for fsbno */
691xfs_btree_get_bufl(
692 xfs_mount_t *mp, /* file system mount point */
693 xfs_trans_t *tp, /* transaction pointer */
694 xfs_fsblock_t fsbno, /* file system block number */
695 uint lock) /* lock flags for get_buf */
696{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 xfs_daddr_t d; /* real disk block address */
698
699 ASSERT(fsbno != NULLFSBLOCK);
700 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000701 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/*
705 * Get a buffer for the block, return it with no data read.
706 * Short-form addressing.
707 */
708xfs_buf_t * /* buffer for agno/agbno */
709xfs_btree_get_bufs(
710 xfs_mount_t *mp, /* file system mount point */
711 xfs_trans_t *tp, /* transaction pointer */
712 xfs_agnumber_t agno, /* allocation group number */
713 xfs_agblock_t agbno, /* allocation group block number */
714 uint lock) /* lock flags for get_buf */
715{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 xfs_daddr_t d; /* real disk block address */
717
718 ASSERT(agno != NULLAGNUMBER);
719 ASSERT(agbno != NULLAGBLOCK);
720 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000721 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * Check for the cursor referring to the last block at the given level.
726 */
727int /* 1=is last block, 0=not last block */
728xfs_btree_islastblock(
729 xfs_btree_cur_t *cur, /* btree cursor */
730 int level) /* level to check */
731{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100732 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfs_buf_t *bp; /* buffer containing block */
734
735 block = xfs_btree_get_block(cur, level, &bp);
736 xfs_btree_check_block(cur, block, level, bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100737 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000738 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200740 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000744 * Change the cursor to point to the first record at the given level.
745 * Other levels are unaffected.
746 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100747STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000748xfs_btree_firstrec(
749 xfs_btree_cur_t *cur, /* btree cursor */
750 int level) /* level to change */
751{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100752 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000753 xfs_buf_t *bp; /* buffer containing block */
754
755 /*
756 * Get the block pointer for this level.
757 */
758 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong1e86eab2017-07-17 14:30:45 -0700759 if (xfs_btree_check_block(cur, block, level, bp))
760 return 0;
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000761 /*
762 * It's empty, there is no such record.
763 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100764 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000765 return 0;
766 /*
767 * Set the ptr value to 1, that's the first record/key.
768 */
769 cur->bc_ptrs[level] = 1;
770 return 1;
771}
772
773/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * Change the cursor to point to the last record in the current block
775 * at the given level. Other levels are unaffected.
776 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100777STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778xfs_btree_lastrec(
779 xfs_btree_cur_t *cur, /* btree cursor */
780 int level) /* level to change */
781{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100782 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 xfs_buf_t *bp; /* buffer containing block */
784
785 /*
786 * Get the block pointer for this level.
787 */
788 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong1e86eab2017-07-17 14:30:45 -0700789 if (xfs_btree_check_block(cur, block, level, bp))
790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /*
792 * It's empty, there is no such record.
793 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100794 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796 /*
797 * Set the ptr value to numrecs, that's the last record/key.
798 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100799 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 1;
801}
802
803/*
804 * Compute first and last byte offsets for the fields given.
805 * Interprets the offsets table, which contains struct field offsets.
806 */
807void
808xfs_btree_offsets(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700809 int64_t fields, /* bitmask of fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 const short *offsets, /* table of field offsets */
811 int nbits, /* number of bits to inspect */
812 int *first, /* output: first byte offset */
813 int *last) /* output: last byte offset */
814{
815 int i; /* current bit number */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700816 int64_t imask; /* mask for current bit number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 ASSERT(fields != 0);
819 /*
820 * Find the lowest bit, so the first byte offset.
821 */
822 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
823 if (imask & fields) {
824 *first = offsets[i];
825 break;
826 }
827 }
828 /*
829 * Find the highest bit, so the last byte offset.
830 */
831 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
832 if (imask & fields) {
833 *last = offsets[i + 1] - 1;
834 break;
835 }
836 }
837}
838
839/*
840 * Get a buffer for the block, return it read in.
841 * Long-form addressing.
842 */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100843int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100845 struct xfs_mount *mp, /* file system mount point */
846 struct xfs_trans *tp, /* transaction pointer */
847 xfs_fsblock_t fsbno, /* file system block number */
848 uint lock, /* lock flags for read_buf */
849 struct xfs_buf **bpp, /* buffer for fsbno */
850 int refval, /* ref count value for buffer */
Dave Chinner1813dd62012-11-14 17:54:40 +1100851 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100853 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100855 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800857 if (!xfs_verify_fsbno(mp, fsbno))
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -0800858 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100860 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Dave Chinner1813dd62012-11-14 17:54:40 +1100861 mp->m_bsize, lock, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return error;
Dave Chinner821eb212010-12-02 16:31:13 +1100864 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000865 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *bpp = bp;
867 return 0;
868}
869
870/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 * Read-ahead the block, don't wait for it, don't return a buffer.
872 * Long-form addressing.
873 */
874/* ARGSUSED */
875void
876xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100877 struct xfs_mount *mp, /* file system mount point */
878 xfs_fsblock_t fsbno, /* file system block number */
879 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100880 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 xfs_daddr_t d;
883
884 ASSERT(fsbno != NULLFSBLOCK);
885 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100886 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/*
890 * Read-ahead the block, don't wait for it, don't return a buffer.
891 * Short-form addressing.
892 */
893/* ARGSUSED */
894void
895xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100896 struct xfs_mount *mp, /* file system mount point */
897 xfs_agnumber_t agno, /* allocation group number */
898 xfs_agblock_t agbno, /* allocation group block number */
899 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100900 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 xfs_daddr_t d;
903
904 ASSERT(agno != NULLAGNUMBER);
905 ASSERT(agbno != NULLAGBLOCK);
906 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100907 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100910STATIC int
911xfs_btree_readahead_lblock(
912 struct xfs_btree_cur *cur,
913 int lr,
914 struct xfs_btree_block *block)
915{
916 int rval = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000917 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
918 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100919
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000920 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100921 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100922 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100923 rval++;
924 }
925
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000926 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100927 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100928 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100929 rval++;
930 }
931
932 return rval;
933}
934
935STATIC int
936xfs_btree_readahead_sblock(
937 struct xfs_btree_cur *cur,
938 int lr,
939 struct xfs_btree_block *block)
940{
941 int rval = 0;
942 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
943 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
944
945
946 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
947 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100948 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100949 rval++;
950 }
951
952 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
953 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100954 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100955 rval++;
956 }
957
958 return rval;
959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/*
962 * Read-ahead btree blocks, at the given level.
963 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
964 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100965STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100966xfs_btree_readahead(
967 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int lev, /* level in btree */
969 int lr) /* left/right bits */
970{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100971 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100973 /*
974 * No readahead needed if we are at the root level and the
975 * btree root is stored in the inode.
976 */
977 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
978 (lev == cur->bc_nlevels - 1))
979 return 0;
980
981 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
982 return 0;
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100985 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
986
987 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
988 return xfs_btree_readahead_lblock(cur, lr, block);
989 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700992STATIC int
Dave Chinner21b5c972013-08-30 10:23:44 +1000993xfs_btree_ptr_to_daddr(
994 struct xfs_btree_cur *cur,
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700995 union xfs_btree_ptr *ptr,
996 xfs_daddr_t *daddr)
Dave Chinner21b5c972013-08-30 10:23:44 +1000997{
Darrick J. Wonge63a1002018-06-04 13:58:34 -0700998 xfs_fsblock_t fsbno;
999 xfs_agblock_t agbno;
1000 int error;
1001
1002 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
1003 if (error)
1004 return error;
1005
Dave Chinner21b5c972013-08-30 10:23:44 +10001006 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001007 fsbno = be64_to_cpu(ptr->l);
1008 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, fsbno);
Dave Chinner21b5c972013-08-30 10:23:44 +10001009 } else {
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001010 agbno = be32_to_cpu(ptr->s);
1011 *daddr = XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
1012 agbno);
Dave Chinner21b5c972013-08-30 10:23:44 +10001013 }
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001014
1015 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10001016}
1017
1018/*
1019 * Readahead @count btree blocks at the given @ptr location.
1020 *
1021 * We don't need to care about long or short form btrees here as we have a
1022 * method of converting the ptr directly to a daddr available to us.
1023 */
1024STATIC void
1025xfs_btree_readahead_ptr(
1026 struct xfs_btree_cur *cur,
1027 union xfs_btree_ptr *ptr,
1028 xfs_extlen_t count)
1029{
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001030 xfs_daddr_t daddr;
1031
1032 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
1033 return;
1034 xfs_buf_readahead(cur->bc_mp->m_ddev_targp, daddr,
Dave Chinner21b5c972013-08-30 10:23:44 +10001035 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/*
1039 * Set the buffer for level "lev" in the cursor to bp, releasing
1040 * any previous buffer.
1041 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001042STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043xfs_btree_setbuf(
1044 xfs_btree_cur_t *cur, /* btree cursor */
1045 int lev, /* level in btree */
1046 xfs_buf_t *bp) /* new buffer to set */
1047{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001048 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001050 if (cur->bc_bufs[lev])
1051 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 cur->bc_bufs[lev] = bp;
1053 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +11001056 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001057 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001059 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1061 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001062 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001064 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1066 }
1067}
Christoph Hellwig637aa502008-10-30 16:55:45 +11001068
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07001069bool
Christoph Hellwig637aa502008-10-30 16:55:45 +11001070xfs_btree_ptr_is_null(
1071 struct xfs_btree_cur *cur,
1072 union xfs_btree_ptr *ptr)
1073{
1074 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001075 return ptr->l == cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001076 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001077 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001078}
1079
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001080STATIC void
1081xfs_btree_set_ptr_null(
1082 struct xfs_btree_cur *cur,
1083 union xfs_btree_ptr *ptr)
1084{
1085 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001086 ptr->l = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001087 else
1088 ptr->s = cpu_to_be32(NULLAGBLOCK);
1089}
1090
Christoph Hellwig637aa502008-10-30 16:55:45 +11001091/*
1092 * Get/set/init sibling pointers
1093 */
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07001094void
Christoph Hellwig637aa502008-10-30 16:55:45 +11001095xfs_btree_get_sibling(
1096 struct xfs_btree_cur *cur,
1097 struct xfs_btree_block *block,
1098 union xfs_btree_ptr *ptr,
1099 int lr)
1100{
1101 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1102
1103 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1104 if (lr == XFS_BB_RIGHTSIB)
1105 ptr->l = block->bb_u.l.bb_rightsib;
1106 else
1107 ptr->l = block->bb_u.l.bb_leftsib;
1108 } else {
1109 if (lr == XFS_BB_RIGHTSIB)
1110 ptr->s = block->bb_u.s.bb_rightsib;
1111 else
1112 ptr->s = block->bb_u.s.bb_leftsib;
1113 }
1114}
1115
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001116STATIC void
1117xfs_btree_set_sibling(
1118 struct xfs_btree_cur *cur,
1119 struct xfs_btree_block *block,
1120 union xfs_btree_ptr *ptr,
1121 int lr)
1122{
1123 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1124
1125 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1126 if (lr == XFS_BB_RIGHTSIB)
1127 block->bb_u.l.bb_rightsib = ptr->l;
1128 else
1129 block->bb_u.l.bb_leftsib = ptr->l;
1130 } else {
1131 if (lr == XFS_BB_RIGHTSIB)
1132 block->bb_u.s.bb_rightsib = ptr->s;
1133 else
1134 block->bb_u.s.bb_leftsib = ptr->s;
1135 }
1136}
1137
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001138void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001139xfs_btree_init_block_int(
1140 struct xfs_mount *mp,
1141 struct xfs_btree_block *buf,
1142 xfs_daddr_t blkno,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001143 xfs_btnum_t btnum,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001144 __u16 level,
1145 __u16 numrecs,
1146 __u64 owner,
1147 unsigned int flags)
1148{
Eric Sandeenf88ae462017-01-27 23:16:37 -08001149 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001150 __u32 magic = xfs_btree_magic(crc, btnum);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001151
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001152 buf->bb_magic = cpu_to_be32(magic);
1153 buf->bb_level = cpu_to_be16(level);
1154 buf->bb_numrecs = cpu_to_be16(numrecs);
1155
1156 if (flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001157 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1158 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001159 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001160 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1161 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001162 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001163 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001164 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001165 }
1166 } else {
1167 /* owner is a 32 bit value on short blocks */
1168 __u32 __owner = (__u32)owner;
1169
1170 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1171 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001172 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001173 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1174 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001175 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001176 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001177 }
1178 }
1179}
1180
1181void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001182xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001183 struct xfs_mount *mp,
1184 struct xfs_buf *bp,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001185 xfs_btnum_t btnum,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001186 __u16 level,
1187 __u16 numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001188 __u64 owner,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001189 unsigned int flags)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001190{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001191 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001192 btnum, level, numrecs, owner, flags);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001193}
1194
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001195STATIC void
1196xfs_btree_init_block_cur(
1197 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001198 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001199 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001200 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001201{
Eric Sandeenaf7d20f2017-01-27 23:16:38 -08001202 __u64 owner;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001203
1204 /*
1205 * we can pull the owner from the cursor right now as the different
1206 * owners align directly with the pointer size of the btree. This may
1207 * change in future, but is safe for current users of the generic btree
1208 * code.
1209 */
1210 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1211 owner = cur->bc_private.b.ip->i_ino;
1212 else
1213 owner = cur->bc_private.a.agno;
1214
1215 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001216 cur->bc_btnum, level, numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001217 owner, cur->bc_flags);
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001218}
1219
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001220/*
1221 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001222 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001223 * will be further refined in the update_lastrec method.
1224 */
1225STATIC int
1226xfs_btree_is_lastrec(
1227 struct xfs_btree_cur *cur,
1228 struct xfs_btree_block *block,
1229 int level)
1230{
1231 union xfs_btree_ptr ptr;
1232
1233 if (level > 0)
1234 return 0;
1235 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1236 return 0;
1237
1238 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1239 if (!xfs_btree_ptr_is_null(cur, &ptr))
1240 return 0;
1241 return 1;
1242}
1243
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001244STATIC void
1245xfs_btree_buf_to_ptr(
1246 struct xfs_btree_cur *cur,
1247 struct xfs_buf *bp,
1248 union xfs_btree_ptr *ptr)
1249{
1250 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1251 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1252 XFS_BUF_ADDR(bp)));
1253 else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001254 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001255 XFS_BUF_ADDR(bp)));
1256 }
1257}
1258
Christoph Hellwig637aa502008-10-30 16:55:45 +11001259STATIC void
1260xfs_btree_set_refs(
1261 struct xfs_btree_cur *cur,
1262 struct xfs_buf *bp)
1263{
1264 switch (cur->bc_btnum) {
1265 case XFS_BTNUM_BNO:
1266 case XFS_BTNUM_CNT:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001267 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001268 break;
1269 case XFS_BTNUM_INO:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001270 case XFS_BTNUM_FINO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001271 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001272 break;
1273 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001274 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001275 break;
Darrick J. Wong035e00a2016-08-03 11:36:07 +10001276 case XFS_BTNUM_RMAP:
1277 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1278 break;
Darrick J. Wong1946b912016-10-03 09:11:18 -07001279 case XFS_BTNUM_REFC:
1280 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1281 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001282 default:
1283 ASSERT(0);
1284 }
1285}
1286
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001287STATIC int
1288xfs_btree_get_buf_block(
1289 struct xfs_btree_cur *cur,
1290 union xfs_btree_ptr *ptr,
1291 int flags,
1292 struct xfs_btree_block **block,
1293 struct xfs_buf **bpp)
1294{
1295 struct xfs_mount *mp = cur->bc_mp;
1296 xfs_daddr_t d;
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001297 int error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001298
1299 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001300 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001301
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001302 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1303 if (error)
1304 return error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001305 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1306 mp->m_bsize, flags);
1307
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001308 if (!*bpp)
Dave Chinner24513372014-06-25 14:58:08 +10001309 return -ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001310
Dave Chinner1813dd62012-11-14 17:54:40 +11001311 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001312 *block = XFS_BUF_TO_BLOCK(*bpp);
1313 return 0;
1314}
1315
Christoph Hellwig637aa502008-10-30 16:55:45 +11001316/*
1317 * Read in the buffer at the given ptr and return the buffer and
1318 * the block pointer within the buffer.
1319 */
1320STATIC int
1321xfs_btree_read_buf_block(
1322 struct xfs_btree_cur *cur,
1323 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001324 int flags,
1325 struct xfs_btree_block **block,
1326 struct xfs_buf **bpp)
1327{
1328 struct xfs_mount *mp = cur->bc_mp;
1329 xfs_daddr_t d;
1330 int error;
1331
1332 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001333 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001334
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001335 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1336 if (error)
1337 return error;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001338 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001339 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001340 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001341 if (error)
1342 return error;
1343
Christoph Hellwig637aa502008-10-30 16:55:45 +11001344 xfs_btree_set_refs(cur, *bpp);
1345 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001346 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001347}
1348
1349/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001350 * Copy keys from one btree block to another.
1351 */
1352STATIC void
1353xfs_btree_copy_keys(
1354 struct xfs_btree_cur *cur,
1355 union xfs_btree_key *dst_key,
1356 union xfs_btree_key *src_key,
1357 int numkeys)
1358{
1359 ASSERT(numkeys >= 0);
1360 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1361}
1362
1363/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001364 * Copy records from one btree block to another.
1365 */
1366STATIC void
1367xfs_btree_copy_recs(
1368 struct xfs_btree_cur *cur,
1369 union xfs_btree_rec *dst_rec,
1370 union xfs_btree_rec *src_rec,
1371 int numrecs)
1372{
1373 ASSERT(numrecs >= 0);
1374 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1375}
1376
1377/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001378 * Copy block pointers from one btree block to another.
1379 */
1380STATIC void
1381xfs_btree_copy_ptrs(
1382 struct xfs_btree_cur *cur,
1383 union xfs_btree_ptr *dst_ptr,
1384 union xfs_btree_ptr *src_ptr,
1385 int numptrs)
1386{
1387 ASSERT(numptrs >= 0);
1388 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1389}
1390
1391/*
1392 * Shift keys one index left/right inside a single btree block.
1393 */
1394STATIC void
1395xfs_btree_shift_keys(
1396 struct xfs_btree_cur *cur,
1397 union xfs_btree_key *key,
1398 int dir,
1399 int numkeys)
1400{
1401 char *dst_key;
1402
1403 ASSERT(numkeys >= 0);
1404 ASSERT(dir == 1 || dir == -1);
1405
1406 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1407 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1408}
1409
1410/*
1411 * Shift records one index left/right inside a single btree block.
1412 */
1413STATIC void
1414xfs_btree_shift_recs(
1415 struct xfs_btree_cur *cur,
1416 union xfs_btree_rec *rec,
1417 int dir,
1418 int numrecs)
1419{
1420 char *dst_rec;
1421
1422 ASSERT(numrecs >= 0);
1423 ASSERT(dir == 1 || dir == -1);
1424
1425 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1426 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1427}
1428
1429/*
1430 * Shift block pointers one index left/right inside a single btree block.
1431 */
1432STATIC void
1433xfs_btree_shift_ptrs(
1434 struct xfs_btree_cur *cur,
1435 union xfs_btree_ptr *ptr,
1436 int dir,
1437 int numptrs)
1438{
1439 char *dst_ptr;
1440
1441 ASSERT(numptrs >= 0);
1442 ASSERT(dir == 1 || dir == -1);
1443
1444 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1445 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1446}
1447
1448/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001449 * Log key values from the btree block.
1450 */
1451STATIC void
1452xfs_btree_log_keys(
1453 struct xfs_btree_cur *cur,
1454 struct xfs_buf *bp,
1455 int first,
1456 int last)
1457{
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001458
1459 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001460 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001461 xfs_trans_log_buf(cur->bc_tp, bp,
1462 xfs_btree_key_offset(cur, first),
1463 xfs_btree_key_offset(cur, last + 1) - 1);
1464 } else {
1465 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1466 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1467 }
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001468}
1469
1470/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001471 * Log record values from the btree block.
1472 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001473void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001474xfs_btree_log_recs(
1475 struct xfs_btree_cur *cur,
1476 struct xfs_buf *bp,
1477 int first,
1478 int last)
1479{
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001480
Dave Chinner61fe1352013-04-03 16:11:30 +11001481 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001482 xfs_trans_log_buf(cur->bc_tp, bp,
1483 xfs_btree_rec_offset(cur, first),
1484 xfs_btree_rec_offset(cur, last + 1) - 1);
1485
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001486}
1487
1488/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001489 * Log block pointer fields from a btree block (nonleaf).
1490 */
1491STATIC void
1492xfs_btree_log_ptrs(
1493 struct xfs_btree_cur *cur, /* btree cursor */
1494 struct xfs_buf *bp, /* buffer containing btree block */
1495 int first, /* index of first pointer to log */
1496 int last) /* index of last pointer to log */
1497{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001498
1499 if (bp) {
1500 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1501 int level = xfs_btree_get_level(block);
1502
Dave Chinner61fe1352013-04-03 16:11:30 +11001503 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001504 xfs_trans_log_buf(cur->bc_tp, bp,
1505 xfs_btree_ptr_offset(cur, first, level),
1506 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1507 } else {
1508 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1509 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1510 }
1511
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001512}
1513
1514/*
1515 * Log fields from a btree block header.
1516 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001517void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001518xfs_btree_log_block(
1519 struct xfs_btree_cur *cur, /* btree cursor */
1520 struct xfs_buf *bp, /* buffer containing btree block */
1521 int fields) /* mask of fields: XFS_BB_... */
1522{
1523 int first; /* first byte offset logged */
1524 int last; /* last byte offset logged */
1525 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001526 offsetof(struct xfs_btree_block, bb_magic),
1527 offsetof(struct xfs_btree_block, bb_level),
1528 offsetof(struct xfs_btree_block, bb_numrecs),
1529 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1530 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001531 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1532 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1533 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1534 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1535 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1536 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001537 };
1538 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001539 offsetof(struct xfs_btree_block, bb_magic),
1540 offsetof(struct xfs_btree_block, bb_level),
1541 offsetof(struct xfs_btree_block, bb_numrecs),
1542 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1543 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001544 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1545 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1546 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1547 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1548 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1549 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1550 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001551 };
1552
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001553 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001554 int nbits;
1555
1556 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1557 /*
1558 * We don't log the CRC when updating a btree
1559 * block but instead recreate it during log
1560 * recovery. As the log buffers have checksums
1561 * of their own this is safe and avoids logging a crc
1562 * update in a lot of places.
1563 */
1564 if (fields == XFS_BB_ALL_BITS)
1565 fields = XFS_BB_ALL_BITS_CRC;
1566 nbits = XFS_BB_NUM_BITS_CRC;
1567 } else {
1568 nbits = XFS_BB_NUM_BITS;
1569 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001570 xfs_btree_offsets(fields,
1571 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1572 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001573 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001574 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001575 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1576 } else {
1577 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1578 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1579 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001580}
1581
1582/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001583 * Increment cursor by one record at the level.
1584 * For nonzero levels the leaf-ward information is untouched.
1585 */
1586int /* error */
1587xfs_btree_increment(
1588 struct xfs_btree_cur *cur,
1589 int level,
1590 int *stat) /* success/failure */
1591{
1592 struct xfs_btree_block *block;
1593 union xfs_btree_ptr ptr;
1594 struct xfs_buf *bp;
1595 int error; /* error return value */
1596 int lev;
1597
Christoph Hellwig637aa502008-10-30 16:55:45 +11001598 ASSERT(level < cur->bc_nlevels);
1599
1600 /* Read-ahead to the right at this level. */
1601 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1602
1603 /* Get a pointer to the btree block. */
1604 block = xfs_btree_get_block(cur, level, &bp);
1605
1606#ifdef DEBUG
1607 error = xfs_btree_check_block(cur, block, level, bp);
1608 if (error)
1609 goto error0;
1610#endif
1611
1612 /* We're done if we remain in the block after the increment. */
1613 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1614 goto out1;
1615
1616 /* Fail if we just went off the right edge of the tree. */
1617 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1618 if (xfs_btree_ptr_is_null(cur, &ptr))
1619 goto out0;
1620
1621 XFS_BTREE_STATS_INC(cur, increment);
1622
1623 /*
1624 * March up the tree incrementing pointers.
1625 * Stop when we don't go off the right edge of a block.
1626 */
1627 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1628 block = xfs_btree_get_block(cur, lev, &bp);
1629
1630#ifdef DEBUG
1631 error = xfs_btree_check_block(cur, block, lev, bp);
1632 if (error)
1633 goto error0;
1634#endif
1635
1636 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1637 break;
1638
1639 /* Read-ahead the right block for the next loop. */
1640 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1641 }
1642
1643 /*
1644 * If we went off the root then we are either seriously
1645 * confused or have the tree root in an inode.
1646 */
1647 if (lev == cur->bc_nlevels) {
1648 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1649 goto out0;
1650 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001651 error = -EFSCORRUPTED;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001652 goto error0;
1653 }
1654 ASSERT(lev < cur->bc_nlevels);
1655
1656 /*
1657 * Now walk back down the tree, fixing up the cursor's buffer
1658 * pointers and key numbers.
1659 */
1660 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1661 union xfs_btree_ptr *ptrp;
1662
1663 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001664 --lev;
1665 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001666 if (error)
1667 goto error0;
1668
1669 xfs_btree_setbuf(cur, lev, bp);
1670 cur->bc_ptrs[lev] = 1;
1671 }
1672out1:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001673 *stat = 1;
1674 return 0;
1675
1676out0:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001677 *stat = 0;
1678 return 0;
1679
1680error0:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001681 return error;
1682}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001683
1684/*
1685 * Decrement cursor by one record at the level.
1686 * For nonzero levels the leaf-ward information is untouched.
1687 */
1688int /* error */
1689xfs_btree_decrement(
1690 struct xfs_btree_cur *cur,
1691 int level,
1692 int *stat) /* success/failure */
1693{
1694 struct xfs_btree_block *block;
1695 xfs_buf_t *bp;
1696 int error; /* error return value */
1697 int lev;
1698 union xfs_btree_ptr ptr;
1699
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001700 ASSERT(level < cur->bc_nlevels);
1701
1702 /* Read-ahead to the left at this level. */
1703 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1704
1705 /* We're done if we remain in the block after the decrement. */
1706 if (--cur->bc_ptrs[level] > 0)
1707 goto out1;
1708
1709 /* Get a pointer to the btree block. */
1710 block = xfs_btree_get_block(cur, level, &bp);
1711
1712#ifdef DEBUG
1713 error = xfs_btree_check_block(cur, block, level, bp);
1714 if (error)
1715 goto error0;
1716#endif
1717
1718 /* Fail if we just went off the left edge of the tree. */
1719 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1720 if (xfs_btree_ptr_is_null(cur, &ptr))
1721 goto out0;
1722
1723 XFS_BTREE_STATS_INC(cur, decrement);
1724
1725 /*
1726 * March up the tree decrementing pointers.
1727 * Stop when we don't go off the left edge of a block.
1728 */
1729 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1730 if (--cur->bc_ptrs[lev] > 0)
1731 break;
1732 /* Read-ahead the left block for the next loop. */
1733 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1734 }
1735
1736 /*
1737 * If we went off the root then we are seriously confused.
1738 * or the root of the tree is in an inode.
1739 */
1740 if (lev == cur->bc_nlevels) {
1741 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1742 goto out0;
1743 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001744 error = -EFSCORRUPTED;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001745 goto error0;
1746 }
1747 ASSERT(lev < cur->bc_nlevels);
1748
1749 /*
1750 * Now walk back down the tree, fixing up the cursor's buffer
1751 * pointers and key numbers.
1752 */
1753 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1754 union xfs_btree_ptr *ptrp;
1755
1756 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001757 --lev;
1758 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001759 if (error)
1760 goto error0;
1761 xfs_btree_setbuf(cur, lev, bp);
1762 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1763 }
1764out1:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001765 *stat = 1;
1766 return 0;
1767
1768out0:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001769 *stat = 0;
1770 return 0;
1771
1772error0:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001773 return error;
1774}
1775
Darrick J. Wong26788092017-06-16 11:00:07 -07001776int
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001777xfs_btree_lookup_get_block(
1778 struct xfs_btree_cur *cur, /* btree cursor */
1779 int level, /* level in the btree */
1780 union xfs_btree_ptr *pp, /* ptr to btree block */
1781 struct xfs_btree_block **blkp) /* return btree block */
1782{
1783 struct xfs_buf *bp; /* buffer pointer for btree block */
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001784 xfs_daddr_t daddr;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001785 int error = 0;
1786
1787 /* special case the root block if in an inode */
1788 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1789 (level == cur->bc_nlevels - 1)) {
1790 *blkp = xfs_btree_get_iroot(cur);
1791 return 0;
1792 }
1793
1794 /*
1795 * If the old buffer at this level for the disk address we are
1796 * looking for re-use it.
1797 *
1798 * Otherwise throw it away and get a new one.
1799 */
1800 bp = cur->bc_bufs[level];
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001801 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1802 if (error)
1803 return error;
1804 if (bp && XFS_BUF_ADDR(bp) == daddr) {
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001805 *blkp = XFS_BUF_TO_BLOCK(bp);
1806 return 0;
1807 }
1808
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001809 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001810 if (error)
1811 return error;
1812
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001813 /* Check the inode owner since the verifiers don't. */
1814 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
Brian Foster99c794c2017-08-29 10:08:39 -07001815 !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) &&
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001816 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1817 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1818 cur->bc_private.b.ip->i_ino)
1819 goto out_bad;
1820
1821 /* Did we get the level we were looking for? */
1822 if (be16_to_cpu((*blkp)->bb_level) != level)
1823 goto out_bad;
1824
1825 /* Check that internal nodes have at least one record. */
1826 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1827 goto out_bad;
1828
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001829 xfs_btree_setbuf(cur, level, bp);
1830 return 0;
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001831
1832out_bad:
1833 *blkp = NULL;
1834 xfs_trans_brelse(cur->bc_tp, bp);
1835 return -EFSCORRUPTED;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001836}
1837
1838/*
1839 * Get current search key. For level 0 we don't actually have a key
1840 * structure so we make one up from the record. For all other levels
1841 * we just return the right key.
1842 */
1843STATIC union xfs_btree_key *
1844xfs_lookup_get_search_key(
1845 struct xfs_btree_cur *cur,
1846 int level,
1847 int keyno,
1848 struct xfs_btree_block *block,
1849 union xfs_btree_key *kp)
1850{
1851 if (level == 0) {
1852 cur->bc_ops->init_key_from_rec(kp,
1853 xfs_btree_rec_addr(cur, keyno, block));
1854 return kp;
1855 }
1856
1857 return xfs_btree_key_addr(cur, keyno, block);
1858}
1859
1860/*
1861 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001862 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001863 */
1864int /* error */
1865xfs_btree_lookup(
1866 struct xfs_btree_cur *cur, /* btree cursor */
1867 xfs_lookup_t dir, /* <=, ==, or >= */
1868 int *stat) /* success/failure */
1869{
1870 struct xfs_btree_block *block; /* current btree block */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001871 int64_t diff; /* difference for the current key */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001872 int error; /* error return value */
1873 int keyno; /* current key number */
1874 int level; /* level in the btree */
1875 union xfs_btree_ptr *pp; /* ptr to btree block */
1876 union xfs_btree_ptr ptr; /* ptr to btree block */
1877
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001878 XFS_BTREE_STATS_INC(cur, lookup);
1879
Darrick J. Wonged150e12016-08-26 15:58:40 +10001880 /* No such thing as a zero-level tree. */
1881 if (cur->bc_nlevels == 0)
1882 return -EFSCORRUPTED;
1883
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001884 block = NULL;
1885 keyno = 0;
1886
1887 /* initialise start pointer from cursor */
1888 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1889 pp = &ptr;
1890
1891 /*
1892 * Iterate over each level in the btree, starting at the root.
1893 * For each level above the leaves, find the key we need, based
1894 * on the lookup record, then follow the corresponding block
1895 * pointer down to the next level.
1896 */
1897 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1898 /* Get the block we need to do the lookup on. */
1899 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1900 if (error)
1901 goto error0;
1902
1903 if (diff == 0) {
1904 /*
1905 * If we already had a key match at a higher level, we
1906 * know we need to use the first entry in this block.
1907 */
1908 keyno = 1;
1909 } else {
1910 /* Otherwise search this block. Do a binary search. */
1911
1912 int high; /* high entry number */
1913 int low; /* low entry number */
1914
1915 /* Set low and high entry numbers, 1-based. */
1916 low = 1;
1917 high = xfs_btree_get_numrecs(block);
1918 if (!high) {
1919 /* Block is empty, must be an empty leaf. */
Darrick J. Wongeeee0d62018-06-03 16:10:14 -07001920 if (level != 0 || cur->bc_nlevels != 1) {
1921 XFS_CORRUPTION_ERROR(__func__,
1922 XFS_ERRLEVEL_LOW,
Darrick J. Wong2551a532018-06-04 10:23:54 -07001923 cur->bc_mp, block,
1924 sizeof(*block));
Darrick J. Wongeeee0d62018-06-03 16:10:14 -07001925 return -EFSCORRUPTED;
1926 }
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001927
1928 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001929 *stat = 0;
1930 return 0;
1931 }
1932
1933 /* Binary search the block. */
1934 while (low <= high) {
1935 union xfs_btree_key key;
1936 union xfs_btree_key *kp;
1937
1938 XFS_BTREE_STATS_INC(cur, compare);
1939
1940 /* keyno is average of low and high. */
1941 keyno = (low + high) >> 1;
1942
1943 /* Get current search key */
1944 kp = xfs_lookup_get_search_key(cur, level,
1945 keyno, block, &key);
1946
1947 /*
1948 * Compute difference to get next direction:
1949 * - less than, move right
1950 * - greater than, move left
1951 * - equal, we're done
1952 */
1953 diff = cur->bc_ops->key_diff(cur, kp);
1954 if (diff < 0)
1955 low = keyno + 1;
1956 else if (diff > 0)
1957 high = keyno - 1;
1958 else
1959 break;
1960 }
1961 }
1962
1963 /*
1964 * If there are more levels, set up for the next level
1965 * by getting the block number and filling in the cursor.
1966 */
1967 if (level > 0) {
1968 /*
1969 * If we moved left, need the previous key number,
1970 * unless there isn't one.
1971 */
1972 if (diff > 0 && --keyno < 1)
1973 keyno = 1;
1974 pp = xfs_btree_ptr_addr(cur, keyno, block);
1975
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07001976 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001977 if (error)
1978 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07001979
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001980 cur->bc_ptrs[level] = keyno;
1981 }
1982 }
1983
1984 /* Done with the search. See if we need to adjust the results. */
1985 if (dir != XFS_LOOKUP_LE && diff < 0) {
1986 keyno++;
1987 /*
1988 * If ge search and we went off the end of the block, but it's
1989 * not the last block, we're in the wrong block.
1990 */
1991 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1992 if (dir == XFS_LOOKUP_GE &&
1993 keyno > xfs_btree_get_numrecs(block) &&
1994 !xfs_btree_ptr_is_null(cur, &ptr)) {
1995 int i;
1996
1997 cur->bc_ptrs[0] = keyno;
1998 error = xfs_btree_increment(cur, 0, &i);
1999 if (error)
2000 goto error0;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11002001 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11002002 *stat = 1;
2003 return 0;
2004 }
2005 } else if (dir == XFS_LOOKUP_LE && diff > 0)
2006 keyno--;
2007 cur->bc_ptrs[0] = keyno;
2008
2009 /* Return if we succeeded or not. */
2010 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
2011 *stat = 0;
2012 else if (dir != XFS_LOOKUP_EQ || diff == 0)
2013 *stat = 1;
2014 else
2015 *stat = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11002016 return 0;
2017
2018error0:
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11002019 return error;
2020}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002021
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002022/* Find the high key storage area from a regular key. */
Darrick J. Wong2fdbec52017-10-25 15:03:46 -07002023union xfs_btree_key *
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002024xfs_btree_high_key_from_key(
2025 struct xfs_btree_cur *cur,
2026 union xfs_btree_key *key)
2027{
2028 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2029 return (union xfs_btree_key *)((char *)key +
2030 (cur->bc_ops->key_len / 2));
2031}
2032
Darrick J. Wong973b8312016-08-03 12:22:12 +10002033/* Determine the low (and high if overlapped) keys of a leaf block */
2034STATIC void
2035xfs_btree_get_leaf_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002036 struct xfs_btree_cur *cur,
2037 struct xfs_btree_block *block,
2038 union xfs_btree_key *key)
2039{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002040 union xfs_btree_key max_hkey;
2041 union xfs_btree_key hkey;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002042 union xfs_btree_rec *rec;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002043 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002044 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002045
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002046 rec = xfs_btree_rec_addr(cur, 1, block);
2047 cur->bc_ops->init_key_from_rec(key, rec);
2048
Darrick J. Wong973b8312016-08-03 12:22:12 +10002049 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002050
Darrick J. Wong973b8312016-08-03 12:22:12 +10002051 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2052 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2053 rec = xfs_btree_rec_addr(cur, n, block);
2054 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2055 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2056 > 0)
2057 max_hkey = hkey;
2058 }
2059
2060 high = xfs_btree_high_key_from_key(cur, key);
2061 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2062 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002063}
2064
Darrick J. Wong973b8312016-08-03 12:22:12 +10002065/* Determine the low (and high if overlapped) keys of a node block */
2066STATIC void
2067xfs_btree_get_node_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002068 struct xfs_btree_cur *cur,
2069 struct xfs_btree_block *block,
2070 union xfs_btree_key *key)
2071{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002072 union xfs_btree_key *hkey;
2073 union xfs_btree_key *max_hkey;
2074 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002075 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002076
Darrick J. Wong973b8312016-08-03 12:22:12 +10002077 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2078 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2079 cur->bc_ops->key_len / 2);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002080
Darrick J. Wong973b8312016-08-03 12:22:12 +10002081 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2082 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2083 hkey = xfs_btree_high_key_addr(cur, n, block);
2084 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2085 max_hkey = hkey;
2086 }
2087
2088 high = xfs_btree_high_key_from_key(cur, key);
2089 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2090 } else {
2091 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2092 cur->bc_ops->key_len);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002093 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002094}
2095
Darrick J. Wong70b22652016-08-03 11:03:38 +10002096/* Derive the keys for any btree block. */
Darrick J. Wong2fdbec52017-10-25 15:03:46 -07002097void
Darrick J. Wong70b22652016-08-03 11:03:38 +10002098xfs_btree_get_keys(
2099 struct xfs_btree_cur *cur,
2100 struct xfs_btree_block *block,
2101 union xfs_btree_key *key)
2102{
2103 if (be16_to_cpu(block->bb_level) == 0)
Darrick J. Wong973b8312016-08-03 12:22:12 +10002104 xfs_btree_get_leaf_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002105 else
Darrick J. Wong973b8312016-08-03 12:22:12 +10002106 xfs_btree_get_node_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002107}
2108
2109/*
2110 * Decide if we need to update the parent keys of a btree block. For
2111 * a standard btree this is only necessary if we're updating the first
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002112 * record/key. For an overlapping btree, we must always update the
2113 * keys because the highest key can be in any of the records or keys
2114 * in the block.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002115 */
2116static inline bool
2117xfs_btree_needs_key_update(
2118 struct xfs_btree_cur *cur,
2119 int ptr)
2120{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002121 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2122}
2123
2124/*
2125 * Update the low and high parent keys of the given level, progressing
2126 * towards the root. If force_all is false, stop if the keys for a given
2127 * level do not need updating.
2128 */
2129STATIC int
2130__xfs_btree_updkeys(
2131 struct xfs_btree_cur *cur,
2132 int level,
2133 struct xfs_btree_block *block,
2134 struct xfs_buf *bp0,
2135 bool force_all)
2136{
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002137 union xfs_btree_key key; /* keys from current level */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002138 union xfs_btree_key *lkey; /* keys from the next level up */
2139 union xfs_btree_key *hkey;
2140 union xfs_btree_key *nlkey; /* keys from the next level up */
2141 union xfs_btree_key *nhkey;
2142 struct xfs_buf *bp;
2143 int ptr;
2144
2145 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2146
2147 /* Exit if there aren't any parent levels to update. */
2148 if (level + 1 >= cur->bc_nlevels)
2149 return 0;
2150
2151 trace_xfs_btree_updkeys(cur, level, bp0);
2152
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002153 lkey = &key;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002154 hkey = xfs_btree_high_key_from_key(cur, lkey);
2155 xfs_btree_get_keys(cur, block, lkey);
2156 for (level++; level < cur->bc_nlevels; level++) {
2157#ifdef DEBUG
2158 int error;
2159#endif
2160 block = xfs_btree_get_block(cur, level, &bp);
2161 trace_xfs_btree_updkeys(cur, level, bp);
2162#ifdef DEBUG
2163 error = xfs_btree_check_block(cur, block, level, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002164 if (error)
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002165 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002166#endif
2167 ptr = cur->bc_ptrs[level];
2168 nlkey = xfs_btree_key_addr(cur, ptr, block);
2169 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2170 if (!force_all &&
2171 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2172 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2173 break;
2174 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2175 xfs_btree_log_keys(cur, bp, ptr, ptr);
2176 if (level + 1 >= cur->bc_nlevels)
2177 break;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002178 xfs_btree_get_node_keys(cur, block, lkey);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002179 }
2180
2181 return 0;
2182}
2183
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002184/* Update all the keys from some level in cursor back to the root. */
2185STATIC int
2186xfs_btree_updkeys_force(
2187 struct xfs_btree_cur *cur,
2188 int level)
2189{
2190 struct xfs_buf *bp;
2191 struct xfs_btree_block *block;
2192
2193 block = xfs_btree_get_block(cur, level, &bp);
2194 return __xfs_btree_updkeys(cur, level, block, bp, true);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002195}
2196
2197/*
2198 * Update the parent keys of the given level, progressing towards the root.
2199 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002200STATIC int
Darrick J. Wong70b22652016-08-03 11:03:38 +10002201xfs_btree_update_keys(
2202 struct xfs_btree_cur *cur,
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002203 int level)
2204{
2205 struct xfs_btree_block *block;
2206 struct xfs_buf *bp;
2207 union xfs_btree_key *kp;
Darrick J. Wong70b22652016-08-03 11:03:38 +10002208 union xfs_btree_key key;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002209 int ptr;
2210
Darrick J. Wong973b8312016-08-03 12:22:12 +10002211 ASSERT(level >= 0);
2212
2213 block = xfs_btree_get_block(cur, level, &bp);
2214 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2215 return __xfs_btree_updkeys(cur, level, block, bp, false);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002216
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002217 /*
2218 * Go up the tree from this level toward the root.
2219 * At each level, update the key value to the value input.
2220 * Stop when we reach a level where the cursor isn't pointing
2221 * at the first entry in the block.
2222 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002223 xfs_btree_get_keys(cur, block, &key);
2224 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002225#ifdef DEBUG
2226 int error;
2227#endif
2228 block = xfs_btree_get_block(cur, level, &bp);
2229#ifdef DEBUG
2230 error = xfs_btree_check_block(cur, block, level, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002231 if (error)
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002232 return error;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002233#endif
2234 ptr = cur->bc_ptrs[level];
2235 kp = xfs_btree_key_addr(cur, ptr, block);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002236 xfs_btree_copy_keys(cur, kp, &key, 1);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002237 xfs_btree_log_keys(cur, bp, ptr, ptr);
2238 }
2239
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002240 return 0;
2241}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002242
2243/*
2244 * Update the record referred to by cur to the value in the
2245 * given record. This either works (return 0) or gets an
2246 * EFSCORRUPTED error.
2247 */
2248int
2249xfs_btree_update(
2250 struct xfs_btree_cur *cur,
2251 union xfs_btree_rec *rec)
2252{
2253 struct xfs_btree_block *block;
2254 struct xfs_buf *bp;
2255 int error;
2256 int ptr;
2257 union xfs_btree_rec *rp;
2258
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002259 /* Pick up the current block. */
2260 block = xfs_btree_get_block(cur, 0, &bp);
2261
2262#ifdef DEBUG
2263 error = xfs_btree_check_block(cur, block, 0, bp);
2264 if (error)
2265 goto error0;
2266#endif
2267 /* Get the address of the rec to be updated. */
2268 ptr = cur->bc_ptrs[0];
2269 rp = xfs_btree_rec_addr(cur, ptr, block);
2270
2271 /* Fill in the new contents and log them. */
2272 xfs_btree_copy_recs(cur, rp, rec, 1);
2273 xfs_btree_log_recs(cur, bp, ptr, ptr);
2274
2275 /*
2276 * If we are tracking the last record in the tree and
2277 * we are at the far right edge of the tree, update it.
2278 */
2279 if (xfs_btree_is_lastrec(cur, block, 0)) {
2280 cur->bc_ops->update_lastrec(cur, block, rec,
2281 ptr, LASTREC_UPDATE);
2282 }
2283
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002284 /* Pass new key value up to our parent. */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002285 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002286 error = xfs_btree_update_keys(cur, 0);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002287 if (error)
2288 goto error0;
2289 }
2290
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002291 return 0;
2292
2293error0:
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002294 return error;
2295}
2296
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002297/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11002298 * Move 1 record left from cur/level if possible.
2299 * Update cur to reflect the new path.
2300 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002301STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002302xfs_btree_lshift(
2303 struct xfs_btree_cur *cur,
2304 int level,
2305 int *stat) /* success/failure */
2306{
Christoph Hellwig687b8902008-10-30 16:56:53 +11002307 struct xfs_buf *lbp; /* left buffer pointer */
2308 struct xfs_btree_block *left; /* left btree block */
2309 int lrecs; /* left record count */
2310 struct xfs_buf *rbp; /* right buffer pointer */
2311 struct xfs_btree_block *right; /* right btree block */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002312 struct xfs_btree_cur *tcur; /* temporary btree cursor */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002313 int rrecs; /* right record count */
2314 union xfs_btree_ptr lptr; /* left btree pointer */
2315 union xfs_btree_key *rkp = NULL; /* right btree key */
2316 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2317 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2318 int error; /* error return value */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002319 int i;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002320
Christoph Hellwig687b8902008-10-30 16:56:53 +11002321 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2322 level == cur->bc_nlevels - 1)
2323 goto out0;
2324
2325 /* Set up variables for this block as "right". */
2326 right = xfs_btree_get_block(cur, level, &rbp);
2327
2328#ifdef DEBUG
2329 error = xfs_btree_check_block(cur, right, level, rbp);
2330 if (error)
2331 goto error0;
2332#endif
2333
2334 /* If we've got no left sibling then we can't shift an entry left. */
2335 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2336 if (xfs_btree_ptr_is_null(cur, &lptr))
2337 goto out0;
2338
2339 /*
2340 * If the cursor entry is the one that would be moved, don't
2341 * do it... it's too complicated.
2342 */
2343 if (cur->bc_ptrs[level] <= 1)
2344 goto out0;
2345
2346 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002347 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002348 if (error)
2349 goto error0;
2350
2351 /* If it's full, it can't take another entry. */
2352 lrecs = xfs_btree_get_numrecs(left);
2353 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2354 goto out0;
2355
2356 rrecs = xfs_btree_get_numrecs(right);
2357
2358 /*
2359 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002360 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002361 * later.
2362 */
2363 lrecs++;
2364 rrecs--;
2365
2366 XFS_BTREE_STATS_INC(cur, lshift);
2367 XFS_BTREE_STATS_ADD(cur, moves, 1);
2368
2369 /*
2370 * If non-leaf, copy a key and a ptr to the left block.
2371 * Log the changes to the left block.
2372 */
2373 if (level > 0) {
2374 /* It's a non-leaf. Move keys and pointers. */
2375 union xfs_btree_key *lkp; /* left btree key */
2376 union xfs_btree_ptr *lpp; /* left address pointer */
2377
2378 lkp = xfs_btree_key_addr(cur, lrecs, left);
2379 rkp = xfs_btree_key_addr(cur, 1, right);
2380
2381 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2382 rpp = xfs_btree_ptr_addr(cur, 1, right);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002383
2384 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002385 if (error)
2386 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002387
Christoph Hellwig687b8902008-10-30 16:56:53 +11002388 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2389 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2390
2391 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2392 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2393
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002394 ASSERT(cur->bc_ops->keys_inorder(cur,
2395 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002396 } else {
2397 /* It's a leaf. Move records. */
2398 union xfs_btree_rec *lrp; /* left record pointer */
2399
2400 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2401 rrp = xfs_btree_rec_addr(cur, 1, right);
2402
2403 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2404 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2405
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002406 ASSERT(cur->bc_ops->recs_inorder(cur,
2407 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002408 }
2409
2410 xfs_btree_set_numrecs(left, lrecs);
2411 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2412
2413 xfs_btree_set_numrecs(right, rrecs);
2414 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2415
2416 /*
2417 * Slide the contents of right down one entry.
2418 */
2419 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2420 if (level > 0) {
2421 /* It's a nonleaf. operate on keys and ptrs */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002422 int i; /* loop index */
2423
2424 for (i = 0; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002425 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002426 if (error)
2427 goto error0;
2428 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002429
Christoph Hellwig687b8902008-10-30 16:56:53 +11002430 xfs_btree_shift_keys(cur,
2431 xfs_btree_key_addr(cur, 2, right),
2432 -1, rrecs);
2433 xfs_btree_shift_ptrs(cur,
2434 xfs_btree_ptr_addr(cur, 2, right),
2435 -1, rrecs);
2436
2437 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2438 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2439 } else {
2440 /* It's a leaf. operate on records */
2441 xfs_btree_shift_recs(cur,
2442 xfs_btree_rec_addr(cur, 2, right),
2443 -1, rrecs);
2444 xfs_btree_log_recs(cur, rbp, 1, rrecs);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002445 }
2446
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002447 /*
2448 * Using a temporary cursor, update the parent key values of the
2449 * block on the left.
2450 */
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002451 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2452 error = xfs_btree_dup_cursor(cur, &tcur);
2453 if (error)
2454 goto error0;
2455 i = xfs_btree_firstrec(tcur, level);
2456 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002457
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002458 error = xfs_btree_decrement(tcur, level, &i);
2459 if (error)
2460 goto error1;
2461
2462 /* Update the parent high keys of the left block, if needed. */
2463 error = xfs_btree_update_keys(tcur, level);
2464 if (error)
2465 goto error1;
2466
2467 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2468 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002469
Darrick J. Wong70b22652016-08-03 11:03:38 +10002470 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002471 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002472 if (error)
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002473 goto error0;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002474
2475 /* Slide the cursor value left one. */
2476 cur->bc_ptrs[level]--;
2477
Christoph Hellwig687b8902008-10-30 16:56:53 +11002478 *stat = 1;
2479 return 0;
2480
2481out0:
Christoph Hellwig687b8902008-10-30 16:56:53 +11002482 *stat = 0;
2483 return 0;
2484
2485error0:
Christoph Hellwig687b8902008-10-30 16:56:53 +11002486 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002487
2488error1:
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002489 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2490 return error;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002491}
2492
2493/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002494 * Move 1 record right from cur/level if possible.
2495 * Update cur to reflect the new path.
2496 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002497STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002498xfs_btree_rshift(
2499 struct xfs_btree_cur *cur,
2500 int level,
2501 int *stat) /* success/failure */
2502{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002503 struct xfs_buf *lbp; /* left buffer pointer */
2504 struct xfs_btree_block *left; /* left btree block */
2505 struct xfs_buf *rbp; /* right buffer pointer */
2506 struct xfs_btree_block *right; /* right btree block */
2507 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2508 union xfs_btree_ptr rptr; /* right block pointer */
2509 union xfs_btree_key *rkp; /* right btree key */
2510 int rrecs; /* right record count */
2511 int lrecs; /* left record count */
2512 int error; /* error return value */
2513 int i; /* loop counter */
2514
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002515 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2516 (level == cur->bc_nlevels - 1))
2517 goto out0;
2518
2519 /* Set up variables for this block as "left". */
2520 left = xfs_btree_get_block(cur, level, &lbp);
2521
2522#ifdef DEBUG
2523 error = xfs_btree_check_block(cur, left, level, lbp);
2524 if (error)
2525 goto error0;
2526#endif
2527
2528 /* If we've got no right sibling then we can't shift an entry right. */
2529 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2530 if (xfs_btree_ptr_is_null(cur, &rptr))
2531 goto out0;
2532
2533 /*
2534 * If the cursor entry is the one that would be moved, don't
2535 * do it... it's too complicated.
2536 */
2537 lrecs = xfs_btree_get_numrecs(left);
2538 if (cur->bc_ptrs[level] >= lrecs)
2539 goto out0;
2540
2541 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002542 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002543 if (error)
2544 goto error0;
2545
2546 /* If it's full, it can't take another entry. */
2547 rrecs = xfs_btree_get_numrecs(right);
2548 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2549 goto out0;
2550
2551 XFS_BTREE_STATS_INC(cur, rshift);
2552 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2553
2554 /*
2555 * Make a hole at the start of the right neighbor block, then
2556 * copy the last left block entry to the hole.
2557 */
2558 if (level > 0) {
2559 /* It's a nonleaf. make a hole in the keys and ptrs */
2560 union xfs_btree_key *lkp;
2561 union xfs_btree_ptr *lpp;
2562 union xfs_btree_ptr *rpp;
2563
2564 lkp = xfs_btree_key_addr(cur, lrecs, left);
2565 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2566 rkp = xfs_btree_key_addr(cur, 1, right);
2567 rpp = xfs_btree_ptr_addr(cur, 1, right);
2568
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002569 for (i = rrecs - 1; i >= 0; i--) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002570 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002571 if (error)
2572 goto error0;
2573 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002574
2575 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2576 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2577
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002578 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002579 if (error)
2580 goto error0;
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002581
2582 /* Now put the new data in, and log it. */
2583 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2584 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2585
2586 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2587 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2588
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002589 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2590 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002591 } else {
2592 /* It's a leaf. make a hole in the records */
2593 union xfs_btree_rec *lrp;
2594 union xfs_btree_rec *rrp;
2595
2596 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2597 rrp = xfs_btree_rec_addr(cur, 1, right);
2598
2599 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2600
2601 /* Now put the new data in, and log it. */
2602 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2603 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002604 }
2605
2606 /*
2607 * Decrement and log left's numrecs, bump and log right's numrecs.
2608 */
2609 xfs_btree_set_numrecs(left, --lrecs);
2610 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2611
2612 xfs_btree_set_numrecs(right, ++rrecs);
2613 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2614
2615 /*
2616 * Using a temporary cursor, update the parent key values of the
2617 * block on the right.
2618 */
2619 error = xfs_btree_dup_cursor(cur, &tcur);
2620 if (error)
2621 goto error0;
2622 i = xfs_btree_lastrec(tcur, level);
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002623 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002624
2625 error = xfs_btree_increment(tcur, level, &i);
2626 if (error)
2627 goto error1;
2628
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002629 /* Update the parent high keys of the left block, if needed. */
2630 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002631 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002632 if (error)
2633 goto error1;
2634 }
2635
Darrick J. Wong70b22652016-08-03 11:03:38 +10002636 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002637 error = xfs_btree_update_keys(tcur, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002638 if (error)
2639 goto error1;
2640
2641 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2642
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002643 *stat = 1;
2644 return 0;
2645
2646out0:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002647 *stat = 0;
2648 return 0;
2649
2650error0:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002651 return error;
2652
2653error1:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002654 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2655 return error;
2656}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002657
2658/*
2659 * Split cur/level block in half.
2660 * Return new block number and the key to its first
2661 * record (to be inserted into parent).
2662 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002663STATIC int /* error */
Dave Chinnercf11da92014-07-15 07:08:24 +10002664__xfs_btree_split(
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002665 struct xfs_btree_cur *cur,
2666 int level,
2667 union xfs_btree_ptr *ptrp,
2668 union xfs_btree_key *key,
2669 struct xfs_btree_cur **curp,
2670 int *stat) /* success/failure */
2671{
2672 union xfs_btree_ptr lptr; /* left sibling block ptr */
2673 struct xfs_buf *lbp; /* left buffer pointer */
2674 struct xfs_btree_block *left; /* left btree block */
2675 union xfs_btree_ptr rptr; /* right sibling block ptr */
2676 struct xfs_buf *rbp; /* right buffer pointer */
2677 struct xfs_btree_block *right; /* right btree block */
2678 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2679 struct xfs_buf *rrbp; /* right-right buffer pointer */
2680 struct xfs_btree_block *rrblock; /* right-right btree block */
2681 int lrecs;
2682 int rrecs;
2683 int src_index;
2684 int error; /* error return value */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002685 int i;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002686
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002687 XFS_BTREE_STATS_INC(cur, split);
2688
2689 /* Set up left block (current one). */
2690 left = xfs_btree_get_block(cur, level, &lbp);
2691
2692#ifdef DEBUG
2693 error = xfs_btree_check_block(cur, left, level, lbp);
2694 if (error)
2695 goto error0;
2696#endif
2697
2698 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2699
2700 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002701 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002702 if (error)
2703 goto error0;
2704 if (*stat == 0)
2705 goto out0;
2706 XFS_BTREE_STATS_INC(cur, alloc);
2707
2708 /* Set up the new block as "right". */
2709 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2710 if (error)
2711 goto error0;
2712
2713 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002714 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002715
2716 /*
2717 * Split the entries between the old and the new block evenly.
2718 * Make sure that if there's an odd number of entries now, that
2719 * each new block will have the same number of entries.
2720 */
2721 lrecs = xfs_btree_get_numrecs(left);
2722 rrecs = lrecs / 2;
2723 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2724 rrecs++;
2725 src_index = (lrecs - rrecs + 1);
2726
2727 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2728
Darrick J. Wong70b22652016-08-03 11:03:38 +10002729 /* Adjust numrecs for the later get_*_keys() calls. */
2730 lrecs -= rrecs;
2731 xfs_btree_set_numrecs(left, lrecs);
2732 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2733
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002734 /*
2735 * Copy btree block entries from the left block over to the
2736 * new block, the right. Update the right block and log the
2737 * changes.
2738 */
2739 if (level > 0) {
2740 /* It's a non-leaf. Move keys and pointers. */
2741 union xfs_btree_key *lkp; /* left btree key */
2742 union xfs_btree_ptr *lpp; /* left address pointer */
2743 union xfs_btree_key *rkp; /* right btree key */
2744 union xfs_btree_ptr *rpp; /* right address pointer */
2745
2746 lkp = xfs_btree_key_addr(cur, src_index, left);
2747 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2748 rkp = xfs_btree_key_addr(cur, 1, right);
2749 rpp = xfs_btree_ptr_addr(cur, 1, right);
2750
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002751 for (i = src_index; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002752 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002753 if (error)
2754 goto error0;
2755 }
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002756
Darrick J. Wong70b22652016-08-03 11:03:38 +10002757 /* Copy the keys & pointers to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002758 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2759 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2760
2761 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2762 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2763
Darrick J. Wong70b22652016-08-03 11:03:38 +10002764 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002765 xfs_btree_get_node_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002766 } else {
2767 /* It's a leaf. Move records. */
2768 union xfs_btree_rec *lrp; /* left record pointer */
2769 union xfs_btree_rec *rrp; /* right record pointer */
2770
2771 lrp = xfs_btree_rec_addr(cur, src_index, left);
2772 rrp = xfs_btree_rec_addr(cur, 1, right);
2773
Darrick J. Wong70b22652016-08-03 11:03:38 +10002774 /* Copy records to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002775 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2776 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2777
Darrick J. Wong70b22652016-08-03 11:03:38 +10002778 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002779 xfs_btree_get_leaf_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002780 }
2781
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002782 /*
2783 * Find the left block number by looking in the buffer.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002784 * Adjust sibling pointers.
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002785 */
2786 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2787 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2788 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2789 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2790
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002791 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2792 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2793
2794 /*
2795 * If there's a block to the new block's right, make that block
2796 * point back to right instead of to left.
2797 */
2798 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002799 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002800 0, &rrblock, &rrbp);
2801 if (error)
2802 goto error0;
2803 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2804 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2805 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002806
2807 /* Update the parent high keys of the left block, if needed. */
2808 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002809 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002810 if (error)
2811 goto error0;
2812 }
2813
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002814 /*
2815 * If the cursor is really in the right block, move it there.
2816 * If it's just pointing past the last entry in left, then we'll
2817 * insert there, so don't change anything in that case.
2818 */
2819 if (cur->bc_ptrs[level] > lrecs + 1) {
2820 xfs_btree_setbuf(cur, level, rbp);
2821 cur->bc_ptrs[level] -= lrecs;
2822 }
2823 /*
2824 * If there are more levels, we'll need another cursor which refers
2825 * the right block, no matter where this cursor was.
2826 */
2827 if (level + 1 < cur->bc_nlevels) {
2828 error = xfs_btree_dup_cursor(cur, curp);
2829 if (error)
2830 goto error0;
2831 (*curp)->bc_ptrs[level + 1]++;
2832 }
2833 *ptrp = rptr;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002834 *stat = 1;
2835 return 0;
2836out0:
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002837 *stat = 0;
2838 return 0;
2839
2840error0:
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002841 return error;
2842}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002843
Dave Chinnercf11da92014-07-15 07:08:24 +10002844struct xfs_btree_split_args {
2845 struct xfs_btree_cur *cur;
2846 int level;
2847 union xfs_btree_ptr *ptrp;
2848 union xfs_btree_key *key;
2849 struct xfs_btree_cur **curp;
2850 int *stat; /* success/failure */
2851 int result;
2852 bool kswapd; /* allocation in kswapd context */
2853 struct completion *done;
2854 struct work_struct work;
2855};
2856
2857/*
2858 * Stack switching interfaces for allocation
2859 */
2860static void
2861xfs_btree_split_worker(
2862 struct work_struct *work)
2863{
2864 struct xfs_btree_split_args *args = container_of(work,
2865 struct xfs_btree_split_args, work);
2866 unsigned long pflags;
Michal Hocko90707332017-05-03 14:53:12 -07002867 unsigned long new_pflags = PF_MEMALLOC_NOFS;
Dave Chinnercf11da92014-07-15 07:08:24 +10002868
2869 /*
2870 * we are in a transaction context here, but may also be doing work
2871 * in kswapd context, and hence we may need to inherit that state
2872 * temporarily to ensure that we don't block waiting for memory reclaim
2873 * in any way.
2874 */
2875 if (args->kswapd)
2876 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2877
2878 current_set_flags_nested(&pflags, new_pflags);
2879
2880 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2881 args->key, args->curp, args->stat);
2882 complete(args->done);
2883
2884 current_restore_flags_nested(&pflags, new_pflags);
2885}
2886
2887/*
2888 * BMBT split requests often come in with little stack to work on. Push
2889 * them off to a worker thread so there is lots of stack to use. For the other
2890 * btree types, just call directly to avoid the context switch overhead here.
2891 */
2892STATIC int /* error */
2893xfs_btree_split(
2894 struct xfs_btree_cur *cur,
2895 int level,
2896 union xfs_btree_ptr *ptrp,
2897 union xfs_btree_key *key,
2898 struct xfs_btree_cur **curp,
2899 int *stat) /* success/failure */
2900{
2901 struct xfs_btree_split_args args;
2902 DECLARE_COMPLETION_ONSTACK(done);
2903
2904 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2905 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2906
2907 args.cur = cur;
2908 args.level = level;
2909 args.ptrp = ptrp;
2910 args.key = key;
2911 args.curp = curp;
2912 args.stat = stat;
2913 args.done = &done;
2914 args.kswapd = current_is_kswapd();
2915 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2916 queue_work(xfs_alloc_wq, &args.work);
2917 wait_for_completion(&done);
2918 destroy_work_on_stack(&args.work);
2919 return args.result;
2920}
2921
2922
Christoph Hellwig344207c2008-10-30 16:57:16 +11002923/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002924 * Copy the old inode root contents into a real block and make the
2925 * broot point to it.
2926 */
2927int /* error */
2928xfs_btree_new_iroot(
2929 struct xfs_btree_cur *cur, /* btree cursor */
2930 int *logflags, /* logging flags for inode */
2931 int *stat) /* return status - 0 fail */
2932{
2933 struct xfs_buf *cbp; /* buffer for cblock */
2934 struct xfs_btree_block *block; /* btree block */
2935 struct xfs_btree_block *cblock; /* child btree block */
2936 union xfs_btree_key *ckp; /* child key pointer */
2937 union xfs_btree_ptr *cpp; /* child ptr pointer */
2938 union xfs_btree_key *kp; /* pointer to btree key */
2939 union xfs_btree_ptr *pp; /* pointer to block addr */
2940 union xfs_btree_ptr nptr; /* new block addr */
2941 int level; /* btree level */
2942 int error; /* error return code */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002943 int i; /* loop counter */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002944
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002945 XFS_BTREE_STATS_INC(cur, newroot);
2946
2947 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2948
2949 level = cur->bc_nlevels - 1;
2950
2951 block = xfs_btree_get_iroot(cur);
2952 pp = xfs_btree_ptr_addr(cur, 1, block);
2953
2954 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002955 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002956 if (error)
2957 goto error0;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002958 if (*stat == 0)
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002959 return 0;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002960
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002961 XFS_BTREE_STATS_INC(cur, alloc);
2962
2963 /* Copy the root into a real block. */
2964 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2965 if (error)
2966 goto error0;
2967
Dave Chinner088c9f62013-06-12 12:19:08 +10002968 /*
2969 * we can't just memcpy() the root in for CRC enabled btree blocks.
2970 * In that case have to also ensure the blkno remains correct
2971 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002972 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002973 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2974 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2975 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2976 else
2977 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2978 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002979
2980 be16_add_cpu(&block->bb_level, 1);
2981 xfs_btree_set_numrecs(block, 1);
2982 cur->bc_nlevels++;
2983 cur->bc_ptrs[level + 1] = 1;
2984
2985 kp = xfs_btree_key_addr(cur, 1, block);
2986 ckp = xfs_btree_key_addr(cur, 1, cblock);
2987 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2988
2989 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002990 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002991 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002992 if (error)
2993 goto error0;
2994 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002995
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002996 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2997
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002998 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002999 if (error)
3000 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003001
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003002 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3003
3004 xfs_iroot_realloc(cur->bc_private.b.ip,
3005 1 - xfs_btree_get_numrecs(cblock),
3006 cur->bc_private.b.whichfork);
3007
3008 xfs_btree_setbuf(cur, level, cbp);
3009
3010 /*
3011 * Do all this logging at the end so that
3012 * the root is at the right level.
3013 */
3014 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3015 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3016 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3017
3018 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06003019 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003020 *stat = 1;
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003021 return 0;
3022error0:
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003023 return error;
3024}
3025
3026/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11003027 * Allocate a new root block, fill it in.
3028 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11003029STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11003030xfs_btree_new_root(
3031 struct xfs_btree_cur *cur, /* btree cursor */
3032 int *stat) /* success/failure */
3033{
3034 struct xfs_btree_block *block; /* one half of the old root block */
3035 struct xfs_buf *bp; /* buffer containing block */
3036 int error; /* error return value */
3037 struct xfs_buf *lbp; /* left buffer pointer */
3038 struct xfs_btree_block *left; /* left btree block */
3039 struct xfs_buf *nbp; /* new (root) buffer */
3040 struct xfs_btree_block *new; /* new (root) btree block */
3041 int nptr; /* new value for key index, 1 or 2 */
3042 struct xfs_buf *rbp; /* right buffer pointer */
3043 struct xfs_btree_block *right; /* right btree block */
3044 union xfs_btree_ptr rptr;
3045 union xfs_btree_ptr lptr;
3046
Christoph Hellwig344207c2008-10-30 16:57:16 +11003047 XFS_BTREE_STATS_INC(cur, newroot);
3048
3049 /* initialise our start point from the cursor */
3050 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3051
3052 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10003053 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003054 if (error)
3055 goto error0;
3056 if (*stat == 0)
3057 goto out0;
3058 XFS_BTREE_STATS_INC(cur, alloc);
3059
3060 /* Set up the new block. */
3061 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3062 if (error)
3063 goto error0;
3064
3065 /* Set the root in the holding structure increasing the level by 1. */
3066 cur->bc_ops->set_root(cur, &lptr, 1);
3067
3068 /*
3069 * At the previous root level there are now two blocks: the old root,
3070 * and the new block generated when it was split. We don't know which
3071 * one the cursor is pointing at, so we set up variables "left" and
3072 * "right" for each case.
3073 */
3074 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3075
3076#ifdef DEBUG
3077 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3078 if (error)
3079 goto error0;
3080#endif
3081
3082 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3083 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3084 /* Our block is left, pick up the right block. */
3085 lbp = bp;
3086 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3087 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003088 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003089 if (error)
3090 goto error0;
3091 bp = rbp;
3092 nptr = 1;
3093 } else {
3094 /* Our block is right, pick up the left block. */
3095 rbp = bp;
3096 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3097 right = block;
3098 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003099 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003100 if (error)
3101 goto error0;
3102 bp = lbp;
3103 nptr = 2;
3104 }
Darrick J. Wong70b22652016-08-03 11:03:38 +10003105
Christoph Hellwig344207c2008-10-30 16:57:16 +11003106 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05003107 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003108 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3109 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3110 !xfs_btree_ptr_is_null(cur, &rptr));
3111
3112 /* Fill in the key data in the new root. */
3113 if (xfs_btree_get_level(left) > 0) {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003114 /*
3115 * Get the keys for the left block's keys and put them directly
3116 * in the parent block. Do the same for the right block.
3117 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003118 xfs_btree_get_node_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003119 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003120 xfs_btree_get_node_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003121 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003122 } else {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003123 /*
3124 * Get the keys for the left block's records and put them
3125 * directly in the parent block. Do the same for the right
3126 * block.
3127 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003128 xfs_btree_get_leaf_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003129 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003130 xfs_btree_get_leaf_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003131 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003132 }
3133 xfs_btree_log_keys(cur, nbp, 1, 2);
3134
3135 /* Fill in the pointer data in the new root. */
3136 xfs_btree_copy_ptrs(cur,
3137 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3138 xfs_btree_copy_ptrs(cur,
3139 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3140 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3141
3142 /* Fix up the cursor. */
3143 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3144 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3145 cur->bc_nlevels++;
Christoph Hellwig344207c2008-10-30 16:57:16 +11003146 *stat = 1;
3147 return 0;
3148error0:
Christoph Hellwig344207c2008-10-30 16:57:16 +11003149 return error;
3150out0:
Christoph Hellwig344207c2008-10-30 16:57:16 +11003151 *stat = 0;
3152 return 0;
3153}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003154
3155STATIC int
3156xfs_btree_make_block_unfull(
3157 struct xfs_btree_cur *cur, /* btree cursor */
3158 int level, /* btree level */
3159 int numrecs,/* # of recs in block */
3160 int *oindex,/* old tree index */
3161 int *index, /* new tree index */
3162 union xfs_btree_ptr *nptr, /* new btree ptr */
3163 struct xfs_btree_cur **ncur, /* new btree cursor */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003164 union xfs_btree_key *key, /* key of new block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003165 int *stat)
3166{
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003167 int error = 0;
3168
3169 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3170 level == cur->bc_nlevels - 1) {
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003171 struct xfs_inode *ip = cur->bc_private.b.ip;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003172
3173 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3174 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003175 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
Darrick J. Wong0d309792016-08-03 11:01:25 +10003176 *stat = 1;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003177 } else {
3178 /* A root block that needs replacing */
3179 int logflags = 0;
3180
3181 error = xfs_btree_new_iroot(cur, &logflags, stat);
3182 if (error || *stat == 0)
3183 return error;
3184
3185 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3186 }
3187
3188 return 0;
3189 }
3190
3191 /* First, try shifting an entry to the right neighbor. */
3192 error = xfs_btree_rshift(cur, level, stat);
3193 if (error || *stat)
3194 return error;
3195
3196 /* Next, try shifting an entry to the left neighbor. */
3197 error = xfs_btree_lshift(cur, level, stat);
3198 if (error)
3199 return error;
3200
3201 if (*stat) {
3202 *oindex = *index = cur->bc_ptrs[level];
3203 return 0;
3204 }
3205
3206 /*
3207 * Next, try splitting the current block in half.
3208 *
3209 * If this works we have to re-set our variables because we
3210 * could be in a different block now.
3211 */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003212 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003213 if (error || *stat == 0)
3214 return error;
3215
3216
3217 *index = cur->bc_ptrs[level];
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003218 return 0;
3219}
3220
3221/*
3222 * Insert one record/level. Return information to the caller
3223 * allowing the next level up to proceed if necessary.
3224 */
3225STATIC int
3226xfs_btree_insrec(
3227 struct xfs_btree_cur *cur, /* btree cursor */
3228 int level, /* level to insert record at */
3229 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003230 union xfs_btree_rec *rec, /* record to insert */
3231 union xfs_btree_key *key, /* i/o: block key for ptrp */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003232 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3233 int *stat) /* success/failure */
3234{
3235 struct xfs_btree_block *block; /* btree block */
3236 struct xfs_buf *bp; /* buffer for block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003237 union xfs_btree_ptr nptr; /* new block ptr */
3238 struct xfs_btree_cur *ncur; /* new btree cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003239 union xfs_btree_key nkey; /* new block key */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003240 union xfs_btree_key *lkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003241 int optr; /* old key/record index */
3242 int ptr; /* key/record index */
3243 int numrecs;/* number of records */
3244 int error; /* error return value */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003245 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003246 xfs_daddr_t old_bn;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003247
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003248 ncur = NULL;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003249 lkey = &nkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003250
3251 /*
3252 * If we have an external root pointer, and we've made it to the
3253 * root level, allocate a new root block and we're done.
3254 */
3255 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3256 (level >= cur->bc_nlevels)) {
3257 error = xfs_btree_new_root(cur, stat);
3258 xfs_btree_set_ptr_null(cur, ptrp);
3259
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003260 return error;
3261 }
3262
3263 /* If we're off the left edge, return failure. */
3264 ptr = cur->bc_ptrs[level];
3265 if (ptr == 0) {
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003266 *stat = 0;
3267 return 0;
3268 }
3269
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003270 optr = ptr;
3271
3272 XFS_BTREE_STATS_INC(cur, insrec);
3273
3274 /* Get pointers to the btree buffer and block. */
3275 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003276 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003277 numrecs = xfs_btree_get_numrecs(block);
3278
3279#ifdef DEBUG
3280 error = xfs_btree_check_block(cur, block, level, bp);
3281 if (error)
3282 goto error0;
3283
3284 /* Check that the new entry is being inserted in the right place. */
3285 if (ptr <= numrecs) {
3286 if (level == 0) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003287 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003288 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003289 } else {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003290 ASSERT(cur->bc_ops->keys_inorder(cur, key,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003291 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003292 }
3293 }
3294#endif
3295
3296 /*
3297 * If the block is full, we can't insert the new entry until we
3298 * make the block un-full.
3299 */
3300 xfs_btree_set_ptr_null(cur, &nptr);
3301 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3302 error = xfs_btree_make_block_unfull(cur, level, numrecs,
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003303 &optr, &ptr, &nptr, &ncur, lkey, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003304 if (error || *stat == 0)
3305 goto error0;
3306 }
3307
3308 /*
3309 * The current block may have changed if the block was
3310 * previously full and we have just made space in it.
3311 */
3312 block = xfs_btree_get_block(cur, level, &bp);
3313 numrecs = xfs_btree_get_numrecs(block);
3314
3315#ifdef DEBUG
3316 error = xfs_btree_check_block(cur, block, level, bp);
3317 if (error)
3318 return error;
3319#endif
3320
3321 /*
3322 * At this point we know there's room for our new entry in the block
3323 * we're pointing at.
3324 */
3325 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3326
3327 if (level > 0) {
3328 /* It's a nonleaf. make a hole in the keys and ptrs */
3329 union xfs_btree_key *kp;
3330 union xfs_btree_ptr *pp;
3331
3332 kp = xfs_btree_key_addr(cur, ptr, block);
3333 pp = xfs_btree_ptr_addr(cur, ptr, block);
3334
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003335 for (i = numrecs - ptr; i >= 0; i--) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003336 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003337 if (error)
3338 return error;
3339 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003340
3341 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3342 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3343
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003344 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003345 if (error)
3346 goto error0;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003347
3348 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003349 xfs_btree_copy_keys(cur, kp, key, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003350 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3351 numrecs++;
3352 xfs_btree_set_numrecs(block, numrecs);
3353 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3354 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3355#ifdef DEBUG
3356 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003357 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3358 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003359 }
3360#endif
3361 } else {
3362 /* It's a leaf. make a hole in the records */
3363 union xfs_btree_rec *rp;
3364
3365 rp = xfs_btree_rec_addr(cur, ptr, block);
3366
3367 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3368
3369 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003370 xfs_btree_copy_recs(cur, rp, rec, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003371 xfs_btree_set_numrecs(block, ++numrecs);
3372 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3373#ifdef DEBUG
3374 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003375 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3376 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003377 }
3378#endif
3379 }
3380
3381 /* Log the new number of records in the btree header. */
3382 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3383
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003384 /*
3385 * If we just inserted into a new tree block, we have to
3386 * recalculate nkey here because nkey is out of date.
3387 *
3388 * Otherwise we're just updating an existing block (having shoved
3389 * some records into the new tree block), so use the regular key
3390 * update mechanism.
3391 */
3392 if (bp && bp->b_bn != old_bn) {
3393 xfs_btree_get_keys(cur, block, lkey);
3394 } else if (xfs_btree_needs_key_update(cur, optr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003395 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003396 if (error)
3397 goto error0;
3398 }
3399
3400 /*
3401 * If we are tracking the last record in the tree and
3402 * we are at the far right edge of the tree, update it.
3403 */
3404 if (xfs_btree_is_lastrec(cur, block, level)) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003405 cur->bc_ops->update_lastrec(cur, block, rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003406 ptr, LASTREC_INSREC);
3407 }
3408
3409 /*
3410 * Return the new block number, if any.
3411 * If there is one, give back a record value and a cursor too.
3412 */
3413 *ptrp = nptr;
3414 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003415 xfs_btree_copy_keys(cur, key, lkey, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003416 *curp = ncur;
3417 }
3418
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003419 *stat = 1;
3420 return 0;
3421
3422error0:
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003423 return error;
3424}
3425
3426/*
3427 * Insert the record at the point referenced by cur.
3428 *
3429 * A multi-level split of the tree on insert will invalidate the original
3430 * cursor. All callers of this function should assume that the cursor is
3431 * no longer valid and revalidate it.
3432 */
3433int
3434xfs_btree_insert(
3435 struct xfs_btree_cur *cur,
3436 int *stat)
3437{
3438 int error; /* error return value */
3439 int i; /* result value, 0 for failure */
3440 int level; /* current level number in btree */
3441 union xfs_btree_ptr nptr; /* new block number (split result) */
3442 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3443 struct xfs_btree_cur *pcur; /* previous level's cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003444 union xfs_btree_key bkey; /* key of block to insert */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003445 union xfs_btree_key *key;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003446 union xfs_btree_rec rec; /* record to insert */
3447
3448 level = 0;
3449 ncur = NULL;
3450 pcur = cur;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003451 key = &bkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003452
3453 xfs_btree_set_ptr_null(cur, &nptr);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003454
3455 /* Make a key out of the record data to be inserted, and save it. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003456 cur->bc_ops->init_rec_from_cur(cur, &rec);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003457 cur->bc_ops->init_key_from_rec(key, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003458
3459 /*
3460 * Loop going up the tree, starting at the leaf level.
3461 * Stop when we don't get a split block, that must mean that
3462 * the insert is finished with this level.
3463 */
3464 do {
3465 /*
3466 * Insert nrec/nptr into this level of the tree.
3467 * Note if we fail, nptr will be null.
3468 */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003469 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003470 &ncur, &i);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003471 if (error) {
3472 if (pcur != cur)
3473 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3474 goto error0;
3475 }
3476
Eric Sandeenc29aad42015-02-23 22:39:08 +11003477 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003478 level++;
3479
3480 /*
3481 * See if the cursor we just used is trash.
3482 * Can't trash the caller's cursor, but otherwise we should
3483 * if ncur is a new cursor or we're about to be done.
3484 */
3485 if (pcur != cur &&
3486 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3487 /* Save the state from the cursor before we trash it */
3488 if (cur->bc_ops->update_cursor)
3489 cur->bc_ops->update_cursor(pcur, cur);
3490 cur->bc_nlevels = pcur->bc_nlevels;
3491 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3492 }
3493 /* If we got a new cursor, switch to it. */
3494 if (ncur) {
3495 pcur = ncur;
3496 ncur = NULL;
3497 }
3498 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3499
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003500 *stat = i;
3501 return 0;
3502error0:
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003503 return error;
3504}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003505
3506/*
3507 * Try to merge a non-leaf block back into the inode root.
3508 *
3509 * Note: the killroot names comes from the fact that we're effectively
3510 * killing the old root block. But because we can't just delete the
3511 * inode we have to copy the single block it was pointing to into the
3512 * inode.
3513 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003514STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003515xfs_btree_kill_iroot(
3516 struct xfs_btree_cur *cur)
3517{
3518 int whichfork = cur->bc_private.b.whichfork;
3519 struct xfs_inode *ip = cur->bc_private.b.ip;
3520 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3521 struct xfs_btree_block *block;
3522 struct xfs_btree_block *cblock;
3523 union xfs_btree_key *kp;
3524 union xfs_btree_key *ckp;
3525 union xfs_btree_ptr *pp;
3526 union xfs_btree_ptr *cpp;
3527 struct xfs_buf *cbp;
3528 int level;
3529 int index;
3530 int numrecs;
Christoph Hellwig196328e2016-02-08 14:58:07 +11003531 int error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003532#ifdef DEBUG
3533 union xfs_btree_ptr ptr;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003534#endif
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003535 int i;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003536
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003537 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3538 ASSERT(cur->bc_nlevels > 1);
3539
3540 /*
3541 * Don't deal with the root block needs to be a leaf case.
3542 * We're just going to turn the thing back into extents anyway.
3543 */
3544 level = cur->bc_nlevels - 1;
3545 if (level == 1)
3546 goto out0;
3547
3548 /*
3549 * Give up if the root has multiple children.
3550 */
3551 block = xfs_btree_get_iroot(cur);
3552 if (xfs_btree_get_numrecs(block) != 1)
3553 goto out0;
3554
3555 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3556 numrecs = xfs_btree_get_numrecs(cblock);
3557
3558 /*
3559 * Only do this if the next level will fit.
3560 * Then the data must be copied up to the inode,
3561 * instead of freeing the root you free the next level.
3562 */
3563 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3564 goto out0;
3565
3566 XFS_BTREE_STATS_INC(cur, killroot);
3567
3568#ifdef DEBUG
3569 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3570 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3571 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3572 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3573#endif
3574
3575 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3576 if (index) {
3577 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3578 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003579 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003580 }
3581
3582 be16_add_cpu(&block->bb_numrecs, index);
3583 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3584
3585 kp = xfs_btree_key_addr(cur, 1, block);
3586 ckp = xfs_btree_key_addr(cur, 1, cblock);
3587 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3588
3589 pp = xfs_btree_ptr_addr(cur, 1, block);
3590 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003591
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003592 for (i = 0; i < numrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003593 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003594 if (error)
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003595 return error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003596 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003597
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003598 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3599
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003600 error = xfs_btree_free_block(cur, cbp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003601 if (error)
Christoph Hellwig196328e2016-02-08 14:58:07 +11003602 return error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003603
3604 cur->bc_bufs[level - 1] = NULL;
3605 be16_add_cpu(&block->bb_level, -1);
3606 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003607 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003608 cur->bc_nlevels--;
3609out0:
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003610 return 0;
3611}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003612
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003613/*
3614 * Kill the current root node, and replace it with it's only child node.
3615 */
3616STATIC int
3617xfs_btree_kill_root(
3618 struct xfs_btree_cur *cur,
3619 struct xfs_buf *bp,
3620 int level,
3621 union xfs_btree_ptr *newroot)
3622{
3623 int error;
3624
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003625 XFS_BTREE_STATS_INC(cur, killroot);
3626
3627 /*
3628 * Update the root pointer, decreasing the level by 1 and then
3629 * free the old root.
3630 */
3631 cur->bc_ops->set_root(cur, newroot, -1);
3632
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003633 error = xfs_btree_free_block(cur, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003634 if (error)
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003635 return error;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003636
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003637 cur->bc_bufs[level] = NULL;
3638 cur->bc_ra[level] = 0;
3639 cur->bc_nlevels--;
3640
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003641 return 0;
3642}
3643
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003644STATIC int
3645xfs_btree_dec_cursor(
3646 struct xfs_btree_cur *cur,
3647 int level,
3648 int *stat)
3649{
3650 int error;
3651 int i;
3652
3653 if (level > 0) {
3654 error = xfs_btree_decrement(cur, level, &i);
3655 if (error)
3656 return error;
3657 }
3658
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003659 *stat = 1;
3660 return 0;
3661}
3662
3663/*
3664 * Single level of the btree record deletion routine.
3665 * Delete record pointed to by cur/level.
3666 * Remove the record from its block then rebalance the tree.
3667 * Return 0 for error, 1 for done, 2 to go on to the next level.
3668 */
3669STATIC int /* error */
3670xfs_btree_delrec(
3671 struct xfs_btree_cur *cur, /* btree cursor */
3672 int level, /* level removing record from */
3673 int *stat) /* fail/done/go-on */
3674{
3675 struct xfs_btree_block *block; /* btree block */
3676 union xfs_btree_ptr cptr; /* current block ptr */
3677 struct xfs_buf *bp; /* buffer for block */
3678 int error; /* error return value */
3679 int i; /* loop counter */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003680 union xfs_btree_ptr lptr; /* left sibling block ptr */
3681 struct xfs_buf *lbp; /* left buffer pointer */
3682 struct xfs_btree_block *left; /* left btree block */
3683 int lrecs = 0; /* left record count */
3684 int ptr; /* key/record index */
3685 union xfs_btree_ptr rptr; /* right sibling block ptr */
3686 struct xfs_buf *rbp; /* right buffer pointer */
3687 struct xfs_btree_block *right; /* right btree block */
3688 struct xfs_btree_block *rrblock; /* right-right btree block */
3689 struct xfs_buf *rrbp; /* right-right buffer pointer */
3690 int rrecs = 0; /* right record count */
3691 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3692 int numrecs; /* temporary numrec count */
3693
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003694 tcur = NULL;
3695
3696 /* Get the index of the entry being deleted, check for nothing there. */
3697 ptr = cur->bc_ptrs[level];
3698 if (ptr == 0) {
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003699 *stat = 0;
3700 return 0;
3701 }
3702
3703 /* Get the buffer & block containing the record or key/ptr. */
3704 block = xfs_btree_get_block(cur, level, &bp);
3705 numrecs = xfs_btree_get_numrecs(block);
3706
3707#ifdef DEBUG
3708 error = xfs_btree_check_block(cur, block, level, bp);
3709 if (error)
3710 goto error0;
3711#endif
3712
3713 /* Fail if we're off the end of the block. */
3714 if (ptr > numrecs) {
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003715 *stat = 0;
3716 return 0;
3717 }
3718
3719 XFS_BTREE_STATS_INC(cur, delrec);
3720 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3721
3722 /* Excise the entries being deleted. */
3723 if (level > 0) {
3724 /* It's a nonleaf. operate on keys and ptrs */
3725 union xfs_btree_key *lkp;
3726 union xfs_btree_ptr *lpp;
3727
3728 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3729 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3730
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003731 for (i = 0; i < numrecs - ptr; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003732 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003733 if (error)
3734 goto error0;
3735 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003736
3737 if (ptr < numrecs) {
3738 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3739 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3740 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3741 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3742 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003743 } else {
3744 /* It's a leaf. operate on records */
3745 if (ptr < numrecs) {
3746 xfs_btree_shift_recs(cur,
3747 xfs_btree_rec_addr(cur, ptr + 1, block),
3748 -1, numrecs - ptr);
3749 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3750 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003751 }
3752
3753 /*
3754 * Decrement and log the number of entries in the block.
3755 */
3756 xfs_btree_set_numrecs(block, --numrecs);
3757 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3758
3759 /*
3760 * If we are tracking the last record in the tree and
3761 * we are at the far right edge of the tree, update it.
3762 */
3763 if (xfs_btree_is_lastrec(cur, block, level)) {
3764 cur->bc_ops->update_lastrec(cur, block, NULL,
3765 ptr, LASTREC_DELREC);
3766 }
3767
3768 /*
3769 * We're at the root level. First, shrink the root block in-memory.
3770 * Try to get rid of the next level down. If we can't then there's
3771 * nothing left to do.
3772 */
3773 if (level == cur->bc_nlevels - 1) {
3774 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3775 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3776 cur->bc_private.b.whichfork);
3777
3778 error = xfs_btree_kill_iroot(cur);
3779 if (error)
3780 goto error0;
3781
3782 error = xfs_btree_dec_cursor(cur, level, stat);
3783 if (error)
3784 goto error0;
3785 *stat = 1;
3786 return 0;
3787 }
3788
3789 /*
3790 * If this is the root level, and there's only one entry left,
3791 * and it's NOT the leaf level, then we can get rid of this
3792 * level.
3793 */
3794 if (numrecs == 1 && level > 0) {
3795 union xfs_btree_ptr *pp;
3796 /*
3797 * pp is still set to the first pointer in the block.
3798 * Make it the new root of the btree.
3799 */
3800 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003801 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003802 if (error)
3803 goto error0;
3804 } else if (level > 0) {
3805 error = xfs_btree_dec_cursor(cur, level, stat);
3806 if (error)
3807 goto error0;
3808 }
3809 *stat = 1;
3810 return 0;
3811 }
3812
3813 /*
3814 * If we deleted the leftmost entry in the block, update the
3815 * key values above us in the tree.
3816 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003817 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003818 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003819 if (error)
3820 goto error0;
3821 }
3822
3823 /*
3824 * If the number of records remaining in the block is at least
3825 * the minimum, we're done.
3826 */
3827 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3828 error = xfs_btree_dec_cursor(cur, level, stat);
3829 if (error)
3830 goto error0;
3831 return 0;
3832 }
3833
3834 /*
3835 * Otherwise, we have to move some records around to keep the
3836 * tree balanced. Look at the left and right sibling blocks to
3837 * see if we can re-balance by moving only one record.
3838 */
3839 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3840 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3841
3842 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3843 /*
3844 * One child of root, need to get a chance to copy its contents
3845 * into the root and delete it. Can't go up to next level,
3846 * there's nothing to delete there.
3847 */
3848 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3849 xfs_btree_ptr_is_null(cur, &lptr) &&
3850 level == cur->bc_nlevels - 2) {
3851 error = xfs_btree_kill_iroot(cur);
3852 if (!error)
3853 error = xfs_btree_dec_cursor(cur, level, stat);
3854 if (error)
3855 goto error0;
3856 return 0;
3857 }
3858 }
3859
3860 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3861 !xfs_btree_ptr_is_null(cur, &lptr));
3862
3863 /*
3864 * Duplicate the cursor so our btree manipulations here won't
3865 * disrupt the next level up.
3866 */
3867 error = xfs_btree_dup_cursor(cur, &tcur);
3868 if (error)
3869 goto error0;
3870
3871 /*
3872 * If there's a right sibling, see if it's ok to shift an entry
3873 * out of it.
3874 */
3875 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3876 /*
3877 * Move the temp cursor to the last entry in the next block.
3878 * Actually any entry but the first would suffice.
3879 */
3880 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003881 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003882
3883 error = xfs_btree_increment(tcur, level, &i);
3884 if (error)
3885 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003886 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003887
3888 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003889 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003890
3891 /* Grab a pointer to the block. */
3892 right = xfs_btree_get_block(tcur, level, &rbp);
3893#ifdef DEBUG
3894 error = xfs_btree_check_block(tcur, right, level, rbp);
3895 if (error)
3896 goto error0;
3897#endif
3898 /* Grab the current block number, for future use. */
3899 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3900
3901 /*
3902 * If right block is full enough so that removing one entry
3903 * won't make it too empty, and left-shifting an entry out
3904 * of right to us works, we're done.
3905 */
3906 if (xfs_btree_get_numrecs(right) - 1 >=
3907 cur->bc_ops->get_minrecs(tcur, level)) {
3908 error = xfs_btree_lshift(tcur, level, &i);
3909 if (error)
3910 goto error0;
3911 if (i) {
3912 ASSERT(xfs_btree_get_numrecs(block) >=
3913 cur->bc_ops->get_minrecs(tcur, level));
3914
3915 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3916 tcur = NULL;
3917
3918 error = xfs_btree_dec_cursor(cur, level, stat);
3919 if (error)
3920 goto error0;
3921 return 0;
3922 }
3923 }
3924
3925 /*
3926 * Otherwise, grab the number of records in right for
3927 * future reference, and fix up the temp cursor to point
3928 * to our block again (last record).
3929 */
3930 rrecs = xfs_btree_get_numrecs(right);
3931 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3932 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003933 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003934
3935 error = xfs_btree_decrement(tcur, level, &i);
3936 if (error)
3937 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003938 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003939 }
3940 }
3941
3942 /*
3943 * If there's a left sibling, see if it's ok to shift an entry
3944 * out of it.
3945 */
3946 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3947 /*
3948 * Move the temp cursor to the first entry in the
3949 * previous block.
3950 */
3951 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003952 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003953
3954 error = xfs_btree_decrement(tcur, level, &i);
3955 if (error)
3956 goto error0;
3957 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003958 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
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,
4300 void *data)
4301{
4302 union xfs_btree_ptr lptr;
4303 int level;
4304 struct xfs_btree_block *block = NULL;
4305 int error = 0;
4306
4307 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4308
4309 /* for each level */
4310 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4311 /* grab the left hand block */
4312 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4313 if (error)
4314 return error;
4315
4316 /* readahead the left most block for the next level down */
4317 if (level > 0) {
4318 union xfs_btree_ptr *ptr;
4319
4320 ptr = xfs_btree_ptr_addr(cur, 1, block);
4321 xfs_btree_readahead_ptr(cur, ptr, 1);
4322
4323 /* save for the next iteration of the loop */
Eric Sandeena4d768e2017-05-22 19:54:10 -07004324 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
Darrick J. Wong28a89562016-08-03 11:10:55 +10004325 }
4326
4327 /* for each buffer in the level */
4328 do {
4329 error = xfs_btree_visit_block(cur, level, fn, data);
4330 } while (!error);
4331
4332 if (error != -ENOENT)
4333 return error;
4334 }
4335
4336 return 0;
4337}
4338
Dave Chinner21b5c972013-08-30 10:23:44 +10004339/*
4340 * Change the owner of a btree.
4341 *
4342 * The mechanism we use here is ordered buffer logging. Because we don't know
4343 * how many buffers were are going to need to modify, we don't really want to
4344 * have to make transaction reservations for the worst case of every buffer in a
4345 * full size btree as that may be more space that we can fit in the log....
4346 *
4347 * We do the btree walk in the most optimal manner possible - we have sibling
4348 * pointers so we can just walk all the blocks on each level from left to right
4349 * in a single pass, and then move to the next level and do the same. We can
4350 * also do readahead on the sibling pointers to get IO moving more quickly,
4351 * though for slow disks this is unlikely to make much difference to performance
4352 * as the amount of CPU work we have to do before moving to the next block is
4353 * relatively small.
4354 *
4355 * For each btree block that we load, modify the owner appropriately, set the
4356 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4357 * we mark the region we change dirty so that if the buffer is relogged in
4358 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10004359 * correctly relogged in that transaction. If we are in recovery context, then
4360 * just queue the modified buffer as delayed write buffer so the transaction
4361 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10004362 */
Darrick J. Wong28a89562016-08-03 11:10:55 +10004363struct xfs_btree_block_change_owner_info {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004364 uint64_t new_owner;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004365 struct list_head *buffer_list;
4366};
4367
Dave Chinner21b5c972013-08-30 10:23:44 +10004368static int
4369xfs_btree_block_change_owner(
4370 struct xfs_btree_cur *cur,
4371 int level,
Darrick J. Wong28a89562016-08-03 11:10:55 +10004372 void *data)
Dave Chinner21b5c972013-08-30 10:23:44 +10004373{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004374 struct xfs_btree_block_change_owner_info *bbcoi = data;
Dave Chinner21b5c972013-08-30 10:23:44 +10004375 struct xfs_btree_block *block;
4376 struct xfs_buf *bp;
Dave Chinner21b5c972013-08-30 10:23:44 +10004377
4378 /* modify the owner */
4379 block = xfs_btree_get_block(cur, level, &bp);
Brian Foster2dd3d702017-08-29 10:08:40 -07004380 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4381 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4382 return 0;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004383 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
Brian Foster2dd3d702017-08-29 10:08:40 -07004384 } else {
4385 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4386 return 0;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004387 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
Brian Foster2dd3d702017-08-29 10:08:40 -07004388 }
Dave Chinner21b5c972013-08-30 10:23:44 +10004389
4390 /*
Dave Chinner638f44162013-08-30 10:23:45 +10004391 * If the block is a root block hosted in an inode, we might not have a
4392 * buffer pointer here and we shouldn't attempt to log the change as the
4393 * information is already held in the inode and discarded when the root
4394 * block is formatted into the on-disk inode fork. We still change it,
4395 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10004396 */
Brian Foster2dd3d702017-08-29 10:08:40 -07004397 if (!bp) {
Dave Chinner21b5c972013-08-30 10:23:44 +10004398 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4399 ASSERT(level == cur->bc_nlevels - 1);
Brian Foster2dd3d702017-08-29 10:08:40 -07004400 return 0;
4401 }
4402
4403 if (cur->bc_tp) {
4404 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4405 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4406 return -EAGAIN;
4407 }
4408 } else {
4409 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
Dave Chinner21b5c972013-08-30 10:23:44 +10004410 }
4411
Darrick J. Wong28a89562016-08-03 11:10:55 +10004412 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10004413}
4414
4415int
4416xfs_btree_change_owner(
4417 struct xfs_btree_cur *cur,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004418 uint64_t new_owner,
Dave Chinner638f44162013-08-30 10:23:45 +10004419 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004420{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004421 struct xfs_btree_block_change_owner_info bbcoi;
Dave Chinner21b5c972013-08-30 10:23:44 +10004422
Darrick J. Wong28a89562016-08-03 11:10:55 +10004423 bbcoi.new_owner = new_owner;
4424 bbcoi.buffer_list = buffer_list;
Dave Chinner21b5c972013-08-30 10:23:44 +10004425
Darrick J. Wong28a89562016-08-03 11:10:55 +10004426 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4427 &bbcoi);
Dave Chinner21b5c972013-08-30 10:23:44 +10004428}
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004429
Darrick J. Wong8368a602018-01-08 10:51:00 -08004430/* Verify the v5 fields of a long-format btree block. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004431xfs_failaddr_t
Darrick J. Wong8368a602018-01-08 10:51:00 -08004432xfs_btree_lblock_v5hdr_verify(
4433 struct xfs_buf *bp,
4434 uint64_t owner)
4435{
4436 struct xfs_mount *mp = bp->b_target->bt_mount;
4437 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4438
4439 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004440 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004441 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004442 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004443 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004444 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004445 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4446 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004447 return __this_address;
4448 return NULL;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004449}
4450
4451/* Verify a long-format btree block. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004452xfs_failaddr_t
Darrick J. Wong8368a602018-01-08 10:51:00 -08004453xfs_btree_lblock_verify(
4454 struct xfs_buf *bp,
4455 unsigned int max_recs)
4456{
4457 struct xfs_mount *mp = bp->b_target->bt_mount;
4458 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4459
4460 /* numrecs verification */
4461 if (be16_to_cpu(block->bb_numrecs) > max_recs)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004462 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004463
4464 /* sibling pointer verification */
4465 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4466 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004467 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004468 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4469 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004470 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004471
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004472 return NULL;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004473}
4474
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004475/**
4476 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4477 * btree block
4478 *
4479 * @bp: buffer containing the btree block
4480 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4481 * @pag_max_level: pointer to the per-ag max level field
4482 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004483xfs_failaddr_t
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004484xfs_btree_sblock_v5hdr_verify(
4485 struct xfs_buf *bp)
4486{
4487 struct xfs_mount *mp = bp->b_target->bt_mount;
4488 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4489 struct xfs_perag *pag = bp->b_pag;
4490
4491 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004492 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004493 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004494 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004495 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004496 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004497 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004498 return __this_address;
4499 return NULL;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004500}
4501
4502/**
4503 * xfs_btree_sblock_verify() -- verify a short-format btree block
4504 *
4505 * @bp: buffer containing the btree block
4506 * @max_recs: maximum records allowed in this btree node
4507 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004508xfs_failaddr_t
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004509xfs_btree_sblock_verify(
4510 struct xfs_buf *bp,
4511 unsigned int max_recs)
4512{
4513 struct xfs_mount *mp = bp->b_target->bt_mount;
4514 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004515 xfs_agblock_t agno;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004516
4517 /* numrecs verification */
4518 if (be16_to_cpu(block->bb_numrecs) > max_recs)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004519 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004520
4521 /* sibling pointer verification */
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004522 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4523 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4524 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004525 return __this_address;
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004526 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4527 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004528 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004529
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004530 return NULL;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004531}
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004532
4533/*
4534 * Calculate the number of btree levels needed to store a given number of
4535 * records in a short-format btree.
4536 */
4537uint
4538xfs_btree_compute_maxlevels(
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004539 uint *limits,
4540 unsigned long len)
4541{
4542 uint level;
4543 unsigned long maxblocks;
4544
4545 maxblocks = (len + limits[0] - 1) / limits[0];
4546 for (level = 1; maxblocks > 1; level++)
4547 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4548 return level;
4549}
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004550
4551/*
4552 * Query a regular btree for all records overlapping a given interval.
4553 * Start with a LE lookup of the key of low_rec and return all records
4554 * until we find a record with a key greater than the key of high_rec.
4555 */
4556STATIC int
4557xfs_btree_simple_query_range(
4558 struct xfs_btree_cur *cur,
4559 union xfs_btree_key *low_key,
4560 union xfs_btree_key *high_key,
4561 xfs_btree_query_range_fn fn,
4562 void *priv)
4563{
4564 union xfs_btree_rec *recp;
4565 union xfs_btree_key rec_key;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004566 int64_t diff;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004567 int stat;
4568 bool firstrec = true;
4569 int error;
4570
4571 ASSERT(cur->bc_ops->init_high_key_from_rec);
4572 ASSERT(cur->bc_ops->diff_two_keys);
4573
4574 /*
4575 * Find the leftmost record. The btree cursor must be set
4576 * to the low record used to generate low_key.
4577 */
4578 stat = 0;
4579 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4580 if (error)
4581 goto out;
4582
Darrick J. Wong5b5c2db2016-08-26 16:00:10 +10004583 /* Nothing? See if there's anything to the right. */
4584 if (!stat) {
4585 error = xfs_btree_increment(cur, 0, &stat);
4586 if (error)
4587 goto out;
4588 }
4589
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004590 while (stat) {
4591 /* Find the record. */
4592 error = xfs_btree_get_rec(cur, &recp, &stat);
4593 if (error || !stat)
4594 break;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004595
4596 /* Skip if high_key(rec) < low_key. */
4597 if (firstrec) {
Darrick J. Wong72227892016-08-26 15:59:50 +10004598 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004599 firstrec = false;
4600 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4601 &rec_key);
4602 if (diff > 0)
4603 goto advloop;
4604 }
4605
4606 /* Stop if high_key < low_key(rec). */
Darrick J. Wong72227892016-08-26 15:59:50 +10004607 cur->bc_ops->init_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004608 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4609 if (diff > 0)
4610 break;
4611
4612 /* Callback */
4613 error = fn(cur, recp, priv);
4614 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4615 break;
4616
4617advloop:
4618 /* Move on to the next record. */
4619 error = xfs_btree_increment(cur, 0, &stat);
4620 if (error)
4621 break;
4622 }
4623
4624out:
4625 return error;
4626}
4627
4628/*
4629 * Query an overlapped interval btree for all records overlapping a given
4630 * interval. This function roughly follows the algorithm given in
4631 * "Interval Trees" of _Introduction to Algorithms_, which is section
4632 * 14.3 in the 2nd and 3rd editions.
4633 *
4634 * First, generate keys for the low and high records passed in.
4635 *
4636 * For any leaf node, generate the high and low keys for the record.
4637 * If the record keys overlap with the query low/high keys, pass the
4638 * record to the function iterator.
4639 *
4640 * For any internal node, compare the low and high keys of each
4641 * pointer against the query low/high keys. If there's an overlap,
4642 * follow the pointer.
4643 *
4644 * As an optimization, we stop scanning a block when we find a low key
4645 * that is greater than the query's high key.
4646 */
4647STATIC int
4648xfs_btree_overlapped_query_range(
4649 struct xfs_btree_cur *cur,
4650 union xfs_btree_key *low_key,
4651 union xfs_btree_key *high_key,
4652 xfs_btree_query_range_fn fn,
4653 void *priv)
4654{
4655 union xfs_btree_ptr ptr;
4656 union xfs_btree_ptr *pp;
4657 union xfs_btree_key rec_key;
4658 union xfs_btree_key rec_hkey;
4659 union xfs_btree_key *lkp;
4660 union xfs_btree_key *hkp;
4661 union xfs_btree_rec *recp;
4662 struct xfs_btree_block *block;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004663 int64_t ldiff;
4664 int64_t hdiff;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004665 int level;
4666 struct xfs_buf *bp;
4667 int i;
4668 int error;
4669
4670 /* Load the root of the btree. */
4671 level = cur->bc_nlevels - 1;
4672 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4673 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4674 if (error)
4675 return error;
4676 xfs_btree_get_block(cur, level, &bp);
4677 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4678#ifdef DEBUG
4679 error = xfs_btree_check_block(cur, block, level, bp);
4680 if (error)
4681 goto out;
4682#endif
4683 cur->bc_ptrs[level] = 1;
4684
4685 while (level < cur->bc_nlevels) {
4686 block = xfs_btree_get_block(cur, level, &bp);
4687
4688 /* End of node, pop back towards the root. */
4689 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4690pop_up:
4691 if (level < cur->bc_nlevels - 1)
4692 cur->bc_ptrs[level + 1]++;
4693 level++;
4694 continue;
4695 }
4696
4697 if (level == 0) {
4698 /* Handle a leaf node. */
4699 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4700
4701 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4702 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4703 low_key);
4704
4705 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4706 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4707 &rec_key);
4708
4709 /*
4710 * If (record's high key >= query's low key) and
4711 * (query's high key >= record's low key), then
4712 * this record overlaps the query range; callback.
4713 */
4714 if (ldiff >= 0 && hdiff >= 0) {
4715 error = fn(cur, recp, priv);
4716 if (error < 0 ||
4717 error == XFS_BTREE_QUERY_RANGE_ABORT)
4718 break;
4719 } else if (hdiff < 0) {
4720 /* Record is larger than high key; pop. */
4721 goto pop_up;
4722 }
4723 cur->bc_ptrs[level]++;
4724 continue;
4725 }
4726
4727 /* Handle an internal node. */
4728 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4729 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4730 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4731
4732 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4733 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4734
4735 /*
4736 * If (pointer's high key >= query's low key) and
4737 * (query's high key >= pointer's low key), then
4738 * this record overlaps the query range; follow pointer.
4739 */
4740 if (ldiff >= 0 && hdiff >= 0) {
4741 level--;
4742 error = xfs_btree_lookup_get_block(cur, level, pp,
4743 &block);
4744 if (error)
4745 goto out;
4746 xfs_btree_get_block(cur, level, &bp);
4747 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4748#ifdef DEBUG
4749 error = xfs_btree_check_block(cur, block, level, bp);
4750 if (error)
4751 goto out;
4752#endif
4753 cur->bc_ptrs[level] = 1;
4754 continue;
4755 } else if (hdiff < 0) {
4756 /* The low key is larger than the upper range; pop. */
4757 goto pop_up;
4758 }
4759 cur->bc_ptrs[level]++;
4760 }
4761
4762out:
4763 /*
4764 * If we don't end this function with the cursor pointing at a record
4765 * block, a subsequent non-error cursor deletion will not release
4766 * node-level buffers, causing a buffer leak. This is quite possible
4767 * with a zero-results range query, so release the buffers if we
4768 * failed to return any results.
4769 */
4770 if (cur->bc_bufs[0] == NULL) {
4771 for (i = 0; i < cur->bc_nlevels; i++) {
4772 if (cur->bc_bufs[i]) {
4773 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4774 cur->bc_bufs[i] = NULL;
4775 cur->bc_ptrs[i] = 0;
4776 cur->bc_ra[i] = 0;
4777 }
4778 }
4779 }
4780
4781 return error;
4782}
4783
4784/*
4785 * Query a btree for all records overlapping a given interval of keys. The
4786 * supplied function will be called with each record found; return one of the
4787 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4788 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4789 * negative error code.
4790 */
4791int
4792xfs_btree_query_range(
4793 struct xfs_btree_cur *cur,
4794 union xfs_btree_irec *low_rec,
4795 union xfs_btree_irec *high_rec,
4796 xfs_btree_query_range_fn fn,
4797 void *priv)
4798{
4799 union xfs_btree_rec rec;
4800 union xfs_btree_key low_key;
4801 union xfs_btree_key high_key;
4802
4803 /* Find the keys of both ends of the interval. */
4804 cur->bc_rec = *high_rec;
4805 cur->bc_ops->init_rec_from_cur(cur, &rec);
4806 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4807
4808 cur->bc_rec = *low_rec;
4809 cur->bc_ops->init_rec_from_cur(cur, &rec);
4810 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4811
4812 /* Enforce low key < high key. */
4813 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4814 return -EINVAL;
4815
4816 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4817 return xfs_btree_simple_query_range(cur, &low_key,
4818 &high_key, fn, priv);
4819 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4820 fn, priv);
4821}
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004822
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004823/* Query a btree for all records. */
4824int
4825xfs_btree_query_all(
4826 struct xfs_btree_cur *cur,
4827 xfs_btree_query_range_fn fn,
4828 void *priv)
4829{
Darrick J. Wong5a4c7332017-06-16 11:00:04 -07004830 union xfs_btree_key low_key;
4831 union xfs_btree_key high_key;
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004832
Darrick J. Wong5a4c7332017-06-16 11:00:04 -07004833 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4834 memset(&low_key, 0, sizeof(low_key));
4835 memset(&high_key, 0xFF, sizeof(high_key));
4836
4837 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004838}
4839
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004840/*
4841 * Calculate the number of blocks needed to store a given number of records
4842 * in a short-format (per-AG metadata) btree.
4843 */
Darrick J. Wong14861c42018-05-09 10:02:01 -07004844unsigned long long
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004845xfs_btree_calc_size(
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004846 uint *limits,
4847 unsigned long long len)
4848{
4849 int level;
4850 int maxrecs;
Darrick J. Wong14861c42018-05-09 10:02:01 -07004851 unsigned long long rval;
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004852
4853 maxrecs = limits[0];
4854 for (level = 0, rval = 0; len > 1; level++) {
4855 len += maxrecs - 1;
4856 do_div(len, maxrecs);
4857 maxrecs = limits[1];
4858 rval += len;
4859 }
4860 return rval;
4861}
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004862
Eric Biggersf1b82432016-10-20 15:42:30 +11004863static int
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004864xfs_btree_count_blocks_helper(
4865 struct xfs_btree_cur *cur,
4866 int level,
4867 void *data)
4868{
4869 xfs_extlen_t *blocks = data;
4870 (*blocks)++;
4871
4872 return 0;
4873}
4874
4875/* Count the blocks in a btree and return the result in *blocks. */
4876int
4877xfs_btree_count_blocks(
4878 struct xfs_btree_cur *cur,
4879 xfs_extlen_t *blocks)
4880{
4881 *blocks = 0;
4882 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4883 blocks);
4884}
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07004885
4886/* Compare two btree pointers. */
4887int64_t
4888xfs_btree_diff_two_ptrs(
4889 struct xfs_btree_cur *cur,
4890 const union xfs_btree_ptr *a,
4891 const union xfs_btree_ptr *b)
4892{
4893 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4894 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4895 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4896}
Darrick J. Wongce1d8022018-01-16 18:52:12 -08004897
4898/* If there's an extent, we're done. */
4899STATIC int
4900xfs_btree_has_record_helper(
4901 struct xfs_btree_cur *cur,
4902 union xfs_btree_rec *rec,
4903 void *priv)
4904{
4905 return XFS_BTREE_QUERY_RANGE_ABORT;
4906}
4907
4908/* Is there a record covering a given range of keys? */
4909int
4910xfs_btree_has_record(
4911 struct xfs_btree_cur *cur,
4912 union xfs_btree_irec *low,
4913 union xfs_btree_irec *high,
4914 bool *exists)
4915{
4916 int error;
4917
4918 error = xfs_btree_query_range(cur, low, high,
4919 &xfs_btree_has_record_helper, NULL);
4920 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
4921 *exists = true;
4922 return 0;
4923 }
4924 *exists = false;
4925 return error;
4926}
Darrick J. Wong08daa3c2018-05-09 10:02:03 -07004927
4928/* Are there more records in this btree? */
4929bool
4930xfs_btree_has_more_records(
4931 struct xfs_btree_cur *cur)
4932{
4933 struct xfs_btree_block *block;
4934 struct xfs_buf *bp;
4935
4936 block = xfs_btree_get_block(cur, 0, &bp);
4937
4938 /* There are still records in this block. */
4939 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4940 return true;
4941
4942 /* There are more record blocks. */
4943 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4944 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4945 else
4946 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);
4947}