]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_attr.c
xfs: clean up busy extent naming
[karo-tx-linux.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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_trans.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_attr.h"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_error.h"
39 #include "xfs_quota.h"
40 #include "xfs_trans_space.h"
41 #include "xfs_rw.h"
42 #include "xfs_vnodeops.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 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
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_node_list(xfs_attr_list_context_t *context);
75 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
76 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
77
78 /*
79  * Routines to manipulate out-of-line attribute values.
80  */
81 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
82 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
83
84 #define ATTR_RMTVALUE_MAPSIZE   1       /* # of map entries at once */
85
86 STATIC int
87 xfs_attr_name_to_xname(
88         struct xfs_name *xname,
89         const unsigned char *aname)
90 {
91         if (!aname)
92                 return EINVAL;
93         xname->name = aname;
94         xname->len = strlen((char *)aname);
95         if (xname->len >= MAXNAMELEN)
96                 return EFAULT;          /* match IRIX behaviour */
97
98         return 0;
99 }
100
101 STATIC int
102 xfs_inode_hasattr(
103         struct xfs_inode        *ip)
104 {
105         if (!XFS_IFORK_Q(ip) ||
106             (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
107              ip->i_d.di_anextents == 0))
108                 return 0;
109         return 1;
110 }
111
112 /*========================================================================
113  * Overall external interface routines.
114  *========================================================================*/
115
116 STATIC int
117 xfs_attr_get_int(
118         struct xfs_inode        *ip,
119         struct xfs_name         *name,
120         unsigned char           *value,
121         int                     *valuelenp,
122         int                     flags)
123 {
124         xfs_da_args_t   args;
125         int             error;
126
127         if (!xfs_inode_hasattr(ip))
128                 return ENOATTR;
129
130         /*
131          * Fill in the arg structure for this request.
132          */
133         memset((char *)&args, 0, sizeof(args));
134         args.name = name->name;
135         args.namelen = name->len;
136         args.value = value;
137         args.valuelen = *valuelenp;
138         args.flags = flags;
139         args.hashval = xfs_da_hashname(args.name, args.namelen);
140         args.dp = ip;
141         args.whichfork = XFS_ATTR_FORK;
142
143         /*
144          * Decide on what work routines to call based on the inode size.
145          */
146         if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
147                 error = xfs_attr_shortform_getvalue(&args);
148         } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
149                 error = xfs_attr_leaf_get(&args);
150         } else {
151                 error = xfs_attr_node_get(&args);
152         }
153
154         /*
155          * Return the number of bytes in the value to the caller.
156          */
157         *valuelenp = args.valuelen;
158
159         if (error == EEXIST)
160                 error = 0;
161         return(error);
162 }
163
164 int
165 xfs_attr_get(
166         xfs_inode_t     *ip,
167         const unsigned char *name,
168         unsigned char   *value,
169         int             *valuelenp,
170         int             flags)
171 {
172         int             error;
173         struct xfs_name xname;
174
175         XFS_STATS_INC(xs_attr_get);
176
177         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
178                 return(EIO);
179
180         error = xfs_attr_name_to_xname(&xname, name);
181         if (error)
182                 return error;
183
184         xfs_ilock(ip, XFS_ILOCK_SHARED);
185         error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
186         xfs_iunlock(ip, XFS_ILOCK_SHARED);
187         return(error);
188 }
189
190 /*
191  * Calculate how many blocks we need for the new attribute,
192  */
193 STATIC int
194 xfs_attr_calc_size(
195         struct xfs_inode        *ip,
196         int                     namelen,
197         int                     valuelen,
198         int                     *local)
199 {
200         struct xfs_mount        *mp = ip->i_mount;
201         int                     size;
202         int                     nblks;
203
204         /*
205          * Determine space new attribute will use, and if it would be
206          * "local" or "remote" (note: local != inline).
207          */
208         size = xfs_attr_leaf_newentsize(namelen, valuelen,
209                                         mp->m_sb.sb_blocksize, local);
210
211         nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
212         if (*local) {
213                 if (size > (mp->m_sb.sb_blocksize >> 1)) {
214                         /* Double split possible */
215                         nblks *= 2;
216                 }
217         } else {
218                 /*
219                  * Out of line attribute, cannot double split, but
220                  * make room for the attribute value itself.
221                  */
222                 uint    dblocks = XFS_B_TO_FSB(mp, valuelen);
223                 nblks += dblocks;
224                 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
225         }
226
227         return nblks;
228 }
229
230 STATIC int
231 xfs_attr_set_int(
232         struct xfs_inode *dp,
233         struct xfs_name *name,
234         unsigned char   *value,
235         int             valuelen,
236         int             flags)
237 {
238         xfs_da_args_t   args;
239         xfs_fsblock_t   firstblock;
240         xfs_bmap_free_t flist;
241         int             error, err2, committed;
242         xfs_mount_t     *mp = dp->i_mount;
243         int             rsvd = (flags & ATTR_ROOT) != 0;
244         int             local;
245
246         /*
247          * Attach the dquots to the inode.
248          */
249         error = xfs_qm_dqattach(dp, 0);
250         if (error)
251                 return error;
252
253         /*
254          * If the inode doesn't have an attribute fork, add one.
255          * (inode must not be locked when we call this routine)
256          */
257         if (XFS_IFORK_Q(dp) == 0) {
258                 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
259                               XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
260
261                 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
262                         return(error);
263         }
264
265         /*
266          * Fill in the arg structure for this request.
267          */
268         memset((char *)&args, 0, sizeof(args));
269         args.name = name->name;
270         args.namelen = name->len;
271         args.value = value;
272         args.valuelen = valuelen;
273         args.flags = flags;
274         args.hashval = xfs_da_hashname(args.name, args.namelen);
275         args.dp = dp;
276         args.firstblock = &firstblock;
277         args.flist = &flist;
278         args.whichfork = XFS_ATTR_FORK;
279         args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
280
281         /* Size is now blocks for attribute data */
282         args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
283
284         /*
285          * Start our first transaction of the day.
286          *
287          * All future transactions during this code must be "chained" off
288          * this one via the trans_dup() call.  All transactions will contain
289          * the inode, and the inode will always be marked with trans_ihold().
290          * Since the inode will be locked in all transactions, we must log
291          * the inode in every transaction to let it float upward through
292          * the log.
293          */
294         args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
295
296         /*
297          * Root fork attributes can use reserved data blocks for this
298          * operation if necessary
299          */
300
301         if (rsvd)
302                 args.trans->t_flags |= XFS_TRANS_RESERVE;
303
304         if ((error = xfs_trans_reserve(args.trans, args.total,
305                         XFS_ATTRSET_LOG_RES(mp, args.total), 0,
306                         XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
307                 xfs_trans_cancel(args.trans, 0);
308                 return(error);
309         }
310         xfs_ilock(dp, XFS_ILOCK_EXCL);
311
312         error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
313                                 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
314                                        XFS_QMOPT_RES_REGBLKS);
315         if (error) {
316                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
317                 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
318                 return (error);
319         }
320
321         xfs_trans_ijoin(args.trans, dp, 0);
322
323         /*
324          * If the attribute list is non-existent or a shortform list,
325          * upgrade it to a single-leaf-block attribute list.
326          */
327         if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
328             ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
329              (dp->i_d.di_anextents == 0))) {
330
331                 /*
332                  * Build initial attribute list (if required).
333                  */
334                 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
335                         xfs_attr_shortform_create(&args);
336
337                 /*
338                  * Try to add the attr to the attribute list in
339                  * the inode.
340                  */
341                 error = xfs_attr_shortform_addname(&args);
342                 if (error != ENOSPC) {
343                         /*
344                          * Commit the shortform mods, and we're done.
345                          * NOTE: this is also the error path (EEXIST, etc).
346                          */
347                         ASSERT(args.trans != NULL);
348
349                         /*
350                          * If this is a synchronous mount, make sure that
351                          * the transaction goes to disk before returning
352                          * to the user.
353                          */
354                         if (mp->m_flags & XFS_MOUNT_WSYNC) {
355                                 xfs_trans_set_sync(args.trans);
356                         }
357
358                         if (!error && (flags & ATTR_KERNOTIME) == 0) {
359                                 xfs_trans_ichgtime(args.trans, dp,
360                                                         XFS_ICHGTIME_CHG);
361                         }
362                         err2 = xfs_trans_commit(args.trans,
363                                                  XFS_TRANS_RELEASE_LOG_RES);
364                         xfs_iunlock(dp, XFS_ILOCK_EXCL);
365
366                         return(error == 0 ? err2 : error);
367                 }
368
369                 /*
370                  * It won't fit in the shortform, transform to a leaf block.
371                  * GROT: another possible req'mt for a double-split btree op.
372                  */
373                 xfs_bmap_init(args.flist, args.firstblock);
374                 error = xfs_attr_shortform_to_leaf(&args);
375                 if (!error) {
376                         error = xfs_bmap_finish(&args.trans, args.flist,
377                                                 &committed);
378                 }
379                 if (error) {
380                         ASSERT(committed);
381                         args.trans = NULL;
382                         xfs_bmap_cancel(&flist);
383                         goto out;
384                 }
385
386                 /*
387                  * bmap_finish() may have committed the last trans and started
388                  * a new one.  We need the inode to be in all transactions.
389                  */
390                 if (committed)
391                         xfs_trans_ijoin(args.trans, dp, 0);
392
393                 /*
394                  * Commit the leaf transformation.  We'll need another (linked)
395                  * transaction to add the new attribute to the leaf.
396                  */
397
398                 error = xfs_trans_roll(&args.trans, dp);
399                 if (error)
400                         goto out;
401
402         }
403
404         if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
405                 error = xfs_attr_leaf_addname(&args);
406         } else {
407                 error = xfs_attr_node_addname(&args);
408         }
409         if (error) {
410                 goto out;
411         }
412
413         /*
414          * If this is a synchronous mount, make sure that the
415          * transaction goes to disk before returning to the user.
416          */
417         if (mp->m_flags & XFS_MOUNT_WSYNC) {
418                 xfs_trans_set_sync(args.trans);
419         }
420
421         if ((flags & ATTR_KERNOTIME) == 0)
422                 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
423
424         /*
425          * Commit the last in the sequence of transactions.
426          */
427         xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
428         error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
429         xfs_iunlock(dp, XFS_ILOCK_EXCL);
430
431         return(error);
432
433 out:
434         if (args.trans)
435                 xfs_trans_cancel(args.trans,
436                         XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
437         xfs_iunlock(dp, XFS_ILOCK_EXCL);
438         return(error);
439 }
440
441 int
442 xfs_attr_set(
443         xfs_inode_t     *dp,
444         const unsigned char *name,
445         unsigned char   *value,
446         int             valuelen,
447         int             flags)
448 {
449         int             error;
450         struct xfs_name xname;
451
452         XFS_STATS_INC(xs_attr_set);
453
454         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
455                 return (EIO);
456
457         error = xfs_attr_name_to_xname(&xname, name);
458         if (error)
459                 return error;
460
461         return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
462 }
463
464 /*
465  * Generic handler routine to remove a name from an attribute list.
466  * Transitions attribute list from Btree to shortform as necessary.
467  */
468 STATIC int
469 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
470 {
471         xfs_da_args_t   args;
472         xfs_fsblock_t   firstblock;
473         xfs_bmap_free_t flist;
474         int             error;
475         xfs_mount_t     *mp = dp->i_mount;
476
477         /*
478          * Fill in the arg structure for this request.
479          */
480         memset((char *)&args, 0, sizeof(args));
481         args.name = name->name;
482         args.namelen = name->len;
483         args.flags = flags;
484         args.hashval = xfs_da_hashname(args.name, args.namelen);
485         args.dp = dp;
486         args.firstblock = &firstblock;
487         args.flist = &flist;
488         args.total = 0;
489         args.whichfork = XFS_ATTR_FORK;
490
491         /*
492          * we have no control over the attribute names that userspace passes us
493          * to remove, so we have to allow the name lookup prior to attribute
494          * removal to fail.
495          */
496         args.op_flags = XFS_DA_OP_OKNOENT;
497
498         /*
499          * Attach the dquots to the inode.
500          */
501         error = xfs_qm_dqattach(dp, 0);
502         if (error)
503                 return error;
504
505         /*
506          * Start our first transaction of the day.
507          *
508          * All future transactions during this code must be "chained" off
509          * this one via the trans_dup() call.  All transactions will contain
510          * the inode, and the inode will always be marked with trans_ihold().
511          * Since the inode will be locked in all transactions, we must log
512          * the inode in every transaction to let it float upward through
513          * the log.
514          */
515         args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
516
517         /*
518          * Root fork attributes can use reserved data blocks for this
519          * operation if necessary
520          */
521
522         if (flags & ATTR_ROOT)
523                 args.trans->t_flags |= XFS_TRANS_RESERVE;
524
525         if ((error = xfs_trans_reserve(args.trans,
526                                       XFS_ATTRRM_SPACE_RES(mp),
527                                       XFS_ATTRRM_LOG_RES(mp),
528                                       0, XFS_TRANS_PERM_LOG_RES,
529                                       XFS_ATTRRM_LOG_COUNT))) {
530                 xfs_trans_cancel(args.trans, 0);
531                 return(error);
532         }
533
534         xfs_ilock(dp, XFS_ILOCK_EXCL);
535         /*
536          * No need to make quota reservations here. We expect to release some
537          * blocks not allocate in the common case.
538          */
539         xfs_trans_ijoin(args.trans, dp, 0);
540
541         /*
542          * Decide on what work routines to call based on the inode size.
543          */
544         if (!xfs_inode_hasattr(dp)) {
545                 error = XFS_ERROR(ENOATTR);
546                 goto out;
547         }
548         if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
549                 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
550                 error = xfs_attr_shortform_remove(&args);
551                 if (error) {
552                         goto out;
553                 }
554         } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
555                 error = xfs_attr_leaf_removename(&args);
556         } else {
557                 error = xfs_attr_node_removename(&args);
558         }
559         if (error) {
560                 goto out;
561         }
562
563         /*
564          * If this is a synchronous mount, make sure that the
565          * transaction goes to disk before returning to the user.
566          */
567         if (mp->m_flags & XFS_MOUNT_WSYNC) {
568                 xfs_trans_set_sync(args.trans);
569         }
570
571         if ((flags & ATTR_KERNOTIME) == 0)
572                 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
573
574         /*
575          * Commit the last in the sequence of transactions.
576          */
577         xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
578         error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
579         xfs_iunlock(dp, XFS_ILOCK_EXCL);
580
581         return(error);
582
583 out:
584         if (args.trans)
585                 xfs_trans_cancel(args.trans,
586                         XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
587         xfs_iunlock(dp, XFS_ILOCK_EXCL);
588         return(error);
589 }
590
591 int
592 xfs_attr_remove(
593         xfs_inode_t     *dp,
594         const unsigned char *name,
595         int             flags)
596 {
597         int             error;
598         struct xfs_name xname;
599
600         XFS_STATS_INC(xs_attr_remove);
601
602         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
603                 return (EIO);
604
605         error = xfs_attr_name_to_xname(&xname, name);
606         if (error)
607                 return error;
608
609         xfs_ilock(dp, XFS_ILOCK_SHARED);
610         if (!xfs_inode_hasattr(dp)) {
611                 xfs_iunlock(dp, XFS_ILOCK_SHARED);
612                 return XFS_ERROR(ENOATTR);
613         }
614         xfs_iunlock(dp, XFS_ILOCK_SHARED);
615
616         return xfs_attr_remove_int(dp, &xname, flags);
617 }
618
619 int
620 xfs_attr_list_int(xfs_attr_list_context_t *context)
621 {
622         int error;
623         xfs_inode_t *dp = context->dp;
624
625         XFS_STATS_INC(xs_attr_list);
626
627         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
628                 return EIO;
629
630         xfs_ilock(dp, XFS_ILOCK_SHARED);
631
632         /*
633          * Decide on what work routines to call based on the inode size.
634          */
635         if (!xfs_inode_hasattr(dp)) {
636                 error = 0;
637         } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
638                 error = xfs_attr_shortform_list(context);
639         } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
640                 error = xfs_attr_leaf_list(context);
641         } else {
642                 error = xfs_attr_node_list(context);
643         }
644
645         xfs_iunlock(dp, XFS_ILOCK_SHARED);
646
647         return error;
648 }
649
650 #define ATTR_ENTBASESIZE                /* minimum bytes used by an attr */ \
651         (((struct attrlist_ent *) 0)->a_name - (char *) 0)
652 #define ATTR_ENTSIZE(namelen)           /* actual bytes used by an attr */ \
653         ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
654          & ~(sizeof(u_int32_t)-1))
655
656 /*
657  * Format an attribute and copy it out to the user's buffer.
658  * Take care to check values and protect against them changing later,
659  * we may be reading them directly out of a user buffer.
660  */
661 /*ARGSUSED*/
662 STATIC int
663 xfs_attr_put_listent(
664         xfs_attr_list_context_t *context,
665         int             flags,
666         unsigned char   *name,
667         int             namelen,
668         int             valuelen,
669         unsigned char   *value)
670 {
671         struct attrlist *alist = (struct attrlist *)context->alist;
672         attrlist_ent_t *aep;
673         int arraytop;
674
675         ASSERT(!(context->flags & ATTR_KERNOVAL));
676         ASSERT(context->count >= 0);
677         ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
678         ASSERT(context->firstu >= sizeof(*alist));
679         ASSERT(context->firstu <= context->bufsize);
680
681         /*
682          * Only list entries in the right namespace.
683          */
684         if (((context->flags & ATTR_SECURE) == 0) !=
685             ((flags & XFS_ATTR_SECURE) == 0))
686                 return 0;
687         if (((context->flags & ATTR_ROOT) == 0) !=
688             ((flags & XFS_ATTR_ROOT) == 0))
689                 return 0;
690
691         arraytop = sizeof(*alist) +
692                         context->count * sizeof(alist->al_offset[0]);
693         context->firstu -= ATTR_ENTSIZE(namelen);
694         if (context->firstu < arraytop) {
695                 trace_xfs_attr_list_full(context);
696                 alist->al_more = 1;
697                 context->seen_enough = 1;
698                 return 1;
699         }
700
701         aep = (attrlist_ent_t *)&context->alist[context->firstu];
702         aep->a_valuelen = valuelen;
703         memcpy(aep->a_name, name, namelen);
704         aep->a_name[namelen] = 0;
705         alist->al_offset[context->count++] = context->firstu;
706         alist->al_count = context->count;
707         trace_xfs_attr_list_add(context);
708         return 0;
709 }
710
711 /*
712  * Generate a list of extended attribute names and optionally
713  * also value lengths.  Positive return value follows the XFS
714  * convention of being an error, zero or negative return code
715  * is the length of the buffer returned (negated), indicating
716  * success.
717  */
718 int
719 xfs_attr_list(
720         xfs_inode_t     *dp,
721         char            *buffer,
722         int             bufsize,
723         int             flags,
724         attrlist_cursor_kern_t *cursor)
725 {
726         xfs_attr_list_context_t context;
727         struct attrlist *alist;
728         int error;
729
730         /*
731          * Validate the cursor.
732          */
733         if (cursor->pad1 || cursor->pad2)
734                 return(XFS_ERROR(EINVAL));
735         if ((cursor->initted == 0) &&
736             (cursor->hashval || cursor->blkno || cursor->offset))
737                 return XFS_ERROR(EINVAL);
738
739         /*
740          * Check for a properly aligned buffer.
741          */
742         if (((long)buffer) & (sizeof(int)-1))
743                 return XFS_ERROR(EFAULT);
744         if (flags & ATTR_KERNOVAL)
745                 bufsize = 0;
746
747         /*
748          * Initialize the output buffer.
749          */
750         memset(&context, 0, sizeof(context));
751         context.dp = dp;
752         context.cursor = cursor;
753         context.resynch = 1;
754         context.flags = flags;
755         context.alist = buffer;
756         context.bufsize = (bufsize & ~(sizeof(int)-1));  /* align */
757         context.firstu = context.bufsize;
758         context.put_listent = xfs_attr_put_listent;
759
760         alist = (struct attrlist *)context.alist;
761         alist->al_count = 0;
762         alist->al_more = 0;
763         alist->al_offset[0] = context.bufsize;
764
765         error = xfs_attr_list_int(&context);
766         ASSERT(error >= 0);
767         return error;
768 }
769
770 int                                                             /* error */
771 xfs_attr_inactive(xfs_inode_t *dp)
772 {
773         xfs_trans_t *trans;
774         xfs_mount_t *mp;
775         int error;
776
777         mp = dp->i_mount;
778         ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
779
780         xfs_ilock(dp, XFS_ILOCK_SHARED);
781         if (!xfs_inode_hasattr(dp) ||
782             dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
783                 xfs_iunlock(dp, XFS_ILOCK_SHARED);
784                 return 0;
785         }
786         xfs_iunlock(dp, XFS_ILOCK_SHARED);
787
788         /*
789          * Start our first transaction of the day.
790          *
791          * All future transactions during this code must be "chained" off
792          * this one via the trans_dup() call.  All transactions will contain
793          * the inode, and the inode will always be marked with trans_ihold().
794          * Since the inode will be locked in all transactions, we must log
795          * the inode in every transaction to let it float upward through
796          * the log.
797          */
798         trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
799         if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
800                                       XFS_TRANS_PERM_LOG_RES,
801                                       XFS_ATTRINVAL_LOG_COUNT))) {
802                 xfs_trans_cancel(trans, 0);
803                 return(error);
804         }
805         xfs_ilock(dp, XFS_ILOCK_EXCL);
806
807         /*
808          * No need to make quota reservations here. We expect to release some
809          * blocks, not allocate, in the common case.
810          */
811         xfs_trans_ijoin(trans, dp, 0);
812
813         /*
814          * Decide on what work routines to call based on the inode size.
815          */
816         if (!xfs_inode_hasattr(dp) ||
817             dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
818                 error = 0;
819                 goto out;
820         }
821         error = xfs_attr_root_inactive(&trans, dp);
822         if (error)
823                 goto out;
824
825         error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
826         if (error)
827                 goto out;
828
829         error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
830         xfs_iunlock(dp, XFS_ILOCK_EXCL);
831
832         return(error);
833
834 out:
835         xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
836         xfs_iunlock(dp, XFS_ILOCK_EXCL);
837         return(error);
838 }
839
840
841
842 /*========================================================================
843  * External routines when attribute list is inside the inode
844  *========================================================================*/
845
846 /*
847  * Add a name to the shortform attribute list structure
848  * This is the external routine.
849  */
850 STATIC int
851 xfs_attr_shortform_addname(xfs_da_args_t *args)
852 {
853         int newsize, forkoff, retval;
854
855         trace_xfs_attr_sf_addname(args);
856
857         retval = xfs_attr_shortform_lookup(args);
858         if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
859                 return(retval);
860         } else if (retval == EEXIST) {
861                 if (args->flags & ATTR_CREATE)
862                         return(retval);
863                 retval = xfs_attr_shortform_remove(args);
864                 ASSERT(retval == 0);
865         }
866
867         if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
868             args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
869                 return(XFS_ERROR(ENOSPC));
870
871         newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
872         newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
873
874         forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
875         if (!forkoff)
876                 return(XFS_ERROR(ENOSPC));
877
878         xfs_attr_shortform_add(args, forkoff);
879         return(0);
880 }
881
882
883 /*========================================================================
884  * External routines when attribute list is one block
885  *========================================================================*/
886
887 /*
888  * Add a name to the leaf attribute list structure
889  *
890  * This leaf block cannot have a "remote" value, we only call this routine
891  * if bmap_one_block() says there is only one block (ie: no remote blks).
892  */
893 STATIC int
894 xfs_attr_leaf_addname(xfs_da_args_t *args)
895 {
896         xfs_inode_t *dp;
897         xfs_dabuf_t *bp;
898         int retval, error, committed, forkoff;
899
900         trace_xfs_attr_leaf_addname(args);
901
902         /*
903          * Read the (only) block in the attribute list in.
904          */
905         dp = args->dp;
906         args->blkno = 0;
907         error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
908                                              XFS_ATTR_FORK);
909         if (error)
910                 return(error);
911         ASSERT(bp != NULL);
912
913         /*
914          * Look up the given attribute in the leaf block.  Figure out if
915          * the given flags produce an error or call for an atomic rename.
916          */
917         retval = xfs_attr_leaf_lookup_int(bp, args);
918         if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
919                 xfs_da_brelse(args->trans, bp);
920                 return(retval);
921         } else if (retval == EEXIST) {
922                 if (args->flags & ATTR_CREATE) {        /* pure create op */
923                         xfs_da_brelse(args->trans, bp);
924                         return(retval);
925                 }
926
927                 trace_xfs_attr_leaf_replace(args);
928
929                 args->op_flags |= XFS_DA_OP_RENAME;     /* an atomic rename */
930                 args->blkno2 = args->blkno;             /* set 2nd entry info*/
931                 args->index2 = args->index;
932                 args->rmtblkno2 = args->rmtblkno;
933                 args->rmtblkcnt2 = args->rmtblkcnt;
934         }
935
936         /*
937          * Add the attribute to the leaf block, transitioning to a Btree
938          * if required.
939          */
940         retval = xfs_attr_leaf_add(bp, args);
941         xfs_da_buf_done(bp);
942         if (retval == ENOSPC) {
943                 /*
944                  * Promote the attribute list to the Btree format, then
945                  * Commit that transaction so that the node_addname() call
946                  * can manage its own transactions.
947                  */
948                 xfs_bmap_init(args->flist, args->firstblock);
949                 error = xfs_attr_leaf_to_node(args);
950                 if (!error) {
951                         error = xfs_bmap_finish(&args->trans, args->flist,
952                                                 &committed);
953                 }
954                 if (error) {
955                         ASSERT(committed);
956                         args->trans = NULL;
957                         xfs_bmap_cancel(args->flist);
958                         return(error);
959                 }
960
961                 /*
962                  * bmap_finish() may have committed the last trans and started
963                  * a new one.  We need the inode to be in all transactions.
964                  */
965                 if (committed)
966                         xfs_trans_ijoin(args->trans, dp, 0);
967
968                 /*
969                  * Commit the current trans (including the inode) and start
970                  * a new one.
971                  */
972                 error = xfs_trans_roll(&args->trans, dp);
973                 if (error)
974                         return (error);
975
976                 /*
977                  * Fob the whole rest of the problem off on the Btree code.
978                  */
979                 error = xfs_attr_node_addname(args);
980                 return(error);
981         }
982
983         /*
984          * Commit the transaction that added the attr name so that
985          * later routines can manage their own transactions.
986          */
987         error = xfs_trans_roll(&args->trans, dp);
988         if (error)
989                 return (error);
990
991         /*
992          * If there was an out-of-line value, allocate the blocks we
993          * identified for its storage and copy the value.  This is done
994          * after we create the attribute so that we don't overflow the
995          * maximum size of a transaction and/or hit a deadlock.
996          */
997         if (args->rmtblkno > 0) {
998                 error = xfs_attr_rmtval_set(args);
999                 if (error)
1000                         return(error);
1001         }
1002
1003         /*
1004          * If this is an atomic rename operation, we must "flip" the
1005          * incomplete flags on the "new" and "old" attribute/value pairs
1006          * so that one disappears and one appears atomically.  Then we
1007          * must remove the "old" attribute/value pair.
1008          */
1009         if (args->op_flags & XFS_DA_OP_RENAME) {
1010                 /*
1011                  * In a separate transaction, set the incomplete flag on the
1012                  * "old" attr and clear the incomplete flag on the "new" attr.
1013                  */
1014                 error = xfs_attr_leaf_flipflags(args);
1015                 if (error)
1016                         return(error);
1017
1018                 /*
1019                  * Dismantle the "old" attribute/value pair by removing
1020                  * a "remote" value (if it exists).
1021                  */
1022                 args->index = args->index2;
1023                 args->blkno = args->blkno2;
1024                 args->rmtblkno = args->rmtblkno2;
1025                 args->rmtblkcnt = args->rmtblkcnt2;
1026                 if (args->rmtblkno) {
1027                         error = xfs_attr_rmtval_remove(args);
1028                         if (error)
1029                                 return(error);
1030                 }
1031
1032                 /*
1033                  * Read in the block containing the "old" attr, then
1034                  * remove the "old" attr from that block (neat, huh!)
1035                  */
1036                 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1037                                                      &bp, XFS_ATTR_FORK);
1038                 if (error)
1039                         return(error);
1040                 ASSERT(bp != NULL);
1041                 (void)xfs_attr_leaf_remove(bp, args);
1042
1043                 /*
1044                  * If the result is small enough, shrink it all into the inode.
1045                  */
1046                 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1047                         xfs_bmap_init(args->flist, args->firstblock);
1048                         error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1049                         /* bp is gone due to xfs_da_shrink_inode */
1050                         if (!error) {
1051                                 error = xfs_bmap_finish(&args->trans,
1052                                                         args->flist,
1053                                                         &committed);
1054                         }
1055                         if (error) {
1056                                 ASSERT(committed);
1057                                 args->trans = NULL;
1058                                 xfs_bmap_cancel(args->flist);
1059                                 return(error);
1060                         }
1061
1062                         /*
1063                          * bmap_finish() may have committed the last trans
1064                          * and started a new one.  We need the inode to be
1065                          * in all transactions.
1066                          */
1067                         if (committed)
1068                                 xfs_trans_ijoin(args->trans, dp, 0);
1069                 } else
1070                         xfs_da_buf_done(bp);
1071
1072                 /*
1073                  * Commit the remove and start the next trans in series.
1074                  */
1075                 error = xfs_trans_roll(&args->trans, dp);
1076
1077         } else if (args->rmtblkno > 0) {
1078                 /*
1079                  * Added a "remote" value, just clear the incomplete flag.
1080                  */
1081                 error = xfs_attr_leaf_clearflag(args);
1082         }
1083         return(error);
1084 }
1085
1086 /*
1087  * Remove a name from the leaf attribute list structure
1088  *
1089  * This leaf block cannot have a "remote" value, we only call this routine
1090  * if bmap_one_block() says there is only one block (ie: no remote blks).
1091  */
1092 STATIC int
1093 xfs_attr_leaf_removename(xfs_da_args_t *args)
1094 {
1095         xfs_inode_t *dp;
1096         xfs_dabuf_t *bp;
1097         int error, committed, forkoff;
1098
1099         trace_xfs_attr_leaf_removename(args);
1100
1101         /*
1102          * Remove the attribute.
1103          */
1104         dp = args->dp;
1105         args->blkno = 0;
1106         error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1107                                              XFS_ATTR_FORK);
1108         if (error) {
1109                 return(error);
1110         }
1111
1112         ASSERT(bp != NULL);
1113         error = xfs_attr_leaf_lookup_int(bp, args);
1114         if (error == ENOATTR) {
1115                 xfs_da_brelse(args->trans, bp);
1116                 return(error);
1117         }
1118
1119         (void)xfs_attr_leaf_remove(bp, args);
1120
1121         /*
1122          * If the result is small enough, shrink it all into the inode.
1123          */
1124         if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1125                 xfs_bmap_init(args->flist, args->firstblock);
1126                 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1127                 /* bp is gone due to xfs_da_shrink_inode */
1128                 if (!error) {
1129                         error = xfs_bmap_finish(&args->trans, args->flist,
1130                                                 &committed);
1131                 }
1132                 if (error) {
1133                         ASSERT(committed);
1134                         args->trans = NULL;
1135                         xfs_bmap_cancel(args->flist);
1136                         return(error);
1137                 }
1138
1139                 /*
1140                  * bmap_finish() may have committed the last trans and started
1141                  * a new one.  We need the inode to be in all transactions.
1142                  */
1143                 if (committed)
1144                         xfs_trans_ijoin(args->trans, dp, 0);
1145         } else
1146                 xfs_da_buf_done(bp);
1147         return(0);
1148 }
1149
1150 /*
1151  * Look up a name in a leaf attribute list structure.
1152  *
1153  * This leaf block cannot have a "remote" value, we only call this routine
1154  * if bmap_one_block() says there is only one block (ie: no remote blks).
1155  */
1156 STATIC int
1157 xfs_attr_leaf_get(xfs_da_args_t *args)
1158 {
1159         xfs_dabuf_t *bp;
1160         int error;
1161
1162         args->blkno = 0;
1163         error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1164                                              XFS_ATTR_FORK);
1165         if (error)
1166                 return(error);
1167         ASSERT(bp != NULL);
1168
1169         error = xfs_attr_leaf_lookup_int(bp, args);
1170         if (error != EEXIST)  {
1171                 xfs_da_brelse(args->trans, bp);
1172                 return(error);
1173         }
1174         error = xfs_attr_leaf_getvalue(bp, args);
1175         xfs_da_brelse(args->trans, bp);
1176         if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1177                 error = xfs_attr_rmtval_get(args);
1178         }
1179         return(error);
1180 }
1181
1182 /*
1183  * Copy out attribute entries for attr_list(), for leaf attribute lists.
1184  */
1185 STATIC int
1186 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1187 {
1188         xfs_attr_leafblock_t *leaf;
1189         int error;
1190         xfs_dabuf_t *bp;
1191
1192         context->cursor->blkno = 0;
1193         error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1194         if (error)
1195                 return XFS_ERROR(error);
1196         ASSERT(bp != NULL);
1197         leaf = bp->data;
1198         if (unlikely(leaf->hdr.info.magic != cpu_to_be16(XFS_ATTR_LEAF_MAGIC))) {
1199                 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1200                                      context->dp->i_mount, leaf);
1201                 xfs_da_brelse(NULL, bp);
1202                 return XFS_ERROR(EFSCORRUPTED);
1203         }
1204
1205         error = xfs_attr_leaf_list_int(bp, context);
1206         xfs_da_brelse(NULL, bp);
1207         return XFS_ERROR(error);
1208 }
1209
1210
1211 /*========================================================================
1212  * External routines when attribute list size > XFS_LBSIZE(mp).
1213  *========================================================================*/
1214
1215 /*
1216  * Add a name to a Btree-format attribute list.
1217  *
1218  * This will involve walking down the Btree, and may involve splitting
1219  * leaf nodes and even splitting intermediate nodes up to and including
1220  * the root node (a special case of an intermediate node).
1221  *
1222  * "Remote" attribute values confuse the issue and atomic rename operations
1223  * add a whole extra layer of confusion on top of that.
1224  */
1225 STATIC int
1226 xfs_attr_node_addname(xfs_da_args_t *args)
1227 {
1228         xfs_da_state_t *state;
1229         xfs_da_state_blk_t *blk;
1230         xfs_inode_t *dp;
1231         xfs_mount_t *mp;
1232         int committed, retval, error;
1233
1234         trace_xfs_attr_node_addname(args);
1235
1236         /*
1237          * Fill in bucket of arguments/results/context to carry around.
1238          */
1239         dp = args->dp;
1240         mp = dp->i_mount;
1241 restart:
1242         state = xfs_da_state_alloc();
1243         state->args = args;
1244         state->mp = mp;
1245         state->blocksize = state->mp->m_sb.sb_blocksize;
1246         state->node_ents = state->mp->m_attr_node_ents;
1247
1248         /*
1249          * Search to see if name already exists, and get back a pointer
1250          * to where it should go.
1251          */
1252         error = xfs_da_node_lookup_int(state, &retval);
1253         if (error)
1254                 goto out;
1255         blk = &state->path.blk[ state->path.active-1 ];
1256         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1257         if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1258                 goto out;
1259         } else if (retval == EEXIST) {
1260                 if (args->flags & ATTR_CREATE)
1261                         goto out;
1262
1263                 trace_xfs_attr_node_replace(args);
1264
1265                 args->op_flags |= XFS_DA_OP_RENAME;     /* atomic rename op */
1266                 args->blkno2 = args->blkno;             /* set 2nd entry info*/
1267                 args->index2 = args->index;
1268                 args->rmtblkno2 = args->rmtblkno;
1269                 args->rmtblkcnt2 = args->rmtblkcnt;
1270                 args->rmtblkno = 0;
1271                 args->rmtblkcnt = 0;
1272         }
1273
1274         retval = xfs_attr_leaf_add(blk->bp, state->args);
1275         if (retval == ENOSPC) {
1276                 if (state->path.active == 1) {
1277                         /*
1278                          * Its really a single leaf node, but it had
1279                          * out-of-line values so it looked like it *might*
1280                          * have been a b-tree.
1281                          */
1282                         xfs_da_state_free(state);
1283                         xfs_bmap_init(args->flist, args->firstblock);
1284                         error = xfs_attr_leaf_to_node(args);
1285                         if (!error) {
1286                                 error = xfs_bmap_finish(&args->trans,
1287                                                         args->flist,
1288                                                         &committed);
1289                         }
1290                         if (error) {
1291                                 ASSERT(committed);
1292                                 args->trans = NULL;
1293                                 xfs_bmap_cancel(args->flist);
1294                                 goto out;
1295                         }
1296
1297                         /*
1298                          * bmap_finish() may have committed the last trans
1299                          * and started a new one.  We need the inode to be
1300                          * in all transactions.
1301                          */
1302                         if (committed)
1303                                 xfs_trans_ijoin(args->trans, dp, 0);
1304
1305                         /*
1306                          * Commit the node conversion and start the next
1307                          * trans in the chain.
1308                          */
1309                         error = xfs_trans_roll(&args->trans, dp);
1310                         if (error)
1311                                 goto out;
1312
1313                         goto restart;
1314                 }
1315
1316                 /*
1317                  * Split as many Btree elements as required.
1318                  * This code tracks the new and old attr's location
1319                  * in the index/blkno/rmtblkno/rmtblkcnt fields and
1320                  * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1321                  */
1322                 xfs_bmap_init(args->flist, args->firstblock);
1323                 error = xfs_da_split(state);
1324                 if (!error) {
1325                         error = xfs_bmap_finish(&args->trans, args->flist,
1326                                                 &committed);
1327                 }
1328                 if (error) {
1329                         ASSERT(committed);
1330                         args->trans = NULL;
1331                         xfs_bmap_cancel(args->flist);
1332                         goto out;
1333                 }
1334
1335                 /*
1336                  * bmap_finish() may have committed the last trans and started
1337                  * a new one.  We need the inode to be in all transactions.
1338                  */
1339                 if (committed)
1340                         xfs_trans_ijoin(args->trans, dp, 0);
1341         } else {
1342                 /*
1343                  * Addition succeeded, update Btree hashvals.
1344                  */
1345                 xfs_da_fixhashpath(state, &state->path);
1346         }
1347
1348         /*
1349          * Kill the state structure, we're done with it and need to
1350          * allow the buffers to come back later.
1351          */
1352         xfs_da_state_free(state);
1353         state = NULL;
1354
1355         /*
1356          * Commit the leaf addition or btree split and start the next
1357          * trans in the chain.
1358          */
1359         error = xfs_trans_roll(&args->trans, dp);
1360         if (error)
1361                 goto out;
1362
1363         /*
1364          * If there was an out-of-line value, allocate the blocks we
1365          * identified for its storage and copy the value.  This is done
1366          * after we create the attribute so that we don't overflow the
1367          * maximum size of a transaction and/or hit a deadlock.
1368          */
1369         if (args->rmtblkno > 0) {
1370                 error = xfs_attr_rmtval_set(args);
1371                 if (error)
1372                         return(error);
1373         }
1374
1375         /*
1376          * If this is an atomic rename operation, we must "flip" the
1377          * incomplete flags on the "new" and "old" attribute/value pairs
1378          * so that one disappears and one appears atomically.  Then we
1379          * must remove the "old" attribute/value pair.
1380          */
1381         if (args->op_flags & XFS_DA_OP_RENAME) {
1382                 /*
1383                  * In a separate transaction, set the incomplete flag on the
1384                  * "old" attr and clear the incomplete flag on the "new" attr.
1385                  */
1386                 error = xfs_attr_leaf_flipflags(args);
1387                 if (error)
1388                         goto out;
1389
1390                 /*
1391                  * Dismantle the "old" attribute/value pair by removing
1392                  * a "remote" value (if it exists).
1393                  */
1394                 args->index = args->index2;
1395                 args->blkno = args->blkno2;
1396                 args->rmtblkno = args->rmtblkno2;
1397                 args->rmtblkcnt = args->rmtblkcnt2;
1398                 if (args->rmtblkno) {
1399                         error = xfs_attr_rmtval_remove(args);
1400                         if (error)
1401                                 return(error);
1402                 }
1403
1404                 /*
1405                  * Re-find the "old" attribute entry after any split ops.
1406                  * The INCOMPLETE flag means that we will find the "old"
1407                  * attr, not the "new" one.
1408                  */
1409                 args->flags |= XFS_ATTR_INCOMPLETE;
1410                 state = xfs_da_state_alloc();
1411                 state->args = args;
1412                 state->mp = mp;
1413                 state->blocksize = state->mp->m_sb.sb_blocksize;
1414                 state->node_ents = state->mp->m_attr_node_ents;
1415                 state->inleaf = 0;
1416                 error = xfs_da_node_lookup_int(state, &retval);
1417                 if (error)
1418                         goto out;
1419
1420                 /*
1421                  * Remove the name and update the hashvals in the tree.
1422                  */
1423                 blk = &state->path.blk[ state->path.active-1 ];
1424                 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1425                 error = xfs_attr_leaf_remove(blk->bp, args);
1426                 xfs_da_fixhashpath(state, &state->path);
1427
1428                 /*
1429                  * Check to see if the tree needs to be collapsed.
1430                  */
1431                 if (retval && (state->path.active > 1)) {
1432                         xfs_bmap_init(args->flist, args->firstblock);
1433                         error = xfs_da_join(state);
1434                         if (!error) {
1435                                 error = xfs_bmap_finish(&args->trans,
1436                                                         args->flist,
1437                                                         &committed);
1438                         }
1439                         if (error) {
1440                                 ASSERT(committed);
1441                                 args->trans = NULL;
1442                                 xfs_bmap_cancel(args->flist);
1443                                 goto out;
1444                         }
1445
1446                         /*
1447                          * bmap_finish() may have committed the last trans
1448                          * and started a new one.  We need the inode to be
1449                          * in all transactions.
1450                          */
1451                         if (committed)
1452                                 xfs_trans_ijoin(args->trans, dp, 0);
1453                 }
1454
1455                 /*
1456                  * Commit and start the next trans in the chain.
1457                  */
1458                 error = xfs_trans_roll(&args->trans, dp);
1459                 if (error)
1460                         goto out;
1461
1462         } else if (args->rmtblkno > 0) {
1463                 /*
1464                  * Added a "remote" value, just clear the incomplete flag.
1465                  */
1466                 error = xfs_attr_leaf_clearflag(args);
1467                 if (error)
1468                         goto out;
1469         }
1470         retval = error = 0;
1471
1472 out:
1473         if (state)
1474                 xfs_da_state_free(state);
1475         if (error)
1476                 return(error);
1477         return(retval);
1478 }
1479
1480 /*
1481  * Remove a name from a B-tree attribute list.
1482  *
1483  * This will involve walking down the Btree, and may involve joining
1484  * leaf nodes and even joining intermediate nodes up to and including
1485  * the root node (a special case of an intermediate node).
1486  */
1487 STATIC int
1488 xfs_attr_node_removename(xfs_da_args_t *args)
1489 {
1490         xfs_da_state_t *state;
1491         xfs_da_state_blk_t *blk;
1492         xfs_inode_t *dp;
1493         xfs_dabuf_t *bp;
1494         int retval, error, committed, forkoff;
1495
1496         trace_xfs_attr_node_removename(args);
1497
1498         /*
1499          * Tie a string around our finger to remind us where we are.
1500          */
1501         dp = args->dp;
1502         state = xfs_da_state_alloc();
1503         state->args = args;
1504         state->mp = dp->i_mount;
1505         state->blocksize = state->mp->m_sb.sb_blocksize;
1506         state->node_ents = state->mp->m_attr_node_ents;
1507
1508         /*
1509          * Search to see if name exists, and get back a pointer to it.
1510          */
1511         error = xfs_da_node_lookup_int(state, &retval);
1512         if (error || (retval != EEXIST)) {
1513                 if (error == 0)
1514                         error = retval;
1515                 goto out;
1516         }
1517
1518         /*
1519          * If there is an out-of-line value, de-allocate the blocks.
1520          * This is done before we remove the attribute so that we don't
1521          * overflow the maximum size of a transaction and/or hit a deadlock.
1522          */
1523         blk = &state->path.blk[ state->path.active-1 ];
1524         ASSERT(blk->bp != NULL);
1525         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1526         if (args->rmtblkno > 0) {
1527                 /*
1528                  * Fill in disk block numbers in the state structure
1529                  * so that we can get the buffers back after we commit
1530                  * several transactions in the following calls.
1531                  */
1532                 error = xfs_attr_fillstate(state);
1533                 if (error)
1534                         goto out;
1535
1536                 /*
1537                  * Mark the attribute as INCOMPLETE, then bunmapi() the
1538                  * remote value.
1539                  */
1540                 error = xfs_attr_leaf_setflag(args);
1541                 if (error)
1542                         goto out;
1543                 error = xfs_attr_rmtval_remove(args);
1544                 if (error)
1545                         goto out;
1546
1547                 /*
1548                  * Refill the state structure with buffers, the prior calls
1549                  * released our buffers.
1550                  */
1551                 error = xfs_attr_refillstate(state);
1552                 if (error)
1553                         goto out;
1554         }
1555
1556         /*
1557          * Remove the name and update the hashvals in the tree.
1558          */
1559         blk = &state->path.blk[ state->path.active-1 ];
1560         ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1561         retval = xfs_attr_leaf_remove(blk->bp, args);
1562         xfs_da_fixhashpath(state, &state->path);
1563
1564         /*
1565          * Check to see if the tree needs to be collapsed.
1566          */
1567         if (retval && (state->path.active > 1)) {
1568                 xfs_bmap_init(args->flist, args->firstblock);
1569                 error = xfs_da_join(state);
1570                 if (!error) {
1571                         error = xfs_bmap_finish(&args->trans, args->flist,
1572                                                 &committed);
1573                 }
1574                 if (error) {
1575                         ASSERT(committed);
1576                         args->trans = NULL;
1577                         xfs_bmap_cancel(args->flist);
1578                         goto out;
1579                 }
1580
1581                 /*
1582                  * bmap_finish() may have committed the last trans and started
1583                  * a new one.  We need the inode to be in all transactions.
1584                  */
1585                 if (committed)
1586                         xfs_trans_ijoin(args->trans, dp, 0);
1587
1588                 /*
1589                  * Commit the Btree join operation and start a new trans.
1590                  */
1591                 error = xfs_trans_roll(&args->trans, dp);
1592                 if (error)
1593                         goto out;
1594         }
1595
1596         /*
1597          * If the result is small enough, push it all into the inode.
1598          */
1599         if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1600                 /*
1601                  * Have to get rid of the copy of this dabuf in the state.
1602                  */
1603                 ASSERT(state->path.active == 1);
1604                 ASSERT(state->path.blk[0].bp);
1605                 xfs_da_buf_done(state->path.blk[0].bp);
1606                 state->path.blk[0].bp = NULL;
1607
1608                 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1609                                                      XFS_ATTR_FORK);
1610                 if (error)
1611                         goto out;
1612                 ASSERT((((xfs_attr_leafblock_t *)bp->data)->hdr.info.magic) ==
1613                        cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1614
1615                 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1616                         xfs_bmap_init(args->flist, args->firstblock);
1617                         error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1618                         /* bp is gone due to xfs_da_shrink_inode */
1619                         if (!error) {
1620                                 error = xfs_bmap_finish(&args->trans,
1621                                                         args->flist,
1622                                                         &committed);
1623                         }
1624                         if (error) {
1625                                 ASSERT(committed);
1626                                 args->trans = NULL;
1627                                 xfs_bmap_cancel(args->flist);
1628                                 goto out;
1629                         }
1630
1631                         /*
1632                          * bmap_finish() may have committed the last trans
1633                          * and started a new one.  We need the inode to be
1634                          * in all transactions.
1635                          */
1636                         if (committed)
1637                                 xfs_trans_ijoin(args->trans, dp, 0);
1638                 } else
1639                         xfs_da_brelse(args->trans, bp);
1640         }
1641         error = 0;
1642
1643 out:
1644         xfs_da_state_free(state);
1645         return(error);
1646 }
1647
1648 /*
1649  * Fill in the disk block numbers in the state structure for the buffers
1650  * that are attached to the state structure.
1651  * This is done so that we can quickly reattach ourselves to those buffers
1652  * after some set of transaction commits have released these buffers.
1653  */
1654 STATIC int
1655 xfs_attr_fillstate(xfs_da_state_t *state)
1656 {
1657         xfs_da_state_path_t *path;
1658         xfs_da_state_blk_t *blk;
1659         int level;
1660
1661         /*
1662          * Roll down the "path" in the state structure, storing the on-disk
1663          * block number for those buffers in the "path".
1664          */
1665         path = &state->path;
1666         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1667         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1668                 if (blk->bp) {
1669                         blk->disk_blkno = xfs_da_blkno(blk->bp);
1670                         xfs_da_buf_done(blk->bp);
1671                         blk->bp = NULL;
1672                 } else {
1673                         blk->disk_blkno = 0;
1674                 }
1675         }
1676
1677         /*
1678          * Roll down the "altpath" in the state structure, storing the on-disk
1679          * block number for those buffers in the "altpath".
1680          */
1681         path = &state->altpath;
1682         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1683         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1684                 if (blk->bp) {
1685                         blk->disk_blkno = xfs_da_blkno(blk->bp);
1686                         xfs_da_buf_done(blk->bp);
1687                         blk->bp = NULL;
1688                 } else {
1689                         blk->disk_blkno = 0;
1690                 }
1691         }
1692
1693         return(0);
1694 }
1695
1696 /*
1697  * Reattach the buffers to the state structure based on the disk block
1698  * numbers stored in the state structure.
1699  * This is done after some set of transaction commits have released those
1700  * buffers from our grip.
1701  */
1702 STATIC int
1703 xfs_attr_refillstate(xfs_da_state_t *state)
1704 {
1705         xfs_da_state_path_t *path;
1706         xfs_da_state_blk_t *blk;
1707         int level, error;
1708
1709         /*
1710          * Roll down the "path" in the state structure, storing the on-disk
1711          * block number for those buffers in the "path".
1712          */
1713         path = &state->path;
1714         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1715         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1716                 if (blk->disk_blkno) {
1717                         error = xfs_da_read_buf(state->args->trans,
1718                                                 state->args->dp,
1719                                                 blk->blkno, blk->disk_blkno,
1720                                                 &blk->bp, XFS_ATTR_FORK);
1721                         if (error)
1722                                 return(error);
1723                 } else {
1724                         blk->bp = NULL;
1725                 }
1726         }
1727
1728         /*
1729          * Roll down the "altpath" in the state structure, storing the on-disk
1730          * block number for those buffers in the "altpath".
1731          */
1732         path = &state->altpath;
1733         ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1734         for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1735                 if (blk->disk_blkno) {
1736                         error = xfs_da_read_buf(state->args->trans,
1737                                                 state->args->dp,
1738                                                 blk->blkno, blk->disk_blkno,
1739                                                 &blk->bp, XFS_ATTR_FORK);
1740                         if (error)
1741                                 return(error);
1742                 } else {
1743                         blk->bp = NULL;
1744                 }
1745         }
1746
1747         return(0);
1748 }
1749
1750 /*
1751  * Look up a filename in a node attribute list.
1752  *
1753  * This routine gets called for any attribute fork that has more than one
1754  * block, ie: both true Btree attr lists and for single-leaf-blocks with
1755  * "remote" values taking up more blocks.
1756  */
1757 STATIC int
1758 xfs_attr_node_get(xfs_da_args_t *args)
1759 {
1760         xfs_da_state_t *state;
1761         xfs_da_state_blk_t *blk;
1762         int error, retval;
1763         int i;
1764
1765         state = xfs_da_state_alloc();
1766         state->args = args;
1767         state->mp = args->dp->i_mount;
1768         state->blocksize = state->mp->m_sb.sb_blocksize;
1769         state->node_ents = state->mp->m_attr_node_ents;
1770
1771         /*
1772          * Search to see if name exists, and get back a pointer to it.
1773          */
1774         error = xfs_da_node_lookup_int(state, &retval);
1775         if (error) {
1776                 retval = error;
1777         } else if (retval == EEXIST) {
1778                 blk = &state->path.blk[ state->path.active-1 ];
1779                 ASSERT(blk->bp != NULL);
1780                 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1781
1782                 /*
1783                  * Get the value, local or "remote"
1784                  */
1785                 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1786                 if (!retval && (args->rmtblkno > 0)
1787                     && !(args->flags & ATTR_KERNOVAL)) {
1788                         retval = xfs_attr_rmtval_get(args);
1789                 }
1790         }
1791
1792         /*
1793          * If not in a transaction, we have to release all the buffers.
1794          */
1795         for (i = 0; i < state->path.active; i++) {
1796                 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1797                 state->path.blk[i].bp = NULL;
1798         }
1799
1800         xfs_da_state_free(state);
1801         return(retval);
1802 }
1803
1804 STATIC int                                                      /* error */
1805 xfs_attr_node_list(xfs_attr_list_context_t *context)
1806 {
1807         attrlist_cursor_kern_t *cursor;
1808         xfs_attr_leafblock_t *leaf;
1809         xfs_da_intnode_t *node;
1810         xfs_da_node_entry_t *btree;
1811         int error, i;
1812         xfs_dabuf_t *bp;
1813
1814         cursor = context->cursor;
1815         cursor->initted = 1;
1816
1817         /*
1818          * Do all sorts of validation on the passed-in cursor structure.
1819          * If anything is amiss, ignore the cursor and look up the hashval
1820          * starting from the btree root.
1821          */
1822         bp = NULL;
1823         if (cursor->blkno > 0) {
1824                 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1825                                               &bp, XFS_ATTR_FORK);
1826                 if ((error != 0) && (error != EFSCORRUPTED))
1827                         return(error);
1828                 if (bp) {
1829                         node = bp->data;
1830                         switch (be16_to_cpu(node->hdr.info.magic)) {
1831                         case XFS_DA_NODE_MAGIC:
1832                                 trace_xfs_attr_list_wrong_blk(context);
1833                                 xfs_da_brelse(NULL, bp);
1834                                 bp = NULL;
1835                                 break;
1836                         case XFS_ATTR_LEAF_MAGIC:
1837                                 leaf = bp->data;
1838                                 if (cursor->hashval > be32_to_cpu(leaf->entries[
1839                                     be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1840                                         trace_xfs_attr_list_wrong_blk(context);
1841                                         xfs_da_brelse(NULL, bp);
1842                                         bp = NULL;
1843                                 } else if (cursor->hashval <=
1844                                              be32_to_cpu(leaf->entries[0].hashval)) {
1845                                         trace_xfs_attr_list_wrong_blk(context);
1846                                         xfs_da_brelse(NULL, bp);
1847                                         bp = NULL;
1848                                 }
1849                                 break;
1850                         default:
1851                                 trace_xfs_attr_list_wrong_blk(context);
1852                                 xfs_da_brelse(NULL, bp);
1853                                 bp = NULL;
1854                         }
1855                 }
1856         }
1857
1858         /*
1859          * We did not find what we expected given the cursor's contents,
1860          * so we start from the top and work down based on the hash value.
1861          * Note that start of node block is same as start of leaf block.
1862          */
1863         if (bp == NULL) {
1864                 cursor->blkno = 0;
1865                 for (;;) {
1866                         error = xfs_da_read_buf(NULL, context->dp,
1867                                                       cursor->blkno, -1, &bp,
1868                                                       XFS_ATTR_FORK);
1869                         if (error)
1870                                 return(error);
1871                         if (unlikely(bp == NULL)) {
1872                                 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1873                                                  XFS_ERRLEVEL_LOW,
1874                                                  context->dp->i_mount);
1875                                 return(XFS_ERROR(EFSCORRUPTED));
1876                         }
1877                         node = bp->data;
1878                         if (node->hdr.info.magic ==
1879                             cpu_to_be16(XFS_ATTR_LEAF_MAGIC))
1880                                 break;
1881                         if (unlikely(node->hdr.info.magic !=
1882                                      cpu_to_be16(XFS_DA_NODE_MAGIC))) {
1883                                 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1884                                                      XFS_ERRLEVEL_LOW,
1885                                                      context->dp->i_mount,
1886                                                      node);
1887                                 xfs_da_brelse(NULL, bp);
1888                                 return(XFS_ERROR(EFSCORRUPTED));
1889                         }
1890                         btree = node->btree;
1891                         for (i = 0; i < be16_to_cpu(node->hdr.count);
1892                                                                 btree++, i++) {
1893                                 if (cursor->hashval
1894                                                 <= be32_to_cpu(btree->hashval)) {
1895                                         cursor->blkno = be32_to_cpu(btree->before);
1896                                         trace_xfs_attr_list_node_descend(context,
1897                                                                          btree);
1898                                         break;
1899                                 }
1900                         }
1901                         if (i == be16_to_cpu(node->hdr.count)) {
1902                                 xfs_da_brelse(NULL, bp);
1903                                 return(0);
1904                         }
1905                         xfs_da_brelse(NULL, bp);
1906                 }
1907         }
1908         ASSERT(bp != NULL);
1909
1910         /*
1911          * Roll upward through the blocks, processing each leaf block in
1912          * order.  As long as there is space in the result buffer, keep
1913          * adding the information.
1914          */
1915         for (;;) {
1916                 leaf = bp->data;
1917                 if (unlikely(leaf->hdr.info.magic !=
1918                              cpu_to_be16(XFS_ATTR_LEAF_MAGIC))) {
1919                         XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1920                                              XFS_ERRLEVEL_LOW,
1921                                              context->dp->i_mount, leaf);
1922                         xfs_da_brelse(NULL, bp);
1923                         return(XFS_ERROR(EFSCORRUPTED));
1924                 }
1925                 error = xfs_attr_leaf_list_int(bp, context);
1926                 if (error) {
1927                         xfs_da_brelse(NULL, bp);
1928                         return error;
1929                 }
1930                 if (context->seen_enough || leaf->hdr.info.forw == 0)
1931                         break;
1932                 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1933                 xfs_da_brelse(NULL, bp);
1934                 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1935                                               &bp, XFS_ATTR_FORK);
1936                 if (error)
1937                         return(error);
1938                 if (unlikely((bp == NULL))) {
1939                         XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1940                                          XFS_ERRLEVEL_LOW,
1941                                          context->dp->i_mount);
1942                         return(XFS_ERROR(EFSCORRUPTED));
1943                 }
1944         }
1945         xfs_da_brelse(NULL, bp);
1946         return(0);
1947 }
1948
1949
1950 /*========================================================================
1951  * External routines for manipulating out-of-line attribute values.
1952  *========================================================================*/
1953
1954 /*
1955  * Read the value associated with an attribute from the out-of-line buffer
1956  * that we stored it in.
1957  */
1958 int
1959 xfs_attr_rmtval_get(xfs_da_args_t *args)
1960 {
1961         xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1962         xfs_mount_t *mp;
1963         xfs_daddr_t dblkno;
1964         void *dst;
1965         xfs_buf_t *bp;
1966         int nmap, error, tmp, valuelen, blkcnt, i;
1967         xfs_dablk_t lblkno;
1968
1969         ASSERT(!(args->flags & ATTR_KERNOVAL));
1970
1971         mp = args->dp->i_mount;
1972         dst = args->value;
1973         valuelen = args->valuelen;
1974         lblkno = args->rmtblkno;
1975         while (valuelen > 0) {
1976                 nmap = ATTR_RMTVALUE_MAPSIZE;
1977                 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
1978                                        args->rmtblkcnt, map, &nmap,
1979                                        XFS_BMAPI_ATTRFORK);
1980                 if (error)
1981                         return(error);
1982                 ASSERT(nmap >= 1);
1983
1984                 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
1985                         ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
1986                                (map[i].br_startblock != HOLESTARTBLOCK));
1987                         dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
1988                         blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
1989                         error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1990                                                    dblkno, blkcnt, 0, &bp);
1991                         if (error)
1992                                 return(error);
1993
1994                         tmp = min_t(int, valuelen, BBTOB(bp->b_length));
1995                         xfs_buf_iomove(bp, 0, tmp, dst, XBRW_READ);
1996                         xfs_buf_relse(bp);
1997                         dst += tmp;
1998                         valuelen -= tmp;
1999
2000                         lblkno += map[i].br_blockcount;
2001                 }
2002         }
2003         ASSERT(valuelen == 0);
2004         return(0);
2005 }
2006
2007 /*
2008  * Write the value associated with an attribute into the out-of-line buffer
2009  * that we have defined for it.
2010  */
2011 STATIC int
2012 xfs_attr_rmtval_set(xfs_da_args_t *args)
2013 {
2014         xfs_mount_t *mp;
2015         xfs_fileoff_t lfileoff;
2016         xfs_inode_t *dp;
2017         xfs_bmbt_irec_t map;
2018         xfs_daddr_t dblkno;
2019         void *src;
2020         xfs_buf_t *bp;
2021         xfs_dablk_t lblkno;
2022         int blkcnt, valuelen, nmap, error, tmp, committed;
2023
2024         dp = args->dp;
2025         mp = dp->i_mount;
2026         src = args->value;
2027
2028         /*
2029          * Find a "hole" in the attribute address space large enough for
2030          * us to drop the new attribute's value into.
2031          */
2032         blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2033         lfileoff = 0;
2034         error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2035                                                    XFS_ATTR_FORK);
2036         if (error) {
2037                 return(error);
2038         }
2039         args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2040         args->rmtblkcnt = blkcnt;
2041
2042         /*
2043          * Roll through the "value", allocating blocks on disk as required.
2044          */
2045         while (blkcnt > 0) {
2046                 /*
2047                  * Allocate a single extent, up to the size of the value.
2048                  */
2049                 xfs_bmap_init(args->flist, args->firstblock);
2050                 nmap = 1;
2051                 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
2052                                   blkcnt,
2053                                   XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2054                                   args->firstblock, args->total, &map, &nmap,
2055                                   args->flist);
2056                 if (!error) {
2057                         error = xfs_bmap_finish(&args->trans, args->flist,
2058                                                 &committed);
2059                 }
2060                 if (error) {
2061                         ASSERT(committed);
2062                         args->trans = NULL;
2063                         xfs_bmap_cancel(args->flist);
2064                         return(error);
2065                 }
2066
2067                 /*
2068                  * bmap_finish() may have committed the last trans and started
2069                  * a new one.  We need the inode to be in all transactions.
2070                  */
2071                 if (committed)
2072                         xfs_trans_ijoin(args->trans, dp, 0);
2073
2074                 ASSERT(nmap == 1);
2075                 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2076                        (map.br_startblock != HOLESTARTBLOCK));
2077                 lblkno += map.br_blockcount;
2078                 blkcnt -= map.br_blockcount;
2079
2080                 /*
2081                  * Start the next trans in the chain.
2082                  */
2083                 error = xfs_trans_roll(&args->trans, dp);
2084                 if (error)
2085                         return (error);
2086         }
2087
2088         /*
2089          * Roll through the "value", copying the attribute value to the
2090          * already-allocated blocks.  Blocks are written synchronously
2091          * so that we can know they are all on disk before we turn off
2092          * the INCOMPLETE flag.
2093          */
2094         lblkno = args->rmtblkno;
2095         valuelen = args->valuelen;
2096         while (valuelen > 0) {
2097                 int buflen;
2098
2099                 /*
2100                  * Try to remember where we decided to put the value.
2101                  */
2102                 xfs_bmap_init(args->flist, args->firstblock);
2103                 nmap = 1;
2104                 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
2105                                        args->rmtblkcnt, &map, &nmap,
2106                                        XFS_BMAPI_ATTRFORK);
2107                 if (error)
2108                         return(error);
2109                 ASSERT(nmap == 1);
2110                 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2111                        (map.br_startblock != HOLESTARTBLOCK));
2112
2113                 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2114                 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2115
2116                 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
2117                 if (!bp)
2118                         return ENOMEM;
2119
2120                 buflen = BBTOB(bp->b_length);
2121                 tmp = min_t(int, valuelen, buflen);
2122                 xfs_buf_iomove(bp, 0, tmp, src, XBRW_WRITE);
2123                 if (tmp < buflen)
2124                         xfs_buf_zero(bp, tmp, buflen - tmp);
2125
2126                 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
2127                 xfs_buf_relse(bp);
2128                 if (error)
2129                         return error;
2130                 src += tmp;
2131                 valuelen -= tmp;
2132
2133                 lblkno += map.br_blockcount;
2134         }
2135         ASSERT(valuelen == 0);
2136         return(0);
2137 }
2138
2139 /*
2140  * Remove the value associated with an attribute by deleting the
2141  * out-of-line buffer that it is stored on.
2142  */
2143 STATIC int
2144 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2145 {
2146         xfs_mount_t *mp;
2147         xfs_bmbt_irec_t map;
2148         xfs_buf_t *bp;
2149         xfs_daddr_t dblkno;
2150         xfs_dablk_t lblkno;
2151         int valuelen, blkcnt, nmap, error, done, committed;
2152
2153         mp = args->dp->i_mount;
2154
2155         /*
2156          * Roll through the "value", invalidating the attribute value's
2157          * blocks.
2158          */
2159         lblkno = args->rmtblkno;
2160         valuelen = args->rmtblkcnt;
2161         while (valuelen > 0) {
2162                 /*
2163                  * Try to remember where we decided to put the value.
2164                  */
2165                 nmap = 1;
2166                 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
2167                                        args->rmtblkcnt, &map, &nmap,
2168                                        XFS_BMAPI_ATTRFORK);
2169                 if (error)
2170                         return(error);
2171                 ASSERT(nmap == 1);
2172                 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2173                        (map.br_startblock != HOLESTARTBLOCK));
2174
2175                 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2176                 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2177
2178                 /*
2179                  * If the "remote" value is in the cache, remove it.
2180                  */
2181                 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
2182                 if (bp) {
2183                         xfs_buf_stale(bp);
2184                         xfs_buf_relse(bp);
2185                         bp = NULL;
2186                 }
2187
2188                 valuelen -= map.br_blockcount;
2189
2190                 lblkno += map.br_blockcount;
2191         }
2192
2193         /*
2194          * Keep de-allocating extents until the remote-value region is gone.
2195          */
2196         lblkno = args->rmtblkno;
2197         blkcnt = args->rmtblkcnt;
2198         done = 0;
2199         while (!done) {
2200                 xfs_bmap_init(args->flist, args->firstblock);
2201                 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2202                                     XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2203                                     1, args->firstblock, args->flist,
2204                                     &done);
2205                 if (!error) {
2206                         error = xfs_bmap_finish(&args->trans, args->flist,
2207                                                 &committed);
2208                 }
2209                 if (error) {
2210                         ASSERT(committed);
2211                         args->trans = NULL;
2212                         xfs_bmap_cancel(args->flist);
2213                         return(error);
2214                 }
2215
2216                 /*
2217                  * bmap_finish() may have committed the last trans and started
2218                  * a new one.  We need the inode to be in all transactions.
2219                  */
2220                 if (committed)
2221                         xfs_trans_ijoin(args->trans, args->dp, 0);
2222
2223                 /*
2224                  * Close out trans and start the next one in the chain.
2225                  */
2226                 error = xfs_trans_roll(&args->trans, args->dp);
2227                 if (error)
2228                         return (error);
2229         }
2230         return(0);
2231 }