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