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