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