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"
24 #include "xfs_trans.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_attr_sf.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_alloc.h"
34 #include "xfs_inode_item.h"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_error.h"
39 #include "xfs_quota.h"
40 #include "xfs_trans_space.h"
42 #include "xfs_vnodeops.h"
43 #include "xfs_trace.h"
48 * Provide the external interfaces to manage attribute lists.
51 /*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
56 * Internal routines when attribute list fits inside the inode.
58 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
61 * Internal routines when attribute list is one block.
63 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
64 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
65 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
66 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
69 * Internal routines when attribute list is more than one block.
71 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
72 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
73 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
74 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
75 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
76 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
79 * Routines to manipulate out-of-line attribute values.
81 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
82 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
84 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
87 xfs_attr_name_to_xname(
88 struct xfs_name *xname,
89 const unsigned char *aname)
94 xname->len = strlen((char *)aname);
95 if (xname->len >= MAXNAMELEN)
96 return EFAULT; /* match IRIX behaviour */
103 struct xfs_inode *ip)
105 if (!XFS_IFORK_Q(ip) ||
106 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
107 ip->i_d.di_anextents == 0))
112 /*========================================================================
113 * Overall external interface routines.
114 *========================================================================*/
118 struct xfs_inode *ip,
119 struct xfs_name *name,
120 unsigned char *value,
127 if (!xfs_inode_hasattr(ip))
131 * Fill in the arg structure for this request.
133 memset((char *)&args, 0, sizeof(args));
134 args.name = name->name;
135 args.namelen = name->len;
137 args.valuelen = *valuelenp;
139 args.hashval = xfs_da_hashname(args.name, args.namelen);
141 args.whichfork = XFS_ATTR_FORK;
144 * Decide on what work routines to call based on the inode size.
146 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
147 error = xfs_attr_shortform_getvalue(&args);
148 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
149 error = xfs_attr_leaf_get(&args);
151 error = xfs_attr_node_get(&args);
155 * Return the number of bytes in the value to the caller.
157 *valuelenp = args.valuelen;
167 const unsigned char *name,
168 unsigned char *value,
173 struct xfs_name xname;
175 XFS_STATS_INC(xs_attr_get);
177 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
180 error = xfs_attr_name_to_xname(&xname, name);
184 xfs_ilock(ip, XFS_ILOCK_SHARED);
185 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
186 xfs_iunlock(ip, XFS_ILOCK_SHARED);
191 * Calculate how many blocks we need for the new attribute,
195 struct xfs_inode *ip,
200 struct xfs_mount *mp = ip->i_mount;
205 * Determine space new attribute will use, and if it would be
206 * "local" or "remote" (note: local != inline).
208 size = xfs_attr_leaf_newentsize(namelen, valuelen,
209 mp->m_sb.sb_blocksize, local);
211 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
213 if (size > (mp->m_sb.sb_blocksize >> 1)) {
214 /* Double split possible */
219 * Out of line attribute, cannot double split, but
220 * make room for the attribute value itself.
222 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
224 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
232 struct xfs_inode *dp,
233 struct xfs_name *name,
234 unsigned char *value,
239 xfs_fsblock_t firstblock;
240 xfs_bmap_free_t flist;
241 int error, err2, committed;
242 xfs_mount_t *mp = dp->i_mount;
243 int rsvd = (flags & ATTR_ROOT) != 0;
247 * Attach the dquots to the inode.
249 error = xfs_qm_dqattach(dp, 0);
254 * If the inode doesn't have an attribute fork, add one.
255 * (inode must not be locked when we call this routine)
257 if (XFS_IFORK_Q(dp) == 0) {
258 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
259 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
261 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
266 * Fill in the arg structure for this request.
268 memset((char *)&args, 0, sizeof(args));
269 args.name = name->name;
270 args.namelen = name->len;
272 args.valuelen = valuelen;
274 args.hashval = xfs_da_hashname(args.name, args.namelen);
276 args.firstblock = &firstblock;
278 args.whichfork = XFS_ATTR_FORK;
279 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
281 /* Size is now blocks for attribute data */
282 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
285 * Start our first transaction of the day.
287 * All future transactions during this code must be "chained" off
288 * this one via the trans_dup() call. All transactions will contain
289 * the inode, and the inode will always be marked with trans_ihold().
290 * Since the inode will be locked in all transactions, we must log
291 * the inode in every transaction to let it float upward through
294 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
297 * Root fork attributes can use reserved data blocks for this
298 * operation if necessary
302 args.trans->t_flags |= XFS_TRANS_RESERVE;
304 if ((error = xfs_trans_reserve(args.trans, args.total,
305 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
306 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
307 xfs_trans_cancel(args.trans, 0);
310 xfs_ilock(dp, XFS_ILOCK_EXCL);
312 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
313 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
314 XFS_QMOPT_RES_REGBLKS);
316 xfs_iunlock(dp, XFS_ILOCK_EXCL);
317 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
321 xfs_trans_ijoin(args.trans, dp, 0);
324 * If the attribute list is non-existent or a shortform list,
325 * upgrade it to a single-leaf-block attribute list.
327 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
328 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
329 (dp->i_d.di_anextents == 0))) {
332 * Build initial attribute list (if required).
334 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
335 xfs_attr_shortform_create(&args);
338 * Try to add the attr to the attribute list in
341 error = xfs_attr_shortform_addname(&args);
342 if (error != ENOSPC) {
344 * Commit the shortform mods, and we're done.
345 * NOTE: this is also the error path (EEXIST, etc).
347 ASSERT(args.trans != NULL);
350 * If this is a synchronous mount, make sure that
351 * the transaction goes to disk before returning
354 if (mp->m_flags & XFS_MOUNT_WSYNC) {
355 xfs_trans_set_sync(args.trans);
358 if (!error && (flags & ATTR_KERNOTIME) == 0) {
359 xfs_trans_ichgtime(args.trans, dp,
362 err2 = xfs_trans_commit(args.trans,
363 XFS_TRANS_RELEASE_LOG_RES);
364 xfs_iunlock(dp, XFS_ILOCK_EXCL);
366 return(error == 0 ? err2 : error);
370 * It won't fit in the shortform, transform to a leaf block.
371 * GROT: another possible req'mt for a double-split btree op.
373 xfs_bmap_init(args.flist, args.firstblock);
374 error = xfs_attr_shortform_to_leaf(&args);
376 error = xfs_bmap_finish(&args.trans, args.flist,
382 xfs_bmap_cancel(&flist);
387 * bmap_finish() may have committed the last trans and started
388 * a new one. We need the inode to be in all transactions.
391 xfs_trans_ijoin(args.trans, dp, 0);
394 * Commit the leaf transformation. We'll need another (linked)
395 * transaction to add the new attribute to the leaf.
398 error = xfs_trans_roll(&args.trans, dp);
404 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
405 error = xfs_attr_leaf_addname(&args);
407 error = xfs_attr_node_addname(&args);
414 * If this is a synchronous mount, make sure that the
415 * transaction goes to disk before returning to the user.
417 if (mp->m_flags & XFS_MOUNT_WSYNC) {
418 xfs_trans_set_sync(args.trans);
421 if ((flags & ATTR_KERNOTIME) == 0)
422 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
425 * Commit the last in the sequence of transactions.
427 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
428 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
429 xfs_iunlock(dp, XFS_ILOCK_EXCL);
435 xfs_trans_cancel(args.trans,
436 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
437 xfs_iunlock(dp, XFS_ILOCK_EXCL);
444 const unsigned char *name,
445 unsigned char *value,
450 struct xfs_name xname;
452 XFS_STATS_INC(xs_attr_set);
454 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
457 error = xfs_attr_name_to_xname(&xname, name);
461 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
465 * Generic handler routine to remove a name from an attribute list.
466 * Transitions attribute list from Btree to shortform as necessary.
469 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
472 xfs_fsblock_t firstblock;
473 xfs_bmap_free_t flist;
475 xfs_mount_t *mp = dp->i_mount;
478 * Fill in the arg structure for this request.
480 memset((char *)&args, 0, sizeof(args));
481 args.name = name->name;
482 args.namelen = name->len;
484 args.hashval = xfs_da_hashname(args.name, args.namelen);
486 args.firstblock = &firstblock;
489 args.whichfork = XFS_ATTR_FORK;
492 * we have no control over the attribute names that userspace passes us
493 * to remove, so we have to allow the name lookup prior to attribute
496 args.op_flags = XFS_DA_OP_OKNOENT;
499 * Attach the dquots to the inode.
501 error = xfs_qm_dqattach(dp, 0);
506 * Start our first transaction of the day.
508 * All future transactions during this code must be "chained" off
509 * this one via the trans_dup() call. All transactions will contain
510 * the inode, and the inode will always be marked with trans_ihold().
511 * Since the inode will be locked in all transactions, we must log
512 * the inode in every transaction to let it float upward through
515 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
518 * Root fork attributes can use reserved data blocks for this
519 * operation if necessary
522 if (flags & ATTR_ROOT)
523 args.trans->t_flags |= XFS_TRANS_RESERVE;
525 if ((error = xfs_trans_reserve(args.trans,
526 XFS_ATTRRM_SPACE_RES(mp),
527 XFS_ATTRRM_LOG_RES(mp),
528 0, XFS_TRANS_PERM_LOG_RES,
529 XFS_ATTRRM_LOG_COUNT))) {
530 xfs_trans_cancel(args.trans, 0);
534 xfs_ilock(dp, XFS_ILOCK_EXCL);
536 * No need to make quota reservations here. We expect to release some
537 * blocks not allocate in the common case.
539 xfs_trans_ijoin(args.trans, dp, 0);
542 * Decide on what work routines to call based on the inode size.
544 if (!xfs_inode_hasattr(dp)) {
545 error = XFS_ERROR(ENOATTR);
548 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
549 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
550 error = xfs_attr_shortform_remove(&args);
554 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
555 error = xfs_attr_leaf_removename(&args);
557 error = xfs_attr_node_removename(&args);
564 * If this is a synchronous mount, make sure that the
565 * transaction goes to disk before returning to the user.
567 if (mp->m_flags & XFS_MOUNT_WSYNC) {
568 xfs_trans_set_sync(args.trans);
571 if ((flags & ATTR_KERNOTIME) == 0)
572 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
575 * Commit the last in the sequence of transactions.
577 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
578 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
579 xfs_iunlock(dp, XFS_ILOCK_EXCL);
585 xfs_trans_cancel(args.trans,
586 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
587 xfs_iunlock(dp, XFS_ILOCK_EXCL);
594 const unsigned char *name,
598 struct xfs_name xname;
600 XFS_STATS_INC(xs_attr_remove);
602 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
605 error = xfs_attr_name_to_xname(&xname, name);
609 xfs_ilock(dp, XFS_ILOCK_SHARED);
610 if (!xfs_inode_hasattr(dp)) {
611 xfs_iunlock(dp, XFS_ILOCK_SHARED);
612 return XFS_ERROR(ENOATTR);
614 xfs_iunlock(dp, XFS_ILOCK_SHARED);
616 return xfs_attr_remove_int(dp, &xname, flags);
620 xfs_attr_list_int(xfs_attr_list_context_t *context)
623 xfs_inode_t *dp = context->dp;
625 XFS_STATS_INC(xs_attr_list);
627 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
630 xfs_ilock(dp, XFS_ILOCK_SHARED);
633 * Decide on what work routines to call based on the inode size.
635 if (!xfs_inode_hasattr(dp)) {
637 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
638 error = xfs_attr_shortform_list(context);
639 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
640 error = xfs_attr_leaf_list(context);
642 error = xfs_attr_node_list(context);
645 xfs_iunlock(dp, XFS_ILOCK_SHARED);
650 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
651 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
652 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
653 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
654 & ~(sizeof(u_int32_t)-1))
657 * Format an attribute and copy it out to the user's buffer.
658 * Take care to check values and protect against them changing later,
659 * we may be reading them directly out of a user buffer.
663 xfs_attr_put_listent(
664 xfs_attr_list_context_t *context,
669 unsigned char *value)
671 struct attrlist *alist = (struct attrlist *)context->alist;
675 ASSERT(!(context->flags & ATTR_KERNOVAL));
676 ASSERT(context->count >= 0);
677 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
678 ASSERT(context->firstu >= sizeof(*alist));
679 ASSERT(context->firstu <= context->bufsize);
682 * Only list entries in the right namespace.
684 if (((context->flags & ATTR_SECURE) == 0) !=
685 ((flags & XFS_ATTR_SECURE) == 0))
687 if (((context->flags & ATTR_ROOT) == 0) !=
688 ((flags & XFS_ATTR_ROOT) == 0))
691 arraytop = sizeof(*alist) +
692 context->count * sizeof(alist->al_offset[0]);
693 context->firstu -= ATTR_ENTSIZE(namelen);
694 if (context->firstu < arraytop) {
695 trace_xfs_attr_list_full(context);
697 context->seen_enough = 1;
701 aep = (attrlist_ent_t *)&context->alist[context->firstu];
702 aep->a_valuelen = valuelen;
703 memcpy(aep->a_name, name, namelen);
704 aep->a_name[namelen] = 0;
705 alist->al_offset[context->count++] = context->firstu;
706 alist->al_count = context->count;
707 trace_xfs_attr_list_add(context);
712 * Generate a list of extended attribute names and optionally
713 * also value lengths. Positive return value follows the XFS
714 * convention of being an error, zero or negative return code
715 * is the length of the buffer returned (negated), indicating
724 attrlist_cursor_kern_t *cursor)
726 xfs_attr_list_context_t context;
727 struct attrlist *alist;
731 * Validate the cursor.
733 if (cursor->pad1 || cursor->pad2)
734 return(XFS_ERROR(EINVAL));
735 if ((cursor->initted == 0) &&
736 (cursor->hashval || cursor->blkno || cursor->offset))
737 return XFS_ERROR(EINVAL);
740 * Check for a properly aligned buffer.
742 if (((long)buffer) & (sizeof(int)-1))
743 return XFS_ERROR(EFAULT);
744 if (flags & ATTR_KERNOVAL)
748 * Initialize the output buffer.
750 memset(&context, 0, sizeof(context));
752 context.cursor = cursor;
754 context.flags = flags;
755 context.alist = buffer;
756 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
757 context.firstu = context.bufsize;
758 context.put_listent = xfs_attr_put_listent;
760 alist = (struct attrlist *)context.alist;
763 alist->al_offset[0] = context.bufsize;
765 error = xfs_attr_list_int(&context);
771 xfs_attr_inactive(xfs_inode_t *dp)
778 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
780 xfs_ilock(dp, XFS_ILOCK_SHARED);
781 if (!xfs_inode_hasattr(dp) ||
782 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
783 xfs_iunlock(dp, XFS_ILOCK_SHARED);
786 xfs_iunlock(dp, XFS_ILOCK_SHARED);
789 * Start our first transaction of the day.
791 * All future transactions during this code must be "chained" off
792 * this one via the trans_dup() call. All transactions will contain
793 * the inode, and the inode will always be marked with trans_ihold().
794 * Since the inode will be locked in all transactions, we must log
795 * the inode in every transaction to let it float upward through
798 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
799 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
800 XFS_TRANS_PERM_LOG_RES,
801 XFS_ATTRINVAL_LOG_COUNT))) {
802 xfs_trans_cancel(trans, 0);
805 xfs_ilock(dp, XFS_ILOCK_EXCL);
808 * No need to make quota reservations here. We expect to release some
809 * blocks, not allocate, in the common case.
811 xfs_trans_ijoin(trans, dp, 0);
814 * Decide on what work routines to call based on the inode size.
816 if (!xfs_inode_hasattr(dp) ||
817 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
821 error = xfs_attr_root_inactive(&trans, dp);
825 error = xfs_itruncate_extents(&trans, dp, XFS_ATTR_FORK, 0);
829 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
830 xfs_iunlock(dp, XFS_ILOCK_EXCL);
835 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
836 xfs_iunlock(dp, XFS_ILOCK_EXCL);
842 /*========================================================================
843 * External routines when attribute list is inside the inode
844 *========================================================================*/
847 * Add a name to the shortform attribute list structure
848 * This is the external routine.
851 xfs_attr_shortform_addname(xfs_da_args_t *args)
853 int newsize, forkoff, retval;
855 trace_xfs_attr_sf_addname(args);
857 retval = xfs_attr_shortform_lookup(args);
858 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
860 } else if (retval == EEXIST) {
861 if (args->flags & ATTR_CREATE)
863 retval = xfs_attr_shortform_remove(args);
867 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
868 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
869 return(XFS_ERROR(ENOSPC));
871 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
872 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
874 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
876 return(XFS_ERROR(ENOSPC));
878 xfs_attr_shortform_add(args, forkoff);
883 /*========================================================================
884 * External routines when attribute list is one block
885 *========================================================================*/
888 * Add a name to the leaf attribute list structure
890 * This leaf block cannot have a "remote" value, we only call this routine
891 * if bmap_one_block() says there is only one block (ie: no remote blks).
894 xfs_attr_leaf_addname(xfs_da_args_t *args)
898 int retval, error, committed, forkoff;
900 trace_xfs_attr_leaf_addname(args);
903 * Read the (only) block in the attribute list in.
907 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
914 * Look up the given attribute in the leaf block. Figure out if
915 * the given flags produce an error or call for an atomic rename.
917 retval = xfs_attr_leaf_lookup_int(bp, args);
918 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
919 xfs_da_brelse(args->trans, bp);
921 } else if (retval == EEXIST) {
922 if (args->flags & ATTR_CREATE) { /* pure create op */
923 xfs_da_brelse(args->trans, bp);
927 trace_xfs_attr_leaf_replace(args);
929 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
930 args->blkno2 = args->blkno; /* set 2nd entry info*/
931 args->index2 = args->index;
932 args->rmtblkno2 = args->rmtblkno;
933 args->rmtblkcnt2 = args->rmtblkcnt;
937 * Add the attribute to the leaf block, transitioning to a Btree
940 retval = xfs_attr_leaf_add(bp, args);
942 if (retval == ENOSPC) {
944 * Promote the attribute list to the Btree format, then
945 * Commit that transaction so that the node_addname() call
946 * can manage its own transactions.
948 xfs_bmap_init(args->flist, args->firstblock);
949 error = xfs_attr_leaf_to_node(args);
951 error = xfs_bmap_finish(&args->trans, args->flist,
957 xfs_bmap_cancel(args->flist);
962 * bmap_finish() may have committed the last trans and started
963 * a new one. We need the inode to be in all transactions.
966 xfs_trans_ijoin(args->trans, dp, 0);
969 * Commit the current trans (including the inode) and start
972 error = xfs_trans_roll(&args->trans, dp);
977 * Fob the whole rest of the problem off on the Btree code.
979 error = xfs_attr_node_addname(args);
984 * Commit the transaction that added the attr name so that
985 * later routines can manage their own transactions.
987 error = xfs_trans_roll(&args->trans, dp);
992 * If there was an out-of-line value, allocate the blocks we
993 * identified for its storage and copy the value. This is done
994 * after we create the attribute so that we don't overflow the
995 * maximum size of a transaction and/or hit a deadlock.
997 if (args->rmtblkno > 0) {
998 error = xfs_attr_rmtval_set(args);
1004 * If this is an atomic rename operation, we must "flip" the
1005 * incomplete flags on the "new" and "old" attribute/value pairs
1006 * so that one disappears and one appears atomically. Then we
1007 * must remove the "old" attribute/value pair.
1009 if (args->op_flags & XFS_DA_OP_RENAME) {
1011 * In a separate transaction, set the incomplete flag on the
1012 * "old" attr and clear the incomplete flag on the "new" attr.
1014 error = xfs_attr_leaf_flipflags(args);
1019 * Dismantle the "old" attribute/value pair by removing
1020 * a "remote" value (if it exists).
1022 args->index = args->index2;
1023 args->blkno = args->blkno2;
1024 args->rmtblkno = args->rmtblkno2;
1025 args->rmtblkcnt = args->rmtblkcnt2;
1026 if (args->rmtblkno) {
1027 error = xfs_attr_rmtval_remove(args);
1033 * Read in the block containing the "old" attr, then
1034 * remove the "old" attr from that block (neat, huh!)
1036 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1037 &bp, XFS_ATTR_FORK);
1041 (void)xfs_attr_leaf_remove(bp, args);
1044 * If the result is small enough, shrink it all into the inode.
1046 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1047 xfs_bmap_init(args->flist, args->firstblock);
1048 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1049 /* bp is gone due to xfs_da_shrink_inode */
1051 error = xfs_bmap_finish(&args->trans,
1058 xfs_bmap_cancel(args->flist);
1063 * bmap_finish() may have committed the last trans
1064 * and started a new one. We need the inode to be
1065 * in all transactions.
1068 xfs_trans_ijoin(args->trans, dp, 0);
1070 xfs_da_buf_done(bp);
1073 * Commit the remove and start the next trans in series.
1075 error = xfs_trans_roll(&args->trans, dp);
1077 } else if (args->rmtblkno > 0) {
1079 * Added a "remote" value, just clear the incomplete flag.
1081 error = xfs_attr_leaf_clearflag(args);
1087 * Remove a name from the leaf attribute list structure
1089 * This leaf block cannot have a "remote" value, we only call this routine
1090 * if bmap_one_block() says there is only one block (ie: no remote blks).
1093 xfs_attr_leaf_removename(xfs_da_args_t *args)
1097 int error, committed, forkoff;
1099 trace_xfs_attr_leaf_removename(args);
1102 * Remove the attribute.
1106 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1113 error = xfs_attr_leaf_lookup_int(bp, args);
1114 if (error == ENOATTR) {
1115 xfs_da_brelse(args->trans, bp);
1119 (void)xfs_attr_leaf_remove(bp, args);
1122 * If the result is small enough, shrink it all into the inode.
1124 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1125 xfs_bmap_init(args->flist, args->firstblock);
1126 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1127 /* bp is gone due to xfs_da_shrink_inode */
1129 error = xfs_bmap_finish(&args->trans, args->flist,
1135 xfs_bmap_cancel(args->flist);
1140 * bmap_finish() may have committed the last trans and started
1141 * a new one. We need the inode to be in all transactions.
1144 xfs_trans_ijoin(args->trans, dp, 0);
1146 xfs_da_buf_done(bp);
1151 * Look up a name in a leaf attribute list structure.
1153 * This leaf block cannot have a "remote" value, we only call this routine
1154 * if bmap_one_block() says there is only one block (ie: no remote blks).
1157 xfs_attr_leaf_get(xfs_da_args_t *args)
1163 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1169 error = xfs_attr_leaf_lookup_int(bp, args);
1170 if (error != EEXIST) {
1171 xfs_da_brelse(args->trans, bp);
1174 error = xfs_attr_leaf_getvalue(bp, args);
1175 xfs_da_brelse(args->trans, bp);
1176 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1177 error = xfs_attr_rmtval_get(args);
1183 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1186 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1188 xfs_attr_leafblock_t *leaf;
1192 context->cursor->blkno = 0;
1193 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1195 return XFS_ERROR(error);
1198 if (unlikely(leaf->hdr.info.magic != cpu_to_be16(XFS_ATTR_LEAF_MAGIC))) {
1199 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1200 context->dp->i_mount, leaf);
1201 xfs_da_brelse(NULL, bp);
1202 return XFS_ERROR(EFSCORRUPTED);
1205 error = xfs_attr_leaf_list_int(bp, context);
1206 xfs_da_brelse(NULL, bp);
1207 return XFS_ERROR(error);
1211 /*========================================================================
1212 * External routines when attribute list size > XFS_LBSIZE(mp).
1213 *========================================================================*/
1216 * Add a name to a Btree-format attribute list.
1218 * This will involve walking down the Btree, and may involve splitting
1219 * leaf nodes and even splitting intermediate nodes up to and including
1220 * the root node (a special case of an intermediate node).
1222 * "Remote" attribute values confuse the issue and atomic rename operations
1223 * add a whole extra layer of confusion on top of that.
1226 xfs_attr_node_addname(xfs_da_args_t *args)
1228 xfs_da_state_t *state;
1229 xfs_da_state_blk_t *blk;
1232 int committed, retval, error;
1234 trace_xfs_attr_node_addname(args);
1237 * Fill in bucket of arguments/results/context to carry around.
1242 state = xfs_da_state_alloc();
1245 state->blocksize = state->mp->m_sb.sb_blocksize;
1246 state->node_ents = state->mp->m_attr_node_ents;
1249 * Search to see if name already exists, and get back a pointer
1250 * to where it should go.
1252 error = xfs_da_node_lookup_int(state, &retval);
1255 blk = &state->path.blk[ state->path.active-1 ];
1256 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1257 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1259 } else if (retval == EEXIST) {
1260 if (args->flags & ATTR_CREATE)
1263 trace_xfs_attr_node_replace(args);
1265 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1266 args->blkno2 = args->blkno; /* set 2nd entry info*/
1267 args->index2 = args->index;
1268 args->rmtblkno2 = args->rmtblkno;
1269 args->rmtblkcnt2 = args->rmtblkcnt;
1271 args->rmtblkcnt = 0;
1274 retval = xfs_attr_leaf_add(blk->bp, state->args);
1275 if (retval == ENOSPC) {
1276 if (state->path.active == 1) {
1278 * Its really a single leaf node, but it had
1279 * out-of-line values so it looked like it *might*
1280 * have been a b-tree.
1282 xfs_da_state_free(state);
1283 xfs_bmap_init(args->flist, args->firstblock);
1284 error = xfs_attr_leaf_to_node(args);
1286 error = xfs_bmap_finish(&args->trans,
1293 xfs_bmap_cancel(args->flist);
1298 * bmap_finish() may have committed the last trans
1299 * and started a new one. We need the inode to be
1300 * in all transactions.
1303 xfs_trans_ijoin(args->trans, dp, 0);
1306 * Commit the node conversion and start the next
1307 * trans in the chain.
1309 error = xfs_trans_roll(&args->trans, dp);
1317 * Split as many Btree elements as required.
1318 * This code tracks the new and old attr's location
1319 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1320 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1322 xfs_bmap_init(args->flist, args->firstblock);
1323 error = xfs_da_split(state);
1325 error = xfs_bmap_finish(&args->trans, args->flist,
1331 xfs_bmap_cancel(args->flist);
1336 * bmap_finish() may have committed the last trans and started
1337 * a new one. We need the inode to be in all transactions.
1340 xfs_trans_ijoin(args->trans, dp, 0);
1343 * Addition succeeded, update Btree hashvals.
1345 xfs_da_fixhashpath(state, &state->path);
1349 * Kill the state structure, we're done with it and need to
1350 * allow the buffers to come back later.
1352 xfs_da_state_free(state);
1356 * Commit the leaf addition or btree split and start the next
1357 * trans in the chain.
1359 error = xfs_trans_roll(&args->trans, dp);
1364 * If there was an out-of-line value, allocate the blocks we
1365 * identified for its storage and copy the value. This is done
1366 * after we create the attribute so that we don't overflow the
1367 * maximum size of a transaction and/or hit a deadlock.
1369 if (args->rmtblkno > 0) {
1370 error = xfs_attr_rmtval_set(args);
1376 * If this is an atomic rename operation, we must "flip" the
1377 * incomplete flags on the "new" and "old" attribute/value pairs
1378 * so that one disappears and one appears atomically. Then we
1379 * must remove the "old" attribute/value pair.
1381 if (args->op_flags & XFS_DA_OP_RENAME) {
1383 * In a separate transaction, set the incomplete flag on the
1384 * "old" attr and clear the incomplete flag on the "new" attr.
1386 error = xfs_attr_leaf_flipflags(args);
1391 * Dismantle the "old" attribute/value pair by removing
1392 * a "remote" value (if it exists).
1394 args->index = args->index2;
1395 args->blkno = args->blkno2;
1396 args->rmtblkno = args->rmtblkno2;
1397 args->rmtblkcnt = args->rmtblkcnt2;
1398 if (args->rmtblkno) {
1399 error = xfs_attr_rmtval_remove(args);
1405 * Re-find the "old" attribute entry after any split ops.
1406 * The INCOMPLETE flag means that we will find the "old"
1407 * attr, not the "new" one.
1409 args->flags |= XFS_ATTR_INCOMPLETE;
1410 state = xfs_da_state_alloc();
1413 state->blocksize = state->mp->m_sb.sb_blocksize;
1414 state->node_ents = state->mp->m_attr_node_ents;
1416 error = xfs_da_node_lookup_int(state, &retval);
1421 * Remove the name and update the hashvals in the tree.
1423 blk = &state->path.blk[ state->path.active-1 ];
1424 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1425 error = xfs_attr_leaf_remove(blk->bp, args);
1426 xfs_da_fixhashpath(state, &state->path);
1429 * Check to see if the tree needs to be collapsed.
1431 if (retval && (state->path.active > 1)) {
1432 xfs_bmap_init(args->flist, args->firstblock);
1433 error = xfs_da_join(state);
1435 error = xfs_bmap_finish(&args->trans,
1442 xfs_bmap_cancel(args->flist);
1447 * bmap_finish() may have committed the last trans
1448 * and started a new one. We need the inode to be
1449 * in all transactions.
1452 xfs_trans_ijoin(args->trans, dp, 0);
1456 * Commit and start the next trans in the chain.
1458 error = xfs_trans_roll(&args->trans, dp);
1462 } else if (args->rmtblkno > 0) {
1464 * Added a "remote" value, just clear the incomplete flag.
1466 error = xfs_attr_leaf_clearflag(args);
1474 xfs_da_state_free(state);
1481 * Remove a name from a B-tree attribute list.
1483 * This will involve walking down the Btree, and may involve joining
1484 * leaf nodes and even joining intermediate nodes up to and including
1485 * the root node (a special case of an intermediate node).
1488 xfs_attr_node_removename(xfs_da_args_t *args)
1490 xfs_da_state_t *state;
1491 xfs_da_state_blk_t *blk;
1494 int retval, error, committed, forkoff;
1496 trace_xfs_attr_node_removename(args);
1499 * Tie a string around our finger to remind us where we are.
1502 state = xfs_da_state_alloc();
1504 state->mp = dp->i_mount;
1505 state->blocksize = state->mp->m_sb.sb_blocksize;
1506 state->node_ents = state->mp->m_attr_node_ents;
1509 * Search to see if name exists, and get back a pointer to it.
1511 error = xfs_da_node_lookup_int(state, &retval);
1512 if (error || (retval != EEXIST)) {
1519 * If there is an out-of-line value, de-allocate the blocks.
1520 * This is done before we remove the attribute so that we don't
1521 * overflow the maximum size of a transaction and/or hit a deadlock.
1523 blk = &state->path.blk[ state->path.active-1 ];
1524 ASSERT(blk->bp != NULL);
1525 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1526 if (args->rmtblkno > 0) {
1528 * Fill in disk block numbers in the state structure
1529 * so that we can get the buffers back after we commit
1530 * several transactions in the following calls.
1532 error = xfs_attr_fillstate(state);
1537 * Mark the attribute as INCOMPLETE, then bunmapi() the
1540 error = xfs_attr_leaf_setflag(args);
1543 error = xfs_attr_rmtval_remove(args);
1548 * Refill the state structure with buffers, the prior calls
1549 * released our buffers.
1551 error = xfs_attr_refillstate(state);
1557 * Remove the name and update the hashvals in the tree.
1559 blk = &state->path.blk[ state->path.active-1 ];
1560 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1561 retval = xfs_attr_leaf_remove(blk->bp, args);
1562 xfs_da_fixhashpath(state, &state->path);
1565 * Check to see if the tree needs to be collapsed.
1567 if (retval && (state->path.active > 1)) {
1568 xfs_bmap_init(args->flist, args->firstblock);
1569 error = xfs_da_join(state);
1571 error = xfs_bmap_finish(&args->trans, args->flist,
1577 xfs_bmap_cancel(args->flist);
1582 * bmap_finish() may have committed the last trans and started
1583 * a new one. We need the inode to be in all transactions.
1586 xfs_trans_ijoin(args->trans, dp, 0);
1589 * Commit the Btree join operation and start a new trans.
1591 error = xfs_trans_roll(&args->trans, dp);
1597 * If the result is small enough, push it all into the inode.
1599 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1601 * Have to get rid of the copy of this dabuf in the state.
1603 ASSERT(state->path.active == 1);
1604 ASSERT(state->path.blk[0].bp);
1605 xfs_da_buf_done(state->path.blk[0].bp);
1606 state->path.blk[0].bp = NULL;
1608 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1612 ASSERT((((xfs_attr_leafblock_t *)bp->data)->hdr.info.magic) ==
1613 cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
1615 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1616 xfs_bmap_init(args->flist, args->firstblock);
1617 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1618 /* bp is gone due to xfs_da_shrink_inode */
1620 error = xfs_bmap_finish(&args->trans,
1627 xfs_bmap_cancel(args->flist);
1632 * bmap_finish() may have committed the last trans
1633 * and started a new one. We need the inode to be
1634 * in all transactions.
1637 xfs_trans_ijoin(args->trans, dp, 0);
1639 xfs_da_brelse(args->trans, bp);
1644 xfs_da_state_free(state);
1649 * Fill in the disk block numbers in the state structure for the buffers
1650 * that are attached to the state structure.
1651 * This is done so that we can quickly reattach ourselves to those buffers
1652 * after some set of transaction commits have released these buffers.
1655 xfs_attr_fillstate(xfs_da_state_t *state)
1657 xfs_da_state_path_t *path;
1658 xfs_da_state_blk_t *blk;
1662 * Roll down the "path" in the state structure, storing the on-disk
1663 * block number for those buffers in the "path".
1665 path = &state->path;
1666 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1667 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1669 blk->disk_blkno = xfs_da_blkno(blk->bp);
1670 xfs_da_buf_done(blk->bp);
1673 blk->disk_blkno = 0;
1678 * Roll down the "altpath" in the state structure, storing the on-disk
1679 * block number for those buffers in the "altpath".
1681 path = &state->altpath;
1682 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1683 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1685 blk->disk_blkno = xfs_da_blkno(blk->bp);
1686 xfs_da_buf_done(blk->bp);
1689 blk->disk_blkno = 0;
1697 * Reattach the buffers to the state structure based on the disk block
1698 * numbers stored in the state structure.
1699 * This is done after some set of transaction commits have released those
1700 * buffers from our grip.
1703 xfs_attr_refillstate(xfs_da_state_t *state)
1705 xfs_da_state_path_t *path;
1706 xfs_da_state_blk_t *blk;
1710 * Roll down the "path" in the state structure, storing the on-disk
1711 * block number for those buffers in the "path".
1713 path = &state->path;
1714 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1715 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1716 if (blk->disk_blkno) {
1717 error = xfs_da_read_buf(state->args->trans,
1719 blk->blkno, blk->disk_blkno,
1720 &blk->bp, XFS_ATTR_FORK);
1729 * Roll down the "altpath" in the state structure, storing the on-disk
1730 * block number for those buffers in the "altpath".
1732 path = &state->altpath;
1733 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1734 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1735 if (blk->disk_blkno) {
1736 error = xfs_da_read_buf(state->args->trans,
1738 blk->blkno, blk->disk_blkno,
1739 &blk->bp, XFS_ATTR_FORK);
1751 * Look up a filename in a node attribute list.
1753 * This routine gets called for any attribute fork that has more than one
1754 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1755 * "remote" values taking up more blocks.
1758 xfs_attr_node_get(xfs_da_args_t *args)
1760 xfs_da_state_t *state;
1761 xfs_da_state_blk_t *blk;
1765 state = xfs_da_state_alloc();
1767 state->mp = args->dp->i_mount;
1768 state->blocksize = state->mp->m_sb.sb_blocksize;
1769 state->node_ents = state->mp->m_attr_node_ents;
1772 * Search to see if name exists, and get back a pointer to it.
1774 error = xfs_da_node_lookup_int(state, &retval);
1777 } else if (retval == EEXIST) {
1778 blk = &state->path.blk[ state->path.active-1 ];
1779 ASSERT(blk->bp != NULL);
1780 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1783 * Get the value, local or "remote"
1785 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1786 if (!retval && (args->rmtblkno > 0)
1787 && !(args->flags & ATTR_KERNOVAL)) {
1788 retval = xfs_attr_rmtval_get(args);
1793 * If not in a transaction, we have to release all the buffers.
1795 for (i = 0; i < state->path.active; i++) {
1796 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1797 state->path.blk[i].bp = NULL;
1800 xfs_da_state_free(state);
1804 STATIC int /* error */
1805 xfs_attr_node_list(xfs_attr_list_context_t *context)
1807 attrlist_cursor_kern_t *cursor;
1808 xfs_attr_leafblock_t *leaf;
1809 xfs_da_intnode_t *node;
1810 xfs_da_node_entry_t *btree;
1814 cursor = context->cursor;
1815 cursor->initted = 1;
1818 * Do all sorts of validation on the passed-in cursor structure.
1819 * If anything is amiss, ignore the cursor and look up the hashval
1820 * starting from the btree root.
1823 if (cursor->blkno > 0) {
1824 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1825 &bp, XFS_ATTR_FORK);
1826 if ((error != 0) && (error != EFSCORRUPTED))
1830 switch (be16_to_cpu(node->hdr.info.magic)) {
1831 case XFS_DA_NODE_MAGIC:
1832 trace_xfs_attr_list_wrong_blk(context);
1833 xfs_da_brelse(NULL, bp);
1836 case XFS_ATTR_LEAF_MAGIC:
1838 if (cursor->hashval > be32_to_cpu(leaf->entries[
1839 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1840 trace_xfs_attr_list_wrong_blk(context);
1841 xfs_da_brelse(NULL, bp);
1843 } else if (cursor->hashval <=
1844 be32_to_cpu(leaf->entries[0].hashval)) {
1845 trace_xfs_attr_list_wrong_blk(context);
1846 xfs_da_brelse(NULL, bp);
1851 trace_xfs_attr_list_wrong_blk(context);
1852 xfs_da_brelse(NULL, bp);
1859 * We did not find what we expected given the cursor's contents,
1860 * so we start from the top and work down based on the hash value.
1861 * Note that start of node block is same as start of leaf block.
1866 error = xfs_da_read_buf(NULL, context->dp,
1867 cursor->blkno, -1, &bp,
1871 if (unlikely(bp == NULL)) {
1872 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1874 context->dp->i_mount);
1875 return(XFS_ERROR(EFSCORRUPTED));
1878 if (node->hdr.info.magic ==
1879 cpu_to_be16(XFS_ATTR_LEAF_MAGIC))
1881 if (unlikely(node->hdr.info.magic !=
1882 cpu_to_be16(XFS_DA_NODE_MAGIC))) {
1883 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1885 context->dp->i_mount,
1887 xfs_da_brelse(NULL, bp);
1888 return(XFS_ERROR(EFSCORRUPTED));
1890 btree = node->btree;
1891 for (i = 0; i < be16_to_cpu(node->hdr.count);
1894 <= be32_to_cpu(btree->hashval)) {
1895 cursor->blkno = be32_to_cpu(btree->before);
1896 trace_xfs_attr_list_node_descend(context,
1901 if (i == be16_to_cpu(node->hdr.count)) {
1902 xfs_da_brelse(NULL, bp);
1905 xfs_da_brelse(NULL, bp);
1911 * Roll upward through the blocks, processing each leaf block in
1912 * order. As long as there is space in the result buffer, keep
1913 * adding the information.
1917 if (unlikely(leaf->hdr.info.magic !=
1918 cpu_to_be16(XFS_ATTR_LEAF_MAGIC))) {
1919 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1921 context->dp->i_mount, leaf);
1922 xfs_da_brelse(NULL, bp);
1923 return(XFS_ERROR(EFSCORRUPTED));
1925 error = xfs_attr_leaf_list_int(bp, context);
1927 xfs_da_brelse(NULL, bp);
1930 if (context->seen_enough || leaf->hdr.info.forw == 0)
1932 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1933 xfs_da_brelse(NULL, bp);
1934 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1935 &bp, XFS_ATTR_FORK);
1938 if (unlikely((bp == NULL))) {
1939 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1941 context->dp->i_mount);
1942 return(XFS_ERROR(EFSCORRUPTED));
1945 xfs_da_brelse(NULL, bp);
1950 /*========================================================================
1951 * External routines for manipulating out-of-line attribute values.
1952 *========================================================================*/
1955 * Read the value associated with an attribute from the out-of-line buffer
1956 * that we stored it in.
1959 xfs_attr_rmtval_get(xfs_da_args_t *args)
1961 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1966 int nmap, error, tmp, valuelen, blkcnt, i;
1969 ASSERT(!(args->flags & ATTR_KERNOVAL));
1971 mp = args->dp->i_mount;
1973 valuelen = args->valuelen;
1974 lblkno = args->rmtblkno;
1975 while (valuelen > 0) {
1976 nmap = ATTR_RMTVALUE_MAPSIZE;
1977 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
1978 args->rmtblkcnt, map, &nmap,
1979 XFS_BMAPI_ATTRFORK);
1984 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
1985 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
1986 (map[i].br_startblock != HOLESTARTBLOCK));
1987 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
1988 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
1989 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
1990 dblkno, blkcnt, 0, &bp);
1994 tmp = min_t(int, valuelen, BBTOB(bp->b_length));
1995 xfs_buf_iomove(bp, 0, tmp, dst, XBRW_READ);
2000 lblkno += map[i].br_blockcount;
2003 ASSERT(valuelen == 0);
2008 * Write the value associated with an attribute into the out-of-line buffer
2009 * that we have defined for it.
2012 xfs_attr_rmtval_set(xfs_da_args_t *args)
2015 xfs_fileoff_t lfileoff;
2017 xfs_bmbt_irec_t map;
2022 int blkcnt, valuelen, nmap, error, tmp, committed;
2029 * Find a "hole" in the attribute address space large enough for
2030 * us to drop the new attribute's value into.
2032 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2034 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2039 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2040 args->rmtblkcnt = blkcnt;
2043 * Roll through the "value", allocating blocks on disk as required.
2045 while (blkcnt > 0) {
2047 * Allocate a single extent, up to the size of the value.
2049 xfs_bmap_init(args->flist, args->firstblock);
2051 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
2053 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2054 args->firstblock, args->total, &map, &nmap,
2057 error = xfs_bmap_finish(&args->trans, args->flist,
2063 xfs_bmap_cancel(args->flist);
2068 * bmap_finish() may have committed the last trans and started
2069 * a new one. We need the inode to be in all transactions.
2072 xfs_trans_ijoin(args->trans, dp, 0);
2075 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2076 (map.br_startblock != HOLESTARTBLOCK));
2077 lblkno += map.br_blockcount;
2078 blkcnt -= map.br_blockcount;
2081 * Start the next trans in the chain.
2083 error = xfs_trans_roll(&args->trans, dp);
2089 * Roll through the "value", copying the attribute value to the
2090 * already-allocated blocks. Blocks are written synchronously
2091 * so that we can know they are all on disk before we turn off
2092 * the INCOMPLETE flag.
2094 lblkno = args->rmtblkno;
2095 valuelen = args->valuelen;
2096 while (valuelen > 0) {
2100 * Try to remember where we decided to put the value.
2102 xfs_bmap_init(args->flist, args->firstblock);
2104 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
2105 args->rmtblkcnt, &map, &nmap,
2106 XFS_BMAPI_ATTRFORK);
2110 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2111 (map.br_startblock != HOLESTARTBLOCK));
2113 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2114 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2116 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
2120 buflen = BBTOB(bp->b_length);
2121 tmp = min_t(int, valuelen, buflen);
2122 xfs_buf_iomove(bp, 0, tmp, src, XBRW_WRITE);
2124 xfs_buf_zero(bp, tmp, buflen - tmp);
2126 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
2133 lblkno += map.br_blockcount;
2135 ASSERT(valuelen == 0);
2140 * Remove the value associated with an attribute by deleting the
2141 * out-of-line buffer that it is stored on.
2144 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2147 xfs_bmbt_irec_t map;
2151 int valuelen, blkcnt, nmap, error, done, committed;
2153 mp = args->dp->i_mount;
2156 * Roll through the "value", invalidating the attribute value's
2159 lblkno = args->rmtblkno;
2160 valuelen = args->rmtblkcnt;
2161 while (valuelen > 0) {
2163 * Try to remember where we decided to put the value.
2166 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
2167 args->rmtblkcnt, &map, &nmap,
2168 XFS_BMAPI_ATTRFORK);
2172 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2173 (map.br_startblock != HOLESTARTBLOCK));
2175 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2176 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2179 * If the "remote" value is in the cache, remove it.
2181 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
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, 0);
2224 * Close out trans and start the next one in the chain.
2226 error = xfs_trans_roll(&args->trans, args->dp);