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