AUDIT: Speed up audit_filter_syscall() for the non-auditable case.
[linux-2.6/suspend2-2.6.18.git] / fs / xfs / xfs_attr.c
bloba41ad3a5e5540af909dcc446357da97b5111baef
1 /*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include "xfs.h"
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"
69 * xfs_attr.c
71 * Provide the external interfaces to manage attribute lists.
74 #define ATTR_SYSCOUNT 2
75 STATIC struct attrnames posix_acl_access;
76 STATIC struct attrnames posix_acl_default;
77 STATIC struct attrnames *attr_system_names[ATTR_SYSCOUNT];
79 /*========================================================================
80 * Function prototypes for the kernel.
81 *========================================================================*/
84 * Internal routines when attribute list fits inside the inode.
86 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
89 * Internal routines when attribute list is one block.
91 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
92 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
93 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
94 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
97 * Internal routines when attribute list is more than one block.
99 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
100 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
101 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
102 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
103 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
104 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
107 * Routines to manipulate out-of-line attribute values.
109 STATIC int xfs_attr_rmtval_get(xfs_da_args_t *args);
110 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
111 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
113 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
115 #if defined(XFS_ATTR_TRACE)
116 ktrace_t *xfs_attr_trace_buf;
117 #endif
120 /*========================================================================
121 * Overall external interface routines.
122 *========================================================================*/
125 xfs_attr_fetch(xfs_inode_t *ip, char *name, int namelen,
126 char *value, int *valuelenp, int flags, struct cred *cred)
128 xfs_da_args_t args;
129 int error;
131 if ((XFS_IFORK_Q(ip) == 0) ||
132 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
133 ip->i_d.di_anextents == 0))
134 return(ENOATTR);
136 if (!(flags & (ATTR_KERNACCESS|ATTR_SECURE))) {
137 if ((error = xfs_iaccess(ip, S_IRUSR, cred)))
138 return(XFS_ERROR(error));
142 * Fill in the arg structure for this request.
144 memset((char *)&args, 0, sizeof(args));
145 args.name = name;
146 args.namelen = namelen;
147 args.value = value;
148 args.valuelen = *valuelenp;
149 args.flags = flags;
150 args.hashval = xfs_da_hashname(args.name, args.namelen);
151 args.dp = ip;
152 args.whichfork = XFS_ATTR_FORK;
155 * Decide on what work routines to call based on the inode size.
157 if (XFS_IFORK_Q(ip) == 0 ||
158 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
159 ip->i_d.di_anextents == 0)) {
160 error = XFS_ERROR(ENOATTR);
161 } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
162 error = xfs_attr_shortform_getvalue(&args);
163 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
164 error = xfs_attr_leaf_get(&args);
165 } else {
166 error = xfs_attr_node_get(&args);
170 * Return the number of bytes in the value to the caller.
172 *valuelenp = args.valuelen;
174 if (error == EEXIST)
175 error = 0;
176 return(error);
180 xfs_attr_get(bhv_desc_t *bdp, char *name, char *value, int *valuelenp,
181 int flags, struct cred *cred)
183 xfs_inode_t *ip = XFS_BHVTOI(bdp);
184 int error, namelen;
186 XFS_STATS_INC(xs_attr_get);
188 if (!name)
189 return(EINVAL);
190 namelen = strlen(name);
191 if (namelen >= MAXNAMELEN)
192 return(EFAULT); /* match IRIX behaviour */
194 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
195 return(EIO);
197 xfs_ilock(ip, XFS_ILOCK_SHARED);
198 error = xfs_attr_fetch(ip, name, namelen, value, valuelenp, flags, cred);
199 xfs_iunlock(ip, XFS_ILOCK_SHARED);
200 return(error);
203 /*ARGSUSED*/
204 int /* error */
205 xfs_attr_set(bhv_desc_t *bdp, char *name, char *value, int valuelen, int flags,
206 struct cred *cred)
208 xfs_da_args_t args;
209 xfs_inode_t *dp;
210 xfs_fsblock_t firstblock;
211 xfs_bmap_free_t flist;
212 int error, err2, committed;
213 int local, size;
214 uint nblks;
215 xfs_mount_t *mp;
216 int rsvd = (flags & ATTR_ROOT) != 0;
217 int namelen;
219 namelen = strlen(name);
220 if (namelen >= MAXNAMELEN)
221 return EFAULT; /* match IRIX behaviour */
223 XFS_STATS_INC(xs_attr_set);
225 dp = XFS_BHVTOI(bdp);
226 mp = dp->i_mount;
227 if (XFS_FORCED_SHUTDOWN(mp))
228 return (EIO);
230 xfs_ilock(dp, XFS_ILOCK_SHARED);
231 if (!(flags & ATTR_SECURE) &&
232 (error = xfs_iaccess(dp, S_IWUSR, cred))) {
233 xfs_iunlock(dp, XFS_ILOCK_SHARED);
234 return(XFS_ERROR(error));
236 xfs_iunlock(dp, XFS_ILOCK_SHARED);
239 * Attach the dquots to the inode.
241 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
242 return (error);
245 * If the inode doesn't have an attribute fork, add one.
246 * (inode must not be locked when we call this routine)
248 if (XFS_IFORK_Q(dp) == 0) {
249 error = xfs_bmap_add_attrfork(dp, rsvd);
250 if (error)
251 return(error);
255 * Fill in the arg structure for this request.
257 memset((char *)&args, 0, sizeof(args));
258 args.name = name;
259 args.namelen = namelen;
260 args.value = value;
261 args.valuelen = valuelen;
262 args.flags = flags;
263 args.hashval = xfs_da_hashname(args.name, args.namelen);
264 args.dp = dp;
265 args.firstblock = &firstblock;
266 args.flist = &flist;
267 args.whichfork = XFS_ATTR_FORK;
268 args.oknoent = 1;
270 /* Determine space new attribute will use, and if it will be inline
271 * or out of line.
273 size = xfs_attr_leaf_newentsize(&args, mp->m_sb.sb_blocksize, &local);
275 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
276 if (local) {
277 if (size > (mp->m_sb.sb_blocksize >> 1)) {
278 /* Double split possible */
279 nblks <<= 1;
281 } else {
282 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
283 /* Out of line attribute, cannot double split, but make
284 * room for the attribute value itself.
286 nblks += dblocks;
287 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
290 /* Size is now blocks for attribute data */
291 args.total = nblks;
294 * Start our first transaction of the day.
296 * All future transactions during this code must be "chained" off
297 * this one via the trans_dup() call. All transactions will contain
298 * the inode, and the inode will always be marked with trans_ihold().
299 * Since the inode will be locked in all transactions, we must log
300 * the inode in every transaction to let it float upward through
301 * the log.
303 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
306 * Root fork attributes can use reserved data blocks for this
307 * operation if necessary
310 if (rsvd)
311 args.trans->t_flags |= XFS_TRANS_RESERVE;
313 if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
314 XFS_ATTRSET_LOG_RES(mp, nblks),
315 0, XFS_TRANS_PERM_LOG_RES,
316 XFS_ATTRSET_LOG_COUNT))) {
317 xfs_trans_cancel(args.trans, 0);
318 return(error);
320 xfs_ilock(dp, XFS_ILOCK_EXCL);
322 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
323 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
324 XFS_QMOPT_RES_REGBLKS);
325 if (error) {
326 xfs_iunlock(dp, XFS_ILOCK_EXCL);
327 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
328 return (error);
331 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
332 xfs_trans_ihold(args.trans, dp);
335 * If the attribute list is non-existant or a shortform list,
336 * upgrade it to a single-leaf-block attribute list.
338 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
339 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
340 (dp->i_d.di_anextents == 0))) {
343 * Build initial attribute list (if required).
345 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
346 (void)xfs_attr_shortform_create(&args);
349 * Try to add the attr to the attribute list in
350 * the inode.
352 error = xfs_attr_shortform_addname(&args);
353 if (error != ENOSPC) {
355 * Commit the shortform mods, and we're done.
356 * NOTE: this is also the error path (EEXIST, etc).
358 ASSERT(args.trans != NULL);
361 * If this is a synchronous mount, make sure that
362 * the transaction goes to disk before returning
363 * to the user.
365 if (mp->m_flags & XFS_MOUNT_WSYNC) {
366 xfs_trans_set_sync(args.trans);
368 err2 = xfs_trans_commit(args.trans,
369 XFS_TRANS_RELEASE_LOG_RES,
370 NULL);
371 xfs_iunlock(dp, XFS_ILOCK_EXCL);
374 * Hit the inode change time.
376 if (!error && (flags & ATTR_KERNOTIME) == 0) {
377 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
379 return(error == 0 ? err2 : error);
383 * It won't fit in the shortform, transform to a leaf block.
384 * GROT: another possible req'mt for a double-split btree op.
386 XFS_BMAP_INIT(args.flist, args.firstblock);
387 error = xfs_attr_shortform_to_leaf(&args);
388 if (!error) {
389 error = xfs_bmap_finish(&args.trans, args.flist,
390 *args.firstblock, &committed);
392 if (error) {
393 ASSERT(committed);
394 args.trans = NULL;
395 xfs_bmap_cancel(&flist);
396 goto out;
400 * bmap_finish() may have committed the last trans and started
401 * a new one. We need the inode to be in all transactions.
403 if (committed) {
404 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
405 xfs_trans_ihold(args.trans, dp);
409 * Commit the leaf transformation. We'll need another (linked)
410 * transaction to add the new attribute to the leaf.
412 if ((error = xfs_attr_rolltrans(&args.trans, dp)))
413 goto out;
417 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
418 error = xfs_attr_leaf_addname(&args);
419 } else {
420 error = xfs_attr_node_addname(&args);
422 if (error) {
423 goto out;
427 * If this is a synchronous mount, make sure that the
428 * transaction goes to disk before returning to the user.
430 if (mp->m_flags & XFS_MOUNT_WSYNC) {
431 xfs_trans_set_sync(args.trans);
435 * Commit the last in the sequence of transactions.
437 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
438 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
439 NULL);
440 xfs_iunlock(dp, XFS_ILOCK_EXCL);
443 * Hit the inode change time.
445 if (!error && (flags & ATTR_KERNOTIME) == 0) {
446 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
449 return(error);
451 out:
452 if (args.trans)
453 xfs_trans_cancel(args.trans,
454 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
455 xfs_iunlock(dp, XFS_ILOCK_EXCL);
456 return(error);
460 * Generic handler routine to remove a name from an attribute list.
461 * Transitions attribute list from Btree to shortform as necessary.
463 /*ARGSUSED*/
464 int /* error */
465 xfs_attr_remove(bhv_desc_t *bdp, char *name, int flags, struct cred *cred)
467 xfs_da_args_t args;
468 xfs_inode_t *dp;
469 xfs_fsblock_t firstblock;
470 xfs_bmap_free_t flist;
471 int error;
472 xfs_mount_t *mp;
473 int namelen;
475 ASSERT(MAXNAMELEN-1<=0xff); /* length is stored in uint8 */
476 namelen = strlen(name);
477 if (namelen>=MAXNAMELEN)
478 return EFAULT; /* match irix behaviour */
480 XFS_STATS_INC(xs_attr_remove);
482 dp = XFS_BHVTOI(bdp);
483 mp = dp->i_mount;
484 if (XFS_FORCED_SHUTDOWN(mp))
485 return (EIO);
487 xfs_ilock(dp, XFS_ILOCK_SHARED);
488 if (!(flags & ATTR_SECURE) &&
489 (error = xfs_iaccess(dp, S_IWUSR, cred))) {
490 xfs_iunlock(dp, XFS_ILOCK_SHARED);
491 return(XFS_ERROR(error));
492 } else if (XFS_IFORK_Q(dp) == 0 ||
493 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
494 dp->i_d.di_anextents == 0)) {
495 xfs_iunlock(dp, XFS_ILOCK_SHARED);
496 return(XFS_ERROR(ENOATTR));
498 xfs_iunlock(dp, XFS_ILOCK_SHARED);
501 * Fill in the arg structure for this request.
503 memset((char *)&args, 0, sizeof(args));
504 args.name = name;
505 args.namelen = namelen;
506 args.flags = flags;
507 args.hashval = xfs_da_hashname(args.name, args.namelen);
508 args.dp = dp;
509 args.firstblock = &firstblock;
510 args.flist = &flist;
511 args.total = 0;
512 args.whichfork = XFS_ATTR_FORK;
515 * Attach the dquots to the inode.
517 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
518 return (error);
521 * Start our first transaction of the day.
523 * All future transactions during this code must be "chained" off
524 * this one via the trans_dup() call. All transactions will contain
525 * the inode, and the inode will always be marked with trans_ihold().
526 * Since the inode will be locked in all transactions, we must log
527 * the inode in every transaction to let it float upward through
528 * the log.
530 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
533 * Root fork attributes can use reserved data blocks for this
534 * operation if necessary
537 if (flags & ATTR_ROOT)
538 args.trans->t_flags |= XFS_TRANS_RESERVE;
540 if ((error = xfs_trans_reserve(args.trans,
541 XFS_ATTRRM_SPACE_RES(mp),
542 XFS_ATTRRM_LOG_RES(mp),
543 0, XFS_TRANS_PERM_LOG_RES,
544 XFS_ATTRRM_LOG_COUNT))) {
545 xfs_trans_cancel(args.trans, 0);
546 return(error);
550 xfs_ilock(dp, XFS_ILOCK_EXCL);
552 * No need to make quota reservations here. We expect to release some
553 * blocks not allocate in the common case.
555 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
556 xfs_trans_ihold(args.trans, dp);
559 * Decide on what work routines to call based on the inode size.
561 if (XFS_IFORK_Q(dp) == 0 ||
562 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
563 dp->i_d.di_anextents == 0)) {
564 error = XFS_ERROR(ENOATTR);
565 goto out;
567 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
568 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
569 error = xfs_attr_shortform_remove(&args);
570 if (error) {
571 goto out;
573 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
574 error = xfs_attr_leaf_removename(&args);
575 } else {
576 error = xfs_attr_node_removename(&args);
578 if (error) {
579 goto out;
583 * If this is a synchronous mount, make sure that the
584 * transaction goes to disk before returning to the user.
586 if (mp->m_flags & XFS_MOUNT_WSYNC) {
587 xfs_trans_set_sync(args.trans);
591 * Commit the last in the sequence of transactions.
593 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
594 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
595 NULL);
596 xfs_iunlock(dp, XFS_ILOCK_EXCL);
599 * Hit the inode change time.
601 if (!error && (flags & ATTR_KERNOTIME) == 0) {
602 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
605 return(error);
607 out:
608 if (args.trans)
609 xfs_trans_cancel(args.trans,
610 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
611 xfs_iunlock(dp, XFS_ILOCK_EXCL);
612 return(error);
616 * Generate a list of extended attribute names and optionally
617 * also value lengths. Positive return value follows the XFS
618 * convention of being an error, zero or negative return code
619 * is the length of the buffer returned (negated), indicating
620 * success.
623 xfs_attr_list(bhv_desc_t *bdp, char *buffer, int bufsize, int flags,
624 attrlist_cursor_kern_t *cursor, struct cred *cred)
626 xfs_attr_list_context_t context;
627 xfs_inode_t *dp;
628 int error;
630 XFS_STATS_INC(xs_attr_list);
633 * Validate the cursor.
635 if (cursor->pad1 || cursor->pad2)
636 return(XFS_ERROR(EINVAL));
637 if ((cursor->initted == 0) &&
638 (cursor->hashval || cursor->blkno || cursor->offset))
639 return(XFS_ERROR(EINVAL));
642 * Check for a properly aligned buffer.
644 if (((long)buffer) & (sizeof(int)-1))
645 return(XFS_ERROR(EFAULT));
646 if (flags & ATTR_KERNOVAL)
647 bufsize = 0;
650 * Initialize the output buffer.
652 context.dp = dp = XFS_BHVTOI(bdp);
653 context.cursor = cursor;
654 context.count = 0;
655 context.dupcnt = 0;
656 context.resynch = 1;
657 context.flags = flags;
658 if (!(flags & ATTR_KERNAMELS)) {
659 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
660 context.firstu = context.bufsize;
661 context.alist = (attrlist_t *)buffer;
662 context.alist->al_count = 0;
663 context.alist->al_more = 0;
664 context.alist->al_offset[0] = context.bufsize;
666 else {
667 context.bufsize = bufsize;
668 context.firstu = context.bufsize;
669 context.alist = (attrlist_t *)buffer;
672 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
673 return (EIO);
675 xfs_ilock(dp, XFS_ILOCK_SHARED);
676 if (!(flags & ATTR_SECURE) &&
677 (error = xfs_iaccess(dp, S_IRUSR, cred))) {
678 xfs_iunlock(dp, XFS_ILOCK_SHARED);
679 return(XFS_ERROR(error));
683 * Decide on what work routines to call based on the inode size.
685 xfs_attr_trace_l_c("syscall start", &context);
686 if (XFS_IFORK_Q(dp) == 0 ||
687 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
688 dp->i_d.di_anextents == 0)) {
689 error = 0;
690 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
691 error = xfs_attr_shortform_list(&context);
692 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
693 error = xfs_attr_leaf_list(&context);
694 } else {
695 error = xfs_attr_node_list(&context);
697 xfs_iunlock(dp, XFS_ILOCK_SHARED);
698 xfs_attr_trace_l_c("syscall end", &context);
700 if (!(context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS))) {
701 ASSERT(error >= 0);
703 else { /* must return negated buffer size or the error */
704 if (context.count < 0)
705 error = XFS_ERROR(ERANGE);
706 else
707 error = -context.count;
710 return(error);
713 int /* error */
714 xfs_attr_inactive(xfs_inode_t *dp)
716 xfs_trans_t *trans;
717 xfs_mount_t *mp;
718 int error;
720 mp = dp->i_mount;
721 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
723 xfs_ilock(dp, XFS_ILOCK_SHARED);
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_SHARED);
729 return(0);
731 xfs_iunlock(dp, XFS_ILOCK_SHARED);
734 * Start our first transaction of the day.
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.
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);
750 xfs_ilock(dp, XFS_ILOCK_EXCL);
753 * No need to make quota reservations here. We expect to release some
754 * blocks, not allocate, in the common case.
756 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
757 xfs_trans_ihold(trans, dp);
760 * Decide on what work routines to call based on the inode size.
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;
769 error = xfs_attr_root_inactive(&trans, dp);
770 if (error)
771 goto out;
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.
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;
786 * Commit the last in the sequence of transactions.
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);
793 return(error);
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);
803 /*========================================================================
804 * External routines when attribute list is inside the inode
805 *========================================================================*/
808 * Add a name to the shortform attribute list structure
809 * This is the external routine.
811 STATIC int
812 xfs_attr_shortform_addname(xfs_da_args_t *args)
814 int newsize, retval;
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);
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));
836 return(0);
840 /*========================================================================
841 * External routines when attribute list is one block
842 *========================================================================*/
845 * Add a name to the leaf attribute list structure
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).
851 xfs_attr_leaf_addname(xfs_da_args_t *args)
853 xfs_inode_t *dp;
854 xfs_dabuf_t *bp;
855 int retval, error, committed;
858 * Read the (only) block in the attribute list in.
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);
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.
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);
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;
889 * Add the attribute to the leaf block, transitioning to a Btree
890 * if required.
892 retval = xfs_attr_leaf_add(bp, args);
893 xfs_da_buf_done(bp);
894 if (retval == ENOSPC) {
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.
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);
906 if (error) {
907 ASSERT(committed);
908 args->trans = NULL;
909 xfs_bmap_cancel(args->flist);
910 return(error);
914 * bmap_finish() may have committed the last trans and started
915 * a new one. We need the inode to be in all transactions.
917 if (committed) {
918 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
919 xfs_trans_ihold(args->trans, dp);
923 * Commit the current trans (including the inode) and start
924 * a new one.
926 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
927 return (error);
930 * Fob the whole rest of the problem off on the Btree code.
932 error = xfs_attr_node_addname(args);
933 return(error);
937 * Commit the transaction that added the attr name so that
938 * later routines can manage their own transactions.
940 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
941 return (error);
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.
949 if (args->rmtblkno > 0) {
950 error = xfs_attr_rmtval_set(args);
951 if (error)
952 return(error);
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.
961 if (args->rename) {
963 * In a separate transaction, set the incomplete flag on the
964 * "old" attr and clear the incomplete flag on the "new" attr.
966 error = xfs_attr_leaf_flipflags(args);
967 if (error)
968 return(error);
971 * Dismantle the "old" attribute/value pair by removing
972 * a "remote" value (if it exists).
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);
985 * Read in the block containing the "old" attr, then
986 * remove the "old" attr from that block (neat, huh!)
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);
996 * If the result is small enough, shrink it all into the inode.
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);
1008 if (error) {
1009 ASSERT(committed);
1010 args->trans = NULL;
1011 xfs_bmap_cancel(args->flist);
1012 return(error);
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.
1020 if (committed) {
1021 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1022 xfs_trans_ihold(args->trans, dp);
1024 } else
1025 xfs_da_buf_done(bp);
1028 * Commit the remove and start the next trans in series.
1030 error = xfs_attr_rolltrans(&args->trans, dp);
1032 } else if (args->rmtblkno > 0) {
1034 * Added a "remote" value, just clear the incomplete flag.
1036 error = xfs_attr_leaf_clearflag(args);
1038 return(error);
1042 * Remove a name from the leaf attribute list structure
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).
1047 STATIC int
1048 xfs_attr_leaf_removename(xfs_da_args_t *args)
1050 xfs_inode_t *dp;
1051 xfs_dabuf_t *bp;
1052 int committed;
1053 int error;
1056 * Remove the attribute.
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);
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);
1073 (void)xfs_attr_leaf_remove(bp, args);
1076 * If the result is small enough, shrink it all into the inode.
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);
1086 if (error) {
1087 ASSERT(committed);
1088 args->trans = NULL;
1089 xfs_bmap_cancel(args->flist);
1090 return(error);
1094 * bmap_finish() may have committed the last trans and started
1095 * a new one. We need the inode to be in all transactions.
1097 if (committed) {
1098 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1099 xfs_trans_ihold(args->trans, dp);
1101 } else
1102 xfs_da_buf_done(bp);
1103 return(0);
1107 * Look up a name in a leaf attribute list structure.
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).
1112 STATIC int
1113 xfs_attr_leaf_get(xfs_da_args_t *args)
1115 xfs_dabuf_t *bp;
1116 int error;
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);
1125 error = xfs_attr_leaf_lookup_int(bp, args);
1126 if (error != EEXIST) {
1127 xfs_da_brelse(args->trans, bp);
1128 return(error);
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);
1135 return(error);
1139 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1141 STATIC int
1142 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1144 xfs_attr_leafblock_t *leaf;
1145 int error;
1146 xfs_dabuf_t *bp;
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));
1162 (void)xfs_attr_leaf_list_int(bp, context);
1163 xfs_da_brelse(NULL, bp);
1164 return(0);
1168 /*========================================================================
1169 * External routines when attribute list size > XFS_LBSIZE(mp).
1170 *========================================================================*/
1173 * Add a name to a Btree-format attribute list.
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).
1179 * "Remote" attribute values confuse the issue and atomic rename operations
1180 * add a whole extra layer of confusion on top of that.
1182 STATIC int
1183 xfs_attr_node_addname(xfs_da_args_t *args)
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;
1192 * Fill in bucket of arguments/results/context to carry around.
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;
1204 * Search to see if name already exists, and get back a pointer
1205 * to where it should go.
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;
1226 retval = xfs_attr_leaf_add(blk->bp, state->args);
1227 if (retval == ENOSPC) {
1228 if (state->path.active == 1) {
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.
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);
1243 if (error) {
1244 ASSERT(committed);
1245 args->trans = NULL;
1246 xfs_bmap_cancel(args->flist);
1247 goto out;
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.
1255 if (committed) {
1256 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1257 xfs_trans_ihold(args->trans, dp);
1261 * Commit the node conversion and start the next
1262 * trans in the chain.
1264 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1265 goto out;
1267 goto restart;
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.
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);
1282 if (error) {
1283 ASSERT(committed);
1284 args->trans = NULL;
1285 xfs_bmap_cancel(args->flist);
1286 goto out;
1290 * bmap_finish() may have committed the last trans and started
1291 * a new one. We need the inode to be in all transactions.
1293 if (committed) {
1294 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1295 xfs_trans_ihold(args->trans, dp);
1297 } else {
1299 * Addition succeeded, update Btree hashvals.
1301 xfs_da_fixhashpath(state, &state->path);
1305 * Kill the state structure, we're done with it and need to
1306 * allow the buffers to come back later.
1308 xfs_da_state_free(state);
1309 state = NULL;
1312 * Commit the leaf addition or btree split and start the next
1313 * trans in the chain.
1315 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1316 goto out;
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.
1324 if (args->rmtblkno > 0) {
1325 error = xfs_attr_rmtval_set(args);
1326 if (error)
1327 return(error);
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.
1336 if (args->rename) {
1338 * In a separate transaction, set the incomplete flag on the
1339 * "old" attr and clear the incomplete flag on the "new" attr.
1341 error = xfs_attr_leaf_flipflags(args);
1342 if (error)
1343 goto out;
1346 * Dismantle the "old" attribute/value pair by removing
1347 * a "remote" value (if it exists).
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);
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.
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;
1376 * Remove the name and update the hashvals in the tree.
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);
1384 * Check to see if the tree needs to be collapsed.
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);
1395 if (error) {
1396 ASSERT(committed);
1397 args->trans = NULL;
1398 xfs_bmap_cancel(args->flist);
1399 goto out;
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.
1407 if (committed) {
1408 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1409 xfs_trans_ihold(args->trans, dp);
1414 * Commit and start the next trans in the chain.
1416 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1417 goto out;
1419 } else if (args->rmtblkno > 0) {
1421 * Added a "remote" value, just clear the incomplete flag.
1423 error = xfs_attr_leaf_clearflag(args);
1424 if (error)
1425 goto out;
1427 retval = error = 0;
1429 out:
1430 if (state)
1431 xfs_da_state_free(state);
1432 if (error)
1433 return(error);
1434 return(retval);
1438 * Remove a name from a B-tree attribute list.
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).
1444 STATIC int
1445 xfs_attr_node_removename(xfs_da_args_t *args)
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;
1454 * Tie a string around our finger to remind us where we are.
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;
1464 * Search to see if name exists, and get back a pointer to it.
1466 error = xfs_da_node_lookup_int(state, &retval);
1467 if (error || (retval != EEXIST)) {
1468 if (error == 0)
1469 error = retval;
1470 goto out;
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.
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) {
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.
1487 error = xfs_attr_fillstate(state);
1488 if (error)
1489 goto out;
1492 * Mark the attribute as INCOMPLETE, then bunmapi() the
1493 * remote value.
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;
1503 * Refill the state structure with buffers, the prior calls
1504 * released our buffers.
1506 error = xfs_attr_refillstate(state);
1507 if (error)
1508 goto out;
1512 * Remove the name and update the hashvals in the tree.
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);
1520 * Check to see if the tree needs to be collapsed.
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);
1529 if (error) {
1530 ASSERT(committed);
1531 args->trans = NULL;
1532 xfs_bmap_cancel(args->flist);
1533 goto out;
1537 * bmap_finish() may have committed the last trans and started
1538 * a new one. We need the inode to be in all transactions.
1540 if (committed) {
1541 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1542 xfs_trans_ihold(args->trans, dp);
1546 * Commit the Btree join operation and start a new trans.
1548 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1549 goto out;
1553 * If the result is small enough, push it all into the inode.
1555 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1557 * Have to get rid of the copy of this dabuf in the state.
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;
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);
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);
1582 if (error) {
1583 ASSERT(committed);
1584 args->trans = NULL;
1585 xfs_bmap_cancel(args->flist);
1586 goto out;
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.
1594 if (committed) {
1595 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1596 xfs_trans_ihold(args->trans, dp);
1598 } else
1599 xfs_da_brelse(args->trans, bp);
1601 error = 0;
1603 out:
1604 xfs_da_state_free(state);
1605 return(error);
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.
1614 STATIC int
1615 xfs_attr_fillstate(xfs_da_state_t *state)
1617 xfs_da_state_path_t *path;
1618 xfs_da_state_blk_t *blk;
1619 int level;
1622 * Roll down the "path" in the state structure, storing the on-disk
1623 * block number for those buffers in the "path".
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;
1638 * Roll down the "altpath" in the state structure, storing the on-disk
1639 * block number for those buffers in the "altpath".
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;
1653 return(0);
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.
1662 STATIC int
1663 xfs_attr_refillstate(xfs_da_state_t *state)
1665 xfs_da_state_path_t *path;
1666 xfs_da_state_blk_t *blk;
1667 int level, error;
1670 * Roll down the "path" in the state structure, storing the on-disk
1671 * block number for those buffers in the "path".
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;
1689 * Roll down the "altpath" in the state structure, storing the on-disk
1690 * block number for those buffers in the "altpath".
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;
1707 return(0);
1711 * Look up a filename in a node attribute list.
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.
1717 STATIC int
1718 xfs_attr_node_get(xfs_da_args_t *args)
1720 xfs_da_state_t *state;
1721 xfs_da_state_blk_t *blk;
1722 int error, retval;
1723 int i;
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;
1732 * Search to see if name exists, and get back a pointer to it.
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);
1743 * Get the value, local or "remote"
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);
1753 * If not in a transaction, we have to release all the buffers.
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;
1760 xfs_da_state_free(state);
1761 return(retval);
1764 STATIC int /* error */
1765 xfs_attr_node_list(xfs_attr_list_context_t *context)
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;
1774 cursor = context->cursor;
1775 cursor->initted = 1;
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.
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;
1815 break;
1816 default:
1817 xfs_attr_trace_l_c("wrong blk - ??", context);
1818 xfs_da_brelse(NULL, bp);
1819 bp = NULL;
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.
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));
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));
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;
1869 if (i == INT_GET(node->hdr.count, ARCH_CONVERT)) {
1870 xfs_da_brelse(NULL, bp);
1871 return(0);
1873 xfs_da_brelse(NULL, bp);
1876 ASSERT(bp != NULL);
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.
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));
1893 error = xfs_attr_leaf_list_int(bp, context);
1894 if (error || !leaf->hdr.info.forw)
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));
1909 xfs_da_brelse(NULL, bp);
1910 return(0);
1914 /*========================================================================
1915 * External routines for manipulating out-of-line attribute values.
1916 *========================================================================*/
1919 * Read the value associated with an attribute from the out-of-line buffer
1920 * that we stored it in.
1922 STATIC int
1923 xfs_attr_rmtval_get(xfs_da_args_t *args)
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;
1933 ASSERT(!(args->flags & ATTR_KERNOVAL));
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);
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);
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;
1966 lblkno += map[i].br_blockcount;
1969 ASSERT(valuelen == 0);
1970 return(0);
1974 * Write the value associated with an attribute into the out-of-line buffer
1975 * that we have defined for it.
1977 STATIC int
1978 xfs_attr_rmtval_set(xfs_da_args_t *args)
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;
1990 dp = args->dp;
1991 mp = dp->i_mount;
1992 src = args->value;
1995 * Find a "hole" in the attribute address space large enough for
1996 * us to drop the new attribute's value into.
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);
2005 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2006 args->rmtblkcnt = blkcnt;
2009 * Roll through the "value", allocating blocks on disk as required.
2011 while (blkcnt > 0) {
2013 * Allocate a single extent, up to the size of the value.
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);
2027 if (error) {
2028 ASSERT(committed);
2029 args->trans = NULL;
2030 xfs_bmap_cancel(args->flist);
2031 return(error);
2035 * bmap_finish() may have committed the last trans and started
2036 * a new one. We need the inode to be in all transactions.
2038 if (committed) {
2039 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2040 xfs_trans_ihold(args->trans, dp);
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;
2050 * Start the next trans in the chain.
2052 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2053 return (error);
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.
2062 lblkno = args->rmtblkno;
2063 valuelen = args->valuelen;
2064 while (valuelen > 0) {
2066 * Try to remember where we decided to put the value.
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);
2077 ASSERT(nmap == 1);
2078 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2079 (map.br_startblock != HOLESTARTBLOCK));
2081 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2082 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
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));
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);
2097 src += tmp;
2098 valuelen -= tmp;
2100 lblkno += map.br_blockcount;
2102 ASSERT(valuelen == 0);
2103 return(0);
2107 * Remove the value associated with an attribute by deleting the
2108 * out-of-line buffer that it is stored on.
2110 STATIC int
2111 xfs_attr_rmtval_remove(xfs_da_args_t *args)
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;
2120 mp = args->dp->i_mount;
2123 * Roll through the "value", invalidating the attribute value's
2124 * blocks.
2126 lblkno = args->rmtblkno;
2127 valuelen = args->rmtblkcnt;
2128 while (valuelen > 0) {
2130 * Try to remember where we decided to put the value.
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);
2142 ASSERT(nmap == 1);
2143 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2144 (map.br_startblock != HOLESTARTBLOCK));
2146 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2147 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2150 * If the "remote" value is in the cache, remove it.
2152 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2153 XFS_INCORE_TRYLOCK);
2154 if (bp) {
2155 XFS_BUF_STALE(bp);
2156 XFS_BUF_UNDELAYWRITE(bp);
2157 xfs_buf_relse(bp);
2158 bp = NULL;
2161 valuelen -= map.br_blockcount;
2163 lblkno += map.br_blockcount;
2167 * Keep de-allocating extents until the remote-value region is gone.
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);
2181 if (error) {
2182 ASSERT(committed);
2183 args->trans = NULL;
2184 xfs_bmap_cancel(args->flist);
2185 return(error);
2189 * bmap_finish() may have committed the last trans and started
2190 * a new one. We need the inode to be in all transactions.
2192 if (committed) {
2193 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2194 xfs_trans_ihold(args->trans, args->dp);
2198 * Close out trans and start the next one in the chain.
2200 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2201 return (error);
2203 return(0);
2206 #if defined(XFS_ATTR_TRACE)
2208 * Add a trace buffer entry for an attr_list context structure.
2210 void
2211 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
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);
2236 * Add a trace buffer entry for a context structure and a Btree node.
2238 void
2239 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2240 struct xfs_da_intnode *node)
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));
2265 * Add a trace buffer entry for a context structure and a Btree element.
2267 void
2268 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2269 struct xfs_da_node_entry *btree)
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);
2294 * Add a trace buffer entry for a context structure and a leaf block.
2296 void
2297 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2298 struct xfs_attr_leafblock *leaf)
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));
2323 * Add a trace buffer entry for the arguments given to the routine,
2324 * generic form.
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)
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);
2345 #endif /* XFS_ATTR_TRACE */
2348 /*========================================================================
2349 * System (pseudo) namespace attribute interface routines.
2350 *========================================================================*/
2352 STATIC int
2353 posix_acl_access_set(
2354 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2356 return xfs_acl_vset(vp, data, size, _ACL_TYPE_ACCESS);
2359 STATIC int
2360 posix_acl_access_remove(
2361 struct vnode *vp, char *name, int xflags)
2363 return xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
2366 STATIC int
2367 posix_acl_access_get(
2368 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2370 return xfs_acl_vget(vp, data, size, _ACL_TYPE_ACCESS);
2373 STATIC int
2374 posix_acl_access_exists(
2375 vnode_t *vp)
2377 return xfs_acl_vhasacl_access(vp);
2380 STATIC int
2381 posix_acl_default_set(
2382 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2384 return xfs_acl_vset(vp, data, size, _ACL_TYPE_DEFAULT);
2387 STATIC int
2388 posix_acl_default_get(
2389 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2391 return xfs_acl_vget(vp, data, size, _ACL_TYPE_DEFAULT);
2394 STATIC int
2395 posix_acl_default_remove(
2396 struct vnode *vp, char *name, int xflags)
2398 return xfs_acl_vremove(vp, _ACL_TYPE_DEFAULT);
2401 STATIC int
2402 posix_acl_default_exists(
2403 vnode_t *vp)
2405 return xfs_acl_vhasacl_default(vp);
2408 STATIC 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,
2417 STATIC 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,
2426 STATIC struct attrnames *attr_system_names[] =
2427 { &posix_acl_access, &posix_acl_default };
2430 /*========================================================================
2431 * Namespace-prefix-style attribute name interface routines.
2432 *========================================================================*/
2434 STATIC int
2435 attr_generic_set(
2436 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2438 int error;
2440 VOP_ATTR_SET(vp, name, data, size, xflags, NULL, error);
2441 return -error;
2444 STATIC int
2445 attr_generic_get(
2446 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2448 int error, asize = size;
2450 VOP_ATTR_GET(vp, name, data, &asize, xflags, NULL, error);
2451 if (!error)
2452 return asize;
2453 return -error;
2456 STATIC int
2457 attr_generic_remove(
2458 struct vnode *vp, char *name, int xflags)
2460 int error;
2462 VOP_ATTR_REMOVE(vp, name, xflags, NULL, error);
2463 return -error;
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)
2474 char *p = data + *result;
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;
2489 STATIC int
2490 attr_system_list(
2491 struct vnode *vp,
2492 void *data,
2493 size_t size,
2494 ssize_t *result)
2496 attrnames_t *namesp;
2497 int i, error = 0;
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;
2508 return error;
2512 attr_generic_list(
2513 struct vnode *vp, void *data, size_t size, int xflags, ssize_t *result)
2515 attrlist_cursor_kern_t cursor = { 0 };
2516 int error;
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);
2525 attrnames_t *
2526 attr_lookup_namespace(
2527 char *name,
2528 struct attrnames **names,
2529 int nnames)
2531 int i;
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;
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;
2544 STATIC int
2545 attr_user_capable(
2546 struct vnode *vp,
2547 cred_t *cred)
2549 struct inode *inode = LINVFS_GET_IP(vp);
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;
2562 STATIC int
2563 attr_trusted_capable(
2564 struct vnode *vp,
2565 cred_t *cred)
2567 struct inode *inode = LINVFS_GET_IP(vp);
2569 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2570 return -EPERM;
2571 if (!capable(CAP_SYS_ADMIN))
2572 return -EPERM;
2573 return 0;
2576 STATIC int
2577 attr_secure_capable(
2578 struct vnode *vp,
2579 cred_t *cred)
2581 return -ENOSECURITY;
2584 STATIC int
2585 attr_system_set(
2586 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2588 attrnames_t *namesp;
2589 int error;
2591 if (xflags & ATTR_CREATE)
2592 return -EINVAL;
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;
2603 STATIC int
2604 attr_system_get(
2605 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2607 attrnames_t *namesp;
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);
2615 STATIC int
2616 attr_system_remove(
2617 struct vnode *vp, char *name, int xflags)
2619 attrnames_t *namesp;
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);
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,
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,
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,
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,
2666 struct attrnames *attr_namespaces[] =
2667 { &attr_system, &attr_trusted, &attr_secure, &attr_user };