blob: 77afb4a899b9d2fea1c08dc75d1a50879a0a650e [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
Christoph Hellwig38bb7422008-10-30 16:56:22 +110028#include "xfs_inode_item.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050029#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000032#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050033#include "xfs_cksum.h"
Dave Chinnercf11da92014-07-15 07:08:24 +100034#include "xfs_alloc.h"
Brian Fostera45086e2015-10-12 15:59:25 +110035#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Cursor allocation zone.
39 */
40kmem_zone_t *xfs_btree_cur_zone;
41
42/*
43 * Btree magic numbers.
44 */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050045static const __uint32_t xfs_magics[2][XFS_BTNUM_MAX] = {
Brian Fosteraafc3c22014-04-24 16:00:52 +100046 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC,
47 XFS_FIBT_MAGIC },
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050048 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
Brian Fosteraafc3c22014-04-24 16:00:52 +100049 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC, XFS_FIBT_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050051#define xfs_btree_magic(cur) \
52 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110055STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110056xfs_btree_check_lblock(
57 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110058 struct xfs_btree_block *block, /* btree long form block pointer */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110059 int level, /* level of the btree block */
60 struct xfs_buf *bp) /* buffer for block, if any */
61{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050062 int lblock_ok = 1; /* block passes checks */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110063 struct xfs_mount *mp; /* file system mount point */
64
65 mp = cur->bc_mp;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050066
67 if (xfs_sb_version_hascrc(&mp->m_sb)) {
68 lblock_ok = lblock_ok &&
Eric Sandeence748ea2015-07-29 11:53:31 +100069 uuid_equal(&block->bb_u.l.bb_uuid,
70 &mp->m_sb.sb_meta_uuid) &&
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050071 block->bb_u.l.bb_blkno == cpu_to_be64(
72 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
73 }
74
75 lblock_ok = lblock_ok &&
76 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110077 be16_to_cpu(block->bb_level) == level &&
78 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +110079 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110080 block->bb_u.l.bb_leftsib &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100081 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110082 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050083 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110084 block->bb_u.l.bb_rightsib &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +100085 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110086 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050087 be64_to_cpu(block->bb_u.l.bb_rightsib)));
88
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110089 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
90 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
91 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
92 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000093 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050094 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +100095 return -EFSCORRUPTED;
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110096 }
97 return 0;
98}
99
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100100STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101xfs_btree_check_sblock(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100102 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100103 struct xfs_btree_block *block, /* btree short form block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int level, /* level of the btree block */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100105 struct xfs_buf *bp) /* buffer containing block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500107 struct xfs_mount *mp; /* file system mount point */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100108 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
109 struct xfs_agf *agf; /* ag. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 xfs_agblock_t agflen; /* native ag. freespace length */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500111 int sblock_ok = 1; /* block passes checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500113 mp = cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 agbp = cur->bc_private.a.agbp;
115 agf = XFS_BUF_TO_AGF(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100116 agflen = be32_to_cpu(agf->agf_length);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500117
118 if (xfs_sb_version_hascrc(&mp->m_sb)) {
119 sblock_ok = sblock_ok &&
Eric Sandeence748ea2015-07-29 11:53:31 +1000120 uuid_equal(&block->bb_u.s.bb_uuid,
121 &mp->m_sb.sb_meta_uuid) &&
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500122 block->bb_u.s.bb_blkno == cpu_to_be64(
123 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
124 }
125
126 sblock_ok = sblock_ok &&
127 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100128 be16_to_cpu(block->bb_level) == level &&
129 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100130 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200131 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100132 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
133 block->bb_u.s.bb_leftsib &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200134 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100135 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
136 block->bb_u.s.bb_rightsib;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500137
138 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
140 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
141 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000142 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500143 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000144 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 return 0;
147}
148
149/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100150 * Debug routine: check that block header is ok.
151 */
152int
153xfs_btree_check_block(
154 struct xfs_btree_cur *cur, /* btree cursor */
155 struct xfs_btree_block *block, /* generic btree block pointer */
156 int level, /* level of the btree block */
157 struct xfs_buf *bp) /* buffer containing block, if any */
158{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100159 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
160 return xfs_btree_check_lblock(cur, block, level, bp);
161 else
162 return xfs_btree_check_sblock(cur, block, level, bp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100163}
164
165/*
166 * Check that (long) pointer is ok.
167 */
168int /* error (0 or EFSCORRUPTED) */
169xfs_btree_check_lptr(
170 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000171 xfs_fsblock_t bno, /* btree block disk address */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100172 int level) /* btree block level */
173{
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100174 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100175 level > 0 &&
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000176 bno != NULLFSBLOCK &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100177 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
178 return 0;
179}
180
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100181#ifdef DEBUG
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100182/*
183 * Check that (short) pointer is ok.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100185STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186xfs_btree_check_sptr(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100187 struct xfs_btree_cur *cur, /* btree cursor */
188 xfs_agblock_t bno, /* btree block disk address */
189 int level) /* btree block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100191 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric Sandeen5fb5aee2015-02-23 22:39:13 +1100193 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 level > 0 &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100195 bno != NULLAGBLOCK &&
196 bno != 0 &&
197 bno < agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return 0;
199}
200
201/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100202 * Check that block ptr is ok.
203 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100204STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100205xfs_btree_check_ptr(
206 struct xfs_btree_cur *cur, /* btree cursor */
207 union xfs_btree_ptr *ptr, /* btree block disk address */
208 int index, /* offset from ptr to check */
209 int level) /* btree block level */
210{
211 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
212 return xfs_btree_check_lptr(cur,
213 be64_to_cpu((&ptr->l)[index]), level);
214 } else {
215 return xfs_btree_check_sptr(cur,
216 be32_to_cpu((&ptr->s)[index]), level);
217 }
218}
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100219#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100220
221/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500222 * Calculate CRC on the whole btree block and stuff it into the
223 * long-form btree header.
224 *
225 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100226 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500227 * it to disk.
228 */
229void
230xfs_btree_lblock_calc_crc(
231 struct xfs_buf *bp)
232{
233 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
234 struct xfs_buf_log_item *bip = bp->b_fspriv;
235
236 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
237 return;
238 if (bip)
239 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100240 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500241}
242
243bool
244xfs_btree_lblock_verify_crc(
245 struct xfs_buf *bp)
246{
Brian Fostera45086e2015-10-12 15:59:25 +1100247 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
248 struct xfs_mount *mp = bp->b_target->bt_mount;
249
250 if (xfs_sb_version_hascrc(&mp->m_sb)) {
251 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.l.bb_lsn)))
252 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100253 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100254 }
Eric Sandeen51582172014-02-27 15:17:27 +1100255
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500256 return true;
257}
258
259/*
260 * Calculate CRC on the whole btree block and stuff it into the
261 * short-form btree header.
262 *
263 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
Geliang Tangfef4ded2015-10-12 16:02:32 +1100264 * it into the buffer so recovery knows what the last modification was that made
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500265 * it to disk.
266 */
267void
268xfs_btree_sblock_calc_crc(
269 struct xfs_buf *bp)
270{
271 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
272 struct xfs_buf_log_item *bip = bp->b_fspriv;
273
274 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
275 return;
276 if (bip)
277 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100278 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500279}
280
281bool
282xfs_btree_sblock_verify_crc(
283 struct xfs_buf *bp)
284{
Brian Fostera45086e2015-10-12 15:59:25 +1100285 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
286 struct xfs_mount *mp = bp->b_target->bt_mount;
287
288 if (xfs_sb_version_hascrc(&mp->m_sb)) {
289 if (!xfs_log_check_lsn(mp, be64_to_cpu(block->bb_u.s.bb_lsn)))
290 return false;
Eric Sandeen51582172014-02-27 15:17:27 +1100291 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Brian Fostera45086e2015-10-12 15:59:25 +1100292 }
Eric Sandeen51582172014-02-27 15:17:27 +1100293
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500294 return true;
295}
296
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +1100297static int
298xfs_btree_free_block(
299 struct xfs_btree_cur *cur,
300 struct xfs_buf *bp)
301{
302 int error;
303
304 error = cur->bc_ops->free_block(cur, bp);
305 if (!error)
306 XFS_BTREE_STATS_INC(cur, free);
307 return error;
308}
309
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500310/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * Delete the btree cursor.
312 */
313void
314xfs_btree_del_cursor(
315 xfs_btree_cur_t *cur, /* btree cursor */
316 int error) /* del because of error */
317{
318 int i; /* btree level */
319
320 /*
321 * Clear the buffer pointers, and release the buffers.
322 * If we're doing this in the face of an error, we
323 * need to make sure to inspect all of the entries
324 * in the bc_bufs array for buffers to be unlocked.
325 * This is because some of the btree code works from
326 * level n down to 0, and if we get an error along
327 * the way we won't have initialized all the entries
328 * down to 0.
329 */
330 for (i = 0; i < cur->bc_nlevels; i++) {
331 if (cur->bc_bufs[i])
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000332 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else if (!error)
334 break;
335 }
336 /*
337 * Can't free a bmap cursor without having dealt with the
338 * allocated indirect blocks' accounting.
339 */
340 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
341 cur->bc_private.b.allocated == 0);
342 /*
343 * Free the cursor.
344 */
345 kmem_zone_free(xfs_btree_cur_zone, cur);
346}
347
348/*
349 * Duplicate the btree cursor.
350 * Allocate a new one, copy the record, re-get the buffers.
351 */
352int /* error */
353xfs_btree_dup_cursor(
354 xfs_btree_cur_t *cur, /* input cursor */
355 xfs_btree_cur_t **ncur) /* output cursor */
356{
357 xfs_buf_t *bp; /* btree block's buffer pointer */
358 int error; /* error return value */
359 int i; /* level number of btree block */
360 xfs_mount_t *mp; /* mount structure for filesystem */
361 xfs_btree_cur_t *new; /* new cursor value */
362 xfs_trans_t *tp; /* transaction pointer, can be NULL */
363
364 tp = cur->bc_tp;
365 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
368 * Allocate a new cursor like the old one.
369 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100370 new = cur->bc_ops->dup_cursor(cur);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /*
373 * Copy the record currently in the cursor.
374 */
375 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * For each level current, re-get the buffer and copy the ptr value.
379 */
380 for (i = 0; i < new->bc_nlevels; i++) {
381 new->bc_ptrs[i] = cur->bc_ptrs[i];
382 new->bc_ra[i] = cur->bc_ra[i];
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100383 bp = cur->bc_bufs[i];
384 if (bp) {
385 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
386 XFS_BUF_ADDR(bp), mp->m_bsize,
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100387 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100388 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100389 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 xfs_btree_del_cursor(new, error);
391 *ncur = NULL;
392 return error;
393 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500394 }
395 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *ncur = new;
398 return 0;
399}
400
401/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100402 * XFS btree block layout and addressing:
403 *
404 * There are two types of blocks in the btree: leaf and non-leaf blocks.
405 *
406 * The leaf record start with a header then followed by records containing
407 * the values. A non-leaf block also starts with the same header, and
408 * then first contains lookup keys followed by an equal number of pointers
409 * to the btree blocks at the previous level.
410 *
411 * +--------+-------+-------+-------+-------+-------+-------+
412 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
413 * +--------+-------+-------+-------+-------+-------+-------+
414 *
415 * +--------+-------+-------+-------+-------+-------+-------+
416 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
417 * +--------+-------+-------+-------+-------+-------+-------+
418 *
419 * The header is called struct xfs_btree_block for reasons better left unknown
420 * and comes in different versions for short (32bit) and long (64bit) block
421 * pointers. The record and key structures are defined by the btree instances
422 * and opaque to the btree core. The block pointers are simple disk endian
423 * integers, available in a short (32bit) and long (64bit) variant.
424 *
425 * The helpers below calculate the offset of a given record, key or pointer
426 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
427 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
428 * inside the btree block is done using indices starting at one, not zero!
429 */
430
431/*
432 * Return size of the btree block header for this btree instance.
433 */
434static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
435{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500436 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
437 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
438 return XFS_BTREE_LBLOCK_CRC_LEN;
439 return XFS_BTREE_LBLOCK_LEN;
440 }
441 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
442 return XFS_BTREE_SBLOCK_CRC_LEN;
443 return XFS_BTREE_SBLOCK_LEN;
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100444}
445
446/*
447 * Return size of btree block pointers for this btree instance.
448 */
449static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
450{
451 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
452 sizeof(__be64) : sizeof(__be32);
453}
454
455/*
456 * Calculate offset of the n-th record in a btree block.
457 */
458STATIC size_t
459xfs_btree_rec_offset(
460 struct xfs_btree_cur *cur,
461 int n)
462{
463 return xfs_btree_block_len(cur) +
464 (n - 1) * cur->bc_ops->rec_len;
465}
466
467/*
468 * Calculate offset of the n-th key in a btree block.
469 */
470STATIC size_t
471xfs_btree_key_offset(
472 struct xfs_btree_cur *cur,
473 int n)
474{
475 return xfs_btree_block_len(cur) +
476 (n - 1) * cur->bc_ops->key_len;
477}
478
479/*
480 * Calculate offset of the n-th block pointer in a btree block.
481 */
482STATIC size_t
483xfs_btree_ptr_offset(
484 struct xfs_btree_cur *cur,
485 int n,
486 int level)
487{
488 return xfs_btree_block_len(cur) +
489 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
490 (n - 1) * xfs_btree_ptr_len(cur);
491}
492
493/*
494 * Return a pointer to the n-th record in the btree block.
495 */
496STATIC union xfs_btree_rec *
497xfs_btree_rec_addr(
498 struct xfs_btree_cur *cur,
499 int n,
500 struct xfs_btree_block *block)
501{
502 return (union xfs_btree_rec *)
503 ((char *)block + xfs_btree_rec_offset(cur, n));
504}
505
506/*
507 * Return a pointer to the n-th key in the btree block.
508 */
509STATIC union xfs_btree_key *
510xfs_btree_key_addr(
511 struct xfs_btree_cur *cur,
512 int n,
513 struct xfs_btree_block *block)
514{
515 return (union xfs_btree_key *)
516 ((char *)block + xfs_btree_key_offset(cur, n));
517}
518
519/*
520 * Return a pointer to the n-th block pointer in the btree block.
521 */
522STATIC union xfs_btree_ptr *
523xfs_btree_ptr_addr(
524 struct xfs_btree_cur *cur,
525 int n,
526 struct xfs_btree_block *block)
527{
528 int level = xfs_btree_get_level(block);
529
530 ASSERT(block->bb_level != 0);
531
532 return (union xfs_btree_ptr *)
533 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
534}
535
536/*
Zhi Yong Wu1cb93862013-08-07 10:11:05 +0000537 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100538 *
539 * For now this btree implementation assumes the btree root is always
540 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100542STATIC struct xfs_btree_block *
543xfs_btree_get_iroot(
544 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Christoph Hellwig8186e512008-10-30 16:54:22 +1100546 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Christoph Hellwig8186e512008-10-30 16:54:22 +1100548 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
549 return (struct xfs_btree_block *)ifp->if_broot;
550}
551
552/*
553 * Retrieve the block pointer from the cursor at the given level.
554 * This may be an inode btree root or from a buffer.
555 */
556STATIC struct xfs_btree_block * /* generic btree block pointer */
557xfs_btree_get_block(
558 struct xfs_btree_cur *cur, /* btree cursor */
559 int level, /* level in btree */
560 struct xfs_buf **bpp) /* buffer containing the block */
561{
562 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
563 (level == cur->bc_nlevels - 1)) {
564 *bpp = NULL;
565 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100567
568 *bpp = cur->bc_bufs[level];
569 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/*
573 * Get a buffer for the block, return it with no data read.
574 * Long-form addressing.
575 */
576xfs_buf_t * /* buffer for fsbno */
577xfs_btree_get_bufl(
578 xfs_mount_t *mp, /* file system mount point */
579 xfs_trans_t *tp, /* transaction pointer */
580 xfs_fsblock_t fsbno, /* file system block number */
581 uint lock) /* lock flags for get_buf */
582{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 xfs_daddr_t d; /* real disk block address */
584
585 ASSERT(fsbno != NULLFSBLOCK);
586 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000587 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * Get a buffer for the block, return it with no data read.
592 * Short-form addressing.
593 */
594xfs_buf_t * /* buffer for agno/agbno */
595xfs_btree_get_bufs(
596 xfs_mount_t *mp, /* file system mount point */
597 xfs_trans_t *tp, /* transaction pointer */
598 xfs_agnumber_t agno, /* allocation group number */
599 xfs_agblock_t agbno, /* allocation group block number */
600 uint lock) /* lock flags for get_buf */
601{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfs_daddr_t d; /* real disk block address */
603
604 ASSERT(agno != NULLAGNUMBER);
605 ASSERT(agbno != NULLAGBLOCK);
606 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner36de9552014-06-06 16:02:12 +1000607 return xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * Check for the cursor referring to the last block at the given level.
612 */
613int /* 1=is last block, 0=not last block */
614xfs_btree_islastblock(
615 xfs_btree_cur_t *cur, /* btree cursor */
616 int level) /* level to check */
617{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100618 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_buf_t *bp; /* buffer containing block */
620
621 block = xfs_btree_get_block(cur, level, &bp);
622 xfs_btree_check_block(cur, block, level, bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100623 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000624 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200626 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000630 * Change the cursor to point to the first record at the given level.
631 * Other levels are unaffected.
632 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100633STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000634xfs_btree_firstrec(
635 xfs_btree_cur_t *cur, /* btree cursor */
636 int level) /* level to change */
637{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100638 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000639 xfs_buf_t *bp; /* buffer containing block */
640
641 /*
642 * Get the block pointer for this level.
643 */
644 block = xfs_btree_get_block(cur, level, &bp);
645 xfs_btree_check_block(cur, block, level, bp);
646 /*
647 * It's empty, there is no such record.
648 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100649 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000650 return 0;
651 /*
652 * Set the ptr value to 1, that's the first record/key.
653 */
654 cur->bc_ptrs[level] = 1;
655 return 1;
656}
657
658/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * Change the cursor to point to the last record in the current block
660 * at the given level. Other levels are unaffected.
661 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100662STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663xfs_btree_lastrec(
664 xfs_btree_cur_t *cur, /* btree cursor */
665 int level) /* level to change */
666{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100667 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 xfs_buf_t *bp; /* buffer containing block */
669
670 /*
671 * Get the block pointer for this level.
672 */
673 block = xfs_btree_get_block(cur, level, &bp);
674 xfs_btree_check_block(cur, block, level, bp);
675 /*
676 * It's empty, there is no such record.
677 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100678 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return 0;
680 /*
681 * Set the ptr value to numrecs, that's the last record/key.
682 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100683 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 1;
685}
686
687/*
688 * Compute first and last byte offsets for the fields given.
689 * Interprets the offsets table, which contains struct field offsets.
690 */
691void
692xfs_btree_offsets(
693 __int64_t fields, /* bitmask of fields */
694 const short *offsets, /* table of field offsets */
695 int nbits, /* number of bits to inspect */
696 int *first, /* output: first byte offset */
697 int *last) /* output: last byte offset */
698{
699 int i; /* current bit number */
700 __int64_t imask; /* mask for current bit number */
701
702 ASSERT(fields != 0);
703 /*
704 * Find the lowest bit, so the first byte offset.
705 */
706 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
707 if (imask & fields) {
708 *first = offsets[i];
709 break;
710 }
711 }
712 /*
713 * Find the highest bit, so the last byte offset.
714 */
715 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
716 if (imask & fields) {
717 *last = offsets[i + 1] - 1;
718 break;
719 }
720 }
721}
722
723/*
724 * Get a buffer for the block, return it read in.
725 * Long-form addressing.
726 */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100727int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100729 struct xfs_mount *mp, /* file system mount point */
730 struct xfs_trans *tp, /* transaction pointer */
731 xfs_fsblock_t fsbno, /* file system block number */
732 uint lock, /* lock flags for read_buf */
733 struct xfs_buf **bpp, /* buffer for fsbno */
734 int refval, /* ref count value for buffer */
Dave Chinner1813dd62012-11-14 17:54:40 +1100735 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100737 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100739 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 ASSERT(fsbno != NULLFSBLOCK);
742 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100743 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Dave Chinner1813dd62012-11-14 17:54:40 +1100744 mp->m_bsize, lock, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100745 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return error;
Dave Chinner821eb212010-12-02 16:31:13 +1100747 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000748 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *bpp = bp;
750 return 0;
751}
752
753/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * Read-ahead the block, don't wait for it, don't return a buffer.
755 * Long-form addressing.
756 */
757/* ARGSUSED */
758void
759xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100760 struct xfs_mount *mp, /* file system mount point */
761 xfs_fsblock_t fsbno, /* file system block number */
762 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100763 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 xfs_daddr_t d;
766
767 ASSERT(fsbno != NULLFSBLOCK);
768 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100769 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772/*
773 * Read-ahead the block, don't wait for it, don't return a buffer.
774 * Short-form addressing.
775 */
776/* ARGSUSED */
777void
778xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100779 struct xfs_mount *mp, /* file system mount point */
780 xfs_agnumber_t agno, /* allocation group number */
781 xfs_agblock_t agbno, /* allocation group block number */
782 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100783 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 xfs_daddr_t d;
786
787 ASSERT(agno != NULLAGNUMBER);
788 ASSERT(agbno != NULLAGBLOCK);
789 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100790 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100793STATIC int
794xfs_btree_readahead_lblock(
795 struct xfs_btree_cur *cur,
796 int lr,
797 struct xfs_btree_block *block)
798{
799 int rval = 0;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000800 xfs_fsblock_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
801 xfs_fsblock_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100802
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000803 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100804 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100805 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100806 rval++;
807 }
808
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000809 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLFSBLOCK) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100810 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100811 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100812 rval++;
813 }
814
815 return rval;
816}
817
818STATIC int
819xfs_btree_readahead_sblock(
820 struct xfs_btree_cur *cur,
821 int lr,
822 struct xfs_btree_block *block)
823{
824 int rval = 0;
825 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
826 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
827
828
829 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
830 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100831 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100832 rval++;
833 }
834
835 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
836 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100837 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100838 rval++;
839 }
840
841 return rval;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
845 * Read-ahead btree blocks, at the given level.
846 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
847 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100848STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100849xfs_btree_readahead(
850 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int lev, /* level in btree */
852 int lr) /* left/right bits */
853{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100854 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100856 /*
857 * No readahead needed if we are at the root level and the
858 * btree root is stored in the inode.
859 */
860 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
861 (lev == cur->bc_nlevels - 1))
862 return 0;
863
864 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
865 return 0;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100868 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
869
870 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
871 return xfs_btree_readahead_lblock(cur, lr, block);
872 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Dave Chinner21b5c972013-08-30 10:23:44 +1000875STATIC xfs_daddr_t
876xfs_btree_ptr_to_daddr(
877 struct xfs_btree_cur *cur,
878 union xfs_btree_ptr *ptr)
879{
880 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000881 ASSERT(ptr->l != cpu_to_be64(NULLFSBLOCK));
Dave Chinner21b5c972013-08-30 10:23:44 +1000882
883 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
884 } else {
885 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
886 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
887
888 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
889 be32_to_cpu(ptr->s));
890 }
891}
892
893/*
894 * Readahead @count btree blocks at the given @ptr location.
895 *
896 * We don't need to care about long or short form btrees here as we have a
897 * method of converting the ptr directly to a daddr available to us.
898 */
899STATIC void
900xfs_btree_readahead_ptr(
901 struct xfs_btree_cur *cur,
902 union xfs_btree_ptr *ptr,
903 xfs_extlen_t count)
904{
905 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
906 xfs_btree_ptr_to_daddr(cur, ptr),
907 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
911 * Set the buffer for level "lev" in the cursor to bp, releasing
912 * any previous buffer.
913 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000914STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915xfs_btree_setbuf(
916 xfs_btree_cur_t *cur, /* btree cursor */
917 int lev, /* level in btree */
918 xfs_buf_t *bp) /* new buffer to set */
919{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100920 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000922 if (cur->bc_bufs[lev])
923 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 cur->bc_bufs[lev] = bp;
925 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100928 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000929 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000931 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
933 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200934 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200936 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
938 }
939}
Christoph Hellwig637aa502008-10-30 16:55:45 +1100940
941STATIC int
942xfs_btree_ptr_is_null(
943 struct xfs_btree_cur *cur,
944 union xfs_btree_ptr *ptr)
945{
946 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000947 return ptr->l == cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100948 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200949 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100950}
951
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100952STATIC void
953xfs_btree_set_ptr_null(
954 struct xfs_btree_cur *cur,
955 union xfs_btree_ptr *ptr)
956{
957 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000958 ptr->l = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100959 else
960 ptr->s = cpu_to_be32(NULLAGBLOCK);
961}
962
Christoph Hellwig637aa502008-10-30 16:55:45 +1100963/*
964 * Get/set/init sibling pointers
965 */
966STATIC void
967xfs_btree_get_sibling(
968 struct xfs_btree_cur *cur,
969 struct xfs_btree_block *block,
970 union xfs_btree_ptr *ptr,
971 int lr)
972{
973 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
974
975 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
976 if (lr == XFS_BB_RIGHTSIB)
977 ptr->l = block->bb_u.l.bb_rightsib;
978 else
979 ptr->l = block->bb_u.l.bb_leftsib;
980 } else {
981 if (lr == XFS_BB_RIGHTSIB)
982 ptr->s = block->bb_u.s.bb_rightsib;
983 else
984 ptr->s = block->bb_u.s.bb_leftsib;
985 }
986}
987
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100988STATIC void
989xfs_btree_set_sibling(
990 struct xfs_btree_cur *cur,
991 struct xfs_btree_block *block,
992 union xfs_btree_ptr *ptr,
993 int lr)
994{
995 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
996
997 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
998 if (lr == XFS_BB_RIGHTSIB)
999 block->bb_u.l.bb_rightsib = ptr->l;
1000 else
1001 block->bb_u.l.bb_leftsib = ptr->l;
1002 } else {
1003 if (lr == XFS_BB_RIGHTSIB)
1004 block->bb_u.s.bb_rightsib = ptr->s;
1005 else
1006 block->bb_u.s.bb_leftsib = ptr->s;
1007 }
1008}
1009
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001010void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001011xfs_btree_init_block_int(
1012 struct xfs_mount *mp,
1013 struct xfs_btree_block *buf,
1014 xfs_daddr_t blkno,
1015 __u32 magic,
1016 __u16 level,
1017 __u16 numrecs,
1018 __u64 owner,
1019 unsigned int flags)
1020{
1021 buf->bb_magic = cpu_to_be32(magic);
1022 buf->bb_level = cpu_to_be16(level);
1023 buf->bb_numrecs = cpu_to_be16(numrecs);
1024
1025 if (flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001026 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLFSBLOCK);
1027 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLFSBLOCK);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001028 if (flags & XFS_BTREE_CRC_BLOCKS) {
1029 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1030 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001031 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001032 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001033 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001034 }
1035 } else {
1036 /* owner is a 32 bit value on short blocks */
1037 __u32 __owner = (__u32)owner;
1038
1039 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1040 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1041 if (flags & XFS_BTREE_CRC_BLOCKS) {
1042 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1043 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
Eric Sandeence748ea2015-07-29 11:53:31 +10001044 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001045 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001046 }
1047 }
1048}
1049
1050void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001051xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001052 struct xfs_mount *mp,
1053 struct xfs_buf *bp,
1054 __u32 magic,
1055 __u16 level,
1056 __u16 numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001057 __u64 owner,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001058 unsigned int flags)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001059{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001060 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1061 magic, level, numrecs, owner, flags);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001062}
1063
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001064STATIC void
1065xfs_btree_init_block_cur(
1066 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001067 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001068 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001069 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001070{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001071 __u64 owner;
1072
1073 /*
1074 * we can pull the owner from the cursor right now as the different
1075 * owners align directly with the pointer size of the btree. This may
1076 * change in future, but is safe for current users of the generic btree
1077 * code.
1078 */
1079 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1080 owner = cur->bc_private.b.ip->i_ino;
1081 else
1082 owner = cur->bc_private.a.agno;
1083
1084 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1085 xfs_btree_magic(cur), level, numrecs,
1086 owner, cur->bc_flags);
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001087}
1088
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001089/*
1090 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001091 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001092 * will be further refined in the update_lastrec method.
1093 */
1094STATIC int
1095xfs_btree_is_lastrec(
1096 struct xfs_btree_cur *cur,
1097 struct xfs_btree_block *block,
1098 int level)
1099{
1100 union xfs_btree_ptr ptr;
1101
1102 if (level > 0)
1103 return 0;
1104 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1105 return 0;
1106
1107 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1108 if (!xfs_btree_ptr_is_null(cur, &ptr))
1109 return 0;
1110 return 1;
1111}
1112
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001113STATIC void
1114xfs_btree_buf_to_ptr(
1115 struct xfs_btree_cur *cur,
1116 struct xfs_buf *bp,
1117 union xfs_btree_ptr *ptr)
1118{
1119 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1120 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1121 XFS_BUF_ADDR(bp)));
1122 else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001123 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001124 XFS_BUF_ADDR(bp)));
1125 }
1126}
1127
Christoph Hellwig637aa502008-10-30 16:55:45 +11001128STATIC void
1129xfs_btree_set_refs(
1130 struct xfs_btree_cur *cur,
1131 struct xfs_buf *bp)
1132{
1133 switch (cur->bc_btnum) {
1134 case XFS_BTNUM_BNO:
1135 case XFS_BTNUM_CNT:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001136 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001137 break;
1138 case XFS_BTNUM_INO:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001139 case XFS_BTNUM_FINO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001140 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001141 break;
1142 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001143 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001144 break;
1145 default:
1146 ASSERT(0);
1147 }
1148}
1149
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001150STATIC int
1151xfs_btree_get_buf_block(
1152 struct xfs_btree_cur *cur,
1153 union xfs_btree_ptr *ptr,
1154 int flags,
1155 struct xfs_btree_block **block,
1156 struct xfs_buf **bpp)
1157{
1158 struct xfs_mount *mp = cur->bc_mp;
1159 xfs_daddr_t d;
1160
1161 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001162 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001163
1164 d = xfs_btree_ptr_to_daddr(cur, ptr);
1165 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1166 mp->m_bsize, flags);
1167
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001168 if (!*bpp)
Dave Chinner24513372014-06-25 14:58:08 +10001169 return -ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001170
Dave Chinner1813dd62012-11-14 17:54:40 +11001171 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001172 *block = XFS_BUF_TO_BLOCK(*bpp);
1173 return 0;
1174}
1175
Christoph Hellwig637aa502008-10-30 16:55:45 +11001176/*
1177 * Read in the buffer at the given ptr and return the buffer and
1178 * the block pointer within the buffer.
1179 */
1180STATIC int
1181xfs_btree_read_buf_block(
1182 struct xfs_btree_cur *cur,
1183 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001184 int flags,
1185 struct xfs_btree_block **block,
1186 struct xfs_buf **bpp)
1187{
1188 struct xfs_mount *mp = cur->bc_mp;
1189 xfs_daddr_t d;
1190 int error;
1191
1192 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001193 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001194
1195 d = xfs_btree_ptr_to_daddr(cur, ptr);
1196 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001197 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001198 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001199 if (error)
1200 return error;
1201
Christoph Hellwig637aa502008-10-30 16:55:45 +11001202 xfs_btree_set_refs(cur, *bpp);
1203 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001204 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001205}
1206
1207/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001208 * Copy keys from one btree block to another.
1209 */
1210STATIC void
1211xfs_btree_copy_keys(
1212 struct xfs_btree_cur *cur,
1213 union xfs_btree_key *dst_key,
1214 union xfs_btree_key *src_key,
1215 int numkeys)
1216{
1217 ASSERT(numkeys >= 0);
1218 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1219}
1220
1221/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001222 * Copy records from one btree block to another.
1223 */
1224STATIC void
1225xfs_btree_copy_recs(
1226 struct xfs_btree_cur *cur,
1227 union xfs_btree_rec *dst_rec,
1228 union xfs_btree_rec *src_rec,
1229 int numrecs)
1230{
1231 ASSERT(numrecs >= 0);
1232 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1233}
1234
1235/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001236 * Copy block pointers from one btree block to another.
1237 */
1238STATIC void
1239xfs_btree_copy_ptrs(
1240 struct xfs_btree_cur *cur,
1241 union xfs_btree_ptr *dst_ptr,
1242 union xfs_btree_ptr *src_ptr,
1243 int numptrs)
1244{
1245 ASSERT(numptrs >= 0);
1246 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1247}
1248
1249/*
1250 * Shift keys one index left/right inside a single btree block.
1251 */
1252STATIC void
1253xfs_btree_shift_keys(
1254 struct xfs_btree_cur *cur,
1255 union xfs_btree_key *key,
1256 int dir,
1257 int numkeys)
1258{
1259 char *dst_key;
1260
1261 ASSERT(numkeys >= 0);
1262 ASSERT(dir == 1 || dir == -1);
1263
1264 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1265 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1266}
1267
1268/*
1269 * Shift records one index left/right inside a single btree block.
1270 */
1271STATIC void
1272xfs_btree_shift_recs(
1273 struct xfs_btree_cur *cur,
1274 union xfs_btree_rec *rec,
1275 int dir,
1276 int numrecs)
1277{
1278 char *dst_rec;
1279
1280 ASSERT(numrecs >= 0);
1281 ASSERT(dir == 1 || dir == -1);
1282
1283 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1284 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1285}
1286
1287/*
1288 * Shift block pointers one index left/right inside a single btree block.
1289 */
1290STATIC void
1291xfs_btree_shift_ptrs(
1292 struct xfs_btree_cur *cur,
1293 union xfs_btree_ptr *ptr,
1294 int dir,
1295 int numptrs)
1296{
1297 char *dst_ptr;
1298
1299 ASSERT(numptrs >= 0);
1300 ASSERT(dir == 1 || dir == -1);
1301
1302 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1303 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1304}
1305
1306/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001307 * Log key values from the btree block.
1308 */
1309STATIC void
1310xfs_btree_log_keys(
1311 struct xfs_btree_cur *cur,
1312 struct xfs_buf *bp,
1313 int first,
1314 int last)
1315{
1316 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1317 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1318
1319 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001320 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001321 xfs_trans_log_buf(cur->bc_tp, bp,
1322 xfs_btree_key_offset(cur, first),
1323 xfs_btree_key_offset(cur, last + 1) - 1);
1324 } else {
1325 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1326 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1327 }
1328
1329 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1330}
1331
1332/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001333 * Log record values from the btree block.
1334 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001335void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001336xfs_btree_log_recs(
1337 struct xfs_btree_cur *cur,
1338 struct xfs_buf *bp,
1339 int first,
1340 int last)
1341{
1342 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1343 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1344
Dave Chinner61fe1352013-04-03 16:11:30 +11001345 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001346 xfs_trans_log_buf(cur->bc_tp, bp,
1347 xfs_btree_rec_offset(cur, first),
1348 xfs_btree_rec_offset(cur, last + 1) - 1);
1349
1350 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1351}
1352
1353/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001354 * Log block pointer fields from a btree block (nonleaf).
1355 */
1356STATIC void
1357xfs_btree_log_ptrs(
1358 struct xfs_btree_cur *cur, /* btree cursor */
1359 struct xfs_buf *bp, /* buffer containing btree block */
1360 int first, /* index of first pointer to log */
1361 int last) /* index of last pointer to log */
1362{
1363 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1364 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1365
1366 if (bp) {
1367 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1368 int level = xfs_btree_get_level(block);
1369
Dave Chinner61fe1352013-04-03 16:11:30 +11001370 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001371 xfs_trans_log_buf(cur->bc_tp, bp,
1372 xfs_btree_ptr_offset(cur, first, level),
1373 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1374 } else {
1375 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1376 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1377 }
1378
1379 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1380}
1381
1382/*
1383 * Log fields from a btree block header.
1384 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001385void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001386xfs_btree_log_block(
1387 struct xfs_btree_cur *cur, /* btree cursor */
1388 struct xfs_buf *bp, /* buffer containing btree block */
1389 int fields) /* mask of fields: XFS_BB_... */
1390{
1391 int first; /* first byte offset logged */
1392 int last; /* last byte offset logged */
1393 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001394 offsetof(struct xfs_btree_block, bb_magic),
1395 offsetof(struct xfs_btree_block, bb_level),
1396 offsetof(struct xfs_btree_block, bb_numrecs),
1397 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1398 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001399 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1400 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1401 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1402 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1403 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1404 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001405 };
1406 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001407 offsetof(struct xfs_btree_block, bb_magic),
1408 offsetof(struct xfs_btree_block, bb_level),
1409 offsetof(struct xfs_btree_block, bb_numrecs),
1410 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1411 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001412 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1413 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1414 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1415 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1416 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1417 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1418 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001419 };
1420
1421 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1422 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1423
1424 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001425 int nbits;
1426
1427 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1428 /*
1429 * We don't log the CRC when updating a btree
1430 * block but instead recreate it during log
1431 * recovery. As the log buffers have checksums
1432 * of their own this is safe and avoids logging a crc
1433 * update in a lot of places.
1434 */
1435 if (fields == XFS_BB_ALL_BITS)
1436 fields = XFS_BB_ALL_BITS_CRC;
1437 nbits = XFS_BB_NUM_BITS_CRC;
1438 } else {
1439 nbits = XFS_BB_NUM_BITS;
1440 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001441 xfs_btree_offsets(fields,
1442 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1443 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001444 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001445 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001446 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1447 } else {
1448 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1449 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1450 }
1451
1452 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1453}
1454
1455/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001456 * Increment cursor by one record at the level.
1457 * For nonzero levels the leaf-ward information is untouched.
1458 */
1459int /* error */
1460xfs_btree_increment(
1461 struct xfs_btree_cur *cur,
1462 int level,
1463 int *stat) /* success/failure */
1464{
1465 struct xfs_btree_block *block;
1466 union xfs_btree_ptr ptr;
1467 struct xfs_buf *bp;
1468 int error; /* error return value */
1469 int lev;
1470
1471 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1472 XFS_BTREE_TRACE_ARGI(cur, level);
1473
1474 ASSERT(level < cur->bc_nlevels);
1475
1476 /* Read-ahead to the right at this level. */
1477 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1478
1479 /* Get a pointer to the btree block. */
1480 block = xfs_btree_get_block(cur, level, &bp);
1481
1482#ifdef DEBUG
1483 error = xfs_btree_check_block(cur, block, level, bp);
1484 if (error)
1485 goto error0;
1486#endif
1487
1488 /* We're done if we remain in the block after the increment. */
1489 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1490 goto out1;
1491
1492 /* Fail if we just went off the right edge of the tree. */
1493 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1494 if (xfs_btree_ptr_is_null(cur, &ptr))
1495 goto out0;
1496
1497 XFS_BTREE_STATS_INC(cur, increment);
1498
1499 /*
1500 * March up the tree incrementing pointers.
1501 * Stop when we don't go off the right edge of a block.
1502 */
1503 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1504 block = xfs_btree_get_block(cur, lev, &bp);
1505
1506#ifdef DEBUG
1507 error = xfs_btree_check_block(cur, block, lev, bp);
1508 if (error)
1509 goto error0;
1510#endif
1511
1512 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1513 break;
1514
1515 /* Read-ahead the right block for the next loop. */
1516 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1517 }
1518
1519 /*
1520 * If we went off the root then we are either seriously
1521 * confused or have the tree root in an inode.
1522 */
1523 if (lev == cur->bc_nlevels) {
1524 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1525 goto out0;
1526 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001527 error = -EFSCORRUPTED;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001528 goto error0;
1529 }
1530 ASSERT(lev < cur->bc_nlevels);
1531
1532 /*
1533 * Now walk back down the tree, fixing up the cursor's buffer
1534 * pointers and key numbers.
1535 */
1536 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1537 union xfs_btree_ptr *ptrp;
1538
1539 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001540 --lev;
1541 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001542 if (error)
1543 goto error0;
1544
1545 xfs_btree_setbuf(cur, lev, bp);
1546 cur->bc_ptrs[lev] = 1;
1547 }
1548out1:
1549 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1550 *stat = 1;
1551 return 0;
1552
1553out0:
1554 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1555 *stat = 0;
1556 return 0;
1557
1558error0:
1559 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1560 return error;
1561}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001562
1563/*
1564 * Decrement cursor by one record at the level.
1565 * For nonzero levels the leaf-ward information is untouched.
1566 */
1567int /* error */
1568xfs_btree_decrement(
1569 struct xfs_btree_cur *cur,
1570 int level,
1571 int *stat) /* success/failure */
1572{
1573 struct xfs_btree_block *block;
1574 xfs_buf_t *bp;
1575 int error; /* error return value */
1576 int lev;
1577 union xfs_btree_ptr ptr;
1578
1579 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1580 XFS_BTREE_TRACE_ARGI(cur, level);
1581
1582 ASSERT(level < cur->bc_nlevels);
1583
1584 /* Read-ahead to the left at this level. */
1585 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1586
1587 /* We're done if we remain in the block after the decrement. */
1588 if (--cur->bc_ptrs[level] > 0)
1589 goto out1;
1590
1591 /* Get a pointer to the btree block. */
1592 block = xfs_btree_get_block(cur, level, &bp);
1593
1594#ifdef DEBUG
1595 error = xfs_btree_check_block(cur, block, level, bp);
1596 if (error)
1597 goto error0;
1598#endif
1599
1600 /* Fail if we just went off the left edge of the tree. */
1601 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1602 if (xfs_btree_ptr_is_null(cur, &ptr))
1603 goto out0;
1604
1605 XFS_BTREE_STATS_INC(cur, decrement);
1606
1607 /*
1608 * March up the tree decrementing pointers.
1609 * Stop when we don't go off the left edge of a block.
1610 */
1611 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1612 if (--cur->bc_ptrs[lev] > 0)
1613 break;
1614 /* Read-ahead the left block for the next loop. */
1615 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1616 }
1617
1618 /*
1619 * If we went off the root then we are seriously confused.
1620 * or the root of the tree is in an inode.
1621 */
1622 if (lev == cur->bc_nlevels) {
1623 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1624 goto out0;
1625 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001626 error = -EFSCORRUPTED;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001627 goto error0;
1628 }
1629 ASSERT(lev < cur->bc_nlevels);
1630
1631 /*
1632 * Now walk back down the tree, fixing up the cursor's buffer
1633 * pointers and key numbers.
1634 */
1635 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1636 union xfs_btree_ptr *ptrp;
1637
1638 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001639 --lev;
1640 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001641 if (error)
1642 goto error0;
1643 xfs_btree_setbuf(cur, lev, bp);
1644 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1645 }
1646out1:
1647 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1648 *stat = 1;
1649 return 0;
1650
1651out0:
1652 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1653 *stat = 0;
1654 return 0;
1655
1656error0:
1657 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1658 return error;
1659}
1660
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001661STATIC int
1662xfs_btree_lookup_get_block(
1663 struct xfs_btree_cur *cur, /* btree cursor */
1664 int level, /* level in the btree */
1665 union xfs_btree_ptr *pp, /* ptr to btree block */
1666 struct xfs_btree_block **blkp) /* return btree block */
1667{
1668 struct xfs_buf *bp; /* buffer pointer for btree block */
1669 int error = 0;
1670
1671 /* special case the root block if in an inode */
1672 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1673 (level == cur->bc_nlevels - 1)) {
1674 *blkp = xfs_btree_get_iroot(cur);
1675 return 0;
1676 }
1677
1678 /*
1679 * If the old buffer at this level for the disk address we are
1680 * looking for re-use it.
1681 *
1682 * Otherwise throw it away and get a new one.
1683 */
1684 bp = cur->bc_bufs[level];
1685 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1686 *blkp = XFS_BUF_TO_BLOCK(bp);
1687 return 0;
1688 }
1689
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001690 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001691 if (error)
1692 return error;
1693
1694 xfs_btree_setbuf(cur, level, bp);
1695 return 0;
1696}
1697
1698/*
1699 * Get current search key. For level 0 we don't actually have a key
1700 * structure so we make one up from the record. For all other levels
1701 * we just return the right key.
1702 */
1703STATIC union xfs_btree_key *
1704xfs_lookup_get_search_key(
1705 struct xfs_btree_cur *cur,
1706 int level,
1707 int keyno,
1708 struct xfs_btree_block *block,
1709 union xfs_btree_key *kp)
1710{
1711 if (level == 0) {
1712 cur->bc_ops->init_key_from_rec(kp,
1713 xfs_btree_rec_addr(cur, keyno, block));
1714 return kp;
1715 }
1716
1717 return xfs_btree_key_addr(cur, keyno, block);
1718}
1719
1720/*
1721 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001722 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001723 */
1724int /* error */
1725xfs_btree_lookup(
1726 struct xfs_btree_cur *cur, /* btree cursor */
1727 xfs_lookup_t dir, /* <=, ==, or >= */
1728 int *stat) /* success/failure */
1729{
1730 struct xfs_btree_block *block; /* current btree block */
1731 __int64_t diff; /* difference for the current key */
1732 int error; /* error return value */
1733 int keyno; /* current key number */
1734 int level; /* level in the btree */
1735 union xfs_btree_ptr *pp; /* ptr to btree block */
1736 union xfs_btree_ptr ptr; /* ptr to btree block */
1737
1738 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1739 XFS_BTREE_TRACE_ARGI(cur, dir);
1740
1741 XFS_BTREE_STATS_INC(cur, lookup);
1742
1743 block = NULL;
1744 keyno = 0;
1745
1746 /* initialise start pointer from cursor */
1747 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1748 pp = &ptr;
1749
1750 /*
1751 * Iterate over each level in the btree, starting at the root.
1752 * For each level above the leaves, find the key we need, based
1753 * on the lookup record, then follow the corresponding block
1754 * pointer down to the next level.
1755 */
1756 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1757 /* Get the block we need to do the lookup on. */
1758 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1759 if (error)
1760 goto error0;
1761
1762 if (diff == 0) {
1763 /*
1764 * If we already had a key match at a higher level, we
1765 * know we need to use the first entry in this block.
1766 */
1767 keyno = 1;
1768 } else {
1769 /* Otherwise search this block. Do a binary search. */
1770
1771 int high; /* high entry number */
1772 int low; /* low entry number */
1773
1774 /* Set low and high entry numbers, 1-based. */
1775 low = 1;
1776 high = xfs_btree_get_numrecs(block);
1777 if (!high) {
1778 /* Block is empty, must be an empty leaf. */
1779 ASSERT(level == 0 && cur->bc_nlevels == 1);
1780
1781 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1782 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1783 *stat = 0;
1784 return 0;
1785 }
1786
1787 /* Binary search the block. */
1788 while (low <= high) {
1789 union xfs_btree_key key;
1790 union xfs_btree_key *kp;
1791
1792 XFS_BTREE_STATS_INC(cur, compare);
1793
1794 /* keyno is average of low and high. */
1795 keyno = (low + high) >> 1;
1796
1797 /* Get current search key */
1798 kp = xfs_lookup_get_search_key(cur, level,
1799 keyno, block, &key);
1800
1801 /*
1802 * Compute difference to get next direction:
1803 * - less than, move right
1804 * - greater than, move left
1805 * - equal, we're done
1806 */
1807 diff = cur->bc_ops->key_diff(cur, kp);
1808 if (diff < 0)
1809 low = keyno + 1;
1810 else if (diff > 0)
1811 high = keyno - 1;
1812 else
1813 break;
1814 }
1815 }
1816
1817 /*
1818 * If there are more levels, set up for the next level
1819 * by getting the block number and filling in the cursor.
1820 */
1821 if (level > 0) {
1822 /*
1823 * If we moved left, need the previous key number,
1824 * unless there isn't one.
1825 */
1826 if (diff > 0 && --keyno < 1)
1827 keyno = 1;
1828 pp = xfs_btree_ptr_addr(cur, keyno, block);
1829
1830#ifdef DEBUG
1831 error = xfs_btree_check_ptr(cur, pp, 0, level);
1832 if (error)
1833 goto error0;
1834#endif
1835 cur->bc_ptrs[level] = keyno;
1836 }
1837 }
1838
1839 /* Done with the search. See if we need to adjust the results. */
1840 if (dir != XFS_LOOKUP_LE && diff < 0) {
1841 keyno++;
1842 /*
1843 * If ge search and we went off the end of the block, but it's
1844 * not the last block, we're in the wrong block.
1845 */
1846 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1847 if (dir == XFS_LOOKUP_GE &&
1848 keyno > xfs_btree_get_numrecs(block) &&
1849 !xfs_btree_ptr_is_null(cur, &ptr)) {
1850 int i;
1851
1852 cur->bc_ptrs[0] = keyno;
1853 error = xfs_btree_increment(cur, 0, &i);
1854 if (error)
1855 goto error0;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11001856 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp, i == 1);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001857 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1858 *stat = 1;
1859 return 0;
1860 }
1861 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1862 keyno--;
1863 cur->bc_ptrs[0] = keyno;
1864
1865 /* Return if we succeeded or not. */
1866 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1867 *stat = 0;
1868 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1869 *stat = 1;
1870 else
1871 *stat = 0;
1872 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1873 return 0;
1874
1875error0:
1876 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1877 return error;
1878}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001879
1880/*
1881 * Update keys at all levels from here to the root along the cursor's path.
1882 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11001883STATIC int
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001884xfs_btree_updkey(
1885 struct xfs_btree_cur *cur,
1886 union xfs_btree_key *keyp,
1887 int level)
1888{
1889 struct xfs_btree_block *block;
1890 struct xfs_buf *bp;
1891 union xfs_btree_key *kp;
1892 int ptr;
1893
1894 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1895 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1896
1897 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1898
1899 /*
1900 * Go up the tree from this level toward the root.
1901 * At each level, update the key value to the value input.
1902 * Stop when we reach a level where the cursor isn't pointing
1903 * at the first entry in the block.
1904 */
1905 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1906#ifdef DEBUG
1907 int error;
1908#endif
1909 block = xfs_btree_get_block(cur, level, &bp);
1910#ifdef DEBUG
1911 error = xfs_btree_check_block(cur, block, level, bp);
1912 if (error) {
1913 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1914 return error;
1915 }
1916#endif
1917 ptr = cur->bc_ptrs[level];
1918 kp = xfs_btree_key_addr(cur, ptr, block);
1919 xfs_btree_copy_keys(cur, kp, keyp, 1);
1920 xfs_btree_log_keys(cur, bp, ptr, ptr);
1921 }
1922
1923 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1924 return 0;
1925}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001926
1927/*
1928 * Update the record referred to by cur to the value in the
1929 * given record. This either works (return 0) or gets an
1930 * EFSCORRUPTED error.
1931 */
1932int
1933xfs_btree_update(
1934 struct xfs_btree_cur *cur,
1935 union xfs_btree_rec *rec)
1936{
1937 struct xfs_btree_block *block;
1938 struct xfs_buf *bp;
1939 int error;
1940 int ptr;
1941 union xfs_btree_rec *rp;
1942
1943 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1944 XFS_BTREE_TRACE_ARGR(cur, rec);
1945
1946 /* Pick up the current block. */
1947 block = xfs_btree_get_block(cur, 0, &bp);
1948
1949#ifdef DEBUG
1950 error = xfs_btree_check_block(cur, block, 0, bp);
1951 if (error)
1952 goto error0;
1953#endif
1954 /* Get the address of the rec to be updated. */
1955 ptr = cur->bc_ptrs[0];
1956 rp = xfs_btree_rec_addr(cur, ptr, block);
1957
1958 /* Fill in the new contents and log them. */
1959 xfs_btree_copy_recs(cur, rp, rec, 1);
1960 xfs_btree_log_recs(cur, bp, ptr, ptr);
1961
1962 /*
1963 * If we are tracking the last record in the tree and
1964 * we are at the far right edge of the tree, update it.
1965 */
1966 if (xfs_btree_is_lastrec(cur, block, 0)) {
1967 cur->bc_ops->update_lastrec(cur, block, rec,
1968 ptr, LASTREC_UPDATE);
1969 }
1970
1971 /* Updating first rec in leaf. Pass new key value up to our parent. */
1972 if (ptr == 1) {
1973 union xfs_btree_key key;
1974
1975 cur->bc_ops->init_key_from_rec(&key, rec);
1976 error = xfs_btree_updkey(cur, &key, 1);
1977 if (error)
1978 goto error0;
1979 }
1980
1981 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1982 return 0;
1983
1984error0:
1985 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1986 return error;
1987}
1988
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001989/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11001990 * Move 1 record left from cur/level if possible.
1991 * Update cur to reflect the new path.
1992 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11001993STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11001994xfs_btree_lshift(
1995 struct xfs_btree_cur *cur,
1996 int level,
1997 int *stat) /* success/failure */
1998{
1999 union xfs_btree_key key; /* btree key */
2000 struct xfs_buf *lbp; /* left buffer pointer */
2001 struct xfs_btree_block *left; /* left btree block */
2002 int lrecs; /* left record count */
2003 struct xfs_buf *rbp; /* right buffer pointer */
2004 struct xfs_btree_block *right; /* right btree block */
2005 int rrecs; /* right record count */
2006 union xfs_btree_ptr lptr; /* left btree pointer */
2007 union xfs_btree_key *rkp = NULL; /* right btree key */
2008 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
2009 union xfs_btree_rec *rrp = NULL; /* right record pointer */
2010 int error; /* error return value */
2011
2012 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2013 XFS_BTREE_TRACE_ARGI(cur, level);
2014
2015 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2016 level == cur->bc_nlevels - 1)
2017 goto out0;
2018
2019 /* Set up variables for this block as "right". */
2020 right = xfs_btree_get_block(cur, level, &rbp);
2021
2022#ifdef DEBUG
2023 error = xfs_btree_check_block(cur, right, level, rbp);
2024 if (error)
2025 goto error0;
2026#endif
2027
2028 /* If we've got no left sibling then we can't shift an entry left. */
2029 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2030 if (xfs_btree_ptr_is_null(cur, &lptr))
2031 goto out0;
2032
2033 /*
2034 * If the cursor entry is the one that would be moved, don't
2035 * do it... it's too complicated.
2036 */
2037 if (cur->bc_ptrs[level] <= 1)
2038 goto out0;
2039
2040 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002041 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002042 if (error)
2043 goto error0;
2044
2045 /* If it's full, it can't take another entry. */
2046 lrecs = xfs_btree_get_numrecs(left);
2047 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2048 goto out0;
2049
2050 rrecs = xfs_btree_get_numrecs(right);
2051
2052 /*
2053 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002054 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002055 * later.
2056 */
2057 lrecs++;
2058 rrecs--;
2059
2060 XFS_BTREE_STATS_INC(cur, lshift);
2061 XFS_BTREE_STATS_ADD(cur, moves, 1);
2062
2063 /*
2064 * If non-leaf, copy a key and a ptr to the left block.
2065 * Log the changes to the left block.
2066 */
2067 if (level > 0) {
2068 /* It's a non-leaf. Move keys and pointers. */
2069 union xfs_btree_key *lkp; /* left btree key */
2070 union xfs_btree_ptr *lpp; /* left address pointer */
2071
2072 lkp = xfs_btree_key_addr(cur, lrecs, left);
2073 rkp = xfs_btree_key_addr(cur, 1, right);
2074
2075 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2076 rpp = xfs_btree_ptr_addr(cur, 1, right);
2077#ifdef DEBUG
2078 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2079 if (error)
2080 goto error0;
2081#endif
2082 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2083 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2084
2085 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2086 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2087
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002088 ASSERT(cur->bc_ops->keys_inorder(cur,
2089 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002090 } else {
2091 /* It's a leaf. Move records. */
2092 union xfs_btree_rec *lrp; /* left record pointer */
2093
2094 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2095 rrp = xfs_btree_rec_addr(cur, 1, right);
2096
2097 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2098 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2099
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002100 ASSERT(cur->bc_ops->recs_inorder(cur,
2101 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002102 }
2103
2104 xfs_btree_set_numrecs(left, lrecs);
2105 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2106
2107 xfs_btree_set_numrecs(right, rrecs);
2108 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2109
2110 /*
2111 * Slide the contents of right down one entry.
2112 */
2113 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2114 if (level > 0) {
2115 /* It's a nonleaf. operate on keys and ptrs */
2116#ifdef DEBUG
2117 int i; /* loop index */
2118
2119 for (i = 0; i < rrecs; i++) {
2120 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2121 if (error)
2122 goto error0;
2123 }
2124#endif
2125 xfs_btree_shift_keys(cur,
2126 xfs_btree_key_addr(cur, 2, right),
2127 -1, rrecs);
2128 xfs_btree_shift_ptrs(cur,
2129 xfs_btree_ptr_addr(cur, 2, right),
2130 -1, rrecs);
2131
2132 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2133 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2134 } else {
2135 /* It's a leaf. operate on records */
2136 xfs_btree_shift_recs(cur,
2137 xfs_btree_rec_addr(cur, 2, right),
2138 -1, rrecs);
2139 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2140
2141 /*
2142 * If it's the first record in the block, we'll need a key
2143 * structure to pass up to the next level (updkey).
2144 */
2145 cur->bc_ops->init_key_from_rec(&key,
2146 xfs_btree_rec_addr(cur, 1, right));
2147 rkp = &key;
2148 }
2149
2150 /* Update the parent key values of right. */
2151 error = xfs_btree_updkey(cur, rkp, level + 1);
2152 if (error)
2153 goto error0;
2154
2155 /* Slide the cursor value left one. */
2156 cur->bc_ptrs[level]--;
2157
2158 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2159 *stat = 1;
2160 return 0;
2161
2162out0:
2163 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2164 *stat = 0;
2165 return 0;
2166
2167error0:
2168 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2169 return error;
2170}
2171
2172/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002173 * Move 1 record right from cur/level if possible.
2174 * Update cur to reflect the new path.
2175 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002176STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002177xfs_btree_rshift(
2178 struct xfs_btree_cur *cur,
2179 int level,
2180 int *stat) /* success/failure */
2181{
2182 union xfs_btree_key key; /* btree key */
2183 struct xfs_buf *lbp; /* left buffer pointer */
2184 struct xfs_btree_block *left; /* left btree block */
2185 struct xfs_buf *rbp; /* right buffer pointer */
2186 struct xfs_btree_block *right; /* right btree block */
2187 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2188 union xfs_btree_ptr rptr; /* right block pointer */
2189 union xfs_btree_key *rkp; /* right btree key */
2190 int rrecs; /* right record count */
2191 int lrecs; /* left record count */
2192 int error; /* error return value */
2193 int i; /* loop counter */
2194
2195 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2196 XFS_BTREE_TRACE_ARGI(cur, level);
2197
2198 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2199 (level == cur->bc_nlevels - 1))
2200 goto out0;
2201
2202 /* Set up variables for this block as "left". */
2203 left = xfs_btree_get_block(cur, level, &lbp);
2204
2205#ifdef DEBUG
2206 error = xfs_btree_check_block(cur, left, level, lbp);
2207 if (error)
2208 goto error0;
2209#endif
2210
2211 /* If we've got no right sibling then we can't shift an entry right. */
2212 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2213 if (xfs_btree_ptr_is_null(cur, &rptr))
2214 goto out0;
2215
2216 /*
2217 * If the cursor entry is the one that would be moved, don't
2218 * do it... it's too complicated.
2219 */
2220 lrecs = xfs_btree_get_numrecs(left);
2221 if (cur->bc_ptrs[level] >= lrecs)
2222 goto out0;
2223
2224 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002225 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002226 if (error)
2227 goto error0;
2228
2229 /* If it's full, it can't take another entry. */
2230 rrecs = xfs_btree_get_numrecs(right);
2231 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2232 goto out0;
2233
2234 XFS_BTREE_STATS_INC(cur, rshift);
2235 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2236
2237 /*
2238 * Make a hole at the start of the right neighbor block, then
2239 * copy the last left block entry to the hole.
2240 */
2241 if (level > 0) {
2242 /* It's a nonleaf. make a hole in the keys and ptrs */
2243 union xfs_btree_key *lkp;
2244 union xfs_btree_ptr *lpp;
2245 union xfs_btree_ptr *rpp;
2246
2247 lkp = xfs_btree_key_addr(cur, lrecs, left);
2248 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2249 rkp = xfs_btree_key_addr(cur, 1, right);
2250 rpp = xfs_btree_ptr_addr(cur, 1, right);
2251
2252#ifdef DEBUG
2253 for (i = rrecs - 1; i >= 0; i--) {
2254 error = xfs_btree_check_ptr(cur, rpp, i, level);
2255 if (error)
2256 goto error0;
2257 }
2258#endif
2259
2260 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2261 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2262
2263#ifdef DEBUG
2264 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2265 if (error)
2266 goto error0;
2267#endif
2268
2269 /* Now put the new data in, and log it. */
2270 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2271 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2272
2273 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2274 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2275
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002276 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2277 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002278 } else {
2279 /* It's a leaf. make a hole in the records */
2280 union xfs_btree_rec *lrp;
2281 union xfs_btree_rec *rrp;
2282
2283 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2284 rrp = xfs_btree_rec_addr(cur, 1, right);
2285
2286 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2287
2288 /* Now put the new data in, and log it. */
2289 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2290 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2291
2292 cur->bc_ops->init_key_from_rec(&key, rrp);
2293 rkp = &key;
2294
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002295 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2296 xfs_btree_rec_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002297 }
2298
2299 /*
2300 * Decrement and log left's numrecs, bump and log right's numrecs.
2301 */
2302 xfs_btree_set_numrecs(left, --lrecs);
2303 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2304
2305 xfs_btree_set_numrecs(right, ++rrecs);
2306 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2307
2308 /*
2309 * Using a temporary cursor, update the parent key values of the
2310 * block on the right.
2311 */
2312 error = xfs_btree_dup_cursor(cur, &tcur);
2313 if (error)
2314 goto error0;
2315 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11002316 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002317
2318 error = xfs_btree_increment(tcur, level, &i);
2319 if (error)
2320 goto error1;
2321
2322 error = xfs_btree_updkey(tcur, rkp, level + 1);
2323 if (error)
2324 goto error1;
2325
2326 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2327
2328 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2329 *stat = 1;
2330 return 0;
2331
2332out0:
2333 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2334 *stat = 0;
2335 return 0;
2336
2337error0:
2338 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2339 return error;
2340
2341error1:
2342 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2343 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2344 return error;
2345}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002346
2347/*
2348 * Split cur/level block in half.
2349 * Return new block number and the key to its first
2350 * record (to be inserted into parent).
2351 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002352STATIC int /* error */
Dave Chinnercf11da92014-07-15 07:08:24 +10002353__xfs_btree_split(
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002354 struct xfs_btree_cur *cur,
2355 int level,
2356 union xfs_btree_ptr *ptrp,
2357 union xfs_btree_key *key,
2358 struct xfs_btree_cur **curp,
2359 int *stat) /* success/failure */
2360{
2361 union xfs_btree_ptr lptr; /* left sibling block ptr */
2362 struct xfs_buf *lbp; /* left buffer pointer */
2363 struct xfs_btree_block *left; /* left btree block */
2364 union xfs_btree_ptr rptr; /* right sibling block ptr */
2365 struct xfs_buf *rbp; /* right buffer pointer */
2366 struct xfs_btree_block *right; /* right btree block */
2367 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2368 struct xfs_buf *rrbp; /* right-right buffer pointer */
2369 struct xfs_btree_block *rrblock; /* right-right btree block */
2370 int lrecs;
2371 int rrecs;
2372 int src_index;
2373 int error; /* error return value */
2374#ifdef DEBUG
2375 int i;
2376#endif
2377
2378 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2379 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2380
2381 XFS_BTREE_STATS_INC(cur, split);
2382
2383 /* Set up left block (current one). */
2384 left = xfs_btree_get_block(cur, level, &lbp);
2385
2386#ifdef DEBUG
2387 error = xfs_btree_check_block(cur, left, level, lbp);
2388 if (error)
2389 goto error0;
2390#endif
2391
2392 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2393
2394 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002395 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, stat);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002396 if (error)
2397 goto error0;
2398 if (*stat == 0)
2399 goto out0;
2400 XFS_BTREE_STATS_INC(cur, alloc);
2401
2402 /* Set up the new block as "right". */
2403 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2404 if (error)
2405 goto error0;
2406
2407 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002408 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002409
2410 /*
2411 * Split the entries between the old and the new block evenly.
2412 * Make sure that if there's an odd number of entries now, that
2413 * each new block will have the same number of entries.
2414 */
2415 lrecs = xfs_btree_get_numrecs(left);
2416 rrecs = lrecs / 2;
2417 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2418 rrecs++;
2419 src_index = (lrecs - rrecs + 1);
2420
2421 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2422
2423 /*
2424 * Copy btree block entries from the left block over to the
2425 * new block, the right. Update the right block and log the
2426 * changes.
2427 */
2428 if (level > 0) {
2429 /* It's a non-leaf. Move keys and pointers. */
2430 union xfs_btree_key *lkp; /* left btree key */
2431 union xfs_btree_ptr *lpp; /* left address pointer */
2432 union xfs_btree_key *rkp; /* right btree key */
2433 union xfs_btree_ptr *rpp; /* right address pointer */
2434
2435 lkp = xfs_btree_key_addr(cur, src_index, left);
2436 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2437 rkp = xfs_btree_key_addr(cur, 1, right);
2438 rpp = xfs_btree_ptr_addr(cur, 1, right);
2439
2440#ifdef DEBUG
2441 for (i = src_index; i < rrecs; i++) {
2442 error = xfs_btree_check_ptr(cur, lpp, i, level);
2443 if (error)
2444 goto error0;
2445 }
2446#endif
2447
2448 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2449 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2450
2451 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2452 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2453
2454 /* Grab the keys to the entries moved to the right block */
2455 xfs_btree_copy_keys(cur, key, rkp, 1);
2456 } else {
2457 /* It's a leaf. Move records. */
2458 union xfs_btree_rec *lrp; /* left record pointer */
2459 union xfs_btree_rec *rrp; /* right record pointer */
2460
2461 lrp = xfs_btree_rec_addr(cur, src_index, left);
2462 rrp = xfs_btree_rec_addr(cur, 1, right);
2463
2464 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2465 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2466
2467 cur->bc_ops->init_key_from_rec(key,
2468 xfs_btree_rec_addr(cur, 1, right));
2469 }
2470
2471
2472 /*
2473 * Find the left block number by looking in the buffer.
2474 * Adjust numrecs, sibling pointers.
2475 */
2476 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2477 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2478 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2479 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2480
2481 lrecs -= rrecs;
2482 xfs_btree_set_numrecs(left, lrecs);
2483 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2484
2485 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2486 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2487
2488 /*
2489 * If there's a block to the new block's right, make that block
2490 * point back to right instead of to left.
2491 */
2492 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002493 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002494 0, &rrblock, &rrbp);
2495 if (error)
2496 goto error0;
2497 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2498 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2499 }
2500 /*
2501 * If the cursor is really in the right block, move it there.
2502 * If it's just pointing past the last entry in left, then we'll
2503 * insert there, so don't change anything in that case.
2504 */
2505 if (cur->bc_ptrs[level] > lrecs + 1) {
2506 xfs_btree_setbuf(cur, level, rbp);
2507 cur->bc_ptrs[level] -= lrecs;
2508 }
2509 /*
2510 * If there are more levels, we'll need another cursor which refers
2511 * the right block, no matter where this cursor was.
2512 */
2513 if (level + 1 < cur->bc_nlevels) {
2514 error = xfs_btree_dup_cursor(cur, curp);
2515 if (error)
2516 goto error0;
2517 (*curp)->bc_ptrs[level + 1]++;
2518 }
2519 *ptrp = rptr;
2520 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2521 *stat = 1;
2522 return 0;
2523out0:
2524 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2525 *stat = 0;
2526 return 0;
2527
2528error0:
2529 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2530 return error;
2531}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002532
Dave Chinnercf11da92014-07-15 07:08:24 +10002533struct xfs_btree_split_args {
2534 struct xfs_btree_cur *cur;
2535 int level;
2536 union xfs_btree_ptr *ptrp;
2537 union xfs_btree_key *key;
2538 struct xfs_btree_cur **curp;
2539 int *stat; /* success/failure */
2540 int result;
2541 bool kswapd; /* allocation in kswapd context */
2542 struct completion *done;
2543 struct work_struct work;
2544};
2545
2546/*
2547 * Stack switching interfaces for allocation
2548 */
2549static void
2550xfs_btree_split_worker(
2551 struct work_struct *work)
2552{
2553 struct xfs_btree_split_args *args = container_of(work,
2554 struct xfs_btree_split_args, work);
2555 unsigned long pflags;
2556 unsigned long new_pflags = PF_FSTRANS;
2557
2558 /*
2559 * we are in a transaction context here, but may also be doing work
2560 * in kswapd context, and hence we may need to inherit that state
2561 * temporarily to ensure that we don't block waiting for memory reclaim
2562 * in any way.
2563 */
2564 if (args->kswapd)
2565 new_pflags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
2566
2567 current_set_flags_nested(&pflags, new_pflags);
2568
2569 args->result = __xfs_btree_split(args->cur, args->level, args->ptrp,
2570 args->key, args->curp, args->stat);
2571 complete(args->done);
2572
2573 current_restore_flags_nested(&pflags, new_pflags);
2574}
2575
2576/*
2577 * BMBT split requests often come in with little stack to work on. Push
2578 * them off to a worker thread so there is lots of stack to use. For the other
2579 * btree types, just call directly to avoid the context switch overhead here.
2580 */
2581STATIC int /* error */
2582xfs_btree_split(
2583 struct xfs_btree_cur *cur,
2584 int level,
2585 union xfs_btree_ptr *ptrp,
2586 union xfs_btree_key *key,
2587 struct xfs_btree_cur **curp,
2588 int *stat) /* success/failure */
2589{
2590 struct xfs_btree_split_args args;
2591 DECLARE_COMPLETION_ONSTACK(done);
2592
2593 if (cur->bc_btnum != XFS_BTNUM_BMAP)
2594 return __xfs_btree_split(cur, level, ptrp, key, curp, stat);
2595
2596 args.cur = cur;
2597 args.level = level;
2598 args.ptrp = ptrp;
2599 args.key = key;
2600 args.curp = curp;
2601 args.stat = stat;
2602 args.done = &done;
2603 args.kswapd = current_is_kswapd();
2604 INIT_WORK_ONSTACK(&args.work, xfs_btree_split_worker);
2605 queue_work(xfs_alloc_wq, &args.work);
2606 wait_for_completion(&done);
2607 destroy_work_on_stack(&args.work);
2608 return args.result;
2609}
2610
2611
Christoph Hellwig344207c2008-10-30 16:57:16 +11002612/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002613 * Copy the old inode root contents into a real block and make the
2614 * broot point to it.
2615 */
2616int /* error */
2617xfs_btree_new_iroot(
2618 struct xfs_btree_cur *cur, /* btree cursor */
2619 int *logflags, /* logging flags for inode */
2620 int *stat) /* return status - 0 fail */
2621{
2622 struct xfs_buf *cbp; /* buffer for cblock */
2623 struct xfs_btree_block *block; /* btree block */
2624 struct xfs_btree_block *cblock; /* child btree block */
2625 union xfs_btree_key *ckp; /* child key pointer */
2626 union xfs_btree_ptr *cpp; /* child ptr pointer */
2627 union xfs_btree_key *kp; /* pointer to btree key */
2628 union xfs_btree_ptr *pp; /* pointer to block addr */
2629 union xfs_btree_ptr nptr; /* new block addr */
2630 int level; /* btree level */
2631 int error; /* error return code */
2632#ifdef DEBUG
2633 int i; /* loop counter */
2634#endif
2635
2636 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2637 XFS_BTREE_STATS_INC(cur, newroot);
2638
2639 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2640
2641 level = cur->bc_nlevels - 1;
2642
2643 block = xfs_btree_get_iroot(cur);
2644 pp = xfs_btree_ptr_addr(cur, 1, block);
2645
2646 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002647 error = cur->bc_ops->alloc_block(cur, pp, &nptr, stat);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002648 if (error)
2649 goto error0;
2650 if (*stat == 0) {
2651 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2652 return 0;
2653 }
2654 XFS_BTREE_STATS_INC(cur, alloc);
2655
2656 /* Copy the root into a real block. */
2657 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2658 if (error)
2659 goto error0;
2660
Dave Chinner088c9f62013-06-12 12:19:08 +10002661 /*
2662 * we can't just memcpy() the root in for CRC enabled btree blocks.
2663 * In that case have to also ensure the blkno remains correct
2664 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002665 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002666 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2667 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2668 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2669 else
2670 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2671 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002672
2673 be16_add_cpu(&block->bb_level, 1);
2674 xfs_btree_set_numrecs(block, 1);
2675 cur->bc_nlevels++;
2676 cur->bc_ptrs[level + 1] = 1;
2677
2678 kp = xfs_btree_key_addr(cur, 1, block);
2679 ckp = xfs_btree_key_addr(cur, 1, cblock);
2680 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2681
2682 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2683#ifdef DEBUG
2684 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2685 error = xfs_btree_check_ptr(cur, pp, i, level);
2686 if (error)
2687 goto error0;
2688 }
2689#endif
2690 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2691
2692#ifdef DEBUG
2693 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2694 if (error)
2695 goto error0;
2696#endif
2697 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2698
2699 xfs_iroot_realloc(cur->bc_private.b.ip,
2700 1 - xfs_btree_get_numrecs(cblock),
2701 cur->bc_private.b.whichfork);
2702
2703 xfs_btree_setbuf(cur, level, cbp);
2704
2705 /*
2706 * Do all this logging at the end so that
2707 * the root is at the right level.
2708 */
2709 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2710 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2711 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2712
2713 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06002714 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002715 *stat = 1;
2716 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2717 return 0;
2718error0:
2719 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2720 return error;
2721}
2722
2723/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11002724 * Allocate a new root block, fill it in.
2725 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002726STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11002727xfs_btree_new_root(
2728 struct xfs_btree_cur *cur, /* btree cursor */
2729 int *stat) /* success/failure */
2730{
2731 struct xfs_btree_block *block; /* one half of the old root block */
2732 struct xfs_buf *bp; /* buffer containing block */
2733 int error; /* error return value */
2734 struct xfs_buf *lbp; /* left buffer pointer */
2735 struct xfs_btree_block *left; /* left btree block */
2736 struct xfs_buf *nbp; /* new (root) buffer */
2737 struct xfs_btree_block *new; /* new (root) btree block */
2738 int nptr; /* new value for key index, 1 or 2 */
2739 struct xfs_buf *rbp; /* right buffer pointer */
2740 struct xfs_btree_block *right; /* right btree block */
2741 union xfs_btree_ptr rptr;
2742 union xfs_btree_ptr lptr;
2743
2744 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2745 XFS_BTREE_STATS_INC(cur, newroot);
2746
2747 /* initialise our start point from the cursor */
2748 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2749
2750 /* Allocate the new block. If we can't do it, we're toast. Give up. */
Eric Sandeen6f8950c2014-04-14 19:03:53 +10002751 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, stat);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002752 if (error)
2753 goto error0;
2754 if (*stat == 0)
2755 goto out0;
2756 XFS_BTREE_STATS_INC(cur, alloc);
2757
2758 /* Set up the new block. */
2759 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2760 if (error)
2761 goto error0;
2762
2763 /* Set the root in the holding structure increasing the level by 1. */
2764 cur->bc_ops->set_root(cur, &lptr, 1);
2765
2766 /*
2767 * At the previous root level there are now two blocks: the old root,
2768 * and the new block generated when it was split. We don't know which
2769 * one the cursor is pointing at, so we set up variables "left" and
2770 * "right" for each case.
2771 */
2772 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2773
2774#ifdef DEBUG
2775 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2776 if (error)
2777 goto error0;
2778#endif
2779
2780 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2781 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2782 /* Our block is left, pick up the right block. */
2783 lbp = bp;
2784 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2785 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002786 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002787 if (error)
2788 goto error0;
2789 bp = rbp;
2790 nptr = 1;
2791 } else {
2792 /* Our block is right, pick up the left block. */
2793 rbp = bp;
2794 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2795 right = block;
2796 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002797 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002798 if (error)
2799 goto error0;
2800 bp = lbp;
2801 nptr = 2;
2802 }
2803 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002804 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002805 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2806 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2807 !xfs_btree_ptr_is_null(cur, &rptr));
2808
2809 /* Fill in the key data in the new root. */
2810 if (xfs_btree_get_level(left) > 0) {
2811 xfs_btree_copy_keys(cur,
2812 xfs_btree_key_addr(cur, 1, new),
2813 xfs_btree_key_addr(cur, 1, left), 1);
2814 xfs_btree_copy_keys(cur,
2815 xfs_btree_key_addr(cur, 2, new),
2816 xfs_btree_key_addr(cur, 1, right), 1);
2817 } else {
2818 cur->bc_ops->init_key_from_rec(
2819 xfs_btree_key_addr(cur, 1, new),
2820 xfs_btree_rec_addr(cur, 1, left));
2821 cur->bc_ops->init_key_from_rec(
2822 xfs_btree_key_addr(cur, 2, new),
2823 xfs_btree_rec_addr(cur, 1, right));
2824 }
2825 xfs_btree_log_keys(cur, nbp, 1, 2);
2826
2827 /* Fill in the pointer data in the new root. */
2828 xfs_btree_copy_ptrs(cur,
2829 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2830 xfs_btree_copy_ptrs(cur,
2831 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2832 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2833
2834 /* Fix up the cursor. */
2835 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2836 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2837 cur->bc_nlevels++;
2838 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2839 *stat = 1;
2840 return 0;
2841error0:
2842 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2843 return error;
2844out0:
2845 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2846 *stat = 0;
2847 return 0;
2848}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002849
2850STATIC int
2851xfs_btree_make_block_unfull(
2852 struct xfs_btree_cur *cur, /* btree cursor */
2853 int level, /* btree level */
2854 int numrecs,/* # of recs in block */
2855 int *oindex,/* old tree index */
2856 int *index, /* new tree index */
2857 union xfs_btree_ptr *nptr, /* new btree ptr */
2858 struct xfs_btree_cur **ncur, /* new btree cursor */
2859 union xfs_btree_rec *nrec, /* new record */
2860 int *stat)
2861{
2862 union xfs_btree_key key; /* new btree key value */
2863 int error = 0;
2864
2865 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2866 level == cur->bc_nlevels - 1) {
2867 struct xfs_inode *ip = cur->bc_private.b.ip;
2868
2869 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2870 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002871 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2872 } else {
2873 /* A root block that needs replacing */
2874 int logflags = 0;
2875
2876 error = xfs_btree_new_iroot(cur, &logflags, stat);
2877 if (error || *stat == 0)
2878 return error;
2879
2880 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2881 }
2882
2883 return 0;
2884 }
2885
2886 /* First, try shifting an entry to the right neighbor. */
2887 error = xfs_btree_rshift(cur, level, stat);
2888 if (error || *stat)
2889 return error;
2890
2891 /* Next, try shifting an entry to the left neighbor. */
2892 error = xfs_btree_lshift(cur, level, stat);
2893 if (error)
2894 return error;
2895
2896 if (*stat) {
2897 *oindex = *index = cur->bc_ptrs[level];
2898 return 0;
2899 }
2900
2901 /*
2902 * Next, try splitting the current block in half.
2903 *
2904 * If this works we have to re-set our variables because we
2905 * could be in a different block now.
2906 */
2907 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2908 if (error || *stat == 0)
2909 return error;
2910
2911
2912 *index = cur->bc_ptrs[level];
2913 cur->bc_ops->init_rec_from_key(&key, nrec);
2914 return 0;
2915}
2916
2917/*
2918 * Insert one record/level. Return information to the caller
2919 * allowing the next level up to proceed if necessary.
2920 */
2921STATIC int
2922xfs_btree_insrec(
2923 struct xfs_btree_cur *cur, /* btree cursor */
2924 int level, /* level to insert record at */
2925 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2926 union xfs_btree_rec *recp, /* i/o: record data inserted */
2927 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2928 int *stat) /* success/failure */
2929{
2930 struct xfs_btree_block *block; /* btree block */
2931 struct xfs_buf *bp; /* buffer for block */
2932 union xfs_btree_key key; /* btree key */
2933 union xfs_btree_ptr nptr; /* new block ptr */
2934 struct xfs_btree_cur *ncur; /* new btree cursor */
2935 union xfs_btree_rec nrec; /* new record count */
2936 int optr; /* old key/record index */
2937 int ptr; /* key/record index */
2938 int numrecs;/* number of records */
2939 int error; /* error return value */
2940#ifdef DEBUG
2941 int i;
2942#endif
2943
2944 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2945 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2946
2947 ncur = NULL;
2948
2949 /*
2950 * If we have an external root pointer, and we've made it to the
2951 * root level, allocate a new root block and we're done.
2952 */
2953 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2954 (level >= cur->bc_nlevels)) {
2955 error = xfs_btree_new_root(cur, stat);
2956 xfs_btree_set_ptr_null(cur, ptrp);
2957
2958 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2959 return error;
2960 }
2961
2962 /* If we're off the left edge, return failure. */
2963 ptr = cur->bc_ptrs[level];
2964 if (ptr == 0) {
2965 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2966 *stat = 0;
2967 return 0;
2968 }
2969
2970 /* Make a key out of the record data to be inserted, and save it. */
2971 cur->bc_ops->init_key_from_rec(&key, recp);
2972
2973 optr = ptr;
2974
2975 XFS_BTREE_STATS_INC(cur, insrec);
2976
2977 /* Get pointers to the btree buffer and block. */
2978 block = xfs_btree_get_block(cur, level, &bp);
2979 numrecs = xfs_btree_get_numrecs(block);
2980
2981#ifdef DEBUG
2982 error = xfs_btree_check_block(cur, block, level, bp);
2983 if (error)
2984 goto error0;
2985
2986 /* Check that the new entry is being inserted in the right place. */
2987 if (ptr <= numrecs) {
2988 if (level == 0) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002989 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2990 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002991 } else {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002992 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2993 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002994 }
2995 }
2996#endif
2997
2998 /*
2999 * If the block is full, we can't insert the new entry until we
3000 * make the block un-full.
3001 */
3002 xfs_btree_set_ptr_null(cur, &nptr);
3003 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
3004 error = xfs_btree_make_block_unfull(cur, level, numrecs,
3005 &optr, &ptr, &nptr, &ncur, &nrec, stat);
3006 if (error || *stat == 0)
3007 goto error0;
3008 }
3009
3010 /*
3011 * The current block may have changed if the block was
3012 * previously full and we have just made space in it.
3013 */
3014 block = xfs_btree_get_block(cur, level, &bp);
3015 numrecs = xfs_btree_get_numrecs(block);
3016
3017#ifdef DEBUG
3018 error = xfs_btree_check_block(cur, block, level, bp);
3019 if (error)
3020 return error;
3021#endif
3022
3023 /*
3024 * At this point we know there's room for our new entry in the block
3025 * we're pointing at.
3026 */
3027 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
3028
3029 if (level > 0) {
3030 /* It's a nonleaf. make a hole in the keys and ptrs */
3031 union xfs_btree_key *kp;
3032 union xfs_btree_ptr *pp;
3033
3034 kp = xfs_btree_key_addr(cur, ptr, block);
3035 pp = xfs_btree_ptr_addr(cur, ptr, block);
3036
3037#ifdef DEBUG
3038 for (i = numrecs - ptr; i >= 0; i--) {
3039 error = xfs_btree_check_ptr(cur, pp, i, level);
3040 if (error)
3041 return error;
3042 }
3043#endif
3044
3045 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
3046 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
3047
3048#ifdef DEBUG
3049 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
3050 if (error)
3051 goto error0;
3052#endif
3053
3054 /* Now put the new data in, bump numrecs and log it. */
3055 xfs_btree_copy_keys(cur, kp, &key, 1);
3056 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
3057 numrecs++;
3058 xfs_btree_set_numrecs(block, numrecs);
3059 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
3060 xfs_btree_log_keys(cur, bp, ptr, numrecs);
3061#ifdef DEBUG
3062 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003063 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
3064 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003065 }
3066#endif
3067 } else {
3068 /* It's a leaf. make a hole in the records */
3069 union xfs_btree_rec *rp;
3070
3071 rp = xfs_btree_rec_addr(cur, ptr, block);
3072
3073 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
3074
3075 /* Now put the new data in, bump numrecs and log it. */
3076 xfs_btree_copy_recs(cur, rp, recp, 1);
3077 xfs_btree_set_numrecs(block, ++numrecs);
3078 xfs_btree_log_recs(cur, bp, ptr, numrecs);
3079#ifdef DEBUG
3080 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11003081 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
3082 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003083 }
3084#endif
3085 }
3086
3087 /* Log the new number of records in the btree header. */
3088 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3089
3090 /* If we inserted at the start of a block, update the parents' keys. */
3091 if (optr == 1) {
3092 error = xfs_btree_updkey(cur, &key, level + 1);
3093 if (error)
3094 goto error0;
3095 }
3096
3097 /*
3098 * If we are tracking the last record in the tree and
3099 * we are at the far right edge of the tree, update it.
3100 */
3101 if (xfs_btree_is_lastrec(cur, block, level)) {
3102 cur->bc_ops->update_lastrec(cur, block, recp,
3103 ptr, LASTREC_INSREC);
3104 }
3105
3106 /*
3107 * Return the new block number, if any.
3108 * If there is one, give back a record value and a cursor too.
3109 */
3110 *ptrp = nptr;
3111 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3112 *recp = nrec;
3113 *curp = ncur;
3114 }
3115
3116 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3117 *stat = 1;
3118 return 0;
3119
3120error0:
3121 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3122 return error;
3123}
3124
3125/*
3126 * Insert the record at the point referenced by cur.
3127 *
3128 * A multi-level split of the tree on insert will invalidate the original
3129 * cursor. All callers of this function should assume that the cursor is
3130 * no longer valid and revalidate it.
3131 */
3132int
3133xfs_btree_insert(
3134 struct xfs_btree_cur *cur,
3135 int *stat)
3136{
3137 int error; /* error return value */
3138 int i; /* result value, 0 for failure */
3139 int level; /* current level number in btree */
3140 union xfs_btree_ptr nptr; /* new block number (split result) */
3141 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3142 struct xfs_btree_cur *pcur; /* previous level's cursor */
3143 union xfs_btree_rec rec; /* record to insert */
3144
3145 level = 0;
3146 ncur = NULL;
3147 pcur = cur;
3148
3149 xfs_btree_set_ptr_null(cur, &nptr);
3150 cur->bc_ops->init_rec_from_cur(cur, &rec);
3151
3152 /*
3153 * Loop going up the tree, starting at the leaf level.
3154 * Stop when we don't get a split block, that must mean that
3155 * the insert is finished with this level.
3156 */
3157 do {
3158 /*
3159 * Insert nrec/nptr into this level of the tree.
3160 * Note if we fail, nptr will be null.
3161 */
3162 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3163 if (error) {
3164 if (pcur != cur)
3165 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3166 goto error0;
3167 }
3168
Eric Sandeenc29aad42015-02-23 22:39:08 +11003169 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003170 level++;
3171
3172 /*
3173 * See if the cursor we just used is trash.
3174 * Can't trash the caller's cursor, but otherwise we should
3175 * if ncur is a new cursor or we're about to be done.
3176 */
3177 if (pcur != cur &&
3178 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3179 /* Save the state from the cursor before we trash it */
3180 if (cur->bc_ops->update_cursor)
3181 cur->bc_ops->update_cursor(pcur, cur);
3182 cur->bc_nlevels = pcur->bc_nlevels;
3183 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3184 }
3185 /* If we got a new cursor, switch to it. */
3186 if (ncur) {
3187 pcur = ncur;
3188 ncur = NULL;
3189 }
3190 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3191
3192 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3193 *stat = i;
3194 return 0;
3195error0:
3196 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3197 return error;
3198}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003199
3200/*
3201 * Try to merge a non-leaf block back into the inode root.
3202 *
3203 * Note: the killroot names comes from the fact that we're effectively
3204 * killing the old root block. But because we can't just delete the
3205 * inode we have to copy the single block it was pointing to into the
3206 * inode.
3207 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003208STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003209xfs_btree_kill_iroot(
3210 struct xfs_btree_cur *cur)
3211{
3212 int whichfork = cur->bc_private.b.whichfork;
3213 struct xfs_inode *ip = cur->bc_private.b.ip;
3214 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3215 struct xfs_btree_block *block;
3216 struct xfs_btree_block *cblock;
3217 union xfs_btree_key *kp;
3218 union xfs_btree_key *ckp;
3219 union xfs_btree_ptr *pp;
3220 union xfs_btree_ptr *cpp;
3221 struct xfs_buf *cbp;
3222 int level;
3223 int index;
3224 int numrecs;
Christoph Hellwig196328e2016-02-08 14:58:07 +11003225 int error;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003226#ifdef DEBUG
3227 union xfs_btree_ptr ptr;
3228 int i;
3229#endif
3230
3231 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3232
3233 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3234 ASSERT(cur->bc_nlevels > 1);
3235
3236 /*
3237 * Don't deal with the root block needs to be a leaf case.
3238 * We're just going to turn the thing back into extents anyway.
3239 */
3240 level = cur->bc_nlevels - 1;
3241 if (level == 1)
3242 goto out0;
3243
3244 /*
3245 * Give up if the root has multiple children.
3246 */
3247 block = xfs_btree_get_iroot(cur);
3248 if (xfs_btree_get_numrecs(block) != 1)
3249 goto out0;
3250
3251 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3252 numrecs = xfs_btree_get_numrecs(cblock);
3253
3254 /*
3255 * Only do this if the next level will fit.
3256 * Then the data must be copied up to the inode,
3257 * instead of freeing the root you free the next level.
3258 */
3259 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3260 goto out0;
3261
3262 XFS_BTREE_STATS_INC(cur, killroot);
3263
3264#ifdef DEBUG
3265 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3266 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3267 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3268 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3269#endif
3270
3271 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3272 if (index) {
3273 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3274 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003275 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003276 }
3277
3278 be16_add_cpu(&block->bb_numrecs, index);
3279 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3280
3281 kp = xfs_btree_key_addr(cur, 1, block);
3282 ckp = xfs_btree_key_addr(cur, 1, cblock);
3283 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3284
3285 pp = xfs_btree_ptr_addr(cur, 1, block);
3286 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3287#ifdef DEBUG
3288 for (i = 0; i < numrecs; i++) {
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003289 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3290 if (error) {
3291 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3292 return error;
3293 }
3294 }
3295#endif
3296 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3297
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003298 error = xfs_btree_free_block(cur, cbp);
Christoph Hellwig196328e2016-02-08 14:58:07 +11003299 if (error) {
3300 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3301 return error;
3302 }
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003303
3304 cur->bc_bufs[level - 1] = NULL;
3305 be16_add_cpu(&block->bb_level, -1);
3306 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003307 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003308 cur->bc_nlevels--;
3309out0:
3310 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3311 return 0;
3312}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003313
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003314/*
3315 * Kill the current root node, and replace it with it's only child node.
3316 */
3317STATIC int
3318xfs_btree_kill_root(
3319 struct xfs_btree_cur *cur,
3320 struct xfs_buf *bp,
3321 int level,
3322 union xfs_btree_ptr *newroot)
3323{
3324 int error;
3325
3326 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3327 XFS_BTREE_STATS_INC(cur, killroot);
3328
3329 /*
3330 * Update the root pointer, decreasing the level by 1 and then
3331 * free the old root.
3332 */
3333 cur->bc_ops->set_root(cur, newroot, -1);
3334
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003335 error = xfs_btree_free_block(cur, bp);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003336 if (error) {
3337 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3338 return error;
3339 }
3340
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003341 cur->bc_bufs[level] = NULL;
3342 cur->bc_ra[level] = 0;
3343 cur->bc_nlevels--;
3344
3345 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3346 return 0;
3347}
3348
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003349STATIC int
3350xfs_btree_dec_cursor(
3351 struct xfs_btree_cur *cur,
3352 int level,
3353 int *stat)
3354{
3355 int error;
3356 int i;
3357
3358 if (level > 0) {
3359 error = xfs_btree_decrement(cur, level, &i);
3360 if (error)
3361 return error;
3362 }
3363
3364 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3365 *stat = 1;
3366 return 0;
3367}
3368
3369/*
3370 * Single level of the btree record deletion routine.
3371 * Delete record pointed to by cur/level.
3372 * Remove the record from its block then rebalance the tree.
3373 * Return 0 for error, 1 for done, 2 to go on to the next level.
3374 */
3375STATIC int /* error */
3376xfs_btree_delrec(
3377 struct xfs_btree_cur *cur, /* btree cursor */
3378 int level, /* level removing record from */
3379 int *stat) /* fail/done/go-on */
3380{
3381 struct xfs_btree_block *block; /* btree block */
3382 union xfs_btree_ptr cptr; /* current block ptr */
3383 struct xfs_buf *bp; /* buffer for block */
3384 int error; /* error return value */
3385 int i; /* loop counter */
3386 union xfs_btree_key key; /* storage for keyp */
3387 union xfs_btree_key *keyp = &key; /* passed to the next level */
3388 union xfs_btree_ptr lptr; /* left sibling block ptr */
3389 struct xfs_buf *lbp; /* left buffer pointer */
3390 struct xfs_btree_block *left; /* left btree block */
3391 int lrecs = 0; /* left record count */
3392 int ptr; /* key/record index */
3393 union xfs_btree_ptr rptr; /* right sibling block ptr */
3394 struct xfs_buf *rbp; /* right buffer pointer */
3395 struct xfs_btree_block *right; /* right btree block */
3396 struct xfs_btree_block *rrblock; /* right-right btree block */
3397 struct xfs_buf *rrbp; /* right-right buffer pointer */
3398 int rrecs = 0; /* right record count */
3399 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3400 int numrecs; /* temporary numrec count */
3401
3402 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3403 XFS_BTREE_TRACE_ARGI(cur, level);
3404
3405 tcur = NULL;
3406
3407 /* Get the index of the entry being deleted, check for nothing there. */
3408 ptr = cur->bc_ptrs[level];
3409 if (ptr == 0) {
3410 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3411 *stat = 0;
3412 return 0;
3413 }
3414
3415 /* Get the buffer & block containing the record or key/ptr. */
3416 block = xfs_btree_get_block(cur, level, &bp);
3417 numrecs = xfs_btree_get_numrecs(block);
3418
3419#ifdef DEBUG
3420 error = xfs_btree_check_block(cur, block, level, bp);
3421 if (error)
3422 goto error0;
3423#endif
3424
3425 /* Fail if we're off the end of the block. */
3426 if (ptr > numrecs) {
3427 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3428 *stat = 0;
3429 return 0;
3430 }
3431
3432 XFS_BTREE_STATS_INC(cur, delrec);
3433 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3434
3435 /* Excise the entries being deleted. */
3436 if (level > 0) {
3437 /* It's a nonleaf. operate on keys and ptrs */
3438 union xfs_btree_key *lkp;
3439 union xfs_btree_ptr *lpp;
3440
3441 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3442 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3443
3444#ifdef DEBUG
3445 for (i = 0; i < numrecs - ptr; i++) {
3446 error = xfs_btree_check_ptr(cur, lpp, i, level);
3447 if (error)
3448 goto error0;
3449 }
3450#endif
3451
3452 if (ptr < numrecs) {
3453 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3454 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3455 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3456 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3457 }
3458
3459 /*
3460 * If it's the first record in the block, we'll need to pass a
3461 * key up to the next level (updkey).
3462 */
3463 if (ptr == 1)
3464 keyp = xfs_btree_key_addr(cur, 1, block);
3465 } else {
3466 /* It's a leaf. operate on records */
3467 if (ptr < numrecs) {
3468 xfs_btree_shift_recs(cur,
3469 xfs_btree_rec_addr(cur, ptr + 1, block),
3470 -1, numrecs - ptr);
3471 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3472 }
3473
3474 /*
3475 * If it's the first record in the block, we'll need a key
3476 * structure to pass up to the next level (updkey).
3477 */
3478 if (ptr == 1) {
3479 cur->bc_ops->init_key_from_rec(&key,
3480 xfs_btree_rec_addr(cur, 1, block));
3481 keyp = &key;
3482 }
3483 }
3484
3485 /*
3486 * Decrement and log the number of entries in the block.
3487 */
3488 xfs_btree_set_numrecs(block, --numrecs);
3489 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3490
3491 /*
3492 * If we are tracking the last record in the tree and
3493 * we are at the far right edge of the tree, update it.
3494 */
3495 if (xfs_btree_is_lastrec(cur, block, level)) {
3496 cur->bc_ops->update_lastrec(cur, block, NULL,
3497 ptr, LASTREC_DELREC);
3498 }
3499
3500 /*
3501 * We're at the root level. First, shrink the root block in-memory.
3502 * Try to get rid of the next level down. If we can't then there's
3503 * nothing left to do.
3504 */
3505 if (level == cur->bc_nlevels - 1) {
3506 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3507 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3508 cur->bc_private.b.whichfork);
3509
3510 error = xfs_btree_kill_iroot(cur);
3511 if (error)
3512 goto error0;
3513
3514 error = xfs_btree_dec_cursor(cur, level, stat);
3515 if (error)
3516 goto error0;
3517 *stat = 1;
3518 return 0;
3519 }
3520
3521 /*
3522 * If this is the root level, and there's only one entry left,
3523 * and it's NOT the leaf level, then we can get rid of this
3524 * level.
3525 */
3526 if (numrecs == 1 && level > 0) {
3527 union xfs_btree_ptr *pp;
3528 /*
3529 * pp is still set to the first pointer in the block.
3530 * Make it the new root of the btree.
3531 */
3532 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003533 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003534 if (error)
3535 goto error0;
3536 } else if (level > 0) {
3537 error = xfs_btree_dec_cursor(cur, level, stat);
3538 if (error)
3539 goto error0;
3540 }
3541 *stat = 1;
3542 return 0;
3543 }
3544
3545 /*
3546 * If we deleted the leftmost entry in the block, update the
3547 * key values above us in the tree.
3548 */
3549 if (ptr == 1) {
3550 error = xfs_btree_updkey(cur, keyp, level + 1);
3551 if (error)
3552 goto error0;
3553 }
3554
3555 /*
3556 * If the number of records remaining in the block is at least
3557 * the minimum, we're done.
3558 */
3559 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3560 error = xfs_btree_dec_cursor(cur, level, stat);
3561 if (error)
3562 goto error0;
3563 return 0;
3564 }
3565
3566 /*
3567 * Otherwise, we have to move some records around to keep the
3568 * tree balanced. Look at the left and right sibling blocks to
3569 * see if we can re-balance by moving only one record.
3570 */
3571 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3572 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3573
3574 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3575 /*
3576 * One child of root, need to get a chance to copy its contents
3577 * into the root and delete it. Can't go up to next level,
3578 * there's nothing to delete there.
3579 */
3580 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3581 xfs_btree_ptr_is_null(cur, &lptr) &&
3582 level == cur->bc_nlevels - 2) {
3583 error = xfs_btree_kill_iroot(cur);
3584 if (!error)
3585 error = xfs_btree_dec_cursor(cur, level, stat);
3586 if (error)
3587 goto error0;
3588 return 0;
3589 }
3590 }
3591
3592 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3593 !xfs_btree_ptr_is_null(cur, &lptr));
3594
3595 /*
3596 * Duplicate the cursor so our btree manipulations here won't
3597 * disrupt the next level up.
3598 */
3599 error = xfs_btree_dup_cursor(cur, &tcur);
3600 if (error)
3601 goto error0;
3602
3603 /*
3604 * If there's a right sibling, see if it's ok to shift an entry
3605 * out of it.
3606 */
3607 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3608 /*
3609 * Move the temp cursor to the last entry in the next block.
3610 * Actually any entry but the first would suffice.
3611 */
3612 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003613 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003614
3615 error = xfs_btree_increment(tcur, level, &i);
3616 if (error)
3617 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003618 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003619
3620 i = xfs_btree_lastrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003621 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003622
3623 /* Grab a pointer to the block. */
3624 right = xfs_btree_get_block(tcur, level, &rbp);
3625#ifdef DEBUG
3626 error = xfs_btree_check_block(tcur, right, level, rbp);
3627 if (error)
3628 goto error0;
3629#endif
3630 /* Grab the current block number, for future use. */
3631 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3632
3633 /*
3634 * If right block is full enough so that removing one entry
3635 * won't make it too empty, and left-shifting an entry out
3636 * of right to us works, we're done.
3637 */
3638 if (xfs_btree_get_numrecs(right) - 1 >=
3639 cur->bc_ops->get_minrecs(tcur, level)) {
3640 error = xfs_btree_lshift(tcur, level, &i);
3641 if (error)
3642 goto error0;
3643 if (i) {
3644 ASSERT(xfs_btree_get_numrecs(block) >=
3645 cur->bc_ops->get_minrecs(tcur, level));
3646
3647 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3648 tcur = NULL;
3649
3650 error = xfs_btree_dec_cursor(cur, level, stat);
3651 if (error)
3652 goto error0;
3653 return 0;
3654 }
3655 }
3656
3657 /*
3658 * Otherwise, grab the number of records in right for
3659 * future reference, and fix up the temp cursor to point
3660 * to our block again (last record).
3661 */
3662 rrecs = xfs_btree_get_numrecs(right);
3663 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3664 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003665 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003666
3667 error = xfs_btree_decrement(tcur, level, &i);
3668 if (error)
3669 goto error0;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003670 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003671 }
3672 }
3673
3674 /*
3675 * If there's a left sibling, see if it's ok to shift an entry
3676 * out of it.
3677 */
3678 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3679 /*
3680 * Move the temp cursor to the first entry in the
3681 * previous block.
3682 */
3683 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003684 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003685
3686 error = xfs_btree_decrement(tcur, level, &i);
3687 if (error)
3688 goto error0;
3689 i = xfs_btree_firstrec(tcur, level);
Eric Sandeenc29aad42015-02-23 22:39:08 +11003690 XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, i == 1, error0);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003691
3692 /* Grab a pointer to the block. */
3693 left = xfs_btree_get_block(tcur, level, &lbp);
3694#ifdef DEBUG
3695 error = xfs_btree_check_block(cur, left, level, lbp);
3696 if (error)
3697 goto error0;
3698#endif
3699 /* Grab the current block number, for future use. */
3700 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3701
3702 /*
3703 * If left block is full enough so that removing one entry
3704 * won't make it too empty, and right-shifting an entry out
3705 * of left to us works, we're done.
3706 */
3707 if (xfs_btree_get_numrecs(left) - 1 >=
3708 cur->bc_ops->get_minrecs(tcur, level)) {
3709 error = xfs_btree_rshift(tcur, level, &i);
3710 if (error)
3711 goto error0;
3712 if (i) {
3713 ASSERT(xfs_btree_get_numrecs(block) >=
3714 cur->bc_ops->get_minrecs(tcur, level));
3715 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3716 tcur = NULL;
3717 if (level == 0)
3718 cur->bc_ptrs[0]++;
3719 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3720 *stat = 1;
3721 return 0;
3722 }
3723 }
3724
3725 /*
3726 * Otherwise, grab the number of records in right for
3727 * future reference.
3728 */
3729 lrecs = xfs_btree_get_numrecs(left);
3730 }
3731
3732 /* Delete the temp cursor, we're done with it. */
3733 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3734 tcur = NULL;
3735
3736 /* If here, we need to do a join to keep the tree balanced. */
3737 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3738
3739 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3740 lrecs + xfs_btree_get_numrecs(block) <=
3741 cur->bc_ops->get_maxrecs(cur, level)) {
3742 /*
3743 * Set "right" to be the starting block,
3744 * "left" to be the left neighbor.
3745 */
3746 rptr = cptr;
3747 right = block;
3748 rbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003749 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003750 if (error)
3751 goto error0;
3752
3753 /*
3754 * If that won't work, see if we can join with the right neighbor block.
3755 */
3756 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3757 rrecs + xfs_btree_get_numrecs(block) <=
3758 cur->bc_ops->get_maxrecs(cur, level)) {
3759 /*
3760 * Set "left" to be the starting block,
3761 * "right" to be the right neighbor.
3762 */
3763 lptr = cptr;
3764 left = block;
3765 lbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003766 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003767 if (error)
3768 goto error0;
3769
3770 /*
3771 * Otherwise, we can't fix the imbalance.
3772 * Just return. This is probably a logic error, but it's not fatal.
3773 */
3774 } else {
3775 error = xfs_btree_dec_cursor(cur, level, stat);
3776 if (error)
3777 goto error0;
3778 return 0;
3779 }
3780
3781 rrecs = xfs_btree_get_numrecs(right);
3782 lrecs = xfs_btree_get_numrecs(left);
3783
3784 /*
3785 * We're now going to join "left" and "right" by moving all the stuff
3786 * in "right" to "left" and deleting "right".
3787 */
3788 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3789 if (level > 0) {
3790 /* It's a non-leaf. Move keys and pointers. */
3791 union xfs_btree_key *lkp; /* left btree key */
3792 union xfs_btree_ptr *lpp; /* left address pointer */
3793 union xfs_btree_key *rkp; /* right btree key */
3794 union xfs_btree_ptr *rpp; /* right address pointer */
3795
3796 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3797 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3798 rkp = xfs_btree_key_addr(cur, 1, right);
3799 rpp = xfs_btree_ptr_addr(cur, 1, right);
3800#ifdef DEBUG
3801 for (i = 1; i < rrecs; i++) {
3802 error = xfs_btree_check_ptr(cur, rpp, i, level);
3803 if (error)
3804 goto error0;
3805 }
3806#endif
3807 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3808 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3809
3810 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3811 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3812 } else {
3813 /* It's a leaf. Move records. */
3814 union xfs_btree_rec *lrp; /* left record pointer */
3815 union xfs_btree_rec *rrp; /* right record pointer */
3816
3817 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3818 rrp = xfs_btree_rec_addr(cur, 1, right);
3819
3820 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3821 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3822 }
3823
3824 XFS_BTREE_STATS_INC(cur, join);
3825
3826 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003827 * Fix up the number of records and right block pointer in the
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003828 * surviving block, and log it.
3829 */
3830 xfs_btree_set_numrecs(left, lrecs + rrecs);
3831 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3832 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3833 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3834
3835 /* If there is a right sibling, point it to the remaining block. */
3836 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3837 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003838 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003839 if (error)
3840 goto error0;
3841 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3842 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3843 }
3844
3845 /* Free the deleted block. */
Christoph Hellwigc46ee8a2016-02-08 14:58:07 +11003846 error = xfs_btree_free_block(cur, rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003847 if (error)
3848 goto error0;
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003849
3850 /*
3851 * If we joined with the left neighbor, set the buffer in the
3852 * cursor to the left block, and fix up the index.
3853 */
3854 if (bp != lbp) {
3855 cur->bc_bufs[level] = lbp;
3856 cur->bc_ptrs[level] += lrecs;
3857 cur->bc_ra[level] = 0;
3858 }
3859 /*
3860 * If we joined with the right neighbor and there's a level above
3861 * us, increment the cursor at that level.
3862 */
3863 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3864 (level + 1 < cur->bc_nlevels)) {
3865 error = xfs_btree_increment(cur, level + 1, &i);
3866 if (error)
3867 goto error0;
3868 }
3869
3870 /*
3871 * Readjust the ptr at this level if it's not a leaf, since it's
3872 * still pointing at the deletion point, which makes the cursor
3873 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3874 * We can't use decrement because it would change the next level up.
3875 */
3876 if (level > 0)
3877 cur->bc_ptrs[level]--;
3878
3879 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3880 /* Return value means the next level up has something to do. */
3881 *stat = 2;
3882 return 0;
3883
3884error0:
3885 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3886 if (tcur)
3887 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3888 return error;
3889}
3890
3891/*
3892 * Delete the record pointed to by cur.
3893 * The cursor refers to the place where the record was (could be inserted)
3894 * when the operation returns.
3895 */
3896int /* error */
3897xfs_btree_delete(
3898 struct xfs_btree_cur *cur,
3899 int *stat) /* success/failure */
3900{
3901 int error; /* error return value */
3902 int level;
3903 int i;
3904
3905 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3906
3907 /*
3908 * Go up the tree, starting at leaf level.
3909 *
3910 * If 2 is returned then a join was done; go to the next level.
3911 * Otherwise we are done.
3912 */
3913 for (level = 0, i = 2; i == 2; level++) {
3914 error = xfs_btree_delrec(cur, level, &i);
3915 if (error)
3916 goto error0;
3917 }
3918
3919 if (i == 0) {
3920 for (level = 1; level < cur->bc_nlevels; level++) {
3921 if (cur->bc_ptrs[level] == 0) {
3922 error = xfs_btree_decrement(cur, level, &i);
3923 if (error)
3924 goto error0;
3925 break;
3926 }
3927 }
3928 }
3929
3930 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3931 *stat = i;
3932 return 0;
3933error0:
3934 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3935 return error;
3936}
Christoph Hellwig8cc938f2008-10-30 16:58:11 +11003937
3938/*
3939 * Get the data from the pointed-to record.
3940 */
3941int /* error */
3942xfs_btree_get_rec(
3943 struct xfs_btree_cur *cur, /* btree cursor */
3944 union xfs_btree_rec **recp, /* output: btree record */
3945 int *stat) /* output: success/failure */
3946{
3947 struct xfs_btree_block *block; /* btree block */
3948 struct xfs_buf *bp; /* buffer pointer */
3949 int ptr; /* record number */
3950#ifdef DEBUG
3951 int error; /* error return value */
3952#endif
3953
3954 ptr = cur->bc_ptrs[0];
3955 block = xfs_btree_get_block(cur, 0, &bp);
3956
3957#ifdef DEBUG
3958 error = xfs_btree_check_block(cur, block, 0, bp);
3959 if (error)
3960 return error;
3961#endif
3962
3963 /*
3964 * Off the right end or left end, return failure.
3965 */
3966 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3967 *stat = 0;
3968 return 0;
3969 }
3970
3971 /*
3972 * Point to the record and extract its data.
3973 */
3974 *recp = xfs_btree_rec_addr(cur, ptr, block);
3975 *stat = 1;
3976 return 0;
3977}
Dave Chinner21b5c972013-08-30 10:23:44 +10003978
3979/*
3980 * Change the owner of a btree.
3981 *
3982 * The mechanism we use here is ordered buffer logging. Because we don't know
3983 * how many buffers were are going to need to modify, we don't really want to
3984 * have to make transaction reservations for the worst case of every buffer in a
3985 * full size btree as that may be more space that we can fit in the log....
3986 *
3987 * We do the btree walk in the most optimal manner possible - we have sibling
3988 * pointers so we can just walk all the blocks on each level from left to right
3989 * in a single pass, and then move to the next level and do the same. We can
3990 * also do readahead on the sibling pointers to get IO moving more quickly,
3991 * though for slow disks this is unlikely to make much difference to performance
3992 * as the amount of CPU work we have to do before moving to the next block is
3993 * relatively small.
3994 *
3995 * For each btree block that we load, modify the owner appropriately, set the
3996 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3997 * we mark the region we change dirty so that if the buffer is relogged in
3998 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10003999 * correctly relogged in that transaction. If we are in recovery context, then
4000 * just queue the modified buffer as delayed write buffer so the transaction
4001 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10004002 */
4003static int
4004xfs_btree_block_change_owner(
4005 struct xfs_btree_cur *cur,
4006 int level,
Dave Chinner638f44162013-08-30 10:23:45 +10004007 __uint64_t new_owner,
4008 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004009{
4010 struct xfs_btree_block *block;
4011 struct xfs_buf *bp;
4012 union xfs_btree_ptr rptr;
4013
4014 /* do right sibling readahead */
4015 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
4016
4017 /* modify the owner */
4018 block = xfs_btree_get_block(cur, level, &bp);
4019 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
4020 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
4021 else
4022 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
4023
4024 /*
Dave Chinner638f44162013-08-30 10:23:45 +10004025 * If the block is a root block hosted in an inode, we might not have a
4026 * buffer pointer here and we shouldn't attempt to log the change as the
4027 * information is already held in the inode and discarded when the root
4028 * block is formatted into the on-disk inode fork. We still change it,
4029 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10004030 */
4031 if (bp) {
Dave Chinner638f44162013-08-30 10:23:45 +10004032 if (cur->bc_tp) {
4033 xfs_trans_ordered_buf(cur->bc_tp, bp);
4034 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
4035 } else {
4036 xfs_buf_delwri_queue(bp, buffer_list);
4037 }
Dave Chinner21b5c972013-08-30 10:23:44 +10004038 } else {
4039 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
4040 ASSERT(level == cur->bc_nlevels - 1);
4041 }
4042
4043 /* now read rh sibling block for next iteration */
4044 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
4045 if (xfs_btree_ptr_is_null(cur, &rptr))
Dave Chinner24513372014-06-25 14:58:08 +10004046 return -ENOENT;
Dave Chinner21b5c972013-08-30 10:23:44 +10004047
4048 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
4049}
4050
4051int
4052xfs_btree_change_owner(
4053 struct xfs_btree_cur *cur,
Dave Chinner638f44162013-08-30 10:23:45 +10004054 __uint64_t new_owner,
4055 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10004056{
4057 union xfs_btree_ptr lptr;
4058 int level;
4059 struct xfs_btree_block *block = NULL;
4060 int error = 0;
4061
4062 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
4063
4064 /* for each level */
4065 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
4066 /* grab the left hand block */
4067 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
4068 if (error)
4069 return error;
4070
4071 /* readahead the left most block for the next level down */
4072 if (level > 0) {
4073 union xfs_btree_ptr *ptr;
4074
4075 ptr = xfs_btree_ptr_addr(cur, 1, block);
4076 xfs_btree_readahead_ptr(cur, ptr, 1);
4077
4078 /* save for the next iteration of the loop */
4079 lptr = *ptr;
4080 }
4081
4082 /* for each buffer in the level */
4083 do {
4084 error = xfs_btree_block_change_owner(cur, level,
Dave Chinner638f44162013-08-30 10:23:45 +10004085 new_owner,
4086 buffer_list);
Dave Chinner21b5c972013-08-30 10:23:44 +10004087 } while (!error);
4088
Dave Chinner24513372014-06-25 14:58:08 +10004089 if (error != -ENOENT)
Dave Chinner21b5c972013-08-30 10:23:44 +10004090 return error;
4091 }
4092
4093 return 0;
4094}
Darrick J. Wongc5ab1312016-01-04 16:13:21 +11004095
4096/**
4097 * xfs_btree_sblock_v5hdr_verify() -- verify the v5 fields of a short-format
4098 * btree block
4099 *
4100 * @bp: buffer containing the btree block
4101 * @max_recs: pointer to the m_*_mxr max records field in the xfs mount
4102 * @pag_max_level: pointer to the per-ag max level field
4103 */
4104bool
4105xfs_btree_sblock_v5hdr_verify(
4106 struct xfs_buf *bp)
4107{
4108 struct xfs_mount *mp = bp->b_target->bt_mount;
4109 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4110 struct xfs_perag *pag = bp->b_pag;
4111
4112 if (!xfs_sb_version_hascrc(&mp->m_sb))
4113 return false;
4114 if (!uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
4115 return false;
4116 if (block->bb_u.s.bb_blkno != cpu_to_be64(bp->b_bn))
4117 return false;
4118 if (pag && be32_to_cpu(block->bb_u.s.bb_owner) != pag->pag_agno)
4119 return false;
4120 return true;
4121}
4122
4123/**
4124 * xfs_btree_sblock_verify() -- verify a short-format btree block
4125 *
4126 * @bp: buffer containing the btree block
4127 * @max_recs: maximum records allowed in this btree node
4128 */
4129bool
4130xfs_btree_sblock_verify(
4131 struct xfs_buf *bp,
4132 unsigned int max_recs)
4133{
4134 struct xfs_mount *mp = bp->b_target->bt_mount;
4135 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4136
4137 /* numrecs verification */
4138 if (be16_to_cpu(block->bb_numrecs) > max_recs)
4139 return false;
4140
4141 /* sibling pointer verification */
4142 if (!block->bb_u.s.bb_leftsib ||
4143 (be32_to_cpu(block->bb_u.s.bb_leftsib) >= mp->m_sb.sb_agblocks &&
4144 block->bb_u.s.bb_leftsib != cpu_to_be32(NULLAGBLOCK)))
4145 return false;
4146 if (!block->bb_u.s.bb_rightsib ||
4147 (be32_to_cpu(block->bb_u.s.bb_rightsib) >= mp->m_sb.sb_agblocks &&
4148 block->bb_u.s.bb_rightsib != cpu_to_be32(NULLAGBLOCK)))
4149 return false;
4150
4151 return true;
4152}