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"
52 #include "xfs_vnodeops.h"
57 * Provide the external interfaces to manage attribute lists.
60 #define ATTR_SYSCOUNT 2
61 static struct attrnames posix_acl_access;
62 static struct attrnames posix_acl_default;
63 static struct attrnames *attr_system_names[ATTR_SYSCOUNT];
65 /*========================================================================
66 * Function prototypes for the kernel.
67 *========================================================================*/
70 * Internal routines when attribute list fits inside the inode.
72 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
75 * Internal routines when attribute list is one block.
77 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
78 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
79 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
80 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
83 * Internal routines when attribute list is more than one block.
85 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
86 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
87 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
88 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
89 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
90 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
93 * Routines to manipulate out-of-line attribute values.
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 xfs_attr_name_to_xname(
106 struct xfs_name *xname,
112 xname->len = strlen(aname);
113 if (xname->len >= MAXNAMELEN)
114 return EFAULT; /* match IRIX behaviour */
119 /*========================================================================
120 * Overall external interface routines.
121 *========================================================================*/
124 xfs_attr_fetch(xfs_inode_t *ip, struct xfs_name *name,
125 char *value, int *valuelenp, int flags)
130 if ((XFS_IFORK_Q(ip) == 0) ||
131 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
132 ip->i_d.di_anextents == 0))
136 * Fill in the arg structure for this request.
138 memset((char *)&args, 0, sizeof(args));
139 args.name = name->name;
140 args.namelen = name->len;
142 args.valuelen = *valuelenp;
144 args.hashval = xfs_da_hashname(args.name, args.namelen);
146 args.whichfork = XFS_ATTR_FORK;
149 * Decide on what work routines to call based on the inode size.
151 if (XFS_IFORK_Q(ip) == 0 ||
152 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
153 ip->i_d.di_anextents == 0)) {
154 error = XFS_ERROR(ENOATTR);
155 } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
156 error = xfs_attr_shortform_getvalue(&args);
157 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
158 error = xfs_attr_leaf_get(&args);
160 error = xfs_attr_node_get(&args);
164 * Return the number of bytes in the value to the caller.
166 *valuelenp = args.valuelen;
182 struct xfs_name xname;
184 XFS_STATS_INC(xs_attr_get);
186 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
189 error = xfs_attr_name_to_xname(&xname, name);
193 xfs_ilock(ip, XFS_ILOCK_SHARED);
194 error = xfs_attr_fetch(ip, &xname, value, valuelenp, flags);
195 xfs_iunlock(ip, XFS_ILOCK_SHARED);
200 xfs_attr_set_int(xfs_inode_t *dp, struct xfs_name *name,
201 char *value, int valuelen, int flags)
204 xfs_fsblock_t firstblock;
205 xfs_bmap_free_t flist;
206 int error, err2, committed;
209 xfs_mount_t *mp = dp->i_mount;
210 int rsvd = (flags & ATTR_ROOT) != 0;
213 * Attach the dquots to the inode.
215 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
219 * If the inode doesn't have an attribute fork, add one.
220 * (inode must not be locked when we call this routine)
222 if (XFS_IFORK_Q(dp) == 0) {
223 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
224 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
226 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
231 * Fill in the arg structure for this request.
233 memset((char *)&args, 0, sizeof(args));
234 args.name = name->name;
235 args.namelen = name->len;
237 args.valuelen = valuelen;
239 args.hashval = xfs_da_hashname(args.name, args.namelen);
241 args.firstblock = &firstblock;
243 args.whichfork = XFS_ATTR_FORK;
244 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
247 * Determine space new attribute will use, and if it would be
248 * "local" or "remote" (note: local != inline).
250 size = xfs_attr_leaf_newentsize(name->len, valuelen,
251 mp->m_sb.sb_blocksize, &local);
253 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
255 if (size > (mp->m_sb.sb_blocksize >> 1)) {
256 /* Double split possible */
260 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
261 /* Out of line attribute, cannot double split, but make
262 * room for the attribute value itself.
265 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
268 /* Size is now blocks for attribute data */
272 * Start our first transaction of the day.
274 * All future transactions during this code must be "chained" off
275 * this one via the trans_dup() call. All transactions will contain
276 * the inode, and the inode will always be marked with trans_ihold().
277 * Since the inode will be locked in all transactions, we must log
278 * the inode in every transaction to let it float upward through
281 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
284 * Root fork attributes can use reserved data blocks for this
285 * operation if necessary
289 args.trans->t_flags |= XFS_TRANS_RESERVE;
291 if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
292 XFS_ATTRSET_LOG_RES(mp, nblks),
293 0, XFS_TRANS_PERM_LOG_RES,
294 XFS_ATTRSET_LOG_COUNT))) {
295 xfs_trans_cancel(args.trans, 0);
298 xfs_ilock(dp, XFS_ILOCK_EXCL);
300 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
301 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
302 XFS_QMOPT_RES_REGBLKS);
304 xfs_iunlock(dp, XFS_ILOCK_EXCL);
305 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
309 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
310 xfs_trans_ihold(args.trans, dp);
313 * If the attribute list is non-existent or a shortform list,
314 * upgrade it to a single-leaf-block attribute list.
316 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
317 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
318 (dp->i_d.di_anextents == 0))) {
321 * Build initial attribute list (if required).
323 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
324 xfs_attr_shortform_create(&args);
327 * Try to add the attr to the attribute list in
330 error = xfs_attr_shortform_addname(&args);
331 if (error != ENOSPC) {
333 * Commit the shortform mods, and we're done.
334 * NOTE: this is also the error path (EEXIST, etc).
336 ASSERT(args.trans != NULL);
339 * If this is a synchronous mount, make sure that
340 * the transaction goes to disk before returning
343 if (mp->m_flags & XFS_MOUNT_WSYNC) {
344 xfs_trans_set_sync(args.trans);
346 err2 = xfs_trans_commit(args.trans,
347 XFS_TRANS_RELEASE_LOG_RES);
348 xfs_iunlock(dp, XFS_ILOCK_EXCL);
351 * Hit the inode change time.
353 if (!error && (flags & ATTR_KERNOTIME) == 0) {
354 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
356 return(error == 0 ? err2 : error);
360 * It won't fit in the shortform, transform to a leaf block.
361 * GROT: another possible req'mt for a double-split btree op.
363 XFS_BMAP_INIT(args.flist, args.firstblock);
364 error = xfs_attr_shortform_to_leaf(&args);
366 error = xfs_bmap_finish(&args.trans, args.flist,
372 xfs_bmap_cancel(&flist);
377 * bmap_finish() may have committed the last trans and started
378 * a new one. We need the inode to be in all transactions.
381 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
382 xfs_trans_ihold(args.trans, dp);
386 * Commit the leaf transformation. We'll need another (linked)
387 * transaction to add the new attribute to the leaf.
389 if ((error = xfs_attr_rolltrans(&args.trans, dp)))
394 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
395 error = xfs_attr_leaf_addname(&args);
397 error = xfs_attr_node_addname(&args);
404 * If this is a synchronous mount, make sure that the
405 * transaction goes to disk before returning to the user.
407 if (mp->m_flags & XFS_MOUNT_WSYNC) {
408 xfs_trans_set_sync(args.trans);
412 * Commit the last in the sequence of transactions.
414 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
415 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
416 xfs_iunlock(dp, XFS_ILOCK_EXCL);
419 * Hit the inode change time.
421 if (!error && (flags & ATTR_KERNOTIME) == 0) {
422 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
429 xfs_trans_cancel(args.trans,
430 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
431 xfs_iunlock(dp, XFS_ILOCK_EXCL);
444 struct xfs_name xname;
446 XFS_STATS_INC(xs_attr_set);
448 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
451 error = xfs_attr_name_to_xname(&xname, name);
455 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
459 * Generic handler routine to remove a name from an attribute list.
460 * Transitions attribute list from Btree to shortform as necessary.
463 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
466 xfs_fsblock_t firstblock;
467 xfs_bmap_free_t flist;
469 xfs_mount_t *mp = dp->i_mount;
472 * Fill in the arg structure for this request.
474 memset((char *)&args, 0, sizeof(args));
475 args.name = name->name;
476 args.namelen = name->len;
478 args.hashval = xfs_da_hashname(args.name, args.namelen);
480 args.firstblock = &firstblock;
483 args.whichfork = XFS_ATTR_FORK;
486 * Attach the dquots to the inode.
488 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
492 * Start our first transaction of the day.
494 * All future transactions during this code must be "chained" off
495 * this one via the trans_dup() call. All transactions will contain
496 * the inode, and the inode will always be marked with trans_ihold().
497 * Since the inode will be locked in all transactions, we must log
498 * the inode in every transaction to let it float upward through
501 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
504 * Root fork attributes can use reserved data blocks for this
505 * operation if necessary
508 if (flags & ATTR_ROOT)
509 args.trans->t_flags |= XFS_TRANS_RESERVE;
511 if ((error = xfs_trans_reserve(args.trans,
512 XFS_ATTRRM_SPACE_RES(mp),
513 XFS_ATTRRM_LOG_RES(mp),
514 0, XFS_TRANS_PERM_LOG_RES,
515 XFS_ATTRRM_LOG_COUNT))) {
516 xfs_trans_cancel(args.trans, 0);
520 xfs_ilock(dp, XFS_ILOCK_EXCL);
522 * No need to make quota reservations here. We expect to release some
523 * blocks not allocate in the common case.
525 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
526 xfs_trans_ihold(args.trans, dp);
529 * Decide on what work routines to call based on the inode size.
531 if (XFS_IFORK_Q(dp) == 0 ||
532 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
533 dp->i_d.di_anextents == 0)) {
534 error = XFS_ERROR(ENOATTR);
537 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
538 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
539 error = xfs_attr_shortform_remove(&args);
543 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
544 error = xfs_attr_leaf_removename(&args);
546 error = xfs_attr_node_removename(&args);
553 * If this is a synchronous mount, make sure that the
554 * transaction goes to disk before returning to the user.
556 if (mp->m_flags & XFS_MOUNT_WSYNC) {
557 xfs_trans_set_sync(args.trans);
561 * Commit the last in the sequence of transactions.
563 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
564 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
565 xfs_iunlock(dp, XFS_ILOCK_EXCL);
568 * Hit the inode change time.
570 if (!error && (flags & ATTR_KERNOTIME) == 0) {
571 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
578 xfs_trans_cancel(args.trans,
579 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
580 xfs_iunlock(dp, XFS_ILOCK_EXCL);
591 struct xfs_name xname;
593 XFS_STATS_INC(xs_attr_remove);
595 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
598 error = xfs_attr_name_to_xname(&xname, name);
602 xfs_ilock(dp, XFS_ILOCK_SHARED);
603 if (XFS_IFORK_Q(dp) == 0 ||
604 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
605 dp->i_d.di_anextents == 0)) {
606 xfs_iunlock(dp, XFS_ILOCK_SHARED);
607 return(XFS_ERROR(ENOATTR));
609 xfs_iunlock(dp, XFS_ILOCK_SHARED);
611 return xfs_attr_remove_int(dp, &xname, flags);
615 xfs_attr_list_int(xfs_attr_list_context_t *context)
618 xfs_inode_t *dp = context->dp;
621 * Decide on what work routines to call based on the inode size.
623 if (XFS_IFORK_Q(dp) == 0 ||
624 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
625 dp->i_d.di_anextents == 0)) {
627 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
628 error = xfs_attr_shortform_list(context);
629 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
630 error = xfs_attr_leaf_list(context);
632 error = xfs_attr_node_list(context);
637 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
638 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
639 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
640 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
641 & ~(sizeof(u_int32_t)-1))
644 * Format an attribute and copy it out to the user's buffer.
645 * Take care to check values and protect against them changing later,
646 * we may be reading them directly out of a user buffer.
650 xfs_attr_put_listent(xfs_attr_list_context_t *context, attrnames_t *namesp,
651 char *name, int namelen,
652 int valuelen, char *value)
657 ASSERT(!(context->flags & ATTR_KERNOVAL));
658 ASSERT(context->count >= 0);
659 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
660 ASSERT(context->firstu >= sizeof(*context->alist));
661 ASSERT(context->firstu <= context->bufsize);
663 arraytop = sizeof(*context->alist) +
664 context->count * sizeof(context->alist->al_offset[0]);
665 context->firstu -= ATTR_ENTSIZE(namelen);
666 if (context->firstu < arraytop) {
667 xfs_attr_trace_l_c("buffer full", context);
668 context->alist->al_more = 1;
669 context->seen_enough = 1;
673 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
674 aep->a_valuelen = valuelen;
675 memcpy(aep->a_name, name, namelen);
676 aep->a_name[ namelen ] = 0;
677 context->alist->al_offset[ context->count++ ] = context->firstu;
678 context->alist->al_count = context->count;
679 xfs_attr_trace_l_c("add", context);
684 xfs_attr_kern_list(xfs_attr_list_context_t *context, attrnames_t *namesp,
685 char *name, int namelen,
686 int valuelen, char *value)
691 ASSERT(context->count >= 0);
693 arraytop = context->count + namesp->attr_namelen + namelen + 1;
694 if (arraytop > context->firstu) {
695 context->count = -1; /* insufficient space */
698 offset = (char *)context->alist + context->count;
699 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
700 offset += namesp->attr_namelen;
701 strncpy(offset, name, namelen); /* real name */
704 context->count += namesp->attr_namelen + namelen + 1;
710 xfs_attr_kern_list_sizes(xfs_attr_list_context_t *context, attrnames_t *namesp,
711 char *name, int namelen,
712 int valuelen, char *value)
714 context->count += namesp->attr_namelen + namelen + 1;
719 * Generate a list of extended attribute names and optionally
720 * also value lengths. Positive return value follows the XFS
721 * convention of being an error, zero or negative return code
722 * is the length of the buffer returned (negated), indicating
731 attrlist_cursor_kern_t *cursor)
733 xfs_attr_list_context_t context;
736 XFS_STATS_INC(xs_attr_list);
739 * Validate the cursor.
741 if (cursor->pad1 || cursor->pad2)
742 return(XFS_ERROR(EINVAL));
743 if ((cursor->initted == 0) &&
744 (cursor->hashval || cursor->blkno || cursor->offset))
745 return XFS_ERROR(EINVAL);
748 * Check for a properly aligned buffer.
750 if (((long)buffer) & (sizeof(int)-1))
751 return XFS_ERROR(EFAULT);
752 if (flags & ATTR_KERNOVAL)
756 * Initialize the output buffer.
759 context.cursor = cursor;
763 context.flags = flags;
764 context.seen_enough = 0;
765 context.alist = (attrlist_t *)buffer;
766 context.put_value = 0;
768 if (flags & ATTR_KERNAMELS) {
769 context.bufsize = bufsize;
770 context.firstu = context.bufsize;
771 if (flags & ATTR_KERNOVAL)
772 context.put_listent = xfs_attr_kern_list_sizes;
774 context.put_listent = xfs_attr_kern_list;
776 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
777 context.firstu = context.bufsize;
778 context.alist->al_count = 0;
779 context.alist->al_more = 0;
780 context.alist->al_offset[0] = context.bufsize;
781 context.put_listent = xfs_attr_put_listent;
784 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
787 xfs_ilock(dp, XFS_ILOCK_SHARED);
788 xfs_attr_trace_l_c("syscall start", &context);
790 error = xfs_attr_list_int(&context);
792 xfs_iunlock(dp, XFS_ILOCK_SHARED);
793 xfs_attr_trace_l_c("syscall end", &context);
795 if (context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS)) {
796 /* must return negated buffer size or the error */
797 if (context.count < 0)
798 error = XFS_ERROR(ERANGE);
800 error = -context.count;
808 xfs_attr_inactive(xfs_inode_t *dp)
815 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
817 xfs_ilock(dp, XFS_ILOCK_SHARED);
818 if ((XFS_IFORK_Q(dp) == 0) ||
819 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
820 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
821 dp->i_d.di_anextents == 0)) {
822 xfs_iunlock(dp, XFS_ILOCK_SHARED);
825 xfs_iunlock(dp, XFS_ILOCK_SHARED);
828 * Start our first transaction of the day.
830 * All future transactions during this code must be "chained" off
831 * this one via the trans_dup() call. All transactions will contain
832 * the inode, and the inode will always be marked with trans_ihold().
833 * Since the inode will be locked in all transactions, we must log
834 * the inode in every transaction to let it float upward through
837 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
838 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
839 XFS_TRANS_PERM_LOG_RES,
840 XFS_ATTRINVAL_LOG_COUNT))) {
841 xfs_trans_cancel(trans, 0);
844 xfs_ilock(dp, XFS_ILOCK_EXCL);
847 * No need to make quota reservations here. We expect to release some
848 * blocks, not allocate, in the common case.
850 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
851 xfs_trans_ihold(trans, dp);
854 * Decide on what work routines to call based on the inode size.
856 if ((XFS_IFORK_Q(dp) == 0) ||
857 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
858 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
859 dp->i_d.di_anextents == 0)) {
863 error = xfs_attr_root_inactive(&trans, dp);
867 * signal synchronous inactive transactions unless this
868 * is a synchronous mount filesystem in which case we
869 * know that we're here because we've been called out of
870 * xfs_inactive which means that the last reference is gone
871 * and the unlink transaction has already hit the disk so
872 * async inactive transactions are safe.
874 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
875 (!(mp->m_flags & XFS_MOUNT_WSYNC)
880 * Commit the last in the sequence of transactions.
882 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
883 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
884 xfs_iunlock(dp, XFS_ILOCK_EXCL);
889 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
890 xfs_iunlock(dp, XFS_ILOCK_EXCL);
896 /*========================================================================
897 * External routines when attribute list is inside the inode
898 *========================================================================*/
901 * Add a name to the shortform attribute list structure
902 * This is the external routine.
905 xfs_attr_shortform_addname(xfs_da_args_t *args)
907 int newsize, forkoff, retval;
909 retval = xfs_attr_shortform_lookup(args);
910 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
912 } else if (retval == EEXIST) {
913 if (args->flags & ATTR_CREATE)
915 retval = xfs_attr_shortform_remove(args);
919 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
920 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
921 return(XFS_ERROR(ENOSPC));
923 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
924 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
926 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
928 return(XFS_ERROR(ENOSPC));
930 xfs_attr_shortform_add(args, forkoff);
935 /*========================================================================
936 * External routines when attribute list is one block
937 *========================================================================*/
940 * Add a name to the leaf attribute list structure
942 * This leaf block cannot have a "remote" value, we only call this routine
943 * if bmap_one_block() says there is only one block (ie: no remote blks).
946 xfs_attr_leaf_addname(xfs_da_args_t *args)
950 int retval, error, committed, forkoff;
953 * Read the (only) block in the attribute list in.
957 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
964 * Look up the given attribute in the leaf block. Figure out if
965 * the given flags produce an error or call for an atomic rename.
967 retval = xfs_attr_leaf_lookup_int(bp, args);
968 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
969 xfs_da_brelse(args->trans, bp);
971 } else if (retval == EEXIST) {
972 if (args->flags & ATTR_CREATE) { /* pure create op */
973 xfs_da_brelse(args->trans, bp);
976 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
977 args->blkno2 = args->blkno; /* set 2nd entry info*/
978 args->index2 = args->index;
979 args->rmtblkno2 = args->rmtblkno;
980 args->rmtblkcnt2 = args->rmtblkcnt;
984 * Add the attribute to the leaf block, transitioning to a Btree
987 retval = xfs_attr_leaf_add(bp, args);
989 if (retval == ENOSPC) {
991 * Promote the attribute list to the Btree format, then
992 * Commit that transaction so that the node_addname() call
993 * can manage its own transactions.
995 XFS_BMAP_INIT(args->flist, args->firstblock);
996 error = xfs_attr_leaf_to_node(args);
998 error = xfs_bmap_finish(&args->trans, args->flist,
1004 xfs_bmap_cancel(args->flist);
1009 * bmap_finish() may have committed the last trans and started
1010 * a new one. We need the inode to be in all transactions.
1013 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1014 xfs_trans_ihold(args->trans, dp);
1018 * Commit the current trans (including the inode) and start
1021 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1025 * Fob the whole rest of the problem off on the Btree code.
1027 error = xfs_attr_node_addname(args);
1032 * Commit the transaction that added the attr name so that
1033 * later routines can manage their own transactions.
1035 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1039 * If there was an out-of-line value, allocate the blocks we
1040 * identified for its storage and copy the value. This is done
1041 * after we create the attribute so that we don't overflow the
1042 * maximum size of a transaction and/or hit a deadlock.
1044 if (args->rmtblkno > 0) {
1045 error = xfs_attr_rmtval_set(args);
1051 * If this is an atomic rename operation, we must "flip" the
1052 * incomplete flags on the "new" and "old" attribute/value pairs
1053 * so that one disappears and one appears atomically. Then we
1054 * must remove the "old" attribute/value pair.
1056 if (args->op_flags & XFS_DA_OP_RENAME) {
1058 * In a separate transaction, set the incomplete flag on the
1059 * "old" attr and clear the incomplete flag on the "new" attr.
1061 error = xfs_attr_leaf_flipflags(args);
1066 * Dismantle the "old" attribute/value pair by removing
1067 * a "remote" value (if it exists).
1069 args->index = args->index2;
1070 args->blkno = args->blkno2;
1071 args->rmtblkno = args->rmtblkno2;
1072 args->rmtblkcnt = args->rmtblkcnt2;
1073 if (args->rmtblkno) {
1074 error = xfs_attr_rmtval_remove(args);
1080 * Read in the block containing the "old" attr, then
1081 * remove the "old" attr from that block (neat, huh!)
1083 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1084 &bp, XFS_ATTR_FORK);
1088 (void)xfs_attr_leaf_remove(bp, args);
1091 * If the result is small enough, shrink it all into the inode.
1093 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1094 XFS_BMAP_INIT(args->flist, args->firstblock);
1095 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1096 /* bp is gone due to xfs_da_shrink_inode */
1098 error = xfs_bmap_finish(&args->trans,
1105 xfs_bmap_cancel(args->flist);
1110 * bmap_finish() may have committed the last trans
1111 * and started a new one. We need the inode to be
1112 * in all transactions.
1115 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1116 xfs_trans_ihold(args->trans, dp);
1119 xfs_da_buf_done(bp);
1122 * Commit the remove and start the next trans in series.
1124 error = xfs_attr_rolltrans(&args->trans, dp);
1126 } else if (args->rmtblkno > 0) {
1128 * Added a "remote" value, just clear the incomplete flag.
1130 error = xfs_attr_leaf_clearflag(args);
1136 * Remove a name from the leaf attribute list structure
1138 * This leaf block cannot have a "remote" value, we only call this routine
1139 * if bmap_one_block() says there is only one block (ie: no remote blks).
1142 xfs_attr_leaf_removename(xfs_da_args_t *args)
1146 int error, committed, forkoff;
1149 * Remove the attribute.
1153 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1160 error = xfs_attr_leaf_lookup_int(bp, args);
1161 if (error == ENOATTR) {
1162 xfs_da_brelse(args->trans, bp);
1166 (void)xfs_attr_leaf_remove(bp, args);
1169 * If the result is small enough, shrink it all into the inode.
1171 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1172 XFS_BMAP_INIT(args->flist, args->firstblock);
1173 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1174 /* bp is gone due to xfs_da_shrink_inode */
1176 error = xfs_bmap_finish(&args->trans, args->flist,
1182 xfs_bmap_cancel(args->flist);
1187 * bmap_finish() may have committed the last trans and started
1188 * a new one. We need the inode to be in all transactions.
1191 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1192 xfs_trans_ihold(args->trans, dp);
1195 xfs_da_buf_done(bp);
1200 * Look up a name in a leaf attribute list structure.
1202 * This leaf block cannot have a "remote" value, we only call this routine
1203 * if bmap_one_block() says there is only one block (ie: no remote blks).
1206 xfs_attr_leaf_get(xfs_da_args_t *args)
1212 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1218 error = xfs_attr_leaf_lookup_int(bp, args);
1219 if (error != EEXIST) {
1220 xfs_da_brelse(args->trans, bp);
1223 error = xfs_attr_leaf_getvalue(bp, args);
1224 xfs_da_brelse(args->trans, bp);
1225 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1226 error = xfs_attr_rmtval_get(args);
1232 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1235 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1237 xfs_attr_leafblock_t *leaf;
1241 context->cursor->blkno = 0;
1242 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1244 return XFS_ERROR(error);
1247 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1248 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1249 context->dp->i_mount, leaf);
1250 xfs_da_brelse(NULL, bp);
1251 return XFS_ERROR(EFSCORRUPTED);
1254 error = xfs_attr_leaf_list_int(bp, context);
1255 xfs_da_brelse(NULL, bp);
1256 return XFS_ERROR(error);
1260 /*========================================================================
1261 * External routines when attribute list size > XFS_LBSIZE(mp).
1262 *========================================================================*/
1265 * Add a name to a Btree-format attribute list.
1267 * This will involve walking down the Btree, and may involve splitting
1268 * leaf nodes and even splitting intermediate nodes up to and including
1269 * the root node (a special case of an intermediate node).
1271 * "Remote" attribute values confuse the issue and atomic rename operations
1272 * add a whole extra layer of confusion on top of that.
1275 xfs_attr_node_addname(xfs_da_args_t *args)
1277 xfs_da_state_t *state;
1278 xfs_da_state_blk_t *blk;
1281 int committed, retval, error;
1284 * Fill in bucket of arguments/results/context to carry around.
1289 state = xfs_da_state_alloc();
1292 state->blocksize = state->mp->m_sb.sb_blocksize;
1293 state->node_ents = state->mp->m_attr_node_ents;
1296 * Search to see if name already exists, and get back a pointer
1297 * to where it should go.
1299 error = xfs_da_node_lookup_int(state, &retval);
1302 blk = &state->path.blk[ state->path.active-1 ];
1303 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1304 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1306 } else if (retval == EEXIST) {
1307 if (args->flags & ATTR_CREATE)
1309 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1310 args->blkno2 = args->blkno; /* set 2nd entry info*/
1311 args->index2 = args->index;
1312 args->rmtblkno2 = args->rmtblkno;
1313 args->rmtblkcnt2 = args->rmtblkcnt;
1315 args->rmtblkcnt = 0;
1318 retval = xfs_attr_leaf_add(blk->bp, state->args);
1319 if (retval == ENOSPC) {
1320 if (state->path.active == 1) {
1322 * Its really a single leaf node, but it had
1323 * out-of-line values so it looked like it *might*
1324 * have been a b-tree.
1326 xfs_da_state_free(state);
1327 XFS_BMAP_INIT(args->flist, args->firstblock);
1328 error = xfs_attr_leaf_to_node(args);
1330 error = xfs_bmap_finish(&args->trans,
1337 xfs_bmap_cancel(args->flist);
1342 * bmap_finish() may have committed the last trans
1343 * and started a new one. We need the inode to be
1344 * in all transactions.
1347 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1348 xfs_trans_ihold(args->trans, dp);
1352 * Commit the node conversion and start the next
1353 * trans in the chain.
1355 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1362 * Split as many Btree elements as required.
1363 * This code tracks the new and old attr's location
1364 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1365 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1367 XFS_BMAP_INIT(args->flist, args->firstblock);
1368 error = xfs_da_split(state);
1370 error = xfs_bmap_finish(&args->trans, args->flist,
1376 xfs_bmap_cancel(args->flist);
1381 * bmap_finish() may have committed the last trans and started
1382 * a new one. We need the inode to be in all transactions.
1385 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1386 xfs_trans_ihold(args->trans, dp);
1390 * Addition succeeded, update Btree hashvals.
1392 xfs_da_fixhashpath(state, &state->path);
1396 * Kill the state structure, we're done with it and need to
1397 * allow the buffers to come back later.
1399 xfs_da_state_free(state);
1403 * Commit the leaf addition or btree split and start the next
1404 * trans in the chain.
1406 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1410 * If there was an out-of-line value, allocate the blocks we
1411 * identified for its storage and copy the value. This is done
1412 * after we create the attribute so that we don't overflow the
1413 * maximum size of a transaction and/or hit a deadlock.
1415 if (args->rmtblkno > 0) {
1416 error = xfs_attr_rmtval_set(args);
1422 * If this is an atomic rename operation, we must "flip" the
1423 * incomplete flags on the "new" and "old" attribute/value pairs
1424 * so that one disappears and one appears atomically. Then we
1425 * must remove the "old" attribute/value pair.
1427 if (args->op_flags & XFS_DA_OP_RENAME) {
1429 * In a separate transaction, set the incomplete flag on the
1430 * "old" attr and clear the incomplete flag on the "new" attr.
1432 error = xfs_attr_leaf_flipflags(args);
1437 * Dismantle the "old" attribute/value pair by removing
1438 * a "remote" value (if it exists).
1440 args->index = args->index2;
1441 args->blkno = args->blkno2;
1442 args->rmtblkno = args->rmtblkno2;
1443 args->rmtblkcnt = args->rmtblkcnt2;
1444 if (args->rmtblkno) {
1445 error = xfs_attr_rmtval_remove(args);
1451 * Re-find the "old" attribute entry after any split ops.
1452 * The INCOMPLETE flag means that we will find the "old"
1453 * attr, not the "new" one.
1455 args->flags |= XFS_ATTR_INCOMPLETE;
1456 state = xfs_da_state_alloc();
1459 state->blocksize = state->mp->m_sb.sb_blocksize;
1460 state->node_ents = state->mp->m_attr_node_ents;
1462 error = xfs_da_node_lookup_int(state, &retval);
1467 * Remove the name and update the hashvals in the tree.
1469 blk = &state->path.blk[ state->path.active-1 ];
1470 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1471 error = xfs_attr_leaf_remove(blk->bp, args);
1472 xfs_da_fixhashpath(state, &state->path);
1475 * Check to see if the tree needs to be collapsed.
1477 if (retval && (state->path.active > 1)) {
1478 XFS_BMAP_INIT(args->flist, args->firstblock);
1479 error = xfs_da_join(state);
1481 error = xfs_bmap_finish(&args->trans,
1488 xfs_bmap_cancel(args->flist);
1493 * bmap_finish() may have committed the last trans
1494 * and started a new one. We need the inode to be
1495 * in all transactions.
1498 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1499 xfs_trans_ihold(args->trans, dp);
1504 * Commit and start the next trans in the chain.
1506 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1509 } else if (args->rmtblkno > 0) {
1511 * Added a "remote" value, just clear the incomplete flag.
1513 error = xfs_attr_leaf_clearflag(args);
1521 xfs_da_state_free(state);
1528 * Remove a name from a B-tree attribute list.
1530 * This will involve walking down the Btree, and may involve joining
1531 * leaf nodes and even joining intermediate nodes up to and including
1532 * the root node (a special case of an intermediate node).
1535 xfs_attr_node_removename(xfs_da_args_t *args)
1537 xfs_da_state_t *state;
1538 xfs_da_state_blk_t *blk;
1541 int retval, error, committed, forkoff;
1544 * Tie a string around our finger to remind us where we are.
1547 state = xfs_da_state_alloc();
1549 state->mp = dp->i_mount;
1550 state->blocksize = state->mp->m_sb.sb_blocksize;
1551 state->node_ents = state->mp->m_attr_node_ents;
1554 * Search to see if name exists, and get back a pointer to it.
1556 error = xfs_da_node_lookup_int(state, &retval);
1557 if (error || (retval != EEXIST)) {
1564 * If there is an out-of-line value, de-allocate the blocks.
1565 * This is done before we remove the attribute so that we don't
1566 * overflow the maximum size of a transaction and/or hit a deadlock.
1568 blk = &state->path.blk[ state->path.active-1 ];
1569 ASSERT(blk->bp != NULL);
1570 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1571 if (args->rmtblkno > 0) {
1573 * Fill in disk block numbers in the state structure
1574 * so that we can get the buffers back after we commit
1575 * several transactions in the following calls.
1577 error = xfs_attr_fillstate(state);
1582 * Mark the attribute as INCOMPLETE, then bunmapi() the
1585 error = xfs_attr_leaf_setflag(args);
1588 error = xfs_attr_rmtval_remove(args);
1593 * Refill the state structure with buffers, the prior calls
1594 * released our buffers.
1596 error = xfs_attr_refillstate(state);
1602 * Remove the name and update the hashvals in the tree.
1604 blk = &state->path.blk[ state->path.active-1 ];
1605 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1606 retval = xfs_attr_leaf_remove(blk->bp, args);
1607 xfs_da_fixhashpath(state, &state->path);
1610 * Check to see if the tree needs to be collapsed.
1612 if (retval && (state->path.active > 1)) {
1613 XFS_BMAP_INIT(args->flist, args->firstblock);
1614 error = xfs_da_join(state);
1616 error = xfs_bmap_finish(&args->trans, args->flist,
1622 xfs_bmap_cancel(args->flist);
1627 * bmap_finish() may have committed the last trans and started
1628 * a new one. We need the inode to be in all transactions.
1631 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1632 xfs_trans_ihold(args->trans, dp);
1636 * Commit the Btree join operation and start a new trans.
1638 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1643 * If the result is small enough, push it all into the inode.
1645 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1647 * Have to get rid of the copy of this dabuf in the state.
1649 ASSERT(state->path.active == 1);
1650 ASSERT(state->path.blk[0].bp);
1651 xfs_da_buf_done(state->path.blk[0].bp);
1652 state->path.blk[0].bp = NULL;
1654 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1658 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1659 bp->data)->hdr.info.magic)
1660 == XFS_ATTR_LEAF_MAGIC);
1662 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1663 XFS_BMAP_INIT(args->flist, args->firstblock);
1664 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1665 /* bp is gone due to xfs_da_shrink_inode */
1667 error = xfs_bmap_finish(&args->trans,
1674 xfs_bmap_cancel(args->flist);
1679 * bmap_finish() may have committed the last trans
1680 * and started a new one. We need the inode to be
1681 * in all transactions.
1684 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1685 xfs_trans_ihold(args->trans, dp);
1688 xfs_da_brelse(args->trans, bp);
1693 xfs_da_state_free(state);
1698 * Fill in the disk block numbers in the state structure for the buffers
1699 * that are attached to the state structure.
1700 * This is done so that we can quickly reattach ourselves to those buffers
1701 * after some set of transaction commits have released these buffers.
1704 xfs_attr_fillstate(xfs_da_state_t *state)
1706 xfs_da_state_path_t *path;
1707 xfs_da_state_blk_t *blk;
1711 * Roll down the "path" in the state structure, storing the on-disk
1712 * block number for those buffers in the "path".
1714 path = &state->path;
1715 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1716 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1718 blk->disk_blkno = xfs_da_blkno(blk->bp);
1719 xfs_da_buf_done(blk->bp);
1722 blk->disk_blkno = 0;
1727 * Roll down the "altpath" in the state structure, storing the on-disk
1728 * block number for those buffers in the "altpath".
1730 path = &state->altpath;
1731 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1732 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1734 blk->disk_blkno = xfs_da_blkno(blk->bp);
1735 xfs_da_buf_done(blk->bp);
1738 blk->disk_blkno = 0;
1746 * Reattach the buffers to the state structure based on the disk block
1747 * numbers stored in the state structure.
1748 * This is done after some set of transaction commits have released those
1749 * buffers from our grip.
1752 xfs_attr_refillstate(xfs_da_state_t *state)
1754 xfs_da_state_path_t *path;
1755 xfs_da_state_blk_t *blk;
1759 * Roll down the "path" in the state structure, storing the on-disk
1760 * block number for those buffers in the "path".
1762 path = &state->path;
1763 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1764 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1765 if (blk->disk_blkno) {
1766 error = xfs_da_read_buf(state->args->trans,
1768 blk->blkno, blk->disk_blkno,
1769 &blk->bp, XFS_ATTR_FORK);
1778 * Roll down the "altpath" in the state structure, storing the on-disk
1779 * block number for those buffers in the "altpath".
1781 path = &state->altpath;
1782 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1783 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1784 if (blk->disk_blkno) {
1785 error = xfs_da_read_buf(state->args->trans,
1787 blk->blkno, blk->disk_blkno,
1788 &blk->bp, XFS_ATTR_FORK);
1800 * Look up a filename in a node attribute list.
1802 * This routine gets called for any attribute fork that has more than one
1803 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1804 * "remote" values taking up more blocks.
1807 xfs_attr_node_get(xfs_da_args_t *args)
1809 xfs_da_state_t *state;
1810 xfs_da_state_blk_t *blk;
1814 state = xfs_da_state_alloc();
1816 state->mp = args->dp->i_mount;
1817 state->blocksize = state->mp->m_sb.sb_blocksize;
1818 state->node_ents = state->mp->m_attr_node_ents;
1821 * Search to see if name exists, and get back a pointer to it.
1823 error = xfs_da_node_lookup_int(state, &retval);
1826 } else if (retval == EEXIST) {
1827 blk = &state->path.blk[ state->path.active-1 ];
1828 ASSERT(blk->bp != NULL);
1829 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1832 * Get the value, local or "remote"
1834 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1835 if (!retval && (args->rmtblkno > 0)
1836 && !(args->flags & ATTR_KERNOVAL)) {
1837 retval = xfs_attr_rmtval_get(args);
1842 * If not in a transaction, we have to release all the buffers.
1844 for (i = 0; i < state->path.active; i++) {
1845 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1846 state->path.blk[i].bp = NULL;
1849 xfs_da_state_free(state);
1853 STATIC int /* error */
1854 xfs_attr_node_list(xfs_attr_list_context_t *context)
1856 attrlist_cursor_kern_t *cursor;
1857 xfs_attr_leafblock_t *leaf;
1858 xfs_da_intnode_t *node;
1859 xfs_da_node_entry_t *btree;
1863 cursor = context->cursor;
1864 cursor->initted = 1;
1867 * Do all sorts of validation on the passed-in cursor structure.
1868 * If anything is amiss, ignore the cursor and look up the hashval
1869 * starting from the btree root.
1872 if (cursor->blkno > 0) {
1873 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1874 &bp, XFS_ATTR_FORK);
1875 if ((error != 0) && (error != EFSCORRUPTED))
1879 switch (be16_to_cpu(node->hdr.info.magic)) {
1880 case XFS_DA_NODE_MAGIC:
1881 xfs_attr_trace_l_cn("wrong blk", context, node);
1882 xfs_da_brelse(NULL, bp);
1885 case XFS_ATTR_LEAF_MAGIC:
1887 if (cursor->hashval > be32_to_cpu(leaf->entries[
1888 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1889 xfs_attr_trace_l_cl("wrong blk",
1891 xfs_da_brelse(NULL, bp);
1893 } else if (cursor->hashval <=
1894 be32_to_cpu(leaf->entries[0].hashval)) {
1895 xfs_attr_trace_l_cl("maybe wrong blk",
1897 xfs_da_brelse(NULL, bp);
1902 xfs_attr_trace_l_c("wrong blk - ??", context);
1903 xfs_da_brelse(NULL, bp);
1910 * We did not find what we expected given the cursor's contents,
1911 * so we start from the top and work down based on the hash value.
1912 * Note that start of node block is same as start of leaf block.
1917 error = xfs_da_read_buf(NULL, context->dp,
1918 cursor->blkno, -1, &bp,
1922 if (unlikely(bp == NULL)) {
1923 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1925 context->dp->i_mount);
1926 return(XFS_ERROR(EFSCORRUPTED));
1929 if (be16_to_cpu(node->hdr.info.magic)
1930 == XFS_ATTR_LEAF_MAGIC)
1932 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1933 != XFS_DA_NODE_MAGIC)) {
1934 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1936 context->dp->i_mount,
1938 xfs_da_brelse(NULL, bp);
1939 return(XFS_ERROR(EFSCORRUPTED));
1941 btree = node->btree;
1942 for (i = 0; i < be16_to_cpu(node->hdr.count);
1945 <= be32_to_cpu(btree->hashval)) {
1946 cursor->blkno = be32_to_cpu(btree->before);
1947 xfs_attr_trace_l_cb("descending",
1952 if (i == be16_to_cpu(node->hdr.count)) {
1953 xfs_da_brelse(NULL, bp);
1956 xfs_da_brelse(NULL, bp);
1962 * Roll upward through the blocks, processing each leaf block in
1963 * order. As long as there is space in the result buffer, keep
1964 * adding the information.
1968 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1969 != XFS_ATTR_LEAF_MAGIC)) {
1970 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1972 context->dp->i_mount, leaf);
1973 xfs_da_brelse(NULL, bp);
1974 return(XFS_ERROR(EFSCORRUPTED));
1976 error = xfs_attr_leaf_list_int(bp, context);
1978 xfs_da_brelse(NULL, bp);
1981 if (context->seen_enough || leaf->hdr.info.forw == 0)
1983 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1984 xfs_da_brelse(NULL, bp);
1985 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1986 &bp, XFS_ATTR_FORK);
1989 if (unlikely((bp == NULL))) {
1990 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1992 context->dp->i_mount);
1993 return(XFS_ERROR(EFSCORRUPTED));
1996 xfs_da_brelse(NULL, bp);
2001 /*========================================================================
2002 * External routines for manipulating out-of-line attribute values.
2003 *========================================================================*/
2006 * Read the value associated with an attribute from the out-of-line buffer
2007 * that we stored it in.
2010 xfs_attr_rmtval_get(xfs_da_args_t *args)
2012 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
2017 int nmap, error, tmp, valuelen, blkcnt, i;
2020 ASSERT(!(args->flags & ATTR_KERNOVAL));
2022 mp = args->dp->i_mount;
2024 valuelen = args->valuelen;
2025 lblkno = args->rmtblkno;
2026 while (valuelen > 0) {
2027 nmap = ATTR_RMTVALUE_MAPSIZE;
2028 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2030 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2031 NULL, 0, map, &nmap, NULL, NULL);
2036 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2037 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2038 (map[i].br_startblock != HOLESTARTBLOCK));
2039 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2040 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2041 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2042 blkcnt, XFS_BUF_LOCK, &bp);
2046 tmp = (valuelen < XFS_BUF_SIZE(bp))
2047 ? valuelen : XFS_BUF_SIZE(bp);
2048 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2053 lblkno += map[i].br_blockcount;
2056 ASSERT(valuelen == 0);
2061 * Write the value associated with an attribute into the out-of-line buffer
2062 * that we have defined for it.
2065 xfs_attr_rmtval_set(xfs_da_args_t *args)
2068 xfs_fileoff_t lfileoff;
2070 xfs_bmbt_irec_t map;
2075 int blkcnt, valuelen, nmap, error, tmp, committed;
2082 * Find a "hole" in the attribute address space large enough for
2083 * us to drop the new attribute's value into.
2085 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2087 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2092 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2093 args->rmtblkcnt = blkcnt;
2096 * Roll through the "value", allocating blocks on disk as required.
2098 while (blkcnt > 0) {
2100 * Allocate a single extent, up to the size of the value.
2102 XFS_BMAP_INIT(args->flist, args->firstblock);
2104 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2106 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2108 args->firstblock, args->total, &map, &nmap,
2111 error = xfs_bmap_finish(&args->trans, args->flist,
2117 xfs_bmap_cancel(args->flist);
2122 * bmap_finish() may have committed the last trans and started
2123 * a new one. We need the inode to be in all transactions.
2126 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2127 xfs_trans_ihold(args->trans, dp);
2131 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2132 (map.br_startblock != HOLESTARTBLOCK));
2133 lblkno += map.br_blockcount;
2134 blkcnt -= map.br_blockcount;
2137 * Start the next trans in the chain.
2139 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2144 * Roll through the "value", copying the attribute value to the
2145 * already-allocated blocks. Blocks are written synchronously
2146 * so that we can know they are all on disk before we turn off
2147 * the INCOMPLETE flag.
2149 lblkno = args->rmtblkno;
2150 valuelen = args->valuelen;
2151 while (valuelen > 0) {
2153 * Try to remember where we decided to put the value.
2155 XFS_BMAP_INIT(args->flist, args->firstblock);
2157 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2159 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2160 args->firstblock, 0, &map, &nmap,
2166 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2167 (map.br_startblock != HOLESTARTBLOCK));
2169 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2170 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2172 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2173 blkcnt, XFS_BUF_LOCK);
2175 ASSERT(!XFS_BUF_GETERROR(bp));
2177 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2179 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2180 if (tmp < XFS_BUF_SIZE(bp))
2181 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2182 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2188 lblkno += map.br_blockcount;
2190 ASSERT(valuelen == 0);
2195 * Remove the value associated with an attribute by deleting the
2196 * out-of-line buffer that it is stored on.
2199 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2202 xfs_bmbt_irec_t map;
2206 int valuelen, blkcnt, nmap, error, done, committed;
2208 mp = args->dp->i_mount;
2211 * Roll through the "value", invalidating the attribute value's
2214 lblkno = args->rmtblkno;
2215 valuelen = args->rmtblkcnt;
2216 while (valuelen > 0) {
2218 * Try to remember where we decided to put the value.
2220 XFS_BMAP_INIT(args->flist, args->firstblock);
2222 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2224 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2225 args->firstblock, 0, &map, &nmap,
2231 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2232 (map.br_startblock != HOLESTARTBLOCK));
2234 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2235 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2238 * If the "remote" value is in the cache, remove it.
2240 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2241 XFS_INCORE_TRYLOCK);
2244 XFS_BUF_UNDELAYWRITE(bp);
2249 valuelen -= map.br_blockcount;
2251 lblkno += map.br_blockcount;
2255 * Keep de-allocating extents until the remote-value region is gone.
2257 lblkno = args->rmtblkno;
2258 blkcnt = args->rmtblkcnt;
2261 XFS_BMAP_INIT(args->flist, args->firstblock);
2262 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2263 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2264 1, args->firstblock, args->flist,
2267 error = xfs_bmap_finish(&args->trans, args->flist,
2273 xfs_bmap_cancel(args->flist);
2278 * bmap_finish() may have committed the last trans and started
2279 * a new one. We need the inode to be in all transactions.
2282 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2283 xfs_trans_ihold(args->trans, args->dp);
2287 * Close out trans and start the next one in the chain.
2289 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2295 #if defined(XFS_ATTR_TRACE)
2297 * Add a trace buffer entry for an attr_list context structure.
2300 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2302 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where, context,
2303 (__psunsigned_t)NULL,
2304 (__psunsigned_t)NULL,
2305 (__psunsigned_t)NULL);
2309 * Add a trace buffer entry for a context structure and a Btree node.
2312 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2313 struct xfs_da_intnode *node)
2315 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where, context,
2316 (__psunsigned_t)be16_to_cpu(node->hdr.count),
2317 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
2318 (__psunsigned_t)be32_to_cpu(node->btree[
2319 be16_to_cpu(node->hdr.count)-1].hashval));
2323 * Add a trace buffer entry for a context structure and a Btree element.
2326 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2327 struct xfs_da_node_entry *btree)
2329 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where, context,
2330 (__psunsigned_t)be32_to_cpu(btree->hashval),
2331 (__psunsigned_t)be32_to_cpu(btree->before),
2332 (__psunsigned_t)NULL);
2336 * Add a trace buffer entry for a context structure and a leaf block.
2339 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2340 struct xfs_attr_leafblock *leaf)
2342 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where, context,
2343 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
2344 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2345 (__psunsigned_t)be32_to_cpu(leaf->entries[
2346 be16_to_cpu(leaf->hdr.count)-1].hashval));
2350 * Add a trace buffer entry for the arguments given to the routine,
2354 xfs_attr_trace_enter(int type, char *where,
2355 struct xfs_attr_list_context *context,
2356 __psunsigned_t a13, __psunsigned_t a14,
2359 ASSERT(xfs_attr_trace_buf);
2360 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2361 (void *)((__psunsigned_t)where),
2362 (void *)((__psunsigned_t)context->dp),
2363 (void *)((__psunsigned_t)context->cursor->hashval),
2364 (void *)((__psunsigned_t)context->cursor->blkno),
2365 (void *)((__psunsigned_t)context->cursor->offset),
2366 (void *)((__psunsigned_t)context->alist),
2367 (void *)((__psunsigned_t)context->bufsize),
2368 (void *)((__psunsigned_t)context->count),
2369 (void *)((__psunsigned_t)context->firstu),
2370 (void *)((__psunsigned_t)
2371 (((context->count > 0) &&
2372 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2373 ? (ATTR_ENTRY(context->alist,
2374 context->count-1)->a_valuelen)
2376 (void *)((__psunsigned_t)context->dupcnt),
2377 (void *)((__psunsigned_t)context->flags),
2378 (void *)a13, (void *)a14, (void *)a15);
2380 #endif /* XFS_ATTR_TRACE */
2383 /*========================================================================
2384 * System (pseudo) namespace attribute interface routines.
2385 *========================================================================*/
2388 posix_acl_access_set(
2389 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2391 return xfs_acl_vset(vp, data, size, _ACL_TYPE_ACCESS);
2395 posix_acl_access_remove(
2396 bhv_vnode_t *vp, char *name, int xflags)
2398 return xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
2402 posix_acl_access_get(
2403 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2405 return xfs_acl_vget(vp, data, size, _ACL_TYPE_ACCESS);
2409 posix_acl_access_exists(
2412 return xfs_acl_vhasacl_access(vp);
2416 posix_acl_default_set(
2417 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2419 return xfs_acl_vset(vp, data, size, _ACL_TYPE_DEFAULT);
2423 posix_acl_default_get(
2424 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2426 return xfs_acl_vget(vp, data, size, _ACL_TYPE_DEFAULT);
2430 posix_acl_default_remove(
2431 bhv_vnode_t *vp, char *name, int xflags)
2433 return xfs_acl_vremove(vp, _ACL_TYPE_DEFAULT);
2437 posix_acl_default_exists(
2440 return xfs_acl_vhasacl_default(vp);
2443 static struct attrnames posix_acl_access = {
2444 .attr_name = "posix_acl_access",
2445 .attr_namelen = sizeof("posix_acl_access") - 1,
2446 .attr_get = posix_acl_access_get,
2447 .attr_set = posix_acl_access_set,
2448 .attr_remove = posix_acl_access_remove,
2449 .attr_exists = posix_acl_access_exists,
2452 static struct attrnames posix_acl_default = {
2453 .attr_name = "posix_acl_default",
2454 .attr_namelen = sizeof("posix_acl_default") - 1,
2455 .attr_get = posix_acl_default_get,
2456 .attr_set = posix_acl_default_set,
2457 .attr_remove = posix_acl_default_remove,
2458 .attr_exists = posix_acl_default_exists,
2461 static struct attrnames *attr_system_names[] =
2462 { &posix_acl_access, &posix_acl_default };
2465 /*========================================================================
2466 * Namespace-prefix-style attribute name interface routines.
2467 *========================================================================*/
2471 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2473 return -xfs_attr_set(xfs_vtoi(vp), name, data, size, xflags);
2478 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2480 int error, asize = size;
2482 error = xfs_attr_get(xfs_vtoi(vp), name, data, &asize, xflags);
2489 attr_generic_remove(
2490 bhv_vnode_t *vp, char *name, int xflags)
2492 return -xfs_attr_remove(xfs_vtoi(vp), name, xflags);
2496 attr_generic_listadd(
2497 attrnames_t *prefix,
2498 attrnames_t *namesp,
2503 char *p = data + *result;
2505 *result += prefix->attr_namelen;
2506 *result += namesp->attr_namelen + 1;
2511 strcpy(p, prefix->attr_name);
2512 p += prefix->attr_namelen;
2513 strcpy(p, namesp->attr_name);
2514 p += namesp->attr_namelen + 1;
2525 attrnames_t *namesp;
2528 for (i = 0; i < ATTR_SYSCOUNT; i++) {
2529 namesp = attr_system_names[i];
2530 if (!namesp->attr_exists || !namesp->attr_exists(vp))
2532 error = attr_generic_listadd(&attr_system, namesp,
2533 data, size, result);
2542 bhv_vnode_t *vp, void *data, size_t size, int xflags, ssize_t *result)
2544 attrlist_cursor_kern_t cursor = { 0 };
2547 error = xfs_attr_list(xfs_vtoi(vp), data, size, xflags, &cursor);
2551 return attr_system_list(vp, data, size, result);
2555 attr_lookup_namespace(
2557 struct attrnames **names,
2562 for (i = 0; i < nnames; i++)
2563 if (!strncmp(name, names[i]->attr_name, names[i]->attr_namelen))
2570 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2572 attrnames_t *namesp;
2575 if (xflags & ATTR_CREATE)
2578 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2581 error = namesp->attr_set(vp, name, data, size, xflags);
2583 error = vn_revalidate(vp);
2589 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2591 attrnames_t *namesp;
2593 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2596 return namesp->attr_get(vp, name, data, size, xflags);
2601 bhv_vnode_t *vp, char *name, int xflags)
2603 attrnames_t *namesp;
2605 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2608 return namesp->attr_remove(vp, name, xflags);
2611 struct attrnames attr_system = {
2612 .attr_name = "system.",
2613 .attr_namelen = sizeof("system.") - 1,
2614 .attr_flag = ATTR_SYSTEM,
2615 .attr_get = attr_system_get,
2616 .attr_set = attr_system_set,
2617 .attr_remove = attr_system_remove,
2620 struct attrnames attr_trusted = {
2621 .attr_name = "trusted.",
2622 .attr_namelen = sizeof("trusted.") - 1,
2623 .attr_flag = ATTR_ROOT,
2624 .attr_get = attr_generic_get,
2625 .attr_set = attr_generic_set,
2626 .attr_remove = attr_generic_remove,
2629 struct attrnames attr_secure = {
2630 .attr_name = "security.",
2631 .attr_namelen = sizeof("security.") - 1,
2632 .attr_flag = ATTR_SECURE,
2633 .attr_get = attr_generic_get,
2634 .attr_set = attr_generic_set,
2635 .attr_remove = attr_generic_remove,
2638 struct attrnames attr_user = {
2639 .attr_name = "user.",
2640 .attr_namelen = sizeof("user.") - 1,
2641 .attr_get = attr_generic_get,
2642 .attr_set = attr_generic_set,
2643 .attr_remove = attr_generic_remove,
2646 struct attrnames *attr_namespaces[] =
2647 { &attr_system, &attr_trusted, &attr_secure, &attr_user };