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