blob: 421efa0ef77880f6ed9dcf7b091411976dd531ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100026#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Christoph Hellwig38bb7422008-10-30 16:56:22 +110029#include "xfs_inode_item.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050030#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000033#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050034#include "xfs_cksum.h"
Dave Chinnercf11da92014-07-15 07:08:24 +100035#include "xfs_alloc.h"
Brian Fostera45086e2015-10-12 15:59:25 +110036#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Cursor allocation zone.
40 */
41kmem_zone_t *xfs_btree_cur_zone;
42
43/*
44 * Btree magic numbers.
45 */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050046static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
Darrick J. Wongb8704942016-08-03 11:30:32 +100047 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, 0, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070048 XFS_FIBT_MAGIC, 0 },
Darrick J. Wongb8704942016-08-03 11:30:32 +100049 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC, XFS_RMAP_CRC_MAGIC,
Darrick J. Wong46eeb522016-10-03 09:11:16 -070050 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC,
51 XFS_REFC_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080053
54__uint32_t
55xfs_btree_magic(
56 int crc,
57 xfs_btnum_t btnum)
58{
59 __uint32_t magic = xfs_magics[crc][btnum];
60
61 /* Ensure we asked for crc for crc-only magics. */
62 ASSERT(magic != 0);
63 return magic;
64}
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110066STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110067xfs_btree_check_lblock(
68 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110069 struct xfs_btree_block *block, /* btree long form block pointer */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110070 int level, /* level of the btree block */
71 struct xfs_buf *bp) /* buffer for block, if any */
72{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050073 int lblock_ok = 1; /* block passes checks */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110074 struct xfs_mount *mp; /* file system mount point */
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080075 xfs_btnum_t btnum = cur->bc_btnum;
76 int crc;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110077
78 mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080079 crc = xfs_sb_version_hascrc(&mp->m_sb);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050080
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080081 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050082 lblock_ok = lblock_ok &&
Eric Sandeence748ea2015-07-29 11:53:31 +100083 uuid_equal(&block->bb_u.l.bb_uuid,
84 &mp->m_sb.sb_meta_uuid) &&
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050085 block->bb_u.l.bb_blkno == cpu_to_be64(
86 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
87 }
88
89 lblock_ok = lblock_ok &&
Eric Sandeenaf7d20f2017-01-27 23:16:38 -080090 be32_to_cpu(block->bb_magic) == xfs_btree_magic(crc, btnum) &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110091 be16_to_cpu(block->bb_level) == level &&
92 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +110093 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110094 block->bb_u.l.bb_leftsib &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100095 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110096 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050097 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110098 block->bb_u.l.bb_rightsib &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100099 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100100 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500101 be64_to_cpu(block->bb_u.l.bb_rightsib)));
102
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100103 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
104 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
105 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
106 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000107 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500108 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000109 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100110 }
111 return 0;
112}
113
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100114STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115xfs_btree_check_sblock(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100116 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100117 struct xfs_btree_block *block, /* btree short form block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 int level, /* level of the btree block */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100119 struct xfs_buf *bp) /* buffer containing block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500121 struct xfs_mount *mp; /* file system mount point */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100122 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
123 struct xfs_agf *agf; /* ag. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 xfs_agblock_t agflen; /* native ag. freespace length */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500125 int sblock_ok = 1; /* block passes checks */
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800126 xfs_btnum_t btnum = cur->bc_btnum;
127 int crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500129 mp = cur->bc_mp;
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800130 crc = xfs_sb_version_hascrc(&mp->m_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 agbp = cur->bc_private.a.agbp;
132 agf = XFS_BUF_TO_AGF(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100133 agflen = be32_to_cpu(agf->agf_length);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500134
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800135 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500136 sblock_ok = sblock_ok &&
Eric Sandeence748ea2015-07-29 11:53:31 +1000137 uuid_equal(&block->bb_u.s.bb_uuid,
138 &mp->m_sb.sb_meta_uuid) &&
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500139 block->bb_u.s.bb_blkno == cpu_to_be64(
140 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
141 }
142
143 sblock_ok = sblock_ok &&
Eric Sandeenaf7d20f2017-01-27 23:16:38 -0800144 be32_to_cpu(block->bb_magic) == xfs_btree_magic(crc, btnum) &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100145 be16_to_cpu(block->bb_level) == level &&
146 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100147 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200148 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100149 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
150 block->bb_u.s.bb_leftsib &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200151 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100152 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
153 block->bb_u.s.bb_rightsib;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500154
155 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
157 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
158 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000159 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500160 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000161 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163 return 0;
164}
165
166/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100167 * Debug routine: check that block header is ok.
168 */
169int
170xfs_btree_check_block(
171 struct xfs_btree_cur *cur, /* btree cursor */
172 struct xfs_btree_block *block, /* generic btree block pointer */
173 int level, /* level of the btree block */
174 struct xfs_buf *bp) /* buffer containing block, if any */
175{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100176 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
177 return xfs_btree_check_lblock(cur, block, level, bp);
178 else
179 return xfs_btree_check_sblock(cur, block, level, bp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100180}
181
182/*
183 * Check that (long) pointer is ok.
184 */
185int /* error (0 or EFSCORRUPTED) */
186xfs_btree_check_lptr(
187 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000188 xfs_fsblock_t bno, /* btree block disk address */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100189 int level) /* btree block level */
190{
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100191 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100192 level > 0 &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000193 bno != NULLFSBLOCK &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100194 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
195 return 0;
196}
197
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100198#ifdef DEBUG
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100199/*
200 * Check that (short) pointer is ok.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100202STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203xfs_btree_check_sptr(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100204 struct xfs_btree_cur *cur, /* btree cursor */
205 xfs_agblock_t bno, /* btree block disk address */
206 int level) /* btree block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100208 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100210 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 level > 0 &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100212 bno != NULLAGBLOCK &&
213 bno != 0 &&
214 bno < agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216}
217
218/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100219 * Check that block ptr is ok.
220 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100221STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100222xfs_btree_check_ptr(
223 struct xfs_btree_cur *cur, /* btree cursor */
224 union xfs_btree_ptr *ptr, /* btree block disk address */
225 int index, /* offset from ptr to check */
226 int level) /* btree block level */
227{
228 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
229 return xfs_btree_check_lptr(cur,
230 be64_to_cpu((&ptr->l)[index]), level);
231 } else {
232 return xfs_btree_check_sptr(cur,
233 be32_to_cpu((&ptr->s)[index]), level);
234 }
235}
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100236#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100237
238/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500239 * Calculate CRC on the whole btree block and stuff it into the
240 * long-form btree header.
241 *
242 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100243 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500244 * it to disk.
245 */
246void
247xfs_btree_lblock_calc_crc(
248 struct xfs_buf *bp)
249{
250 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
251 struct xfs_buf_log_item *bip = bp->b_fspriv;
252
253 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
254 return;
255 if (bip)
256 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100257 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500258}
259
260bool
261xfs_btree_lblock_verify_crc(
262 struct xfs_buf *bp)
263{
Brian Fostera45086e2015-10-12 15:59:25 +1100264 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
265 struct xfs_mount *mp = bp->b_target->bt_mount;
266
267 if (xfs_sb_version_hascrc(&mp->m_sb)) {
268 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
269 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100270 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100271 }
Eric Sandeen51582172014-02-27 15:17:27 +1100272
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500273 return true;
274}
275
276/*
277 * Calculate CRC on the whole btree block and stuff it into the
278 * short-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_sblock_calc_crc(
286 struct xfs_buf *bp)
287{
288 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
289 struct xfs_buf_log_item *bip = bp->b_fspriv;
290
291 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
292 return;
293 if (bip)
294 block->bb_u.s.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_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500296}
297
298bool
299xfs_btree_sblock_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.s.bb_lsn)))
307 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100308 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_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
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100314static int
315xfs_btree_free_block(
316 struct xfs_btree_cur *cur,
317 struct xfs_buf *bp)
318{
319 int error;
320
321 error = cur->bc_ops->free_block(cur, bp);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100322 if (!error) {
323 xfs_trans_binval(cur->bc_tp, bp);
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100324 XFS_BTREE_STATS_INC(cur, free);
Christoph Hellwigedfd9dd2016-02-08 14:58:07 +1100325 }
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100326 return error;
327}
328
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500329/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * Delete the btree cursor.
331 */
332void
333xfs_btree_del_cursor(
334 xfs_btree_cur_t *cur, /* btree cursor */
335 int error) /* del because of error */
336{
337 int i; /* btree level */
338
339 /*
340 * Clear the buffer pointers, and release the buffers.
341 * If we're doing this in the face of an error, we
342 * need to make sure to inspect all of the entries
343 * in the bc_bufs array for buffers to be unlocked.
344 * This is because some of the btree code works from
345 * level n down to 0, and if we get an error along
346 * the way we won't have initialized all the entries
347 * down to 0.
348 */
349 for (i = 0; i < cur->bc_nlevels; i++) {
350 if (cur->bc_bufs[i])
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000351 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 else if (!error)
353 break;
354 }
355 /*
356 * Can't free a bmap cursor without having dealt with the
357 * allocated indirect blocks' accounting.
358 */
359 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
360 cur->bc_private.b.allocated == 0);
361 /*
362 * Free the cursor.
363 */
364 kmem_zone_free(xfs_btree_cur_zone, cur);
365}
366
367/*
368 * Duplicate the btree cursor.
369 * Allocate a new one, copy the record, re-get the buffers.
370 */
371int /* error */
372xfs_btree_dup_cursor(
373 xfs_btree_cur_t *cur, /* input cursor */
374 xfs_btree_cur_t **ncur) /* output cursor */
375{
376 xfs_buf_t *bp; /* btree block's buffer pointer */
377 int error; /* error return value */
378 int i; /* level number of btree block */
379 xfs_mount_t *mp; /* mount structure for filesystem */
380 xfs_btree_cur_t *new; /* new cursor value */
381 xfs_trans_t *tp; /* transaction pointer, can be NULL */
382
383 tp = cur->bc_tp;
384 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
387 * Allocate a new cursor like the old one.
388 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100389 new = cur->bc_ops->dup_cursor(cur);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * Copy the record currently in the cursor.
393 */
394 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /*
397 * For each level current, re-get the buffer and copy the ptr value.
398 */
399 for (i = 0; i < new->bc_nlevels; i++) {
400 new->bc_ptrs[i] = cur->bc_ptrs[i];
401 new->bc_ra[i] = cur->bc_ra[i];
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100402 bp = cur->bc_bufs[i];
403 if (bp) {
404 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
405 XFS_BUF_ADDR(bp), mp->m_bsize,
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100406 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100407 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100408 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 xfs_btree_del_cursor(new, error);
410 *ncur = NULL;
411 return error;
412 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500413 }
414 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *ncur = new;
417 return 0;
418}
419
420/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100421 * XFS btree block layout and addressing:
422 *
423 * There are two types of blocks in the btree: leaf and non-leaf blocks.
424 *
425 * The leaf record start with a header then followed by records containing
426 * the values. A non-leaf block also starts with the same header, and
427 * then first contains lookup keys followed by an equal number of pointers
428 * to the btree blocks at the previous level.
429 *
430 * +--------+-------+-------+-------+-------+-------+-------+
431 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
432 * +--------+-------+-------+-------+-------+-------+-------+
433 *
434 * +--------+-------+-------+-------+-------+-------+-------+
435 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
436 * +--------+-------+-------+-------+-------+-------+-------+
437 *
438 * The header is called struct xfs_btree_block for reasons better left unknown
439 * and comes in different versions for short (32bit) and long (64bit) block
440 * pointers. The record and key structures are defined by the btree instances
441 * and opaque to the btree core. The block pointers are simple disk endian
442 * integers, available in a short (32bit) and long (64bit) variant.
443 *
444 * The helpers below calculate the offset of a given record, key or pointer
445 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
446 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
447 * inside the btree block is done using indices starting at one, not zero!
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000448 *
449 * If XFS_BTREE_OVERLAPPING is set, then this btree supports keys containing
450 * overlapping intervals. In such a tree, records are still sorted lowest to
451 * highest and indexed by the smallest key value that refers to the record.
452 * However, nodes are different: each pointer has two associated keys -- one
453 * indexing the lowest key available in the block(s) below (the same behavior
454 * as the key in a regular btree) and another indexing the highest key
455 * available in the block(s) below. Because records are /not/ sorted by the
456 * highest key, all leaf block updates require us to compute the highest key
457 * that matches any record in the leaf and to recursively update the high keys
458 * in the nodes going further up in the tree, if necessary. Nodes look like
459 * this:
460 *
461 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
462 * Non-Leaf: | header | lo1 | hi1 | lo2 | hi2 | ... | ptr 1 | ptr 2 | ... |
463 * +--------+-----+-----+-----+-----+-----+-------+-------+-----+
464 *
465 * To perform an interval query on an overlapped tree, perform the usual
466 * depth-first search and use the low and high keys to decide if we can skip
467 * that particular node. If a leaf node is reached, return the records that
468 * intersect the interval. Note that an interval query may return numerous
469 * entries. For a non-overlapped tree, simply search for the record associated
470 * with the lowest key and iterate forward until a non-matching record is
471 * found. Section 14.3 ("Interval Trees") of _Introduction to Algorithms_ by
472 * Cormen, Leiserson, Rivest, and Stein (2nd or 3rd ed. only) discuss this in
473 * more detail.
474 *
475 * Why do we care about overlapping intervals? Let's say you have a bunch of
476 * reverse mapping records on a reflink filesystem:
477 *
478 * 1: +- file A startblock B offset C length D -----------+
479 * 2: +- file E startblock F offset G length H --------------+
480 * 3: +- file I startblock F offset J length K --+
481 * 4: +- file L... --+
482 *
483 * Now say we want to map block (B+D) into file A at offset (C+D). Ideally,
484 * we'd simply increment the length of record 1. But how do we find the record
485 * that ends at (B+D-1) (i.e. record 1)? A LE lookup of (B+D-1) would return
486 * record 3 because the keys are ordered first by startblock. An interval
487 * query would return records 1 and 2 because they both overlap (B+D-1), and
488 * from that we can pick out record 1 as the appropriate left neighbor.
489 *
490 * In the non-overlapped case you can do a LE lookup and decrement the cursor
491 * because a record's interval must end before the next record.
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100492 */
493
494/*
495 * Return size of the btree block header for this btree instance.
496 */
497static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
498{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500499 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
500 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
501 return XFS_BTREE_LBLOCK_CRC_LEN;
502 return XFS_BTREE_LBLOCK_LEN;
503 }
504 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
505 return XFS_BTREE_SBLOCK_CRC_LEN;
506 return XFS_BTREE_SBLOCK_LEN;
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100507}
508
509/*
510 * Return size of btree block pointers for this btree instance.
511 */
512static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
513{
514 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
515 sizeof(__be64) : sizeof(__be32);
516}
517
518/*
519 * Calculate offset of the n-th record in a btree block.
520 */
521STATIC size_t
522xfs_btree_rec_offset(
523 struct xfs_btree_cur *cur,
524 int n)
525{
526 return xfs_btree_block_len(cur) +
527 (n - 1) * cur->bc_ops->rec_len;
528}
529
530/*
531 * Calculate offset of the n-th key in a btree block.
532 */
533STATIC size_t
534xfs_btree_key_offset(
535 struct xfs_btree_cur *cur,
536 int n)
537{
538 return xfs_btree_block_len(cur) +
539 (n - 1) * cur->bc_ops->key_len;
540}
541
542/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000543 * Calculate offset of the n-th high key in a btree block.
544 */
545STATIC size_t
546xfs_btree_high_key_offset(
547 struct xfs_btree_cur *cur,
548 int n)
549{
550 return xfs_btree_block_len(cur) +
551 (n - 1) * cur->bc_ops->key_len + (cur->bc_ops->key_len / 2);
552}
553
554/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100555 * Calculate offset of the n-th block pointer in a btree block.
556 */
557STATIC size_t
558xfs_btree_ptr_offset(
559 struct xfs_btree_cur *cur,
560 int n,
561 int level)
562{
563 return xfs_btree_block_len(cur) +
564 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
565 (n - 1) * xfs_btree_ptr_len(cur);
566}
567
568/*
569 * Return a pointer to the n-th record in the btree block.
570 */
571STATIC union xfs_btree_rec *
572xfs_btree_rec_addr(
573 struct xfs_btree_cur *cur,
574 int n,
575 struct xfs_btree_block *block)
576{
577 return (union xfs_btree_rec *)
578 ((char *)block + xfs_btree_rec_offset(cur, n));
579}
580
581/*
582 * Return a pointer to the n-th key in the btree block.
583 */
584STATIC union xfs_btree_key *
585xfs_btree_key_addr(
586 struct xfs_btree_cur *cur,
587 int n,
588 struct xfs_btree_block *block)
589{
590 return (union xfs_btree_key *)
591 ((char *)block + xfs_btree_key_offset(cur, n));
592}
593
594/*
Darrick J. Wong2c813ad2016-08-03 11:08:36 +1000595 * Return a pointer to the n-th high key in the btree block.
596 */
597STATIC union xfs_btree_key *
598xfs_btree_high_key_addr(
599 struct xfs_btree_cur *cur,
600 int n,
601 struct xfs_btree_block *block)
602{
603 return (union xfs_btree_key *)
604 ((char *)block + xfs_btree_high_key_offset(cur, n));
605}
606
607/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100608 * Return a pointer to the n-th block pointer in the btree block.
609 */
610STATIC union xfs_btree_ptr *
611xfs_btree_ptr_addr(
612 struct xfs_btree_cur *cur,
613 int n,
614 struct xfs_btree_block *block)
615{
616 int level = xfs_btree_get_level(block);
617
618 ASSERT(block->bb_level != 0);
619
620 return (union xfs_btree_ptr *)
621 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
622}
623
624/*
Zhi Yong Wu1cb93862013-08-07 10:11:05 +0000625 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100626 *
627 * For now this btree implementation assumes the btree root is always
628 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100630STATIC struct xfs_btree_block *
631xfs_btree_get_iroot(
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000632 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000634 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Kaho Ngfbfb24b2016-07-20 10:37:50 +1000636 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
637 return (struct xfs_btree_block *)ifp->if_broot;
Christoph Hellwig8186e512008-10-30 16:54:22 +1100638}
639
640/*
641 * Retrieve the block pointer from the cursor at the given level.
642 * This may be an inode btree root or from a buffer.
643 */
644STATIC struct xfs_btree_block * /* generic btree block pointer */
645xfs_btree_get_block(
646 struct xfs_btree_cur *cur, /* btree cursor */
647 int level, /* level in btree */
648 struct xfs_buf **bpp) /* buffer containing the block */
649{
650 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
651 (level == cur->bc_nlevels - 1)) {
652 *bpp = NULL;
653 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100655
656 *bpp = cur->bc_bufs[level];
657 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
661 * Get a buffer for the block, return it with no data read.
662 * Long-form addressing.
663 */
664xfs_buf_t * /* buffer for fsbno */
665xfs_btree_get_bufl(
666 xfs_mount_t *mp, /* file system mount point */
667 xfs_trans_t *tp, /* transaction pointer */
668 xfs_fsblock_t fsbno, /* file system block number */
669 uint lock) /* lock flags for get_buf */
670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 xfs_daddr_t d; /* real disk block address */
672
673 ASSERT(fsbno != NULLFSBLOCK);
674 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000675 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678/*
679 * Get a buffer for the block, return it with no data read.
680 * Short-form addressing.
681 */
682xfs_buf_t * /* buffer for agno/agbno */
683xfs_btree_get_bufs(
684 xfs_mount_t *mp, /* file system mount point */
685 xfs_trans_t *tp, /* transaction pointer */
686 xfs_agnumber_t agno, /* allocation group number */
687 xfs_agblock_t agbno, /* allocation group block number */
688 uint lock) /* lock flags for get_buf */
689{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 xfs_daddr_t d; /* real disk block address */
691
692 ASSERT(agno != NULLAGNUMBER);
693 ASSERT(agbno != NULLAGBLOCK);
694 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000695 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * Check for the cursor referring to the last block at the given level.
700 */
701int /* 1=is last block, 0=not last block */
702xfs_btree_islastblock(
703 xfs_btree_cur_t *cur, /* btree cursor */
704 int level) /* level to check */
705{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100706 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 xfs_buf_t *bp; /* buffer containing block */
708
709 block = xfs_btree_get_block(cur, level, &bp);
710 xfs_btree_check_block(cur, block, level, bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100711 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000712 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200714 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000718 * Change the cursor to point to the first record at the given level.
719 * Other levels are unaffected.
720 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100721STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000722xfs_btree_firstrec(
723 xfs_btree_cur_t *cur, /* btree cursor */
724 int level) /* level to change */
725{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100726 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000727 xfs_buf_t *bp; /* buffer containing block */
728
729 /*
730 * Get the block pointer for this level.
731 */
732 block = xfs_btree_get_block(cur, level, &bp);
733 xfs_btree_check_block(cur, block, level, bp);
734 /*
735 * It's empty, there is no such record.
736 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100737 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000738 return 0;
739 /*
740 * Set the ptr value to 1, that's the first record/key.
741 */
742 cur->bc_ptrs[level] = 1;
743 return 1;
744}
745
746/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * Change the cursor to point to the last record in the current block
748 * at the given level. Other levels are unaffected.
749 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100750STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751xfs_btree_lastrec(
752 xfs_btree_cur_t *cur, /* btree cursor */
753 int level) /* level to change */
754{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100755 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfs_buf_t *bp; /* buffer containing block */
757
758 /*
759 * Get the block pointer for this level.
760 */
761 block = xfs_btree_get_block(cur, level, &bp);
762 xfs_btree_check_block(cur, block, level, bp);
763 /*
764 * It's empty, there is no such record.
765 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100766 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 0;
768 /*
769 * Set the ptr value to numrecs, that's the last record/key.
770 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100771 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 1;
773}
774
775/*
776 * Compute first and last byte offsets for the fields given.
777 * Interprets the offsets table, which contains struct field offsets.
778 */
779void
780xfs_btree_offsets(
781 __int64_t fields, /* bitmask of fields */
782 const short *offsets, /* table of field offsets */
783 int nbits, /* number of bits to inspect */
784 int *first, /* output: first byte offset */
785 int *last) /* output: last byte offset */
786{
787 int i; /* current bit number */
788 __int64_t imask; /* mask for current bit number */
789
790 ASSERT(fields != 0);
791 /*
792 * Find the lowest bit, so the first byte offset.
793 */
794 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
795 if (imask & fields) {
796 *first = offsets[i];
797 break;
798 }
799 }
800 /*
801 * Find the highest bit, so the last byte offset.
802 */
803 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
804 if (imask & fields) {
805 *last = offsets[i + 1] - 1;
806 break;
807 }
808 }
809}
810
811/*
812 * Get a buffer for the block, return it read in.
813 * Long-form addressing.
814 */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100815int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100817 struct xfs_mount *mp, /* file system mount point */
818 struct xfs_trans *tp, /* transaction pointer */
819 xfs_fsblock_t fsbno, /* file system block number */
820 uint lock, /* lock flags for read_buf */
821 struct xfs_buf **bpp, /* buffer for fsbno */
822 int refval, /* ref count value for buffer */
Dave Chinner1813dd62012-11-14 17:54:40 +1100823 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100825 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100827 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 ASSERT(fsbno != NULLFSBLOCK);
830 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100831 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Dave Chinner1813dd62012-11-14 17:54:40 +1100832 mp->m_bsize, lock, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100833 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return error;
Dave Chinner821eb212010-12-02 16:31:13 +1100835 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000836 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 *bpp = bp;
838 return 0;
839}
840
841/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * Read-ahead the block, don't wait for it, don't return a buffer.
843 * Long-form addressing.
844 */
845/* ARGSUSED */
846void
847xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100848 struct xfs_mount *mp, /* file system mount point */
849 xfs_fsblock_t fsbno, /* file system block number */
850 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100851 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 xfs_daddr_t d;
854
855 ASSERT(fsbno != NULLFSBLOCK);
856 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100857 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
860/*
861 * Read-ahead the block, don't wait for it, don't return a buffer.
862 * Short-form addressing.
863 */
864/* ARGSUSED */
865void
866xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100867 struct xfs_mount *mp, /* file system mount point */
868 xfs_agnumber_t agno, /* allocation group number */
869 xfs_agblock_t agbno, /* allocation group block number */
870 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100871 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 xfs_daddr_t d;
874
875 ASSERT(agno != NULLAGNUMBER);
876 ASSERT(agbno != NULLAGBLOCK);
877 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100878 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100881STATIC int
882xfs_btree_readahead_lblock(
883 struct xfs_btree_cur *cur,
884 int lr,
885 struct xfs_btree_block *block)
886{
887 int rval = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000888 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
889 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100890
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000891 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100892 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100893 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100894 rval++;
895 }
896
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000897 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100898 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100899 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100900 rval++;
901 }
902
903 return rval;
904}
905
906STATIC int
907xfs_btree_readahead_sblock(
908 struct xfs_btree_cur *cur,
909 int lr,
910 struct xfs_btree_block *block)
911{
912 int rval = 0;
913 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
914 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
915
916
917 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
918 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100919 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100920 rval++;
921 }
922
923 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
924 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100925 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100926 rval++;
927 }
928
929 return rval;
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/*
933 * Read-ahead btree blocks, at the given level.
934 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
935 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100936STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100937xfs_btree_readahead(
938 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int lev, /* level in btree */
940 int lr) /* left/right bits */
941{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100942 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100944 /*
945 * No readahead needed if we are at the root level and the
946 * btree root is stored in the inode.
947 */
948 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
949 (lev == cur->bc_nlevels - 1))
950 return 0;
951
952 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
953 return 0;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100956 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
957
958 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
959 return xfs_btree_readahead_lblock(cur, lr, block);
960 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Dave Chinner21b5c972013-08-30 10:23:44 +1000963STATIC xfs_daddr_t
964xfs_btree_ptr_to_daddr(
965 struct xfs_btree_cur *cur,
966 union xfs_btree_ptr *ptr)
967{
968 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000969 ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK));
Dave Chinner21b5c972013-08-30 10:23:44 +1000970
971 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
972 } else {
973 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
974 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
975
976 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
977 be32_to_cpu(ptr->s));
978 }
979}
980
981/*
982 * Readahead @count btree blocks at the given @ptr location.
983 *
984 * We don't need to care about long or short form btrees here as we have a
985 * method of converting the ptr directly to a daddr available to us.
986 */
987STATIC void
988xfs_btree_readahead_ptr(
989 struct xfs_btree_cur *cur,
990 union xfs_btree_ptr *ptr,
991 xfs_extlen_t count)
992{
993 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
994 xfs_btree_ptr_to_daddr(cur, ptr),
995 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * Set the buffer for level "lev" in the cursor to bp, releasing
1000 * any previous buffer.
1001 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001002STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003xfs_btree_setbuf(
1004 xfs_btree_cur_t *cur, /* btree cursor */
1005 int lev, /* level in btree */
1006 xfs_buf_t *bp) /* new buffer to set */
1007{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001008 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001010 if (cur->bc_bufs[lev])
1011 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 cur->bc_bufs[lev] = bp;
1013 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +11001016 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001017 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001019 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1021 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001022 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001024 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
1026 }
1027}
Christoph Hellwig637aa502008-10-30 16:55:45 +11001028
1029STATIC int
1030xfs_btree_ptr_is_null(
1031 struct xfs_btree_cur *cur,
1032 union xfs_btree_ptr *ptr)
1033{
1034 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001035 return ptr->l == cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001036 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001037 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001038}
1039
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001040STATIC void
1041xfs_btree_set_ptr_null(
1042 struct xfs_btree_cur *cur,
1043 union xfs_btree_ptr *ptr)
1044{
1045 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001046 ptr->l = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11001047 else
1048 ptr->s = cpu_to_be32(NULLAGBLOCK);
1049}
1050
Christoph Hellwig637aa502008-10-30 16:55:45 +11001051/*
1052 * Get/set/init sibling pointers
1053 */
1054STATIC void
1055xfs_btree_get_sibling(
1056 struct xfs_btree_cur *cur,
1057 struct xfs_btree_block *block,
1058 union xfs_btree_ptr *ptr,
1059 int lr)
1060{
1061 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1062
1063 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1064 if (lr == XFS_BB_RIGHTSIB)
1065 ptr->l = block->bb_u.l.bb_rightsib;
1066 else
1067 ptr->l = block->bb_u.l.bb_leftsib;
1068 } else {
1069 if (lr == XFS_BB_RIGHTSIB)
1070 ptr->s = block->bb_u.s.bb_rightsib;
1071 else
1072 ptr->s = block->bb_u.s.bb_leftsib;
1073 }
1074}
1075
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001076STATIC void
1077xfs_btree_set_sibling(
1078 struct xfs_btree_cur *cur,
1079 struct xfs_btree_block *block,
1080 union xfs_btree_ptr *ptr,
1081 int lr)
1082{
1083 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
1084
1085 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
1086 if (lr == XFS_BB_RIGHTSIB)
1087 block->bb_u.l.bb_rightsib = ptr->l;
1088 else
1089 block->bb_u.l.bb_leftsib = ptr->l;
1090 } else {
1091 if (lr == XFS_BB_RIGHTSIB)
1092 block->bb_u.s.bb_rightsib = ptr->s;
1093 else
1094 block->bb_u.s.bb_leftsib = ptr->s;
1095 }
1096}
1097
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001098void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001099xfs_btree_init_block_int(
1100 struct xfs_mount *mp,
1101 struct xfs_btree_block *buf,
1102 xfs_daddr_t blkno,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001103 xfs_btnum_t btnum,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001104 __u16 level,
1105 __u16 numrecs,
1106 __u64 owner,
1107 unsigned int flags)
1108{
Eric Sandeenf88ae462017-01-27 23:16:37 -08001109 int crc = xfs_sb_version_hascrc(&mp->m_sb);
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001110 __u32 magic = xfs_btree_magic(crc, btnum);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001111
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001112 buf->bb_magic = cpu_to_be32(magic);
1113 buf->bb_level = cpu_to_be16(level);
1114 buf->bb_numrecs = cpu_to_be16(numrecs);
1115
1116 if (flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001117 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1118 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001119 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001120 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1121 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001122 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001123 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001124 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001125 }
1126 } else {
1127 /* owner is a 32 bit value on short blocks */
1128 __u32 __owner = (__u32)owner;
1129
1130 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1131 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
Eric Sandeenf88ae462017-01-27 23:16:37 -08001132 if (crc) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001133 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1134 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001135 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001136 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001137 }
1138 }
1139}
1140
1141void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001142xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001143 struct xfs_mount *mp,
1144 struct xfs_buf *bp,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001145 xfs_btnum_t btnum,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001146 __u16 level,
1147 __u16 numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001148 __u64 owner,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001149 unsigned int flags)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001150{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001151 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001152 btnum, level, numrecs, owner, flags);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001153}
1154
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001155STATIC void
1156xfs_btree_init_block_cur(
1157 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001158 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001159 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001160 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001161{
Eric Sandeenaf7d20f2017-01-27 23:16:38 -08001162 __u64 owner;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001163
1164 /*
1165 * we can pull the owner from the cursor right now as the different
1166 * owners align directly with the pointer size of the btree. This may
1167 * change in future, but is safe for current users of the generic btree
1168 * code.
1169 */
1170 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1171 owner = cur->bc_private.b.ip->i_ino;
1172 else
1173 owner = cur->bc_private.a.agno;
1174
1175 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
Eric Sandeenb6f41e42017-01-27 23:16:39 -08001176 cur->bc_btnum, level, numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001177 owner, cur->bc_flags);
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001178}
1179
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001180/*
1181 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001182 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001183 * will be further refined in the update_lastrec method.
1184 */
1185STATIC int
1186xfs_btree_is_lastrec(
1187 struct xfs_btree_cur *cur,
1188 struct xfs_btree_block *block,
1189 int level)
1190{
1191 union xfs_btree_ptr ptr;
1192
1193 if (level > 0)
1194 return 0;
1195 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1196 return 0;
1197
1198 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1199 if (!xfs_btree_ptr_is_null(cur, &ptr))
1200 return 0;
1201 return 1;
1202}
1203
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001204STATIC void
1205xfs_btree_buf_to_ptr(
1206 struct xfs_btree_cur *cur,
1207 struct xfs_buf *bp,
1208 union xfs_btree_ptr *ptr)
1209{
1210 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1211 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1212 XFS_BUF_ADDR(bp)));
1213 else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001214 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001215 XFS_BUF_ADDR(bp)));
1216 }
1217}
1218
Christoph Hellwig637aa502008-10-30 16:55:45 +11001219STATIC void
1220xfs_btree_set_refs(
1221 struct xfs_btree_cur *cur,
1222 struct xfs_buf *bp)
1223{
1224 switch (cur->bc_btnum) {
1225 case XFS_BTNUM_BNO:
1226 case XFS_BTNUM_CNT:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001227 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001228 break;
1229 case XFS_BTNUM_INO:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001230 case XFS_BTNUM_FINO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001231 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001232 break;
1233 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001234 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001235 break;
Darrick J. Wong035e00a2016-08-03 11:36:07 +10001236 case XFS_BTNUM_RMAP:
1237 xfs_buf_set_ref(bp, XFS_RMAP_BTREE_REF);
1238 break;
Darrick J. Wong1946b912016-10-03 09:11:18 -07001239 case XFS_BTNUM_REFC:
1240 xfs_buf_set_ref(bp, XFS_REFC_BTREE_REF);
1241 break;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001242 default:
1243 ASSERT(0);
1244 }
1245}
1246
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001247STATIC int
1248xfs_btree_get_buf_block(
1249 struct xfs_btree_cur *cur,
1250 union xfs_btree_ptr *ptr,
1251 int flags,
1252 struct xfs_btree_block **block,
1253 struct xfs_buf **bpp)
1254{
1255 struct xfs_mount *mp = cur->bc_mp;
1256 xfs_daddr_t d;
1257
1258 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001259 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001260
1261 d = xfs_btree_ptr_to_daddr(cur, ptr);
1262 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1263 mp->m_bsize, flags);
1264
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001265 if (!*bpp)
Dave Chinner24513372014-06-25 14:58:08 +10001266 return -ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001267
Dave Chinner1813dd62012-11-14 17:54:40 +11001268 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001269 *block = XFS_BUF_TO_BLOCK(*bpp);
1270 return 0;
1271}
1272
Christoph Hellwig637aa502008-10-30 16:55:45 +11001273/*
1274 * Read in the buffer at the given ptr and return the buffer and
1275 * the block pointer within the buffer.
1276 */
1277STATIC int
1278xfs_btree_read_buf_block(
1279 struct xfs_btree_cur *cur,
1280 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001281 int flags,
1282 struct xfs_btree_block **block,
1283 struct xfs_buf **bpp)
1284{
1285 struct xfs_mount *mp = cur->bc_mp;
1286 xfs_daddr_t d;
1287 int error;
1288
1289 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001290 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001291
1292 d = xfs_btree_ptr_to_daddr(cur, ptr);
1293 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001294 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001295 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001296 if (error)
1297 return error;
1298
Christoph Hellwig637aa502008-10-30 16:55:45 +11001299 xfs_btree_set_refs(cur, *bpp);
1300 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001301 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001302}
1303
1304/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001305 * Copy keys from one btree block to another.
1306 */
1307STATIC void
1308xfs_btree_copy_keys(
1309 struct xfs_btree_cur *cur,
1310 union xfs_btree_key *dst_key,
1311 union xfs_btree_key *src_key,
1312 int numkeys)
1313{
1314 ASSERT(numkeys >= 0);
1315 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1316}
1317
1318/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001319 * Copy records from one btree block to another.
1320 */
1321STATIC void
1322xfs_btree_copy_recs(
1323 struct xfs_btree_cur *cur,
1324 union xfs_btree_rec *dst_rec,
1325 union xfs_btree_rec *src_rec,
1326 int numrecs)
1327{
1328 ASSERT(numrecs >= 0);
1329 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1330}
1331
1332/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001333 * Copy block pointers from one btree block to another.
1334 */
1335STATIC void
1336xfs_btree_copy_ptrs(
1337 struct xfs_btree_cur *cur,
1338 union xfs_btree_ptr *dst_ptr,
1339 union xfs_btree_ptr *src_ptr,
1340 int numptrs)
1341{
1342 ASSERT(numptrs >= 0);
1343 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1344}
1345
1346/*
1347 * Shift keys one index left/right inside a single btree block.
1348 */
1349STATIC void
1350xfs_btree_shift_keys(
1351 struct xfs_btree_cur *cur,
1352 union xfs_btree_key *key,
1353 int dir,
1354 int numkeys)
1355{
1356 char *dst_key;
1357
1358 ASSERT(numkeys >= 0);
1359 ASSERT(dir == 1 || dir == -1);
1360
1361 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1362 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1363}
1364
1365/*
1366 * Shift records one index left/right inside a single btree block.
1367 */
1368STATIC void
1369xfs_btree_shift_recs(
1370 struct xfs_btree_cur *cur,
1371 union xfs_btree_rec *rec,
1372 int dir,
1373 int numrecs)
1374{
1375 char *dst_rec;
1376
1377 ASSERT(numrecs >= 0);
1378 ASSERT(dir == 1 || dir == -1);
1379
1380 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1381 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1382}
1383
1384/*
1385 * Shift block pointers one index left/right inside a single btree block.
1386 */
1387STATIC void
1388xfs_btree_shift_ptrs(
1389 struct xfs_btree_cur *cur,
1390 union xfs_btree_ptr *ptr,
1391 int dir,
1392 int numptrs)
1393{
1394 char *dst_ptr;
1395
1396 ASSERT(numptrs >= 0);
1397 ASSERT(dir == 1 || dir == -1);
1398
1399 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1400 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1401}
1402
1403/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001404 * Log key values from the btree block.
1405 */
1406STATIC void
1407xfs_btree_log_keys(
1408 struct xfs_btree_cur *cur,
1409 struct xfs_buf *bp,
1410 int first,
1411 int last)
1412{
1413 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1414 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1415
1416 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001417 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001418 xfs_trans_log_buf(cur->bc_tp, bp,
1419 xfs_btree_key_offset(cur, first),
1420 xfs_btree_key_offset(cur, last + 1) - 1);
1421 } else {
1422 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1423 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1424 }
1425
1426 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1427}
1428
1429/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001430 * Log record values from the btree block.
1431 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001432void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001433xfs_btree_log_recs(
1434 struct xfs_btree_cur *cur,
1435 struct xfs_buf *bp,
1436 int first,
1437 int last)
1438{
1439 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1440 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1441
Dave Chinner61fe1352013-04-03 16:11:30 +11001442 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001443 xfs_trans_log_buf(cur->bc_tp, bp,
1444 xfs_btree_rec_offset(cur, first),
1445 xfs_btree_rec_offset(cur, last + 1) - 1);
1446
1447 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1448}
1449
1450/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001451 * Log block pointer fields from a btree block (nonleaf).
1452 */
1453STATIC void
1454xfs_btree_log_ptrs(
1455 struct xfs_btree_cur *cur, /* btree cursor */
1456 struct xfs_buf *bp, /* buffer containing btree block */
1457 int first, /* index of first pointer to log */
1458 int last) /* index of last pointer to log */
1459{
1460 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1461 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1462
1463 if (bp) {
1464 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1465 int level = xfs_btree_get_level(block);
1466
Dave Chinner61fe1352013-04-03 16:11:30 +11001467 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001468 xfs_trans_log_buf(cur->bc_tp, bp,
1469 xfs_btree_ptr_offset(cur, first, level),
1470 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1471 } else {
1472 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1473 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1474 }
1475
1476 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1477}
1478
1479/*
1480 * Log fields from a btree block header.
1481 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001482void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001483xfs_btree_log_block(
1484 struct xfs_btree_cur *cur, /* btree cursor */
1485 struct xfs_buf *bp, /* buffer containing btree block */
1486 int fields) /* mask of fields: XFS_BB_... */
1487{
1488 int first; /* first byte offset logged */
1489 int last; /* last byte offset logged */
1490 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001491 offsetof(struct xfs_btree_block, bb_magic),
1492 offsetof(struct xfs_btree_block, bb_level),
1493 offsetof(struct xfs_btree_block, bb_numrecs),
1494 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1495 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001496 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1497 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1498 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1499 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1500 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1501 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001502 };
1503 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001504 offsetof(struct xfs_btree_block, bb_magic),
1505 offsetof(struct xfs_btree_block, bb_level),
1506 offsetof(struct xfs_btree_block, bb_numrecs),
1507 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1508 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001509 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1510 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1511 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1512 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1513 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1514 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1515 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001516 };
1517
1518 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1519 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1520
1521 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001522 int nbits;
1523
1524 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1525 /*
1526 * We don't log the CRC when updating a btree
1527 * block but instead recreate it during log
1528 * recovery. As the log buffers have checksums
1529 * of their own this is safe and avoids logging a crc
1530 * update in a lot of places.
1531 */
1532 if (fields == XFS_BB_ALL_BITS)
1533 fields = XFS_BB_ALL_BITS_CRC;
1534 nbits = XFS_BB_NUM_BITS_CRC;
1535 } else {
1536 nbits = XFS_BB_NUM_BITS;
1537 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001538 xfs_btree_offsets(fields,
1539 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1540 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001541 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001542 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001543 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1544 } else {
1545 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1546 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1547 }
1548
1549 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1550}
1551
1552/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001553 * Increment cursor by one record at the level.
1554 * For nonzero levels the leaf-ward information is untouched.
1555 */
1556int /* error */
1557xfs_btree_increment(
1558 struct xfs_btree_cur *cur,
1559 int level,
1560 int *stat) /* success/failure */
1561{
1562 struct xfs_btree_block *block;
1563 union xfs_btree_ptr ptr;
1564 struct xfs_buf *bp;
1565 int error; /* error return value */
1566 int lev;
1567
1568 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1569 XFS_BTREE_TRACE_ARGI(cur, level);
1570
1571 ASSERT(level < cur->bc_nlevels);
1572
1573 /* Read-ahead to the right at this level. */
1574 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1575
1576 /* Get a pointer to the btree block. */
1577 block = xfs_btree_get_block(cur, level, &bp);
1578
1579#ifdef DEBUG
1580 error = xfs_btree_check_block(cur, block, level, bp);
1581 if (error)
1582 goto error0;
1583#endif
1584
1585 /* We're done if we remain in the block after the increment. */
1586 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1587 goto out1;
1588
1589 /* Fail if we just went off the right edge of the tree. */
1590 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1591 if (xfs_btree_ptr_is_null(cur, &ptr))
1592 goto out0;
1593
1594 XFS_BTREE_STATS_INC(cur, increment);
1595
1596 /*
1597 * March up the tree incrementing pointers.
1598 * Stop when we don't go off the right edge of a block.
1599 */
1600 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1601 block = xfs_btree_get_block(cur, lev, &bp);
1602
1603#ifdef DEBUG
1604 error = xfs_btree_check_block(cur, block, lev, bp);
1605 if (error)
1606 goto error0;
1607#endif
1608
1609 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1610 break;
1611
1612 /* Read-ahead the right block for the next loop. */
1613 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1614 }
1615
1616 /*
1617 * If we went off the root then we are either seriously
1618 * confused or have the tree root in an inode.
1619 */
1620 if (lev == cur->bc_nlevels) {
1621 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1622 goto out0;
1623 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001624 error = -EFSCORRUPTED;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001625 goto error0;
1626 }
1627 ASSERT(lev < cur->bc_nlevels);
1628
1629 /*
1630 * Now walk back down the tree, fixing up the cursor's buffer
1631 * pointers and key numbers.
1632 */
1633 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1634 union xfs_btree_ptr *ptrp;
1635
1636 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001637 --lev;
1638 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001639 if (error)
1640 goto error0;
1641
1642 xfs_btree_setbuf(cur, lev, bp);
1643 cur->bc_ptrs[lev] = 1;
1644 }
1645out1:
1646 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1647 *stat = 1;
1648 return 0;
1649
1650out0:
1651 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1652 *stat = 0;
1653 return 0;
1654
1655error0:
1656 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1657 return error;
1658}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001659
1660/*
1661 * Decrement cursor by one record at the level.
1662 * For nonzero levels the leaf-ward information is untouched.
1663 */
1664int /* error */
1665xfs_btree_decrement(
1666 struct xfs_btree_cur *cur,
1667 int level,
1668 int *stat) /* success/failure */
1669{
1670 struct xfs_btree_block *block;
1671 xfs_buf_t *bp;
1672 int error; /* error return value */
1673 int lev;
1674 union xfs_btree_ptr ptr;
1675
1676 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1677 XFS_BTREE_TRACE_ARGI(cur, level);
1678
1679 ASSERT(level < cur->bc_nlevels);
1680
1681 /* Read-ahead to the left at this level. */
1682 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1683
1684 /* We're done if we remain in the block after the decrement. */
1685 if (--cur->bc_ptrs[level] > 0)
1686 goto out1;
1687
1688 /* Get a pointer to the btree block. */
1689 block = xfs_btree_get_block(cur, level, &bp);
1690
1691#ifdef DEBUG
1692 error = xfs_btree_check_block(cur, block, level, bp);
1693 if (error)
1694 goto error0;
1695#endif
1696
1697 /* Fail if we just went off the left edge of the tree. */
1698 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1699 if (xfs_btree_ptr_is_null(cur, &ptr))
1700 goto out0;
1701
1702 XFS_BTREE_STATS_INC(cur, decrement);
1703
1704 /*
1705 * March up the tree decrementing pointers.
1706 * Stop when we don't go off the left edge of a block.
1707 */
1708 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1709 if (--cur->bc_ptrs[lev] > 0)
1710 break;
1711 /* Read-ahead the left block for the next loop. */
1712 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1713 }
1714
1715 /*
1716 * If we went off the root then we are seriously confused.
1717 * or the root of the tree is in an inode.
1718 */
1719 if (lev == cur->bc_nlevels) {
1720 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1721 goto out0;
1722 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001723 error = -EFSCORRUPTED;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001724 goto error0;
1725 }
1726 ASSERT(lev < cur->bc_nlevels);
1727
1728 /*
1729 * Now walk back down the tree, fixing up the cursor's buffer
1730 * pointers and key numbers.
1731 */
1732 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1733 union xfs_btree_ptr *ptrp;
1734
1735 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001736 --lev;
1737 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001738 if (error)
1739 goto error0;
1740 xfs_btree_setbuf(cur, lev, bp);
1741 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1742 }
1743out1:
1744 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1745 *stat = 1;
1746 return 0;
1747
1748out0:
1749 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1750 *stat = 0;
1751 return 0;
1752
1753error0:
1754 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1755 return error;
1756}
1757
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001758STATIC int
1759xfs_btree_lookup_get_block(
1760 struct xfs_btree_cur *cur, /* btree cursor */
1761 int level, /* level in the btree */
1762 union xfs_btree_ptr *pp, /* ptr to btree block */
1763 struct xfs_btree_block **blkp) /* return btree block */
1764{
1765 struct xfs_buf *bp; /* buffer pointer for btree block */
1766 int error = 0;
1767
1768 /* special case the root block if in an inode */
1769 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1770 (level == cur->bc_nlevels - 1)) {
1771 *blkp = xfs_btree_get_iroot(cur);
1772 return 0;
1773 }
1774
1775 /*
1776 * If the old buffer at this level for the disk address we are
1777 * looking for re-use it.
1778 *
1779 * Otherwise throw it away and get a new one.
1780 */
1781 bp = cur->bc_bufs[level];
1782 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1783 *blkp = XFS_BUF_TO_BLOCK(bp);
1784 return 0;
1785 }
1786
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001787 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001788 if (error)
1789 return error;
1790
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001791 /* Check the inode owner since the verifiers don't. */
1792 if (xfs_sb_version_hascrc(&cur->bc_mp->m_sb) &&
1793 (cur->bc_flags & XFS_BTREE_LONG_PTRS) &&
1794 be64_to_cpu((*blkp)->bb_u.l.bb_owner) !=
1795 cur->bc_private.b.ip->i_ino)
1796 goto out_bad;
1797
1798 /* Did we get the level we were looking for? */
1799 if (be16_to_cpu((*blkp)->bb_level) != level)
1800 goto out_bad;
1801
1802 /* Check that internal nodes have at least one record. */
1803 if (level != 0 && be16_to_cpu((*blkp)->bb_numrecs) == 0)
1804 goto out_bad;
1805
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001806 xfs_btree_setbuf(cur, level, bp);
1807 return 0;
Darrick J. Wongbb3be7e2016-12-05 12:33:54 +11001808
1809out_bad:
1810 *blkp = NULL;
1811 xfs_trans_brelse(cur->bc_tp, bp);
1812 return -EFSCORRUPTED;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001813}
1814
1815/*
1816 * Get current search key. For level 0 we don't actually have a key
1817 * structure so we make one up from the record. For all other levels
1818 * we just return the right key.
1819 */
1820STATIC union xfs_btree_key *
1821xfs_lookup_get_search_key(
1822 struct xfs_btree_cur *cur,
1823 int level,
1824 int keyno,
1825 struct xfs_btree_block *block,
1826 union xfs_btree_key *kp)
1827{
1828 if (level == 0) {
1829 cur->bc_ops->init_key_from_rec(kp,
1830 xfs_btree_rec_addr(cur, keyno, block));
1831 return kp;
1832 }
1833
1834 return xfs_btree_key_addr(cur, keyno, block);
1835}
1836
1837/*
1838 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001839 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001840 */
1841int /* error */
1842xfs_btree_lookup(
1843 struct xfs_btree_cur *cur, /* btree cursor */
1844 xfs_lookup_t dir, /* <=, ==, or >= */
1845 int *stat) /* success/failure */
1846{
1847 struct xfs_btree_block *block; /* current btree block */
1848 __int64_t diff; /* difference for the current key */
1849 int error; /* error return value */
1850 int keyno; /* current key number */
1851 int level; /* level in the btree */
1852 union xfs_btree_ptr *pp; /* ptr to btree block */
1853 union xfs_btree_ptr ptr; /* ptr to btree block */
1854
1855 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1856 XFS_BTREE_TRACE_ARGI(cur, dir);
1857
1858 XFS_BTREE_STATS_INC(cur, lookup);
1859
Darrick J. Wonged150e12016-08-26 15:58:40 +10001860 /* No such thing as a zero-level tree. */
1861 if (cur->bc_nlevels == 0)
1862 return -EFSCORRUPTED;
1863
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001864 block = NULL;
1865 keyno = 0;
1866
1867 /* initialise start pointer from cursor */
1868 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1869 pp = &ptr;
1870
1871 /*
1872 * Iterate over each level in the btree, starting at the root.
1873 * For each level above the leaves, find the key we need, based
1874 * on the lookup record, then follow the corresponding block
1875 * pointer down to the next level.
1876 */
1877 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1878 /* Get the block we need to do the lookup on. */
1879 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1880 if (error)
1881 goto error0;
1882
1883 if (diff == 0) {
1884 /*
1885 * If we already had a key match at a higher level, we
1886 * know we need to use the first entry in this block.
1887 */
1888 keyno = 1;
1889 } else {
1890 /* Otherwise search this block. Do a binary search. */
1891
1892 int high; /* high entry number */
1893 int low; /* low entry number */
1894
1895 /* Set low and high entry numbers, 1-based. */
1896 low = 1;
1897 high = xfs_btree_get_numrecs(block);
1898 if (!high) {
1899 /* Block is empty, must be an empty leaf. */
1900 ASSERT(level == 0 && cur->bc_nlevels == 1);
1901
1902 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1903 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1904 *stat = 0;
1905 return 0;
1906 }
1907
1908 /* Binary search the block. */
1909 while (low <= high) {
1910 union xfs_btree_key key;
1911 union xfs_btree_key *kp;
1912
1913 XFS_BTREE_STATS_INC(cur, compare);
1914
1915 /* keyno is average of low and high. */
1916 keyno = (low + high) >> 1;
1917
1918 /* Get current search key */
1919 kp = xfs_lookup_get_search_key(cur, level,
1920 keyno, block, &key);
1921
1922 /*
1923 * Compute difference to get next direction:
1924 * - less than, move right
1925 * - greater than, move left
1926 * - equal, we're done
1927 */
1928 diff = cur->bc_ops->key_diff(cur, kp);
1929 if (diff < 0)
1930 low = keyno + 1;
1931 else if (diff > 0)
1932 high = keyno - 1;
1933 else
1934 break;
1935 }
1936 }
1937
1938 /*
1939 * If there are more levels, set up for the next level
1940 * by getting the block number and filling in the cursor.
1941 */
1942 if (level > 0) {
1943 /*
1944 * If we moved left, need the previous key number,
1945 * unless there isn't one.
1946 */
1947 if (diff > 0 && --keyno < 1)
1948 keyno = 1;
1949 pp = xfs_btree_ptr_addr(cur, keyno, block);
1950
1951#ifdef DEBUG
1952 error = xfs_btree_check_ptr(cur, pp, 0, level);
1953 if (error)
1954 goto error0;
1955#endif
1956 cur->bc_ptrs[level] = keyno;
1957 }
1958 }
1959
1960 /* Done with the search. See if we need to adjust the results. */
1961 if (dir != XFS_LOOKUP_LE && diff < 0) {
1962 keyno++;
1963 /*
1964 * If ge search and we went off the end of the block, but it's
1965 * not the last block, we're in the wrong block.
1966 */
1967 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1968 if (dir == XFS_LOOKUP_GE &&
1969 keyno > xfs_btree_get_numrecs(block) &&
1970 !xfs_btree_ptr_is_null(cur, &ptr)) {
1971 int i;
1972
1973 cur->bc_ptrs[0] = keyno;
1974 error = xfs_btree_increment(cur, 0, &i);
1975 if (error)
1976 goto error0;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001977 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001978 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1979 *stat = 1;
1980 return 0;
1981 }
1982 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1983 keyno--;
1984 cur->bc_ptrs[0] = keyno;
1985
1986 /* Return if we succeeded or not. */
1987 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1988 *stat = 0;
1989 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1990 *stat = 1;
1991 else
1992 *stat = 0;
1993 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1994 return 0;
1995
1996error0:
1997 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1998 return error;
1999}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002000
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002001/* Find the high key storage area from a regular key. */
2002STATIC union xfs_btree_key *
2003xfs_btree_high_key_from_key(
2004 struct xfs_btree_cur *cur,
2005 union xfs_btree_key *key)
2006{
2007 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2008 return (union xfs_btree_key *)((char *)key +
2009 (cur->bc_ops->key_len / 2));
2010}
2011
Darrick J. Wong973b8312016-08-03 12:22:12 +10002012/* Determine the low (and high if overlapped) keys of a leaf block */
2013STATIC void
2014xfs_btree_get_leaf_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002015 struct xfs_btree_cur *cur,
2016 struct xfs_btree_block *block,
2017 union xfs_btree_key *key)
2018{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002019 union xfs_btree_key max_hkey;
2020 union xfs_btree_key hkey;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002021 union xfs_btree_rec *rec;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002022 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002023 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002024
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002025 rec = xfs_btree_rec_addr(cur, 1, block);
2026 cur->bc_ops->init_key_from_rec(key, rec);
2027
Darrick J. Wong973b8312016-08-03 12:22:12 +10002028 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002029
Darrick J. Wong973b8312016-08-03 12:22:12 +10002030 cur->bc_ops->init_high_key_from_rec(&max_hkey, rec);
2031 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2032 rec = xfs_btree_rec_addr(cur, n, block);
2033 cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2034 if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2035 > 0)
2036 max_hkey = hkey;
2037 }
2038
2039 high = xfs_btree_high_key_from_key(cur, key);
2040 memcpy(high, &max_hkey, cur->bc_ops->key_len / 2);
2041 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002042}
2043
Darrick J. Wong973b8312016-08-03 12:22:12 +10002044/* Determine the low (and high if overlapped) keys of a node block */
2045STATIC void
2046xfs_btree_get_node_keys(
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002047 struct xfs_btree_cur *cur,
2048 struct xfs_btree_block *block,
2049 union xfs_btree_key *key)
2050{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002051 union xfs_btree_key *hkey;
2052 union xfs_btree_key *max_hkey;
2053 union xfs_btree_key *high;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002054 int n;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002055
Darrick J. Wong973b8312016-08-03 12:22:12 +10002056 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2057 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2058 cur->bc_ops->key_len / 2);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002059
Darrick J. Wong973b8312016-08-03 12:22:12 +10002060 max_hkey = xfs_btree_high_key_addr(cur, 1, block);
2061 for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
2062 hkey = xfs_btree_high_key_addr(cur, n, block);
2063 if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2064 max_hkey = hkey;
2065 }
2066
2067 high = xfs_btree_high_key_from_key(cur, key);
2068 memcpy(high, max_hkey, cur->bc_ops->key_len / 2);
2069 } else {
2070 memcpy(key, xfs_btree_key_addr(cur, 1, block),
2071 cur->bc_ops->key_len);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002072 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002073}
2074
Darrick J. Wong70b22652016-08-03 11:03:38 +10002075/* Derive the keys for any btree block. */
2076STATIC void
2077xfs_btree_get_keys(
2078 struct xfs_btree_cur *cur,
2079 struct xfs_btree_block *block,
2080 union xfs_btree_key *key)
2081{
2082 if (be16_to_cpu(block->bb_level) == 0)
Darrick J. Wong973b8312016-08-03 12:22:12 +10002083 xfs_btree_get_leaf_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002084 else
Darrick J. Wong973b8312016-08-03 12:22:12 +10002085 xfs_btree_get_node_keys(cur, block, key);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002086}
2087
2088/*
2089 * Decide if we need to update the parent keys of a btree block. For
2090 * a standard btree this is only necessary if we're updating the first
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002091 * record/key. For an overlapping btree, we must always update the
2092 * keys because the highest key can be in any of the records or keys
2093 * in the block.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002094 */
2095static inline bool
2096xfs_btree_needs_key_update(
2097 struct xfs_btree_cur *cur,
2098 int ptr)
2099{
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002100 return (cur->bc_flags & XFS_BTREE_OVERLAPPING) || ptr == 1;
2101}
2102
2103/*
2104 * Update the low and high parent keys of the given level, progressing
2105 * towards the root. If force_all is false, stop if the keys for a given
2106 * level do not need updating.
2107 */
2108STATIC int
2109__xfs_btree_updkeys(
2110 struct xfs_btree_cur *cur,
2111 int level,
2112 struct xfs_btree_block *block,
2113 struct xfs_buf *bp0,
2114 bool force_all)
2115{
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002116 union xfs_btree_key key; /* keys from current level */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002117 union xfs_btree_key *lkey; /* keys from the next level up */
2118 union xfs_btree_key *hkey;
2119 union xfs_btree_key *nlkey; /* keys from the next level up */
2120 union xfs_btree_key *nhkey;
2121 struct xfs_buf *bp;
2122 int ptr;
2123
2124 ASSERT(cur->bc_flags & XFS_BTREE_OVERLAPPING);
2125
2126 /* Exit if there aren't any parent levels to update. */
2127 if (level + 1 >= cur->bc_nlevels)
2128 return 0;
2129
2130 trace_xfs_btree_updkeys(cur, level, bp0);
2131
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10002132 lkey = &key;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002133 hkey = xfs_btree_high_key_from_key(cur, lkey);
2134 xfs_btree_get_keys(cur, block, lkey);
2135 for (level++; level < cur->bc_nlevels; level++) {
2136#ifdef DEBUG
2137 int error;
2138#endif
2139 block = xfs_btree_get_block(cur, level, &bp);
2140 trace_xfs_btree_updkeys(cur, level, bp);
2141#ifdef DEBUG
2142 error = xfs_btree_check_block(cur, block, level, bp);
2143 if (error) {
2144 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2145 return error;
2146 }
2147#endif
2148 ptr = cur->bc_ptrs[level];
2149 nlkey = xfs_btree_key_addr(cur, ptr, block);
2150 nhkey = xfs_btree_high_key_addr(cur, ptr, block);
2151 if (!force_all &&
2152 !(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2153 cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2154 break;
2155 xfs_btree_copy_keys(cur, nlkey, lkey, 1);
2156 xfs_btree_log_keys(cur, bp, ptr, ptr);
2157 if (level + 1 >= cur->bc_nlevels)
2158 break;
Darrick J. Wong973b8312016-08-03 12:22:12 +10002159 xfs_btree_get_node_keys(cur, block, lkey);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002160 }
2161
2162 return 0;
2163}
2164
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002165/* Update all the keys from some level in cursor back to the root. */
2166STATIC int
2167xfs_btree_updkeys_force(
2168 struct xfs_btree_cur *cur,
2169 int level)
2170{
2171 struct xfs_buf *bp;
2172 struct xfs_btree_block *block;
2173
2174 block = xfs_btree_get_block(cur, level, &bp);
2175 return __xfs_btree_updkeys(cur, level, block, bp, true);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002176}
2177
2178/*
2179 * Update the parent keys of the given level, progressing towards the root.
2180 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002181STATIC int
Darrick J. Wong70b22652016-08-03 11:03:38 +10002182xfs_btree_update_keys(
2183 struct xfs_btree_cur *cur,
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002184 int level)
2185{
2186 struct xfs_btree_block *block;
2187 struct xfs_buf *bp;
2188 union xfs_btree_key *kp;
Darrick J. Wong70b22652016-08-03 11:03:38 +10002189 union xfs_btree_key key;
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002190 int ptr;
2191
Darrick J. Wong973b8312016-08-03 12:22:12 +10002192 ASSERT(level >= 0);
2193
2194 block = xfs_btree_get_block(cur, level, &bp);
2195 if (cur->bc_flags & XFS_BTREE_OVERLAPPING)
2196 return __xfs_btree_updkeys(cur, level, block, bp, false);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002197
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002198 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2199 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
2200
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002201 /*
2202 * Go up the tree from this level toward the root.
2203 * At each level, update the key value to the value input.
2204 * Stop when we reach a level where the cursor isn't pointing
2205 * at the first entry in the block.
2206 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002207 xfs_btree_get_keys(cur, block, &key);
2208 for (level++, ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002209#ifdef DEBUG
2210 int error;
2211#endif
2212 block = xfs_btree_get_block(cur, level, &bp);
2213#ifdef DEBUG
2214 error = xfs_btree_check_block(cur, block, level, bp);
2215 if (error) {
2216 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2217 return error;
2218 }
2219#endif
2220 ptr = cur->bc_ptrs[level];
2221 kp = xfs_btree_key_addr(cur, ptr, block);
Darrick J. Wong70b22652016-08-03 11:03:38 +10002222 xfs_btree_copy_keys(cur, kp, &key, 1);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11002223 xfs_btree_log_keys(cur, bp, ptr, ptr);
2224 }
2225
2226 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2227 return 0;
2228}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002229
2230/*
2231 * Update the record referred to by cur to the value in the
2232 * given record. This either works (return 0) or gets an
2233 * EFSCORRUPTED error.
2234 */
2235int
2236xfs_btree_update(
2237 struct xfs_btree_cur *cur,
2238 union xfs_btree_rec *rec)
2239{
2240 struct xfs_btree_block *block;
2241 struct xfs_buf *bp;
2242 int error;
2243 int ptr;
2244 union xfs_btree_rec *rp;
2245
2246 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2247 XFS_BTREE_TRACE_ARGR(cur, rec);
2248
2249 /* Pick up the current block. */
2250 block = xfs_btree_get_block(cur, 0, &bp);
2251
2252#ifdef DEBUG
2253 error = xfs_btree_check_block(cur, block, 0, bp);
2254 if (error)
2255 goto error0;
2256#endif
2257 /* Get the address of the rec to be updated. */
2258 ptr = cur->bc_ptrs[0];
2259 rp = xfs_btree_rec_addr(cur, ptr, block);
2260
2261 /* Fill in the new contents and log them. */
2262 xfs_btree_copy_recs(cur, rp, rec, 1);
2263 xfs_btree_log_recs(cur, bp, ptr, ptr);
2264
2265 /*
2266 * If we are tracking the last record in the tree and
2267 * we are at the far right edge of the tree, update it.
2268 */
2269 if (xfs_btree_is_lastrec(cur, block, 0)) {
2270 cur->bc_ops->update_lastrec(cur, block, rec,
2271 ptr, LASTREC_UPDATE);
2272 }
2273
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002274 /* Pass new key value up to our parent. */
Darrick J. Wong70b22652016-08-03 11:03:38 +10002275 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002276 error = xfs_btree_update_keys(cur, 0);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11002277 if (error)
2278 goto error0;
2279 }
2280
2281 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2282 return 0;
2283
2284error0:
2285 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2286 return error;
2287}
2288
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002289/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11002290 * Move 1 record left from cur/level if possible.
2291 * Update cur to reflect the new path.
2292 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002293STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002294xfs_btree_lshift(
2295 struct xfs_btree_cur *cur,
2296 int level,
2297 int *stat) /* success/failure */
2298{
Christoph Hellwig687b8902008-10-30 16:56:53 +11002299 struct xfs_buf *lbp; /* left buffer pointer */
2300 struct xfs_btree_block *left; /* left btree block */
2301 int lrecs; /* left record count */
2302 struct xfs_buf *rbp; /* right buffer pointer */
2303 struct xfs_btree_block *right; /* right btree block */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002304 struct xfs_btree_cur *tcur; /* temporary btree cursor */
Christoph Hellwig687b8902008-10-30 16:56:53 +11002305 int rrecs; /* right record count */
2306 union xfs_btree_ptr lptr; /* left btree pointer */
2307 union xfs_btree_key *rkp = NULL; /* right btree key */
2308 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2309 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2310 int error; /* error return value */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002311 int i;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002312
2313 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2314 XFS_BTREE_TRACE_ARGI(cur, level);
2315
2316 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2317 level == cur->bc_nlevels - 1)
2318 goto out0;
2319
2320 /* Set up variables for this block as "right". */
2321 right = xfs_btree_get_block(cur, level, &rbp);
2322
2323#ifdef DEBUG
2324 error = xfs_btree_check_block(cur, right, level, rbp);
2325 if (error)
2326 goto error0;
2327#endif
2328
2329 /* If we've got no left sibling then we can't shift an entry left. */
2330 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2331 if (xfs_btree_ptr_is_null(cur, &lptr))
2332 goto out0;
2333
2334 /*
2335 * If the cursor entry is the one that would be moved, don't
2336 * do it... it's too complicated.
2337 */
2338 if (cur->bc_ptrs[level] <= 1)
2339 goto out0;
2340
2341 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002342 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002343 if (error)
2344 goto error0;
2345
2346 /* If it's full, it can't take another entry. */
2347 lrecs = xfs_btree_get_numrecs(left);
2348 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2349 goto out0;
2350
2351 rrecs = xfs_btree_get_numrecs(right);
2352
2353 /*
2354 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002355 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002356 * later.
2357 */
2358 lrecs++;
2359 rrecs--;
2360
2361 XFS_BTREE_STATS_INC(cur, lshift);
2362 XFS_BTREE_STATS_ADD(cur, moves, 1);
2363
2364 /*
2365 * If non-leaf, copy a key and a ptr to the left block.
2366 * Log the changes to the left block.
2367 */
2368 if (level > 0) {
2369 /* It's a non-leaf. Move keys and pointers. */
2370 union xfs_btree_key *lkp; /* left btree key */
2371 union xfs_btree_ptr *lpp; /* left address pointer */
2372
2373 lkp = xfs_btree_key_addr(cur, lrecs, left);
2374 rkp = xfs_btree_key_addr(cur, 1, right);
2375
2376 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2377 rpp = xfs_btree_ptr_addr(cur, 1, right);
2378#ifdef DEBUG
2379 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2380 if (error)
2381 goto error0;
2382#endif
2383 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2384 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2385
2386 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2387 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2388
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002389 ASSERT(cur->bc_ops->keys_inorder(cur,
2390 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002391 } else {
2392 /* It's a leaf. Move records. */
2393 union xfs_btree_rec *lrp; /* left record pointer */
2394
2395 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2396 rrp = xfs_btree_rec_addr(cur, 1, right);
2397
2398 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2399 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2400
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002401 ASSERT(cur->bc_ops->recs_inorder(cur,
2402 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002403 }
2404
2405 xfs_btree_set_numrecs(left, lrecs);
2406 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2407
2408 xfs_btree_set_numrecs(right, rrecs);
2409 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2410
2411 /*
2412 * Slide the contents of right down one entry.
2413 */
2414 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2415 if (level > 0) {
2416 /* It's a nonleaf. operate on keys and ptrs */
2417#ifdef DEBUG
2418 int i; /* loop index */
2419
2420 for (i = 0; i < rrecs; i++) {
2421 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2422 if (error)
2423 goto error0;
2424 }
2425#endif
2426 xfs_btree_shift_keys(cur,
2427 xfs_btree_key_addr(cur, 2, right),
2428 -1, rrecs);
2429 xfs_btree_shift_ptrs(cur,
2430 xfs_btree_ptr_addr(cur, 2, right),
2431 -1, rrecs);
2432
2433 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2434 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2435 } else {
2436 /* It's a leaf. operate on records */
2437 xfs_btree_shift_recs(cur,
2438 xfs_btree_rec_addr(cur, 2, right),
2439 -1, rrecs);
2440 xfs_btree_log_recs(cur, rbp, 1, rrecs);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002441 }
2442
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002443 /*
2444 * Using a temporary cursor, update the parent key values of the
2445 * block on the left.
2446 */
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002447 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
2448 error = xfs_btree_dup_cursor(cur, &tcur);
2449 if (error)
2450 goto error0;
2451 i = xfs_btree_firstrec(tcur, level);
2452 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002453
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002454 error = xfs_btree_decrement(tcur, level, &i);
2455 if (error)
2456 goto error1;
2457
2458 /* Update the parent high keys of the left block, if needed. */
2459 error = xfs_btree_update_keys(tcur, level);
2460 if (error)
2461 goto error1;
2462
2463 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2464 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002465
Darrick J. Wong70b22652016-08-03 11:03:38 +10002466 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002467 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002468 if (error)
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002469 goto error0;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002470
2471 /* Slide the cursor value left one. */
2472 cur->bc_ptrs[level]--;
2473
2474 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2475 *stat = 1;
2476 return 0;
2477
2478out0:
2479 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2480 *stat = 0;
2481 return 0;
2482
2483error0:
2484 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2485 return error;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002486
2487error1:
2488 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2489 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2490 return error;
Christoph Hellwig687b8902008-10-30 16:56:53 +11002491}
2492
2493/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002494 * Move 1 record right from cur/level if possible.
2495 * Update cur to reflect the new path.
2496 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002497STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002498xfs_btree_rshift(
2499 struct xfs_btree_cur *cur,
2500 int level,
2501 int *stat) /* success/failure */
2502{
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002503 struct xfs_buf *lbp; /* left buffer pointer */
2504 struct xfs_btree_block *left; /* left btree block */
2505 struct xfs_buf *rbp; /* right buffer pointer */
2506 struct xfs_btree_block *right; /* right btree block */
2507 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2508 union xfs_btree_ptr rptr; /* right block pointer */
2509 union xfs_btree_key *rkp; /* right btree key */
2510 int rrecs; /* right record count */
2511 int lrecs; /* left record count */
2512 int error; /* error return value */
2513 int i; /* loop counter */
2514
2515 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2516 XFS_BTREE_TRACE_ARGI(cur, level);
2517
2518 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2519 (level == cur->bc_nlevels - 1))
2520 goto out0;
2521
2522 /* Set up variables for this block as "left". */
2523 left = xfs_btree_get_block(cur, level, &lbp);
2524
2525#ifdef DEBUG
2526 error = xfs_btree_check_block(cur, left, level, lbp);
2527 if (error)
2528 goto error0;
2529#endif
2530
2531 /* If we've got no right sibling then we can't shift an entry right. */
2532 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2533 if (xfs_btree_ptr_is_null(cur, &rptr))
2534 goto out0;
2535
2536 /*
2537 * If the cursor entry is the one that would be moved, don't
2538 * do it... it's too complicated.
2539 */
2540 lrecs = xfs_btree_get_numrecs(left);
2541 if (cur->bc_ptrs[level] >= lrecs)
2542 goto out0;
2543
2544 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002545 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002546 if (error)
2547 goto error0;
2548
2549 /* If it's full, it can't take another entry. */
2550 rrecs = xfs_btree_get_numrecs(right);
2551 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2552 goto out0;
2553
2554 XFS_BTREE_STATS_INC(cur, rshift);
2555 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2556
2557 /*
2558 * Make a hole at the start of the right neighbor block, then
2559 * copy the last left block entry to the hole.
2560 */
2561 if (level > 0) {
2562 /* It's a nonleaf. make a hole in the keys and ptrs */
2563 union xfs_btree_key *lkp;
2564 union xfs_btree_ptr *lpp;
2565 union xfs_btree_ptr *rpp;
2566
2567 lkp = xfs_btree_key_addr(cur, lrecs, left);
2568 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2569 rkp = xfs_btree_key_addr(cur, 1, right);
2570 rpp = xfs_btree_ptr_addr(cur, 1, right);
2571
2572#ifdef DEBUG
2573 for (i = rrecs - 1; i >= 0; i--) {
2574 error = xfs_btree_check_ptr(cur, rpp, i, level);
2575 if (error)
2576 goto error0;
2577 }
2578#endif
2579
2580 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2581 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2582
2583#ifdef DEBUG
2584 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2585 if (error)
2586 goto error0;
2587#endif
2588
2589 /* Now put the new data in, and log it. */
2590 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2591 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2592
2593 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2594 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2595
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002596 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2597 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002598 } else {
2599 /* It's a leaf. make a hole in the records */
2600 union xfs_btree_rec *lrp;
2601 union xfs_btree_rec *rrp;
2602
2603 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2604 rrp = xfs_btree_rec_addr(cur, 1, right);
2605
2606 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2607
2608 /* Now put the new data in, and log it. */
2609 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2610 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002611 }
2612
2613 /*
2614 * Decrement and log left's numrecs, bump and log right's numrecs.
2615 */
2616 xfs_btree_set_numrecs(left, --lrecs);
2617 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2618
2619 xfs_btree_set_numrecs(right, ++rrecs);
2620 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2621
2622 /*
2623 * Using a temporary cursor, update the parent key values of the
2624 * block on the right.
2625 */
2626 error = xfs_btree_dup_cursor(cur, &tcur);
2627 if (error)
2628 goto error0;
2629 i = xfs_btree_lastrec(tcur, level);
Darrick J. Wongc1d22ae2016-08-03 12:26:22 +10002630 XFS_WANT_CORRUPTED_GOTO(tcur->bc_mp, i == 1, error0);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002631
2632 error = xfs_btree_increment(tcur, level, &i);
2633 if (error)
2634 goto error1;
2635
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002636 /* Update the parent high keys of the left block, if needed. */
2637 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002638 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002639 if (error)
2640 goto error1;
2641 }
2642
Darrick J. Wong70b22652016-08-03 11:03:38 +10002643 /* Update the parent keys of the right block. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002644 error = xfs_btree_update_keys(tcur, level);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002645 if (error)
2646 goto error1;
2647
2648 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2649
2650 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2651 *stat = 1;
2652 return 0;
2653
2654out0:
2655 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2656 *stat = 0;
2657 return 0;
2658
2659error0:
2660 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2661 return error;
2662
2663error1:
2664 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2665 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2666 return error;
2667}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002668
2669/*
2670 * Split cur/level block in half.
2671 * Return new block number and the key to its first
2672 * record (to be inserted into parent).
2673 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002674STATIC int /* error */
Dave Chinnercf11da92014-07-15 07:08:24 +10002675__xfs_btree_split(
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002676 struct xfs_btree_cur *cur,
2677 int level,
2678 union xfs_btree_ptr *ptrp,
2679 union xfs_btree_key *key,
2680 struct xfs_btree_cur **curp,
2681 int *stat) /* success/failure */
2682{
2683 union xfs_btree_ptr lptr; /* left sibling block ptr */
2684 struct xfs_buf *lbp; /* left buffer pointer */
2685 struct xfs_btree_block *left; /* left btree block */
2686 union xfs_btree_ptr rptr; /* right sibling block ptr */
2687 struct xfs_buf *rbp; /* right buffer pointer */
2688 struct xfs_btree_block *right; /* right btree block */
2689 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2690 struct xfs_buf *rrbp; /* right-right buffer pointer */
2691 struct xfs_btree_block *rrblock; /* right-right btree block */
2692 int lrecs;
2693 int rrecs;
2694 int src_index;
2695 int error; /* error return value */
2696#ifdef DEBUG
2697 int i;
2698#endif
2699
2700 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2701 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2702
2703 XFS_BTREE_STATS_INC(cur, split);
2704
2705 /* Set up left block (current one). */
2706 left = xfs_btree_get_block(cur, level, &lbp);
2707
2708#ifdef DEBUG
2709 error = xfs_btree_check_block(cur, left, level, lbp);
2710 if (error)
2711 goto error0;
2712#endif
2713
2714 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2715
2716 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002717 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002718 if (error)
2719 goto error0;
2720 if (*stat == 0)
2721 goto out0;
2722 XFS_BTREE_STATS_INC(cur, alloc);
2723
2724 /* Set up the new block as "right". */
2725 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2726 if (error)
2727 goto error0;
2728
2729 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002730 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002731
2732 /*
2733 * Split the entries between the old and the new block evenly.
2734 * Make sure that if there's an odd number of entries now, that
2735 * each new block will have the same number of entries.
2736 */
2737 lrecs = xfs_btree_get_numrecs(left);
2738 rrecs = lrecs / 2;
2739 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2740 rrecs++;
2741 src_index = (lrecs - rrecs + 1);
2742
2743 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2744
Darrick J. Wong70b22652016-08-03 11:03:38 +10002745 /* Adjust numrecs for the later get_*_keys() calls. */
2746 lrecs -= rrecs;
2747 xfs_btree_set_numrecs(left, lrecs);
2748 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2749
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002750 /*
2751 * Copy btree block entries from the left block over to the
2752 * new block, the right. Update the right block and log the
2753 * changes.
2754 */
2755 if (level > 0) {
2756 /* It's a non-leaf. Move keys and pointers. */
2757 union xfs_btree_key *lkp; /* left btree key */
2758 union xfs_btree_ptr *lpp; /* left address pointer */
2759 union xfs_btree_key *rkp; /* right btree key */
2760 union xfs_btree_ptr *rpp; /* right address pointer */
2761
2762 lkp = xfs_btree_key_addr(cur, src_index, left);
2763 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2764 rkp = xfs_btree_key_addr(cur, 1, right);
2765 rpp = xfs_btree_ptr_addr(cur, 1, right);
2766
2767#ifdef DEBUG
2768 for (i = src_index; i < rrecs; i++) {
2769 error = xfs_btree_check_ptr(cur, lpp, i, level);
2770 if (error)
2771 goto error0;
2772 }
2773#endif
2774
Darrick J. Wong70b22652016-08-03 11:03:38 +10002775 /* Copy the keys & pointers to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002776 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2777 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2778
2779 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2780 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2781
Darrick J. Wong70b22652016-08-03 11:03:38 +10002782 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002783 xfs_btree_get_node_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002784 } else {
2785 /* It's a leaf. Move records. */
2786 union xfs_btree_rec *lrp; /* left record pointer */
2787 union xfs_btree_rec *rrp; /* right record pointer */
2788
2789 lrp = xfs_btree_rec_addr(cur, src_index, left);
2790 rrp = xfs_btree_rec_addr(cur, 1, right);
2791
Darrick J. Wong70b22652016-08-03 11:03:38 +10002792 /* Copy records to the new block. */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002793 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2794 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2795
Darrick J. Wong70b22652016-08-03 11:03:38 +10002796 /* Stash the keys of the new block for later insertion. */
Darrick J. Wong973b8312016-08-03 12:22:12 +10002797 xfs_btree_get_leaf_keys(cur, right, key);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002798 }
2799
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002800 /*
2801 * Find the left block number by looking in the buffer.
Darrick J. Wong70b22652016-08-03 11:03:38 +10002802 * Adjust sibling pointers.
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002803 */
2804 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2805 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2806 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2807 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2808
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002809 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2810 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2811
2812 /*
2813 * If there's a block to the new block's right, make that block
2814 * point back to right instead of to left.
2815 */
2816 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002817 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002818 0, &rrblock, &rrbp);
2819 if (error)
2820 goto error0;
2821 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2822 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2823 }
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002824
2825 /* Update the parent high keys of the left block, if needed. */
2826 if (cur->bc_flags & XFS_BTREE_OVERLAPPING) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10002827 error = xfs_btree_update_keys(cur, level);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10002828 if (error)
2829 goto error0;
2830 }
2831
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002832 /*
2833 * If the cursor is really in the right block, move it there.
2834 * If it's just pointing past the last entry in left, then we'll
2835 * insert there, so don't change anything in that case.
2836 */
2837 if (cur->bc_ptrs[level] > lrecs + 1) {
2838 xfs_btree_setbuf(cur, level, rbp);
2839 cur->bc_ptrs[level] -= lrecs;
2840 }
2841 /*
2842 * If there are more levels, we'll need another cursor which refers
2843 * the right block, no matter where this cursor was.
2844 */
2845 if (level + 1 < cur->bc_nlevels) {
2846 error = xfs_btree_dup_cursor(cur, curp);
2847 if (error)
2848 goto error0;
2849 (*curp)->bc_ptrs[level + 1]++;
2850 }
2851 *ptrp = rptr;
2852 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2853 *stat = 1;
2854 return 0;
2855out0:
2856 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2857 *stat = 0;
2858 return 0;
2859
2860error0:
2861 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2862 return error;
2863}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002864
Dave Chinnercf11da92014-07-15 07:08:24 +10002865struct xfs_btree_split_args {
2866 struct xfs_btree_cur *cur;
2867 int level;
2868 union xfs_btree_ptr *ptrp;
2869 union xfs_btree_key *key;
2870 struct xfs_btree_cur **curp;
2871 int *stat; /* success/failure */
2872 int result;
2873 bool kswapd; /* allocation in kswapd context */
2874 struct completion *done;
2875 struct work_struct work;
2876};
2877
2878/*
2879 * Stack switching interfaces for allocation
2880 */
2881static void
2882xfs_btree_split_worker(
2883 struct work_struct *work)
2884{
2885 struct xfs_btree_split_args *args = container_of(work,
2886 struct xfs_btree_split_args, work);
2887 unsigned long pflags;
2888 unsigned long new_pflags = PF_FSTRANS;
2889
2890 /*
2891 * we are in a transaction context here, but may also be doing work
2892 * in kswapd context, and hence we may need to inherit that state
2893 * temporarily to ensure that we don't block waiting for memory reclaim
2894 * in any way.
2895 */
2896 if (args->kswapd)
2897 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2898
2899 current_set_flags_nested(&pflags, new_pflags);
2900
2901 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2902 args->key, args->curp, args->stat);
2903 complete(args->done);
2904
2905 current_restore_flags_nested(&pflags, new_pflags);
2906}
2907
2908/*
2909 * BMBT split requests often come in with little stack to work on. Push
2910 * them off to a worker thread so there is lots of stack to use. For the other
2911 * btree types, just call directly to avoid the context switch overhead here.
2912 */
2913STATIC int /* error */
2914xfs_btree_split(
2915 struct xfs_btree_cur *cur,
2916 int level,
2917 union xfs_btree_ptr *ptrp,
2918 union xfs_btree_key *key,
2919 struct xfs_btree_cur **curp,
2920 int *stat) /* success/failure */
2921{
2922 struct xfs_btree_split_args args;
2923 DECLARE_COMPLETION_ONSTACK(done);
2924
2925 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2926 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2927
2928 args.cur = cur;
2929 args.level = level;
2930 args.ptrp = ptrp;
2931 args.key = key;
2932 args.curp = curp;
2933 args.stat = stat;
2934 args.done = &done;
2935 args.kswapd = current_is_kswapd();
2936 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2937 queue_work(xfs_alloc_wq, &args.work);
2938 wait_for_completion(&done);
2939 destroy_work_on_stack(&args.work);
2940 return args.result;
2941}
2942
2943
Christoph Hellwig344207c2008-10-30 16:57:16 +11002944/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002945 * Copy the old inode root contents into a real block and make the
2946 * broot point to it.
2947 */
2948int /* error */
2949xfs_btree_new_iroot(
2950 struct xfs_btree_cur *cur, /* btree cursor */
2951 int *logflags, /* logging flags for inode */
2952 int *stat) /* return status - 0 fail */
2953{
2954 struct xfs_buf *cbp; /* buffer for cblock */
2955 struct xfs_btree_block *block; /* btree block */
2956 struct xfs_btree_block *cblock; /* child btree block */
2957 union xfs_btree_key *ckp; /* child key pointer */
2958 union xfs_btree_ptr *cpp; /* child ptr pointer */
2959 union xfs_btree_key *kp; /* pointer to btree key */
2960 union xfs_btree_ptr *pp; /* pointer to block addr */
2961 union xfs_btree_ptr nptr; /* new block addr */
2962 int level; /* btree level */
2963 int error; /* error return code */
2964#ifdef DEBUG
2965 int i; /* loop counter */
2966#endif
2967
2968 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2969 XFS_BTREE_STATS_INC(cur, newroot);
2970
2971 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2972
2973 level = cur->bc_nlevels - 1;
2974
2975 block = xfs_btree_get_iroot(cur);
2976 pp = xfs_btree_ptr_addr(cur, 1, block);
2977
2978 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002979 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002980 if (error)
2981 goto error0;
2982 if (*stat == 0) {
2983 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2984 return 0;
2985 }
2986 XFS_BTREE_STATS_INC(cur, alloc);
2987
2988 /* Copy the root into a real block. */
2989 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2990 if (error)
2991 goto error0;
2992
Dave Chinner088c9f62013-06-12 12:19:08 +10002993 /*
2994 * we can't just memcpy() the root in for CRC enabled btree blocks.
2995 * In that case have to also ensure the blkno remains correct
2996 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002997 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002998 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2999 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3000 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
3001 else
3002 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
3003 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003004
3005 be16_add_cpu(&block->bb_level, 1);
3006 xfs_btree_set_numrecs(block, 1);
3007 cur->bc_nlevels++;
3008 cur->bc_ptrs[level + 1] = 1;
3009
3010 kp = xfs_btree_key_addr(cur, 1, block);
3011 ckp = xfs_btree_key_addr(cur, 1, cblock);
3012 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
3013
3014 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3015#ifdef DEBUG
3016 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
3017 error = xfs_btree_check_ptr(cur, pp, i, level);
3018 if (error)
3019 goto error0;
3020 }
3021#endif
3022 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
3023
3024#ifdef DEBUG
3025 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
3026 if (error)
3027 goto error0;
3028#endif
3029 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
3030
3031 xfs_iroot_realloc(cur->bc_private.b.ip,
3032 1 - xfs_btree_get_numrecs(cblock),
3033 cur->bc_private.b.whichfork);
3034
3035 xfs_btree_setbuf(cur, level, cbp);
3036
3037 /*
3038 * Do all this logging at the end so that
3039 * the root is at the right level.
3040 */
3041 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
3042 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3043 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
3044
3045 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06003046 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11003047 *stat = 1;
3048 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3049 return 0;
3050error0:
3051 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3052 return error;
3053}
3054
3055/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11003056 * Allocate a new root block, fill it in.
3057 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11003058STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11003059xfs_btree_new_root(
3060 struct xfs_btree_cur *cur, /* btree cursor */
3061 int *stat) /* success/failure */
3062{
3063 struct xfs_btree_block *block; /* one half of the old root block */
3064 struct xfs_buf *bp; /* buffer containing block */
3065 int error; /* error return value */
3066 struct xfs_buf *lbp; /* left buffer pointer */
3067 struct xfs_btree_block *left; /* left btree block */
3068 struct xfs_buf *nbp; /* new (root) buffer */
3069 struct xfs_btree_block *new; /* new (root) btree block */
3070 int nptr; /* new value for key index, 1 or 2 */
3071 struct xfs_buf *rbp; /* right buffer pointer */
3072 struct xfs_btree_block *right; /* right btree block */
3073 union xfs_btree_ptr rptr;
3074 union xfs_btree_ptr lptr;
3075
3076 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3077 XFS_BTREE_STATS_INC(cur, newroot);
3078
3079 /* initialise our start point from the cursor */
3080 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
3081
3082 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10003083 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003084 if (error)
3085 goto error0;
3086 if (*stat == 0)
3087 goto out0;
3088 XFS_BTREE_STATS_INC(cur, alloc);
3089
3090 /* Set up the new block. */
3091 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
3092 if (error)
3093 goto error0;
3094
3095 /* Set the root in the holding structure increasing the level by 1. */
3096 cur->bc_ops->set_root(cur, &lptr, 1);
3097
3098 /*
3099 * At the previous root level there are now two blocks: the old root,
3100 * and the new block generated when it was split. We don't know which
3101 * one the cursor is pointing at, so we set up variables "left" and
3102 * "right" for each case.
3103 */
3104 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
3105
3106#ifdef DEBUG
3107 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
3108 if (error)
3109 goto error0;
3110#endif
3111
3112 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3113 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3114 /* Our block is left, pick up the right block. */
3115 lbp = bp;
3116 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
3117 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003118 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003119 if (error)
3120 goto error0;
3121 bp = rbp;
3122 nptr = 1;
3123 } else {
3124 /* Our block is right, pick up the left block. */
3125 rbp = bp;
3126 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
3127 right = block;
3128 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003129 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003130 if (error)
3131 goto error0;
3132 bp = lbp;
3133 nptr = 2;
3134 }
Darrick J. Wong70b22652016-08-03 11:03:38 +10003135
Christoph Hellwig344207c2008-10-30 16:57:16 +11003136 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05003137 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11003138 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
3139 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
3140 !xfs_btree_ptr_is_null(cur, &rptr));
3141
3142 /* Fill in the key data in the new root. */
3143 if (xfs_btree_get_level(left) > 0) {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003144 /*
3145 * Get the keys for the left block's keys and put them directly
3146 * in the parent block. Do the same for the right block.
3147 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003148 xfs_btree_get_node_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003149 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003150 xfs_btree_get_node_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003151 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003152 } else {
Darrick J. Wong70b22652016-08-03 11:03:38 +10003153 /*
3154 * Get the keys for the left block's records and put them
3155 * directly in the parent block. Do the same for the right
3156 * block.
3157 */
Darrick J. Wong973b8312016-08-03 12:22:12 +10003158 xfs_btree_get_leaf_keys(cur, left,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003159 xfs_btree_key_addr(cur, 1, new));
Darrick J. Wong973b8312016-08-03 12:22:12 +10003160 xfs_btree_get_leaf_keys(cur, right,
Darrick J. Wong70b22652016-08-03 11:03:38 +10003161 xfs_btree_key_addr(cur, 2, new));
Christoph Hellwig344207c2008-10-30 16:57:16 +11003162 }
3163 xfs_btree_log_keys(cur, nbp, 1, 2);
3164
3165 /* Fill in the pointer data in the new root. */
3166 xfs_btree_copy_ptrs(cur,
3167 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
3168 xfs_btree_copy_ptrs(cur,
3169 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
3170 xfs_btree_log_ptrs(cur, nbp, 1, 2);
3171
3172 /* Fix up the cursor. */
3173 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
3174 cur->bc_ptrs[cur->bc_nlevels] = nptr;
3175 cur->bc_nlevels++;
3176 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3177 *stat = 1;
3178 return 0;
3179error0:
3180 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3181 return error;
3182out0:
3183 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3184 *stat = 0;
3185 return 0;
3186}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003187
3188STATIC int
3189xfs_btree_make_block_unfull(
3190 struct xfs_btree_cur *cur, /* btree cursor */
3191 int level, /* btree level */
3192 int numrecs,/* # of recs in block */
3193 int *oindex,/* old tree index */
3194 int *index, /* new tree index */
3195 union xfs_btree_ptr *nptr, /* new btree ptr */
3196 struct xfs_btree_cur **ncur, /* new btree cursor */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003197 union xfs_btree_key *key, /* key of new block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003198 int *stat)
3199{
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003200 int error = 0;
3201
3202 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3203 level == cur->bc_nlevels - 1) {
3204 struct xfs_inode *ip = cur->bc_private.b.ip;
3205
3206 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
3207 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003208 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
Darrick J. Wong0d309792016-08-03 11:01:25 +10003209 *stat = 1;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003210 } else {
3211 /* A root block that needs replacing */
3212 int logflags = 0;
3213
3214 error = xfs_btree_new_iroot(cur, &logflags, stat);
3215 if (error || *stat == 0)
3216 return error;
3217
3218 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
3219 }
3220
3221 return 0;
3222 }
3223
3224 /* First, try shifting an entry to the right neighbor. */
3225 error = xfs_btree_rshift(cur, level, stat);
3226 if (error || *stat)
3227 return error;
3228
3229 /* Next, try shifting an entry to the left neighbor. */
3230 error = xfs_btree_lshift(cur, level, stat);
3231 if (error)
3232 return error;
3233
3234 if (*stat) {
3235 *oindex = *index = cur->bc_ptrs[level];
3236 return 0;
3237 }
3238
3239 /*
3240 * Next, try splitting the current block in half.
3241 *
3242 * If this works we have to re-set our variables because we
3243 * could be in a different block now.
3244 */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003245 error = xfs_btree_split(cur, level, nptr, key, ncur, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003246 if (error || *stat == 0)
3247 return error;
3248
3249
3250 *index = cur->bc_ptrs[level];
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003251 return 0;
3252}
3253
3254/*
3255 * Insert one record/level. Return information to the caller
3256 * allowing the next level up to proceed if necessary.
3257 */
3258STATIC int
3259xfs_btree_insrec(
3260 struct xfs_btree_cur *cur, /* btree cursor */
3261 int level, /* level to insert record at */
3262 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003263 union xfs_btree_rec *rec, /* record to insert */
3264 union xfs_btree_key *key, /* i/o: block key for ptrp */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003265 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
3266 int *stat) /* success/failure */
3267{
3268 struct xfs_btree_block *block; /* btree block */
3269 struct xfs_buf *bp; /* buffer for block */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003270 union xfs_btree_ptr nptr; /* new block ptr */
3271 struct xfs_btree_cur *ncur; /* new btree cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003272 union xfs_btree_key nkey; /* new block key */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003273 union xfs_btree_key *lkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003274 int optr; /* old key/record index */
3275 int ptr; /* key/record index */
3276 int numrecs;/* number of records */
3277 int error; /* error return value */
3278#ifdef DEBUG
3279 int i;
3280#endif
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003281 xfs_daddr_t old_bn;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003282
3283 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003284 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003285
3286 ncur = NULL;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003287 lkey = &nkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003288
3289 /*
3290 * If we have an external root pointer, and we've made it to the
3291 * root level, allocate a new root block and we're done.
3292 */
3293 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
3294 (level >= cur->bc_nlevels)) {
3295 error = xfs_btree_new_root(cur, stat);
3296 xfs_btree_set_ptr_null(cur, ptrp);
3297
3298 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3299 return error;
3300 }
3301
3302 /* If we're off the left edge, return failure. */
3303 ptr = cur->bc_ptrs[level];
3304 if (ptr == 0) {
3305 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3306 *stat = 0;
3307 return 0;
3308 }
3309
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003310 optr = ptr;
3311
3312 XFS_BTREE_STATS_INC(cur, insrec);
3313
3314 /* Get pointers to the btree buffer and block. */
3315 block = xfs_btree_get_block(cur, level, &bp);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003316 old_bn = bp ? bp->b_bn : XFS_BUF_DADDR_NULL;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003317 numrecs = xfs_btree_get_numrecs(block);
3318
3319#ifdef DEBUG
3320 error = xfs_btree_check_block(cur, block, level, bp);
3321 if (error)
3322 goto error0;
3323
3324 /* Check that the new entry is being inserted in the right place. */
3325 if (ptr <= numrecs) {
3326 if (level == 0) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003327 ASSERT(cur->bc_ops->recs_inorder(cur, rec,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003328 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003329 } else {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003330 ASSERT(cur->bc_ops->keys_inorder(cur, key,
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003331 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003332 }
3333 }
3334#endif
3335
3336 /*
3337 * If the block is full, we can't insert the new entry until we
3338 * make the block un-full.
3339 */
3340 xfs_btree_set_ptr_null(cur, &nptr);
3341 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3342 error = xfs_btree_make_block_unfull(cur, level, numrecs,
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003343 &optr, &ptr, &nptr, &ncur, lkey, stat);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003344 if (error || *stat == 0)
3345 goto error0;
3346 }
3347
3348 /*
3349 * The current block may have changed if the block was
3350 * previously full and we have just made space in it.
3351 */
3352 block = xfs_btree_get_block(cur, level, &bp);
3353 numrecs = xfs_btree_get_numrecs(block);
3354
3355#ifdef DEBUG
3356 error = xfs_btree_check_block(cur, block, level, bp);
3357 if (error)
3358 return error;
3359#endif
3360
3361 /*
3362 * At this point we know there's room for our new entry in the block
3363 * we're pointing at.
3364 */
3365 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3366
3367 if (level > 0) {
3368 /* It's a nonleaf. make a hole in the keys and ptrs */
3369 union xfs_btree_key *kp;
3370 union xfs_btree_ptr *pp;
3371
3372 kp = xfs_btree_key_addr(cur, ptr, block);
3373 pp = xfs_btree_ptr_addr(cur, ptr, block);
3374
3375#ifdef DEBUG
3376 for (i = numrecs - ptr; i >= 0; i--) {
3377 error = xfs_btree_check_ptr(cur, pp, i, level);
3378 if (error)
3379 return error;
3380 }
3381#endif
3382
3383 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3384 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3385
3386#ifdef DEBUG
3387 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
3388 if (error)
3389 goto error0;
3390#endif
3391
3392 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003393 xfs_btree_copy_keys(cur, kp, key, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003394 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3395 numrecs++;
3396 xfs_btree_set_numrecs(block, numrecs);
3397 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3398 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3399#ifdef DEBUG
3400 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003401 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3402 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003403 }
3404#endif
3405 } else {
3406 /* It's a leaf. make a hole in the records */
3407 union xfs_btree_rec *rp;
3408
3409 rp = xfs_btree_rec_addr(cur, ptr, block);
3410
3411 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3412
3413 /* Now put the new data in, bump numrecs and log it. */
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003414 xfs_btree_copy_recs(cur, rp, rec, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003415 xfs_btree_set_numrecs(block, ++numrecs);
3416 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3417#ifdef DEBUG
3418 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003419 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3420 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003421 }
3422#endif
3423 }
3424
3425 /* Log the new number of records in the btree header. */
3426 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3427
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003428 /*
3429 * If we just inserted into a new tree block, we have to
3430 * recalculate nkey here because nkey is out of date.
3431 *
3432 * Otherwise we're just updating an existing block (having shoved
3433 * some records into the new tree block), so use the regular key
3434 * update mechanism.
3435 */
3436 if (bp && bp->b_bn != old_bn) {
3437 xfs_btree_get_keys(cur, block, lkey);
3438 } else if (xfs_btree_needs_key_update(cur, optr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003439 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003440 if (error)
3441 goto error0;
3442 }
3443
3444 /*
3445 * If we are tracking the last record in the tree and
3446 * we are at the far right edge of the tree, update it.
3447 */
3448 if (xfs_btree_is_lastrec(cur, block, level)) {
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003449 cur->bc_ops->update_lastrec(cur, block, rec,
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003450 ptr, LASTREC_INSREC);
3451 }
3452
3453 /*
3454 * Return the new block number, if any.
3455 * If there is one, give back a record value and a cursor too.
3456 */
3457 *ptrp = nptr;
3458 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003459 xfs_btree_copy_keys(cur, key, lkey, 1);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003460 *curp = ncur;
3461 }
3462
3463 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3464 *stat = 1;
3465 return 0;
3466
3467error0:
3468 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3469 return error;
3470}
3471
3472/*
3473 * Insert the record at the point referenced by cur.
3474 *
3475 * A multi-level split of the tree on insert will invalidate the original
3476 * cursor. All callers of this function should assume that the cursor is
3477 * no longer valid and revalidate it.
3478 */
3479int
3480xfs_btree_insert(
3481 struct xfs_btree_cur *cur,
3482 int *stat)
3483{
3484 int error; /* error return value */
3485 int i; /* result value, 0 for failure */
3486 int level; /* current level number in btree */
3487 union xfs_btree_ptr nptr; /* new block number (split result) */
3488 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3489 struct xfs_btree_cur *pcur; /* previous level's cursor */
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003490 union xfs_btree_key bkey; /* key of block to insert */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003491 union xfs_btree_key *key;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003492 union xfs_btree_rec rec; /* record to insert */
3493
3494 level = 0;
3495 ncur = NULL;
3496 pcur = cur;
Darrick J. Wonga1d46cf2016-09-19 10:24:36 +10003497 key = &bkey;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003498
3499 xfs_btree_set_ptr_null(cur, &nptr);
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003500
3501 /* Make a key out of the record data to be inserted, and save it. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003502 cur->bc_ops->init_rec_from_cur(cur, &rec);
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003503 cur->bc_ops->init_key_from_rec(key, &rec);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003504
3505 /*
3506 * Loop going up the tree, starting at the leaf level.
3507 * Stop when we don't get a split block, that must mean that
3508 * the insert is finished with this level.
3509 */
3510 do {
3511 /*
3512 * Insert nrec/nptr into this level of the tree.
3513 * Note if we fail, nptr will be null.
3514 */
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10003515 error = xfs_btree_insrec(pcur, level, &nptr, &rec, key,
Darrick J. Wonge5821e52016-08-03 11:02:39 +10003516 &ncur, &i);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003517 if (error) {
3518 if (pcur != cur)
3519 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3520 goto error0;
3521 }
3522
Eric Sandeenc29aad42015-02-23 22:39:08 +11003523 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003524 level++;
3525
3526 /*
3527 * See if the cursor we just used is trash.
3528 * Can't trash the caller's cursor, but otherwise we should
3529 * if ncur is a new cursor or we're about to be done.
3530 */
3531 if (pcur != cur &&
3532 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3533 /* Save the state from the cursor before we trash it */
3534 if (cur->bc_ops->update_cursor)
3535 cur->bc_ops->update_cursor(pcur, cur);
3536 cur->bc_nlevels = pcur->bc_nlevels;
3537 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3538 }
3539 /* If we got a new cursor, switch to it. */
3540 if (ncur) {
3541 pcur = ncur;
3542 ncur = NULL;
3543 }
3544 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3545
3546 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3547 *stat = i;
3548 return 0;
3549error0:
3550 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3551 return error;
3552}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003553
3554/*
3555 * Try to merge a non-leaf block back into the inode root.
3556 *
3557 * Note: the killroot names comes from the fact that we're effectively
3558 * killing the old root block. But because we can't just delete the
3559 * inode we have to copy the single block it was pointing to into the
3560 * inode.
3561 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003562STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003563xfs_btree_kill_iroot(
3564 struct xfs_btree_cur *cur)
3565{
3566 int whichfork = cur->bc_private.b.whichfork;
3567 struct xfs_inode *ip = cur->bc_private.b.ip;
3568 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3569 struct xfs_btree_block *block;
3570 struct xfs_btree_block *cblock;
3571 union xfs_btree_key *kp;
3572 union xfs_btree_key *ckp;
3573 union xfs_btree_ptr *pp;
3574 union xfs_btree_ptr *cpp;
3575 struct xfs_buf *cbp;
3576 int level;
3577 int index;
3578 int numrecs;
Christoph Hellwig196328e2016-02-08 14:58:07 +11003579 int error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003580#ifdef DEBUG
3581 union xfs_btree_ptr ptr;
3582 int i;
3583#endif
3584
3585 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3586
3587 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3588 ASSERT(cur->bc_nlevels > 1);
3589
3590 /*
3591 * Don't deal with the root block needs to be a leaf case.
3592 * We're just going to turn the thing back into extents anyway.
3593 */
3594 level = cur->bc_nlevels - 1;
3595 if (level == 1)
3596 goto out0;
3597
3598 /*
3599 * Give up if the root has multiple children.
3600 */
3601 block = xfs_btree_get_iroot(cur);
3602 if (xfs_btree_get_numrecs(block) != 1)
3603 goto out0;
3604
3605 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3606 numrecs = xfs_btree_get_numrecs(cblock);
3607
3608 /*
3609 * Only do this if the next level will fit.
3610 * Then the data must be copied up to the inode,
3611 * instead of freeing the root you free the next level.
3612 */
3613 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3614 goto out0;
3615
3616 XFS_BTREE_STATS_INC(cur, killroot);
3617
3618#ifdef DEBUG
3619 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3620 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3621 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3622 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3623#endif
3624
3625 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3626 if (index) {
3627 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3628 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003629 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003630 }
3631
3632 be16_add_cpu(&block->bb_numrecs, index);
3633 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3634
3635 kp = xfs_btree_key_addr(cur, 1, block);
3636 ckp = xfs_btree_key_addr(cur, 1, cblock);
3637 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3638
3639 pp = xfs_btree_ptr_addr(cur, 1, block);
3640 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3641#ifdef DEBUG
3642 for (i = 0; i < numrecs; i++) {
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003643 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3644 if (error) {
3645 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3646 return error;
3647 }
3648 }
3649#endif
3650 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3651
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003652 error = xfs_btree_free_block(cur, cbp);
Christoph Hellwig196328e2016-02-08 14:58:07 +11003653 if (error) {
3654 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3655 return error;
3656 }
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003657
3658 cur->bc_bufs[level - 1] = NULL;
3659 be16_add_cpu(&block->bb_level, -1);
3660 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003661 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003662 cur->bc_nlevels--;
3663out0:
3664 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3665 return 0;
3666}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003667
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003668/*
3669 * Kill the current root node, and replace it with it's only child node.
3670 */
3671STATIC int
3672xfs_btree_kill_root(
3673 struct xfs_btree_cur *cur,
3674 struct xfs_buf *bp,
3675 int level,
3676 union xfs_btree_ptr *newroot)
3677{
3678 int error;
3679
3680 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3681 XFS_BTREE_STATS_INC(cur, killroot);
3682
3683 /*
3684 * Update the root pointer, decreasing the level by 1 and then
3685 * free the old root.
3686 */
3687 cur->bc_ops->set_root(cur, newroot, -1);
3688
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003689 error = xfs_btree_free_block(cur, bp);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003690 if (error) {
3691 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3692 return error;
3693 }
3694
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003695 cur->bc_bufs[level] = NULL;
3696 cur->bc_ra[level] = 0;
3697 cur->bc_nlevels--;
3698
3699 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3700 return 0;
3701}
3702
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003703STATIC int
3704xfs_btree_dec_cursor(
3705 struct xfs_btree_cur *cur,
3706 int level,
3707 int *stat)
3708{
3709 int error;
3710 int i;
3711
3712 if (level > 0) {
3713 error = xfs_btree_decrement(cur, level, &i);
3714 if (error)
3715 return error;
3716 }
3717
3718 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3719 *stat = 1;
3720 return 0;
3721}
3722
3723/*
3724 * Single level of the btree record deletion routine.
3725 * Delete record pointed to by cur/level.
3726 * Remove the record from its block then rebalance the tree.
3727 * Return 0 for error, 1 for done, 2 to go on to the next level.
3728 */
3729STATIC int /* error */
3730xfs_btree_delrec(
3731 struct xfs_btree_cur *cur, /* btree cursor */
3732 int level, /* level removing record from */
3733 int *stat) /* fail/done/go-on */
3734{
3735 struct xfs_btree_block *block; /* btree block */
3736 union xfs_btree_ptr cptr; /* current block ptr */
3737 struct xfs_buf *bp; /* buffer for block */
3738 int error; /* error return value */
3739 int i; /* loop counter */
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003740 union xfs_btree_ptr lptr; /* left sibling block ptr */
3741 struct xfs_buf *lbp; /* left buffer pointer */
3742 struct xfs_btree_block *left; /* left btree block */
3743 int lrecs = 0; /* left record count */
3744 int ptr; /* key/record index */
3745 union xfs_btree_ptr rptr; /* right sibling block ptr */
3746 struct xfs_buf *rbp; /* right buffer pointer */
3747 struct xfs_btree_block *right; /* right btree block */
3748 struct xfs_btree_block *rrblock; /* right-right btree block */
3749 struct xfs_buf *rrbp; /* right-right buffer pointer */
3750 int rrecs = 0; /* right record count */
3751 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3752 int numrecs; /* temporary numrec count */
3753
3754 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3755 XFS_BTREE_TRACE_ARGI(cur, level);
3756
3757 tcur = NULL;
3758
3759 /* Get the index of the entry being deleted, check for nothing there. */
3760 ptr = cur->bc_ptrs[level];
3761 if (ptr == 0) {
3762 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3763 *stat = 0;
3764 return 0;
3765 }
3766
3767 /* Get the buffer & block containing the record or key/ptr. */
3768 block = xfs_btree_get_block(cur, level, &bp);
3769 numrecs = xfs_btree_get_numrecs(block);
3770
3771#ifdef DEBUG
3772 error = xfs_btree_check_block(cur, block, level, bp);
3773 if (error)
3774 goto error0;
3775#endif
3776
3777 /* Fail if we're off the end of the block. */
3778 if (ptr > numrecs) {
3779 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3780 *stat = 0;
3781 return 0;
3782 }
3783
3784 XFS_BTREE_STATS_INC(cur, delrec);
3785 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3786
3787 /* Excise the entries being deleted. */
3788 if (level > 0) {
3789 /* It's a nonleaf. operate on keys and ptrs */
3790 union xfs_btree_key *lkp;
3791 union xfs_btree_ptr *lpp;
3792
3793 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3794 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3795
3796#ifdef DEBUG
3797 for (i = 0; i < numrecs - ptr; i++) {
3798 error = xfs_btree_check_ptr(cur, lpp, i, level);
3799 if (error)
3800 goto error0;
3801 }
3802#endif
3803
3804 if (ptr < numrecs) {
3805 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3806 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3807 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3808 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3809 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003810 } else {
3811 /* It's a leaf. operate on records */
3812 if (ptr < numrecs) {
3813 xfs_btree_shift_recs(cur,
3814 xfs_btree_rec_addr(cur, ptr + 1, block),
3815 -1, numrecs - ptr);
3816 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3817 }
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003818 }
3819
3820 /*
3821 * Decrement and log the number of entries in the block.
3822 */
3823 xfs_btree_set_numrecs(block, --numrecs);
3824 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3825
3826 /*
3827 * If we are tracking the last record in the tree and
3828 * we are at the far right edge of the tree, update it.
3829 */
3830 if (xfs_btree_is_lastrec(cur, block, level)) {
3831 cur->bc_ops->update_lastrec(cur, block, NULL,
3832 ptr, LASTREC_DELREC);
3833 }
3834
3835 /*
3836 * We're at the root level. First, shrink the root block in-memory.
3837 * Try to get rid of the next level down. If we can't then there's
3838 * nothing left to do.
3839 */
3840 if (level == cur->bc_nlevels - 1) {
3841 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3842 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3843 cur->bc_private.b.whichfork);
3844
3845 error = xfs_btree_kill_iroot(cur);
3846 if (error)
3847 goto error0;
3848
3849 error = xfs_btree_dec_cursor(cur, level, stat);
3850 if (error)
3851 goto error0;
3852 *stat = 1;
3853 return 0;
3854 }
3855
3856 /*
3857 * If this is the root level, and there's only one entry left,
3858 * and it's NOT the leaf level, then we can get rid of this
3859 * level.
3860 */
3861 if (numrecs == 1 && level > 0) {
3862 union xfs_btree_ptr *pp;
3863 /*
3864 * pp is still set to the first pointer in the block.
3865 * Make it the new root of the btree.
3866 */
3867 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003868 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003869 if (error)
3870 goto error0;
3871 } else if (level > 0) {
3872 error = xfs_btree_dec_cursor(cur, level, stat);
3873 if (error)
3874 goto error0;
3875 }
3876 *stat = 1;
3877 return 0;
3878 }
3879
3880 /*
3881 * If we deleted the leftmost entry in the block, update the
3882 * key values above us in the tree.
3883 */
Darrick J. Wong70b22652016-08-03 11:03:38 +10003884 if (xfs_btree_needs_key_update(cur, ptr)) {
Darrick J. Wong973b8312016-08-03 12:22:12 +10003885 error = xfs_btree_update_keys(cur, level);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003886 if (error)
3887 goto error0;
3888 }
3889
3890 /*
3891 * If the number of records remaining in the block is at least
3892 * the minimum, we're done.
3893 */
3894 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3895 error = xfs_btree_dec_cursor(cur, level, stat);
3896 if (error)
3897 goto error0;
3898 return 0;
3899 }
3900
3901 /*
3902 * Otherwise, we have to move some records around to keep the
3903 * tree balanced. Look at the left and right sibling blocks to
3904 * see if we can re-balance by moving only one record.
3905 */
3906 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3907 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3908
3909 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3910 /*
3911 * One child of root, need to get a chance to copy its contents
3912 * into the root and delete it. Can't go up to next level,
3913 * there's nothing to delete there.
3914 */
3915 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3916 xfs_btree_ptr_is_null(cur, &lptr) &&
3917 level == cur->bc_nlevels - 2) {
3918 error = xfs_btree_kill_iroot(cur);
3919 if (!error)
3920 error = xfs_btree_dec_cursor(cur, level, stat);
3921 if (error)
3922 goto error0;
3923 return 0;
3924 }
3925 }
3926
3927 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3928 !xfs_btree_ptr_is_null(cur, &lptr));
3929
3930 /*
3931 * Duplicate the cursor so our btree manipulations here won't
3932 * disrupt the next level up.
3933 */
3934 error = xfs_btree_dup_cursor(cur, &tcur);
3935 if (error)
3936 goto error0;
3937
3938 /*
3939 * If there's a right sibling, see if it's ok to shift an entry
3940 * out of it.
3941 */
3942 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3943 /*
3944 * Move the temp cursor to the last entry in the next block.
3945 * Actually any entry but the first would suffice.
3946 */
3947 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003948 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003949
3950 error = xfs_btree_increment(tcur, level, &i);
3951 if (error)
3952 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003953 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003954
3955 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003956 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003957
3958 /* Grab a pointer to the block. */
3959 right = xfs_btree_get_block(tcur, level, &rbp);
3960#ifdef DEBUG
3961 error = xfs_btree_check_block(tcur, right, level, rbp);
3962 if (error)
3963 goto error0;
3964#endif
3965 /* Grab the current block number, for future use. */
3966 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3967
3968 /*
3969 * If right block is full enough so that removing one entry
3970 * won't make it too empty, and left-shifting an entry out
3971 * of right to us works, we're done.
3972 */
3973 if (xfs_btree_get_numrecs(right) - 1 >=
3974 cur->bc_ops->get_minrecs(tcur, level)) {
3975 error = xfs_btree_lshift(tcur, level, &i);
3976 if (error)
3977 goto error0;
3978 if (i) {
3979 ASSERT(xfs_btree_get_numrecs(block) >=
3980 cur->bc_ops->get_minrecs(tcur, level));
3981
3982 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3983 tcur = NULL;
3984
3985 error = xfs_btree_dec_cursor(cur, level, stat);
3986 if (error)
3987 goto error0;
3988 return 0;
3989 }
3990 }
3991
3992 /*
3993 * Otherwise, grab the number of records in right for
3994 * future reference, and fix up the temp cursor to point
3995 * to our block again (last record).
3996 */
3997 rrecs = xfs_btree_get_numrecs(right);
3998 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3999 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11004000 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004001
4002 error = xfs_btree_decrement(tcur, level, &i);
4003 if (error)
4004 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004005 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004006 }
4007 }
4008
4009 /*
4010 * If there's a left sibling, see if it's ok to shift an entry
4011 * out of it.
4012 */
4013 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
4014 /*
4015 * Move the temp cursor to the first entry in the
4016 * previous block.
4017 */
4018 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11004019 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004020
4021 error = xfs_btree_decrement(tcur, level, &i);
4022 if (error)
4023 goto error0;
4024 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11004025 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004026
4027 /* Grab a pointer to the block. */
4028 left = xfs_btree_get_block(tcur, level, &lbp);
4029#ifdef DEBUG
4030 error = xfs_btree_check_block(cur, left, level, lbp);
4031 if (error)
4032 goto error0;
4033#endif
4034 /* Grab the current block number, for future use. */
4035 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
4036
4037 /*
4038 * If left block is full enough so that removing one entry
4039 * won't make it too empty, and right-shifting an entry out
4040 * of left to us works, we're done.
4041 */
4042 if (xfs_btree_get_numrecs(left) - 1 >=
4043 cur->bc_ops->get_minrecs(tcur, level)) {
4044 error = xfs_btree_rshift(tcur, level, &i);
4045 if (error)
4046 goto error0;
4047 if (i) {
4048 ASSERT(xfs_btree_get_numrecs(block) >=
4049 cur->bc_ops->get_minrecs(tcur, level));
4050 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4051 tcur = NULL;
4052 if (level == 0)
4053 cur->bc_ptrs[0]++;
4054 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4055 *stat = 1;
4056 return 0;
4057 }
4058 }
4059
4060 /*
4061 * Otherwise, grab the number of records in right for
4062 * future reference.
4063 */
4064 lrecs = xfs_btree_get_numrecs(left);
4065 }
4066
4067 /* Delete the temp cursor, we're done with it. */
4068 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
4069 tcur = NULL;
4070
4071 /* If here, we need to do a join to keep the tree balanced. */
4072 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
4073
4074 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
4075 lrecs + xfs_btree_get_numrecs(block) <=
4076 cur->bc_ops->get_maxrecs(cur, level)) {
4077 /*
4078 * Set "right" to be the starting block,
4079 * "left" to be the left neighbor.
4080 */
4081 rptr = cptr;
4082 right = block;
4083 rbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004084 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004085 if (error)
4086 goto error0;
4087
4088 /*
4089 * If that won't work, see if we can join with the right neighbor block.
4090 */
4091 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
4092 rrecs + xfs_btree_get_numrecs(block) <=
4093 cur->bc_ops->get_maxrecs(cur, level)) {
4094 /*
4095 * Set "left" to be the starting block,
4096 * "right" to be the right neighbor.
4097 */
4098 lptr = cptr;
4099 left = block;
4100 lbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004101 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004102 if (error)
4103 goto error0;
4104
4105 /*
4106 * Otherwise, we can't fix the imbalance.
4107 * Just return. This is probably a logic error, but it's not fatal.
4108 */
4109 } else {
4110 error = xfs_btree_dec_cursor(cur, level, stat);
4111 if (error)
4112 goto error0;
4113 return 0;
4114 }
4115
4116 rrecs = xfs_btree_get_numrecs(right);
4117 lrecs = xfs_btree_get_numrecs(left);
4118
4119 /*
4120 * We're now going to join "left" and "right" by moving all the stuff
4121 * in "right" to "left" and deleting "right".
4122 */
4123 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
4124 if (level > 0) {
4125 /* It's a non-leaf. Move keys and pointers. */
4126 union xfs_btree_key *lkp; /* left btree key */
4127 union xfs_btree_ptr *lpp; /* left address pointer */
4128 union xfs_btree_key *rkp; /* right btree key */
4129 union xfs_btree_ptr *rpp; /* right address pointer */
4130
4131 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
4132 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
4133 rkp = xfs_btree_key_addr(cur, 1, right);
4134 rpp = xfs_btree_ptr_addr(cur, 1, right);
4135#ifdef DEBUG
4136 for (i = 1; i < rrecs; i++) {
4137 error = xfs_btree_check_ptr(cur, rpp, i, level);
4138 if (error)
4139 goto error0;
4140 }
4141#endif
4142 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
4143 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
4144
4145 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
4146 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
4147 } else {
4148 /* It's a leaf. Move records. */
4149 union xfs_btree_rec *lrp; /* left record pointer */
4150 union xfs_btree_rec *rrp; /* right record pointer */
4151
4152 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
4153 rrp = xfs_btree_rec_addr(cur, 1, right);
4154
4155 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
4156 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
4157 }
4158
4159 XFS_BTREE_STATS_INC(cur, join);
4160
4161 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02004162 * Fix up the number of records and right block pointer in the
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004163 * surviving block, and log it.
4164 */
4165 xfs_btree_set_numrecs(left, lrecs + rrecs);
4166 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
4167 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4168 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
4169
4170 /* If there is a right sibling, point it to the remaining block. */
4171 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
4172 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10004173 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004174 if (error)
4175 goto error0;
4176 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
4177 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
4178 }
4179
4180 /* Free the deleted block. */
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11004181 error = xfs_btree_free_block(cur, rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004182 if (error)
4183 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004184
4185 /*
4186 * If we joined with the left neighbor, set the buffer in the
4187 * cursor to the left block, and fix up the index.
4188 */
4189 if (bp != lbp) {
4190 cur->bc_bufs[level] = lbp;
4191 cur->bc_ptrs[level] += lrecs;
4192 cur->bc_ra[level] = 0;
4193 }
4194 /*
4195 * If we joined with the right neighbor and there's a level above
4196 * us, increment the cursor at that level.
4197 */
4198 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
4199 (level + 1 < cur->bc_nlevels)) {
4200 error = xfs_btree_increment(cur, level + 1, &i);
4201 if (error)
4202 goto error0;
4203 }
4204
4205 /*
4206 * Readjust the ptr at this level if it's not a leaf, since it's
4207 * still pointing at the deletion point, which makes the cursor
4208 * inconsistent. If this makes the ptr 0, the caller fixes it up.
4209 * We can't use decrement because it would change the next level up.
4210 */
4211 if (level > 0)
4212 cur->bc_ptrs[level]--;
4213
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004214 /*
4215 * We combined blocks, so we have to update the parent keys if the
4216 * btree supports overlapped intervals. However, bc_ptrs[level + 1]
4217 * points to the old block so that the caller knows which record to
4218 * delete. Therefore, the caller must be savvy enough to call updkeys
4219 * for us if we return stat == 2. The other exit points from this
4220 * function don't require deletions further up the tree, so they can
4221 * call updkeys directly.
4222 */
4223
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004224 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4225 /* Return value means the next level up has something to do. */
4226 *stat = 2;
4227 return 0;
4228
4229error0:
4230 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4231 if (tcur)
4232 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
4233 return error;
4234}
4235
4236/*
4237 * Delete the record pointed to by cur.
4238 * The cursor refers to the place where the record was (could be inserted)
4239 * when the operation returns.
4240 */
4241int /* error */
4242xfs_btree_delete(
4243 struct xfs_btree_cur *cur,
4244 int *stat) /* success/failure */
4245{
4246 int error; /* error return value */
4247 int level;
4248 int i;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004249 bool joined = false;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004250
4251 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
4252
4253 /*
4254 * Go up the tree, starting at leaf level.
4255 *
4256 * If 2 is returned then a join was done; go to the next level.
4257 * Otherwise we are done.
4258 */
4259 for (level = 0, i = 2; i == 2; level++) {
4260 error = xfs_btree_delrec(cur, level, &i);
4261 if (error)
4262 goto error0;
Darrick J. Wong2c813ad2016-08-03 11:08:36 +10004263 if (i == 2)
4264 joined = true;
4265 }
4266
4267 /*
4268 * If we combined blocks as part of deleting the record, delrec won't
4269 * have updated the parent high keys so we have to do that here.
4270 */
4271 if (joined && (cur->bc_flags & XFS_BTREE_OVERLAPPING)) {
4272 error = xfs_btree_updkeys_force(cur, 0);
4273 if (error)
4274 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11004275 }
4276
4277 if (i == 0) {
4278 for (level = 1; level < cur->bc_nlevels; level++) {
4279 if (cur->bc_ptrs[level] == 0) {
4280 error = xfs_btree_decrement(cur, level, &i);
4281 if (error)
4282 goto error0;
4283 break;
4284 }
4285 }
4286 }
4287
4288 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
4289 *stat = i;
4290 return 0;
4291error0:
4292 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
4293 return error;
4294}
Christoph Hellwig8cc938f2008-10-30 16:58:11 +11004295
4296/*
4297 * Get the data from the pointed-to record.
4298 */
4299int /* error */
4300xfs_btree_get_rec(
4301 struct xfs_btree_cur *cur, /* btree cursor */
4302 union xfs_btree_rec **recp, /* output: btree record */
4303 int *stat) /* output: success/failure */
4304{
4305 struct xfs_btree_block *block; /* btree block */
4306 struct xfs_buf *bp; /* buffer pointer */
4307 int ptr; /* record number */
4308#ifdef DEBUG
4309 int error; /* error return value */
4310#endif
4311
4312 ptr = cur->bc_ptrs[0];
4313 block = xfs_btree_get_block(cur, 0, &bp);
4314
4315#ifdef DEBUG
4316 error = xfs_btree_check_block(cur, block, 0, bp);
4317 if (error)
4318 return error;
4319#endif
4320
4321 /*
4322 * Off the right end or left end, return failure.
4323 */
4324 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
4325 *stat = 0;
4326 return 0;
4327 }
4328
4329 /*
4330 * Point to the record and extract its data.
4331 */
4332 *recp = xfs_btree_rec_addr(cur, ptr, block);
4333 *stat = 1;
4334 return 0;
4335}
Dave Chinner21b5c972013-08-30 10:23:44 +10004336
Darrick J. Wong28a89562016-08-03 11:10:55 +10004337/* Visit a block in a btree. */
4338STATIC int
4339xfs_btree_visit_block(
4340 struct xfs_btree_cur *cur,
4341 int level,
4342 xfs_btree_visit_blocks_fn fn,
4343 void *data)
4344{
4345 struct xfs_btree_block *block;
4346 struct xfs_buf *bp;
4347 union xfs_btree_ptr rptr;
4348 int error;
4349
4350 /* do right sibling readahead */
4351 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4352 block = xfs_btree_get_block(cur, level, &bp);
4353
4354 /* process the block */
4355 error = fn(cur, level, data);
4356 if (error)
4357 return error;
4358
4359 /* now read rh sibling block for next iteration */
4360 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4361 if (xfs_btree_ptr_is_null(cur, &rptr))
4362 return -ENOENT;
4363
4364 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4365}
4366
4367
4368/* Visit every block in a btree. */
4369int
4370xfs_btree_visit_blocks(
4371 struct xfs_btree_cur *cur,
4372 xfs_btree_visit_blocks_fn fn,
4373 void *data)
4374{
4375 union xfs_btree_ptr lptr;
4376 int level;
4377 struct xfs_btree_block *block = NULL;
4378 int error = 0;
4379
4380 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4381
4382 /* for each level */
4383 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4384 /* grab the left hand block */
4385 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4386 if (error)
4387 return error;
4388
4389 /* readahead the left most block for the next level down */
4390 if (level > 0) {
4391 union xfs_btree_ptr *ptr;
4392
4393 ptr = xfs_btree_ptr_addr(cur, 1, block);
4394 xfs_btree_readahead_ptr(cur, ptr, 1);
4395
4396 /* save for the next iteration of the loop */
4397 lptr = *ptr;
4398 }
4399
4400 /* for each buffer in the level */
4401 do {
4402 error = xfs_btree_visit_block(cur, level, fn, data);
4403 } while (!error);
4404
4405 if (error != -ENOENT)
4406 return error;
4407 }
4408
4409 return 0;
4410}
4411
Dave Chinner21b5c972013-08-30 10:23:44 +10004412/*
4413 * Change the owner of a btree.
4414 *
4415 * The mechanism we use here is ordered buffer logging. Because we don't know
4416 * how many buffers were are going to need to modify, we don't really want to
4417 * have to make transaction reservations for the worst case of every buffer in a
4418 * full size btree as that may be more space that we can fit in the log....
4419 *
4420 * We do the btree walk in the most optimal manner possible - we have sibling
4421 * pointers so we can just walk all the blocks on each level from left to right
4422 * in a single pass, and then move to the next level and do the same. We can
4423 * also do readahead on the sibling pointers to get IO moving more quickly,
4424 * though for slow disks this is unlikely to make much difference to performance
4425 * as the amount of CPU work we have to do before moving to the next block is
4426 * relatively small.
4427 *
4428 * For each btree block that we load, modify the owner appropriately, set the
4429 * buffer as an ordered buffer and log it appropriately. We need to ensure that
4430 * we mark the region we change dirty so that if the buffer is relogged in
4431 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10004432 * correctly relogged in that transaction. If we are in recovery context, then
4433 * just queue the modified buffer as delayed write buffer so the transaction
4434 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10004435 */
Darrick J. Wong28a89562016-08-03 11:10:55 +10004436struct xfs_btree_block_change_owner_info {
4437 __uint64_t new_owner;
4438 struct list_head *buffer_list;
4439};
4440
Dave Chinner21b5c972013-08-30 10:23:44 +10004441static int
4442xfs_btree_block_change_owner(
4443 struct xfs_btree_cur *cur,
4444 int level,
Darrick J. Wong28a89562016-08-03 11:10:55 +10004445 void *data)
Dave Chinner21b5c972013-08-30 10:23:44 +10004446{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004447 struct xfs_btree_block_change_owner_info *bbcoi = data;
Dave Chinner21b5c972013-08-30 10:23:44 +10004448 struct xfs_btree_block *block;
4449 struct xfs_buf *bp;
Dave Chinner21b5c972013-08-30 10:23:44 +10004450
4451 /* modify the owner */
4452 block = xfs_btree_get_block(cur, level, &bp);
4453 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Darrick J. Wong28a89562016-08-03 11:10:55 +10004454 block->bb_u.l.bb_owner = cpu_to_be64(bbcoi->new_owner);
Dave Chinner21b5c972013-08-30 10:23:44 +10004455 else
Darrick J. Wong28a89562016-08-03 11:10:55 +10004456 block->bb_u.s.bb_owner = cpu_to_be32(bbcoi->new_owner);
Dave Chinner21b5c972013-08-30 10:23:44 +10004457
4458 /*
Dave Chinner638f44162013-08-30 10:23:45 +10004459 * If the block is a root block hosted in an inode, we might not have a
4460 * buffer pointer here and we shouldn't attempt to log the change as the
4461 * information is already held in the inode and discarded when the root
4462 * block is formatted into the on-disk inode fork. We still change it,
4463 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10004464 */
4465 if (bp) {
Dave Chinner638f44162013-08-30 10:23:45 +10004466 if (cur->bc_tp) {
4467 xfs_trans_ordered_buf(cur->bc_tp, bp);
4468 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4469 } else {
Darrick J. Wong28a89562016-08-03 11:10:55 +10004470 xfs_buf_delwri_queue(bp, bbcoi->buffer_list);
Dave Chinner638f44162013-08-30 10:23:45 +10004471 }
Dave Chinner21b5c972013-08-30 10:23:44 +10004472 } else {
4473 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4474 ASSERT(level == cur->bc_nlevels - 1);
4475 }
4476
Darrick J. Wong28a89562016-08-03 11:10:55 +10004477 return 0;
Dave Chinner21b5c972013-08-30 10:23:44 +10004478}
4479
4480int
4481xfs_btree_change_owner(
4482 struct xfs_btree_cur *cur,
Dave Chinner638f44162013-08-30 10:23:45 +10004483 __uint64_t new_owner,
4484 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004485{
Darrick J. Wong28a89562016-08-03 11:10:55 +10004486 struct xfs_btree_block_change_owner_info bbcoi;
Dave Chinner21b5c972013-08-30 10:23:44 +10004487
Darrick J. Wong28a89562016-08-03 11:10:55 +10004488 bbcoi.new_owner = new_owner;
4489 bbcoi.buffer_list = buffer_list;
Dave Chinner21b5c972013-08-30 10:23:44 +10004490
Darrick J. Wong28a89562016-08-03 11:10:55 +10004491 return xfs_btree_visit_blocks(cur, xfs_btree_block_change_owner,
4492 &bbcoi);
Dave Chinner21b5c972013-08-30 10:23:44 +10004493}
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004494
4495/**
4496 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4497 * btree block
4498 *
4499 * @bp: buffer containing the btree block
4500 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4501 * @pag_max_level: pointer to the per-ag max level field
4502 */
4503bool
4504xfs_btree_sblock_v5hdr_verify(
4505 struct xfs_buf *bp)
4506{
4507 struct xfs_mount *mp = bp->b_target->bt_mount;
4508 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4509 struct xfs_perag *pag = bp->b_pag;
4510
4511 if (!xfs_sb_version_hascrc(&mp->m_sb))
4512 return false;
4513 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4514 return false;
4515 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4516 return false;
4517 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4518 return false;
4519 return true;
4520}
4521
4522/**
4523 * xfs_btree_sblock_verify() -- verify a short-format btree block
4524 *
4525 * @bp: buffer containing the btree block
4526 * @max_recs: maximum records allowed in this btree node
4527 */
4528bool
4529xfs_btree_sblock_verify(
4530 struct xfs_buf *bp,
4531 unsigned int max_recs)
4532{
4533 struct xfs_mount *mp = bp->b_target->bt_mount;
4534 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4535
4536 /* numrecs verification */
4537 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4538 return false;
4539
4540 /* sibling pointer verification */
4541 if (!block->bb_u.s.bb_leftsib ||
4542 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
4543 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
4544 return false;
4545 if (!block->bb_u.s.bb_rightsib ||
4546 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
4547 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
4548 return false;
4549
4550 return true;
4551}
Darrick J. Wong19b54ee2016-06-21 11:53:28 +10004552
4553/*
4554 * Calculate the number of btree levels needed to store a given number of
4555 * records in a short-format btree.
4556 */
4557uint
4558xfs_btree_compute_maxlevels(
4559 struct xfs_mount *mp,
4560 uint *limits,
4561 unsigned long len)
4562{
4563 uint level;
4564 unsigned long maxblocks;
4565
4566 maxblocks = (len + limits[0] - 1) / limits[0];
4567 for (level = 1; maxblocks > 1; level++)
4568 maxblocks = (maxblocks + limits[1] - 1) / limits[1];
4569 return level;
4570}
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004571
4572/*
4573 * Query a regular btree for all records overlapping a given interval.
4574 * Start with a LE lookup of the key of low_rec and return all records
4575 * until we find a record with a key greater than the key of high_rec.
4576 */
4577STATIC int
4578xfs_btree_simple_query_range(
4579 struct xfs_btree_cur *cur,
4580 union xfs_btree_key *low_key,
4581 union xfs_btree_key *high_key,
4582 xfs_btree_query_range_fn fn,
4583 void *priv)
4584{
4585 union xfs_btree_rec *recp;
4586 union xfs_btree_key rec_key;
4587 __int64_t diff;
4588 int stat;
4589 bool firstrec = true;
4590 int error;
4591
4592 ASSERT(cur->bc_ops->init_high_key_from_rec);
4593 ASSERT(cur->bc_ops->diff_two_keys);
4594
4595 /*
4596 * Find the leftmost record. The btree cursor must be set
4597 * to the low record used to generate low_key.
4598 */
4599 stat = 0;
4600 error = xfs_btree_lookup(cur, XFS_LOOKUP_LE, &stat);
4601 if (error)
4602 goto out;
4603
Darrick J. Wong5b5c2db2016-08-26 16:00:10 +10004604 /* Nothing? See if there's anything to the right. */
4605 if (!stat) {
4606 error = xfs_btree_increment(cur, 0, &stat);
4607 if (error)
4608 goto out;
4609 }
4610
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004611 while (stat) {
4612 /* Find the record. */
4613 error = xfs_btree_get_rec(cur, &recp, &stat);
4614 if (error || !stat)
4615 break;
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004616
4617 /* Skip if high_key(rec) < low_key. */
4618 if (firstrec) {
Darrick J. Wong72227892016-08-26 15:59:50 +10004619 cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004620 firstrec = false;
4621 diff = cur->bc_ops->diff_two_keys(cur, low_key,
4622 &rec_key);
4623 if (diff > 0)
4624 goto advloop;
4625 }
4626
4627 /* Stop if high_key < low_key(rec). */
Darrick J. Wong72227892016-08-26 15:59:50 +10004628 cur->bc_ops->init_key_from_rec(&rec_key, recp);
Darrick J. Wong105f7d82016-08-03 11:10:21 +10004629 diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4630 if (diff > 0)
4631 break;
4632
4633 /* Callback */
4634 error = fn(cur, recp, priv);
4635 if (error < 0 || error == XFS_BTREE_QUERY_RANGE_ABORT)
4636 break;
4637
4638advloop:
4639 /* Move on to the next record. */
4640 error = xfs_btree_increment(cur, 0, &stat);
4641 if (error)
4642 break;
4643 }
4644
4645out:
4646 return error;
4647}
4648
4649/*
4650 * Query an overlapped interval btree for all records overlapping a given
4651 * interval. This function roughly follows the algorithm given in
4652 * "Interval Trees" of _Introduction to Algorithms_, which is section
4653 * 14.3 in the 2nd and 3rd editions.
4654 *
4655 * First, generate keys for the low and high records passed in.
4656 *
4657 * For any leaf node, generate the high and low keys for the record.
4658 * If the record keys overlap with the query low/high keys, pass the
4659 * record to the function iterator.
4660 *
4661 * For any internal node, compare the low and high keys of each
4662 * pointer against the query low/high keys. If there's an overlap,
4663 * follow the pointer.
4664 *
4665 * As an optimization, we stop scanning a block when we find a low key
4666 * that is greater than the query's high key.
4667 */
4668STATIC int
4669xfs_btree_overlapped_query_range(
4670 struct xfs_btree_cur *cur,
4671 union xfs_btree_key *low_key,
4672 union xfs_btree_key *high_key,
4673 xfs_btree_query_range_fn fn,
4674 void *priv)
4675{
4676 union xfs_btree_ptr ptr;
4677 union xfs_btree_ptr *pp;
4678 union xfs_btree_key rec_key;
4679 union xfs_btree_key rec_hkey;
4680 union xfs_btree_key *lkp;
4681 union xfs_btree_key *hkp;
4682 union xfs_btree_rec *recp;
4683 struct xfs_btree_block *block;
4684 __int64_t ldiff;
4685 __int64_t hdiff;
4686 int level;
4687 struct xfs_buf *bp;
4688 int i;
4689 int error;
4690
4691 /* Load the root of the btree. */
4692 level = cur->bc_nlevels - 1;
4693 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
4694 error = xfs_btree_lookup_get_block(cur, level, &ptr, &block);
4695 if (error)
4696 return error;
4697 xfs_btree_get_block(cur, level, &bp);
4698 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4699#ifdef DEBUG
4700 error = xfs_btree_check_block(cur, block, level, bp);
4701 if (error)
4702 goto out;
4703#endif
4704 cur->bc_ptrs[level] = 1;
4705
4706 while (level < cur->bc_nlevels) {
4707 block = xfs_btree_get_block(cur, level, &bp);
4708
4709 /* End of node, pop back towards the root. */
4710 if (cur->bc_ptrs[level] > be16_to_cpu(block->bb_numrecs)) {
4711pop_up:
4712 if (level < cur->bc_nlevels - 1)
4713 cur->bc_ptrs[level + 1]++;
4714 level++;
4715 continue;
4716 }
4717
4718 if (level == 0) {
4719 /* Handle a leaf node. */
4720 recp = xfs_btree_rec_addr(cur, cur->bc_ptrs[0], block);
4721
4722 cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4723 ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4724 low_key);
4725
4726 cur->bc_ops->init_key_from_rec(&rec_key, recp);
4727 hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4728 &rec_key);
4729
4730 /*
4731 * If (record's high key >= query's low key) and
4732 * (query's high key >= record's low key), then
4733 * this record overlaps the query range; callback.
4734 */
4735 if (ldiff >= 0 && hdiff >= 0) {
4736 error = fn(cur, recp, priv);
4737 if (error < 0 ||
4738 error == XFS_BTREE_QUERY_RANGE_ABORT)
4739 break;
4740 } else if (hdiff < 0) {
4741 /* Record is larger than high key; pop. */
4742 goto pop_up;
4743 }
4744 cur->bc_ptrs[level]++;
4745 continue;
4746 }
4747
4748 /* Handle an internal node. */
4749 lkp = xfs_btree_key_addr(cur, cur->bc_ptrs[level], block);
4750 hkp = xfs_btree_high_key_addr(cur, cur->bc_ptrs[level], block);
4751 pp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[level], block);
4752
4753 ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4754 hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4755
4756 /*
4757 * If (pointer's high key >= query's low key) and
4758 * (query's high key >= pointer's low key), then
4759 * this record overlaps the query range; follow pointer.
4760 */
4761 if (ldiff >= 0 && hdiff >= 0) {
4762 level--;
4763 error = xfs_btree_lookup_get_block(cur, level, pp,
4764 &block);
4765 if (error)
4766 goto out;
4767 xfs_btree_get_block(cur, level, &bp);
4768 trace_xfs_btree_overlapped_query_range(cur, level, bp);
4769#ifdef DEBUG
4770 error = xfs_btree_check_block(cur, block, level, bp);
4771 if (error)
4772 goto out;
4773#endif
4774 cur->bc_ptrs[level] = 1;
4775 continue;
4776 } else if (hdiff < 0) {
4777 /* The low key is larger than the upper range; pop. */
4778 goto pop_up;
4779 }
4780 cur->bc_ptrs[level]++;
4781 }
4782
4783out:
4784 /*
4785 * If we don't end this function with the cursor pointing at a record
4786 * block, a subsequent non-error cursor deletion will not release
4787 * node-level buffers, causing a buffer leak. This is quite possible
4788 * with a zero-results range query, so release the buffers if we
4789 * failed to return any results.
4790 */
4791 if (cur->bc_bufs[0] == NULL) {
4792 for (i = 0; i < cur->bc_nlevels; i++) {
4793 if (cur->bc_bufs[i]) {
4794 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
4795 cur->bc_bufs[i] = NULL;
4796 cur->bc_ptrs[i] = 0;
4797 cur->bc_ra[i] = 0;
4798 }
4799 }
4800 }
4801
4802 return error;
4803}
4804
4805/*
4806 * Query a btree for all records overlapping a given interval of keys. The
4807 * supplied function will be called with each record found; return one of the
4808 * XFS_BTREE_QUERY_RANGE_{CONTINUE,ABORT} values or the usual negative error
4809 * code. This function returns XFS_BTREE_QUERY_RANGE_ABORT, zero, or a
4810 * negative error code.
4811 */
4812int
4813xfs_btree_query_range(
4814 struct xfs_btree_cur *cur,
4815 union xfs_btree_irec *low_rec,
4816 union xfs_btree_irec *high_rec,
4817 xfs_btree_query_range_fn fn,
4818 void *priv)
4819{
4820 union xfs_btree_rec rec;
4821 union xfs_btree_key low_key;
4822 union xfs_btree_key high_key;
4823
4824 /* Find the keys of both ends of the interval. */
4825 cur->bc_rec = *high_rec;
4826 cur->bc_ops->init_rec_from_cur(cur, &rec);
4827 cur->bc_ops->init_key_from_rec(&high_key, &rec);
4828
4829 cur->bc_rec = *low_rec;
4830 cur->bc_ops->init_rec_from_cur(cur, &rec);
4831 cur->bc_ops->init_key_from_rec(&low_key, &rec);
4832
4833 /* Enforce low key < high key. */
4834 if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4835 return -EINVAL;
4836
4837 if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
4838 return xfs_btree_simple_query_range(cur, &low_key,
4839 &high_key, fn, priv);
4840 return xfs_btree_overlapped_query_range(cur, &low_key, &high_key,
4841 fn, priv);
4842}
Darrick J. Wong4ed3f682016-09-19 10:25:03 +10004843
4844/*
4845 * Calculate the number of blocks needed to store a given number of records
4846 * in a short-format (per-AG metadata) btree.
4847 */
4848xfs_extlen_t
4849xfs_btree_calc_size(
4850 struct xfs_mount *mp,
4851 uint *limits,
4852 unsigned long long len)
4853{
4854 int level;
4855 int maxrecs;
4856 xfs_extlen_t rval;
4857
4858 maxrecs = limits[0];
4859 for (level = 0, rval = 0; len > 1; level++) {
4860 len += maxrecs - 1;
4861 do_div(len, maxrecs);
4862 maxrecs = limits[1];
4863 rval += len;
4864 }
4865 return rval;
4866}
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004867
Eric Biggersf1b82432016-10-20 15:42:30 +11004868static int
Darrick J. Wongc611cc02016-09-19 10:25:20 +10004869xfs_btree_count_blocks_helper(
4870 struct xfs_btree_cur *cur,
4871 int level,
4872 void *data)
4873{
4874 xfs_extlen_t *blocks = data;
4875 (*blocks)++;
4876
4877 return 0;
4878}
4879
4880/* Count the blocks in a btree and return the result in *blocks. */
4881int
4882xfs_btree_count_blocks(
4883 struct xfs_btree_cur *cur,
4884 xfs_extlen_t *blocks)
4885{
4886 *blocks = 0;
4887 return xfs_btree_visit_blocks(cur, xfs_btree_count_blocks_helper,
4888 blocks);
4889}