xref: /openbmc/linux/fs/xfs/libxfs/xfs_attr_leaf.c (revision 750afb08)
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_sb.h"
15 #include "xfs_mount.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
18 #include "xfs_inode.h"
19 #include "xfs_trans.h"
20 #include "xfs_inode_item.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_bmap.h"
23 #include "xfs_attr_sf.h"
24 #include "xfs_attr_remote.h"
25 #include "xfs_attr.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_error.h"
28 #include "xfs_trace.h"
29 #include "xfs_buf_item.h"
30 #include "xfs_cksum.h"
31 #include "xfs_dir2.h"
32 #include "xfs_log.h"
33 
34 
35 /*
36  * xfs_attr_leaf.c
37  *
38  * Routines to implement leaf blocks of attributes as Btrees of hashed names.
39  */
40 
41 /*========================================================================
42  * Function prototypes for the kernel.
43  *========================================================================*/
44 
45 /*
46  * Routines used for growing the Btree.
47  */
48 STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
49 				 xfs_dablk_t which_block, struct xfs_buf **bpp);
50 STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
51 				   struct xfs_attr3_icleaf_hdr *ichdr,
52 				   struct xfs_da_args *args, int freemap_index);
53 STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
54 				   struct xfs_attr3_icleaf_hdr *ichdr,
55 				   struct xfs_buf *leaf_buffer);
56 STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
57 						   xfs_da_state_blk_t *blk1,
58 						   xfs_da_state_blk_t *blk2);
59 STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
60 			xfs_da_state_blk_t *leaf_blk_1,
61 			struct xfs_attr3_icleaf_hdr *ichdr1,
62 			xfs_da_state_blk_t *leaf_blk_2,
63 			struct xfs_attr3_icleaf_hdr *ichdr2,
64 			int *number_entries_in_blk1,
65 			int *number_usedbytes_in_blk1);
66 
67 /*
68  * Utility routines.
69  */
70 STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
71 			struct xfs_attr_leafblock *src_leaf,
72 			struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
73 			struct xfs_attr_leafblock *dst_leaf,
74 			struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
75 			int move_count);
76 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
77 
78 /*
79  * attr3 block 'firstused' conversion helpers.
80  *
81  * firstused refers to the offset of the first used byte of the nameval region
82  * of an attr leaf block. The region starts at the tail of the block and expands
83  * backwards towards the middle. As such, firstused is initialized to the block
84  * size for an empty leaf block and is reduced from there.
85  *
86  * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
87  * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
88  * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
89  * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
90  * the attr block size. The following helpers manage the conversion between the
91  * in-core and on-disk formats.
92  */
93 
94 static void
95 xfs_attr3_leaf_firstused_from_disk(
96 	struct xfs_da_geometry		*geo,
97 	struct xfs_attr3_icleaf_hdr	*to,
98 	struct xfs_attr_leafblock	*from)
99 {
100 	struct xfs_attr3_leaf_hdr	*hdr3;
101 
102 	if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
103 		hdr3 = (struct xfs_attr3_leaf_hdr *) from;
104 		to->firstused = be16_to_cpu(hdr3->firstused);
105 	} else {
106 		to->firstused = be16_to_cpu(from->hdr.firstused);
107 	}
108 
109 	/*
110 	 * Convert from the magic fsb size value to actual blocksize. This
111 	 * should only occur for empty blocks when the block size overflows
112 	 * 16-bits.
113 	 */
114 	if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
115 		ASSERT(!to->count && !to->usedbytes);
116 		ASSERT(geo->blksize > USHRT_MAX);
117 		to->firstused = geo->blksize;
118 	}
119 }
120 
121 static void
122 xfs_attr3_leaf_firstused_to_disk(
123 	struct xfs_da_geometry		*geo,
124 	struct xfs_attr_leafblock	*to,
125 	struct xfs_attr3_icleaf_hdr	*from)
126 {
127 	struct xfs_attr3_leaf_hdr	*hdr3;
128 	uint32_t			firstused;
129 
130 	/* magic value should only be seen on disk */
131 	ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
132 
133 	/*
134 	 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
135 	 * value. This only overflows at the max supported value of 64k. Use the
136 	 * magic on-disk value to represent block size in this case.
137 	 */
138 	firstused = from->firstused;
139 	if (firstused > USHRT_MAX) {
140 		ASSERT(from->firstused == geo->blksize);
141 		firstused = XFS_ATTR3_LEAF_NULLOFF;
142 	}
143 
144 	if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
145 		hdr3 = (struct xfs_attr3_leaf_hdr *) to;
146 		hdr3->firstused = cpu_to_be16(firstused);
147 	} else {
148 		to->hdr.firstused = cpu_to_be16(firstused);
149 	}
150 }
151 
152 void
153 xfs_attr3_leaf_hdr_from_disk(
154 	struct xfs_da_geometry		*geo,
155 	struct xfs_attr3_icleaf_hdr	*to,
156 	struct xfs_attr_leafblock	*from)
157 {
158 	int	i;
159 
160 	ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
161 	       from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
162 
163 	if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
164 		struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
165 
166 		to->forw = be32_to_cpu(hdr3->info.hdr.forw);
167 		to->back = be32_to_cpu(hdr3->info.hdr.back);
168 		to->magic = be16_to_cpu(hdr3->info.hdr.magic);
169 		to->count = be16_to_cpu(hdr3->count);
170 		to->usedbytes = be16_to_cpu(hdr3->usedbytes);
171 		xfs_attr3_leaf_firstused_from_disk(geo, to, from);
172 		to->holes = hdr3->holes;
173 
174 		for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
175 			to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
176 			to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
177 		}
178 		return;
179 	}
180 	to->forw = be32_to_cpu(from->hdr.info.forw);
181 	to->back = be32_to_cpu(from->hdr.info.back);
182 	to->magic = be16_to_cpu(from->hdr.info.magic);
183 	to->count = be16_to_cpu(from->hdr.count);
184 	to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
185 	xfs_attr3_leaf_firstused_from_disk(geo, to, from);
186 	to->holes = from->hdr.holes;
187 
188 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
189 		to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
190 		to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
191 	}
192 }
193 
194 void
195 xfs_attr3_leaf_hdr_to_disk(
196 	struct xfs_da_geometry		*geo,
197 	struct xfs_attr_leafblock	*to,
198 	struct xfs_attr3_icleaf_hdr	*from)
199 {
200 	int				i;
201 
202 	ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
203 	       from->magic == XFS_ATTR3_LEAF_MAGIC);
204 
205 	if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
206 		struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
207 
208 		hdr3->info.hdr.forw = cpu_to_be32(from->forw);
209 		hdr3->info.hdr.back = cpu_to_be32(from->back);
210 		hdr3->info.hdr.magic = cpu_to_be16(from->magic);
211 		hdr3->count = cpu_to_be16(from->count);
212 		hdr3->usedbytes = cpu_to_be16(from->usedbytes);
213 		xfs_attr3_leaf_firstused_to_disk(geo, to, from);
214 		hdr3->holes = from->holes;
215 		hdr3->pad1 = 0;
216 
217 		for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
218 			hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
219 			hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
220 		}
221 		return;
222 	}
223 	to->hdr.info.forw = cpu_to_be32(from->forw);
224 	to->hdr.info.back = cpu_to_be32(from->back);
225 	to->hdr.info.magic = cpu_to_be16(from->magic);
226 	to->hdr.count = cpu_to_be16(from->count);
227 	to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
228 	xfs_attr3_leaf_firstused_to_disk(geo, to, from);
229 	to->hdr.holes = from->holes;
230 	to->hdr.pad1 = 0;
231 
232 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
233 		to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
234 		to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
235 	}
236 }
237 
238 static xfs_failaddr_t
239 xfs_attr3_leaf_verify(
240 	struct xfs_buf			*bp)
241 {
242 	struct xfs_attr3_icleaf_hdr	ichdr;
243 	struct xfs_mount		*mp = bp->b_target->bt_mount;
244 	struct xfs_attr_leafblock	*leaf = bp->b_addr;
245 	struct xfs_attr_leaf_entry	*entries;
246 	uint32_t			end;	/* must be 32bit - see below */
247 	int				i;
248 
249 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
250 
251 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
252 		struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
253 
254 		if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
255 			return __this_address;
256 
257 		if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
258 			return __this_address;
259 		if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
260 			return __this_address;
261 		if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
262 			return __this_address;
263 	} else {
264 		if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
265 			return __this_address;
266 	}
267 	/*
268 	 * In recovery there is a transient state where count == 0 is valid
269 	 * because we may have transitioned an empty shortform attr to a leaf
270 	 * if the attr didn't fit in shortform.
271 	 */
272 	if (!xfs_log_in_recovery(mp) && ichdr.count == 0)
273 		return __this_address;
274 
275 	/*
276 	 * firstused is the block offset of the first name info structure.
277 	 * Make sure it doesn't go off the block or crash into the header.
278 	 */
279 	if (ichdr.firstused > mp->m_attr_geo->blksize)
280 		return __this_address;
281 	if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
282 		return __this_address;
283 
284 	/* Make sure the entries array doesn't crash into the name info. */
285 	entries = xfs_attr3_leaf_entryp(bp->b_addr);
286 	if ((char *)&entries[ichdr.count] >
287 	    (char *)bp->b_addr + ichdr.firstused)
288 		return __this_address;
289 
290 	/* XXX: need to range check rest of attr header values */
291 	/* XXX: hash order check? */
292 
293 	/*
294 	 * Quickly check the freemap information.  Attribute data has to be
295 	 * aligned to 4-byte boundaries, and likewise for the free space.
296 	 *
297 	 * Note that for 64k block size filesystems, the freemap entries cannot
298 	 * overflow as they are only be16 fields. However, when checking end
299 	 * pointer of the freemap, we have to be careful to detect overflows and
300 	 * so use uint32_t for those checks.
301 	 */
302 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
303 		if (ichdr.freemap[i].base > mp->m_attr_geo->blksize)
304 			return __this_address;
305 		if (ichdr.freemap[i].base & 0x3)
306 			return __this_address;
307 		if (ichdr.freemap[i].size > mp->m_attr_geo->blksize)
308 			return __this_address;
309 		if (ichdr.freemap[i].size & 0x3)
310 			return __this_address;
311 
312 		/* be care of 16 bit overflows here */
313 		end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size;
314 		if (end < ichdr.freemap[i].base)
315 			return __this_address;
316 		if (end > mp->m_attr_geo->blksize)
317 			return __this_address;
318 	}
319 
320 	return NULL;
321 }
322 
323 static void
324 xfs_attr3_leaf_write_verify(
325 	struct xfs_buf	*bp)
326 {
327 	struct xfs_mount	*mp = bp->b_target->bt_mount;
328 	struct xfs_buf_log_item	*bip = bp->b_log_item;
329 	struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
330 	xfs_failaddr_t		fa;
331 
332 	fa = xfs_attr3_leaf_verify(bp);
333 	if (fa) {
334 		xfs_verifier_error(bp, -EFSCORRUPTED, fa);
335 		return;
336 	}
337 
338 	if (!xfs_sb_version_hascrc(&mp->m_sb))
339 		return;
340 
341 	if (bip)
342 		hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
343 
344 	xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
345 }
346 
347 /*
348  * leaf/node format detection on trees is sketchy, so a node read can be done on
349  * leaf level blocks when detection identifies the tree as a node format tree
350  * incorrectly. In this case, we need to swap the verifier to match the correct
351  * format of the block being read.
352  */
353 static void
354 xfs_attr3_leaf_read_verify(
355 	struct xfs_buf		*bp)
356 {
357 	struct xfs_mount	*mp = bp->b_target->bt_mount;
358 	xfs_failaddr_t		fa;
359 
360 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
361 	     !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
362 		xfs_verifier_error(bp, -EFSBADCRC, __this_address);
363 	else {
364 		fa = xfs_attr3_leaf_verify(bp);
365 		if (fa)
366 			xfs_verifier_error(bp, -EFSCORRUPTED, fa);
367 	}
368 }
369 
370 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
371 	.name = "xfs_attr3_leaf",
372 	.verify_read = xfs_attr3_leaf_read_verify,
373 	.verify_write = xfs_attr3_leaf_write_verify,
374 	.verify_struct = xfs_attr3_leaf_verify,
375 };
376 
377 int
378 xfs_attr3_leaf_read(
379 	struct xfs_trans	*tp,
380 	struct xfs_inode	*dp,
381 	xfs_dablk_t		bno,
382 	xfs_daddr_t		mappedbno,
383 	struct xfs_buf		**bpp)
384 {
385 	int			err;
386 
387 	err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
388 				XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
389 	if (!err && tp && *bpp)
390 		xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
391 	return err;
392 }
393 
394 /*========================================================================
395  * Namespace helper routines
396  *========================================================================*/
397 
398 /*
399  * If namespace bits don't match return 0.
400  * If all match then return 1.
401  */
402 STATIC int
403 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
404 {
405 	return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
406 }
407 
408 
409 /*========================================================================
410  * External routines when attribute fork size < XFS_LITINO(mp).
411  *========================================================================*/
412 
413 /*
414  * Query whether the requested number of additional bytes of extended
415  * attribute space will be able to fit inline.
416  *
417  * Returns zero if not, else the di_forkoff fork offset to be used in the
418  * literal area for attribute data once the new bytes have been added.
419  *
420  * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
421  * special case for dev/uuid inodes, they have fixed size data forks.
422  */
423 int
424 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
425 {
426 	int offset;
427 	int minforkoff;	/* lower limit on valid forkoff locations */
428 	int maxforkoff;	/* upper limit on valid forkoff locations */
429 	int dsize;
430 	xfs_mount_t *mp = dp->i_mount;
431 
432 	/* rounded down */
433 	offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
434 
435 	if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) {
436 		minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
437 		return (offset >= minforkoff) ? minforkoff : 0;
438 	}
439 
440 	/*
441 	 * If the requested numbers of bytes is smaller or equal to the
442 	 * current attribute fork size we can always proceed.
443 	 *
444 	 * Note that if_bytes in the data fork might actually be larger than
445 	 * the current data fork size is due to delalloc extents. In that
446 	 * case either the extent count will go down when they are converted
447 	 * to real extents, or the delalloc conversion will take care of the
448 	 * literal area rebalancing.
449 	 */
450 	if (bytes <= XFS_IFORK_ASIZE(dp))
451 		return dp->i_d.di_forkoff;
452 
453 	/*
454 	 * For attr2 we can try to move the forkoff if there is space in the
455 	 * literal area, but for the old format we are done if there is no
456 	 * space in the fixed attribute fork.
457 	 */
458 	if (!(mp->m_flags & XFS_MOUNT_ATTR2))
459 		return 0;
460 
461 	dsize = dp->i_df.if_bytes;
462 
463 	switch (dp->i_d.di_format) {
464 	case XFS_DINODE_FMT_EXTENTS:
465 		/*
466 		 * If there is no attr fork and the data fork is extents,
467 		 * determine if creating the default attr fork will result
468 		 * in the extents form migrating to btree. If so, the
469 		 * minimum offset only needs to be the space required for
470 		 * the btree root.
471 		 */
472 		if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
473 		    xfs_default_attroffset(dp))
474 			dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
475 		break;
476 	case XFS_DINODE_FMT_BTREE:
477 		/*
478 		 * If we have a data btree then keep forkoff if we have one,
479 		 * otherwise we are adding a new attr, so then we set
480 		 * minforkoff to where the btree root can finish so we have
481 		 * plenty of room for attrs
482 		 */
483 		if (dp->i_d.di_forkoff) {
484 			if (offset < dp->i_d.di_forkoff)
485 				return 0;
486 			return dp->i_d.di_forkoff;
487 		}
488 		dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
489 		break;
490 	}
491 
492 	/*
493 	 * A data fork btree root must have space for at least
494 	 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
495 	 */
496 	minforkoff = max(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
497 	minforkoff = roundup(minforkoff, 8) >> 3;
498 
499 	/* attr fork btree root can have at least this many key/ptr pairs */
500 	maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
501 			XFS_BMDR_SPACE_CALC(MINABTPTRS);
502 	maxforkoff = maxforkoff >> 3;	/* rounded down */
503 
504 	if (offset >= maxforkoff)
505 		return maxforkoff;
506 	if (offset >= minforkoff)
507 		return offset;
508 	return 0;
509 }
510 
511 /*
512  * Switch on the ATTR2 superblock bit (implies also FEATURES2)
513  */
514 STATIC void
515 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
516 {
517 	if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
518 	    !(xfs_sb_version_hasattr2(&mp->m_sb))) {
519 		spin_lock(&mp->m_sb_lock);
520 		if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
521 			xfs_sb_version_addattr2(&mp->m_sb);
522 			spin_unlock(&mp->m_sb_lock);
523 			xfs_log_sb(tp);
524 		} else
525 			spin_unlock(&mp->m_sb_lock);
526 	}
527 }
528 
529 /*
530  * Create the initial contents of a shortform attribute list.
531  */
532 void
533 xfs_attr_shortform_create(xfs_da_args_t *args)
534 {
535 	xfs_attr_sf_hdr_t *hdr;
536 	xfs_inode_t *dp;
537 	struct xfs_ifork *ifp;
538 
539 	trace_xfs_attr_sf_create(args);
540 
541 	dp = args->dp;
542 	ASSERT(dp != NULL);
543 	ifp = dp->i_afp;
544 	ASSERT(ifp != NULL);
545 	ASSERT(ifp->if_bytes == 0);
546 	if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
547 		ifp->if_flags &= ~XFS_IFEXTENTS;	/* just in case */
548 		dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
549 		ifp->if_flags |= XFS_IFINLINE;
550 	} else {
551 		ASSERT(ifp->if_flags & XFS_IFINLINE);
552 	}
553 	xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
554 	hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
555 	hdr->count = 0;
556 	hdr->totsize = cpu_to_be16(sizeof(*hdr));
557 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
558 }
559 
560 /*
561  * Add a name/value pair to the shortform attribute list.
562  * Overflow from the inode has already been checked for.
563  */
564 void
565 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
566 {
567 	xfs_attr_shortform_t *sf;
568 	xfs_attr_sf_entry_t *sfe;
569 	int i, offset, size;
570 	xfs_mount_t *mp;
571 	xfs_inode_t *dp;
572 	struct xfs_ifork *ifp;
573 
574 	trace_xfs_attr_sf_add(args);
575 
576 	dp = args->dp;
577 	mp = dp->i_mount;
578 	dp->i_d.di_forkoff = forkoff;
579 
580 	ifp = dp->i_afp;
581 	ASSERT(ifp->if_flags & XFS_IFINLINE);
582 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
583 	sfe = &sf->list[0];
584 	for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
585 #ifdef DEBUG
586 		if (sfe->namelen != args->namelen)
587 			continue;
588 		if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
589 			continue;
590 		if (!xfs_attr_namesp_match(args->flags, sfe->flags))
591 			continue;
592 		ASSERT(0);
593 #endif
594 	}
595 
596 	offset = (char *)sfe - (char *)sf;
597 	size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
598 	xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
599 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
600 	sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
601 
602 	sfe->namelen = args->namelen;
603 	sfe->valuelen = args->valuelen;
604 	sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
605 	memcpy(sfe->nameval, args->name, args->namelen);
606 	memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
607 	sf->hdr.count++;
608 	be16_add_cpu(&sf->hdr.totsize, size);
609 	xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
610 
611 	xfs_sbversion_add_attr2(mp, args->trans);
612 }
613 
614 /*
615  * After the last attribute is removed revert to original inode format,
616  * making all literal area available to the data fork once more.
617  */
618 void
619 xfs_attr_fork_remove(
620 	struct xfs_inode	*ip,
621 	struct xfs_trans	*tp)
622 {
623 	xfs_idestroy_fork(ip, XFS_ATTR_FORK);
624 	ip->i_d.di_forkoff = 0;
625 	ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
626 
627 	ASSERT(ip->i_d.di_anextents == 0);
628 	ASSERT(ip->i_afp == NULL);
629 
630 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
631 }
632 
633 /*
634  * Remove an attribute from the shortform attribute list structure.
635  */
636 int
637 xfs_attr_shortform_remove(xfs_da_args_t *args)
638 {
639 	xfs_attr_shortform_t *sf;
640 	xfs_attr_sf_entry_t *sfe;
641 	int base, size=0, end, totsize, i;
642 	xfs_mount_t *mp;
643 	xfs_inode_t *dp;
644 
645 	trace_xfs_attr_sf_remove(args);
646 
647 	dp = args->dp;
648 	mp = dp->i_mount;
649 	base = sizeof(xfs_attr_sf_hdr_t);
650 	sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
651 	sfe = &sf->list[0];
652 	end = sf->hdr.count;
653 	for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
654 					base += size, i++) {
655 		size = XFS_ATTR_SF_ENTSIZE(sfe);
656 		if (sfe->namelen != args->namelen)
657 			continue;
658 		if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
659 			continue;
660 		if (!xfs_attr_namesp_match(args->flags, sfe->flags))
661 			continue;
662 		break;
663 	}
664 	if (i == end)
665 		return -ENOATTR;
666 
667 	/*
668 	 * Fix up the attribute fork data, covering the hole
669 	 */
670 	end = base + size;
671 	totsize = be16_to_cpu(sf->hdr.totsize);
672 	if (end != totsize)
673 		memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
674 	sf->hdr.count--;
675 	be16_add_cpu(&sf->hdr.totsize, -size);
676 
677 	/*
678 	 * Fix up the start offset of the attribute fork
679 	 */
680 	totsize -= size;
681 	if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
682 	    (mp->m_flags & XFS_MOUNT_ATTR2) &&
683 	    (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
684 	    !(args->op_flags & XFS_DA_OP_ADDNAME)) {
685 		xfs_attr_fork_remove(dp, args->trans);
686 	} else {
687 		xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
688 		dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
689 		ASSERT(dp->i_d.di_forkoff);
690 		ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
691 				(args->op_flags & XFS_DA_OP_ADDNAME) ||
692 				!(mp->m_flags & XFS_MOUNT_ATTR2) ||
693 				dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
694 		xfs_trans_log_inode(args->trans, dp,
695 					XFS_ILOG_CORE | XFS_ILOG_ADATA);
696 	}
697 
698 	xfs_sbversion_add_attr2(mp, args->trans);
699 
700 	return 0;
701 }
702 
703 /*
704  * Look up a name in a shortform attribute list structure.
705  */
706 /*ARGSUSED*/
707 int
708 xfs_attr_shortform_lookup(xfs_da_args_t *args)
709 {
710 	xfs_attr_shortform_t *sf;
711 	xfs_attr_sf_entry_t *sfe;
712 	int i;
713 	struct xfs_ifork *ifp;
714 
715 	trace_xfs_attr_sf_lookup(args);
716 
717 	ifp = args->dp->i_afp;
718 	ASSERT(ifp->if_flags & XFS_IFINLINE);
719 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
720 	sfe = &sf->list[0];
721 	for (i = 0; i < sf->hdr.count;
722 				sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
723 		if (sfe->namelen != args->namelen)
724 			continue;
725 		if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
726 			continue;
727 		if (!xfs_attr_namesp_match(args->flags, sfe->flags))
728 			continue;
729 		return -EEXIST;
730 	}
731 	return -ENOATTR;
732 }
733 
734 /*
735  * Look up a name in a shortform attribute list structure.
736  */
737 /*ARGSUSED*/
738 int
739 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
740 {
741 	xfs_attr_shortform_t *sf;
742 	xfs_attr_sf_entry_t *sfe;
743 	int i;
744 
745 	ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
746 	sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
747 	sfe = &sf->list[0];
748 	for (i = 0; i < sf->hdr.count;
749 				sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
750 		if (sfe->namelen != args->namelen)
751 			continue;
752 		if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
753 			continue;
754 		if (!xfs_attr_namesp_match(args->flags, sfe->flags))
755 			continue;
756 		if (args->flags & ATTR_KERNOVAL) {
757 			args->valuelen = sfe->valuelen;
758 			return -EEXIST;
759 		}
760 		if (args->valuelen < sfe->valuelen) {
761 			args->valuelen = sfe->valuelen;
762 			return -ERANGE;
763 		}
764 		args->valuelen = sfe->valuelen;
765 		memcpy(args->value, &sfe->nameval[args->namelen],
766 						    args->valuelen);
767 		return -EEXIST;
768 	}
769 	return -ENOATTR;
770 }
771 
772 /*
773  * Convert from using the shortform to the leaf.  On success, return the
774  * buffer so that we can keep it locked until we're totally done with it.
775  */
776 int
777 xfs_attr_shortform_to_leaf(
778 	struct xfs_da_args		*args,
779 	struct xfs_buf			**leaf_bp)
780 {
781 	struct xfs_inode		*dp;
782 	struct xfs_attr_shortform	*sf;
783 	struct xfs_attr_sf_entry	*sfe;
784 	struct xfs_da_args		nargs;
785 	char				*tmpbuffer;
786 	int				error, i, size;
787 	xfs_dablk_t			blkno;
788 	struct xfs_buf			*bp;
789 	struct xfs_ifork		*ifp;
790 
791 	trace_xfs_attr_sf_to_leaf(args);
792 
793 	dp = args->dp;
794 	ifp = dp->i_afp;
795 	sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
796 	size = be16_to_cpu(sf->hdr.totsize);
797 	tmpbuffer = kmem_alloc(size, KM_SLEEP);
798 	ASSERT(tmpbuffer != NULL);
799 	memcpy(tmpbuffer, ifp->if_u1.if_data, size);
800 	sf = (xfs_attr_shortform_t *)tmpbuffer;
801 
802 	xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
803 	xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
804 
805 	bp = NULL;
806 	error = xfs_da_grow_inode(args, &blkno);
807 	if (error) {
808 		/*
809 		 * If we hit an IO error middle of the transaction inside
810 		 * grow_inode(), we may have inconsistent data. Bail out.
811 		 */
812 		if (error == -EIO)
813 			goto out;
814 		xfs_idata_realloc(dp, size, XFS_ATTR_FORK);	/* try to put */
815 		memcpy(ifp->if_u1.if_data, tmpbuffer, size);	/* it back */
816 		goto out;
817 	}
818 
819 	ASSERT(blkno == 0);
820 	error = xfs_attr3_leaf_create(args, blkno, &bp);
821 	if (error) {
822 		/* xfs_attr3_leaf_create may not have instantiated a block */
823 		if (bp && (xfs_da_shrink_inode(args, 0, bp) != 0))
824 			goto out;
825 		xfs_idata_realloc(dp, size, XFS_ATTR_FORK);	/* try to put */
826 		memcpy(ifp->if_u1.if_data, tmpbuffer, size);	/* it back */
827 		goto out;
828 	}
829 
830 	memset((char *)&nargs, 0, sizeof(nargs));
831 	nargs.dp = dp;
832 	nargs.geo = args->geo;
833 	nargs.total = args->total;
834 	nargs.whichfork = XFS_ATTR_FORK;
835 	nargs.trans = args->trans;
836 	nargs.op_flags = XFS_DA_OP_OKNOENT;
837 
838 	sfe = &sf->list[0];
839 	for (i = 0; i < sf->hdr.count; i++) {
840 		nargs.name = sfe->nameval;
841 		nargs.namelen = sfe->namelen;
842 		nargs.value = &sfe->nameval[nargs.namelen];
843 		nargs.valuelen = sfe->valuelen;
844 		nargs.hashval = xfs_da_hashname(sfe->nameval,
845 						sfe->namelen);
846 		nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
847 		error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
848 		ASSERT(error == -ENOATTR);
849 		error = xfs_attr3_leaf_add(bp, &nargs);
850 		ASSERT(error != -ENOSPC);
851 		if (error)
852 			goto out;
853 		sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
854 	}
855 	error = 0;
856 	*leaf_bp = bp;
857 out:
858 	kmem_free(tmpbuffer);
859 	return error;
860 }
861 
862 /*
863  * Check a leaf attribute block to see if all the entries would fit into
864  * a shortform attribute list.
865  */
866 int
867 xfs_attr_shortform_allfit(
868 	struct xfs_buf		*bp,
869 	struct xfs_inode	*dp)
870 {
871 	struct xfs_attr_leafblock *leaf;
872 	struct xfs_attr_leaf_entry *entry;
873 	xfs_attr_leaf_name_local_t *name_loc;
874 	struct xfs_attr3_icleaf_hdr leafhdr;
875 	int			bytes;
876 	int			i;
877 	struct xfs_mount	*mp = bp->b_target->bt_mount;
878 
879 	leaf = bp->b_addr;
880 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
881 	entry = xfs_attr3_leaf_entryp(leaf);
882 
883 	bytes = sizeof(struct xfs_attr_sf_hdr);
884 	for (i = 0; i < leafhdr.count; entry++, i++) {
885 		if (entry->flags & XFS_ATTR_INCOMPLETE)
886 			continue;		/* don't copy partial entries */
887 		if (!(entry->flags & XFS_ATTR_LOCAL))
888 			return 0;
889 		name_loc = xfs_attr3_leaf_name_local(leaf, i);
890 		if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
891 			return 0;
892 		if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
893 			return 0;
894 		bytes += sizeof(struct xfs_attr_sf_entry) - 1
895 				+ name_loc->namelen
896 				+ be16_to_cpu(name_loc->valuelen);
897 	}
898 	if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
899 	    (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
900 	    (bytes == sizeof(struct xfs_attr_sf_hdr)))
901 		return -1;
902 	return xfs_attr_shortform_bytesfit(dp, bytes);
903 }
904 
905 /* Verify the consistency of an inline attribute fork. */
906 xfs_failaddr_t
907 xfs_attr_shortform_verify(
908 	struct xfs_inode		*ip)
909 {
910 	struct xfs_attr_shortform	*sfp;
911 	struct xfs_attr_sf_entry	*sfep;
912 	struct xfs_attr_sf_entry	*next_sfep;
913 	char				*endp;
914 	struct xfs_ifork		*ifp;
915 	int				i;
916 	int				size;
917 
918 	ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
919 	ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
920 	sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
921 	size = ifp->if_bytes;
922 
923 	/*
924 	 * Give up if the attribute is way too short.
925 	 */
926 	if (size < sizeof(struct xfs_attr_sf_hdr))
927 		return __this_address;
928 
929 	endp = (char *)sfp + size;
930 
931 	/* Check all reported entries */
932 	sfep = &sfp->list[0];
933 	for (i = 0; i < sfp->hdr.count; i++) {
934 		/*
935 		 * struct xfs_attr_sf_entry has a variable length.
936 		 * Check the fixed-offset parts of the structure are
937 		 * within the data buffer.
938 		 */
939 		if (((char *)sfep + sizeof(*sfep)) >= endp)
940 			return __this_address;
941 
942 		/* Don't allow names with known bad length. */
943 		if (sfep->namelen == 0)
944 			return __this_address;
945 
946 		/*
947 		 * Check that the variable-length part of the structure is
948 		 * within the data buffer.  The next entry starts after the
949 		 * name component, so nextentry is an acceptable test.
950 		 */
951 		next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
952 		if ((char *)next_sfep > endp)
953 			return __this_address;
954 
955 		/*
956 		 * Check for unknown flags.  Short form doesn't support
957 		 * the incomplete or local bits, so we can use the namespace
958 		 * mask here.
959 		 */
960 		if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
961 			return __this_address;
962 
963 		/*
964 		 * Check for invalid namespace combinations.  We only allow
965 		 * one namespace flag per xattr, so we can just count the
966 		 * bits (i.e. hweight) here.
967 		 */
968 		if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
969 			return __this_address;
970 
971 		sfep = next_sfep;
972 	}
973 	if ((void *)sfep != (void *)endp)
974 		return __this_address;
975 
976 	return NULL;
977 }
978 
979 /*
980  * Convert a leaf attribute list to shortform attribute list
981  */
982 int
983 xfs_attr3_leaf_to_shortform(
984 	struct xfs_buf		*bp,
985 	struct xfs_da_args	*args,
986 	int			forkoff)
987 {
988 	struct xfs_attr_leafblock *leaf;
989 	struct xfs_attr3_icleaf_hdr ichdr;
990 	struct xfs_attr_leaf_entry *entry;
991 	struct xfs_attr_leaf_name_local *name_loc;
992 	struct xfs_da_args	nargs;
993 	struct xfs_inode	*dp = args->dp;
994 	char			*tmpbuffer;
995 	int			error;
996 	int			i;
997 
998 	trace_xfs_attr_leaf_to_sf(args);
999 
1000 	tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
1001 	if (!tmpbuffer)
1002 		return -ENOMEM;
1003 
1004 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1005 
1006 	leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1007 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1008 	entry = xfs_attr3_leaf_entryp(leaf);
1009 
1010 	/* XXX (dgc): buffer is about to be marked stale - why zero it? */
1011 	memset(bp->b_addr, 0, args->geo->blksize);
1012 
1013 	/*
1014 	 * Clean out the prior contents of the attribute list.
1015 	 */
1016 	error = xfs_da_shrink_inode(args, 0, bp);
1017 	if (error)
1018 		goto out;
1019 
1020 	if (forkoff == -1) {
1021 		ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
1022 		ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
1023 		xfs_attr_fork_remove(dp, args->trans);
1024 		goto out;
1025 	}
1026 
1027 	xfs_attr_shortform_create(args);
1028 
1029 	/*
1030 	 * Copy the attributes
1031 	 */
1032 	memset((char *)&nargs, 0, sizeof(nargs));
1033 	nargs.geo = args->geo;
1034 	nargs.dp = dp;
1035 	nargs.total = args->total;
1036 	nargs.whichfork = XFS_ATTR_FORK;
1037 	nargs.trans = args->trans;
1038 	nargs.op_flags = XFS_DA_OP_OKNOENT;
1039 
1040 	for (i = 0; i < ichdr.count; entry++, i++) {
1041 		if (entry->flags & XFS_ATTR_INCOMPLETE)
1042 			continue;	/* don't copy partial entries */
1043 		if (!entry->nameidx)
1044 			continue;
1045 		ASSERT(entry->flags & XFS_ATTR_LOCAL);
1046 		name_loc = xfs_attr3_leaf_name_local(leaf, i);
1047 		nargs.name = name_loc->nameval;
1048 		nargs.namelen = name_loc->namelen;
1049 		nargs.value = &name_loc->nameval[nargs.namelen];
1050 		nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1051 		nargs.hashval = be32_to_cpu(entry->hashval);
1052 		nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
1053 		xfs_attr_shortform_add(&nargs, forkoff);
1054 	}
1055 	error = 0;
1056 
1057 out:
1058 	kmem_free(tmpbuffer);
1059 	return error;
1060 }
1061 
1062 /*
1063  * Convert from using a single leaf to a root node and a leaf.
1064  */
1065 int
1066 xfs_attr3_leaf_to_node(
1067 	struct xfs_da_args	*args)
1068 {
1069 	struct xfs_attr_leafblock *leaf;
1070 	struct xfs_attr3_icleaf_hdr icleafhdr;
1071 	struct xfs_attr_leaf_entry *entries;
1072 	struct xfs_da_node_entry *btree;
1073 	struct xfs_da3_icnode_hdr icnodehdr;
1074 	struct xfs_da_intnode	*node;
1075 	struct xfs_inode	*dp = args->dp;
1076 	struct xfs_mount	*mp = dp->i_mount;
1077 	struct xfs_buf		*bp1 = NULL;
1078 	struct xfs_buf		*bp2 = NULL;
1079 	xfs_dablk_t		blkno;
1080 	int			error;
1081 
1082 	trace_xfs_attr_leaf_to_node(args);
1083 
1084 	error = xfs_da_grow_inode(args, &blkno);
1085 	if (error)
1086 		goto out;
1087 	error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
1088 	if (error)
1089 		goto out;
1090 
1091 	error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
1092 	if (error)
1093 		goto out;
1094 
1095 	/* copy leaf to new buffer, update identifiers */
1096 	xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
1097 	bp2->b_ops = bp1->b_ops;
1098 	memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
1099 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
1100 		struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1101 		hdr3->blkno = cpu_to_be64(bp2->b_bn);
1102 	}
1103 	xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
1104 
1105 	/*
1106 	 * Set up the new root node.
1107 	 */
1108 	error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
1109 	if (error)
1110 		goto out;
1111 	node = bp1->b_addr;
1112 	dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
1113 	btree = dp->d_ops->node_tree_p(node);
1114 
1115 	leaf = bp2->b_addr;
1116 	xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
1117 	entries = xfs_attr3_leaf_entryp(leaf);
1118 
1119 	/* both on-disk, don't endian-flip twice */
1120 	btree[0].hashval = entries[icleafhdr.count - 1].hashval;
1121 	btree[0].before = cpu_to_be32(blkno);
1122 	icnodehdr.count = 1;
1123 	dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
1124 	xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
1125 	error = 0;
1126 out:
1127 	return error;
1128 }
1129 
1130 /*========================================================================
1131  * Routines used for growing the Btree.
1132  *========================================================================*/
1133 
1134 /*
1135  * Create the initial contents of a leaf attribute list
1136  * or a leaf in a node attribute list.
1137  */
1138 STATIC int
1139 xfs_attr3_leaf_create(
1140 	struct xfs_da_args	*args,
1141 	xfs_dablk_t		blkno,
1142 	struct xfs_buf		**bpp)
1143 {
1144 	struct xfs_attr_leafblock *leaf;
1145 	struct xfs_attr3_icleaf_hdr ichdr;
1146 	struct xfs_inode	*dp = args->dp;
1147 	struct xfs_mount	*mp = dp->i_mount;
1148 	struct xfs_buf		*bp;
1149 	int			error;
1150 
1151 	trace_xfs_attr_leaf_create(args);
1152 
1153 	error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1154 					    XFS_ATTR_FORK);
1155 	if (error)
1156 		return error;
1157 	bp->b_ops = &xfs_attr3_leaf_buf_ops;
1158 	xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
1159 	leaf = bp->b_addr;
1160 	memset(leaf, 0, args->geo->blksize);
1161 
1162 	memset(&ichdr, 0, sizeof(ichdr));
1163 	ichdr.firstused = args->geo->blksize;
1164 
1165 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
1166 		struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1167 
1168 		ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1169 
1170 		hdr3->blkno = cpu_to_be64(bp->b_bn);
1171 		hdr3->owner = cpu_to_be64(dp->i_ino);
1172 		uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
1173 
1174 		ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1175 	} else {
1176 		ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1177 		ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
1178 	}
1179 	ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
1180 
1181 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1182 	xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
1183 
1184 	*bpp = bp;
1185 	return 0;
1186 }
1187 
1188 /*
1189  * Split the leaf node, rebalance, then add the new entry.
1190  */
1191 int
1192 xfs_attr3_leaf_split(
1193 	struct xfs_da_state	*state,
1194 	struct xfs_da_state_blk	*oldblk,
1195 	struct xfs_da_state_blk	*newblk)
1196 {
1197 	xfs_dablk_t blkno;
1198 	int error;
1199 
1200 	trace_xfs_attr_leaf_split(state->args);
1201 
1202 	/*
1203 	 * Allocate space for a new leaf node.
1204 	 */
1205 	ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1206 	error = xfs_da_grow_inode(state->args, &blkno);
1207 	if (error)
1208 		return error;
1209 	error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
1210 	if (error)
1211 		return error;
1212 	newblk->blkno = blkno;
1213 	newblk->magic = XFS_ATTR_LEAF_MAGIC;
1214 
1215 	/*
1216 	 * Rebalance the entries across the two leaves.
1217 	 * NOTE: rebalance() currently depends on the 2nd block being empty.
1218 	 */
1219 	xfs_attr3_leaf_rebalance(state, oldblk, newblk);
1220 	error = xfs_da3_blk_link(state, oldblk, newblk);
1221 	if (error)
1222 		return error;
1223 
1224 	/*
1225 	 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1226 	 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1227 	 * "new" attrs info.  Will need the "old" info to remove it later.
1228 	 *
1229 	 * Insert the "new" entry in the correct block.
1230 	 */
1231 	if (state->inleaf) {
1232 		trace_xfs_attr_leaf_add_old(state->args);
1233 		error = xfs_attr3_leaf_add(oldblk->bp, state->args);
1234 	} else {
1235 		trace_xfs_attr_leaf_add_new(state->args);
1236 		error = xfs_attr3_leaf_add(newblk->bp, state->args);
1237 	}
1238 
1239 	/*
1240 	 * Update last hashval in each block since we added the name.
1241 	 */
1242 	oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1243 	newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1244 	return error;
1245 }
1246 
1247 /*
1248  * Add a name to the leaf attribute list structure.
1249  */
1250 int
1251 xfs_attr3_leaf_add(
1252 	struct xfs_buf		*bp,
1253 	struct xfs_da_args	*args)
1254 {
1255 	struct xfs_attr_leafblock *leaf;
1256 	struct xfs_attr3_icleaf_hdr ichdr;
1257 	int			tablesize;
1258 	int			entsize;
1259 	int			sum;
1260 	int			tmp;
1261 	int			i;
1262 
1263 	trace_xfs_attr_leaf_add(args);
1264 
1265 	leaf = bp->b_addr;
1266 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1267 	ASSERT(args->index >= 0 && args->index <= ichdr.count);
1268 	entsize = xfs_attr_leaf_newentsize(args, NULL);
1269 
1270 	/*
1271 	 * Search through freemap for first-fit on new name length.
1272 	 * (may need to figure in size of entry struct too)
1273 	 */
1274 	tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1275 					+ xfs_attr3_leaf_hdr_size(leaf);
1276 	for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1277 		if (tablesize > ichdr.firstused) {
1278 			sum += ichdr.freemap[i].size;
1279 			continue;
1280 		}
1281 		if (!ichdr.freemap[i].size)
1282 			continue;	/* no space in this map */
1283 		tmp = entsize;
1284 		if (ichdr.freemap[i].base < ichdr.firstused)
1285 			tmp += sizeof(xfs_attr_leaf_entry_t);
1286 		if (ichdr.freemap[i].size >= tmp) {
1287 			tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1288 			goto out_log_hdr;
1289 		}
1290 		sum += ichdr.freemap[i].size;
1291 	}
1292 
1293 	/*
1294 	 * If there are no holes in the address space of the block,
1295 	 * and we don't have enough freespace, then compaction will do us
1296 	 * no good and we should just give up.
1297 	 */
1298 	if (!ichdr.holes && sum < entsize)
1299 		return -ENOSPC;
1300 
1301 	/*
1302 	 * Compact the entries to coalesce free space.
1303 	 * This may change the hdr->count via dropping INCOMPLETE entries.
1304 	 */
1305 	xfs_attr3_leaf_compact(args, &ichdr, bp);
1306 
1307 	/*
1308 	 * After compaction, the block is guaranteed to have only one
1309 	 * free region, in freemap[0].  If it is not big enough, give up.
1310 	 */
1311 	if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1312 		tmp = -ENOSPC;
1313 		goto out_log_hdr;
1314 	}
1315 
1316 	tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1317 
1318 out_log_hdr:
1319 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1320 	xfs_trans_log_buf(args->trans, bp,
1321 		XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1322 				xfs_attr3_leaf_hdr_size(leaf)));
1323 	return tmp;
1324 }
1325 
1326 /*
1327  * Add a name to a leaf attribute list structure.
1328  */
1329 STATIC int
1330 xfs_attr3_leaf_add_work(
1331 	struct xfs_buf		*bp,
1332 	struct xfs_attr3_icleaf_hdr *ichdr,
1333 	struct xfs_da_args	*args,
1334 	int			mapindex)
1335 {
1336 	struct xfs_attr_leafblock *leaf;
1337 	struct xfs_attr_leaf_entry *entry;
1338 	struct xfs_attr_leaf_name_local *name_loc;
1339 	struct xfs_attr_leaf_name_remote *name_rmt;
1340 	struct xfs_mount	*mp;
1341 	int			tmp;
1342 	int			i;
1343 
1344 	trace_xfs_attr_leaf_add_work(args);
1345 
1346 	leaf = bp->b_addr;
1347 	ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1348 	ASSERT(args->index >= 0 && args->index <= ichdr->count);
1349 
1350 	/*
1351 	 * Force open some space in the entry array and fill it in.
1352 	 */
1353 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1354 	if (args->index < ichdr->count) {
1355 		tmp  = ichdr->count - args->index;
1356 		tmp *= sizeof(xfs_attr_leaf_entry_t);
1357 		memmove(entry + 1, entry, tmp);
1358 		xfs_trans_log_buf(args->trans, bp,
1359 		    XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1360 	}
1361 	ichdr->count++;
1362 
1363 	/*
1364 	 * Allocate space for the new string (at the end of the run).
1365 	 */
1366 	mp = args->trans->t_mountp;
1367 	ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
1368 	ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1369 	ASSERT(ichdr->freemap[mapindex].size >=
1370 		xfs_attr_leaf_newentsize(args, NULL));
1371 	ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
1372 	ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1373 
1374 	ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
1375 
1376 	entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1377 				     ichdr->freemap[mapindex].size);
1378 	entry->hashval = cpu_to_be32(args->hashval);
1379 	entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1380 	entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1381 	if (args->op_flags & XFS_DA_OP_RENAME) {
1382 		entry->flags |= XFS_ATTR_INCOMPLETE;
1383 		if ((args->blkno2 == args->blkno) &&
1384 		    (args->index2 <= args->index)) {
1385 			args->index2++;
1386 		}
1387 	}
1388 	xfs_trans_log_buf(args->trans, bp,
1389 			  XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1390 	ASSERT((args->index == 0) ||
1391 	       (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1392 	ASSERT((args->index == ichdr->count - 1) ||
1393 	       (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1394 
1395 	/*
1396 	 * For "remote" attribute values, simply note that we need to
1397 	 * allocate space for the "remote" value.  We can't actually
1398 	 * allocate the extents in this transaction, and we can't decide
1399 	 * which blocks they should be as we might allocate more blocks
1400 	 * as part of this transaction (a split operation for example).
1401 	 */
1402 	if (entry->flags & XFS_ATTR_LOCAL) {
1403 		name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
1404 		name_loc->namelen = args->namelen;
1405 		name_loc->valuelen = cpu_to_be16(args->valuelen);
1406 		memcpy((char *)name_loc->nameval, args->name, args->namelen);
1407 		memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1408 				   be16_to_cpu(name_loc->valuelen));
1409 	} else {
1410 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
1411 		name_rmt->namelen = args->namelen;
1412 		memcpy((char *)name_rmt->name, args->name, args->namelen);
1413 		entry->flags |= XFS_ATTR_INCOMPLETE;
1414 		/* just in case */
1415 		name_rmt->valuelen = 0;
1416 		name_rmt->valueblk = 0;
1417 		args->rmtblkno = 1;
1418 		args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1419 		args->rmtvaluelen = args->valuelen;
1420 	}
1421 	xfs_trans_log_buf(args->trans, bp,
1422 	     XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
1423 				   xfs_attr_leaf_entsize(leaf, args->index)));
1424 
1425 	/*
1426 	 * Update the control info for this leaf node
1427 	 */
1428 	if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1429 		ichdr->firstused = be16_to_cpu(entry->nameidx);
1430 
1431 	ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1432 					+ xfs_attr3_leaf_hdr_size(leaf));
1433 	tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1434 					+ xfs_attr3_leaf_hdr_size(leaf);
1435 
1436 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1437 		if (ichdr->freemap[i].base == tmp) {
1438 			ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1439 			ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
1440 		}
1441 	}
1442 	ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1443 	return 0;
1444 }
1445 
1446 /*
1447  * Garbage collect a leaf attribute list block by copying it to a new buffer.
1448  */
1449 STATIC void
1450 xfs_attr3_leaf_compact(
1451 	struct xfs_da_args	*args,
1452 	struct xfs_attr3_icleaf_hdr *ichdr_dst,
1453 	struct xfs_buf		*bp)
1454 {
1455 	struct xfs_attr_leafblock *leaf_src;
1456 	struct xfs_attr_leafblock *leaf_dst;
1457 	struct xfs_attr3_icleaf_hdr ichdr_src;
1458 	struct xfs_trans	*trans = args->trans;
1459 	char			*tmpbuffer;
1460 
1461 	trace_xfs_attr_leaf_compact(args);
1462 
1463 	tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
1464 	memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1465 	memset(bp->b_addr, 0, args->geo->blksize);
1466 	leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1467 	leaf_dst = bp->b_addr;
1468 
1469 	/*
1470 	 * Copy the on-disk header back into the destination buffer to ensure
1471 	 * all the information in the header that is not part of the incore
1472 	 * header structure is preserved.
1473 	 */
1474 	memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1475 
1476 	/* Initialise the incore headers */
1477 	ichdr_src = *ichdr_dst;	/* struct copy */
1478 	ichdr_dst->firstused = args->geo->blksize;
1479 	ichdr_dst->usedbytes = 0;
1480 	ichdr_dst->count = 0;
1481 	ichdr_dst->holes = 0;
1482 	ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1483 	ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1484 						ichdr_dst->freemap[0].base;
1485 
1486 	/* write the header back to initialise the underlying buffer */
1487 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
1488 
1489 	/*
1490 	 * Copy all entry's in the same (sorted) order,
1491 	 * but allocate name/value pairs packed and in sequence.
1492 	 */
1493 	xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1494 				leaf_dst, ichdr_dst, 0, ichdr_src.count);
1495 	/*
1496 	 * this logs the entire buffer, but the caller must write the header
1497 	 * back to the buffer when it is finished modifying it.
1498 	 */
1499 	xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
1500 
1501 	kmem_free(tmpbuffer);
1502 }
1503 
1504 /*
1505  * Compare two leaf blocks "order".
1506  * Return 0 unless leaf2 should go before leaf1.
1507  */
1508 static int
1509 xfs_attr3_leaf_order(
1510 	struct xfs_buf	*leaf1_bp,
1511 	struct xfs_attr3_icleaf_hdr *leaf1hdr,
1512 	struct xfs_buf	*leaf2_bp,
1513 	struct xfs_attr3_icleaf_hdr *leaf2hdr)
1514 {
1515 	struct xfs_attr_leaf_entry *entries1;
1516 	struct xfs_attr_leaf_entry *entries2;
1517 
1518 	entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1519 	entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1520 	if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1521 	    ((be32_to_cpu(entries2[0].hashval) <
1522 	      be32_to_cpu(entries1[0].hashval)) ||
1523 	     (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1524 	      be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1525 		return 1;
1526 	}
1527 	return 0;
1528 }
1529 
1530 int
1531 xfs_attr_leaf_order(
1532 	struct xfs_buf	*leaf1_bp,
1533 	struct xfs_buf	*leaf2_bp)
1534 {
1535 	struct xfs_attr3_icleaf_hdr ichdr1;
1536 	struct xfs_attr3_icleaf_hdr ichdr2;
1537 	struct xfs_mount *mp = leaf1_bp->b_target->bt_mount;
1538 
1539 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1540 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
1541 	return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1542 }
1543 
1544 /*
1545  * Redistribute the attribute list entries between two leaf nodes,
1546  * taking into account the size of the new entry.
1547  *
1548  * NOTE: if new block is empty, then it will get the upper half of the
1549  * old block.  At present, all (one) callers pass in an empty second block.
1550  *
1551  * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1552  * to match what it is doing in splitting the attribute leaf block.  Those
1553  * values are used in "atomic rename" operations on attributes.  Note that
1554  * the "new" and "old" values can end up in different blocks.
1555  */
1556 STATIC void
1557 xfs_attr3_leaf_rebalance(
1558 	struct xfs_da_state	*state,
1559 	struct xfs_da_state_blk	*blk1,
1560 	struct xfs_da_state_blk	*blk2)
1561 {
1562 	struct xfs_da_args	*args;
1563 	struct xfs_attr_leafblock *leaf1;
1564 	struct xfs_attr_leafblock *leaf2;
1565 	struct xfs_attr3_icleaf_hdr ichdr1;
1566 	struct xfs_attr3_icleaf_hdr ichdr2;
1567 	struct xfs_attr_leaf_entry *entries1;
1568 	struct xfs_attr_leaf_entry *entries2;
1569 	int			count;
1570 	int			totallen;
1571 	int			max;
1572 	int			space;
1573 	int			swap;
1574 
1575 	/*
1576 	 * Set up environment.
1577 	 */
1578 	ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1579 	ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1580 	leaf1 = blk1->bp->b_addr;
1581 	leaf2 = blk2->bp->b_addr;
1582 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1583 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
1584 	ASSERT(ichdr2.count == 0);
1585 	args = state->args;
1586 
1587 	trace_xfs_attr_leaf_rebalance(args);
1588 
1589 	/*
1590 	 * Check ordering of blocks, reverse if it makes things simpler.
1591 	 *
1592 	 * NOTE: Given that all (current) callers pass in an empty
1593 	 * second block, this code should never set "swap".
1594 	 */
1595 	swap = 0;
1596 	if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1597 		swap(blk1, blk2);
1598 
1599 		/* swap structures rather than reconverting them */
1600 		swap(ichdr1, ichdr2);
1601 
1602 		leaf1 = blk1->bp->b_addr;
1603 		leaf2 = blk2->bp->b_addr;
1604 		swap = 1;
1605 	}
1606 
1607 	/*
1608 	 * Examine entries until we reduce the absolute difference in
1609 	 * byte usage between the two blocks to a minimum.  Then get
1610 	 * the direction to copy and the number of elements to move.
1611 	 *
1612 	 * "inleaf" is true if the new entry should be inserted into blk1.
1613 	 * If "swap" is also true, then reverse the sense of "inleaf".
1614 	 */
1615 	state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1616 						      blk2, &ichdr2,
1617 						      &count, &totallen);
1618 	if (swap)
1619 		state->inleaf = !state->inleaf;
1620 
1621 	/*
1622 	 * Move any entries required from leaf to leaf:
1623 	 */
1624 	if (count < ichdr1.count) {
1625 		/*
1626 		 * Figure the total bytes to be added to the destination leaf.
1627 		 */
1628 		/* number entries being moved */
1629 		count = ichdr1.count - count;
1630 		space  = ichdr1.usedbytes - totallen;
1631 		space += count * sizeof(xfs_attr_leaf_entry_t);
1632 
1633 		/*
1634 		 * leaf2 is the destination, compact it if it looks tight.
1635 		 */
1636 		max  = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1637 		max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
1638 		if (space > max)
1639 			xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
1640 
1641 		/*
1642 		 * Move high entries from leaf1 to low end of leaf2.
1643 		 */
1644 		xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1645 				ichdr1.count - count, leaf2, &ichdr2, 0, count);
1646 
1647 	} else if (count > ichdr1.count) {
1648 		/*
1649 		 * I assert that since all callers pass in an empty
1650 		 * second buffer, this code should never execute.
1651 		 */
1652 		ASSERT(0);
1653 
1654 		/*
1655 		 * Figure the total bytes to be added to the destination leaf.
1656 		 */
1657 		/* number entries being moved */
1658 		count -= ichdr1.count;
1659 		space  = totallen - ichdr1.usedbytes;
1660 		space += count * sizeof(xfs_attr_leaf_entry_t);
1661 
1662 		/*
1663 		 * leaf1 is the destination, compact it if it looks tight.
1664 		 */
1665 		max  = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1666 		max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
1667 		if (space > max)
1668 			xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
1669 
1670 		/*
1671 		 * Move low entries from leaf2 to high end of leaf1.
1672 		 */
1673 		xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1674 					ichdr1.count, count);
1675 	}
1676 
1677 	xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1678 	xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
1679 	xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1680 	xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
1681 
1682 	/*
1683 	 * Copy out last hashval in each block for B-tree code.
1684 	 */
1685 	entries1 = xfs_attr3_leaf_entryp(leaf1);
1686 	entries2 = xfs_attr3_leaf_entryp(leaf2);
1687 	blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1688 	blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
1689 
1690 	/*
1691 	 * Adjust the expected index for insertion.
1692 	 * NOTE: this code depends on the (current) situation that the
1693 	 * second block was originally empty.
1694 	 *
1695 	 * If the insertion point moved to the 2nd block, we must adjust
1696 	 * the index.  We must also track the entry just following the
1697 	 * new entry for use in an "atomic rename" operation, that entry
1698 	 * is always the "old" entry and the "new" entry is what we are
1699 	 * inserting.  The index/blkno fields refer to the "old" entry,
1700 	 * while the index2/blkno2 fields refer to the "new" entry.
1701 	 */
1702 	if (blk1->index > ichdr1.count) {
1703 		ASSERT(state->inleaf == 0);
1704 		blk2->index = blk1->index - ichdr1.count;
1705 		args->index = args->index2 = blk2->index;
1706 		args->blkno = args->blkno2 = blk2->blkno;
1707 	} else if (blk1->index == ichdr1.count) {
1708 		if (state->inleaf) {
1709 			args->index = blk1->index;
1710 			args->blkno = blk1->blkno;
1711 			args->index2 = 0;
1712 			args->blkno2 = blk2->blkno;
1713 		} else {
1714 			/*
1715 			 * On a double leaf split, the original attr location
1716 			 * is already stored in blkno2/index2, so don't
1717 			 * overwrite it overwise we corrupt the tree.
1718 			 */
1719 			blk2->index = blk1->index - ichdr1.count;
1720 			args->index = blk2->index;
1721 			args->blkno = blk2->blkno;
1722 			if (!state->extravalid) {
1723 				/*
1724 				 * set the new attr location to match the old
1725 				 * one and let the higher level split code
1726 				 * decide where in the leaf to place it.
1727 				 */
1728 				args->index2 = blk2->index;
1729 				args->blkno2 = blk2->blkno;
1730 			}
1731 		}
1732 	} else {
1733 		ASSERT(state->inleaf == 1);
1734 		args->index = args->index2 = blk1->index;
1735 		args->blkno = args->blkno2 = blk1->blkno;
1736 	}
1737 }
1738 
1739 /*
1740  * Examine entries until we reduce the absolute difference in
1741  * byte usage between the two blocks to a minimum.
1742  * GROT: Is this really necessary?  With other than a 512 byte blocksize,
1743  * GROT: there will always be enough room in either block for a new entry.
1744  * GROT: Do a double-split for this case?
1745  */
1746 STATIC int
1747 xfs_attr3_leaf_figure_balance(
1748 	struct xfs_da_state		*state,
1749 	struct xfs_da_state_blk		*blk1,
1750 	struct xfs_attr3_icleaf_hdr	*ichdr1,
1751 	struct xfs_da_state_blk		*blk2,
1752 	struct xfs_attr3_icleaf_hdr	*ichdr2,
1753 	int				*countarg,
1754 	int				*usedbytesarg)
1755 {
1756 	struct xfs_attr_leafblock	*leaf1 = blk1->bp->b_addr;
1757 	struct xfs_attr_leafblock	*leaf2 = blk2->bp->b_addr;
1758 	struct xfs_attr_leaf_entry	*entry;
1759 	int				count;
1760 	int				max;
1761 	int				index;
1762 	int				totallen = 0;
1763 	int				half;
1764 	int				lastdelta;
1765 	int				foundit = 0;
1766 	int				tmp;
1767 
1768 	/*
1769 	 * Examine entries until we reduce the absolute difference in
1770 	 * byte usage between the two blocks to a minimum.
1771 	 */
1772 	max = ichdr1->count + ichdr2->count;
1773 	half = (max + 1) * sizeof(*entry);
1774 	half += ichdr1->usedbytes + ichdr2->usedbytes +
1775 			xfs_attr_leaf_newentsize(state->args, NULL);
1776 	half /= 2;
1777 	lastdelta = state->args->geo->blksize;
1778 	entry = xfs_attr3_leaf_entryp(leaf1);
1779 	for (count = index = 0; count < max; entry++, index++, count++) {
1780 
1781 #define XFS_ATTR_ABS(A)	(((A) < 0) ? -(A) : (A))
1782 		/*
1783 		 * The new entry is in the first block, account for it.
1784 		 */
1785 		if (count == blk1->index) {
1786 			tmp = totallen + sizeof(*entry) +
1787 				xfs_attr_leaf_newentsize(state->args, NULL);
1788 			if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1789 				break;
1790 			lastdelta = XFS_ATTR_ABS(half - tmp);
1791 			totallen = tmp;
1792 			foundit = 1;
1793 		}
1794 
1795 		/*
1796 		 * Wrap around into the second block if necessary.
1797 		 */
1798 		if (count == ichdr1->count) {
1799 			leaf1 = leaf2;
1800 			entry = xfs_attr3_leaf_entryp(leaf1);
1801 			index = 0;
1802 		}
1803 
1804 		/*
1805 		 * Figure out if next leaf entry would be too much.
1806 		 */
1807 		tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1808 									index);
1809 		if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1810 			break;
1811 		lastdelta = XFS_ATTR_ABS(half - tmp);
1812 		totallen = tmp;
1813 #undef XFS_ATTR_ABS
1814 	}
1815 
1816 	/*
1817 	 * Calculate the number of usedbytes that will end up in lower block.
1818 	 * If new entry not in lower block, fix up the count.
1819 	 */
1820 	totallen -= count * sizeof(*entry);
1821 	if (foundit) {
1822 		totallen -= sizeof(*entry) +
1823 				xfs_attr_leaf_newentsize(state->args, NULL);
1824 	}
1825 
1826 	*countarg = count;
1827 	*usedbytesarg = totallen;
1828 	return foundit;
1829 }
1830 
1831 /*========================================================================
1832  * Routines used for shrinking the Btree.
1833  *========================================================================*/
1834 
1835 /*
1836  * Check a leaf block and its neighbors to see if the block should be
1837  * collapsed into one or the other neighbor.  Always keep the block
1838  * with the smaller block number.
1839  * If the current block is over 50% full, don't try to join it, return 0.
1840  * If the block is empty, fill in the state structure and return 2.
1841  * If it can be collapsed, fill in the state structure and return 1.
1842  * If nothing can be done, return 0.
1843  *
1844  * GROT: allow for INCOMPLETE entries in calculation.
1845  */
1846 int
1847 xfs_attr3_leaf_toosmall(
1848 	struct xfs_da_state	*state,
1849 	int			*action)
1850 {
1851 	struct xfs_attr_leafblock *leaf;
1852 	struct xfs_da_state_blk	*blk;
1853 	struct xfs_attr3_icleaf_hdr ichdr;
1854 	struct xfs_buf		*bp;
1855 	xfs_dablk_t		blkno;
1856 	int			bytes;
1857 	int			forward;
1858 	int			error;
1859 	int			retval;
1860 	int			i;
1861 
1862 	trace_xfs_attr_leaf_toosmall(state->args);
1863 
1864 	/*
1865 	 * Check for the degenerate case of the block being over 50% full.
1866 	 * If so, it's not worth even looking to see if we might be able
1867 	 * to coalesce with a sibling.
1868 	 */
1869 	blk = &state->path.blk[ state->path.active-1 ];
1870 	leaf = blk->bp->b_addr;
1871 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
1872 	bytes = xfs_attr3_leaf_hdr_size(leaf) +
1873 		ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1874 		ichdr.usedbytes;
1875 	if (bytes > (state->args->geo->blksize >> 1)) {
1876 		*action = 0;	/* blk over 50%, don't try to join */
1877 		return 0;
1878 	}
1879 
1880 	/*
1881 	 * Check for the degenerate case of the block being empty.
1882 	 * If the block is empty, we'll simply delete it, no need to
1883 	 * coalesce it with a sibling block.  We choose (arbitrarily)
1884 	 * to merge with the forward block unless it is NULL.
1885 	 */
1886 	if (ichdr.count == 0) {
1887 		/*
1888 		 * Make altpath point to the block we want to keep and
1889 		 * path point to the block we want to drop (this one).
1890 		 */
1891 		forward = (ichdr.forw != 0);
1892 		memcpy(&state->altpath, &state->path, sizeof(state->path));
1893 		error = xfs_da3_path_shift(state, &state->altpath, forward,
1894 						 0, &retval);
1895 		if (error)
1896 			return error;
1897 		if (retval) {
1898 			*action = 0;
1899 		} else {
1900 			*action = 2;
1901 		}
1902 		return 0;
1903 	}
1904 
1905 	/*
1906 	 * Examine each sibling block to see if we can coalesce with
1907 	 * at least 25% free space to spare.  We need to figure out
1908 	 * whether to merge with the forward or the backward block.
1909 	 * We prefer coalescing with the lower numbered sibling so as
1910 	 * to shrink an attribute list over time.
1911 	 */
1912 	/* start with smaller blk num */
1913 	forward = ichdr.forw < ichdr.back;
1914 	for (i = 0; i < 2; forward = !forward, i++) {
1915 		struct xfs_attr3_icleaf_hdr ichdr2;
1916 		if (forward)
1917 			blkno = ichdr.forw;
1918 		else
1919 			blkno = ichdr.back;
1920 		if (blkno == 0)
1921 			continue;
1922 		error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
1923 					blkno, -1, &bp);
1924 		if (error)
1925 			return error;
1926 
1927 		xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
1928 
1929 		bytes = state->args->geo->blksize -
1930 			(state->args->geo->blksize >> 2) -
1931 			ichdr.usedbytes - ichdr2.usedbytes -
1932 			((ichdr.count + ichdr2.count) *
1933 					sizeof(xfs_attr_leaf_entry_t)) -
1934 			xfs_attr3_leaf_hdr_size(leaf);
1935 
1936 		xfs_trans_brelse(state->args->trans, bp);
1937 		if (bytes >= 0)
1938 			break;	/* fits with at least 25% to spare */
1939 	}
1940 	if (i >= 2) {
1941 		*action = 0;
1942 		return 0;
1943 	}
1944 
1945 	/*
1946 	 * Make altpath point to the block we want to keep (the lower
1947 	 * numbered block) and path point to the block we want to drop.
1948 	 */
1949 	memcpy(&state->altpath, &state->path, sizeof(state->path));
1950 	if (blkno < blk->blkno) {
1951 		error = xfs_da3_path_shift(state, &state->altpath, forward,
1952 						 0, &retval);
1953 	} else {
1954 		error = xfs_da3_path_shift(state, &state->path, forward,
1955 						 0, &retval);
1956 	}
1957 	if (error)
1958 		return error;
1959 	if (retval) {
1960 		*action = 0;
1961 	} else {
1962 		*action = 1;
1963 	}
1964 	return 0;
1965 }
1966 
1967 /*
1968  * Remove a name from the leaf attribute list structure.
1969  *
1970  * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1971  * If two leaves are 37% full, when combined they will leave 25% free.
1972  */
1973 int
1974 xfs_attr3_leaf_remove(
1975 	struct xfs_buf		*bp,
1976 	struct xfs_da_args	*args)
1977 {
1978 	struct xfs_attr_leafblock *leaf;
1979 	struct xfs_attr3_icleaf_hdr ichdr;
1980 	struct xfs_attr_leaf_entry *entry;
1981 	int			before;
1982 	int			after;
1983 	int			smallest;
1984 	int			entsize;
1985 	int			tablesize;
1986 	int			tmp;
1987 	int			i;
1988 
1989 	trace_xfs_attr_leaf_remove(args);
1990 
1991 	leaf = bp->b_addr;
1992 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1993 
1994 	ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
1995 	ASSERT(args->index >= 0 && args->index < ichdr.count);
1996 	ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1997 					xfs_attr3_leaf_hdr_size(leaf));
1998 
1999 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2000 
2001 	ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2002 	ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2003 
2004 	/*
2005 	 * Scan through free region table:
2006 	 *    check for adjacency of free'd entry with an existing one,
2007 	 *    find smallest free region in case we need to replace it,
2008 	 *    adjust any map that borders the entry table,
2009 	 */
2010 	tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2011 					+ xfs_attr3_leaf_hdr_size(leaf);
2012 	tmp = ichdr.freemap[0].size;
2013 	before = after = -1;
2014 	smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2015 	entsize = xfs_attr_leaf_entsize(leaf, args->index);
2016 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2017 		ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2018 		ASSERT(ichdr.freemap[i].size < args->geo->blksize);
2019 		if (ichdr.freemap[i].base == tablesize) {
2020 			ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2021 			ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
2022 		}
2023 
2024 		if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2025 				be16_to_cpu(entry->nameidx)) {
2026 			before = i;
2027 		} else if (ichdr.freemap[i].base ==
2028 				(be16_to_cpu(entry->nameidx) + entsize)) {
2029 			after = i;
2030 		} else if (ichdr.freemap[i].size < tmp) {
2031 			tmp = ichdr.freemap[i].size;
2032 			smallest = i;
2033 		}
2034 	}
2035 
2036 	/*
2037 	 * Coalesce adjacent freemap regions,
2038 	 * or replace the smallest region.
2039 	 */
2040 	if ((before >= 0) || (after >= 0)) {
2041 		if ((before >= 0) && (after >= 0)) {
2042 			ichdr.freemap[before].size += entsize;
2043 			ichdr.freemap[before].size += ichdr.freemap[after].size;
2044 			ichdr.freemap[after].base = 0;
2045 			ichdr.freemap[after].size = 0;
2046 		} else if (before >= 0) {
2047 			ichdr.freemap[before].size += entsize;
2048 		} else {
2049 			ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2050 			ichdr.freemap[after].size += entsize;
2051 		}
2052 	} else {
2053 		/*
2054 		 * Replace smallest region (if it is smaller than free'd entry)
2055 		 */
2056 		if (ichdr.freemap[smallest].size < entsize) {
2057 			ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2058 			ichdr.freemap[smallest].size = entsize;
2059 		}
2060 	}
2061 
2062 	/*
2063 	 * Did we remove the first entry?
2064 	 */
2065 	if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
2066 		smallest = 1;
2067 	else
2068 		smallest = 0;
2069 
2070 	/*
2071 	 * Compress the remaining entries and zero out the removed stuff.
2072 	 */
2073 	memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2074 	ichdr.usedbytes -= entsize;
2075 	xfs_trans_log_buf(args->trans, bp,
2076 	     XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
2077 				   entsize));
2078 
2079 	tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2080 	memmove(entry, entry + 1, tmp);
2081 	ichdr.count--;
2082 	xfs_trans_log_buf(args->trans, bp,
2083 	    XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2084 
2085 	entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2086 	memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
2087 
2088 	/*
2089 	 * If we removed the first entry, re-find the first used byte
2090 	 * in the name area.  Note that if the entry was the "firstused",
2091 	 * then we don't have a "hole" in our block resulting from
2092 	 * removing the name.
2093 	 */
2094 	if (smallest) {
2095 		tmp = args->geo->blksize;
2096 		entry = xfs_attr3_leaf_entryp(leaf);
2097 		for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2098 			ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2099 			ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2100 
2101 			if (be16_to_cpu(entry->nameidx) < tmp)
2102 				tmp = be16_to_cpu(entry->nameidx);
2103 		}
2104 		ichdr.firstused = tmp;
2105 		ASSERT(ichdr.firstused != 0);
2106 	} else {
2107 		ichdr.holes = 1;	/* mark as needing compaction */
2108 	}
2109 	xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
2110 	xfs_trans_log_buf(args->trans, bp,
2111 			  XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2112 					  xfs_attr3_leaf_hdr_size(leaf)));
2113 
2114 	/*
2115 	 * Check if leaf is less than 50% full, caller may want to
2116 	 * "join" the leaf with a sibling if so.
2117 	 */
2118 	tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2119 	      ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2120 
2121 	return tmp < args->geo->magicpct; /* leaf is < 37% full */
2122 }
2123 
2124 /*
2125  * Move all the attribute list entries from drop_leaf into save_leaf.
2126  */
2127 void
2128 xfs_attr3_leaf_unbalance(
2129 	struct xfs_da_state	*state,
2130 	struct xfs_da_state_blk	*drop_blk,
2131 	struct xfs_da_state_blk	*save_blk)
2132 {
2133 	struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2134 	struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2135 	struct xfs_attr3_icleaf_hdr drophdr;
2136 	struct xfs_attr3_icleaf_hdr savehdr;
2137 	struct xfs_attr_leaf_entry *entry;
2138 
2139 	trace_xfs_attr_leaf_unbalance(state->args);
2140 
2141 	drop_leaf = drop_blk->bp->b_addr;
2142 	save_leaf = save_blk->bp->b_addr;
2143 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2144 	xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
2145 	entry = xfs_attr3_leaf_entryp(drop_leaf);
2146 
2147 	/*
2148 	 * Save last hashval from dying block for later Btree fixup.
2149 	 */
2150 	drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
2151 
2152 	/*
2153 	 * Check if we need a temp buffer, or can we do it in place.
2154 	 * Note that we don't check "leaf" for holes because we will
2155 	 * always be dropping it, toosmall() decided that for us already.
2156 	 */
2157 	if (savehdr.holes == 0) {
2158 		/*
2159 		 * dest leaf has no holes, so we add there.  May need
2160 		 * to make some room in the entry array.
2161 		 */
2162 		if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2163 					 drop_blk->bp, &drophdr)) {
2164 			xfs_attr3_leaf_moveents(state->args,
2165 						drop_leaf, &drophdr, 0,
2166 						save_leaf, &savehdr, 0,
2167 						drophdr.count);
2168 		} else {
2169 			xfs_attr3_leaf_moveents(state->args,
2170 						drop_leaf, &drophdr, 0,
2171 						save_leaf, &savehdr,
2172 						savehdr.count, drophdr.count);
2173 		}
2174 	} else {
2175 		/*
2176 		 * Destination has holes, so we make a temporary copy
2177 		 * of the leaf and add them both to that.
2178 		 */
2179 		struct xfs_attr_leafblock *tmp_leaf;
2180 		struct xfs_attr3_icleaf_hdr tmphdr;
2181 
2182 		tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP);
2183 
2184 		/*
2185 		 * Copy the header into the temp leaf so that all the stuff
2186 		 * not in the incore header is present and gets copied back in
2187 		 * once we've moved all the entries.
2188 		 */
2189 		memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2190 
2191 		memset(&tmphdr, 0, sizeof(tmphdr));
2192 		tmphdr.magic = savehdr.magic;
2193 		tmphdr.forw = savehdr.forw;
2194 		tmphdr.back = savehdr.back;
2195 		tmphdr.firstused = state->args->geo->blksize;
2196 
2197 		/* write the header to the temp buffer to initialise it */
2198 		xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
2199 
2200 		if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2201 					 drop_blk->bp, &drophdr)) {
2202 			xfs_attr3_leaf_moveents(state->args,
2203 						drop_leaf, &drophdr, 0,
2204 						tmp_leaf, &tmphdr, 0,
2205 						drophdr.count);
2206 			xfs_attr3_leaf_moveents(state->args,
2207 						save_leaf, &savehdr, 0,
2208 						tmp_leaf, &tmphdr, tmphdr.count,
2209 						savehdr.count);
2210 		} else {
2211 			xfs_attr3_leaf_moveents(state->args,
2212 						save_leaf, &savehdr, 0,
2213 						tmp_leaf, &tmphdr, 0,
2214 						savehdr.count);
2215 			xfs_attr3_leaf_moveents(state->args,
2216 						drop_leaf, &drophdr, 0,
2217 						tmp_leaf, &tmphdr, tmphdr.count,
2218 						drophdr.count);
2219 		}
2220 		memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
2221 		savehdr = tmphdr; /* struct copy */
2222 		kmem_free(tmp_leaf);
2223 	}
2224 
2225 	xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
2226 	xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
2227 					   state->args->geo->blksize - 1);
2228 
2229 	/*
2230 	 * Copy out last hashval in each block for B-tree code.
2231 	 */
2232 	entry = xfs_attr3_leaf_entryp(save_leaf);
2233 	save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
2234 }
2235 
2236 /*========================================================================
2237  * Routines used for finding things in the Btree.
2238  *========================================================================*/
2239 
2240 /*
2241  * Look up a name in a leaf attribute list structure.
2242  * This is the internal routine, it uses the caller's buffer.
2243  *
2244  * Note that duplicate keys are allowed, but only check within the
2245  * current leaf node.  The Btree code must check in adjacent leaf nodes.
2246  *
2247  * Return in args->index the index into the entry[] array of either
2248  * the found entry, or where the entry should have been (insert before
2249  * that entry).
2250  *
2251  * Don't change the args->value unless we find the attribute.
2252  */
2253 int
2254 xfs_attr3_leaf_lookup_int(
2255 	struct xfs_buf		*bp,
2256 	struct xfs_da_args	*args)
2257 {
2258 	struct xfs_attr_leafblock *leaf;
2259 	struct xfs_attr3_icleaf_hdr ichdr;
2260 	struct xfs_attr_leaf_entry *entry;
2261 	struct xfs_attr_leaf_entry *entries;
2262 	struct xfs_attr_leaf_name_local *name_loc;
2263 	struct xfs_attr_leaf_name_remote *name_rmt;
2264 	xfs_dahash_t		hashval;
2265 	int			probe;
2266 	int			span;
2267 
2268 	trace_xfs_attr_leaf_lookup(args);
2269 
2270 	leaf = bp->b_addr;
2271 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2272 	entries = xfs_attr3_leaf_entryp(leaf);
2273 	if (ichdr.count >= args->geo->blksize / 8)
2274 		return -EFSCORRUPTED;
2275 
2276 	/*
2277 	 * Binary search.  (note: small blocks will skip this loop)
2278 	 */
2279 	hashval = args->hashval;
2280 	probe = span = ichdr.count / 2;
2281 	for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
2282 		span /= 2;
2283 		if (be32_to_cpu(entry->hashval) < hashval)
2284 			probe += span;
2285 		else if (be32_to_cpu(entry->hashval) > hashval)
2286 			probe -= span;
2287 		else
2288 			break;
2289 	}
2290 	if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
2291 		return -EFSCORRUPTED;
2292 	if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
2293 		return -EFSCORRUPTED;
2294 
2295 	/*
2296 	 * Since we may have duplicate hashval's, find the first matching
2297 	 * hashval in the leaf.
2298 	 */
2299 	while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
2300 		entry--;
2301 		probe--;
2302 	}
2303 	while (probe < ichdr.count &&
2304 	       be32_to_cpu(entry->hashval) < hashval) {
2305 		entry++;
2306 		probe++;
2307 	}
2308 	if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
2309 		args->index = probe;
2310 		return -ENOATTR;
2311 	}
2312 
2313 	/*
2314 	 * Duplicate keys may be present, so search all of them for a match.
2315 	 */
2316 	for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
2317 			entry++, probe++) {
2318 /*
2319  * GROT: Add code to remove incomplete entries.
2320  */
2321 		/*
2322 		 * If we are looking for INCOMPLETE entries, show only those.
2323 		 * If we are looking for complete entries, show only those.
2324 		 */
2325 		if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2326 		    (entry->flags & XFS_ATTR_INCOMPLETE)) {
2327 			continue;
2328 		}
2329 		if (entry->flags & XFS_ATTR_LOCAL) {
2330 			name_loc = xfs_attr3_leaf_name_local(leaf, probe);
2331 			if (name_loc->namelen != args->namelen)
2332 				continue;
2333 			if (memcmp(args->name, name_loc->nameval,
2334 							args->namelen) != 0)
2335 				continue;
2336 			if (!xfs_attr_namesp_match(args->flags, entry->flags))
2337 				continue;
2338 			args->index = probe;
2339 			return -EEXIST;
2340 		} else {
2341 			name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
2342 			if (name_rmt->namelen != args->namelen)
2343 				continue;
2344 			if (memcmp(args->name, name_rmt->name,
2345 							args->namelen) != 0)
2346 				continue;
2347 			if (!xfs_attr_namesp_match(args->flags, entry->flags))
2348 				continue;
2349 			args->index = probe;
2350 			args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2351 			args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2352 			args->rmtblkcnt = xfs_attr3_rmt_blocks(
2353 							args->dp->i_mount,
2354 							args->rmtvaluelen);
2355 			return -EEXIST;
2356 		}
2357 	}
2358 	args->index = probe;
2359 	return -ENOATTR;
2360 }
2361 
2362 /*
2363  * Get the value associated with an attribute name from a leaf attribute
2364  * list structure.
2365  */
2366 int
2367 xfs_attr3_leaf_getvalue(
2368 	struct xfs_buf		*bp,
2369 	struct xfs_da_args	*args)
2370 {
2371 	struct xfs_attr_leafblock *leaf;
2372 	struct xfs_attr3_icleaf_hdr ichdr;
2373 	struct xfs_attr_leaf_entry *entry;
2374 	struct xfs_attr_leaf_name_local *name_loc;
2375 	struct xfs_attr_leaf_name_remote *name_rmt;
2376 	int			valuelen;
2377 
2378 	leaf = bp->b_addr;
2379 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2380 	ASSERT(ichdr.count < args->geo->blksize / 8);
2381 	ASSERT(args->index < ichdr.count);
2382 
2383 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2384 	if (entry->flags & XFS_ATTR_LOCAL) {
2385 		name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2386 		ASSERT(name_loc->namelen == args->namelen);
2387 		ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2388 		valuelen = be16_to_cpu(name_loc->valuelen);
2389 		if (args->flags & ATTR_KERNOVAL) {
2390 			args->valuelen = valuelen;
2391 			return 0;
2392 		}
2393 		if (args->valuelen < valuelen) {
2394 			args->valuelen = valuelen;
2395 			return -ERANGE;
2396 		}
2397 		args->valuelen = valuelen;
2398 		memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2399 	} else {
2400 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2401 		ASSERT(name_rmt->namelen == args->namelen);
2402 		ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2403 		args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2404 		args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2405 		args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2406 						       args->rmtvaluelen);
2407 		if (args->flags & ATTR_KERNOVAL) {
2408 			args->valuelen = args->rmtvaluelen;
2409 			return 0;
2410 		}
2411 		if (args->valuelen < args->rmtvaluelen) {
2412 			args->valuelen = args->rmtvaluelen;
2413 			return -ERANGE;
2414 		}
2415 		args->valuelen = args->rmtvaluelen;
2416 	}
2417 	return 0;
2418 }
2419 
2420 /*========================================================================
2421  * Utility routines.
2422  *========================================================================*/
2423 
2424 /*
2425  * Move the indicated entries from one leaf to another.
2426  * NOTE: this routine modifies both source and destination leaves.
2427  */
2428 /*ARGSUSED*/
2429 STATIC void
2430 xfs_attr3_leaf_moveents(
2431 	struct xfs_da_args		*args,
2432 	struct xfs_attr_leafblock	*leaf_s,
2433 	struct xfs_attr3_icleaf_hdr	*ichdr_s,
2434 	int				start_s,
2435 	struct xfs_attr_leafblock	*leaf_d,
2436 	struct xfs_attr3_icleaf_hdr	*ichdr_d,
2437 	int				start_d,
2438 	int				count)
2439 {
2440 	struct xfs_attr_leaf_entry	*entry_s;
2441 	struct xfs_attr_leaf_entry	*entry_d;
2442 	int				desti;
2443 	int				tmp;
2444 	int				i;
2445 
2446 	/*
2447 	 * Check for nothing to do.
2448 	 */
2449 	if (count == 0)
2450 		return;
2451 
2452 	/*
2453 	 * Set up environment.
2454 	 */
2455 	ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2456 	       ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2457 	ASSERT(ichdr_s->magic == ichdr_d->magic);
2458 	ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
2459 	ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2460 					+ xfs_attr3_leaf_hdr_size(leaf_s));
2461 	ASSERT(ichdr_d->count < args->geo->blksize / 8);
2462 	ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2463 					+ xfs_attr3_leaf_hdr_size(leaf_d));
2464 
2465 	ASSERT(start_s < ichdr_s->count);
2466 	ASSERT(start_d <= ichdr_d->count);
2467 	ASSERT(count <= ichdr_s->count);
2468 
2469 
2470 	/*
2471 	 * Move the entries in the destination leaf up to make a hole?
2472 	 */
2473 	if (start_d < ichdr_d->count) {
2474 		tmp  = ichdr_d->count - start_d;
2475 		tmp *= sizeof(xfs_attr_leaf_entry_t);
2476 		entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2477 		entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2478 		memmove(entry_d, entry_s, tmp);
2479 	}
2480 
2481 	/*
2482 	 * Copy all entry's in the same (sorted) order,
2483 	 * but allocate attribute info packed and in sequence.
2484 	 */
2485 	entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2486 	entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2487 	desti = start_d;
2488 	for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2489 		ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
2490 		tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2491 #ifdef GROT
2492 		/*
2493 		 * Code to drop INCOMPLETE entries.  Difficult to use as we
2494 		 * may also need to change the insertion index.  Code turned
2495 		 * off for 6.2, should be revisited later.
2496 		 */
2497 		if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2498 			memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2499 			ichdr_s->usedbytes -= tmp;
2500 			ichdr_s->count -= 1;
2501 			entry_d--;	/* to compensate for ++ in loop hdr */
2502 			desti--;
2503 			if ((start_s + i) < offset)
2504 				result++;	/* insertion index adjustment */
2505 		} else {
2506 #endif /* GROT */
2507 			ichdr_d->firstused -= tmp;
2508 			/* both on-disk, don't endian flip twice */
2509 			entry_d->hashval = entry_s->hashval;
2510 			entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
2511 			entry_d->flags = entry_s->flags;
2512 			ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2513 							<= args->geo->blksize);
2514 			memmove(xfs_attr3_leaf_name(leaf_d, desti),
2515 				xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
2516 			ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2517 							<= args->geo->blksize);
2518 			memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2519 			ichdr_s->usedbytes -= tmp;
2520 			ichdr_d->usedbytes += tmp;
2521 			ichdr_s->count -= 1;
2522 			ichdr_d->count += 1;
2523 			tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2524 					+ xfs_attr3_leaf_hdr_size(leaf_d);
2525 			ASSERT(ichdr_d->firstused >= tmp);
2526 #ifdef GROT
2527 		}
2528 #endif /* GROT */
2529 	}
2530 
2531 	/*
2532 	 * Zero out the entries we just copied.
2533 	 */
2534 	if (start_s == ichdr_s->count) {
2535 		tmp = count * sizeof(xfs_attr_leaf_entry_t);
2536 		entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2537 		ASSERT(((char *)entry_s + tmp) <=
2538 		       ((char *)leaf_s + args->geo->blksize));
2539 		memset(entry_s, 0, tmp);
2540 	} else {
2541 		/*
2542 		 * Move the remaining entries down to fill the hole,
2543 		 * then zero the entries at the top.
2544 		 */
2545 		tmp  = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2546 		entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2547 		entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2548 		memmove(entry_d, entry_s, tmp);
2549 
2550 		tmp = count * sizeof(xfs_attr_leaf_entry_t);
2551 		entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
2552 		ASSERT(((char *)entry_s + tmp) <=
2553 		       ((char *)leaf_s + args->geo->blksize));
2554 		memset(entry_s, 0, tmp);
2555 	}
2556 
2557 	/*
2558 	 * Fill in the freemap information
2559 	 */
2560 	ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2561 	ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2562 	ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2563 	ichdr_d->freemap[1].base = 0;
2564 	ichdr_d->freemap[2].base = 0;
2565 	ichdr_d->freemap[1].size = 0;
2566 	ichdr_d->freemap[2].size = 0;
2567 	ichdr_s->holes = 1;	/* leaf may not be compact */
2568 }
2569 
2570 /*
2571  * Pick up the last hashvalue from a leaf block.
2572  */
2573 xfs_dahash_t
2574 xfs_attr_leaf_lasthash(
2575 	struct xfs_buf	*bp,
2576 	int		*count)
2577 {
2578 	struct xfs_attr3_icleaf_hdr ichdr;
2579 	struct xfs_attr_leaf_entry *entries;
2580 	struct xfs_mount *mp = bp->b_target->bt_mount;
2581 
2582 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
2583 	entries = xfs_attr3_leaf_entryp(bp->b_addr);
2584 	if (count)
2585 		*count = ichdr.count;
2586 	if (!ichdr.count)
2587 		return 0;
2588 	return be32_to_cpu(entries[ichdr.count - 1].hashval);
2589 }
2590 
2591 /*
2592  * Calculate the number of bytes used to store the indicated attribute
2593  * (whether local or remote only calculate bytes in this block).
2594  */
2595 STATIC int
2596 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2597 {
2598 	struct xfs_attr_leaf_entry *entries;
2599 	xfs_attr_leaf_name_local_t *name_loc;
2600 	xfs_attr_leaf_name_remote_t *name_rmt;
2601 	int size;
2602 
2603 	entries = xfs_attr3_leaf_entryp(leaf);
2604 	if (entries[index].flags & XFS_ATTR_LOCAL) {
2605 		name_loc = xfs_attr3_leaf_name_local(leaf, index);
2606 		size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2607 						   be16_to_cpu(name_loc->valuelen));
2608 	} else {
2609 		name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
2610 		size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2611 	}
2612 	return size;
2613 }
2614 
2615 /*
2616  * Calculate the number of bytes that would be required to store the new
2617  * attribute (whether local or remote only calculate bytes in this block).
2618  * This routine decides as a side effect whether the attribute will be
2619  * a "local" or a "remote" attribute.
2620  */
2621 int
2622 xfs_attr_leaf_newentsize(
2623 	struct xfs_da_args	*args,
2624 	int			*local)
2625 {
2626 	int			size;
2627 
2628 	size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2629 	if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2630 		if (local)
2631 			*local = 1;
2632 		return size;
2633 	}
2634 	if (local)
2635 		*local = 0;
2636 	return xfs_attr_leaf_entsize_remote(args->namelen);
2637 }
2638 
2639 
2640 /*========================================================================
2641  * Manage the INCOMPLETE flag in a leaf entry
2642  *========================================================================*/
2643 
2644 /*
2645  * Clear the INCOMPLETE flag on an entry in a leaf block.
2646  */
2647 int
2648 xfs_attr3_leaf_clearflag(
2649 	struct xfs_da_args	*args)
2650 {
2651 	struct xfs_attr_leafblock *leaf;
2652 	struct xfs_attr_leaf_entry *entry;
2653 	struct xfs_attr_leaf_name_remote *name_rmt;
2654 	struct xfs_buf		*bp;
2655 	int			error;
2656 #ifdef DEBUG
2657 	struct xfs_attr3_icleaf_hdr ichdr;
2658 	xfs_attr_leaf_name_local_t *name_loc;
2659 	int namelen;
2660 	char *name;
2661 #endif /* DEBUG */
2662 
2663 	trace_xfs_attr_leaf_clearflag(args);
2664 	/*
2665 	 * Set up the operation.
2666 	 */
2667 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2668 	if (error)
2669 		return error;
2670 
2671 	leaf = bp->b_addr;
2672 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2673 	ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2674 
2675 #ifdef DEBUG
2676 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2677 	ASSERT(args->index < ichdr.count);
2678 	ASSERT(args->index >= 0);
2679 
2680 	if (entry->flags & XFS_ATTR_LOCAL) {
2681 		name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2682 		namelen = name_loc->namelen;
2683 		name = (char *)name_loc->nameval;
2684 	} else {
2685 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2686 		namelen = name_rmt->namelen;
2687 		name = (char *)name_rmt->name;
2688 	}
2689 	ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2690 	ASSERT(namelen == args->namelen);
2691 	ASSERT(memcmp(name, args->name, namelen) == 0);
2692 #endif /* DEBUG */
2693 
2694 	entry->flags &= ~XFS_ATTR_INCOMPLETE;
2695 	xfs_trans_log_buf(args->trans, bp,
2696 			 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2697 
2698 	if (args->rmtblkno) {
2699 		ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2700 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2701 		name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2702 		name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2703 		xfs_trans_log_buf(args->trans, bp,
2704 			 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2705 	}
2706 
2707 	/*
2708 	 * Commit the flag value change and start the next trans in series.
2709 	 */
2710 	return xfs_trans_roll_inode(&args->trans, args->dp);
2711 }
2712 
2713 /*
2714  * Set the INCOMPLETE flag on an entry in a leaf block.
2715  */
2716 int
2717 xfs_attr3_leaf_setflag(
2718 	struct xfs_da_args	*args)
2719 {
2720 	struct xfs_attr_leafblock *leaf;
2721 	struct xfs_attr_leaf_entry *entry;
2722 	struct xfs_attr_leaf_name_remote *name_rmt;
2723 	struct xfs_buf		*bp;
2724 	int error;
2725 #ifdef DEBUG
2726 	struct xfs_attr3_icleaf_hdr ichdr;
2727 #endif
2728 
2729 	trace_xfs_attr_leaf_setflag(args);
2730 
2731 	/*
2732 	 * Set up the operation.
2733 	 */
2734 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2735 	if (error)
2736 		return error;
2737 
2738 	leaf = bp->b_addr;
2739 #ifdef DEBUG
2740 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2741 	ASSERT(args->index < ichdr.count);
2742 	ASSERT(args->index >= 0);
2743 #endif
2744 	entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2745 
2746 	ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2747 	entry->flags |= XFS_ATTR_INCOMPLETE;
2748 	xfs_trans_log_buf(args->trans, bp,
2749 			XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2750 	if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2751 		name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2752 		name_rmt->valueblk = 0;
2753 		name_rmt->valuelen = 0;
2754 		xfs_trans_log_buf(args->trans, bp,
2755 			 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2756 	}
2757 
2758 	/*
2759 	 * Commit the flag value change and start the next trans in series.
2760 	 */
2761 	return xfs_trans_roll_inode(&args->trans, args->dp);
2762 }
2763 
2764 /*
2765  * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2766  * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2767  * entry given by args->blkno2/index2.
2768  *
2769  * Note that they could be in different blocks, or in the same block.
2770  */
2771 int
2772 xfs_attr3_leaf_flipflags(
2773 	struct xfs_da_args	*args)
2774 {
2775 	struct xfs_attr_leafblock *leaf1;
2776 	struct xfs_attr_leafblock *leaf2;
2777 	struct xfs_attr_leaf_entry *entry1;
2778 	struct xfs_attr_leaf_entry *entry2;
2779 	struct xfs_attr_leaf_name_remote *name_rmt;
2780 	struct xfs_buf		*bp1;
2781 	struct xfs_buf		*bp2;
2782 	int error;
2783 #ifdef DEBUG
2784 	struct xfs_attr3_icleaf_hdr ichdr1;
2785 	struct xfs_attr3_icleaf_hdr ichdr2;
2786 	xfs_attr_leaf_name_local_t *name_loc;
2787 	int namelen1, namelen2;
2788 	char *name1, *name2;
2789 #endif /* DEBUG */
2790 
2791 	trace_xfs_attr_leaf_flipflags(args);
2792 
2793 	/*
2794 	 * Read the block containing the "old" attr
2795 	 */
2796 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2797 	if (error)
2798 		return error;
2799 
2800 	/*
2801 	 * Read the block containing the "new" attr, if it is different
2802 	 */
2803 	if (args->blkno2 != args->blkno) {
2804 		error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
2805 					   -1, &bp2);
2806 		if (error)
2807 			return error;
2808 	} else {
2809 		bp2 = bp1;
2810 	}
2811 
2812 	leaf1 = bp1->b_addr;
2813 	entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
2814 
2815 	leaf2 = bp2->b_addr;
2816 	entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
2817 
2818 #ifdef DEBUG
2819 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
2820 	ASSERT(args->index < ichdr1.count);
2821 	ASSERT(args->index >= 0);
2822 
2823 	xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
2824 	ASSERT(args->index2 < ichdr2.count);
2825 	ASSERT(args->index2 >= 0);
2826 
2827 	if (entry1->flags & XFS_ATTR_LOCAL) {
2828 		name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
2829 		namelen1 = name_loc->namelen;
2830 		name1 = (char *)name_loc->nameval;
2831 	} else {
2832 		name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2833 		namelen1 = name_rmt->namelen;
2834 		name1 = (char *)name_rmt->name;
2835 	}
2836 	if (entry2->flags & XFS_ATTR_LOCAL) {
2837 		name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
2838 		namelen2 = name_loc->namelen;
2839 		name2 = (char *)name_loc->nameval;
2840 	} else {
2841 		name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2842 		namelen2 = name_rmt->namelen;
2843 		name2 = (char *)name_rmt->name;
2844 	}
2845 	ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2846 	ASSERT(namelen1 == namelen2);
2847 	ASSERT(memcmp(name1, name2, namelen1) == 0);
2848 #endif /* DEBUG */
2849 
2850 	ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2851 	ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2852 
2853 	entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2854 	xfs_trans_log_buf(args->trans, bp1,
2855 			  XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2856 	if (args->rmtblkno) {
2857 		ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2858 		name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2859 		name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2860 		name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2861 		xfs_trans_log_buf(args->trans, bp1,
2862 			 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2863 	}
2864 
2865 	entry2->flags |= XFS_ATTR_INCOMPLETE;
2866 	xfs_trans_log_buf(args->trans, bp2,
2867 			  XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2868 	if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2869 		name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2870 		name_rmt->valueblk = 0;
2871 		name_rmt->valuelen = 0;
2872 		xfs_trans_log_buf(args->trans, bp2,
2873 			 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2874 	}
2875 
2876 	/*
2877 	 * Commit the flag value change and start the next trans in series.
2878 	 */
2879 	error = xfs_trans_roll_inode(&args->trans, args->dp);
2880 
2881 	return error;
2882 }
2883