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"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_alloc.h"
35 #include "xfs_inode_item.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_error.h"
40 #include "xfs_quota.h"
41 #include "xfs_trans_space.h"
43 #include "xfs_vnodeops.h"
44 #include "xfs_trace.h"
49 * Provide the external interfaces to manage attribute lists.
52 /*========================================================================
53 * Function prototypes for the kernel.
54 *========================================================================*/
57 * Internal routines when attribute list fits inside the inode.
59 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
62 * Internal routines when attribute list is one block.
64 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
65 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
66 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
67 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
70 * Internal routines when attribute list is more than one block.
72 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
73 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
74 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
75 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
76 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
77 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
80 * Routines to manipulate out-of-line attribute values.
82 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
83 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
85 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
88 xfs_attr_name_to_xname(
89 struct xfs_name *xname,
90 const unsigned char *aname)
95 xname->len = strlen((char *)aname);
96 if (xname->len >= MAXNAMELEN)
97 return EFAULT; /* match IRIX behaviour */
104 struct xfs_inode *ip)
106 if (!XFS_IFORK_Q(ip) ||
107 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
108 ip->i_d.di_anextents == 0))
113 /*========================================================================
114 * Overall external interface routines.
115 *========================================================================*/
119 struct xfs_inode *ip,
120 struct xfs_name *name,
121 unsigned char *value,
128 if (!xfs_inode_hasattr(ip))
132 * Fill in the arg structure for this request.
134 memset((char *)&args, 0, sizeof(args));
135 args.name = name->name;
136 args.namelen = name->len;
138 args.valuelen = *valuelenp;
140 args.hashval = xfs_da_hashname(args.name, args.namelen);
142 args.whichfork = XFS_ATTR_FORK;
145 * Decide on what work routines to call based on the inode size.
147 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
148 error = xfs_attr_shortform_getvalue(&args);
149 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
150 error = xfs_attr_leaf_get(&args);
152 error = xfs_attr_node_get(&args);
156 * Return the number of bytes in the value to the caller.
158 *valuelenp = args.valuelen;
168 const unsigned char *name,
169 unsigned char *value,
174 struct xfs_name xname;
176 XFS_STATS_INC(xs_attr_get);
178 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
181 error = xfs_attr_name_to_xname(&xname, name);
185 xfs_ilock(ip, XFS_ILOCK_SHARED);
186 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
187 xfs_iunlock(ip, XFS_ILOCK_SHARED);
192 * Calculate how many blocks we need for the new attribute,
196 struct xfs_inode *ip,
201 struct xfs_mount *mp = ip->i_mount;
206 * Determine space new attribute will use, and if it would be
207 * "local" or "remote" (note: local != inline).
209 size = xfs_attr_leaf_newentsize(namelen, valuelen,
210 mp->m_sb.sb_blocksize, local);
212 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
214 if (size > (mp->m_sb.sb_blocksize >> 1)) {
215 /* Double split possible */
220 * Out of line attribute, cannot double split, but
221 * make room for the attribute value itself.
223 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
225 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
233 struct xfs_inode *dp,
234 struct xfs_name *name,
235 unsigned char *value,
240 xfs_fsblock_t firstblock;
241 xfs_bmap_free_t flist;
242 int error, err2, committed;
243 xfs_mount_t *mp = dp->i_mount;
244 int rsvd = (flags & ATTR_ROOT) != 0;
248 * Attach the dquots to the inode.
250 error = xfs_qm_dqattach(dp, 0);
255 * If the inode doesn't have an attribute fork, add one.
256 * (inode must not be locked when we call this routine)
258 if (XFS_IFORK_Q(dp) == 0) {
259 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
260 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
262 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
267 * Fill in the arg structure for this request.
269 memset((char *)&args, 0, sizeof(args));
270 args.name = name->name;
271 args.namelen = name->len;
273 args.valuelen = valuelen;
275 args.hashval = xfs_da_hashname(args.name, args.namelen);
277 args.firstblock = &firstblock;
279 args.whichfork = XFS_ATTR_FORK;
280 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
282 /* Size is now blocks for attribute data */
283 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
286 * Start our first transaction of the day.
288 * All future transactions during this code must be "chained" off
289 * this one via the trans_dup() call. All transactions will contain
290 * the inode, and the inode will always be marked with trans_ihold().
291 * Since the inode will be locked in all transactions, we must log
292 * the inode in every transaction to let it float upward through
295 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
298 * Root fork attributes can use reserved data blocks for this
299 * operation if necessary
303 args.trans->t_flags |= XFS_TRANS_RESERVE;
305 if ((error = xfs_trans_reserve(args.trans, args.total,
306 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
307 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
308 xfs_trans_cancel(args.trans, 0);
311 xfs_ilock(dp, XFS_ILOCK_EXCL);
313 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
314 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
315 XFS_QMOPT_RES_REGBLKS);
317 xfs_iunlock(dp, XFS_ILOCK_EXCL);
318 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
322 xfs_trans_ijoin(args.trans, dp);
325 * If the attribute list is non-existent or a shortform list,
326 * upgrade it to a single-leaf-block attribute list.
328 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
329 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
330 (dp->i_d.di_anextents == 0))) {
333 * Build initial attribute list (if required).
335 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
336 xfs_attr_shortform_create(&args);
339 * Try to add the attr to the attribute list in
342 error = xfs_attr_shortform_addname(&args);
343 if (error != ENOSPC) {
345 * Commit the shortform mods, and we're done.
346 * NOTE: this is also the error path (EEXIST, etc).
348 ASSERT(args.trans != NULL);
351 * If this is a synchronous mount, make sure that
352 * the transaction goes to disk before returning
355 if (mp->m_flags & XFS_MOUNT_WSYNC) {
356 xfs_trans_set_sync(args.trans);
359 if (!error && (flags & ATTR_KERNOTIME) == 0) {
360 xfs_trans_ichgtime(args.trans, dp,
363 err2 = xfs_trans_commit(args.trans,
364 XFS_TRANS_RELEASE_LOG_RES);
365 xfs_iunlock(dp, XFS_ILOCK_EXCL);
367 return(error == 0 ? err2 : error);
371 * It won't fit in the shortform, transform to a leaf block.
372 * GROT: another possible req'mt for a double-split btree op.
374 xfs_bmap_init(args.flist, args.firstblock);
375 error = xfs_attr_shortform_to_leaf(&args);
377 error = xfs_bmap_finish(&args.trans, args.flist,
383 xfs_bmap_cancel(&flist);
388 * bmap_finish() may have committed the last trans and started
389 * a new one. We need the inode to be in all transactions.
392 xfs_trans_ijoin(args.trans, dp);
395 * Commit the leaf transformation. We'll need another (linked)
396 * transaction to add the new attribute to the leaf.
399 error = xfs_trans_roll(&args.trans, dp);
405 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
406 error = xfs_attr_leaf_addname(&args);
408 error = xfs_attr_node_addname(&args);
415 * If this is a synchronous mount, make sure that the
416 * transaction goes to disk before returning to the user.
418 if (mp->m_flags & XFS_MOUNT_WSYNC) {
419 xfs_trans_set_sync(args.trans);
422 if ((flags & ATTR_KERNOTIME) == 0)
423 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
426 * Commit the last in the sequence of transactions.
428 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
429 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
430 xfs_iunlock(dp, XFS_ILOCK_EXCL);
436 xfs_trans_cancel(args.trans,
437 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
438 xfs_iunlock(dp, XFS_ILOCK_EXCL);
445 const unsigned char *name,
446 unsigned char *value,
451 struct xfs_name xname;
453 XFS_STATS_INC(xs_attr_set);
455 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
458 error = xfs_attr_name_to_xname(&xname, name);
462 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
466 * Generic handler routine to remove a name from an attribute list.
467 * Transitions attribute list from Btree to shortform as necessary.
470 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
473 xfs_fsblock_t firstblock;
474 xfs_bmap_free_t flist;
476 xfs_mount_t *mp = dp->i_mount;
479 * Fill in the arg structure for this request.
481 memset((char *)&args, 0, sizeof(args));
482 args.name = name->name;
483 args.namelen = name->len;
485 args.hashval = xfs_da_hashname(args.name, args.namelen);
487 args.firstblock = &firstblock;
490 args.whichfork = XFS_ATTR_FORK;
493 * Attach the dquots to the inode.
495 error = xfs_qm_dqattach(dp, 0);
500 * Start our first transaction of the day.
502 * All future transactions during this code must be "chained" off
503 * this one via the trans_dup() call. All transactions will contain
504 * the inode, and the inode will always be marked with trans_ihold().
505 * Since the inode will be locked in all transactions, we must log
506 * the inode in every transaction to let it float upward through
509 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
512 * Root fork attributes can use reserved data blocks for this
513 * operation if necessary
516 if (flags & ATTR_ROOT)
517 args.trans->t_flags |= XFS_TRANS_RESERVE;
519 if ((error = xfs_trans_reserve(args.trans,
520 XFS_ATTRRM_SPACE_RES(mp),
521 XFS_ATTRRM_LOG_RES(mp),
522 0, XFS_TRANS_PERM_LOG_RES,
523 XFS_ATTRRM_LOG_COUNT))) {
524 xfs_trans_cancel(args.trans, 0);
528 xfs_ilock(dp, XFS_ILOCK_EXCL);
530 * No need to make quota reservations here. We expect to release some
531 * blocks not allocate in the common case.
533 xfs_trans_ijoin(args.trans, dp);
536 * Decide on what work routines to call based on the inode size.
538 if (!xfs_inode_hasattr(dp)) {
539 error = XFS_ERROR(ENOATTR);
542 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
543 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
544 error = xfs_attr_shortform_remove(&args);
548 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
549 error = xfs_attr_leaf_removename(&args);
551 error = xfs_attr_node_removename(&args);
558 * If this is a synchronous mount, make sure that the
559 * transaction goes to disk before returning to the user.
561 if (mp->m_flags & XFS_MOUNT_WSYNC) {
562 xfs_trans_set_sync(args.trans);
565 if ((flags & ATTR_KERNOTIME) == 0)
566 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
569 * Commit the last in the sequence of transactions.
571 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
572 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
573 xfs_iunlock(dp, XFS_ILOCK_EXCL);
579 xfs_trans_cancel(args.trans,
580 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
581 xfs_iunlock(dp, XFS_ILOCK_EXCL);
588 const unsigned char *name,
592 struct xfs_name xname;
594 XFS_STATS_INC(xs_attr_remove);
596 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
599 error = xfs_attr_name_to_xname(&xname, name);
603 xfs_ilock(dp, XFS_ILOCK_SHARED);
604 if (!xfs_inode_hasattr(dp)) {
605 xfs_iunlock(dp, XFS_ILOCK_SHARED);
606 return XFS_ERROR(ENOATTR);
608 xfs_iunlock(dp, XFS_ILOCK_SHARED);
610 return xfs_attr_remove_int(dp, &xname, flags);
614 xfs_attr_list_int(xfs_attr_list_context_t *context)
617 xfs_inode_t *dp = context->dp;
619 XFS_STATS_INC(xs_attr_list);
621 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
624 xfs_ilock(dp, XFS_ILOCK_SHARED);
627 * Decide on what work routines to call based on the inode size.
629 if (!xfs_inode_hasattr(dp)) {
631 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
632 error = xfs_attr_shortform_list(context);
633 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
634 error = xfs_attr_leaf_list(context);
636 error = xfs_attr_node_list(context);
639 xfs_iunlock(dp, XFS_ILOCK_SHARED);
644 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
645 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
646 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
647 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
648 & ~(sizeof(u_int32_t)-1))
651 * Format an attribute and copy it out to the user's buffer.
652 * Take care to check values and protect against them changing later,
653 * we may be reading them directly out of a user buffer.
657 xfs_attr_put_listent(
658 xfs_attr_list_context_t *context,
663 unsigned char *value)
665 struct attrlist *alist = (struct attrlist *)context->alist;
669 ASSERT(!(context->flags & ATTR_KERNOVAL));
670 ASSERT(context->count >= 0);
671 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
672 ASSERT(context->firstu >= sizeof(*alist));
673 ASSERT(context->firstu <= context->bufsize);
676 * Only list entries in the right namespace.
678 if (((context->flags & ATTR_SECURE) == 0) !=
679 ((flags & XFS_ATTR_SECURE) == 0))
681 if (((context->flags & ATTR_ROOT) == 0) !=
682 ((flags & XFS_ATTR_ROOT) == 0))
685 arraytop = sizeof(*alist) +
686 context->count * sizeof(alist->al_offset[0]);
687 context->firstu -= ATTR_ENTSIZE(namelen);
688 if (context->firstu < arraytop) {
689 trace_xfs_attr_list_full(context);
691 context->seen_enough = 1;
695 aep = (attrlist_ent_t *)&context->alist[context->firstu];
696 aep->a_valuelen = valuelen;
697 memcpy(aep->a_name, name, namelen);
698 aep->a_name[namelen] = 0;
699 alist->al_offset[context->count++] = context->firstu;
700 alist->al_count = context->count;
701 trace_xfs_attr_list_add(context);
706 * Generate a list of extended attribute names and optionally
707 * also value lengths. Positive return value follows the XFS
708 * convention of being an error, zero or negative return code
709 * is the length of the buffer returned (negated), indicating
718 attrlist_cursor_kern_t *cursor)
720 xfs_attr_list_context_t context;
721 struct attrlist *alist;
725 * Validate the cursor.
727 if (cursor->pad1 || cursor->pad2)
728 return(XFS_ERROR(EINVAL));
729 if ((cursor->initted == 0) &&
730 (cursor->hashval || cursor->blkno || cursor->offset))
731 return XFS_ERROR(EINVAL);
734 * Check for a properly aligned buffer.
736 if (((long)buffer) & (sizeof(int)-1))
737 return XFS_ERROR(EFAULT);
738 if (flags & ATTR_KERNOVAL)
742 * Initialize the output buffer.
744 memset(&context, 0, sizeof(context));
746 context.cursor = cursor;
748 context.flags = flags;
749 context.alist = buffer;
750 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
751 context.firstu = context.bufsize;
752 context.put_listent = xfs_attr_put_listent;
754 alist = (struct attrlist *)context.alist;
757 alist->al_offset[0] = context.bufsize;
759 error = xfs_attr_list_int(&context);
765 xfs_attr_inactive(xfs_inode_t *dp)
772 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
774 xfs_ilock(dp, XFS_ILOCK_SHARED);
775 if (!xfs_inode_hasattr(dp) ||
776 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
777 xfs_iunlock(dp, XFS_ILOCK_SHARED);
780 xfs_iunlock(dp, XFS_ILOCK_SHARED);
783 * Start our first transaction of the day.
785 * All future transactions during this code must be "chained" off
786 * this one via the trans_dup() call. All transactions will contain
787 * the inode, and the inode will always be marked with trans_ihold().
788 * Since the inode will be locked in all transactions, we must log
789 * the inode in every transaction to let it float upward through
792 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
793 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
794 XFS_TRANS_PERM_LOG_RES,
795 XFS_ATTRINVAL_LOG_COUNT))) {
796 xfs_trans_cancel(trans, 0);
799 xfs_ilock(dp, XFS_ILOCK_EXCL);
802 * No need to make quota reservations here. We expect to release some
803 * blocks, not allocate, in the common case.
805 xfs_trans_ijoin(trans, dp);
808 * Decide on what work routines to call based on the inode size.
810 if (!xfs_inode_hasattr(dp) ||
811 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
815 error = xfs_attr_root_inactive(&trans, dp);
819 * signal synchronous inactive transactions unless this
820 * is a synchronous mount filesystem in which case we
821 * know that we're here because we've been called out of
822 * xfs_inactive which means that the last reference is gone
823 * and the unlink transaction has already hit the disk so
824 * async inactive transactions are safe.
826 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
827 (!(mp->m_flags & XFS_MOUNT_WSYNC)
832 * Commit the last in the sequence of transactions.
834 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
835 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
836 xfs_iunlock(dp, XFS_ILOCK_EXCL);
841 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
842 xfs_iunlock(dp, XFS_ILOCK_EXCL);
848 /*========================================================================
849 * External routines when attribute list is inside the inode
850 *========================================================================*/
853 * Add a name to the shortform attribute list structure
854 * This is the external routine.
857 xfs_attr_shortform_addname(xfs_da_args_t *args)
859 int newsize, forkoff, retval;
861 retval = xfs_attr_shortform_lookup(args);
862 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
864 } else if (retval == EEXIST) {
865 if (args->flags & ATTR_CREATE)
867 retval = xfs_attr_shortform_remove(args);
871 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
872 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
873 return(XFS_ERROR(ENOSPC));
875 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
876 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
878 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
880 return(XFS_ERROR(ENOSPC));
882 xfs_attr_shortform_add(args, forkoff);
887 /*========================================================================
888 * External routines when attribute list is one block
889 *========================================================================*/
892 * Add a name to the leaf attribute list structure
894 * This leaf block cannot have a "remote" value, we only call this routine
895 * if bmap_one_block() says there is only one block (ie: no remote blks).
898 xfs_attr_leaf_addname(xfs_da_args_t *args)
902 int retval, error, committed, forkoff;
905 * Read the (only) block in the attribute list in.
909 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
916 * Look up the given attribute in the leaf block. Figure out if
917 * the given flags produce an error or call for an atomic rename.
919 retval = xfs_attr_leaf_lookup_int(bp, args);
920 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
921 xfs_da_brelse(args->trans, bp);
923 } else if (retval == EEXIST) {
924 if (args->flags & ATTR_CREATE) { /* pure create op */
925 xfs_da_brelse(args->trans, bp);
928 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
929 args->blkno2 = args->blkno; /* set 2nd entry info*/
930 args->index2 = args->index;
931 args->rmtblkno2 = args->rmtblkno;
932 args->rmtblkcnt2 = args->rmtblkcnt;
936 * Add the attribute to the leaf block, transitioning to a Btree
939 retval = xfs_attr_leaf_add(bp, args);
941 if (retval == ENOSPC) {
943 * Promote the attribute list to the Btree format, then
944 * Commit that transaction so that the node_addname() call
945 * can manage its own transactions.
947 xfs_bmap_init(args->flist, args->firstblock);
948 error = xfs_attr_leaf_to_node(args);
950 error = xfs_bmap_finish(&args->trans, args->flist,
956 xfs_bmap_cancel(args->flist);
961 * bmap_finish() may have committed the last trans and started
962 * a new one. We need the inode to be in all transactions.
965 xfs_trans_ijoin(args->trans, dp);
968 * Commit the current trans (including the inode) and start
971 error = xfs_trans_roll(&args->trans, dp);
976 * Fob the whole rest of the problem off on the Btree code.
978 error = xfs_attr_node_addname(args);
983 * Commit the transaction that added the attr name so that
984 * later routines can manage their own transactions.
986 error = xfs_trans_roll(&args->trans, dp);
991 * If there was an out-of-line value, allocate the blocks we
992 * identified for its storage and copy the value. This is done
993 * after we create the attribute so that we don't overflow the
994 * maximum size of a transaction and/or hit a deadlock.
996 if (args->rmtblkno > 0) {
997 error = xfs_attr_rmtval_set(args);
1003 * If this is an atomic rename operation, we must "flip" the
1004 * incomplete flags on the "new" and "old" attribute/value pairs
1005 * so that one disappears and one appears atomically. Then we
1006 * must remove the "old" attribute/value pair.
1008 if (args->op_flags & XFS_DA_OP_RENAME) {
1010 * In a separate transaction, set the incomplete flag on the
1011 * "old" attr and clear the incomplete flag on the "new" attr.
1013 error = xfs_attr_leaf_flipflags(args);
1018 * Dismantle the "old" attribute/value pair by removing
1019 * a "remote" value (if it exists).
1021 args->index = args->index2;
1022 args->blkno = args->blkno2;
1023 args->rmtblkno = args->rmtblkno2;
1024 args->rmtblkcnt = args->rmtblkcnt2;
1025 if (args->rmtblkno) {
1026 error = xfs_attr_rmtval_remove(args);
1032 * Read in the block containing the "old" attr, then
1033 * remove the "old" attr from that block (neat, huh!)
1035 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1036 &bp, XFS_ATTR_FORK);
1040 (void)xfs_attr_leaf_remove(bp, args);
1043 * If the result is small enough, shrink it all into the inode.
1045 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1046 xfs_bmap_init(args->flist, args->firstblock);
1047 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1048 /* bp is gone due to xfs_da_shrink_inode */
1050 error = xfs_bmap_finish(&args->trans,
1057 xfs_bmap_cancel(args->flist);
1062 * bmap_finish() may have committed the last trans
1063 * and started a new one. We need the inode to be
1064 * in all transactions.
1067 xfs_trans_ijoin(args->trans, dp);
1069 xfs_da_buf_done(bp);
1072 * Commit the remove and start the next trans in series.
1074 error = xfs_trans_roll(&args->trans, dp);
1076 } else if (args->rmtblkno > 0) {
1078 * Added a "remote" value, just clear the incomplete flag.
1080 error = xfs_attr_leaf_clearflag(args);
1086 * Remove a name from the leaf attribute list structure
1088 * This leaf block cannot have a "remote" value, we only call this routine
1089 * if bmap_one_block() says there is only one block (ie: no remote blks).
1092 xfs_attr_leaf_removename(xfs_da_args_t *args)
1096 int error, committed, forkoff;
1099 * Remove the attribute.
1103 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1110 error = xfs_attr_leaf_lookup_int(bp, args);
1111 if (error == ENOATTR) {
1112 xfs_da_brelse(args->trans, bp);
1116 (void)xfs_attr_leaf_remove(bp, args);
1119 * If the result is small enough, shrink it all into the inode.
1121 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1122 xfs_bmap_init(args->flist, args->firstblock);
1123 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1124 /* bp is gone due to xfs_da_shrink_inode */
1126 error = xfs_bmap_finish(&args->trans, args->flist,
1132 xfs_bmap_cancel(args->flist);
1137 * bmap_finish() may have committed the last trans and started
1138 * a new one. We need the inode to be in all transactions.
1141 xfs_trans_ijoin(args->trans, dp);
1143 xfs_da_buf_done(bp);
1148 * Look up a name in a leaf attribute list structure.
1150 * This leaf block cannot have a "remote" value, we only call this routine
1151 * if bmap_one_block() says there is only one block (ie: no remote blks).
1154 xfs_attr_leaf_get(xfs_da_args_t *args)
1160 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1166 error = xfs_attr_leaf_lookup_int(bp, args);
1167 if (error != EEXIST) {
1168 xfs_da_brelse(args->trans, bp);
1171 error = xfs_attr_leaf_getvalue(bp, args);
1172 xfs_da_brelse(args->trans, bp);
1173 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1174 error = xfs_attr_rmtval_get(args);
1180 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1183 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1185 xfs_attr_leafblock_t *leaf;
1189 context->cursor->blkno = 0;
1190 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1192 return XFS_ERROR(error);
1195 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1196 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1197 context->dp->i_mount, leaf);
1198 xfs_da_brelse(NULL, bp);
1199 return XFS_ERROR(EFSCORRUPTED);
1202 error = xfs_attr_leaf_list_int(bp, context);
1203 xfs_da_brelse(NULL, bp);
1204 return XFS_ERROR(error);
1208 /*========================================================================
1209 * External routines when attribute list size > XFS_LBSIZE(mp).
1210 *========================================================================*/
1213 * Add a name to a Btree-format attribute list.
1215 * This will involve walking down the Btree, and may involve splitting
1216 * leaf nodes and even splitting intermediate nodes up to and including
1217 * the root node (a special case of an intermediate node).
1219 * "Remote" attribute values confuse the issue and atomic rename operations
1220 * add a whole extra layer of confusion on top of that.
1223 xfs_attr_node_addname(xfs_da_args_t *args)
1225 xfs_da_state_t *state;
1226 xfs_da_state_blk_t *blk;
1229 int committed, retval, error;
1232 * Fill in bucket of arguments/results/context to carry around.
1237 state = xfs_da_state_alloc();
1240 state->blocksize = state->mp->m_sb.sb_blocksize;
1241 state->node_ents = state->mp->m_attr_node_ents;
1244 * Search to see if name already exists, and get back a pointer
1245 * to where it should go.
1247 error = xfs_da_node_lookup_int(state, &retval);
1250 blk = &state->path.blk[ state->path.active-1 ];
1251 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1252 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1254 } else if (retval == EEXIST) {
1255 if (args->flags & ATTR_CREATE)
1257 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1258 args->blkno2 = args->blkno; /* set 2nd entry info*/
1259 args->index2 = args->index;
1260 args->rmtblkno2 = args->rmtblkno;
1261 args->rmtblkcnt2 = args->rmtblkcnt;
1263 args->rmtblkcnt = 0;
1266 retval = xfs_attr_leaf_add(blk->bp, state->args);
1267 if (retval == ENOSPC) {
1268 if (state->path.active == 1) {
1270 * Its really a single leaf node, but it had
1271 * out-of-line values so it looked like it *might*
1272 * have been a b-tree.
1274 xfs_da_state_free(state);
1275 xfs_bmap_init(args->flist, args->firstblock);
1276 error = xfs_attr_leaf_to_node(args);
1278 error = xfs_bmap_finish(&args->trans,
1285 xfs_bmap_cancel(args->flist);
1290 * bmap_finish() may have committed the last trans
1291 * and started a new one. We need the inode to be
1292 * in all transactions.
1295 xfs_trans_ijoin(args->trans, dp);
1298 * Commit the node conversion and start the next
1299 * trans in the chain.
1301 error = xfs_trans_roll(&args->trans, dp);
1309 * Split as many Btree elements as required.
1310 * This code tracks the new and old attr's location
1311 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1312 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1314 xfs_bmap_init(args->flist, args->firstblock);
1315 error = xfs_da_split(state);
1317 error = xfs_bmap_finish(&args->trans, args->flist,
1323 xfs_bmap_cancel(args->flist);
1328 * bmap_finish() may have committed the last trans and started
1329 * a new one. We need the inode to be in all transactions.
1332 xfs_trans_ijoin(args->trans, dp);
1335 * Addition succeeded, update Btree hashvals.
1337 xfs_da_fixhashpath(state, &state->path);
1341 * Kill the state structure, we're done with it and need to
1342 * allow the buffers to come back later.
1344 xfs_da_state_free(state);
1348 * Commit the leaf addition or btree split and start the next
1349 * trans in the chain.
1351 error = xfs_trans_roll(&args->trans, dp);
1356 * If there was an out-of-line value, allocate the blocks we
1357 * identified for its storage and copy the value. This is done
1358 * after we create the attribute so that we don't overflow the
1359 * maximum size of a transaction and/or hit a deadlock.
1361 if (args->rmtblkno > 0) {
1362 error = xfs_attr_rmtval_set(args);
1368 * If this is an atomic rename operation, we must "flip" the
1369 * incomplete flags on the "new" and "old" attribute/value pairs
1370 * so that one disappears and one appears atomically. Then we
1371 * must remove the "old" attribute/value pair.
1373 if (args->op_flags & XFS_DA_OP_RENAME) {
1375 * In a separate transaction, set the incomplete flag on the
1376 * "old" attr and clear the incomplete flag on the "new" attr.
1378 error = xfs_attr_leaf_flipflags(args);
1383 * Dismantle the "old" attribute/value pair by removing
1384 * a "remote" value (if it exists).
1386 args->index = args->index2;
1387 args->blkno = args->blkno2;
1388 args->rmtblkno = args->rmtblkno2;
1389 args->rmtblkcnt = args->rmtblkcnt2;
1390 if (args->rmtblkno) {
1391 error = xfs_attr_rmtval_remove(args);
1397 * Re-find the "old" attribute entry after any split ops.
1398 * The INCOMPLETE flag means that we will find the "old"
1399 * attr, not the "new" one.
1401 args->flags |= XFS_ATTR_INCOMPLETE;
1402 state = xfs_da_state_alloc();
1405 state->blocksize = state->mp->m_sb.sb_blocksize;
1406 state->node_ents = state->mp->m_attr_node_ents;
1408 error = xfs_da_node_lookup_int(state, &retval);
1413 * Remove the name and update the hashvals in the tree.
1415 blk = &state->path.blk[ state->path.active-1 ];
1416 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1417 error = xfs_attr_leaf_remove(blk->bp, args);
1418 xfs_da_fixhashpath(state, &state->path);
1421 * Check to see if the tree needs to be collapsed.
1423 if (retval && (state->path.active > 1)) {
1424 xfs_bmap_init(args->flist, args->firstblock);
1425 error = xfs_da_join(state);
1427 error = xfs_bmap_finish(&args->trans,
1434 xfs_bmap_cancel(args->flist);
1439 * bmap_finish() may have committed the last trans
1440 * and started a new one. We need the inode to be
1441 * in all transactions.
1444 xfs_trans_ijoin(args->trans, dp);
1448 * Commit and start the next trans in the chain.
1450 error = xfs_trans_roll(&args->trans, dp);
1454 } else if (args->rmtblkno > 0) {
1456 * Added a "remote" value, just clear the incomplete flag.
1458 error = xfs_attr_leaf_clearflag(args);
1466 xfs_da_state_free(state);
1473 * Remove a name from a B-tree attribute list.
1475 * This will involve walking down the Btree, and may involve joining
1476 * leaf nodes and even joining intermediate nodes up to and including
1477 * the root node (a special case of an intermediate node).
1480 xfs_attr_node_removename(xfs_da_args_t *args)
1482 xfs_da_state_t *state;
1483 xfs_da_state_blk_t *blk;
1486 int retval, error, committed, forkoff;
1489 * Tie a string around our finger to remind us where we are.
1492 state = xfs_da_state_alloc();
1494 state->mp = dp->i_mount;
1495 state->blocksize = state->mp->m_sb.sb_blocksize;
1496 state->node_ents = state->mp->m_attr_node_ents;
1499 * Search to see if name exists, and get back a pointer to it.
1501 error = xfs_da_node_lookup_int(state, &retval);
1502 if (error || (retval != EEXIST)) {
1509 * If there is an out-of-line value, de-allocate the blocks.
1510 * This is done before we remove the attribute so that we don't
1511 * overflow the maximum size of a transaction and/or hit a deadlock.
1513 blk = &state->path.blk[ state->path.active-1 ];
1514 ASSERT(blk->bp != NULL);
1515 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1516 if (args->rmtblkno > 0) {
1518 * Fill in disk block numbers in the state structure
1519 * so that we can get the buffers back after we commit
1520 * several transactions in the following calls.
1522 error = xfs_attr_fillstate(state);
1527 * Mark the attribute as INCOMPLETE, then bunmapi() the
1530 error = xfs_attr_leaf_setflag(args);
1533 error = xfs_attr_rmtval_remove(args);
1538 * Refill the state structure with buffers, the prior calls
1539 * released our buffers.
1541 error = xfs_attr_refillstate(state);
1547 * Remove the name and update the hashvals in the tree.
1549 blk = &state->path.blk[ state->path.active-1 ];
1550 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1551 retval = xfs_attr_leaf_remove(blk->bp, args);
1552 xfs_da_fixhashpath(state, &state->path);
1555 * Check to see if the tree needs to be collapsed.
1557 if (retval && (state->path.active > 1)) {
1558 xfs_bmap_init(args->flist, args->firstblock);
1559 error = xfs_da_join(state);
1561 error = xfs_bmap_finish(&args->trans, args->flist,
1567 xfs_bmap_cancel(args->flist);
1572 * bmap_finish() may have committed the last trans and started
1573 * a new one. We need the inode to be in all transactions.
1576 xfs_trans_ijoin(args->trans, dp);
1579 * Commit the Btree join operation and start a new trans.
1581 error = xfs_trans_roll(&args->trans, dp);
1587 * If the result is small enough, push it all into the inode.
1589 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1591 * Have to get rid of the copy of this dabuf in the state.
1593 ASSERT(state->path.active == 1);
1594 ASSERT(state->path.blk[0].bp);
1595 xfs_da_buf_done(state->path.blk[0].bp);
1596 state->path.blk[0].bp = NULL;
1598 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1602 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1603 bp->data)->hdr.info.magic)
1604 == XFS_ATTR_LEAF_MAGIC);
1606 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1607 xfs_bmap_init(args->flist, args->firstblock);
1608 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1609 /* bp is gone due to xfs_da_shrink_inode */
1611 error = xfs_bmap_finish(&args->trans,
1618 xfs_bmap_cancel(args->flist);
1623 * bmap_finish() may have committed the last trans
1624 * and started a new one. We need the inode to be
1625 * in all transactions.
1628 xfs_trans_ijoin(args->trans, dp);
1630 xfs_da_brelse(args->trans, bp);
1635 xfs_da_state_free(state);
1640 * Fill in the disk block numbers in the state structure for the buffers
1641 * that are attached to the state structure.
1642 * This is done so that we can quickly reattach ourselves to those buffers
1643 * after some set of transaction commits have released these buffers.
1646 xfs_attr_fillstate(xfs_da_state_t *state)
1648 xfs_da_state_path_t *path;
1649 xfs_da_state_blk_t *blk;
1653 * Roll down the "path" in the state structure, storing the on-disk
1654 * block number for those buffers in the "path".
1656 path = &state->path;
1657 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1658 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1660 blk->disk_blkno = xfs_da_blkno(blk->bp);
1661 xfs_da_buf_done(blk->bp);
1664 blk->disk_blkno = 0;
1669 * Roll down the "altpath" in the state structure, storing the on-disk
1670 * block number for those buffers in the "altpath".
1672 path = &state->altpath;
1673 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1674 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1676 blk->disk_blkno = xfs_da_blkno(blk->bp);
1677 xfs_da_buf_done(blk->bp);
1680 blk->disk_blkno = 0;
1688 * Reattach the buffers to the state structure based on the disk block
1689 * numbers stored in the state structure.
1690 * This is done after some set of transaction commits have released those
1691 * buffers from our grip.
1694 xfs_attr_refillstate(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++) {
1707 if (blk->disk_blkno) {
1708 error = xfs_da_read_buf(state->args->trans,
1710 blk->blkno, blk->disk_blkno,
1711 &blk->bp, XFS_ATTR_FORK);
1720 * Roll down the "altpath" in the state structure, storing the on-disk
1721 * block number for those buffers in the "altpath".
1723 path = &state->altpath;
1724 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1725 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1726 if (blk->disk_blkno) {
1727 error = xfs_da_read_buf(state->args->trans,
1729 blk->blkno, blk->disk_blkno,
1730 &blk->bp, XFS_ATTR_FORK);
1742 * Look up a filename in a node attribute list.
1744 * This routine gets called for any attribute fork that has more than one
1745 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1746 * "remote" values taking up more blocks.
1749 xfs_attr_node_get(xfs_da_args_t *args)
1751 xfs_da_state_t *state;
1752 xfs_da_state_blk_t *blk;
1756 state = xfs_da_state_alloc();
1758 state->mp = args->dp->i_mount;
1759 state->blocksize = state->mp->m_sb.sb_blocksize;
1760 state->node_ents = state->mp->m_attr_node_ents;
1763 * Search to see if name exists, and get back a pointer to it.
1765 error = xfs_da_node_lookup_int(state, &retval);
1768 } else if (retval == EEXIST) {
1769 blk = &state->path.blk[ state->path.active-1 ];
1770 ASSERT(blk->bp != NULL);
1771 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1774 * Get the value, local or "remote"
1776 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1777 if (!retval && (args->rmtblkno > 0)
1778 && !(args->flags & ATTR_KERNOVAL)) {
1779 retval = xfs_attr_rmtval_get(args);
1784 * If not in a transaction, we have to release all the buffers.
1786 for (i = 0; i < state->path.active; i++) {
1787 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1788 state->path.blk[i].bp = NULL;
1791 xfs_da_state_free(state);
1795 STATIC int /* error */
1796 xfs_attr_node_list(xfs_attr_list_context_t *context)
1798 attrlist_cursor_kern_t *cursor;
1799 xfs_attr_leafblock_t *leaf;
1800 xfs_da_intnode_t *node;
1801 xfs_da_node_entry_t *btree;
1805 cursor = context->cursor;
1806 cursor->initted = 1;
1809 * Do all sorts of validation on the passed-in cursor structure.
1810 * If anything is amiss, ignore the cursor and look up the hashval
1811 * starting from the btree root.
1814 if (cursor->blkno > 0) {
1815 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1816 &bp, XFS_ATTR_FORK);
1817 if ((error != 0) && (error != EFSCORRUPTED))
1821 switch (be16_to_cpu(node->hdr.info.magic)) {
1822 case XFS_DA_NODE_MAGIC:
1823 trace_xfs_attr_list_wrong_blk(context);
1824 xfs_da_brelse(NULL, bp);
1827 case XFS_ATTR_LEAF_MAGIC:
1829 if (cursor->hashval > be32_to_cpu(leaf->entries[
1830 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1831 trace_xfs_attr_list_wrong_blk(context);
1832 xfs_da_brelse(NULL, bp);
1834 } else if (cursor->hashval <=
1835 be32_to_cpu(leaf->entries[0].hashval)) {
1836 trace_xfs_attr_list_wrong_blk(context);
1837 xfs_da_brelse(NULL, bp);
1842 trace_xfs_attr_list_wrong_blk(context);
1843 xfs_da_brelse(NULL, bp);
1850 * We did not find what we expected given the cursor's contents,
1851 * so we start from the top and work down based on the hash value.
1852 * Note that start of node block is same as start of leaf block.
1857 error = xfs_da_read_buf(NULL, context->dp,
1858 cursor->blkno, -1, &bp,
1862 if (unlikely(bp == NULL)) {
1863 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1865 context->dp->i_mount);
1866 return(XFS_ERROR(EFSCORRUPTED));
1869 if (be16_to_cpu(node->hdr.info.magic)
1870 == XFS_ATTR_LEAF_MAGIC)
1872 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1873 != XFS_DA_NODE_MAGIC)) {
1874 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1876 context->dp->i_mount,
1878 xfs_da_brelse(NULL, bp);
1879 return(XFS_ERROR(EFSCORRUPTED));
1881 btree = node->btree;
1882 for (i = 0; i < be16_to_cpu(node->hdr.count);
1885 <= be32_to_cpu(btree->hashval)) {
1886 cursor->blkno = be32_to_cpu(btree->before);
1887 trace_xfs_attr_list_node_descend(context,
1892 if (i == be16_to_cpu(node->hdr.count)) {
1893 xfs_da_brelse(NULL, bp);
1896 xfs_da_brelse(NULL, bp);
1902 * Roll upward through the blocks, processing each leaf block in
1903 * order. As long as there is space in the result buffer, keep
1904 * adding the information.
1908 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1909 != XFS_ATTR_LEAF_MAGIC)) {
1910 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1912 context->dp->i_mount, leaf);
1913 xfs_da_brelse(NULL, bp);
1914 return(XFS_ERROR(EFSCORRUPTED));
1916 error = xfs_attr_leaf_list_int(bp, context);
1918 xfs_da_brelse(NULL, bp);
1921 if (context->seen_enough || leaf->hdr.info.forw == 0)
1923 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1924 xfs_da_brelse(NULL, bp);
1925 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1926 &bp, XFS_ATTR_FORK);
1929 if (unlikely((bp == NULL))) {
1930 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1932 context->dp->i_mount);
1933 return(XFS_ERROR(EFSCORRUPTED));
1936 xfs_da_brelse(NULL, bp);
1941 /*========================================================================
1942 * External routines for manipulating out-of-line attribute values.
1943 *========================================================================*/
1946 * Read the value associated with an attribute from the out-of-line buffer
1947 * that we stored it in.
1950 xfs_attr_rmtval_get(xfs_da_args_t *args)
1952 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1957 int nmap, error, tmp, valuelen, blkcnt, i;
1960 ASSERT(!(args->flags & ATTR_KERNOVAL));
1962 mp = args->dp->i_mount;
1964 valuelen = args->valuelen;
1965 lblkno = args->rmtblkno;
1966 while (valuelen > 0) {
1967 nmap = ATTR_RMTVALUE_MAPSIZE;
1968 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
1970 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
1971 NULL, 0, map, &nmap, NULL);
1976 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
1977 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
1978 (map[i].br_startblock != HOLESTARTBLOCK));
1979 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
1980 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
1981 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
1982 blkcnt, XBF_LOCK | XBF_DONT_BLOCK,
1987 tmp = (valuelen < XFS_BUF_SIZE(bp))
1988 ? valuelen : XFS_BUF_SIZE(bp);
1989 xfs_buf_iomove(bp, 0, tmp, dst, XBRW_READ);
1994 lblkno += map[i].br_blockcount;
1997 ASSERT(valuelen == 0);
2002 * Write the value associated with an attribute into the out-of-line buffer
2003 * that we have defined for it.
2006 xfs_attr_rmtval_set(xfs_da_args_t *args)
2009 xfs_fileoff_t lfileoff;
2011 xfs_bmbt_irec_t map;
2016 int blkcnt, valuelen, nmap, error, tmp, committed;
2023 * Find a "hole" in the attribute address space large enough for
2024 * us to drop the new attribute's value into.
2026 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2028 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2033 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2034 args->rmtblkcnt = blkcnt;
2037 * Roll through the "value", allocating blocks on disk as required.
2039 while (blkcnt > 0) {
2041 * Allocate a single extent, up to the size of the value.
2043 xfs_bmap_init(args->flist, args->firstblock);
2045 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2047 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2049 args->firstblock, args->total, &map, &nmap,
2052 error = xfs_bmap_finish(&args->trans, args->flist,
2058 xfs_bmap_cancel(args->flist);
2063 * bmap_finish() may have committed the last trans and started
2064 * a new one. We need the inode to be in all transactions.
2067 xfs_trans_ijoin(args->trans, dp);
2070 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2071 (map.br_startblock != HOLESTARTBLOCK));
2072 lblkno += map.br_blockcount;
2073 blkcnt -= map.br_blockcount;
2076 * Start the next trans in the chain.
2078 error = xfs_trans_roll(&args->trans, dp);
2084 * Roll through the "value", copying the attribute value to the
2085 * already-allocated blocks. Blocks are written synchronously
2086 * so that we can know they are all on disk before we turn off
2087 * the INCOMPLETE flag.
2089 lblkno = args->rmtblkno;
2090 valuelen = args->valuelen;
2091 while (valuelen > 0) {
2093 * Try to remember where we decided to put the value.
2095 xfs_bmap_init(args->flist, args->firstblock);
2097 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2099 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2100 args->firstblock, 0, &map, &nmap,
2106 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2107 (map.br_startblock != HOLESTARTBLOCK));
2109 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2110 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2112 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt,
2113 XBF_LOCK | XBF_DONT_BLOCK);
2115 ASSERT(!XFS_BUF_GETERROR(bp));
2117 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2119 xfs_buf_iomove(bp, 0, tmp, src, XBRW_WRITE);
2120 if (tmp < XFS_BUF_SIZE(bp))
2121 xfs_buf_zero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2122 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2128 lblkno += map.br_blockcount;
2130 ASSERT(valuelen == 0);
2135 * Remove the value associated with an attribute by deleting the
2136 * out-of-line buffer that it is stored on.
2139 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2142 xfs_bmbt_irec_t map;
2146 int valuelen, blkcnt, nmap, error, done, committed;
2148 mp = args->dp->i_mount;
2151 * Roll through the "value", invalidating the attribute value's
2154 lblkno = args->rmtblkno;
2155 valuelen = args->rmtblkcnt;
2156 while (valuelen > 0) {
2158 * Try to remember where we decided to put the value.
2160 xfs_bmap_init(args->flist, args->firstblock);
2162 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2164 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2165 args->firstblock, 0, &map, &nmap,
2171 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2172 (map.br_startblock != HOLESTARTBLOCK));
2174 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2175 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2178 * If the "remote" value is in the cache, remove it.
2180 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
2183 XFS_BUF_UNDELAYWRITE(bp);
2188 valuelen -= map.br_blockcount;
2190 lblkno += map.br_blockcount;
2194 * Keep de-allocating extents until the remote-value region is gone.
2196 lblkno = args->rmtblkno;
2197 blkcnt = args->rmtblkcnt;
2200 xfs_bmap_init(args->flist, args->firstblock);
2201 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2202 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2203 1, args->firstblock, args->flist,
2206 error = xfs_bmap_finish(&args->trans, args->flist,
2212 xfs_bmap_cancel(args->flist);
2217 * bmap_finish() may have committed the last trans and started
2218 * a new one. We need the inode to be in all transactions.
2221 xfs_trans_ijoin(args->trans, args->dp);
2224 * Close out trans and start the next one in the chain.
2226 error = xfs_trans_roll(&args->trans, args->dp);