xref: /openbmc/linux/fs/xfs/libxfs/xfs_attr.c (revision c8dbaa22)
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_bit.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_attr_sf.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
34 #include "xfs_bmap.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
37 #include "xfs_attr.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_error.h"
41 #include "xfs_quota.h"
42 #include "xfs_trans_space.h"
43 #include "xfs_trace.h"
44 
45 /*
46  * xfs_attr.c
47  *
48  * Provide the external interfaces to manage attribute lists.
49  */
50 
51 /*========================================================================
52  * Function prototypes for the kernel.
53  *========================================================================*/
54 
55 /*
56  * Internal routines when attribute list fits inside the inode.
57  */
58 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
59 
60 /*
61  * Internal routines when attribute list is one block.
62  */
63 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
64 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
66 
67 /*
68  * Internal routines when attribute list is more than one block.
69  */
70 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
71 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
72 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
73 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
74 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
75 
76 
77 STATIC int
78 xfs_attr_args_init(
79 	struct xfs_da_args	*args,
80 	struct xfs_inode	*dp,
81 	const unsigned char	*name,
82 	int			flags)
83 {
84 
85 	if (!name)
86 		return -EINVAL;
87 
88 	memset(args, 0, sizeof(*args));
89 	args->geo = dp->i_mount->m_attr_geo;
90 	args->whichfork = XFS_ATTR_FORK;
91 	args->dp = dp;
92 	args->flags = flags;
93 	args->name = name;
94 	args->namelen = strlen((const char *)name);
95 	if (args->namelen >= MAXNAMELEN)
96 		return -EFAULT;		/* match IRIX behaviour */
97 
98 	args->hashval = xfs_da_hashname(args->name, args->namelen);
99 	return 0;
100 }
101 
102 int
103 xfs_inode_hasattr(
104 	struct xfs_inode	*ip)
105 {
106 	if (!XFS_IFORK_Q(ip) ||
107 	    (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108 	     ip->i_d.di_anextents == 0))
109 		return 0;
110 	return 1;
111 }
112 
113 /*========================================================================
114  * Overall external interface routines.
115  *========================================================================*/
116 
117 /* Retrieve an extended attribute and its value.  Must have ilock. */
118 int
119 xfs_attr_get_ilocked(
120 	struct xfs_inode	*ip,
121 	struct xfs_da_args	*args)
122 {
123 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
124 
125 	if (!xfs_inode_hasattr(ip))
126 		return -ENOATTR;
127 	else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
128 		return xfs_attr_shortform_getvalue(args);
129 	else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
130 		return xfs_attr_leaf_get(args);
131 	else
132 		return xfs_attr_node_get(args);
133 }
134 
135 /* Retrieve an extended attribute by name, and its value. */
136 int
137 xfs_attr_get(
138 	struct xfs_inode	*ip,
139 	const unsigned char	*name,
140 	unsigned char		*value,
141 	int			*valuelenp,
142 	int			flags)
143 {
144 	struct xfs_da_args	args;
145 	uint			lock_mode;
146 	int			error;
147 
148 	XFS_STATS_INC(ip->i_mount, xs_attr_get);
149 
150 	if (XFS_FORCED_SHUTDOWN(ip->i_mount))
151 		return -EIO;
152 
153 	error = xfs_attr_args_init(&args, ip, name, flags);
154 	if (error)
155 		return error;
156 
157 	args.value = value;
158 	args.valuelen = *valuelenp;
159 	/* Entirely possible to look up a name which doesn't exist */
160 	args.op_flags = XFS_DA_OP_OKNOENT;
161 
162 	lock_mode = xfs_ilock_attr_map_shared(ip);
163 	error = xfs_attr_get_ilocked(ip, &args);
164 	xfs_iunlock(ip, lock_mode);
165 
166 	*valuelenp = args.valuelen;
167 	return error == -EEXIST ? 0 : error;
168 }
169 
170 /*
171  * Calculate how many blocks we need for the new attribute,
172  */
173 STATIC int
174 xfs_attr_calc_size(
175 	struct xfs_da_args	*args,
176 	int			*local)
177 {
178 	struct xfs_mount	*mp = args->dp->i_mount;
179 	int			size;
180 	int			nblks;
181 
182 	/*
183 	 * Determine space new attribute will use, and if it would be
184 	 * "local" or "remote" (note: local != inline).
185 	 */
186 	size = xfs_attr_leaf_newentsize(args, local);
187 	nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
188 	if (*local) {
189 		if (size > (args->geo->blksize / 2)) {
190 			/* Double split possible */
191 			nblks *= 2;
192 		}
193 	} else {
194 		/*
195 		 * Out of line attribute, cannot double split, but
196 		 * make room for the attribute value itself.
197 		 */
198 		uint	dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
199 		nblks += dblocks;
200 		nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
201 	}
202 
203 	return nblks;
204 }
205 
206 int
207 xfs_attr_set(
208 	struct xfs_inode	*dp,
209 	const unsigned char	*name,
210 	unsigned char		*value,
211 	int			valuelen,
212 	int			flags)
213 {
214 	struct xfs_mount	*mp = dp->i_mount;
215 	struct xfs_da_args	args;
216 	struct xfs_defer_ops	dfops;
217 	struct xfs_trans_res	tres;
218 	xfs_fsblock_t		firstblock;
219 	int			rsvd = (flags & ATTR_ROOT) != 0;
220 	int			error, err2, local;
221 
222 	XFS_STATS_INC(mp, xs_attr_set);
223 
224 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
225 		return -EIO;
226 
227 	error = xfs_attr_args_init(&args, dp, name, flags);
228 	if (error)
229 		return error;
230 
231 	args.value = value;
232 	args.valuelen = valuelen;
233 	args.firstblock = &firstblock;
234 	args.dfops = &dfops;
235 	args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
236 	args.total = xfs_attr_calc_size(&args, &local);
237 
238 	error = xfs_qm_dqattach(dp, 0);
239 	if (error)
240 		return error;
241 
242 	/*
243 	 * If the inode doesn't have an attribute fork, add one.
244 	 * (inode must not be locked when we call this routine)
245 	 */
246 	if (XFS_IFORK_Q(dp) == 0) {
247 		int sf_size = sizeof(xfs_attr_sf_hdr_t) +
248 			XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
249 
250 		error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
251 		if (error)
252 			return error;
253 	}
254 
255 	tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
256 			 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
257 	tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
258 	tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
259 
260 	/*
261 	 * Root fork attributes can use reserved data blocks for this
262 	 * operation if necessary
263 	 */
264 	error = xfs_trans_alloc(mp, &tres, args.total, 0,
265 			rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
266 	if (error)
267 		return error;
268 
269 	xfs_ilock(dp, XFS_ILOCK_EXCL);
270 	error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
271 				rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
272 				       XFS_QMOPT_RES_REGBLKS);
273 	if (error) {
274 		xfs_iunlock(dp, XFS_ILOCK_EXCL);
275 		xfs_trans_cancel(args.trans);
276 		return error;
277 	}
278 
279 	xfs_trans_ijoin(args.trans, dp, 0);
280 
281 	/*
282 	 * If the attribute list is non-existent or a shortform list,
283 	 * upgrade it to a single-leaf-block attribute list.
284 	 */
285 	if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
286 	    (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
287 	     dp->i_d.di_anextents == 0)) {
288 
289 		/*
290 		 * Build initial attribute list (if required).
291 		 */
292 		if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
293 			xfs_attr_shortform_create(&args);
294 
295 		/*
296 		 * Try to add the attr to the attribute list in
297 		 * the inode.
298 		 */
299 		error = xfs_attr_shortform_addname(&args);
300 		if (error != -ENOSPC) {
301 			/*
302 			 * Commit the shortform mods, and we're done.
303 			 * NOTE: this is also the error path (EEXIST, etc).
304 			 */
305 			ASSERT(args.trans != NULL);
306 
307 			/*
308 			 * If this is a synchronous mount, make sure that
309 			 * the transaction goes to disk before returning
310 			 * to the user.
311 			 */
312 			if (mp->m_flags & XFS_MOUNT_WSYNC)
313 				xfs_trans_set_sync(args.trans);
314 
315 			if (!error && (flags & ATTR_KERNOTIME) == 0) {
316 				xfs_trans_ichgtime(args.trans, dp,
317 							XFS_ICHGTIME_CHG);
318 			}
319 			err2 = xfs_trans_commit(args.trans);
320 			xfs_iunlock(dp, XFS_ILOCK_EXCL);
321 
322 			return error ? error : err2;
323 		}
324 
325 		/*
326 		 * It won't fit in the shortform, transform to a leaf block.
327 		 * GROT: another possible req'mt for a double-split btree op.
328 		 */
329 		xfs_defer_init(args.dfops, args.firstblock);
330 		error = xfs_attr_shortform_to_leaf(&args);
331 		if (!error)
332 			error = xfs_defer_finish(&args.trans, args.dfops, dp);
333 		if (error) {
334 			args.trans = NULL;
335 			xfs_defer_cancel(&dfops);
336 			goto out;
337 		}
338 
339 		/*
340 		 * Commit the leaf transformation.  We'll need another (linked)
341 		 * transaction to add the new attribute to the leaf.
342 		 */
343 
344 		error = xfs_trans_roll(&args.trans, dp);
345 		if (error)
346 			goto out;
347 
348 	}
349 
350 	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
351 		error = xfs_attr_leaf_addname(&args);
352 	else
353 		error = xfs_attr_node_addname(&args);
354 	if (error)
355 		goto out;
356 
357 	/*
358 	 * If this is a synchronous mount, make sure that the
359 	 * transaction goes to disk before returning to the user.
360 	 */
361 	if (mp->m_flags & XFS_MOUNT_WSYNC)
362 		xfs_trans_set_sync(args.trans);
363 
364 	if ((flags & ATTR_KERNOTIME) == 0)
365 		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
366 
367 	/*
368 	 * Commit the last in the sequence of transactions.
369 	 */
370 	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
371 	error = xfs_trans_commit(args.trans);
372 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
373 
374 	return error;
375 
376 out:
377 	if (args.trans)
378 		xfs_trans_cancel(args.trans);
379 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
380 	return error;
381 }
382 
383 /*
384  * Generic handler routine to remove a name from an attribute list.
385  * Transitions attribute list from Btree to shortform as necessary.
386  */
387 int
388 xfs_attr_remove(
389 	struct xfs_inode	*dp,
390 	const unsigned char	*name,
391 	int			flags)
392 {
393 	struct xfs_mount	*mp = dp->i_mount;
394 	struct xfs_da_args	args;
395 	struct xfs_defer_ops	dfops;
396 	xfs_fsblock_t		firstblock;
397 	int			error;
398 
399 	XFS_STATS_INC(mp, xs_attr_remove);
400 
401 	if (XFS_FORCED_SHUTDOWN(dp->i_mount))
402 		return -EIO;
403 
404 	error = xfs_attr_args_init(&args, dp, name, flags);
405 	if (error)
406 		return error;
407 
408 	args.firstblock = &firstblock;
409 	args.dfops = &dfops;
410 
411 	/*
412 	 * we have no control over the attribute names that userspace passes us
413 	 * to remove, so we have to allow the name lookup prior to attribute
414 	 * removal to fail.
415 	 */
416 	args.op_flags = XFS_DA_OP_OKNOENT;
417 
418 	error = xfs_qm_dqattach(dp, 0);
419 	if (error)
420 		return error;
421 
422 	/*
423 	 * Root fork attributes can use reserved data blocks for this
424 	 * operation if necessary
425 	 */
426 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
427 			XFS_ATTRRM_SPACE_RES(mp), 0,
428 			(flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
429 			&args.trans);
430 	if (error)
431 		return error;
432 
433 	xfs_ilock(dp, XFS_ILOCK_EXCL);
434 	/*
435 	 * No need to make quota reservations here. We expect to release some
436 	 * blocks not allocate in the common case.
437 	 */
438 	xfs_trans_ijoin(args.trans, dp, 0);
439 
440 	if (!xfs_inode_hasattr(dp)) {
441 		error = -ENOATTR;
442 	} else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
443 		ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
444 		error = xfs_attr_shortform_remove(&args);
445 	} else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
446 		error = xfs_attr_leaf_removename(&args);
447 	} else {
448 		error = xfs_attr_node_removename(&args);
449 	}
450 
451 	if (error)
452 		goto out;
453 
454 	/*
455 	 * If this is a synchronous mount, make sure that the
456 	 * transaction goes to disk before returning to the user.
457 	 */
458 	if (mp->m_flags & XFS_MOUNT_WSYNC)
459 		xfs_trans_set_sync(args.trans);
460 
461 	if ((flags & ATTR_KERNOTIME) == 0)
462 		xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
463 
464 	/*
465 	 * Commit the last in the sequence of transactions.
466 	 */
467 	xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
468 	error = xfs_trans_commit(args.trans);
469 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
470 
471 	return error;
472 
473 out:
474 	if (args.trans)
475 		xfs_trans_cancel(args.trans);
476 	xfs_iunlock(dp, XFS_ILOCK_EXCL);
477 	return error;
478 }
479 
480 /*========================================================================
481  * External routines when attribute list is inside the inode
482  *========================================================================*/
483 
484 /*
485  * Add a name to the shortform attribute list structure
486  * This is the external routine.
487  */
488 STATIC int
489 xfs_attr_shortform_addname(xfs_da_args_t *args)
490 {
491 	int newsize, forkoff, retval;
492 
493 	trace_xfs_attr_sf_addname(args);
494 
495 	retval = xfs_attr_shortform_lookup(args);
496 	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
497 		return retval;
498 	} else if (retval == -EEXIST) {
499 		if (args->flags & ATTR_CREATE)
500 			return retval;
501 		retval = xfs_attr_shortform_remove(args);
502 		ASSERT(retval == 0);
503 	}
504 
505 	if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
506 	    args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
507 		return -ENOSPC;
508 
509 	newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
510 	newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
511 
512 	forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
513 	if (!forkoff)
514 		return -ENOSPC;
515 
516 	xfs_attr_shortform_add(args, forkoff);
517 	return 0;
518 }
519 
520 
521 /*========================================================================
522  * External routines when attribute list is one block
523  *========================================================================*/
524 
525 /*
526  * Add a name to the leaf attribute list structure
527  *
528  * This leaf block cannot have a "remote" value, we only call this routine
529  * if bmap_one_block() says there is only one block (ie: no remote blks).
530  */
531 STATIC int
532 xfs_attr_leaf_addname(xfs_da_args_t *args)
533 {
534 	xfs_inode_t *dp;
535 	struct xfs_buf *bp;
536 	int retval, error, forkoff;
537 
538 	trace_xfs_attr_leaf_addname(args);
539 
540 	/*
541 	 * Read the (only) block in the attribute list in.
542 	 */
543 	dp = args->dp;
544 	args->blkno = 0;
545 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
546 	if (error)
547 		return error;
548 
549 	/*
550 	 * Look up the given attribute in the leaf block.  Figure out if
551 	 * the given flags produce an error or call for an atomic rename.
552 	 */
553 	retval = xfs_attr3_leaf_lookup_int(bp, args);
554 	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
555 		xfs_trans_brelse(args->trans, bp);
556 		return retval;
557 	} else if (retval == -EEXIST) {
558 		if (args->flags & ATTR_CREATE) {	/* pure create op */
559 			xfs_trans_brelse(args->trans, bp);
560 			return retval;
561 		}
562 
563 		trace_xfs_attr_leaf_replace(args);
564 
565 		/* save the attribute state for later removal*/
566 		args->op_flags |= XFS_DA_OP_RENAME;	/* an atomic rename */
567 		args->blkno2 = args->blkno;		/* set 2nd entry info*/
568 		args->index2 = args->index;
569 		args->rmtblkno2 = args->rmtblkno;
570 		args->rmtblkcnt2 = args->rmtblkcnt;
571 		args->rmtvaluelen2 = args->rmtvaluelen;
572 
573 		/*
574 		 * clear the remote attr state now that it is saved so that the
575 		 * values reflect the state of the attribute we are about to
576 		 * add, not the attribute we just found and will remove later.
577 		 */
578 		args->rmtblkno = 0;
579 		args->rmtblkcnt = 0;
580 		args->rmtvaluelen = 0;
581 	}
582 
583 	/*
584 	 * Add the attribute to the leaf block, transitioning to a Btree
585 	 * if required.
586 	 */
587 	retval = xfs_attr3_leaf_add(bp, args);
588 	if (retval == -ENOSPC) {
589 		/*
590 		 * Promote the attribute list to the Btree format, then
591 		 * Commit that transaction so that the node_addname() call
592 		 * can manage its own transactions.
593 		 */
594 		xfs_defer_init(args->dfops, args->firstblock);
595 		error = xfs_attr3_leaf_to_node(args);
596 		if (!error)
597 			error = xfs_defer_finish(&args->trans, args->dfops, dp);
598 		if (error) {
599 			args->trans = NULL;
600 			xfs_defer_cancel(args->dfops);
601 			return error;
602 		}
603 
604 		/*
605 		 * Commit the current trans (including the inode) and start
606 		 * a new one.
607 		 */
608 		error = xfs_trans_roll(&args->trans, dp);
609 		if (error)
610 			return error;
611 
612 		/*
613 		 * Fob the whole rest of the problem off on the Btree code.
614 		 */
615 		error = xfs_attr_node_addname(args);
616 		return error;
617 	}
618 
619 	/*
620 	 * Commit the transaction that added the attr name so that
621 	 * later routines can manage their own transactions.
622 	 */
623 	error = xfs_trans_roll(&args->trans, dp);
624 	if (error)
625 		return error;
626 
627 	/*
628 	 * If there was an out-of-line value, allocate the blocks we
629 	 * identified for its storage and copy the value.  This is done
630 	 * after we create the attribute so that we don't overflow the
631 	 * maximum size of a transaction and/or hit a deadlock.
632 	 */
633 	if (args->rmtblkno > 0) {
634 		error = xfs_attr_rmtval_set(args);
635 		if (error)
636 			return error;
637 	}
638 
639 	/*
640 	 * If this is an atomic rename operation, we must "flip" the
641 	 * incomplete flags on the "new" and "old" attribute/value pairs
642 	 * so that one disappears and one appears atomically.  Then we
643 	 * must remove the "old" attribute/value pair.
644 	 */
645 	if (args->op_flags & XFS_DA_OP_RENAME) {
646 		/*
647 		 * In a separate transaction, set the incomplete flag on the
648 		 * "old" attr and clear the incomplete flag on the "new" attr.
649 		 */
650 		error = xfs_attr3_leaf_flipflags(args);
651 		if (error)
652 			return error;
653 
654 		/*
655 		 * Dismantle the "old" attribute/value pair by removing
656 		 * a "remote" value (if it exists).
657 		 */
658 		args->index = args->index2;
659 		args->blkno = args->blkno2;
660 		args->rmtblkno = args->rmtblkno2;
661 		args->rmtblkcnt = args->rmtblkcnt2;
662 		args->rmtvaluelen = args->rmtvaluelen2;
663 		if (args->rmtblkno) {
664 			error = xfs_attr_rmtval_remove(args);
665 			if (error)
666 				return error;
667 		}
668 
669 		/*
670 		 * Read in the block containing the "old" attr, then
671 		 * remove the "old" attr from that block (neat, huh!)
672 		 */
673 		error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
674 					   -1, &bp);
675 		if (error)
676 			return error;
677 
678 		xfs_attr3_leaf_remove(bp, args);
679 
680 		/*
681 		 * If the result is small enough, shrink it all into the inode.
682 		 */
683 		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
684 			xfs_defer_init(args->dfops, args->firstblock);
685 			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
686 			/* bp is gone due to xfs_da_shrink_inode */
687 			if (!error)
688 				error = xfs_defer_finish(&args->trans,
689 							args->dfops, dp);
690 			if (error) {
691 				args->trans = NULL;
692 				xfs_defer_cancel(args->dfops);
693 				return error;
694 			}
695 		}
696 
697 		/*
698 		 * Commit the remove and start the next trans in series.
699 		 */
700 		error = xfs_trans_roll(&args->trans, dp);
701 
702 	} else if (args->rmtblkno > 0) {
703 		/*
704 		 * Added a "remote" value, just clear the incomplete flag.
705 		 */
706 		error = xfs_attr3_leaf_clearflag(args);
707 	}
708 	return error;
709 }
710 
711 /*
712  * Remove a name from the leaf attribute list structure
713  *
714  * This leaf block cannot have a "remote" value, we only call this routine
715  * if bmap_one_block() says there is only one block (ie: no remote blks).
716  */
717 STATIC int
718 xfs_attr_leaf_removename(xfs_da_args_t *args)
719 {
720 	xfs_inode_t *dp;
721 	struct xfs_buf *bp;
722 	int error, forkoff;
723 
724 	trace_xfs_attr_leaf_removename(args);
725 
726 	/*
727 	 * Remove the attribute.
728 	 */
729 	dp = args->dp;
730 	args->blkno = 0;
731 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
732 	if (error)
733 		return error;
734 
735 	error = xfs_attr3_leaf_lookup_int(bp, args);
736 	if (error == -ENOATTR) {
737 		xfs_trans_brelse(args->trans, bp);
738 		return error;
739 	}
740 
741 	xfs_attr3_leaf_remove(bp, args);
742 
743 	/*
744 	 * If the result is small enough, shrink it all into the inode.
745 	 */
746 	if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
747 		xfs_defer_init(args->dfops, args->firstblock);
748 		error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
749 		/* bp is gone due to xfs_da_shrink_inode */
750 		if (!error)
751 			error = xfs_defer_finish(&args->trans, args->dfops, dp);
752 		if (error) {
753 			args->trans = NULL;
754 			xfs_defer_cancel(args->dfops);
755 			return error;
756 		}
757 	}
758 	return 0;
759 }
760 
761 /*
762  * Look up a name in a leaf attribute list structure.
763  *
764  * This leaf block cannot have a "remote" value, we only call this routine
765  * if bmap_one_block() says there is only one block (ie: no remote blks).
766  */
767 STATIC int
768 xfs_attr_leaf_get(xfs_da_args_t *args)
769 {
770 	struct xfs_buf *bp;
771 	int error;
772 
773 	trace_xfs_attr_leaf_get(args);
774 
775 	args->blkno = 0;
776 	error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
777 	if (error)
778 		return error;
779 
780 	error = xfs_attr3_leaf_lookup_int(bp, args);
781 	if (error != -EEXIST)  {
782 		xfs_trans_brelse(args->trans, bp);
783 		return error;
784 	}
785 	error = xfs_attr3_leaf_getvalue(bp, args);
786 	xfs_trans_brelse(args->trans, bp);
787 	if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
788 		error = xfs_attr_rmtval_get(args);
789 	}
790 	return error;
791 }
792 
793 /*========================================================================
794  * External routines when attribute list size > geo->blksize
795  *========================================================================*/
796 
797 /*
798  * Add a name to a Btree-format attribute list.
799  *
800  * This will involve walking down the Btree, and may involve splitting
801  * leaf nodes and even splitting intermediate nodes up to and including
802  * the root node (a special case of an intermediate node).
803  *
804  * "Remote" attribute values confuse the issue and atomic rename operations
805  * add a whole extra layer of confusion on top of that.
806  */
807 STATIC int
808 xfs_attr_node_addname(xfs_da_args_t *args)
809 {
810 	xfs_da_state_t *state;
811 	xfs_da_state_blk_t *blk;
812 	xfs_inode_t *dp;
813 	xfs_mount_t *mp;
814 	int retval, error;
815 
816 	trace_xfs_attr_node_addname(args);
817 
818 	/*
819 	 * Fill in bucket of arguments/results/context to carry around.
820 	 */
821 	dp = args->dp;
822 	mp = dp->i_mount;
823 restart:
824 	state = xfs_da_state_alloc();
825 	state->args = args;
826 	state->mp = mp;
827 
828 	/*
829 	 * Search to see if name already exists, and get back a pointer
830 	 * to where it should go.
831 	 */
832 	error = xfs_da3_node_lookup_int(state, &retval);
833 	if (error)
834 		goto out;
835 	blk = &state->path.blk[ state->path.active-1 ];
836 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
837 	if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
838 		goto out;
839 	} else if (retval == -EEXIST) {
840 		if (args->flags & ATTR_CREATE)
841 			goto out;
842 
843 		trace_xfs_attr_node_replace(args);
844 
845 		/* save the attribute state for later removal*/
846 		args->op_flags |= XFS_DA_OP_RENAME;	/* atomic rename op */
847 		args->blkno2 = args->blkno;		/* set 2nd entry info*/
848 		args->index2 = args->index;
849 		args->rmtblkno2 = args->rmtblkno;
850 		args->rmtblkcnt2 = args->rmtblkcnt;
851 		args->rmtvaluelen2 = args->rmtvaluelen;
852 
853 		/*
854 		 * clear the remote attr state now that it is saved so that the
855 		 * values reflect the state of the attribute we are about to
856 		 * add, not the attribute we just found and will remove later.
857 		 */
858 		args->rmtblkno = 0;
859 		args->rmtblkcnt = 0;
860 		args->rmtvaluelen = 0;
861 	}
862 
863 	retval = xfs_attr3_leaf_add(blk->bp, state->args);
864 	if (retval == -ENOSPC) {
865 		if (state->path.active == 1) {
866 			/*
867 			 * Its really a single leaf node, but it had
868 			 * out-of-line values so it looked like it *might*
869 			 * have been a b-tree.
870 			 */
871 			xfs_da_state_free(state);
872 			state = NULL;
873 			xfs_defer_init(args->dfops, args->firstblock);
874 			error = xfs_attr3_leaf_to_node(args);
875 			if (!error)
876 				error = xfs_defer_finish(&args->trans,
877 							args->dfops, dp);
878 			if (error) {
879 				args->trans = NULL;
880 				xfs_defer_cancel(args->dfops);
881 				goto out;
882 			}
883 
884 			/*
885 			 * Commit the node conversion and start the next
886 			 * trans in the chain.
887 			 */
888 			error = xfs_trans_roll(&args->trans, dp);
889 			if (error)
890 				goto out;
891 
892 			goto restart;
893 		}
894 
895 		/*
896 		 * Split as many Btree elements as required.
897 		 * This code tracks the new and old attr's location
898 		 * in the index/blkno/rmtblkno/rmtblkcnt fields and
899 		 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
900 		 */
901 		xfs_defer_init(args->dfops, args->firstblock);
902 		error = xfs_da3_split(state);
903 		if (!error)
904 			error = xfs_defer_finish(&args->trans, args->dfops, dp);
905 		if (error) {
906 			args->trans = NULL;
907 			xfs_defer_cancel(args->dfops);
908 			goto out;
909 		}
910 	} else {
911 		/*
912 		 * Addition succeeded, update Btree hashvals.
913 		 */
914 		xfs_da3_fixhashpath(state, &state->path);
915 	}
916 
917 	/*
918 	 * Kill the state structure, we're done with it and need to
919 	 * allow the buffers to come back later.
920 	 */
921 	xfs_da_state_free(state);
922 	state = NULL;
923 
924 	/*
925 	 * Commit the leaf addition or btree split and start the next
926 	 * trans in the chain.
927 	 */
928 	error = xfs_trans_roll(&args->trans, dp);
929 	if (error)
930 		goto out;
931 
932 	/*
933 	 * If there was an out-of-line value, allocate the blocks we
934 	 * identified for its storage and copy the value.  This is done
935 	 * after we create the attribute so that we don't overflow the
936 	 * maximum size of a transaction and/or hit a deadlock.
937 	 */
938 	if (args->rmtblkno > 0) {
939 		error = xfs_attr_rmtval_set(args);
940 		if (error)
941 			return error;
942 	}
943 
944 	/*
945 	 * If this is an atomic rename operation, we must "flip" the
946 	 * incomplete flags on the "new" and "old" attribute/value pairs
947 	 * so that one disappears and one appears atomically.  Then we
948 	 * must remove the "old" attribute/value pair.
949 	 */
950 	if (args->op_flags & XFS_DA_OP_RENAME) {
951 		/*
952 		 * In a separate transaction, set the incomplete flag on the
953 		 * "old" attr and clear the incomplete flag on the "new" attr.
954 		 */
955 		error = xfs_attr3_leaf_flipflags(args);
956 		if (error)
957 			goto out;
958 
959 		/*
960 		 * Dismantle the "old" attribute/value pair by removing
961 		 * a "remote" value (if it exists).
962 		 */
963 		args->index = args->index2;
964 		args->blkno = args->blkno2;
965 		args->rmtblkno = args->rmtblkno2;
966 		args->rmtblkcnt = args->rmtblkcnt2;
967 		args->rmtvaluelen = args->rmtvaluelen2;
968 		if (args->rmtblkno) {
969 			error = xfs_attr_rmtval_remove(args);
970 			if (error)
971 				return error;
972 		}
973 
974 		/*
975 		 * Re-find the "old" attribute entry after any split ops.
976 		 * The INCOMPLETE flag means that we will find the "old"
977 		 * attr, not the "new" one.
978 		 */
979 		args->flags |= XFS_ATTR_INCOMPLETE;
980 		state = xfs_da_state_alloc();
981 		state->args = args;
982 		state->mp = mp;
983 		state->inleaf = 0;
984 		error = xfs_da3_node_lookup_int(state, &retval);
985 		if (error)
986 			goto out;
987 
988 		/*
989 		 * Remove the name and update the hashvals in the tree.
990 		 */
991 		blk = &state->path.blk[ state->path.active-1 ];
992 		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
993 		error = xfs_attr3_leaf_remove(blk->bp, args);
994 		xfs_da3_fixhashpath(state, &state->path);
995 
996 		/*
997 		 * Check to see if the tree needs to be collapsed.
998 		 */
999 		if (retval && (state->path.active > 1)) {
1000 			xfs_defer_init(args->dfops, args->firstblock);
1001 			error = xfs_da3_join(state);
1002 			if (!error)
1003 				error = xfs_defer_finish(&args->trans,
1004 							args->dfops, dp);
1005 			if (error) {
1006 				args->trans = NULL;
1007 				xfs_defer_cancel(args->dfops);
1008 				goto out;
1009 			}
1010 		}
1011 
1012 		/*
1013 		 * Commit and start the next trans in the chain.
1014 		 */
1015 		error = xfs_trans_roll(&args->trans, dp);
1016 		if (error)
1017 			goto out;
1018 
1019 	} else if (args->rmtblkno > 0) {
1020 		/*
1021 		 * Added a "remote" value, just clear the incomplete flag.
1022 		 */
1023 		error = xfs_attr3_leaf_clearflag(args);
1024 		if (error)
1025 			goto out;
1026 	}
1027 	retval = error = 0;
1028 
1029 out:
1030 	if (state)
1031 		xfs_da_state_free(state);
1032 	if (error)
1033 		return error;
1034 	return retval;
1035 }
1036 
1037 /*
1038  * Remove a name from a B-tree attribute list.
1039  *
1040  * This will involve walking down the Btree, and may involve joining
1041  * leaf nodes and even joining intermediate nodes up to and including
1042  * the root node (a special case of an intermediate node).
1043  */
1044 STATIC int
1045 xfs_attr_node_removename(xfs_da_args_t *args)
1046 {
1047 	xfs_da_state_t *state;
1048 	xfs_da_state_blk_t *blk;
1049 	xfs_inode_t *dp;
1050 	struct xfs_buf *bp;
1051 	int retval, error, forkoff;
1052 
1053 	trace_xfs_attr_node_removename(args);
1054 
1055 	/*
1056 	 * Tie a string around our finger to remind us where we are.
1057 	 */
1058 	dp = args->dp;
1059 	state = xfs_da_state_alloc();
1060 	state->args = args;
1061 	state->mp = dp->i_mount;
1062 
1063 	/*
1064 	 * Search to see if name exists, and get back a pointer to it.
1065 	 */
1066 	error = xfs_da3_node_lookup_int(state, &retval);
1067 	if (error || (retval != -EEXIST)) {
1068 		if (error == 0)
1069 			error = retval;
1070 		goto out;
1071 	}
1072 
1073 	/*
1074 	 * If there is an out-of-line value, de-allocate the blocks.
1075 	 * This is done before we remove the attribute so that we don't
1076 	 * overflow the maximum size of a transaction and/or hit a deadlock.
1077 	 */
1078 	blk = &state->path.blk[ state->path.active-1 ];
1079 	ASSERT(blk->bp != NULL);
1080 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1081 	if (args->rmtblkno > 0) {
1082 		/*
1083 		 * Fill in disk block numbers in the state structure
1084 		 * so that we can get the buffers back after we commit
1085 		 * several transactions in the following calls.
1086 		 */
1087 		error = xfs_attr_fillstate(state);
1088 		if (error)
1089 			goto out;
1090 
1091 		/*
1092 		 * Mark the attribute as INCOMPLETE, then bunmapi() the
1093 		 * remote value.
1094 		 */
1095 		error = xfs_attr3_leaf_setflag(args);
1096 		if (error)
1097 			goto out;
1098 		error = xfs_attr_rmtval_remove(args);
1099 		if (error)
1100 			goto out;
1101 
1102 		/*
1103 		 * Refill the state structure with buffers, the prior calls
1104 		 * released our buffers.
1105 		 */
1106 		error = xfs_attr_refillstate(state);
1107 		if (error)
1108 			goto out;
1109 	}
1110 
1111 	/*
1112 	 * Remove the name and update the hashvals in the tree.
1113 	 */
1114 	blk = &state->path.blk[ state->path.active-1 ];
1115 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1116 	retval = xfs_attr3_leaf_remove(blk->bp, args);
1117 	xfs_da3_fixhashpath(state, &state->path);
1118 
1119 	/*
1120 	 * Check to see if the tree needs to be collapsed.
1121 	 */
1122 	if (retval && (state->path.active > 1)) {
1123 		xfs_defer_init(args->dfops, args->firstblock);
1124 		error = xfs_da3_join(state);
1125 		if (!error)
1126 			error = xfs_defer_finish(&args->trans, args->dfops, dp);
1127 		if (error) {
1128 			args->trans = NULL;
1129 			xfs_defer_cancel(args->dfops);
1130 			goto out;
1131 		}
1132 		/*
1133 		 * Commit the Btree join operation and start a new trans.
1134 		 */
1135 		error = xfs_trans_roll(&args->trans, dp);
1136 		if (error)
1137 			goto out;
1138 	}
1139 
1140 	/*
1141 	 * If the result is small enough, push it all into the inode.
1142 	 */
1143 	if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1144 		/*
1145 		 * Have to get rid of the copy of this dabuf in the state.
1146 		 */
1147 		ASSERT(state->path.active == 1);
1148 		ASSERT(state->path.blk[0].bp);
1149 		state->path.blk[0].bp = NULL;
1150 
1151 		error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1152 		if (error)
1153 			goto out;
1154 
1155 		if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1156 			xfs_defer_init(args->dfops, args->firstblock);
1157 			error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1158 			/* bp is gone due to xfs_da_shrink_inode */
1159 			if (!error)
1160 				error = xfs_defer_finish(&args->trans,
1161 							args->dfops, dp);
1162 			if (error) {
1163 				args->trans = NULL;
1164 				xfs_defer_cancel(args->dfops);
1165 				goto out;
1166 			}
1167 		} else
1168 			xfs_trans_brelse(args->trans, bp);
1169 	}
1170 	error = 0;
1171 
1172 out:
1173 	xfs_da_state_free(state);
1174 	return error;
1175 }
1176 
1177 /*
1178  * Fill in the disk block numbers in the state structure for the buffers
1179  * that are attached to the state structure.
1180  * This is done so that we can quickly reattach ourselves to those buffers
1181  * after some set of transaction commits have released these buffers.
1182  */
1183 STATIC int
1184 xfs_attr_fillstate(xfs_da_state_t *state)
1185 {
1186 	xfs_da_state_path_t *path;
1187 	xfs_da_state_blk_t *blk;
1188 	int level;
1189 
1190 	trace_xfs_attr_fillstate(state->args);
1191 
1192 	/*
1193 	 * Roll down the "path" in the state structure, storing the on-disk
1194 	 * block number for those buffers in the "path".
1195 	 */
1196 	path = &state->path;
1197 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1198 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1199 		if (blk->bp) {
1200 			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1201 			blk->bp = NULL;
1202 		} else {
1203 			blk->disk_blkno = 0;
1204 		}
1205 	}
1206 
1207 	/*
1208 	 * Roll down the "altpath" in the state structure, storing the on-disk
1209 	 * block number for those buffers in the "altpath".
1210 	 */
1211 	path = &state->altpath;
1212 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1213 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1214 		if (blk->bp) {
1215 			blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1216 			blk->bp = NULL;
1217 		} else {
1218 			blk->disk_blkno = 0;
1219 		}
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 /*
1226  * Reattach the buffers to the state structure based on the disk block
1227  * numbers stored in the state structure.
1228  * This is done after some set of transaction commits have released those
1229  * buffers from our grip.
1230  */
1231 STATIC int
1232 xfs_attr_refillstate(xfs_da_state_t *state)
1233 {
1234 	xfs_da_state_path_t *path;
1235 	xfs_da_state_blk_t *blk;
1236 	int level, error;
1237 
1238 	trace_xfs_attr_refillstate(state->args);
1239 
1240 	/*
1241 	 * Roll down the "path" in the state structure, storing the on-disk
1242 	 * block number for those buffers in the "path".
1243 	 */
1244 	path = &state->path;
1245 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1246 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1247 		if (blk->disk_blkno) {
1248 			error = xfs_da3_node_read(state->args->trans,
1249 						state->args->dp,
1250 						blk->blkno, blk->disk_blkno,
1251 						&blk->bp, XFS_ATTR_FORK);
1252 			if (error)
1253 				return error;
1254 		} else {
1255 			blk->bp = NULL;
1256 		}
1257 	}
1258 
1259 	/*
1260 	 * Roll down the "altpath" in the state structure, storing the on-disk
1261 	 * block number for those buffers in the "altpath".
1262 	 */
1263 	path = &state->altpath;
1264 	ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1265 	for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1266 		if (blk->disk_blkno) {
1267 			error = xfs_da3_node_read(state->args->trans,
1268 						state->args->dp,
1269 						blk->blkno, blk->disk_blkno,
1270 						&blk->bp, XFS_ATTR_FORK);
1271 			if (error)
1272 				return error;
1273 		} else {
1274 			blk->bp = NULL;
1275 		}
1276 	}
1277 
1278 	return 0;
1279 }
1280 
1281 /*
1282  * Look up a filename in a node attribute list.
1283  *
1284  * This routine gets called for any attribute fork that has more than one
1285  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1286  * "remote" values taking up more blocks.
1287  */
1288 STATIC int
1289 xfs_attr_node_get(xfs_da_args_t *args)
1290 {
1291 	xfs_da_state_t *state;
1292 	xfs_da_state_blk_t *blk;
1293 	int error, retval;
1294 	int i;
1295 
1296 	trace_xfs_attr_node_get(args);
1297 
1298 	state = xfs_da_state_alloc();
1299 	state->args = args;
1300 	state->mp = args->dp->i_mount;
1301 
1302 	/*
1303 	 * Search to see if name exists, and get back a pointer to it.
1304 	 */
1305 	error = xfs_da3_node_lookup_int(state, &retval);
1306 	if (error) {
1307 		retval = error;
1308 	} else if (retval == -EEXIST) {
1309 		blk = &state->path.blk[ state->path.active-1 ];
1310 		ASSERT(blk->bp != NULL);
1311 		ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1312 
1313 		/*
1314 		 * Get the value, local or "remote"
1315 		 */
1316 		retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1317 		if (!retval && (args->rmtblkno > 0)
1318 		    && !(args->flags & ATTR_KERNOVAL)) {
1319 			retval = xfs_attr_rmtval_get(args);
1320 		}
1321 	}
1322 
1323 	/*
1324 	 * If not in a transaction, we have to release all the buffers.
1325 	 */
1326 	for (i = 0; i < state->path.active; i++) {
1327 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1328 		state->path.blk[i].bp = NULL;
1329 	}
1330 
1331 	xfs_da_state_free(state);
1332 	return retval;
1333 }
1334