blob: 8a301402bbc4e3bb6f54abfa0320a33cba1eddaa [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"
Dave Chinner57062782013-10-15 09:17:51 +110015#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110016#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100017#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020018#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_inode_item.h"
22#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_attr.h"
25#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000027#include "xfs_trace.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100028#include "xfs_cksum.h"
29#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110030#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * xfs_da_btree.c
34 *
35 * Routines to implement directories as Btrees of hashed names.
36 */
37
38/*========================================================================
39 * Function prototypes for the kernel.
40 *========================================================================*/
41
42/*
43 * Routines used for growing the Btree.
44 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100045STATIC int xfs_da3_root_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 xfs_da_state_blk_t *existing_root,
47 xfs_da_state_blk_t *new_child);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100048STATIC int xfs_da3_node_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 xfs_da_state_blk_t *existing_blk,
50 xfs_da_state_blk_t *split_blk,
51 xfs_da_state_blk_t *blk_to_add,
52 int treelevel,
53 int *result);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100054STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 xfs_da_state_blk_t *node_blk_1,
56 xfs_da_state_blk_t *node_blk_2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100057STATIC void xfs_da3_node_add(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 xfs_da_state_blk_t *old_node_blk,
59 xfs_da_state_blk_t *new_node_blk);
60
61/*
62 * Routines used for shrinking the Btree.
63 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100064STATIC int xfs_da3_root_join(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 xfs_da_state_blk_t *root_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100066STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
67STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 xfs_da_state_blk_t *drop_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100069STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 xfs_da_state_blk_t *src_node_blk,
71 xfs_da_state_blk_t *dst_node_blk);
72
73/*
74 * Utility routines.
75 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100076STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100077 xfs_da_state_blk_t *drop_blk,
78 xfs_da_state_blk_t *save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Dave Chinnerf5ea1102013-04-24 18:58:02 +100080
81kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
82
83/*
84 * Allocate a dir-state structure.
85 * We don't put them on the stack since they're large.
86 */
87xfs_da_state_t *
88xfs_da_state_alloc(void)
89{
90 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
91}
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 */
116 kmem_zone_free(xfs_da_state_zone, state);
117}
118
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800119static xfs_failaddr_t
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000120xfs_da3_node_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100121 struct xfs_buf *bp)
122{
123 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000124 struct xfs_da_intnode *hdr = bp->b_addr;
125 struct xfs_da3_icnode_hdr ichdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100126 const struct xfs_dir_ops *ops;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100127
Dave Chinner01ba43b2013-10-29 22:11:52 +1100128 ops = xfs_dir_get_ops(mp, NULL);
129
130 ops->node_hdr_from_disk(&ichdr, hdr);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000131
132 if (xfs_sb_version_hascrc(&mp->m_sb)) {
133 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
134
135 if (ichdr.magic != XFS_DA3_NODE_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800136 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000137
Eric Sandeence748ea2015-07-29 11:53:31 +1000138 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800139 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000140 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800141 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100142 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800143 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000144 } else {
145 if (ichdr.magic != XFS_DA_NODE_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800146 return __this_address;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100147 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000148 if (ichdr.level == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800149 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000150 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800151 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000152 if (ichdr.count == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800153 return __this_address;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100154
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000155 /*
156 * we don't know if the node is for and attribute or directory tree,
157 * so only fail if the count is outside both bounds
158 */
Dave Chinner7ab610f2014-06-06 15:20:02 +1000159 if (ichdr.count > mp->m_dir_geo->node_ents &&
160 ichdr.count > mp->m_attr_geo->node_ents)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800161 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000162
163 /* XXX: hash order check? */
164
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800165 return NULL;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100166}
167
168static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000169xfs_da3_node_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100170 struct xfs_buf *bp)
171{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000172 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800173 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000174 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800175 xfs_failaddr_t fa;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000176
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800177 fa = xfs_da3_node_verify(bp);
178 if (fa) {
179 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000180 return;
181 }
182
183 if (!xfs_sb_version_hascrc(&mp->m_sb))
184 return;
185
186 if (bip)
187 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
188
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100189 xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100190}
191
Dave Chinner1813dd62012-11-14 17:54:40 +1100192/*
193 * leaf/node format detection on trees is sketchy, so a node read can be done on
194 * leaf level blocks when detection identifies the tree as a node format tree
195 * incorrectly. In this case, we need to swap the verifier to match the correct
196 * format of the block being read.
197 */
Dave Chinner612cfbf2012-11-14 17:52:32 +1100198static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000199xfs_da3_node_read_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100200 struct xfs_buf *bp)
201{
Dave Chinnerd9392a42012-11-12 22:54:17 +1100202 struct xfs_da_blkinfo *info = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800203 xfs_failaddr_t fa;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100204
205 switch (be16_to_cpu(info->magic)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000206 case XFS_DA3_NODE_MAGIC:
Eric Sandeence5028c2014-02-27 15:23:10 +1100207 if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800208 xfs_verifier_error(bp, -EFSBADCRC,
209 __this_address);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000210 break;
Eric Sandeence5028c2014-02-27 15:23:10 +1100211 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000212 /* fall through */
Dave Chinnerd9392a42012-11-12 22:54:17 +1100213 case XFS_DA_NODE_MAGIC:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800214 fa = xfs_da3_node_verify(bp);
215 if (fa)
216 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000217 return;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100218 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner7ced60c2013-05-20 09:51:13 +1000219 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000220 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100221 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100222 return;
223 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +1000224 case XFS_DIR3_LEAFN_MAGIC:
225 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100226 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100227 return;
228 default:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800229 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100230 break;
231 }
Dave Chinnerd9392a42012-11-12 22:54:17 +1100232}
233
Darrick J. Wongb5572592018-01-08 10:51:08 -0800234/* Verify the structure of a da3 block. */
235static xfs_failaddr_t
236xfs_da3_node_verify_struct(
237 struct xfs_buf *bp)
238{
239 struct xfs_da_blkinfo *info = bp->b_addr;
240
241 switch (be16_to_cpu(info->magic)) {
242 case XFS_DA3_NODE_MAGIC:
243 case XFS_DA_NODE_MAGIC:
244 return xfs_da3_node_verify(bp);
245 case XFS_ATTR_LEAF_MAGIC:
246 case XFS_ATTR3_LEAF_MAGIC:
247 bp->b_ops = &xfs_attr3_leaf_buf_ops;
248 return bp->b_ops->verify_struct(bp);
249 case XFS_DIR2_LEAFN_MAGIC:
250 case XFS_DIR3_LEAFN_MAGIC:
251 bp->b_ops = &xfs_dir3_leafn_buf_ops;
252 return bp->b_ops->verify_struct(bp);
253 default:
254 return __this_address;
255 }
256}
257
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000258const struct xfs_buf_ops xfs_da3_node_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100259 .name = "xfs_da3_node",
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000260 .verify_read = xfs_da3_node_read_verify,
261 .verify_write = xfs_da3_node_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800262 .verify_struct = xfs_da3_node_verify_struct,
Dave Chinner1813dd62012-11-14 17:54:40 +1100263};
264
Dave Chinnerd9392a42012-11-12 22:54:17 +1100265int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000266xfs_da3_node_read(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100267 struct xfs_trans *tp,
268 struct xfs_inode *dp,
269 xfs_dablk_t bno,
270 xfs_daddr_t mappedbno,
271 struct xfs_buf **bpp,
272 int which_fork)
273{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100274 int err;
275
276 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000277 which_fork, &xfs_da3_node_buf_ops);
Darrick J. Wongcd87d862017-07-07 18:55:17 -0700278 if (!err && tp && *bpp) {
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100279 struct xfs_da_blkinfo *info = (*bpp)->b_addr;
280 int type;
281
282 switch (be16_to_cpu(info->magic)) {
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100283 case XFS_DA_NODE_MAGIC:
Dave Chinnercab09a82013-04-30 21:39:36 +1000284 case XFS_DA3_NODE_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100285 type = XFS_BLFT_DA_NODE_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100286 break;
287 case XFS_ATTR_LEAF_MAGIC:
288 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100289 type = XFS_BLFT_ATTR_LEAF_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100290 break;
291 case XFS_DIR2_LEAFN_MAGIC:
292 case XFS_DIR3_LEAFN_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100293 type = XFS_BLFT_DIR_LEAFN_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100294 break;
295 default:
Darrick J. Wong924cade2018-06-03 16:10:18 -0700296 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
Darrick J. Wong2551a532018-06-04 10:23:54 -0700297 tp->t_mountp, info, sizeof(*info));
Darrick J. Wong924cade2018-06-03 16:10:18 -0700298 xfs_trans_brelse(tp, *bpp);
299 *bpp = NULL;
300 return -EFSCORRUPTED;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100301 }
302 xfs_trans_buf_set_type(tp, *bpp, type);
303 }
304 return err;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*========================================================================
308 * Routines used for growing the Btree.
309 *========================================================================*/
310
311/*
312 * Create the initial contents of an intermediate node.
313 */
314int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000315xfs_da3_node_create(
316 struct xfs_da_args *args,
317 xfs_dablk_t blkno,
318 int level,
319 struct xfs_buf **bpp,
320 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000322 struct xfs_da_intnode *node;
323 struct xfs_trans *tp = args->trans;
324 struct xfs_mount *mp = tp->t_mountp;
325 struct xfs_da3_icnode_hdr ichdr = {0};
326 struct xfs_buf *bp;
327 int error;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100328 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Dave Chinner5a5881c2012-03-22 05:15:13 +0000330 trace_xfs_da_node_create(args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000331 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
Dave Chinner5a5881c2012-03-22 05:15:13 +0000332
Dave Chinner01ba43b2013-10-29 22:11:52 +1100333 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000335 return error;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100336 bp->b_ops = &xfs_da3_node_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100337 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000338 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000340 if (xfs_sb_version_hascrc(&mp->m_sb)) {
341 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
342
Brian Fostera45086e2015-10-12 15:59:25 +1100343 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000344 ichdr.magic = XFS_DA3_NODE_MAGIC;
345 hdr3->info.blkno = cpu_to_be64(bp->b_bn);
346 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000347 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000348 } else {
349 ichdr.magic = XFS_DA_NODE_MAGIC;
350 }
351 ichdr.level = level;
352
Dave Chinner01ba43b2013-10-29 22:11:52 +1100353 dp->d_ops->node_hdr_to_disk(node, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000354 xfs_trans_log_buf(tp, bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100355 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 *bpp = bp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/*
362 * Split a leaf node, rebalance, then possibly split
363 * intermediate nodes, rebalance, etc.
364 */
365int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000366xfs_da3_split(
367 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000369 struct xfs_da_state_blk *oldblk;
370 struct xfs_da_state_blk *newblk;
371 struct xfs_da_state_blk *addblk;
372 struct xfs_da_intnode *node;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000373 int max;
Dave Chinner836a94a2013-08-12 20:49:44 +1000374 int action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000375 int error;
376 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dave Chinner5a5881c2012-03-22 05:15:13 +0000378 trace_xfs_da_split(state->args);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * Walk back up the tree splitting/inserting/adjusting as necessary.
382 * If we need to insert and there isn't room, split the node, then
383 * decide which fragment to insert the new block from below into.
384 * Note that we may split the root this way, but we need more fixup.
385 */
386 max = state->path.active - 1;
387 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
388 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000389 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 addblk = &state->path.blk[max]; /* initial dummy value */
392 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
393 oldblk = &state->path.blk[i];
394 newblk = &state->altpath.blk[i];
395
396 /*
397 * If a leaf node then
398 * Allocate a new leaf node, then rebalance across them.
399 * else if an intermediate node then
400 * We split on the last layer, must we split the node?
401 */
402 switch (oldblk->magic) {
403 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000404 error = xfs_attr3_leaf_split(state, oldblk, newblk);
Dave Chinner24513372014-06-25 14:58:08 +1000405 if ((error != 0) && (error != -ENOSPC)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000406 return error; /* GROT: attr is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408 if (!error) {
409 addblk = newblk;
410 break;
411 }
412 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000413 * Entry wouldn't fit, split the leaf again. The new
414 * extrablk will be consumed by xfs_da3_node_split if
415 * the node is split.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417 state->extravalid = 1;
418 if (state->inleaf) {
419 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000420 trace_xfs_attr_leaf_split_before(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000421 error = xfs_attr3_leaf_split(state, oldblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 &state->extrablk);
423 } else {
424 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000425 trace_xfs_attr_leaf_split_after(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000426 error = xfs_attr3_leaf_split(state, newblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 &state->extrablk);
428 }
429 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000430 return error; /* GROT: attr inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 addblk = newblk;
432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 error = xfs_dir2_leafn_split(state, oldblk, newblk);
435 if (error)
436 return error;
437 addblk = newblk;
438 break;
439 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000440 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 addblk->bp = NULL;
443 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000444 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * Record the newly split block for the next time thru?
447 */
448 if (action)
449 addblk = newblk;
450 else
451 addblk = NULL;
452 break;
453 }
454
455 /*
456 * Update the btree to show the new hashval for this child.
457 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000458 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 if (!addblk)
Eric Sandeend99831f2014-06-22 15:03:54 +1000461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000464 * xfs_da3_node_split() should have consumed any extra blocks we added
465 * during a double leaf split in the attr fork. This is guaranteed as
466 * we can't be here if the attr fork only has a single leaf block.
467 */
468 ASSERT(state->extravalid == 0 ||
469 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
470
471 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 * Split the root node.
473 */
474 ASSERT(state->path.active == 0);
475 oldblk = &state->path.blk[0];
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000476 error = xfs_da3_root_split(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 addblk->bp = NULL;
Eric Sandeend99831f2014-06-22 15:03:54 +1000479 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000483 * Update pointers to the node which used to be block 0 and just got
484 * bumped because of the addition of a new root node. Note that the
485 * original block 0 could be at any position in the list of blocks in
486 * the tree.
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000487 *
Dave Chinner160ae762016-07-22 09:51:05 +1000488 * Note: the magic numbers and sibling pointers are in the same physical
489 * place for both v2 and v3 headers (by design). Hence it doesn't matter
490 * which version of the xfs_da_intnode structure we use here as the
491 * result will be the same using either structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000493 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (node->hdr.info.forw) {
Dave Chinner160ae762016-07-22 09:51:05 +1000495 ASSERT(be32_to_cpu(node->hdr.info.forw) == addblk->blkno);
496 node = addblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100497 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner160ae762016-07-22 09:51:05 +1000498 xfs_trans_log_buf(state->args->trans, addblk->bp,
499 XFS_DA_LOGRANGE(node, &node->hdr.info,
500 sizeof(node->hdr.info)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000502 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100503 if (node->hdr.info.back) {
Dave Chinner160ae762016-07-22 09:51:05 +1000504 ASSERT(be32_to_cpu(node->hdr.info.back) == addblk->blkno);
505 node = addblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100506 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner160ae762016-07-22 09:51:05 +1000507 xfs_trans_log_buf(state->args->trans, addblk->bp,
508 XFS_DA_LOGRANGE(node, &node->hdr.info,
509 sizeof(node->hdr.info)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 addblk->bp = NULL;
Eric Sandeend99831f2014-06-22 15:03:54 +1000512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515/*
516 * Split the root. We have to create a new root and point to the two
517 * parts (the split old root) that we just created. Copy block zero to
518 * the EOF, extending the inode in process.
519 */
520STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000521xfs_da3_root_split(
522 struct xfs_da_state *state,
523 struct xfs_da_state_blk *blk1,
524 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000526 struct xfs_da_intnode *node;
527 struct xfs_da_intnode *oldroot;
528 struct xfs_da_node_entry *btree;
529 struct xfs_da3_icnode_hdr nodehdr;
530 struct xfs_da_args *args;
531 struct xfs_buf *bp;
532 struct xfs_inode *dp;
533 struct xfs_trans *tp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000534 struct xfs_dir2_leaf *leaf;
535 xfs_dablk_t blkno;
536 int level;
537 int error;
538 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Chinner5a5881c2012-03-22 05:15:13 +0000540 trace_xfs_da_root_split(state->args);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
543 * Copy the existing (incorrect) block from the root node position
544 * to a free space somewhere.
545 */
546 args = state->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 error = xfs_da_grow_inode(args, &blkno);
548 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000549 return error;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dp = args->dp;
552 tp = args->trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
554 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000555 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000556 node = bp->b_addr;
557 oldroot = blk1->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000558 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
559 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
Fabian Frederick29916df2015-03-25 14:55:25 +1100560 struct xfs_da3_icnode_hdr icnodehdr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000561
Fabian Frederick29916df2015-03-25 14:55:25 +1100562 dp->d_ops->node_hdr_from_disk(&icnodehdr, oldroot);
Dave Chinner4bceb182013-10-29 22:11:51 +1100563 btree = dp->d_ops->node_tree_p(oldroot);
Fabian Frederick29916df2015-03-25 14:55:25 +1100564 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
565 level = icnodehdr.level;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100566
567 /*
568 * we are about to copy oldroot to bp, so set up the type
569 * of bp while we know exactly what it will be.
570 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100571 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000573 struct xfs_dir3_icleaf_hdr leafhdr;
574 struct xfs_dir2_leaf_entry *ents;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 leaf = (xfs_dir2_leaf_t *)oldroot;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100577 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100578 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000579
580 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
581 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
582 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000583 level = 0;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100584
585 /*
586 * we are about to copy oldroot to bp, so set up the type
587 * of bp while we know exactly what it will be.
588 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100589 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000591
592 /*
593 * we can copy most of the information in the node from one block to
594 * another, but for CRC enabled headers we have to make sure that the
595 * block specific identifiers are kept intact. We update the buffer
596 * directly for this.
597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 memcpy(node, oldroot, size);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000599 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
600 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
601 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
602
603 node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
604 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000605 xfs_trans_log_buf(tp, bp, 0, size - 1);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100606
Dave Chinner1813dd62012-11-14 17:54:40 +1100607 bp->b_ops = blk1->bp->b_ops;
Dave Chinner0a4edc82013-09-02 10:32:01 +1000608 xfs_trans_buf_copy_type(bp, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 blk1->bp = bp;
610 blk1->blkno = blkno;
611
612 /*
613 * Set up the new root node.
614 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000615 error = xfs_da3_node_create(args,
Dave Chinner7dda6e82014-06-06 15:11:18 +1000616 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000617 level + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000619 return error;
620
Dave Chinner1d9025e2012-06-22 18:50:14 +1000621 node = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100622 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +1100623 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000624 btree[0].hashval = cpu_to_be32(blk1->hashval);
625 btree[0].before = cpu_to_be32(blk1->blkno);
626 btree[1].hashval = cpu_to_be32(blk2->hashval);
627 btree[1].before = cpu_to_be32(blk2->blkno);
628 nodehdr.count = 2;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100629 dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +1000632 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
633 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000634 ASSERT(blk1->blkno >= args->geo->leafblk &&
635 blk1->blkno < args->geo->freeblk);
636 ASSERT(blk2->blkno >= args->geo->leafblk &&
637 blk2->blkno < args->geo->freeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639#endif
640
641 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000642 xfs_trans_log_buf(tp, bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000643 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000645 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648/*
649 * Split the node, rebalance, then add the new entry.
650 */
651STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000652xfs_da3_node_split(
653 struct xfs_da_state *state,
654 struct xfs_da_state_blk *oldblk,
655 struct xfs_da_state_blk *newblk,
656 struct xfs_da_state_blk *addblk,
657 int treelevel,
658 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000660 struct xfs_da_intnode *node;
661 struct xfs_da3_icnode_hdr nodehdr;
662 xfs_dablk_t blkno;
663 int newcount;
664 int error;
665 int useextra;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100666 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dave Chinner5a5881c2012-03-22 05:15:13 +0000668 trace_xfs_da_node_split(state->args);
669
Dave Chinner1d9025e2012-06-22 18:50:14 +1000670 node = oldblk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100671 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000674 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000676 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 newcount = 1 + useextra;
678 /*
679 * Do we have to split the node?
680 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +1000681 if (nodehdr.count + newcount > state->args->geo->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /*
683 * Allocate a new node, add to the doubly linked chain of
684 * nodes, then move some of our excess entries into it.
685 */
686 error = xfs_da_grow_inode(state->args, &blkno);
687 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000688 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000690 error = xfs_da3_node_create(state->args, blkno, treelevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 &newblk->bp, state->args->whichfork);
692 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000693 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 newblk->blkno = blkno;
695 newblk->magic = XFS_DA_NODE_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000696 xfs_da3_node_rebalance(state, oldblk, newblk);
697 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000699 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *result = 1;
701 } else {
702 *result = 0;
703 }
704
705 /*
706 * Insert the new entry(s) into the correct block
707 * (updating last hashval in the process).
708 *
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000709 * xfs_da3_node_add() inserts BEFORE the given index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * and as a result of using node_lookup_int() we always
711 * point to a valid entry (not after one), but a split
712 * operation always results in a new block whose hashvals
713 * FOLLOW the current block.
714 *
715 * If we had double-split op below us, then add the extra block too.
716 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000717 node = oldblk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100718 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000719 if (oldblk->index <= nodehdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000721 xfs_da3_node_add(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (useextra) {
723 if (state->extraafter)
724 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000725 xfs_da3_node_add(state, oldblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 state->extravalid = 0;
727 }
728 } else {
729 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000730 xfs_da3_node_add(state, newblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (useextra) {
732 if (state->extraafter)
733 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000734 xfs_da3_node_add(state, newblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 state->extravalid = 0;
736 }
737 }
738
Eric Sandeend99831f2014-06-22 15:03:54 +1000739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Balance the btree elements between two intermediate nodes,
744 * usually one full and one empty.
745 *
746 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
747 */
748STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000749xfs_da3_node_rebalance(
750 struct xfs_da_state *state,
751 struct xfs_da_state_blk *blk1,
752 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000754 struct xfs_da_intnode *node1;
755 struct xfs_da_intnode *node2;
756 struct xfs_da_intnode *tmpnode;
757 struct xfs_da_node_entry *btree1;
758 struct xfs_da_node_entry *btree2;
759 struct xfs_da_node_entry *btree_s;
760 struct xfs_da_node_entry *btree_d;
761 struct xfs_da3_icnode_hdr nodehdr1;
762 struct xfs_da3_icnode_hdr nodehdr2;
763 struct xfs_trans *tp;
764 int count;
765 int tmp;
766 int swap = 0;
Dave Chinner4bceb182013-10-29 22:11:51 +1100767 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Dave Chinner5a5881c2012-03-22 05:15:13 +0000769 trace_xfs_da_node_rebalance(state->args);
770
Dave Chinner1d9025e2012-06-22 18:50:14 +1000771 node1 = blk1->bp->b_addr;
772 node2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100773 dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
774 dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100775 btree1 = dp->d_ops->node_tree_p(node1);
776 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Figure out how many entries need to move, and in which direction.
780 * Swap the nodes around if that makes it simpler.
781 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000782 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
783 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
784 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
785 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 tmpnode = node1;
787 node1 = node2;
788 node2 = tmpnode;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100789 dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
790 dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100791 btree1 = dp->d_ops->node_tree_p(node1);
792 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000793 swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000795
796 count = (nodehdr1.count - nodehdr2.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (count == 0)
798 return;
799 tp = state->args->trans;
800 /*
801 * Two cases: high-to-low and low-to-high.
802 */
803 if (count > 0) {
804 /*
805 * Move elements in node2 up to make a hole.
806 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000807 tmp = nodehdr2.count;
808 if (tmp > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000810 btree_s = &btree2[0];
811 btree_d = &btree2[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 memmove(btree_d, btree_s, tmp);
813 }
814
815 /*
816 * Move the req'd B-tree elements from high in node1 to
817 * low in node2.
818 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000819 nodehdr2.count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000821 btree_s = &btree1[nodehdr1.count - count];
822 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000824 nodehdr1.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 } else {
826 /*
827 * Move the req'd B-tree elements from low in node2 to
828 * high in node1.
829 */
830 count = -count;
831 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000832 btree_s = &btree2[0];
833 btree_d = &btree1[nodehdr1.count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000835 nodehdr1.count += count;
836
Dave Chinner1d9025e2012-06-22 18:50:14 +1000837 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 XFS_DA_LOGRANGE(node1, btree_d, tmp));
839
840 /*
841 * Move elements in node2 down to fill the hole.
842 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000843 tmp = nodehdr2.count - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000845 btree_s = &btree2[count];
846 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 memmove(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000848 nodehdr2.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 /*
852 * Log header of node 1 and all current bits of node 2.
853 */
Dave Chinner01ba43b2013-10-29 22:11:52 +1100854 dp->d_ops->node_hdr_to_disk(node1, &nodehdr1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000855 xfs_trans_log_buf(tp, blk1->bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100856 XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000857
Dave Chinner01ba43b2013-10-29 22:11:52 +1100858 dp->d_ops->node_hdr_to_disk(node2, &nodehdr2);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000859 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 XFS_DA_LOGRANGE(node2, &node2->hdr,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100861 dp->d_ops->node_hdr_size +
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000862 (sizeof(btree2[0]) * nodehdr2.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /*
865 * Record the last hashval from each block for upward propagation.
866 * (note: don't use the swapped node pointers)
867 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000868 if (swap) {
869 node1 = blk1->bp->b_addr;
870 node2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100871 dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
872 dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100873 btree1 = dp->d_ops->node_tree_p(node1);
874 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000875 }
876 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
877 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /*
880 * Adjust the expected index for insertion.
881 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000882 if (blk1->index >= nodehdr1.count) {
883 blk2->index = blk1->index - nodehdr1.count;
884 blk1->index = nodehdr1.count + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886}
887
888/*
889 * Add a new entry to an intermediate node.
890 */
891STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000892xfs_da3_node_add(
893 struct xfs_da_state *state,
894 struct xfs_da_state_blk *oldblk,
895 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000897 struct xfs_da_intnode *node;
898 struct xfs_da3_icnode_hdr nodehdr;
899 struct xfs_da_node_entry *btree;
900 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +1100901 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Dave Chinner5a5881c2012-03-22 05:15:13 +0000903 trace_xfs_da_node_add(state->args);
904
Dave Chinner1d9025e2012-06-22 18:50:14 +1000905 node = oldblk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100906 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +1100907 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000908
909 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000911 if (state->args->whichfork == XFS_DATA_FORK)
Dave Chinner7dda6e82014-06-06 15:11:18 +1000912 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
913 newblk->blkno < state->args->geo->freeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /*
916 * We may need to make some room before we insert the new node.
917 */
918 tmp = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000919 if (oldblk->index < nodehdr.count) {
920 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
921 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000923 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
924 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000925 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000926 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
927 tmp + sizeof(*btree)));
928
929 nodehdr.count += 1;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100930 dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000931 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100932 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /*
935 * Copy the last hash value from the oldblk to propagate upwards.
936 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000937 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/*========================================================================
941 * Routines used for shrinking the Btree.
942 *========================================================================*/
943
944/*
945 * Deallocate an empty leaf node, remove it from its parent,
946 * possibly deallocating that block, etc...
947 */
948int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000949xfs_da3_join(
950 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000952 struct xfs_da_state_blk *drop_blk;
953 struct xfs_da_state_blk *save_blk;
954 int action = 0;
955 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Dave Chinner5a5881c2012-03-22 05:15:13 +0000957 trace_xfs_da_join(state->args);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 drop_blk = &state->path.blk[ state->path.active-1 ];
960 save_blk = &state->altpath.blk[ state->path.active-1 ];
961 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
962 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000963 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
966 * Walk back up the tree joining/deallocating as necessary.
967 * When we stop dropping blocks, break out.
968 */
969 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
970 state->path.active--) {
971 /*
972 * See if we can combine the block with a neighbor.
973 * (action == 0) => no options, just leave
974 * (action == 1) => coalesce, then unlink
975 * (action == 2) => block empty, unlink it
976 */
977 switch (drop_blk->magic) {
978 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000979 error = xfs_attr3_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000981 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (action == 0)
Eric Sandeend99831f2014-06-22 15:03:54 +1000983 return 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000984 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 error = xfs_dir2_leafn_toosmall(state, &action);
988 if (error)
989 return error;
990 if (action == 0)
991 return 0;
992 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
993 break;
994 case XFS_DA_NODE_MAGIC:
995 /*
996 * Remove the offending node, fixup hashvals,
997 * check for a toosmall neighbor.
998 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000999 xfs_da3_node_remove(state, drop_blk);
1000 xfs_da3_fixhashpath(state, &state->path);
1001 error = xfs_da3_node_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001003 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (action == 0)
1005 return 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001006 xfs_da3_node_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001009 xfs_da3_fixhashpath(state, &state->altpath);
1010 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 xfs_da_state_kill_altpath(state);
1012 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001013 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1015 drop_blk->bp);
1016 drop_blk->bp = NULL;
1017 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001018 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 /*
1021 * We joined all the way to the top. If it turns out that
1022 * we only have one entry in the root, make the child block
1023 * the new root.
1024 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001025 xfs_da3_node_remove(state, drop_blk);
1026 xfs_da3_fixhashpath(state, &state->path);
1027 error = xfs_da3_root_join(state, &state->path.blk[0]);
Eric Sandeend99831f2014-06-22 15:03:54 +10001028 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Alex Elder1c4f3322011-07-18 18:14:09 +00001031#ifdef DEBUG
1032static void
1033xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1034{
1035 __be16 magic = blkinfo->magic;
1036
1037 if (level == 1) {
1038 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001039 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001040 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1041 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001042 } else {
1043 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1044 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1045 }
Alex Elder1c4f3322011-07-18 18:14:09 +00001046 ASSERT(!blkinfo->forw);
1047 ASSERT(!blkinfo->back);
1048}
1049#else /* !DEBUG */
1050#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1051#endif /* !DEBUG */
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
1054 * We have only one entry in the root. Copy the only remaining child of
1055 * the old root to block 0 as the new root node.
1056 */
1057STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001058xfs_da3_root_join(
1059 struct xfs_da_state *state,
1060 struct xfs_da_state_blk *root_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001062 struct xfs_da_intnode *oldroot;
1063 struct xfs_da_args *args;
1064 xfs_dablk_t child;
1065 struct xfs_buf *bp;
1066 struct xfs_da3_icnode_hdr oldroothdr;
1067 struct xfs_da_node_entry *btree;
1068 int error;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001069 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Dave Chinner5a5881c2012-03-22 05:15:13 +00001071 trace_xfs_da_root_join(state->args);
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001074
1075 args = state->args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001076 oldroot = root_blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001077 dp->d_ops->node_hdr_from_disk(&oldroothdr, oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001078 ASSERT(oldroothdr.forw == 0);
1079 ASSERT(oldroothdr.back == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /*
1082 * If the root has more than one child, then don't do anything.
1083 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001084 if (oldroothdr.count > 1)
1085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Read in the (only) child block, then copy those bytes into
1089 * the root block's buffer and free the original child block.
1090 */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001091 btree = dp->d_ops->node_tree_p(oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001092 child = be32_to_cpu(btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 ASSERT(child != 0);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001094 error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001095 args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001097 return error;
1098 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
Alex Elder1c4f3322011-07-18 18:14:09 +00001099
Dave Chinner612cfbf2012-11-14 17:52:32 +11001100 /*
1101 * This could be copying a leaf back into the root block in the case of
1102 * there only being a single leaf block left in the tree. Hence we have
Dave Chinner1813dd62012-11-14 17:54:40 +11001103 * to update the b_ops pointer as well to match the buffer type change
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001104 * that could occur. For dir3 blocks we also need to update the block
1105 * number in the buffer header.
Dave Chinner612cfbf2012-11-14 17:52:32 +11001106 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001107 memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
Dave Chinner1813dd62012-11-14 17:54:40 +11001108 root_blk->bp->b_ops = bp->b_ops;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001109 xfs_trans_buf_copy_type(root_blk->bp, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001110 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1111 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1112 da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
1113 }
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001114 xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1115 args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 error = xfs_da_shrink_inode(args, child, bp);
Eric Sandeend99831f2014-06-22 15:03:54 +10001117 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/*
1121 * Check a node block and its neighbors to see if the block should be
1122 * collapsed into one or the other neighbor. Always keep the block
1123 * with the smaller block number.
1124 * If the current block is over 50% full, don't try to join it, return 0.
1125 * If the block is empty, fill in the state structure and return 2.
1126 * If it can be collapsed, fill in the state structure and return 1.
1127 * If nothing can be done, return 0.
1128 */
1129STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001130xfs_da3_node_toosmall(
1131 struct xfs_da_state *state,
1132 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001134 struct xfs_da_intnode *node;
1135 struct xfs_da_state_blk *blk;
1136 struct xfs_da_blkinfo *info;
1137 xfs_dablk_t blkno;
1138 struct xfs_buf *bp;
1139 struct xfs_da3_icnode_hdr nodehdr;
1140 int count;
1141 int forward;
1142 int error;
1143 int retval;
1144 int i;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001145 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Dave Chinneree732592012-11-12 22:53:53 +11001147 trace_xfs_da_node_toosmall(state->args);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 /*
1150 * Check for the degenerate case of the block being over 50% full.
1151 * If so, it's not worth even looking to see if we might be able
1152 * to coalesce with a sibling.
1153 */
1154 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001155 info = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 node = (xfs_da_intnode_t *)info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001157 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001158 if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *action = 0; /* blk over 50%, don't try to join */
Eric Sandeend99831f2014-06-22 15:03:54 +10001160 return 0; /* blk over 50%, don't try to join */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /*
1164 * Check for the degenerate case of the block being empty.
1165 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001166 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * to merge with the forward block unless it is NULL.
1168 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001169 if (nodehdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * Make altpath point to the block we want to keep and
1172 * path point to the block we want to drop (this one).
1173 */
Nathan Scott89da0542006-03-17 17:28:40 +11001174 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001176 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 0, &retval);
1178 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001179 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (retval) {
1181 *action = 0;
1182 } else {
1183 *action = 2;
1184 }
Eric Sandeend99831f2014-06-22 15:03:54 +10001185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 /*
1189 * Examine each sibling block to see if we can coalesce with
1190 * at least 25% free space to spare. We need to figure out
1191 * whether to merge with the forward or the backward block.
1192 * We prefer coalescing with the lower numbered sibling so as
1193 * to shrink a directory over time.
1194 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001195 count = state->args->geo->node_ents;
1196 count -= state->args->geo->node_ents >> 2;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001197 count -= nodehdr.count;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* start with smaller blk num */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001200 forward = nodehdr.forw < nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 for (i = 0; i < 2; forward = !forward, i++) {
Mark Tinguely997def22013-09-23 12:18:58 -05001202 struct xfs_da3_icnode_hdr thdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (forward)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001204 blkno = nodehdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001206 blkno = nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (blkno == 0)
1208 continue;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001209 error = xfs_da3_node_read(state->args->trans, dp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001210 blkno, -1, &bp, state->args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001212 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Dave Chinner1d9025e2012-06-22 18:50:14 +10001214 node = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001215 dp->d_ops->node_hdr_from_disk(&thdr, node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001216 xfs_trans_brelse(state->args->trans, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001217
Mark Tinguely997def22013-09-23 12:18:58 -05001218 if (count - thdr.count >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break; /* fits with at least 25% to spare */
1220 }
1221 if (i >= 2) {
1222 *action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
1226 /*
1227 * Make altpath point to the block we want to keep (the lower
1228 * numbered block) and path point to the block we want to drop.
1229 */
1230 memcpy(&state->altpath, &state->path, sizeof(state->path));
1231 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001232 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 0, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001235 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 0, &retval);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001237 }
1238 if (error)
1239 return error;
1240 if (retval) {
1241 *action = 0;
1242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244 *action = 1;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001245 return 0;
1246}
1247
1248/*
1249 * Pick up the last hashvalue from an intermediate node.
1250 */
1251STATIC uint
1252xfs_da3_node_lasthash(
Dave Chinner4bceb182013-10-29 22:11:51 +11001253 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001254 struct xfs_buf *bp,
1255 int *count)
1256{
1257 struct xfs_da_intnode *node;
1258 struct xfs_da_node_entry *btree;
1259 struct xfs_da3_icnode_hdr nodehdr;
1260
1261 node = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001262 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001263 if (count)
1264 *count = nodehdr.count;
1265 if (!nodehdr.count)
1266 return 0;
Dave Chinner4bceb182013-10-29 22:11:51 +11001267 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001268 return be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271/*
1272 * Walk back up the tree adjusting hash values as necessary,
1273 * when we stop making changes, return.
1274 */
1275void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001276xfs_da3_fixhashpath(
1277 struct xfs_da_state *state,
1278 struct xfs_da_state_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001280 struct xfs_da_state_blk *blk;
1281 struct xfs_da_intnode *node;
1282 struct xfs_da_node_entry *btree;
1283 xfs_dahash_t lasthash=0;
1284 int level;
1285 int count;
Dave Chinner4bceb182013-10-29 22:11:51 +11001286 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Dave Chinneree732592012-11-12 22:53:53 +11001288 trace_xfs_da_fixhashpath(state->args);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 level = path->active-1;
1291 blk = &path->blk[ level ];
1292 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 case XFS_ATTR_LEAF_MAGIC:
1294 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1295 if (count == 0)
1296 return;
1297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 case XFS_DIR2_LEAFN_MAGIC:
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001299 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (count == 0)
1301 return;
1302 break;
1303 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001304 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (count == 0)
1306 return;
1307 break;
1308 }
1309 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001310 struct xfs_da3_icnode_hdr nodehdr;
1311
Dave Chinner1d9025e2012-06-22 18:50:14 +10001312 node = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001313 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001314 btree = dp->d_ops->node_tree_p(node);
Mark Tinguelyc88547a2014-04-04 07:10:49 +11001315 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
1317 blk->hashval = lasthash;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001318 btree[blk->index].hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001319 xfs_trans_log_buf(state->args->trans, blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001320 XFS_DA_LOGRANGE(node, &btree[blk->index],
1321 sizeof(*btree)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001323 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325}
1326
1327/*
1328 * Remove an entry from an intermediate node.
1329 */
1330STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001331xfs_da3_node_remove(
1332 struct xfs_da_state *state,
1333 struct xfs_da_state_blk *drop_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001335 struct xfs_da_intnode *node;
1336 struct xfs_da3_icnode_hdr nodehdr;
1337 struct xfs_da_node_entry *btree;
1338 int index;
1339 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001340 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Dave Chinner5a5881c2012-03-22 05:15:13 +00001342 trace_xfs_da_node_remove(state->args);
1343
Dave Chinner1d9025e2012-06-22 18:50:14 +10001344 node = drop_blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001345 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001346 ASSERT(drop_blk->index < nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 ASSERT(drop_blk->index >= 0);
1348
1349 /*
1350 * Copy over the offending entry, or just zero it out.
1351 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001352 index = drop_blk->index;
Dave Chinner4bceb182013-10-29 22:11:51 +11001353 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001354 if (index < nodehdr.count - 1) {
1355 tmp = nodehdr.count - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001357 memmove(&btree[index], &btree[index + 1], tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001358 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001359 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1360 index = nodehdr.count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001362 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001363 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001364 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1365 nodehdr.count -= 1;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001366 dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001367 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001368 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /*
1371 * Copy the last hash value from the block to propagate upwards.
1372 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001373 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001377 * Unbalance the elements between two intermediate nodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * move all Btree elements from one node into another.
1379 */
1380STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001381xfs_da3_node_unbalance(
1382 struct xfs_da_state *state,
1383 struct xfs_da_state_blk *drop_blk,
1384 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001386 struct xfs_da_intnode *drop_node;
1387 struct xfs_da_intnode *save_node;
1388 struct xfs_da_node_entry *drop_btree;
1389 struct xfs_da_node_entry *save_btree;
1390 struct xfs_da3_icnode_hdr drop_hdr;
1391 struct xfs_da3_icnode_hdr save_hdr;
1392 struct xfs_trans *tp;
1393 int sindex;
1394 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001395 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Dave Chinner5a5881c2012-03-22 05:15:13 +00001397 trace_xfs_da_node_unbalance(state->args);
1398
Dave Chinner1d9025e2012-06-22 18:50:14 +10001399 drop_node = drop_blk->bp->b_addr;
1400 save_node = save_blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001401 dp->d_ops->node_hdr_from_disk(&drop_hdr, drop_node);
1402 dp->d_ops->node_hdr_from_disk(&save_hdr, save_node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001403 drop_btree = dp->d_ops->node_tree_p(drop_node);
1404 save_btree = dp->d_ops->node_tree_p(save_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 tp = state->args->trans;
1406
1407 /*
1408 * If the dying block has lower hashvals, then move all the
1409 * elements in the remaining block up to make a hole.
1410 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001411 if ((be32_to_cpu(drop_btree[0].hashval) <
1412 be32_to_cpu(save_btree[0].hashval)) ||
1413 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1414 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1415 /* XXX: check this - is memmove dst correct? */
1416 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1417 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1418
1419 sindex = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001420 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001421 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1422 (save_hdr.count + drop_hdr.count) *
1423 sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001425 sindex = save_hdr.count;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001426 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001427 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1428 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 /*
1432 * Move all the B-tree elements from drop_blk to save_blk.
1433 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001434 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1435 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1436 save_hdr.count += drop_hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Dave Chinner01ba43b2013-10-29 22:11:52 +11001438 dp->d_ops->node_hdr_to_disk(save_node, &save_hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001439 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001441 dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
1444 * Save the last hashval in the remaining block for upward propagation.
1445 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001446 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449/*========================================================================
1450 * Routines used for finding things in the Btree.
1451 *========================================================================*/
1452
1453/*
1454 * Walk down the Btree looking for a particular filename, filling
1455 * in the state structure as we go.
1456 *
1457 * We will set the state structure to point to each of the elements
1458 * in each of the nodes where either the hashval is or should be.
1459 *
1460 * We support duplicate hashval's so for each entry in the current
1461 * node that could contain the desired hashval, descend. This is a
1462 * pruned depth-first tree search.
1463 */
1464int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001465xfs_da3_node_lookup_int(
1466 struct xfs_da_state *state,
1467 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001469 struct xfs_da_state_blk *blk;
1470 struct xfs_da_blkinfo *curr;
1471 struct xfs_da_intnode *node;
1472 struct xfs_da_node_entry *btree;
1473 struct xfs_da3_icnode_hdr nodehdr;
1474 struct xfs_da_args *args;
1475 xfs_dablk_t blkno;
1476 xfs_dahash_t hashval;
1477 xfs_dahash_t btreehashval;
1478 int probe;
1479 int span;
1480 int max;
1481 int error;
1482 int retval;
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001483 unsigned int expected_level = 0;
Dave Chinner4bceb182013-10-29 22:11:51 +11001484 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 args = state->args;
1487
1488 /*
1489 * Descend thru the B-tree searching each level for the right
1490 * node to use, until the right hashval is found.
1491 */
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001492 blkno = args->geo->leafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 for (blk = &state->path.blk[0], state->path.active = 1;
1494 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1495 blk++, state->path.active++) {
1496 /*
1497 * Read the next node down in the tree.
1498 */
1499 blk->blkno = blkno;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001500 error = xfs_da3_node_read(args->trans, args->dp, blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001501 -1, &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (error) {
1503 blk->blkno = 0;
1504 state->path.active--;
Eric Sandeend99831f2014-06-22 15:03:54 +10001505 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001507 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001508 blk->magic = be16_to_cpu(curr->magic);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001509
Dave Chinner517c2222013-04-24 18:58:55 +10001510 if (blk->magic == XFS_ATTR_LEAF_MAGIC ||
1511 blk->magic == XFS_ATTR3_LEAF_MAGIC) {
1512 blk->magic = XFS_ATTR_LEAF_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001513 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1514 break;
1515 }
1516
1517 if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1518 blk->magic == XFS_DIR3_LEAFN_MAGIC) {
1519 blk->magic = XFS_DIR2_LEAFN_MAGIC;
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001520 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1521 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001522 break;
1523 }
1524
1525 blk->magic = XFS_DA_NODE_MAGIC;
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 /*
1529 * Search an intermediate node for a match.
1530 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001531 node = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001532 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001533 btree = dp->d_ops->node_tree_p(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001535 /* Tree taller than we can handle; bail out! */
1536 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
1537 return -EFSCORRUPTED;
1538
1539 /* Check the level from the root. */
1540 if (blkno == args->geo->leafblk)
1541 expected_level = nodehdr.level - 1;
1542 else if (expected_level != nodehdr.level)
1543 return -EFSCORRUPTED;
1544 else
1545 expected_level--;
1546
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001547 max = nodehdr.count;
1548 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001550 /*
1551 * Binary search. (note: small blocks will skip loop)
1552 */
1553 probe = span = max / 2;
1554 hashval = args->hashval;
1555 while (span > 4) {
1556 span /= 2;
1557 btreehashval = be32_to_cpu(btree[probe].hashval);
1558 if (btreehashval < hashval)
1559 probe += span;
1560 else if (btreehashval > hashval)
1561 probe -= span;
1562 else
1563 break;
1564 }
1565 ASSERT((probe >= 0) && (probe < max));
1566 ASSERT((span <= 4) ||
1567 (be32_to_cpu(btree[probe].hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001569 /*
1570 * Since we may have duplicate hashval's, find the first
1571 * matching hashval in the node.
1572 */
1573 while (probe > 0 &&
1574 be32_to_cpu(btree[probe].hashval) >= hashval) {
1575 probe--;
1576 }
1577 while (probe < max &&
1578 be32_to_cpu(btree[probe].hashval) < hashval) {
1579 probe++;
1580 }
1581
1582 /*
1583 * Pick the right block to descend on.
1584 */
1585 if (probe == max) {
1586 blk->index = max - 1;
1587 blkno = be32_to_cpu(btree[max - 1].before);
1588 } else {
1589 blk->index = probe;
1590 blkno = be32_to_cpu(btree[probe].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001592
1593 /* We can't point back to the root. */
1594 if (blkno == args->geo->leafblk)
1595 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001598 if (expected_level != 0)
1599 return -EFSCORRUPTED;
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 /*
1602 * A leaf block that ends in the hashval that we are interested in
1603 * (final hashval == search hashval) means that the next block may
1604 * contain more entries with the same hashval, shift upward to the
1605 * next leaf and keep searching.
1606 */
1607 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001608 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1610 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001611 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Dave Chinner517c2222013-04-24 18:58:55 +10001612 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 blk->index = args->index;
1614 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001615 } else {
1616 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001617 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Dave Chinner24513372014-06-25 14:58:08 +10001619 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 (blk->hashval == args->hashval)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001621 error = xfs_da3_path_shift(state, &state->path, 1, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 &retval);
1623 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001624 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (retval == 0) {
1626 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001627 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* path_shift() gives ENOENT */
Dave Chinner24513372014-06-25 14:58:08 +10001629 retval = -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 break;
1633 }
1634 *result = retval;
Eric Sandeend99831f2014-06-22 15:03:54 +10001635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/*========================================================================
1639 * Utility routines.
1640 *========================================================================*/
1641
1642/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001643 * Compare two intermediate nodes for "order".
1644 */
1645STATIC int
1646xfs_da3_node_order(
Dave Chinner4bceb182013-10-29 22:11:51 +11001647 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001648 struct xfs_buf *node1_bp,
1649 struct xfs_buf *node2_bp)
1650{
1651 struct xfs_da_intnode *node1;
1652 struct xfs_da_intnode *node2;
1653 struct xfs_da_node_entry *btree1;
1654 struct xfs_da_node_entry *btree2;
1655 struct xfs_da3_icnode_hdr node1hdr;
1656 struct xfs_da3_icnode_hdr node2hdr;
1657
1658 node1 = node1_bp->b_addr;
1659 node2 = node2_bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001660 dp->d_ops->node_hdr_from_disk(&node1hdr, node1);
1661 dp->d_ops->node_hdr_from_disk(&node2hdr, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +11001662 btree1 = dp->d_ops->node_tree_p(node1);
1663 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001664
1665 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1666 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1667 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1668 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1669 return 1;
1670 }
1671 return 0;
1672}
1673
1674/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * Link a new block into a doubly linked list of blocks (of whatever type).
1676 */
1677int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001678xfs_da3_blk_link(
1679 struct xfs_da_state *state,
1680 struct xfs_da_state_blk *old_blk,
1681 struct xfs_da_state_blk *new_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001683 struct xfs_da_blkinfo *old_info;
1684 struct xfs_da_blkinfo *new_info;
1685 struct xfs_da_blkinfo *tmp_info;
1686 struct xfs_da_args *args;
1687 struct xfs_buf *bp;
1688 int before = 0;
1689 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001690 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /*
1693 * Set up environment.
1694 */
1695 args = state->args;
1696 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001697 old_info = old_blk->bp->b_addr;
1698 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001700 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 case XFS_ATTR_LEAF_MAGIC:
1705 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1706 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001708 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001711 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
1713 }
1714
1715 /*
1716 * Link blocks in appropriate order.
1717 */
1718 if (before) {
1719 /*
1720 * Link new block in before existing block.
1721 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001722 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001723 new_info->forw = cpu_to_be32(old_blk->blkno);
1724 new_info->back = old_info->back;
1725 if (old_info->back) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001726 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001727 be32_to_cpu(old_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001728 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001730 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001732 tmp_info = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001733 ASSERT(tmp_info->magic == old_info->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001734 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1735 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001736 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Nathan Scott89da0542006-03-17 17:28:40 +11001738 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 } else {
1740 /*
1741 * Link new block in after existing block.
1742 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001743 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001744 new_info->forw = old_info->forw;
1745 new_info->back = cpu_to_be32(old_blk->blkno);
1746 if (old_info->forw) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001747 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001748 be32_to_cpu(old_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001749 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001751 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001753 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001754 ASSERT(tmp_info->magic == old_info->magic);
1755 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1756 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001757 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Nathan Scott89da0542006-03-17 17:28:40 +11001759 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761
Dave Chinner1d9025e2012-06-22 18:50:14 +10001762 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1763 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Eric Sandeend99831f2014-06-22 15:03:54 +10001764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
1767/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * Unlink a block from a doubly linked list of blocks.
1769 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001770STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001771xfs_da3_blk_unlink(
1772 struct xfs_da_state *state,
1773 struct xfs_da_state_blk *drop_blk,
1774 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001776 struct xfs_da_blkinfo *drop_info;
1777 struct xfs_da_blkinfo *save_info;
1778 struct xfs_da_blkinfo *tmp_info;
1779 struct xfs_da_args *args;
1780 struct xfs_buf *bp;
1781 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /*
1784 * Set up environment.
1785 */
1786 args = state->args;
1787 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001788 save_info = save_blk->bp->b_addr;
1789 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001791 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001794 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1795 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1796 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1797 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 /*
1800 * Unlink the leaf block from the doubly linked chain of leaves.
1801 */
Nathan Scott89da0542006-03-17 17:28:40 +11001802 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001803 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001804 save_info->back = drop_info->back;
1805 if (drop_info->back) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001806 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001807 be32_to_cpu(drop_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001808 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001810 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001812 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001813 ASSERT(tmp_info->magic == save_info->magic);
1814 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1815 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001816 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001820 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001821 save_info->forw = drop_info->forw;
1822 if (drop_info->forw) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001823 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001824 be32_to_cpu(drop_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001825 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001827 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001829 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001830 ASSERT(tmp_info->magic == save_info->magic);
1831 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1832 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001833 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836 }
1837
Dave Chinner1d9025e2012-06-22 18:50:14 +10001838 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Eric Sandeend99831f2014-06-22 15:03:54 +10001839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842/*
1843 * Move a path "forward" or "!forward" one block at the current level.
1844 *
1845 * This routine will adjust a "path" to point to the next block
1846 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1847 * Btree, including updating pointers to the intermediate nodes between
1848 * the new bottom and the root.
1849 */
1850int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001851xfs_da3_path_shift(
1852 struct xfs_da_state *state,
1853 struct xfs_da_state_path *path,
1854 int forward,
1855 int release,
1856 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001858 struct xfs_da_state_blk *blk;
1859 struct xfs_da_blkinfo *info;
1860 struct xfs_da_intnode *node;
1861 struct xfs_da_args *args;
1862 struct xfs_da_node_entry *btree;
1863 struct xfs_da3_icnode_hdr nodehdr;
Brian Foster7df1c172015-08-19 10:32:33 +10001864 struct xfs_buf *bp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001865 xfs_dablk_t blkno = 0;
1866 int level;
1867 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001868 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Dave Chinneree732592012-11-12 22:53:53 +11001870 trace_xfs_da_path_shift(state->args);
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
1873 * Roll up the Btree looking for the first block where our
1874 * current index is not at the edge of the block. Note that
1875 * we skip the bottom layer because we want the sibling block.
1876 */
1877 args = state->args;
1878 ASSERT(args != NULL);
1879 ASSERT(path != NULL);
1880 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1881 level = (path->active-1) - 1; /* skip bottom layer in path */
1882 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001883 node = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001884 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001885 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001886
1887 if (forward && (blk->index < nodehdr.count - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 blk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001889 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 break;
1891 } else if (!forward && (blk->index > 0)) {
1892 blk->index--;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001893 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895 }
1896 }
1897 if (level < 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001898 *result = -ENOENT; /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001899 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Eric Sandeend99831f2014-06-22 15:03:54 +10001900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902
1903 /*
1904 * Roll down the edge of the subtree until we reach the
1905 * same depth we were at originally.
1906 */
1907 for (blk++, level++; level < path->active; blk++, level++) {
1908 /*
Brian Foster7df1c172015-08-19 10:32:33 +10001909 * Read the next child block into a local buffer.
1910 */
1911 error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
1912 args->whichfork);
1913 if (error)
1914 return error;
1915
1916 /*
1917 * Release the old block (if it's dirty, the trans doesn't
1918 * actually let go) and swap the local buffer into the path
1919 * structure. This ensures failure of the above read doesn't set
1920 * a NULL buffer in an active slot in the path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
1922 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001923 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 blk->blkno = blkno;
Brian Foster7df1c172015-08-19 10:32:33 +10001925 blk->bp = bp;
1926
Dave Chinner1d9025e2012-06-22 18:50:14 +10001927 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001928 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001929 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001930 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001931 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001932 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1933 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001934
1935
1936 /*
1937 * Note: we flatten the magic number to a single type so we
1938 * don't have to compare against crc/non-crc types elsewhere.
1939 */
1940 switch (be16_to_cpu(info->magic)) {
1941 case XFS_DA_NODE_MAGIC:
1942 case XFS_DA3_NODE_MAGIC:
1943 blk->magic = XFS_DA_NODE_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 node = (xfs_da_intnode_t *)info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001945 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001946 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001947 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (forward)
1949 blk->index = 0;
1950 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001951 blk->index = nodehdr.count - 1;
1952 blkno = be32_to_cpu(btree[blk->index].before);
1953 break;
1954 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001955 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001956 blk->magic = XFS_ATTR_LEAF_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 ASSERT(level == path->active-1);
1958 blk->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001959 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001960 break;
1961 case XFS_DIR2_LEAFN_MAGIC:
1962 case XFS_DIR3_LEAFN_MAGIC:
1963 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1964 ASSERT(level == path->active-1);
1965 blk->index = 0;
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001966 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1967 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001968 break;
1969 default:
1970 ASSERT(0);
1971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973 }
1974 *result = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978
1979/*========================================================================
1980 * Utility routines.
1981 *========================================================================*/
1982
1983/*
1984 * Implement a simple hash on a character string.
1985 * Rotate the hash value by 7 bits, then XOR each character in.
1986 * This is implemented with some source-level loop unrolling.
1987 */
1988xfs_dahash_t
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001989xfs_da_hashname(const uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 xfs_dahash_t hash;
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /*
1994 * Do four characters at a time as long as we can.
1995 */
1996 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
1997 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
1998 (name[3] << 0) ^ rol32(hash, 7 * 4);
1999
2000 /*
2001 * Now do the rest of the characters.
2002 */
2003 switch (namelen) {
2004 case 3:
2005 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2006 rol32(hash, 7 * 3);
2007 case 2:
2008 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2009 case 1:
2010 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11002011 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return hash;
2013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Barry Naujok5163f952008-05-21 16:41:01 +10002016enum xfs_dacmp
2017xfs_da_compname(
2018 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11002019 const unsigned char *name,
2020 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10002021{
2022 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2023 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2024}
2025
2026static xfs_dahash_t
2027xfs_default_hashname(
2028 struct xfs_name *name)
2029{
2030 return xfs_da_hashname(name->name, name->len);
2031}
2032
2033const struct xfs_nameops xfs_default_nameops = {
2034 .hashname = xfs_default_hashname,
2035 .compname = xfs_da_compname
2036};
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038int
Christoph Hellwig77936d02011-07-13 13:43:49 +02002039xfs_da_grow_inode_int(
2040 struct xfs_da_args *args,
2041 xfs_fileoff_t *bno,
2042 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Christoph Hellwig77936d02011-07-13 13:43:49 +02002044 struct xfs_trans *tp = args->trans;
2045 struct xfs_inode *dp = args->dp;
2046 int w = args->whichfork;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10002047 xfs_rfsblock_t nblks = dp->i_d.di_nblocks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002048 struct xfs_bmbt_irec map, *mapp;
2049 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
2052 * Find a spot in the file space to put the new block.
2053 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02002054 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2055 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /*
2059 * Try mapping it in one filesystem block.
2060 */
2061 nmap = 1;
2062 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002063 error = xfs_bmapi_write(tp, dp, *bno, count,
2064 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 args->firstblock, args->total, &map, &nmap,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002066 args->dfops);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002067 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 ASSERT(nmap <= 1);
2071 if (nmap == 1) {
2072 mapp = &map;
2073 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002074 } else if (nmap == 0 && count > 1) {
2075 xfs_fileoff_t b;
2076 int c;
2077
2078 /*
2079 * If we didn't get it and the block might work if fragmented,
2080 * try without the CONTIG flag. Loop until we get it all.
2081 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002083 for (b = *bno, mapi = 0; b < *bno + count; ) {
Dave Chinner9bb54cb2018-06-07 07:54:02 -07002084 nmap = min(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002085 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002086 error = xfs_bmapi_write(tp, dp, b, c,
2087 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 args->firstblock, args->total,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002089 &mapp[mapi], &nmap, args->dfops);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002090 if (error)
2091 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (nmap < 1)
2093 break;
2094 mapi += nmap;
2095 b = mapp[mapi - 1].br_startoff +
2096 mapp[mapi - 1].br_blockcount;
2097 }
2098 } else {
2099 mapi = 0;
2100 mapp = NULL;
2101 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * Count the blocks we got, make sure it matches the total.
2105 */
2106 for (i = 0, got = 0; i < mapi; i++)
2107 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002108 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02002110 *bno + count) {
Dave Chinner24513372014-06-25 14:58:08 +10002111 error = -ENOSPC;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002112 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002114
David Chinnera7444052008-10-30 17:38:12 +11002115 /* account for newly allocated blocks in reserved blocks total */
2116 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002117
2118out_free_map:
2119 if (mapp != &map)
2120 kmem_free(mapp);
2121 return error;
2122}
2123
2124/*
2125 * Add a block to the btree ahead of the file.
2126 * Return the new block number to the caller.
2127 */
2128int
2129xfs_da_grow_inode(
2130 struct xfs_da_args *args,
2131 xfs_dablk_t *new_blkno)
2132{
2133 xfs_fileoff_t bno;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002134 int error;
2135
Dave Chinner5a5881c2012-03-22 05:15:13 +00002136 trace_xfs_da_grow_inode(args);
2137
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002138 bno = args->geo->leafblk;
2139 error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002140 if (!error)
2141 *new_blkno = (xfs_dablk_t)bno;
2142 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145/*
2146 * Ick. We need to always be able to remove a btree block, even
2147 * if there's no space reservation because the filesystem is full.
2148 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2149 * It swaps the target block with the last block in the file. The
2150 * last block in the file can always be removed since it can't cause
2151 * a bmap btree split to do that.
2152 */
2153STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002154xfs_da3_swap_lastblock(
2155 struct xfs_da_args *args,
2156 xfs_dablk_t *dead_blknop,
2157 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002159 struct xfs_da_blkinfo *dead_info;
2160 struct xfs_da_blkinfo *sib_info;
2161 struct xfs_da_intnode *par_node;
2162 struct xfs_da_intnode *dead_node;
2163 struct xfs_dir2_leaf *dead_leaf2;
2164 struct xfs_da_node_entry *btree;
2165 struct xfs_da3_icnode_hdr par_hdr;
Dave Chinner4bceb182013-10-29 22:11:51 +11002166 struct xfs_inode *dp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002167 struct xfs_trans *tp;
2168 struct xfs_mount *mp;
2169 struct xfs_buf *dead_buf;
2170 struct xfs_buf *last_buf;
2171 struct xfs_buf *sib_buf;
2172 struct xfs_buf *par_buf;
2173 xfs_dahash_t dead_hash;
2174 xfs_fileoff_t lastoff;
2175 xfs_dablk_t dead_blkno;
2176 xfs_dablk_t last_blkno;
2177 xfs_dablk_t sib_blkno;
2178 xfs_dablk_t par_blkno;
2179 int error;
2180 int w;
2181 int entno;
2182 int level;
2183 int dead_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Dave Chinner5a5881c2012-03-22 05:15:13 +00002185 trace_xfs_da_swap_lastblock(args);
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 dead_buf = *dead_bufp;
2188 dead_blkno = *dead_blknop;
2189 tp = args->trans;
Dave Chinner4bceb182013-10-29 22:11:51 +11002190 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 w = args->whichfork;
2192 ASSERT(w == XFS_DATA_FORK);
Dave Chinner4bceb182013-10-29 22:11:51 +11002193 mp = dp->i_mount;
Dave Chinner7dda6e82014-06-06 15:11:18 +10002194 lastoff = args->geo->freeblk;
Dave Chinner4bceb182013-10-29 22:11:51 +11002195 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (error)
2197 return error;
2198 if (unlikely(lastoff == 0)) {
2199 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
2200 mp);
Dave Chinner24513372014-06-25 14:58:08 +10002201 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 /*
2204 * Read the last block in the btree space.
2205 */
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002206 last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
Dave Chinner4bceb182013-10-29 22:11:51 +11002207 error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002208 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return error;
2210 /*
2211 * Copy the last block into the dead buffer and log it.
2212 */
Dave Chinner8f661932014-06-06 15:15:59 +10002213 memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2214 xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002215 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /*
2217 * Get values from the moved block.
2218 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002219 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2220 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2221 struct xfs_dir3_icleaf_hdr leafhdr;
2222 struct xfs_dir2_leaf_entry *ents;
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002225 dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
Dave Chinner4bceb182013-10-29 22:11:51 +11002226 ents = dp->d_ops->leaf_ents_p(dead_leaf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 dead_level = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10002228 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002230 struct xfs_da3_icnode_hdr deadhdr;
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 dead_node = (xfs_da_intnode_t *)dead_info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002233 dp->d_ops->node_hdr_from_disk(&deadhdr, dead_node);
Dave Chinner4bceb182013-10-29 22:11:51 +11002234 btree = dp->d_ops->node_tree_p(dead_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002235 dead_level = deadhdr.level;
2236 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238 sib_buf = par_buf = NULL;
2239 /*
2240 * If the moved block has a left sibling, fix up the pointers.
2241 */
Nathan Scott89da0542006-03-17 17:28:40 +11002242 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002243 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002244 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002246 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002248 be32_to_cpu(sib_info->forw) != last_blkno ||
2249 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
2251 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002252 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 goto done;
2254 }
Nathan Scott89da0542006-03-17 17:28:40 +11002255 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002256 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2258 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 sib_buf = NULL;
2260 }
2261 /*
2262 * If the moved block has a right sibling, fix up the pointers.
2263 */
Nathan Scott89da0542006-03-17 17:28:40 +11002264 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002265 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002266 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002268 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002270 be32_to_cpu(sib_info->back) != last_blkno ||
2271 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
2273 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002274 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto done;
2276 }
Nathan Scott89da0542006-03-17 17:28:40 +11002277 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002278 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2280 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 sib_buf = NULL;
2282 }
Dave Chinner7dda6e82014-06-06 15:11:18 +10002283 par_blkno = args->geo->leafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 level = -1;
2285 /*
2286 * Walk down the tree looking for the parent of the moved block.
2287 */
2288 for (;;) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002289 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002292 par_node = par_buf->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002293 dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002294 if (level >= 0 && level != par_hdr.level + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
2296 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002297 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
2299 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002300 level = par_hdr.level;
Dave Chinner4bceb182013-10-29 22:11:51 +11002301 btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 for (entno = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002303 entno < par_hdr.count &&
2304 be32_to_cpu(btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 entno++)
2306 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002307 if (entno == par_hdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
2309 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002310 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 goto done;
2312 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002313 par_blkno = be32_to_cpu(btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (level == dead_level + 1)
2315 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002316 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 par_buf = NULL;
2318 }
2319 /*
2320 * We're in the right parent block.
2321 * Look for the right entry.
2322 */
2323 for (;;) {
2324 for (;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002325 entno < par_hdr.count &&
2326 be32_to_cpu(btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 entno++)
2328 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002329 if (entno < par_hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002331 par_blkno = par_hdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002332 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 par_buf = NULL;
2334 if (unlikely(par_blkno == 0)) {
2335 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
2336 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002337 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto done;
2339 }
Dave Chinner4bceb182013-10-29 22:11:51 +11002340 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002341 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002343 par_node = par_buf->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002344 dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002345 if (par_hdr.level != level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
2347 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002348 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 goto done;
2350 }
Dave Chinner4bceb182013-10-29 22:11:51 +11002351 btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 entno = 0;
2353 }
2354 /*
2355 * Update the parent entry pointing to the moved block.
2356 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002357 btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002358 xfs_trans_log_buf(tp, par_buf,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002359 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2360 sizeof(btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 *dead_blknop = last_blkno;
2362 *dead_bufp = last_buf;
2363 return 0;
2364done:
2365 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002366 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002368 xfs_trans_brelse(tp, sib_buf);
2369 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return error;
2371}
2372
2373/*
2374 * Remove a btree block from a directory or attribute.
2375 */
2376int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002377xfs_da_shrink_inode(
2378 xfs_da_args_t *args,
2379 xfs_dablk_t dead_blkno,
2380 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
2382 xfs_inode_t *dp;
2383 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Dave Chinner5a5881c2012-03-22 05:15:13 +00002386 trace_xfs_da_shrink_inode(args);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 dp = args->dp;
2389 w = args->whichfork;
2390 tp = args->trans;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002391 count = args->geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 for (;;) {
2393 /*
2394 * Remove extents. If we get ENOSPC for a dir we have to move
2395 * the last block to the place we want to kill.
2396 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002397 error = xfs_bunmapi(tp, dp, dead_blkno, count,
Dave Chinnerab7bb612015-07-29 11:51:01 +10002398 xfs_bmapi_aflag(w), 0, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002399 args->dfops, &done);
Dave Chinner24513372014-06-25 14:58:08 +10002400 if (error == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002402 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002403 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2404 &dead_buf);
2405 if (error)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002406 break;
2407 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002411 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return error;
2413}
2414
2415/*
2416 * See if the mapping(s) for this btree block are valid, i.e.
2417 * don't contain holes, are logically contiguous, and cover the whole range.
2418 */
2419STATIC int
2420xfs_da_map_covers_blocks(
2421 int nmap,
2422 xfs_bmbt_irec_t *mapp,
2423 xfs_dablk_t bno,
2424 int count)
2425{
2426 int i;
2427 xfs_fileoff_t off;
2428
2429 for (i = 0, off = bno; i < nmap; i++) {
2430 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2431 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2432 return 0;
2433 }
2434 if (off != mapp[i].br_startoff) {
2435 return 0;
2436 }
2437 off += mapp[i].br_blockcount;
2438 }
2439 return off == bno + count;
2440}
2441
2442/*
Dave Chinner36054312012-06-22 18:50:13 +10002443 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2444 *
2445 * For the single map case, it is assumed that the caller has provided a pointer
2446 * to a valid xfs_buf_map. For the multiple map case, this function will
2447 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2448 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 */
Dave Chinner36054312012-06-22 18:50:13 +10002450static int
2451xfs_buf_map_from_irec(
2452 struct xfs_mount *mp,
2453 struct xfs_buf_map **mapp,
Dave Chinner836a94a2013-08-12 20:49:44 +10002454 int *nmaps,
Dave Chinner36054312012-06-22 18:50:13 +10002455 struct xfs_bmbt_irec *irecs,
Dave Chinner836a94a2013-08-12 20:49:44 +10002456 int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Dave Chinner36054312012-06-22 18:50:13 +10002458 struct xfs_buf_map *map;
2459 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Dave Chinner36054312012-06-22 18:50:13 +10002461 ASSERT(*nmaps == 1);
2462 ASSERT(nirecs >= 1);
2463
2464 if (nirecs > 1) {
Dave Chinnerb17cb362013-05-20 09:51:12 +10002465 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
2466 KM_SLEEP | KM_NOFS);
Dave Chinner36054312012-06-22 18:50:13 +10002467 if (!map)
Dave Chinner24513372014-06-25 14:58:08 +10002468 return -ENOMEM;
Dave Chinner36054312012-06-22 18:50:13 +10002469 *mapp = map;
2470 }
2471
2472 *nmaps = nirecs;
2473 map = *mapp;
2474 for (i = 0; i < *nmaps; i++) {
2475 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2476 irecs[i].br_startblock != HOLESTARTBLOCK);
2477 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2478 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2479 }
2480 return 0;
2481}
2482
2483/*
2484 * Map the block we are given ready for reading. There are three possible return
2485 * values:
2486 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2487 * caller knows not to execute a subsequent read.
2488 * 0 - if we mapped the block successfully
2489 * >0 - positive error number if there was an error.
2490 */
2491static int
2492xfs_dabuf_map(
Dave Chinner36054312012-06-22 18:50:13 +10002493 struct xfs_inode *dp,
2494 xfs_dablk_t bno,
2495 xfs_daddr_t mappedbno,
2496 int whichfork,
2497 struct xfs_buf_map **map,
2498 int *nmaps)
2499{
2500 struct xfs_mount *mp = dp->i_mount;
2501 int nfsb;
2502 int error = 0;
2503 struct xfs_bmbt_irec irec;
2504 struct xfs_bmbt_irec *irecs = &irec;
2505 int nirecs;
2506
2507 ASSERT(map && *map);
2508 ASSERT(*nmaps == 1);
2509
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002510 if (whichfork == XFS_DATA_FORK)
2511 nfsb = mp->m_dir_geo->fsbcount;
2512 else
2513 nfsb = mp->m_attr_geo->fsbcount;
Dave Chinner36054312012-06-22 18:50:13 +10002514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /*
2516 * Caller doesn't have a mapping. -2 means don't complain
2517 * if we land in a hole.
2518 */
2519 if (mappedbno == -1 || mappedbno == -2) {
2520 /*
2521 * Optimize the one-block case.
2522 */
Dave Chinner36054312012-06-22 18:50:13 +10002523 if (nfsb != 1)
Dave Chinnerb17cb362013-05-20 09:51:12 +10002524 irecs = kmem_zalloc(sizeof(irec) * nfsb,
2525 KM_SLEEP | KM_NOFS);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002526
Dave Chinner36054312012-06-22 18:50:13 +10002527 nirecs = nfsb;
2528 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2529 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002530 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002531 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002533 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2534 irecs->br_startoff = (xfs_fileoff_t)bno;
2535 irecs->br_blockcount = nfsb;
2536 irecs->br_state = 0;
2537 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
Dave Chinner36054312012-06-22 18:50:13 +10002539
2540 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
Dave Chinner24513372014-06-25 14:58:08 +10002541 error = mappedbno == -2 ? -1 : -EFSCORRUPTED;
2542 if (unlikely(error == -EFSCORRUPTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002544 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002545 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2546 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002548 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002549 xfs_alert(mp,
2550"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 i,
Dave Chinner36054312012-06-22 18:50:13 +10002552 (long long)irecs[i].br_startoff,
2553 (long long)irecs[i].br_startblock,
2554 (long long)irecs[i].br_blockcount,
2555 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557 }
2558 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2559 XFS_ERRLEVEL_LOW, mp);
2560 }
Dave Chinner36054312012-06-22 18:50:13 +10002561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
Dave Chinner36054312012-06-22 18:50:13 +10002563 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2564out:
2565 if (irecs != &irec)
2566 kmem_free(irecs);
2567 return error;
2568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Dave Chinner36054312012-06-22 18:50:13 +10002570/*
2571 * Get a buffer for the dir/attr block.
2572 */
2573int
2574xfs_da_get_buf(
2575 struct xfs_trans *trans,
2576 struct xfs_inode *dp,
2577 xfs_dablk_t bno,
2578 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002579 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002580 int whichfork)
2581{
2582 struct xfs_buf *bp;
2583 struct xfs_buf_map map;
2584 struct xfs_buf_map *mapp;
2585 int nmap;
2586 int error;
2587
2588 *bpp = NULL;
2589 mapp = &map;
2590 nmap = 1;
Eric Sandeen9df2dd02014-04-14 19:01:59 +10002591 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002592 &mapp, &nmap);
2593 if (error) {
2594 /* mapping a hole is not an error, but we don't continue */
2595 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002597 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
Dave Chinner36054312012-06-22 18:50:13 +10002599
2600 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2601 mapp, nmap, 0);
Dave Chinner24513372014-06-25 14:58:08 +10002602 error = bp ? bp->b_error : -EIO;
Dave Chinner36054312012-06-22 18:50:13 +10002603 if (error) {
Eric Sandeen6ee49a22014-10-02 09:23:49 +10002604 if (bp)
2605 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002606 goto out_free;
2607 }
2608
Dave Chinner1d9025e2012-06-22 18:50:14 +10002609 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002610
2611out_free:
2612 if (mapp != &map)
2613 kmem_free(mapp);
2614
2615 return error;
2616}
2617
2618/*
2619 * Get a buffer for the dir/attr block, fill in the contents.
2620 */
2621int
2622xfs_da_read_buf(
2623 struct xfs_trans *trans,
2624 struct xfs_inode *dp,
2625 xfs_dablk_t bno,
2626 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002627 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002628 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002629 const struct xfs_buf_ops *ops)
Dave Chinner36054312012-06-22 18:50:13 +10002630{
2631 struct xfs_buf *bp;
2632 struct xfs_buf_map map;
2633 struct xfs_buf_map *mapp;
2634 int nmap;
2635 int error;
2636
2637 *bpp = NULL;
2638 mapp = &map;
2639 nmap = 1;
Eric Sandeen9df2dd02014-04-14 19:01:59 +10002640 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002641 &mapp, &nmap);
2642 if (error) {
2643 /* mapping a hole is not an error, but we don't continue */
2644 if (error == -1)
2645 error = 0;
2646 goto out_free;
2647 }
2648
2649 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2650 dp->i_mount->m_ddev_targp,
Dave Chinner1813dd62012-11-14 17:54:40 +11002651 mapp, nmap, 0, &bp, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002652 if (error)
2653 goto out_free;
2654
2655 if (whichfork == XFS_ATTR_FORK)
2656 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 else
Dave Chinner36054312012-06-22 18:50:13 +10002658 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002659 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002660out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002662 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return error;
2665}
2666
2667/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 * Readahead the dir/attr block.
2669 */
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08002670int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002672 struct xfs_inode *dp,
2673 xfs_dablk_t bno,
Dave Chinnerda6958c2012-11-12 22:54:18 +11002674 xfs_daddr_t mappedbno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002675 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002676 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Dave Chinner36054312012-06-22 18:50:13 +10002678 struct xfs_buf_map map;
2679 struct xfs_buf_map *mapp;
2680 int nmap;
2681 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Dave Chinner36054312012-06-22 18:50:13 +10002683 mapp = &map;
2684 nmap = 1;
Eric Sandeen9df2dd02014-04-14 19:01:59 +10002685 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002686 &mapp, &nmap);
2687 if (error) {
2688 /* mapping a hole is not an error, but we don't continue */
2689 if (error == -1)
2690 error = 0;
2691 goto out_free;
2692 }
2693
2694 mappedbno = mapp[0].bm_bn;
Dave Chinner1813dd62012-11-14 17:54:40 +11002695 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002696
2697out_free:
2698 if (mapp != &map)
2699 kmem_free(mapp);
2700
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08002701 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}