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