blob: 9dc1ecb9713d80dc91677dcdfc899c9c9ba1e5ad [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11009#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_format.h"
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110013#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_mount.h"
Christoph Hellwigd8d11fc2019-11-11 12:59:26 -080015#include "xfs_inode.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100016#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020017#include "xfs_dir2_priv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000022#include "xfs_trace.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100023#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110024#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * xfs_da_btree.c
28 *
29 * Routines to implement directories as Btrees of hashed names.
30 */
31
32/*========================================================================
33 * Function prototypes for the kernel.
34 *========================================================================*/
35
36/*
37 * Routines used for growing the Btree.
38 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100039STATIC int xfs_da3_root_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 xfs_da_state_blk_t *existing_root,
41 xfs_da_state_blk_t *new_child);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100042STATIC int xfs_da3_node_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 xfs_da_state_blk_t *existing_blk,
44 xfs_da_state_blk_t *split_blk,
45 xfs_da_state_blk_t *blk_to_add,
46 int treelevel,
47 int *result);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100048STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 xfs_da_state_blk_t *node_blk_1,
50 xfs_da_state_blk_t *node_blk_2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100051STATIC void xfs_da3_node_add(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 xfs_da_state_blk_t *old_node_blk,
53 xfs_da_state_blk_t *new_node_blk);
54
55/*
56 * Routines used for shrinking the Btree.
57 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100058STATIC int xfs_da3_root_join(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 xfs_da_state_blk_t *root_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100060STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
61STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 xfs_da_state_blk_t *drop_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100063STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 xfs_da_state_blk_t *src_node_blk,
65 xfs_da_state_blk_t *dst_node_blk);
66
67/*
68 * Utility routines.
69 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100070STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100071 xfs_da_state_blk_t *drop_blk,
72 xfs_da_state_blk_t *save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dave Chinnerf5ea1102013-04-24 18:58:02 +100074
Darrick J. Wong182696f2021-10-12 11:09:23 -070075struct kmem_cache *xfs_da_state_cache; /* anchor for dir/attr state */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100076
77/*
78 * Allocate a dir-state structure.
79 * We don't put them on the stack since they're large.
80 */
Carlos Maiolino4491a3d2020-07-22 09:23:18 -070081struct xfs_da_state *
82xfs_da_state_alloc(
83 struct xfs_da_args *args)
Dave Chinnerf5ea1102013-04-24 18:58:02 +100084{
Carlos Maiolino4491a3d2020-07-22 09:23:18 -070085 struct xfs_da_state *state;
86
Darrick J. Wong182696f2021-10-12 11:09:23 -070087 state = kmem_cache_zalloc(xfs_da_state_cache, GFP_NOFS | __GFP_NOFAIL);
Carlos Maiolino4491a3d2020-07-22 09:23:18 -070088 state->args = args;
89 state->mp = args->dp->i_mount;
90 return state;
Dave Chinnerf5ea1102013-04-24 18:58:02 +100091}
92
93/*
94 * Kill the altpath contents of a da-state structure.
95 */
96STATIC void
97xfs_da_state_kill_altpath(xfs_da_state_t *state)
98{
99 int i;
100
101 for (i = 0; i < state->altpath.active; i++)
102 state->altpath.blk[i].bp = NULL;
103 state->altpath.active = 0;
104}
105
106/*
107 * Free a da-state structure.
108 */
109void
110xfs_da_state_free(xfs_da_state_t *state)
111{
112 xfs_da_state_kill_altpath(state);
113#ifdef DEBUG
114 memset((char *)state, 0, sizeof(*state));
115#endif /* DEBUG */
Darrick J. Wong182696f2021-10-12 11:09:23 -0700116 kmem_cache_free(xfs_da_state_cache, state);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000117}
118
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -0800119static inline int xfs_dabuf_nfsb(struct xfs_mount *mp, int whichfork)
120{
121 if (whichfork == XFS_DATA_FORK)
122 return mp->m_dir_geo->fsbcount;
123 return mp->m_attr_geo->fsbcount;
124}
125
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800126void
127xfs_da3_node_hdr_from_disk(
128 struct xfs_mount *mp,
129 struct xfs_da3_icnode_hdr *to,
130 struct xfs_da_intnode *from)
131{
Dave Chinnerebd90272021-08-18 18:46:55 -0700132 if (xfs_has_crc(mp)) {
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800133 struct xfs_da3_intnode *from3 = (struct xfs_da3_intnode *)from;
134
135 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
136 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
137 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
138 to->count = be16_to_cpu(from3->hdr.__count);
139 to->level = be16_to_cpu(from3->hdr.__level);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800140 to->btree = from3->__btree;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800141 ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
142 } else {
143 to->forw = be32_to_cpu(from->hdr.info.forw);
144 to->back = be32_to_cpu(from->hdr.info.back);
145 to->magic = be16_to_cpu(from->hdr.info.magic);
146 to->count = be16_to_cpu(from->hdr.__count);
147 to->level = be16_to_cpu(from->hdr.__level);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800148 to->btree = from->__btree;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800149 ASSERT(to->magic == XFS_DA_NODE_MAGIC);
150 }
151}
152
Christoph Hellwige1c8af12019-11-08 14:57:48 -0800153void
154xfs_da3_node_hdr_to_disk(
155 struct xfs_mount *mp,
156 struct xfs_da_intnode *to,
157 struct xfs_da3_icnode_hdr *from)
158{
Dave Chinnerebd90272021-08-18 18:46:55 -0700159 if (xfs_has_crc(mp)) {
Christoph Hellwige1c8af12019-11-08 14:57:48 -0800160 struct xfs_da3_intnode *to3 = (struct xfs_da3_intnode *)to;
161
162 ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
163 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
164 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
165 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
166 to3->hdr.__count = cpu_to_be16(from->count);
167 to3->hdr.__level = cpu_to_be16(from->level);
168 } else {
169 ASSERT(from->magic == XFS_DA_NODE_MAGIC);
170 to->hdr.info.forw = cpu_to_be32(from->forw);
171 to->hdr.info.back = cpu_to_be32(from->back);
172 to->hdr.info.magic = cpu_to_be16(from->magic);
173 to->hdr.__count = cpu_to_be16(from->count);
174 to->hdr.__level = cpu_to_be16(from->level);
175 }
176}
177
Brian Foster8764f982019-02-07 10:45:48 -0800178/*
179 * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
180 * accessible on v5 filesystems. This header format is common across da node,
181 * attr leaf and dir leaf blocks.
182 */
183xfs_failaddr_t
184xfs_da3_blkinfo_verify(
185 struct xfs_buf *bp,
186 struct xfs_da3_blkinfo *hdr3)
187{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700188 struct xfs_mount *mp = bp->b_mount;
Brian Foster8764f982019-02-07 10:45:48 -0800189 struct xfs_da_blkinfo *hdr = &hdr3->hdr;
190
Darrick J. Wong15baadf2019-02-16 11:47:28 -0800191 if (!xfs_verify_magic16(bp, hdr->magic))
Brian Foster8764f982019-02-07 10:45:48 -0800192 return __this_address;
193
Dave Chinnerebd90272021-08-18 18:46:55 -0700194 if (xfs_has_crc(mp)) {
Brian Foster8764f982019-02-07 10:45:48 -0800195 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
196 return __this_address;
Dave Chinner9343ee72021-08-18 18:47:05 -0700197 if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
Brian Foster8764f982019-02-07 10:45:48 -0800198 return __this_address;
199 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
200 return __this_address;
201 }
202
Darrick J. Wong15baadf2019-02-16 11:47:28 -0800203 return NULL;
Brian Foster8764f982019-02-07 10:45:48 -0800204}
205
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800206static xfs_failaddr_t
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000207xfs_da3_node_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100208 struct xfs_buf *bp)
209{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700210 struct xfs_mount *mp = bp->b_mount;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000211 struct xfs_da_intnode *hdr = bp->b_addr;
212 struct xfs_da3_icnode_hdr ichdr;
Brian Foster8764f982019-02-07 10:45:48 -0800213 xfs_failaddr_t fa;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100214
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800215 xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000216
Brian Foster8764f982019-02-07 10:45:48 -0800217 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
218 if (fa)
219 return fa;
Brian Foster39708c22019-02-07 10:45:48 -0800220
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000221 if (ichdr.level == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800222 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000223 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800224 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000225 if (ichdr.count == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800226 return __this_address;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100227
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000228 /*
229 * we don't know if the node is for and attribute or directory tree,
230 * so only fail if the count is outside both bounds
231 */
Dave Chinner7ab610f2014-06-06 15:20:02 +1000232 if (ichdr.count > mp->m_dir_geo->node_ents &&
233 ichdr.count > mp->m_attr_geo->node_ents)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800234 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000235
236 /* XXX: hash order check? */
237
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800238 return NULL;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100239}
240
241static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000242xfs_da3_node_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100243 struct xfs_buf *bp)
244{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700245 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800246 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000247 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800248 xfs_failaddr_t fa;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000249
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800250 fa = xfs_da3_node_verify(bp);
251 if (fa) {
252 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000253 return;
254 }
255
Dave Chinner38c26bf2021-08-18 18:46:37 -0700256 if (!xfs_has_crc(mp))
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000257 return;
258
259 if (bip)
260 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
261
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100262 xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100263}
264
Dave Chinner1813dd62012-11-14 17:54:40 +1100265/*
266 * leaf/node format detection on trees is sketchy, so a node read can be done on
267 * leaf level blocks when detection identifies the tree as a node format tree
268 * incorrectly. In this case, we need to swap the verifier to match the correct
269 * format of the block being read.
270 */
Dave Chinner612cfbf2012-11-14 17:52:32 +1100271static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000272xfs_da3_node_read_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100273 struct xfs_buf *bp)
274{
Dave Chinnerd9392a42012-11-12 22:54:17 +1100275 struct xfs_da_blkinfo *info = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800276 xfs_failaddr_t fa;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100277
278 switch (be16_to_cpu(info->magic)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000279 case XFS_DA3_NODE_MAGIC:
Eric Sandeence5028c2014-02-27 15:23:10 +1100280 if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800281 xfs_verifier_error(bp, -EFSBADCRC,
282 __this_address);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000283 break;
Eric Sandeence5028c2014-02-27 15:23:10 +1100284 }
Gustavo A. R. Silva53004ee2021-04-20 17:54:36 -0500285 fallthrough;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100286 case XFS_DA_NODE_MAGIC:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800287 fa = xfs_da3_node_verify(bp);
288 if (fa)
289 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000290 return;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100291 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner7ced60c2013-05-20 09:51:13 +1000292 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000293 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100294 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100295 return;
296 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +1000297 case XFS_DIR3_LEAFN_MAGIC:
298 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100299 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100300 return;
301 default:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800302 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100303 break;
304 }
Dave Chinnerd9392a42012-11-12 22:54:17 +1100305}
306
Darrick J. Wongb5572592018-01-08 10:51:08 -0800307/* Verify the structure of a da3 block. */
308static xfs_failaddr_t
309xfs_da3_node_verify_struct(
310 struct xfs_buf *bp)
311{
312 struct xfs_da_blkinfo *info = bp->b_addr;
313
314 switch (be16_to_cpu(info->magic)) {
315 case XFS_DA3_NODE_MAGIC:
316 case XFS_DA_NODE_MAGIC:
317 return xfs_da3_node_verify(bp);
318 case XFS_ATTR_LEAF_MAGIC:
319 case XFS_ATTR3_LEAF_MAGIC:
320 bp->b_ops = &xfs_attr3_leaf_buf_ops;
321 return bp->b_ops->verify_struct(bp);
322 case XFS_DIR2_LEAFN_MAGIC:
323 case XFS_DIR3_LEAFN_MAGIC:
324 bp->b_ops = &xfs_dir3_leafn_buf_ops;
325 return bp->b_ops->verify_struct(bp);
326 default:
327 return __this_address;
328 }
329}
330
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000331const struct xfs_buf_ops xfs_da3_node_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100332 .name = "xfs_da3_node",
Darrick J. Wong15baadf2019-02-16 11:47:28 -0800333 .magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
334 cpu_to_be16(XFS_DA3_NODE_MAGIC) },
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000335 .verify_read = xfs_da3_node_read_verify,
336 .verify_write = xfs_da3_node_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800337 .verify_struct = xfs_da3_node_verify_struct,
Dave Chinner1813dd62012-11-14 17:54:40 +1100338};
339
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800340static int
341xfs_da3_node_set_type(
342 struct xfs_trans *tp,
343 struct xfs_buf *bp)
344{
345 struct xfs_da_blkinfo *info = bp->b_addr;
346
347 switch (be16_to_cpu(info->magic)) {
348 case XFS_DA_NODE_MAGIC:
349 case XFS_DA3_NODE_MAGIC:
350 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
351 return 0;
352 case XFS_ATTR_LEAF_MAGIC:
353 case XFS_ATTR3_LEAF_MAGIC:
354 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_ATTR_LEAF_BUF);
355 return 0;
356 case XFS_DIR2_LEAFN_MAGIC:
357 case XFS_DIR3_LEAFN_MAGIC:
358 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
359 return 0;
360 default:
361 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, tp->t_mountp,
362 info, sizeof(*info));
363 xfs_trans_brelse(tp, bp);
364 return -EFSCORRUPTED;
365 }
366}
367
Dave Chinnerd9392a42012-11-12 22:54:17 +1100368int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000369xfs_da3_node_read(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100370 struct xfs_trans *tp,
371 struct xfs_inode *dp,
372 xfs_dablk_t bno,
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800373 struct xfs_buf **bpp,
374 int whichfork)
375{
376 int error;
377
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800378 error = xfs_da_read_buf(tp, dp, bno, 0, bpp, whichfork,
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800379 &xfs_da3_node_buf_ops);
380 if (error || !*bpp || !tp)
381 return error;
382 return xfs_da3_node_set_type(tp, *bpp);
383}
384
385int
386xfs_da3_node_read_mapped(
387 struct xfs_trans *tp,
388 struct xfs_inode *dp,
Dave Chinnerd9392a42012-11-12 22:54:17 +1100389 xfs_daddr_t mappedbno,
390 struct xfs_buf **bpp,
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800391 int whichfork)
Dave Chinnerd9392a42012-11-12 22:54:17 +1100392{
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800393 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800394 int error;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100395
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800396 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, mappedbno,
397 XFS_FSB_TO_BB(mp, xfs_dabuf_nfsb(mp, whichfork)), 0,
398 bpp, &xfs_da3_node_buf_ops);
399 if (error || !*bpp)
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800400 return error;
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800401
402 if (whichfork == XFS_ATTR_FORK)
403 xfs_buf_set_ref(*bpp, XFS_ATTR_BTREE_REF);
404 else
405 xfs_buf_set_ref(*bpp, XFS_DIR_BTREE_REF);
406
407 if (!tp)
408 return 0;
Christoph Hellwig02c57f02019-11-20 09:46:04 -0800409 return xfs_da3_node_set_type(tp, *bpp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*========================================================================
413 * Routines used for growing the Btree.
414 *========================================================================*/
415
416/*
417 * Create the initial contents of an intermediate node.
418 */
419int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000420xfs_da3_node_create(
421 struct xfs_da_args *args,
422 xfs_dablk_t blkno,
423 int level,
424 struct xfs_buf **bpp,
425 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000427 struct xfs_da_intnode *node;
428 struct xfs_trans *tp = args->trans;
429 struct xfs_mount *mp = tp->t_mountp;
430 struct xfs_da3_icnode_hdr ichdr = {0};
431 struct xfs_buf *bp;
432 int error;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100433 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dave Chinner5a5881c2012-03-22 05:15:13 +0000435 trace_xfs_da_node_create(args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000436 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
Dave Chinner5a5881c2012-03-22 05:15:13 +0000437
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800438 error = xfs_da_get_buf(tp, dp, blkno, &bp, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000440 return error;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100441 bp->b_ops = &xfs_da3_node_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100442 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000443 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Chinner38c26bf2021-08-18 18:46:37 -0700445 if (xfs_has_crc(mp)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000446 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
447
Brian Fostera45086e2015-10-12 15:59:25 +1100448 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000449 ichdr.magic = XFS_DA3_NODE_MAGIC;
Dave Chinner9343ee72021-08-18 18:47:05 -0700450 hdr3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000451 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000452 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000453 } else {
454 ichdr.magic = XFS_DA_NODE_MAGIC;
455 }
456 ichdr.level = level;
457
Christoph Hellwige1c8af12019-11-08 14:57:48 -0800458 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000459 xfs_trans_log_buf(tp, bp,
Christoph Hellwig3b344412019-11-08 14:57:49 -0800460 XFS_DA_LOGRANGE(node, &node->hdr, args->geo->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 *bpp = bp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * Split a leaf node, rebalance, then possibly split
468 * intermediate nodes, rebalance, etc.
469 */
470int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000471xfs_da3_split(
472 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000474 struct xfs_da_state_blk *oldblk;
475 struct xfs_da_state_blk *newblk;
476 struct xfs_da_state_blk *addblk;
477 struct xfs_da_intnode *node;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000478 int max;
Dave Chinner836a94a2013-08-12 20:49:44 +1000479 int action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000480 int error;
481 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Dave Chinner5a5881c2012-03-22 05:15:13 +0000483 trace_xfs_da_split(state->args);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
486 * Walk back up the tree splitting/inserting/adjusting as necessary.
487 * If we need to insert and there isn't room, split the node, then
488 * decide which fragment to insert the new block from below into.
489 * Note that we may split the root this way, but we need more fixup.
490 */
491 max = state->path.active - 1;
492 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
493 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000494 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 addblk = &state->path.blk[max]; /* initial dummy value */
497 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
498 oldblk = &state->path.blk[i];
499 newblk = &state->altpath.blk[i];
500
501 /*
502 * If a leaf node then
503 * Allocate a new leaf node, then rebalance across them.
504 * else if an intermediate node then
505 * We split on the last layer, must we split the node?
506 */
507 switch (oldblk->magic) {
508 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000509 error = xfs_attr3_leaf_split(state, oldblk, newblk);
Dave Chinner24513372014-06-25 14:58:08 +1000510 if ((error != 0) && (error != -ENOSPC)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000511 return error; /* GROT: attr is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 if (!error) {
514 addblk = newblk;
515 break;
516 }
517 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000518 * Entry wouldn't fit, split the leaf again. The new
519 * extrablk will be consumed by xfs_da3_node_split if
520 * the node is split.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
522 state->extravalid = 1;
523 if (state->inleaf) {
524 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000525 trace_xfs_attr_leaf_split_before(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000526 error = xfs_attr3_leaf_split(state, oldblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 &state->extrablk);
528 } else {
529 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000530 trace_xfs_attr_leaf_split_after(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000531 error = xfs_attr3_leaf_split(state, newblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 &state->extrablk);
533 }
534 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000535 return error; /* GROT: attr inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 addblk = newblk;
537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 error = xfs_dir2_leafn_split(state, oldblk, newblk);
540 if (error)
541 return error;
542 addblk = newblk;
543 break;
544 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000545 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 addblk->bp = NULL;
548 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000549 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
551 * Record the newly split block for the next time thru?
552 */
553 if (action)
554 addblk = newblk;
555 else
556 addblk = NULL;
557 break;
558 }
559
560 /*
561 * Update the btree to show the new hashval for this child.
562 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000563 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 if (!addblk)
Eric Sandeend99831f2014-06-22 15:03:54 +1000566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000569 * xfs_da3_node_split() should have consumed any extra blocks we added
570 * during a double leaf split in the attr fork. This is guaranteed as
571 * we can't be here if the attr fork only has a single leaf block.
572 */
573 ASSERT(state->extravalid == 0 ||
574 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
575
576 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 * Split the root node.
578 */
579 ASSERT(state->path.active == 0);
580 oldblk = &state->path.blk[0];
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000581 error = xfs_da3_root_split(state, oldblk, addblk);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700582 if (error)
583 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000586 * Update pointers to the node which used to be block 0 and just got
587 * bumped because of the addition of a new root node. Note that the
588 * original block 0 could be at any position in the list of blocks in
589 * the tree.
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000590 *
Dave Chinner160ae762016-07-22 09:51:05 +1000591 * Note: the magic numbers and sibling pointers are in the same physical
592 * place for both v2 and v3 headers (by design). Hence it doesn't matter
593 * which version of the xfs_da_intnode structure we use here as the
594 * result will be the same using either structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000596 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (node->hdr.info.forw) {
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700598 if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -0700599 xfs_buf_mark_corrupt(oldblk->bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700600 error = -EFSCORRUPTED;
601 goto out;
602 }
Dave Chinner160ae762016-07-22 09:51:05 +1000603 node = addblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100604 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner160ae762016-07-22 09:51:05 +1000605 xfs_trans_log_buf(state->args->trans, addblk->bp,
606 XFS_DA_LOGRANGE(node, &node->hdr.info,
607 sizeof(node->hdr.info)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000609 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100610 if (node->hdr.info.back) {
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700611 if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -0700612 xfs_buf_mark_corrupt(oldblk->bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700613 error = -EFSCORRUPTED;
614 goto out;
615 }
Dave Chinner160ae762016-07-22 09:51:05 +1000616 node = addblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100617 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner160ae762016-07-22 09:51:05 +1000618 xfs_trans_log_buf(state->args->trans, addblk->bp,
619 XFS_DA_LOGRANGE(node, &node->hdr.info,
620 sizeof(node->hdr.info)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700622out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 addblk->bp = NULL;
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700624 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627/*
628 * Split the root. We have to create a new root and point to the two
629 * parts (the split old root) that we just created. Copy block zero to
630 * the EOF, extending the inode in process.
631 */
632STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000633xfs_da3_root_split(
634 struct xfs_da_state *state,
635 struct xfs_da_state_blk *blk1,
636 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000638 struct xfs_da_intnode *node;
639 struct xfs_da_intnode *oldroot;
640 struct xfs_da_node_entry *btree;
641 struct xfs_da3_icnode_hdr nodehdr;
642 struct xfs_da_args *args;
643 struct xfs_buf *bp;
644 struct xfs_inode *dp;
645 struct xfs_trans *tp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000646 struct xfs_dir2_leaf *leaf;
647 xfs_dablk_t blkno;
648 int level;
649 int error;
650 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Chinner5a5881c2012-03-22 05:15:13 +0000652 trace_xfs_da_root_split(state->args);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Copy the existing (incorrect) block from the root node position
656 * to a free space somewhere.
657 */
658 args = state->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 error = xfs_da_grow_inode(args, &blkno);
660 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000661 return error;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 dp = args->dp;
664 tp = args->trans;
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800665 error = xfs_da_get_buf(tp, dp, blkno, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000667 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000668 node = bp->b_addr;
669 oldroot = blk1->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000670 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
671 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
Fabian Frederick29916df2015-03-25 14:55:25 +1100672 struct xfs_da3_icnode_hdr icnodehdr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000673
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800674 xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800675 btree = icnodehdr.btree;
Fabian Frederick29916df2015-03-25 14:55:25 +1100676 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
677 level = icnodehdr.level;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100678
679 /*
680 * we are about to copy oldroot to bp, so set up the type
681 * of bp while we know exactly what it will be.
682 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100683 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000685 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 leaf = (xfs_dir2_leaf_t *)oldroot;
Christoph Hellwig51842552019-11-08 14:57:49 -0800688 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000689
690 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
691 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800692 size = (int)((char *)&leafhdr.ents[leafhdr.count] -
693 (char *)leaf);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000694 level = 0;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100695
696 /*
697 * we are about to copy oldroot to bp, so set up the type
698 * of bp while we know exactly what it will be.
699 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100700 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000702
703 /*
704 * we can copy most of the information in the node from one block to
705 * another, but for CRC enabled headers we have to make sure that the
706 * block specific identifiers are kept intact. We update the buffer
707 * directly for this.
708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 memcpy(node, oldroot, size);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000710 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
711 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
712 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
713
Dave Chinner9343ee72021-08-18 18:47:05 -0700714 node3->hdr.info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000715 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000716 xfs_trans_log_buf(tp, bp, 0, size - 1);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100717
Dave Chinner1813dd62012-11-14 17:54:40 +1100718 bp->b_ops = blk1->bp->b_ops;
Dave Chinner0a4edc82013-09-02 10:32:01 +1000719 xfs_trans_buf_copy_type(bp, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 blk1->bp = bp;
721 blk1->blkno = blkno;
722
723 /*
724 * Set up the new root node.
725 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000726 error = xfs_da3_node_create(args,
Dave Chinner7dda6e82014-06-06 15:11:18 +1000727 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000728 level + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000730 return error;
731
Dave Chinner1d9025e2012-06-22 18:50:14 +1000732 node = bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800733 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800734 btree = nodehdr.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000735 btree[0].hashval = cpu_to_be32(blk1->hashval);
736 btree[0].before = cpu_to_be32(blk1->blkno);
737 btree[1].hashval = cpu_to_be32(blk2->hashval);
738 btree[1].before = cpu_to_be32(blk2->blkno);
739 nodehdr.count = 2;
Christoph Hellwige1c8af12019-11-08 14:57:48 -0800740 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +1000743 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
744 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000745 ASSERT(blk1->blkno >= args->geo->leafblk &&
746 blk1->blkno < args->geo->freeblk);
747 ASSERT(blk2->blkno >= args->geo->leafblk &&
748 blk2->blkno < args->geo->freeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750#endif
751
752 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000753 xfs_trans_log_buf(tp, bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000754 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
760 * Split the node, rebalance, then add the new entry.
761 */
762STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000763xfs_da3_node_split(
764 struct xfs_da_state *state,
765 struct xfs_da_state_blk *oldblk,
766 struct xfs_da_state_blk *newblk,
767 struct xfs_da_state_blk *addblk,
768 int treelevel,
769 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000771 struct xfs_da_intnode *node;
772 struct xfs_da3_icnode_hdr nodehdr;
773 xfs_dablk_t blkno;
774 int newcount;
775 int error;
776 int useextra;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100777 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dave Chinner5a5881c2012-03-22 05:15:13 +0000779 trace_xfs_da_node_split(state->args);
780
Dave Chinner1d9025e2012-06-22 18:50:14 +1000781 node = oldblk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800782 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000785 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000787 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 newcount = 1 + useextra;
789 /*
790 * Do we have to split the node?
791 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +1000792 if (nodehdr.count + newcount > state->args->geo->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
794 * Allocate a new node, add to the doubly linked chain of
795 * nodes, then move some of our excess entries into it.
796 */
797 error = xfs_da_grow_inode(state->args, &blkno);
798 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000799 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000801 error = xfs_da3_node_create(state->args, blkno, treelevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 &newblk->bp, state->args->whichfork);
803 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000804 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 newblk->blkno = blkno;
806 newblk->magic = XFS_DA_NODE_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000807 xfs_da3_node_rebalance(state, oldblk, newblk);
808 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 *result = 1;
812 } else {
813 *result = 0;
814 }
815
816 /*
817 * Insert the new entry(s) into the correct block
818 * (updating last hashval in the process).
819 *
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000820 * xfs_da3_node_add() inserts BEFORE the given index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 * and as a result of using node_lookup_int() we always
822 * point to a valid entry (not after one), but a split
823 * operation always results in a new block whose hashvals
824 * FOLLOW the current block.
825 *
826 * If we had double-split op below us, then add the extra block too.
827 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000828 node = oldblk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800829 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000830 if (oldblk->index <= nodehdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000832 xfs_da3_node_add(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (useextra) {
834 if (state->extraafter)
835 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000836 xfs_da3_node_add(state, oldblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 state->extravalid = 0;
838 }
839 } else {
840 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000841 xfs_da3_node_add(state, newblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (useextra) {
843 if (state->extraafter)
844 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000845 xfs_da3_node_add(state, newblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 state->extravalid = 0;
847 }
848 }
849
Eric Sandeend99831f2014-06-22 15:03:54 +1000850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853/*
854 * Balance the btree elements between two intermediate nodes,
855 * usually one full and one empty.
856 *
857 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
858 */
859STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000860xfs_da3_node_rebalance(
861 struct xfs_da_state *state,
862 struct xfs_da_state_blk *blk1,
863 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000865 struct xfs_da_intnode *node1;
866 struct xfs_da_intnode *node2;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000867 struct xfs_da_node_entry *btree1;
868 struct xfs_da_node_entry *btree2;
869 struct xfs_da_node_entry *btree_s;
870 struct xfs_da_node_entry *btree_d;
871 struct xfs_da3_icnode_hdr nodehdr1;
872 struct xfs_da3_icnode_hdr nodehdr2;
873 struct xfs_trans *tp;
874 int count;
875 int tmp;
876 int swap = 0;
Dave Chinner4bceb182013-10-29 22:11:51 +1100877 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dave Chinner5a5881c2012-03-22 05:15:13 +0000879 trace_xfs_da_node_rebalance(state->args);
880
Dave Chinner1d9025e2012-06-22 18:50:14 +1000881 node1 = blk1->bp->b_addr;
882 node2 = blk2->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800883 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
884 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800885 btree1 = nodehdr1.btree;
886 btree2 = nodehdr2.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /*
889 * Figure out how many entries need to move, and in which direction.
890 * Swap the nodes around if that makes it simpler.
891 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000892 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
893 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
894 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
895 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
Yang Guang5b068aa2021-11-08 11:23:25 -0800896 swap(node1, node2);
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800897 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
898 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800899 btree1 = nodehdr1.btree;
900 btree2 = nodehdr2.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000901 swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000903
904 count = (nodehdr1.count - nodehdr2.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (count == 0)
906 return;
907 tp = state->args->trans;
908 /*
909 * Two cases: high-to-low and low-to-high.
910 */
911 if (count > 0) {
912 /*
913 * Move elements in node2 up to make a hole.
914 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000915 tmp = nodehdr2.count;
916 if (tmp > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000918 btree_s = &btree2[0];
919 btree_d = &btree2[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 memmove(btree_d, btree_s, tmp);
921 }
922
923 /*
924 * Move the req'd B-tree elements from high in node1 to
925 * low in node2.
926 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000927 nodehdr2.count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000929 btree_s = &btree1[nodehdr1.count - count];
930 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000932 nodehdr1.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else {
934 /*
935 * Move the req'd B-tree elements from low in node2 to
936 * high in node1.
937 */
938 count = -count;
939 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000940 btree_s = &btree2[0];
941 btree_d = &btree1[nodehdr1.count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000943 nodehdr1.count += count;
944
Dave Chinner1d9025e2012-06-22 18:50:14 +1000945 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 XFS_DA_LOGRANGE(node1, btree_d, tmp));
947
948 /*
949 * Move elements in node2 down to fill the hole.
950 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000951 tmp = nodehdr2.count - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000953 btree_s = &btree2[count];
954 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 memmove(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000956 nodehdr2.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 /*
960 * Log header of node 1 and all current bits of node 2.
961 */
Christoph Hellwige1c8af12019-11-08 14:57:48 -0800962 xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000963 xfs_trans_log_buf(tp, blk1->bp,
Christoph Hellwig3b344412019-11-08 14:57:49 -0800964 XFS_DA_LOGRANGE(node1, &node1->hdr,
965 state->args->geo->node_hdr_size));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000966
Christoph Hellwige1c8af12019-11-08 14:57:48 -0800967 xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000968 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 XFS_DA_LOGRANGE(node2, &node2->hdr,
Christoph Hellwig3b344412019-11-08 14:57:49 -0800970 state->args->geo->node_hdr_size +
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000971 (sizeof(btree2[0]) * nodehdr2.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
974 * Record the last hashval from each block for upward propagation.
975 * (note: don't use the swapped node pointers)
976 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000977 if (swap) {
978 node1 = blk1->bp->b_addr;
979 node2 = blk2->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -0800980 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
981 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
Christoph Hellwig51908ca2019-11-08 14:57:48 -0800982 btree1 = nodehdr1.btree;
983 btree2 = nodehdr2.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000984 }
985 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
986 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /*
989 * Adjust the expected index for insertion.
990 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000991 if (blk1->index >= nodehdr1.count) {
992 blk2->index = blk1->index - nodehdr1.count;
993 blk1->index = nodehdr1.count + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995}
996
997/*
998 * Add a new entry to an intermediate node.
999 */
1000STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001001xfs_da3_node_add(
1002 struct xfs_da_state *state,
1003 struct xfs_da_state_blk *oldblk,
1004 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001006 struct xfs_da_intnode *node;
1007 struct xfs_da3_icnode_hdr nodehdr;
1008 struct xfs_da_node_entry *btree;
1009 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001010 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Dave Chinner5a5881c2012-03-22 05:15:13 +00001012 trace_xfs_da_node_add(state->args);
1013
Dave Chinner1d9025e2012-06-22 18:50:14 +10001014 node = oldblk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001015 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001016 btree = nodehdr.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001017
1018 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001020 if (state->args->whichfork == XFS_DATA_FORK)
Dave Chinner7dda6e82014-06-06 15:11:18 +10001021 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
1022 newblk->blkno < state->args->geo->freeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /*
1025 * We may need to make some room before we insert the new node.
1026 */
1027 tmp = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001028 if (oldblk->index < nodehdr.count) {
1029 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
1030 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001032 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
1033 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001034 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001035 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
1036 tmp + sizeof(*btree)));
1037
1038 nodehdr.count += 1;
Christoph Hellwige1c8af12019-11-08 14:57:48 -08001039 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001040 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Christoph Hellwig3b344412019-11-08 14:57:49 -08001041 XFS_DA_LOGRANGE(node, &node->hdr,
1042 state->args->geo->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /*
1045 * Copy the last hash value from the oldblk to propagate upwards.
1046 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001047 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050/*========================================================================
1051 * Routines used for shrinking the Btree.
1052 *========================================================================*/
1053
1054/*
1055 * Deallocate an empty leaf node, remove it from its parent,
1056 * possibly deallocating that block, etc...
1057 */
1058int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001059xfs_da3_join(
1060 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001062 struct xfs_da_state_blk *drop_blk;
1063 struct xfs_da_state_blk *save_blk;
1064 int action = 0;
1065 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Dave Chinner5a5881c2012-03-22 05:15:13 +00001067 trace_xfs_da_join(state->args);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 drop_blk = &state->path.blk[ state->path.active-1 ];
1070 save_blk = &state->altpath.blk[ state->path.active-1 ];
1071 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
1072 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001073 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /*
1076 * Walk back up the tree joining/deallocating as necessary.
1077 * When we stop dropping blocks, break out.
1078 */
1079 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
1080 state->path.active--) {
1081 /*
1082 * See if we can combine the block with a neighbor.
1083 * (action == 0) => no options, just leave
1084 * (action == 1) => coalesce, then unlink
1085 * (action == 2) => block empty, unlink it
1086 */
1087 switch (drop_blk->magic) {
1088 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001089 error = xfs_attr3_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001091 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (action == 0)
Eric Sandeend99831f2014-06-22 15:03:54 +10001093 return 0;
Dave Chinner517c2222013-04-24 18:58:55 +10001094 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 error = xfs_dir2_leafn_toosmall(state, &action);
1098 if (error)
1099 return error;
1100 if (action == 0)
1101 return 0;
1102 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1103 break;
1104 case XFS_DA_NODE_MAGIC:
1105 /*
1106 * Remove the offending node, fixup hashvals,
1107 * check for a toosmall neighbor.
1108 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001109 xfs_da3_node_remove(state, drop_blk);
1110 xfs_da3_fixhashpath(state, &state->path);
1111 error = xfs_da3_node_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001113 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (action == 0)
1115 return 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001116 xfs_da3_node_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 break;
1118 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001119 xfs_da3_fixhashpath(state, &state->altpath);
1120 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 xfs_da_state_kill_altpath(state);
1122 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001123 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1125 drop_blk->bp);
1126 drop_blk->bp = NULL;
1127 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001128 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 /*
1131 * We joined all the way to the top. If it turns out that
1132 * we only have one entry in the root, make the child block
1133 * the new root.
1134 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001135 xfs_da3_node_remove(state, drop_blk);
1136 xfs_da3_fixhashpath(state, &state->path);
1137 error = xfs_da3_root_join(state, &state->path.blk[0]);
Eric Sandeend99831f2014-06-22 15:03:54 +10001138 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Alex Elder1c4f3322011-07-18 18:14:09 +00001141#ifdef DEBUG
1142static void
1143xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1144{
1145 __be16 magic = blkinfo->magic;
1146
1147 if (level == 1) {
1148 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001149 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001150 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1151 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001152 } else {
1153 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1154 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1155 }
Alex Elder1c4f3322011-07-18 18:14:09 +00001156 ASSERT(!blkinfo->forw);
1157 ASSERT(!blkinfo->back);
1158}
1159#else /* !DEBUG */
1160#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1161#endif /* !DEBUG */
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/*
1164 * We have only one entry in the root. Copy the only remaining child of
1165 * the old root to block 0 as the new root node.
1166 */
1167STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001168xfs_da3_root_join(
1169 struct xfs_da_state *state,
1170 struct xfs_da_state_blk *root_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001172 struct xfs_da_intnode *oldroot;
1173 struct xfs_da_args *args;
1174 xfs_dablk_t child;
1175 struct xfs_buf *bp;
1176 struct xfs_da3_icnode_hdr oldroothdr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001177 int error;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001178 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Dave Chinner5a5881c2012-03-22 05:15:13 +00001180 trace_xfs_da_root_join(state->args);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001183
1184 args = state->args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001185 oldroot = root_blk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001186 xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001187 ASSERT(oldroothdr.forw == 0);
1188 ASSERT(oldroothdr.back == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * If the root has more than one child, then don't do anything.
1192 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001193 if (oldroothdr.count > 1)
1194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 /*
1197 * Read in the (only) child block, then copy those bytes into
1198 * the root block's buffer and free the original child block.
1199 */
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001200 child = be32_to_cpu(oldroothdr.btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 ASSERT(child != 0);
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001202 error = xfs_da3_node_read(args->trans, dp, child, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001204 return error;
1205 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
Alex Elder1c4f3322011-07-18 18:14:09 +00001206
Dave Chinner612cfbf2012-11-14 17:52:32 +11001207 /*
1208 * This could be copying a leaf back into the root block in the case of
1209 * there only being a single leaf block left in the tree. Hence we have
Dave Chinner1813dd62012-11-14 17:54:40 +11001210 * to update the b_ops pointer as well to match the buffer type change
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001211 * that could occur. For dir3 blocks we also need to update the block
1212 * number in the buffer header.
Dave Chinner612cfbf2012-11-14 17:52:32 +11001213 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001214 memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
Dave Chinner1813dd62012-11-14 17:54:40 +11001215 root_blk->bp->b_ops = bp->b_ops;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001216 xfs_trans_buf_copy_type(root_blk->bp, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001217 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1218 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
Dave Chinner9343ee72021-08-18 18:47:05 -07001219 da3->blkno = cpu_to_be64(xfs_buf_daddr(root_blk->bp));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001220 }
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001221 xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1222 args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 error = xfs_da_shrink_inode(args, child, bp);
Eric Sandeend99831f2014-06-22 15:03:54 +10001224 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227/*
1228 * Check a node block and its neighbors to see if the block should be
1229 * collapsed into one or the other neighbor. Always keep the block
1230 * with the smaller block number.
1231 * If the current block is over 50% full, don't try to join it, return 0.
1232 * If the block is empty, fill in the state structure and return 2.
1233 * If it can be collapsed, fill in the state structure and return 1.
1234 * If nothing can be done, return 0.
1235 */
1236STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001237xfs_da3_node_toosmall(
1238 struct xfs_da_state *state,
1239 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001241 struct xfs_da_intnode *node;
1242 struct xfs_da_state_blk *blk;
1243 struct xfs_da_blkinfo *info;
1244 xfs_dablk_t blkno;
1245 struct xfs_buf *bp;
1246 struct xfs_da3_icnode_hdr nodehdr;
1247 int count;
1248 int forward;
1249 int error;
1250 int retval;
1251 int i;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001252 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Dave Chinneree732592012-11-12 22:53:53 +11001254 trace_xfs_da_node_toosmall(state->args);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * Check for the degenerate case of the block being over 50% full.
1258 * If so, it's not worth even looking to see if we might be able
1259 * to coalesce with a sibling.
1260 */
1261 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001262 info = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 node = (xfs_da_intnode_t *)info;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001264 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001265 if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 *action = 0; /* blk over 50%, don't try to join */
Eric Sandeend99831f2014-06-22 15:03:54 +10001267 return 0; /* blk over 50%, don't try to join */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
1270 /*
1271 * Check for the degenerate case of the block being empty.
1272 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001273 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * to merge with the forward block unless it is NULL.
1275 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001276 if (nodehdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * Make altpath point to the block we want to keep and
1279 * path point to the block we want to drop (this one).
1280 */
Nathan Scott89da0542006-03-17 17:28:40 +11001281 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001283 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 0, &retval);
1285 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001286 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (retval) {
1288 *action = 0;
1289 } else {
1290 *action = 2;
1291 }
Eric Sandeend99831f2014-06-22 15:03:54 +10001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295 /*
1296 * Examine each sibling block to see if we can coalesce with
1297 * at least 25% free space to spare. We need to figure out
1298 * whether to merge with the forward or the backward block.
1299 * We prefer coalescing with the lower numbered sibling so as
1300 * to shrink a directory over time.
1301 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001302 count = state->args->geo->node_ents;
1303 count -= state->args->geo->node_ents >> 2;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001304 count -= nodehdr.count;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* start with smaller blk num */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001307 forward = nodehdr.forw < nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 for (i = 0; i < 2; forward = !forward, i++) {
Mark Tinguely997def22013-09-23 12:18:58 -05001309 struct xfs_da3_icnode_hdr thdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (forward)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001311 blkno = nodehdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001313 blkno = nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (blkno == 0)
1315 continue;
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001316 error = xfs_da3_node_read(state->args->trans, dp, blkno, &bp,
1317 state->args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001319 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Chinner1d9025e2012-06-22 18:50:14 +10001321 node = bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001322 xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001323 xfs_trans_brelse(state->args->trans, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001324
Mark Tinguely997def22013-09-23 12:18:58 -05001325 if (count - thdr.count >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break; /* fits with at least 25% to spare */
1327 }
1328 if (i >= 2) {
1329 *action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
1333 /*
1334 * Make altpath point to the block we want to keep (the lower
1335 * numbered block) and path point to the block we want to drop.
1336 */
1337 memcpy(&state->altpath, &state->path, sizeof(state->path));
1338 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001339 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 0, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001342 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 0, &retval);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001344 }
1345 if (error)
1346 return error;
1347 if (retval) {
1348 *action = 0;
1349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351 *action = 1;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001352 return 0;
1353}
1354
1355/*
1356 * Pick up the last hashvalue from an intermediate node.
1357 */
1358STATIC uint
1359xfs_da3_node_lasthash(
Dave Chinner4bceb182013-10-29 22:11:51 +11001360 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001361 struct xfs_buf *bp,
1362 int *count)
1363{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001364 struct xfs_da3_icnode_hdr nodehdr;
1365
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001366 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001367 if (count)
1368 *count = nodehdr.count;
1369 if (!nodehdr.count)
1370 return 0;
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001371 return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374/*
1375 * Walk back up the tree adjusting hash values as necessary,
1376 * when we stop making changes, return.
1377 */
1378void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001379xfs_da3_fixhashpath(
1380 struct xfs_da_state *state,
1381 struct xfs_da_state_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001383 struct xfs_da_state_blk *blk;
1384 struct xfs_da_intnode *node;
1385 struct xfs_da_node_entry *btree;
1386 xfs_dahash_t lasthash=0;
1387 int level;
1388 int count;
Dave Chinner4bceb182013-10-29 22:11:51 +11001389 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Dave Chinneree732592012-11-12 22:53:53 +11001391 trace_xfs_da_fixhashpath(state->args);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 level = path->active-1;
1394 blk = &path->blk[ level ];
1395 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 case XFS_ATTR_LEAF_MAGIC:
1397 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1398 if (count == 0)
1399 return;
1400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 case XFS_DIR2_LEAFN_MAGIC:
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001402 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (count == 0)
1404 return;
1405 break;
1406 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001407 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (count == 0)
1409 return;
1410 break;
1411 }
1412 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001413 struct xfs_da3_icnode_hdr nodehdr;
1414
Dave Chinner1d9025e2012-06-22 18:50:14 +10001415 node = blk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001416 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001417 btree = nodehdr.btree;
Mark Tinguelyc88547a2014-04-04 07:10:49 +11001418 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 blk->hashval = lasthash;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001421 btree[blk->index].hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001422 xfs_trans_log_buf(state->args->trans, blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001423 XFS_DA_LOGRANGE(node, &btree[blk->index],
1424 sizeof(*btree)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001426 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428}
1429
1430/*
1431 * Remove an entry from an intermediate node.
1432 */
1433STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001434xfs_da3_node_remove(
1435 struct xfs_da_state *state,
1436 struct xfs_da_state_blk *drop_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001438 struct xfs_da_intnode *node;
1439 struct xfs_da3_icnode_hdr nodehdr;
1440 struct xfs_da_node_entry *btree;
1441 int index;
1442 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001443 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Dave Chinner5a5881c2012-03-22 05:15:13 +00001445 trace_xfs_da_node_remove(state->args);
1446
Dave Chinner1d9025e2012-06-22 18:50:14 +10001447 node = drop_blk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001448 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001449 ASSERT(drop_blk->index < nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 ASSERT(drop_blk->index >= 0);
1451
1452 /*
1453 * Copy over the offending entry, or just zero it out.
1454 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001455 index = drop_blk->index;
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001456 btree = nodehdr.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001457 if (index < nodehdr.count - 1) {
1458 tmp = nodehdr.count - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001460 memmove(&btree[index], &btree[index + 1], tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001461 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001462 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1463 index = nodehdr.count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001465 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001466 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001467 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1468 nodehdr.count -= 1;
Christoph Hellwige1c8af12019-11-08 14:57:48 -08001469 xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001470 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Christoph Hellwig3b344412019-11-08 14:57:49 -08001471 XFS_DA_LOGRANGE(node, &node->hdr, state->args->geo->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /*
1474 * Copy the last hash value from the block to propagate upwards.
1475 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001476 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001480 * Unbalance the elements between two intermediate nodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 * move all Btree elements from one node into another.
1482 */
1483STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001484xfs_da3_node_unbalance(
1485 struct xfs_da_state *state,
1486 struct xfs_da_state_blk *drop_blk,
1487 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001489 struct xfs_da_intnode *drop_node;
1490 struct xfs_da_intnode *save_node;
1491 struct xfs_da_node_entry *drop_btree;
1492 struct xfs_da_node_entry *save_btree;
1493 struct xfs_da3_icnode_hdr drop_hdr;
1494 struct xfs_da3_icnode_hdr save_hdr;
1495 struct xfs_trans *tp;
1496 int sindex;
1497 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001498 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Dave Chinner5a5881c2012-03-22 05:15:13 +00001500 trace_xfs_da_node_unbalance(state->args);
1501
Dave Chinner1d9025e2012-06-22 18:50:14 +10001502 drop_node = drop_blk->bp->b_addr;
1503 save_node = save_blk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001504 xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
1505 xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001506 drop_btree = drop_hdr.btree;
1507 save_btree = save_hdr.btree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 tp = state->args->trans;
1509
1510 /*
1511 * If the dying block has lower hashvals, then move all the
1512 * elements in the remaining block up to make a hole.
1513 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001514 if ((be32_to_cpu(drop_btree[0].hashval) <
1515 be32_to_cpu(save_btree[0].hashval)) ||
1516 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1517 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1518 /* XXX: check this - is memmove dst correct? */
1519 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1520 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1521
1522 sindex = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001523 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001524 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1525 (save_hdr.count + drop_hdr.count) *
1526 sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001528 sindex = save_hdr.count;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001529 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001530 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1531 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533
1534 /*
1535 * Move all the B-tree elements from drop_blk to save_blk.
1536 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001537 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1538 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1539 save_hdr.count += drop_hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Christoph Hellwige1c8af12019-11-08 14:57:48 -08001541 xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001542 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
Christoph Hellwig3b344412019-11-08 14:57:49 -08001544 state->args->geo->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 /*
1547 * Save the last hashval in the remaining block for upward propagation.
1548 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001549 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/*========================================================================
1553 * Routines used for finding things in the Btree.
1554 *========================================================================*/
1555
1556/*
1557 * Walk down the Btree looking for a particular filename, filling
1558 * in the state structure as we go.
1559 *
1560 * We will set the state structure to point to each of the elements
1561 * in each of the nodes where either the hashval is or should be.
1562 *
1563 * We support duplicate hashval's so for each entry in the current
1564 * node that could contain the desired hashval, descend. This is a
1565 * pruned depth-first tree search.
1566 */
1567int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001568xfs_da3_node_lookup_int(
1569 struct xfs_da_state *state,
1570 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001572 struct xfs_da_state_blk *blk;
1573 struct xfs_da_blkinfo *curr;
1574 struct xfs_da_intnode *node;
1575 struct xfs_da_node_entry *btree;
1576 struct xfs_da3_icnode_hdr nodehdr;
1577 struct xfs_da_args *args;
1578 xfs_dablk_t blkno;
1579 xfs_dahash_t hashval;
1580 xfs_dahash_t btreehashval;
1581 int probe;
1582 int span;
1583 int max;
1584 int error;
1585 int retval;
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001586 unsigned int expected_level = 0;
Darrick J. Wong611995d2018-08-01 07:40:48 -07001587 uint16_t magic;
Dave Chinner4bceb182013-10-29 22:11:51 +11001588 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 args = state->args;
1591
1592 /*
1593 * Descend thru the B-tree searching each level for the right
1594 * node to use, until the right hashval is found.
1595 */
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001596 blkno = args->geo->leafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 for (blk = &state->path.blk[0], state->path.active = 1;
1598 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1599 blk++, state->path.active++) {
1600 /*
1601 * Read the next node down in the tree.
1602 */
1603 blk->blkno = blkno;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001604 error = xfs_da3_node_read(args->trans, args->dp, blkno,
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001605 &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (error) {
1607 blk->blkno = 0;
1608 state->path.active--;
Eric Sandeend99831f2014-06-22 15:03:54 +10001609 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001611 curr = blk->bp->b_addr;
Darrick J. Wong611995d2018-08-01 07:40:48 -07001612 magic = be16_to_cpu(curr->magic);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001613
Darrick J. Wong611995d2018-08-01 07:40:48 -07001614 if (magic == XFS_ATTR_LEAF_MAGIC ||
1615 magic == XFS_ATTR3_LEAF_MAGIC) {
Dave Chinner517c2222013-04-24 18:58:55 +10001616 blk->magic = XFS_ATTR_LEAF_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001617 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1618 break;
1619 }
1620
Darrick J. Wong611995d2018-08-01 07:40:48 -07001621 if (magic == XFS_DIR2_LEAFN_MAGIC ||
1622 magic == XFS_DIR3_LEAFN_MAGIC) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001623 blk->magic = XFS_DIR2_LEAFN_MAGIC;
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001624 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1625 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001626 break;
1627 }
1628
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001629 if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001630 xfs_buf_mark_corrupt(blk->bp);
Darrick J. Wong56830d62018-08-01 07:40:48 -07001631 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001632 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001633
Darrick J. Wong56830d62018-08-01 07:40:48 -07001634 blk->magic = XFS_DA_NODE_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 /*
1637 * Search an intermediate node for a match.
1638 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001639 node = blk->bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001640 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001641 btree = nodehdr.btree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001643 /* Tree taller than we can handle; bail out! */
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001644 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001645 xfs_buf_mark_corrupt(blk->bp);
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001646 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001647 }
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001648
1649 /* Check the level from the root. */
1650 if (blkno == args->geo->leafblk)
1651 expected_level = nodehdr.level - 1;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001652 else if (expected_level != nodehdr.level) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -07001653 xfs_buf_mark_corrupt(blk->bp);
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001654 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -07001655 } else
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001656 expected_level--;
1657
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001658 max = nodehdr.count;
1659 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001661 /*
1662 * Binary search. (note: small blocks will skip loop)
1663 */
1664 probe = span = max / 2;
1665 hashval = args->hashval;
1666 while (span > 4) {
1667 span /= 2;
1668 btreehashval = be32_to_cpu(btree[probe].hashval);
1669 if (btreehashval < hashval)
1670 probe += span;
1671 else if (btreehashval > hashval)
1672 probe -= span;
1673 else
1674 break;
1675 }
1676 ASSERT((probe >= 0) && (probe < max));
1677 ASSERT((span <= 4) ||
1678 (be32_to_cpu(btree[probe].hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001680 /*
1681 * Since we may have duplicate hashval's, find the first
1682 * matching hashval in the node.
1683 */
1684 while (probe > 0 &&
1685 be32_to_cpu(btree[probe].hashval) >= hashval) {
1686 probe--;
1687 }
1688 while (probe < max &&
1689 be32_to_cpu(btree[probe].hashval) < hashval) {
1690 probe++;
1691 }
1692
1693 /*
1694 * Pick the right block to descend on.
1695 */
1696 if (probe == max) {
1697 blk->index = max - 1;
1698 blkno = be32_to_cpu(btree[max - 1].before);
1699 } else {
1700 blk->index = probe;
1701 blkno = be32_to_cpu(btree[probe].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001703
1704 /* We can't point back to the root. */
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001705 if (XFS_IS_CORRUPT(dp->i_mount, blkno == args->geo->leafblk))
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001706 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001709 if (XFS_IS_CORRUPT(dp->i_mount, expected_level != 0))
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001710 return -EFSCORRUPTED;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
1713 * A leaf block that ends in the hashval that we are interested in
1714 * (final hashval == search hashval) means that the next block may
1715 * contain more entries with the same hashval, shift upward to the
1716 * next leaf and keep searching.
1717 */
1718 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001719 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1721 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001722 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Dave Chinner517c2222013-04-24 18:58:55 +10001723 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 blk->index = args->index;
1725 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001726 } else {
1727 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001728 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Dave Chinner24513372014-06-25 14:58:08 +10001730 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 (blk->hashval == args->hashval)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001732 error = xfs_da3_path_shift(state, &state->path, 1, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 &retval);
1734 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001735 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (retval == 0) {
1737 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001738 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* path_shift() gives ENOENT */
Dave Chinner24513372014-06-25 14:58:08 +10001740 retval = -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743 break;
1744 }
1745 *result = retval;
Eric Sandeend99831f2014-06-22 15:03:54 +10001746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749/*========================================================================
1750 * Utility routines.
1751 *========================================================================*/
1752
1753/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001754 * Compare two intermediate nodes for "order".
1755 */
1756STATIC int
1757xfs_da3_node_order(
Dave Chinner4bceb182013-10-29 22:11:51 +11001758 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001759 struct xfs_buf *node1_bp,
1760 struct xfs_buf *node2_bp)
1761{
1762 struct xfs_da_intnode *node1;
1763 struct xfs_da_intnode *node2;
1764 struct xfs_da_node_entry *btree1;
1765 struct xfs_da_node_entry *btree2;
1766 struct xfs_da3_icnode_hdr node1hdr;
1767 struct xfs_da3_icnode_hdr node2hdr;
1768
1769 node1 = node1_bp->b_addr;
1770 node2 = node2_bp->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08001771 xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
1772 xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001773 btree1 = node1hdr.btree;
1774 btree2 = node2hdr.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001775
1776 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1777 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1778 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1779 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1780 return 1;
1781 }
1782 return 0;
1783}
1784
1785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * Link a new block into a doubly linked list of blocks (of whatever type).
1787 */
1788int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001789xfs_da3_blk_link(
1790 struct xfs_da_state *state,
1791 struct xfs_da_state_blk *old_blk,
1792 struct xfs_da_state_blk *new_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001794 struct xfs_da_blkinfo *old_info;
1795 struct xfs_da_blkinfo *new_info;
1796 struct xfs_da_blkinfo *tmp_info;
1797 struct xfs_da_args *args;
1798 struct xfs_buf *bp;
1799 int before = 0;
1800 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001801 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /*
1804 * Set up environment.
1805 */
1806 args = state->args;
1807 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001808 old_info = old_blk->bp->b_addr;
1809 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001811 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 case XFS_ATTR_LEAF_MAGIC:
1816 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001819 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001822 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 break;
1824 }
1825
1826 /*
1827 * Link blocks in appropriate order.
1828 */
1829 if (before) {
1830 /*
1831 * Link new block in before existing block.
1832 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001833 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001834 new_info->forw = cpu_to_be32(old_blk->blkno);
1835 new_info->back = old_info->back;
1836 if (old_info->back) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001837 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001838 be32_to_cpu(old_info->back),
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001839 &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001841 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001843 tmp_info = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001844 ASSERT(tmp_info->magic == old_info->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001845 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1846 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001847 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Nathan Scott89da0542006-03-17 17:28:40 +11001849 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 } else {
1851 /*
1852 * Link new block in after existing block.
1853 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001854 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001855 new_info->forw = old_info->forw;
1856 new_info->back = cpu_to_be32(old_blk->blkno);
1857 if (old_info->forw) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001858 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001859 be32_to_cpu(old_info->forw),
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001860 &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001862 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001864 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001865 ASSERT(tmp_info->magic == old_info->magic);
1866 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1867 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001868 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Nathan Scott89da0542006-03-17 17:28:40 +11001870 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
Dave Chinner1d9025e2012-06-22 18:50:14 +10001873 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1874 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Eric Sandeend99831f2014-06-22 15:03:54 +10001875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * Unlink a block from a doubly linked list of blocks.
1880 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001881STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001882xfs_da3_blk_unlink(
1883 struct xfs_da_state *state,
1884 struct xfs_da_state_blk *drop_blk,
1885 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001887 struct xfs_da_blkinfo *drop_info;
1888 struct xfs_da_blkinfo *save_info;
1889 struct xfs_da_blkinfo *tmp_info;
1890 struct xfs_da_args *args;
1891 struct xfs_buf *bp;
1892 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 /*
1895 * Set up environment.
1896 */
1897 args = state->args;
1898 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001899 save_info = save_blk->bp->b_addr;
1900 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001902 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001905 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1906 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1907 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1908 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /*
1911 * Unlink the leaf block from the doubly linked chain of leaves.
1912 */
Nathan Scott89da0542006-03-17 17:28:40 +11001913 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001914 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001915 save_info->back = drop_info->back;
1916 if (drop_info->back) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001917 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001918 be32_to_cpu(drop_info->back),
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001919 &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001921 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001923 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001924 ASSERT(tmp_info->magic == save_info->magic);
1925 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1926 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001927 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001931 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001932 save_info->forw = drop_info->forw;
1933 if (drop_info->forw) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001934 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001935 be32_to_cpu(drop_info->forw),
Christoph Hellwig02c57f02019-11-20 09:46:04 -08001936 &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001938 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001940 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001941 ASSERT(tmp_info->magic == save_info->magic);
1942 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1943 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001944 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 }
1948
Dave Chinner1d9025e2012-06-22 18:50:14 +10001949 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Eric Sandeend99831f2014-06-22 15:03:54 +10001950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953/*
1954 * Move a path "forward" or "!forward" one block at the current level.
1955 *
1956 * This routine will adjust a "path" to point to the next block
1957 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1958 * Btree, including updating pointers to the intermediate nodes between
1959 * the new bottom and the root.
1960 */
1961int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001962xfs_da3_path_shift(
1963 struct xfs_da_state *state,
1964 struct xfs_da_state_path *path,
1965 int forward,
1966 int release,
1967 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001969 struct xfs_da_state_blk *blk;
1970 struct xfs_da_blkinfo *info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001971 struct xfs_da_args *args;
1972 struct xfs_da_node_entry *btree;
1973 struct xfs_da3_icnode_hdr nodehdr;
Brian Foster7df1c172015-08-19 10:32:33 +10001974 struct xfs_buf *bp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001975 xfs_dablk_t blkno = 0;
1976 int level;
1977 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001978 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Dave Chinneree732592012-11-12 22:53:53 +11001980 trace_xfs_da_path_shift(state->args);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * Roll up the Btree looking for the first block where our
1984 * current index is not at the edge of the block. Note that
1985 * we skip the bottom layer because we want the sibling block.
1986 */
1987 args = state->args;
1988 ASSERT(args != NULL);
1989 ASSERT(path != NULL);
1990 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1991 level = (path->active-1) - 1; /* skip bottom layer in path */
Qian Cai4982bff2020-02-26 09:36:44 -08001992 for (; level >= 0; level--) {
1993 blk = &path->blk[level];
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001994 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
1995 blk->bp->b_addr);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001996
1997 if (forward && (blk->index < nodehdr.count - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 blk->index++;
Christoph Hellwig51908ca2019-11-08 14:57:48 -08001999 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 break;
2001 } else if (!forward && (blk->index > 0)) {
2002 blk->index--;
Christoph Hellwig51908ca2019-11-08 14:57:48 -08002003 blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
2005 }
2006 }
2007 if (level < 0) {
Dave Chinner24513372014-06-25 14:58:08 +10002008 *result = -ENOENT; /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10002009 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Eric Sandeend99831f2014-06-22 15:03:54 +10002010 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 /*
2014 * Roll down the edge of the subtree until we reach the
2015 * same depth we were at originally.
2016 */
2017 for (blk++, level++; level < path->active; blk++, level++) {
2018 /*
Brian Foster7df1c172015-08-19 10:32:33 +10002019 * Read the next child block into a local buffer.
2020 */
Christoph Hellwig02c57f02019-11-20 09:46:04 -08002021 error = xfs_da3_node_read(args->trans, dp, blkno, &bp,
Brian Foster7df1c172015-08-19 10:32:33 +10002022 args->whichfork);
2023 if (error)
2024 return error;
2025
2026 /*
2027 * Release the old block (if it's dirty, the trans doesn't
2028 * actually let go) and swap the local buffer into the path
2029 * structure. This ensures failure of the above read doesn't set
2030 * a NULL buffer in an active slot in the path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 */
2032 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002033 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 blk->blkno = blkno;
Brian Foster7df1c172015-08-19 10:32:33 +10002035 blk->bp = bp;
2036
Dave Chinner1d9025e2012-06-22 18:50:14 +10002037 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002038 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002039 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002040 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10002041 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10002042 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
2043 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002044
2045
2046 /*
2047 * Note: we flatten the magic number to a single type so we
2048 * don't have to compare against crc/non-crc types elsewhere.
2049 */
2050 switch (be16_to_cpu(info->magic)) {
2051 case XFS_DA_NODE_MAGIC:
2052 case XFS_DA3_NODE_MAGIC:
2053 blk->magic = XFS_DA_NODE_MAGIC;
Christoph Hellwig51908ca2019-11-08 14:57:48 -08002054 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2055 bp->b_addr);
2056 btree = nodehdr.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002057 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (forward)
2059 blk->index = 0;
2060 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002061 blk->index = nodehdr.count - 1;
2062 blkno = be32_to_cpu(btree[blk->index].before);
2063 break;
2064 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10002065 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002066 blk->magic = XFS_ATTR_LEAF_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 ASSERT(level == path->active-1);
2068 blk->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11002069 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002070 break;
2071 case XFS_DIR2_LEAFN_MAGIC:
2072 case XFS_DIR3_LEAFN_MAGIC:
2073 blk->magic = XFS_DIR2_LEAFN_MAGIC;
2074 ASSERT(level == path->active-1);
2075 blk->index = 0;
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07002076 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
2077 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002078 break;
2079 default:
2080 ASSERT(0);
2081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083 }
2084 *result = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088
2089/*========================================================================
2090 * Utility routines.
2091 *========================================================================*/
2092
2093/*
2094 * Implement a simple hash on a character string.
2095 * Rotate the hash value by 7 bits, then XOR each character in.
2096 * This is implemented with some source-level loop unrolling.
2097 */
2098xfs_dahash_t
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002099xfs_da_hashname(const uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
2101 xfs_dahash_t hash;
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * Do four characters at a time as long as we can.
2105 */
2106 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2107 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2108 (name[3] << 0) ^ rol32(hash, 7 * 4);
2109
2110 /*
2111 * Now do the rest of the characters.
2112 */
2113 switch (namelen) {
2114 case 3:
2115 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2116 rol32(hash, 7 * 3);
2117 case 2:
2118 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2119 case 1:
2120 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11002121 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return hash;
2123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Barry Naujok5163f952008-05-21 16:41:01 +10002126enum xfs_dacmp
2127xfs_da_compname(
2128 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11002129 const unsigned char *name,
2130 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10002131{
2132 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2133 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136int
Christoph Hellwig77936d02011-07-13 13:43:49 +02002137xfs_da_grow_inode_int(
2138 struct xfs_da_args *args,
2139 xfs_fileoff_t *bno,
2140 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Christoph Hellwig77936d02011-07-13 13:43:49 +02002142 struct xfs_trans *tp = args->trans;
2143 struct xfs_inode *dp = args->dp;
2144 int w = args->whichfork;
Christoph Hellwig6e73a542021-03-29 11:11:40 -07002145 xfs_rfsblock_t nblks = dp->i_nblocks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002146 struct xfs_bmbt_irec map, *mapp;
2147 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /*
2150 * Find a spot in the file space to put the new block.
2151 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02002152 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2153 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 /*
2157 * Try mapping it in one filesystem block.
2158 */
2159 nmap = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002160 error = xfs_bmapi_write(tp, dp, *bno, count,
2161 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Brian Fostera7beabe2018-07-11 22:26:25 -07002162 args->total, &map, &nmap);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002163 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 ASSERT(nmap <= 1);
2167 if (nmap == 1) {
2168 mapp = &map;
2169 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002170 } else if (nmap == 0 && count > 1) {
2171 xfs_fileoff_t b;
2172 int c;
2173
2174 /*
2175 * If we didn't get it and the block might work if fragmented,
2176 * try without the CONTIG flag. Loop until we get it all.
2177 */
Tetsuo Handa707e0dd2019-08-26 12:06:22 -07002178 mapp = kmem_alloc(sizeof(*mapp) * count, 0);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002179 for (b = *bno, mapi = 0; b < *bno + count; ) {
Dave Chinner9bb54cb2018-06-07 07:54:02 -07002180 nmap = min(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002181 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002182 error = xfs_bmapi_write(tp, dp, b, c,
2183 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Brian Fostera7beabe2018-07-11 22:26:25 -07002184 args->total, &mapp[mapi], &nmap);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002185 if (error)
2186 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (nmap < 1)
2188 break;
2189 mapi += nmap;
2190 b = mapp[mapi - 1].br_startoff +
2191 mapp[mapi - 1].br_blockcount;
2192 }
2193 } else {
2194 mapi = 0;
2195 mapp = NULL;
2196 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * Count the blocks we got, make sure it matches the total.
2200 */
2201 for (i = 0, got = 0; i < mapi; i++)
2202 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002203 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02002205 *bno + count) {
Dave Chinner24513372014-06-25 14:58:08 +10002206 error = -ENOSPC;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002207 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002209
David Chinnera7444052008-10-30 17:38:12 +11002210 /* account for newly allocated blocks in reserved blocks total */
Christoph Hellwig6e73a542021-03-29 11:11:40 -07002211 args->total -= dp->i_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002212
2213out_free_map:
2214 if (mapp != &map)
2215 kmem_free(mapp);
2216 return error;
2217}
2218
2219/*
2220 * Add a block to the btree ahead of the file.
2221 * Return the new block number to the caller.
2222 */
2223int
2224xfs_da_grow_inode(
2225 struct xfs_da_args *args,
2226 xfs_dablk_t *new_blkno)
2227{
2228 xfs_fileoff_t bno;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002229 int error;
2230
Dave Chinner5a5881c2012-03-22 05:15:13 +00002231 trace_xfs_da_grow_inode(args);
2232
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002233 bno = args->geo->leafblk;
2234 error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002235 if (!error)
2236 *new_blkno = (xfs_dablk_t)bno;
2237 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240/*
2241 * Ick. We need to always be able to remove a btree block, even
2242 * if there's no space reservation because the filesystem is full.
2243 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2244 * It swaps the target block with the last block in the file. The
2245 * last block in the file can always be removed since it can't cause
2246 * a bmap btree split to do that.
2247 */
2248STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002249xfs_da3_swap_lastblock(
2250 struct xfs_da_args *args,
2251 xfs_dablk_t *dead_blknop,
2252 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002254 struct xfs_da_blkinfo *dead_info;
2255 struct xfs_da_blkinfo *sib_info;
2256 struct xfs_da_intnode *par_node;
2257 struct xfs_da_intnode *dead_node;
2258 struct xfs_dir2_leaf *dead_leaf2;
2259 struct xfs_da_node_entry *btree;
2260 struct xfs_da3_icnode_hdr par_hdr;
Dave Chinner4bceb182013-10-29 22:11:51 +11002261 struct xfs_inode *dp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002262 struct xfs_trans *tp;
2263 struct xfs_mount *mp;
2264 struct xfs_buf *dead_buf;
2265 struct xfs_buf *last_buf;
2266 struct xfs_buf *sib_buf;
2267 struct xfs_buf *par_buf;
2268 xfs_dahash_t dead_hash;
2269 xfs_fileoff_t lastoff;
2270 xfs_dablk_t dead_blkno;
2271 xfs_dablk_t last_blkno;
2272 xfs_dablk_t sib_blkno;
2273 xfs_dablk_t par_blkno;
2274 int error;
2275 int w;
2276 int entno;
2277 int level;
2278 int dead_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Dave Chinner5a5881c2012-03-22 05:15:13 +00002280 trace_xfs_da_swap_lastblock(args);
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 dead_buf = *dead_bufp;
2283 dead_blkno = *dead_blknop;
2284 tp = args->trans;
Dave Chinner4bceb182013-10-29 22:11:51 +11002285 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 w = args->whichfork;
2287 ASSERT(w == XFS_DATA_FORK);
Dave Chinner4bceb182013-10-29 22:11:51 +11002288 mp = dp->i_mount;
Dave Chinner7dda6e82014-06-06 15:11:18 +10002289 lastoff = args->geo->freeblk;
Dave Chinner4bceb182013-10-29 22:11:51 +11002290 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (error)
2292 return error;
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002293 if (XFS_IS_CORRUPT(mp, lastoff == 0))
Dave Chinner24513372014-06-25 14:58:08 +10002294 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /*
2296 * Read the last block in the btree space.
2297 */
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002298 last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
Christoph Hellwig02c57f02019-11-20 09:46:04 -08002299 error = xfs_da3_node_read(tp, dp, last_blkno, &last_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002300 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return error;
2302 /*
2303 * Copy the last block into the dead buffer and log it.
2304 */
Dave Chinner8f661932014-06-06 15:15:59 +10002305 memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2306 xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002307 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /*
2309 * Get values from the moved block.
2310 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002311 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2312 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2313 struct xfs_dir3_icleaf_hdr leafhdr;
2314 struct xfs_dir2_leaf_entry *ents;
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
Christoph Hellwig51842552019-11-08 14:57:49 -08002317 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr,
2318 dead_leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08002319 ents = leafhdr.ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 dead_level = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10002321 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002323 struct xfs_da3_icnode_hdr deadhdr;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 dead_node = (xfs_da_intnode_t *)dead_info;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08002326 xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
Christoph Hellwig51908ca2019-11-08 14:57:48 -08002327 btree = deadhdr.btree;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002328 dead_level = deadhdr.level;
2329 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 sib_buf = par_buf = NULL;
2332 /*
2333 * If the moved block has a left sibling, fix up the pointers.
2334 */
Nathan Scott89da0542006-03-17 17:28:40 +11002335 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08002336 error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002337 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002339 sib_info = sib_buf->b_addr;
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002340 if (XFS_IS_CORRUPT(mp,
2341 be32_to_cpu(sib_info->forw) != last_blkno ||
2342 sib_info->magic != dead_info->magic)) {
Dave Chinner24513372014-06-25 14:58:08 +10002343 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 goto done;
2345 }
Nathan Scott89da0542006-03-17 17:28:40 +11002346 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002347 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2349 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 sib_buf = NULL;
2351 }
2352 /*
2353 * If the moved block has a right sibling, fix up the pointers.
2354 */
Nathan Scott89da0542006-03-17 17:28:40 +11002355 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08002356 error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002357 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002359 sib_info = sib_buf->b_addr;
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002360 if (XFS_IS_CORRUPT(mp,
2361 be32_to_cpu(sib_info->back) != last_blkno ||
2362 sib_info->magic != dead_info->magic)) {
Dave Chinner24513372014-06-25 14:58:08 +10002363 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto done;
2365 }
Nathan Scott89da0542006-03-17 17:28:40 +11002366 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002367 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2369 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 sib_buf = NULL;
2371 }
Dave Chinner7dda6e82014-06-06 15:11:18 +10002372 par_blkno = args->geo->leafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 level = -1;
2374 /*
2375 * Walk down the tree looking for the parent of the moved block.
2376 */
2377 for (;;) {
Christoph Hellwig02c57f02019-11-20 09:46:04 -08002378 error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002379 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002381 par_node = par_buf->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08002382 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002383 if (XFS_IS_CORRUPT(mp,
2384 level >= 0 && level != par_hdr.level + 1)) {
Dave Chinner24513372014-06-25 14:58:08 +10002385 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 goto done;
2387 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002388 level = par_hdr.level;
Christoph Hellwig51908ca2019-11-08 14:57:48 -08002389 btree = par_hdr.btree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 for (entno = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002391 entno < par_hdr.count &&
2392 be32_to_cpu(btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 entno++)
2394 continue;
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002395 if (XFS_IS_CORRUPT(mp, entno == par_hdr.count)) {
Dave Chinner24513372014-06-25 14:58:08 +10002396 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 goto done;
2398 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002399 par_blkno = be32_to_cpu(btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (level == dead_level + 1)
2401 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002402 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 par_buf = NULL;
2404 }
2405 /*
2406 * We're in the right parent block.
2407 * Look for the right entry.
2408 */
2409 for (;;) {
2410 for (;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002411 entno < par_hdr.count &&
2412 be32_to_cpu(btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 entno++)
2414 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002415 if (entno < par_hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002417 par_blkno = par_hdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002418 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 par_buf = NULL;
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002420 if (XFS_IS_CORRUPT(mp, par_blkno == 0)) {
Dave Chinner24513372014-06-25 14:58:08 +10002421 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto done;
2423 }
Christoph Hellwig02c57f02019-11-20 09:46:04 -08002424 error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002425 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002427 par_node = par_buf->b_addr;
Christoph Hellwigf475dc4d2019-11-08 14:53:00 -08002428 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
Darrick J. Wonga71895c2019-11-11 12:53:22 -08002429 if (XFS_IS_CORRUPT(mp, par_hdr.level != level)) {
Dave Chinner24513372014-06-25 14:58:08 +10002430 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto done;
2432 }
Christoph Hellwig51908ca2019-11-08 14:57:48 -08002433 btree = par_hdr.btree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 entno = 0;
2435 }
2436 /*
2437 * Update the parent entry pointing to the moved block.
2438 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002439 btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002440 xfs_trans_log_buf(tp, par_buf,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002441 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2442 sizeof(btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 *dead_blknop = last_blkno;
2444 *dead_bufp = last_buf;
2445 return 0;
2446done:
2447 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002448 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002450 xfs_trans_brelse(tp, sib_buf);
2451 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return error;
2453}
2454
2455/*
2456 * Remove a btree block from a directory or attribute.
2457 */
2458int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002459xfs_da_shrink_inode(
Brian Foster32a9b7c2018-07-11 22:26:11 -07002460 struct xfs_da_args *args,
2461 xfs_dablk_t dead_blkno,
2462 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Brian Foster32a9b7c2018-07-11 22:26:11 -07002464 struct xfs_inode *dp;
2465 int done, error, w, count;
2466 struct xfs_trans *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Dave Chinner5a5881c2012-03-22 05:15:13 +00002468 trace_xfs_da_shrink_inode(args);
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 dp = args->dp;
2471 w = args->whichfork;
2472 tp = args->trans;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002473 count = args->geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 for (;;) {
2475 /*
2476 * Remove extents. If we get ENOSPC for a dir we have to move
2477 * the last block to the place we want to kill.
2478 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002479 error = xfs_bunmapi(tp, dp, dead_blkno, count,
Brian Foster2af52842018-07-11 22:26:25 -07002480 xfs_bmapi_aflag(w), 0, &done);
Dave Chinner24513372014-06-25 14:58:08 +10002481 if (error == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002483 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002484 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2485 &dead_buf);
2486 if (error)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002487 break;
2488 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
2491 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002492 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return error;
2494}
2495
Dave Chinner36054312012-06-22 18:50:13 +10002496static int
2497xfs_dabuf_map(
Dave Chinner36054312012-06-22 18:50:13 +10002498 struct xfs_inode *dp,
2499 xfs_dablk_t bno,
Christoph Hellwig199e9ba2019-11-20 10:18:50 -08002500 unsigned int flags,
Dave Chinner36054312012-06-22 18:50:13 +10002501 int whichfork,
Christoph Hellwig45feef82019-11-20 10:18:44 -08002502 struct xfs_buf_map **mapp,
Dave Chinner36054312012-06-22 18:50:13 +10002503 int *nmaps)
2504{
2505 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002506 int nfsb = xfs_dabuf_nfsb(mp, whichfork);
Christoph Hellwig45feef82019-11-20 10:18:44 -08002507 struct xfs_bmbt_irec irec, *irecs = &irec;
2508 struct xfs_buf_map *map = *mapp;
2509 xfs_fileoff_t off = bno;
2510 int error = 0, nirecs, i;
Dave Chinner36054312012-06-22 18:50:13 +10002511
Christoph Hellwig45feef82019-11-20 10:18:44 -08002512 if (nfsb > 1)
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002513 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_NOFS);
Christoph Hellwig45feef82019-11-20 10:18:44 -08002514
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002515 nirecs = nfsb;
Christoph Hellwig45feef82019-11-20 10:18:44 -08002516 error = xfs_bmapi_read(dp, bno, nfsb, irecs, &nirecs,
2517 xfs_bmapi_aflag(whichfork));
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002518 if (error)
Christoph Hellwig45feef82019-11-20 10:18:44 -08002519 goto out_free_irecs;
Dave Chinner36054312012-06-22 18:50:13 +10002520
Christoph Hellwig45feef82019-11-20 10:18:44 -08002521 /*
2522 * Use the caller provided map for the single map case, else allocate a
2523 * larger one that needs to be free by the caller.
2524 */
2525 if (nirecs > 1) {
2526 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_NOFS);
Darrick J. Wongfaf8ee82020-03-13 13:17:40 -07002527 if (!map) {
2528 error = -ENOMEM;
Christoph Hellwig45feef82019-11-20 10:18:44 -08002529 goto out_free_irecs;
Darrick J. Wongfaf8ee82020-03-13 13:17:40 -07002530 }
Christoph Hellwig45feef82019-11-20 10:18:44 -08002531 *mapp = map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Christoph Hellwig45feef82019-11-20 10:18:44 -08002533
2534 for (i = 0; i < nirecs; i++) {
2535 if (irecs[i].br_startblock == HOLESTARTBLOCK ||
2536 irecs[i].br_startblock == DELAYSTARTBLOCK)
2537 goto invalid_mapping;
2538 if (off != irecs[i].br_startoff)
2539 goto invalid_mapping;
2540
2541 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2542 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2543 off += irecs[i].br_blockcount;
2544 }
2545
2546 if (off != bno + nfsb)
2547 goto invalid_mapping;
2548
2549 *nmaps = nirecs;
2550out_free_irecs:
Dave Chinner36054312012-06-22 18:50:13 +10002551 if (irecs != &irec)
2552 kmem_free(irecs);
2553 return error;
Christoph Hellwig45feef82019-11-20 10:18:44 -08002554
2555invalid_mapping:
2556 /* Caller ok with no mapping. */
Christoph Hellwig199e9ba2019-11-20 10:18:50 -08002557 if (XFS_IS_CORRUPT(mp, !(flags & XFS_DABUF_MAP_HOLE_OK))) {
Christoph Hellwig45feef82019-11-20 10:18:44 -08002558 error = -EFSCORRUPTED;
2559 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2560 xfs_alert(mp, "%s: bno %u inode %llu",
2561 __func__, bno, dp->i_ino);
2562
2563 for (i = 0; i < nirecs; i++) {
2564 xfs_alert(mp,
2565"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2566 i, irecs[i].br_startoff,
2567 irecs[i].br_startblock,
2568 irecs[i].br_blockcount,
2569 irecs[i].br_state);
2570 }
2571 }
2572 } else {
2573 *nmaps = 0;
2574 }
2575 goto out_free_irecs;
Dave Chinner36054312012-06-22 18:50:13 +10002576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Dave Chinner36054312012-06-22 18:50:13 +10002578/*
2579 * Get a buffer for the dir/attr block.
2580 */
2581int
2582xfs_da_get_buf(
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002583 struct xfs_trans *tp,
Dave Chinner36054312012-06-22 18:50:13 +10002584 struct xfs_inode *dp,
2585 xfs_dablk_t bno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002586 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002587 int whichfork)
2588{
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002589 struct xfs_mount *mp = dp->i_mount;
Dave Chinner36054312012-06-22 18:50:13 +10002590 struct xfs_buf *bp;
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002591 struct xfs_buf_map map, *mapp = &map;
2592 int nmap = 1;
Dave Chinner36054312012-06-22 18:50:13 +10002593 int error;
2594
2595 *bpp = NULL;
Christoph Hellwig2911edb2019-11-20 09:46:05 -08002596 error = xfs_dabuf_map(dp, bno, 0, whichfork, &mapp, &nmap);
Christoph Hellwig199e9ba2019-11-20 10:18:50 -08002597 if (error || nmap == 0)
Dave Chinner36054312012-06-22 18:50:13 +10002598 goto out_free;
Dave Chinner36054312012-06-22 18:50:13 +10002599
Darrick J. Wong9676b542020-01-23 17:01:18 -08002600 error = xfs_trans_get_buf_map(tp, mp->m_ddev_targp, mapp, nmap, 0, &bp);
2601 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002602 goto out_free;
Dave Chinner36054312012-06-22 18:50:13 +10002603
Dave Chinner1d9025e2012-06-22 18:50:14 +10002604 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002605
2606out_free:
2607 if (mapp != &map)
2608 kmem_free(mapp);
2609
2610 return error;
2611}
2612
2613/*
2614 * Get a buffer for the dir/attr block, fill in the contents.
2615 */
2616int
2617xfs_da_read_buf(
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002618 struct xfs_trans *tp,
Dave Chinner36054312012-06-22 18:50:13 +10002619 struct xfs_inode *dp,
2620 xfs_dablk_t bno,
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -08002621 unsigned int flags,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002622 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002623 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002624 const struct xfs_buf_ops *ops)
Dave Chinner36054312012-06-22 18:50:13 +10002625{
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002626 struct xfs_mount *mp = dp->i_mount;
Dave Chinner36054312012-06-22 18:50:13 +10002627 struct xfs_buf *bp;
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002628 struct xfs_buf_map map, *mapp = &map;
2629 int nmap = 1;
Dave Chinner36054312012-06-22 18:50:13 +10002630 int error;
2631
2632 *bpp = NULL;
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -08002633 error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
Christoph Hellwig199e9ba2019-11-20 10:18:50 -08002634 if (error || !nmap)
Dave Chinner36054312012-06-22 18:50:13 +10002635 goto out_free;
Dave Chinner36054312012-06-22 18:50:13 +10002636
Christoph Hellwigfa0d44e2019-11-20 09:46:00 -08002637 error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
2638 &bp, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002639 if (error)
2640 goto out_free;
2641
2642 if (whichfork == XFS_ATTR_FORK)
2643 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 else
Dave Chinner36054312012-06-22 18:50:13 +10002645 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002646 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002647out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002649 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return error;
2652}
2653
2654/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 * Readahead the dir/attr block.
2656 */
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08002657int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002659 struct xfs_inode *dp,
2660 xfs_dablk_t bno,
Christoph Hellwig06566fd2019-11-20 09:46:02 -08002661 unsigned int flags,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002662 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002663 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Dave Chinner36054312012-06-22 18:50:13 +10002665 struct xfs_buf_map map;
2666 struct xfs_buf_map *mapp;
2667 int nmap;
2668 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Dave Chinner36054312012-06-22 18:50:13 +10002670 mapp = &map;
2671 nmap = 1;
Christoph Hellwig06566fd2019-11-20 09:46:02 -08002672 error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
Christoph Hellwig199e9ba2019-11-20 10:18:50 -08002673 if (error || !nmap)
Dave Chinner36054312012-06-22 18:50:13 +10002674 goto out_free;
Dave Chinner36054312012-06-22 18:50:13 +10002675
Dave Chinner1813dd62012-11-14 17:54:40 +11002676 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002677
2678out_free:
2679 if (mapp != &map)
2680 kmem_free(mapp);
2681
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08002682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}