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