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