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
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_alloc.h"
40 #include "xfs_btree.h"
41 #include "xfs_inode_item.h"
44 #include "xfs_attr_leaf.h"
45 #include "xfs_error.h"
46 #include "xfs_quota.h"
47 #include "xfs_trans_space.h"
50 #include "xfs_vnodeops.h"
55 * Provide the external interfaces to manage attribute lists.
58 /*========================================================================
59 * Function prototypes for the kernel.
60 *========================================================================*/
63 * Internal routines when attribute list fits inside the inode.
65 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
68 * Internal routines when attribute list is one block.
70 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
71 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
72 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
73 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
76 * Internal routines when attribute list is more than one block.
78 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
79 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
80 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
81 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
82 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
83 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
86 * Routines to manipulate out-of-line attribute values.
88 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
89 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
91 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
93 #if defined(XFS_ATTR_TRACE)
94 ktrace_t *xfs_attr_trace_buf;
98 xfs_attr_name_to_xname(
99 struct xfs_name *xname,
105 xname->len = strlen(aname);
106 if (xname->len >= MAXNAMELEN)
107 return EFAULT; /* match IRIX behaviour */
114 struct xfs_inode *ip)
116 if (!XFS_IFORK_Q(ip) ||
117 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
118 ip->i_d.di_anextents == 0))
123 /*========================================================================
124 * Overall external interface routines.
125 *========================================================================*/
128 xfs_attr_fetch(xfs_inode_t *ip, struct xfs_name *name,
129 char *value, int *valuelenp, int flags)
134 if (!xfs_inode_hasattr(ip))
138 * Fill in the arg structure for this request.
140 memset((char *)&args, 0, sizeof(args));
141 args.name = name->name;
142 args.namelen = name->len;
144 args.valuelen = *valuelenp;
146 args.hashval = xfs_da_hashname(args.name, args.namelen);
148 args.whichfork = XFS_ATTR_FORK;
151 * Decide on what work routines to call based on the inode size.
153 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
154 error = xfs_attr_shortform_getvalue(&args);
155 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
156 error = xfs_attr_leaf_get(&args);
158 error = xfs_attr_node_get(&args);
162 * Return the number of bytes in the value to the caller.
164 *valuelenp = args.valuelen;
180 struct xfs_name xname;
182 XFS_STATS_INC(xs_attr_get);
184 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
187 error = xfs_attr_name_to_xname(&xname, name);
191 xfs_ilock(ip, XFS_ILOCK_SHARED);
192 error = xfs_attr_fetch(ip, &xname, value, valuelenp, flags);
193 xfs_iunlock(ip, XFS_ILOCK_SHARED);
198 * Calculate how many blocks we need for the new attribute,
202 struct xfs_inode *ip,
207 struct xfs_mount *mp = ip->i_mount;
212 * Determine space new attribute will use, and if it would be
213 * "local" or "remote" (note: local != inline).
215 size = xfs_attr_leaf_newentsize(namelen, valuelen,
216 mp->m_sb.sb_blocksize, local);
218 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
220 if (size > (mp->m_sb.sb_blocksize >> 1)) {
221 /* Double split possible */
226 * Out of line attribute, cannot double split, but
227 * make room for the attribute value itself.
229 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
231 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
238 xfs_attr_set_int(xfs_inode_t *dp, struct xfs_name *name,
239 char *value, int valuelen, int flags)
242 xfs_fsblock_t firstblock;
243 xfs_bmap_free_t flist;
244 int error, err2, committed;
245 xfs_mount_t *mp = dp->i_mount;
246 int rsvd = (flags & ATTR_ROOT) != 0;
250 * Attach the dquots to the inode.
252 error = xfs_qm_dqattach(dp, 0);
257 * If the inode doesn't have an attribute fork, add one.
258 * (inode must not be locked when we call this routine)
260 if (XFS_IFORK_Q(dp) == 0) {
261 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
262 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
264 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
269 * Fill in the arg structure for this request.
271 memset((char *)&args, 0, sizeof(args));
272 args.name = name->name;
273 args.namelen = name->len;
275 args.valuelen = valuelen;
277 args.hashval = xfs_da_hashname(args.name, args.namelen);
279 args.firstblock = &firstblock;
281 args.whichfork = XFS_ATTR_FORK;
282 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
284 /* Size is now blocks for attribute data */
285 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
288 * Start our first transaction of the day.
290 * All future transactions during this code must be "chained" off
291 * this one via the trans_dup() call. All transactions will contain
292 * the inode, and the inode will always be marked with trans_ihold().
293 * Since the inode will be locked in all transactions, we must log
294 * the inode in every transaction to let it float upward through
297 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
300 * Root fork attributes can use reserved data blocks for this
301 * operation if necessary
305 args.trans->t_flags |= XFS_TRANS_RESERVE;
307 if ((error = xfs_trans_reserve(args.trans, args.total,
308 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
309 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
310 xfs_trans_cancel(args.trans, 0);
313 xfs_ilock(dp, XFS_ILOCK_EXCL);
315 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
316 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
317 XFS_QMOPT_RES_REGBLKS);
319 xfs_iunlock(dp, XFS_ILOCK_EXCL);
320 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
324 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
325 xfs_trans_ihold(args.trans, dp);
328 * If the attribute list is non-existent or a shortform list,
329 * upgrade it to a single-leaf-block attribute list.
331 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
332 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
333 (dp->i_d.di_anextents == 0))) {
336 * Build initial attribute list (if required).
338 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
339 xfs_attr_shortform_create(&args);
342 * Try to add the attr to the attribute list in
345 error = xfs_attr_shortform_addname(&args);
346 if (error != ENOSPC) {
348 * Commit the shortform mods, and we're done.
349 * NOTE: this is also the error path (EEXIST, etc).
351 ASSERT(args.trans != NULL);
354 * If this is a synchronous mount, make sure that
355 * the transaction goes to disk before returning
358 if (mp->m_flags & XFS_MOUNT_WSYNC) {
359 xfs_trans_set_sync(args.trans);
361 err2 = xfs_trans_commit(args.trans,
362 XFS_TRANS_RELEASE_LOG_RES);
363 xfs_iunlock(dp, XFS_ILOCK_EXCL);
366 * Hit the inode change time.
368 if (!error && (flags & ATTR_KERNOTIME) == 0) {
369 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
371 return(error == 0 ? err2 : error);
375 * It won't fit in the shortform, transform to a leaf block.
376 * GROT: another possible req'mt for a double-split btree op.
378 xfs_bmap_init(args.flist, args.firstblock);
379 error = xfs_attr_shortform_to_leaf(&args);
381 error = xfs_bmap_finish(&args.trans, args.flist,
387 xfs_bmap_cancel(&flist);
392 * bmap_finish() may have committed the last trans and started
393 * a new one. We need the inode to be in all transactions.
396 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
397 xfs_trans_ihold(args.trans, dp);
401 * Commit the leaf transformation. We'll need another (linked)
402 * transaction to add the new attribute to the leaf.
405 error = xfs_trans_roll(&args.trans, dp);
411 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
412 error = xfs_attr_leaf_addname(&args);
414 error = xfs_attr_node_addname(&args);
421 * If this is a synchronous mount, make sure that the
422 * transaction goes to disk before returning to the user.
424 if (mp->m_flags & XFS_MOUNT_WSYNC) {
425 xfs_trans_set_sync(args.trans);
429 * Commit the last in the sequence of transactions.
431 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
432 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
433 xfs_iunlock(dp, XFS_ILOCK_EXCL);
436 * Hit the inode change time.
438 if (!error && (flags & ATTR_KERNOTIME) == 0) {
439 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
446 xfs_trans_cancel(args.trans,
447 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
448 xfs_iunlock(dp, XFS_ILOCK_EXCL);
461 struct xfs_name xname;
463 XFS_STATS_INC(xs_attr_set);
465 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
468 error = xfs_attr_name_to_xname(&xname, name);
472 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
476 * Generic handler routine to remove a name from an attribute list.
477 * Transitions attribute list from Btree to shortform as necessary.
480 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
483 xfs_fsblock_t firstblock;
484 xfs_bmap_free_t flist;
486 xfs_mount_t *mp = dp->i_mount;
489 * Fill in the arg structure for this request.
491 memset((char *)&args, 0, sizeof(args));
492 args.name = name->name;
493 args.namelen = name->len;
495 args.hashval = xfs_da_hashname(args.name, args.namelen);
497 args.firstblock = &firstblock;
500 args.whichfork = XFS_ATTR_FORK;
503 * Attach the dquots to the inode.
505 error = xfs_qm_dqattach(dp, 0);
510 * Start our first transaction of the day.
512 * All future transactions during this code must be "chained" off
513 * this one via the trans_dup() call. All transactions will contain
514 * the inode, and the inode will always be marked with trans_ihold().
515 * Since the inode will be locked in all transactions, we must log
516 * the inode in every transaction to let it float upward through
519 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
522 * Root fork attributes can use reserved data blocks for this
523 * operation if necessary
526 if (flags & ATTR_ROOT)
527 args.trans->t_flags |= XFS_TRANS_RESERVE;
529 if ((error = xfs_trans_reserve(args.trans,
530 XFS_ATTRRM_SPACE_RES(mp),
531 XFS_ATTRRM_LOG_RES(mp),
532 0, XFS_TRANS_PERM_LOG_RES,
533 XFS_ATTRRM_LOG_COUNT))) {
534 xfs_trans_cancel(args.trans, 0);
538 xfs_ilock(dp, XFS_ILOCK_EXCL);
540 * No need to make quota reservations here. We expect to release some
541 * blocks not allocate in the common case.
543 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
544 xfs_trans_ihold(args.trans, dp);
547 * Decide on what work routines to call based on the inode size.
549 if (!xfs_inode_hasattr(dp)) {
550 error = XFS_ERROR(ENOATTR);
553 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
554 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
555 error = xfs_attr_shortform_remove(&args);
559 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
560 error = xfs_attr_leaf_removename(&args);
562 error = xfs_attr_node_removename(&args);
569 * If this is a synchronous mount, make sure that the
570 * transaction goes to disk before returning to the user.
572 if (mp->m_flags & XFS_MOUNT_WSYNC) {
573 xfs_trans_set_sync(args.trans);
577 * Commit the last in the sequence of transactions.
579 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
580 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
581 xfs_iunlock(dp, XFS_ILOCK_EXCL);
584 * Hit the inode change time.
586 if (!error && (flags & ATTR_KERNOTIME) == 0) {
587 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
594 xfs_trans_cancel(args.trans,
595 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
596 xfs_iunlock(dp, XFS_ILOCK_EXCL);
607 struct xfs_name xname;
609 XFS_STATS_INC(xs_attr_remove);
611 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
614 error = xfs_attr_name_to_xname(&xname, name);
618 xfs_ilock(dp, XFS_ILOCK_SHARED);
619 if (!xfs_inode_hasattr(dp)) {
620 xfs_iunlock(dp, XFS_ILOCK_SHARED);
621 return XFS_ERROR(ENOATTR);
623 xfs_iunlock(dp, XFS_ILOCK_SHARED);
625 return xfs_attr_remove_int(dp, &xname, flags);
629 xfs_attr_list_int(xfs_attr_list_context_t *context)
632 xfs_inode_t *dp = context->dp;
634 XFS_STATS_INC(xs_attr_list);
636 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
639 xfs_ilock(dp, XFS_ILOCK_SHARED);
640 xfs_attr_trace_l_c("syscall start", context);
643 * Decide on what work routines to call based on the inode size.
645 if (!xfs_inode_hasattr(dp)) {
647 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
648 error = xfs_attr_shortform_list(context);
649 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
650 error = xfs_attr_leaf_list(context);
652 error = xfs_attr_node_list(context);
655 xfs_iunlock(dp, XFS_ILOCK_SHARED);
656 xfs_attr_trace_l_c("syscall end", context);
661 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
662 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
663 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
664 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
665 & ~(sizeof(u_int32_t)-1))
668 * Format an attribute and copy it out to the user's buffer.
669 * Take care to check values and protect against them changing later,
670 * we may be reading them directly out of a user buffer.
674 xfs_attr_put_listent(xfs_attr_list_context_t *context, int flags,
675 char *name, int namelen,
676 int valuelen, char *value)
678 struct attrlist *alist = (struct attrlist *)context->alist;
682 ASSERT(!(context->flags & ATTR_KERNOVAL));
683 ASSERT(context->count >= 0);
684 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
685 ASSERT(context->firstu >= sizeof(*alist));
686 ASSERT(context->firstu <= context->bufsize);
689 * Only list entries in the right namespace.
691 if (((context->flags & ATTR_SECURE) == 0) !=
692 ((flags & XFS_ATTR_SECURE) == 0))
694 if (((context->flags & ATTR_ROOT) == 0) !=
695 ((flags & XFS_ATTR_ROOT) == 0))
698 arraytop = sizeof(*alist) +
699 context->count * sizeof(alist->al_offset[0]);
700 context->firstu -= ATTR_ENTSIZE(namelen);
701 if (context->firstu < arraytop) {
702 xfs_attr_trace_l_c("buffer full", context);
704 context->seen_enough = 1;
708 aep = (attrlist_ent_t *)&context->alist[context->firstu];
709 aep->a_valuelen = valuelen;
710 memcpy(aep->a_name, name, namelen);
711 aep->a_name[namelen] = 0;
712 alist->al_offset[context->count++] = context->firstu;
713 alist->al_count = context->count;
714 xfs_attr_trace_l_c("add", context);
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;
734 struct attrlist *alist;
738 * Validate the cursor.
740 if (cursor->pad1 || cursor->pad2)
741 return(XFS_ERROR(EINVAL));
742 if ((cursor->initted == 0) &&
743 (cursor->hashval || cursor->blkno || cursor->offset))
744 return XFS_ERROR(EINVAL);
747 * Check for a properly aligned buffer.
749 if (((long)buffer) & (sizeof(int)-1))
750 return XFS_ERROR(EFAULT);
751 if (flags & ATTR_KERNOVAL)
755 * Initialize the output buffer.
757 memset(&context, 0, sizeof(context));
759 context.cursor = cursor;
761 context.flags = flags;
762 context.alist = buffer;
763 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
764 context.firstu = context.bufsize;
765 context.put_listent = xfs_attr_put_listent;
767 alist = (struct attrlist *)context.alist;
770 alist->al_offset[0] = context.bufsize;
772 error = xfs_attr_list_int(&context);
778 xfs_attr_inactive(xfs_inode_t *dp)
785 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
787 xfs_ilock(dp, XFS_ILOCK_SHARED);
788 if (!xfs_inode_hasattr(dp) ||
789 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
790 xfs_iunlock(dp, XFS_ILOCK_SHARED);
793 xfs_iunlock(dp, XFS_ILOCK_SHARED);
796 * Start our first transaction of the day.
798 * All future transactions during this code must be "chained" off
799 * this one via the trans_dup() call. All transactions will contain
800 * the inode, and the inode will always be marked with trans_ihold().
801 * Since the inode will be locked in all transactions, we must log
802 * the inode in every transaction to let it float upward through
805 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
806 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
807 XFS_TRANS_PERM_LOG_RES,
808 XFS_ATTRINVAL_LOG_COUNT))) {
809 xfs_trans_cancel(trans, 0);
812 xfs_ilock(dp, XFS_ILOCK_EXCL);
815 * No need to make quota reservations here. We expect to release some
816 * blocks, not allocate, in the common case.
818 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
819 xfs_trans_ihold(trans, dp);
822 * Decide on what work routines to call based on the inode size.
824 if (!xfs_inode_hasattr(dp) ||
825 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
829 error = xfs_attr_root_inactive(&trans, dp);
833 * signal synchronous inactive transactions unless this
834 * is a synchronous mount filesystem in which case we
835 * know that we're here because we've been called out of
836 * xfs_inactive which means that the last reference is gone
837 * and the unlink transaction has already hit the disk so
838 * async inactive transactions are safe.
840 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
841 (!(mp->m_flags & XFS_MOUNT_WSYNC)
846 * Commit the last in the sequence of transactions.
848 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
849 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
850 xfs_iunlock(dp, XFS_ILOCK_EXCL);
855 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
856 xfs_iunlock(dp, XFS_ILOCK_EXCL);
862 /*========================================================================
863 * External routines when attribute list is inside the inode
864 *========================================================================*/
867 * Add a name to the shortform attribute list structure
868 * This is the external routine.
871 xfs_attr_shortform_addname(xfs_da_args_t *args)
873 int newsize, forkoff, retval;
875 retval = xfs_attr_shortform_lookup(args);
876 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
878 } else if (retval == EEXIST) {
879 if (args->flags & ATTR_CREATE)
881 retval = xfs_attr_shortform_remove(args);
885 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
886 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
887 return(XFS_ERROR(ENOSPC));
889 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
890 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
892 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
894 return(XFS_ERROR(ENOSPC));
896 xfs_attr_shortform_add(args, forkoff);
901 /*========================================================================
902 * External routines when attribute list is one block
903 *========================================================================*/
906 * Add a name to the leaf attribute list structure
908 * This leaf block cannot have a "remote" value, we only call this routine
909 * if bmap_one_block() says there is only one block (ie: no remote blks).
912 xfs_attr_leaf_addname(xfs_da_args_t *args)
916 int retval, error, committed, forkoff;
919 * Read the (only) block in the attribute list in.
923 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
930 * Look up the given attribute in the leaf block. Figure out if
931 * the given flags produce an error or call for an atomic rename.
933 retval = xfs_attr_leaf_lookup_int(bp, args);
934 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
935 xfs_da_brelse(args->trans, bp);
937 } else if (retval == EEXIST) {
938 if (args->flags & ATTR_CREATE) { /* pure create op */
939 xfs_da_brelse(args->trans, bp);
942 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
943 args->blkno2 = args->blkno; /* set 2nd entry info*/
944 args->index2 = args->index;
945 args->rmtblkno2 = args->rmtblkno;
946 args->rmtblkcnt2 = args->rmtblkcnt;
950 * Add the attribute to the leaf block, transitioning to a Btree
953 retval = xfs_attr_leaf_add(bp, args);
955 if (retval == ENOSPC) {
957 * Promote the attribute list to the Btree format, then
958 * Commit that transaction so that the node_addname() call
959 * can manage its own transactions.
961 xfs_bmap_init(args->flist, args->firstblock);
962 error = xfs_attr_leaf_to_node(args);
964 error = xfs_bmap_finish(&args->trans, args->flist,
970 xfs_bmap_cancel(args->flist);
975 * bmap_finish() may have committed the last trans and started
976 * a new one. We need the inode to be in all transactions.
979 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
980 xfs_trans_ihold(args->trans, dp);
984 * Commit the current trans (including the inode) and start
987 error = xfs_trans_roll(&args->trans, dp);
992 * Fob the whole rest of the problem off on the Btree code.
994 error = xfs_attr_node_addname(args);
999 * Commit the transaction that added the attr name so that
1000 * later routines can manage their own transactions.
1002 error = xfs_trans_roll(&args->trans, dp);
1007 * If there was an out-of-line value, allocate the blocks we
1008 * identified for its storage and copy the value. This is done
1009 * after we create the attribute so that we don't overflow the
1010 * maximum size of a transaction and/or hit a deadlock.
1012 if (args->rmtblkno > 0) {
1013 error = xfs_attr_rmtval_set(args);
1019 * If this is an atomic rename operation, we must "flip" the
1020 * incomplete flags on the "new" and "old" attribute/value pairs
1021 * so that one disappears and one appears atomically. Then we
1022 * must remove the "old" attribute/value pair.
1024 if (args->op_flags & XFS_DA_OP_RENAME) {
1026 * In a separate transaction, set the incomplete flag on the
1027 * "old" attr and clear the incomplete flag on the "new" attr.
1029 error = xfs_attr_leaf_flipflags(args);
1034 * Dismantle the "old" attribute/value pair by removing
1035 * a "remote" value (if it exists).
1037 args->index = args->index2;
1038 args->blkno = args->blkno2;
1039 args->rmtblkno = args->rmtblkno2;
1040 args->rmtblkcnt = args->rmtblkcnt2;
1041 if (args->rmtblkno) {
1042 error = xfs_attr_rmtval_remove(args);
1048 * Read in the block containing the "old" attr, then
1049 * remove the "old" attr from that block (neat, huh!)
1051 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1052 &bp, XFS_ATTR_FORK);
1056 (void)xfs_attr_leaf_remove(bp, args);
1059 * If the result is small enough, shrink it all into the inode.
1061 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1062 xfs_bmap_init(args->flist, args->firstblock);
1063 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1064 /* bp is gone due to xfs_da_shrink_inode */
1066 error = xfs_bmap_finish(&args->trans,
1073 xfs_bmap_cancel(args->flist);
1078 * bmap_finish() may have committed the last trans
1079 * and started a new one. We need the inode to be
1080 * in all transactions.
1083 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1084 xfs_trans_ihold(args->trans, dp);
1087 xfs_da_buf_done(bp);
1090 * Commit the remove and start the next trans in series.
1092 error = xfs_trans_roll(&args->trans, dp);
1094 } else if (args->rmtblkno > 0) {
1096 * Added a "remote" value, just clear the incomplete flag.
1098 error = xfs_attr_leaf_clearflag(args);
1104 * Remove a name from the leaf attribute list structure
1106 * This leaf block cannot have a "remote" value, we only call this routine
1107 * if bmap_one_block() says there is only one block (ie: no remote blks).
1110 xfs_attr_leaf_removename(xfs_da_args_t *args)
1114 int error, committed, forkoff;
1117 * Remove the attribute.
1121 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1128 error = xfs_attr_leaf_lookup_int(bp, args);
1129 if (error == ENOATTR) {
1130 xfs_da_brelse(args->trans, bp);
1134 (void)xfs_attr_leaf_remove(bp, args);
1137 * If the result is small enough, shrink it all into the inode.
1139 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1140 xfs_bmap_init(args->flist, args->firstblock);
1141 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1142 /* bp is gone due to xfs_da_shrink_inode */
1144 error = xfs_bmap_finish(&args->trans, args->flist,
1150 xfs_bmap_cancel(args->flist);
1155 * bmap_finish() may have committed the last trans and started
1156 * a new one. We need the inode to be in all transactions.
1159 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1160 xfs_trans_ihold(args->trans, dp);
1163 xfs_da_buf_done(bp);
1168 * Look up a name in a leaf attribute list structure.
1170 * This leaf block cannot have a "remote" value, we only call this routine
1171 * if bmap_one_block() says there is only one block (ie: no remote blks).
1174 xfs_attr_leaf_get(xfs_da_args_t *args)
1180 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1186 error = xfs_attr_leaf_lookup_int(bp, args);
1187 if (error != EEXIST) {
1188 xfs_da_brelse(args->trans, bp);
1191 error = xfs_attr_leaf_getvalue(bp, args);
1192 xfs_da_brelse(args->trans, bp);
1193 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1194 error = xfs_attr_rmtval_get(args);
1200 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1203 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1205 xfs_attr_leafblock_t *leaf;
1209 context->cursor->blkno = 0;
1210 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1212 return XFS_ERROR(error);
1215 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1216 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1217 context->dp->i_mount, leaf);
1218 xfs_da_brelse(NULL, bp);
1219 return XFS_ERROR(EFSCORRUPTED);
1222 error = xfs_attr_leaf_list_int(bp, context);
1223 xfs_da_brelse(NULL, bp);
1224 return XFS_ERROR(error);
1228 /*========================================================================
1229 * External routines when attribute list size > XFS_LBSIZE(mp).
1230 *========================================================================*/
1233 * Add a name to a Btree-format attribute list.
1235 * This will involve walking down the Btree, and may involve splitting
1236 * leaf nodes and even splitting intermediate nodes up to and including
1237 * the root node (a special case of an intermediate node).
1239 * "Remote" attribute values confuse the issue and atomic rename operations
1240 * add a whole extra layer of confusion on top of that.
1243 xfs_attr_node_addname(xfs_da_args_t *args)
1245 xfs_da_state_t *state;
1246 xfs_da_state_blk_t *blk;
1249 int committed, retval, error;
1252 * Fill in bucket of arguments/results/context to carry around.
1257 state = xfs_da_state_alloc();
1260 state->blocksize = state->mp->m_sb.sb_blocksize;
1261 state->node_ents = state->mp->m_attr_node_ents;
1264 * Search to see if name already exists, and get back a pointer
1265 * to where it should go.
1267 error = xfs_da_node_lookup_int(state, &retval);
1270 blk = &state->path.blk[ state->path.active-1 ];
1271 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1272 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1274 } else if (retval == EEXIST) {
1275 if (args->flags & ATTR_CREATE)
1277 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1278 args->blkno2 = args->blkno; /* set 2nd entry info*/
1279 args->index2 = args->index;
1280 args->rmtblkno2 = args->rmtblkno;
1281 args->rmtblkcnt2 = args->rmtblkcnt;
1283 args->rmtblkcnt = 0;
1286 retval = xfs_attr_leaf_add(blk->bp, state->args);
1287 if (retval == ENOSPC) {
1288 if (state->path.active == 1) {
1290 * Its really a single leaf node, but it had
1291 * out-of-line values so it looked like it *might*
1292 * have been a b-tree.
1294 xfs_da_state_free(state);
1295 xfs_bmap_init(args->flist, args->firstblock);
1296 error = xfs_attr_leaf_to_node(args);
1298 error = xfs_bmap_finish(&args->trans,
1305 xfs_bmap_cancel(args->flist);
1310 * bmap_finish() may have committed the last trans
1311 * and started a new one. We need the inode to be
1312 * in all transactions.
1315 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1316 xfs_trans_ihold(args->trans, dp);
1320 * Commit the node conversion and start the next
1321 * trans in the chain.
1323 error = xfs_trans_roll(&args->trans, dp);
1331 * Split as many Btree elements as required.
1332 * This code tracks the new and old attr's location
1333 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1334 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1336 xfs_bmap_init(args->flist, args->firstblock);
1337 error = xfs_da_split(state);
1339 error = xfs_bmap_finish(&args->trans, args->flist,
1345 xfs_bmap_cancel(args->flist);
1350 * bmap_finish() may have committed the last trans and started
1351 * a new one. We need the inode to be in all transactions.
1354 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1355 xfs_trans_ihold(args->trans, dp);
1359 * Addition succeeded, update Btree hashvals.
1361 xfs_da_fixhashpath(state, &state->path);
1365 * Kill the state structure, we're done with it and need to
1366 * allow the buffers to come back later.
1368 xfs_da_state_free(state);
1372 * Commit the leaf addition or btree split and start the next
1373 * trans in the chain.
1375 error = xfs_trans_roll(&args->trans, dp);
1380 * If there was an out-of-line value, allocate the blocks we
1381 * identified for its storage and copy the value. This is done
1382 * after we create the attribute so that we don't overflow the
1383 * maximum size of a transaction and/or hit a deadlock.
1385 if (args->rmtblkno > 0) {
1386 error = xfs_attr_rmtval_set(args);
1392 * If this is an atomic rename operation, we must "flip" the
1393 * incomplete flags on the "new" and "old" attribute/value pairs
1394 * so that one disappears and one appears atomically. Then we
1395 * must remove the "old" attribute/value pair.
1397 if (args->op_flags & XFS_DA_OP_RENAME) {
1399 * In a separate transaction, set the incomplete flag on the
1400 * "old" attr and clear the incomplete flag on the "new" attr.
1402 error = xfs_attr_leaf_flipflags(args);
1407 * Dismantle the "old" attribute/value pair by removing
1408 * a "remote" value (if it exists).
1410 args->index = args->index2;
1411 args->blkno = args->blkno2;
1412 args->rmtblkno = args->rmtblkno2;
1413 args->rmtblkcnt = args->rmtblkcnt2;
1414 if (args->rmtblkno) {
1415 error = xfs_attr_rmtval_remove(args);
1421 * Re-find the "old" attribute entry after any split ops.
1422 * The INCOMPLETE flag means that we will find the "old"
1423 * attr, not the "new" one.
1425 args->flags |= XFS_ATTR_INCOMPLETE;
1426 state = xfs_da_state_alloc();
1429 state->blocksize = state->mp->m_sb.sb_blocksize;
1430 state->node_ents = state->mp->m_attr_node_ents;
1432 error = xfs_da_node_lookup_int(state, &retval);
1437 * Remove the name and update the hashvals in the tree.
1439 blk = &state->path.blk[ state->path.active-1 ];
1440 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1441 error = xfs_attr_leaf_remove(blk->bp, args);
1442 xfs_da_fixhashpath(state, &state->path);
1445 * Check to see if the tree needs to be collapsed.
1447 if (retval && (state->path.active > 1)) {
1448 xfs_bmap_init(args->flist, args->firstblock);
1449 error = xfs_da_join(state);
1451 error = xfs_bmap_finish(&args->trans,
1458 xfs_bmap_cancel(args->flist);
1463 * bmap_finish() may have committed the last trans
1464 * and started a new one. We need the inode to be
1465 * in all transactions.
1468 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1469 xfs_trans_ihold(args->trans, dp);
1474 * Commit and start the next trans in the chain.
1476 error = xfs_trans_roll(&args->trans, dp);
1480 } else if (args->rmtblkno > 0) {
1482 * Added a "remote" value, just clear the incomplete flag.
1484 error = xfs_attr_leaf_clearflag(args);
1492 xfs_da_state_free(state);
1499 * Remove a name from a B-tree attribute list.
1501 * This will involve walking down the Btree, and may involve joining
1502 * leaf nodes and even joining intermediate nodes up to and including
1503 * the root node (a special case of an intermediate node).
1506 xfs_attr_node_removename(xfs_da_args_t *args)
1508 xfs_da_state_t *state;
1509 xfs_da_state_blk_t *blk;
1512 int retval, error, committed, forkoff;
1515 * Tie a string around our finger to remind us where we are.
1518 state = xfs_da_state_alloc();
1520 state->mp = dp->i_mount;
1521 state->blocksize = state->mp->m_sb.sb_blocksize;
1522 state->node_ents = state->mp->m_attr_node_ents;
1525 * Search to see if name exists, and get back a pointer to it.
1527 error = xfs_da_node_lookup_int(state, &retval);
1528 if (error || (retval != EEXIST)) {
1535 * If there is an out-of-line value, de-allocate the blocks.
1536 * This is done before we remove the attribute so that we don't
1537 * overflow the maximum size of a transaction and/or hit a deadlock.
1539 blk = &state->path.blk[ state->path.active-1 ];
1540 ASSERT(blk->bp != NULL);
1541 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1542 if (args->rmtblkno > 0) {
1544 * Fill in disk block numbers in the state structure
1545 * so that we can get the buffers back after we commit
1546 * several transactions in the following calls.
1548 error = xfs_attr_fillstate(state);
1553 * Mark the attribute as INCOMPLETE, then bunmapi() the
1556 error = xfs_attr_leaf_setflag(args);
1559 error = xfs_attr_rmtval_remove(args);
1564 * Refill the state structure with buffers, the prior calls
1565 * released our buffers.
1567 error = xfs_attr_refillstate(state);
1573 * Remove the name and update the hashvals in the tree.
1575 blk = &state->path.blk[ state->path.active-1 ];
1576 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1577 retval = xfs_attr_leaf_remove(blk->bp, args);
1578 xfs_da_fixhashpath(state, &state->path);
1581 * Check to see if the tree needs to be collapsed.
1583 if (retval && (state->path.active > 1)) {
1584 xfs_bmap_init(args->flist, args->firstblock);
1585 error = xfs_da_join(state);
1587 error = xfs_bmap_finish(&args->trans, args->flist,
1593 xfs_bmap_cancel(args->flist);
1598 * bmap_finish() may have committed the last trans and started
1599 * a new one. We need the inode to be in all transactions.
1602 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1603 xfs_trans_ihold(args->trans, dp);
1607 * Commit the Btree join operation and start a new trans.
1609 error = xfs_trans_roll(&args->trans, dp);
1615 * If the result is small enough, push it all into the inode.
1617 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1619 * Have to get rid of the copy of this dabuf in the state.
1621 ASSERT(state->path.active == 1);
1622 ASSERT(state->path.blk[0].bp);
1623 xfs_da_buf_done(state->path.blk[0].bp);
1624 state->path.blk[0].bp = NULL;
1626 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1630 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1631 bp->data)->hdr.info.magic)
1632 == XFS_ATTR_LEAF_MAGIC);
1634 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1635 xfs_bmap_init(args->flist, args->firstblock);
1636 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1637 /* bp is gone due to xfs_da_shrink_inode */
1639 error = xfs_bmap_finish(&args->trans,
1646 xfs_bmap_cancel(args->flist);
1651 * bmap_finish() may have committed the last trans
1652 * and started a new one. We need the inode to be
1653 * in all transactions.
1656 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1657 xfs_trans_ihold(args->trans, dp);
1660 xfs_da_brelse(args->trans, bp);
1665 xfs_da_state_free(state);
1670 * Fill in the disk block numbers in the state structure for the buffers
1671 * that are attached to the state structure.
1672 * This is done so that we can quickly reattach ourselves to those buffers
1673 * after some set of transaction commits have released these buffers.
1676 xfs_attr_fillstate(xfs_da_state_t *state)
1678 xfs_da_state_path_t *path;
1679 xfs_da_state_blk_t *blk;
1683 * Roll down the "path" in the state structure, storing the on-disk
1684 * block number for those buffers in the "path".
1686 path = &state->path;
1687 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1688 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1690 blk->disk_blkno = xfs_da_blkno(blk->bp);
1691 xfs_da_buf_done(blk->bp);
1694 blk->disk_blkno = 0;
1699 * Roll down the "altpath" in the state structure, storing the on-disk
1700 * block number for those buffers in the "altpath".
1702 path = &state->altpath;
1703 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1704 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1706 blk->disk_blkno = xfs_da_blkno(blk->bp);
1707 xfs_da_buf_done(blk->bp);
1710 blk->disk_blkno = 0;
1718 * Reattach the buffers to the state structure based on the disk block
1719 * numbers stored in the state structure.
1720 * This is done after some set of transaction commits have released those
1721 * buffers from our grip.
1724 xfs_attr_refillstate(xfs_da_state_t *state)
1726 xfs_da_state_path_t *path;
1727 xfs_da_state_blk_t *blk;
1731 * Roll down the "path" in the state structure, storing the on-disk
1732 * block number for those buffers in the "path".
1734 path = &state->path;
1735 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1736 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1737 if (blk->disk_blkno) {
1738 error = xfs_da_read_buf(state->args->trans,
1740 blk->blkno, blk->disk_blkno,
1741 &blk->bp, XFS_ATTR_FORK);
1750 * Roll down the "altpath" in the state structure, storing the on-disk
1751 * block number for those buffers in the "altpath".
1753 path = &state->altpath;
1754 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1755 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1756 if (blk->disk_blkno) {
1757 error = xfs_da_read_buf(state->args->trans,
1759 blk->blkno, blk->disk_blkno,
1760 &blk->bp, XFS_ATTR_FORK);
1772 * Look up a filename in a node attribute list.
1774 * This routine gets called for any attribute fork that has more than one
1775 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1776 * "remote" values taking up more blocks.
1779 xfs_attr_node_get(xfs_da_args_t *args)
1781 xfs_da_state_t *state;
1782 xfs_da_state_blk_t *blk;
1786 state = xfs_da_state_alloc();
1788 state->mp = args->dp->i_mount;
1789 state->blocksize = state->mp->m_sb.sb_blocksize;
1790 state->node_ents = state->mp->m_attr_node_ents;
1793 * Search to see if name exists, and get back a pointer to it.
1795 error = xfs_da_node_lookup_int(state, &retval);
1798 } else if (retval == EEXIST) {
1799 blk = &state->path.blk[ state->path.active-1 ];
1800 ASSERT(blk->bp != NULL);
1801 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1804 * Get the value, local or "remote"
1806 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1807 if (!retval && (args->rmtblkno > 0)
1808 && !(args->flags & ATTR_KERNOVAL)) {
1809 retval = xfs_attr_rmtval_get(args);
1814 * If not in a transaction, we have to release all the buffers.
1816 for (i = 0; i < state->path.active; i++) {
1817 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1818 state->path.blk[i].bp = NULL;
1821 xfs_da_state_free(state);
1825 STATIC int /* error */
1826 xfs_attr_node_list(xfs_attr_list_context_t *context)
1828 attrlist_cursor_kern_t *cursor;
1829 xfs_attr_leafblock_t *leaf;
1830 xfs_da_intnode_t *node;
1831 xfs_da_node_entry_t *btree;
1835 cursor = context->cursor;
1836 cursor->initted = 1;
1839 * Do all sorts of validation on the passed-in cursor structure.
1840 * If anything is amiss, ignore the cursor and look up the hashval
1841 * starting from the btree root.
1844 if (cursor->blkno > 0) {
1845 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1846 &bp, XFS_ATTR_FORK);
1847 if ((error != 0) && (error != EFSCORRUPTED))
1851 switch (be16_to_cpu(node->hdr.info.magic)) {
1852 case XFS_DA_NODE_MAGIC:
1853 xfs_attr_trace_l_cn("wrong blk", context, node);
1854 xfs_da_brelse(NULL, bp);
1857 case XFS_ATTR_LEAF_MAGIC:
1859 if (cursor->hashval > be32_to_cpu(leaf->entries[
1860 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1861 xfs_attr_trace_l_cl("wrong blk",
1863 xfs_da_brelse(NULL, bp);
1865 } else if (cursor->hashval <=
1866 be32_to_cpu(leaf->entries[0].hashval)) {
1867 xfs_attr_trace_l_cl("maybe wrong blk",
1869 xfs_da_brelse(NULL, bp);
1874 xfs_attr_trace_l_c("wrong blk - ??", context);
1875 xfs_da_brelse(NULL, bp);
1882 * We did not find what we expected given the cursor's contents,
1883 * so we start from the top and work down based on the hash value.
1884 * Note that start of node block is same as start of leaf block.
1889 error = xfs_da_read_buf(NULL, context->dp,
1890 cursor->blkno, -1, &bp,
1894 if (unlikely(bp == NULL)) {
1895 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1897 context->dp->i_mount);
1898 return(XFS_ERROR(EFSCORRUPTED));
1901 if (be16_to_cpu(node->hdr.info.magic)
1902 == XFS_ATTR_LEAF_MAGIC)
1904 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1905 != XFS_DA_NODE_MAGIC)) {
1906 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1908 context->dp->i_mount,
1910 xfs_da_brelse(NULL, bp);
1911 return(XFS_ERROR(EFSCORRUPTED));
1913 btree = node->btree;
1914 for (i = 0; i < be16_to_cpu(node->hdr.count);
1917 <= be32_to_cpu(btree->hashval)) {
1918 cursor->blkno = be32_to_cpu(btree->before);
1919 xfs_attr_trace_l_cb("descending",
1924 if (i == be16_to_cpu(node->hdr.count)) {
1925 xfs_da_brelse(NULL, bp);
1928 xfs_da_brelse(NULL, bp);
1934 * Roll upward through the blocks, processing each leaf block in
1935 * order. As long as there is space in the result buffer, keep
1936 * adding the information.
1940 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1941 != XFS_ATTR_LEAF_MAGIC)) {
1942 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1944 context->dp->i_mount, leaf);
1945 xfs_da_brelse(NULL, bp);
1946 return(XFS_ERROR(EFSCORRUPTED));
1948 error = xfs_attr_leaf_list_int(bp, context);
1950 xfs_da_brelse(NULL, bp);
1953 if (context->seen_enough || leaf->hdr.info.forw == 0)
1955 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1956 xfs_da_brelse(NULL, bp);
1957 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1958 &bp, XFS_ATTR_FORK);
1961 if (unlikely((bp == NULL))) {
1962 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1964 context->dp->i_mount);
1965 return(XFS_ERROR(EFSCORRUPTED));
1968 xfs_da_brelse(NULL, bp);
1973 /*========================================================================
1974 * External routines for manipulating out-of-line attribute values.
1975 *========================================================================*/
1978 * Read the value associated with an attribute from the out-of-line buffer
1979 * that we stored it in.
1982 xfs_attr_rmtval_get(xfs_da_args_t *args)
1984 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1989 int nmap, error, tmp, valuelen, blkcnt, i;
1992 ASSERT(!(args->flags & ATTR_KERNOVAL));
1994 mp = args->dp->i_mount;
1996 valuelen = args->valuelen;
1997 lblkno = args->rmtblkno;
1998 while (valuelen > 0) {
1999 nmap = ATTR_RMTVALUE_MAPSIZE;
2000 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2002 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2003 NULL, 0, map, &nmap, NULL, NULL);
2008 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2009 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2010 (map[i].br_startblock != HOLESTARTBLOCK));
2011 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2012 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2013 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2014 blkcnt, XFS_BUF_LOCK, &bp);
2018 tmp = (valuelen < XFS_BUF_SIZE(bp))
2019 ? valuelen : XFS_BUF_SIZE(bp);
2020 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2025 lblkno += map[i].br_blockcount;
2028 ASSERT(valuelen == 0);
2033 * Write the value associated with an attribute into the out-of-line buffer
2034 * that we have defined for it.
2037 xfs_attr_rmtval_set(xfs_da_args_t *args)
2040 xfs_fileoff_t lfileoff;
2042 xfs_bmbt_irec_t map;
2047 int blkcnt, valuelen, nmap, error, tmp, committed;
2054 * Find a "hole" in the attribute address space large enough for
2055 * us to drop the new attribute's value into.
2057 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2059 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2064 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2065 args->rmtblkcnt = blkcnt;
2068 * Roll through the "value", allocating blocks on disk as required.
2070 while (blkcnt > 0) {
2072 * Allocate a single extent, up to the size of the value.
2074 xfs_bmap_init(args->flist, args->firstblock);
2076 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2078 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2080 args->firstblock, args->total, &map, &nmap,
2083 error = xfs_bmap_finish(&args->trans, args->flist,
2089 xfs_bmap_cancel(args->flist);
2094 * bmap_finish() may have committed the last trans and started
2095 * a new one. We need the inode to be in all transactions.
2098 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2099 xfs_trans_ihold(args->trans, dp);
2103 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2104 (map.br_startblock != HOLESTARTBLOCK));
2105 lblkno += map.br_blockcount;
2106 blkcnt -= map.br_blockcount;
2109 * Start the next trans in the chain.
2111 error = xfs_trans_roll(&args->trans, dp);
2117 * Roll through the "value", copying the attribute value to the
2118 * already-allocated blocks. Blocks are written synchronously
2119 * so that we can know they are all on disk before we turn off
2120 * the INCOMPLETE flag.
2122 lblkno = args->rmtblkno;
2123 valuelen = args->valuelen;
2124 while (valuelen > 0) {
2126 * Try to remember where we decided to put the value.
2128 xfs_bmap_init(args->flist, args->firstblock);
2130 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2132 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2133 args->firstblock, 0, &map, &nmap,
2139 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2140 (map.br_startblock != HOLESTARTBLOCK));
2142 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2143 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2145 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2146 blkcnt, XFS_BUF_LOCK);
2148 ASSERT(!XFS_BUF_GETERROR(bp));
2150 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2152 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2153 if (tmp < XFS_BUF_SIZE(bp))
2154 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2155 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2161 lblkno += map.br_blockcount;
2163 ASSERT(valuelen == 0);
2168 * Remove the value associated with an attribute by deleting the
2169 * out-of-line buffer that it is stored on.
2172 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2175 xfs_bmbt_irec_t map;
2179 int valuelen, blkcnt, nmap, error, done, committed;
2181 mp = args->dp->i_mount;
2184 * Roll through the "value", invalidating the attribute value's
2187 lblkno = args->rmtblkno;
2188 valuelen = args->rmtblkcnt;
2189 while (valuelen > 0) {
2191 * Try to remember where we decided to put the value.
2193 xfs_bmap_init(args->flist, args->firstblock);
2195 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2197 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2198 args->firstblock, 0, &map, &nmap,
2204 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2205 (map.br_startblock != HOLESTARTBLOCK));
2207 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2208 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2211 * If the "remote" value is in the cache, remove it.
2213 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2214 XFS_INCORE_TRYLOCK);
2217 XFS_BUF_UNDELAYWRITE(bp);
2222 valuelen -= map.br_blockcount;
2224 lblkno += map.br_blockcount;
2228 * Keep de-allocating extents until the remote-value region is gone.
2230 lblkno = args->rmtblkno;
2231 blkcnt = args->rmtblkcnt;
2234 xfs_bmap_init(args->flist, args->firstblock);
2235 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2236 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2237 1, args->firstblock, args->flist,
2240 error = xfs_bmap_finish(&args->trans, args->flist,
2246 xfs_bmap_cancel(args->flist);
2251 * bmap_finish() may have committed the last trans and started
2252 * a new one. We need the inode to be in all transactions.
2255 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2256 xfs_trans_ihold(args->trans, args->dp);
2260 * Close out trans and start the next one in the chain.
2262 error = xfs_trans_roll(&args->trans, args->dp);
2269 #if defined(XFS_ATTR_TRACE)
2271 * Add a trace buffer entry for an attr_list context structure.
2274 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2276 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where, context,
2277 (__psunsigned_t)NULL,
2278 (__psunsigned_t)NULL,
2279 (__psunsigned_t)NULL);
2283 * Add a trace buffer entry for a context structure and a Btree node.
2286 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2287 struct xfs_da_intnode *node)
2289 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where, context,
2290 (__psunsigned_t)be16_to_cpu(node->hdr.count),
2291 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
2292 (__psunsigned_t)be32_to_cpu(node->btree[
2293 be16_to_cpu(node->hdr.count)-1].hashval));
2297 * Add a trace buffer entry for a context structure and a Btree element.
2300 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2301 struct xfs_da_node_entry *btree)
2303 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where, context,
2304 (__psunsigned_t)be32_to_cpu(btree->hashval),
2305 (__psunsigned_t)be32_to_cpu(btree->before),
2306 (__psunsigned_t)NULL);
2310 * Add a trace buffer entry for a context structure and a leaf block.
2313 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2314 struct xfs_attr_leafblock *leaf)
2316 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where, context,
2317 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
2318 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2319 (__psunsigned_t)be32_to_cpu(leaf->entries[
2320 be16_to_cpu(leaf->hdr.count)-1].hashval));
2324 * Add a trace buffer entry for the arguments given to the routine,
2328 xfs_attr_trace_enter(int type, char *where,
2329 struct xfs_attr_list_context *context,
2330 __psunsigned_t a13, __psunsigned_t a14,
2333 ASSERT(xfs_attr_trace_buf);
2334 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2335 (void *)((__psunsigned_t)where),
2336 (void *)((__psunsigned_t)context->dp),
2337 (void *)((__psunsigned_t)context->cursor->hashval),
2338 (void *)((__psunsigned_t)context->cursor->blkno),
2339 (void *)((__psunsigned_t)context->cursor->offset),
2340 (void *)((__psunsigned_t)context->alist),
2341 (void *)((__psunsigned_t)context->bufsize),
2342 (void *)((__psunsigned_t)context->count),
2343 (void *)((__psunsigned_t)context->firstu),
2345 (void *)((__psunsigned_t)context->dupcnt),
2346 (void *)((__psunsigned_t)context->flags),
2347 (void *)a13, (void *)a14, (void *)a15);
2349 #endif /* XFS_ATTR_TRACE */