blob: 41dd749dc0e731e7419892feedc071252af2233c [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_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110029#include "xfs_trans.h"
Christoph Hellwig38bb7422008-10-30 16:56:22 +110030#include "xfs_inode_item.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050031#include "xfs_buf_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050035#include "xfs_cksum.h"
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] = {
46 { XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC },
47 { XFS_ABTB_CRC_MAGIC, XFS_ABTC_CRC_MAGIC,
48 XFS_BMAP_CRC_MAGIC, XFS_IBT_CRC_MAGIC }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050050#define xfs_btree_magic(cur) \
51 xfs_magics[!!((cur)->bc_flags & XFS_BTREE_CRC_BLOCKS)][cur->bc_btnum]
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110054STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110055xfs_btree_check_lblock(
56 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110057 struct xfs_btree_block *block, /* btree long form block pointer */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110058 int level, /* level of the btree block */
59 struct xfs_buf *bp) /* buffer for block, if any */
60{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050061 int lblock_ok = 1; /* block passes checks */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110062 struct xfs_mount *mp; /* file system mount point */
63
64 mp = cur->bc_mp;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050065
66 if (xfs_sb_version_hascrc(&mp->m_sb)) {
67 lblock_ok = lblock_ok &&
68 uuid_equal(&block->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid) &&
69 block->bb_u.l.bb_blkno == cpu_to_be64(
70 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
71 }
72
73 lblock_ok = lblock_ok &&
74 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110075 be16_to_cpu(block->bb_level) == level &&
76 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +110077 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110078 block->bb_u.l.bb_leftsib &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +020079 (block->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110080 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050081 be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110082 block->bb_u.l.bb_rightsib &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +020083 (block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +110084 XFS_FSB_SANITY_CHECK(mp,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050085 be64_to_cpu(block->bb_u.l.bb_rightsib)));
86
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110087 if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
88 XFS_ERRTAG_BTREE_CHECK_LBLOCK,
89 XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
90 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000091 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -050092 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +110093 return XFS_ERROR(EFSCORRUPTED);
94 }
95 return 0;
96}
97
Christoph Hellwig3cc75242008-10-30 16:58:41 +110098STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099xfs_btree_check_sblock(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100100 struct xfs_btree_cur *cur, /* btree cursor */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100101 struct xfs_btree_block *block, /* btree short form block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int level, /* level of the btree block */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100103 struct xfs_buf *bp) /* buffer containing block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500105 struct xfs_mount *mp; /* file system mount point */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100106 struct xfs_buf *agbp; /* buffer for ag. freespace struct */
107 struct xfs_agf *agf; /* ag. freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 xfs_agblock_t agflen; /* native ag. freespace length */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500109 int sblock_ok = 1; /* block passes checks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500111 mp = cur->bc_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 agbp = cur->bc_private.a.agbp;
113 agf = XFS_BUF_TO_AGF(agbp);
Christoph Hellwig16259e72005-11-02 15:11:25 +1100114 agflen = be32_to_cpu(agf->agf_length);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500115
116 if (xfs_sb_version_hascrc(&mp->m_sb)) {
117 sblock_ok = sblock_ok &&
118 uuid_equal(&block->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid) &&
119 block->bb_u.s.bb_blkno == cpu_to_be64(
120 bp ? bp->b_bn : XFS_BUF_DADDR_NULL);
121 }
122
123 sblock_ok = sblock_ok &&
124 be32_to_cpu(block->bb_magic) == xfs_btree_magic(cur) &&
Christoph Hellwig16259e72005-11-02 15:11:25 +1100125 be16_to_cpu(block->bb_level) == level &&
126 be16_to_cpu(block->bb_numrecs) <=
Christoph Hellwigce5e42d2008-10-30 16:55:23 +1100127 cur->bc_ops->get_maxrecs(cur, level) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200128 (block->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100129 be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
130 block->bb_u.s.bb_leftsib &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200131 (block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK) ||
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100132 be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
133 block->bb_u.s.bb_rightsib;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500134
135 if (unlikely(XFS_TEST_ERROR(!sblock_ok, mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 XFS_ERRTAG_BTREE_CHECK_SBLOCK,
137 XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
138 if (bp)
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000139 trace_xfs_btree_corrupt(bp, _RET_IP_);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500140 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return XFS_ERROR(EFSCORRUPTED);
142 }
143 return 0;
144}
145
146/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100147 * Debug routine: check that block header is ok.
148 */
149int
150xfs_btree_check_block(
151 struct xfs_btree_cur *cur, /* btree cursor */
152 struct xfs_btree_block *block, /* generic btree block pointer */
153 int level, /* level of the btree block */
154 struct xfs_buf *bp) /* buffer containing block, if any */
155{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100156 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
157 return xfs_btree_check_lblock(cur, block, level, bp);
158 else
159 return xfs_btree_check_sblock(cur, block, level, bp);
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100160}
161
162/*
163 * Check that (long) pointer is ok.
164 */
165int /* error (0 or EFSCORRUPTED) */
166xfs_btree_check_lptr(
167 struct xfs_btree_cur *cur, /* btree cursor */
168 xfs_dfsbno_t bno, /* btree block disk address */
169 int level) /* btree block level */
170{
171 XFS_WANT_CORRUPTED_RETURN(
172 level > 0 &&
173 bno != NULLDFSBNO &&
174 XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
175 return 0;
176}
177
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100178#ifdef DEBUG
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100179/*
180 * Check that (short) pointer is ok.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100182STATIC int /* error (0 or EFSCORRUPTED) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183xfs_btree_check_sptr(
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100184 struct xfs_btree_cur *cur, /* btree cursor */
185 xfs_agblock_t bno, /* btree block disk address */
186 int level) /* btree block level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100188 xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 XFS_WANT_CORRUPTED_RETURN(
191 level > 0 &&
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100192 bno != NULLAGBLOCK &&
193 bno != 0 &&
194 bno < agblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196}
197
198/*
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100199 * Check that block ptr is ok.
200 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100201STATIC int /* error (0 or EFSCORRUPTED) */
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100202xfs_btree_check_ptr(
203 struct xfs_btree_cur *cur, /* btree cursor */
204 union xfs_btree_ptr *ptr, /* btree block disk address */
205 int index, /* offset from ptr to check */
206 int level) /* btree block level */
207{
208 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
209 return xfs_btree_check_lptr(cur,
210 be64_to_cpu((&ptr->l)[index]), level);
211 } else {
212 return xfs_btree_check_sptr(cur,
213 be32_to_cpu((&ptr->s)[index]), level);
214 }
215}
Lachlan McIlroy24ee0e42008-10-30 17:05:26 +1100216#endif
Christoph Hellwiga23f6ef2008-10-30 16:54:53 +1100217
218/*
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500219 * Calculate CRC on the whole btree block and stuff it into the
220 * long-form btree header.
221 *
222 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
223 * it into the buffer so recovery knows what the last modifcation was that made
224 * it to disk.
225 */
226void
227xfs_btree_lblock_calc_crc(
228 struct xfs_buf *bp)
229{
230 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
231 struct xfs_buf_log_item *bip = bp->b_fspriv;
232
233 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
234 return;
235 if (bip)
236 block->bb_u.l.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100237 xfs_buf_update_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500238}
239
240bool
241xfs_btree_lblock_verify_crc(
242 struct xfs_buf *bp)
243{
244 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
Eric Sandeen51582172014-02-27 15:17:27 +1100245 return xfs_buf_verify_cksum(bp, XFS_BTREE_LBLOCK_CRC_OFF);
246
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500247 return true;
248}
249
250/*
251 * Calculate CRC on the whole btree block and stuff it into the
252 * short-form btree header.
253 *
254 * Prior to calculting the CRC, pull the LSN out of the buffer log item and put
255 * it into the buffer so recovery knows what the last modifcation was that made
256 * it to disk.
257 */
258void
259xfs_btree_sblock_calc_crc(
260 struct xfs_buf *bp)
261{
262 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
263 struct xfs_buf_log_item *bip = bp->b_fspriv;
264
265 if (!xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
266 return;
267 if (bip)
268 block->bb_u.s.bb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100269 xfs_buf_update_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500270}
271
272bool
273xfs_btree_sblock_verify_crc(
274 struct xfs_buf *bp)
275{
276 if (xfs_sb_version_hascrc(&bp->b_target->bt_mount->m_sb))
Eric Sandeen51582172014-02-27 15:17:27 +1100277 return xfs_buf_verify_cksum(bp, XFS_BTREE_SBLOCK_CRC_OFF);
278
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500279 return true;
280}
281
282/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * Delete the btree cursor.
284 */
285void
286xfs_btree_del_cursor(
287 xfs_btree_cur_t *cur, /* btree cursor */
288 int error) /* del because of error */
289{
290 int i; /* btree level */
291
292 /*
293 * Clear the buffer pointers, and release the buffers.
294 * If we're doing this in the face of an error, we
295 * need to make sure to inspect all of the entries
296 * in the bc_bufs array for buffers to be unlocked.
297 * This is because some of the btree code works from
298 * level n down to 0, and if we get an error along
299 * the way we won't have initialized all the entries
300 * down to 0.
301 */
302 for (i = 0; i < cur->bc_nlevels; i++) {
303 if (cur->bc_bufs[i])
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000304 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 else if (!error)
306 break;
307 }
308 /*
309 * Can't free a bmap cursor without having dealt with the
310 * allocated indirect blocks' accounting.
311 */
312 ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
313 cur->bc_private.b.allocated == 0);
314 /*
315 * Free the cursor.
316 */
317 kmem_zone_free(xfs_btree_cur_zone, cur);
318}
319
320/*
321 * Duplicate the btree cursor.
322 * Allocate a new one, copy the record, re-get the buffers.
323 */
324int /* error */
325xfs_btree_dup_cursor(
326 xfs_btree_cur_t *cur, /* input cursor */
327 xfs_btree_cur_t **ncur) /* output cursor */
328{
329 xfs_buf_t *bp; /* btree block's buffer pointer */
330 int error; /* error return value */
331 int i; /* level number of btree block */
332 xfs_mount_t *mp; /* mount structure for filesystem */
333 xfs_btree_cur_t *new; /* new cursor value */
334 xfs_trans_t *tp; /* transaction pointer, can be NULL */
335
336 tp = cur->bc_tp;
337 mp = cur->bc_mp;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /*
340 * Allocate a new cursor like the old one.
341 */
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100342 new = cur->bc_ops->dup_cursor(cur);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
345 * Copy the record currently in the cursor.
346 */
347 new->bc_rec = cur->bc_rec;
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * For each level current, re-get the buffer and copy the ptr value.
351 */
352 for (i = 0; i < new->bc_nlevels; i++) {
353 new->bc_ptrs[i] = cur->bc_ptrs[i];
354 new->bc_ra[i] = cur->bc_ra[i];
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100355 bp = cur->bc_bufs[i];
356 if (bp) {
357 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
358 XFS_BUF_ADDR(bp), mp->m_bsize,
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100359 0, &bp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100360 cur->bc_ops->buf_ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100361 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 xfs_btree_del_cursor(new, error);
363 *ncur = NULL;
364 return error;
365 }
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500366 }
367 new->bc_bufs[i] = bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *ncur = new;
370 return 0;
371}
372
373/*
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100374 * XFS btree block layout and addressing:
375 *
376 * There are two types of blocks in the btree: leaf and non-leaf blocks.
377 *
378 * The leaf record start with a header then followed by records containing
379 * the values. A non-leaf block also starts with the same header, and
380 * then first contains lookup keys followed by an equal number of pointers
381 * to the btree blocks at the previous level.
382 *
383 * +--------+-------+-------+-------+-------+-------+-------+
384 * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
385 * +--------+-------+-------+-------+-------+-------+-------+
386 *
387 * +--------+-------+-------+-------+-------+-------+-------+
388 * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
389 * +--------+-------+-------+-------+-------+-------+-------+
390 *
391 * The header is called struct xfs_btree_block for reasons better left unknown
392 * and comes in different versions for short (32bit) and long (64bit) block
393 * pointers. The record and key structures are defined by the btree instances
394 * and opaque to the btree core. The block pointers are simple disk endian
395 * integers, available in a short (32bit) and long (64bit) variant.
396 *
397 * The helpers below calculate the offset of a given record, key or pointer
398 * into a btree block (xfs_btree_*_offset) or return a pointer to the given
399 * record, key or pointer (xfs_btree_*_addr). Note that all addressing
400 * inside the btree block is done using indices starting at one, not zero!
401 */
402
403/*
404 * Return size of the btree block header for this btree instance.
405 */
406static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
407{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500408 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
409 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
410 return XFS_BTREE_LBLOCK_CRC_LEN;
411 return XFS_BTREE_LBLOCK_LEN;
412 }
413 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS)
414 return XFS_BTREE_SBLOCK_CRC_LEN;
415 return XFS_BTREE_SBLOCK_LEN;
Christoph Hellwig65f1eae2008-10-30 16:55:34 +1100416}
417
418/*
419 * Return size of btree block pointers for this btree instance.
420 */
421static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
422{
423 return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
424 sizeof(__be64) : sizeof(__be32);
425}
426
427/*
428 * Calculate offset of the n-th record in a btree block.
429 */
430STATIC size_t
431xfs_btree_rec_offset(
432 struct xfs_btree_cur *cur,
433 int n)
434{
435 return xfs_btree_block_len(cur) +
436 (n - 1) * cur->bc_ops->rec_len;
437}
438
439/*
440 * Calculate offset of the n-th key in a btree block.
441 */
442STATIC size_t
443xfs_btree_key_offset(
444 struct xfs_btree_cur *cur,
445 int n)
446{
447 return xfs_btree_block_len(cur) +
448 (n - 1) * cur->bc_ops->key_len;
449}
450
451/*
452 * Calculate offset of the n-th block pointer in a btree block.
453 */
454STATIC size_t
455xfs_btree_ptr_offset(
456 struct xfs_btree_cur *cur,
457 int n,
458 int level)
459{
460 return xfs_btree_block_len(cur) +
461 cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
462 (n - 1) * xfs_btree_ptr_len(cur);
463}
464
465/*
466 * Return a pointer to the n-th record in the btree block.
467 */
468STATIC union xfs_btree_rec *
469xfs_btree_rec_addr(
470 struct xfs_btree_cur *cur,
471 int n,
472 struct xfs_btree_block *block)
473{
474 return (union xfs_btree_rec *)
475 ((char *)block + xfs_btree_rec_offset(cur, n));
476}
477
478/*
479 * Return a pointer to the n-th key in the btree block.
480 */
481STATIC union xfs_btree_key *
482xfs_btree_key_addr(
483 struct xfs_btree_cur *cur,
484 int n,
485 struct xfs_btree_block *block)
486{
487 return (union xfs_btree_key *)
488 ((char *)block + xfs_btree_key_offset(cur, n));
489}
490
491/*
492 * Return a pointer to the n-th block pointer in the btree block.
493 */
494STATIC union xfs_btree_ptr *
495xfs_btree_ptr_addr(
496 struct xfs_btree_cur *cur,
497 int n,
498 struct xfs_btree_block *block)
499{
500 int level = xfs_btree_get_level(block);
501
502 ASSERT(block->bb_level != 0);
503
504 return (union xfs_btree_ptr *)
505 ((char *)block + xfs_btree_ptr_offset(cur, n, level));
506}
507
508/*
Zhi Yong Wu1cb93862013-08-07 10:11:05 +0000509 * Get the root block which is stored in the inode.
Christoph Hellwig8186e512008-10-30 16:54:22 +1100510 *
511 * For now this btree implementation assumes the btree root is always
512 * stored in the if_broot field of an inode fork.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
Christoph Hellwig8186e512008-10-30 16:54:22 +1100514STATIC struct xfs_btree_block *
515xfs_btree_get_iroot(
516 struct xfs_btree_cur *cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Christoph Hellwig8186e512008-10-30 16:54:22 +1100518 struct xfs_ifork *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Christoph Hellwig8186e512008-10-30 16:54:22 +1100520 ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
521 return (struct xfs_btree_block *)ifp->if_broot;
522}
523
524/*
525 * Retrieve the block pointer from the cursor at the given level.
526 * This may be an inode btree root or from a buffer.
527 */
528STATIC struct xfs_btree_block * /* generic btree block pointer */
529xfs_btree_get_block(
530 struct xfs_btree_cur *cur, /* btree cursor */
531 int level, /* level in btree */
532 struct xfs_buf **bpp) /* buffer containing the block */
533{
534 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
535 (level == cur->bc_nlevels - 1)) {
536 *bpp = NULL;
537 return xfs_btree_get_iroot(cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Christoph Hellwig8186e512008-10-30 16:54:22 +1100539
540 *bpp = cur->bc_bufs[level];
541 return XFS_BUF_TO_BLOCK(*bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * Get a buffer for the block, return it with no data read.
546 * Long-form addressing.
547 */
548xfs_buf_t * /* buffer for fsbno */
549xfs_btree_get_bufl(
550 xfs_mount_t *mp, /* file system mount point */
551 xfs_trans_t *tp, /* transaction pointer */
552 xfs_fsblock_t fsbno, /* file system block number */
553 uint lock) /* lock flags for get_buf */
554{
555 xfs_buf_t *bp; /* buffer pointer (return value) */
556 xfs_daddr_t d; /* real disk block address */
557
558 ASSERT(fsbno != NULLFSBLOCK);
559 d = XFS_FSB_TO_DADDR(mp, fsbno);
560 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000561 ASSERT(!xfs_buf_geterror(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return bp;
563}
564
565/*
566 * Get a buffer for the block, return it with no data read.
567 * Short-form addressing.
568 */
569xfs_buf_t * /* buffer for agno/agbno */
570xfs_btree_get_bufs(
571 xfs_mount_t *mp, /* file system mount point */
572 xfs_trans_t *tp, /* transaction pointer */
573 xfs_agnumber_t agno, /* allocation group number */
574 xfs_agblock_t agbno, /* allocation group block number */
575 uint lock) /* lock flags for get_buf */
576{
577 xfs_buf_t *bp; /* buffer pointer (return value) */
578 xfs_daddr_t d; /* real disk block address */
579
580 ASSERT(agno != NULLAGNUMBER);
581 ASSERT(agbno != NULLAGBLOCK);
582 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
583 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000584 ASSERT(!xfs_buf_geterror(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return bp;
586}
587
588/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * Check for the cursor referring to the last block at the given level.
590 */
591int /* 1=is last block, 0=not last block */
592xfs_btree_islastblock(
593 xfs_btree_cur_t *cur, /* btree cursor */
594 int level) /* level to check */
595{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100596 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_buf_t *bp; /* buffer containing block */
598
599 block = xfs_btree_get_block(cur, level, &bp);
600 xfs_btree_check_block(cur, block, level, bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100601 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200602 return block->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200604 return block->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/*
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000608 * Change the cursor to point to the first record at the given level.
609 * Other levels are unaffected.
610 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100611STATIC int /* success=1, failure=0 */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000612xfs_btree_firstrec(
613 xfs_btree_cur_t *cur, /* btree cursor */
614 int level) /* level to change */
615{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100616 struct xfs_btree_block *block; /* generic btree block pointer */
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000617 xfs_buf_t *bp; /* buffer containing block */
618
619 /*
620 * Get the block pointer for this level.
621 */
622 block = xfs_btree_get_block(cur, level, &bp);
623 xfs_btree_check_block(cur, block, level, bp);
624 /*
625 * It's empty, there is no such record.
626 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100627 if (!block->bb_numrecs)
Christoph Hellwigcdcf43332008-08-13 16:23:50 +1000628 return 0;
629 /*
630 * Set the ptr value to 1, that's the first record/key.
631 */
632 cur->bc_ptrs[level] = 1;
633 return 1;
634}
635
636/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * Change the cursor to point to the last record in the current block
638 * at the given level. Other levels are unaffected.
639 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100640STATIC int /* success=1, failure=0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641xfs_btree_lastrec(
642 xfs_btree_cur_t *cur, /* btree cursor */
643 int level) /* level to change */
644{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100645 struct xfs_btree_block *block; /* generic btree block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xfs_buf_t *bp; /* buffer containing block */
647
648 /*
649 * Get the block pointer for this level.
650 */
651 block = xfs_btree_get_block(cur, level, &bp);
652 xfs_btree_check_block(cur, block, level, bp);
653 /*
654 * It's empty, there is no such record.
655 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100656 if (!block->bb_numrecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658 /*
659 * Set the ptr value to numrecs, that's the last record/key.
660 */
Christoph Hellwigf2277f02008-10-30 16:53:47 +1100661 cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 1;
663}
664
665/*
666 * Compute first and last byte offsets for the fields given.
667 * Interprets the offsets table, which contains struct field offsets.
668 */
669void
670xfs_btree_offsets(
671 __int64_t fields, /* bitmask of fields */
672 const short *offsets, /* table of field offsets */
673 int nbits, /* number of bits to inspect */
674 int *first, /* output: first byte offset */
675 int *last) /* output: last byte offset */
676{
677 int i; /* current bit number */
678 __int64_t imask; /* mask for current bit number */
679
680 ASSERT(fields != 0);
681 /*
682 * Find the lowest bit, so the first byte offset.
683 */
684 for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
685 if (imask & fields) {
686 *first = offsets[i];
687 break;
688 }
689 }
690 /*
691 * Find the highest bit, so the last byte offset.
692 */
693 for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
694 if (imask & fields) {
695 *last = offsets[i + 1] - 1;
696 break;
697 }
698 }
699}
700
701/*
702 * Get a buffer for the block, return it read in.
703 * Long-form addressing.
704 */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100705int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706xfs_btree_read_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100707 struct xfs_mount *mp, /* file system mount point */
708 struct xfs_trans *tp, /* transaction pointer */
709 xfs_fsblock_t fsbno, /* file system block number */
710 uint lock, /* lock flags for read_buf */
711 struct xfs_buf **bpp, /* buffer for fsbno */
712 int refval, /* ref count value for buffer */
Dave Chinner1813dd62012-11-14 17:54:40 +1100713 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100715 struct xfs_buf *bp; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 xfs_daddr_t d; /* real disk block address */
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100717 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 ASSERT(fsbno != NULLFSBLOCK);
720 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100721 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
Dave Chinner1813dd62012-11-14 17:54:40 +1100722 mp->m_bsize, lock, &bp, ops);
Dave Chinnerc3f8fc72012-11-12 22:54:01 +1100723 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000725 ASSERT(!xfs_buf_geterror(bp));
Dave Chinner821eb212010-12-02 16:31:13 +1100726 if (bp)
Christoph Hellwig38f23232011-10-10 16:52:45 +0000727 xfs_buf_set_ref(bp, refval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *bpp = bp;
729 return 0;
730}
731
732/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * Read-ahead the block, don't wait for it, don't return a buffer.
734 * Long-form addressing.
735 */
736/* ARGSUSED */
737void
738xfs_btree_reada_bufl(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100739 struct xfs_mount *mp, /* file system mount point */
740 xfs_fsblock_t fsbno, /* file system block number */
741 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100742 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 xfs_daddr_t d;
745
746 ASSERT(fsbno != NULLFSBLOCK);
747 d = XFS_FSB_TO_DADDR(mp, fsbno);
Dave Chinner1813dd62012-11-14 17:54:40 +1100748 xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * Read-ahead the block, don't wait for it, don't return a buffer.
753 * Short-form addressing.
754 */
755/* ARGSUSED */
756void
757xfs_btree_reada_bufs(
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100758 struct xfs_mount *mp, /* file system mount point */
759 xfs_agnumber_t agno, /* allocation group number */
760 xfs_agblock_t agbno, /* allocation group block number */
761 xfs_extlen_t count, /* count of filesystem blocks */
Dave Chinner1813dd62012-11-14 17:54:40 +1100762 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 xfs_daddr_t d;
765
766 ASSERT(agno != NULLAGNUMBER);
767 ASSERT(agbno != NULLAGBLOCK);
768 d = XFS_AGB_TO_DADDR(mp, agno, agbno);
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
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100772STATIC int
773xfs_btree_readahead_lblock(
774 struct xfs_btree_cur *cur,
775 int lr,
776 struct xfs_btree_block *block)
777{
778 int rval = 0;
Christoph Hellwige6edbd12009-01-08 13:42:23 -0500779 xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
780 xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100781
782 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100783 xfs_btree_reada_bufl(cur->bc_mp, left, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100784 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100785 rval++;
786 }
787
788 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
Dave Chinner3d3e6f62012-11-12 22:54:08 +1100789 xfs_btree_reada_bufl(cur->bc_mp, right, 1,
Dave Chinner1813dd62012-11-14 17:54:40 +1100790 cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100791 rval++;
792 }
793
794 return rval;
795}
796
797STATIC int
798xfs_btree_readahead_sblock(
799 struct xfs_btree_cur *cur,
800 int lr,
801 struct xfs_btree_block *block)
802{
803 int rval = 0;
804 xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
805 xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
806
807
808 if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
809 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100810 left, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100811 rval++;
812 }
813
814 if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
815 xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100816 right, 1, cur->bc_ops->buf_ops);
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100817 rval++;
818 }
819
820 return rval;
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*
824 * Read-ahead btree blocks, at the given level.
825 * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
826 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +1100827STATIC int
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100828xfs_btree_readahead(
829 struct xfs_btree_cur *cur, /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int lev, /* level in btree */
831 int lr) /* left/right bits */
832{
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100833 struct xfs_btree_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100835 /*
836 * No readahead needed if we are at the root level and the
837 * btree root is stored in the inode.
838 */
839 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
840 (lev == cur->bc_nlevels - 1))
841 return 0;
842
843 if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
844 return 0;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 cur->bc_ra[lev] |= lr;
Christoph Hellwigb524bfe2008-10-30 16:54:43 +1100847 block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
848
849 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
850 return xfs_btree_readahead_lblock(cur, lr, block);
851 return xfs_btree_readahead_sblock(cur, lr, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Dave Chinner21b5c972013-08-30 10:23:44 +1000854STATIC xfs_daddr_t
855xfs_btree_ptr_to_daddr(
856 struct xfs_btree_cur *cur,
857 union xfs_btree_ptr *ptr)
858{
859 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
860 ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
861
862 return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
863 } else {
864 ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
865 ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
866
867 return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
868 be32_to_cpu(ptr->s));
869 }
870}
871
872/*
873 * Readahead @count btree blocks at the given @ptr location.
874 *
875 * We don't need to care about long or short form btrees here as we have a
876 * method of converting the ptr directly to a daddr available to us.
877 */
878STATIC void
879xfs_btree_readahead_ptr(
880 struct xfs_btree_cur *cur,
881 union xfs_btree_ptr *ptr,
882 xfs_extlen_t count)
883{
884 xfs_buf_readahead(cur->bc_mp->m_ddev_targp,
885 xfs_btree_ptr_to_daddr(cur, ptr),
886 cur->bc_mp->m_bsize * count, cur->bc_ops->buf_ops);
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/*
890 * Set the buffer for level "lev" in the cursor to bp, releasing
891 * any previous buffer.
892 */
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000893STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894xfs_btree_setbuf(
895 xfs_btree_cur_t *cur, /* btree cursor */
896 int lev, /* level in btree */
897 xfs_buf_t *bp) /* new buffer to set */
898{
Christoph Hellwig7cc95a82008-10-30 17:14:34 +1100899 struct xfs_btree_block *b; /* btree block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000901 if (cur->bc_bufs[lev])
902 xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 cur->bc_bufs[lev] = bp;
904 cur->bc_ra[lev] = 0;
Christoph Hellwigc0e59e12010-09-07 23:34:07 +0000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 b = XFS_BUF_TO_BLOCK(bp);
Christoph Hellwige99ab902008-10-30 16:54:33 +1100907 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200908 if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200910 if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
912 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200913 if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200915 if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
917 }
918}
Christoph Hellwig637aa502008-10-30 16:55:45 +1100919
920STATIC int
921xfs_btree_ptr_is_null(
922 struct xfs_btree_cur *cur,
923 union xfs_btree_ptr *ptr)
924{
925 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200926 return ptr->l == cpu_to_be64(NULLDFSBNO);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100927 else
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200928 return ptr->s == cpu_to_be32(NULLAGBLOCK);
Christoph Hellwig637aa502008-10-30 16:55:45 +1100929}
930
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100931STATIC void
932xfs_btree_set_ptr_null(
933 struct xfs_btree_cur *cur,
934 union xfs_btree_ptr *ptr)
935{
936 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
Dave Chinner33ad9652009-01-21 15:22:17 +1100937 ptr->l = cpu_to_be64(NULLDFSBNO);
Christoph Hellwig4b22a572008-10-30 16:57:40 +1100938 else
939 ptr->s = cpu_to_be32(NULLAGBLOCK);
940}
941
Christoph Hellwig637aa502008-10-30 16:55:45 +1100942/*
943 * Get/set/init sibling pointers
944 */
945STATIC void
946xfs_btree_get_sibling(
947 struct xfs_btree_cur *cur,
948 struct xfs_btree_block *block,
949 union xfs_btree_ptr *ptr,
950 int lr)
951{
952 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
953
954 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
955 if (lr == XFS_BB_RIGHTSIB)
956 ptr->l = block->bb_u.l.bb_rightsib;
957 else
958 ptr->l = block->bb_u.l.bb_leftsib;
959 } else {
960 if (lr == XFS_BB_RIGHTSIB)
961 ptr->s = block->bb_u.s.bb_rightsib;
962 else
963 ptr->s = block->bb_u.s.bb_leftsib;
964 }
965}
966
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +1100967STATIC void
968xfs_btree_set_sibling(
969 struct xfs_btree_cur *cur,
970 struct xfs_btree_block *block,
971 union xfs_btree_ptr *ptr,
972 int lr)
973{
974 ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
975
976 if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
977 if (lr == XFS_BB_RIGHTSIB)
978 block->bb_u.l.bb_rightsib = ptr->l;
979 else
980 block->bb_u.l.bb_leftsib = ptr->l;
981 } else {
982 if (lr == XFS_BB_RIGHTSIB)
983 block->bb_u.s.bb_rightsib = ptr->s;
984 else
985 block->bb_u.s.bb_leftsib = ptr->s;
986 }
987}
988
Dave Chinnerb64f3a32012-11-13 16:40:27 -0600989void
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500990xfs_btree_init_block_int(
991 struct xfs_mount *mp,
992 struct xfs_btree_block *buf,
993 xfs_daddr_t blkno,
994 __u32 magic,
995 __u16 level,
996 __u16 numrecs,
997 __u64 owner,
998 unsigned int flags)
999{
1000 buf->bb_magic = cpu_to_be32(magic);
1001 buf->bb_level = cpu_to_be16(level);
1002 buf->bb_numrecs = cpu_to_be16(numrecs);
1003
1004 if (flags & XFS_BTREE_LONG_PTRS) {
1005 buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1006 buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1007 if (flags & XFS_BTREE_CRC_BLOCKS) {
1008 buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
1009 buf->bb_u.l.bb_owner = cpu_to_be64(owner);
1010 uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
1011 buf->bb_u.l.bb_pad = 0;
Dave Chinnerb58fa552013-08-28 21:22:46 +10001012 buf->bb_u.l.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001013 }
1014 } else {
1015 /* owner is a 32 bit value on short blocks */
1016 __u32 __owner = (__u32)owner;
1017
1018 buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
1019 buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
1020 if (flags & XFS_BTREE_CRC_BLOCKS) {
1021 buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
1022 buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
1023 uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
Dave Chinnerb58fa552013-08-28 21:22:46 +10001024 buf->bb_u.s.bb_lsn = 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001025 }
1026 }
1027}
1028
1029void
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001030xfs_btree_init_block(
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001031 struct xfs_mount *mp,
1032 struct xfs_buf *bp,
1033 __u32 magic,
1034 __u16 level,
1035 __u16 numrecs,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001036 __u64 owner,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001037 unsigned int flags)
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001038{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001039 xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1040 magic, level, numrecs, owner, flags);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001041}
1042
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001043STATIC void
1044xfs_btree_init_block_cur(
1045 struct xfs_btree_cur *cur,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001046 struct xfs_buf *bp,
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001047 int level,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001048 int numrecs)
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001049{
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001050 __u64 owner;
1051
1052 /*
1053 * we can pull the owner from the cursor right now as the different
1054 * owners align directly with the pointer size of the btree. This may
1055 * change in future, but is safe for current users of the generic btree
1056 * code.
1057 */
1058 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1059 owner = cur->bc_private.b.ip->i_ino;
1060 else
1061 owner = cur->bc_private.a.agno;
1062
1063 xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
1064 xfs_btree_magic(cur), level, numrecs,
1065 owner, cur->bc_flags);
Dave Chinnerb64f3a32012-11-13 16:40:27 -06001066}
1067
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001068/*
1069 * Return true if ptr is the last record in the btree and
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001070 * we need to track updates to this record. The decision
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001071 * will be further refined in the update_lastrec method.
1072 */
1073STATIC int
1074xfs_btree_is_lastrec(
1075 struct xfs_btree_cur *cur,
1076 struct xfs_btree_block *block,
1077 int level)
1078{
1079 union xfs_btree_ptr ptr;
1080
1081 if (level > 0)
1082 return 0;
1083 if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
1084 return 0;
1085
1086 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1087 if (!xfs_btree_ptr_is_null(cur, &ptr))
1088 return 0;
1089 return 1;
1090}
1091
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001092STATIC void
1093xfs_btree_buf_to_ptr(
1094 struct xfs_btree_cur *cur,
1095 struct xfs_buf *bp,
1096 union xfs_btree_ptr *ptr)
1097{
1098 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
1099 ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
1100 XFS_BUF_ADDR(bp)));
1101 else {
Eric Sandeen9d87c312009-01-14 23:22:07 -06001102 ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001103 XFS_BUF_ADDR(bp)));
1104 }
1105}
1106
Christoph Hellwig637aa502008-10-30 16:55:45 +11001107STATIC void
1108xfs_btree_set_refs(
1109 struct xfs_btree_cur *cur,
1110 struct xfs_buf *bp)
1111{
1112 switch (cur->bc_btnum) {
1113 case XFS_BTNUM_BNO:
1114 case XFS_BTNUM_CNT:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001115 xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001116 break;
1117 case XFS_BTNUM_INO:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001118 xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001119 break;
1120 case XFS_BTNUM_BMAP:
Christoph Hellwig38f23232011-10-10 16:52:45 +00001121 xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001122 break;
1123 default:
1124 ASSERT(0);
1125 }
1126}
1127
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001128STATIC int
1129xfs_btree_get_buf_block(
1130 struct xfs_btree_cur *cur,
1131 union xfs_btree_ptr *ptr,
1132 int flags,
1133 struct xfs_btree_block **block,
1134 struct xfs_buf **bpp)
1135{
1136 struct xfs_mount *mp = cur->bc_mp;
1137 xfs_daddr_t d;
1138
1139 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001140 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001141
1142 d = xfs_btree_ptr_to_daddr(cur, ptr);
1143 *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
1144 mp->m_bsize, flags);
1145
Chandra Seetharaman2a30f36d2011-09-20 13:56:55 +00001146 if (!*bpp)
1147 return ENOMEM;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001148
Dave Chinner1813dd62012-11-14 17:54:40 +11001149 (*bpp)->b_ops = cur->bc_ops->buf_ops;
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11001150 *block = XFS_BUF_TO_BLOCK(*bpp);
1151 return 0;
1152}
1153
Christoph Hellwig637aa502008-10-30 16:55:45 +11001154/*
1155 * Read in the buffer at the given ptr and return the buffer and
1156 * the block pointer within the buffer.
1157 */
1158STATIC int
1159xfs_btree_read_buf_block(
1160 struct xfs_btree_cur *cur,
1161 union xfs_btree_ptr *ptr,
Christoph Hellwig637aa502008-10-30 16:55:45 +11001162 int flags,
1163 struct xfs_btree_block **block,
1164 struct xfs_buf **bpp)
1165{
1166 struct xfs_mount *mp = cur->bc_mp;
1167 xfs_daddr_t d;
1168 int error;
1169
1170 /* need to sort out how callers deal with failures first */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00001171 ASSERT(!(flags & XBF_TRYLOCK));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001172
1173 d = xfs_btree_ptr_to_daddr(cur, ptr);
1174 error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001175 mp->m_bsize, flags, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +11001176 cur->bc_ops->buf_ops);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001177 if (error)
1178 return error;
1179
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00001180 ASSERT(!xfs_buf_geterror(*bpp));
Christoph Hellwig637aa502008-10-30 16:55:45 +11001181 xfs_btree_set_refs(cur, *bpp);
1182 *block = XFS_BUF_TO_BLOCK(*bpp);
Dave Chinner3d3e6f62012-11-12 22:54:08 +11001183 return 0;
Christoph Hellwig637aa502008-10-30 16:55:45 +11001184}
1185
1186/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001187 * Copy keys from one btree block to another.
1188 */
1189STATIC void
1190xfs_btree_copy_keys(
1191 struct xfs_btree_cur *cur,
1192 union xfs_btree_key *dst_key,
1193 union xfs_btree_key *src_key,
1194 int numkeys)
1195{
1196 ASSERT(numkeys >= 0);
1197 memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
1198}
1199
1200/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001201 * Copy records from one btree block to another.
1202 */
1203STATIC void
1204xfs_btree_copy_recs(
1205 struct xfs_btree_cur *cur,
1206 union xfs_btree_rec *dst_rec,
1207 union xfs_btree_rec *src_rec,
1208 int numrecs)
1209{
1210 ASSERT(numrecs >= 0);
1211 memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
1212}
1213
1214/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001215 * Copy block pointers from one btree block to another.
1216 */
1217STATIC void
1218xfs_btree_copy_ptrs(
1219 struct xfs_btree_cur *cur,
1220 union xfs_btree_ptr *dst_ptr,
1221 union xfs_btree_ptr *src_ptr,
1222 int numptrs)
1223{
1224 ASSERT(numptrs >= 0);
1225 memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
1226}
1227
1228/*
1229 * Shift keys one index left/right inside a single btree block.
1230 */
1231STATIC void
1232xfs_btree_shift_keys(
1233 struct xfs_btree_cur *cur,
1234 union xfs_btree_key *key,
1235 int dir,
1236 int numkeys)
1237{
1238 char *dst_key;
1239
1240 ASSERT(numkeys >= 0);
1241 ASSERT(dir == 1 || dir == -1);
1242
1243 dst_key = (char *)key + (dir * cur->bc_ops->key_len);
1244 memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
1245}
1246
1247/*
1248 * Shift records one index left/right inside a single btree block.
1249 */
1250STATIC void
1251xfs_btree_shift_recs(
1252 struct xfs_btree_cur *cur,
1253 union xfs_btree_rec *rec,
1254 int dir,
1255 int numrecs)
1256{
1257 char *dst_rec;
1258
1259 ASSERT(numrecs >= 0);
1260 ASSERT(dir == 1 || dir == -1);
1261
1262 dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
1263 memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
1264}
1265
1266/*
1267 * Shift block pointers one index left/right inside a single btree block.
1268 */
1269STATIC void
1270xfs_btree_shift_ptrs(
1271 struct xfs_btree_cur *cur,
1272 union xfs_btree_ptr *ptr,
1273 int dir,
1274 int numptrs)
1275{
1276 char *dst_ptr;
1277
1278 ASSERT(numptrs >= 0);
1279 ASSERT(dir == 1 || dir == -1);
1280
1281 dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
1282 memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
1283}
1284
1285/*
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001286 * Log key values from the btree block.
1287 */
1288STATIC void
1289xfs_btree_log_keys(
1290 struct xfs_btree_cur *cur,
1291 struct xfs_buf *bp,
1292 int first,
1293 int last)
1294{
1295 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1296 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1297
1298 if (bp) {
Dave Chinner61fe1352013-04-03 16:11:30 +11001299 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001300 xfs_trans_log_buf(cur->bc_tp, bp,
1301 xfs_btree_key_offset(cur, first),
1302 xfs_btree_key_offset(cur, last + 1) - 1);
1303 } else {
1304 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1305 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1306 }
1307
1308 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1309}
1310
1311/*
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001312 * Log record values from the btree block.
1313 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001314void
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001315xfs_btree_log_recs(
1316 struct xfs_btree_cur *cur,
1317 struct xfs_buf *bp,
1318 int first,
1319 int last)
1320{
1321 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1322 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1323
Dave Chinner61fe1352013-04-03 16:11:30 +11001324 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001325 xfs_trans_log_buf(cur->bc_tp, bp,
1326 xfs_btree_rec_offset(cur, first),
1327 xfs_btree_rec_offset(cur, last + 1) - 1);
1328
1329 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1330}
1331
1332/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001333 * Log block pointer fields from a btree block (nonleaf).
1334 */
1335STATIC void
1336xfs_btree_log_ptrs(
1337 struct xfs_btree_cur *cur, /* btree cursor */
1338 struct xfs_buf *bp, /* buffer containing btree block */
1339 int first, /* index of first pointer to log */
1340 int last) /* index of last pointer to log */
1341{
1342 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1343 XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
1344
1345 if (bp) {
1346 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
1347 int level = xfs_btree_get_level(block);
1348
Dave Chinner61fe1352013-04-03 16:11:30 +11001349 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001350 xfs_trans_log_buf(cur->bc_tp, bp,
1351 xfs_btree_ptr_offset(cur, first, level),
1352 xfs_btree_ptr_offset(cur, last + 1, level) - 1);
1353 } else {
1354 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1355 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1356 }
1357
1358 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1359}
1360
1361/*
1362 * Log fields from a btree block header.
1363 */
Christoph Hellwigfd6bcc5b2008-10-30 16:58:21 +11001364void
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001365xfs_btree_log_block(
1366 struct xfs_btree_cur *cur, /* btree cursor */
1367 struct xfs_buf *bp, /* buffer containing btree block */
1368 int fields) /* mask of fields: XFS_BB_... */
1369{
1370 int first; /* first byte offset logged */
1371 int last; /* last byte offset logged */
1372 static const short soffsets[] = { /* table of offsets (short) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001373 offsetof(struct xfs_btree_block, bb_magic),
1374 offsetof(struct xfs_btree_block, bb_level),
1375 offsetof(struct xfs_btree_block, bb_numrecs),
1376 offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
1377 offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001378 offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
1379 offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
1380 offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
1381 offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
1382 offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
1383 XFS_BTREE_SBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001384 };
1385 static const short loffsets[] = { /* table of offsets (long) */
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11001386 offsetof(struct xfs_btree_block, bb_magic),
1387 offsetof(struct xfs_btree_block, bb_level),
1388 offsetof(struct xfs_btree_block, bb_numrecs),
1389 offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
1390 offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001391 offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
1392 offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
1393 offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
1394 offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
1395 offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
1396 offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
1397 XFS_BTREE_LBLOCK_CRC_LEN
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001398 };
1399
1400 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1401 XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
1402
1403 if (bp) {
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001404 int nbits;
1405
1406 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
1407 /*
1408 * We don't log the CRC when updating a btree
1409 * block but instead recreate it during log
1410 * recovery. As the log buffers have checksums
1411 * of their own this is safe and avoids logging a crc
1412 * update in a lot of places.
1413 */
1414 if (fields == XFS_BB_ALL_BITS)
1415 fields = XFS_BB_ALL_BITS_CRC;
1416 nbits = XFS_BB_NUM_BITS_CRC;
1417 } else {
1418 nbits = XFS_BB_NUM_BITS;
1419 }
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001420 xfs_btree_offsets(fields,
1421 (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
1422 loffsets : soffsets,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001423 nbits, &first, &last);
Dave Chinner61fe1352013-04-03 16:11:30 +11001424 xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001425 xfs_trans_log_buf(cur->bc_tp, bp, first, last);
1426 } else {
1427 xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
1428 xfs_ilog_fbroot(cur->bc_private.b.whichfork));
1429 }
1430
1431 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1432}
1433
1434/*
Christoph Hellwig637aa502008-10-30 16:55:45 +11001435 * Increment cursor by one record at the level.
1436 * For nonzero levels the leaf-ward information is untouched.
1437 */
1438int /* error */
1439xfs_btree_increment(
1440 struct xfs_btree_cur *cur,
1441 int level,
1442 int *stat) /* success/failure */
1443{
1444 struct xfs_btree_block *block;
1445 union xfs_btree_ptr ptr;
1446 struct xfs_buf *bp;
1447 int error; /* error return value */
1448 int lev;
1449
1450 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1451 XFS_BTREE_TRACE_ARGI(cur, level);
1452
1453 ASSERT(level < cur->bc_nlevels);
1454
1455 /* Read-ahead to the right at this level. */
1456 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
1457
1458 /* Get a pointer to the btree block. */
1459 block = xfs_btree_get_block(cur, level, &bp);
1460
1461#ifdef DEBUG
1462 error = xfs_btree_check_block(cur, block, level, bp);
1463 if (error)
1464 goto error0;
1465#endif
1466
1467 /* We're done if we remain in the block after the increment. */
1468 if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
1469 goto out1;
1470
1471 /* Fail if we just went off the right edge of the tree. */
1472 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1473 if (xfs_btree_ptr_is_null(cur, &ptr))
1474 goto out0;
1475
1476 XFS_BTREE_STATS_INC(cur, increment);
1477
1478 /*
1479 * March up the tree incrementing pointers.
1480 * Stop when we don't go off the right edge of a block.
1481 */
1482 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1483 block = xfs_btree_get_block(cur, lev, &bp);
1484
1485#ifdef DEBUG
1486 error = xfs_btree_check_block(cur, block, lev, bp);
1487 if (error)
1488 goto error0;
1489#endif
1490
1491 if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
1492 break;
1493
1494 /* Read-ahead the right block for the next loop. */
1495 xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
1496 }
1497
1498 /*
1499 * If we went off the root then we are either seriously
1500 * confused or have the tree root in an inode.
1501 */
1502 if (lev == cur->bc_nlevels) {
1503 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1504 goto out0;
1505 ASSERT(0);
1506 error = EFSCORRUPTED;
1507 goto error0;
1508 }
1509 ASSERT(lev < cur->bc_nlevels);
1510
1511 /*
1512 * Now walk back down the tree, fixing up the cursor's buffer
1513 * pointers and key numbers.
1514 */
1515 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1516 union xfs_btree_ptr *ptrp;
1517
1518 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001519 --lev;
1520 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig637aa502008-10-30 16:55:45 +11001521 if (error)
1522 goto error0;
1523
1524 xfs_btree_setbuf(cur, lev, bp);
1525 cur->bc_ptrs[lev] = 1;
1526 }
1527out1:
1528 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1529 *stat = 1;
1530 return 0;
1531
1532out0:
1533 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1534 *stat = 0;
1535 return 0;
1536
1537error0:
1538 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1539 return error;
1540}
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001541
1542/*
1543 * Decrement cursor by one record at the level.
1544 * For nonzero levels the leaf-ward information is untouched.
1545 */
1546int /* error */
1547xfs_btree_decrement(
1548 struct xfs_btree_cur *cur,
1549 int level,
1550 int *stat) /* success/failure */
1551{
1552 struct xfs_btree_block *block;
1553 xfs_buf_t *bp;
1554 int error; /* error return value */
1555 int lev;
1556 union xfs_btree_ptr ptr;
1557
1558 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1559 XFS_BTREE_TRACE_ARGI(cur, level);
1560
1561 ASSERT(level < cur->bc_nlevels);
1562
1563 /* Read-ahead to the left at this level. */
1564 xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
1565
1566 /* We're done if we remain in the block after the decrement. */
1567 if (--cur->bc_ptrs[level] > 0)
1568 goto out1;
1569
1570 /* Get a pointer to the btree block. */
1571 block = xfs_btree_get_block(cur, level, &bp);
1572
1573#ifdef DEBUG
1574 error = xfs_btree_check_block(cur, block, level, bp);
1575 if (error)
1576 goto error0;
1577#endif
1578
1579 /* Fail if we just went off the left edge of the tree. */
1580 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
1581 if (xfs_btree_ptr_is_null(cur, &ptr))
1582 goto out0;
1583
1584 XFS_BTREE_STATS_INC(cur, decrement);
1585
1586 /*
1587 * March up the tree decrementing pointers.
1588 * Stop when we don't go off the left edge of a block.
1589 */
1590 for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
1591 if (--cur->bc_ptrs[lev] > 0)
1592 break;
1593 /* Read-ahead the left block for the next loop. */
1594 xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
1595 }
1596
1597 /*
1598 * If we went off the root then we are seriously confused.
1599 * or the root of the tree is in an inode.
1600 */
1601 if (lev == cur->bc_nlevels) {
1602 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
1603 goto out0;
1604 ASSERT(0);
1605 error = EFSCORRUPTED;
1606 goto error0;
1607 }
1608 ASSERT(lev < cur->bc_nlevels);
1609
1610 /*
1611 * Now walk back down the tree, fixing up the cursor's buffer
1612 * pointers and key numbers.
1613 */
1614 for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
1615 union xfs_btree_ptr *ptrp;
1616
1617 ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001618 --lev;
1619 error = xfs_btree_read_buf_block(cur, ptrp, 0, &block, &bp);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11001620 if (error)
1621 goto error0;
1622 xfs_btree_setbuf(cur, lev, bp);
1623 cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
1624 }
1625out1:
1626 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1627 *stat = 1;
1628 return 0;
1629
1630out0:
1631 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1632 *stat = 0;
1633 return 0;
1634
1635error0:
1636 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1637 return error;
1638}
1639
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001640STATIC int
1641xfs_btree_lookup_get_block(
1642 struct xfs_btree_cur *cur, /* btree cursor */
1643 int level, /* level in the btree */
1644 union xfs_btree_ptr *pp, /* ptr to btree block */
1645 struct xfs_btree_block **blkp) /* return btree block */
1646{
1647 struct xfs_buf *bp; /* buffer pointer for btree block */
1648 int error = 0;
1649
1650 /* special case the root block if in an inode */
1651 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1652 (level == cur->bc_nlevels - 1)) {
1653 *blkp = xfs_btree_get_iroot(cur);
1654 return 0;
1655 }
1656
1657 /*
1658 * If the old buffer at this level for the disk address we are
1659 * looking for re-use it.
1660 *
1661 * Otherwise throw it away and get a new one.
1662 */
1663 bp = cur->bc_bufs[level];
1664 if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
1665 *blkp = XFS_BUF_TO_BLOCK(bp);
1666 return 0;
1667 }
1668
Eric Sandeen0d7409b2014-04-14 18:59:56 +10001669 error = xfs_btree_read_buf_block(cur, pp, 0, blkp, &bp);
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001670 if (error)
1671 return error;
1672
1673 xfs_btree_setbuf(cur, level, bp);
1674 return 0;
1675}
1676
1677/*
1678 * Get current search key. For level 0 we don't actually have a key
1679 * structure so we make one up from the record. For all other levels
1680 * we just return the right key.
1681 */
1682STATIC union xfs_btree_key *
1683xfs_lookup_get_search_key(
1684 struct xfs_btree_cur *cur,
1685 int level,
1686 int keyno,
1687 struct xfs_btree_block *block,
1688 union xfs_btree_key *kp)
1689{
1690 if (level == 0) {
1691 cur->bc_ops->init_key_from_rec(kp,
1692 xfs_btree_rec_addr(cur, keyno, block));
1693 return kp;
1694 }
1695
1696 return xfs_btree_key_addr(cur, keyno, block);
1697}
1698
1699/*
1700 * Lookup the record. The cursor is made to point to it, based on dir.
Zhi Yong Wu49d3da12013-08-07 10:11:00 +00001701 * stat is set to 0 if can't find any such record, 1 for success.
Christoph Hellwigfe033cc2008-10-30 16:56:09 +11001702 */
1703int /* error */
1704xfs_btree_lookup(
1705 struct xfs_btree_cur *cur, /* btree cursor */
1706 xfs_lookup_t dir, /* <=, ==, or >= */
1707 int *stat) /* success/failure */
1708{
1709 struct xfs_btree_block *block; /* current btree block */
1710 __int64_t diff; /* difference for the current key */
1711 int error; /* error return value */
1712 int keyno; /* current key number */
1713 int level; /* level in the btree */
1714 union xfs_btree_ptr *pp; /* ptr to btree block */
1715 union xfs_btree_ptr ptr; /* ptr to btree block */
1716
1717 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1718 XFS_BTREE_TRACE_ARGI(cur, dir);
1719
1720 XFS_BTREE_STATS_INC(cur, lookup);
1721
1722 block = NULL;
1723 keyno = 0;
1724
1725 /* initialise start pointer from cursor */
1726 cur->bc_ops->init_ptr_from_cur(cur, &ptr);
1727 pp = &ptr;
1728
1729 /*
1730 * Iterate over each level in the btree, starting at the root.
1731 * For each level above the leaves, find the key we need, based
1732 * on the lookup record, then follow the corresponding block
1733 * pointer down to the next level.
1734 */
1735 for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
1736 /* Get the block we need to do the lookup on. */
1737 error = xfs_btree_lookup_get_block(cur, level, pp, &block);
1738 if (error)
1739 goto error0;
1740
1741 if (diff == 0) {
1742 /*
1743 * If we already had a key match at a higher level, we
1744 * know we need to use the first entry in this block.
1745 */
1746 keyno = 1;
1747 } else {
1748 /* Otherwise search this block. Do a binary search. */
1749
1750 int high; /* high entry number */
1751 int low; /* low entry number */
1752
1753 /* Set low and high entry numbers, 1-based. */
1754 low = 1;
1755 high = xfs_btree_get_numrecs(block);
1756 if (!high) {
1757 /* Block is empty, must be an empty leaf. */
1758 ASSERT(level == 0 && cur->bc_nlevels == 1);
1759
1760 cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
1761 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1762 *stat = 0;
1763 return 0;
1764 }
1765
1766 /* Binary search the block. */
1767 while (low <= high) {
1768 union xfs_btree_key key;
1769 union xfs_btree_key *kp;
1770
1771 XFS_BTREE_STATS_INC(cur, compare);
1772
1773 /* keyno is average of low and high. */
1774 keyno = (low + high) >> 1;
1775
1776 /* Get current search key */
1777 kp = xfs_lookup_get_search_key(cur, level,
1778 keyno, block, &key);
1779
1780 /*
1781 * Compute difference to get next direction:
1782 * - less than, move right
1783 * - greater than, move left
1784 * - equal, we're done
1785 */
1786 diff = cur->bc_ops->key_diff(cur, kp);
1787 if (diff < 0)
1788 low = keyno + 1;
1789 else if (diff > 0)
1790 high = keyno - 1;
1791 else
1792 break;
1793 }
1794 }
1795
1796 /*
1797 * If there are more levels, set up for the next level
1798 * by getting the block number and filling in the cursor.
1799 */
1800 if (level > 0) {
1801 /*
1802 * If we moved left, need the previous key number,
1803 * unless there isn't one.
1804 */
1805 if (diff > 0 && --keyno < 1)
1806 keyno = 1;
1807 pp = xfs_btree_ptr_addr(cur, keyno, block);
1808
1809#ifdef DEBUG
1810 error = xfs_btree_check_ptr(cur, pp, 0, level);
1811 if (error)
1812 goto error0;
1813#endif
1814 cur->bc_ptrs[level] = keyno;
1815 }
1816 }
1817
1818 /* Done with the search. See if we need to adjust the results. */
1819 if (dir != XFS_LOOKUP_LE && diff < 0) {
1820 keyno++;
1821 /*
1822 * If ge search and we went off the end of the block, but it's
1823 * not the last block, we're in the wrong block.
1824 */
1825 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
1826 if (dir == XFS_LOOKUP_GE &&
1827 keyno > xfs_btree_get_numrecs(block) &&
1828 !xfs_btree_ptr_is_null(cur, &ptr)) {
1829 int i;
1830
1831 cur->bc_ptrs[0] = keyno;
1832 error = xfs_btree_increment(cur, 0, &i);
1833 if (error)
1834 goto error0;
1835 XFS_WANT_CORRUPTED_RETURN(i == 1);
1836 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1837 *stat = 1;
1838 return 0;
1839 }
1840 } else if (dir == XFS_LOOKUP_LE && diff > 0)
1841 keyno--;
1842 cur->bc_ptrs[0] = keyno;
1843
1844 /* Return if we succeeded or not. */
1845 if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
1846 *stat = 0;
1847 else if (dir != XFS_LOOKUP_EQ || diff == 0)
1848 *stat = 1;
1849 else
1850 *stat = 0;
1851 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1852 return 0;
1853
1854error0:
1855 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1856 return error;
1857}
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001858
1859/*
1860 * Update keys at all levels from here to the root along the cursor's path.
1861 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11001862STATIC int
Christoph Hellwig38bb7422008-10-30 16:56:22 +11001863xfs_btree_updkey(
1864 struct xfs_btree_cur *cur,
1865 union xfs_btree_key *keyp,
1866 int level)
1867{
1868 struct xfs_btree_block *block;
1869 struct xfs_buf *bp;
1870 union xfs_btree_key *kp;
1871 int ptr;
1872
1873 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1874 XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
1875
1876 ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
1877
1878 /*
1879 * Go up the tree from this level toward the root.
1880 * At each level, update the key value to the value input.
1881 * Stop when we reach a level where the cursor isn't pointing
1882 * at the first entry in the block.
1883 */
1884 for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
1885#ifdef DEBUG
1886 int error;
1887#endif
1888 block = xfs_btree_get_block(cur, level, &bp);
1889#ifdef DEBUG
1890 error = xfs_btree_check_block(cur, block, level, bp);
1891 if (error) {
1892 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1893 return error;
1894 }
1895#endif
1896 ptr = cur->bc_ptrs[level];
1897 kp = xfs_btree_key_addr(cur, ptr, block);
1898 xfs_btree_copy_keys(cur, kp, keyp, 1);
1899 xfs_btree_log_keys(cur, bp, ptr, ptr);
1900 }
1901
1902 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1903 return 0;
1904}
Christoph Hellwig278d0ca2008-10-30 16:56:32 +11001905
1906/*
1907 * Update the record referred to by cur to the value in the
1908 * given record. This either works (return 0) or gets an
1909 * EFSCORRUPTED error.
1910 */
1911int
1912xfs_btree_update(
1913 struct xfs_btree_cur *cur,
1914 union xfs_btree_rec *rec)
1915{
1916 struct xfs_btree_block *block;
1917 struct xfs_buf *bp;
1918 int error;
1919 int ptr;
1920 union xfs_btree_rec *rp;
1921
1922 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1923 XFS_BTREE_TRACE_ARGR(cur, rec);
1924
1925 /* Pick up the current block. */
1926 block = xfs_btree_get_block(cur, 0, &bp);
1927
1928#ifdef DEBUG
1929 error = xfs_btree_check_block(cur, block, 0, bp);
1930 if (error)
1931 goto error0;
1932#endif
1933 /* Get the address of the rec to be updated. */
1934 ptr = cur->bc_ptrs[0];
1935 rp = xfs_btree_rec_addr(cur, ptr, block);
1936
1937 /* Fill in the new contents and log them. */
1938 xfs_btree_copy_recs(cur, rp, rec, 1);
1939 xfs_btree_log_recs(cur, bp, ptr, ptr);
1940
1941 /*
1942 * If we are tracking the last record in the tree and
1943 * we are at the far right edge of the tree, update it.
1944 */
1945 if (xfs_btree_is_lastrec(cur, block, 0)) {
1946 cur->bc_ops->update_lastrec(cur, block, rec,
1947 ptr, LASTREC_UPDATE);
1948 }
1949
1950 /* Updating first rec in leaf. Pass new key value up to our parent. */
1951 if (ptr == 1) {
1952 union xfs_btree_key key;
1953
1954 cur->bc_ops->init_key_from_rec(&key, rec);
1955 error = xfs_btree_updkey(cur, &key, 1);
1956 if (error)
1957 goto error0;
1958 }
1959
1960 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
1961 return 0;
1962
1963error0:
1964 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
1965 return error;
1966}
1967
Christoph Hellwig9eaead52008-10-30 16:56:43 +11001968/*
Christoph Hellwig687b8902008-10-30 16:56:53 +11001969 * Move 1 record left from cur/level if possible.
1970 * Update cur to reflect the new path.
1971 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11001972STATIC int /* error */
Christoph Hellwig687b8902008-10-30 16:56:53 +11001973xfs_btree_lshift(
1974 struct xfs_btree_cur *cur,
1975 int level,
1976 int *stat) /* success/failure */
1977{
1978 union xfs_btree_key key; /* btree key */
1979 struct xfs_buf *lbp; /* left buffer pointer */
1980 struct xfs_btree_block *left; /* left btree block */
1981 int lrecs; /* left record count */
1982 struct xfs_buf *rbp; /* right buffer pointer */
1983 struct xfs_btree_block *right; /* right btree block */
1984 int rrecs; /* right record count */
1985 union xfs_btree_ptr lptr; /* left btree pointer */
1986 union xfs_btree_key *rkp = NULL; /* right btree key */
1987 union xfs_btree_ptr *rpp = NULL; /* right address pointer */
1988 union xfs_btree_rec *rrp = NULL; /* right record pointer */
1989 int error; /* error return value */
1990
1991 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
1992 XFS_BTREE_TRACE_ARGI(cur, level);
1993
1994 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
1995 level == cur->bc_nlevels - 1)
1996 goto out0;
1997
1998 /* Set up variables for this block as "right". */
1999 right = xfs_btree_get_block(cur, level, &rbp);
2000
2001#ifdef DEBUG
2002 error = xfs_btree_check_block(cur, right, level, rbp);
2003 if (error)
2004 goto error0;
2005#endif
2006
2007 /* If we've got no left sibling then we can't shift an entry left. */
2008 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2009 if (xfs_btree_ptr_is_null(cur, &lptr))
2010 goto out0;
2011
2012 /*
2013 * If the cursor entry is the one that would be moved, don't
2014 * do it... it's too complicated.
2015 */
2016 if (cur->bc_ptrs[level] <= 1)
2017 goto out0;
2018
2019 /* Set up the left neighbor as "left". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002020 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig687b8902008-10-30 16:56:53 +11002021 if (error)
2022 goto error0;
2023
2024 /* If it's full, it can't take another entry. */
2025 lrecs = xfs_btree_get_numrecs(left);
2026 if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
2027 goto out0;
2028
2029 rrecs = xfs_btree_get_numrecs(right);
2030
2031 /*
2032 * We add one entry to the left side and remove one for the right side.
Malcolm Parsons9da096f2009-03-29 09:55:42 +02002033 * Account for it here, the changes will be updated on disk and logged
Christoph Hellwig687b8902008-10-30 16:56:53 +11002034 * later.
2035 */
2036 lrecs++;
2037 rrecs--;
2038
2039 XFS_BTREE_STATS_INC(cur, lshift);
2040 XFS_BTREE_STATS_ADD(cur, moves, 1);
2041
2042 /*
2043 * If non-leaf, copy a key and a ptr to the left block.
2044 * Log the changes to the left block.
2045 */
2046 if (level > 0) {
2047 /* It's a non-leaf. Move keys and pointers. */
2048 union xfs_btree_key *lkp; /* left btree key */
2049 union xfs_btree_ptr *lpp; /* left address pointer */
2050
2051 lkp = xfs_btree_key_addr(cur, lrecs, left);
2052 rkp = xfs_btree_key_addr(cur, 1, right);
2053
2054 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2055 rpp = xfs_btree_ptr_addr(cur, 1, right);
2056#ifdef DEBUG
2057 error = xfs_btree_check_ptr(cur, rpp, 0, level);
2058 if (error)
2059 goto error0;
2060#endif
2061 xfs_btree_copy_keys(cur, lkp, rkp, 1);
2062 xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
2063
2064 xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
2065 xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
2066
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002067 ASSERT(cur->bc_ops->keys_inorder(cur,
2068 xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002069 } else {
2070 /* It's a leaf. Move records. */
2071 union xfs_btree_rec *lrp; /* left record pointer */
2072
2073 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2074 rrp = xfs_btree_rec_addr(cur, 1, right);
2075
2076 xfs_btree_copy_recs(cur, lrp, rrp, 1);
2077 xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
2078
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002079 ASSERT(cur->bc_ops->recs_inorder(cur,
2080 xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
Christoph Hellwig687b8902008-10-30 16:56:53 +11002081 }
2082
2083 xfs_btree_set_numrecs(left, lrecs);
2084 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2085
2086 xfs_btree_set_numrecs(right, rrecs);
2087 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2088
2089 /*
2090 * Slide the contents of right down one entry.
2091 */
2092 XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
2093 if (level > 0) {
2094 /* It's a nonleaf. operate on keys and ptrs */
2095#ifdef DEBUG
2096 int i; /* loop index */
2097
2098 for (i = 0; i < rrecs; i++) {
2099 error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
2100 if (error)
2101 goto error0;
2102 }
2103#endif
2104 xfs_btree_shift_keys(cur,
2105 xfs_btree_key_addr(cur, 2, right),
2106 -1, rrecs);
2107 xfs_btree_shift_ptrs(cur,
2108 xfs_btree_ptr_addr(cur, 2, right),
2109 -1, rrecs);
2110
2111 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2112 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2113 } else {
2114 /* It's a leaf. operate on records */
2115 xfs_btree_shift_recs(cur,
2116 xfs_btree_rec_addr(cur, 2, right),
2117 -1, rrecs);
2118 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2119
2120 /*
2121 * If it's the first record in the block, we'll need a key
2122 * structure to pass up to the next level (updkey).
2123 */
2124 cur->bc_ops->init_key_from_rec(&key,
2125 xfs_btree_rec_addr(cur, 1, right));
2126 rkp = &key;
2127 }
2128
2129 /* Update the parent key values of right. */
2130 error = xfs_btree_updkey(cur, rkp, level + 1);
2131 if (error)
2132 goto error0;
2133
2134 /* Slide the cursor value left one. */
2135 cur->bc_ptrs[level]--;
2136
2137 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2138 *stat = 1;
2139 return 0;
2140
2141out0:
2142 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2143 *stat = 0;
2144 return 0;
2145
2146error0:
2147 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2148 return error;
2149}
2150
2151/*
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002152 * Move 1 record right from cur/level if possible.
2153 * Update cur to reflect the new path.
2154 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002155STATIC int /* error */
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002156xfs_btree_rshift(
2157 struct xfs_btree_cur *cur,
2158 int level,
2159 int *stat) /* success/failure */
2160{
2161 union xfs_btree_key key; /* btree key */
2162 struct xfs_buf *lbp; /* left buffer pointer */
2163 struct xfs_btree_block *left; /* left btree block */
2164 struct xfs_buf *rbp; /* right buffer pointer */
2165 struct xfs_btree_block *right; /* right btree block */
2166 struct xfs_btree_cur *tcur; /* temporary btree cursor */
2167 union xfs_btree_ptr rptr; /* right block pointer */
2168 union xfs_btree_key *rkp; /* right btree key */
2169 int rrecs; /* right record count */
2170 int lrecs; /* left record count */
2171 int error; /* error return value */
2172 int i; /* loop counter */
2173
2174 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2175 XFS_BTREE_TRACE_ARGI(cur, level);
2176
2177 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2178 (level == cur->bc_nlevels - 1))
2179 goto out0;
2180
2181 /* Set up variables for this block as "left". */
2182 left = xfs_btree_get_block(cur, level, &lbp);
2183
2184#ifdef DEBUG
2185 error = xfs_btree_check_block(cur, left, level, lbp);
2186 if (error)
2187 goto error0;
2188#endif
2189
2190 /* If we've got no right sibling then we can't shift an entry right. */
2191 xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2192 if (xfs_btree_ptr_is_null(cur, &rptr))
2193 goto out0;
2194
2195 /*
2196 * If the cursor entry is the one that would be moved, don't
2197 * do it... it's too complicated.
2198 */
2199 lrecs = xfs_btree_get_numrecs(left);
2200 if (cur->bc_ptrs[level] >= lrecs)
2201 goto out0;
2202
2203 /* Set up the right neighbor as "right". */
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002204 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002205 if (error)
2206 goto error0;
2207
2208 /* If it's full, it can't take another entry. */
2209 rrecs = xfs_btree_get_numrecs(right);
2210 if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
2211 goto out0;
2212
2213 XFS_BTREE_STATS_INC(cur, rshift);
2214 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2215
2216 /*
2217 * Make a hole at the start of the right neighbor block, then
2218 * copy the last left block entry to the hole.
2219 */
2220 if (level > 0) {
2221 /* It's a nonleaf. make a hole in the keys and ptrs */
2222 union xfs_btree_key *lkp;
2223 union xfs_btree_ptr *lpp;
2224 union xfs_btree_ptr *rpp;
2225
2226 lkp = xfs_btree_key_addr(cur, lrecs, left);
2227 lpp = xfs_btree_ptr_addr(cur, lrecs, left);
2228 rkp = xfs_btree_key_addr(cur, 1, right);
2229 rpp = xfs_btree_ptr_addr(cur, 1, right);
2230
2231#ifdef DEBUG
2232 for (i = rrecs - 1; i >= 0; i--) {
2233 error = xfs_btree_check_ptr(cur, rpp, i, level);
2234 if (error)
2235 goto error0;
2236 }
2237#endif
2238
2239 xfs_btree_shift_keys(cur, rkp, 1, rrecs);
2240 xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
2241
2242#ifdef DEBUG
2243 error = xfs_btree_check_ptr(cur, lpp, 0, level);
2244 if (error)
2245 goto error0;
2246#endif
2247
2248 /* Now put the new data in, and log it. */
2249 xfs_btree_copy_keys(cur, rkp, lkp, 1);
2250 xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
2251
2252 xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
2253 xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
2254
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002255 ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
2256 xfs_btree_key_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002257 } else {
2258 /* It's a leaf. make a hole in the records */
2259 union xfs_btree_rec *lrp;
2260 union xfs_btree_rec *rrp;
2261
2262 lrp = xfs_btree_rec_addr(cur, lrecs, left);
2263 rrp = xfs_btree_rec_addr(cur, 1, right);
2264
2265 xfs_btree_shift_recs(cur, rrp, 1, rrecs);
2266
2267 /* Now put the new data in, and log it. */
2268 xfs_btree_copy_recs(cur, rrp, lrp, 1);
2269 xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
2270
2271 cur->bc_ops->init_key_from_rec(&key, rrp);
2272 rkp = &key;
2273
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002274 ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
2275 xfs_btree_rec_addr(cur, 2, right)));
Christoph Hellwig9eaead52008-10-30 16:56:43 +11002276 }
2277
2278 /*
2279 * Decrement and log left's numrecs, bump and log right's numrecs.
2280 */
2281 xfs_btree_set_numrecs(left, --lrecs);
2282 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
2283
2284 xfs_btree_set_numrecs(right, ++rrecs);
2285 xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
2286
2287 /*
2288 * Using a temporary cursor, update the parent key values of the
2289 * block on the right.
2290 */
2291 error = xfs_btree_dup_cursor(cur, &tcur);
2292 if (error)
2293 goto error0;
2294 i = xfs_btree_lastrec(tcur, level);
2295 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
2296
2297 error = xfs_btree_increment(tcur, level, &i);
2298 if (error)
2299 goto error1;
2300
2301 error = xfs_btree_updkey(tcur, rkp, level + 1);
2302 if (error)
2303 goto error1;
2304
2305 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
2306
2307 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2308 *stat = 1;
2309 return 0;
2310
2311out0:
2312 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2313 *stat = 0;
2314 return 0;
2315
2316error0:
2317 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2318 return error;
2319
2320error1:
2321 XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
2322 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
2323 return error;
2324}
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002325
2326/*
2327 * Split cur/level block in half.
2328 * Return new block number and the key to its first
2329 * record (to be inserted into parent).
2330 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002331STATIC int /* error */
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002332xfs_btree_split(
2333 struct xfs_btree_cur *cur,
2334 int level,
2335 union xfs_btree_ptr *ptrp,
2336 union xfs_btree_key *key,
2337 struct xfs_btree_cur **curp,
2338 int *stat) /* success/failure */
2339{
2340 union xfs_btree_ptr lptr; /* left sibling block ptr */
2341 struct xfs_buf *lbp; /* left buffer pointer */
2342 struct xfs_btree_block *left; /* left btree block */
2343 union xfs_btree_ptr rptr; /* right sibling block ptr */
2344 struct xfs_buf *rbp; /* right buffer pointer */
2345 struct xfs_btree_block *right; /* right btree block */
2346 union xfs_btree_ptr rrptr; /* right-right sibling ptr */
2347 struct xfs_buf *rrbp; /* right-right buffer pointer */
2348 struct xfs_btree_block *rrblock; /* right-right btree block */
2349 int lrecs;
2350 int rrecs;
2351 int src_index;
2352 int error; /* error return value */
2353#ifdef DEBUG
2354 int i;
2355#endif
2356
2357 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2358 XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
2359
2360 XFS_BTREE_STATS_INC(cur, split);
2361
2362 /* Set up left block (current one). */
2363 left = xfs_btree_get_block(cur, level, &lbp);
2364
2365#ifdef DEBUG
2366 error = xfs_btree_check_block(cur, left, level, lbp);
2367 if (error)
2368 goto error0;
2369#endif
2370
2371 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2372
2373 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2374 error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
2375 if (error)
2376 goto error0;
2377 if (*stat == 0)
2378 goto out0;
2379 XFS_BTREE_STATS_INC(cur, alloc);
2380
2381 /* Set up the new block as "right". */
2382 error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
2383 if (error)
2384 goto error0;
2385
2386 /* Fill in the btree header for the new right block. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002387 xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002388
2389 /*
2390 * Split the entries between the old and the new block evenly.
2391 * Make sure that if there's an odd number of entries now, that
2392 * each new block will have the same number of entries.
2393 */
2394 lrecs = xfs_btree_get_numrecs(left);
2395 rrecs = lrecs / 2;
2396 if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
2397 rrecs++;
2398 src_index = (lrecs - rrecs + 1);
2399
2400 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
2401
2402 /*
2403 * Copy btree block entries from the left block over to the
2404 * new block, the right. Update the right block and log the
2405 * changes.
2406 */
2407 if (level > 0) {
2408 /* It's a non-leaf. Move keys and pointers. */
2409 union xfs_btree_key *lkp; /* left btree key */
2410 union xfs_btree_ptr *lpp; /* left address pointer */
2411 union xfs_btree_key *rkp; /* right btree key */
2412 union xfs_btree_ptr *rpp; /* right address pointer */
2413
2414 lkp = xfs_btree_key_addr(cur, src_index, left);
2415 lpp = xfs_btree_ptr_addr(cur, src_index, left);
2416 rkp = xfs_btree_key_addr(cur, 1, right);
2417 rpp = xfs_btree_ptr_addr(cur, 1, right);
2418
2419#ifdef DEBUG
2420 for (i = src_index; i < rrecs; i++) {
2421 error = xfs_btree_check_ptr(cur, lpp, i, level);
2422 if (error)
2423 goto error0;
2424 }
2425#endif
2426
2427 xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
2428 xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
2429
2430 xfs_btree_log_keys(cur, rbp, 1, rrecs);
2431 xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
2432
2433 /* Grab the keys to the entries moved to the right block */
2434 xfs_btree_copy_keys(cur, key, rkp, 1);
2435 } else {
2436 /* It's a leaf. Move records. */
2437 union xfs_btree_rec *lrp; /* left record pointer */
2438 union xfs_btree_rec *rrp; /* right record pointer */
2439
2440 lrp = xfs_btree_rec_addr(cur, src_index, left);
2441 rrp = xfs_btree_rec_addr(cur, 1, right);
2442
2443 xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
2444 xfs_btree_log_recs(cur, rbp, 1, rrecs);
2445
2446 cur->bc_ops->init_key_from_rec(key,
2447 xfs_btree_rec_addr(cur, 1, right));
2448 }
2449
2450
2451 /*
2452 * Find the left block number by looking in the buffer.
2453 * Adjust numrecs, sibling pointers.
2454 */
2455 xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
2456 xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
2457 xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
2458 xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
2459
2460 lrecs -= rrecs;
2461 xfs_btree_set_numrecs(left, lrecs);
2462 xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
2463
2464 xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
2465 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
2466
2467 /*
2468 * If there's a block to the new block's right, make that block
2469 * point back to right instead of to left.
2470 */
2471 if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002472 error = xfs_btree_read_buf_block(cur, &rrptr,
Christoph Hellwigf5eb8e72008-10-30 16:57:03 +11002473 0, &rrblock, &rrbp);
2474 if (error)
2475 goto error0;
2476 xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
2477 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
2478 }
2479 /*
2480 * If the cursor is really in the right block, move it there.
2481 * If it's just pointing past the last entry in left, then we'll
2482 * insert there, so don't change anything in that case.
2483 */
2484 if (cur->bc_ptrs[level] > lrecs + 1) {
2485 xfs_btree_setbuf(cur, level, rbp);
2486 cur->bc_ptrs[level] -= lrecs;
2487 }
2488 /*
2489 * If there are more levels, we'll need another cursor which refers
2490 * the right block, no matter where this cursor was.
2491 */
2492 if (level + 1 < cur->bc_nlevels) {
2493 error = xfs_btree_dup_cursor(cur, curp);
2494 if (error)
2495 goto error0;
2496 (*curp)->bc_ptrs[level + 1]++;
2497 }
2498 *ptrp = rptr;
2499 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2500 *stat = 1;
2501 return 0;
2502out0:
2503 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2504 *stat = 0;
2505 return 0;
2506
2507error0:
2508 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2509 return error;
2510}
Christoph Hellwig344207c2008-10-30 16:57:16 +11002511
2512/*
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002513 * Copy the old inode root contents into a real block and make the
2514 * broot point to it.
2515 */
2516int /* error */
2517xfs_btree_new_iroot(
2518 struct xfs_btree_cur *cur, /* btree cursor */
2519 int *logflags, /* logging flags for inode */
2520 int *stat) /* return status - 0 fail */
2521{
2522 struct xfs_buf *cbp; /* buffer for cblock */
2523 struct xfs_btree_block *block; /* btree block */
2524 struct xfs_btree_block *cblock; /* child btree block */
2525 union xfs_btree_key *ckp; /* child key pointer */
2526 union xfs_btree_ptr *cpp; /* child ptr pointer */
2527 union xfs_btree_key *kp; /* pointer to btree key */
2528 union xfs_btree_ptr *pp; /* pointer to block addr */
2529 union xfs_btree_ptr nptr; /* new block addr */
2530 int level; /* btree level */
2531 int error; /* error return code */
2532#ifdef DEBUG
2533 int i; /* loop counter */
2534#endif
2535
2536 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2537 XFS_BTREE_STATS_INC(cur, newroot);
2538
2539 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
2540
2541 level = cur->bc_nlevels - 1;
2542
2543 block = xfs_btree_get_iroot(cur);
2544 pp = xfs_btree_ptr_addr(cur, 1, block);
2545
2546 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2547 error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
2548 if (error)
2549 goto error0;
2550 if (*stat == 0) {
2551 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2552 return 0;
2553 }
2554 XFS_BTREE_STATS_INC(cur, alloc);
2555
2556 /* Copy the root into a real block. */
2557 error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
2558 if (error)
2559 goto error0;
2560
Dave Chinner088c9f62013-06-12 12:19:08 +10002561 /*
2562 * we can't just memcpy() the root in for CRC enabled btree blocks.
2563 * In that case have to also ensure the blkno remains correct
2564 */
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002565 memcpy(cblock, block, xfs_btree_block_len(cur));
Dave Chinner088c9f62013-06-12 12:19:08 +10002566 if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
2567 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
2568 cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
2569 else
2570 cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
2571 }
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002572
2573 be16_add_cpu(&block->bb_level, 1);
2574 xfs_btree_set_numrecs(block, 1);
2575 cur->bc_nlevels++;
2576 cur->bc_ptrs[level + 1] = 1;
2577
2578 kp = xfs_btree_key_addr(cur, 1, block);
2579 ckp = xfs_btree_key_addr(cur, 1, cblock);
2580 xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
2581
2582 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
2583#ifdef DEBUG
2584 for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
2585 error = xfs_btree_check_ptr(cur, pp, i, level);
2586 if (error)
2587 goto error0;
2588 }
2589#endif
2590 xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
2591
2592#ifdef DEBUG
2593 error = xfs_btree_check_ptr(cur, &nptr, 0, level);
2594 if (error)
2595 goto error0;
2596#endif
2597 xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
2598
2599 xfs_iroot_realloc(cur->bc_private.b.ip,
2600 1 - xfs_btree_get_numrecs(cblock),
2601 cur->bc_private.b.whichfork);
2602
2603 xfs_btree_setbuf(cur, level, cbp);
2604
2605 /*
2606 * Do all this logging at the end so that
2607 * the root is at the right level.
2608 */
2609 xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
2610 xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2611 xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
2612
2613 *logflags |=
Eric Sandeen9d87c312009-01-14 23:22:07 -06002614 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11002615 *stat = 1;
2616 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2617 return 0;
2618error0:
2619 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2620 return error;
2621}
2622
2623/*
Christoph Hellwig344207c2008-10-30 16:57:16 +11002624 * Allocate a new root block, fill it in.
2625 */
Christoph Hellwig3cc75242008-10-30 16:58:41 +11002626STATIC int /* error */
Christoph Hellwig344207c2008-10-30 16:57:16 +11002627xfs_btree_new_root(
2628 struct xfs_btree_cur *cur, /* btree cursor */
2629 int *stat) /* success/failure */
2630{
2631 struct xfs_btree_block *block; /* one half of the old root block */
2632 struct xfs_buf *bp; /* buffer containing block */
2633 int error; /* error return value */
2634 struct xfs_buf *lbp; /* left buffer pointer */
2635 struct xfs_btree_block *left; /* left btree block */
2636 struct xfs_buf *nbp; /* new (root) buffer */
2637 struct xfs_btree_block *new; /* new (root) btree block */
2638 int nptr; /* new value for key index, 1 or 2 */
2639 struct xfs_buf *rbp; /* right buffer pointer */
2640 struct xfs_btree_block *right; /* right btree block */
2641 union xfs_btree_ptr rptr;
2642 union xfs_btree_ptr lptr;
2643
2644 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2645 XFS_BTREE_STATS_INC(cur, newroot);
2646
2647 /* initialise our start point from the cursor */
2648 cur->bc_ops->init_ptr_from_cur(cur, &rptr);
2649
2650 /* Allocate the new block. If we can't do it, we're toast. Give up. */
2651 error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
2652 if (error)
2653 goto error0;
2654 if (*stat == 0)
2655 goto out0;
2656 XFS_BTREE_STATS_INC(cur, alloc);
2657
2658 /* Set up the new block. */
2659 error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
2660 if (error)
2661 goto error0;
2662
2663 /* Set the root in the holding structure increasing the level by 1. */
2664 cur->bc_ops->set_root(cur, &lptr, 1);
2665
2666 /*
2667 * At the previous root level there are now two blocks: the old root,
2668 * and the new block generated when it was split. We don't know which
2669 * one the cursor is pointing at, so we set up variables "left" and
2670 * "right" for each case.
2671 */
2672 block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
2673
2674#ifdef DEBUG
2675 error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
2676 if (error)
2677 goto error0;
2678#endif
2679
2680 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
2681 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
2682 /* Our block is left, pick up the right block. */
2683 lbp = bp;
2684 xfs_btree_buf_to_ptr(cur, lbp, &lptr);
2685 left = block;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002686 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002687 if (error)
2688 goto error0;
2689 bp = rbp;
2690 nptr = 1;
2691 } else {
2692 /* Our block is right, pick up the left block. */
2693 rbp = bp;
2694 xfs_btree_buf_to_ptr(cur, rbp, &rptr);
2695 right = block;
2696 xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
Eric Sandeen0d7409b2014-04-14 18:59:56 +10002697 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002698 if (error)
2699 goto error0;
2700 bp = lbp;
2701 nptr = 2;
2702 }
2703 /* Fill in the new block's btree header and log it. */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002704 xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
Christoph Hellwig344207c2008-10-30 16:57:16 +11002705 xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
2706 ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
2707 !xfs_btree_ptr_is_null(cur, &rptr));
2708
2709 /* Fill in the key data in the new root. */
2710 if (xfs_btree_get_level(left) > 0) {
2711 xfs_btree_copy_keys(cur,
2712 xfs_btree_key_addr(cur, 1, new),
2713 xfs_btree_key_addr(cur, 1, left), 1);
2714 xfs_btree_copy_keys(cur,
2715 xfs_btree_key_addr(cur, 2, new),
2716 xfs_btree_key_addr(cur, 1, right), 1);
2717 } else {
2718 cur->bc_ops->init_key_from_rec(
2719 xfs_btree_key_addr(cur, 1, new),
2720 xfs_btree_rec_addr(cur, 1, left));
2721 cur->bc_ops->init_key_from_rec(
2722 xfs_btree_key_addr(cur, 2, new),
2723 xfs_btree_rec_addr(cur, 1, right));
2724 }
2725 xfs_btree_log_keys(cur, nbp, 1, 2);
2726
2727 /* Fill in the pointer data in the new root. */
2728 xfs_btree_copy_ptrs(cur,
2729 xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
2730 xfs_btree_copy_ptrs(cur,
2731 xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
2732 xfs_btree_log_ptrs(cur, nbp, 1, 2);
2733
2734 /* Fix up the cursor. */
2735 xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
2736 cur->bc_ptrs[cur->bc_nlevels] = nptr;
2737 cur->bc_nlevels++;
2738 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2739 *stat = 1;
2740 return 0;
2741error0:
2742 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
2743 return error;
2744out0:
2745 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2746 *stat = 0;
2747 return 0;
2748}
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002749
2750STATIC int
2751xfs_btree_make_block_unfull(
2752 struct xfs_btree_cur *cur, /* btree cursor */
2753 int level, /* btree level */
2754 int numrecs,/* # of recs in block */
2755 int *oindex,/* old tree index */
2756 int *index, /* new tree index */
2757 union xfs_btree_ptr *nptr, /* new btree ptr */
2758 struct xfs_btree_cur **ncur, /* new btree cursor */
2759 union xfs_btree_rec *nrec, /* new record */
2760 int *stat)
2761{
2762 union xfs_btree_key key; /* new btree key value */
2763 int error = 0;
2764
2765 if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2766 level == cur->bc_nlevels - 1) {
2767 struct xfs_inode *ip = cur->bc_private.b.ip;
2768
2769 if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
2770 /* A root block that can be made bigger. */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002771 xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
2772 } else {
2773 /* A root block that needs replacing */
2774 int logflags = 0;
2775
2776 error = xfs_btree_new_iroot(cur, &logflags, stat);
2777 if (error || *stat == 0)
2778 return error;
2779
2780 xfs_trans_log_inode(cur->bc_tp, ip, logflags);
2781 }
2782
2783 return 0;
2784 }
2785
2786 /* First, try shifting an entry to the right neighbor. */
2787 error = xfs_btree_rshift(cur, level, stat);
2788 if (error || *stat)
2789 return error;
2790
2791 /* Next, try shifting an entry to the left neighbor. */
2792 error = xfs_btree_lshift(cur, level, stat);
2793 if (error)
2794 return error;
2795
2796 if (*stat) {
2797 *oindex = *index = cur->bc_ptrs[level];
2798 return 0;
2799 }
2800
2801 /*
2802 * Next, try splitting the current block in half.
2803 *
2804 * If this works we have to re-set our variables because we
2805 * could be in a different block now.
2806 */
2807 error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
2808 if (error || *stat == 0)
2809 return error;
2810
2811
2812 *index = cur->bc_ptrs[level];
2813 cur->bc_ops->init_rec_from_key(&key, nrec);
2814 return 0;
2815}
2816
2817/*
2818 * Insert one record/level. Return information to the caller
2819 * allowing the next level up to proceed if necessary.
2820 */
2821STATIC int
2822xfs_btree_insrec(
2823 struct xfs_btree_cur *cur, /* btree cursor */
2824 int level, /* level to insert record at */
2825 union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
2826 union xfs_btree_rec *recp, /* i/o: record data inserted */
2827 struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
2828 int *stat) /* success/failure */
2829{
2830 struct xfs_btree_block *block; /* btree block */
2831 struct xfs_buf *bp; /* buffer for block */
2832 union xfs_btree_key key; /* btree key */
2833 union xfs_btree_ptr nptr; /* new block ptr */
2834 struct xfs_btree_cur *ncur; /* new btree cursor */
2835 union xfs_btree_rec nrec; /* new record count */
2836 int optr; /* old key/record index */
2837 int ptr; /* key/record index */
2838 int numrecs;/* number of records */
2839 int error; /* error return value */
2840#ifdef DEBUG
2841 int i;
2842#endif
2843
2844 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
2845 XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
2846
2847 ncur = NULL;
2848
2849 /*
2850 * If we have an external root pointer, and we've made it to the
2851 * root level, allocate a new root block and we're done.
2852 */
2853 if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
2854 (level >= cur->bc_nlevels)) {
2855 error = xfs_btree_new_root(cur, stat);
2856 xfs_btree_set_ptr_null(cur, ptrp);
2857
2858 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2859 return error;
2860 }
2861
2862 /* If we're off the left edge, return failure. */
2863 ptr = cur->bc_ptrs[level];
2864 if (ptr == 0) {
2865 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
2866 *stat = 0;
2867 return 0;
2868 }
2869
2870 /* Make a key out of the record data to be inserted, and save it. */
2871 cur->bc_ops->init_key_from_rec(&key, recp);
2872
2873 optr = ptr;
2874
2875 XFS_BTREE_STATS_INC(cur, insrec);
2876
2877 /* Get pointers to the btree buffer and block. */
2878 block = xfs_btree_get_block(cur, level, &bp);
2879 numrecs = xfs_btree_get_numrecs(block);
2880
2881#ifdef DEBUG
2882 error = xfs_btree_check_block(cur, block, level, bp);
2883 if (error)
2884 goto error0;
2885
2886 /* Check that the new entry is being inserted in the right place. */
2887 if (ptr <= numrecs) {
2888 if (level == 0) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002889 ASSERT(cur->bc_ops->recs_inorder(cur, recp,
2890 xfs_btree_rec_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002891 } else {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002892 ASSERT(cur->bc_ops->keys_inorder(cur, &key,
2893 xfs_btree_key_addr(cur, ptr, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002894 }
2895 }
2896#endif
2897
2898 /*
2899 * If the block is full, we can't insert the new entry until we
2900 * make the block un-full.
2901 */
2902 xfs_btree_set_ptr_null(cur, &nptr);
2903 if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
2904 error = xfs_btree_make_block_unfull(cur, level, numrecs,
2905 &optr, &ptr, &nptr, &ncur, &nrec, stat);
2906 if (error || *stat == 0)
2907 goto error0;
2908 }
2909
2910 /*
2911 * The current block may have changed if the block was
2912 * previously full and we have just made space in it.
2913 */
2914 block = xfs_btree_get_block(cur, level, &bp);
2915 numrecs = xfs_btree_get_numrecs(block);
2916
2917#ifdef DEBUG
2918 error = xfs_btree_check_block(cur, block, level, bp);
2919 if (error)
2920 return error;
2921#endif
2922
2923 /*
2924 * At this point we know there's room for our new entry in the block
2925 * we're pointing at.
2926 */
2927 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
2928
2929 if (level > 0) {
2930 /* It's a nonleaf. make a hole in the keys and ptrs */
2931 union xfs_btree_key *kp;
2932 union xfs_btree_ptr *pp;
2933
2934 kp = xfs_btree_key_addr(cur, ptr, block);
2935 pp = xfs_btree_ptr_addr(cur, ptr, block);
2936
2937#ifdef DEBUG
2938 for (i = numrecs - ptr; i >= 0; i--) {
2939 error = xfs_btree_check_ptr(cur, pp, i, level);
2940 if (error)
2941 return error;
2942 }
2943#endif
2944
2945 xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
2946 xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
2947
2948#ifdef DEBUG
2949 error = xfs_btree_check_ptr(cur, ptrp, 0, level);
2950 if (error)
2951 goto error0;
2952#endif
2953
2954 /* Now put the new data in, bump numrecs and log it. */
2955 xfs_btree_copy_keys(cur, kp, &key, 1);
2956 xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
2957 numrecs++;
2958 xfs_btree_set_numrecs(block, numrecs);
2959 xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
2960 xfs_btree_log_keys(cur, bp, ptr, numrecs);
2961#ifdef DEBUG
2962 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002963 ASSERT(cur->bc_ops->keys_inorder(cur, kp,
2964 xfs_btree_key_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002965 }
2966#endif
2967 } else {
2968 /* It's a leaf. make a hole in the records */
2969 union xfs_btree_rec *rp;
2970
2971 rp = xfs_btree_rec_addr(cur, ptr, block);
2972
2973 xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
2974
2975 /* Now put the new data in, bump numrecs and log it. */
2976 xfs_btree_copy_recs(cur, rp, recp, 1);
2977 xfs_btree_set_numrecs(block, ++numrecs);
2978 xfs_btree_log_recs(cur, bp, ptr, numrecs);
2979#ifdef DEBUG
2980 if (ptr < numrecs) {
Christoph Hellwig4a26e662008-10-30 16:58:32 +11002981 ASSERT(cur->bc_ops->recs_inorder(cur, rp,
2982 xfs_btree_rec_addr(cur, ptr + 1, block)));
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002983 }
2984#endif
2985 }
2986
2987 /* Log the new number of records in the btree header. */
2988 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
2989
2990 /* If we inserted at the start of a block, update the parents' keys. */
2991 if (optr == 1) {
2992 error = xfs_btree_updkey(cur, &key, level + 1);
2993 if (error)
2994 goto error0;
2995 }
2996
2997 /*
2998 * If we are tracking the last record in the tree and
2999 * we are at the far right edge of the tree, update it.
3000 */
3001 if (xfs_btree_is_lastrec(cur, block, level)) {
3002 cur->bc_ops->update_lastrec(cur, block, recp,
3003 ptr, LASTREC_INSREC);
3004 }
3005
3006 /*
3007 * Return the new block number, if any.
3008 * If there is one, give back a record value and a cursor too.
3009 */
3010 *ptrp = nptr;
3011 if (!xfs_btree_ptr_is_null(cur, &nptr)) {
3012 *recp = nrec;
3013 *curp = ncur;
3014 }
3015
3016 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3017 *stat = 1;
3018 return 0;
3019
3020error0:
3021 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3022 return error;
3023}
3024
3025/*
3026 * Insert the record at the point referenced by cur.
3027 *
3028 * A multi-level split of the tree on insert will invalidate the original
3029 * cursor. All callers of this function should assume that the cursor is
3030 * no longer valid and revalidate it.
3031 */
3032int
3033xfs_btree_insert(
3034 struct xfs_btree_cur *cur,
3035 int *stat)
3036{
3037 int error; /* error return value */
3038 int i; /* result value, 0 for failure */
3039 int level; /* current level number in btree */
3040 union xfs_btree_ptr nptr; /* new block number (split result) */
3041 struct xfs_btree_cur *ncur; /* new cursor (split result) */
3042 struct xfs_btree_cur *pcur; /* previous level's cursor */
3043 union xfs_btree_rec rec; /* record to insert */
3044
3045 level = 0;
3046 ncur = NULL;
3047 pcur = cur;
3048
3049 xfs_btree_set_ptr_null(cur, &nptr);
3050 cur->bc_ops->init_rec_from_cur(cur, &rec);
3051
3052 /*
3053 * Loop going up the tree, starting at the leaf level.
3054 * Stop when we don't get a split block, that must mean that
3055 * the insert is finished with this level.
3056 */
3057 do {
3058 /*
3059 * Insert nrec/nptr into this level of the tree.
3060 * Note if we fail, nptr will be null.
3061 */
3062 error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
3063 if (error) {
3064 if (pcur != cur)
3065 xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
3066 goto error0;
3067 }
3068
3069 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3070 level++;
3071
3072 /*
3073 * See if the cursor we just used is trash.
3074 * Can't trash the caller's cursor, but otherwise we should
3075 * if ncur is a new cursor or we're about to be done.
3076 */
3077 if (pcur != cur &&
3078 (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
3079 /* Save the state from the cursor before we trash it */
3080 if (cur->bc_ops->update_cursor)
3081 cur->bc_ops->update_cursor(pcur, cur);
3082 cur->bc_nlevels = pcur->bc_nlevels;
3083 xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
3084 }
3085 /* If we got a new cursor, switch to it. */
3086 if (ncur) {
3087 pcur = ncur;
3088 ncur = NULL;
3089 }
3090 } while (!xfs_btree_ptr_is_null(cur, &nptr));
3091
3092 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3093 *stat = i;
3094 return 0;
3095error0:
3096 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3097 return error;
3098}
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003099
3100/*
3101 * Try to merge a non-leaf block back into the inode root.
3102 *
3103 * Note: the killroot names comes from the fact that we're effectively
3104 * killing the old root block. But because we can't just delete the
3105 * inode we have to copy the single block it was pointing to into the
3106 * inode.
3107 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003108STATIC int
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003109xfs_btree_kill_iroot(
3110 struct xfs_btree_cur *cur)
3111{
3112 int whichfork = cur->bc_private.b.whichfork;
3113 struct xfs_inode *ip = cur->bc_private.b.ip;
3114 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3115 struct xfs_btree_block *block;
3116 struct xfs_btree_block *cblock;
3117 union xfs_btree_key *kp;
3118 union xfs_btree_key *ckp;
3119 union xfs_btree_ptr *pp;
3120 union xfs_btree_ptr *cpp;
3121 struct xfs_buf *cbp;
3122 int level;
3123 int index;
3124 int numrecs;
3125#ifdef DEBUG
3126 union xfs_btree_ptr ptr;
3127 int i;
3128#endif
3129
3130 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3131
3132 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3133 ASSERT(cur->bc_nlevels > 1);
3134
3135 /*
3136 * Don't deal with the root block needs to be a leaf case.
3137 * We're just going to turn the thing back into extents anyway.
3138 */
3139 level = cur->bc_nlevels - 1;
3140 if (level == 1)
3141 goto out0;
3142
3143 /*
3144 * Give up if the root has multiple children.
3145 */
3146 block = xfs_btree_get_iroot(cur);
3147 if (xfs_btree_get_numrecs(block) != 1)
3148 goto out0;
3149
3150 cblock = xfs_btree_get_block(cur, level - 1, &cbp);
3151 numrecs = xfs_btree_get_numrecs(cblock);
3152
3153 /*
3154 * Only do this if the next level will fit.
3155 * Then the data must be copied up to the inode,
3156 * instead of freeing the root you free the next level.
3157 */
3158 if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
3159 goto out0;
3160
3161 XFS_BTREE_STATS_INC(cur, killroot);
3162
3163#ifdef DEBUG
3164 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
3165 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3166 xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
3167 ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
3168#endif
3169
3170 index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
3171 if (index) {
3172 xfs_iroot_realloc(cur->bc_private.b.ip, index,
3173 cur->bc_private.b.whichfork);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003174 block = ifp->if_broot;
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003175 }
3176
3177 be16_add_cpu(&block->bb_numrecs, index);
3178 ASSERT(block->bb_numrecs == cblock->bb_numrecs);
3179
3180 kp = xfs_btree_key_addr(cur, 1, block);
3181 ckp = xfs_btree_key_addr(cur, 1, cblock);
3182 xfs_btree_copy_keys(cur, kp, ckp, numrecs);
3183
3184 pp = xfs_btree_ptr_addr(cur, 1, block);
3185 cpp = xfs_btree_ptr_addr(cur, 1, cblock);
3186#ifdef DEBUG
3187 for (i = 0; i < numrecs; i++) {
3188 int error;
3189
3190 error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
3191 if (error) {
3192 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3193 return error;
3194 }
3195 }
3196#endif
3197 xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
3198
3199 cur->bc_ops->free_block(cur, cbp);
3200 XFS_BTREE_STATS_INC(cur, free);
3201
3202 cur->bc_bufs[level - 1] = NULL;
3203 be16_add_cpu(&block->bb_level, -1);
3204 xfs_trans_log_inode(cur->bc_tp, ip,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003205 XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
Christoph Hellwigd4b3a4b2008-10-30 16:57:51 +11003206 cur->bc_nlevels--;
3207out0:
3208 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3209 return 0;
3210}
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003211
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003212/*
3213 * Kill the current root node, and replace it with it's only child node.
3214 */
3215STATIC int
3216xfs_btree_kill_root(
3217 struct xfs_btree_cur *cur,
3218 struct xfs_buf *bp,
3219 int level,
3220 union xfs_btree_ptr *newroot)
3221{
3222 int error;
3223
3224 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3225 XFS_BTREE_STATS_INC(cur, killroot);
3226
3227 /*
3228 * Update the root pointer, decreasing the level by 1 and then
3229 * free the old root.
3230 */
3231 cur->bc_ops->set_root(cur, newroot, -1);
3232
3233 error = cur->bc_ops->free_block(cur, bp);
3234 if (error) {
3235 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3236 return error;
3237 }
3238
3239 XFS_BTREE_STATS_INC(cur, free);
3240
3241 cur->bc_bufs[level] = NULL;
3242 cur->bc_ra[level] = 0;
3243 cur->bc_nlevels--;
3244
3245 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3246 return 0;
3247}
3248
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003249STATIC int
3250xfs_btree_dec_cursor(
3251 struct xfs_btree_cur *cur,
3252 int level,
3253 int *stat)
3254{
3255 int error;
3256 int i;
3257
3258 if (level > 0) {
3259 error = xfs_btree_decrement(cur, level, &i);
3260 if (error)
3261 return error;
3262 }
3263
3264 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3265 *stat = 1;
3266 return 0;
3267}
3268
3269/*
3270 * Single level of the btree record deletion routine.
3271 * Delete record pointed to by cur/level.
3272 * Remove the record from its block then rebalance the tree.
3273 * Return 0 for error, 1 for done, 2 to go on to the next level.
3274 */
3275STATIC int /* error */
3276xfs_btree_delrec(
3277 struct xfs_btree_cur *cur, /* btree cursor */
3278 int level, /* level removing record from */
3279 int *stat) /* fail/done/go-on */
3280{
3281 struct xfs_btree_block *block; /* btree block */
3282 union xfs_btree_ptr cptr; /* current block ptr */
3283 struct xfs_buf *bp; /* buffer for block */
3284 int error; /* error return value */
3285 int i; /* loop counter */
3286 union xfs_btree_key key; /* storage for keyp */
3287 union xfs_btree_key *keyp = &key; /* passed to the next level */
3288 union xfs_btree_ptr lptr; /* left sibling block ptr */
3289 struct xfs_buf *lbp; /* left buffer pointer */
3290 struct xfs_btree_block *left; /* left btree block */
3291 int lrecs = 0; /* left record count */
3292 int ptr; /* key/record index */
3293 union xfs_btree_ptr rptr; /* right sibling block ptr */
3294 struct xfs_buf *rbp; /* right buffer pointer */
3295 struct xfs_btree_block *right; /* right btree block */
3296 struct xfs_btree_block *rrblock; /* right-right btree block */
3297 struct xfs_buf *rrbp; /* right-right buffer pointer */
3298 int rrecs = 0; /* right record count */
3299 struct xfs_btree_cur *tcur; /* temporary btree cursor */
3300 int numrecs; /* temporary numrec count */
3301
3302 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3303 XFS_BTREE_TRACE_ARGI(cur, level);
3304
3305 tcur = NULL;
3306
3307 /* Get the index of the entry being deleted, check for nothing there. */
3308 ptr = cur->bc_ptrs[level];
3309 if (ptr == 0) {
3310 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3311 *stat = 0;
3312 return 0;
3313 }
3314
3315 /* Get the buffer & block containing the record or key/ptr. */
3316 block = xfs_btree_get_block(cur, level, &bp);
3317 numrecs = xfs_btree_get_numrecs(block);
3318
3319#ifdef DEBUG
3320 error = xfs_btree_check_block(cur, block, level, bp);
3321 if (error)
3322 goto error0;
3323#endif
3324
3325 /* Fail if we're off the end of the block. */
3326 if (ptr > numrecs) {
3327 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3328 *stat = 0;
3329 return 0;
3330 }
3331
3332 XFS_BTREE_STATS_INC(cur, delrec);
3333 XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
3334
3335 /* Excise the entries being deleted. */
3336 if (level > 0) {
3337 /* It's a nonleaf. operate on keys and ptrs */
3338 union xfs_btree_key *lkp;
3339 union xfs_btree_ptr *lpp;
3340
3341 lkp = xfs_btree_key_addr(cur, ptr + 1, block);
3342 lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
3343
3344#ifdef DEBUG
3345 for (i = 0; i < numrecs - ptr; i++) {
3346 error = xfs_btree_check_ptr(cur, lpp, i, level);
3347 if (error)
3348 goto error0;
3349 }
3350#endif
3351
3352 if (ptr < numrecs) {
3353 xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
3354 xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
3355 xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
3356 xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
3357 }
3358
3359 /*
3360 * If it's the first record in the block, we'll need to pass a
3361 * key up to the next level (updkey).
3362 */
3363 if (ptr == 1)
3364 keyp = xfs_btree_key_addr(cur, 1, block);
3365 } else {
3366 /* It's a leaf. operate on records */
3367 if (ptr < numrecs) {
3368 xfs_btree_shift_recs(cur,
3369 xfs_btree_rec_addr(cur, ptr + 1, block),
3370 -1, numrecs - ptr);
3371 xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
3372 }
3373
3374 /*
3375 * If it's the first record in the block, we'll need a key
3376 * structure to pass up to the next level (updkey).
3377 */
3378 if (ptr == 1) {
3379 cur->bc_ops->init_key_from_rec(&key,
3380 xfs_btree_rec_addr(cur, 1, block));
3381 keyp = &key;
3382 }
3383 }
3384
3385 /*
3386 * Decrement and log the number of entries in the block.
3387 */
3388 xfs_btree_set_numrecs(block, --numrecs);
3389 xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
3390
3391 /*
3392 * If we are tracking the last record in the tree and
3393 * we are at the far right edge of the tree, update it.
3394 */
3395 if (xfs_btree_is_lastrec(cur, block, level)) {
3396 cur->bc_ops->update_lastrec(cur, block, NULL,
3397 ptr, LASTREC_DELREC);
3398 }
3399
3400 /*
3401 * We're at the root level. First, shrink the root block in-memory.
3402 * Try to get rid of the next level down. If we can't then there's
3403 * nothing left to do.
3404 */
3405 if (level == cur->bc_nlevels - 1) {
3406 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3407 xfs_iroot_realloc(cur->bc_private.b.ip, -1,
3408 cur->bc_private.b.whichfork);
3409
3410 error = xfs_btree_kill_iroot(cur);
3411 if (error)
3412 goto error0;
3413
3414 error = xfs_btree_dec_cursor(cur, level, stat);
3415 if (error)
3416 goto error0;
3417 *stat = 1;
3418 return 0;
3419 }
3420
3421 /*
3422 * If this is the root level, and there's only one entry left,
3423 * and it's NOT the leaf level, then we can get rid of this
3424 * level.
3425 */
3426 if (numrecs == 1 && level > 0) {
3427 union xfs_btree_ptr *pp;
3428 /*
3429 * pp is still set to the first pointer in the block.
3430 * Make it the new root of the btree.
3431 */
3432 pp = xfs_btree_ptr_addr(cur, 1, block);
Christoph Hellwigc0e59e12010-09-07 23:34:07 +00003433 error = xfs_btree_kill_root(cur, bp, level, pp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003434 if (error)
3435 goto error0;
3436 } else if (level > 0) {
3437 error = xfs_btree_dec_cursor(cur, level, stat);
3438 if (error)
3439 goto error0;
3440 }
3441 *stat = 1;
3442 return 0;
3443 }
3444
3445 /*
3446 * If we deleted the leftmost entry in the block, update the
3447 * key values above us in the tree.
3448 */
3449 if (ptr == 1) {
3450 error = xfs_btree_updkey(cur, keyp, level + 1);
3451 if (error)
3452 goto error0;
3453 }
3454
3455 /*
3456 * If the number of records remaining in the block is at least
3457 * the minimum, we're done.
3458 */
3459 if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
3460 error = xfs_btree_dec_cursor(cur, level, stat);
3461 if (error)
3462 goto error0;
3463 return 0;
3464 }
3465
3466 /*
3467 * Otherwise, we have to move some records around to keep the
3468 * tree balanced. Look at the left and right sibling blocks to
3469 * see if we can re-balance by moving only one record.
3470 */
3471 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3472 xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
3473
3474 if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
3475 /*
3476 * One child of root, need to get a chance to copy its contents
3477 * into the root and delete it. Can't go up to next level,
3478 * there's nothing to delete there.
3479 */
3480 if (xfs_btree_ptr_is_null(cur, &rptr) &&
3481 xfs_btree_ptr_is_null(cur, &lptr) &&
3482 level == cur->bc_nlevels - 2) {
3483 error = xfs_btree_kill_iroot(cur);
3484 if (!error)
3485 error = xfs_btree_dec_cursor(cur, level, stat);
3486 if (error)
3487 goto error0;
3488 return 0;
3489 }
3490 }
3491
3492 ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
3493 !xfs_btree_ptr_is_null(cur, &lptr));
3494
3495 /*
3496 * Duplicate the cursor so our btree manipulations here won't
3497 * disrupt the next level up.
3498 */
3499 error = xfs_btree_dup_cursor(cur, &tcur);
3500 if (error)
3501 goto error0;
3502
3503 /*
3504 * If there's a right sibling, see if it's ok to shift an entry
3505 * out of it.
3506 */
3507 if (!xfs_btree_ptr_is_null(cur, &rptr)) {
3508 /*
3509 * Move the temp cursor to the last entry in the next block.
3510 * Actually any entry but the first would suffice.
3511 */
3512 i = xfs_btree_lastrec(tcur, level);
3513 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3514
3515 error = xfs_btree_increment(tcur, level, &i);
3516 if (error)
3517 goto error0;
3518 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3519
3520 i = xfs_btree_lastrec(tcur, level);
3521 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3522
3523 /* Grab a pointer to the block. */
3524 right = xfs_btree_get_block(tcur, level, &rbp);
3525#ifdef DEBUG
3526 error = xfs_btree_check_block(tcur, right, level, rbp);
3527 if (error)
3528 goto error0;
3529#endif
3530 /* Grab the current block number, for future use. */
3531 xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
3532
3533 /*
3534 * If right block is full enough so that removing one entry
3535 * won't make it too empty, and left-shifting an entry out
3536 * of right to us works, we're done.
3537 */
3538 if (xfs_btree_get_numrecs(right) - 1 >=
3539 cur->bc_ops->get_minrecs(tcur, level)) {
3540 error = xfs_btree_lshift(tcur, level, &i);
3541 if (error)
3542 goto error0;
3543 if (i) {
3544 ASSERT(xfs_btree_get_numrecs(block) >=
3545 cur->bc_ops->get_minrecs(tcur, level));
3546
3547 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3548 tcur = NULL;
3549
3550 error = xfs_btree_dec_cursor(cur, level, stat);
3551 if (error)
3552 goto error0;
3553 return 0;
3554 }
3555 }
3556
3557 /*
3558 * Otherwise, grab the number of records in right for
3559 * future reference, and fix up the temp cursor to point
3560 * to our block again (last record).
3561 */
3562 rrecs = xfs_btree_get_numrecs(right);
3563 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3564 i = xfs_btree_firstrec(tcur, level);
3565 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3566
3567 error = xfs_btree_decrement(tcur, level, &i);
3568 if (error)
3569 goto error0;
3570 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3571 }
3572 }
3573
3574 /*
3575 * If there's a left sibling, see if it's ok to shift an entry
3576 * out of it.
3577 */
3578 if (!xfs_btree_ptr_is_null(cur, &lptr)) {
3579 /*
3580 * Move the temp cursor to the first entry in the
3581 * previous block.
3582 */
3583 i = xfs_btree_firstrec(tcur, level);
3584 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3585
3586 error = xfs_btree_decrement(tcur, level, &i);
3587 if (error)
3588 goto error0;
3589 i = xfs_btree_firstrec(tcur, level);
3590 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
3591
3592 /* Grab a pointer to the block. */
3593 left = xfs_btree_get_block(tcur, level, &lbp);
3594#ifdef DEBUG
3595 error = xfs_btree_check_block(cur, left, level, lbp);
3596 if (error)
3597 goto error0;
3598#endif
3599 /* Grab the current block number, for future use. */
3600 xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
3601
3602 /*
3603 * If left block is full enough so that removing one entry
3604 * won't make it too empty, and right-shifting an entry out
3605 * of left to us works, we're done.
3606 */
3607 if (xfs_btree_get_numrecs(left) - 1 >=
3608 cur->bc_ops->get_minrecs(tcur, level)) {
3609 error = xfs_btree_rshift(tcur, level, &i);
3610 if (error)
3611 goto error0;
3612 if (i) {
3613 ASSERT(xfs_btree_get_numrecs(block) >=
3614 cur->bc_ops->get_minrecs(tcur, level));
3615 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3616 tcur = NULL;
3617 if (level == 0)
3618 cur->bc_ptrs[0]++;
3619 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3620 *stat = 1;
3621 return 0;
3622 }
3623 }
3624
3625 /*
3626 * Otherwise, grab the number of records in right for
3627 * future reference.
3628 */
3629 lrecs = xfs_btree_get_numrecs(left);
3630 }
3631
3632 /* Delete the temp cursor, we're done with it. */
3633 xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
3634 tcur = NULL;
3635
3636 /* If here, we need to do a join to keep the tree balanced. */
3637 ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
3638
3639 if (!xfs_btree_ptr_is_null(cur, &lptr) &&
3640 lrecs + xfs_btree_get_numrecs(block) <=
3641 cur->bc_ops->get_maxrecs(cur, level)) {
3642 /*
3643 * Set "right" to be the starting block,
3644 * "left" to be the left neighbor.
3645 */
3646 rptr = cptr;
3647 right = block;
3648 rbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003649 error = xfs_btree_read_buf_block(cur, &lptr, 0, &left, &lbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003650 if (error)
3651 goto error0;
3652
3653 /*
3654 * If that won't work, see if we can join with the right neighbor block.
3655 */
3656 } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
3657 rrecs + xfs_btree_get_numrecs(block) <=
3658 cur->bc_ops->get_maxrecs(cur, level)) {
3659 /*
3660 * Set "left" to be the starting block,
3661 * "right" to be the right neighbor.
3662 */
3663 lptr = cptr;
3664 left = block;
3665 lbp = bp;
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003666 error = xfs_btree_read_buf_block(cur, &rptr, 0, &right, &rbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003667 if (error)
3668 goto error0;
3669
3670 /*
3671 * Otherwise, we can't fix the imbalance.
3672 * Just return. This is probably a logic error, but it's not fatal.
3673 */
3674 } else {
3675 error = xfs_btree_dec_cursor(cur, level, stat);
3676 if (error)
3677 goto error0;
3678 return 0;
3679 }
3680
3681 rrecs = xfs_btree_get_numrecs(right);
3682 lrecs = xfs_btree_get_numrecs(left);
3683
3684 /*
3685 * We're now going to join "left" and "right" by moving all the stuff
3686 * in "right" to "left" and deleting "right".
3687 */
3688 XFS_BTREE_STATS_ADD(cur, moves, rrecs);
3689 if (level > 0) {
3690 /* It's a non-leaf. Move keys and pointers. */
3691 union xfs_btree_key *lkp; /* left btree key */
3692 union xfs_btree_ptr *lpp; /* left address pointer */
3693 union xfs_btree_key *rkp; /* right btree key */
3694 union xfs_btree_ptr *rpp; /* right address pointer */
3695
3696 lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
3697 lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
3698 rkp = xfs_btree_key_addr(cur, 1, right);
3699 rpp = xfs_btree_ptr_addr(cur, 1, right);
3700#ifdef DEBUG
3701 for (i = 1; i < rrecs; i++) {
3702 error = xfs_btree_check_ptr(cur, rpp, i, level);
3703 if (error)
3704 goto error0;
3705 }
3706#endif
3707 xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
3708 xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
3709
3710 xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
3711 xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
3712 } else {
3713 /* It's a leaf. Move records. */
3714 union xfs_btree_rec *lrp; /* left record pointer */
3715 union xfs_btree_rec *rrp; /* right record pointer */
3716
3717 lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
3718 rrp = xfs_btree_rec_addr(cur, 1, right);
3719
3720 xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
3721 xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
3722 }
3723
3724 XFS_BTREE_STATS_INC(cur, join);
3725
3726 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003727 * Fix up the number of records and right block pointer in the
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003728 * surviving block, and log it.
3729 */
3730 xfs_btree_set_numrecs(left, lrecs + rrecs);
3731 xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
3732 xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3733 xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
3734
3735 /* If there is a right sibling, point it to the remaining block. */
3736 xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
3737 if (!xfs_btree_ptr_is_null(cur, &cptr)) {
Eric Sandeen0d7409b2014-04-14 18:59:56 +10003738 error = xfs_btree_read_buf_block(cur, &cptr, 0, &rrblock, &rrbp);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11003739 if (error)
3740 goto error0;
3741 xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
3742 xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
3743 }
3744
3745 /* Free the deleted block. */
3746 error = cur->bc_ops->free_block(cur, rbp);
3747 if (error)
3748 goto error0;
3749 XFS_BTREE_STATS_INC(cur, free);
3750
3751 /*
3752 * If we joined with the left neighbor, set the buffer in the
3753 * cursor to the left block, and fix up the index.
3754 */
3755 if (bp != lbp) {
3756 cur->bc_bufs[level] = lbp;
3757 cur->bc_ptrs[level] += lrecs;
3758 cur->bc_ra[level] = 0;
3759 }
3760 /*
3761 * If we joined with the right neighbor and there's a level above
3762 * us, increment the cursor at that level.
3763 */
3764 else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
3765 (level + 1 < cur->bc_nlevels)) {
3766 error = xfs_btree_increment(cur, level + 1, &i);
3767 if (error)
3768 goto error0;
3769 }
3770
3771 /*
3772 * Readjust the ptr at this level if it's not a leaf, since it's
3773 * still pointing at the deletion point, which makes the cursor
3774 * inconsistent. If this makes the ptr 0, the caller fixes it up.
3775 * We can't use decrement because it would change the next level up.
3776 */
3777 if (level > 0)
3778 cur->bc_ptrs[level]--;
3779
3780 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3781 /* Return value means the next level up has something to do. */
3782 *stat = 2;
3783 return 0;
3784
3785error0:
3786 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3787 if (tcur)
3788 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
3789 return error;
3790}
3791
3792/*
3793 * Delete the record pointed to by cur.
3794 * The cursor refers to the place where the record was (could be inserted)
3795 * when the operation returns.
3796 */
3797int /* error */
3798xfs_btree_delete(
3799 struct xfs_btree_cur *cur,
3800 int *stat) /* success/failure */
3801{
3802 int error; /* error return value */
3803 int level;
3804 int i;
3805
3806 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
3807
3808 /*
3809 * Go up the tree, starting at leaf level.
3810 *
3811 * If 2 is returned then a join was done; go to the next level.
3812 * Otherwise we are done.
3813 */
3814 for (level = 0, i = 2; i == 2; level++) {
3815 error = xfs_btree_delrec(cur, level, &i);
3816 if (error)
3817 goto error0;
3818 }
3819
3820 if (i == 0) {
3821 for (level = 1; level < cur->bc_nlevels; level++) {
3822 if (cur->bc_ptrs[level] == 0) {
3823 error = xfs_btree_decrement(cur, level, &i);
3824 if (error)
3825 goto error0;
3826 break;
3827 }
3828 }
3829 }
3830
3831 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
3832 *stat = i;
3833 return 0;
3834error0:
3835 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
3836 return error;
3837}
Christoph Hellwig8cc938f2008-10-30 16:58:11 +11003838
3839/*
3840 * Get the data from the pointed-to record.
3841 */
3842int /* error */
3843xfs_btree_get_rec(
3844 struct xfs_btree_cur *cur, /* btree cursor */
3845 union xfs_btree_rec **recp, /* output: btree record */
3846 int *stat) /* output: success/failure */
3847{
3848 struct xfs_btree_block *block; /* btree block */
3849 struct xfs_buf *bp; /* buffer pointer */
3850 int ptr; /* record number */
3851#ifdef DEBUG
3852 int error; /* error return value */
3853#endif
3854
3855 ptr = cur->bc_ptrs[0];
3856 block = xfs_btree_get_block(cur, 0, &bp);
3857
3858#ifdef DEBUG
3859 error = xfs_btree_check_block(cur, block, 0, bp);
3860 if (error)
3861 return error;
3862#endif
3863
3864 /*
3865 * Off the right end or left end, return failure.
3866 */
3867 if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
3868 *stat = 0;
3869 return 0;
3870 }
3871
3872 /*
3873 * Point to the record and extract its data.
3874 */
3875 *recp = xfs_btree_rec_addr(cur, ptr, block);
3876 *stat = 1;
3877 return 0;
3878}
Dave Chinner21b5c972013-08-30 10:23:44 +10003879
3880/*
3881 * Change the owner of a btree.
3882 *
3883 * The mechanism we use here is ordered buffer logging. Because we don't know
3884 * how many buffers were are going to need to modify, we don't really want to
3885 * have to make transaction reservations for the worst case of every buffer in a
3886 * full size btree as that may be more space that we can fit in the log....
3887 *
3888 * We do the btree walk in the most optimal manner possible - we have sibling
3889 * pointers so we can just walk all the blocks on each level from left to right
3890 * in a single pass, and then move to the next level and do the same. We can
3891 * also do readahead on the sibling pointers to get IO moving more quickly,
3892 * though for slow disks this is unlikely to make much difference to performance
3893 * as the amount of CPU work we have to do before moving to the next block is
3894 * relatively small.
3895 *
3896 * For each btree block that we load, modify the owner appropriately, set the
3897 * buffer as an ordered buffer and log it appropriately. We need to ensure that
3898 * we mark the region we change dirty so that if the buffer is relogged in
3899 * a subsequent transaction the changes we make here as an ordered buffer are
Dave Chinner638f44162013-08-30 10:23:45 +10003900 * correctly relogged in that transaction. If we are in recovery context, then
3901 * just queue the modified buffer as delayed write buffer so the transaction
3902 * recovery completion writes the changes to disk.
Dave Chinner21b5c972013-08-30 10:23:44 +10003903 */
3904static int
3905xfs_btree_block_change_owner(
3906 struct xfs_btree_cur *cur,
3907 int level,
Dave Chinner638f44162013-08-30 10:23:45 +10003908 __uint64_t new_owner,
3909 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10003910{
3911 struct xfs_btree_block *block;
3912 struct xfs_buf *bp;
3913 union xfs_btree_ptr rptr;
3914
3915 /* do right sibling readahead */
3916 xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
3917
3918 /* modify the owner */
3919 block = xfs_btree_get_block(cur, level, &bp);
3920 if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
3921 block->bb_u.l.bb_owner = cpu_to_be64(new_owner);
3922 else
3923 block->bb_u.s.bb_owner = cpu_to_be32(new_owner);
3924
3925 /*
Dave Chinner638f44162013-08-30 10:23:45 +10003926 * If the block is a root block hosted in an inode, we might not have a
3927 * buffer pointer here and we shouldn't attempt to log the change as the
3928 * information is already held in the inode and discarded when the root
3929 * block is formatted into the on-disk inode fork. We still change it,
3930 * though, so everything is consistent in memory.
Dave Chinner21b5c972013-08-30 10:23:44 +10003931 */
3932 if (bp) {
Dave Chinner638f44162013-08-30 10:23:45 +10003933 if (cur->bc_tp) {
3934 xfs_trans_ordered_buf(cur->bc_tp, bp);
3935 xfs_btree_log_block(cur, bp, XFS_BB_OWNER);
3936 } else {
3937 xfs_buf_delwri_queue(bp, buffer_list);
3938 }
Dave Chinner21b5c972013-08-30 10:23:44 +10003939 } else {
3940 ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
3941 ASSERT(level == cur->bc_nlevels - 1);
3942 }
3943
3944 /* now read rh sibling block for next iteration */
3945 xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
3946 if (xfs_btree_ptr_is_null(cur, &rptr))
3947 return ENOENT;
3948
3949 return xfs_btree_lookup_get_block(cur, level, &rptr, &block);
3950}
3951
3952int
3953xfs_btree_change_owner(
3954 struct xfs_btree_cur *cur,
Dave Chinner638f44162013-08-30 10:23:45 +10003955 __uint64_t new_owner,
3956 struct list_head *buffer_list)
Dave Chinner21b5c972013-08-30 10:23:44 +10003957{
3958 union xfs_btree_ptr lptr;
3959 int level;
3960 struct xfs_btree_block *block = NULL;
3961 int error = 0;
3962
3963 cur->bc_ops->init_ptr_from_cur(cur, &lptr);
3964
3965 /* for each level */
3966 for (level = cur->bc_nlevels - 1; level >= 0; level--) {
3967 /* grab the left hand block */
3968 error = xfs_btree_lookup_get_block(cur, level, &lptr, &block);
3969 if (error)
3970 return error;
3971
3972 /* readahead the left most block for the next level down */
3973 if (level > 0) {
3974 union xfs_btree_ptr *ptr;
3975
3976 ptr = xfs_btree_ptr_addr(cur, 1, block);
3977 xfs_btree_readahead_ptr(cur, ptr, 1);
3978
3979 /* save for the next iteration of the loop */
3980 lptr = *ptr;
3981 }
3982
3983 /* for each buffer in the level */
3984 do {
3985 error = xfs_btree_block_change_owner(cur, level,
Dave Chinner638f44162013-08-30 10:23:45 +10003986 new_owner,
3987 buffer_list);
Dave Chinner21b5c972013-08-30 10:23:44 +10003988 } while (!error);
3989
3990 if (error != ENOENT)
3991 return error;
3992 }
3993
3994 return 0;
3995}