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