xref: /openbmc/linux/fs/xfs/libxfs/xfs_da_btree.c (revision d37cf9b63113f13d742713881ce691fc615d8b3b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_bit.h"
14 #include "xfs_mount.h"
15 #include "xfs_inode.h"
16 #include "xfs_dir2.h"
17 #include "xfs_dir2_priv.h"
18 #include "xfs_trans.h"
19 #include "xfs_bmap.h"
20 #include "xfs_attr_leaf.h"
21 #include "xfs_error.h"
22 #include "xfs_trace.h"
23 #include "xfs_buf_item.h"
24 #include "xfs_log.h"
25 #include "xfs_errortag.h"
26 
27 /*
28  * xfs_da_btree.c
29  *
30  * Routines to implement directories as Btrees of hashed names.
31  */
32 
33 /*========================================================================
34  * Function prototypes for the kernel.
35  *========================================================================*/
36 
37 /*
38  * Routines used for growing the Btree.
39  */
40 STATIC int xfs_da3_root_split(xfs_da_state_t *state,
41 					    xfs_da_state_blk_t *existing_root,
42 					    xfs_da_state_blk_t *new_child);
43 STATIC int xfs_da3_node_split(xfs_da_state_t *state,
44 					    xfs_da_state_blk_t *existing_blk,
45 					    xfs_da_state_blk_t *split_blk,
46 					    xfs_da_state_blk_t *blk_to_add,
47 					    int treelevel,
48 					    int *result);
49 STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
50 					 xfs_da_state_blk_t *node_blk_1,
51 					 xfs_da_state_blk_t *node_blk_2);
52 STATIC void xfs_da3_node_add(xfs_da_state_t *state,
53 				   xfs_da_state_blk_t *old_node_blk,
54 				   xfs_da_state_blk_t *new_node_blk);
55 
56 /*
57  * Routines used for shrinking the Btree.
58  */
59 STATIC int xfs_da3_root_join(xfs_da_state_t *state,
60 					   xfs_da_state_blk_t *root_blk);
61 STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
62 STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
63 					      xfs_da_state_blk_t *drop_blk);
64 STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
65 					 xfs_da_state_blk_t *src_node_blk,
66 					 xfs_da_state_blk_t *dst_node_blk);
67 
68 /*
69  * Utility routines.
70  */
71 STATIC int	xfs_da3_blk_unlink(xfs_da_state_t *state,
72 				  xfs_da_state_blk_t *drop_blk,
73 				  xfs_da_state_blk_t *save_blk);
74 
75 
76 struct kmem_cache	*xfs_da_state_cache;	/* anchor for dir/attr state */
77 
78 /*
79  * Allocate a dir-state structure.
80  * We don't put them on the stack since they're large.
81  */
82 struct xfs_da_state *
xfs_da_state_alloc(struct xfs_da_args * args)83 xfs_da_state_alloc(
84 	struct xfs_da_args	*args)
85 {
86 	struct xfs_da_state	*state;
87 
88 	state = kmem_cache_zalloc(xfs_da_state_cache, GFP_NOFS | __GFP_NOFAIL);
89 	state->args = args;
90 	state->mp = args->dp->i_mount;
91 	return state;
92 }
93 
94 /*
95  * Kill the altpath contents of a da-state structure.
96  */
97 STATIC void
xfs_da_state_kill_altpath(xfs_da_state_t * state)98 xfs_da_state_kill_altpath(xfs_da_state_t *state)
99 {
100 	int	i;
101 
102 	for (i = 0; i < state->altpath.active; i++)
103 		state->altpath.blk[i].bp = NULL;
104 	state->altpath.active = 0;
105 }
106 
107 /*
108  * Free a da-state structure.
109  */
110 void
xfs_da_state_free(xfs_da_state_t * state)111 xfs_da_state_free(xfs_da_state_t *state)
112 {
113 	xfs_da_state_kill_altpath(state);
114 #ifdef DEBUG
115 	memset((char *)state, 0, sizeof(*state));
116 #endif /* DEBUG */
117 	kmem_cache_free(xfs_da_state_cache, state);
118 }
119 
120 void
xfs_da_state_reset(struct xfs_da_state * state,struct xfs_da_args * args)121 xfs_da_state_reset(
122 	struct xfs_da_state	*state,
123 	struct xfs_da_args	*args)
124 {
125 	xfs_da_state_kill_altpath(state);
126 	memset(state, 0, sizeof(struct xfs_da_state));
127 	state->args = args;
128 	state->mp = state->args->dp->i_mount;
129 }
130 
xfs_dabuf_nfsb(struct xfs_mount * mp,int whichfork)131 static inline int xfs_dabuf_nfsb(struct xfs_mount *mp, int whichfork)
132 {
133 	if (whichfork == XFS_DATA_FORK)
134 		return mp->m_dir_geo->fsbcount;
135 	return mp->m_attr_geo->fsbcount;
136 }
137 
138 void
xfs_da3_node_hdr_from_disk(struct xfs_mount * mp,struct xfs_da3_icnode_hdr * to,struct xfs_da_intnode * from)139 xfs_da3_node_hdr_from_disk(
140 	struct xfs_mount		*mp,
141 	struct xfs_da3_icnode_hdr	*to,
142 	struct xfs_da_intnode		*from)
143 {
144 	if (xfs_has_crc(mp)) {
145 		struct xfs_da3_intnode	*from3 = (struct xfs_da3_intnode *)from;
146 
147 		to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
148 		to->back = be32_to_cpu(from3->hdr.info.hdr.back);
149 		to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
150 		to->count = be16_to_cpu(from3->hdr.__count);
151 		to->level = be16_to_cpu(from3->hdr.__level);
152 		to->btree = from3->__btree;
153 		ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
154 	} else {
155 		to->forw = be32_to_cpu(from->hdr.info.forw);
156 		to->back = be32_to_cpu(from->hdr.info.back);
157 		to->magic = be16_to_cpu(from->hdr.info.magic);
158 		to->count = be16_to_cpu(from->hdr.__count);
159 		to->level = be16_to_cpu(from->hdr.__level);
160 		to->btree = from->__btree;
161 		ASSERT(to->magic == XFS_DA_NODE_MAGIC);
162 	}
163 }
164 
165 void
xfs_da3_node_hdr_to_disk(struct xfs_mount * mp,struct xfs_da_intnode * to,struct xfs_da3_icnode_hdr * from)166 xfs_da3_node_hdr_to_disk(
167 	struct xfs_mount		*mp,
168 	struct xfs_da_intnode		*to,
169 	struct xfs_da3_icnode_hdr	*from)
170 {
171 	if (xfs_has_crc(mp)) {
172 		struct xfs_da3_intnode	*to3 = (struct xfs_da3_intnode *)to;
173 
174 		ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
175 		to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
176 		to3->hdr.info.hdr.back = cpu_to_be32(from->back);
177 		to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
178 		to3->hdr.__count = cpu_to_be16(from->count);
179 		to3->hdr.__level = cpu_to_be16(from->level);
180 	} else {
181 		ASSERT(from->magic == XFS_DA_NODE_MAGIC);
182 		to->hdr.info.forw = cpu_to_be32(from->forw);
183 		to->hdr.info.back = cpu_to_be32(from->back);
184 		to->hdr.info.magic = cpu_to_be16(from->magic);
185 		to->hdr.__count = cpu_to_be16(from->count);
186 		to->hdr.__level = cpu_to_be16(from->level);
187 	}
188 }
189 
190 /*
191  * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
192  * accessible on v5 filesystems. This header format is common across da node,
193  * attr leaf and dir leaf blocks.
194  */
195 xfs_failaddr_t
xfs_da3_blkinfo_verify(struct xfs_buf * bp,struct xfs_da3_blkinfo * hdr3)196 xfs_da3_blkinfo_verify(
197 	struct xfs_buf		*bp,
198 	struct xfs_da3_blkinfo	*hdr3)
199 {
200 	struct xfs_mount	*mp = bp->b_mount;
201 	struct xfs_da_blkinfo	*hdr = &hdr3->hdr;
202 
203 	if (!xfs_verify_magic16(bp, hdr->magic))
204 		return __this_address;
205 
206 	if (xfs_has_crc(mp)) {
207 		if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
208 			return __this_address;
209 		if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
210 			return __this_address;
211 		if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
212 			return __this_address;
213 	}
214 
215 	return NULL;
216 }
217 
218 static xfs_failaddr_t
xfs_da3_node_verify(struct xfs_buf * bp)219 xfs_da3_node_verify(
220 	struct xfs_buf		*bp)
221 {
222 	struct xfs_mount	*mp = bp->b_mount;
223 	struct xfs_da_intnode	*hdr = bp->b_addr;
224 	struct xfs_da3_icnode_hdr ichdr;
225 	xfs_failaddr_t		fa;
226 
227 	xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
228 
229 	fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
230 	if (fa)
231 		return fa;
232 
233 	if (ichdr.level == 0)
234 		return __this_address;
235 	if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
236 		return __this_address;
237 	if (ichdr.count == 0)
238 		return __this_address;
239 
240 	/*
241 	 * we don't know if the node is for and attribute or directory tree,
242 	 * so only fail if the count is outside both bounds
243 	 */
244 	if (ichdr.count > mp->m_dir_geo->node_ents &&
245 	    ichdr.count > mp->m_attr_geo->node_ents)
246 		return __this_address;
247 
248 	/* XXX: hash order check? */
249 
250 	return NULL;
251 }
252 
253 static void
xfs_da3_node_write_verify(struct xfs_buf * bp)254 xfs_da3_node_write_verify(
255 	struct xfs_buf	*bp)
256 {
257 	struct xfs_mount	*mp = bp->b_mount;
258 	struct xfs_buf_log_item	*bip = bp->b_log_item;
259 	struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
260 	xfs_failaddr_t		fa;
261 
262 	fa = xfs_da3_node_verify(bp);
263 	if (fa) {
264 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
265 		return;
266 	}
267 
268 	if (!xfs_has_crc(mp))
269 		return;
270 
271 	if (bip)
272 		hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
273 
274 	xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
275 }
276 
277 /*
278  * leaf/node format detection on trees is sketchy, so a node read can be done on
279  * leaf level blocks when detection identifies the tree as a node format tree
280  * incorrectly. In this case, we need to swap the verifier to match the correct
281  * format of the block being read.
282  */
283 static void
xfs_da3_node_read_verify(struct xfs_buf * bp)284 xfs_da3_node_read_verify(
285 	struct xfs_buf		*bp)
286 {
287 	struct xfs_da_blkinfo	*info = bp->b_addr;
288 	xfs_failaddr_t		fa;
289 
290 	switch (be16_to_cpu(info->magic)) {
291 		case XFS_DA3_NODE_MAGIC:
292 			if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
293 				xfs_verifier_error(bp, -EFSBADCRC,
294 						__this_address);
295 				break;
296 			}
297 			fallthrough;
298 		case XFS_DA_NODE_MAGIC:
299 			fa = xfs_da3_node_verify(bp);
300 			if (fa)
301 				xfs_verifier_error(bp, -EFSCORRUPTED, fa);
302 			return;
303 		case XFS_ATTR_LEAF_MAGIC:
304 		case XFS_ATTR3_LEAF_MAGIC:
305 			bp->b_ops = &xfs_attr3_leaf_buf_ops;
306 			bp->b_ops->verify_read(bp);
307 			return;
308 		case XFS_DIR2_LEAFN_MAGIC:
309 		case XFS_DIR3_LEAFN_MAGIC:
310 			bp->b_ops = &xfs_dir3_leafn_buf_ops;
311 			bp->b_ops->verify_read(bp);
312 			return;
313 		default:
314 			xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
315 			break;
316 	}
317 }
318 
319 /* Verify the structure of a da3 block. */
320 static xfs_failaddr_t
xfs_da3_node_verify_struct(struct xfs_buf * bp)321 xfs_da3_node_verify_struct(
322 	struct xfs_buf		*bp)
323 {
324 	struct xfs_da_blkinfo	*info = bp->b_addr;
325 
326 	switch (be16_to_cpu(info->magic)) {
327 	case XFS_DA3_NODE_MAGIC:
328 	case XFS_DA_NODE_MAGIC:
329 		return xfs_da3_node_verify(bp);
330 	case XFS_ATTR_LEAF_MAGIC:
331 	case XFS_ATTR3_LEAF_MAGIC:
332 		bp->b_ops = &xfs_attr3_leaf_buf_ops;
333 		return bp->b_ops->verify_struct(bp);
334 	case XFS_DIR2_LEAFN_MAGIC:
335 	case XFS_DIR3_LEAFN_MAGIC:
336 		bp->b_ops = &xfs_dir3_leafn_buf_ops;
337 		return bp->b_ops->verify_struct(bp);
338 	default:
339 		return __this_address;
340 	}
341 }
342 
343 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
344 	.name = "xfs_da3_node",
345 	.magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
346 		     cpu_to_be16(XFS_DA3_NODE_MAGIC) },
347 	.verify_read = xfs_da3_node_read_verify,
348 	.verify_write = xfs_da3_node_write_verify,
349 	.verify_struct = xfs_da3_node_verify_struct,
350 };
351 
352 static int
xfs_da3_node_set_type(struct xfs_trans * tp,struct xfs_buf * bp)353 xfs_da3_node_set_type(
354 	struct xfs_trans	*tp,
355 	struct xfs_buf		*bp)
356 {
357 	struct xfs_da_blkinfo	*info = bp->b_addr;
358 
359 	switch (be16_to_cpu(info->magic)) {
360 	case XFS_DA_NODE_MAGIC:
361 	case XFS_DA3_NODE_MAGIC:
362 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
363 		return 0;
364 	case XFS_ATTR_LEAF_MAGIC:
365 	case XFS_ATTR3_LEAF_MAGIC:
366 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_ATTR_LEAF_BUF);
367 		return 0;
368 	case XFS_DIR2_LEAFN_MAGIC:
369 	case XFS_DIR3_LEAFN_MAGIC:
370 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
371 		return 0;
372 	default:
373 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, tp->t_mountp,
374 				info, sizeof(*info));
375 		xfs_trans_brelse(tp, bp);
376 		return -EFSCORRUPTED;
377 	}
378 }
379 
380 int
xfs_da3_node_read(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t bno,struct xfs_buf ** bpp,int whichfork)381 xfs_da3_node_read(
382 	struct xfs_trans	*tp,
383 	struct xfs_inode	*dp,
384 	xfs_dablk_t		bno,
385 	struct xfs_buf		**bpp,
386 	int			whichfork)
387 {
388 	int			error;
389 
390 	error = xfs_da_read_buf(tp, dp, bno, 0, bpp, whichfork,
391 			&xfs_da3_node_buf_ops);
392 	if (error || !*bpp || !tp)
393 		return error;
394 	return xfs_da3_node_set_type(tp, *bpp);
395 }
396 
397 int
xfs_da3_node_read_mapped(struct xfs_trans * tp,struct xfs_inode * dp,xfs_daddr_t mappedbno,struct xfs_buf ** bpp,int whichfork)398 xfs_da3_node_read_mapped(
399 	struct xfs_trans	*tp,
400 	struct xfs_inode	*dp,
401 	xfs_daddr_t		mappedbno,
402 	struct xfs_buf		**bpp,
403 	int			whichfork)
404 {
405 	struct xfs_mount	*mp = dp->i_mount;
406 	int			error;
407 
408 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, mappedbno,
409 			XFS_FSB_TO_BB(mp, xfs_dabuf_nfsb(mp, whichfork)), 0,
410 			bpp, &xfs_da3_node_buf_ops);
411 	if (error || !*bpp)
412 		return error;
413 
414 	if (whichfork == XFS_ATTR_FORK)
415 		xfs_buf_set_ref(*bpp, XFS_ATTR_BTREE_REF);
416 	else
417 		xfs_buf_set_ref(*bpp, XFS_DIR_BTREE_REF);
418 
419 	if (!tp)
420 		return 0;
421 	return xfs_da3_node_set_type(tp, *bpp);
422 }
423 
424 /*========================================================================
425  * Routines used for growing the Btree.
426  *========================================================================*/
427 
428 /*
429  * Create the initial contents of an intermediate node.
430  */
431 int
xfs_da3_node_create(struct xfs_da_args * args,xfs_dablk_t blkno,int level,struct xfs_buf ** bpp,int whichfork)432 xfs_da3_node_create(
433 	struct xfs_da_args	*args,
434 	xfs_dablk_t		blkno,
435 	int			level,
436 	struct xfs_buf		**bpp,
437 	int			whichfork)
438 {
439 	struct xfs_da_intnode	*node;
440 	struct xfs_trans	*tp = args->trans;
441 	struct xfs_mount	*mp = tp->t_mountp;
442 	struct xfs_da3_icnode_hdr ichdr = {0};
443 	struct xfs_buf		*bp;
444 	int			error;
445 	struct xfs_inode	*dp = args->dp;
446 
447 	trace_xfs_da_node_create(args);
448 	ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
449 
450 	error = xfs_da_get_buf(tp, dp, blkno, &bp, whichfork);
451 	if (error)
452 		return error;
453 	bp->b_ops = &xfs_da3_node_buf_ops;
454 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
455 	node = bp->b_addr;
456 
457 	if (xfs_has_crc(mp)) {
458 		struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
459 
460 		memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
461 		ichdr.magic = XFS_DA3_NODE_MAGIC;
462 		hdr3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
463 		hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
464 		uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
465 	} else {
466 		ichdr.magic = XFS_DA_NODE_MAGIC;
467 	}
468 	ichdr.level = level;
469 
470 	xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
471 	xfs_trans_log_buf(tp, bp,
472 		XFS_DA_LOGRANGE(node, &node->hdr, args->geo->node_hdr_size));
473 
474 	*bpp = bp;
475 	return 0;
476 }
477 
478 /*
479  * Split a leaf node, rebalance, then possibly split
480  * intermediate nodes, rebalance, etc.
481  */
482 int							/* error */
xfs_da3_split(struct xfs_da_state * state)483 xfs_da3_split(
484 	struct xfs_da_state	*state)
485 {
486 	struct xfs_da_state_blk	*oldblk;
487 	struct xfs_da_state_blk	*newblk;
488 	struct xfs_da_state_blk	*addblk;
489 	struct xfs_da_intnode	*node;
490 	int			max;
491 	int			action = 0;
492 	int			error;
493 	int			i;
494 
495 	trace_xfs_da_split(state->args);
496 
497 	if (XFS_TEST_ERROR(false, state->mp, XFS_ERRTAG_DA_LEAF_SPLIT))
498 		return -EIO;
499 
500 	/*
501 	 * Walk back up the tree splitting/inserting/adjusting as necessary.
502 	 * If we need to insert and there isn't room, split the node, then
503 	 * decide which fragment to insert the new block from below into.
504 	 * Note that we may split the root this way, but we need more fixup.
505 	 */
506 	max = state->path.active - 1;
507 	ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
508 	ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
509 	       state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
510 
511 	addblk = &state->path.blk[max];		/* initial dummy value */
512 	for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
513 		oldblk = &state->path.blk[i];
514 		newblk = &state->altpath.blk[i];
515 
516 		/*
517 		 * If a leaf node then
518 		 *     Allocate a new leaf node, then rebalance across them.
519 		 * else if an intermediate node then
520 		 *     We split on the last layer, must we split the node?
521 		 */
522 		switch (oldblk->magic) {
523 		case XFS_ATTR_LEAF_MAGIC:
524 			error = xfs_attr3_leaf_split(state, oldblk, newblk);
525 			if (error < 0)
526 				return error;	/* GROT: attr is inconsistent */
527 			if (!error) {
528 				addblk = newblk;
529 				break;
530 			}
531 			/*
532 			 * Entry wouldn't fit, split the leaf again. The new
533 			 * extrablk will be consumed by xfs_da3_node_split if
534 			 * the node is split.
535 			 */
536 			state->extravalid = 1;
537 			if (state->inleaf) {
538 				state->extraafter = 0;	/* before newblk */
539 				trace_xfs_attr_leaf_split_before(state->args);
540 				error = xfs_attr3_leaf_split(state, oldblk,
541 							    &state->extrablk);
542 			} else {
543 				state->extraafter = 1;	/* after newblk */
544 				trace_xfs_attr_leaf_split_after(state->args);
545 				error = xfs_attr3_leaf_split(state, newblk,
546 							    &state->extrablk);
547 			}
548 			if (error == 1)
549 				return -ENOSPC;
550 			if (error)
551 				return error;	/* GROT: attr inconsistent */
552 			addblk = newblk;
553 			break;
554 		case XFS_DIR2_LEAFN_MAGIC:
555 			error = xfs_dir2_leafn_split(state, oldblk, newblk);
556 			if (error)
557 				return error;
558 			addblk = newblk;
559 			break;
560 		case XFS_DA_NODE_MAGIC:
561 			error = xfs_da3_node_split(state, oldblk, newblk, addblk,
562 							 max - i, &action);
563 			addblk->bp = NULL;
564 			if (error)
565 				return error;	/* GROT: dir is inconsistent */
566 			/*
567 			 * Record the newly split block for the next time thru?
568 			 */
569 			if (action)
570 				addblk = newblk;
571 			else
572 				addblk = NULL;
573 			break;
574 		}
575 
576 		/*
577 		 * Update the btree to show the new hashval for this child.
578 		 */
579 		xfs_da3_fixhashpath(state, &state->path);
580 	}
581 	if (!addblk)
582 		return 0;
583 
584 	/*
585 	 * xfs_da3_node_split() should have consumed any extra blocks we added
586 	 * during a double leaf split in the attr fork. This is guaranteed as
587 	 * we can't be here if the attr fork only has a single leaf block.
588 	 */
589 	ASSERT(state->extravalid == 0 ||
590 	       state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
591 
592 	/*
593 	 * Split the root node.
594 	 */
595 	ASSERT(state->path.active == 0);
596 	oldblk = &state->path.blk[0];
597 	error = xfs_da3_root_split(state, oldblk, addblk);
598 	if (error)
599 		goto out;
600 
601 	/*
602 	 * Update pointers to the node which used to be block 0 and just got
603 	 * bumped because of the addition of a new root node.  Note that the
604 	 * original block 0 could be at any position in the list of blocks in
605 	 * the tree.
606 	 *
607 	 * Note: the magic numbers and sibling pointers are in the same physical
608 	 * place for both v2 and v3 headers (by design). Hence it doesn't matter
609 	 * which version of the xfs_da_intnode structure we use here as the
610 	 * result will be the same using either structure.
611 	 */
612 	node = oldblk->bp->b_addr;
613 	if (node->hdr.info.forw) {
614 		if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
615 			xfs_buf_mark_corrupt(oldblk->bp);
616 			error = -EFSCORRUPTED;
617 			goto out;
618 		}
619 		node = addblk->bp->b_addr;
620 		node->hdr.info.back = cpu_to_be32(oldblk->blkno);
621 		xfs_trans_log_buf(state->args->trans, addblk->bp,
622 				  XFS_DA_LOGRANGE(node, &node->hdr.info,
623 				  sizeof(node->hdr.info)));
624 	}
625 	node = oldblk->bp->b_addr;
626 	if (node->hdr.info.back) {
627 		if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
628 			xfs_buf_mark_corrupt(oldblk->bp);
629 			error = -EFSCORRUPTED;
630 			goto out;
631 		}
632 		node = addblk->bp->b_addr;
633 		node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
634 		xfs_trans_log_buf(state->args->trans, addblk->bp,
635 				  XFS_DA_LOGRANGE(node, &node->hdr.info,
636 				  sizeof(node->hdr.info)));
637 	}
638 out:
639 	addblk->bp = NULL;
640 	return error;
641 }
642 
643 /*
644  * Split the root.  We have to create a new root and point to the two
645  * parts (the split old root) that we just created.  Copy block zero to
646  * the EOF, extending the inode in process.
647  */
648 STATIC int						/* error */
xfs_da3_root_split(struct xfs_da_state * state,struct xfs_da_state_blk * blk1,struct xfs_da_state_blk * blk2)649 xfs_da3_root_split(
650 	struct xfs_da_state	*state,
651 	struct xfs_da_state_blk	*blk1,
652 	struct xfs_da_state_blk	*blk2)
653 {
654 	struct xfs_da_intnode	*node;
655 	struct xfs_da_intnode	*oldroot;
656 	struct xfs_da_node_entry *btree;
657 	struct xfs_da3_icnode_hdr nodehdr;
658 	struct xfs_da_args	*args;
659 	struct xfs_buf		*bp;
660 	struct xfs_inode	*dp;
661 	struct xfs_trans	*tp;
662 	struct xfs_dir2_leaf	*leaf;
663 	xfs_dablk_t		blkno;
664 	int			level;
665 	int			error;
666 	int			size;
667 
668 	trace_xfs_da_root_split(state->args);
669 
670 	/*
671 	 * Copy the existing (incorrect) block from the root node position
672 	 * to a free space somewhere.
673 	 */
674 	args = state->args;
675 	error = xfs_da_grow_inode(args, &blkno);
676 	if (error)
677 		return error;
678 
679 	dp = args->dp;
680 	tp = args->trans;
681 	error = xfs_da_get_buf(tp, dp, blkno, &bp, args->whichfork);
682 	if (error)
683 		return error;
684 	node = bp->b_addr;
685 	oldroot = blk1->bp->b_addr;
686 	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
687 	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
688 		struct xfs_da3_icnode_hdr icnodehdr;
689 
690 		xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
691 		btree = icnodehdr.btree;
692 		size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
693 		level = icnodehdr.level;
694 
695 		/*
696 		 * we are about to copy oldroot to bp, so set up the type
697 		 * of bp while we know exactly what it will be.
698 		 */
699 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
700 	} else {
701 		struct xfs_dir3_icleaf_hdr leafhdr;
702 
703 		leaf = (xfs_dir2_leaf_t *)oldroot;
704 		xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
705 
706 		ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
707 		       leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
708 		size = (int)((char *)&leafhdr.ents[leafhdr.count] -
709 			(char *)leaf);
710 		level = 0;
711 
712 		/*
713 		 * we are about to copy oldroot to bp, so set up the type
714 		 * of bp while we know exactly what it will be.
715 		 */
716 		xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
717 	}
718 
719 	/*
720 	 * we can copy most of the information in the node from one block to
721 	 * another, but for CRC enabled headers we have to make sure that the
722 	 * block specific identifiers are kept intact. We update the buffer
723 	 * directly for this.
724 	 */
725 	memcpy(node, oldroot, size);
726 	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
727 	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
728 		struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
729 
730 		node3->hdr.info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
731 	}
732 	xfs_trans_log_buf(tp, bp, 0, size - 1);
733 
734 	bp->b_ops = blk1->bp->b_ops;
735 	xfs_trans_buf_copy_type(bp, blk1->bp);
736 	blk1->bp = bp;
737 	blk1->blkno = blkno;
738 
739 	/*
740 	 * Set up the new root node.
741 	 */
742 	error = xfs_da3_node_create(args,
743 		(args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
744 		level + 1, &bp, args->whichfork);
745 	if (error)
746 		return error;
747 
748 	node = bp->b_addr;
749 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
750 	btree = nodehdr.btree;
751 	btree[0].hashval = cpu_to_be32(blk1->hashval);
752 	btree[0].before = cpu_to_be32(blk1->blkno);
753 	btree[1].hashval = cpu_to_be32(blk2->hashval);
754 	btree[1].before = cpu_to_be32(blk2->blkno);
755 	nodehdr.count = 2;
756 	xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
757 
758 #ifdef DEBUG
759 	if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
760 	    oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
761 		ASSERT(blk1->blkno >= args->geo->leafblk &&
762 		       blk1->blkno < args->geo->freeblk);
763 		ASSERT(blk2->blkno >= args->geo->leafblk &&
764 		       blk2->blkno < args->geo->freeblk);
765 	}
766 #endif
767 
768 	/* Header is already logged by xfs_da_node_create */
769 	xfs_trans_log_buf(tp, bp,
770 		XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
771 
772 	return 0;
773 }
774 
775 /*
776  * Split the node, rebalance, then add the new entry.
777  */
778 STATIC int						/* error */
xfs_da3_node_split(struct xfs_da_state * state,struct xfs_da_state_blk * oldblk,struct xfs_da_state_blk * newblk,struct xfs_da_state_blk * addblk,int treelevel,int * result)779 xfs_da3_node_split(
780 	struct xfs_da_state	*state,
781 	struct xfs_da_state_blk	*oldblk,
782 	struct xfs_da_state_blk	*newblk,
783 	struct xfs_da_state_blk	*addblk,
784 	int			treelevel,
785 	int			*result)
786 {
787 	struct xfs_da_intnode	*node;
788 	struct xfs_da3_icnode_hdr nodehdr;
789 	xfs_dablk_t		blkno;
790 	int			newcount;
791 	int			error;
792 	int			useextra;
793 	struct xfs_inode	*dp = state->args->dp;
794 
795 	trace_xfs_da_node_split(state->args);
796 
797 	node = oldblk->bp->b_addr;
798 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
799 
800 	/*
801 	 * With V2 dirs the extra block is data or freespace.
802 	 */
803 	useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
804 	newcount = 1 + useextra;
805 	/*
806 	 * Do we have to split the node?
807 	 */
808 	if (nodehdr.count + newcount > state->args->geo->node_ents) {
809 		/*
810 		 * Allocate a new node, add to the doubly linked chain of
811 		 * nodes, then move some of our excess entries into it.
812 		 */
813 		error = xfs_da_grow_inode(state->args, &blkno);
814 		if (error)
815 			return error;	/* GROT: dir is inconsistent */
816 
817 		error = xfs_da3_node_create(state->args, blkno, treelevel,
818 					   &newblk->bp, state->args->whichfork);
819 		if (error)
820 			return error;	/* GROT: dir is inconsistent */
821 		newblk->blkno = blkno;
822 		newblk->magic = XFS_DA_NODE_MAGIC;
823 		xfs_da3_node_rebalance(state, oldblk, newblk);
824 		error = xfs_da3_blk_link(state, oldblk, newblk);
825 		if (error)
826 			return error;
827 		*result = 1;
828 	} else {
829 		*result = 0;
830 	}
831 
832 	/*
833 	 * Insert the new entry(s) into the correct block
834 	 * (updating last hashval in the process).
835 	 *
836 	 * xfs_da3_node_add() inserts BEFORE the given index,
837 	 * and as a result of using node_lookup_int() we always
838 	 * point to a valid entry (not after one), but a split
839 	 * operation always results in a new block whose hashvals
840 	 * FOLLOW the current block.
841 	 *
842 	 * If we had double-split op below us, then add the extra block too.
843 	 */
844 	node = oldblk->bp->b_addr;
845 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
846 	if (oldblk->index <= nodehdr.count) {
847 		oldblk->index++;
848 		xfs_da3_node_add(state, oldblk, addblk);
849 		if (useextra) {
850 			if (state->extraafter)
851 				oldblk->index++;
852 			xfs_da3_node_add(state, oldblk, &state->extrablk);
853 			state->extravalid = 0;
854 		}
855 	} else {
856 		newblk->index++;
857 		xfs_da3_node_add(state, newblk, addblk);
858 		if (useextra) {
859 			if (state->extraafter)
860 				newblk->index++;
861 			xfs_da3_node_add(state, newblk, &state->extrablk);
862 			state->extravalid = 0;
863 		}
864 	}
865 
866 	return 0;
867 }
868 
869 /*
870  * Balance the btree elements between two intermediate nodes,
871  * usually one full and one empty.
872  *
873  * NOTE: if blk2 is empty, then it will get the upper half of blk1.
874  */
875 STATIC void
xfs_da3_node_rebalance(struct xfs_da_state * state,struct xfs_da_state_blk * blk1,struct xfs_da_state_blk * blk2)876 xfs_da3_node_rebalance(
877 	struct xfs_da_state	*state,
878 	struct xfs_da_state_blk	*blk1,
879 	struct xfs_da_state_blk	*blk2)
880 {
881 	struct xfs_da_intnode	*node1;
882 	struct xfs_da_intnode	*node2;
883 	struct xfs_da_node_entry *btree1;
884 	struct xfs_da_node_entry *btree2;
885 	struct xfs_da_node_entry *btree_s;
886 	struct xfs_da_node_entry *btree_d;
887 	struct xfs_da3_icnode_hdr nodehdr1;
888 	struct xfs_da3_icnode_hdr nodehdr2;
889 	struct xfs_trans	*tp;
890 	int			count;
891 	int			tmp;
892 	int			swap = 0;
893 	struct xfs_inode	*dp = state->args->dp;
894 
895 	trace_xfs_da_node_rebalance(state->args);
896 
897 	node1 = blk1->bp->b_addr;
898 	node2 = blk2->bp->b_addr;
899 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
900 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
901 	btree1 = nodehdr1.btree;
902 	btree2 = nodehdr2.btree;
903 
904 	/*
905 	 * Figure out how many entries need to move, and in which direction.
906 	 * Swap the nodes around if that makes it simpler.
907 	 */
908 	if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
909 	    ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
910 	     (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
911 			be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
912 		swap(node1, node2);
913 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
914 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
915 		btree1 = nodehdr1.btree;
916 		btree2 = nodehdr2.btree;
917 		swap = 1;
918 	}
919 
920 	count = (nodehdr1.count - nodehdr2.count) / 2;
921 	if (count == 0)
922 		return;
923 	tp = state->args->trans;
924 	/*
925 	 * Two cases: high-to-low and low-to-high.
926 	 */
927 	if (count > 0) {
928 		/*
929 		 * Move elements in node2 up to make a hole.
930 		 */
931 		tmp = nodehdr2.count;
932 		if (tmp > 0) {
933 			tmp *= (uint)sizeof(xfs_da_node_entry_t);
934 			btree_s = &btree2[0];
935 			btree_d = &btree2[count];
936 			memmove(btree_d, btree_s, tmp);
937 		}
938 
939 		/*
940 		 * Move the req'd B-tree elements from high in node1 to
941 		 * low in node2.
942 		 */
943 		nodehdr2.count += count;
944 		tmp = count * (uint)sizeof(xfs_da_node_entry_t);
945 		btree_s = &btree1[nodehdr1.count - count];
946 		btree_d = &btree2[0];
947 		memcpy(btree_d, btree_s, tmp);
948 		nodehdr1.count -= count;
949 	} else {
950 		/*
951 		 * Move the req'd B-tree elements from low in node2 to
952 		 * high in node1.
953 		 */
954 		count = -count;
955 		tmp = count * (uint)sizeof(xfs_da_node_entry_t);
956 		btree_s = &btree2[0];
957 		btree_d = &btree1[nodehdr1.count];
958 		memcpy(btree_d, btree_s, tmp);
959 		nodehdr1.count += count;
960 
961 		xfs_trans_log_buf(tp, blk1->bp,
962 			XFS_DA_LOGRANGE(node1, btree_d, tmp));
963 
964 		/*
965 		 * Move elements in node2 down to fill the hole.
966 		 */
967 		tmp  = nodehdr2.count - count;
968 		tmp *= (uint)sizeof(xfs_da_node_entry_t);
969 		btree_s = &btree2[count];
970 		btree_d = &btree2[0];
971 		memmove(btree_d, btree_s, tmp);
972 		nodehdr2.count -= count;
973 	}
974 
975 	/*
976 	 * Log header of node 1 and all current bits of node 2.
977 	 */
978 	xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
979 	xfs_trans_log_buf(tp, blk1->bp,
980 		XFS_DA_LOGRANGE(node1, &node1->hdr,
981 				state->args->geo->node_hdr_size));
982 
983 	xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
984 	xfs_trans_log_buf(tp, blk2->bp,
985 		XFS_DA_LOGRANGE(node2, &node2->hdr,
986 				state->args->geo->node_hdr_size +
987 				(sizeof(btree2[0]) * nodehdr2.count)));
988 
989 	/*
990 	 * Record the last hashval from each block for upward propagation.
991 	 * (note: don't use the swapped node pointers)
992 	 */
993 	if (swap) {
994 		node1 = blk1->bp->b_addr;
995 		node2 = blk2->bp->b_addr;
996 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
997 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
998 		btree1 = nodehdr1.btree;
999 		btree2 = nodehdr2.btree;
1000 	}
1001 	blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
1002 	blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
1003 
1004 	/*
1005 	 * Adjust the expected index for insertion.
1006 	 */
1007 	if (blk1->index >= nodehdr1.count) {
1008 		blk2->index = blk1->index - nodehdr1.count;
1009 		blk1->index = nodehdr1.count + 1;	/* make it invalid */
1010 	}
1011 }
1012 
1013 /*
1014  * Add a new entry to an intermediate node.
1015  */
1016 STATIC void
xfs_da3_node_add(struct xfs_da_state * state,struct xfs_da_state_blk * oldblk,struct xfs_da_state_blk * newblk)1017 xfs_da3_node_add(
1018 	struct xfs_da_state	*state,
1019 	struct xfs_da_state_blk	*oldblk,
1020 	struct xfs_da_state_blk	*newblk)
1021 {
1022 	struct xfs_da_intnode	*node;
1023 	struct xfs_da3_icnode_hdr nodehdr;
1024 	struct xfs_da_node_entry *btree;
1025 	int			tmp;
1026 	struct xfs_inode	*dp = state->args->dp;
1027 
1028 	trace_xfs_da_node_add(state->args);
1029 
1030 	node = oldblk->bp->b_addr;
1031 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1032 	btree = nodehdr.btree;
1033 
1034 	ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
1035 	ASSERT(newblk->blkno != 0);
1036 	if (state->args->whichfork == XFS_DATA_FORK)
1037 		ASSERT(newblk->blkno >= state->args->geo->leafblk &&
1038 		       newblk->blkno < state->args->geo->freeblk);
1039 
1040 	/*
1041 	 * We may need to make some room before we insert the new node.
1042 	 */
1043 	tmp = 0;
1044 	if (oldblk->index < nodehdr.count) {
1045 		tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
1046 		memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
1047 	}
1048 	btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
1049 	btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
1050 	xfs_trans_log_buf(state->args->trans, oldblk->bp,
1051 		XFS_DA_LOGRANGE(node, &btree[oldblk->index],
1052 				tmp + sizeof(*btree)));
1053 
1054 	nodehdr.count += 1;
1055 	xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1056 	xfs_trans_log_buf(state->args->trans, oldblk->bp,
1057 		XFS_DA_LOGRANGE(node, &node->hdr,
1058 				state->args->geo->node_hdr_size));
1059 
1060 	/*
1061 	 * Copy the last hash value from the oldblk to propagate upwards.
1062 	 */
1063 	oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1064 }
1065 
1066 /*========================================================================
1067  * Routines used for shrinking the Btree.
1068  *========================================================================*/
1069 
1070 /*
1071  * Deallocate an empty leaf node, remove it from its parent,
1072  * possibly deallocating that block, etc...
1073  */
1074 int
xfs_da3_join(struct xfs_da_state * state)1075 xfs_da3_join(
1076 	struct xfs_da_state	*state)
1077 {
1078 	struct xfs_da_state_blk	*drop_blk;
1079 	struct xfs_da_state_blk	*save_blk;
1080 	int			action = 0;
1081 	int			error;
1082 
1083 	trace_xfs_da_join(state->args);
1084 
1085 	drop_blk = &state->path.blk[ state->path.active-1 ];
1086 	save_blk = &state->altpath.blk[ state->path.active-1 ];
1087 	ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
1088 	ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
1089 	       drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1090 
1091 	/*
1092 	 * Walk back up the tree joining/deallocating as necessary.
1093 	 * When we stop dropping blocks, break out.
1094 	 */
1095 	for (  ; state->path.active >= 2; drop_blk--, save_blk--,
1096 		 state->path.active--) {
1097 		/*
1098 		 * See if we can combine the block with a neighbor.
1099 		 *   (action == 0) => no options, just leave
1100 		 *   (action == 1) => coalesce, then unlink
1101 		 *   (action == 2) => block empty, unlink it
1102 		 */
1103 		switch (drop_blk->magic) {
1104 		case XFS_ATTR_LEAF_MAGIC:
1105 			error = xfs_attr3_leaf_toosmall(state, &action);
1106 			if (error)
1107 				return error;
1108 			if (action == 0)
1109 				return 0;
1110 			xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
1111 			break;
1112 		case XFS_DIR2_LEAFN_MAGIC:
1113 			error = xfs_dir2_leafn_toosmall(state, &action);
1114 			if (error)
1115 				return error;
1116 			if (action == 0)
1117 				return 0;
1118 			xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1119 			break;
1120 		case XFS_DA_NODE_MAGIC:
1121 			/*
1122 			 * Remove the offending node, fixup hashvals,
1123 			 * check for a toosmall neighbor.
1124 			 */
1125 			xfs_da3_node_remove(state, drop_blk);
1126 			xfs_da3_fixhashpath(state, &state->path);
1127 			error = xfs_da3_node_toosmall(state, &action);
1128 			if (error)
1129 				return error;
1130 			if (action == 0)
1131 				return 0;
1132 			xfs_da3_node_unbalance(state, drop_blk, save_blk);
1133 			break;
1134 		}
1135 		xfs_da3_fixhashpath(state, &state->altpath);
1136 		error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
1137 		xfs_da_state_kill_altpath(state);
1138 		if (error)
1139 			return error;
1140 		error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1141 							 drop_blk->bp);
1142 		drop_blk->bp = NULL;
1143 		if (error)
1144 			return error;
1145 	}
1146 	/*
1147 	 * We joined all the way to the top.  If it turns out that
1148 	 * we only have one entry in the root, make the child block
1149 	 * the new root.
1150 	 */
1151 	xfs_da3_node_remove(state, drop_blk);
1152 	xfs_da3_fixhashpath(state, &state->path);
1153 	error = xfs_da3_root_join(state, &state->path.blk[0]);
1154 	return error;
1155 }
1156 
1157 #ifdef	DEBUG
1158 static void
xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo * blkinfo,__u16 level)1159 xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1160 {
1161 	__be16	magic = blkinfo->magic;
1162 
1163 	if (level == 1) {
1164 		ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1165 		       magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1166 		       magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1167 		       magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1168 	} else {
1169 		ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1170 		       magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1171 	}
1172 	ASSERT(!blkinfo->forw);
1173 	ASSERT(!blkinfo->back);
1174 }
1175 #else	/* !DEBUG */
1176 #define	xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1177 #endif	/* !DEBUG */
1178 
1179 /*
1180  * We have only one entry in the root.  Copy the only remaining child of
1181  * the old root to block 0 as the new root node.
1182  */
1183 STATIC int
xfs_da3_root_join(struct xfs_da_state * state,struct xfs_da_state_blk * root_blk)1184 xfs_da3_root_join(
1185 	struct xfs_da_state	*state,
1186 	struct xfs_da_state_blk	*root_blk)
1187 {
1188 	struct xfs_da_intnode	*oldroot;
1189 	struct xfs_da_args	*args;
1190 	xfs_dablk_t		child;
1191 	struct xfs_buf		*bp;
1192 	struct xfs_da3_icnode_hdr oldroothdr;
1193 	int			error;
1194 	struct xfs_inode	*dp = state->args->dp;
1195 
1196 	trace_xfs_da_root_join(state->args);
1197 
1198 	ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1199 
1200 	args = state->args;
1201 	oldroot = root_blk->bp->b_addr;
1202 	xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
1203 	ASSERT(oldroothdr.forw == 0);
1204 	ASSERT(oldroothdr.back == 0);
1205 
1206 	/*
1207 	 * If the root has more than one child, then don't do anything.
1208 	 */
1209 	if (oldroothdr.count > 1)
1210 		return 0;
1211 
1212 	/*
1213 	 * Read in the (only) child block, then copy those bytes into
1214 	 * the root block's buffer and free the original child block.
1215 	 */
1216 	child = be32_to_cpu(oldroothdr.btree[0].before);
1217 	ASSERT(child != 0);
1218 	error = xfs_da3_node_read(args->trans, dp, child, &bp, args->whichfork);
1219 	if (error)
1220 		return error;
1221 	xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1222 
1223 	/*
1224 	 * This could be copying a leaf back into the root block in the case of
1225 	 * there only being a single leaf block left in the tree. Hence we have
1226 	 * to update the b_ops pointer as well to match the buffer type change
1227 	 * that could occur. For dir3 blocks we also need to update the block
1228 	 * number in the buffer header.
1229 	 */
1230 	memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1231 	root_blk->bp->b_ops = bp->b_ops;
1232 	xfs_trans_buf_copy_type(root_blk->bp, bp);
1233 	if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1234 		struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1235 		da3->blkno = cpu_to_be64(xfs_buf_daddr(root_blk->bp));
1236 	}
1237 	xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1238 			  args->geo->blksize - 1);
1239 	error = xfs_da_shrink_inode(args, child, bp);
1240 	return error;
1241 }
1242 
1243 /*
1244  * Check a node block and its neighbors to see if the block should be
1245  * collapsed into one or the other neighbor.  Always keep the block
1246  * with the smaller block number.
1247  * If the current block is over 50% full, don't try to join it, return 0.
1248  * If the block is empty, fill in the state structure and return 2.
1249  * If it can be collapsed, fill in the state structure and return 1.
1250  * If nothing can be done, return 0.
1251  */
1252 STATIC int
xfs_da3_node_toosmall(struct xfs_da_state * state,int * action)1253 xfs_da3_node_toosmall(
1254 	struct xfs_da_state	*state,
1255 	int			*action)
1256 {
1257 	struct xfs_da_intnode	*node;
1258 	struct xfs_da_state_blk	*blk;
1259 	struct xfs_da_blkinfo	*info;
1260 	xfs_dablk_t		blkno;
1261 	struct xfs_buf		*bp;
1262 	struct xfs_da3_icnode_hdr nodehdr;
1263 	int			count;
1264 	int			forward;
1265 	int			error;
1266 	int			retval;
1267 	int			i;
1268 	struct xfs_inode	*dp = state->args->dp;
1269 
1270 	trace_xfs_da_node_toosmall(state->args);
1271 
1272 	/*
1273 	 * Check for the degenerate case of the block being over 50% full.
1274 	 * If so, it's not worth even looking to see if we might be able
1275 	 * to coalesce with a sibling.
1276 	 */
1277 	blk = &state->path.blk[ state->path.active-1 ];
1278 	info = blk->bp->b_addr;
1279 	node = (xfs_da_intnode_t *)info;
1280 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1281 	if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
1282 		*action = 0;	/* blk over 50%, don't try to join */
1283 		return 0;	/* blk over 50%, don't try to join */
1284 	}
1285 
1286 	/*
1287 	 * Check for the degenerate case of the block being empty.
1288 	 * If the block is empty, we'll simply delete it, no need to
1289 	 * coalesce it with a sibling block.  We choose (arbitrarily)
1290 	 * to merge with the forward block unless it is NULL.
1291 	 */
1292 	if (nodehdr.count == 0) {
1293 		/*
1294 		 * Make altpath point to the block we want to keep and
1295 		 * path point to the block we want to drop (this one).
1296 		 */
1297 		forward = (info->forw != 0);
1298 		memcpy(&state->altpath, &state->path, sizeof(state->path));
1299 		error = xfs_da3_path_shift(state, &state->altpath, forward,
1300 						 0, &retval);
1301 		if (error)
1302 			return error;
1303 		if (retval) {
1304 			*action = 0;
1305 		} else {
1306 			*action = 2;
1307 		}
1308 		return 0;
1309 	}
1310 
1311 	/*
1312 	 * Examine each sibling block to see if we can coalesce with
1313 	 * at least 25% free space to spare.  We need to figure out
1314 	 * whether to merge with the forward or the backward block.
1315 	 * We prefer coalescing with the lower numbered sibling so as
1316 	 * to shrink a directory over time.
1317 	 */
1318 	count  = state->args->geo->node_ents;
1319 	count -= state->args->geo->node_ents >> 2;
1320 	count -= nodehdr.count;
1321 
1322 	/* start with smaller blk num */
1323 	forward = nodehdr.forw < nodehdr.back;
1324 	for (i = 0; i < 2; forward = !forward, i++) {
1325 		struct xfs_da3_icnode_hdr thdr;
1326 		if (forward)
1327 			blkno = nodehdr.forw;
1328 		else
1329 			blkno = nodehdr.back;
1330 		if (blkno == 0)
1331 			continue;
1332 		error = xfs_da3_node_read(state->args->trans, dp, blkno, &bp,
1333 				state->args->whichfork);
1334 		if (error)
1335 			return error;
1336 
1337 		node = bp->b_addr;
1338 		xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
1339 		xfs_trans_brelse(state->args->trans, bp);
1340 
1341 		if (count - thdr.count >= 0)
1342 			break;	/* fits with at least 25% to spare */
1343 	}
1344 	if (i >= 2) {
1345 		*action = 0;
1346 		return 0;
1347 	}
1348 
1349 	/*
1350 	 * Make altpath point to the block we want to keep (the lower
1351 	 * numbered block) and path point to the block we want to drop.
1352 	 */
1353 	memcpy(&state->altpath, &state->path, sizeof(state->path));
1354 	if (blkno < blk->blkno) {
1355 		error = xfs_da3_path_shift(state, &state->altpath, forward,
1356 						 0, &retval);
1357 	} else {
1358 		error = xfs_da3_path_shift(state, &state->path, forward,
1359 						 0, &retval);
1360 	}
1361 	if (error)
1362 		return error;
1363 	if (retval) {
1364 		*action = 0;
1365 		return 0;
1366 	}
1367 	*action = 1;
1368 	return 0;
1369 }
1370 
1371 /*
1372  * Pick up the last hashvalue from an intermediate node.
1373  */
1374 STATIC uint
xfs_da3_node_lasthash(struct xfs_inode * dp,struct xfs_buf * bp,int * count)1375 xfs_da3_node_lasthash(
1376 	struct xfs_inode	*dp,
1377 	struct xfs_buf		*bp,
1378 	int			*count)
1379 {
1380 	struct xfs_da3_icnode_hdr nodehdr;
1381 
1382 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
1383 	if (count)
1384 		*count = nodehdr.count;
1385 	if (!nodehdr.count)
1386 		return 0;
1387 	return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
1388 }
1389 
1390 /*
1391  * Walk back up the tree adjusting hash values as necessary,
1392  * when we stop making changes, return.
1393  */
1394 void
xfs_da3_fixhashpath(struct xfs_da_state * state,struct xfs_da_state_path * path)1395 xfs_da3_fixhashpath(
1396 	struct xfs_da_state	*state,
1397 	struct xfs_da_state_path *path)
1398 {
1399 	struct xfs_da_state_blk	*blk;
1400 	struct xfs_da_intnode	*node;
1401 	struct xfs_da_node_entry *btree;
1402 	xfs_dahash_t		lasthash=0;
1403 	int			level;
1404 	int			count;
1405 	struct xfs_inode	*dp = state->args->dp;
1406 
1407 	trace_xfs_da_fixhashpath(state->args);
1408 
1409 	level = path->active-1;
1410 	blk = &path->blk[ level ];
1411 	switch (blk->magic) {
1412 	case XFS_ATTR_LEAF_MAGIC:
1413 		lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1414 		if (count == 0)
1415 			return;
1416 		break;
1417 	case XFS_DIR2_LEAFN_MAGIC:
1418 		lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
1419 		if (count == 0)
1420 			return;
1421 		break;
1422 	case XFS_DA_NODE_MAGIC:
1423 		lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
1424 		if (count == 0)
1425 			return;
1426 		break;
1427 	}
1428 	for (blk--, level--; level >= 0; blk--, level--) {
1429 		struct xfs_da3_icnode_hdr nodehdr;
1430 
1431 		node = blk->bp->b_addr;
1432 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1433 		btree = nodehdr.btree;
1434 		if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
1435 			break;
1436 		blk->hashval = lasthash;
1437 		btree[blk->index].hashval = cpu_to_be32(lasthash);
1438 		xfs_trans_log_buf(state->args->trans, blk->bp,
1439 				  XFS_DA_LOGRANGE(node, &btree[blk->index],
1440 						  sizeof(*btree)));
1441 
1442 		lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1443 	}
1444 }
1445 
1446 /*
1447  * Remove an entry from an intermediate node.
1448  */
1449 STATIC void
xfs_da3_node_remove(struct xfs_da_state * state,struct xfs_da_state_blk * drop_blk)1450 xfs_da3_node_remove(
1451 	struct xfs_da_state	*state,
1452 	struct xfs_da_state_blk	*drop_blk)
1453 {
1454 	struct xfs_da_intnode	*node;
1455 	struct xfs_da3_icnode_hdr nodehdr;
1456 	struct xfs_da_node_entry *btree;
1457 	int			index;
1458 	int			tmp;
1459 	struct xfs_inode	*dp = state->args->dp;
1460 
1461 	trace_xfs_da_node_remove(state->args);
1462 
1463 	node = drop_blk->bp->b_addr;
1464 	xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1465 	ASSERT(drop_blk->index < nodehdr.count);
1466 	ASSERT(drop_blk->index >= 0);
1467 
1468 	/*
1469 	 * Copy over the offending entry, or just zero it out.
1470 	 */
1471 	index = drop_blk->index;
1472 	btree = nodehdr.btree;
1473 	if (index < nodehdr.count - 1) {
1474 		tmp  = nodehdr.count - index - 1;
1475 		tmp *= (uint)sizeof(xfs_da_node_entry_t);
1476 		memmove(&btree[index], &btree[index + 1], tmp);
1477 		xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1478 		    XFS_DA_LOGRANGE(node, &btree[index], tmp));
1479 		index = nodehdr.count - 1;
1480 	}
1481 	memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1482 	xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1483 	    XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1484 	nodehdr.count -= 1;
1485 	xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1486 	xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1487 	    XFS_DA_LOGRANGE(node, &node->hdr, state->args->geo->node_hdr_size));
1488 
1489 	/*
1490 	 * Copy the last hash value from the block to propagate upwards.
1491 	 */
1492 	drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
1493 }
1494 
1495 /*
1496  * Unbalance the elements between two intermediate nodes,
1497  * move all Btree elements from one node into another.
1498  */
1499 STATIC void
xfs_da3_node_unbalance(struct xfs_da_state * state,struct xfs_da_state_blk * drop_blk,struct xfs_da_state_blk * save_blk)1500 xfs_da3_node_unbalance(
1501 	struct xfs_da_state	*state,
1502 	struct xfs_da_state_blk	*drop_blk,
1503 	struct xfs_da_state_blk	*save_blk)
1504 {
1505 	struct xfs_da_intnode	*drop_node;
1506 	struct xfs_da_intnode	*save_node;
1507 	struct xfs_da_node_entry *drop_btree;
1508 	struct xfs_da_node_entry *save_btree;
1509 	struct xfs_da3_icnode_hdr drop_hdr;
1510 	struct xfs_da3_icnode_hdr save_hdr;
1511 	struct xfs_trans	*tp;
1512 	int			sindex;
1513 	int			tmp;
1514 	struct xfs_inode	*dp = state->args->dp;
1515 
1516 	trace_xfs_da_node_unbalance(state->args);
1517 
1518 	drop_node = drop_blk->bp->b_addr;
1519 	save_node = save_blk->bp->b_addr;
1520 	xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
1521 	xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
1522 	drop_btree = drop_hdr.btree;
1523 	save_btree = save_hdr.btree;
1524 	tp = state->args->trans;
1525 
1526 	/*
1527 	 * If the dying block has lower hashvals, then move all the
1528 	 * elements in the remaining block up to make a hole.
1529 	 */
1530 	if ((be32_to_cpu(drop_btree[0].hashval) <
1531 			be32_to_cpu(save_btree[0].hashval)) ||
1532 	    (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1533 			be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1534 		/* XXX: check this - is memmove dst correct? */
1535 		tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1536 		memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1537 
1538 		sindex = 0;
1539 		xfs_trans_log_buf(tp, save_blk->bp,
1540 			XFS_DA_LOGRANGE(save_node, &save_btree[0],
1541 				(save_hdr.count + drop_hdr.count) *
1542 						sizeof(xfs_da_node_entry_t)));
1543 	} else {
1544 		sindex = save_hdr.count;
1545 		xfs_trans_log_buf(tp, save_blk->bp,
1546 			XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1547 				drop_hdr.count * sizeof(xfs_da_node_entry_t)));
1548 	}
1549 
1550 	/*
1551 	 * Move all the B-tree elements from drop_blk to save_blk.
1552 	 */
1553 	tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1554 	memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1555 	save_hdr.count += drop_hdr.count;
1556 
1557 	xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
1558 	xfs_trans_log_buf(tp, save_blk->bp,
1559 		XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1560 				state->args->geo->node_hdr_size));
1561 
1562 	/*
1563 	 * Save the last hashval in the remaining block for upward propagation.
1564 	 */
1565 	save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
1566 }
1567 
1568 /*========================================================================
1569  * Routines used for finding things in the Btree.
1570  *========================================================================*/
1571 
1572 /*
1573  * Walk down the Btree looking for a particular filename, filling
1574  * in the state structure as we go.
1575  *
1576  * We will set the state structure to point to each of the elements
1577  * in each of the nodes where either the hashval is or should be.
1578  *
1579  * We support duplicate hashval's so for each entry in the current
1580  * node that could contain the desired hashval, descend.  This is a
1581  * pruned depth-first tree search.
1582  */
1583 int							/* error */
xfs_da3_node_lookup_int(struct xfs_da_state * state,int * result)1584 xfs_da3_node_lookup_int(
1585 	struct xfs_da_state	*state,
1586 	int			*result)
1587 {
1588 	struct xfs_da_state_blk	*blk;
1589 	struct xfs_da_blkinfo	*curr;
1590 	struct xfs_da_intnode	*node;
1591 	struct xfs_da_node_entry *btree;
1592 	struct xfs_da3_icnode_hdr nodehdr;
1593 	struct xfs_da_args	*args;
1594 	xfs_dablk_t		blkno;
1595 	xfs_dahash_t		hashval;
1596 	xfs_dahash_t		btreehashval;
1597 	int			probe;
1598 	int			span;
1599 	int			max;
1600 	int			error;
1601 	int			retval;
1602 	unsigned int		expected_level = 0;
1603 	uint16_t		magic;
1604 	struct xfs_inode	*dp = state->args->dp;
1605 
1606 	args = state->args;
1607 
1608 	/*
1609 	 * Descend thru the B-tree searching each level for the right
1610 	 * node to use, until the right hashval is found.
1611 	 */
1612 	blkno = args->geo->leafblk;
1613 	for (blk = &state->path.blk[0], state->path.active = 1;
1614 			 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1615 			 blk++, state->path.active++) {
1616 		/*
1617 		 * Read the next node down in the tree.
1618 		 */
1619 		blk->blkno = blkno;
1620 		error = xfs_da3_node_read(args->trans, args->dp, blkno,
1621 					&blk->bp, args->whichfork);
1622 		if (error) {
1623 			blk->blkno = 0;
1624 			state->path.active--;
1625 			return error;
1626 		}
1627 		curr = blk->bp->b_addr;
1628 		magic = be16_to_cpu(curr->magic);
1629 
1630 		if (magic == XFS_ATTR_LEAF_MAGIC ||
1631 		    magic == XFS_ATTR3_LEAF_MAGIC) {
1632 			blk->magic = XFS_ATTR_LEAF_MAGIC;
1633 			blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1634 			break;
1635 		}
1636 
1637 		if (magic == XFS_DIR2_LEAFN_MAGIC ||
1638 		    magic == XFS_DIR3_LEAFN_MAGIC) {
1639 			blk->magic = XFS_DIR2_LEAFN_MAGIC;
1640 			blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1641 							      blk->bp, NULL);
1642 			break;
1643 		}
1644 
1645 		if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
1646 			xfs_buf_mark_corrupt(blk->bp);
1647 			return -EFSCORRUPTED;
1648 		}
1649 
1650 		blk->magic = XFS_DA_NODE_MAGIC;
1651 
1652 		/*
1653 		 * Search an intermediate node for a match.
1654 		 */
1655 		node = blk->bp->b_addr;
1656 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1657 		btree = nodehdr.btree;
1658 
1659 		/* Tree taller than we can handle; bail out! */
1660 		if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
1661 			xfs_buf_mark_corrupt(blk->bp);
1662 			return -EFSCORRUPTED;
1663 		}
1664 
1665 		/* Check the level from the root. */
1666 		if (blkno == args->geo->leafblk)
1667 			expected_level = nodehdr.level - 1;
1668 		else if (expected_level != nodehdr.level) {
1669 			xfs_buf_mark_corrupt(blk->bp);
1670 			return -EFSCORRUPTED;
1671 		} else
1672 			expected_level--;
1673 
1674 		max = nodehdr.count;
1675 		blk->hashval = be32_to_cpu(btree[max - 1].hashval);
1676 
1677 		/*
1678 		 * Binary search.  (note: small blocks will skip loop)
1679 		 */
1680 		probe = span = max / 2;
1681 		hashval = args->hashval;
1682 		while (span > 4) {
1683 			span /= 2;
1684 			btreehashval = be32_to_cpu(btree[probe].hashval);
1685 			if (btreehashval < hashval)
1686 				probe += span;
1687 			else if (btreehashval > hashval)
1688 				probe -= span;
1689 			else
1690 				break;
1691 		}
1692 		ASSERT((probe >= 0) && (probe < max));
1693 		ASSERT((span <= 4) ||
1694 			(be32_to_cpu(btree[probe].hashval) == hashval));
1695 
1696 		/*
1697 		 * Since we may have duplicate hashval's, find the first
1698 		 * matching hashval in the node.
1699 		 */
1700 		while (probe > 0 &&
1701 		       be32_to_cpu(btree[probe].hashval) >= hashval) {
1702 			probe--;
1703 		}
1704 		while (probe < max &&
1705 		       be32_to_cpu(btree[probe].hashval) < hashval) {
1706 			probe++;
1707 		}
1708 
1709 		/*
1710 		 * Pick the right block to descend on.
1711 		 */
1712 		if (probe == max) {
1713 			blk->index = max - 1;
1714 			blkno = be32_to_cpu(btree[max - 1].before);
1715 		} else {
1716 			blk->index = probe;
1717 			blkno = be32_to_cpu(btree[probe].before);
1718 		}
1719 
1720 		/* We can't point back to the root. */
1721 		if (XFS_IS_CORRUPT(dp->i_mount, blkno == args->geo->leafblk))
1722 			return -EFSCORRUPTED;
1723 	}
1724 
1725 	if (XFS_IS_CORRUPT(dp->i_mount, expected_level != 0))
1726 		return -EFSCORRUPTED;
1727 
1728 	/*
1729 	 * A leaf block that ends in the hashval that we are interested in
1730 	 * (final hashval == search hashval) means that the next block may
1731 	 * contain more entries with the same hashval, shift upward to the
1732 	 * next leaf and keep searching.
1733 	 */
1734 	for (;;) {
1735 		if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
1736 			retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1737 							&blk->index, state);
1738 		} else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1739 			retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
1740 			blk->index = args->index;
1741 			args->blkno = blk->blkno;
1742 		} else {
1743 			ASSERT(0);
1744 			return -EFSCORRUPTED;
1745 		}
1746 		if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
1747 		    (blk->hashval == args->hashval)) {
1748 			error = xfs_da3_path_shift(state, &state->path, 1, 1,
1749 							 &retval);
1750 			if (error)
1751 				return error;
1752 			if (retval == 0) {
1753 				continue;
1754 			} else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1755 				/* path_shift() gives ENOENT */
1756 				retval = -ENOATTR;
1757 			}
1758 		}
1759 		break;
1760 	}
1761 	*result = retval;
1762 	return 0;
1763 }
1764 
1765 /*========================================================================
1766  * Utility routines.
1767  *========================================================================*/
1768 
1769 /*
1770  * Compare two intermediate nodes for "order".
1771  */
1772 STATIC int
xfs_da3_node_order(struct xfs_inode * dp,struct xfs_buf * node1_bp,struct xfs_buf * node2_bp)1773 xfs_da3_node_order(
1774 	struct xfs_inode *dp,
1775 	struct xfs_buf	*node1_bp,
1776 	struct xfs_buf	*node2_bp)
1777 {
1778 	struct xfs_da_intnode	*node1;
1779 	struct xfs_da_intnode	*node2;
1780 	struct xfs_da_node_entry *btree1;
1781 	struct xfs_da_node_entry *btree2;
1782 	struct xfs_da3_icnode_hdr node1hdr;
1783 	struct xfs_da3_icnode_hdr node2hdr;
1784 
1785 	node1 = node1_bp->b_addr;
1786 	node2 = node2_bp->b_addr;
1787 	xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
1788 	xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
1789 	btree1 = node1hdr.btree;
1790 	btree2 = node2hdr.btree;
1791 
1792 	if (node1hdr.count > 0 && node2hdr.count > 0 &&
1793 	    ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1794 	     (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1795 	      be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1796 		return 1;
1797 	}
1798 	return 0;
1799 }
1800 
1801 /*
1802  * Link a new block into a doubly linked list of blocks (of whatever type).
1803  */
1804 int							/* error */
xfs_da3_blk_link(struct xfs_da_state * state,struct xfs_da_state_blk * old_blk,struct xfs_da_state_blk * new_blk)1805 xfs_da3_blk_link(
1806 	struct xfs_da_state	*state,
1807 	struct xfs_da_state_blk	*old_blk,
1808 	struct xfs_da_state_blk	*new_blk)
1809 {
1810 	struct xfs_da_blkinfo	*old_info;
1811 	struct xfs_da_blkinfo	*new_info;
1812 	struct xfs_da_blkinfo	*tmp_info;
1813 	struct xfs_da_args	*args;
1814 	struct xfs_buf		*bp;
1815 	int			before = 0;
1816 	int			error;
1817 	struct xfs_inode	*dp = state->args->dp;
1818 
1819 	/*
1820 	 * Set up environment.
1821 	 */
1822 	args = state->args;
1823 	ASSERT(args != NULL);
1824 	old_info = old_blk->bp->b_addr;
1825 	new_info = new_blk->bp->b_addr;
1826 	ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1827 	       old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1828 	       old_blk->magic == XFS_ATTR_LEAF_MAGIC);
1829 
1830 	switch (old_blk->magic) {
1831 	case XFS_ATTR_LEAF_MAGIC:
1832 		before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1833 		break;
1834 	case XFS_DIR2_LEAFN_MAGIC:
1835 		before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
1836 		break;
1837 	case XFS_DA_NODE_MAGIC:
1838 		before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
1839 		break;
1840 	}
1841 
1842 	/*
1843 	 * Link blocks in appropriate order.
1844 	 */
1845 	if (before) {
1846 		/*
1847 		 * Link new block in before existing block.
1848 		 */
1849 		trace_xfs_da_link_before(args);
1850 		new_info->forw = cpu_to_be32(old_blk->blkno);
1851 		new_info->back = old_info->back;
1852 		if (old_info->back) {
1853 			error = xfs_da3_node_read(args->trans, dp,
1854 						be32_to_cpu(old_info->back),
1855 						&bp, args->whichfork);
1856 			if (error)
1857 				return error;
1858 			ASSERT(bp != NULL);
1859 			tmp_info = bp->b_addr;
1860 			ASSERT(tmp_info->magic == old_info->magic);
1861 			ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1862 			tmp_info->forw = cpu_to_be32(new_blk->blkno);
1863 			xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1864 		}
1865 		old_info->back = cpu_to_be32(new_blk->blkno);
1866 	} else {
1867 		/*
1868 		 * Link new block in after existing block.
1869 		 */
1870 		trace_xfs_da_link_after(args);
1871 		new_info->forw = old_info->forw;
1872 		new_info->back = cpu_to_be32(old_blk->blkno);
1873 		if (old_info->forw) {
1874 			error = xfs_da3_node_read(args->trans, dp,
1875 						be32_to_cpu(old_info->forw),
1876 						&bp, args->whichfork);
1877 			if (error)
1878 				return error;
1879 			ASSERT(bp != NULL);
1880 			tmp_info = bp->b_addr;
1881 			ASSERT(tmp_info->magic == old_info->magic);
1882 			ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1883 			tmp_info->back = cpu_to_be32(new_blk->blkno);
1884 			xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1885 		}
1886 		old_info->forw = cpu_to_be32(new_blk->blkno);
1887 	}
1888 
1889 	xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1890 	xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
1891 	return 0;
1892 }
1893 
1894 /*
1895  * Unlink a block from a doubly linked list of blocks.
1896  */
1897 STATIC int						/* error */
xfs_da3_blk_unlink(struct xfs_da_state * state,struct xfs_da_state_blk * drop_blk,struct xfs_da_state_blk * save_blk)1898 xfs_da3_blk_unlink(
1899 	struct xfs_da_state	*state,
1900 	struct xfs_da_state_blk	*drop_blk,
1901 	struct xfs_da_state_blk	*save_blk)
1902 {
1903 	struct xfs_da_blkinfo	*drop_info;
1904 	struct xfs_da_blkinfo	*save_info;
1905 	struct xfs_da_blkinfo	*tmp_info;
1906 	struct xfs_da_args	*args;
1907 	struct xfs_buf		*bp;
1908 	int			error;
1909 
1910 	/*
1911 	 * Set up environment.
1912 	 */
1913 	args = state->args;
1914 	ASSERT(args != NULL);
1915 	save_info = save_blk->bp->b_addr;
1916 	drop_info = drop_blk->bp->b_addr;
1917 	ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1918 	       save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1919 	       save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1920 	ASSERT(save_blk->magic == drop_blk->magic);
1921 	ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1922 	       (be32_to_cpu(save_info->back) == drop_blk->blkno));
1923 	ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1924 	       (be32_to_cpu(drop_info->back) == save_blk->blkno));
1925 
1926 	/*
1927 	 * Unlink the leaf block from the doubly linked chain of leaves.
1928 	 */
1929 	if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1930 		trace_xfs_da_unlink_back(args);
1931 		save_info->back = drop_info->back;
1932 		if (drop_info->back) {
1933 			error = xfs_da3_node_read(args->trans, args->dp,
1934 						be32_to_cpu(drop_info->back),
1935 						&bp, args->whichfork);
1936 			if (error)
1937 				return error;
1938 			ASSERT(bp != NULL);
1939 			tmp_info = bp->b_addr;
1940 			ASSERT(tmp_info->magic == save_info->magic);
1941 			ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1942 			tmp_info->forw = cpu_to_be32(save_blk->blkno);
1943 			xfs_trans_log_buf(args->trans, bp, 0,
1944 						    sizeof(*tmp_info) - 1);
1945 		}
1946 	} else {
1947 		trace_xfs_da_unlink_forward(args);
1948 		save_info->forw = drop_info->forw;
1949 		if (drop_info->forw) {
1950 			error = xfs_da3_node_read(args->trans, args->dp,
1951 						be32_to_cpu(drop_info->forw),
1952 						&bp, args->whichfork);
1953 			if (error)
1954 				return error;
1955 			ASSERT(bp != NULL);
1956 			tmp_info = bp->b_addr;
1957 			ASSERT(tmp_info->magic == save_info->magic);
1958 			ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1959 			tmp_info->back = cpu_to_be32(save_blk->blkno);
1960 			xfs_trans_log_buf(args->trans, bp, 0,
1961 						    sizeof(*tmp_info) - 1);
1962 		}
1963 	}
1964 
1965 	xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
1966 	return 0;
1967 }
1968 
1969 /*
1970  * Move a path "forward" or "!forward" one block at the current level.
1971  *
1972  * This routine will adjust a "path" to point to the next block
1973  * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1974  * Btree, including updating pointers to the intermediate nodes between
1975  * the new bottom and the root.
1976  */
1977 int							/* error */
xfs_da3_path_shift(struct xfs_da_state * state,struct xfs_da_state_path * path,int forward,int release,int * result)1978 xfs_da3_path_shift(
1979 	struct xfs_da_state	*state,
1980 	struct xfs_da_state_path *path,
1981 	int			forward,
1982 	int			release,
1983 	int			*result)
1984 {
1985 	struct xfs_da_state_blk	*blk;
1986 	struct xfs_da_blkinfo	*info;
1987 	struct xfs_da_args	*args;
1988 	struct xfs_da_node_entry *btree;
1989 	struct xfs_da3_icnode_hdr nodehdr;
1990 	struct xfs_buf		*bp;
1991 	xfs_dablk_t		blkno = 0;
1992 	int			level;
1993 	int			error;
1994 	struct xfs_inode	*dp = state->args->dp;
1995 
1996 	trace_xfs_da_path_shift(state->args);
1997 
1998 	/*
1999 	 * Roll up the Btree looking for the first block where our
2000 	 * current index is not at the edge of the block.  Note that
2001 	 * we skip the bottom layer because we want the sibling block.
2002 	 */
2003 	args = state->args;
2004 	ASSERT(args != NULL);
2005 	ASSERT(path != NULL);
2006 	ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
2007 	level = (path->active-1) - 1;	/* skip bottom layer in path */
2008 	for (; level >= 0; level--) {
2009 		blk = &path->blk[level];
2010 		xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2011 					   blk->bp->b_addr);
2012 
2013 		if (forward && (blk->index < nodehdr.count - 1)) {
2014 			blk->index++;
2015 			blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
2016 			break;
2017 		} else if (!forward && (blk->index > 0)) {
2018 			blk->index--;
2019 			blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
2020 			break;
2021 		}
2022 	}
2023 	if (level < 0) {
2024 		*result = -ENOENT;	/* we're out of our tree */
2025 		ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
2026 		return 0;
2027 	}
2028 
2029 	/*
2030 	 * Roll down the edge of the subtree until we reach the
2031 	 * same depth we were at originally.
2032 	 */
2033 	for (blk++, level++; level < path->active; blk++, level++) {
2034 		/*
2035 		 * Read the next child block into a local buffer.
2036 		 */
2037 		error = xfs_da3_node_read(args->trans, dp, blkno, &bp,
2038 					  args->whichfork);
2039 		if (error)
2040 			return error;
2041 
2042 		/*
2043 		 * Release the old block (if it's dirty, the trans doesn't
2044 		 * actually let go) and swap the local buffer into the path
2045 		 * structure. This ensures failure of the above read doesn't set
2046 		 * a NULL buffer in an active slot in the path.
2047 		 */
2048 		if (release)
2049 			xfs_trans_brelse(args->trans, blk->bp);
2050 		blk->blkno = blkno;
2051 		blk->bp = bp;
2052 
2053 		info = blk->bp->b_addr;
2054 		ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
2055 		       info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
2056 		       info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2057 		       info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
2058 		       info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
2059 		       info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
2060 
2061 
2062 		/*
2063 		 * Note: we flatten the magic number to a single type so we
2064 		 * don't have to compare against crc/non-crc types elsewhere.
2065 		 */
2066 		switch (be16_to_cpu(info->magic)) {
2067 		case XFS_DA_NODE_MAGIC:
2068 		case XFS_DA3_NODE_MAGIC:
2069 			blk->magic = XFS_DA_NODE_MAGIC;
2070 			xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2071 						   bp->b_addr);
2072 			btree = nodehdr.btree;
2073 			blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
2074 			if (forward)
2075 				blk->index = 0;
2076 			else
2077 				blk->index = nodehdr.count - 1;
2078 			blkno = be32_to_cpu(btree[blk->index].before);
2079 			break;
2080 		case XFS_ATTR_LEAF_MAGIC:
2081 		case XFS_ATTR3_LEAF_MAGIC:
2082 			blk->magic = XFS_ATTR_LEAF_MAGIC;
2083 			ASSERT(level == path->active-1);
2084 			blk->index = 0;
2085 			blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
2086 			break;
2087 		case XFS_DIR2_LEAFN_MAGIC:
2088 		case XFS_DIR3_LEAFN_MAGIC:
2089 			blk->magic = XFS_DIR2_LEAFN_MAGIC;
2090 			ASSERT(level == path->active-1);
2091 			blk->index = 0;
2092 			blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
2093 							      blk->bp, NULL);
2094 			break;
2095 		default:
2096 			ASSERT(0);
2097 			break;
2098 		}
2099 	}
2100 	*result = 0;
2101 	return 0;
2102 }
2103 
2104 
2105 /*========================================================================
2106  * Utility routines.
2107  *========================================================================*/
2108 
2109 /*
2110  * Implement a simple hash on a character string.
2111  * Rotate the hash value by 7 bits, then XOR each character in.
2112  * This is implemented with some source-level loop unrolling.
2113  */
2114 xfs_dahash_t
xfs_da_hashname(const uint8_t * name,int namelen)2115 xfs_da_hashname(const uint8_t *name, int namelen)
2116 {
2117 	xfs_dahash_t hash;
2118 
2119 	/*
2120 	 * Do four characters at a time as long as we can.
2121 	 */
2122 	for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2123 		hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2124 		       (name[3] << 0) ^ rol32(hash, 7 * 4);
2125 
2126 	/*
2127 	 * Now do the rest of the characters.
2128 	 */
2129 	switch (namelen) {
2130 	case 3:
2131 		return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2132 		       rol32(hash, 7 * 3);
2133 	case 2:
2134 		return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2135 	case 1:
2136 		return (name[0] << 0) ^ rol32(hash, 7 * 1);
2137 	default: /* case 0: */
2138 		return hash;
2139 	}
2140 }
2141 
2142 enum xfs_dacmp
xfs_da_compname(struct xfs_da_args * args,const unsigned char * name,int len)2143 xfs_da_compname(
2144 	struct xfs_da_args *args,
2145 	const unsigned char *name,
2146 	int		len)
2147 {
2148 	return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2149 					XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2150 }
2151 
2152 int
xfs_da_grow_inode_int(struct xfs_da_args * args,xfs_fileoff_t * bno,int count)2153 xfs_da_grow_inode_int(
2154 	struct xfs_da_args	*args,
2155 	xfs_fileoff_t		*bno,
2156 	int			count)
2157 {
2158 	struct xfs_trans	*tp = args->trans;
2159 	struct xfs_inode	*dp = args->dp;
2160 	int			w = args->whichfork;
2161 	xfs_rfsblock_t		nblks = dp->i_nblocks;
2162 	struct xfs_bmbt_irec	map, *mapp = &map;
2163 	int			nmap, error, got, i, mapi = 1;
2164 
2165 	/*
2166 	 * Find a spot in the file space to put the new block.
2167 	 */
2168 	error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2169 	if (error)
2170 		return error;
2171 
2172 	/*
2173 	 * Try mapping it in one filesystem block.
2174 	 */
2175 	nmap = 1;
2176 	error = xfs_bmapi_write(tp, dp, *bno, count,
2177 			xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
2178 			args->total, &map, &nmap);
2179 	if (error == -ENOSPC && count > 1) {
2180 		xfs_fileoff_t		b;
2181 		int			c;
2182 
2183 		/*
2184 		 * If we didn't get it and the block might work if fragmented,
2185 		 * try without the CONTIG flag.  Loop until we get it all.
2186 		 */
2187 		mapp = kmem_alloc(sizeof(*mapp) * count, 0);
2188 		for (b = *bno, mapi = 0; b < *bno + count; ) {
2189 			c = (int)(*bno + count - b);
2190 			nmap = min(XFS_BMAP_MAX_NMAP, c);
2191 			error = xfs_bmapi_write(tp, dp, b, c,
2192 					xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2193 					args->total, &mapp[mapi], &nmap);
2194 			if (error)
2195 				goto out_free_map;
2196 			mapi += nmap;
2197 			b = mapp[mapi - 1].br_startoff +
2198 			    mapp[mapi - 1].br_blockcount;
2199 		}
2200 	}
2201 	if (error)
2202 		goto out_free_map;
2203 
2204 	/*
2205 	 * Count the blocks we got, make sure it matches the total.
2206 	 */
2207 	for (i = 0, got = 0; i < mapi; i++)
2208 		got += mapp[i].br_blockcount;
2209 	if (got != count || mapp[0].br_startoff != *bno ||
2210 	    mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2211 	    *bno + count) {
2212 		error = -ENOSPC;
2213 		goto out_free_map;
2214 	}
2215 
2216 	/* account for newly allocated blocks in reserved blocks total */
2217 	args->total -= dp->i_nblocks - nblks;
2218 
2219 out_free_map:
2220 	if (mapp != &map)
2221 		kmem_free(mapp);
2222 	return error;
2223 }
2224 
2225 /*
2226  * Add a block to the btree ahead of the file.
2227  * Return the new block number to the caller.
2228  */
2229 int
xfs_da_grow_inode(struct xfs_da_args * args,xfs_dablk_t * new_blkno)2230 xfs_da_grow_inode(
2231 	struct xfs_da_args	*args,
2232 	xfs_dablk_t		*new_blkno)
2233 {
2234 	xfs_fileoff_t		bno;
2235 	int			error;
2236 
2237 	trace_xfs_da_grow_inode(args);
2238 
2239 	bno = args->geo->leafblk;
2240 	error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2241 	if (!error)
2242 		*new_blkno = (xfs_dablk_t)bno;
2243 	return error;
2244 }
2245 
2246 /*
2247  * Ick.  We need to always be able to remove a btree block, even
2248  * if there's no space reservation because the filesystem is full.
2249  * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2250  * It swaps the target block with the last block in the file.  The
2251  * last block in the file can always be removed since it can't cause
2252  * a bmap btree split to do that.
2253  */
2254 STATIC int
xfs_da3_swap_lastblock(struct xfs_da_args * args,xfs_dablk_t * dead_blknop,struct xfs_buf ** dead_bufp)2255 xfs_da3_swap_lastblock(
2256 	struct xfs_da_args	*args,
2257 	xfs_dablk_t		*dead_blknop,
2258 	struct xfs_buf		**dead_bufp)
2259 {
2260 	struct xfs_da_blkinfo	*dead_info;
2261 	struct xfs_da_blkinfo	*sib_info;
2262 	struct xfs_da_intnode	*par_node;
2263 	struct xfs_da_intnode	*dead_node;
2264 	struct xfs_dir2_leaf	*dead_leaf2;
2265 	struct xfs_da_node_entry *btree;
2266 	struct xfs_da3_icnode_hdr par_hdr;
2267 	struct xfs_inode	*dp;
2268 	struct xfs_trans	*tp;
2269 	struct xfs_mount	*mp;
2270 	struct xfs_buf		*dead_buf;
2271 	struct xfs_buf		*last_buf;
2272 	struct xfs_buf		*sib_buf;
2273 	struct xfs_buf		*par_buf;
2274 	xfs_dahash_t		dead_hash;
2275 	xfs_fileoff_t		lastoff;
2276 	xfs_dablk_t		dead_blkno;
2277 	xfs_dablk_t		last_blkno;
2278 	xfs_dablk_t		sib_blkno;
2279 	xfs_dablk_t		par_blkno;
2280 	int			error;
2281 	int			w;
2282 	int			entno;
2283 	int			level;
2284 	int			dead_level;
2285 
2286 	trace_xfs_da_swap_lastblock(args);
2287 
2288 	dead_buf = *dead_bufp;
2289 	dead_blkno = *dead_blknop;
2290 	tp = args->trans;
2291 	dp = args->dp;
2292 	w = args->whichfork;
2293 	ASSERT(w == XFS_DATA_FORK);
2294 	mp = dp->i_mount;
2295 	lastoff = args->geo->freeblk;
2296 	error = xfs_bmap_last_before(tp, dp, &lastoff, w);
2297 	if (error)
2298 		return error;
2299 	if (XFS_IS_CORRUPT(mp, lastoff == 0))
2300 		return -EFSCORRUPTED;
2301 	/*
2302 	 * Read the last block in the btree space.
2303 	 */
2304 	last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
2305 	error = xfs_da3_node_read(tp, dp, last_blkno, &last_buf, w);
2306 	if (error)
2307 		return error;
2308 	/*
2309 	 * Copy the last block into the dead buffer and log it.
2310 	 * On CRC-enabled file systems, also update the stamped in blkno.
2311 	 */
2312 	memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2313 	if (xfs_has_crc(mp)) {
2314 		struct xfs_da3_blkinfo *da3 = dead_buf->b_addr;
2315 
2316 		da3->blkno = cpu_to_be64(xfs_buf_daddr(dead_buf));
2317 	}
2318 	xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2319 	dead_info = dead_buf->b_addr;
2320 
2321 	/*
2322 	 * Get values from the moved block.
2323 	 */
2324 	if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2325 	    dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2326 		struct xfs_dir3_icleaf_hdr leafhdr;
2327 		struct xfs_dir2_leaf_entry *ents;
2328 
2329 		dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2330 		xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr,
2331 					    dead_leaf2);
2332 		ents = leafhdr.ents;
2333 		dead_level = 0;
2334 		dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
2335 	} else {
2336 		struct xfs_da3_icnode_hdr deadhdr;
2337 
2338 		dead_node = (xfs_da_intnode_t *)dead_info;
2339 		xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
2340 		btree = deadhdr.btree;
2341 		dead_level = deadhdr.level;
2342 		dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
2343 	}
2344 	sib_buf = par_buf = NULL;
2345 	/*
2346 	 * If the moved block has a left sibling, fix up the pointers.
2347 	 */
2348 	if ((sib_blkno = be32_to_cpu(dead_info->back))) {
2349 		error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
2350 		if (error)
2351 			goto done;
2352 		sib_info = sib_buf->b_addr;
2353 		if (XFS_IS_CORRUPT(mp,
2354 				   be32_to_cpu(sib_info->forw) != last_blkno ||
2355 				   sib_info->magic != dead_info->magic)) {
2356 			error = -EFSCORRUPTED;
2357 			goto done;
2358 		}
2359 		sib_info->forw = cpu_to_be32(dead_blkno);
2360 		xfs_trans_log_buf(tp, sib_buf,
2361 			XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2362 					sizeof(sib_info->forw)));
2363 		sib_buf = NULL;
2364 	}
2365 	/*
2366 	 * If the moved block has a right sibling, fix up the pointers.
2367 	 */
2368 	if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
2369 		error = xfs_da3_node_read(tp, dp, sib_blkno, &sib_buf, w);
2370 		if (error)
2371 			goto done;
2372 		sib_info = sib_buf->b_addr;
2373 		if (XFS_IS_CORRUPT(mp,
2374 				   be32_to_cpu(sib_info->back) != last_blkno ||
2375 				   sib_info->magic != dead_info->magic)) {
2376 			error = -EFSCORRUPTED;
2377 			goto done;
2378 		}
2379 		sib_info->back = cpu_to_be32(dead_blkno);
2380 		xfs_trans_log_buf(tp, sib_buf,
2381 			XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2382 					sizeof(sib_info->back)));
2383 		sib_buf = NULL;
2384 	}
2385 	par_blkno = args->geo->leafblk;
2386 	level = -1;
2387 	/*
2388 	 * Walk down the tree looking for the parent of the moved block.
2389 	 */
2390 	for (;;) {
2391 		error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
2392 		if (error)
2393 			goto done;
2394 		par_node = par_buf->b_addr;
2395 		xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2396 		if (XFS_IS_CORRUPT(mp,
2397 				   level >= 0 && level != par_hdr.level + 1)) {
2398 			error = -EFSCORRUPTED;
2399 			goto done;
2400 		}
2401 		level = par_hdr.level;
2402 		btree = par_hdr.btree;
2403 		for (entno = 0;
2404 		     entno < par_hdr.count &&
2405 		     be32_to_cpu(btree[entno].hashval) < dead_hash;
2406 		     entno++)
2407 			continue;
2408 		if (XFS_IS_CORRUPT(mp, entno == par_hdr.count)) {
2409 			error = -EFSCORRUPTED;
2410 			goto done;
2411 		}
2412 		par_blkno = be32_to_cpu(btree[entno].before);
2413 		if (level == dead_level + 1)
2414 			break;
2415 		xfs_trans_brelse(tp, par_buf);
2416 		par_buf = NULL;
2417 	}
2418 	/*
2419 	 * We're in the right parent block.
2420 	 * Look for the right entry.
2421 	 */
2422 	for (;;) {
2423 		for (;
2424 		     entno < par_hdr.count &&
2425 		     be32_to_cpu(btree[entno].before) != last_blkno;
2426 		     entno++)
2427 			continue;
2428 		if (entno < par_hdr.count)
2429 			break;
2430 		par_blkno = par_hdr.forw;
2431 		xfs_trans_brelse(tp, par_buf);
2432 		par_buf = NULL;
2433 		if (XFS_IS_CORRUPT(mp, par_blkno == 0)) {
2434 			error = -EFSCORRUPTED;
2435 			goto done;
2436 		}
2437 		error = xfs_da3_node_read(tp, dp, par_blkno, &par_buf, w);
2438 		if (error)
2439 			goto done;
2440 		par_node = par_buf->b_addr;
2441 		xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2442 		if (XFS_IS_CORRUPT(mp, par_hdr.level != level)) {
2443 			error = -EFSCORRUPTED;
2444 			goto done;
2445 		}
2446 		btree = par_hdr.btree;
2447 		entno = 0;
2448 	}
2449 	/*
2450 	 * Update the parent entry pointing to the moved block.
2451 	 */
2452 	btree[entno].before = cpu_to_be32(dead_blkno);
2453 	xfs_trans_log_buf(tp, par_buf,
2454 		XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2455 				sizeof(btree[entno].before)));
2456 	*dead_blknop = last_blkno;
2457 	*dead_bufp = last_buf;
2458 	return 0;
2459 done:
2460 	if (par_buf)
2461 		xfs_trans_brelse(tp, par_buf);
2462 	if (sib_buf)
2463 		xfs_trans_brelse(tp, sib_buf);
2464 	xfs_trans_brelse(tp, last_buf);
2465 	return error;
2466 }
2467 
2468 /*
2469  * Remove a btree block from a directory or attribute.
2470  */
2471 int
xfs_da_shrink_inode(struct xfs_da_args * args,xfs_dablk_t dead_blkno,struct xfs_buf * dead_buf)2472 xfs_da_shrink_inode(
2473 	struct xfs_da_args	*args,
2474 	xfs_dablk_t		dead_blkno,
2475 	struct xfs_buf		*dead_buf)
2476 {
2477 	struct xfs_inode	*dp;
2478 	int			done, error, w, count;
2479 	struct xfs_trans	*tp;
2480 
2481 	trace_xfs_da_shrink_inode(args);
2482 
2483 	dp = args->dp;
2484 	w = args->whichfork;
2485 	tp = args->trans;
2486 	count = args->geo->fsbcount;
2487 	for (;;) {
2488 		/*
2489 		 * Remove extents.  If we get ENOSPC for a dir we have to move
2490 		 * the last block to the place we want to kill.
2491 		 */
2492 		error = xfs_bunmapi(tp, dp, dead_blkno, count,
2493 				    xfs_bmapi_aflag(w), 0, &done);
2494 		if (error == -ENOSPC) {
2495 			if (w != XFS_DATA_FORK)
2496 				break;
2497 			error = xfs_da3_swap_lastblock(args, &dead_blkno,
2498 						      &dead_buf);
2499 			if (error)
2500 				break;
2501 		} else {
2502 			break;
2503 		}
2504 	}
2505 	xfs_trans_binval(tp, dead_buf);
2506 	return error;
2507 }
2508 
2509 static int
xfs_dabuf_map(struct xfs_inode * dp,xfs_dablk_t bno,unsigned int flags,int whichfork,struct xfs_buf_map ** mapp,int * nmaps)2510 xfs_dabuf_map(
2511 	struct xfs_inode	*dp,
2512 	xfs_dablk_t		bno,
2513 	unsigned int		flags,
2514 	int			whichfork,
2515 	struct xfs_buf_map	**mapp,
2516 	int			*nmaps)
2517 {
2518 	struct xfs_mount	*mp = dp->i_mount;
2519 	int			nfsb = xfs_dabuf_nfsb(mp, whichfork);
2520 	struct xfs_bmbt_irec	irec, *irecs = &irec;
2521 	struct xfs_buf_map	*map = *mapp;
2522 	xfs_fileoff_t		off = bno;
2523 	int			error = 0, nirecs, i;
2524 
2525 	if (nfsb > 1)
2526 		irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_NOFS);
2527 
2528 	nirecs = nfsb;
2529 	error = xfs_bmapi_read(dp, bno, nfsb, irecs, &nirecs,
2530 			xfs_bmapi_aflag(whichfork));
2531 	if (error)
2532 		goto out_free_irecs;
2533 
2534 	/*
2535 	 * Use the caller provided map for the single map case, else allocate a
2536 	 * larger one that needs to be free by the caller.
2537 	 */
2538 	if (nirecs > 1) {
2539 		map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_NOFS);
2540 		if (!map) {
2541 			error = -ENOMEM;
2542 			goto out_free_irecs;
2543 		}
2544 		*mapp = map;
2545 	}
2546 
2547 	for (i = 0; i < nirecs; i++) {
2548 		if (irecs[i].br_startblock == HOLESTARTBLOCK ||
2549 		    irecs[i].br_startblock == DELAYSTARTBLOCK)
2550 			goto invalid_mapping;
2551 		if (off != irecs[i].br_startoff)
2552 			goto invalid_mapping;
2553 
2554 		map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2555 		map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2556 		off += irecs[i].br_blockcount;
2557 	}
2558 
2559 	if (off != bno + nfsb)
2560 		goto invalid_mapping;
2561 
2562 	*nmaps = nirecs;
2563 out_free_irecs:
2564 	if (irecs != &irec)
2565 		kmem_free(irecs);
2566 	return error;
2567 
2568 invalid_mapping:
2569 	/* Caller ok with no mapping. */
2570 	if (XFS_IS_CORRUPT(mp, !(flags & XFS_DABUF_MAP_HOLE_OK))) {
2571 		error = -EFSCORRUPTED;
2572 		if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2573 			xfs_alert(mp, "%s: bno %u inode %llu",
2574 					__func__, bno, dp->i_ino);
2575 
2576 			for (i = 0; i < nirecs; i++) {
2577 				xfs_alert(mp,
2578 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2579 					i, irecs[i].br_startoff,
2580 					irecs[i].br_startblock,
2581 					irecs[i].br_blockcount,
2582 					irecs[i].br_state);
2583 			}
2584 		}
2585 	} else {
2586 		*nmaps = 0;
2587 	}
2588 	goto out_free_irecs;
2589 }
2590 
2591 /*
2592  * Get a buffer for the dir/attr block.
2593  */
2594 int
xfs_da_get_buf(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t bno,struct xfs_buf ** bpp,int whichfork)2595 xfs_da_get_buf(
2596 	struct xfs_trans	*tp,
2597 	struct xfs_inode	*dp,
2598 	xfs_dablk_t		bno,
2599 	struct xfs_buf		**bpp,
2600 	int			whichfork)
2601 {
2602 	struct xfs_mount	*mp = dp->i_mount;
2603 	struct xfs_buf		*bp;
2604 	struct xfs_buf_map	map, *mapp = &map;
2605 	int			nmap = 1;
2606 	int			error;
2607 
2608 	*bpp = NULL;
2609 	error = xfs_dabuf_map(dp, bno, 0, whichfork, &mapp, &nmap);
2610 	if (error || nmap == 0)
2611 		goto out_free;
2612 
2613 	error = xfs_trans_get_buf_map(tp, mp->m_ddev_targp, mapp, nmap, 0, &bp);
2614 	if (error)
2615 		goto out_free;
2616 
2617 	*bpp = bp;
2618 
2619 out_free:
2620 	if (mapp != &map)
2621 		kmem_free(mapp);
2622 
2623 	return error;
2624 }
2625 
2626 /*
2627  * Get a buffer for the dir/attr block, fill in the contents.
2628  */
2629 int
xfs_da_read_buf(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dablk_t bno,unsigned int flags,struct xfs_buf ** bpp,int whichfork,const struct xfs_buf_ops * ops)2630 xfs_da_read_buf(
2631 	struct xfs_trans	*tp,
2632 	struct xfs_inode	*dp,
2633 	xfs_dablk_t		bno,
2634 	unsigned int		flags,
2635 	struct xfs_buf		**bpp,
2636 	int			whichfork,
2637 	const struct xfs_buf_ops *ops)
2638 {
2639 	struct xfs_mount	*mp = dp->i_mount;
2640 	struct xfs_buf		*bp;
2641 	struct xfs_buf_map	map, *mapp = &map;
2642 	int			nmap = 1;
2643 	int			error;
2644 
2645 	*bpp = NULL;
2646 	error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
2647 	if (error || !nmap)
2648 		goto out_free;
2649 
2650 	error = xfs_trans_read_buf_map(mp, tp, mp->m_ddev_targp, mapp, nmap, 0,
2651 			&bp, ops);
2652 	if (error)
2653 		goto out_free;
2654 
2655 	if (whichfork == XFS_ATTR_FORK)
2656 		xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
2657 	else
2658 		xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2659 	*bpp = bp;
2660 out_free:
2661 	if (mapp != &map)
2662 		kmem_free(mapp);
2663 
2664 	return error;
2665 }
2666 
2667 /*
2668  * Readahead the dir/attr block.
2669  */
2670 int
xfs_da_reada_buf(struct xfs_inode * dp,xfs_dablk_t bno,unsigned int flags,int whichfork,const struct xfs_buf_ops * ops)2671 xfs_da_reada_buf(
2672 	struct xfs_inode	*dp,
2673 	xfs_dablk_t		bno,
2674 	unsigned int		flags,
2675 	int			whichfork,
2676 	const struct xfs_buf_ops *ops)
2677 {
2678 	struct xfs_buf_map	map;
2679 	struct xfs_buf_map	*mapp;
2680 	int			nmap;
2681 	int			error;
2682 
2683 	mapp = &map;
2684 	nmap = 1;
2685 	error = xfs_dabuf_map(dp, bno, flags, whichfork, &mapp, &nmap);
2686 	if (error || !nmap)
2687 		goto out_free;
2688 
2689 	xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
2690 
2691 out_free:
2692 	if (mapp != &map)
2693 		kmem_free(mapp);
2694 
2695 	return error;
2696 }
2697