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