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 /*========================================================================
61 * Function prototypes for the kernel.
62 *========================================================================*/
65 * Internal routines when attribute list fits inside the inode.
67 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
70 * Internal routines when attribute list is one block.
72 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
73 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
74 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
75 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
78 * Internal routines when attribute list is more than one block.
80 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
81 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
82 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
83 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
84 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
85 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
88 * Routines to manipulate out-of-line attribute values.
90 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
91 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
93 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
95 #if defined(XFS_ATTR_TRACE)
96 ktrace_t *xfs_attr_trace_buf;
100 xfs_attr_name_to_xname(
101 struct xfs_name *xname,
107 xname->len = strlen(aname);
108 if (xname->len >= MAXNAMELEN)
109 return EFAULT; /* match IRIX behaviour */
116 struct xfs_inode *ip)
118 if (!XFS_IFORK_Q(ip) ||
119 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
120 ip->i_d.di_anextents == 0))
125 /*========================================================================
126 * Overall external interface routines.
127 *========================================================================*/
130 xfs_attr_fetch(xfs_inode_t *ip, struct xfs_name *name,
131 char *value, int *valuelenp, int flags)
136 if (!xfs_inode_hasattr(ip))
140 * Fill in the arg structure for this request.
142 memset((char *)&args, 0, sizeof(args));
143 args.name = name->name;
144 args.namelen = name->len;
146 args.valuelen = *valuelenp;
148 args.hashval = xfs_da_hashname(args.name, args.namelen);
150 args.whichfork = XFS_ATTR_FORK;
153 * Decide on what work routines to call based on the inode size.
155 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_inode_hasattr(dp)) {
532 error = XFS_ERROR(ENOATTR);
535 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
536 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
537 error = xfs_attr_shortform_remove(&args);
541 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
542 error = xfs_attr_leaf_removename(&args);
544 error = xfs_attr_node_removename(&args);
551 * If this is a synchronous mount, make sure that the
552 * transaction goes to disk before returning to the user.
554 if (mp->m_flags & XFS_MOUNT_WSYNC) {
555 xfs_trans_set_sync(args.trans);
559 * Commit the last in the sequence of transactions.
561 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
562 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
563 xfs_iunlock(dp, XFS_ILOCK_EXCL);
566 * Hit the inode change time.
568 if (!error && (flags & ATTR_KERNOTIME) == 0) {
569 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
576 xfs_trans_cancel(args.trans,
577 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
578 xfs_iunlock(dp, XFS_ILOCK_EXCL);
589 struct xfs_name xname;
591 XFS_STATS_INC(xs_attr_remove);
593 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
596 error = xfs_attr_name_to_xname(&xname, name);
600 xfs_ilock(dp, XFS_ILOCK_SHARED);
601 if (!xfs_inode_hasattr(dp)) {
602 xfs_iunlock(dp, XFS_ILOCK_SHARED);
603 return XFS_ERROR(ENOATTR);
605 xfs_iunlock(dp, XFS_ILOCK_SHARED);
607 return xfs_attr_remove_int(dp, &xname, flags);
611 xfs_attr_list_int(xfs_attr_list_context_t *context)
614 xfs_inode_t *dp = context->dp;
617 * Decide on what work routines to call based on the inode size.
619 if (!xfs_inode_hasattr(dp)) {
621 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
622 error = xfs_attr_shortform_list(context);
623 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
624 error = xfs_attr_leaf_list(context);
626 error = xfs_attr_node_list(context);
631 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
632 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
633 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
634 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
635 & ~(sizeof(u_int32_t)-1))
638 * Format an attribute and copy it out to the user's buffer.
639 * Take care to check values and protect against them changing later,
640 * we may be reading them directly out of a user buffer.
644 xfs_attr_put_listent(xfs_attr_list_context_t *context, attrnames_t *namesp,
645 char *name, int namelen,
646 int valuelen, char *value)
651 ASSERT(!(context->flags & ATTR_KERNOVAL));
652 ASSERT(context->count >= 0);
653 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
654 ASSERT(context->firstu >= sizeof(*context->alist));
655 ASSERT(context->firstu <= context->bufsize);
657 arraytop = sizeof(*context->alist) +
658 context->count * sizeof(context->alist->al_offset[0]);
659 context->firstu -= ATTR_ENTSIZE(namelen);
660 if (context->firstu < arraytop) {
661 xfs_attr_trace_l_c("buffer full", context);
662 context->alist->al_more = 1;
663 context->seen_enough = 1;
667 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
668 aep->a_valuelen = valuelen;
669 memcpy(aep->a_name, name, namelen);
670 aep->a_name[ namelen ] = 0;
671 context->alist->al_offset[ context->count++ ] = context->firstu;
672 context->alist->al_count = context->count;
673 xfs_attr_trace_l_c("add", context);
678 xfs_attr_kern_list(xfs_attr_list_context_t *context, attrnames_t *namesp,
679 char *name, int namelen,
680 int valuelen, char *value)
685 ASSERT(context->count >= 0);
687 arraytop = context->count + namesp->attr_namelen + namelen + 1;
688 if (arraytop > context->firstu) {
689 context->count = -1; /* insufficient space */
692 offset = (char *)context->alist + context->count;
693 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
694 offset += namesp->attr_namelen;
695 strncpy(offset, name, namelen); /* real name */
698 context->count += namesp->attr_namelen + namelen + 1;
704 xfs_attr_kern_list_sizes(xfs_attr_list_context_t *context, attrnames_t *namesp,
705 char *name, int namelen,
706 int valuelen, char *value)
708 context->count += namesp->attr_namelen + namelen + 1;
713 * Generate a list of extended attribute names and optionally
714 * also value lengths. Positive return value follows the XFS
715 * convention of being an error, zero or negative return code
716 * is the length of the buffer returned (negated), indicating
725 attrlist_cursor_kern_t *cursor)
727 xfs_attr_list_context_t context;
730 XFS_STATS_INC(xs_attr_list);
733 * Validate the cursor.
735 if (cursor->pad1 || cursor->pad2)
736 return(XFS_ERROR(EINVAL));
737 if ((cursor->initted == 0) &&
738 (cursor->hashval || cursor->blkno || cursor->offset))
739 return XFS_ERROR(EINVAL);
742 * Check for a properly aligned buffer.
744 if (((long)buffer) & (sizeof(int)-1))
745 return XFS_ERROR(EFAULT);
746 if (flags & ATTR_KERNOVAL)
750 * Initialize the output buffer.
753 context.cursor = cursor;
757 context.flags = flags;
758 context.seen_enough = 0;
759 context.alist = (attrlist_t *)buffer;
760 context.put_value = 0;
762 if (flags & ATTR_KERNAMELS) {
763 context.bufsize = bufsize;
764 context.firstu = context.bufsize;
765 if (flags & ATTR_KERNOVAL)
766 context.put_listent = xfs_attr_kern_list_sizes;
768 context.put_listent = xfs_attr_kern_list;
770 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
771 context.firstu = context.bufsize;
772 context.alist->al_count = 0;
773 context.alist->al_more = 0;
774 context.alist->al_offset[0] = context.bufsize;
775 context.put_listent = xfs_attr_put_listent;
778 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
781 xfs_ilock(dp, XFS_ILOCK_SHARED);
782 xfs_attr_trace_l_c("syscall start", &context);
784 error = xfs_attr_list_int(&context);
786 xfs_iunlock(dp, XFS_ILOCK_SHARED);
787 xfs_attr_trace_l_c("syscall end", &context);
789 if (context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS)) {
790 /* must return negated buffer size or the error */
791 if (context.count < 0)
792 error = XFS_ERROR(ERANGE);
794 error = -context.count;
802 xfs_attr_inactive(xfs_inode_t *dp)
809 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
811 xfs_ilock(dp, XFS_ILOCK_SHARED);
812 if (!xfs_inode_hasattr(dp) ||
813 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
814 xfs_iunlock(dp, XFS_ILOCK_SHARED);
817 xfs_iunlock(dp, XFS_ILOCK_SHARED);
820 * Start our first transaction of the day.
822 * All future transactions during this code must be "chained" off
823 * this one via the trans_dup() call. All transactions will contain
824 * the inode, and the inode will always be marked with trans_ihold().
825 * Since the inode will be locked in all transactions, we must log
826 * the inode in every transaction to let it float upward through
829 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
830 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
831 XFS_TRANS_PERM_LOG_RES,
832 XFS_ATTRINVAL_LOG_COUNT))) {
833 xfs_trans_cancel(trans, 0);
836 xfs_ilock(dp, XFS_ILOCK_EXCL);
839 * No need to make quota reservations here. We expect to release some
840 * blocks, not allocate, in the common case.
842 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
843 xfs_trans_ihold(trans, dp);
846 * Decide on what work routines to call based on the inode size.
848 if (!xfs_inode_hasattr(dp) ||
849 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
853 error = xfs_attr_root_inactive(&trans, dp);
857 * signal synchronous inactive transactions unless this
858 * is a synchronous mount filesystem in which case we
859 * know that we're here because we've been called out of
860 * xfs_inactive which means that the last reference is gone
861 * and the unlink transaction has already hit the disk so
862 * async inactive transactions are safe.
864 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
865 (!(mp->m_flags & XFS_MOUNT_WSYNC)
870 * Commit the last in the sequence of transactions.
872 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
873 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
874 xfs_iunlock(dp, XFS_ILOCK_EXCL);
879 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
880 xfs_iunlock(dp, XFS_ILOCK_EXCL);
886 /*========================================================================
887 * External routines when attribute list is inside the inode
888 *========================================================================*/
891 * Add a name to the shortform attribute list structure
892 * This is the external routine.
895 xfs_attr_shortform_addname(xfs_da_args_t *args)
897 int newsize, forkoff, retval;
899 retval = xfs_attr_shortform_lookup(args);
900 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
902 } else if (retval == EEXIST) {
903 if (args->flags & ATTR_CREATE)
905 retval = xfs_attr_shortform_remove(args);
909 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
910 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
911 return(XFS_ERROR(ENOSPC));
913 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
914 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
916 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
918 return(XFS_ERROR(ENOSPC));
920 xfs_attr_shortform_add(args, forkoff);
925 /*========================================================================
926 * External routines when attribute list is one block
927 *========================================================================*/
930 * Add a name to the leaf attribute list structure
932 * This leaf block cannot have a "remote" value, we only call this routine
933 * if bmap_one_block() says there is only one block (ie: no remote blks).
936 xfs_attr_leaf_addname(xfs_da_args_t *args)
940 int retval, error, committed, forkoff;
943 * Read the (only) block in the attribute list in.
947 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
954 * Look up the given attribute in the leaf block. Figure out if
955 * the given flags produce an error or call for an atomic rename.
957 retval = xfs_attr_leaf_lookup_int(bp, args);
958 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
959 xfs_da_brelse(args->trans, bp);
961 } else if (retval == EEXIST) {
962 if (args->flags & ATTR_CREATE) { /* pure create op */
963 xfs_da_brelse(args->trans, bp);
966 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
967 args->blkno2 = args->blkno; /* set 2nd entry info*/
968 args->index2 = args->index;
969 args->rmtblkno2 = args->rmtblkno;
970 args->rmtblkcnt2 = args->rmtblkcnt;
974 * Add the attribute to the leaf block, transitioning to a Btree
977 retval = xfs_attr_leaf_add(bp, args);
979 if (retval == ENOSPC) {
981 * Promote the attribute list to the Btree format, then
982 * Commit that transaction so that the node_addname() call
983 * can manage its own transactions.
985 XFS_BMAP_INIT(args->flist, args->firstblock);
986 error = xfs_attr_leaf_to_node(args);
988 error = xfs_bmap_finish(&args->trans, args->flist,
994 xfs_bmap_cancel(args->flist);
999 * bmap_finish() may have committed the last trans and started
1000 * a new one. We need the inode to be in all transactions.
1003 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1004 xfs_trans_ihold(args->trans, dp);
1008 * Commit the current trans (including the inode) and start
1011 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1015 * Fob the whole rest of the problem off on the Btree code.
1017 error = xfs_attr_node_addname(args);
1022 * Commit the transaction that added the attr name so that
1023 * later routines can manage their own transactions.
1025 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1029 * If there was an out-of-line value, allocate the blocks we
1030 * identified for its storage and copy the value. This is done
1031 * after we create the attribute so that we don't overflow the
1032 * maximum size of a transaction and/or hit a deadlock.
1034 if (args->rmtblkno > 0) {
1035 error = xfs_attr_rmtval_set(args);
1041 * If this is an atomic rename operation, we must "flip" the
1042 * incomplete flags on the "new" and "old" attribute/value pairs
1043 * so that one disappears and one appears atomically. Then we
1044 * must remove the "old" attribute/value pair.
1046 if (args->op_flags & XFS_DA_OP_RENAME) {
1048 * In a separate transaction, set the incomplete flag on the
1049 * "old" attr and clear the incomplete flag on the "new" attr.
1051 error = xfs_attr_leaf_flipflags(args);
1056 * Dismantle the "old" attribute/value pair by removing
1057 * a "remote" value (if it exists).
1059 args->index = args->index2;
1060 args->blkno = args->blkno2;
1061 args->rmtblkno = args->rmtblkno2;
1062 args->rmtblkcnt = args->rmtblkcnt2;
1063 if (args->rmtblkno) {
1064 error = xfs_attr_rmtval_remove(args);
1070 * Read in the block containing the "old" attr, then
1071 * remove the "old" attr from that block (neat, huh!)
1073 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1074 &bp, XFS_ATTR_FORK);
1078 (void)xfs_attr_leaf_remove(bp, args);
1081 * If the result is small enough, shrink it all into the inode.
1083 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1084 XFS_BMAP_INIT(args->flist, args->firstblock);
1085 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1086 /* bp is gone due to xfs_da_shrink_inode */
1088 error = xfs_bmap_finish(&args->trans,
1095 xfs_bmap_cancel(args->flist);
1100 * bmap_finish() may have committed the last trans
1101 * and started a new one. We need the inode to be
1102 * in all transactions.
1105 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1106 xfs_trans_ihold(args->trans, dp);
1109 xfs_da_buf_done(bp);
1112 * Commit the remove and start the next trans in series.
1114 error = xfs_attr_rolltrans(&args->trans, dp);
1116 } else if (args->rmtblkno > 0) {
1118 * Added a "remote" value, just clear the incomplete flag.
1120 error = xfs_attr_leaf_clearflag(args);
1126 * Remove a name from the leaf attribute list structure
1128 * This leaf block cannot have a "remote" value, we only call this routine
1129 * if bmap_one_block() says there is only one block (ie: no remote blks).
1132 xfs_attr_leaf_removename(xfs_da_args_t *args)
1136 int error, committed, forkoff;
1139 * Remove the attribute.
1143 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1150 error = xfs_attr_leaf_lookup_int(bp, args);
1151 if (error == ENOATTR) {
1152 xfs_da_brelse(args->trans, bp);
1156 (void)xfs_attr_leaf_remove(bp, args);
1159 * If the result is small enough, shrink it all into the inode.
1161 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1162 XFS_BMAP_INIT(args->flist, args->firstblock);
1163 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1164 /* bp is gone due to xfs_da_shrink_inode */
1166 error = xfs_bmap_finish(&args->trans, args->flist,
1172 xfs_bmap_cancel(args->flist);
1177 * bmap_finish() may have committed the last trans and started
1178 * a new one. We need the inode to be in all transactions.
1181 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1182 xfs_trans_ihold(args->trans, dp);
1185 xfs_da_buf_done(bp);
1190 * Look up a name in a leaf attribute list structure.
1192 * This leaf block cannot have a "remote" value, we only call this routine
1193 * if bmap_one_block() says there is only one block (ie: no remote blks).
1196 xfs_attr_leaf_get(xfs_da_args_t *args)
1202 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1208 error = xfs_attr_leaf_lookup_int(bp, args);
1209 if (error != EEXIST) {
1210 xfs_da_brelse(args->trans, bp);
1213 error = xfs_attr_leaf_getvalue(bp, args);
1214 xfs_da_brelse(args->trans, bp);
1215 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1216 error = xfs_attr_rmtval_get(args);
1222 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1225 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1227 xfs_attr_leafblock_t *leaf;
1231 context->cursor->blkno = 0;
1232 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1234 return XFS_ERROR(error);
1237 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1238 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1239 context->dp->i_mount, leaf);
1240 xfs_da_brelse(NULL, bp);
1241 return XFS_ERROR(EFSCORRUPTED);
1244 error = xfs_attr_leaf_list_int(bp, context);
1245 xfs_da_brelse(NULL, bp);
1246 return XFS_ERROR(error);
1250 /*========================================================================
1251 * External routines when attribute list size > XFS_LBSIZE(mp).
1252 *========================================================================*/
1255 * Add a name to a Btree-format attribute list.
1257 * This will involve walking down the Btree, and may involve splitting
1258 * leaf nodes and even splitting intermediate nodes up to and including
1259 * the root node (a special case of an intermediate node).
1261 * "Remote" attribute values confuse the issue and atomic rename operations
1262 * add a whole extra layer of confusion on top of that.
1265 xfs_attr_node_addname(xfs_da_args_t *args)
1267 xfs_da_state_t *state;
1268 xfs_da_state_blk_t *blk;
1271 int committed, retval, error;
1274 * Fill in bucket of arguments/results/context to carry around.
1279 state = xfs_da_state_alloc();
1282 state->blocksize = state->mp->m_sb.sb_blocksize;
1283 state->node_ents = state->mp->m_attr_node_ents;
1286 * Search to see if name already exists, and get back a pointer
1287 * to where it should go.
1289 error = xfs_da_node_lookup_int(state, &retval);
1292 blk = &state->path.blk[ state->path.active-1 ];
1293 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1294 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1296 } else if (retval == EEXIST) {
1297 if (args->flags & ATTR_CREATE)
1299 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1300 args->blkno2 = args->blkno; /* set 2nd entry info*/
1301 args->index2 = args->index;
1302 args->rmtblkno2 = args->rmtblkno;
1303 args->rmtblkcnt2 = args->rmtblkcnt;
1305 args->rmtblkcnt = 0;
1308 retval = xfs_attr_leaf_add(blk->bp, state->args);
1309 if (retval == ENOSPC) {
1310 if (state->path.active == 1) {
1312 * Its really a single leaf node, but it had
1313 * out-of-line values so it looked like it *might*
1314 * have been a b-tree.
1316 xfs_da_state_free(state);
1317 XFS_BMAP_INIT(args->flist, args->firstblock);
1318 error = xfs_attr_leaf_to_node(args);
1320 error = xfs_bmap_finish(&args->trans,
1327 xfs_bmap_cancel(args->flist);
1332 * bmap_finish() may have committed the last trans
1333 * and started a new one. We need the inode to be
1334 * in all transactions.
1337 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1338 xfs_trans_ihold(args->trans, dp);
1342 * Commit the node conversion and start the next
1343 * trans in the chain.
1345 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1352 * Split as many Btree elements as required.
1353 * This code tracks the new and old attr's location
1354 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1355 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1357 XFS_BMAP_INIT(args->flist, args->firstblock);
1358 error = xfs_da_split(state);
1360 error = xfs_bmap_finish(&args->trans, args->flist,
1366 xfs_bmap_cancel(args->flist);
1371 * bmap_finish() may have committed the last trans and started
1372 * a new one. We need the inode to be in all transactions.
1375 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1376 xfs_trans_ihold(args->trans, dp);
1380 * Addition succeeded, update Btree hashvals.
1382 xfs_da_fixhashpath(state, &state->path);
1386 * Kill the state structure, we're done with it and need to
1387 * allow the buffers to come back later.
1389 xfs_da_state_free(state);
1393 * Commit the leaf addition or btree split and start the next
1394 * trans in the chain.
1396 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1400 * If there was an out-of-line value, allocate the blocks we
1401 * identified for its storage and copy the value. This is done
1402 * after we create the attribute so that we don't overflow the
1403 * maximum size of a transaction and/or hit a deadlock.
1405 if (args->rmtblkno > 0) {
1406 error = xfs_attr_rmtval_set(args);
1412 * If this is an atomic rename operation, we must "flip" the
1413 * incomplete flags on the "new" and "old" attribute/value pairs
1414 * so that one disappears and one appears atomically. Then we
1415 * must remove the "old" attribute/value pair.
1417 if (args->op_flags & XFS_DA_OP_RENAME) {
1419 * In a separate transaction, set the incomplete flag on the
1420 * "old" attr and clear the incomplete flag on the "new" attr.
1422 error = xfs_attr_leaf_flipflags(args);
1427 * Dismantle the "old" attribute/value pair by removing
1428 * a "remote" value (if it exists).
1430 args->index = args->index2;
1431 args->blkno = args->blkno2;
1432 args->rmtblkno = args->rmtblkno2;
1433 args->rmtblkcnt = args->rmtblkcnt2;
1434 if (args->rmtblkno) {
1435 error = xfs_attr_rmtval_remove(args);
1441 * Re-find the "old" attribute entry after any split ops.
1442 * The INCOMPLETE flag means that we will find the "old"
1443 * attr, not the "new" one.
1445 args->flags |= XFS_ATTR_INCOMPLETE;
1446 state = xfs_da_state_alloc();
1449 state->blocksize = state->mp->m_sb.sb_blocksize;
1450 state->node_ents = state->mp->m_attr_node_ents;
1452 error = xfs_da_node_lookup_int(state, &retval);
1457 * Remove the name and update the hashvals in the tree.
1459 blk = &state->path.blk[ state->path.active-1 ];
1460 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1461 error = xfs_attr_leaf_remove(blk->bp, args);
1462 xfs_da_fixhashpath(state, &state->path);
1465 * Check to see if the tree needs to be collapsed.
1467 if (retval && (state->path.active > 1)) {
1468 XFS_BMAP_INIT(args->flist, args->firstblock);
1469 error = xfs_da_join(state);
1471 error = xfs_bmap_finish(&args->trans,
1478 xfs_bmap_cancel(args->flist);
1483 * bmap_finish() may have committed the last trans
1484 * and started a new one. We need the inode to be
1485 * in all transactions.
1488 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1489 xfs_trans_ihold(args->trans, dp);
1494 * Commit and start the next trans in the chain.
1496 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1499 } else if (args->rmtblkno > 0) {
1501 * Added a "remote" value, just clear the incomplete flag.
1503 error = xfs_attr_leaf_clearflag(args);
1511 xfs_da_state_free(state);
1518 * Remove a name from a B-tree attribute list.
1520 * This will involve walking down the Btree, and may involve joining
1521 * leaf nodes and even joining intermediate nodes up to and including
1522 * the root node (a special case of an intermediate node).
1525 xfs_attr_node_removename(xfs_da_args_t *args)
1527 xfs_da_state_t *state;
1528 xfs_da_state_blk_t *blk;
1531 int retval, error, committed, forkoff;
1534 * Tie a string around our finger to remind us where we are.
1537 state = xfs_da_state_alloc();
1539 state->mp = dp->i_mount;
1540 state->blocksize = state->mp->m_sb.sb_blocksize;
1541 state->node_ents = state->mp->m_attr_node_ents;
1544 * Search to see if name exists, and get back a pointer to it.
1546 error = xfs_da_node_lookup_int(state, &retval);
1547 if (error || (retval != EEXIST)) {
1554 * If there is an out-of-line value, de-allocate the blocks.
1555 * This is done before we remove the attribute so that we don't
1556 * overflow the maximum size of a transaction and/or hit a deadlock.
1558 blk = &state->path.blk[ state->path.active-1 ];
1559 ASSERT(blk->bp != NULL);
1560 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1561 if (args->rmtblkno > 0) {
1563 * Fill in disk block numbers in the state structure
1564 * so that we can get the buffers back after we commit
1565 * several transactions in the following calls.
1567 error = xfs_attr_fillstate(state);
1572 * Mark the attribute as INCOMPLETE, then bunmapi() the
1575 error = xfs_attr_leaf_setflag(args);
1578 error = xfs_attr_rmtval_remove(args);
1583 * Refill the state structure with buffers, the prior calls
1584 * released our buffers.
1586 error = xfs_attr_refillstate(state);
1592 * Remove the name and update the hashvals in the tree.
1594 blk = &state->path.blk[ state->path.active-1 ];
1595 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1596 retval = xfs_attr_leaf_remove(blk->bp, args);
1597 xfs_da_fixhashpath(state, &state->path);
1600 * Check to see if the tree needs to be collapsed.
1602 if (retval && (state->path.active > 1)) {
1603 XFS_BMAP_INIT(args->flist, args->firstblock);
1604 error = xfs_da_join(state);
1606 error = xfs_bmap_finish(&args->trans, args->flist,
1612 xfs_bmap_cancel(args->flist);
1617 * bmap_finish() may have committed the last trans and started
1618 * a new one. We need the inode to be in all transactions.
1621 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1622 xfs_trans_ihold(args->trans, dp);
1626 * Commit the Btree join operation and start a new trans.
1628 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1633 * If the result is small enough, push it all into the inode.
1635 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1637 * Have to get rid of the copy of this dabuf in the state.
1639 ASSERT(state->path.active == 1);
1640 ASSERT(state->path.blk[0].bp);
1641 xfs_da_buf_done(state->path.blk[0].bp);
1642 state->path.blk[0].bp = NULL;
1644 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1648 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1649 bp->data)->hdr.info.magic)
1650 == XFS_ATTR_LEAF_MAGIC);
1652 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1653 XFS_BMAP_INIT(args->flist, args->firstblock);
1654 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1655 /* bp is gone due to xfs_da_shrink_inode */
1657 error = xfs_bmap_finish(&args->trans,
1664 xfs_bmap_cancel(args->flist);
1669 * bmap_finish() may have committed the last trans
1670 * and started a new one. We need the inode to be
1671 * in all transactions.
1674 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1675 xfs_trans_ihold(args->trans, dp);
1678 xfs_da_brelse(args->trans, bp);
1683 xfs_da_state_free(state);
1688 * Fill in the disk block numbers in the state structure for the buffers
1689 * that are attached to the state structure.
1690 * This is done so that we can quickly reattach ourselves to those buffers
1691 * after some set of transaction commits have released these buffers.
1694 xfs_attr_fillstate(xfs_da_state_t *state)
1696 xfs_da_state_path_t *path;
1697 xfs_da_state_blk_t *blk;
1701 * Roll down the "path" in the state structure, storing the on-disk
1702 * block number for those buffers in the "path".
1704 path = &state->path;
1705 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1706 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1708 blk->disk_blkno = xfs_da_blkno(blk->bp);
1709 xfs_da_buf_done(blk->bp);
1712 blk->disk_blkno = 0;
1717 * Roll down the "altpath" in the state structure, storing the on-disk
1718 * block number for those buffers in the "altpath".
1720 path = &state->altpath;
1721 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1722 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1724 blk->disk_blkno = xfs_da_blkno(blk->bp);
1725 xfs_da_buf_done(blk->bp);
1728 blk->disk_blkno = 0;
1736 * Reattach the buffers to the state structure based on the disk block
1737 * numbers stored in the state structure.
1738 * This is done after some set of transaction commits have released those
1739 * buffers from our grip.
1742 xfs_attr_refillstate(xfs_da_state_t *state)
1744 xfs_da_state_path_t *path;
1745 xfs_da_state_blk_t *blk;
1749 * Roll down the "path" in the state structure, storing the on-disk
1750 * block number for those buffers in the "path".
1752 path = &state->path;
1753 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1754 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1755 if (blk->disk_blkno) {
1756 error = xfs_da_read_buf(state->args->trans,
1758 blk->blkno, blk->disk_blkno,
1759 &blk->bp, XFS_ATTR_FORK);
1768 * Roll down the "altpath" in the state structure, storing the on-disk
1769 * block number for those buffers in the "altpath".
1771 path = &state->altpath;
1772 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1773 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1774 if (blk->disk_blkno) {
1775 error = xfs_da_read_buf(state->args->trans,
1777 blk->blkno, blk->disk_blkno,
1778 &blk->bp, XFS_ATTR_FORK);
1790 * Look up a filename in a node attribute list.
1792 * This routine gets called for any attribute fork that has more than one
1793 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1794 * "remote" values taking up more blocks.
1797 xfs_attr_node_get(xfs_da_args_t *args)
1799 xfs_da_state_t *state;
1800 xfs_da_state_blk_t *blk;
1804 state = xfs_da_state_alloc();
1806 state->mp = args->dp->i_mount;
1807 state->blocksize = state->mp->m_sb.sb_blocksize;
1808 state->node_ents = state->mp->m_attr_node_ents;
1811 * Search to see if name exists, and get back a pointer to it.
1813 error = xfs_da_node_lookup_int(state, &retval);
1816 } else if (retval == EEXIST) {
1817 blk = &state->path.blk[ state->path.active-1 ];
1818 ASSERT(blk->bp != NULL);
1819 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1822 * Get the value, local or "remote"
1824 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1825 if (!retval && (args->rmtblkno > 0)
1826 && !(args->flags & ATTR_KERNOVAL)) {
1827 retval = xfs_attr_rmtval_get(args);
1832 * If not in a transaction, we have to release all the buffers.
1834 for (i = 0; i < state->path.active; i++) {
1835 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1836 state->path.blk[i].bp = NULL;
1839 xfs_da_state_free(state);
1843 STATIC int /* error */
1844 xfs_attr_node_list(xfs_attr_list_context_t *context)
1846 attrlist_cursor_kern_t *cursor;
1847 xfs_attr_leafblock_t *leaf;
1848 xfs_da_intnode_t *node;
1849 xfs_da_node_entry_t *btree;
1853 cursor = context->cursor;
1854 cursor->initted = 1;
1857 * Do all sorts of validation on the passed-in cursor structure.
1858 * If anything is amiss, ignore the cursor and look up the hashval
1859 * starting from the btree root.
1862 if (cursor->blkno > 0) {
1863 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1864 &bp, XFS_ATTR_FORK);
1865 if ((error != 0) && (error != EFSCORRUPTED))
1869 switch (be16_to_cpu(node->hdr.info.magic)) {
1870 case XFS_DA_NODE_MAGIC:
1871 xfs_attr_trace_l_cn("wrong blk", context, node);
1872 xfs_da_brelse(NULL, bp);
1875 case XFS_ATTR_LEAF_MAGIC:
1877 if (cursor->hashval > be32_to_cpu(leaf->entries[
1878 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1879 xfs_attr_trace_l_cl("wrong blk",
1881 xfs_da_brelse(NULL, bp);
1883 } else if (cursor->hashval <=
1884 be32_to_cpu(leaf->entries[0].hashval)) {
1885 xfs_attr_trace_l_cl("maybe wrong blk",
1887 xfs_da_brelse(NULL, bp);
1892 xfs_attr_trace_l_c("wrong blk - ??", context);
1893 xfs_da_brelse(NULL, bp);
1900 * We did not find what we expected given the cursor's contents,
1901 * so we start from the top and work down based on the hash value.
1902 * Note that start of node block is same as start of leaf block.
1907 error = xfs_da_read_buf(NULL, context->dp,
1908 cursor->blkno, -1, &bp,
1912 if (unlikely(bp == NULL)) {
1913 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1915 context->dp->i_mount);
1916 return(XFS_ERROR(EFSCORRUPTED));
1919 if (be16_to_cpu(node->hdr.info.magic)
1920 == XFS_ATTR_LEAF_MAGIC)
1922 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1923 != XFS_DA_NODE_MAGIC)) {
1924 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1926 context->dp->i_mount,
1928 xfs_da_brelse(NULL, bp);
1929 return(XFS_ERROR(EFSCORRUPTED));
1931 btree = node->btree;
1932 for (i = 0; i < be16_to_cpu(node->hdr.count);
1935 <= be32_to_cpu(btree->hashval)) {
1936 cursor->blkno = be32_to_cpu(btree->before);
1937 xfs_attr_trace_l_cb("descending",
1942 if (i == be16_to_cpu(node->hdr.count)) {
1943 xfs_da_brelse(NULL, bp);
1946 xfs_da_brelse(NULL, bp);
1952 * Roll upward through the blocks, processing each leaf block in
1953 * order. As long as there is space in the result buffer, keep
1954 * adding the information.
1958 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1959 != XFS_ATTR_LEAF_MAGIC)) {
1960 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1962 context->dp->i_mount, leaf);
1963 xfs_da_brelse(NULL, bp);
1964 return(XFS_ERROR(EFSCORRUPTED));
1966 error = xfs_attr_leaf_list_int(bp, context);
1968 xfs_da_brelse(NULL, bp);
1971 if (context->seen_enough || leaf->hdr.info.forw == 0)
1973 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1974 xfs_da_brelse(NULL, bp);
1975 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1976 &bp, XFS_ATTR_FORK);
1979 if (unlikely((bp == NULL))) {
1980 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1982 context->dp->i_mount);
1983 return(XFS_ERROR(EFSCORRUPTED));
1986 xfs_da_brelse(NULL, bp);
1991 /*========================================================================
1992 * External routines for manipulating out-of-line attribute values.
1993 *========================================================================*/
1996 * Read the value associated with an attribute from the out-of-line buffer
1997 * that we stored it in.
2000 xfs_attr_rmtval_get(xfs_da_args_t *args)
2002 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
2007 int nmap, error, tmp, valuelen, blkcnt, i;
2010 ASSERT(!(args->flags & ATTR_KERNOVAL));
2012 mp = args->dp->i_mount;
2014 valuelen = args->valuelen;
2015 lblkno = args->rmtblkno;
2016 while (valuelen > 0) {
2017 nmap = ATTR_RMTVALUE_MAPSIZE;
2018 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2020 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2021 NULL, 0, map, &nmap, NULL, NULL);
2026 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2027 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2028 (map[i].br_startblock != HOLESTARTBLOCK));
2029 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2030 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2031 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2032 blkcnt, XFS_BUF_LOCK, &bp);
2036 tmp = (valuelen < XFS_BUF_SIZE(bp))
2037 ? valuelen : XFS_BUF_SIZE(bp);
2038 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2043 lblkno += map[i].br_blockcount;
2046 ASSERT(valuelen == 0);
2051 * Write the value associated with an attribute into the out-of-line buffer
2052 * that we have defined for it.
2055 xfs_attr_rmtval_set(xfs_da_args_t *args)
2058 xfs_fileoff_t lfileoff;
2060 xfs_bmbt_irec_t map;
2065 int blkcnt, valuelen, nmap, error, tmp, committed;
2072 * Find a "hole" in the attribute address space large enough for
2073 * us to drop the new attribute's value into.
2075 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2077 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2082 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2083 args->rmtblkcnt = blkcnt;
2086 * Roll through the "value", allocating blocks on disk as required.
2088 while (blkcnt > 0) {
2090 * Allocate a single extent, up to the size of the value.
2092 XFS_BMAP_INIT(args->flist, args->firstblock);
2094 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2096 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2098 args->firstblock, args->total, &map, &nmap,
2101 error = xfs_bmap_finish(&args->trans, args->flist,
2107 xfs_bmap_cancel(args->flist);
2112 * bmap_finish() may have committed the last trans and started
2113 * a new one. We need the inode to be in all transactions.
2116 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2117 xfs_trans_ihold(args->trans, dp);
2121 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2122 (map.br_startblock != HOLESTARTBLOCK));
2123 lblkno += map.br_blockcount;
2124 blkcnt -= map.br_blockcount;
2127 * Start the next trans in the chain.
2129 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2134 * Roll through the "value", copying the attribute value to the
2135 * already-allocated blocks. Blocks are written synchronously
2136 * so that we can know they are all on disk before we turn off
2137 * the INCOMPLETE flag.
2139 lblkno = args->rmtblkno;
2140 valuelen = args->valuelen;
2141 while (valuelen > 0) {
2143 * Try to remember where we decided to put the value.
2145 XFS_BMAP_INIT(args->flist, args->firstblock);
2147 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2149 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2150 args->firstblock, 0, &map, &nmap,
2156 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2157 (map.br_startblock != HOLESTARTBLOCK));
2159 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2160 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2162 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2163 blkcnt, XFS_BUF_LOCK);
2165 ASSERT(!XFS_BUF_GETERROR(bp));
2167 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2169 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2170 if (tmp < XFS_BUF_SIZE(bp))
2171 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2172 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2178 lblkno += map.br_blockcount;
2180 ASSERT(valuelen == 0);
2185 * Remove the value associated with an attribute by deleting the
2186 * out-of-line buffer that it is stored on.
2189 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2192 xfs_bmbt_irec_t map;
2196 int valuelen, blkcnt, nmap, error, done, committed;
2198 mp = args->dp->i_mount;
2201 * Roll through the "value", invalidating the attribute value's
2204 lblkno = args->rmtblkno;
2205 valuelen = args->rmtblkcnt;
2206 while (valuelen > 0) {
2208 * Try to remember where we decided to put the value.
2210 XFS_BMAP_INIT(args->flist, args->firstblock);
2212 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2214 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2215 args->firstblock, 0, &map, &nmap,
2221 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2222 (map.br_startblock != HOLESTARTBLOCK));
2224 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2225 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2228 * If the "remote" value is in the cache, remove it.
2230 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2231 XFS_INCORE_TRYLOCK);
2234 XFS_BUF_UNDELAYWRITE(bp);
2239 valuelen -= map.br_blockcount;
2241 lblkno += map.br_blockcount;
2245 * Keep de-allocating extents until the remote-value region is gone.
2247 lblkno = args->rmtblkno;
2248 blkcnt = args->rmtblkcnt;
2251 XFS_BMAP_INIT(args->flist, args->firstblock);
2252 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2253 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2254 1, args->firstblock, args->flist,
2257 error = xfs_bmap_finish(&args->trans, args->flist,
2263 xfs_bmap_cancel(args->flist);
2268 * bmap_finish() may have committed the last trans and started
2269 * a new one. We need the inode to be in all transactions.
2272 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2273 xfs_trans_ihold(args->trans, args->dp);
2277 * Close out trans and start the next one in the chain.
2279 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2285 #if defined(XFS_ATTR_TRACE)
2287 * Add a trace buffer entry for an attr_list context structure.
2290 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2292 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where, context,
2293 (__psunsigned_t)NULL,
2294 (__psunsigned_t)NULL,
2295 (__psunsigned_t)NULL);
2299 * Add a trace buffer entry for a context structure and a Btree node.
2302 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2303 struct xfs_da_intnode *node)
2305 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where, context,
2306 (__psunsigned_t)be16_to_cpu(node->hdr.count),
2307 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
2308 (__psunsigned_t)be32_to_cpu(node->btree[
2309 be16_to_cpu(node->hdr.count)-1].hashval));
2313 * Add a trace buffer entry for a context structure and a Btree element.
2316 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2317 struct xfs_da_node_entry *btree)
2319 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where, context,
2320 (__psunsigned_t)be32_to_cpu(btree->hashval),
2321 (__psunsigned_t)be32_to_cpu(btree->before),
2322 (__psunsigned_t)NULL);
2326 * Add a trace buffer entry for a context structure and a leaf block.
2329 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2330 struct xfs_attr_leafblock *leaf)
2332 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where, context,
2333 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
2334 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2335 (__psunsigned_t)be32_to_cpu(leaf->entries[
2336 be16_to_cpu(leaf->hdr.count)-1].hashval));
2340 * Add a trace buffer entry for the arguments given to the routine,
2344 xfs_attr_trace_enter(int type, char *where,
2345 struct xfs_attr_list_context *context,
2346 __psunsigned_t a13, __psunsigned_t a14,
2349 ASSERT(xfs_attr_trace_buf);
2350 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2351 (void *)((__psunsigned_t)where),
2352 (void *)((__psunsigned_t)context->dp),
2353 (void *)((__psunsigned_t)context->cursor->hashval),
2354 (void *)((__psunsigned_t)context->cursor->blkno),
2355 (void *)((__psunsigned_t)context->cursor->offset),
2356 (void *)((__psunsigned_t)context->alist),
2357 (void *)((__psunsigned_t)context->bufsize),
2358 (void *)((__psunsigned_t)context->count),
2359 (void *)((__psunsigned_t)context->firstu),
2360 (void *)((__psunsigned_t)
2361 (((context->count > 0) &&
2362 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2363 ? (ATTR_ENTRY(context->alist,
2364 context->count-1)->a_valuelen)
2366 (void *)((__psunsigned_t)context->dupcnt),
2367 (void *)((__psunsigned_t)context->flags),
2368 (void *)a13, (void *)a14, (void *)a15);
2370 #endif /* XFS_ATTR_TRACE */