blob: 1427887a1974bb829b6fa24971fa00855b803e75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
34#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_attr.h"
37#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100040#include "xfs_cksum.h"
41#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110042#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * xfs_da_btree.c
46 *
47 * Routines to implement directories as Btrees of hashed names.
48 */
49
50/*========================================================================
51 * Function prototypes for the kernel.
52 *========================================================================*/
53
54/*
55 * Routines used for growing the Btree.
56 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100057STATIC int xfs_da3_root_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 xfs_da_state_blk_t *existing_root,
59 xfs_da_state_blk_t *new_child);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100060STATIC int xfs_da3_node_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 xfs_da_state_blk_t *existing_blk,
62 xfs_da_state_blk_t *split_blk,
63 xfs_da_state_blk_t *blk_to_add,
64 int treelevel,
65 int *result);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100066STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 xfs_da_state_blk_t *node_blk_1,
68 xfs_da_state_blk_t *node_blk_2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100069STATIC void xfs_da3_node_add(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 xfs_da_state_blk_t *old_node_blk,
71 xfs_da_state_blk_t *new_node_blk);
72
73/*
74 * Routines used for shrinking the Btree.
75 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100076STATIC int xfs_da3_root_join(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 xfs_da_state_blk_t *root_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100078STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
79STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 xfs_da_state_blk_t *drop_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100081STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 xfs_da_state_blk_t *src_node_blk,
83 xfs_da_state_blk_t *dst_node_blk);
84
85/*
86 * Utility routines.
87 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100088STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100089 xfs_da_state_blk_t *drop_blk,
90 xfs_da_state_blk_t *save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Dave Chinnerf5ea1102013-04-24 18:58:02 +100092
93kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
94
95/*
96 * Allocate a dir-state structure.
97 * We don't put them on the stack since they're large.
98 */
99xfs_da_state_t *
100xfs_da_state_alloc(void)
101{
102 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
103}
104
105/*
106 * Kill the altpath contents of a da-state structure.
107 */
108STATIC void
109xfs_da_state_kill_altpath(xfs_da_state_t *state)
110{
111 int i;
112
113 for (i = 0; i < state->altpath.active; i++)
114 state->altpath.blk[i].bp = NULL;
115 state->altpath.active = 0;
116}
117
118/*
119 * Free a da-state structure.
120 */
121void
122xfs_da_state_free(xfs_da_state_t *state)
123{
124 xfs_da_state_kill_altpath(state);
125#ifdef DEBUG
126 memset((char *)state, 0, sizeof(*state));
127#endif /* DEBUG */
128 kmem_zone_free(xfs_da_state_zone, state);
129}
130
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800131static xfs_failaddr_t
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000132xfs_da3_node_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100133 struct xfs_buf *bp)
134{
135 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000136 struct xfs_da_intnode *hdr = bp->b_addr;
137 struct xfs_da3_icnode_hdr ichdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100138 const struct xfs_dir_ops *ops;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100139
Dave Chinner01ba43b2013-10-29 22:11:52 +1100140 ops = xfs_dir_get_ops(mp, NULL);
141
142 ops->node_hdr_from_disk(&ichdr, hdr);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000143
144 if (xfs_sb_version_hascrc(&mp->m_sb)) {
145 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
146
147 if (ichdr.magic != XFS_DA3_NODE_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800148 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000149
Eric Sandeence748ea2015-07-29 11:53:31 +1000150 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800151 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000152 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800153 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100154 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800155 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000156 } else {
157 if (ichdr.magic != XFS_DA_NODE_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800158 return __this_address;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100159 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000160 if (ichdr.level == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800161 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000162 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800163 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000164 if (ichdr.count == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800165 return __this_address;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100166
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000167 /*
168 * we don't know if the node is for and attribute or directory tree,
169 * so only fail if the count is outside both bounds
170 */
Dave Chinner7ab610f2014-06-06 15:20:02 +1000171 if (ichdr.count > mp->m_dir_geo->node_ents &&
172 ichdr.count > mp->m_attr_geo->node_ents)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800173 return __this_address;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000174
175 /* XXX: hash order check? */
176
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800177 return NULL;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100178}
179
180static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000181xfs_da3_node_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100182 struct xfs_buf *bp)
183{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000184 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800185 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000186 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800187 xfs_failaddr_t fa;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000188
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800189 fa = xfs_da3_node_verify(bp);
190 if (fa) {
191 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000192 return;
193 }
194
195 if (!xfs_sb_version_hascrc(&mp->m_sb))
196 return;
197
198 if (bip)
199 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
200
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100201 xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100202}
203
Dave Chinner1813dd62012-11-14 17:54:40 +1100204/*
205 * leaf/node format detection on trees is sketchy, so a node read can be done on
206 * leaf level blocks when detection identifies the tree as a node format tree
207 * incorrectly. In this case, we need to swap the verifier to match the correct
208 * format of the block being read.
209 */
Dave Chinner612cfbf2012-11-14 17:52:32 +1100210static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000211xfs_da3_node_read_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100212 struct xfs_buf *bp)
213{
Dave Chinnerd9392a42012-11-12 22:54:17 +1100214 struct xfs_da_blkinfo *info = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800215 xfs_failaddr_t fa;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100216
217 switch (be16_to_cpu(info->magic)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000218 case XFS_DA3_NODE_MAGIC:
Eric Sandeence5028c2014-02-27 15:23:10 +1100219 if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800220 xfs_verifier_error(bp, -EFSBADCRC,
221 __this_address);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000222 break;
Eric Sandeence5028c2014-02-27 15:23:10 +1100223 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000224 /* fall through */
Dave Chinnerd9392a42012-11-12 22:54:17 +1100225 case XFS_DA_NODE_MAGIC:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800226 fa = xfs_da3_node_verify(bp);
227 if (fa)
228 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000229 return;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100230 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner7ced60c2013-05-20 09:51:13 +1000231 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000232 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100233 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100234 return;
235 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +1000236 case XFS_DIR3_LEAFN_MAGIC:
237 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100238 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100239 return;
240 default:
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800241 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100242 break;
243 }
Dave Chinnerd9392a42012-11-12 22:54:17 +1100244}
245
Darrick J. Wongb5572592018-01-08 10:51:08 -0800246/* Verify the structure of a da3 block. */
247static xfs_failaddr_t
248xfs_da3_node_verify_struct(
249 struct xfs_buf *bp)
250{
251 struct xfs_da_blkinfo *info = bp->b_addr;
252
253 switch (be16_to_cpu(info->magic)) {
254 case XFS_DA3_NODE_MAGIC:
255 case XFS_DA_NODE_MAGIC:
256 return xfs_da3_node_verify(bp);
257 case XFS_ATTR_LEAF_MAGIC:
258 case XFS_ATTR3_LEAF_MAGIC:
259 bp->b_ops = &xfs_attr3_leaf_buf_ops;
260 return bp->b_ops->verify_struct(bp);
261 case XFS_DIR2_LEAFN_MAGIC:
262 case XFS_DIR3_LEAFN_MAGIC:
263 bp->b_ops = &xfs_dir3_leafn_buf_ops;
264 return bp->b_ops->verify_struct(bp);
265 default:
266 return __this_address;
267 }
268}
269
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000270const struct xfs_buf_ops xfs_da3_node_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100271 .name = "xfs_da3_node",
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000272 .verify_read = xfs_da3_node_read_verify,
273 .verify_write = xfs_da3_node_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800274 .verify_struct = xfs_da3_node_verify_struct,
Dave Chinner1813dd62012-11-14 17:54:40 +1100275};
276
Dave Chinnerd9392a42012-11-12 22:54:17 +1100277int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000278xfs_da3_node_read(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100279 struct xfs_trans *tp,
280 struct xfs_inode *dp,
281 xfs_dablk_t bno,
282 xfs_daddr_t mappedbno,
283 struct xfs_buf **bpp,
284 int which_fork)
285{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100286 int err;
287
288 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000289 which_fork, &xfs_da3_node_buf_ops);
Darrick J. Wongcd87d862017-07-07 18:55:17 -0700290 if (!err && tp && *bpp) {
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100291 struct xfs_da_blkinfo *info = (*bpp)->b_addr;
292 int type;
293
294 switch (be16_to_cpu(info->magic)) {
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100295 case XFS_DA_NODE_MAGIC:
Dave Chinnercab09a82013-04-30 21:39:36 +1000296 case XFS_DA3_NODE_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100297 type = XFS_BLFT_DA_NODE_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100298 break;
299 case XFS_ATTR_LEAF_MAGIC:
300 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100301 type = XFS_BLFT_ATTR_LEAF_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100302 break;
303 case XFS_DIR2_LEAFN_MAGIC:
304 case XFS_DIR3_LEAFN_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100305 type = XFS_BLFT_DIR_LEAFN_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100306 break;
307 default:
Darrick J. Wong924cade2018-06-03 16:10:18 -0700308 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
Darrick J. Wong2551a532018-06-04 10:23:54 -0700309 tp->t_mountp, info, sizeof(*info));
Darrick J. Wong924cade2018-06-03 16:10:18 -0700310 xfs_trans_brelse(tp, *bpp);
311 *bpp = NULL;
312 return -EFSCORRUPTED;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100313 }
314 xfs_trans_buf_set_type(tp, *bpp, type);
315 }
316 return err;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*========================================================================
320 * Routines used for growing the Btree.
321 *========================================================================*/
322
323/*
324 * Create the initial contents of an intermediate node.
325 */
326int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000327xfs_da3_node_create(
328 struct xfs_da_args *args,
329 xfs_dablk_t blkno,
330 int level,
331 struct xfs_buf **bpp,
332 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000334 struct xfs_da_intnode *node;
335 struct xfs_trans *tp = args->trans;
336 struct xfs_mount *mp = tp->t_mountp;
337 struct xfs_da3_icnode_hdr ichdr = {0};
338 struct xfs_buf *bp;
339 int error;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100340 struct xfs_inode *dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dave Chinner5a5881c2012-03-22 05:15:13 +0000342 trace_xfs_da_node_create(args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000343 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
Dave Chinner5a5881c2012-03-22 05:15:13 +0000344
Dave Chinner01ba43b2013-10-29 22:11:52 +1100345 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000347 return error;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100348 bp->b_ops = &xfs_da3_node_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100349 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000350 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000352 if (xfs_sb_version_hascrc(&mp->m_sb)) {
353 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
354
Brian Fostera45086e2015-10-12 15:59:25 +1100355 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000356 ichdr.magic = XFS_DA3_NODE_MAGIC;
357 hdr3->info.blkno = cpu_to_be64(bp->b_bn);
358 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000359 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000360 } else {
361 ichdr.magic = XFS_DA_NODE_MAGIC;
362 }
363 ichdr.level = level;
364
Dave Chinner01ba43b2013-10-29 22:11:52 +1100365 dp->d_ops->node_hdr_to_disk(node, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000366 xfs_trans_log_buf(tp, bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100367 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 *bpp = bp;
Eric Sandeend99831f2014-06-22 15:03:54 +1000370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373/*
374 * Split a leaf node, rebalance, then possibly split
375 * intermediate nodes, rebalance, etc.
376 */
377int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000378xfs_da3_split(
379 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000381 struct xfs_da_state_blk *oldblk;
382 struct xfs_da_state_blk *newblk;
383 struct xfs_da_state_blk *addblk;
384 struct xfs_da_intnode *node;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000385 int max;
Dave Chinner836a94a2013-08-12 20:49:44 +1000386 int action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000387 int error;
388 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dave Chinner5a5881c2012-03-22 05:15:13 +0000390 trace_xfs_da_split(state->args);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
393 * Walk back up the tree splitting/inserting/adjusting as necessary.
394 * If we need to insert and there isn't room, split the node, then
395 * decide which fragment to insert the new block from below into.
396 * Note that we may split the root this way, but we need more fixup.
397 */
398 max = state->path.active - 1;
399 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
400 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000401 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 addblk = &state->path.blk[max]; /* initial dummy value */
404 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
405 oldblk = &state->path.blk[i];
406 newblk = &state->altpath.blk[i];
407
408 /*
409 * If a leaf node then
410 * Allocate a new leaf node, then rebalance across them.
411 * else if an intermediate node then
412 * We split on the last layer, must we split the node?
413 */
414 switch (oldblk->magic) {
415 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000416 error = xfs_attr3_leaf_split(state, oldblk, newblk);
Dave Chinner24513372014-06-25 14:58:08 +1000417 if ((error != 0) && (error != -ENOSPC)) {
Eric Sandeend99831f2014-06-22 15:03:54 +1000418 return error; /* GROT: attr is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 if (!error) {
421 addblk = newblk;
422 break;
423 }
424 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000425 * Entry wouldn't fit, split the leaf again. The new
426 * extrablk will be consumed by xfs_da3_node_split if
427 * the node is split.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
429 state->extravalid = 1;
430 if (state->inleaf) {
431 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000432 trace_xfs_attr_leaf_split_before(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000433 error = xfs_attr3_leaf_split(state, oldblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 &state->extrablk);
435 } else {
436 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000437 trace_xfs_attr_leaf_split_after(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000438 error = xfs_attr3_leaf_split(state, newblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 &state->extrablk);
440 }
441 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000442 return error; /* GROT: attr inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 addblk = newblk;
444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 error = xfs_dir2_leafn_split(state, oldblk, newblk);
447 if (error)
448 return error;
449 addblk = newblk;
450 break;
451 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000452 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 addblk->bp = NULL;
455 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000456 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * Record the newly split block for the next time thru?
459 */
460 if (action)
461 addblk = newblk;
462 else
463 addblk = NULL;
464 break;
465 }
466
467 /*
468 * Update the btree to show the new hashval for this child.
469 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000470 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 if (!addblk)
Eric Sandeend99831f2014-06-22 15:03:54 +1000473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000476 * xfs_da3_node_split() should have consumed any extra blocks we added
477 * during a double leaf split in the attr fork. This is guaranteed as
478 * we can't be here if the attr fork only has a single leaf block.
479 */
480 ASSERT(state->extravalid == 0 ||
481 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
482
483 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * Split the root node.
485 */
486 ASSERT(state->path.active == 0);
487 oldblk = &state->path.blk[0];
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000488 error = xfs_da3_root_split(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 addblk->bp = NULL;
Eric Sandeend99831f2014-06-22 15:03:54 +1000491 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494 /*
Dave Chinner160ae762016-07-22 09:51:05 +1000495 * Update pointers to the node which used to be block 0 and just got
496 * bumped because of the addition of a new root node. Note that the
497 * original block 0 could be at any position in the list of blocks in
498 * the tree.
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000499 *
Dave Chinner160ae762016-07-22 09:51:05 +1000500 * Note: the magic numbers and sibling pointers are in the same physical
501 * place for both v2 and v3 headers (by design). Hence it doesn't matter
502 * which version of the xfs_da_intnode structure we use here as the
503 * result will be the same using either structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000505 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (node->hdr.info.forw) {
Dave Chinner160ae762016-07-22 09:51:05 +1000507 ASSERT(be32_to_cpu(node->hdr.info.forw) == addblk->blkno);
508 node = addblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100509 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner160ae762016-07-22 09:51:05 +1000510 xfs_trans_log_buf(state->args->trans, addblk->bp,
511 XFS_DA_LOGRANGE(node, &node->hdr.info,
512 sizeof(node->hdr.info)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100515 if (node->hdr.info.back) {
Dave Chinner160ae762016-07-22 09:51:05 +1000516 ASSERT(be32_to_cpu(node->hdr.info.back) == addblk->blkno);
517 node = addblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100518 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner160ae762016-07-22 09:51:05 +1000519 xfs_trans_log_buf(state->args->trans, addblk->bp,
520 XFS_DA_LOGRANGE(node, &node->hdr.info,
521 sizeof(node->hdr.info)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 addblk->bp = NULL;
Eric Sandeend99831f2014-06-22 15:03:54 +1000524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
528 * Split the root. We have to create a new root and point to the two
529 * parts (the split old root) that we just created. Copy block zero to
530 * the EOF, extending the inode in process.
531 */
532STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000533xfs_da3_root_split(
534 struct xfs_da_state *state,
535 struct xfs_da_state_blk *blk1,
536 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000538 struct xfs_da_intnode *node;
539 struct xfs_da_intnode *oldroot;
540 struct xfs_da_node_entry *btree;
541 struct xfs_da3_icnode_hdr nodehdr;
542 struct xfs_da_args *args;
543 struct xfs_buf *bp;
544 struct xfs_inode *dp;
545 struct xfs_trans *tp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000546 struct xfs_dir2_leaf *leaf;
547 xfs_dablk_t blkno;
548 int level;
549 int error;
550 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dave Chinner5a5881c2012-03-22 05:15:13 +0000552 trace_xfs_da_root_split(state->args);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
555 * Copy the existing (incorrect) block from the root node position
556 * to a free space somewhere.
557 */
558 args = state->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 error = xfs_da_grow_inode(args, &blkno);
560 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000561 return error;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 dp = args->dp;
564 tp = args->trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
566 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000567 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000568 node = bp->b_addr;
569 oldroot = blk1->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000570 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
571 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
Fabian Frederick29916df2015-03-25 14:55:25 +1100572 struct xfs_da3_icnode_hdr icnodehdr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000573
Fabian Frederick29916df2015-03-25 14:55:25 +1100574 dp->d_ops->node_hdr_from_disk(&icnodehdr, oldroot);
Dave Chinner4bceb182013-10-29 22:11:51 +1100575 btree = dp->d_ops->node_tree_p(oldroot);
Fabian Frederick29916df2015-03-25 14:55:25 +1100576 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
577 level = icnodehdr.level;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100578
579 /*
580 * we are about to copy oldroot to bp, so set up the type
581 * of bp while we know exactly what it will be.
582 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100583 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000585 struct xfs_dir3_icleaf_hdr leafhdr;
586 struct xfs_dir2_leaf_entry *ents;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 leaf = (xfs_dir2_leaf_t *)oldroot;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100589 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100590 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000591
592 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
593 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
594 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000595 level = 0;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100596
597 /*
598 * we are about to copy oldroot to bp, so set up the type
599 * of bp while we know exactly what it will be.
600 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100601 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000603
604 /*
605 * we can copy most of the information in the node from one block to
606 * another, but for CRC enabled headers we have to make sure that the
607 * block specific identifiers are kept intact. We update the buffer
608 * directly for this.
609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 memcpy(node, oldroot, size);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000611 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
612 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
613 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
614
615 node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
616 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000617 xfs_trans_log_buf(tp, bp, 0, size - 1);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100618
Dave Chinner1813dd62012-11-14 17:54:40 +1100619 bp->b_ops = blk1->bp->b_ops;
Dave Chinner0a4edc82013-09-02 10:32:01 +1000620 xfs_trans_buf_copy_type(bp, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 blk1->bp = bp;
622 blk1->blkno = blkno;
623
624 /*
625 * Set up the new root node.
626 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000627 error = xfs_da3_node_create(args,
Dave Chinner7dda6e82014-06-06 15:11:18 +1000628 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000629 level + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000631 return error;
632
Dave Chinner1d9025e2012-06-22 18:50:14 +1000633 node = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100634 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +1100635 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000636 btree[0].hashval = cpu_to_be32(blk1->hashval);
637 btree[0].before = cpu_to_be32(blk1->blkno);
638 btree[1].hashval = cpu_to_be32(blk2->hashval);
639 btree[1].before = cpu_to_be32(blk2->blkno);
640 nodehdr.count = 2;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100641 dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +1000644 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
645 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
Dave Chinner7dda6e82014-06-06 15:11:18 +1000646 ASSERT(blk1->blkno >= args->geo->leafblk &&
647 blk1->blkno < args->geo->freeblk);
648 ASSERT(blk2->blkno >= args->geo->leafblk &&
649 blk2->blkno < args->geo->freeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651#endif
652
653 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000654 xfs_trans_log_buf(tp, bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000655 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
661 * Split the node, rebalance, then add the new entry.
662 */
663STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000664xfs_da3_node_split(
665 struct xfs_da_state *state,
666 struct xfs_da_state_blk *oldblk,
667 struct xfs_da_state_blk *newblk,
668 struct xfs_da_state_blk *addblk,
669 int treelevel,
670 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000672 struct xfs_da_intnode *node;
673 struct xfs_da3_icnode_hdr nodehdr;
674 xfs_dablk_t blkno;
675 int newcount;
676 int error;
677 int useextra;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100678 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Chinner5a5881c2012-03-22 05:15:13 +0000680 trace_xfs_da_node_split(state->args);
681
Dave Chinner1d9025e2012-06-22 18:50:14 +1000682 node = oldblk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100683 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000686 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000688 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 newcount = 1 + useextra;
690 /*
691 * Do we have to split the node?
692 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +1000693 if (nodehdr.count + newcount > state->args->geo->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 /*
695 * Allocate a new node, add to the doubly linked chain of
696 * nodes, then move some of our excess entries into it.
697 */
698 error = xfs_da_grow_inode(state->args, &blkno);
699 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000700 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000702 error = xfs_da3_node_create(state->args, blkno, treelevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 &newblk->bp, state->args->whichfork);
704 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000705 return error; /* GROT: dir is inconsistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 newblk->blkno = blkno;
707 newblk->magic = XFS_DA_NODE_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000708 xfs_da3_node_rebalance(state, oldblk, newblk);
709 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000711 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 *result = 1;
713 } else {
714 *result = 0;
715 }
716
717 /*
718 * Insert the new entry(s) into the correct block
719 * (updating last hashval in the process).
720 *
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000721 * xfs_da3_node_add() inserts BEFORE the given index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 * and as a result of using node_lookup_int() we always
723 * point to a valid entry (not after one), but a split
724 * operation always results in a new block whose hashvals
725 * FOLLOW the current block.
726 *
727 * If we had double-split op below us, then add the extra block too.
728 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000729 node = oldblk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100730 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000731 if (oldblk->index <= nodehdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000733 xfs_da3_node_add(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (useextra) {
735 if (state->extraafter)
736 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000737 xfs_da3_node_add(state, oldblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 state->extravalid = 0;
739 }
740 } else {
741 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000742 xfs_da3_node_add(state, newblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (useextra) {
744 if (state->extraafter)
745 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000746 xfs_da3_node_add(state, newblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 state->extravalid = 0;
748 }
749 }
750
Eric Sandeend99831f2014-06-22 15:03:54 +1000751 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*
755 * Balance the btree elements between two intermediate nodes,
756 * usually one full and one empty.
757 *
758 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
759 */
760STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000761xfs_da3_node_rebalance(
762 struct xfs_da_state *state,
763 struct xfs_da_state_blk *blk1,
764 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000766 struct xfs_da_intnode *node1;
767 struct xfs_da_intnode *node2;
768 struct xfs_da_intnode *tmpnode;
769 struct xfs_da_node_entry *btree1;
770 struct xfs_da_node_entry *btree2;
771 struct xfs_da_node_entry *btree_s;
772 struct xfs_da_node_entry *btree_d;
773 struct xfs_da3_icnode_hdr nodehdr1;
774 struct xfs_da3_icnode_hdr nodehdr2;
775 struct xfs_trans *tp;
776 int count;
777 int tmp;
778 int swap = 0;
Dave Chinner4bceb182013-10-29 22:11:51 +1100779 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Chinner5a5881c2012-03-22 05:15:13 +0000781 trace_xfs_da_node_rebalance(state->args);
782
Dave Chinner1d9025e2012-06-22 18:50:14 +1000783 node1 = blk1->bp->b_addr;
784 node2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100785 dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
786 dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100787 btree1 = dp->d_ops->node_tree_p(node1);
788 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * Figure out how many entries need to move, and in which direction.
792 * Swap the nodes around if that makes it simpler.
793 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000794 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
795 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
796 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
797 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 tmpnode = node1;
799 node1 = node2;
800 node2 = tmpnode;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100801 dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
802 dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100803 btree1 = dp->d_ops->node_tree_p(node1);
804 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000805 swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000807
808 count = (nodehdr1.count - nodehdr2.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (count == 0)
810 return;
811 tp = state->args->trans;
812 /*
813 * Two cases: high-to-low and low-to-high.
814 */
815 if (count > 0) {
816 /*
817 * Move elements in node2 up to make a hole.
818 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000819 tmp = nodehdr2.count;
820 if (tmp > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000822 btree_s = &btree2[0];
823 btree_d = &btree2[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 memmove(btree_d, btree_s, tmp);
825 }
826
827 /*
828 * Move the req'd B-tree elements from high in node1 to
829 * low in node2.
830 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000831 nodehdr2.count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000833 btree_s = &btree1[nodehdr1.count - count];
834 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000836 nodehdr1.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 } else {
838 /*
839 * Move the req'd B-tree elements from low in node2 to
840 * high in node1.
841 */
842 count = -count;
843 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000844 btree_s = &btree2[0];
845 btree_d = &btree1[nodehdr1.count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000847 nodehdr1.count += count;
848
Dave Chinner1d9025e2012-06-22 18:50:14 +1000849 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 XFS_DA_LOGRANGE(node1, btree_d, tmp));
851
852 /*
853 * Move elements in node2 down to fill the hole.
854 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000855 tmp = nodehdr2.count - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000857 btree_s = &btree2[count];
858 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 memmove(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000860 nodehdr2.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
863 /*
864 * Log header of node 1 and all current bits of node 2.
865 */
Dave Chinner01ba43b2013-10-29 22:11:52 +1100866 dp->d_ops->node_hdr_to_disk(node1, &nodehdr1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000867 xfs_trans_log_buf(tp, blk1->bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100868 XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000869
Dave Chinner01ba43b2013-10-29 22:11:52 +1100870 dp->d_ops->node_hdr_to_disk(node2, &nodehdr2);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000871 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 XFS_DA_LOGRANGE(node2, &node2->hdr,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100873 dp->d_ops->node_hdr_size +
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000874 (sizeof(btree2[0]) * nodehdr2.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /*
877 * Record the last hashval from each block for upward propagation.
878 * (note: don't use the swapped node pointers)
879 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000880 if (swap) {
881 node1 = blk1->bp->b_addr;
882 node2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100883 dp->d_ops->node_hdr_from_disk(&nodehdr1, node1);
884 dp->d_ops->node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100885 btree1 = dp->d_ops->node_tree_p(node1);
886 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000887 }
888 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
889 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Adjust the expected index for insertion.
893 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000894 if (blk1->index >= nodehdr1.count) {
895 blk2->index = blk1->index - nodehdr1.count;
896 blk1->index = nodehdr1.count + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898}
899
900/*
901 * Add a new entry to an intermediate node.
902 */
903STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000904xfs_da3_node_add(
905 struct xfs_da_state *state,
906 struct xfs_da_state_blk *oldblk,
907 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000909 struct xfs_da_intnode *node;
910 struct xfs_da3_icnode_hdr nodehdr;
911 struct xfs_da_node_entry *btree;
912 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +1100913 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Dave Chinner5a5881c2012-03-22 05:15:13 +0000915 trace_xfs_da_node_add(state->args);
916
Dave Chinner1d9025e2012-06-22 18:50:14 +1000917 node = oldblk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100918 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +1100919 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000920
921 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000923 if (state->args->whichfork == XFS_DATA_FORK)
Dave Chinner7dda6e82014-06-06 15:11:18 +1000924 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
925 newblk->blkno < state->args->geo->freeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /*
928 * We may need to make some room before we insert the new node.
929 */
930 tmp = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000931 if (oldblk->index < nodehdr.count) {
932 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
933 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000935 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
936 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000937 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000938 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
939 tmp + sizeof(*btree)));
940
941 nodehdr.count += 1;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100942 dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000943 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100944 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /*
947 * Copy the last hash value from the oldblk to propagate upwards.
948 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000949 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/*========================================================================
953 * Routines used for shrinking the Btree.
954 *========================================================================*/
955
956/*
957 * Deallocate an empty leaf node, remove it from its parent,
958 * possibly deallocating that block, etc...
959 */
960int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000961xfs_da3_join(
962 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000964 struct xfs_da_state_blk *drop_blk;
965 struct xfs_da_state_blk *save_blk;
966 int action = 0;
967 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Chinner5a5881c2012-03-22 05:15:13 +0000969 trace_xfs_da_join(state->args);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 drop_blk = &state->path.blk[ state->path.active-1 ];
972 save_blk = &state->altpath.blk[ state->path.active-1 ];
973 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
974 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000975 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /*
978 * Walk back up the tree joining/deallocating as necessary.
979 * When we stop dropping blocks, break out.
980 */
981 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
982 state->path.active--) {
983 /*
984 * See if we can combine the block with a neighbor.
985 * (action == 0) => no options, just leave
986 * (action == 1) => coalesce, then unlink
987 * (action == 2) => block empty, unlink it
988 */
989 switch (drop_blk->magic) {
990 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000991 error = xfs_attr3_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000993 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (action == 0)
Eric Sandeend99831f2014-06-22 15:03:54 +1000995 return 0;
Dave Chinner517c2222013-04-24 18:58:55 +1000996 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 error = xfs_dir2_leafn_toosmall(state, &action);
1000 if (error)
1001 return error;
1002 if (action == 0)
1003 return 0;
1004 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1005 break;
1006 case XFS_DA_NODE_MAGIC:
1007 /*
1008 * Remove the offending node, fixup hashvals,
1009 * check for a toosmall neighbor.
1010 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001011 xfs_da3_node_remove(state, drop_blk);
1012 xfs_da3_fixhashpath(state, &state->path);
1013 error = xfs_da3_node_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001015 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (action == 0)
1017 return 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001018 xfs_da3_node_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
1020 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001021 xfs_da3_fixhashpath(state, &state->altpath);
1022 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 xfs_da_state_kill_altpath(state);
1024 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001025 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1027 drop_blk->bp);
1028 drop_blk->bp = NULL;
1029 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001030 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 /*
1033 * We joined all the way to the top. If it turns out that
1034 * we only have one entry in the root, make the child block
1035 * the new root.
1036 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001037 xfs_da3_node_remove(state, drop_blk);
1038 xfs_da3_fixhashpath(state, &state->path);
1039 error = xfs_da3_root_join(state, &state->path.blk[0]);
Eric Sandeend99831f2014-06-22 15:03:54 +10001040 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Alex Elder1c4f3322011-07-18 18:14:09 +00001043#ifdef DEBUG
1044static void
1045xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1046{
1047 __be16 magic = blkinfo->magic;
1048
1049 if (level == 1) {
1050 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001051 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001052 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1053 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001054 } else {
1055 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1056 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1057 }
Alex Elder1c4f3322011-07-18 18:14:09 +00001058 ASSERT(!blkinfo->forw);
1059 ASSERT(!blkinfo->back);
1060}
1061#else /* !DEBUG */
1062#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1063#endif /* !DEBUG */
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * We have only one entry in the root. Copy the only remaining child of
1067 * the old root to block 0 as the new root node.
1068 */
1069STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001070xfs_da3_root_join(
1071 struct xfs_da_state *state,
1072 struct xfs_da_state_blk *root_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001074 struct xfs_da_intnode *oldroot;
1075 struct xfs_da_args *args;
1076 xfs_dablk_t child;
1077 struct xfs_buf *bp;
1078 struct xfs_da3_icnode_hdr oldroothdr;
1079 struct xfs_da_node_entry *btree;
1080 int error;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001081 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Dave Chinner5a5881c2012-03-22 05:15:13 +00001083 trace_xfs_da_root_join(state->args);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001086
1087 args = state->args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001088 oldroot = root_blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001089 dp->d_ops->node_hdr_from_disk(&oldroothdr, oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001090 ASSERT(oldroothdr.forw == 0);
1091 ASSERT(oldroothdr.back == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /*
1094 * If the root has more than one child, then don't do anything.
1095 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001096 if (oldroothdr.count > 1)
1097 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /*
1100 * Read in the (only) child block, then copy those bytes into
1101 * the root block's buffer and free the original child block.
1102 */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001103 btree = dp->d_ops->node_tree_p(oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001104 child = be32_to_cpu(btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ASSERT(child != 0);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001106 error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001107 args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001109 return error;
1110 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
Alex Elder1c4f3322011-07-18 18:14:09 +00001111
Dave Chinner612cfbf2012-11-14 17:52:32 +11001112 /*
1113 * This could be copying a leaf back into the root block in the case of
1114 * there only being a single leaf block left in the tree. Hence we have
Dave Chinner1813dd62012-11-14 17:54:40 +11001115 * to update the b_ops pointer as well to match the buffer type change
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001116 * that could occur. For dir3 blocks we also need to update the block
1117 * number in the buffer header.
Dave Chinner612cfbf2012-11-14 17:52:32 +11001118 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001119 memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
Dave Chinner1813dd62012-11-14 17:54:40 +11001120 root_blk->bp->b_ops = bp->b_ops;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001121 xfs_trans_buf_copy_type(root_blk->bp, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001122 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1123 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1124 da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
1125 }
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001126 xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1127 args->geo->blksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 error = xfs_da_shrink_inode(args, child, bp);
Eric Sandeend99831f2014-06-22 15:03:54 +10001129 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/*
1133 * Check a node block and its neighbors to see if the block should be
1134 * collapsed into one or the other neighbor. Always keep the block
1135 * with the smaller block number.
1136 * If the current block is over 50% full, don't try to join it, return 0.
1137 * If the block is empty, fill in the state structure and return 2.
1138 * If it can be collapsed, fill in the state structure and return 1.
1139 * If nothing can be done, return 0.
1140 */
1141STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001142xfs_da3_node_toosmall(
1143 struct xfs_da_state *state,
1144 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001146 struct xfs_da_intnode *node;
1147 struct xfs_da_state_blk *blk;
1148 struct xfs_da_blkinfo *info;
1149 xfs_dablk_t blkno;
1150 struct xfs_buf *bp;
1151 struct xfs_da3_icnode_hdr nodehdr;
1152 int count;
1153 int forward;
1154 int error;
1155 int retval;
1156 int i;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001157 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Dave Chinneree732592012-11-12 22:53:53 +11001159 trace_xfs_da_node_toosmall(state->args);
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Check for the degenerate case of the block being over 50% full.
1163 * If so, it's not worth even looking to see if we might be able
1164 * to coalesce with a sibling.
1165 */
1166 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001167 info = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 node = (xfs_da_intnode_t *)info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001169 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001170 if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 *action = 0; /* blk over 50%, don't try to join */
Eric Sandeend99831f2014-06-22 15:03:54 +10001172 return 0; /* blk over 50%, don't try to join */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
1175 /*
1176 * Check for the degenerate case of the block being empty.
1177 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001178 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * to merge with the forward block unless it is NULL.
1180 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001181 if (nodehdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /*
1183 * Make altpath point to the block we want to keep and
1184 * path point to the block we want to drop (this one).
1185 */
Nathan Scott89da0542006-03-17 17:28:40 +11001186 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001188 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 0, &retval);
1190 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001191 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (retval) {
1193 *action = 0;
1194 } else {
1195 *action = 2;
1196 }
Eric Sandeend99831f2014-06-22 15:03:54 +10001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
1200 /*
1201 * Examine each sibling block to see if we can coalesce with
1202 * at least 25% free space to spare. We need to figure out
1203 * whether to merge with the forward or the backward block.
1204 * We prefer coalescing with the lower numbered sibling so as
1205 * to shrink a directory over time.
1206 */
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001207 count = state->args->geo->node_ents;
1208 count -= state->args->geo->node_ents >> 2;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001209 count -= nodehdr.count;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* start with smaller blk num */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001212 forward = nodehdr.forw < nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 for (i = 0; i < 2; forward = !forward, i++) {
Mark Tinguely997def22013-09-23 12:18:58 -05001214 struct xfs_da3_icnode_hdr thdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (forward)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001216 blkno = nodehdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001218 blkno = nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (blkno == 0)
1220 continue;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001221 error = xfs_da3_node_read(state->args->trans, dp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001222 blkno, -1, &bp, state->args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001224 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Chinner1d9025e2012-06-22 18:50:14 +10001226 node = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001227 dp->d_ops->node_hdr_from_disk(&thdr, node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001228 xfs_trans_brelse(state->args->trans, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001229
Mark Tinguely997def22013-09-23 12:18:58 -05001230 if (count - thdr.count >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 break; /* fits with at least 25% to spare */
1232 }
1233 if (i >= 2) {
1234 *action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
1238 /*
1239 * Make altpath point to the block we want to keep (the lower
1240 * numbered block) and path point to the block we want to drop.
1241 */
1242 memcpy(&state->altpath, &state->path, sizeof(state->path));
1243 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001244 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 0, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001247 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 0, &retval);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001249 }
1250 if (error)
1251 return error;
1252 if (retval) {
1253 *action = 0;
1254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256 *action = 1;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001257 return 0;
1258}
1259
1260/*
1261 * Pick up the last hashvalue from an intermediate node.
1262 */
1263STATIC uint
1264xfs_da3_node_lasthash(
Dave Chinner4bceb182013-10-29 22:11:51 +11001265 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001266 struct xfs_buf *bp,
1267 int *count)
1268{
1269 struct xfs_da_intnode *node;
1270 struct xfs_da_node_entry *btree;
1271 struct xfs_da3_icnode_hdr nodehdr;
1272
1273 node = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001274 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001275 if (count)
1276 *count = nodehdr.count;
1277 if (!nodehdr.count)
1278 return 0;
Dave Chinner4bceb182013-10-29 22:11:51 +11001279 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001280 return be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283/*
1284 * Walk back up the tree adjusting hash values as necessary,
1285 * when we stop making changes, return.
1286 */
1287void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001288xfs_da3_fixhashpath(
1289 struct xfs_da_state *state,
1290 struct xfs_da_state_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001292 struct xfs_da_state_blk *blk;
1293 struct xfs_da_intnode *node;
1294 struct xfs_da_node_entry *btree;
1295 xfs_dahash_t lasthash=0;
1296 int level;
1297 int count;
Dave Chinner4bceb182013-10-29 22:11:51 +11001298 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Dave Chinneree732592012-11-12 22:53:53 +11001300 trace_xfs_da_fixhashpath(state->args);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 level = path->active-1;
1303 blk = &path->blk[ level ];
1304 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 case XFS_ATTR_LEAF_MAGIC:
1306 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1307 if (count == 0)
1308 return;
1309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 case XFS_DIR2_LEAFN_MAGIC:
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001311 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (count == 0)
1313 return;
1314 break;
1315 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001316 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (count == 0)
1318 return;
1319 break;
1320 }
1321 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001322 struct xfs_da3_icnode_hdr nodehdr;
1323
Dave Chinner1d9025e2012-06-22 18:50:14 +10001324 node = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001325 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001326 btree = dp->d_ops->node_tree_p(node);
Mark Tinguelyc88547a2014-04-04 07:10:49 +11001327 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
1329 blk->hashval = lasthash;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001330 btree[blk->index].hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001331 xfs_trans_log_buf(state->args->trans, blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001332 XFS_DA_LOGRANGE(node, &btree[blk->index],
1333 sizeof(*btree)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001335 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337}
1338
1339/*
1340 * Remove an entry from an intermediate node.
1341 */
1342STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001343xfs_da3_node_remove(
1344 struct xfs_da_state *state,
1345 struct xfs_da_state_blk *drop_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001347 struct xfs_da_intnode *node;
1348 struct xfs_da3_icnode_hdr nodehdr;
1349 struct xfs_da_node_entry *btree;
1350 int index;
1351 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001352 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dave Chinner5a5881c2012-03-22 05:15:13 +00001354 trace_xfs_da_node_remove(state->args);
1355
Dave Chinner1d9025e2012-06-22 18:50:14 +10001356 node = drop_blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001357 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001358 ASSERT(drop_blk->index < nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 ASSERT(drop_blk->index >= 0);
1360
1361 /*
1362 * Copy over the offending entry, or just zero it out.
1363 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001364 index = drop_blk->index;
Dave Chinner4bceb182013-10-29 22:11:51 +11001365 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001366 if (index < nodehdr.count - 1) {
1367 tmp = nodehdr.count - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001369 memmove(&btree[index], &btree[index + 1], tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001370 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001371 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1372 index = nodehdr.count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001374 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001375 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001376 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1377 nodehdr.count -= 1;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001378 dp->d_ops->node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001379 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001380 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /*
1383 * Copy the last hash value from the block to propagate upwards.
1384 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001385 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001389 * Unbalance the elements between two intermediate nodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 * move all Btree elements from one node into another.
1391 */
1392STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001393xfs_da3_node_unbalance(
1394 struct xfs_da_state *state,
1395 struct xfs_da_state_blk *drop_blk,
1396 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001398 struct xfs_da_intnode *drop_node;
1399 struct xfs_da_intnode *save_node;
1400 struct xfs_da_node_entry *drop_btree;
1401 struct xfs_da_node_entry *save_btree;
1402 struct xfs_da3_icnode_hdr drop_hdr;
1403 struct xfs_da3_icnode_hdr save_hdr;
1404 struct xfs_trans *tp;
1405 int sindex;
1406 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001407 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Dave Chinner5a5881c2012-03-22 05:15:13 +00001409 trace_xfs_da_node_unbalance(state->args);
1410
Dave Chinner1d9025e2012-06-22 18:50:14 +10001411 drop_node = drop_blk->bp->b_addr;
1412 save_node = save_blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001413 dp->d_ops->node_hdr_from_disk(&drop_hdr, drop_node);
1414 dp->d_ops->node_hdr_from_disk(&save_hdr, save_node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001415 drop_btree = dp->d_ops->node_tree_p(drop_node);
1416 save_btree = dp->d_ops->node_tree_p(save_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 tp = state->args->trans;
1418
1419 /*
1420 * If the dying block has lower hashvals, then move all the
1421 * elements in the remaining block up to make a hole.
1422 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001423 if ((be32_to_cpu(drop_btree[0].hashval) <
1424 be32_to_cpu(save_btree[0].hashval)) ||
1425 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1426 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1427 /* XXX: check this - is memmove dst correct? */
1428 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1429 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1430
1431 sindex = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001432 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001433 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1434 (save_hdr.count + drop_hdr.count) *
1435 sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001437 sindex = save_hdr.count;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001438 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001439 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1440 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442
1443 /*
1444 * Move all the B-tree elements from drop_blk to save_blk.
1445 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001446 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1447 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1448 save_hdr.count += drop_hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dave Chinner01ba43b2013-10-29 22:11:52 +11001450 dp->d_ops->node_hdr_to_disk(save_node, &save_hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001451 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001453 dp->d_ops->node_hdr_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /*
1456 * Save the last hashval in the remaining block for upward propagation.
1457 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001458 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*========================================================================
1462 * Routines used for finding things in the Btree.
1463 *========================================================================*/
1464
1465/*
1466 * Walk down the Btree looking for a particular filename, filling
1467 * in the state structure as we go.
1468 *
1469 * We will set the state structure to point to each of the elements
1470 * in each of the nodes where either the hashval is or should be.
1471 *
1472 * We support duplicate hashval's so for each entry in the current
1473 * node that could contain the desired hashval, descend. This is a
1474 * pruned depth-first tree search.
1475 */
1476int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001477xfs_da3_node_lookup_int(
1478 struct xfs_da_state *state,
1479 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001481 struct xfs_da_state_blk *blk;
1482 struct xfs_da_blkinfo *curr;
1483 struct xfs_da_intnode *node;
1484 struct xfs_da_node_entry *btree;
1485 struct xfs_da3_icnode_hdr nodehdr;
1486 struct xfs_da_args *args;
1487 xfs_dablk_t blkno;
1488 xfs_dahash_t hashval;
1489 xfs_dahash_t btreehashval;
1490 int probe;
1491 int span;
1492 int max;
1493 int error;
1494 int retval;
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001495 unsigned int expected_level = 0;
Dave Chinner4bceb182013-10-29 22:11:51 +11001496 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 args = state->args;
1499
1500 /*
1501 * Descend thru the B-tree searching each level for the right
1502 * node to use, until the right hashval is found.
1503 */
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001504 blkno = args->geo->leafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 for (blk = &state->path.blk[0], state->path.active = 1;
1506 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1507 blk++, state->path.active++) {
1508 /*
1509 * Read the next node down in the tree.
1510 */
1511 blk->blkno = blkno;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001512 error = xfs_da3_node_read(args->trans, args->dp, blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001513 -1, &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (error) {
1515 blk->blkno = 0;
1516 state->path.active--;
Eric Sandeend99831f2014-06-22 15:03:54 +10001517 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001519 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001520 blk->magic = be16_to_cpu(curr->magic);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001521
Dave Chinner517c2222013-04-24 18:58:55 +10001522 if (blk->magic == XFS_ATTR_LEAF_MAGIC ||
1523 blk->magic == XFS_ATTR3_LEAF_MAGIC) {
1524 blk->magic = XFS_ATTR_LEAF_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001525 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1526 break;
1527 }
1528
1529 if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1530 blk->magic == XFS_DIR3_LEAFN_MAGIC) {
1531 blk->magic = XFS_DIR2_LEAFN_MAGIC;
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001532 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1533 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001534 break;
1535 }
1536
1537 blk->magic = XFS_DA_NODE_MAGIC;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 /*
1541 * Search an intermediate node for a match.
1542 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001543 node = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001544 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001545 btree = dp->d_ops->node_tree_p(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001547 /* Tree taller than we can handle; bail out! */
1548 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
1549 return -EFSCORRUPTED;
1550
1551 /* Check the level from the root. */
1552 if (blkno == args->geo->leafblk)
1553 expected_level = nodehdr.level - 1;
1554 else if (expected_level != nodehdr.level)
1555 return -EFSCORRUPTED;
1556 else
1557 expected_level--;
1558
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001559 max = nodehdr.count;
1560 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001562 /*
1563 * Binary search. (note: small blocks will skip loop)
1564 */
1565 probe = span = max / 2;
1566 hashval = args->hashval;
1567 while (span > 4) {
1568 span /= 2;
1569 btreehashval = be32_to_cpu(btree[probe].hashval);
1570 if (btreehashval < hashval)
1571 probe += span;
1572 else if (btreehashval > hashval)
1573 probe -= span;
1574 else
1575 break;
1576 }
1577 ASSERT((probe >= 0) && (probe < max));
1578 ASSERT((span <= 4) ||
1579 (be32_to_cpu(btree[probe].hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001581 /*
1582 * Since we may have duplicate hashval's, find the first
1583 * matching hashval in the node.
1584 */
1585 while (probe > 0 &&
1586 be32_to_cpu(btree[probe].hashval) >= hashval) {
1587 probe--;
1588 }
1589 while (probe < max &&
1590 be32_to_cpu(btree[probe].hashval) < hashval) {
1591 probe++;
1592 }
1593
1594 /*
1595 * Pick the right block to descend on.
1596 */
1597 if (probe == max) {
1598 blk->index = max - 1;
1599 blkno = be32_to_cpu(btree[max - 1].before);
1600 } else {
1601 blk->index = probe;
1602 blkno = be32_to_cpu(btree[probe].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001604
1605 /* We can't point back to the root. */
1606 if (blkno == args->geo->leafblk)
1607 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609
Darrick J. Wong8210f4d2017-10-25 16:59:43 -07001610 if (expected_level != 0)
1611 return -EFSCORRUPTED;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 /*
1614 * A leaf block that ends in the hashval that we are interested in
1615 * (final hashval == search hashval) means that the next block may
1616 * contain more entries with the same hashval, shift upward to the
1617 * next leaf and keep searching.
1618 */
1619 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001620 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1622 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001623 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Dave Chinner517c2222013-04-24 18:58:55 +10001624 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 blk->index = args->index;
1626 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001627 } else {
1628 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001629 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
Dave Chinner24513372014-06-25 14:58:08 +10001631 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 (blk->hashval == args->hashval)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001633 error = xfs_da3_path_shift(state, &state->path, 1, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 &retval);
1635 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001636 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (retval == 0) {
1638 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001639 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /* path_shift() gives ENOENT */
Dave Chinner24513372014-06-25 14:58:08 +10001641 retval = -ENOATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 break;
1645 }
1646 *result = retval;
Eric Sandeend99831f2014-06-22 15:03:54 +10001647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650/*========================================================================
1651 * Utility routines.
1652 *========================================================================*/
1653
1654/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001655 * Compare two intermediate nodes for "order".
1656 */
1657STATIC int
1658xfs_da3_node_order(
Dave Chinner4bceb182013-10-29 22:11:51 +11001659 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001660 struct xfs_buf *node1_bp,
1661 struct xfs_buf *node2_bp)
1662{
1663 struct xfs_da_intnode *node1;
1664 struct xfs_da_intnode *node2;
1665 struct xfs_da_node_entry *btree1;
1666 struct xfs_da_node_entry *btree2;
1667 struct xfs_da3_icnode_hdr node1hdr;
1668 struct xfs_da3_icnode_hdr node2hdr;
1669
1670 node1 = node1_bp->b_addr;
1671 node2 = node2_bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001672 dp->d_ops->node_hdr_from_disk(&node1hdr, node1);
1673 dp->d_ops->node_hdr_from_disk(&node2hdr, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +11001674 btree1 = dp->d_ops->node_tree_p(node1);
1675 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001676
1677 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1678 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1679 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1680 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1681 return 1;
1682 }
1683 return 0;
1684}
1685
1686/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * Link a new block into a doubly linked list of blocks (of whatever type).
1688 */
1689int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001690xfs_da3_blk_link(
1691 struct xfs_da_state *state,
1692 struct xfs_da_state_blk *old_blk,
1693 struct xfs_da_state_blk *new_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001695 struct xfs_da_blkinfo *old_info;
1696 struct xfs_da_blkinfo *new_info;
1697 struct xfs_da_blkinfo *tmp_info;
1698 struct xfs_da_args *args;
1699 struct xfs_buf *bp;
1700 int before = 0;
1701 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001702 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /*
1705 * Set up environment.
1706 */
1707 args = state->args;
1708 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001709 old_info = old_blk->bp->b_addr;
1710 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001712 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 case XFS_ATTR_LEAF_MAGIC:
1717 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001720 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 break;
1722 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001723 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725 }
1726
1727 /*
1728 * Link blocks in appropriate order.
1729 */
1730 if (before) {
1731 /*
1732 * Link new block in before existing block.
1733 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001734 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001735 new_info->forw = cpu_to_be32(old_blk->blkno);
1736 new_info->back = old_info->back;
1737 if (old_info->back) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001738 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001739 be32_to_cpu(old_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001740 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001742 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001744 tmp_info = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001745 ASSERT(tmp_info->magic == old_info->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001746 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1747 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001748 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Nathan Scott89da0542006-03-17 17:28:40 +11001750 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 } else {
1752 /*
1753 * Link new block in after existing block.
1754 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001755 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001756 new_info->forw = old_info->forw;
1757 new_info->back = cpu_to_be32(old_blk->blkno);
1758 if (old_info->forw) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001759 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001760 be32_to_cpu(old_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001761 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001763 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001765 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001766 ASSERT(tmp_info->magic == old_info->magic);
1767 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1768 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001769 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Nathan Scott89da0542006-03-17 17:28:40 +11001771 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
Dave Chinner1d9025e2012-06-22 18:50:14 +10001774 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1775 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Eric Sandeend99831f2014-06-22 15:03:54 +10001776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * Unlink a block from a doubly linked list of blocks.
1781 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001782STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001783xfs_da3_blk_unlink(
1784 struct xfs_da_state *state,
1785 struct xfs_da_state_blk *drop_blk,
1786 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001788 struct xfs_da_blkinfo *drop_info;
1789 struct xfs_da_blkinfo *save_info;
1790 struct xfs_da_blkinfo *tmp_info;
1791 struct xfs_da_args *args;
1792 struct xfs_buf *bp;
1793 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /*
1796 * Set up environment.
1797 */
1798 args = state->args;
1799 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001800 save_info = save_blk->bp->b_addr;
1801 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001803 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001806 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1807 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1808 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1809 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /*
1812 * Unlink the leaf block from the doubly linked chain of leaves.
1813 */
Nathan Scott89da0542006-03-17 17:28:40 +11001814 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001815 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001816 save_info->back = drop_info->back;
1817 if (drop_info->back) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001818 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001819 be32_to_cpu(drop_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001820 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001822 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001824 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001825 ASSERT(tmp_info->magic == save_info->magic);
1826 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1827 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001828 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001832 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001833 save_info->forw = drop_info->forw;
1834 if (drop_info->forw) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001835 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001836 be32_to_cpu(drop_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001837 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +10001839 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001841 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001842 ASSERT(tmp_info->magic == save_info->magic);
1843 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1844 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001845 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 }
1849
Dave Chinner1d9025e2012-06-22 18:50:14 +10001850 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Eric Sandeend99831f2014-06-22 15:03:54 +10001851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854/*
1855 * Move a path "forward" or "!forward" one block at the current level.
1856 *
1857 * This routine will adjust a "path" to point to the next block
1858 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1859 * Btree, including updating pointers to the intermediate nodes between
1860 * the new bottom and the root.
1861 */
1862int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001863xfs_da3_path_shift(
1864 struct xfs_da_state *state,
1865 struct xfs_da_state_path *path,
1866 int forward,
1867 int release,
1868 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001870 struct xfs_da_state_blk *blk;
1871 struct xfs_da_blkinfo *info;
1872 struct xfs_da_intnode *node;
1873 struct xfs_da_args *args;
1874 struct xfs_da_node_entry *btree;
1875 struct xfs_da3_icnode_hdr nodehdr;
Brian Foster7df1c172015-08-19 10:32:33 +10001876 struct xfs_buf *bp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001877 xfs_dablk_t blkno = 0;
1878 int level;
1879 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001880 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Dave Chinneree732592012-11-12 22:53:53 +11001882 trace_xfs_da_path_shift(state->args);
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /*
1885 * Roll up the Btree looking for the first block where our
1886 * current index is not at the edge of the block. Note that
1887 * we skip the bottom layer because we want the sibling block.
1888 */
1889 args = state->args;
1890 ASSERT(args != NULL);
1891 ASSERT(path != NULL);
1892 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1893 level = (path->active-1) - 1; /* skip bottom layer in path */
1894 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001895 node = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001896 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001897 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001898
1899 if (forward && (blk->index < nodehdr.count - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 blk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001901 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
1903 } else if (!forward && (blk->index > 0)) {
1904 blk->index--;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001905 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907 }
1908 }
1909 if (level < 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001910 *result = -ENOENT; /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001911 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Eric Sandeend99831f2014-06-22 15:03:54 +10001912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914
1915 /*
1916 * Roll down the edge of the subtree until we reach the
1917 * same depth we were at originally.
1918 */
1919 for (blk++, level++; level < path->active; blk++, level++) {
1920 /*
Brian Foster7df1c172015-08-19 10:32:33 +10001921 * Read the next child block into a local buffer.
1922 */
1923 error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
1924 args->whichfork);
1925 if (error)
1926 return error;
1927
1928 /*
1929 * Release the old block (if it's dirty, the trans doesn't
1930 * actually let go) and swap the local buffer into the path
1931 * structure. This ensures failure of the above read doesn't set
1932 * a NULL buffer in an active slot in the path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
1934 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001935 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 blk->blkno = blkno;
Brian Foster7df1c172015-08-19 10:32:33 +10001937 blk->bp = bp;
1938
Dave Chinner1d9025e2012-06-22 18:50:14 +10001939 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001940 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001941 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001942 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001943 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001944 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1945 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001946
1947
1948 /*
1949 * Note: we flatten the magic number to a single type so we
1950 * don't have to compare against crc/non-crc types elsewhere.
1951 */
1952 switch (be16_to_cpu(info->magic)) {
1953 case XFS_DA_NODE_MAGIC:
1954 case XFS_DA3_NODE_MAGIC:
1955 blk->magic = XFS_DA_NODE_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 node = (xfs_da_intnode_t *)info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001957 dp->d_ops->node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001958 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001959 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (forward)
1961 blk->index = 0;
1962 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001963 blk->index = nodehdr.count - 1;
1964 blkno = be32_to_cpu(btree[blk->index].before);
1965 break;
1966 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001967 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001968 blk->magic = XFS_ATTR_LEAF_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 ASSERT(level == path->active-1);
1970 blk->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001971 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001972 break;
1973 case XFS_DIR2_LEAFN_MAGIC:
1974 case XFS_DIR3_LEAFN_MAGIC:
1975 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1976 ASSERT(level == path->active-1);
1977 blk->index = 0;
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001978 blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1979 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001980 break;
1981 default:
1982 ASSERT(0);
1983 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985 }
1986 *result = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001987 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990
1991/*========================================================================
1992 * Utility routines.
1993 *========================================================================*/
1994
1995/*
1996 * Implement a simple hash on a character string.
1997 * Rotate the hash value by 7 bits, then XOR each character in.
1998 * This is implemented with some source-level loop unrolling.
1999 */
2000xfs_dahash_t
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002001xfs_da_hashname(const uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 xfs_dahash_t hash;
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*
2006 * Do four characters at a time as long as we can.
2007 */
2008 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2009 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2010 (name[3] << 0) ^ rol32(hash, 7 * 4);
2011
2012 /*
2013 * Now do the rest of the characters.
2014 */
2015 switch (namelen) {
2016 case 3:
2017 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2018 rol32(hash, 7 * 3);
2019 case 2:
2020 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2021 case 1:
2022 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11002023 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return hash;
2025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Barry Naujok5163f952008-05-21 16:41:01 +10002028enum xfs_dacmp
2029xfs_da_compname(
2030 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11002031 const unsigned char *name,
2032 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10002033{
2034 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2035 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2036}
2037
2038static xfs_dahash_t
2039xfs_default_hashname(
2040 struct xfs_name *name)
2041{
2042 return xfs_da_hashname(name->name, name->len);
2043}
2044
2045const struct xfs_nameops xfs_default_nameops = {
2046 .hashname = xfs_default_hashname,
2047 .compname = xfs_da_compname
2048};
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050int
Christoph Hellwig77936d02011-07-13 13:43:49 +02002051xfs_da_grow_inode_int(
2052 struct xfs_da_args *args,
2053 xfs_fileoff_t *bno,
2054 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Christoph Hellwig77936d02011-07-13 13:43:49 +02002056 struct xfs_trans *tp = args->trans;
2057 struct xfs_inode *dp = args->dp;
2058 int w = args->whichfork;
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10002059 xfs_rfsblock_t nblks = dp->i_d.di_nblocks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002060 struct xfs_bmbt_irec map, *mapp;
2061 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /*
2064 * Find a spot in the file space to put the new block.
2065 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02002066 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2067 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 /*
2071 * Try mapping it in one filesystem block.
2072 */
2073 nmap = 1;
2074 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002075 error = xfs_bmapi_write(tp, dp, *bno, count,
2076 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 args->firstblock, args->total, &map, &nmap,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002078 args->dfops);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002079 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 ASSERT(nmap <= 1);
2083 if (nmap == 1) {
2084 mapp = &map;
2085 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002086 } else if (nmap == 0 && count > 1) {
2087 xfs_fileoff_t b;
2088 int c;
2089
2090 /*
2091 * If we didn't get it and the block might work if fragmented,
2092 * try without the CONTIG flag. Loop until we get it all.
2093 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002095 for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002097 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002098 error = xfs_bmapi_write(tp, dp, b, c,
2099 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 args->firstblock, args->total,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002101 &mapp[mapi], &nmap, args->dfops);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002102 if (error)
2103 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (nmap < 1)
2105 break;
2106 mapi += nmap;
2107 b = mapp[mapi - 1].br_startoff +
2108 mapp[mapi - 1].br_blockcount;
2109 }
2110 } else {
2111 mapi = 0;
2112 mapp = NULL;
2113 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /*
2116 * Count the blocks we got, make sure it matches the total.
2117 */
2118 for (i = 0, got = 0; i < mapi; i++)
2119 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002120 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02002122 *bno + count) {
Dave Chinner24513372014-06-25 14:58:08 +10002123 error = -ENOSPC;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002124 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002126
David Chinnera7444052008-10-30 17:38:12 +11002127 /* account for newly allocated blocks in reserved blocks total */
2128 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002129
2130out_free_map:
2131 if (mapp != &map)
2132 kmem_free(mapp);
2133 return error;
2134}
2135
2136/*
2137 * Add a block to the btree ahead of the file.
2138 * Return the new block number to the caller.
2139 */
2140int
2141xfs_da_grow_inode(
2142 struct xfs_da_args *args,
2143 xfs_dablk_t *new_blkno)
2144{
2145 xfs_fileoff_t bno;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002146 int error;
2147
Dave Chinner5a5881c2012-03-22 05:15:13 +00002148 trace_xfs_da_grow_inode(args);
2149
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002150 bno = args->geo->leafblk;
2151 error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002152 if (!error)
2153 *new_blkno = (xfs_dablk_t)bno;
2154 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
2157/*
2158 * Ick. We need to always be able to remove a btree block, even
2159 * if there's no space reservation because the filesystem is full.
2160 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2161 * It swaps the target block with the last block in the file. The
2162 * last block in the file can always be removed since it can't cause
2163 * a bmap btree split to do that.
2164 */
2165STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002166xfs_da3_swap_lastblock(
2167 struct xfs_da_args *args,
2168 xfs_dablk_t *dead_blknop,
2169 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002171 struct xfs_da_blkinfo *dead_info;
2172 struct xfs_da_blkinfo *sib_info;
2173 struct xfs_da_intnode *par_node;
2174 struct xfs_da_intnode *dead_node;
2175 struct xfs_dir2_leaf *dead_leaf2;
2176 struct xfs_da_node_entry *btree;
2177 struct xfs_da3_icnode_hdr par_hdr;
Dave Chinner4bceb182013-10-29 22:11:51 +11002178 struct xfs_inode *dp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002179 struct xfs_trans *tp;
2180 struct xfs_mount *mp;
2181 struct xfs_buf *dead_buf;
2182 struct xfs_buf *last_buf;
2183 struct xfs_buf *sib_buf;
2184 struct xfs_buf *par_buf;
2185 xfs_dahash_t dead_hash;
2186 xfs_fileoff_t lastoff;
2187 xfs_dablk_t dead_blkno;
2188 xfs_dablk_t last_blkno;
2189 xfs_dablk_t sib_blkno;
2190 xfs_dablk_t par_blkno;
2191 int error;
2192 int w;
2193 int entno;
2194 int level;
2195 int dead_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Dave Chinner5a5881c2012-03-22 05:15:13 +00002197 trace_xfs_da_swap_lastblock(args);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 dead_buf = *dead_bufp;
2200 dead_blkno = *dead_blknop;
2201 tp = args->trans;
Dave Chinner4bceb182013-10-29 22:11:51 +11002202 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 w = args->whichfork;
2204 ASSERT(w == XFS_DATA_FORK);
Dave Chinner4bceb182013-10-29 22:11:51 +11002205 mp = dp->i_mount;
Dave Chinner7dda6e82014-06-06 15:11:18 +10002206 lastoff = args->geo->freeblk;
Dave Chinner4bceb182013-10-29 22:11:51 +11002207 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (error)
2209 return error;
2210 if (unlikely(lastoff == 0)) {
2211 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
2212 mp);
Dave Chinner24513372014-06-25 14:58:08 +10002213 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 /*
2216 * Read the last block in the btree space.
2217 */
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002218 last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
Dave Chinner4bceb182013-10-29 22:11:51 +11002219 error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002220 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return error;
2222 /*
2223 * Copy the last block into the dead buffer and log it.
2224 */
Dave Chinner8f661932014-06-06 15:15:59 +10002225 memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2226 xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002227 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /*
2229 * Get values from the moved block.
2230 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002231 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2232 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2233 struct xfs_dir3_icleaf_hdr leafhdr;
2234 struct xfs_dir2_leaf_entry *ents;
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002237 dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
Dave Chinner4bceb182013-10-29 22:11:51 +11002238 ents = dp->d_ops->leaf_ents_p(dead_leaf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 dead_level = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10002240 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002242 struct xfs_da3_icnode_hdr deadhdr;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 dead_node = (xfs_da_intnode_t *)dead_info;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002245 dp->d_ops->node_hdr_from_disk(&deadhdr, dead_node);
Dave Chinner4bceb182013-10-29 22:11:51 +11002246 btree = dp->d_ops->node_tree_p(dead_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002247 dead_level = deadhdr.level;
2248 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 sib_buf = par_buf = NULL;
2251 /*
2252 * If the moved block has a left sibling, fix up the pointers.
2253 */
Nathan Scott89da0542006-03-17 17:28:40 +11002254 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002255 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002256 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002258 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002260 be32_to_cpu(sib_info->forw) != last_blkno ||
2261 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
2263 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002264 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto done;
2266 }
Nathan Scott89da0542006-03-17 17:28:40 +11002267 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002268 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2270 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 sib_buf = NULL;
2272 }
2273 /*
2274 * If the moved block has a right sibling, fix up the pointers.
2275 */
Nathan Scott89da0542006-03-17 17:28:40 +11002276 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002277 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002278 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002280 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002282 be32_to_cpu(sib_info->back) != last_blkno ||
2283 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
2285 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002286 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 goto done;
2288 }
Nathan Scott89da0542006-03-17 17:28:40 +11002289 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002290 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2292 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 sib_buf = NULL;
2294 }
Dave Chinner7dda6e82014-06-06 15:11:18 +10002295 par_blkno = args->geo->leafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 level = -1;
2297 /*
2298 * Walk down the tree looking for the parent of the moved block.
2299 */
2300 for (;;) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002301 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002302 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002304 par_node = par_buf->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002305 dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002306 if (level >= 0 && level != par_hdr.level + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
2308 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002309 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto done;
2311 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002312 level = par_hdr.level;
Dave Chinner4bceb182013-10-29 22:11:51 +11002313 btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 for (entno = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002315 entno < par_hdr.count &&
2316 be32_to_cpu(btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 entno++)
2318 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002319 if (entno == par_hdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
2321 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002322 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 goto done;
2324 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002325 par_blkno = be32_to_cpu(btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (level == dead_level + 1)
2327 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002328 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 par_buf = NULL;
2330 }
2331 /*
2332 * We're in the right parent block.
2333 * Look for the right entry.
2334 */
2335 for (;;) {
2336 for (;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002337 entno < par_hdr.count &&
2338 be32_to_cpu(btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 entno++)
2340 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002341 if (entno < par_hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002343 par_blkno = par_hdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002344 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 par_buf = NULL;
2346 if (unlikely(par_blkno == 0)) {
2347 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
2348 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002349 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 goto done;
2351 }
Dave Chinner4bceb182013-10-29 22:11:51 +11002352 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002355 par_node = par_buf->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002356 dp->d_ops->node_hdr_from_disk(&par_hdr, par_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002357 if (par_hdr.level != level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
2359 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002360 error = -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto done;
2362 }
Dave Chinner4bceb182013-10-29 22:11:51 +11002363 btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 entno = 0;
2365 }
2366 /*
2367 * Update the parent entry pointing to the moved block.
2368 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002369 btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002370 xfs_trans_log_buf(tp, par_buf,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002371 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2372 sizeof(btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 *dead_blknop = last_blkno;
2374 *dead_bufp = last_buf;
2375 return 0;
2376done:
2377 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002378 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002380 xfs_trans_brelse(tp, sib_buf);
2381 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return error;
2383}
2384
2385/*
2386 * Remove a btree block from a directory or attribute.
2387 */
2388int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002389xfs_da_shrink_inode(
2390 xfs_da_args_t *args,
2391 xfs_dablk_t dead_blkno,
2392 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 xfs_inode_t *dp;
2395 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Dave Chinner5a5881c2012-03-22 05:15:13 +00002398 trace_xfs_da_shrink_inode(args);
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 dp = args->dp;
2401 w = args->whichfork;
2402 tp = args->trans;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002403 count = args->geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 for (;;) {
2405 /*
2406 * Remove extents. If we get ENOSPC for a dir we have to move
2407 * the last block to the place we want to kill.
2408 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002409 error = xfs_bunmapi(tp, dp, dead_blkno, count,
Dave Chinnerab7bb612015-07-29 11:51:01 +10002410 xfs_bmapi_aflag(w), 0, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002411 args->dfops, &done);
Dave Chinner24513372014-06-25 14:58:08 +10002412 if (error == -ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002414 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002415 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2416 &dead_buf);
2417 if (error)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002418 break;
2419 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002423 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return error;
2425}
2426
2427/*
2428 * See if the mapping(s) for this btree block are valid, i.e.
2429 * don't contain holes, are logically contiguous, and cover the whole range.
2430 */
2431STATIC int
2432xfs_da_map_covers_blocks(
2433 int nmap,
2434 xfs_bmbt_irec_t *mapp,
2435 xfs_dablk_t bno,
2436 int count)
2437{
2438 int i;
2439 xfs_fileoff_t off;
2440
2441 for (i = 0, off = bno; i < nmap; i++) {
2442 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2443 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2444 return 0;
2445 }
2446 if (off != mapp[i].br_startoff) {
2447 return 0;
2448 }
2449 off += mapp[i].br_blockcount;
2450 }
2451 return off == bno + count;
2452}
2453
2454/*
Dave Chinner36054312012-06-22 18:50:13 +10002455 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2456 *
2457 * For the single map case, it is assumed that the caller has provided a pointer
2458 * to a valid xfs_buf_map. For the multiple map case, this function will
2459 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2460 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 */
Dave Chinner36054312012-06-22 18:50:13 +10002462static int
2463xfs_buf_map_from_irec(
2464 struct xfs_mount *mp,
2465 struct xfs_buf_map **mapp,
Dave Chinner836a94a2013-08-12 20:49:44 +10002466 int *nmaps,
Dave Chinner36054312012-06-22 18:50:13 +10002467 struct xfs_bmbt_irec *irecs,
Dave Chinner836a94a2013-08-12 20:49:44 +10002468 int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Dave Chinner36054312012-06-22 18:50:13 +10002470 struct xfs_buf_map *map;
2471 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Dave Chinner36054312012-06-22 18:50:13 +10002473 ASSERT(*nmaps == 1);
2474 ASSERT(nirecs >= 1);
2475
2476 if (nirecs > 1) {
Dave Chinnerb17cb362013-05-20 09:51:12 +10002477 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
2478 KM_SLEEP | KM_NOFS);
Dave Chinner36054312012-06-22 18:50:13 +10002479 if (!map)
Dave Chinner24513372014-06-25 14:58:08 +10002480 return -ENOMEM;
Dave Chinner36054312012-06-22 18:50:13 +10002481 *mapp = map;
2482 }
2483
2484 *nmaps = nirecs;
2485 map = *mapp;
2486 for (i = 0; i < *nmaps; i++) {
2487 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2488 irecs[i].br_startblock != HOLESTARTBLOCK);
2489 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2490 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2491 }
2492 return 0;
2493}
2494
2495/*
2496 * Map the block we are given ready for reading. There are three possible return
2497 * values:
2498 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2499 * caller knows not to execute a subsequent read.
2500 * 0 - if we mapped the block successfully
2501 * >0 - positive error number if there was an error.
2502 */
2503static int
2504xfs_dabuf_map(
Dave Chinner36054312012-06-22 18:50:13 +10002505 struct xfs_inode *dp,
2506 xfs_dablk_t bno,
2507 xfs_daddr_t mappedbno,
2508 int whichfork,
2509 struct xfs_buf_map **map,
2510 int *nmaps)
2511{
2512 struct xfs_mount *mp = dp->i_mount;
2513 int nfsb;
2514 int error = 0;
2515 struct xfs_bmbt_irec irec;
2516 struct xfs_bmbt_irec *irecs = &irec;
2517 int nirecs;
2518
2519 ASSERT(map && *map);
2520 ASSERT(*nmaps == 1);
2521
Dave Chinnerd6cf1302014-06-06 15:14:11 +10002522 if (whichfork == XFS_DATA_FORK)
2523 nfsb = mp->m_dir_geo->fsbcount;
2524 else
2525 nfsb = mp->m_attr_geo->fsbcount;
Dave Chinner36054312012-06-22 18:50:13 +10002526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 /*
2528 * Caller doesn't have a mapping. -2 means don't complain
2529 * if we land in a hole.
2530 */
2531 if (mappedbno == -1 || mappedbno == -2) {
2532 /*
2533 * Optimize the one-block case.
2534 */
Dave Chinner36054312012-06-22 18:50:13 +10002535 if (nfsb != 1)
Dave Chinnerb17cb362013-05-20 09:51:12 +10002536 irecs = kmem_zalloc(sizeof(irec) * nfsb,
2537 KM_SLEEP | KM_NOFS);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002538
Dave Chinner36054312012-06-22 18:50:13 +10002539 nirecs = nfsb;
2540 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2541 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002542 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002543 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002545 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2546 irecs->br_startoff = (xfs_fileoff_t)bno;
2547 irecs->br_blockcount = nfsb;
2548 irecs->br_state = 0;
2549 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
Dave Chinner36054312012-06-22 18:50:13 +10002551
2552 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
Dave Chinner24513372014-06-25 14:58:08 +10002553 error = mappedbno == -2 ? -1 : -EFSCORRUPTED;
2554 if (unlikely(error == -EFSCORRUPTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002556 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002557 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2558 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002560 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002561 xfs_alert(mp,
2562"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 i,
Dave Chinner36054312012-06-22 18:50:13 +10002564 (long long)irecs[i].br_startoff,
2565 (long long)irecs[i].br_startblock,
2566 (long long)irecs[i].br_blockcount,
2567 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569 }
2570 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2571 XFS_ERRLEVEL_LOW, mp);
2572 }
Dave Chinner36054312012-06-22 18:50:13 +10002573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
Dave Chinner36054312012-06-22 18:50:13 +10002575 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2576out:
2577 if (irecs != &irec)
2578 kmem_free(irecs);
2579 return error;
2580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Dave Chinner36054312012-06-22 18:50:13 +10002582/*
2583 * Get a buffer for the dir/attr block.
2584 */
2585int
2586xfs_da_get_buf(
2587 struct xfs_trans *trans,
2588 struct xfs_inode *dp,
2589 xfs_dablk_t bno,
2590 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002591 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002592 int whichfork)
2593{
2594 struct xfs_buf *bp;
2595 struct xfs_buf_map map;
2596 struct xfs_buf_map *mapp;
2597 int nmap;
2598 int error;
2599
2600 *bpp = NULL;
2601 mapp = &map;
2602 nmap = 1;
Eric Sandeen9df2dd02014-04-14 19:01:59 +10002603 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002604 &mapp, &nmap);
2605 if (error) {
2606 /* mapping a hole is not an error, but we don't continue */
2607 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002609 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
Dave Chinner36054312012-06-22 18:50:13 +10002611
2612 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2613 mapp, nmap, 0);
Dave Chinner24513372014-06-25 14:58:08 +10002614 error = bp ? bp->b_error : -EIO;
Dave Chinner36054312012-06-22 18:50:13 +10002615 if (error) {
Eric Sandeen6ee49a22014-10-02 09:23:49 +10002616 if (bp)
2617 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002618 goto out_free;
2619 }
2620
Dave Chinner1d9025e2012-06-22 18:50:14 +10002621 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002622
2623out_free:
2624 if (mapp != &map)
2625 kmem_free(mapp);
2626
2627 return error;
2628}
2629
2630/*
2631 * Get a buffer for the dir/attr block, fill in the contents.
2632 */
2633int
2634xfs_da_read_buf(
2635 struct xfs_trans *trans,
2636 struct xfs_inode *dp,
2637 xfs_dablk_t bno,
2638 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002639 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002640 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002641 const struct xfs_buf_ops *ops)
Dave Chinner36054312012-06-22 18:50:13 +10002642{
2643 struct xfs_buf *bp;
2644 struct xfs_buf_map map;
2645 struct xfs_buf_map *mapp;
2646 int nmap;
2647 int error;
2648
2649 *bpp = NULL;
2650 mapp = &map;
2651 nmap = 1;
Eric Sandeen9df2dd02014-04-14 19:01:59 +10002652 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002653 &mapp, &nmap);
2654 if (error) {
2655 /* mapping a hole is not an error, but we don't continue */
2656 if (error == -1)
2657 error = 0;
2658 goto out_free;
2659 }
2660
2661 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2662 dp->i_mount->m_ddev_targp,
Dave Chinner1813dd62012-11-14 17:54:40 +11002663 mapp, nmap, 0, &bp, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002664 if (error)
2665 goto out_free;
2666
2667 if (whichfork == XFS_ATTR_FORK)
2668 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 else
Dave Chinner36054312012-06-22 18:50:13 +10002670 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002671 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002672out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002674 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 return error;
2677}
2678
2679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 * Readahead the dir/attr block.
2681 */
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08002682int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002684 struct xfs_inode *dp,
2685 xfs_dablk_t bno,
Dave Chinnerda6958c2012-11-12 22:54:18 +11002686 xfs_daddr_t mappedbno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002687 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002688 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
Dave Chinner36054312012-06-22 18:50:13 +10002690 struct xfs_buf_map map;
2691 struct xfs_buf_map *mapp;
2692 int nmap;
2693 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Dave Chinner36054312012-06-22 18:50:13 +10002695 mapp = &map;
2696 nmap = 1;
Eric Sandeen9df2dd02014-04-14 19:01:59 +10002697 error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002698 &mapp, &nmap);
2699 if (error) {
2700 /* mapping a hole is not an error, but we don't continue */
2701 if (error == -1)
2702 error = 0;
2703 goto out_free;
2704 }
2705
2706 mappedbno = mapp[0].bm_bn;
Dave Chinner1813dd62012-11-14 17:54:40 +11002707 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002708
2709out_free:
2710 if (mapp != &map)
2711 kmem_free(mapp);
2712
Darrick J. Wong7a652bb2017-02-02 15:13:58 -08002713 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}