blob: 6b589e4f703e07aff80b2e4aeb3d73433ea8c071 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100026#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Christoph Hellwig38bb7422008-10-30 16:56:22 +110029#include "xfs_inode_item.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050030#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_btree.h"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070032#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050035#include "xfs_cksum.h"
Dave Chinnercf11da92014-07-15 07:08:24 +100036#include "xfs_alloc.h"
Brian Fostera45086e2015-10-12 15:59:25 +110037#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Cursor allocation zone.
41 */
42kmem_zone_t *xfs_btree_cur_zone;
43
44/*
45 * Btree magic numbers.
46 */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070047static const uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
Darrick J. Wongb8704942016-08-03 11:30:32 +100048 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070049 XFS_FIBT_MAGIC, 0 },
Darrick J. Wongb8704942016-08-03 11:30:32 +100050 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070051 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
52 XFS_REFC_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080054
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070055uint32_t
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080056xfs_btree_magic(
57 int crc,
58 xfs_btnum_t btnum)
59{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -070060 uint32_t magic = xfs_magics[crc][btnum];
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080061
62 /* Ensure we asked for crc for crc-only magics. */
63 ASSERT(magic != 0);
64 return magic;
65}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Darrick J. Wong52c732e2017-10-17 21:37:33 -070067/*
68 * Check a long btree block header. Return the address of the failing check,
69 * or NULL if everything is ok.
70 */
71xfs_failaddr_t
72__xfs_btree_check_lblock(
73 struct xfs_btree_cur *cur,
74 struct xfs_btree_block *block,
75 int level,
76 struct xfs_buf *bp)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110077{
Darrick J. Wong52c732e2017-10-17 21:37:33 -070078 struct xfs_mount *mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080079 xfs_btnum_t btnum = cur->bc_btnum;
Darrick J. Wong52c732e2017-10-17 21:37:33 -070080 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050081
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080082 if (crc) {
Darrick J. Wong52c732e2017-10-17 21:37:33 -070083 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
84 return __this_address;
85 if (block->bb_u.l.bb_blkno !=
86 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
87 return __this_address;
88 if (block->bb_u.l.bb_pad != cpu_to_be32(0))
89 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050090 }
91
Darrick J. Wong52c732e2017-10-17 21:37:33 -070092 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
93 return __this_address;
94 if (be16_to_cpu(block->bb_level) != level)
95 return __this_address;
96 if (be16_to_cpu(block->bb_numrecs) >
97 cur->bc_ops->get_maxrecs(cur, level))
98 return __this_address;
99 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
100 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_leftsib),
101 level + 1))
102 return __this_address;
103 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
104 !xfs_btree_check_lptr(cur, be64_to_cpu(block->bb_u.l.bb_rightsib),
105 level + 1))
106 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500107
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700108 return NULL;
109}
110
111/* Check a long btree block header. */
Christoph Hellwig4483eb52017-11-06 11:54:01 -0800112static int
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700113xfs_btree_check_lblock(
114 struct xfs_btree_cur *cur,
115 struct xfs_btree_block *block,
116 int level,
117 struct xfs_buf *bp)
118{
119 struct xfs_mount *mp = cur->bc_mp;
120 xfs_failaddr_t fa;
121
122 fa = __xfs_btree_check_lblock(cur, block, level, bp);
123 if (unlikely(XFS_TEST_ERROR(fa != NULL, mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700124 XFS_ERRTAG_BTREE_CHECK_LBLOCK))) {
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100125 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000126 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500127 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000128 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100129 }
130 return 0;
131}
132
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700133/*
134 * Check a short btree block header. Return the address of the failing check,
135 * or NULL if everything is ok.
136 */
137xfs_failaddr_t
138__xfs_btree_check_sblock(
139 struct xfs_btree_cur *cur,
140 struct xfs_btree_block *block,
141 int level,
142 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700144 struct xfs_mount *mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800145 xfs_btnum_t btnum = cur->bc_btnum;
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700146 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500147
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800148 if (crc) {
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700149 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
150 return __this_address;
151 if (block->bb_u.s.bb_blkno !=
152 cpu_to_be64(bp ? bp->b_bn : XFS_BUF_DADDR_NULL))
153 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500154 }
155
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700156 if (be32_to_cpu(block->bb_magic) != xfs_btree_magic(crc, btnum))
157 return __this_address;
158 if (be16_to_cpu(block->bb_level) != level)
159 return __this_address;
160 if (be16_to_cpu(block->bb_numrecs) >
161 cur->bc_ops->get_maxrecs(cur, level))
162 return __this_address;
163 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
164 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_leftsib),
165 level + 1))
166 return __this_address;
167 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
168 !xfs_btree_check_sptr(cur, be32_to_cpu(block->bb_u.s.bb_rightsib),
169 level + 1))
170 return __this_address;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500171
Darrick J. Wong52c732e2017-10-17 21:37:33 -0700172 return NULL;
173}
174
175/* Check a short btree block header. */
176STATIC int
177xfs_btree_check_sblock(
178 struct xfs_btree_cur *cur,
179 struct xfs_btree_block *block,
180 int level,
181 struct xfs_buf *bp)
182{
183 struct xfs_mount *mp = cur->bc_mp;
184 xfs_failaddr_t fa;
185
186 fa = __xfs_btree_check_sblock(cur, block, level, bp);
187 if (unlikely(XFS_TEST_ERROR(fa != NULL, mp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -0700188 XFS_ERRTAG_BTREE_CHECK_SBLOCK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000190 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500191 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000192 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194 return 0;
195}
196
197/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100198 * Debug routine: check that block header is ok.
199 */
200int
201xfs_btree_check_block(
202 struct xfs_btree_cur *cur, /* btree cursor */
203 struct xfs_btree_block *block, /* generic btree block pointer */
204 int level, /* level of the btree block */
205 struct xfs_buf *bp) /* buffer containing block, if any */
206{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100207 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
208 return xfs_btree_check_lblock(cur, block, level, bp);
209 else
210 return xfs_btree_check_sblock(cur, block, level, bp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100211}
212
Darrick J. Wongf1357612017-10-17 21:37:33 -0700213/* Check that this long pointer is valid and points within the fs. */
214bool
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100215xfs_btree_check_lptr(
Darrick J. Wongf1357612017-10-17 21:37:33 -0700216 struct xfs_btree_cur *cur,
217 xfs_fsblock_t fsbno,
218 int level)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100219{
Darrick J. Wongf1357612017-10-17 21:37:33 -0700220 if (level <= 0)
221 return false;
222 return xfs_verify_fsbno(cur->bc_mp, fsbno);
223}
224
225/* Check that this short pointer is valid and points within the AG. */
226bool
227xfs_btree_check_sptr(
228 struct xfs_btree_cur *cur,
229 xfs_agblock_t agbno,
230 int level)
231{
232 if (level <= 0)
233 return false;
234 return xfs_verify_agbno(cur->bc_mp, cur->bc_private.a.agno, agbno);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100235}
236
237/*
Darrick J. Wongf1357612017-10-17 21:37:33 -0700238 * Check that a given (indexed) btree pointer at a certain level of a
239 * btree is valid and doesn't point past where it should.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
Christoph Hellwig4483eb52017-11-06 11:54:01 -0800241static int
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100242xfs_btree_check_ptr(
Darrick J. Wongf1357612017-10-17 21:37:33 -0700243 struct xfs_btree_cur *cur,
244 union xfs_btree_ptr *ptr,
245 int index,
246 int level)
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100247{
248 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700249 if (xfs_btree_check_lptr(cur, be64_to_cpu((&ptr->l)[index]),
250 level))
251 return 0;
252 xfs_err(cur->bc_mp,
253"Inode %llu fork %d: Corrupt btree %d pointer at level %d index %d.",
254 cur->bc_private.b.ip->i_ino,
255 cur->bc_private.b.whichfork, cur->bc_btnum,
256 level, index);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100257 } else {
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700258 if (xfs_btree_check_sptr(cur, be32_to_cpu((&ptr->s)[index]),
259 level))
260 return 0;
261 xfs_err(cur->bc_mp,
262"AG %u: Corrupt btree %d pointer at level %d index %d.",
263 cur->bc_private.a.agno, cur->bc_btnum,
264 level, index);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100265 }
Darrick J. Wongf1357612017-10-17 21:37:33 -0700266
Darrick J. Wong85ae0102018-06-03 16:10:12 -0700267 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100268}
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -0700269
270#ifdef DEBUG
271# define xfs_btree_debug_check_ptr xfs_btree_check_ptr
272#else
273# define xfs_btree_debug_check_ptr(...) (0)
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100274#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100275
276/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500277 * Calculate CRC on the whole btree block and stuff it into the
278 * long-form btree header.
279 *
280 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100281 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500282 * it to disk.
283 */
284void
285xfs_btree_lblock_calc_crc(
286 struct xfs_buf *bp)
287{
288 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800289 struct xfs_buf_log_item *bip = bp->b_log_item;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500290
291 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
292 return;
293 if (bip)
294 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100295 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500296}
297
298bool
299xfs_btree_lblock_verify_crc(
300 struct xfs_buf *bp)
301{
Brian Fostera45086e2015-10-12 15:59:25 +1100302 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
303 struct xfs_mount *mp = bp->b_target->bt_mount;
304
305 if (xfs_sb_version_hascrc(&mp->m_sb)) {
306 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
307 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100308 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100309 }
Eric Sandeen51582172014-02-27 15:17:27 +1100310
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500311 return true;
312}
313
314/*
315 * Calculate CRC on the whole btree block and stuff it into the
316 * short-form btree header.
317 *
318 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100319 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500320 * it to disk.
321 */
322void
323xfs_btree_sblock_calc_crc(
324 struct xfs_buf *bp)
325{
326 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800327 struct xfs_buf_log_item *bip = bp->b_log_item;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500328
329 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
330 return;
331 if (bip)
332 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100333 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500334}
335
336bool
337xfs_btree_sblock_verify_crc(
338 struct xfs_buf *bp)
339{
Brian Fostera45086e2015-10-12 15:59:25 +1100340 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
341 struct xfs_mount *mp = bp->b_target->bt_mount;
342
343 if (xfs_sb_version_hascrc(&mp->m_sb)) {
344 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800345 return __this_address;
Eric Sandeen51582172014-02-27 15:17:27 +1100346 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100347 }
Eric Sandeen51582172014-02-27 15:17:27 +1100348
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500349 return true;
350}
351
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100352static int
353xfs_btree_free_block(
354 struct xfs_btree_cur *cur,
355 struct xfs_buf *bp)
356{
357 int error;
358
359 error = cur->bc_ops->free_block(cur, bp);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100360 if (!error) {
361 xfs_trans_binval(cur->bc_tp, bp);
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100362 XFS_BTREE_STATS_INC(cur, free);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100363 }
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100364 return error;
365}
366
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500367/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * Delete the btree cursor.
369 */
370void
371xfs_btree_del_cursor(
372 xfs_btree_cur_t *cur, /* btree cursor */
373 int error) /* del because of error */
374{
375 int i; /* btree level */
376
377 /*
378 * Clear the buffer pointers, and release the buffers.
379 * If we're doing this in the face of an error, we
380 * need to make sure to inspect all of the entries
381 * in the bc_bufs array for buffers to be unlocked.
382 * This is because some of the btree code works from
383 * level n down to 0, and if we get an error along
384 * the way we won't have initialized all the entries
385 * down to 0.
386 */
387 for (i = 0; i < cur->bc_nlevels; i++) {
388 if (cur->bc_bufs[i])
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000389 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 else if (!error)
391 break;
392 }
393 /*
394 * Can't free a bmap cursor without having dealt with the
395 * allocated indirect blocks' accounting.
396 */
397 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
398 cur->bc_private.b.allocated == 0);
399 /*
400 * Free the cursor.
401 */
402 kmem_zone_free(xfs_btree_cur_zone, cur);
403}
404
405/*
406 * Duplicate the btree cursor.
407 * Allocate a new one, copy the record, re-get the buffers.
408 */
409int /* error */
410xfs_btree_dup_cursor(
411 xfs_btree_cur_t *cur, /* input cursor */
412 xfs_btree_cur_t **ncur) /* output cursor */
413{
414 xfs_buf_t *bp; /* btree block's buffer pointer */
415 int error; /* error return value */
416 int i; /* level number of btree block */
417 xfs_mount_t *mp; /* mount structure for filesystem */
418 xfs_btree_cur_t *new; /* new cursor value */
419 xfs_trans_t *tp; /* transaction pointer, can be NULL */
420
421 tp = cur->bc_tp;
422 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
425 * Allocate a new cursor like the old one.
426 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100427 new = cur->bc_ops->dup_cursor(cur);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
430 * Copy the record currently in the cursor.
431 */
432 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
435 * For each level current, re-get the buffer and copy the ptr value.
436 */
437 for (i = 0; i < new->bc_nlevels; i++) {
438 new->bc_ptrs[i] = cur->bc_ptrs[i];
439 new->bc_ra[i] = cur->bc_ra[i];
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100440 bp = cur->bc_bufs[i];
441 if (bp) {
442 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
443 XFS_BUF_ADDR(bp), mp->m_bsize,
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100444 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100445 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100446 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 xfs_btree_del_cursor(new, error);
448 *ncur = NULL;
449 return error;
450 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500451 }
452 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *ncur = new;
455 return 0;
456}
457
458/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100459 * XFS btree block layout and addressing:
460 *
461 * There are two types of blocks in the btree: leaf and non-leaf blocks.
462 *
463 * The leaf record start with a header then followed by records containing
464 * the values. A non-leaf block also starts with the same header, and
465 * then first contains lookup keys followed by an equal number of pointers
466 * to the btree blocks at the previous level.
467 *
468 * +--------+-------+-------+-------+-------+-------+-------+
469 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
470 * +--------+-------+-------+-------+-------+-------+-------+
471 *
472 * +--------+-------+-------+-------+-------+-------+-------+
473 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
474 * +--------+-------+-------+-------+-------+-------+-------+
475 *
476 * The header is called struct xfs_btree_block for reasons better left unknown
477 * and comes in different versions for short (32bit) and long (64bit) block
478 * pointers. The record and key structures are defined by the btree instances
479 * and opaque to the btree core. The block pointers are simple disk endian
480 * integers, available in a short (32bit) and long (64bit) variant.
481 *
482 * The helpers below calculate the offset of a given record, key or pointer
483 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
484 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
485 * inside the btree block is done using indices starting at one, not zero!
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000486 *
487 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
488 * overlapping intervals. In such a tree, records are still sorted lowest to
489 * highest and indexed by the smallest key value that refers to the record.
490 * However, nodes are different: each pointer has two associated keys -- one
491 * indexing the lowest key available in the block(s) below (the same behavior
492 * as the key in a regular btree) and another indexing the highest key
493 * available in the block(s) below. Because records are /not/ sorted by the
494 * highest key, all leaf block updates require us to compute the highest key
495 * that matches any record in the leaf and to recursively update the high keys
496 * in the nodes going further up in the tree, if necessary. Nodes look like
497 * this:
498 *
499 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
500 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
501 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
502 *
503 * To perform an interval query on an overlapped tree, perform the usual
504 * depth-first search and use the low and high keys to decide if we can skip
505 * that particular node. If a leaf node is reached, return the records that
506 * intersect the interval. Note that an interval query may return numerous
507 * entries. For a non-overlapped tree, simply search for the record associated
508 * with the lowest key and iterate forward until a non-matching record is
509 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
510 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
511 * more detail.
512 *
513 * Why do we care about overlapping intervals? Let's say you have a bunch of
514 * reverse mapping records on a reflink filesystem:
515 *
516 * 1: +- file A startblock B offset C length D -----------+
517 * 2: +- file E startblock F offset G length H --------------+
518 * 3: +- file I startblock F offset J length K --+
519 * 4: +- file L... --+
520 *
521 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
522 * we'd simply increment the length of record 1. But how do we find the record
523 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
524 * record 3 because the keys are ordered first by startblock. An interval
525 * query would return records 1 and 2 because they both overlap (B+D-1), and
526 * from that we can pick out record 1 as the appropriate left neighbor.
527 *
528 * In the non-overlapped case you can do a LE lookup and decrement the cursor
529 * because a record's interval must end before the next record.
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100530 */
531
532/*
533 * Return size of the btree block header for this btree instance.
534 */
535static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
536{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500537 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
538 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
539 return XFS_BTREE_LBLOCK_CRC_LEN;
540 return XFS_BTREE_LBLOCK_LEN;
541 }
542 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
543 return XFS_BTREE_SBLOCK_CRC_LEN;
544 return XFS_BTREE_SBLOCK_LEN;
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100545}
546
547/*
548 * Return size of btree block pointers for this btree instance.
549 */
550static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
551{
552 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
553 sizeof(__be64) : sizeof(__be32);
554}
555
556/*
557 * Calculate offset of the n-th record in a btree block.
558 */
559STATIC size_t
560xfs_btree_rec_offset(
561 struct xfs_btree_cur *cur,
562 int n)
563{
564 return xfs_btree_block_len(cur) +
565 (n - 1) * cur->bc_ops->rec_len;
566}
567
568/*
569 * Calculate offset of the n-th key in a btree block.
570 */
571STATIC size_t
572xfs_btree_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;
578}
579
580/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000581 * Calculate offset of the n-th high key in a btree block.
582 */
583STATIC size_t
584xfs_btree_high_key_offset(
585 struct xfs_btree_cur *cur,
586 int n)
587{
588 return xfs_btree_block_len(cur) +
589 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
590}
591
592/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100593 * Calculate offset of the n-th block pointer in a btree block.
594 */
595STATIC size_t
596xfs_btree_ptr_offset(
597 struct xfs_btree_cur *cur,
598 int n,
599 int level)
600{
601 return xfs_btree_block_len(cur) +
602 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
603 (n - 1) * xfs_btree_ptr_len(cur);
604}
605
606/*
607 * Return a pointer to the n-th record in the btree block.
608 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700609union xfs_btree_rec *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100610xfs_btree_rec_addr(
611 struct xfs_btree_cur *cur,
612 int n,
613 struct xfs_btree_block *block)
614{
615 return (union xfs_btree_rec *)
616 ((char *)block + xfs_btree_rec_offset(cur, n));
617}
618
619/*
620 * Return a pointer to the n-th key in the btree block.
621 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700622union xfs_btree_key *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100623xfs_btree_key_addr(
624 struct xfs_btree_cur *cur,
625 int n,
626 struct xfs_btree_block *block)
627{
628 return (union xfs_btree_key *)
629 ((char *)block + xfs_btree_key_offset(cur, n));
630}
631
632/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000633 * Return a pointer to the n-th high key in the btree block.
634 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700635union xfs_btree_key *
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000636xfs_btree_high_key_addr(
637 struct xfs_btree_cur *cur,
638 int n,
639 struct xfs_btree_block *block)
640{
641 return (union xfs_btree_key *)
642 ((char *)block + xfs_btree_high_key_offset(cur, n));
643}
644
645/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100646 * Return a pointer to the n-th block pointer in the btree block.
647 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700648union xfs_btree_ptr *
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100649xfs_btree_ptr_addr(
650 struct xfs_btree_cur *cur,
651 int n,
652 struct xfs_btree_block *block)
653{
654 int level = xfs_btree_get_level(block);
655
656 ASSERT(block->bb_level != 0);
657
658 return (union xfs_btree_ptr *)
659 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
660}
661
662/*
Zhi Yong Wu1cb93862013-08-07 10:11:05 +0000663 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100664 *
665 * For now this btree implementation assumes the btree root is always
666 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100668STATIC struct xfs_btree_block *
669xfs_btree_get_iroot(
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000670 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000672 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000674 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
675 return (struct xfs_btree_block *)ifp->if_broot;
Christoph Hellwig8186e512008-10-30 16:54:22 +1100676}
677
678/*
679 * Retrieve the block pointer from the cursor at the given level.
680 * This may be an inode btree root or from a buffer.
681 */
Darrick J. Wong26788092017-06-16 11:00:07 -0700682struct xfs_btree_block * /* generic btree block pointer */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100683xfs_btree_get_block(
684 struct xfs_btree_cur *cur, /* btree cursor */
685 int level, /* level in btree */
686 struct xfs_buf **bpp) /* buffer containing the block */
687{
688 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
689 (level == cur->bc_nlevels - 1)) {
690 *bpp = NULL;
691 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100693
694 *bpp = cur->bc_bufs[level];
695 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698/*
699 * Get a buffer for the block, return it with no data read.
700 * Long-form addressing.
701 */
702xfs_buf_t * /* buffer for fsbno */
703xfs_btree_get_bufl(
704 xfs_mount_t *mp, /* file system mount point */
705 xfs_trans_t *tp, /* transaction pointer */
706 xfs_fsblock_t fsbno, /* file system block number */
707 uint lock) /* lock flags for get_buf */
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfs_daddr_t d; /* real disk block address */
710
711 ASSERT(fsbno != NULLFSBLOCK);
712 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000713 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*
717 * Get a buffer for the block, return it with no data read.
718 * Short-form addressing.
719 */
720xfs_buf_t * /* buffer for agno/agbno */
721xfs_btree_get_bufs(
722 xfs_mount_t *mp, /* file system mount point */
723 xfs_trans_t *tp, /* transaction pointer */
724 xfs_agnumber_t agno, /* allocation group number */
725 xfs_agblock_t agbno, /* allocation group block number */
726 uint lock) /* lock flags for get_buf */
727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 xfs_daddr_t d; /* real disk block address */
729
730 ASSERT(agno != NULLAGNUMBER);
731 ASSERT(agbno != NULLAGBLOCK);
732 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000733 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 * Check for the cursor referring to the last block at the given level.
738 */
739int /* 1=is last block, 0=not last block */
740xfs_btree_islastblock(
741 xfs_btree_cur_t *cur, /* btree cursor */
742 int level) /* level to check */
743{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100744 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 xfs_buf_t *bp; /* buffer containing block */
746
747 block = xfs_btree_get_block(cur, level, &bp);
748 xfs_btree_check_block(cur, block, level, bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100749 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000750 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200752 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000756 * Change the cursor to point to the first record at the given level.
757 * Other levels are unaffected.
758 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100759STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000760xfs_btree_firstrec(
761 xfs_btree_cur_t *cur, /* btree cursor */
762 int level) /* level to change */
763{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100764 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000765 xfs_buf_t *bp; /* buffer containing block */
766
767 /*
768 * Get the block pointer for this level.
769 */
770 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong1e86eab2017-07-17 14:30:45 -0700771 if (xfs_btree_check_block(cur, block, level, bp))
772 return 0;
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000773 /*
774 * It's empty, there is no such record.
775 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100776 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000777 return 0;
778 /*
779 * Set the ptr value to 1, that's the first record/key.
780 */
781 cur->bc_ptrs[level] = 1;
782 return 1;
783}
784
785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * Change the cursor to point to the last record in the current block
787 * at the given level. Other levels are unaffected.
788 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100789STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790xfs_btree_lastrec(
791 xfs_btree_cur_t *cur, /* btree cursor */
792 int level) /* level to change */
793{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100794 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 xfs_buf_t *bp; /* buffer containing block */
796
797 /*
798 * Get the block pointer for this level.
799 */
800 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong1e86eab2017-07-17 14:30:45 -0700801 if (xfs_btree_check_block(cur, block, level, bp))
802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /*
804 * It's empty, there is no such record.
805 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100806 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808 /*
809 * Set the ptr value to numrecs, that's the last record/key.
810 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100811 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 1;
813}
814
815/*
816 * Compute first and last byte offsets for the fields given.
817 * Interprets the offsets table, which contains struct field offsets.
818 */
819void
820xfs_btree_offsets(
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700821 int64_t fields, /* bitmask of fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 const short *offsets, /* table of field offsets */
823 int nbits, /* number of bits to inspect */
824 int *first, /* output: first byte offset */
825 int *last) /* output: last byte offset */
826{
827 int i; /* current bit number */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700828 int64_t imask; /* mask for current bit number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 ASSERT(fields != 0);
831 /*
832 * Find the lowest bit, so the first byte offset.
833 */
834 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
835 if (imask & fields) {
836 *first = offsets[i];
837 break;
838 }
839 }
840 /*
841 * Find the highest bit, so the last byte offset.
842 */
843 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
844 if (imask & fields) {
845 *last = offsets[i + 1] - 1;
846 break;
847 }
848 }
849}
850
851/*
852 * Get a buffer for the block, return it read in.
853 * Long-form addressing.
854 */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100855int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100857 struct xfs_mount *mp, /* file system mount point */
858 struct xfs_trans *tp, /* transaction pointer */
859 xfs_fsblock_t fsbno, /* file system block number */
860 uint lock, /* lock flags for read_buf */
861 struct xfs_buf **bpp, /* buffer for fsbno */
862 int refval, /* ref count value for buffer */
Dave Chinner1813dd62012-11-14 17:54:40 +1100863 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100865 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100867 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800869 if (!xfs_verify_fsbno(mp, fsbno))
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -0800870 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100872 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Dave Chinner1813dd62012-11-14 17:54:40 +1100873 mp->m_bsize, lock, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100874 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return error;
Dave Chinner821eb212010-12-02 16:31:13 +1100876 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000877 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 *bpp = bp;
879 return 0;
880}
881
882/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Read-ahead the block, don't wait for it, don't return a buffer.
884 * Long-form addressing.
885 */
886/* ARGSUSED */
887void
888xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100889 struct xfs_mount *mp, /* file system mount point */
890 xfs_fsblock_t fsbno, /* file system block number */
891 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100892 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 xfs_daddr_t d;
895
896 ASSERT(fsbno != NULLFSBLOCK);
897 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100898 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/*
902 * Read-ahead the block, don't wait for it, don't return a buffer.
903 * Short-form addressing.
904 */
905/* ARGSUSED */
906void
907xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100908 struct xfs_mount *mp, /* file system mount point */
909 xfs_agnumber_t agno, /* allocation group number */
910 xfs_agblock_t agbno, /* allocation group block number */
911 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100912 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 xfs_daddr_t d;
915
916 ASSERT(agno != NULLAGNUMBER);
917 ASSERT(agbno != NULLAGBLOCK);
918 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100919 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100922STATIC int
923xfs_btree_readahead_lblock(
924 struct xfs_btree_cur *cur,
925 int lr,
926 struct xfs_btree_block *block)
927{
928 int rval = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000929 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
930 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100931
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000932 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100933 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100934 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100935 rval++;
936 }
937
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000938 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100939 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100940 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100941 rval++;
942 }
943
944 return rval;
945}
946
947STATIC int
948xfs_btree_readahead_sblock(
949 struct xfs_btree_cur *cur,
950 int lr,
951 struct xfs_btree_block *block)
952{
953 int rval = 0;
954 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
955 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
956
957
958 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
959 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100960 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100961 rval++;
962 }
963
964 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
965 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100966 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100967 rval++;
968 }
969
970 return rval;
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/*
974 * Read-ahead btree blocks, at the given level.
975 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
976 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100977STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100978xfs_btree_readahead(
979 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int lev, /* level in btree */
981 int lr) /* left/right bits */
982{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100983 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100985 /*
986 * No readahead needed if we are at the root level and the
987 * btree root is stored in the inode.
988 */
989 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
990 (lev == cur->bc_nlevels - 1))
991 return 0;
992
993 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
994 return 0;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100997 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
998
999 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1000 return xfs_btree_readahead_lblock(cur, lr, block);
1001 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001004STATIC int
Dave Chinner21b5c972013-08-30 10:23:44 +10001005xfs_btree_ptr_to_daddr(
1006 struct xfs_btree_cur *cur,
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001007 union xfs_btree_ptr *ptr,
1008 xfs_daddr_t *daddr)
Dave Chinner21b5c972013-08-30 10:23:44 +10001009{
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001010 xfs_fsblock_t fsbno;
1011 xfs_agblock_t agbno;
1012 int error;
1013
1014 error = xfs_btree_check_ptr(cur, ptr, 0, 1);
1015 if (error)
1016 return error;
1017
Dave Chinner21b5c972013-08-30 10:23:44 +10001018 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001019 fsbno = be64_to_cpu(ptr->l);
1020 *daddr = XFS_FSB_TO_DADDR(cur->bc_mp, fsbno);
Dave Chinner21b5c972013-08-30 10:23:44 +10001021 } else {
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001022 agbno = be32_to_cpu(ptr->s);
1023 *daddr = XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
1024 agbno);
Dave Chinner21b5c972013-08-30 10:23:44 +10001025 }
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001026
1027 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10001028}
1029
1030/*
1031 * Readahead @count btree blocks at the given @ptr location.
1032 *
1033 * We don't need to care about long or short form btrees here as we have a
1034 * method of converting the ptr directly to a daddr available to us.
1035 */
1036STATIC void
1037xfs_btree_readahead_ptr(
1038 struct xfs_btree_cur *cur,
1039 union xfs_btree_ptr *ptr,
1040 xfs_extlen_t count)
1041{
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001042 xfs_daddr_t daddr;
1043
1044 if (xfs_btree_ptr_to_daddr(cur, ptr, &daddr))
1045 return;
1046 xfs_buf_readahead(cur->bc_mp->m_ddev_targp, daddr,
Dave Chinner21b5c972013-08-30 10:23:44 +10001047 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*
1051 * Set the buffer for level "lev" in the cursor to bp, releasing
1052 * any previous buffer.
1053 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001054STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055xfs_btree_setbuf(
1056 xfs_btree_cur_t *cur, /* btree cursor */
1057 int lev, /* level in btree */
1058 xfs_buf_t *bp) /* new buffer to set */
1059{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001060 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001062 if (cur->bc_bufs[lev])
1063 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 cur->bc_bufs[lev] = bp;
1065 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +11001068 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001069 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001071 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1073 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001074 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001076 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1078 }
1079}
Christoph Hellwig637aa502008-10-30 16:55:45 +11001080
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07001081bool
Christoph Hellwig637aa502008-10-30 16:55:45 +11001082xfs_btree_ptr_is_null(
1083 struct xfs_btree_cur *cur,
1084 union xfs_btree_ptr *ptr)
1085{
1086 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001087 return ptr->l == cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001088 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001089 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001090}
1091
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001092STATIC void
1093xfs_btree_set_ptr_null(
1094 struct xfs_btree_cur *cur,
1095 union xfs_btree_ptr *ptr)
1096{
1097 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001098 ptr->l = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001099 else
1100 ptr->s = cpu_to_be32(NULLAGBLOCK);
1101}
1102
Christoph Hellwig637aa502008-10-30 16:55:45 +11001103/*
1104 * Get/set/init sibling pointers
1105 */
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07001106void
Christoph Hellwig637aa502008-10-30 16:55:45 +11001107xfs_btree_get_sibling(
1108 struct xfs_btree_cur *cur,
1109 struct xfs_btree_block *block,
1110 union xfs_btree_ptr *ptr,
1111 int lr)
1112{
1113 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1114
1115 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1116 if (lr == XFS_BB_RIGHTSIB)
1117 ptr->l = block->bb_u.l.bb_rightsib;
1118 else
1119 ptr->l = block->bb_u.l.bb_leftsib;
1120 } else {
1121 if (lr == XFS_BB_RIGHTSIB)
1122 ptr->s = block->bb_u.s.bb_rightsib;
1123 else
1124 ptr->s = block->bb_u.s.bb_leftsib;
1125 }
1126}
1127
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001128STATIC void
1129xfs_btree_set_sibling(
1130 struct xfs_btree_cur *cur,
1131 struct xfs_btree_block *block,
1132 union xfs_btree_ptr *ptr,
1133 int lr)
1134{
1135 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1136
1137 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1138 if (lr == XFS_BB_RIGHTSIB)
1139 block->bb_u.l.bb_rightsib = ptr->l;
1140 else
1141 block->bb_u.l.bb_leftsib = ptr->l;
1142 } else {
1143 if (lr == XFS_BB_RIGHTSIB)
1144 block->bb_u.s.bb_rightsib = ptr->s;
1145 else
1146 block->bb_u.s.bb_leftsib = ptr->s;
1147 }
1148}
1149
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001150void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001151xfs_btree_init_block_int(
1152 struct xfs_mount *mp,
1153 struct xfs_btree_block *buf,
1154 xfs_daddr_t blkno,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001155 xfs_btnum_t btnum,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001156 __u16 level,
1157 __u16 numrecs,
1158 __u64 owner,
1159 unsigned int flags)
1160{
Eric Sandeenf88ae462017-01-27 23:16:37 -08001161 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001162 __u32 magic = xfs_btree_magic(crc, btnum);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001163
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001164 buf->bb_magic = cpu_to_be32(magic);
1165 buf->bb_level = cpu_to_be16(level);
1166 buf->bb_numrecs = cpu_to_be16(numrecs);
1167
1168 if (flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001169 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1170 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001171 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001172 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1173 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001174 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001175 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001176 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001177 }
1178 } else {
1179 /* owner is a 32 bit value on short blocks */
1180 __u32 __owner = (__u32)owner;
1181
1182 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1183 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001184 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001185 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1186 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001187 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001188 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001189 }
1190 }
1191}
1192
1193void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001194xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001195 struct xfs_mount *mp,
1196 struct xfs_buf *bp,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001197 xfs_btnum_t btnum,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001198 __u16 level,
1199 __u16 numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001200 __u64 owner,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001201 unsigned int flags)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001202{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001203 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001204 btnum, level, numrecs, owner, flags);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001205}
1206
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001207STATIC void
1208xfs_btree_init_block_cur(
1209 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001210 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001211 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001212 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001213{
Eric Sandeenaf7d20f2017-01-27 23:16:38 -08001214 __u64 owner;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001215
1216 /*
1217 * we can pull the owner from the cursor right now as the different
1218 * owners align directly with the pointer size of the btree. This may
1219 * change in future, but is safe for current users of the generic btree
1220 * code.
1221 */
1222 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1223 owner = cur->bc_private.b.ip->i_ino;
1224 else
1225 owner = cur->bc_private.a.agno;
1226
1227 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001228 cur->bc_btnum, level, numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001229 owner, cur->bc_flags);
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001230}
1231
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001232/*
1233 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001234 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001235 * will be further refined in the update_lastrec method.
1236 */
1237STATIC int
1238xfs_btree_is_lastrec(
1239 struct xfs_btree_cur *cur,
1240 struct xfs_btree_block *block,
1241 int level)
1242{
1243 union xfs_btree_ptr ptr;
1244
1245 if (level > 0)
1246 return 0;
1247 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1248 return 0;
1249
1250 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1251 if (!xfs_btree_ptr_is_null(cur, &ptr))
1252 return 0;
1253 return 1;
1254}
1255
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001256STATIC void
1257xfs_btree_buf_to_ptr(
1258 struct xfs_btree_cur *cur,
1259 struct xfs_buf *bp,
1260 union xfs_btree_ptr *ptr)
1261{
1262 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1263 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1264 XFS_BUF_ADDR(bp)));
1265 else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001266 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001267 XFS_BUF_ADDR(bp)));
1268 }
1269}
1270
Christoph Hellwig637aa502008-10-30 16:55:45 +11001271STATIC void
1272xfs_btree_set_refs(
1273 struct xfs_btree_cur *cur,
1274 struct xfs_buf *bp)
1275{
1276 switch (cur->bc_btnum) {
1277 case XFS_BTNUM_BNO:
1278 case XFS_BTNUM_CNT:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001279 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001280 break;
1281 case XFS_BTNUM_INO:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001282 case XFS_BTNUM_FINO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001283 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001284 break;
1285 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001286 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001287 break;
Darrick J. Wong035e00a2016-08-03 11:36:07 +10001288 case XFS_BTNUM_RMAP:
1289 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1290 break;
Darrick J. Wong1946b912016-10-03 09:11:18 -07001291 case XFS_BTNUM_REFC:
1292 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1293 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001294 default:
1295 ASSERT(0);
1296 }
1297}
1298
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001299STATIC int
1300xfs_btree_get_buf_block(
1301 struct xfs_btree_cur *cur,
1302 union xfs_btree_ptr *ptr,
1303 int flags,
1304 struct xfs_btree_block **block,
1305 struct xfs_buf **bpp)
1306{
1307 struct xfs_mount *mp = cur->bc_mp;
1308 xfs_daddr_t d;
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001309 int error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001310
1311 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001312 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001313
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001314 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1315 if (error)
1316 return error;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001317 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1318 mp->m_bsize, flags);
1319
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001320 if (!*bpp)
Dave Chinner24513372014-06-25 14:58:08 +10001321 return -ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001322
Dave Chinner1813dd62012-11-14 17:54:40 +11001323 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001324 *block = XFS_BUF_TO_BLOCK(*bpp);
1325 return 0;
1326}
1327
Christoph Hellwig637aa502008-10-30 16:55:45 +11001328/*
1329 * Read in the buffer at the given ptr and return the buffer and
1330 * the block pointer within the buffer.
1331 */
1332STATIC int
1333xfs_btree_read_buf_block(
1334 struct xfs_btree_cur *cur,
1335 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001336 int flags,
1337 struct xfs_btree_block **block,
1338 struct xfs_buf **bpp)
1339{
1340 struct xfs_mount *mp = cur->bc_mp;
1341 xfs_daddr_t d;
1342 int error;
1343
1344 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001345 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001346
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001347 error = xfs_btree_ptr_to_daddr(cur, ptr, &d);
1348 if (error)
1349 return error;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001350 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001351 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001352 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001353 if (error)
1354 return error;
1355
Christoph Hellwig637aa502008-10-30 16:55:45 +11001356 xfs_btree_set_refs(cur, *bpp);
1357 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001358 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001359}
1360
1361/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001362 * Copy keys from one btree block to another.
1363 */
1364STATIC void
1365xfs_btree_copy_keys(
1366 struct xfs_btree_cur *cur,
1367 union xfs_btree_key *dst_key,
1368 union xfs_btree_key *src_key,
1369 int numkeys)
1370{
1371 ASSERT(numkeys >= 0);
1372 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1373}
1374
1375/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001376 * Copy records from one btree block to another.
1377 */
1378STATIC void
1379xfs_btree_copy_recs(
1380 struct xfs_btree_cur *cur,
1381 union xfs_btree_rec *dst_rec,
1382 union xfs_btree_rec *src_rec,
1383 int numrecs)
1384{
1385 ASSERT(numrecs >= 0);
1386 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1387}
1388
1389/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001390 * Copy block pointers from one btree block to another.
1391 */
1392STATIC void
1393xfs_btree_copy_ptrs(
1394 struct xfs_btree_cur *cur,
1395 union xfs_btree_ptr *dst_ptr,
1396 union xfs_btree_ptr *src_ptr,
1397 int numptrs)
1398{
1399 ASSERT(numptrs >= 0);
1400 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1401}
1402
1403/*
1404 * Shift keys one index left/right inside a single btree block.
1405 */
1406STATIC void
1407xfs_btree_shift_keys(
1408 struct xfs_btree_cur *cur,
1409 union xfs_btree_key *key,
1410 int dir,
1411 int numkeys)
1412{
1413 char *dst_key;
1414
1415 ASSERT(numkeys >= 0);
1416 ASSERT(dir == 1 || dir == -1);
1417
1418 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1419 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1420}
1421
1422/*
1423 * Shift records one index left/right inside a single btree block.
1424 */
1425STATIC void
1426xfs_btree_shift_recs(
1427 struct xfs_btree_cur *cur,
1428 union xfs_btree_rec *rec,
1429 int dir,
1430 int numrecs)
1431{
1432 char *dst_rec;
1433
1434 ASSERT(numrecs >= 0);
1435 ASSERT(dir == 1 || dir == -1);
1436
1437 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1438 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1439}
1440
1441/*
1442 * Shift block pointers one index left/right inside a single btree block.
1443 */
1444STATIC void
1445xfs_btree_shift_ptrs(
1446 struct xfs_btree_cur *cur,
1447 union xfs_btree_ptr *ptr,
1448 int dir,
1449 int numptrs)
1450{
1451 char *dst_ptr;
1452
1453 ASSERT(numptrs >= 0);
1454 ASSERT(dir == 1 || dir == -1);
1455
1456 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1457 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1458}
1459
1460/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001461 * Log key values from the btree block.
1462 */
1463STATIC void
1464xfs_btree_log_keys(
1465 struct xfs_btree_cur *cur,
1466 struct xfs_buf *bp,
1467 int first,
1468 int last)
1469{
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001470
1471 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001472 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001473 xfs_trans_log_buf(cur->bc_tp, bp,
1474 xfs_btree_key_offset(cur, first),
1475 xfs_btree_key_offset(cur, last + 1) - 1);
1476 } else {
1477 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1478 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1479 }
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001480}
1481
1482/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001483 * Log record values from the btree block.
1484 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001485void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001486xfs_btree_log_recs(
1487 struct xfs_btree_cur *cur,
1488 struct xfs_buf *bp,
1489 int first,
1490 int last)
1491{
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001492
Dave Chinner61fe1352013-04-03 16:11:30 +11001493 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001494 xfs_trans_log_buf(cur->bc_tp, bp,
1495 xfs_btree_rec_offset(cur, first),
1496 xfs_btree_rec_offset(cur, last + 1) - 1);
1497
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001498}
1499
1500/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001501 * Log block pointer fields from a btree block (nonleaf).
1502 */
1503STATIC void
1504xfs_btree_log_ptrs(
1505 struct xfs_btree_cur *cur, /* btree cursor */
1506 struct xfs_buf *bp, /* buffer containing btree block */
1507 int first, /* index of first pointer to log */
1508 int last) /* index of last pointer to log */
1509{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001510
1511 if (bp) {
1512 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1513 int level = xfs_btree_get_level(block);
1514
Dave Chinner61fe1352013-04-03 16:11:30 +11001515 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001516 xfs_trans_log_buf(cur->bc_tp, bp,
1517 xfs_btree_ptr_offset(cur, first, level),
1518 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1519 } else {
1520 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1521 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1522 }
1523
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001524}
1525
1526/*
1527 * Log fields from a btree block header.
1528 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001529void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001530xfs_btree_log_block(
1531 struct xfs_btree_cur *cur, /* btree cursor */
1532 struct xfs_buf *bp, /* buffer containing btree block */
1533 int fields) /* mask of fields: XFS_BB_... */
1534{
1535 int first; /* first byte offset logged */
1536 int last; /* last byte offset logged */
1537 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001538 offsetof(struct xfs_btree_block, bb_magic),
1539 offsetof(struct xfs_btree_block, bb_level),
1540 offsetof(struct xfs_btree_block, bb_numrecs),
1541 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1542 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001543 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1544 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1545 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1546 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1547 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1548 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001549 };
1550 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001551 offsetof(struct xfs_btree_block, bb_magic),
1552 offsetof(struct xfs_btree_block, bb_level),
1553 offsetof(struct xfs_btree_block, bb_numrecs),
1554 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1555 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001556 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1557 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1558 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1559 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1560 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1561 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1562 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001563 };
1564
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001565 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001566 int nbits;
1567
1568 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1569 /*
1570 * We don't log the CRC when updating a btree
1571 * block but instead recreate it during log
1572 * recovery. As the log buffers have checksums
1573 * of their own this is safe and avoids logging a crc
1574 * update in a lot of places.
1575 */
1576 if (fields == XFS_BB_ALL_BITS)
1577 fields = XFS_BB_ALL_BITS_CRC;
1578 nbits = XFS_BB_NUM_BITS_CRC;
1579 } else {
1580 nbits = XFS_BB_NUM_BITS;
1581 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001582 xfs_btree_offsets(fields,
1583 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1584 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001585 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001586 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001587 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1588 } else {
1589 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1590 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1591 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001592}
1593
1594/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001595 * Increment cursor by one record at the level.
1596 * For nonzero levels the leaf-ward information is untouched.
1597 */
1598int /* error */
1599xfs_btree_increment(
1600 struct xfs_btree_cur *cur,
1601 int level,
1602 int *stat) /* success/failure */
1603{
1604 struct xfs_btree_block *block;
1605 union xfs_btree_ptr ptr;
1606 struct xfs_buf *bp;
1607 int error; /* error return value */
1608 int lev;
1609
Christoph Hellwig637aa502008-10-30 16:55:45 +11001610 ASSERT(level < cur->bc_nlevels);
1611
1612 /* Read-ahead to the right at this level. */
1613 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1614
1615 /* Get a pointer to the btree block. */
1616 block = xfs_btree_get_block(cur, level, &bp);
1617
1618#ifdef DEBUG
1619 error = xfs_btree_check_block(cur, block, level, bp);
1620 if (error)
1621 goto error0;
1622#endif
1623
1624 /* We're done if we remain in the block after the increment. */
1625 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1626 goto out1;
1627
1628 /* Fail if we just went off the right edge of the tree. */
1629 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1630 if (xfs_btree_ptr_is_null(cur, &ptr))
1631 goto out0;
1632
1633 XFS_BTREE_STATS_INC(cur, increment);
1634
1635 /*
1636 * March up the tree incrementing pointers.
1637 * Stop when we don't go off the right edge of a block.
1638 */
1639 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1640 block = xfs_btree_get_block(cur, lev, &bp);
1641
1642#ifdef DEBUG
1643 error = xfs_btree_check_block(cur, block, lev, bp);
1644 if (error)
1645 goto error0;
1646#endif
1647
1648 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1649 break;
1650
1651 /* Read-ahead the right block for the next loop. */
1652 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1653 }
1654
1655 /*
1656 * If we went off the root then we are either seriously
1657 * confused or have the tree root in an inode.
1658 */
1659 if (lev == cur->bc_nlevels) {
1660 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1661 goto out0;
1662 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001663 error = -EFSCORRUPTED;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001664 goto error0;
1665 }
1666 ASSERT(lev < cur->bc_nlevels);
1667
1668 /*
1669 * Now walk back down the tree, fixing up the cursor's buffer
1670 * pointers and key numbers.
1671 */
1672 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1673 union xfs_btree_ptr *ptrp;
1674
1675 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001676 --lev;
1677 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001678 if (error)
1679 goto error0;
1680
1681 xfs_btree_setbuf(cur, lev, bp);
1682 cur->bc_ptrs[lev] = 1;
1683 }
1684out1:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001685 *stat = 1;
1686 return 0;
1687
1688out0:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001689 *stat = 0;
1690 return 0;
1691
1692error0:
Christoph Hellwig637aa502008-10-30 16:55:45 +11001693 return error;
1694}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001695
1696/*
1697 * Decrement cursor by one record at the level.
1698 * For nonzero levels the leaf-ward information is untouched.
1699 */
1700int /* error */
1701xfs_btree_decrement(
1702 struct xfs_btree_cur *cur,
1703 int level,
1704 int *stat) /* success/failure */
1705{
1706 struct xfs_btree_block *block;
1707 xfs_buf_t *bp;
1708 int error; /* error return value */
1709 int lev;
1710 union xfs_btree_ptr ptr;
1711
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001712 ASSERT(level < cur->bc_nlevels);
1713
1714 /* Read-ahead to the left at this level. */
1715 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1716
1717 /* We're done if we remain in the block after the decrement. */
1718 if (--cur->bc_ptrs[level] > 0)
1719 goto out1;
1720
1721 /* Get a pointer to the btree block. */
1722 block = xfs_btree_get_block(cur, level, &bp);
1723
1724#ifdef DEBUG
1725 error = xfs_btree_check_block(cur, block, level, bp);
1726 if (error)
1727 goto error0;
1728#endif
1729
1730 /* Fail if we just went off the left edge of the tree. */
1731 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1732 if (xfs_btree_ptr_is_null(cur, &ptr))
1733 goto out0;
1734
1735 XFS_BTREE_STATS_INC(cur, decrement);
1736
1737 /*
1738 * March up the tree decrementing pointers.
1739 * Stop when we don't go off the left edge of a block.
1740 */
1741 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1742 if (--cur->bc_ptrs[lev] > 0)
1743 break;
1744 /* Read-ahead the left block for the next loop. */
1745 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1746 }
1747
1748 /*
1749 * If we went off the root then we are seriously confused.
1750 * or the root of the tree is in an inode.
1751 */
1752 if (lev == cur->bc_nlevels) {
1753 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1754 goto out0;
1755 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001756 error = -EFSCORRUPTED;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001757 goto error0;
1758 }
1759 ASSERT(lev < cur->bc_nlevels);
1760
1761 /*
1762 * Now walk back down the tree, fixing up the cursor's buffer
1763 * pointers and key numbers.
1764 */
1765 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1766 union xfs_btree_ptr *ptrp;
1767
1768 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001769 --lev;
1770 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001771 if (error)
1772 goto error0;
1773 xfs_btree_setbuf(cur, lev, bp);
1774 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1775 }
1776out1:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001777 *stat = 1;
1778 return 0;
1779
1780out0:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001781 *stat = 0;
1782 return 0;
1783
1784error0:
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001785 return error;
1786}
1787
Darrick J. Wong26788092017-06-16 11:00:07 -07001788int
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001789xfs_btree_lookup_get_block(
1790 struct xfs_btree_cur *cur, /* btree cursor */
1791 int level, /* level in the btree */
1792 union xfs_btree_ptr *pp, /* ptr to btree block */
1793 struct xfs_btree_block **blkp) /* return btree block */
1794{
1795 struct xfs_buf *bp; /* buffer pointer for btree block */
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001796 xfs_daddr_t daddr;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001797 int error = 0;
1798
1799 /* special case the root block if in an inode */
1800 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1801 (level == cur->bc_nlevels - 1)) {
1802 *blkp = xfs_btree_get_iroot(cur);
1803 return 0;
1804 }
1805
1806 /*
1807 * If the old buffer at this level for the disk address we are
1808 * looking for re-use it.
1809 *
1810 * Otherwise throw it away and get a new one.
1811 */
1812 bp = cur->bc_bufs[level];
Darrick J. Wonge63a1002018-06-04 13:58:34 -07001813 error = xfs_btree_ptr_to_daddr(cur, pp, &daddr);
1814 if (error)
1815 return error;
1816 if (bp && XFS_BUF_ADDR(bp) == daddr) {
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001817 *blkp = XFS_BUF_TO_BLOCK(bp);
1818 return 0;
1819 }
1820
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001821 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001822 if (error)
1823 return error;
1824
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001825 /* Check the inode owner since the verifiers don't. */
1826 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
Brian Foster99c794c2017-08-29 10:08:39 -07001827 !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_INVALID_OWNER) &&
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001828 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1829 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1830 cur->bc_private.b.ip->i_ino)
1831 goto out_bad;
1832
1833 /* Did we get the level we were looking for? */
1834 if (be16_to_cpu((*blkp)->bb_level) != level)
1835 goto out_bad;
1836
1837 /* Check that internal nodes have at least one record. */
1838 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1839 goto out_bad;
1840
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001841 xfs_btree_setbuf(cur, level, bp);
1842 return 0;
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001843
1844out_bad:
1845 *blkp = NULL;
1846 xfs_trans_brelse(cur->bc_tp, bp);
1847 return -EFSCORRUPTED;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001848}
1849
1850/*
1851 * Get current search key. For level 0 we don't actually have a key
1852 * structure so we make one up from the record. For all other levels
1853 * we just return the right key.
1854 */
1855STATIC union xfs_btree_key *
1856xfs_lookup_get_search_key(
1857 struct xfs_btree_cur *cur,
1858 int level,
1859 int keyno,
1860 struct xfs_btree_block *block,
1861 union xfs_btree_key *kp)
1862{
1863 if (level == 0) {
1864 cur->bc_ops->init_key_from_rec(kp,
1865 xfs_btree_rec_addr(cur, keyno, block));
1866 return kp;
1867 }
1868
1869 return xfs_btree_key_addr(cur, keyno, block);
1870}
1871
1872/*
1873 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001874 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001875 */
1876int /* error */
1877xfs_btree_lookup(
1878 struct xfs_btree_cur *cur, /* btree cursor */
1879 xfs_lookup_t dir, /* <=, ==, or >= */
1880 int *stat) /* success/failure */
1881{
1882 struct xfs_btree_block *block; /* current btree block */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001883 int64_t diff; /* difference for the current key */
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001884 int error; /* error return value */
1885 int keyno; /* current key number */
1886 int level; /* level in the btree */
1887 union xfs_btree_ptr *pp; /* ptr to btree block */
1888 union xfs_btree_ptr ptr; /* ptr to btree block */
1889
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001890 XFS_BTREE_STATS_INC(cur, lookup);
1891
Darrick J. Wonged150e12016-08-26 15:58:40 +10001892 /* No such thing as a zero-level tree. */
1893 if (cur->bc_nlevels == 0)
1894 return -EFSCORRUPTED;
1895
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001896 block = NULL;
1897 keyno = 0;
1898
1899 /* initialise start pointer from cursor */
1900 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1901 pp = &ptr;
1902
1903 /*
1904 * Iterate over each level in the btree, starting at the root.
1905 * For each level above the leaves, find the key we need, based
1906 * on the lookup record, then follow the corresponding block
1907 * pointer down to the next level.
1908 */
1909 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1910 /* Get the block we need to do the lookup on. */
1911 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1912 if (error)
1913 goto error0;
1914
1915 if (diff == 0) {
1916 /*
1917 * If we already had a key match at a higher level, we
1918 * know we need to use the first entry in this block.
1919 */
1920 keyno = 1;
1921 } else {
1922 /* Otherwise search this block. Do a binary search. */
1923
1924 int high; /* high entry number */
1925 int low; /* low entry number */
1926
1927 /* Set low and high entry numbers, 1-based. */
1928 low = 1;
1929 high = xfs_btree_get_numrecs(block);
1930 if (!high) {
1931 /* Block is empty, must be an empty leaf. */
Darrick J. Wongeeee0d62018-06-03 16:10:14 -07001932 if (level != 0 || cur->bc_nlevels != 1) {
1933 XFS_CORRUPTION_ERROR(__func__,
1934 XFS_ERRLEVEL_LOW,
Darrick J. Wong2551a532018-06-04 10:23:54 -07001935 cur->bc_mp, block,
1936 sizeof(*block));
Darrick J. Wongeeee0d62018-06-03 16:10:14 -07001937 return -EFSCORRUPTED;
1938 }
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001939
1940 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001941 *stat = 0;
1942 return 0;
1943 }
1944
1945 /* Binary search the block. */
1946 while (low <= high) {
1947 union xfs_btree_key key;
1948 union xfs_btree_key *kp;
1949
1950 XFS_BTREE_STATS_INC(cur, compare);
1951
1952 /* keyno is average of low and high. */
1953 keyno = (low + high) >> 1;
1954
1955 /* Get current search key */
1956 kp = xfs_lookup_get_search_key(cur, level,
1957 keyno, block, &key);
1958
1959 /*
1960 * Compute difference to get next direction:
1961 * - less than, move right
1962 * - greater than, move left
1963 * - equal, we're done
1964 */
1965 diff = cur->bc_ops->key_diff(cur, kp);
1966 if (diff < 0)
1967 low = keyno + 1;
1968 else if (diff > 0)
1969 high = keyno - 1;
1970 else
1971 break;
1972 }
1973 }
1974
1975 /*
1976 * If there are more levels, set up for the next level
1977 * by getting the block number and filling in the cursor.
1978 */
1979 if (level > 0) {
1980 /*
1981 * If we moved left, need the previous key number,
1982 * unless there isn't one.
1983 */
1984 if (diff > 0 && --keyno < 1)
1985 keyno = 1;
1986 pp = xfs_btree_ptr_addr(cur, keyno, block);
1987
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07001988 error = xfs_btree_debug_check_ptr(cur, pp, 0, level);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001989 if (error)
1990 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07001991
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001992 cur->bc_ptrs[level] = keyno;
1993 }
1994 }
1995
1996 /* Done with the search. See if we need to adjust the results. */
1997 if (dir != XFS_LOOKUP_LE && diff < 0) {
1998 keyno++;
1999 /*
2000 * If ge search and we went off the end of the block, but it's
2001 * not the last block, we're in the wrong block.
2002 */
2003 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
2004 if (dir == XFS_LOOKUP_GE &&
2005 keyno > xfs_btree_get_numrecs(block) &&
2006 !xfs_btree_ptr_is_null(cur, &ptr)) {
2007 int i;
2008
2009 cur->bc_ptrs[0] = keyno;
2010 error = xfs_btree_increment(cur, 0, &i);
2011 if (error)
2012 goto error0;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11002013 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11002014 *stat = 1;
2015 return 0;
2016 }
2017 } else if (dir == XFS_LOOKUP_LE && diff > 0)
2018 keyno--;
2019 cur->bc_ptrs[0] = keyno;
2020
2021 /* Return if we succeeded or not. */
2022 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
2023 *stat = 0;
2024 else if (dir != XFS_LOOKUP_EQ || diff == 0)
2025 *stat = 1;
2026 else
2027 *stat = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11002028 return 0;
2029
2030error0:
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11002031 return error;
2032}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002033
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002034/* Find the high key storage area from a regular key. */
Darrick J. Wong2fdbec52017-10-25 15:03:46 -07002035union xfs_btree_key *
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002036xfs_btree_high_key_from_key(
2037 struct xfs_btree_cur *cur,
2038 union xfs_btree_key *key)
2039{
2040 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2041 return (union xfs_btree_key *)((char *)key +
2042 (cur->bc_ops->key_len / 2));
2043}
2044
Darrick J. Wong973b8312016-08-03 12:22:12 +10002045/* Determine the low (and high if overlapped) keys of a leaf block */
2046STATIC void
2047xfs_btree_get_leaf_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002048 struct xfs_btree_cur *cur,
2049 struct xfs_btree_block *block,
2050 union xfs_btree_key *key)
2051{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002052 union xfs_btree_key max_hkey;
2053 union xfs_btree_key hkey;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002054 union xfs_btree_rec *rec;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002055 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002056 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002057
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002058 rec = xfs_btree_rec_addr(cur, 1, block);
2059 cur->bc_ops->init_key_from_rec(key, rec);
2060
Darrick J. Wong973b8312016-08-03 12:22:12 +10002061 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002062
Darrick J. Wong973b8312016-08-03 12:22:12 +10002063 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2064 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2065 rec = xfs_btree_rec_addr(cur, n, block);
2066 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2067 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2068 > 0)
2069 max_hkey = hkey;
2070 }
2071
2072 high = xfs_btree_high_key_from_key(cur, key);
2073 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2074 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002075}
2076
Darrick J. Wong973b8312016-08-03 12:22:12 +10002077/* Determine the low (and high if overlapped) keys of a node block */
2078STATIC void
2079xfs_btree_get_node_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002080 struct xfs_btree_cur *cur,
2081 struct xfs_btree_block *block,
2082 union xfs_btree_key *key)
2083{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002084 union xfs_btree_key *hkey;
2085 union xfs_btree_key *max_hkey;
2086 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002087 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002088
Darrick J. Wong973b8312016-08-03 12:22:12 +10002089 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2090 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2091 cur->bc_ops->key_len / 2);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002092
Darrick J. Wong973b8312016-08-03 12:22:12 +10002093 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2094 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2095 hkey = xfs_btree_high_key_addr(cur, n, block);
2096 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2097 max_hkey = hkey;
2098 }
2099
2100 high = xfs_btree_high_key_from_key(cur, key);
2101 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2102 } else {
2103 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2104 cur->bc_ops->key_len);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002105 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002106}
2107
Darrick J. Wong70b22652016-08-03 11:03:38 +10002108/* Derive the keys for any btree block. */
Darrick J. Wong2fdbec52017-10-25 15:03:46 -07002109void
Darrick J. Wong70b22652016-08-03 11:03:38 +10002110xfs_btree_get_keys(
2111 struct xfs_btree_cur *cur,
2112 struct xfs_btree_block *block,
2113 union xfs_btree_key *key)
2114{
2115 if (be16_to_cpu(block->bb_level) == 0)
Darrick J. Wong973b8312016-08-03 12:22:12 +10002116 xfs_btree_get_leaf_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002117 else
Darrick J. Wong973b8312016-08-03 12:22:12 +10002118 xfs_btree_get_node_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002119}
2120
2121/*
2122 * Decide if we need to update the parent keys of a btree block. For
2123 * a standard btree this is only necessary if we're updating the first
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002124 * record/key. For an overlapping btree, we must always update the
2125 * keys because the highest key can be in any of the records or keys
2126 * in the block.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002127 */
2128static inline bool
2129xfs_btree_needs_key_update(
2130 struct xfs_btree_cur *cur,
2131 int ptr)
2132{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002133 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2134}
2135
2136/*
2137 * Update the low and high parent keys of the given level, progressing
2138 * towards the root. If force_all is false, stop if the keys for a given
2139 * level do not need updating.
2140 */
2141STATIC int
2142__xfs_btree_updkeys(
2143 struct xfs_btree_cur *cur,
2144 int level,
2145 struct xfs_btree_block *block,
2146 struct xfs_buf *bp0,
2147 bool force_all)
2148{
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002149 union xfs_btree_key key; /* keys from current level */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002150 union xfs_btree_key *lkey; /* keys from the next level up */
2151 union xfs_btree_key *hkey;
2152 union xfs_btree_key *nlkey; /* keys from the next level up */
2153 union xfs_btree_key *nhkey;
2154 struct xfs_buf *bp;
2155 int ptr;
2156
2157 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2158
2159 /* Exit if there aren't any parent levels to update. */
2160 if (level + 1 >= cur->bc_nlevels)
2161 return 0;
2162
2163 trace_xfs_btree_updkeys(cur, level, bp0);
2164
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002165 lkey = &key;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002166 hkey = xfs_btree_high_key_from_key(cur, lkey);
2167 xfs_btree_get_keys(cur, block, lkey);
2168 for (level++; level < cur->bc_nlevels; level++) {
2169#ifdef DEBUG
2170 int error;
2171#endif
2172 block = xfs_btree_get_block(cur, level, &bp);
2173 trace_xfs_btree_updkeys(cur, level, bp);
2174#ifdef DEBUG
2175 error = xfs_btree_check_block(cur, block, level, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002176 if (error)
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002177 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002178#endif
2179 ptr = cur->bc_ptrs[level];
2180 nlkey = xfs_btree_key_addr(cur, ptr, block);
2181 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2182 if (!force_all &&
2183 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2184 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2185 break;
2186 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2187 xfs_btree_log_keys(cur, bp, ptr, ptr);
2188 if (level + 1 >= cur->bc_nlevels)
2189 break;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002190 xfs_btree_get_node_keys(cur, block, lkey);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002191 }
2192
2193 return 0;
2194}
2195
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002196/* Update all the keys from some level in cursor back to the root. */
2197STATIC int
2198xfs_btree_updkeys_force(
2199 struct xfs_btree_cur *cur,
2200 int level)
2201{
2202 struct xfs_buf *bp;
2203 struct xfs_btree_block *block;
2204
2205 block = xfs_btree_get_block(cur, level, &bp);
2206 return __xfs_btree_updkeys(cur, level, block, bp, true);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002207}
2208
2209/*
2210 * Update the parent keys of the given level, progressing towards the root.
2211 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002212STATIC int
Darrick J. Wong70b22652016-08-03 11:03:38 +10002213xfs_btree_update_keys(
2214 struct xfs_btree_cur *cur,
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002215 int level)
2216{
2217 struct xfs_btree_block *block;
2218 struct xfs_buf *bp;
2219 union xfs_btree_key *kp;
Darrick J. Wong70b22652016-08-03 11:03:38 +10002220 union xfs_btree_key key;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002221 int ptr;
2222
Darrick J. Wong973b8312016-08-03 12:22:12 +10002223 ASSERT(level >= 0);
2224
2225 block = xfs_btree_get_block(cur, level, &bp);
2226 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2227 return __xfs_btree_updkeys(cur, level, block, bp, false);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002228
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002229 /*
2230 * Go up the tree from this level toward the root.
2231 * At each level, update the key value to the value input.
2232 * Stop when we reach a level where the cursor isn't pointing
2233 * at the first entry in the block.
2234 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002235 xfs_btree_get_keys(cur, block, &key);
2236 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002237#ifdef DEBUG
2238 int error;
2239#endif
2240 block = xfs_btree_get_block(cur, level, &bp);
2241#ifdef DEBUG
2242 error = xfs_btree_check_block(cur, block, level, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002243 if (error)
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002244 return error;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002245#endif
2246 ptr = cur->bc_ptrs[level];
2247 kp = xfs_btree_key_addr(cur, ptr, block);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002248 xfs_btree_copy_keys(cur, kp, &key, 1);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002249 xfs_btree_log_keys(cur, bp, ptr, ptr);
2250 }
2251
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002252 return 0;
2253}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002254
2255/*
2256 * Update the record referred to by cur to the value in the
2257 * given record. This either works (return 0) or gets an
2258 * EFSCORRUPTED error.
2259 */
2260int
2261xfs_btree_update(
2262 struct xfs_btree_cur *cur,
2263 union xfs_btree_rec *rec)
2264{
2265 struct xfs_btree_block *block;
2266 struct xfs_buf *bp;
2267 int error;
2268 int ptr;
2269 union xfs_btree_rec *rp;
2270
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002271 /* Pick up the current block. */
2272 block = xfs_btree_get_block(cur, 0, &bp);
2273
2274#ifdef DEBUG
2275 error = xfs_btree_check_block(cur, block, 0, bp);
2276 if (error)
2277 goto error0;
2278#endif
2279 /* Get the address of the rec to be updated. */
2280 ptr = cur->bc_ptrs[0];
2281 rp = xfs_btree_rec_addr(cur, ptr, block);
2282
2283 /* Fill in the new contents and log them. */
2284 xfs_btree_copy_recs(cur, rp, rec, 1);
2285 xfs_btree_log_recs(cur, bp, ptr, ptr);
2286
2287 /*
2288 * If we are tracking the last record in the tree and
2289 * we are at the far right edge of the tree, update it.
2290 */
2291 if (xfs_btree_is_lastrec(cur, block, 0)) {
2292 cur->bc_ops->update_lastrec(cur, block, rec,
2293 ptr, LASTREC_UPDATE);
2294 }
2295
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002296 /* Pass new key value up to our parent. */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002297 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002298 error = xfs_btree_update_keys(cur, 0);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002299 if (error)
2300 goto error0;
2301 }
2302
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002303 return 0;
2304
2305error0:
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002306 return error;
2307}
2308
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002309/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11002310 * Move 1 record left from cur/level if possible.
2311 * Update cur to reflect the new path.
2312 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002313STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002314xfs_btree_lshift(
2315 struct xfs_btree_cur *cur,
2316 int level,
2317 int *stat) /* success/failure */
2318{
Christoph Hellwig687b8902008-10-30 16:56:53 +11002319 struct xfs_buf *lbp; /* left buffer pointer */
2320 struct xfs_btree_block *left; /* left btree block */
2321 int lrecs; /* left record count */
2322 struct xfs_buf *rbp; /* right buffer pointer */
2323 struct xfs_btree_block *right; /* right btree block */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002324 struct xfs_btree_cur *tcur; /* temporary btree cursor */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002325 int rrecs; /* right record count */
2326 union xfs_btree_ptr lptr; /* left btree pointer */
2327 union xfs_btree_key *rkp = NULL; /* right btree key */
2328 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2329 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2330 int error; /* error return value */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002331 int i;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002332
Christoph Hellwig687b8902008-10-30 16:56:53 +11002333 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2334 level == cur->bc_nlevels - 1)
2335 goto out0;
2336
2337 /* Set up variables for this block as "right". */
2338 right = xfs_btree_get_block(cur, level, &rbp);
2339
2340#ifdef DEBUG
2341 error = xfs_btree_check_block(cur, right, level, rbp);
2342 if (error)
2343 goto error0;
2344#endif
2345
2346 /* If we've got no left sibling then we can't shift an entry left. */
2347 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2348 if (xfs_btree_ptr_is_null(cur, &lptr))
2349 goto out0;
2350
2351 /*
2352 * If the cursor entry is the one that would be moved, don't
2353 * do it... it's too complicated.
2354 */
2355 if (cur->bc_ptrs[level] <= 1)
2356 goto out0;
2357
2358 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002359 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002360 if (error)
2361 goto error0;
2362
2363 /* If it's full, it can't take another entry. */
2364 lrecs = xfs_btree_get_numrecs(left);
2365 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2366 goto out0;
2367
2368 rrecs = xfs_btree_get_numrecs(right);
2369
2370 /*
2371 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002372 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002373 * later.
2374 */
2375 lrecs++;
2376 rrecs--;
2377
2378 XFS_BTREE_STATS_INC(cur, lshift);
2379 XFS_BTREE_STATS_ADD(cur, moves, 1);
2380
2381 /*
2382 * If non-leaf, copy a key and a ptr to the left block.
2383 * Log the changes to the left block.
2384 */
2385 if (level > 0) {
2386 /* It's a non-leaf. Move keys and pointers. */
2387 union xfs_btree_key *lkp; /* left btree key */
2388 union xfs_btree_ptr *lpp; /* left address pointer */
2389
2390 lkp = xfs_btree_key_addr(cur, lrecs, left);
2391 rkp = xfs_btree_key_addr(cur, 1, right);
2392
2393 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2394 rpp = xfs_btree_ptr_addr(cur, 1, right);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002395
2396 error = xfs_btree_debug_check_ptr(cur, rpp, 0, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002397 if (error)
2398 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002399
Christoph Hellwig687b8902008-10-30 16:56:53 +11002400 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2401 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2402
2403 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2404 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2405
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002406 ASSERT(cur->bc_ops->keys_inorder(cur,
2407 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002408 } else {
2409 /* It's a leaf. Move records. */
2410 union xfs_btree_rec *lrp; /* left record pointer */
2411
2412 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2413 rrp = xfs_btree_rec_addr(cur, 1, right);
2414
2415 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2416 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2417
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002418 ASSERT(cur->bc_ops->recs_inorder(cur,
2419 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002420 }
2421
2422 xfs_btree_set_numrecs(left, lrecs);
2423 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2424
2425 xfs_btree_set_numrecs(right, rrecs);
2426 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2427
2428 /*
2429 * Slide the contents of right down one entry.
2430 */
2431 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2432 if (level > 0) {
2433 /* It's a nonleaf. operate on keys and ptrs */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002434 int i; /* loop index */
2435
2436 for (i = 0; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002437 error = xfs_btree_debug_check_ptr(cur, rpp, i + 1, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002438 if (error)
2439 goto error0;
2440 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002441
Christoph Hellwig687b8902008-10-30 16:56:53 +11002442 xfs_btree_shift_keys(cur,
2443 xfs_btree_key_addr(cur, 2, right),
2444 -1, rrecs);
2445 xfs_btree_shift_ptrs(cur,
2446 xfs_btree_ptr_addr(cur, 2, right),
2447 -1, rrecs);
2448
2449 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2450 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2451 } else {
2452 /* It's a leaf. operate on records */
2453 xfs_btree_shift_recs(cur,
2454 xfs_btree_rec_addr(cur, 2, right),
2455 -1, rrecs);
2456 xfs_btree_log_recs(cur, rbp, 1, rrecs);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002457 }
2458
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002459 /*
2460 * Using a temporary cursor, update the parent key values of the
2461 * block on the left.
2462 */
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002463 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2464 error = xfs_btree_dup_cursor(cur, &tcur);
2465 if (error)
2466 goto error0;
2467 i = xfs_btree_firstrec(tcur, level);
2468 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002469
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002470 error = xfs_btree_decrement(tcur, level, &i);
2471 if (error)
2472 goto error1;
2473
2474 /* Update the parent high keys of the left block, if needed. */
2475 error = xfs_btree_update_keys(tcur, level);
2476 if (error)
2477 goto error1;
2478
2479 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2480 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002481
Darrick J. Wong70b22652016-08-03 11:03:38 +10002482 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002483 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002484 if (error)
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002485 goto error0;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002486
2487 /* Slide the cursor value left one. */
2488 cur->bc_ptrs[level]--;
2489
Christoph Hellwig687b8902008-10-30 16:56:53 +11002490 *stat = 1;
2491 return 0;
2492
2493out0:
Christoph Hellwig687b8902008-10-30 16:56:53 +11002494 *stat = 0;
2495 return 0;
2496
2497error0:
Christoph Hellwig687b8902008-10-30 16:56:53 +11002498 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002499
2500error1:
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002501 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2502 return error;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002503}
2504
2505/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002506 * Move 1 record right from cur/level if possible.
2507 * Update cur to reflect the new path.
2508 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002509STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002510xfs_btree_rshift(
2511 struct xfs_btree_cur *cur,
2512 int level,
2513 int *stat) /* success/failure */
2514{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002515 struct xfs_buf *lbp; /* left buffer pointer */
2516 struct xfs_btree_block *left; /* left btree block */
2517 struct xfs_buf *rbp; /* right buffer pointer */
2518 struct xfs_btree_block *right; /* right btree block */
2519 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2520 union xfs_btree_ptr rptr; /* right block pointer */
2521 union xfs_btree_key *rkp; /* right btree key */
2522 int rrecs; /* right record count */
2523 int lrecs; /* left record count */
2524 int error; /* error return value */
2525 int i; /* loop counter */
2526
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002527 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2528 (level == cur->bc_nlevels - 1))
2529 goto out0;
2530
2531 /* Set up variables for this block as "left". */
2532 left = xfs_btree_get_block(cur, level, &lbp);
2533
2534#ifdef DEBUG
2535 error = xfs_btree_check_block(cur, left, level, lbp);
2536 if (error)
2537 goto error0;
2538#endif
2539
2540 /* If we've got no right sibling then we can't shift an entry right. */
2541 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2542 if (xfs_btree_ptr_is_null(cur, &rptr))
2543 goto out0;
2544
2545 /*
2546 * If the cursor entry is the one that would be moved, don't
2547 * do it... it's too complicated.
2548 */
2549 lrecs = xfs_btree_get_numrecs(left);
2550 if (cur->bc_ptrs[level] >= lrecs)
2551 goto out0;
2552
2553 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002554 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002555 if (error)
2556 goto error0;
2557
2558 /* If it's full, it can't take another entry. */
2559 rrecs = xfs_btree_get_numrecs(right);
2560 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2561 goto out0;
2562
2563 XFS_BTREE_STATS_INC(cur, rshift);
2564 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2565
2566 /*
2567 * Make a hole at the start of the right neighbor block, then
2568 * copy the last left block entry to the hole.
2569 */
2570 if (level > 0) {
2571 /* It's a nonleaf. make a hole in the keys and ptrs */
2572 union xfs_btree_key *lkp;
2573 union xfs_btree_ptr *lpp;
2574 union xfs_btree_ptr *rpp;
2575
2576 lkp = xfs_btree_key_addr(cur, lrecs, left);
2577 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2578 rkp = xfs_btree_key_addr(cur, 1, right);
2579 rpp = xfs_btree_ptr_addr(cur, 1, right);
2580
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002581 for (i = rrecs - 1; i >= 0; i--) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002582 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002583 if (error)
2584 goto error0;
2585 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002586
2587 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2588 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2589
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002590 error = xfs_btree_debug_check_ptr(cur, lpp, 0, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002591 if (error)
2592 goto error0;
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002593
2594 /* Now put the new data in, and log it. */
2595 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2596 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2597
2598 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2599 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2600
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002601 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2602 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002603 } else {
2604 /* It's a leaf. make a hole in the records */
2605 union xfs_btree_rec *lrp;
2606 union xfs_btree_rec *rrp;
2607
2608 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2609 rrp = xfs_btree_rec_addr(cur, 1, right);
2610
2611 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2612
2613 /* Now put the new data in, and log it. */
2614 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2615 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002616 }
2617
2618 /*
2619 * Decrement and log left's numrecs, bump and log right's numrecs.
2620 */
2621 xfs_btree_set_numrecs(left, --lrecs);
2622 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2623
2624 xfs_btree_set_numrecs(right, ++rrecs);
2625 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2626
2627 /*
2628 * Using a temporary cursor, update the parent key values of the
2629 * block on the right.
2630 */
2631 error = xfs_btree_dup_cursor(cur, &tcur);
2632 if (error)
2633 goto error0;
2634 i = xfs_btree_lastrec(tcur, level);
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002635 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002636
2637 error = xfs_btree_increment(tcur, level, &i);
2638 if (error)
2639 goto error1;
2640
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002641 /* Update the parent high keys of the left block, if needed. */
2642 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002643 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002644 if (error)
2645 goto error1;
2646 }
2647
Darrick J. Wong70b22652016-08-03 11:03:38 +10002648 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002649 error = xfs_btree_update_keys(tcur, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002650 if (error)
2651 goto error1;
2652
2653 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2654
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002655 *stat = 1;
2656 return 0;
2657
2658out0:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002659 *stat = 0;
2660 return 0;
2661
2662error0:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002663 return error;
2664
2665error1:
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002666 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2667 return error;
2668}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002669
2670/*
2671 * Split cur/level block in half.
2672 * Return new block number and the key to its first
2673 * record (to be inserted into parent).
2674 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002675STATIC int /* error */
Dave Chinnercf11da92014-07-15 07:08:24 +10002676__xfs_btree_split(
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002677 struct xfs_btree_cur *cur,
2678 int level,
2679 union xfs_btree_ptr *ptrp,
2680 union xfs_btree_key *key,
2681 struct xfs_btree_cur **curp,
2682 int *stat) /* success/failure */
2683{
2684 union xfs_btree_ptr lptr; /* left sibling block ptr */
2685 struct xfs_buf *lbp; /* left buffer pointer */
2686 struct xfs_btree_block *left; /* left btree block */
2687 union xfs_btree_ptr rptr; /* right sibling block ptr */
2688 struct xfs_buf *rbp; /* right buffer pointer */
2689 struct xfs_btree_block *right; /* right btree block */
2690 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2691 struct xfs_buf *rrbp; /* right-right buffer pointer */
2692 struct xfs_btree_block *rrblock; /* right-right btree block */
2693 int lrecs;
2694 int rrecs;
2695 int src_index;
2696 int error; /* error return value */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002697 int i;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002698
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002699 XFS_BTREE_STATS_INC(cur, split);
2700
2701 /* Set up left block (current one). */
2702 left = xfs_btree_get_block(cur, level, &lbp);
2703
2704#ifdef DEBUG
2705 error = xfs_btree_check_block(cur, left, level, lbp);
2706 if (error)
2707 goto error0;
2708#endif
2709
2710 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2711
2712 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002713 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002714 if (error)
2715 goto error0;
2716 if (*stat == 0)
2717 goto out0;
2718 XFS_BTREE_STATS_INC(cur, alloc);
2719
2720 /* Set up the new block as "right". */
2721 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2722 if (error)
2723 goto error0;
2724
2725 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002726 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002727
2728 /*
2729 * Split the entries between the old and the new block evenly.
2730 * Make sure that if there's an odd number of entries now, that
2731 * each new block will have the same number of entries.
2732 */
2733 lrecs = xfs_btree_get_numrecs(left);
2734 rrecs = lrecs / 2;
2735 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2736 rrecs++;
2737 src_index = (lrecs - rrecs + 1);
2738
2739 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2740
Darrick J. Wong70b22652016-08-03 11:03:38 +10002741 /* Adjust numrecs for the later get_*_keys() calls. */
2742 lrecs -= rrecs;
2743 xfs_btree_set_numrecs(left, lrecs);
2744 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2745
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002746 /*
2747 * Copy btree block entries from the left block over to the
2748 * new block, the right. Update the right block and log the
2749 * changes.
2750 */
2751 if (level > 0) {
2752 /* It's a non-leaf. Move keys and pointers. */
2753 union xfs_btree_key *lkp; /* left btree key */
2754 union xfs_btree_ptr *lpp; /* left address pointer */
2755 union xfs_btree_key *rkp; /* right btree key */
2756 union xfs_btree_ptr *rpp; /* right address pointer */
2757
2758 lkp = xfs_btree_key_addr(cur, src_index, left);
2759 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2760 rkp = xfs_btree_key_addr(cur, 1, right);
2761 rpp = xfs_btree_ptr_addr(cur, 1, right);
2762
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002763 for (i = src_index; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07002764 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002765 if (error)
2766 goto error0;
2767 }
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002768
Darrick J. Wong70b22652016-08-03 11:03:38 +10002769 /* Copy the keys & pointers to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002770 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2771 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2772
2773 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2774 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2775
Darrick J. Wong70b22652016-08-03 11:03:38 +10002776 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002777 xfs_btree_get_node_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002778 } else {
2779 /* It's a leaf. Move records. */
2780 union xfs_btree_rec *lrp; /* left record pointer */
2781 union xfs_btree_rec *rrp; /* right record pointer */
2782
2783 lrp = xfs_btree_rec_addr(cur, src_index, left);
2784 rrp = xfs_btree_rec_addr(cur, 1, right);
2785
Darrick J. Wong70b22652016-08-03 11:03:38 +10002786 /* Copy records to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002787 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2788 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2789
Darrick J. Wong70b22652016-08-03 11:03:38 +10002790 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002791 xfs_btree_get_leaf_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002792 }
2793
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002794 /*
2795 * Find the left block number by looking in the buffer.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002796 * Adjust sibling pointers.
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002797 */
2798 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2799 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2800 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2801 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2802
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002803 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2804 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2805
2806 /*
2807 * If there's a block to the new block's right, make that block
2808 * point back to right instead of to left.
2809 */
2810 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002811 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002812 0, &rrblock, &rrbp);
2813 if (error)
2814 goto error0;
2815 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2816 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2817 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002818
2819 /* Update the parent high keys of the left block, if needed. */
2820 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002821 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002822 if (error)
2823 goto error0;
2824 }
2825
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002826 /*
2827 * If the cursor is really in the right block, move it there.
2828 * If it's just pointing past the last entry in left, then we'll
2829 * insert there, so don't change anything in that case.
2830 */
2831 if (cur->bc_ptrs[level] > lrecs + 1) {
2832 xfs_btree_setbuf(cur, level, rbp);
2833 cur->bc_ptrs[level] -= lrecs;
2834 }
2835 /*
2836 * If there are more levels, we'll need another cursor which refers
2837 * the right block, no matter where this cursor was.
2838 */
2839 if (level + 1 < cur->bc_nlevels) {
2840 error = xfs_btree_dup_cursor(cur, curp);
2841 if (error)
2842 goto error0;
2843 (*curp)->bc_ptrs[level + 1]++;
2844 }
2845 *ptrp = rptr;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002846 *stat = 1;
2847 return 0;
2848out0:
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002849 *stat = 0;
2850 return 0;
2851
2852error0:
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002853 return error;
2854}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002855
Dave Chinnercf11da92014-07-15 07:08:24 +10002856struct xfs_btree_split_args {
2857 struct xfs_btree_cur *cur;
2858 int level;
2859 union xfs_btree_ptr *ptrp;
2860 union xfs_btree_key *key;
2861 struct xfs_btree_cur **curp;
2862 int *stat; /* success/failure */
2863 int result;
2864 bool kswapd; /* allocation in kswapd context */
2865 struct completion *done;
2866 struct work_struct work;
2867};
2868
2869/*
2870 * Stack switching interfaces for allocation
2871 */
2872static void
2873xfs_btree_split_worker(
2874 struct work_struct *work)
2875{
2876 struct xfs_btree_split_args *args = container_of(work,
2877 struct xfs_btree_split_args, work);
2878 unsigned long pflags;
Michal Hocko90707332017-05-03 14:53:12 -07002879 unsigned long new_pflags = PF_MEMALLOC_NOFS;
Dave Chinnercf11da92014-07-15 07:08:24 +10002880
2881 /*
2882 * we are in a transaction context here, but may also be doing work
2883 * in kswapd context, and hence we may need to inherit that state
2884 * temporarily to ensure that we don't block waiting for memory reclaim
2885 * in any way.
2886 */
2887 if (args->kswapd)
2888 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2889
2890 current_set_flags_nested(&pflags, new_pflags);
2891
2892 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2893 args->key, args->curp, args->stat);
2894 complete(args->done);
2895
2896 current_restore_flags_nested(&pflags, new_pflags);
2897}
2898
2899/*
2900 * BMBT split requests often come in with little stack to work on. Push
2901 * them off to a worker thread so there is lots of stack to use. For the other
2902 * btree types, just call directly to avoid the context switch overhead here.
2903 */
2904STATIC int /* error */
2905xfs_btree_split(
2906 struct xfs_btree_cur *cur,
2907 int level,
2908 union xfs_btree_ptr *ptrp,
2909 union xfs_btree_key *key,
2910 struct xfs_btree_cur **curp,
2911 int *stat) /* success/failure */
2912{
2913 struct xfs_btree_split_args args;
2914 DECLARE_COMPLETION_ONSTACK(done);
2915
2916 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2917 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2918
2919 args.cur = cur;
2920 args.level = level;
2921 args.ptrp = ptrp;
2922 args.key = key;
2923 args.curp = curp;
2924 args.stat = stat;
2925 args.done = &done;
2926 args.kswapd = current_is_kswapd();
2927 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2928 queue_work(xfs_alloc_wq, &args.work);
2929 wait_for_completion(&done);
2930 destroy_work_on_stack(&args.work);
2931 return args.result;
2932}
2933
2934
Christoph Hellwig344207c2008-10-30 16:57:16 +11002935/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002936 * Copy the old inode root contents into a real block and make the
2937 * broot point to it.
2938 */
2939int /* error */
2940xfs_btree_new_iroot(
2941 struct xfs_btree_cur *cur, /* btree cursor */
2942 int *logflags, /* logging flags for inode */
2943 int *stat) /* return status - 0 fail */
2944{
2945 struct xfs_buf *cbp; /* buffer for cblock */
2946 struct xfs_btree_block *block; /* btree block */
2947 struct xfs_btree_block *cblock; /* child btree block */
2948 union xfs_btree_key *ckp; /* child key pointer */
2949 union xfs_btree_ptr *cpp; /* child ptr pointer */
2950 union xfs_btree_key *kp; /* pointer to btree key */
2951 union xfs_btree_ptr *pp; /* pointer to block addr */
2952 union xfs_btree_ptr nptr; /* new block addr */
2953 int level; /* btree level */
2954 int error; /* error return code */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002955 int i; /* loop counter */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002956
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002957 XFS_BTREE_STATS_INC(cur, newroot);
2958
2959 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2960
2961 level = cur->bc_nlevels - 1;
2962
2963 block = xfs_btree_get_iroot(cur);
2964 pp = xfs_btree_ptr_addr(cur, 1, block);
2965
2966 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002967 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002968 if (error)
2969 goto error0;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002970 if (*stat == 0)
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002971 return 0;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08002972
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002973 XFS_BTREE_STATS_INC(cur, alloc);
2974
2975 /* Copy the root into a real block. */
2976 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2977 if (error)
2978 goto error0;
2979
Dave Chinner088c9f62013-06-12 12:19:08 +10002980 /*
2981 * we can't just memcpy() the root in for CRC enabled btree blocks.
2982 * In that case have to also ensure the blkno remains correct
2983 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002984 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002985 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2986 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2987 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2988 else
2989 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2990 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002991
2992 be16_add_cpu(&block->bb_level, 1);
2993 xfs_btree_set_numrecs(block, 1);
2994 cur->bc_nlevels++;
2995 cur->bc_ptrs[level + 1] = 1;
2996
2997 kp = xfs_btree_key_addr(cur, 1, block);
2998 ckp = xfs_btree_key_addr(cur, 1, cblock);
2999 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
3000
3001 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003002 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003003 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003004 if (error)
3005 goto error0;
3006 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003007
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003008 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
3009
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003010 error = xfs_btree_debug_check_ptr(cur, &nptr, 0, level);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003011 if (error)
3012 goto error0;
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003013
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003014 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3015
3016 xfs_iroot_realloc(cur->bc_private.b.ip,
3017 1 - xfs_btree_get_numrecs(cblock),
3018 cur->bc_private.b.whichfork);
3019
3020 xfs_btree_setbuf(cur, level, cbp);
3021
3022 /*
3023 * Do all this logging at the end so that
3024 * the root is at the right level.
3025 */
3026 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3027 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3028 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3029
3030 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06003031 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003032 *stat = 1;
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003033 return 0;
3034error0:
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003035 return error;
3036}
3037
3038/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11003039 * Allocate a new root block, fill it in.
3040 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11003041STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11003042xfs_btree_new_root(
3043 struct xfs_btree_cur *cur, /* btree cursor */
3044 int *stat) /* success/failure */
3045{
3046 struct xfs_btree_block *block; /* one half of the old root block */
3047 struct xfs_buf *bp; /* buffer containing block */
3048 int error; /* error return value */
3049 struct xfs_buf *lbp; /* left buffer pointer */
3050 struct xfs_btree_block *left; /* left btree block */
3051 struct xfs_buf *nbp; /* new (root) buffer */
3052 struct xfs_btree_block *new; /* new (root) btree block */
3053 int nptr; /* new value for key index, 1 or 2 */
3054 struct xfs_buf *rbp; /* right buffer pointer */
3055 struct xfs_btree_block *right; /* right btree block */
3056 union xfs_btree_ptr rptr;
3057 union xfs_btree_ptr lptr;
3058
Christoph Hellwig344207c2008-10-30 16:57:16 +11003059 XFS_BTREE_STATS_INC(cur, newroot);
3060
3061 /* initialise our start point from the cursor */
3062 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3063
3064 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10003065 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003066 if (error)
3067 goto error0;
3068 if (*stat == 0)
3069 goto out0;
3070 XFS_BTREE_STATS_INC(cur, alloc);
3071
3072 /* Set up the new block. */
3073 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3074 if (error)
3075 goto error0;
3076
3077 /* Set the root in the holding structure increasing the level by 1. */
3078 cur->bc_ops->set_root(cur, &lptr, 1);
3079
3080 /*
3081 * At the previous root level there are now two blocks: the old root,
3082 * and the new block generated when it was split. We don't know which
3083 * one the cursor is pointing at, so we set up variables "left" and
3084 * "right" for each case.
3085 */
3086 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3087
3088#ifdef DEBUG
3089 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3090 if (error)
3091 goto error0;
3092#endif
3093
3094 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3095 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3096 /* Our block is left, pick up the right block. */
3097 lbp = bp;
3098 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3099 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003100 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003101 if (error)
3102 goto error0;
3103 bp = rbp;
3104 nptr = 1;
3105 } else {
3106 /* Our block is right, pick up the left block. */
3107 rbp = bp;
3108 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3109 right = block;
3110 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003111 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003112 if (error)
3113 goto error0;
3114 bp = lbp;
3115 nptr = 2;
3116 }
Darrick J. Wong70b22652016-08-03 11:03:38 +10003117
Christoph Hellwig344207c2008-10-30 16:57:16 +11003118 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05003119 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003120 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3121 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3122 !xfs_btree_ptr_is_null(cur, &rptr));
3123
3124 /* Fill in the key data in the new root. */
3125 if (xfs_btree_get_level(left) > 0) {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003126 /*
3127 * Get the keys for the left block's keys and put them directly
3128 * in the parent block. Do the same for the right block.
3129 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003130 xfs_btree_get_node_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003131 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003132 xfs_btree_get_node_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003133 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003134 } else {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003135 /*
3136 * Get the keys for the left block's records and put them
3137 * directly in the parent block. Do the same for the right
3138 * block.
3139 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003140 xfs_btree_get_leaf_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003141 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003142 xfs_btree_get_leaf_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003143 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003144 }
3145 xfs_btree_log_keys(cur, nbp, 1, 2);
3146
3147 /* Fill in the pointer data in the new root. */
3148 xfs_btree_copy_ptrs(cur,
3149 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3150 xfs_btree_copy_ptrs(cur,
3151 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3152 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3153
3154 /* Fix up the cursor. */
3155 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3156 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3157 cur->bc_nlevels++;
Christoph Hellwig344207c2008-10-30 16:57:16 +11003158 *stat = 1;
3159 return 0;
3160error0:
Christoph Hellwig344207c2008-10-30 16:57:16 +11003161 return error;
3162out0:
Christoph Hellwig344207c2008-10-30 16:57:16 +11003163 *stat = 0;
3164 return 0;
3165}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003166
3167STATIC int
3168xfs_btree_make_block_unfull(
3169 struct xfs_btree_cur *cur, /* btree cursor */
3170 int level, /* btree level */
3171 int numrecs,/* # of recs in block */
3172 int *oindex,/* old tree index */
3173 int *index, /* new tree index */
3174 union xfs_btree_ptr *nptr, /* new btree ptr */
3175 struct xfs_btree_cur **ncur, /* new btree cursor */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003176 union xfs_btree_key *key, /* key of new block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003177 int *stat)
3178{
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003179 int error = 0;
3180
3181 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3182 level == cur->bc_nlevels - 1) {
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003183 struct xfs_inode *ip = cur->bc_private.b.ip;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003184
3185 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3186 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003187 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
Darrick J. Wong0d309792016-08-03 11:01:25 +10003188 *stat = 1;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003189 } else {
3190 /* A root block that needs replacing */
3191 int logflags = 0;
3192
3193 error = xfs_btree_new_iroot(cur, &logflags, stat);
3194 if (error || *stat == 0)
3195 return error;
3196
3197 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3198 }
3199
3200 return 0;
3201 }
3202
3203 /* First, try shifting an entry to the right neighbor. */
3204 error = xfs_btree_rshift(cur, level, stat);
3205 if (error || *stat)
3206 return error;
3207
3208 /* Next, try shifting an entry to the left neighbor. */
3209 error = xfs_btree_lshift(cur, level, stat);
3210 if (error)
3211 return error;
3212
3213 if (*stat) {
3214 *oindex = *index = cur->bc_ptrs[level];
3215 return 0;
3216 }
3217
3218 /*
3219 * Next, try splitting the current block in half.
3220 *
3221 * If this works we have to re-set our variables because we
3222 * could be in a different block now.
3223 */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003224 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003225 if (error || *stat == 0)
3226 return error;
3227
3228
3229 *index = cur->bc_ptrs[level];
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003230 return 0;
3231}
3232
3233/*
3234 * Insert one record/level. Return information to the caller
3235 * allowing the next level up to proceed if necessary.
3236 */
3237STATIC int
3238xfs_btree_insrec(
3239 struct xfs_btree_cur *cur, /* btree cursor */
3240 int level, /* level to insert record at */
3241 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003242 union xfs_btree_rec *rec, /* record to insert */
3243 union xfs_btree_key *key, /* i/o: block key for ptrp */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003244 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3245 int *stat) /* success/failure */
3246{
3247 struct xfs_btree_block *block; /* btree block */
3248 struct xfs_buf *bp; /* buffer for block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003249 union xfs_btree_ptr nptr; /* new block ptr */
3250 struct xfs_btree_cur *ncur; /* new btree cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003251 union xfs_btree_key nkey; /* new block key */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003252 union xfs_btree_key *lkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003253 int optr; /* old key/record index */
3254 int ptr; /* key/record index */
3255 int numrecs;/* number of records */
3256 int error; /* error return value */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003257 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003258 xfs_daddr_t old_bn;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003259
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003260 ncur = NULL;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003261 lkey = &nkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003262
3263 /*
3264 * If we have an external root pointer, and we've made it to the
3265 * root level, allocate a new root block and we're done.
3266 */
3267 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3268 (level >= cur->bc_nlevels)) {
3269 error = xfs_btree_new_root(cur, stat);
3270 xfs_btree_set_ptr_null(cur, ptrp);
3271
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003272 return error;
3273 }
3274
3275 /* If we're off the left edge, return failure. */
3276 ptr = cur->bc_ptrs[level];
3277 if (ptr == 0) {
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003278 *stat = 0;
3279 return 0;
3280 }
3281
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003282 optr = ptr;
3283
3284 XFS_BTREE_STATS_INC(cur, insrec);
3285
3286 /* Get pointers to the btree buffer and block. */
3287 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003288 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003289 numrecs = xfs_btree_get_numrecs(block);
3290
3291#ifdef DEBUG
3292 error = xfs_btree_check_block(cur, block, level, bp);
3293 if (error)
3294 goto error0;
3295
3296 /* Check that the new entry is being inserted in the right place. */
3297 if (ptr <= numrecs) {
3298 if (level == 0) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003299 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003300 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003301 } else {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003302 ASSERT(cur->bc_ops->keys_inorder(cur, key,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003303 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003304 }
3305 }
3306#endif
3307
3308 /*
3309 * If the block is full, we can't insert the new entry until we
3310 * make the block un-full.
3311 */
3312 xfs_btree_set_ptr_null(cur, &nptr);
3313 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3314 error = xfs_btree_make_block_unfull(cur, level, numrecs,
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003315 &optr, &ptr, &nptr, &ncur, lkey, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003316 if (error || *stat == 0)
3317 goto error0;
3318 }
3319
3320 /*
3321 * The current block may have changed if the block was
3322 * previously full and we have just made space in it.
3323 */
3324 block = xfs_btree_get_block(cur, level, &bp);
3325 numrecs = xfs_btree_get_numrecs(block);
3326
3327#ifdef DEBUG
3328 error = xfs_btree_check_block(cur, block, level, bp);
3329 if (error)
3330 return error;
3331#endif
3332
3333 /*
3334 * At this point we know there's room for our new entry in the block
3335 * we're pointing at.
3336 */
3337 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3338
3339 if (level > 0) {
3340 /* It's a nonleaf. make a hole in the keys and ptrs */
3341 union xfs_btree_key *kp;
3342 union xfs_btree_ptr *pp;
3343
3344 kp = xfs_btree_key_addr(cur, ptr, block);
3345 pp = xfs_btree_ptr_addr(cur, ptr, block);
3346
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003347 for (i = numrecs - ptr; i >= 0; i--) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003348 error = xfs_btree_debug_check_ptr(cur, pp, i, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003349 if (error)
3350 return error;
3351 }
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003352
3353 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3354 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3355
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003356 error = xfs_btree_debug_check_ptr(cur, ptrp, 0, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003357 if (error)
3358 goto error0;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003359
3360 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003361 xfs_btree_copy_keys(cur, kp, key, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003362 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3363 numrecs++;
3364 xfs_btree_set_numrecs(block, numrecs);
3365 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3366 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3367#ifdef DEBUG
3368 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003369 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3370 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003371 }
3372#endif
3373 } else {
3374 /* It's a leaf. make a hole in the records */
3375 union xfs_btree_rec *rp;
3376
3377 rp = xfs_btree_rec_addr(cur, ptr, block);
3378
3379 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3380
3381 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003382 xfs_btree_copy_recs(cur, rp, rec, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003383 xfs_btree_set_numrecs(block, ++numrecs);
3384 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3385#ifdef DEBUG
3386 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003387 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3388 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003389 }
3390#endif
3391 }
3392
3393 /* Log the new number of records in the btree header. */
3394 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3395
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003396 /*
3397 * If we just inserted into a new tree block, we have to
3398 * recalculate nkey here because nkey is out of date.
3399 *
3400 * Otherwise we're just updating an existing block (having shoved
3401 * some records into the new tree block), so use the regular key
3402 * update mechanism.
3403 */
3404 if (bp && bp->b_bn != old_bn) {
3405 xfs_btree_get_keys(cur, block, lkey);
3406 } else if (xfs_btree_needs_key_update(cur, optr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003407 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003408 if (error)
3409 goto error0;
3410 }
3411
3412 /*
3413 * If we are tracking the last record in the tree and
3414 * we are at the far right edge of the tree, update it.
3415 */
3416 if (xfs_btree_is_lastrec(cur, block, level)) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003417 cur->bc_ops->update_lastrec(cur, block, rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003418 ptr, LASTREC_INSREC);
3419 }
3420
3421 /*
3422 * Return the new block number, if any.
3423 * If there is one, give back a record value and a cursor too.
3424 */
3425 *ptrp = nptr;
3426 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003427 xfs_btree_copy_keys(cur, key, lkey, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003428 *curp = ncur;
3429 }
3430
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003431 *stat = 1;
3432 return 0;
3433
3434error0:
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003435 return error;
3436}
3437
3438/*
3439 * Insert the record at the point referenced by cur.
3440 *
3441 * A multi-level split of the tree on insert will invalidate the original
3442 * cursor. All callers of this function should assume that the cursor is
3443 * no longer valid and revalidate it.
3444 */
3445int
3446xfs_btree_insert(
3447 struct xfs_btree_cur *cur,
3448 int *stat)
3449{
3450 int error; /* error return value */
3451 int i; /* result value, 0 for failure */
3452 int level; /* current level number in btree */
3453 union xfs_btree_ptr nptr; /* new block number (split result) */
3454 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3455 struct xfs_btree_cur *pcur; /* previous level's cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003456 union xfs_btree_key bkey; /* key of block to insert */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003457 union xfs_btree_key *key;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003458 union xfs_btree_rec rec; /* record to insert */
3459
3460 level = 0;
3461 ncur = NULL;
3462 pcur = cur;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003463 key = &bkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003464
3465 xfs_btree_set_ptr_null(cur, &nptr);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003466
3467 /* Make a key out of the record data to be inserted, and save it. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003468 cur->bc_ops->init_rec_from_cur(cur, &rec);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003469 cur->bc_ops->init_key_from_rec(key, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003470
3471 /*
3472 * Loop going up the tree, starting at the leaf level.
3473 * Stop when we don't get a split block, that must mean that
3474 * the insert is finished with this level.
3475 */
3476 do {
3477 /*
3478 * Insert nrec/nptr into this level of the tree.
3479 * Note if we fail, nptr will be null.
3480 */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003481 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003482 &ncur, &i);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003483 if (error) {
3484 if (pcur != cur)
3485 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3486 goto error0;
3487 }
3488
Eric Sandeenc29aad42015-02-23 22:39:08 +11003489 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003490 level++;
3491
3492 /*
3493 * See if the cursor we just used is trash.
3494 * Can't trash the caller's cursor, but otherwise we should
3495 * if ncur is a new cursor or we're about to be done.
3496 */
3497 if (pcur != cur &&
3498 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3499 /* Save the state from the cursor before we trash it */
3500 if (cur->bc_ops->update_cursor)
3501 cur->bc_ops->update_cursor(pcur, cur);
3502 cur->bc_nlevels = pcur->bc_nlevels;
3503 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3504 }
3505 /* If we got a new cursor, switch to it. */
3506 if (ncur) {
3507 pcur = ncur;
3508 ncur = NULL;
3509 }
3510 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3511
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003512 *stat = i;
3513 return 0;
3514error0:
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003515 return error;
3516}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003517
3518/*
3519 * Try to merge a non-leaf block back into the inode root.
3520 *
3521 * Note: the killroot names comes from the fact that we're effectively
3522 * killing the old root block. But because we can't just delete the
3523 * inode we have to copy the single block it was pointing to into the
3524 * inode.
3525 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003526STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003527xfs_btree_kill_iroot(
3528 struct xfs_btree_cur *cur)
3529{
3530 int whichfork = cur->bc_private.b.whichfork;
3531 struct xfs_inode *ip = cur->bc_private.b.ip;
3532 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3533 struct xfs_btree_block *block;
3534 struct xfs_btree_block *cblock;
3535 union xfs_btree_key *kp;
3536 union xfs_btree_key *ckp;
3537 union xfs_btree_ptr *pp;
3538 union xfs_btree_ptr *cpp;
3539 struct xfs_buf *cbp;
3540 int level;
3541 int index;
3542 int numrecs;
Christoph Hellwig196328e2016-02-08 14:58:07 +11003543 int error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003544#ifdef DEBUG
3545 union xfs_btree_ptr ptr;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003546#endif
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003547 int i;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003548
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003549 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3550 ASSERT(cur->bc_nlevels > 1);
3551
3552 /*
3553 * Don't deal with the root block needs to be a leaf case.
3554 * We're just going to turn the thing back into extents anyway.
3555 */
3556 level = cur->bc_nlevels - 1;
3557 if (level == 1)
3558 goto out0;
3559
3560 /*
3561 * Give up if the root has multiple children.
3562 */
3563 block = xfs_btree_get_iroot(cur);
3564 if (xfs_btree_get_numrecs(block) != 1)
3565 goto out0;
3566
3567 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3568 numrecs = xfs_btree_get_numrecs(cblock);
3569
3570 /*
3571 * Only do this if the next level will fit.
3572 * Then the data must be copied up to the inode,
3573 * instead of freeing the root you free the next level.
3574 */
3575 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3576 goto out0;
3577
3578 XFS_BTREE_STATS_INC(cur, killroot);
3579
3580#ifdef DEBUG
3581 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3582 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3583 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3584 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3585#endif
3586
3587 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3588 if (index) {
3589 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3590 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003591 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003592 }
3593
3594 be16_add_cpu(&block->bb_numrecs, index);
3595 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3596
3597 kp = xfs_btree_key_addr(cur, 1, block);
3598 ckp = xfs_btree_key_addr(cur, 1, cblock);
3599 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3600
3601 pp = xfs_btree_ptr_addr(cur, 1, block);
3602 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003603
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003604 for (i = 0; i < numrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003605 error = xfs_btree_debug_check_ptr(cur, cpp, i, level - 1);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003606 if (error)
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003607 return error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003608 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003609
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003610 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3611
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003612 error = xfs_btree_free_block(cur, cbp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003613 if (error)
Christoph Hellwig196328e2016-02-08 14:58:07 +11003614 return error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003615
3616 cur->bc_bufs[level - 1] = NULL;
3617 be16_add_cpu(&block->bb_level, -1);
3618 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003619 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003620 cur->bc_nlevels--;
3621out0:
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003622 return 0;
3623}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003624
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003625/*
3626 * Kill the current root node, and replace it with it's only child node.
3627 */
3628STATIC int
3629xfs_btree_kill_root(
3630 struct xfs_btree_cur *cur,
3631 struct xfs_buf *bp,
3632 int level,
3633 union xfs_btree_ptr *newroot)
3634{
3635 int error;
3636
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003637 XFS_BTREE_STATS_INC(cur, killroot);
3638
3639 /*
3640 * Update the root pointer, decreasing the level by 1 and then
3641 * free the old root.
3642 */
3643 cur->bc_ops->set_root(cur, newroot, -1);
3644
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003645 error = xfs_btree_free_block(cur, bp);
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003646 if (error)
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003647 return error;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003648
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003649 cur->bc_bufs[level] = NULL;
3650 cur->bc_ra[level] = 0;
3651 cur->bc_nlevels--;
3652
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003653 return 0;
3654}
3655
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003656STATIC int
3657xfs_btree_dec_cursor(
3658 struct xfs_btree_cur *cur,
3659 int level,
3660 int *stat)
3661{
3662 int error;
3663 int i;
3664
3665 if (level > 0) {
3666 error = xfs_btree_decrement(cur, level, &i);
3667 if (error)
3668 return error;
3669 }
3670
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003671 *stat = 1;
3672 return 0;
3673}
3674
3675/*
3676 * Single level of the btree record deletion routine.
3677 * Delete record pointed to by cur/level.
3678 * Remove the record from its block then rebalance the tree.
3679 * Return 0 for error, 1 for done, 2 to go on to the next level.
3680 */
3681STATIC int /* error */
3682xfs_btree_delrec(
3683 struct xfs_btree_cur *cur, /* btree cursor */
3684 int level, /* level removing record from */
3685 int *stat) /* fail/done/go-on */
3686{
3687 struct xfs_btree_block *block; /* btree block */
3688 union xfs_btree_ptr cptr; /* current block ptr */
3689 struct xfs_buf *bp; /* buffer for block */
3690 int error; /* error return value */
3691 int i; /* loop counter */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003692 union xfs_btree_ptr lptr; /* left sibling block ptr */
3693 struct xfs_buf *lbp; /* left buffer pointer */
3694 struct xfs_btree_block *left; /* left btree block */
3695 int lrecs = 0; /* left record count */
3696 int ptr; /* key/record index */
3697 union xfs_btree_ptr rptr; /* right sibling block ptr */
3698 struct xfs_buf *rbp; /* right buffer pointer */
3699 struct xfs_btree_block *right; /* right btree block */
3700 struct xfs_btree_block *rrblock; /* right-right btree block */
3701 struct xfs_buf *rrbp; /* right-right buffer pointer */
3702 int rrecs = 0; /* right record count */
3703 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3704 int numrecs; /* temporary numrec count */
3705
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003706 tcur = NULL;
3707
3708 /* Get the index of the entry being deleted, check for nothing there. */
3709 ptr = cur->bc_ptrs[level];
3710 if (ptr == 0) {
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003711 *stat = 0;
3712 return 0;
3713 }
3714
3715 /* Get the buffer & block containing the record or key/ptr. */
3716 block = xfs_btree_get_block(cur, level, &bp);
3717 numrecs = xfs_btree_get_numrecs(block);
3718
3719#ifdef DEBUG
3720 error = xfs_btree_check_block(cur, block, level, bp);
3721 if (error)
3722 goto error0;
3723#endif
3724
3725 /* Fail if we're off the end of the block. */
3726 if (ptr > numrecs) {
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003727 *stat = 0;
3728 return 0;
3729 }
3730
3731 XFS_BTREE_STATS_INC(cur, delrec);
3732 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3733
3734 /* Excise the entries being deleted. */
3735 if (level > 0) {
3736 /* It's a nonleaf. operate on keys and ptrs */
3737 union xfs_btree_key *lkp;
3738 union xfs_btree_ptr *lpp;
3739
3740 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3741 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3742
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003743 for (i = 0; i < numrecs - ptr; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07003744 error = xfs_btree_debug_check_ptr(cur, lpp, i, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003745 if (error)
3746 goto error0;
3747 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003748
3749 if (ptr < numrecs) {
3750 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3751 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3752 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3753 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3754 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003755 } else {
3756 /* It's a leaf. operate on records */
3757 if (ptr < numrecs) {
3758 xfs_btree_shift_recs(cur,
3759 xfs_btree_rec_addr(cur, ptr + 1, block),
3760 -1, numrecs - ptr);
3761 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3762 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003763 }
3764
3765 /*
3766 * Decrement and log the number of entries in the block.
3767 */
3768 xfs_btree_set_numrecs(block, --numrecs);
3769 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3770
3771 /*
3772 * If we are tracking the last record in the tree and
3773 * we are at the far right edge of the tree, update it.
3774 */
3775 if (xfs_btree_is_lastrec(cur, block, level)) {
3776 cur->bc_ops->update_lastrec(cur, block, NULL,
3777 ptr, LASTREC_DELREC);
3778 }
3779
3780 /*
3781 * We're at the root level. First, shrink the root block in-memory.
3782 * Try to get rid of the next level down. If we can't then there's
3783 * nothing left to do.
3784 */
3785 if (level == cur->bc_nlevels - 1) {
3786 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3787 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3788 cur->bc_private.b.whichfork);
3789
3790 error = xfs_btree_kill_iroot(cur);
3791 if (error)
3792 goto error0;
3793
3794 error = xfs_btree_dec_cursor(cur, level, stat);
3795 if (error)
3796 goto error0;
3797 *stat = 1;
3798 return 0;
3799 }
3800
3801 /*
3802 * If this is the root level, and there's only one entry left,
3803 * and it's NOT the leaf level, then we can get rid of this
3804 * level.
3805 */
3806 if (numrecs == 1 && level > 0) {
3807 union xfs_btree_ptr *pp;
3808 /*
3809 * pp is still set to the first pointer in the block.
3810 * Make it the new root of the btree.
3811 */
3812 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003813 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003814 if (error)
3815 goto error0;
3816 } else if (level > 0) {
3817 error = xfs_btree_dec_cursor(cur, level, stat);
3818 if (error)
3819 goto error0;
3820 }
3821 *stat = 1;
3822 return 0;
3823 }
3824
3825 /*
3826 * If we deleted the leftmost entry in the block, update the
3827 * key values above us in the tree.
3828 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003829 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003830 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003831 if (error)
3832 goto error0;
3833 }
3834
3835 /*
3836 * If the number of records remaining in the block is at least
3837 * the minimum, we're done.
3838 */
3839 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3840 error = xfs_btree_dec_cursor(cur, level, stat);
3841 if (error)
3842 goto error0;
3843 return 0;
3844 }
3845
3846 /*
3847 * Otherwise, we have to move some records around to keep the
3848 * tree balanced. Look at the left and right sibling blocks to
3849 * see if we can re-balance by moving only one record.
3850 */
3851 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3852 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3853
3854 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3855 /*
3856 * One child of root, need to get a chance to copy its contents
3857 * into the root and delete it. Can't go up to next level,
3858 * there's nothing to delete there.
3859 */
3860 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3861 xfs_btree_ptr_is_null(cur, &lptr) &&
3862 level == cur->bc_nlevels - 2) {
3863 error = xfs_btree_kill_iroot(cur);
3864 if (!error)
3865 error = xfs_btree_dec_cursor(cur, level, stat);
3866 if (error)
3867 goto error0;
3868 return 0;
3869 }
3870 }
3871
3872 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3873 !xfs_btree_ptr_is_null(cur, &lptr));
3874
3875 /*
3876 * Duplicate the cursor so our btree manipulations here won't
3877 * disrupt the next level up.
3878 */
3879 error = xfs_btree_dup_cursor(cur, &tcur);
3880 if (error)
3881 goto error0;
3882
3883 /*
3884 * If there's a right sibling, see if it's ok to shift an entry
3885 * out of it.
3886 */
3887 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3888 /*
3889 * Move the temp cursor to the last entry in the next block.
3890 * Actually any entry but the first would suffice.
3891 */
3892 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003893 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003894
3895 error = xfs_btree_increment(tcur, level, &i);
3896 if (error)
3897 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003898 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003899
3900 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003901 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003902
3903 /* Grab a pointer to the block. */
3904 right = xfs_btree_get_block(tcur, level, &rbp);
3905#ifdef DEBUG
3906 error = xfs_btree_check_block(tcur, right, level, rbp);
3907 if (error)
3908 goto error0;
3909#endif
3910 /* Grab the current block number, for future use. */
3911 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3912
3913 /*
3914 * If right block is full enough so that removing one entry
3915 * won't make it too empty, and left-shifting an entry out
3916 * of right to us works, we're done.
3917 */
3918 if (xfs_btree_get_numrecs(right) - 1 >=
3919 cur->bc_ops->get_minrecs(tcur, level)) {
3920 error = xfs_btree_lshift(tcur, level, &i);
3921 if (error)
3922 goto error0;
3923 if (i) {
3924 ASSERT(xfs_btree_get_numrecs(block) >=
3925 cur->bc_ops->get_minrecs(tcur, level));
3926
3927 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3928 tcur = NULL;
3929
3930 error = xfs_btree_dec_cursor(cur, level, stat);
3931 if (error)
3932 goto error0;
3933 return 0;
3934 }
3935 }
3936
3937 /*
3938 * Otherwise, grab the number of records in right for
3939 * future reference, and fix up the temp cursor to point
3940 * to our block again (last record).
3941 */
3942 rrecs = xfs_btree_get_numrecs(right);
3943 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3944 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003945 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003946
3947 error = xfs_btree_decrement(tcur, level, &i);
3948 if (error)
3949 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003950 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003951 }
3952 }
3953
3954 /*
3955 * If there's a left sibling, see if it's ok to shift an entry
3956 * out of it.
3957 */
3958 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3959 /*
3960 * Move the temp cursor to the first entry in the
3961 * previous block.
3962 */
3963 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003964 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003965
3966 error = xfs_btree_decrement(tcur, level, &i);
3967 if (error)
3968 goto error0;
3969 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003970 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003971
3972 /* Grab a pointer to the block. */
3973 left = xfs_btree_get_block(tcur, level, &lbp);
3974#ifdef DEBUG
3975 error = xfs_btree_check_block(cur, left, level, lbp);
3976 if (error)
3977 goto error0;
3978#endif
3979 /* Grab the current block number, for future use. */
3980 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3981
3982 /*
3983 * If left block is full enough so that removing one entry
3984 * won't make it too empty, and right-shifting an entry out
3985 * of left to us works, we're done.
3986 */
3987 if (xfs_btree_get_numrecs(left) - 1 >=
3988 cur->bc_ops->get_minrecs(tcur, level)) {
3989 error = xfs_btree_rshift(tcur, level, &i);
3990 if (error)
3991 goto error0;
3992 if (i) {
3993 ASSERT(xfs_btree_get_numrecs(block) >=
3994 cur->bc_ops->get_minrecs(tcur, level));
3995 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3996 tcur = NULL;
3997 if (level == 0)
3998 cur->bc_ptrs[0]++;
Carlos Maiolinoe157ebd2018-03-06 17:03:30 -08003999
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004000 *stat = 1;
4001 return 0;
4002 }
4003 }
4004
4005 /*
4006 * Otherwise, grab the number of records in right for
4007 * future reference.
4008 */
4009 lrecs = xfs_btree_get_numrecs(left);
4010 }
4011
4012 /* Delete the temp cursor, we're done with it. */
4013 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4014 tcur = NULL;
4015
4016 /* If here, we need to do a join to keep the tree balanced. */
4017 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4018
4019 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4020 lrecs + xfs_btree_get_numrecs(block) <=
4021 cur->bc_ops->get_maxrecs(cur, level)) {
4022 /*
4023 * Set "right" to be the starting block,
4024 * "left" to be the left neighbor.
4025 */
4026 rptr = cptr;
4027 right = block;
4028 rbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004029 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004030 if (error)
4031 goto error0;
4032
4033 /*
4034 * If that won't work, see if we can join with the right neighbor block.
4035 */
4036 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4037 rrecs + xfs_btree_get_numrecs(block) <=
4038 cur->bc_ops->get_maxrecs(cur, level)) {
4039 /*
4040 * Set "left" to be the starting block,
4041 * "right" to be the right neighbor.
4042 */
4043 lptr = cptr;
4044 left = block;
4045 lbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004046 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004047 if (error)
4048 goto error0;
4049
4050 /*
4051 * Otherwise, we can't fix the imbalance.
4052 * Just return. This is probably a logic error, but it's not fatal.
4053 */
4054 } else {
4055 error = xfs_btree_dec_cursor(cur, level, stat);
4056 if (error)
4057 goto error0;
4058 return 0;
4059 }
4060
4061 rrecs = xfs_btree_get_numrecs(right);
4062 lrecs = xfs_btree_get_numrecs(left);
4063
4064 /*
4065 * We're now going to join "left" and "right" by moving all the stuff
4066 * in "right" to "left" and deleting "right".
4067 */
4068 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4069 if (level > 0) {
4070 /* It's a non-leaf. Move keys and pointers. */
4071 union xfs_btree_key *lkp; /* left btree key */
4072 union xfs_btree_ptr *lpp; /* left address pointer */
4073 union xfs_btree_key *rkp; /* right btree key */
4074 union xfs_btree_ptr *rpp; /* right address pointer */
4075
4076 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4077 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4078 rkp = xfs_btree_key_addr(cur, 1, right);
4079 rpp = xfs_btree_ptr_addr(cur, 1, right);
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07004080
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004081 for (i = 1; i < rrecs; i++) {
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07004082 error = xfs_btree_debug_check_ptr(cur, rpp, i, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004083 if (error)
4084 goto error0;
4085 }
Darrick J. Wong4cbae4b2018-06-03 21:10:48 -07004086
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004087 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4088 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4089
4090 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4091 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4092 } else {
4093 /* It's a leaf. Move records. */
4094 union xfs_btree_rec *lrp; /* left record pointer */
4095 union xfs_btree_rec *rrp; /* right record pointer */
4096
4097 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4098 rrp = xfs_btree_rec_addr(cur, 1, right);
4099
4100 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4101 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4102 }
4103
4104 XFS_BTREE_STATS_INC(cur, join);
4105
4106 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004107 * Fix up the number of records and right block pointer in the
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004108 * surviving block, and log it.
4109 */
4110 xfs_btree_set_numrecs(left, lrecs + rrecs);
4111 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4112 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4113 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4114
4115 /* If there is a right sibling, point it to the remaining block. */
4116 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4117 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004118 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004119 if (error)
4120 goto error0;
4121 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4122 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4123 }
4124
4125 /* Free the deleted block. */
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11004126 error = xfs_btree_free_block(cur, rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004127 if (error)
4128 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004129
4130 /*
4131 * If we joined with the left neighbor, set the buffer in the
4132 * cursor to the left block, and fix up the index.
4133 */
4134 if (bp != lbp) {
4135 cur->bc_bufs[level] = lbp;
4136 cur->bc_ptrs[level] += lrecs;
4137 cur->bc_ra[level] = 0;
4138 }
4139 /*
4140 * If we joined with the right neighbor and there's a level above
4141 * us, increment the cursor at that level.
4142 */
4143 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4144 (level + 1 < cur->bc_nlevels)) {
4145 error = xfs_btree_increment(cur, level + 1, &i);
4146 if (error)
4147 goto error0;
4148 }
4149
4150 /*
4151 * Readjust the ptr at this level if it's not a leaf, since it's
4152 * still pointing at the deletion point, which makes the cursor
4153 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4154 * We can't use decrement because it would change the next level up.
4155 */
4156 if (level > 0)
4157 cur->bc_ptrs[level]--;
4158
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004159 /*
4160 * We combined blocks, so we have to update the parent keys if the
4161 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4162 * points to the old block so that the caller knows which record to
4163 * delete. Therefore, the caller must be savvy enough to call updkeys
4164 * for us if we return stat == 2. The other exit points from this
4165 * function don't require deletions further up the tree, so they can
4166 * call updkeys directly.
4167 */
4168
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004169 /* Return value means the next level up has something to do. */
4170 *stat = 2;
4171 return 0;
4172
4173error0:
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004174 if (tcur)
4175 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4176 return error;
4177}
4178
4179/*
4180 * Delete the record pointed to by cur.
4181 * The cursor refers to the place where the record was (could be inserted)
4182 * when the operation returns.
4183 */
4184int /* error */
4185xfs_btree_delete(
4186 struct xfs_btree_cur *cur,
4187 int *stat) /* success/failure */
4188{
4189 int error; /* error return value */
4190 int level;
4191 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004192 bool joined = false;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004193
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004194 /*
4195 * Go up the tree, starting at leaf level.
4196 *
4197 * If 2 is returned then a join was done; go to the next level.
4198 * Otherwise we are done.
4199 */
4200 for (level = 0, i = 2; i == 2; level++) {
4201 error = xfs_btree_delrec(cur, level, &i);
4202 if (error)
4203 goto error0;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004204 if (i == 2)
4205 joined = true;
4206 }
4207
4208 /*
4209 * If we combined blocks as part of deleting the record, delrec won't
4210 * have updated the parent high keys so we have to do that here.
4211 */
4212 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4213 error = xfs_btree_updkeys_force(cur, 0);
4214 if (error)
4215 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004216 }
4217
4218 if (i == 0) {
4219 for (level = 1; level < cur->bc_nlevels; level++) {
4220 if (cur->bc_ptrs[level] == 0) {
4221 error = xfs_btree_decrement(cur, level, &i);
4222 if (error)
4223 goto error0;
4224 break;
4225 }
4226 }
4227 }
4228
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004229 *stat = i;
4230 return 0;
4231error0:
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004232 return error;
4233}
Christoph Hellwig8cc938f2008-10-30 16:58:11 +11004234
4235/*
4236 * Get the data from the pointed-to record.
4237 */
4238int /* error */
4239xfs_btree_get_rec(
4240 struct xfs_btree_cur *cur, /* btree cursor */
4241 union xfs_btree_rec **recp, /* output: btree record */
4242 int *stat) /* output: success/failure */
4243{
4244 struct xfs_btree_block *block; /* btree block */
4245 struct xfs_buf *bp; /* buffer pointer */
4246 int ptr; /* record number */
4247#ifdef DEBUG
4248 int error; /* error return value */
4249#endif
4250
4251 ptr = cur->bc_ptrs[0];
4252 block = xfs_btree_get_block(cur, 0, &bp);
4253
4254#ifdef DEBUG
4255 error = xfs_btree_check_block(cur, block, 0, bp);
4256 if (error)
4257 return error;
4258#endif
4259
4260 /*
4261 * Off the right end or left end, return failure.
4262 */
4263 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4264 *stat = 0;
4265 return 0;
4266 }
4267
4268 /*
4269 * Point to the record and extract its data.
4270 */
4271 *recp = xfs_btree_rec_addr(cur, ptr, block);
4272 *stat = 1;
4273 return 0;
4274}
Dave Chinner21b5c972013-08-30 10:23:44 +10004275
Darrick J. Wong28a89562016-08-03 11:10:55 +10004276/* Visit a block in a btree. */
4277STATIC int
4278xfs_btree_visit_block(
4279 struct xfs_btree_cur *cur,
4280 int level,
4281 xfs_btree_visit_blocks_fn fn,
4282 void *data)
4283{
4284 struct xfs_btree_block *block;
4285 struct xfs_buf *bp;
4286 union xfs_btree_ptr rptr;
4287 int error;
4288
4289 /* do right sibling readahead */
4290 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4291 block = xfs_btree_get_block(cur, level, &bp);
4292
4293 /* process the block */
4294 error = fn(cur, level, data);
4295 if (error)
4296 return error;
4297
4298 /* now read rh sibling block for next iteration */
4299 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4300 if (xfs_btree_ptr_is_null(cur, &rptr))
4301 return -ENOENT;
4302
4303 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4304}
4305
4306
4307/* Visit every block in a btree. */
4308int
4309xfs_btree_visit_blocks(
4310 struct xfs_btree_cur *cur,
4311 xfs_btree_visit_blocks_fn fn,
4312 void *data)
4313{
4314 union xfs_btree_ptr lptr;
4315 int level;
4316 struct xfs_btree_block *block = NULL;
4317 int error = 0;
4318
4319 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4320
4321 /* for each level */
4322 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4323 /* grab the left hand block */
4324 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4325 if (error)
4326 return error;
4327
4328 /* readahead the left most block for the next level down */
4329 if (level > 0) {
4330 union xfs_btree_ptr *ptr;
4331
4332 ptr = xfs_btree_ptr_addr(cur, 1, block);
4333 xfs_btree_readahead_ptr(cur, ptr, 1);
4334
4335 /* save for the next iteration of the loop */
Eric Sandeena4d768e2017-05-22 19:54:10 -07004336 xfs_btree_copy_ptrs(cur, &lptr, ptr, 1);
Darrick J. Wong28a89562016-08-03 11:10:55 +10004337 }
4338
4339 /* for each buffer in the level */
4340 do {
4341 error = xfs_btree_visit_block(cur, level, fn, data);
4342 } while (!error);
4343
4344 if (error != -ENOENT)
4345 return error;
4346 }
4347
4348 return 0;
4349}
4350
Dave Chinner21b5c972013-08-30 10:23:44 +10004351/*
4352 * Change the owner of a btree.
4353 *
4354 * The mechanism we use here is ordered buffer logging. Because we don't know
4355 * how many buffers were are going to need to modify, we don't really want to
4356 * have to make transaction reservations for the worst case of every buffer in a
4357 * full size btree as that may be more space that we can fit in the log....
4358 *
4359 * We do the btree walk in the most optimal manner possible - we have sibling
4360 * pointers so we can just walk all the blocks on each level from left to right
4361 * in a single pass, and then move to the next level and do the same. We can
4362 * also do readahead on the sibling pointers to get IO moving more quickly,
4363 * though for slow disks this is unlikely to make much difference to performance
4364 * as the amount of CPU work we have to do before moving to the next block is
4365 * relatively small.
4366 *
4367 * For each btree block that we load, modify the owner appropriately, set the
4368 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4369 * we mark the region we change dirty so that if the buffer is relogged in
4370 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10004371 * correctly relogged in that transaction. If we are in recovery context, then
4372 * just queue the modified buffer as delayed write buffer so the transaction
4373 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10004374 */
Darrick J. Wong28a89562016-08-03 11:10:55 +10004375struct xfs_btree_block_change_owner_info {
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004376 uint64_t new_owner;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004377 struct list_head *buffer_list;
4378};
4379
Dave Chinner21b5c972013-08-30 10:23:44 +10004380static int
4381xfs_btree_block_change_owner(
4382 struct xfs_btree_cur *cur,
4383 int level,
Darrick J. Wong28a89562016-08-03 11:10:55 +10004384 void *data)
Dave Chinner21b5c972013-08-30 10:23:44 +10004385{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004386 struct xfs_btree_block_change_owner_info *bbcoi = data;
Dave Chinner21b5c972013-08-30 10:23:44 +10004387 struct xfs_btree_block *block;
4388 struct xfs_buf *bp;
Dave Chinner21b5c972013-08-30 10:23:44 +10004389
4390 /* modify the owner */
4391 block = xfs_btree_get_block(cur, level, &bp);
Brian Foster2dd3d702017-08-29 10:08:40 -07004392 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
4393 if (block->bb_u.l.bb_owner == cpu_to_be64(bbcoi->new_owner))
4394 return 0;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004395 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
Brian Foster2dd3d702017-08-29 10:08:40 -07004396 } else {
4397 if (block->bb_u.s.bb_owner == cpu_to_be32(bbcoi->new_owner))
4398 return 0;
Darrick J. Wong28a89562016-08-03 11:10:55 +10004399 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
Brian Foster2dd3d702017-08-29 10:08:40 -07004400 }
Dave Chinner21b5c972013-08-30 10:23:44 +10004401
4402 /*
Dave Chinner638f44162013-08-30 10:23:45 +10004403 * If the block is a root block hosted in an inode, we might not have a
4404 * buffer pointer here and we shouldn't attempt to log the change as the
4405 * information is already held in the inode and discarded when the root
4406 * block is formatted into the on-disk inode fork. We still change it,
4407 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10004408 */
Brian Foster2dd3d702017-08-29 10:08:40 -07004409 if (!bp) {
Dave Chinner21b5c972013-08-30 10:23:44 +10004410 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4411 ASSERT(level == cur->bc_nlevels - 1);
Brian Foster2dd3d702017-08-29 10:08:40 -07004412 return 0;
4413 }
4414
4415 if (cur->bc_tp) {
4416 if (!xfs_trans_ordered_buf(cur->bc_tp, bp)) {
4417 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4418 return -EAGAIN;
4419 }
4420 } else {
4421 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
Dave Chinner21b5c972013-08-30 10:23:44 +10004422 }
4423
Darrick J. Wong28a89562016-08-03 11:10:55 +10004424 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10004425}
4426
4427int
4428xfs_btree_change_owner(
4429 struct xfs_btree_cur *cur,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004430 uint64_t new_owner,
Dave Chinner638f44162013-08-30 10:23:45 +10004431 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004432{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004433 struct xfs_btree_block_change_owner_info bbcoi;
Dave Chinner21b5c972013-08-30 10:23:44 +10004434
Darrick J. Wong28a89562016-08-03 11:10:55 +10004435 bbcoi.new_owner = new_owner;
4436 bbcoi.buffer_list = buffer_list;
Dave Chinner21b5c972013-08-30 10:23:44 +10004437
Darrick J. Wong28a89562016-08-03 11:10:55 +10004438 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4439 &bbcoi);
Dave Chinner21b5c972013-08-30 10:23:44 +10004440}
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004441
Darrick J. Wong8368a602018-01-08 10:51:00 -08004442/* Verify the v5 fields of a long-format btree block. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004443xfs_failaddr_t
Darrick J. Wong8368a602018-01-08 10:51:00 -08004444xfs_btree_lblock_v5hdr_verify(
4445 struct xfs_buf *bp,
4446 uint64_t owner)
4447{
4448 struct xfs_mount *mp = bp->b_target->bt_mount;
4449 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4450
4451 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004452 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004453 if (!uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004454 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004455 if (block->bb_u.l.bb_blkno != cpu_to_be64(bp->b_bn))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004456 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004457 if (owner != XFS_RMAP_OWN_UNKNOWN &&
4458 be64_to_cpu(block->bb_u.l.bb_owner) != owner)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004459 return __this_address;
4460 return NULL;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004461}
4462
4463/* Verify a long-format btree block. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004464xfs_failaddr_t
Darrick J. Wong8368a602018-01-08 10:51:00 -08004465xfs_btree_lblock_verify(
4466 struct xfs_buf *bp,
4467 unsigned int max_recs)
4468{
4469 struct xfs_mount *mp = bp->b_target->bt_mount;
4470 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4471
4472 /* numrecs verification */
4473 if (be16_to_cpu(block->bb_numrecs) > max_recs)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004474 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004475
4476 /* sibling pointer verification */
4477 if (block->bb_u.l.bb_leftsib != cpu_to_be64(NULLFSBLOCK) &&
4478 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_leftsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004479 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004480 if (block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK) &&
4481 !xfs_verify_fsbno(mp, be64_to_cpu(block->bb_u.l.bb_rightsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004482 return __this_address;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004483
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004484 return NULL;
Darrick J. Wong8368a602018-01-08 10:51:00 -08004485}
4486
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004487/**
4488 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4489 * btree block
4490 *
4491 * @bp: buffer containing the btree block
4492 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4493 * @pag_max_level: pointer to the per-ag max level field
4494 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004495xfs_failaddr_t
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004496xfs_btree_sblock_v5hdr_verify(
4497 struct xfs_buf *bp)
4498{
4499 struct xfs_mount *mp = bp->b_target->bt_mount;
4500 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4501 struct xfs_perag *pag = bp->b_pag;
4502
4503 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004504 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004505 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004506 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004507 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004508 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004509 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004510 return __this_address;
4511 return NULL;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004512}
4513
4514/**
4515 * xfs_btree_sblock_verify() -- verify a short-format btree block
4516 *
4517 * @bp: buffer containing the btree block
4518 * @max_recs: maximum records allowed in this btree node
4519 */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004520xfs_failaddr_t
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004521xfs_btree_sblock_verify(
4522 struct xfs_buf *bp,
4523 unsigned int max_recs)
4524{
4525 struct xfs_mount *mp = bp->b_target->bt_mount;
4526 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004527 xfs_agblock_t agno;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004528
4529 /* numrecs verification */
4530 if (be16_to_cpu(block->bb_numrecs) > max_recs)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004531 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004532
4533 /* sibling pointer verification */
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004534 agno = xfs_daddr_to_agno(mp, XFS_BUF_ADDR(bp));
4535 if (block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK) &&
4536 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_leftsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004537 return __this_address;
Darrick J. Wonge1e55aa2018-01-08 10:51:01 -08004538 if (block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK) &&
4539 !xfs_verify_agbno(mp, agno, be32_to_cpu(block->bb_u.s.bb_rightsib)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004540 return __this_address;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004541
Darrick J. Wonga6a781a2018-01-08 10:51:03 -08004542 return NULL;
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004543}
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004544
4545/*
4546 * Calculate the number of btree levels needed to store a given number of
4547 * records in a short-format btree.
4548 */
4549uint
4550xfs_btree_compute_maxlevels(
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004551 uint *limits,
4552 unsigned long len)
4553{
4554 uint level;
4555 unsigned long maxblocks;
4556
4557 maxblocks = (len + limits[0] - 1) / limits[0];
4558 for (level = 1; maxblocks > 1; level++)
4559 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4560 return level;
4561}
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004562
4563/*
4564 * Query a regular btree for all records overlapping a given interval.
4565 * Start with a LE lookup of the key of low_rec and return all records
4566 * until we find a record with a key greater than the key of high_rec.
4567 */
4568STATIC int
4569xfs_btree_simple_query_range(
4570 struct xfs_btree_cur *cur,
4571 union xfs_btree_key *low_key,
4572 union xfs_btree_key *high_key,
4573 xfs_btree_query_range_fn fn,
4574 void *priv)
4575{
4576 union xfs_btree_rec *recp;
4577 union xfs_btree_key rec_key;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004578 int64_t diff;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004579 int stat;
4580 bool firstrec = true;
4581 int error;
4582
4583 ASSERT(cur->bc_ops->init_high_key_from_rec);
4584 ASSERT(cur->bc_ops->diff_two_keys);
4585
4586 /*
4587 * Find the leftmost record. The btree cursor must be set
4588 * to the low record used to generate low_key.
4589 */
4590 stat = 0;
4591 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4592 if (error)
4593 goto out;
4594
Darrick J. Wong5b5c2db2016-08-26 16:00:10 +10004595 /* Nothing? See if there's anything to the right. */
4596 if (!stat) {
4597 error = xfs_btree_increment(cur, 0, &stat);
4598 if (error)
4599 goto out;
4600 }
4601
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004602 while (stat) {
4603 /* Find the record. */
4604 error = xfs_btree_get_rec(cur, &recp, &stat);
4605 if (error || !stat)
4606 break;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004607
4608 /* Skip if high_key(rec) < low_key. */
4609 if (firstrec) {
Darrick J. Wong72227892016-08-26 15:59:50 +10004610 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004611 firstrec = false;
4612 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4613 &rec_key);
4614 if (diff > 0)
4615 goto advloop;
4616 }
4617
4618 /* Stop if high_key < low_key(rec). */
Darrick J. Wong72227892016-08-26 15:59:50 +10004619 cur->bc_ops->init_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004620 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4621 if (diff > 0)
4622 break;
4623
4624 /* Callback */
4625 error = fn(cur, recp, priv);
4626 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4627 break;
4628
4629advloop:
4630 /* Move on to the next record. */
4631 error = xfs_btree_increment(cur, 0, &stat);
4632 if (error)
4633 break;
4634 }
4635
4636out:
4637 return error;
4638}
4639
4640/*
4641 * Query an overlapped interval btree for all records overlapping a given
4642 * interval. This function roughly follows the algorithm given in
4643 * "Interval Trees" of _Introduction to Algorithms_, which is section
4644 * 14.3 in the 2nd and 3rd editions.
4645 *
4646 * First, generate keys for the low and high records passed in.
4647 *
4648 * For any leaf node, generate the high and low keys for the record.
4649 * If the record keys overlap with the query low/high keys, pass the
4650 * record to the function iterator.
4651 *
4652 * For any internal node, compare the low and high keys of each
4653 * pointer against the query low/high keys. If there's an overlap,
4654 * follow the pointer.
4655 *
4656 * As an optimization, we stop scanning a block when we find a low key
4657 * that is greater than the query's high key.
4658 */
4659STATIC int
4660xfs_btree_overlapped_query_range(
4661 struct xfs_btree_cur *cur,
4662 union xfs_btree_key *low_key,
4663 union xfs_btree_key *high_key,
4664 xfs_btree_query_range_fn fn,
4665 void *priv)
4666{
4667 union xfs_btree_ptr ptr;
4668 union xfs_btree_ptr *pp;
4669 union xfs_btree_key rec_key;
4670 union xfs_btree_key rec_hkey;
4671 union xfs_btree_key *lkp;
4672 union xfs_btree_key *hkp;
4673 union xfs_btree_rec *recp;
4674 struct xfs_btree_block *block;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07004675 int64_t ldiff;
4676 int64_t hdiff;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004677 int level;
4678 struct xfs_buf *bp;
4679 int i;
4680 int error;
4681
4682 /* Load the root of the btree. */
4683 level = cur->bc_nlevels - 1;
4684 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4685 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4686 if (error)
4687 return error;
4688 xfs_btree_get_block(cur, level, &bp);
4689 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4690#ifdef DEBUG
4691 error = xfs_btree_check_block(cur, block, level, bp);
4692 if (error)
4693 goto out;
4694#endif
4695 cur->bc_ptrs[level] = 1;
4696
4697 while (level < cur->bc_nlevels) {
4698 block = xfs_btree_get_block(cur, level, &bp);
4699
4700 /* End of node, pop back towards the root. */
4701 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4702pop_up:
4703 if (level < cur->bc_nlevels - 1)
4704 cur->bc_ptrs[level + 1]++;
4705 level++;
4706 continue;
4707 }
4708
4709 if (level == 0) {
4710 /* Handle a leaf node. */
4711 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4712
4713 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4714 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4715 low_key);
4716
4717 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4718 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4719 &rec_key);
4720
4721 /*
4722 * If (record's high key >= query's low key) and
4723 * (query's high key >= record's low key), then
4724 * this record overlaps the query range; callback.
4725 */
4726 if (ldiff >= 0 && hdiff >= 0) {
4727 error = fn(cur, recp, priv);
4728 if (error < 0 ||
4729 error == XFS_BTREE_QUERY_RANGE_ABORT)
4730 break;
4731 } else if (hdiff < 0) {
4732 /* Record is larger than high key; pop. */
4733 goto pop_up;
4734 }
4735 cur->bc_ptrs[level]++;
4736 continue;
4737 }
4738
4739 /* Handle an internal node. */
4740 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4741 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4742 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4743
4744 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4745 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4746
4747 /*
4748 * If (pointer's high key >= query's low key) and
4749 * (query's high key >= pointer's low key), then
4750 * this record overlaps the query range; follow pointer.
4751 */
4752 if (ldiff >= 0 && hdiff >= 0) {
4753 level--;
4754 error = xfs_btree_lookup_get_block(cur, level, pp,
4755 &block);
4756 if (error)
4757 goto out;
4758 xfs_btree_get_block(cur, level, &bp);
4759 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4760#ifdef DEBUG
4761 error = xfs_btree_check_block(cur, block, level, bp);
4762 if (error)
4763 goto out;
4764#endif
4765 cur->bc_ptrs[level] = 1;
4766 continue;
4767 } else if (hdiff < 0) {
4768 /* The low key is larger than the upper range; pop. */
4769 goto pop_up;
4770 }
4771 cur->bc_ptrs[level]++;
4772 }
4773
4774out:
4775 /*
4776 * If we don't end this function with the cursor pointing at a record
4777 * block, a subsequent non-error cursor deletion will not release
4778 * node-level buffers, causing a buffer leak. This is quite possible
4779 * with a zero-results range query, so release the buffers if we
4780 * failed to return any results.
4781 */
4782 if (cur->bc_bufs[0] == NULL) {
4783 for (i = 0; i < cur->bc_nlevels; i++) {
4784 if (cur->bc_bufs[i]) {
4785 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4786 cur->bc_bufs[i] = NULL;
4787 cur->bc_ptrs[i] = 0;
4788 cur->bc_ra[i] = 0;
4789 }
4790 }
4791 }
4792
4793 return error;
4794}
4795
4796/*
4797 * Query a btree for all records overlapping a given interval of keys. The
4798 * supplied function will be called with each record found; return one of the
4799 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4800 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4801 * negative error code.
4802 */
4803int
4804xfs_btree_query_range(
4805 struct xfs_btree_cur *cur,
4806 union xfs_btree_irec *low_rec,
4807 union xfs_btree_irec *high_rec,
4808 xfs_btree_query_range_fn fn,
4809 void *priv)
4810{
4811 union xfs_btree_rec rec;
4812 union xfs_btree_key low_key;
4813 union xfs_btree_key high_key;
4814
4815 /* Find the keys of both ends of the interval. */
4816 cur->bc_rec = *high_rec;
4817 cur->bc_ops->init_rec_from_cur(cur, &rec);
4818 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4819
4820 cur->bc_rec = *low_rec;
4821 cur->bc_ops->init_rec_from_cur(cur, &rec);
4822 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4823
4824 /* Enforce low key < high key. */
4825 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4826 return -EINVAL;
4827
4828 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4829 return xfs_btree_simple_query_range(cur, &low_key,
4830 &high_key, fn, priv);
4831 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4832 fn, priv);
4833}
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004834
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004835/* Query a btree for all records. */
4836int
4837xfs_btree_query_all(
4838 struct xfs_btree_cur *cur,
4839 xfs_btree_query_range_fn fn,
4840 void *priv)
4841{
Darrick J. Wong5a4c7332017-06-16 11:00:04 -07004842 union xfs_btree_key low_key;
4843 union xfs_btree_key high_key;
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004844
Darrick J. Wong5a4c7332017-06-16 11:00:04 -07004845 memset(&cur->bc_rec, 0, sizeof(cur->bc_rec));
4846 memset(&low_key, 0, sizeof(low_key));
4847 memset(&high_key, 0xFF, sizeof(high_key));
4848
4849 return xfs_btree_simple_query_range(cur, &low_key, &high_key, fn, priv);
Darrick J. Wonge9a25992017-03-28 14:56:35 -07004850}
4851
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004852/*
4853 * Calculate the number of blocks needed to store a given number of records
4854 * in a short-format (per-AG metadata) btree.
4855 */
Darrick J. Wong14861c42018-05-09 10:02:01 -07004856unsigned long long
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004857xfs_btree_calc_size(
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004858 uint *limits,
4859 unsigned long long len)
4860{
4861 int level;
4862 int maxrecs;
Darrick J. Wong14861c42018-05-09 10:02:01 -07004863 unsigned long long rval;
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004864
4865 maxrecs = limits[0];
4866 for (level = 0, rval = 0; len > 1; level++) {
4867 len += maxrecs - 1;
4868 do_div(len, maxrecs);
4869 maxrecs = limits[1];
4870 rval += len;
4871 }
4872 return rval;
4873}
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004874
Eric Biggersf1b82432016-10-20 15:42:30 +11004875static int
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004876xfs_btree_count_blocks_helper(
4877 struct xfs_btree_cur *cur,
4878 int level,
4879 void *data)
4880{
4881 xfs_extlen_t *blocks = data;
4882 (*blocks)++;
4883
4884 return 0;
4885}
4886
4887/* Count the blocks in a btree and return the result in *blocks. */
4888int
4889xfs_btree_count_blocks(
4890 struct xfs_btree_cur *cur,
4891 xfs_extlen_t *blocks)
4892{
4893 *blocks = 0;
4894 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4895 blocks);
4896}
Darrick J. Wongcc3e0942017-10-17 21:37:37 -07004897
4898/* Compare two btree pointers. */
4899int64_t
4900xfs_btree_diff_two_ptrs(
4901 struct xfs_btree_cur *cur,
4902 const union xfs_btree_ptr *a,
4903 const union xfs_btree_ptr *b)
4904{
4905 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4906 return (int64_t)be64_to_cpu(a->l) - be64_to_cpu(b->l);
4907 return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
4908}
Darrick J. Wongce1d8022018-01-16 18:52:12 -08004909
4910/* If there's an extent, we're done. */
4911STATIC int
4912xfs_btree_has_record_helper(
4913 struct xfs_btree_cur *cur,
4914 union xfs_btree_rec *rec,
4915 void *priv)
4916{
4917 return XFS_BTREE_QUERY_RANGE_ABORT;
4918}
4919
4920/* Is there a record covering a given range of keys? */
4921int
4922xfs_btree_has_record(
4923 struct xfs_btree_cur *cur,
4924 union xfs_btree_irec *low,
4925 union xfs_btree_irec *high,
4926 bool *exists)
4927{
4928 int error;
4929
4930 error = xfs_btree_query_range(cur, low, high,
4931 &xfs_btree_has_record_helper, NULL);
4932 if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
4933 *exists = true;
4934 return 0;
4935 }
4936 *exists = false;
4937 return error;
4938}
Darrick J. Wong08daa3c2018-05-09 10:02:03 -07004939
4940/* Are there more records in this btree? */
4941bool
4942xfs_btree_has_more_records(
4943 struct xfs_btree_cur *cur)
4944{
4945 struct xfs_btree_block *block;
4946 struct xfs_buf *bp;
4947
4948 block = xfs_btree_get_block(cur, 0, &bp);
4949
4950 /* There are still records in this block. */
4951 if (cur->bc_ptrs[0] < xfs_btree_get_numrecs(block))
4952 return true;
4953
4954 /* There are more record blocks. */
4955 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4956 return block->bb_u.l.bb_rightsib != cpu_to_be64(NULLFSBLOCK);
4957 else
4958 return block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK);
4959}