1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2021-2024 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_trans.h"
14 #include "xfs_btree.h"
15 #include "xfs_error.h"
16 #include "xfs_buf_mem.h"
17 #include "xfs_btree_mem.h"
19 #include "xfs_buf_item.h"
20 #include "xfs_trace.h"
22 /* Set the root of an in-memory btree. */
25 struct xfs_btree_cur *cur,
26 const union xfs_btree_ptr *ptr,
29 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_MEM);
31 cur->bc_mem.xfbtree->root = *ptr;
32 cur->bc_mem.xfbtree->nlevels += inc;
35 /* Initialize a pointer from the in-memory btree header. */
37 xfbtree_init_ptr_from_cur(
38 struct xfs_btree_cur *cur,
39 union xfs_btree_ptr *ptr)
41 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_MEM);
43 *ptr = cur->bc_mem.xfbtree->root;
46 /* Duplicate an in-memory btree cursor. */
47 struct xfs_btree_cur *
49 struct xfs_btree_cur *cur)
51 struct xfs_btree_cur *ncur;
53 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_MEM);
55 ncur = xfs_btree_alloc_cursor(cur->bc_mp, cur->bc_tp, cur->bc_ops,
56 cur->bc_maxlevels, cur->bc_cache);
57 ncur->bc_flags = cur->bc_flags;
58 ncur->bc_nlevels = cur->bc_nlevels;
59 ncur->bc_mem.xfbtree = cur->bc_mem.xfbtree;
62 ncur->bc_mem.pag = xfs_perag_hold(cur->bc_mem.pag);
67 /* Close the btree xfile and release all resources. */
72 xfs_buftarg_drain(xfbt->target);
75 /* Compute the number of bytes available for records. */
76 static inline unsigned int
79 const struct xfs_btree_ops *ops)
81 return XMBUF_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN;
84 /* Initialize an empty leaf block as the btree root. */
86 xfbtree_init_leaf_block(
89 const struct xfs_btree_ops *ops)
92 xfbno_t bno = xfbt->highest_bno++;
95 error = xfs_buf_get(xfbt->target, xfbno_to_daddr(bno), XFBNO_BBSIZE,
100 trace_xfbtree_create_root_buf(xfbt, bp);
102 bp->b_ops = ops->buf_ops;
103 xfs_btree_init_buf(mp, bp, ops, 0, 0, xfbt->owner);
106 xfbt->root.l = cpu_to_be64(bno);
111 * Create an in-memory btree root that can be used with the given xmbuf.
112 * Callers must set xfbt->owner.
116 struct xfs_mount *mp,
117 struct xfbtree *xfbt,
118 struct xfs_buftarg *btp,
119 const struct xfs_btree_ops *ops)
121 unsigned int blocklen = xfbtree_rec_bytes(mp, ops);
122 unsigned int keyptr_len;
125 /* Requires a long-format CRC-format btree */
126 if (!xfs_has_crc(mp)) {
127 ASSERT(xfs_has_crc(mp));
130 if (ops->ptr_len != XFS_BTREE_LONG_PTR_LEN) {
131 ASSERT(ops->ptr_len == XFS_BTREE_LONG_PTR_LEN);
135 memset(xfbt, 0, sizeof(*xfbt));
138 /* Set up min/maxrecs for this btree. */
139 keyptr_len = ops->key_len + sizeof(__be64);
140 xfbt->maxrecs[0] = blocklen / ops->rec_len;
141 xfbt->maxrecs[1] = blocklen / keyptr_len;
142 xfbt->minrecs[0] = xfbt->maxrecs[0] / 2;
143 xfbt->minrecs[1] = xfbt->maxrecs[1] / 2;
144 xfbt->highest_bno = 0;
147 /* Initialize the empty btree. */
148 error = xfbtree_init_leaf_block(mp, xfbt, ops);
152 trace_xfbtree_init(mp, xfbt, ops);
157 xfs_buftarg_drain(xfbt->target);
161 /* Allocate a block to our in-memory btree. */
164 struct xfs_btree_cur *cur,
165 const union xfs_btree_ptr *start,
166 union xfs_btree_ptr *new,
169 struct xfbtree *xfbt = cur->bc_mem.xfbtree;
170 xfbno_t bno = xfbt->highest_bno++;
172 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_MEM);
174 trace_xfbtree_alloc_block(xfbt, cur, bno);
176 /* Fail if the block address exceeds the maximum for the buftarg. */
177 if (!xfbtree_verify_bno(xfbt, bno)) {
178 ASSERT(xfbtree_verify_bno(xfbt, bno));
183 new->l = cpu_to_be64(bno);
188 /* Free a block from our in-memory btree. */
191 struct xfs_btree_cur *cur,
194 struct xfbtree *xfbt = cur->bc_mem.xfbtree;
195 xfs_daddr_t daddr = xfs_buf_daddr(bp);
196 xfbno_t bno = xfs_daddr_to_xfbno(daddr);
198 ASSERT(cur->bc_ops->type == XFS_BTREE_TYPE_MEM);
200 trace_xfbtree_free_block(xfbt, cur, bno);
202 if (bno + 1 == xfbt->highest_bno)
208 /* Return the minimum number of records for a btree block. */
211 struct xfs_btree_cur *cur,
214 struct xfbtree *xfbt = cur->bc_mem.xfbtree;
216 return xfbt->minrecs[level != 0];
219 /* Return the maximum number of records for a btree block. */
222 struct xfs_btree_cur *cur,
225 struct xfbtree *xfbt = cur->bc_mem.xfbtree;
227 return xfbt->maxrecs[level != 0];
230 /* If this log item is a buffer item that came from the xfbtree, return it. */
231 static inline struct xfs_buf *
233 struct xfbtree *xfbt,
234 const struct xfs_log_item *lip)
236 const struct xfs_buf_log_item *bli;
239 if (lip->li_type != XFS_LI_BUF)
242 bli = container_of(lip, struct xfs_buf_log_item, bli_item);
244 if (bp->b_target != xfbt->target)
251 * Commit changes to the incore btree immediately by writing all dirty xfbtree
252 * buffers to the backing xfile. This detaches all xfbtree buffers from the
253 * transaction, even on failure. The buffer locks are dropped between the
254 * delwri queue and submit, so the caller must synchronize btree access.
256 * Normally we'd let the buffers commit with the transaction and get written to
257 * the xfile via the log, but online repair stages ephemeral btrees in memory
258 * and uses the btree_staging functions to write new btrees to disk atomically.
259 * The in-memory btree (and its backing store) are discarded at the end of the
260 * repair phase, which means that xfbtree buffers cannot commit with the rest
263 * In other words, online repair only needs the transaction to collect buffer
264 * pointers and to avoid buffer deadlocks, not to guarantee consistency of
268 xfbtree_trans_commit(
269 struct xfbtree *xfbt,
270 struct xfs_trans *tp)
272 struct xfs_log_item *lip, *n;
273 bool tp_dirty = false;
277 * For each xfbtree buffer attached to the transaction, write the dirty
278 * buffers to the xfile and release them.
280 list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
281 struct xfs_buf *bp = xfbtree_buf_match(xfbt, lip);
284 if (test_bit(XFS_LI_DIRTY, &lip->li_flags))
289 trace_xfbtree_trans_commit_buf(xfbt, bp);
291 xmbuf_trans_bdetach(tp, bp);
294 * If the buffer fails verification, note the failure but
295 * continue walking the transaction items so that we remove all
296 * ephemeral btree buffers.
299 error = xmbuf_finalize(bp);
305 * Reset the transaction's dirty flag to reflect the dirty state of the
306 * log items that are still attached.
308 tp->t_flags = (tp->t_flags & ~XFS_TRANS_DIRTY) |
309 (tp_dirty ? XFS_TRANS_DIRTY : 0);
315 * Cancel changes to the incore btree by detaching all the xfbtree buffers.
316 * Changes are not undone, so callers must not access the btree ever again.
319 xfbtree_trans_cancel(
320 struct xfbtree *xfbt,
321 struct xfs_trans *tp)
323 struct xfs_log_item *lip, *n;
324 bool tp_dirty = false;
326 list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) {
327 struct xfs_buf *bp = xfbtree_buf_match(xfbt, lip);
330 if (test_bit(XFS_LI_DIRTY, &lip->li_flags))
335 trace_xfbtree_trans_cancel_buf(xfbt, bp);
337 xmbuf_trans_bdetach(tp, bp);
342 * Reset the transaction's dirty flag to reflect the dirty state of the
343 * log items that are still attached.
345 tp->t_flags = (tp->t_flags & ~XFS_TRANS_DIRTY) |
346 (tp_dirty ? XFS_TRANS_DIRTY : 0);