2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/capability.h>
23 #include "xfs_types.h"
27 #include "xfs_trans.h"
31 #include "xfs_dmapi.h"
32 #include "xfs_mount.h"
33 #include "xfs_da_btree.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_alloc.h"
42 #include "xfs_btree.h"
43 #include "xfs_inode_item.h"
46 #include "xfs_attr_leaf.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_trans_space.h"
56 * Provide the external interfaces to manage attribute lists.
59 #define ATTR_SYSCOUNT 2
60 STATIC struct attrnames posix_acl_access;
61 STATIC struct attrnames posix_acl_default;
62 STATIC struct attrnames *attr_system_names[ATTR_SYSCOUNT];
64 /*========================================================================
65 * Function prototypes for the kernel.
66 *========================================================================*/
69 * Internal routines when attribute list fits inside the inode.
71 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
74 * Internal routines when attribute list is one block.
76 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
77 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
78 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
79 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
82 * Internal routines when attribute list is more than one block.
84 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
85 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
86 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
87 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
88 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
89 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
92 * Routines to manipulate out-of-line attribute values.
94 STATIC int xfs_attr_rmtval_get(xfs_da_args_t *args);
95 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
96 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
98 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
100 #if defined(XFS_ATTR_TRACE)
101 ktrace_t *xfs_attr_trace_buf;
105 /*========================================================================
106 * Overall external interface routines.
107 *========================================================================*/
110 xfs_attr_fetch(xfs_inode_t *ip, const char *name, int namelen,
111 char *value, int *valuelenp, int flags, struct cred *cred)
116 if ((XFS_IFORK_Q(ip) == 0) ||
117 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
118 ip->i_d.di_anextents == 0))
122 * Fill in the arg structure for this request.
124 memset((char *)&args, 0, sizeof(args));
126 args.namelen = namelen;
128 args.valuelen = *valuelenp;
130 args.hashval = xfs_da_hashname(args.name, args.namelen);
132 args.whichfork = XFS_ATTR_FORK;
135 * Decide on what work routines to call based on the inode size.
137 if (XFS_IFORK_Q(ip) == 0 ||
138 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
139 ip->i_d.di_anextents == 0)) {
140 error = XFS_ERROR(ENOATTR);
141 } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
142 error = xfs_attr_shortform_getvalue(&args);
143 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
144 error = xfs_attr_leaf_get(&args);
146 error = xfs_attr_node_get(&args);
150 * Return the number of bytes in the value to the caller.
152 *valuelenp = args.valuelen;
160 xfs_attr_get(bhv_desc_t *bdp, const char *name, char *value, int *valuelenp,
161 int flags, struct cred *cred)
163 xfs_inode_t *ip = XFS_BHVTOI(bdp);
166 XFS_STATS_INC(xs_attr_get);
170 namelen = strlen(name);
171 if (namelen >= MAXNAMELEN)
172 return(EFAULT); /* match IRIX behaviour */
174 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
177 xfs_ilock(ip, XFS_ILOCK_SHARED);
178 error = xfs_attr_fetch(ip, name, namelen, value, valuelenp, flags, cred);
179 xfs_iunlock(ip, XFS_ILOCK_SHARED);
184 xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
185 char *value, int valuelen, int flags)
188 xfs_fsblock_t firstblock;
189 xfs_bmap_free_t flist;
190 int error, err2, committed;
193 xfs_mount_t *mp = dp->i_mount;
194 int rsvd = (flags & ATTR_ROOT) != 0;
197 * Attach the dquots to the inode.
199 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
203 * Determine space new attribute will use, and if it would be
204 * "local" or "remote" (note: local != inline).
206 size = xfs_attr_leaf_newentsize(namelen, valuelen,
207 mp->m_sb.sb_blocksize, &local);
210 * If the inode doesn't have an attribute fork, add one.
211 * (inode must not be locked when we call this routine)
213 if (XFS_IFORK_Q(dp) == 0) {
214 if ((error = xfs_bmap_add_attrfork(dp, size, rsvd)))
219 * Fill in the arg structure for this request.
221 memset((char *)&args, 0, sizeof(args));
223 args.namelen = namelen;
225 args.valuelen = valuelen;
227 args.hashval = xfs_da_hashname(args.name, args.namelen);
229 args.firstblock = &firstblock;
231 args.whichfork = XFS_ATTR_FORK;
235 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
237 if (size > (mp->m_sb.sb_blocksize >> 1)) {
238 /* Double split possible */
242 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
243 /* Out of line attribute, cannot double split, but make
244 * room for the attribute value itself.
247 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
250 /* Size is now blocks for attribute data */
254 * Start our first transaction of the day.
256 * All future transactions during this code must be "chained" off
257 * this one via the trans_dup() call. All transactions will contain
258 * the inode, and the inode will always be marked with trans_ihold().
259 * Since the inode will be locked in all transactions, we must log
260 * the inode in every transaction to let it float upward through
263 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
266 * Root fork attributes can use reserved data blocks for this
267 * operation if necessary
271 args.trans->t_flags |= XFS_TRANS_RESERVE;
273 if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
274 XFS_ATTRSET_LOG_RES(mp, nblks),
275 0, XFS_TRANS_PERM_LOG_RES,
276 XFS_ATTRSET_LOG_COUNT))) {
277 xfs_trans_cancel(args.trans, 0);
280 xfs_ilock(dp, XFS_ILOCK_EXCL);
282 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
283 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
284 XFS_QMOPT_RES_REGBLKS);
286 xfs_iunlock(dp, XFS_ILOCK_EXCL);
287 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
291 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
292 xfs_trans_ihold(args.trans, dp);
295 * If the attribute list is non-existent or a shortform list,
296 * upgrade it to a single-leaf-block attribute list.
298 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
299 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
300 (dp->i_d.di_anextents == 0))) {
303 * Build initial attribute list (if required).
305 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
306 xfs_attr_shortform_create(&args);
309 * Try to add the attr to the attribute list in
312 error = xfs_attr_shortform_addname(&args);
313 if (error != ENOSPC) {
315 * Commit the shortform mods, and we're done.
316 * NOTE: this is also the error path (EEXIST, etc).
318 ASSERT(args.trans != NULL);
321 * If this is a synchronous mount, make sure that
322 * the transaction goes to disk before returning
325 if (mp->m_flags & XFS_MOUNT_WSYNC) {
326 xfs_trans_set_sync(args.trans);
328 err2 = xfs_trans_commit(args.trans,
329 XFS_TRANS_RELEASE_LOG_RES,
331 xfs_iunlock(dp, XFS_ILOCK_EXCL);
334 * Hit the inode change time.
336 if (!error && (flags & ATTR_KERNOTIME) == 0) {
337 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
339 return(error == 0 ? err2 : error);
343 * It won't fit in the shortform, transform to a leaf block.
344 * GROT: another possible req'mt for a double-split btree op.
346 XFS_BMAP_INIT(args.flist, args.firstblock);
347 error = xfs_attr_shortform_to_leaf(&args);
349 error = xfs_bmap_finish(&args.trans, args.flist,
350 *args.firstblock, &committed);
355 xfs_bmap_cancel(&flist);
360 * bmap_finish() may have committed the last trans and started
361 * a new one. We need the inode to be in all transactions.
364 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
365 xfs_trans_ihold(args.trans, dp);
369 * Commit the leaf transformation. We'll need another (linked)
370 * transaction to add the new attribute to the leaf.
372 if ((error = xfs_attr_rolltrans(&args.trans, dp)))
377 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
378 error = xfs_attr_leaf_addname(&args);
380 error = xfs_attr_node_addname(&args);
387 * If this is a synchronous mount, make sure that the
388 * transaction goes to disk before returning to the user.
390 if (mp->m_flags & XFS_MOUNT_WSYNC) {
391 xfs_trans_set_sync(args.trans);
395 * Commit the last in the sequence of transactions.
397 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
398 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
400 xfs_iunlock(dp, XFS_ILOCK_EXCL);
403 * Hit the inode change time.
405 if (!error && (flags & ATTR_KERNOTIME) == 0) {
406 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
413 xfs_trans_cancel(args.trans,
414 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
415 xfs_iunlock(dp, XFS_ILOCK_EXCL);
420 xfs_attr_set(bhv_desc_t *bdp, const char *name, char *value, int valuelen, int flags,
426 namelen = strlen(name);
427 if (namelen >= MAXNAMELEN)
428 return EFAULT; /* match IRIX behaviour */
430 XFS_STATS_INC(xs_attr_set);
432 dp = XFS_BHVTOI(bdp);
433 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
436 return xfs_attr_set_int(dp, name, namelen, value, valuelen, flags);
440 * Generic handler routine to remove a name from an attribute list.
441 * Transitions attribute list from Btree to shortform as necessary.
444 xfs_attr_remove_int(xfs_inode_t *dp, const char *name, int namelen, int flags)
447 xfs_fsblock_t firstblock;
448 xfs_bmap_free_t flist;
450 xfs_mount_t *mp = dp->i_mount;
453 * Fill in the arg structure for this request.
455 memset((char *)&args, 0, sizeof(args));
457 args.namelen = namelen;
459 args.hashval = xfs_da_hashname(args.name, args.namelen);
461 args.firstblock = &firstblock;
464 args.whichfork = XFS_ATTR_FORK;
467 * Attach the dquots to the inode.
469 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
473 * Start our first transaction of the day.
475 * All future transactions during this code must be "chained" off
476 * this one via the trans_dup() call. All transactions will contain
477 * the inode, and the inode will always be marked with trans_ihold().
478 * Since the inode will be locked in all transactions, we must log
479 * the inode in every transaction to let it float upward through
482 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
485 * Root fork attributes can use reserved data blocks for this
486 * operation if necessary
489 if (flags & ATTR_ROOT)
490 args.trans->t_flags |= XFS_TRANS_RESERVE;
492 if ((error = xfs_trans_reserve(args.trans,
493 XFS_ATTRRM_SPACE_RES(mp),
494 XFS_ATTRRM_LOG_RES(mp),
495 0, XFS_TRANS_PERM_LOG_RES,
496 XFS_ATTRRM_LOG_COUNT))) {
497 xfs_trans_cancel(args.trans, 0);
501 xfs_ilock(dp, XFS_ILOCK_EXCL);
503 * No need to make quota reservations here. We expect to release some
504 * blocks not allocate in the common case.
506 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
507 xfs_trans_ihold(args.trans, dp);
510 * Decide on what work routines to call based on the inode size.
512 if (XFS_IFORK_Q(dp) == 0 ||
513 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
514 dp->i_d.di_anextents == 0)) {
515 error = XFS_ERROR(ENOATTR);
518 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
519 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
520 error = xfs_attr_shortform_remove(&args);
524 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
525 error = xfs_attr_leaf_removename(&args);
527 error = xfs_attr_node_removename(&args);
534 * If this is a synchronous mount, make sure that the
535 * transaction goes to disk before returning to the user.
537 if (mp->m_flags & XFS_MOUNT_WSYNC) {
538 xfs_trans_set_sync(args.trans);
542 * Commit the last in the sequence of transactions.
544 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
545 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
547 xfs_iunlock(dp, XFS_ILOCK_EXCL);
550 * Hit the inode change time.
552 if (!error && (flags & ATTR_KERNOTIME) == 0) {
553 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
560 xfs_trans_cancel(args.trans,
561 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
562 xfs_iunlock(dp, XFS_ILOCK_EXCL);
567 xfs_attr_remove(bhv_desc_t *bdp, const char *name, int flags, struct cred *cred)
572 namelen = strlen(name);
573 if (namelen >= MAXNAMELEN)
574 return EFAULT; /* match IRIX behaviour */
576 XFS_STATS_INC(xs_attr_remove);
578 dp = XFS_BHVTOI(bdp);
579 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
582 xfs_ilock(dp, XFS_ILOCK_SHARED);
583 if (XFS_IFORK_Q(dp) == 0 ||
584 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
585 dp->i_d.di_anextents == 0)) {
586 xfs_iunlock(dp, XFS_ILOCK_SHARED);
587 return(XFS_ERROR(ENOATTR));
589 xfs_iunlock(dp, XFS_ILOCK_SHARED);
591 return xfs_attr_remove_int(dp, name, namelen, flags);
595 * Generate a list of extended attribute names and optionally
596 * also value lengths. Positive return value follows the XFS
597 * convention of being an error, zero or negative return code
598 * is the length of the buffer returned (negated), indicating
602 xfs_attr_list(bhv_desc_t *bdp, char *buffer, int bufsize, int flags,
603 attrlist_cursor_kern_t *cursor, struct cred *cred)
605 xfs_attr_list_context_t context;
609 XFS_STATS_INC(xs_attr_list);
612 * Validate the cursor.
614 if (cursor->pad1 || cursor->pad2)
615 return(XFS_ERROR(EINVAL));
616 if ((cursor->initted == 0) &&
617 (cursor->hashval || cursor->blkno || cursor->offset))
618 return(XFS_ERROR(EINVAL));
621 * Check for a properly aligned buffer.
623 if (((long)buffer) & (sizeof(int)-1))
624 return(XFS_ERROR(EFAULT));
625 if (flags & ATTR_KERNOVAL)
629 * Initialize the output buffer.
631 context.dp = dp = XFS_BHVTOI(bdp);
632 context.cursor = cursor;
636 context.flags = flags;
637 if (!(flags & ATTR_KERNAMELS)) {
638 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
639 context.firstu = context.bufsize;
640 context.alist = (attrlist_t *)buffer;
641 context.alist->al_count = 0;
642 context.alist->al_more = 0;
643 context.alist->al_offset[0] = context.bufsize;
646 context.bufsize = bufsize;
647 context.firstu = context.bufsize;
648 context.alist = (attrlist_t *)buffer;
651 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
654 xfs_ilock(dp, XFS_ILOCK_SHARED);
656 * Decide on what work routines to call based on the inode size.
658 xfs_attr_trace_l_c("syscall start", &context);
659 if (XFS_IFORK_Q(dp) == 0 ||
660 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
661 dp->i_d.di_anextents == 0)) {
663 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
664 error = xfs_attr_shortform_list(&context);
665 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
666 error = xfs_attr_leaf_list(&context);
668 error = xfs_attr_node_list(&context);
670 xfs_iunlock(dp, XFS_ILOCK_SHARED);
671 xfs_attr_trace_l_c("syscall end", &context);
673 if (!(context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS))) {
676 else { /* must return negated buffer size or the error */
677 if (context.count < 0)
678 error = XFS_ERROR(ERANGE);
680 error = -context.count;
687 xfs_attr_inactive(xfs_inode_t *dp)
694 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
696 xfs_ilock(dp, XFS_ILOCK_SHARED);
697 if ((XFS_IFORK_Q(dp) == 0) ||
698 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
699 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
700 dp->i_d.di_anextents == 0)) {
701 xfs_iunlock(dp, XFS_ILOCK_SHARED);
704 xfs_iunlock(dp, XFS_ILOCK_SHARED);
707 * Start our first transaction of the day.
709 * All future transactions during this code must be "chained" off
710 * this one via the trans_dup() call. All transactions will contain
711 * the inode, and the inode will always be marked with trans_ihold().
712 * Since the inode will be locked in all transactions, we must log
713 * the inode in every transaction to let it float upward through
716 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
717 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
718 XFS_TRANS_PERM_LOG_RES,
719 XFS_ATTRINVAL_LOG_COUNT))) {
720 xfs_trans_cancel(trans, 0);
723 xfs_ilock(dp, XFS_ILOCK_EXCL);
726 * No need to make quota reservations here. We expect to release some
727 * blocks, not allocate, in the common case.
729 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
730 xfs_trans_ihold(trans, dp);
733 * Decide on what work routines to call based on the inode size.
735 if ((XFS_IFORK_Q(dp) == 0) ||
736 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
737 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
738 dp->i_d.di_anextents == 0)) {
742 error = xfs_attr_root_inactive(&trans, dp);
746 * signal synchronous inactive transactions unless this
747 * is a synchronous mount filesystem in which case we
748 * know that we're here because we've been called out of
749 * xfs_inactive which means that the last reference is gone
750 * and the unlink transaction has already hit the disk so
751 * async inactive transactions are safe.
753 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
754 (!(mp->m_flags & XFS_MOUNT_WSYNC)
759 * Commit the last in the sequence of transactions.
761 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
762 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES,
764 xfs_iunlock(dp, XFS_ILOCK_EXCL);
769 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
770 xfs_iunlock(dp, XFS_ILOCK_EXCL);
776 /*========================================================================
777 * External routines when attribute list is inside the inode
778 *========================================================================*/
781 * Add a name to the shortform attribute list structure
782 * This is the external routine.
785 xfs_attr_shortform_addname(xfs_da_args_t *args)
787 int newsize, forkoff, retval;
789 retval = xfs_attr_shortform_lookup(args);
790 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
792 } else if (retval == EEXIST) {
793 if (args->flags & ATTR_CREATE)
795 retval = xfs_attr_shortform_remove(args);
799 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
800 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
801 return(XFS_ERROR(ENOSPC));
803 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
804 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
806 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
808 return(XFS_ERROR(ENOSPC));
810 xfs_attr_shortform_add(args, forkoff);
815 /*========================================================================
816 * External routines when attribute list is one block
817 *========================================================================*/
820 * Add a name to the leaf attribute list structure
822 * This leaf block cannot have a "remote" value, we only call this routine
823 * if bmap_one_block() says there is only one block (ie: no remote blks).
826 xfs_attr_leaf_addname(xfs_da_args_t *args)
830 int retval, error, committed, forkoff;
833 * Read the (only) block in the attribute list in.
837 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
844 * Look up the given attribute in the leaf block. Figure out if
845 * the given flags produce an error or call for an atomic rename.
847 retval = xfs_attr_leaf_lookup_int(bp, args);
848 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
849 xfs_da_brelse(args->trans, bp);
851 } else if (retval == EEXIST) {
852 if (args->flags & ATTR_CREATE) { /* pure create op */
853 xfs_da_brelse(args->trans, bp);
856 args->rename = 1; /* an atomic rename */
857 args->blkno2 = args->blkno; /* set 2nd entry info*/
858 args->index2 = args->index;
859 args->rmtblkno2 = args->rmtblkno;
860 args->rmtblkcnt2 = args->rmtblkcnt;
864 * Add the attribute to the leaf block, transitioning to a Btree
867 retval = xfs_attr_leaf_add(bp, args);
869 if (retval == ENOSPC) {
871 * Promote the attribute list to the Btree format, then
872 * Commit that transaction so that the node_addname() call
873 * can manage its own transactions.
875 XFS_BMAP_INIT(args->flist, args->firstblock);
876 error = xfs_attr_leaf_to_node(args);
878 error = xfs_bmap_finish(&args->trans, args->flist,
879 *args->firstblock, &committed);
884 xfs_bmap_cancel(args->flist);
889 * bmap_finish() may have committed the last trans and started
890 * a new one. We need the inode to be in all transactions.
893 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
894 xfs_trans_ihold(args->trans, dp);
898 * Commit the current trans (including the inode) and start
901 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
905 * Fob the whole rest of the problem off on the Btree code.
907 error = xfs_attr_node_addname(args);
912 * Commit the transaction that added the attr name so that
913 * later routines can manage their own transactions.
915 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
919 * If there was an out-of-line value, allocate the blocks we
920 * identified for its storage and copy the value. This is done
921 * after we create the attribute so that we don't overflow the
922 * maximum size of a transaction and/or hit a deadlock.
924 if (args->rmtblkno > 0) {
925 error = xfs_attr_rmtval_set(args);
931 * If this is an atomic rename operation, we must "flip" the
932 * incomplete flags on the "new" and "old" attribute/value pairs
933 * so that one disappears and one appears atomically. Then we
934 * must remove the "old" attribute/value pair.
938 * In a separate transaction, set the incomplete flag on the
939 * "old" attr and clear the incomplete flag on the "new" attr.
941 error = xfs_attr_leaf_flipflags(args);
946 * Dismantle the "old" attribute/value pair by removing
947 * a "remote" value (if it exists).
949 args->index = args->index2;
950 args->blkno = args->blkno2;
951 args->rmtblkno = args->rmtblkno2;
952 args->rmtblkcnt = args->rmtblkcnt2;
953 if (args->rmtblkno) {
954 error = xfs_attr_rmtval_remove(args);
960 * Read in the block containing the "old" attr, then
961 * remove the "old" attr from that block (neat, huh!)
963 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
968 (void)xfs_attr_leaf_remove(bp, args);
971 * If the result is small enough, shrink it all into the inode.
973 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
974 XFS_BMAP_INIT(args->flist, args->firstblock);
975 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
976 /* bp is gone due to xfs_da_shrink_inode */
978 error = xfs_bmap_finish(&args->trans,
986 xfs_bmap_cancel(args->flist);
991 * bmap_finish() may have committed the last trans
992 * and started a new one. We need the inode to be
993 * in all transactions.
996 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
997 xfs_trans_ihold(args->trans, dp);
1000 xfs_da_buf_done(bp);
1003 * Commit the remove and start the next trans in series.
1005 error = xfs_attr_rolltrans(&args->trans, dp);
1007 } else if (args->rmtblkno > 0) {
1009 * Added a "remote" value, just clear the incomplete flag.
1011 error = xfs_attr_leaf_clearflag(args);
1017 * Remove a name from the leaf attribute list structure
1019 * This leaf block cannot have a "remote" value, we only call this routine
1020 * if bmap_one_block() says there is only one block (ie: no remote blks).
1023 xfs_attr_leaf_removename(xfs_da_args_t *args)
1027 int error, committed, forkoff;
1030 * Remove the attribute.
1034 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1041 error = xfs_attr_leaf_lookup_int(bp, args);
1042 if (error == ENOATTR) {
1043 xfs_da_brelse(args->trans, bp);
1047 (void)xfs_attr_leaf_remove(bp, args);
1050 * If the result is small enough, shrink it all into the inode.
1052 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1053 XFS_BMAP_INIT(args->flist, args->firstblock);
1054 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1055 /* bp is gone due to xfs_da_shrink_inode */
1057 error = xfs_bmap_finish(&args->trans, args->flist,
1058 *args->firstblock, &committed);
1063 xfs_bmap_cancel(args->flist);
1068 * bmap_finish() may have committed the last trans and started
1069 * a new one. We need the inode to be in all transactions.
1072 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1073 xfs_trans_ihold(args->trans, dp);
1076 xfs_da_buf_done(bp);
1081 * Look up a name in a leaf attribute list structure.
1083 * This leaf block cannot have a "remote" value, we only call this routine
1084 * if bmap_one_block() says there is only one block (ie: no remote blks).
1087 xfs_attr_leaf_get(xfs_da_args_t *args)
1093 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1099 error = xfs_attr_leaf_lookup_int(bp, args);
1100 if (error != EEXIST) {
1101 xfs_da_brelse(args->trans, bp);
1104 error = xfs_attr_leaf_getvalue(bp, args);
1105 xfs_da_brelse(args->trans, bp);
1106 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1107 error = xfs_attr_rmtval_get(args);
1113 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1116 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1118 xfs_attr_leafblock_t *leaf;
1122 context->cursor->blkno = 0;
1123 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1128 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1129 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1130 context->dp->i_mount, leaf);
1131 xfs_da_brelse(NULL, bp);
1132 return(XFS_ERROR(EFSCORRUPTED));
1135 (void)xfs_attr_leaf_list_int(bp, context);
1136 xfs_da_brelse(NULL, bp);
1141 /*========================================================================
1142 * External routines when attribute list size > XFS_LBSIZE(mp).
1143 *========================================================================*/
1146 * Add a name to a Btree-format attribute list.
1148 * This will involve walking down the Btree, and may involve splitting
1149 * leaf nodes and even splitting intermediate nodes up to and including
1150 * the root node (a special case of an intermediate node).
1152 * "Remote" attribute values confuse the issue and atomic rename operations
1153 * add a whole extra layer of confusion on top of that.
1156 xfs_attr_node_addname(xfs_da_args_t *args)
1158 xfs_da_state_t *state;
1159 xfs_da_state_blk_t *blk;
1162 int committed, retval, error;
1165 * Fill in bucket of arguments/results/context to carry around.
1170 state = xfs_da_state_alloc();
1173 state->blocksize = state->mp->m_sb.sb_blocksize;
1174 state->node_ents = state->mp->m_attr_node_ents;
1177 * Search to see if name already exists, and get back a pointer
1178 * to where it should go.
1180 error = xfs_da_node_lookup_int(state, &retval);
1183 blk = &state->path.blk[ state->path.active-1 ];
1184 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1185 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1187 } else if (retval == EEXIST) {
1188 if (args->flags & ATTR_CREATE)
1190 args->rename = 1; /* atomic rename op */
1191 args->blkno2 = args->blkno; /* set 2nd entry info*/
1192 args->index2 = args->index;
1193 args->rmtblkno2 = args->rmtblkno;
1194 args->rmtblkcnt2 = args->rmtblkcnt;
1196 args->rmtblkcnt = 0;
1199 retval = xfs_attr_leaf_add(blk->bp, state->args);
1200 if (retval == ENOSPC) {
1201 if (state->path.active == 1) {
1203 * Its really a single leaf node, but it had
1204 * out-of-line values so it looked like it *might*
1205 * have been a b-tree.
1207 xfs_da_state_free(state);
1208 XFS_BMAP_INIT(args->flist, args->firstblock);
1209 error = xfs_attr_leaf_to_node(args);
1211 error = xfs_bmap_finish(&args->trans,
1219 xfs_bmap_cancel(args->flist);
1224 * bmap_finish() may have committed the last trans
1225 * and started a new one. We need the inode to be
1226 * in all transactions.
1229 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1230 xfs_trans_ihold(args->trans, dp);
1234 * Commit the node conversion and start the next
1235 * trans in the chain.
1237 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1244 * Split as many Btree elements as required.
1245 * This code tracks the new and old attr's location
1246 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1247 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1249 XFS_BMAP_INIT(args->flist, args->firstblock);
1250 error = xfs_da_split(state);
1252 error = xfs_bmap_finish(&args->trans, args->flist,
1253 *args->firstblock, &committed);
1258 xfs_bmap_cancel(args->flist);
1263 * bmap_finish() may have committed the last trans and started
1264 * a new one. We need the inode to be in all transactions.
1267 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1268 xfs_trans_ihold(args->trans, dp);
1272 * Addition succeeded, update Btree hashvals.
1274 xfs_da_fixhashpath(state, &state->path);
1278 * Kill the state structure, we're done with it and need to
1279 * allow the buffers to come back later.
1281 xfs_da_state_free(state);
1285 * Commit the leaf addition or btree split and start the next
1286 * trans in the chain.
1288 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1292 * If there was an out-of-line value, allocate the blocks we
1293 * identified for its storage and copy the value. This is done
1294 * after we create the attribute so that we don't overflow the
1295 * maximum size of a transaction and/or hit a deadlock.
1297 if (args->rmtblkno > 0) {
1298 error = xfs_attr_rmtval_set(args);
1304 * If this is an atomic rename operation, we must "flip" the
1305 * incomplete flags on the "new" and "old" attribute/value pairs
1306 * so that one disappears and one appears atomically. Then we
1307 * must remove the "old" attribute/value pair.
1311 * In a separate transaction, set the incomplete flag on the
1312 * "old" attr and clear the incomplete flag on the "new" attr.
1314 error = xfs_attr_leaf_flipflags(args);
1319 * Dismantle the "old" attribute/value pair by removing
1320 * a "remote" value (if it exists).
1322 args->index = args->index2;
1323 args->blkno = args->blkno2;
1324 args->rmtblkno = args->rmtblkno2;
1325 args->rmtblkcnt = args->rmtblkcnt2;
1326 if (args->rmtblkno) {
1327 error = xfs_attr_rmtval_remove(args);
1333 * Re-find the "old" attribute entry after any split ops.
1334 * The INCOMPLETE flag means that we will find the "old"
1335 * attr, not the "new" one.
1337 args->flags |= XFS_ATTR_INCOMPLETE;
1338 state = xfs_da_state_alloc();
1341 state->blocksize = state->mp->m_sb.sb_blocksize;
1342 state->node_ents = state->mp->m_attr_node_ents;
1344 error = xfs_da_node_lookup_int(state, &retval);
1349 * Remove the name and update the hashvals in the tree.
1351 blk = &state->path.blk[ state->path.active-1 ];
1352 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1353 error = xfs_attr_leaf_remove(blk->bp, args);
1354 xfs_da_fixhashpath(state, &state->path);
1357 * Check to see if the tree needs to be collapsed.
1359 if (retval && (state->path.active > 1)) {
1360 XFS_BMAP_INIT(args->flist, args->firstblock);
1361 error = xfs_da_join(state);
1363 error = xfs_bmap_finish(&args->trans,
1371 xfs_bmap_cancel(args->flist);
1376 * bmap_finish() may have committed the last trans
1377 * and started a new one. We need the inode to be
1378 * in all transactions.
1381 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1382 xfs_trans_ihold(args->trans, dp);
1387 * Commit and start the next trans in the chain.
1389 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1392 } else if (args->rmtblkno > 0) {
1394 * Added a "remote" value, just clear the incomplete flag.
1396 error = xfs_attr_leaf_clearflag(args);
1404 xfs_da_state_free(state);
1411 * Remove a name from a B-tree attribute list.
1413 * This will involve walking down the Btree, and may involve joining
1414 * leaf nodes and even joining intermediate nodes up to and including
1415 * the root node (a special case of an intermediate node).
1418 xfs_attr_node_removename(xfs_da_args_t *args)
1420 xfs_da_state_t *state;
1421 xfs_da_state_blk_t *blk;
1424 int retval, error, committed, forkoff;
1427 * Tie a string around our finger to remind us where we are.
1430 state = xfs_da_state_alloc();
1432 state->mp = dp->i_mount;
1433 state->blocksize = state->mp->m_sb.sb_blocksize;
1434 state->node_ents = state->mp->m_attr_node_ents;
1437 * Search to see if name exists, and get back a pointer to it.
1439 error = xfs_da_node_lookup_int(state, &retval);
1440 if (error || (retval != EEXIST)) {
1447 * If there is an out-of-line value, de-allocate the blocks.
1448 * This is done before we remove the attribute so that we don't
1449 * overflow the maximum size of a transaction and/or hit a deadlock.
1451 blk = &state->path.blk[ state->path.active-1 ];
1452 ASSERT(blk->bp != NULL);
1453 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1454 if (args->rmtblkno > 0) {
1456 * Fill in disk block numbers in the state structure
1457 * so that we can get the buffers back after we commit
1458 * several transactions in the following calls.
1460 error = xfs_attr_fillstate(state);
1465 * Mark the attribute as INCOMPLETE, then bunmapi() the
1468 error = xfs_attr_leaf_setflag(args);
1471 error = xfs_attr_rmtval_remove(args);
1476 * Refill the state structure with buffers, the prior calls
1477 * released our buffers.
1479 error = xfs_attr_refillstate(state);
1485 * Remove the name and update the hashvals in the tree.
1487 blk = &state->path.blk[ state->path.active-1 ];
1488 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1489 retval = xfs_attr_leaf_remove(blk->bp, args);
1490 xfs_da_fixhashpath(state, &state->path);
1493 * Check to see if the tree needs to be collapsed.
1495 if (retval && (state->path.active > 1)) {
1496 XFS_BMAP_INIT(args->flist, args->firstblock);
1497 error = xfs_da_join(state);
1499 error = xfs_bmap_finish(&args->trans, args->flist,
1500 *args->firstblock, &committed);
1505 xfs_bmap_cancel(args->flist);
1510 * bmap_finish() may have committed the last trans and started
1511 * a new one. We need the inode to be in all transactions.
1514 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1515 xfs_trans_ihold(args->trans, dp);
1519 * Commit the Btree join operation and start a new trans.
1521 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1526 * If the result is small enough, push it all into the inode.
1528 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1530 * Have to get rid of the copy of this dabuf in the state.
1532 ASSERT(state->path.active == 1);
1533 ASSERT(state->path.blk[0].bp);
1534 xfs_da_buf_done(state->path.blk[0].bp);
1535 state->path.blk[0].bp = NULL;
1537 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1541 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1542 bp->data)->hdr.info.magic)
1543 == XFS_ATTR_LEAF_MAGIC);
1545 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1546 XFS_BMAP_INIT(args->flist, args->firstblock);
1547 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1548 /* bp is gone due to xfs_da_shrink_inode */
1550 error = xfs_bmap_finish(&args->trans,
1558 xfs_bmap_cancel(args->flist);
1563 * bmap_finish() may have committed the last trans
1564 * and started a new one. We need the inode to be
1565 * in all transactions.
1568 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1569 xfs_trans_ihold(args->trans, dp);
1572 xfs_da_brelse(args->trans, bp);
1577 xfs_da_state_free(state);
1582 * Fill in the disk block numbers in the state structure for the buffers
1583 * that are attached to the state structure.
1584 * This is done so that we can quickly reattach ourselves to those buffers
1585 * after some set of transaction commits have released these buffers.
1588 xfs_attr_fillstate(xfs_da_state_t *state)
1590 xfs_da_state_path_t *path;
1591 xfs_da_state_blk_t *blk;
1595 * Roll down the "path" in the state structure, storing the on-disk
1596 * block number for those buffers in the "path".
1598 path = &state->path;
1599 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1600 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1602 blk->disk_blkno = xfs_da_blkno(blk->bp);
1603 xfs_da_buf_done(blk->bp);
1606 blk->disk_blkno = 0;
1611 * Roll down the "altpath" in the state structure, storing the on-disk
1612 * block number for those buffers in the "altpath".
1614 path = &state->altpath;
1615 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1616 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1618 blk->disk_blkno = xfs_da_blkno(blk->bp);
1619 xfs_da_buf_done(blk->bp);
1622 blk->disk_blkno = 0;
1630 * Reattach the buffers to the state structure based on the disk block
1631 * numbers stored in the state structure.
1632 * This is done after some set of transaction commits have released those
1633 * buffers from our grip.
1636 xfs_attr_refillstate(xfs_da_state_t *state)
1638 xfs_da_state_path_t *path;
1639 xfs_da_state_blk_t *blk;
1643 * Roll down the "path" in the state structure, storing the on-disk
1644 * block number for those buffers in the "path".
1646 path = &state->path;
1647 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1648 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1649 if (blk->disk_blkno) {
1650 error = xfs_da_read_buf(state->args->trans,
1652 blk->blkno, blk->disk_blkno,
1653 &blk->bp, XFS_ATTR_FORK);
1662 * Roll down the "altpath" in the state structure, storing the on-disk
1663 * block number for those buffers in the "altpath".
1665 path = &state->altpath;
1666 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1667 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1668 if (blk->disk_blkno) {
1669 error = xfs_da_read_buf(state->args->trans,
1671 blk->blkno, blk->disk_blkno,
1672 &blk->bp, XFS_ATTR_FORK);
1684 * Look up a filename in a node attribute list.
1686 * This routine gets called for any attribute fork that has more than one
1687 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1688 * "remote" values taking up more blocks.
1691 xfs_attr_node_get(xfs_da_args_t *args)
1693 xfs_da_state_t *state;
1694 xfs_da_state_blk_t *blk;
1698 state = xfs_da_state_alloc();
1700 state->mp = args->dp->i_mount;
1701 state->blocksize = state->mp->m_sb.sb_blocksize;
1702 state->node_ents = state->mp->m_attr_node_ents;
1705 * Search to see if name exists, and get back a pointer to it.
1707 error = xfs_da_node_lookup_int(state, &retval);
1710 } else if (retval == EEXIST) {
1711 blk = &state->path.blk[ state->path.active-1 ];
1712 ASSERT(blk->bp != NULL);
1713 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1716 * Get the value, local or "remote"
1718 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1719 if (!retval && (args->rmtblkno > 0)
1720 && !(args->flags & ATTR_KERNOVAL)) {
1721 retval = xfs_attr_rmtval_get(args);
1726 * If not in a transaction, we have to release all the buffers.
1728 for (i = 0; i < state->path.active; i++) {
1729 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1730 state->path.blk[i].bp = NULL;
1733 xfs_da_state_free(state);
1737 STATIC int /* error */
1738 xfs_attr_node_list(xfs_attr_list_context_t *context)
1740 attrlist_cursor_kern_t *cursor;
1741 xfs_attr_leafblock_t *leaf;
1742 xfs_da_intnode_t *node;
1743 xfs_da_node_entry_t *btree;
1747 cursor = context->cursor;
1748 cursor->initted = 1;
1751 * Do all sorts of validation on the passed-in cursor structure.
1752 * If anything is amiss, ignore the cursor and look up the hashval
1753 * starting from the btree root.
1756 if (cursor->blkno > 0) {
1757 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1758 &bp, XFS_ATTR_FORK);
1759 if ((error != 0) && (error != EFSCORRUPTED))
1763 switch (be16_to_cpu(node->hdr.info.magic)) {
1764 case XFS_DA_NODE_MAGIC:
1765 xfs_attr_trace_l_cn("wrong blk", context, node);
1766 xfs_da_brelse(NULL, bp);
1769 case XFS_ATTR_LEAF_MAGIC:
1771 if (cursor->hashval > be32_to_cpu(leaf->entries[
1772 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1773 xfs_attr_trace_l_cl("wrong blk",
1775 xfs_da_brelse(NULL, bp);
1777 } else if (cursor->hashval <=
1778 be32_to_cpu(leaf->entries[0].hashval)) {
1779 xfs_attr_trace_l_cl("maybe wrong blk",
1781 xfs_da_brelse(NULL, bp);
1786 xfs_attr_trace_l_c("wrong blk - ??", context);
1787 xfs_da_brelse(NULL, bp);
1794 * We did not find what we expected given the cursor's contents,
1795 * so we start from the top and work down based on the hash value.
1796 * Note that start of node block is same as start of leaf block.
1801 error = xfs_da_read_buf(NULL, context->dp,
1802 cursor->blkno, -1, &bp,
1806 if (unlikely(bp == NULL)) {
1807 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1809 context->dp->i_mount);
1810 return(XFS_ERROR(EFSCORRUPTED));
1813 if (be16_to_cpu(node->hdr.info.magic)
1814 == XFS_ATTR_LEAF_MAGIC)
1816 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1817 != XFS_DA_NODE_MAGIC)) {
1818 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1820 context->dp->i_mount,
1822 xfs_da_brelse(NULL, bp);
1823 return(XFS_ERROR(EFSCORRUPTED));
1825 btree = node->btree;
1826 for (i = 0; i < be16_to_cpu(node->hdr.count);
1829 <= be32_to_cpu(btree->hashval)) {
1830 cursor->blkno = be32_to_cpu(btree->before);
1831 xfs_attr_trace_l_cb("descending",
1836 if (i == be16_to_cpu(node->hdr.count)) {
1837 xfs_da_brelse(NULL, bp);
1840 xfs_da_brelse(NULL, bp);
1846 * Roll upward through the blocks, processing each leaf block in
1847 * order. As long as there is space in the result buffer, keep
1848 * adding the information.
1852 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1853 != XFS_ATTR_LEAF_MAGIC)) {
1854 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1856 context->dp->i_mount, leaf);
1857 xfs_da_brelse(NULL, bp);
1858 return(XFS_ERROR(EFSCORRUPTED));
1860 error = xfs_attr_leaf_list_int(bp, context);
1861 if (error || !leaf->hdr.info.forw)
1862 break; /* not really an error, buffer full or EOF */
1863 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1864 xfs_da_brelse(NULL, bp);
1865 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1866 &bp, XFS_ATTR_FORK);
1869 if (unlikely((bp == NULL))) {
1870 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1872 context->dp->i_mount);
1873 return(XFS_ERROR(EFSCORRUPTED));
1876 xfs_da_brelse(NULL, bp);
1881 /*========================================================================
1882 * External routines for manipulating out-of-line attribute values.
1883 *========================================================================*/
1886 * Read the value associated with an attribute from the out-of-line buffer
1887 * that we stored it in.
1890 xfs_attr_rmtval_get(xfs_da_args_t *args)
1892 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1897 int nmap, error, tmp, valuelen, blkcnt, i;
1900 ASSERT(!(args->flags & ATTR_KERNOVAL));
1902 mp = args->dp->i_mount;
1904 valuelen = args->valuelen;
1905 lblkno = args->rmtblkno;
1906 while (valuelen > 0) {
1907 nmap = ATTR_RMTVALUE_MAPSIZE;
1908 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
1910 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
1911 NULL, 0, map, &nmap, NULL, NULL);
1916 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
1917 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
1918 (map[i].br_startblock != HOLESTARTBLOCK));
1919 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
1920 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
1921 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
1922 blkcnt, XFS_BUF_LOCK, &bp);
1926 tmp = (valuelen < XFS_BUF_SIZE(bp))
1927 ? valuelen : XFS_BUF_SIZE(bp);
1928 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
1933 lblkno += map[i].br_blockcount;
1936 ASSERT(valuelen == 0);
1941 * Write the value associated with an attribute into the out-of-line buffer
1942 * that we have defined for it.
1945 xfs_attr_rmtval_set(xfs_da_args_t *args)
1948 xfs_fileoff_t lfileoff;
1950 xfs_bmbt_irec_t map;
1955 int blkcnt, valuelen, nmap, error, tmp, committed;
1962 * Find a "hole" in the attribute address space large enough for
1963 * us to drop the new attribute's value into.
1965 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1967 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
1972 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
1973 args->rmtblkcnt = blkcnt;
1976 * Roll through the "value", allocating blocks on disk as required.
1978 while (blkcnt > 0) {
1980 * Allocate a single extent, up to the size of the value.
1982 XFS_BMAP_INIT(args->flist, args->firstblock);
1984 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
1986 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
1988 args->firstblock, args->total, &map, &nmap,
1991 error = xfs_bmap_finish(&args->trans, args->flist,
1992 *args->firstblock, &committed);
1997 xfs_bmap_cancel(args->flist);
2002 * bmap_finish() may have committed the last trans and started
2003 * a new one. We need the inode to be in all transactions.
2006 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2007 xfs_trans_ihold(args->trans, dp);
2011 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2012 (map.br_startblock != HOLESTARTBLOCK));
2013 lblkno += map.br_blockcount;
2014 blkcnt -= map.br_blockcount;
2017 * Start the next trans in the chain.
2019 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2024 * Roll through the "value", copying the attribute value to the
2025 * already-allocated blocks. Blocks are written synchronously
2026 * so that we can know they are all on disk before we turn off
2027 * the INCOMPLETE flag.
2029 lblkno = args->rmtblkno;
2030 valuelen = args->valuelen;
2031 while (valuelen > 0) {
2033 * Try to remember where we decided to put the value.
2035 XFS_BMAP_INIT(args->flist, args->firstblock);
2037 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2039 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2040 args->firstblock, 0, &map, &nmap,
2046 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2047 (map.br_startblock != HOLESTARTBLOCK));
2049 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2050 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2052 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2053 blkcnt, XFS_BUF_LOCK);
2055 ASSERT(!XFS_BUF_GETERROR(bp));
2057 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2059 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2060 if (tmp < XFS_BUF_SIZE(bp))
2061 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2062 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2068 lblkno += map.br_blockcount;
2070 ASSERT(valuelen == 0);
2075 * Remove the value associated with an attribute by deleting the
2076 * out-of-line buffer that it is stored on.
2079 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2082 xfs_bmbt_irec_t map;
2086 int valuelen, blkcnt, nmap, error, done, committed;
2088 mp = args->dp->i_mount;
2091 * Roll through the "value", invalidating the attribute value's
2094 lblkno = args->rmtblkno;
2095 valuelen = args->rmtblkcnt;
2096 while (valuelen > 0) {
2098 * Try to remember where we decided to put the value.
2100 XFS_BMAP_INIT(args->flist, args->firstblock);
2102 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2104 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2105 args->firstblock, 0, &map, &nmap,
2111 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2112 (map.br_startblock != HOLESTARTBLOCK));
2114 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2115 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2118 * If the "remote" value is in the cache, remove it.
2120 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2121 XFS_INCORE_TRYLOCK);
2124 XFS_BUF_UNDELAYWRITE(bp);
2129 valuelen -= map.br_blockcount;
2131 lblkno += map.br_blockcount;
2135 * Keep de-allocating extents until the remote-value region is gone.
2137 lblkno = args->rmtblkno;
2138 blkcnt = args->rmtblkcnt;
2141 XFS_BMAP_INIT(args->flist, args->firstblock);
2142 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2143 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2144 1, args->firstblock, args->flist,
2147 error = xfs_bmap_finish(&args->trans, args->flist,
2148 *args->firstblock, &committed);
2153 xfs_bmap_cancel(args->flist);
2158 * bmap_finish() may have committed the last trans and started
2159 * a new one. We need the inode to be in all transactions.
2162 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2163 xfs_trans_ihold(args->trans, args->dp);
2167 * Close out trans and start the next one in the chain.
2169 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2175 #if defined(XFS_ATTR_TRACE)
2177 * Add a trace buffer entry for an attr_list context structure.
2180 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2182 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where,
2183 (__psunsigned_t)context->dp,
2184 (__psunsigned_t)context->cursor->hashval,
2185 (__psunsigned_t)context->cursor->blkno,
2186 (__psunsigned_t)context->cursor->offset,
2187 (__psunsigned_t)context->alist,
2188 (__psunsigned_t)context->bufsize,
2189 (__psunsigned_t)context->count,
2190 (__psunsigned_t)context->firstu,
2192 ((context->count > 0) &&
2193 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2194 ? (ATTR_ENTRY(context->alist,
2195 context->count-1)->a_valuelen)
2197 (__psunsigned_t)context->dupcnt,
2198 (__psunsigned_t)context->flags,
2199 (__psunsigned_t)NULL,
2200 (__psunsigned_t)NULL,
2201 (__psunsigned_t)NULL);
2205 * Add a trace buffer entry for a context structure and a Btree node.
2208 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2209 struct xfs_da_intnode *node)
2211 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where,
2212 (__psunsigned_t)context->dp,
2213 (__psunsigned_t)context->cursor->hashval,
2214 (__psunsigned_t)context->cursor->blkno,
2215 (__psunsigned_t)context->cursor->offset,
2216 (__psunsigned_t)context->alist,
2217 (__psunsigned_t)context->bufsize,
2218 (__psunsigned_t)context->count,
2219 (__psunsigned_t)context->firstu,
2221 ((context->count > 0) &&
2222 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2223 ? (ATTR_ENTRY(context->alist,
2224 context->count-1)->a_valuelen)
2226 (__psunsigned_t)context->dupcnt,
2227 (__psunsigned_t)context->flags,
2228 (__psunsigned_t)be16_to_cpu(node->hdr.count),
2229 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
2230 (__psunsigned_t)be32_to_cpu(node->btree[
2231 be16_to_cpu(node->hdr.count)-1].hashval));
2235 * Add a trace buffer entry for a context structure and a Btree element.
2238 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2239 struct xfs_da_node_entry *btree)
2241 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where,
2242 (__psunsigned_t)context->dp,
2243 (__psunsigned_t)context->cursor->hashval,
2244 (__psunsigned_t)context->cursor->blkno,
2245 (__psunsigned_t)context->cursor->offset,
2246 (__psunsigned_t)context->alist,
2247 (__psunsigned_t)context->bufsize,
2248 (__psunsigned_t)context->count,
2249 (__psunsigned_t)context->firstu,
2251 ((context->count > 0) &&
2252 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2253 ? (ATTR_ENTRY(context->alist,
2254 context->count-1)->a_valuelen)
2256 (__psunsigned_t)context->dupcnt,
2257 (__psunsigned_t)context->flags,
2258 (__psunsigned_t)be32_to_cpu(btree->hashval),
2259 (__psunsigned_t)be32_to_cpu(btree->before),
2260 (__psunsigned_t)NULL);
2264 * Add a trace buffer entry for a context structure and a leaf block.
2267 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2268 struct xfs_attr_leafblock *leaf)
2270 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where,
2271 (__psunsigned_t)context->dp,
2272 (__psunsigned_t)context->cursor->hashval,
2273 (__psunsigned_t)context->cursor->blkno,
2274 (__psunsigned_t)context->cursor->offset,
2275 (__psunsigned_t)context->alist,
2276 (__psunsigned_t)context->bufsize,
2277 (__psunsigned_t)context->count,
2278 (__psunsigned_t)context->firstu,
2280 ((context->count > 0) &&
2281 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2282 ? (ATTR_ENTRY(context->alist,
2283 context->count-1)->a_valuelen)
2285 (__psunsigned_t)context->dupcnt,
2286 (__psunsigned_t)context->flags,
2287 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
2288 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2289 (__psunsigned_t)be32_to_cpu(leaf->entries[
2290 be16_to_cpu(leaf->hdr.count)-1].hashval));
2294 * Add a trace buffer entry for the arguments given to the routine,
2298 xfs_attr_trace_enter(int type, char *where,
2299 __psunsigned_t a2, __psunsigned_t a3,
2300 __psunsigned_t a4, __psunsigned_t a5,
2301 __psunsigned_t a6, __psunsigned_t a7,
2302 __psunsigned_t a8, __psunsigned_t a9,
2303 __psunsigned_t a10, __psunsigned_t a11,
2304 __psunsigned_t a12, __psunsigned_t a13,
2305 __psunsigned_t a14, __psunsigned_t a15)
2307 ASSERT(xfs_attr_trace_buf);
2308 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2310 (void *)a2, (void *)a3, (void *)a4,
2311 (void *)a5, (void *)a6, (void *)a7,
2312 (void *)a8, (void *)a9, (void *)a10,
2313 (void *)a11, (void *)a12, (void *)a13,
2314 (void *)a14, (void *)a15);
2316 #endif /* XFS_ATTR_TRACE */
2319 /*========================================================================
2320 * System (pseudo) namespace attribute interface routines.
2321 *========================================================================*/
2324 posix_acl_access_set(
2325 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2327 return xfs_acl_vset(vp, data, size, _ACL_TYPE_ACCESS);
2331 posix_acl_access_remove(
2332 bhv_vnode_t *vp, char *name, int xflags)
2334 return xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
2338 posix_acl_access_get(
2339 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2341 return xfs_acl_vget(vp, data, size, _ACL_TYPE_ACCESS);
2345 posix_acl_access_exists(
2348 return xfs_acl_vhasacl_access(vp);
2352 posix_acl_default_set(
2353 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2355 return xfs_acl_vset(vp, data, size, _ACL_TYPE_DEFAULT);
2359 posix_acl_default_get(
2360 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2362 return xfs_acl_vget(vp, data, size, _ACL_TYPE_DEFAULT);
2366 posix_acl_default_remove(
2367 bhv_vnode_t *vp, char *name, int xflags)
2369 return xfs_acl_vremove(vp, _ACL_TYPE_DEFAULT);
2373 posix_acl_default_exists(
2376 return xfs_acl_vhasacl_default(vp);
2379 STATIC struct attrnames posix_acl_access = {
2380 .attr_name = "posix_acl_access",
2381 .attr_namelen = sizeof("posix_acl_access") - 1,
2382 .attr_get = posix_acl_access_get,
2383 .attr_set = posix_acl_access_set,
2384 .attr_remove = posix_acl_access_remove,
2385 .attr_exists = posix_acl_access_exists,
2388 STATIC struct attrnames posix_acl_default = {
2389 .attr_name = "posix_acl_default",
2390 .attr_namelen = sizeof("posix_acl_default") - 1,
2391 .attr_get = posix_acl_default_get,
2392 .attr_set = posix_acl_default_set,
2393 .attr_remove = posix_acl_default_remove,
2394 .attr_exists = posix_acl_default_exists,
2397 STATIC struct attrnames *attr_system_names[] =
2398 { &posix_acl_access, &posix_acl_default };
2401 /*========================================================================
2402 * Namespace-prefix-style attribute name interface routines.
2403 *========================================================================*/
2407 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2409 return -bhv_vop_attr_set(vp, name, data, size, xflags, NULL);
2414 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2416 int error, asize = size;
2418 error = bhv_vop_attr_get(vp, name, data, &asize, xflags, NULL);
2425 attr_generic_remove(
2426 bhv_vnode_t *vp, char *name, int xflags)
2428 return -bhv_vop_attr_remove(vp, name, xflags, NULL);
2432 attr_generic_listadd(
2433 attrnames_t *prefix,
2434 attrnames_t *namesp,
2439 char *p = data + *result;
2441 *result += prefix->attr_namelen;
2442 *result += namesp->attr_namelen + 1;
2447 strcpy(p, prefix->attr_name);
2448 p += prefix->attr_namelen;
2449 strcpy(p, namesp->attr_name);
2450 p += namesp->attr_namelen + 1;
2461 attrnames_t *namesp;
2464 for (i = 0; i < ATTR_SYSCOUNT; i++) {
2465 namesp = attr_system_names[i];
2466 if (!namesp->attr_exists || !namesp->attr_exists(vp))
2468 error = attr_generic_listadd(&attr_system, namesp,
2469 data, size, result);
2478 bhv_vnode_t *vp, void *data, size_t size, int xflags, ssize_t *result)
2480 attrlist_cursor_kern_t cursor = { 0 };
2483 error = bhv_vop_attr_list(vp, data, size, xflags, &cursor, NULL);
2487 return attr_system_list(vp, data, size, result);
2491 attr_lookup_namespace(
2493 struct attrnames **names,
2498 for (i = 0; i < nnames; i++)
2499 if (!strncmp(name, names[i]->attr_name, names[i]->attr_namelen))
2505 * Some checks to prevent people abusing EAs to get over quota:
2506 * - Don't allow modifying user EAs on devices/symlinks;
2507 * - Don't allow modifying user EAs if sticky bit set;
2514 struct inode *inode = vn_to_inode(vp);
2516 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2518 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
2519 !capable(CAP_SYS_ADMIN))
2521 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
2522 (current_fsuid(cred) != inode->i_uid) && !capable(CAP_FOWNER))
2528 attr_trusted_capable(
2532 struct inode *inode = vn_to_inode(vp);
2534 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2536 if (!capable(CAP_SYS_ADMIN))
2542 attr_secure_capable(
2546 return -ENOSECURITY;
2551 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2553 attrnames_t *namesp;
2556 if (xflags & ATTR_CREATE)
2559 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2562 error = namesp->attr_set(vp, name, data, size, xflags);
2564 error = vn_revalidate(vp);
2570 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2572 attrnames_t *namesp;
2574 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2577 return namesp->attr_get(vp, name, data, size, xflags);
2582 bhv_vnode_t *vp, char *name, int xflags)
2584 attrnames_t *namesp;
2586 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2589 return namesp->attr_remove(vp, name, xflags);
2592 struct attrnames attr_system = {
2593 .attr_name = "system.",
2594 .attr_namelen = sizeof("system.") - 1,
2595 .attr_flag = ATTR_SYSTEM,
2596 .attr_get = attr_system_get,
2597 .attr_set = attr_system_set,
2598 .attr_remove = attr_system_remove,
2599 .attr_capable = (attrcapable_t)fs_noerr,
2602 struct attrnames attr_trusted = {
2603 .attr_name = "trusted.",
2604 .attr_namelen = sizeof("trusted.") - 1,
2605 .attr_flag = ATTR_ROOT,
2606 .attr_get = attr_generic_get,
2607 .attr_set = attr_generic_set,
2608 .attr_remove = attr_generic_remove,
2609 .attr_capable = attr_trusted_capable,
2612 struct attrnames attr_secure = {
2613 .attr_name = "security.",
2614 .attr_namelen = sizeof("security.") - 1,
2615 .attr_flag = ATTR_SECURE,
2616 .attr_get = attr_generic_get,
2617 .attr_set = attr_generic_set,
2618 .attr_remove = attr_generic_remove,
2619 .attr_capable = attr_secure_capable,
2622 struct attrnames attr_user = {
2623 .attr_name = "user.",
2624 .attr_namelen = sizeof("user.") - 1,
2625 .attr_get = attr_generic_get,
2626 .attr_set = attr_generic_set,
2627 .attr_remove = attr_generic_remove,
2628 .attr_capable = attr_user_capable,
2631 struct attrnames *attr_namespaces[] =
2632 { &attr_system, &attr_trusted, &attr_secure, &attr_user };