2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_alloc.h"
40 #include "xfs_btree.h"
41 #include "xfs_inode_item.h"
44 #include "xfs_attr_leaf.h"
45 #include "xfs_error.h"
46 #include "xfs_quota.h"
47 #include "xfs_trans_space.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
55 * Provide the external interfaces to manage attribute lists.
58 /*========================================================================
59 * Function prototypes for the kernel.
60 *========================================================================*/
63 * Internal routines when attribute list fits inside the inode.
65 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
68 * Internal routines when attribute list is one block.
70 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
71 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
72 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
73 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
76 * Internal routines when attribute list is more than one block.
78 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
79 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
80 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
81 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
82 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
83 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
86 * Routines to manipulate out-of-line attribute values.
88 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
89 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
91 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
94 xfs_attr_name_to_xname(
95 struct xfs_name *xname,
96 const unsigned char *aname)
101 xname->len = strlen((char *)aname);
102 if (xname->len >= MAXNAMELEN)
103 return EFAULT; /* match IRIX behaviour */
110 struct xfs_inode *ip)
112 if (!XFS_IFORK_Q(ip) ||
113 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
114 ip->i_d.di_anextents == 0))
119 /*========================================================================
120 * Overall external interface routines.
121 *========================================================================*/
125 struct xfs_inode *ip,
126 struct xfs_name *name,
127 unsigned char *value,
134 if (!xfs_inode_hasattr(ip))
138 * Fill in the arg structure for this request.
140 memset((char *)&args, 0, sizeof(args));
141 args.name = name->name;
142 args.namelen = name->len;
144 args.valuelen = *valuelenp;
146 args.hashval = xfs_da_hashname(args.name, args.namelen);
148 args.whichfork = XFS_ATTR_FORK;
151 * Decide on what work routines to call based on the inode size.
153 if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
154 error = xfs_attr_shortform_getvalue(&args);
155 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
156 error = xfs_attr_leaf_get(&args);
158 error = xfs_attr_node_get(&args);
162 * Return the number of bytes in the value to the caller.
164 *valuelenp = args.valuelen;
174 const unsigned char *name,
175 unsigned char *value,
180 struct xfs_name xname;
182 XFS_STATS_INC(xs_attr_get);
184 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
187 error = xfs_attr_name_to_xname(&xname, name);
191 xfs_ilock(ip, XFS_ILOCK_SHARED);
192 error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
193 xfs_iunlock(ip, XFS_ILOCK_SHARED);
198 * Calculate how many blocks we need for the new attribute,
202 struct xfs_inode *ip,
207 struct xfs_mount *mp = ip->i_mount;
212 * Determine space new attribute will use, and if it would be
213 * "local" or "remote" (note: local != inline).
215 size = xfs_attr_leaf_newentsize(namelen, valuelen,
216 mp->m_sb.sb_blocksize, local);
218 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
220 if (size > (mp->m_sb.sb_blocksize >> 1)) {
221 /* Double split possible */
226 * Out of line attribute, cannot double split, but
227 * make room for the attribute value itself.
229 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
231 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
239 struct xfs_inode *dp,
240 struct xfs_name *name,
241 unsigned char *value,
246 xfs_fsblock_t firstblock;
247 xfs_bmap_free_t flist;
248 int error, err2, committed;
249 xfs_mount_t *mp = dp->i_mount;
250 int rsvd = (flags & ATTR_ROOT) != 0;
254 * Attach the dquots to the inode.
256 error = xfs_qm_dqattach(dp, 0);
261 * If the inode doesn't have an attribute fork, add one.
262 * (inode must not be locked when we call this routine)
264 if (XFS_IFORK_Q(dp) == 0) {
265 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
266 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
268 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
273 * Fill in the arg structure for this request.
275 memset((char *)&args, 0, sizeof(args));
276 args.name = name->name;
277 args.namelen = name->len;
279 args.valuelen = valuelen;
281 args.hashval = xfs_da_hashname(args.name, args.namelen);
283 args.firstblock = &firstblock;
285 args.whichfork = XFS_ATTR_FORK;
286 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
288 /* Size is now blocks for attribute data */
289 args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
292 * Start our first transaction of the day.
294 * All future transactions during this code must be "chained" off
295 * this one via the trans_dup() call. All transactions will contain
296 * the inode, and the inode will always be marked with trans_ihold().
297 * Since the inode will be locked in all transactions, we must log
298 * the inode in every transaction to let it float upward through
301 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
304 * Root fork attributes can use reserved data blocks for this
305 * operation if necessary
309 args.trans->t_flags |= XFS_TRANS_RESERVE;
311 if ((error = xfs_trans_reserve(args.trans, args.total,
312 XFS_ATTRSET_LOG_RES(mp, args.total), 0,
313 XFS_TRANS_PERM_LOG_RES, XFS_ATTRSET_LOG_COUNT))) {
314 xfs_trans_cancel(args.trans, 0);
317 xfs_ilock(dp, XFS_ILOCK_EXCL);
319 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
320 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
321 XFS_QMOPT_RES_REGBLKS);
323 xfs_iunlock(dp, XFS_ILOCK_EXCL);
324 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
328 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
329 xfs_trans_ihold(args.trans, dp);
332 * If the attribute list is non-existent or a shortform list,
333 * upgrade it to a single-leaf-block attribute list.
335 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
336 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
337 (dp->i_d.di_anextents == 0))) {
340 * Build initial attribute list (if required).
342 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
343 xfs_attr_shortform_create(&args);
346 * Try to add the attr to the attribute list in
349 error = xfs_attr_shortform_addname(&args);
350 if (error != ENOSPC) {
352 * Commit the shortform mods, and we're done.
353 * NOTE: this is also the error path (EEXIST, etc).
355 ASSERT(args.trans != NULL);
358 * If this is a synchronous mount, make sure that
359 * the transaction goes to disk before returning
362 if (mp->m_flags & XFS_MOUNT_WSYNC) {
363 xfs_trans_set_sync(args.trans);
365 err2 = xfs_trans_commit(args.trans,
366 XFS_TRANS_RELEASE_LOG_RES);
367 xfs_iunlock(dp, XFS_ILOCK_EXCL);
370 * Hit the inode change time.
372 if (!error && (flags & ATTR_KERNOTIME) == 0) {
373 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
375 return(error == 0 ? err2 : error);
379 * It won't fit in the shortform, transform to a leaf block.
380 * GROT: another possible req'mt for a double-split btree op.
382 xfs_bmap_init(args.flist, args.firstblock);
383 error = xfs_attr_shortform_to_leaf(&args);
385 error = xfs_bmap_finish(&args.trans, args.flist,
391 xfs_bmap_cancel(&flist);
396 * bmap_finish() may have committed the last trans and started
397 * a new one. We need the inode to be in all transactions.
400 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
401 xfs_trans_ihold(args.trans, dp);
405 * Commit the leaf transformation. We'll need another (linked)
406 * transaction to add the new attribute to the leaf.
409 error = xfs_trans_roll(&args.trans, dp);
415 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
416 error = xfs_attr_leaf_addname(&args);
418 error = xfs_attr_node_addname(&args);
425 * If this is a synchronous mount, make sure that the
426 * transaction goes to disk before returning to the user.
428 if (mp->m_flags & XFS_MOUNT_WSYNC) {
429 xfs_trans_set_sync(args.trans);
433 * Commit the last in the sequence of transactions.
435 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
436 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
437 xfs_iunlock(dp, XFS_ILOCK_EXCL);
440 * Hit the inode change time.
442 if (!error && (flags & ATTR_KERNOTIME) == 0) {
443 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
450 xfs_trans_cancel(args.trans,
451 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
452 xfs_iunlock(dp, XFS_ILOCK_EXCL);
459 const unsigned char *name,
460 unsigned char *value,
465 struct xfs_name xname;
467 XFS_STATS_INC(xs_attr_set);
469 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
472 error = xfs_attr_name_to_xname(&xname, name);
476 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
480 * Generic handler routine to remove a name from an attribute list.
481 * Transitions attribute list from Btree to shortform as necessary.
484 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
487 xfs_fsblock_t firstblock;
488 xfs_bmap_free_t flist;
490 xfs_mount_t *mp = dp->i_mount;
493 * Fill in the arg structure for this request.
495 memset((char *)&args, 0, sizeof(args));
496 args.name = name->name;
497 args.namelen = name->len;
499 args.hashval = xfs_da_hashname(args.name, args.namelen);
501 args.firstblock = &firstblock;
504 args.whichfork = XFS_ATTR_FORK;
507 * Attach the dquots to the inode.
509 error = xfs_qm_dqattach(dp, 0);
514 * Start our first transaction of the day.
516 * All future transactions during this code must be "chained" off
517 * this one via the trans_dup() call. All transactions will contain
518 * the inode, and the inode will always be marked with trans_ihold().
519 * Since the inode will be locked in all transactions, we must log
520 * the inode in every transaction to let it float upward through
523 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
526 * Root fork attributes can use reserved data blocks for this
527 * operation if necessary
530 if (flags & ATTR_ROOT)
531 args.trans->t_flags |= XFS_TRANS_RESERVE;
533 if ((error = xfs_trans_reserve(args.trans,
534 XFS_ATTRRM_SPACE_RES(mp),
535 XFS_ATTRRM_LOG_RES(mp),
536 0, XFS_TRANS_PERM_LOG_RES,
537 XFS_ATTRRM_LOG_COUNT))) {
538 xfs_trans_cancel(args.trans, 0);
542 xfs_ilock(dp, XFS_ILOCK_EXCL);
544 * No need to make quota reservations here. We expect to release some
545 * blocks not allocate in the common case.
547 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
548 xfs_trans_ihold(args.trans, dp);
551 * Decide on what work routines to call based on the inode size.
553 if (!xfs_inode_hasattr(dp)) {
554 error = XFS_ERROR(ENOATTR);
557 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
558 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
559 error = xfs_attr_shortform_remove(&args);
563 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
564 error = xfs_attr_leaf_removename(&args);
566 error = xfs_attr_node_removename(&args);
573 * If this is a synchronous mount, make sure that the
574 * transaction goes to disk before returning to the user.
576 if (mp->m_flags & XFS_MOUNT_WSYNC) {
577 xfs_trans_set_sync(args.trans);
581 * Commit the last in the sequence of transactions.
583 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
584 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
585 xfs_iunlock(dp, XFS_ILOCK_EXCL);
588 * Hit the inode change time.
590 if (!error && (flags & ATTR_KERNOTIME) == 0) {
591 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
598 xfs_trans_cancel(args.trans,
599 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
600 xfs_iunlock(dp, XFS_ILOCK_EXCL);
607 const unsigned char *name,
611 struct xfs_name xname;
613 XFS_STATS_INC(xs_attr_remove);
615 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
618 error = xfs_attr_name_to_xname(&xname, name);
622 xfs_ilock(dp, XFS_ILOCK_SHARED);
623 if (!xfs_inode_hasattr(dp)) {
624 xfs_iunlock(dp, XFS_ILOCK_SHARED);
625 return XFS_ERROR(ENOATTR);
627 xfs_iunlock(dp, XFS_ILOCK_SHARED);
629 return xfs_attr_remove_int(dp, &xname, flags);
633 xfs_attr_list_int(xfs_attr_list_context_t *context)
636 xfs_inode_t *dp = context->dp;
638 XFS_STATS_INC(xs_attr_list);
640 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
643 xfs_ilock(dp, XFS_ILOCK_SHARED);
646 * Decide on what work routines to call based on the inode size.
648 if (!xfs_inode_hasattr(dp)) {
650 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
651 error = xfs_attr_shortform_list(context);
652 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
653 error = xfs_attr_leaf_list(context);
655 error = xfs_attr_node_list(context);
658 xfs_iunlock(dp, XFS_ILOCK_SHARED);
663 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
664 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
665 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
666 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
667 & ~(sizeof(u_int32_t)-1))
670 * Format an attribute and copy it out to the user's buffer.
671 * Take care to check values and protect against them changing later,
672 * we may be reading them directly out of a user buffer.
676 xfs_attr_put_listent(
677 xfs_attr_list_context_t *context,
682 unsigned char *value)
684 struct attrlist *alist = (struct attrlist *)context->alist;
688 ASSERT(!(context->flags & ATTR_KERNOVAL));
689 ASSERT(context->count >= 0);
690 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
691 ASSERT(context->firstu >= sizeof(*alist));
692 ASSERT(context->firstu <= context->bufsize);
695 * Only list entries in the right namespace.
697 if (((context->flags & ATTR_SECURE) == 0) !=
698 ((flags & XFS_ATTR_SECURE) == 0))
700 if (((context->flags & ATTR_ROOT) == 0) !=
701 ((flags & XFS_ATTR_ROOT) == 0))
704 arraytop = sizeof(*alist) +
705 context->count * sizeof(alist->al_offset[0]);
706 context->firstu -= ATTR_ENTSIZE(namelen);
707 if (context->firstu < arraytop) {
708 trace_xfs_attr_list_full(context);
710 context->seen_enough = 1;
714 aep = (attrlist_ent_t *)&context->alist[context->firstu];
715 aep->a_valuelen = valuelen;
716 memcpy(aep->a_name, name, namelen);
717 aep->a_name[namelen] = 0;
718 alist->al_offset[context->count++] = context->firstu;
719 alist->al_count = context->count;
720 trace_xfs_attr_list_add(context);
725 * Generate a list of extended attribute names and optionally
726 * also value lengths. Positive return value follows the XFS
727 * convention of being an error, zero or negative return code
728 * is the length of the buffer returned (negated), indicating
737 attrlist_cursor_kern_t *cursor)
739 xfs_attr_list_context_t context;
740 struct attrlist *alist;
744 * Validate the cursor.
746 if (cursor->pad1 || cursor->pad2)
747 return(XFS_ERROR(EINVAL));
748 if ((cursor->initted == 0) &&
749 (cursor->hashval || cursor->blkno || cursor->offset))
750 return XFS_ERROR(EINVAL);
753 * Check for a properly aligned buffer.
755 if (((long)buffer) & (sizeof(int)-1))
756 return XFS_ERROR(EFAULT);
757 if (flags & ATTR_KERNOVAL)
761 * Initialize the output buffer.
763 memset(&context, 0, sizeof(context));
765 context.cursor = cursor;
767 context.flags = flags;
768 context.alist = buffer;
769 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
770 context.firstu = context.bufsize;
771 context.put_listent = xfs_attr_put_listent;
773 alist = (struct attrlist *)context.alist;
776 alist->al_offset[0] = context.bufsize;
778 error = xfs_attr_list_int(&context);
784 xfs_attr_inactive(xfs_inode_t *dp)
791 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
793 xfs_ilock(dp, XFS_ILOCK_SHARED);
794 if (!xfs_inode_hasattr(dp) ||
795 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
796 xfs_iunlock(dp, XFS_ILOCK_SHARED);
799 xfs_iunlock(dp, XFS_ILOCK_SHARED);
802 * Start our first transaction of the day.
804 * All future transactions during this code must be "chained" off
805 * this one via the trans_dup() call. All transactions will contain
806 * the inode, and the inode will always be marked with trans_ihold().
807 * Since the inode will be locked in all transactions, we must log
808 * the inode in every transaction to let it float upward through
811 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
812 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
813 XFS_TRANS_PERM_LOG_RES,
814 XFS_ATTRINVAL_LOG_COUNT))) {
815 xfs_trans_cancel(trans, 0);
818 xfs_ilock(dp, XFS_ILOCK_EXCL);
821 * No need to make quota reservations here. We expect to release some
822 * blocks, not allocate, in the common case.
824 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
825 xfs_trans_ihold(trans, dp);
828 * Decide on what work routines to call based on the inode size.
830 if (!xfs_inode_hasattr(dp) ||
831 dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
835 error = xfs_attr_root_inactive(&trans, dp);
839 * signal synchronous inactive transactions unless this
840 * is a synchronous mount filesystem in which case we
841 * know that we're here because we've been called out of
842 * xfs_inactive which means that the last reference is gone
843 * and the unlink transaction has already hit the disk so
844 * async inactive transactions are safe.
846 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
847 (!(mp->m_flags & XFS_MOUNT_WSYNC)
852 * Commit the last in the sequence of transactions.
854 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
855 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
856 xfs_iunlock(dp, XFS_ILOCK_EXCL);
861 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
862 xfs_iunlock(dp, XFS_ILOCK_EXCL);
868 /*========================================================================
869 * External routines when attribute list is inside the inode
870 *========================================================================*/
873 * Add a name to the shortform attribute list structure
874 * This is the external routine.
877 xfs_attr_shortform_addname(xfs_da_args_t *args)
879 int newsize, forkoff, retval;
881 retval = xfs_attr_shortform_lookup(args);
882 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
884 } else if (retval == EEXIST) {
885 if (args->flags & ATTR_CREATE)
887 retval = xfs_attr_shortform_remove(args);
891 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
892 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
893 return(XFS_ERROR(ENOSPC));
895 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
896 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
898 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
900 return(XFS_ERROR(ENOSPC));
902 xfs_attr_shortform_add(args, forkoff);
907 /*========================================================================
908 * External routines when attribute list is one block
909 *========================================================================*/
912 * Add a name to the leaf attribute list structure
914 * This leaf block cannot have a "remote" value, we only call this routine
915 * if bmap_one_block() says there is only one block (ie: no remote blks).
918 xfs_attr_leaf_addname(xfs_da_args_t *args)
922 int retval, error, committed, forkoff;
925 * Read the (only) block in the attribute list in.
929 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
936 * Look up the given attribute in the leaf block. Figure out if
937 * the given flags produce an error or call for an atomic rename.
939 retval = xfs_attr_leaf_lookup_int(bp, args);
940 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
941 xfs_da_brelse(args->trans, bp);
943 } else if (retval == EEXIST) {
944 if (args->flags & ATTR_CREATE) { /* pure create op */
945 xfs_da_brelse(args->trans, bp);
948 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
949 args->blkno2 = args->blkno; /* set 2nd entry info*/
950 args->index2 = args->index;
951 args->rmtblkno2 = args->rmtblkno;
952 args->rmtblkcnt2 = args->rmtblkcnt;
956 * Add the attribute to the leaf block, transitioning to a Btree
959 retval = xfs_attr_leaf_add(bp, args);
961 if (retval == ENOSPC) {
963 * Promote the attribute list to the Btree format, then
964 * Commit that transaction so that the node_addname() call
965 * can manage its own transactions.
967 xfs_bmap_init(args->flist, args->firstblock);
968 error = xfs_attr_leaf_to_node(args);
970 error = xfs_bmap_finish(&args->trans, args->flist,
976 xfs_bmap_cancel(args->flist);
981 * bmap_finish() may have committed the last trans and started
982 * a new one. We need the inode to be in all transactions.
985 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
986 xfs_trans_ihold(args->trans, dp);
990 * Commit the current trans (including the inode) and start
993 error = xfs_trans_roll(&args->trans, dp);
998 * Fob the whole rest of the problem off on the Btree code.
1000 error = xfs_attr_node_addname(args);
1005 * Commit the transaction that added the attr name so that
1006 * later routines can manage their own transactions.
1008 error = xfs_trans_roll(&args->trans, dp);
1013 * If there was an out-of-line value, allocate the blocks we
1014 * identified for its storage and copy the value. This is done
1015 * after we create the attribute so that we don't overflow the
1016 * maximum size of a transaction and/or hit a deadlock.
1018 if (args->rmtblkno > 0) {
1019 error = xfs_attr_rmtval_set(args);
1025 * If this is an atomic rename operation, we must "flip" the
1026 * incomplete flags on the "new" and "old" attribute/value pairs
1027 * so that one disappears and one appears atomically. Then we
1028 * must remove the "old" attribute/value pair.
1030 if (args->op_flags & XFS_DA_OP_RENAME) {
1032 * In a separate transaction, set the incomplete flag on the
1033 * "old" attr and clear the incomplete flag on the "new" attr.
1035 error = xfs_attr_leaf_flipflags(args);
1040 * Dismantle the "old" attribute/value pair by removing
1041 * a "remote" value (if it exists).
1043 args->index = args->index2;
1044 args->blkno = args->blkno2;
1045 args->rmtblkno = args->rmtblkno2;
1046 args->rmtblkcnt = args->rmtblkcnt2;
1047 if (args->rmtblkno) {
1048 error = xfs_attr_rmtval_remove(args);
1054 * Read in the block containing the "old" attr, then
1055 * remove the "old" attr from that block (neat, huh!)
1057 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1058 &bp, XFS_ATTR_FORK);
1062 (void)xfs_attr_leaf_remove(bp, args);
1065 * If the result is small enough, shrink it all into the inode.
1067 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1068 xfs_bmap_init(args->flist, args->firstblock);
1069 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1070 /* bp is gone due to xfs_da_shrink_inode */
1072 error = xfs_bmap_finish(&args->trans,
1079 xfs_bmap_cancel(args->flist);
1084 * bmap_finish() may have committed the last trans
1085 * and started a new one. We need the inode to be
1086 * in all transactions.
1089 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1090 xfs_trans_ihold(args->trans, dp);
1093 xfs_da_buf_done(bp);
1096 * Commit the remove and start the next trans in series.
1098 error = xfs_trans_roll(&args->trans, dp);
1100 } else if (args->rmtblkno > 0) {
1102 * Added a "remote" value, just clear the incomplete flag.
1104 error = xfs_attr_leaf_clearflag(args);
1110 * Remove a name from the leaf attribute list structure
1112 * This leaf block cannot have a "remote" value, we only call this routine
1113 * if bmap_one_block() says there is only one block (ie: no remote blks).
1116 xfs_attr_leaf_removename(xfs_da_args_t *args)
1120 int error, committed, forkoff;
1123 * Remove the attribute.
1127 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1134 error = xfs_attr_leaf_lookup_int(bp, args);
1135 if (error == ENOATTR) {
1136 xfs_da_brelse(args->trans, bp);
1140 (void)xfs_attr_leaf_remove(bp, args);
1143 * If the result is small enough, shrink it all into the inode.
1145 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1146 xfs_bmap_init(args->flist, args->firstblock);
1147 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1148 /* bp is gone due to xfs_da_shrink_inode */
1150 error = xfs_bmap_finish(&args->trans, args->flist,
1156 xfs_bmap_cancel(args->flist);
1161 * bmap_finish() may have committed the last trans and started
1162 * a new one. We need the inode to be in all transactions.
1165 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1166 xfs_trans_ihold(args->trans, dp);
1169 xfs_da_buf_done(bp);
1174 * Look up a name in a leaf attribute list structure.
1176 * This leaf block cannot have a "remote" value, we only call this routine
1177 * if bmap_one_block() says there is only one block (ie: no remote blks).
1180 xfs_attr_leaf_get(xfs_da_args_t *args)
1186 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1192 error = xfs_attr_leaf_lookup_int(bp, args);
1193 if (error != EEXIST) {
1194 xfs_da_brelse(args->trans, bp);
1197 error = xfs_attr_leaf_getvalue(bp, args);
1198 xfs_da_brelse(args->trans, bp);
1199 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1200 error = xfs_attr_rmtval_get(args);
1206 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1209 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1211 xfs_attr_leafblock_t *leaf;
1215 context->cursor->blkno = 0;
1216 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1218 return XFS_ERROR(error);
1221 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1222 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1223 context->dp->i_mount, leaf);
1224 xfs_da_brelse(NULL, bp);
1225 return XFS_ERROR(EFSCORRUPTED);
1228 error = xfs_attr_leaf_list_int(bp, context);
1229 xfs_da_brelse(NULL, bp);
1230 return XFS_ERROR(error);
1234 /*========================================================================
1235 * External routines when attribute list size > XFS_LBSIZE(mp).
1236 *========================================================================*/
1239 * Add a name to a Btree-format attribute list.
1241 * This will involve walking down the Btree, and may involve splitting
1242 * leaf nodes and even splitting intermediate nodes up to and including
1243 * the root node (a special case of an intermediate node).
1245 * "Remote" attribute values confuse the issue and atomic rename operations
1246 * add a whole extra layer of confusion on top of that.
1249 xfs_attr_node_addname(xfs_da_args_t *args)
1251 xfs_da_state_t *state;
1252 xfs_da_state_blk_t *blk;
1255 int committed, retval, error;
1258 * Fill in bucket of arguments/results/context to carry around.
1263 state = xfs_da_state_alloc();
1266 state->blocksize = state->mp->m_sb.sb_blocksize;
1267 state->node_ents = state->mp->m_attr_node_ents;
1270 * Search to see if name already exists, and get back a pointer
1271 * to where it should go.
1273 error = xfs_da_node_lookup_int(state, &retval);
1276 blk = &state->path.blk[ state->path.active-1 ];
1277 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1278 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1280 } else if (retval == EEXIST) {
1281 if (args->flags & ATTR_CREATE)
1283 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1284 args->blkno2 = args->blkno; /* set 2nd entry info*/
1285 args->index2 = args->index;
1286 args->rmtblkno2 = args->rmtblkno;
1287 args->rmtblkcnt2 = args->rmtblkcnt;
1289 args->rmtblkcnt = 0;
1292 retval = xfs_attr_leaf_add(blk->bp, state->args);
1293 if (retval == ENOSPC) {
1294 if (state->path.active == 1) {
1296 * Its really a single leaf node, but it had
1297 * out-of-line values so it looked like it *might*
1298 * have been a b-tree.
1300 xfs_da_state_free(state);
1301 xfs_bmap_init(args->flist, args->firstblock);
1302 error = xfs_attr_leaf_to_node(args);
1304 error = xfs_bmap_finish(&args->trans,
1311 xfs_bmap_cancel(args->flist);
1316 * bmap_finish() may have committed the last trans
1317 * and started a new one. We need the inode to be
1318 * in all transactions.
1321 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1322 xfs_trans_ihold(args->trans, dp);
1326 * Commit the node conversion and start the next
1327 * trans in the chain.
1329 error = xfs_trans_roll(&args->trans, dp);
1337 * Split as many Btree elements as required.
1338 * This code tracks the new and old attr's location
1339 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1340 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1342 xfs_bmap_init(args->flist, args->firstblock);
1343 error = xfs_da_split(state);
1345 error = xfs_bmap_finish(&args->trans, args->flist,
1351 xfs_bmap_cancel(args->flist);
1356 * bmap_finish() may have committed the last trans and started
1357 * a new one. We need the inode to be in all transactions.
1360 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1361 xfs_trans_ihold(args->trans, dp);
1365 * Addition succeeded, update Btree hashvals.
1367 xfs_da_fixhashpath(state, &state->path);
1371 * Kill the state structure, we're done with it and need to
1372 * allow the buffers to come back later.
1374 xfs_da_state_free(state);
1378 * Commit the leaf addition or btree split and start the next
1379 * trans in the chain.
1381 error = xfs_trans_roll(&args->trans, dp);
1386 * If there was an out-of-line value, allocate the blocks we
1387 * identified for its storage and copy the value. This is done
1388 * after we create the attribute so that we don't overflow the
1389 * maximum size of a transaction and/or hit a deadlock.
1391 if (args->rmtblkno > 0) {
1392 error = xfs_attr_rmtval_set(args);
1398 * If this is an atomic rename operation, we must "flip" the
1399 * incomplete flags on the "new" and "old" attribute/value pairs
1400 * so that one disappears and one appears atomically. Then we
1401 * must remove the "old" attribute/value pair.
1403 if (args->op_flags & XFS_DA_OP_RENAME) {
1405 * In a separate transaction, set the incomplete flag on the
1406 * "old" attr and clear the incomplete flag on the "new" attr.
1408 error = xfs_attr_leaf_flipflags(args);
1413 * Dismantle the "old" attribute/value pair by removing
1414 * a "remote" value (if it exists).
1416 args->index = args->index2;
1417 args->blkno = args->blkno2;
1418 args->rmtblkno = args->rmtblkno2;
1419 args->rmtblkcnt = args->rmtblkcnt2;
1420 if (args->rmtblkno) {
1421 error = xfs_attr_rmtval_remove(args);
1427 * Re-find the "old" attribute entry after any split ops.
1428 * The INCOMPLETE flag means that we will find the "old"
1429 * attr, not the "new" one.
1431 args->flags |= XFS_ATTR_INCOMPLETE;
1432 state = xfs_da_state_alloc();
1435 state->blocksize = state->mp->m_sb.sb_blocksize;
1436 state->node_ents = state->mp->m_attr_node_ents;
1438 error = xfs_da_node_lookup_int(state, &retval);
1443 * Remove the name and update the hashvals in the tree.
1445 blk = &state->path.blk[ state->path.active-1 ];
1446 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1447 error = xfs_attr_leaf_remove(blk->bp, args);
1448 xfs_da_fixhashpath(state, &state->path);
1451 * Check to see if the tree needs to be collapsed.
1453 if (retval && (state->path.active > 1)) {
1454 xfs_bmap_init(args->flist, args->firstblock);
1455 error = xfs_da_join(state);
1457 error = xfs_bmap_finish(&args->trans,
1464 xfs_bmap_cancel(args->flist);
1469 * bmap_finish() may have committed the last trans
1470 * and started a new one. We need the inode to be
1471 * in all transactions.
1474 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1475 xfs_trans_ihold(args->trans, dp);
1480 * Commit and start the next trans in the chain.
1482 error = xfs_trans_roll(&args->trans, dp);
1486 } else if (args->rmtblkno > 0) {
1488 * Added a "remote" value, just clear the incomplete flag.
1490 error = xfs_attr_leaf_clearflag(args);
1498 xfs_da_state_free(state);
1505 * Remove a name from a B-tree attribute list.
1507 * This will involve walking down the Btree, and may involve joining
1508 * leaf nodes and even joining intermediate nodes up to and including
1509 * the root node (a special case of an intermediate node).
1512 xfs_attr_node_removename(xfs_da_args_t *args)
1514 xfs_da_state_t *state;
1515 xfs_da_state_blk_t *blk;
1518 int retval, error, committed, forkoff;
1521 * Tie a string around our finger to remind us where we are.
1524 state = xfs_da_state_alloc();
1526 state->mp = dp->i_mount;
1527 state->blocksize = state->mp->m_sb.sb_blocksize;
1528 state->node_ents = state->mp->m_attr_node_ents;
1531 * Search to see if name exists, and get back a pointer to it.
1533 error = xfs_da_node_lookup_int(state, &retval);
1534 if (error || (retval != EEXIST)) {
1541 * If there is an out-of-line value, de-allocate the blocks.
1542 * This is done before we remove the attribute so that we don't
1543 * overflow the maximum size of a transaction and/or hit a deadlock.
1545 blk = &state->path.blk[ state->path.active-1 ];
1546 ASSERT(blk->bp != NULL);
1547 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1548 if (args->rmtblkno > 0) {
1550 * Fill in disk block numbers in the state structure
1551 * so that we can get the buffers back after we commit
1552 * several transactions in the following calls.
1554 error = xfs_attr_fillstate(state);
1559 * Mark the attribute as INCOMPLETE, then bunmapi() the
1562 error = xfs_attr_leaf_setflag(args);
1565 error = xfs_attr_rmtval_remove(args);
1570 * Refill the state structure with buffers, the prior calls
1571 * released our buffers.
1573 error = xfs_attr_refillstate(state);
1579 * Remove the name and update the hashvals in the tree.
1581 blk = &state->path.blk[ state->path.active-1 ];
1582 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1583 retval = xfs_attr_leaf_remove(blk->bp, args);
1584 xfs_da_fixhashpath(state, &state->path);
1587 * Check to see if the tree needs to be collapsed.
1589 if (retval && (state->path.active > 1)) {
1590 xfs_bmap_init(args->flist, args->firstblock);
1591 error = xfs_da_join(state);
1593 error = xfs_bmap_finish(&args->trans, args->flist,
1599 xfs_bmap_cancel(args->flist);
1604 * bmap_finish() may have committed the last trans and started
1605 * a new one. We need the inode to be in all transactions.
1608 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1609 xfs_trans_ihold(args->trans, dp);
1613 * Commit the Btree join operation and start a new trans.
1615 error = xfs_trans_roll(&args->trans, dp);
1621 * If the result is small enough, push it all into the inode.
1623 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1625 * Have to get rid of the copy of this dabuf in the state.
1627 ASSERT(state->path.active == 1);
1628 ASSERT(state->path.blk[0].bp);
1629 xfs_da_buf_done(state->path.blk[0].bp);
1630 state->path.blk[0].bp = NULL;
1632 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1636 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1637 bp->data)->hdr.info.magic)
1638 == XFS_ATTR_LEAF_MAGIC);
1640 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1641 xfs_bmap_init(args->flist, args->firstblock);
1642 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1643 /* bp is gone due to xfs_da_shrink_inode */
1645 error = xfs_bmap_finish(&args->trans,
1652 xfs_bmap_cancel(args->flist);
1657 * bmap_finish() may have committed the last trans
1658 * and started a new one. We need the inode to be
1659 * in all transactions.
1662 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1663 xfs_trans_ihold(args->trans, dp);
1666 xfs_da_brelse(args->trans, bp);
1671 xfs_da_state_free(state);
1676 * Fill in the disk block numbers in the state structure for the buffers
1677 * that are attached to the state structure.
1678 * This is done so that we can quickly reattach ourselves to those buffers
1679 * after some set of transaction commits have released these buffers.
1682 xfs_attr_fillstate(xfs_da_state_t *state)
1684 xfs_da_state_path_t *path;
1685 xfs_da_state_blk_t *blk;
1689 * Roll down the "path" in the state structure, storing the on-disk
1690 * block number for those buffers in the "path".
1692 path = &state->path;
1693 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1694 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1696 blk->disk_blkno = xfs_da_blkno(blk->bp);
1697 xfs_da_buf_done(blk->bp);
1700 blk->disk_blkno = 0;
1705 * Roll down the "altpath" in the state structure, storing the on-disk
1706 * block number for those buffers in the "altpath".
1708 path = &state->altpath;
1709 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1710 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1712 blk->disk_blkno = xfs_da_blkno(blk->bp);
1713 xfs_da_buf_done(blk->bp);
1716 blk->disk_blkno = 0;
1724 * Reattach the buffers to the state structure based on the disk block
1725 * numbers stored in the state structure.
1726 * This is done after some set of transaction commits have released those
1727 * buffers from our grip.
1730 xfs_attr_refillstate(xfs_da_state_t *state)
1732 xfs_da_state_path_t *path;
1733 xfs_da_state_blk_t *blk;
1737 * Roll down the "path" in the state structure, storing the on-disk
1738 * block number for those buffers in the "path".
1740 path = &state->path;
1741 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1742 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1743 if (blk->disk_blkno) {
1744 error = xfs_da_read_buf(state->args->trans,
1746 blk->blkno, blk->disk_blkno,
1747 &blk->bp, XFS_ATTR_FORK);
1756 * Roll down the "altpath" in the state structure, storing the on-disk
1757 * block number for those buffers in the "altpath".
1759 path = &state->altpath;
1760 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1761 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1762 if (blk->disk_blkno) {
1763 error = xfs_da_read_buf(state->args->trans,
1765 blk->blkno, blk->disk_blkno,
1766 &blk->bp, XFS_ATTR_FORK);
1778 * Look up a filename in a node attribute list.
1780 * This routine gets called for any attribute fork that has more than one
1781 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1782 * "remote" values taking up more blocks.
1785 xfs_attr_node_get(xfs_da_args_t *args)
1787 xfs_da_state_t *state;
1788 xfs_da_state_blk_t *blk;
1792 state = xfs_da_state_alloc();
1794 state->mp = args->dp->i_mount;
1795 state->blocksize = state->mp->m_sb.sb_blocksize;
1796 state->node_ents = state->mp->m_attr_node_ents;
1799 * Search to see if name exists, and get back a pointer to it.
1801 error = xfs_da_node_lookup_int(state, &retval);
1804 } else if (retval == EEXIST) {
1805 blk = &state->path.blk[ state->path.active-1 ];
1806 ASSERT(blk->bp != NULL);
1807 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1810 * Get the value, local or "remote"
1812 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1813 if (!retval && (args->rmtblkno > 0)
1814 && !(args->flags & ATTR_KERNOVAL)) {
1815 retval = xfs_attr_rmtval_get(args);
1820 * If not in a transaction, we have to release all the buffers.
1822 for (i = 0; i < state->path.active; i++) {
1823 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1824 state->path.blk[i].bp = NULL;
1827 xfs_da_state_free(state);
1831 STATIC int /* error */
1832 xfs_attr_node_list(xfs_attr_list_context_t *context)
1834 attrlist_cursor_kern_t *cursor;
1835 xfs_attr_leafblock_t *leaf;
1836 xfs_da_intnode_t *node;
1837 xfs_da_node_entry_t *btree;
1841 cursor = context->cursor;
1842 cursor->initted = 1;
1845 * Do all sorts of validation on the passed-in cursor structure.
1846 * If anything is amiss, ignore the cursor and look up the hashval
1847 * starting from the btree root.
1850 if (cursor->blkno > 0) {
1851 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1852 &bp, XFS_ATTR_FORK);
1853 if ((error != 0) && (error != EFSCORRUPTED))
1857 switch (be16_to_cpu(node->hdr.info.magic)) {
1858 case XFS_DA_NODE_MAGIC:
1859 trace_xfs_attr_list_wrong_blk(context);
1860 xfs_da_brelse(NULL, bp);
1863 case XFS_ATTR_LEAF_MAGIC:
1865 if (cursor->hashval > be32_to_cpu(leaf->entries[
1866 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1867 trace_xfs_attr_list_wrong_blk(context);
1868 xfs_da_brelse(NULL, bp);
1870 } else if (cursor->hashval <=
1871 be32_to_cpu(leaf->entries[0].hashval)) {
1872 trace_xfs_attr_list_wrong_blk(context);
1873 xfs_da_brelse(NULL, bp);
1878 trace_xfs_attr_list_wrong_blk(context);
1879 xfs_da_brelse(NULL, bp);
1886 * We did not find what we expected given the cursor's contents,
1887 * so we start from the top and work down based on the hash value.
1888 * Note that start of node block is same as start of leaf block.
1893 error = xfs_da_read_buf(NULL, context->dp,
1894 cursor->blkno, -1, &bp,
1898 if (unlikely(bp == NULL)) {
1899 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1901 context->dp->i_mount);
1902 return(XFS_ERROR(EFSCORRUPTED));
1905 if (be16_to_cpu(node->hdr.info.magic)
1906 == XFS_ATTR_LEAF_MAGIC)
1908 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1909 != XFS_DA_NODE_MAGIC)) {
1910 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1912 context->dp->i_mount,
1914 xfs_da_brelse(NULL, bp);
1915 return(XFS_ERROR(EFSCORRUPTED));
1917 btree = node->btree;
1918 for (i = 0; i < be16_to_cpu(node->hdr.count);
1921 <= be32_to_cpu(btree->hashval)) {
1922 cursor->blkno = be32_to_cpu(btree->before);
1923 trace_xfs_attr_list_node_descend(context,
1928 if (i == be16_to_cpu(node->hdr.count)) {
1929 xfs_da_brelse(NULL, bp);
1932 xfs_da_brelse(NULL, bp);
1938 * Roll upward through the blocks, processing each leaf block in
1939 * order. As long as there is space in the result buffer, keep
1940 * adding the information.
1944 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1945 != XFS_ATTR_LEAF_MAGIC)) {
1946 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1948 context->dp->i_mount, leaf);
1949 xfs_da_brelse(NULL, bp);
1950 return(XFS_ERROR(EFSCORRUPTED));
1952 error = xfs_attr_leaf_list_int(bp, context);
1954 xfs_da_brelse(NULL, bp);
1957 if (context->seen_enough || leaf->hdr.info.forw == 0)
1959 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1960 xfs_da_brelse(NULL, bp);
1961 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1962 &bp, XFS_ATTR_FORK);
1965 if (unlikely((bp == NULL))) {
1966 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1968 context->dp->i_mount);
1969 return(XFS_ERROR(EFSCORRUPTED));
1972 xfs_da_brelse(NULL, bp);
1977 /*========================================================================
1978 * External routines for manipulating out-of-line attribute values.
1979 *========================================================================*/
1982 * Read the value associated with an attribute from the out-of-line buffer
1983 * that we stored it in.
1986 xfs_attr_rmtval_get(xfs_da_args_t *args)
1988 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1993 int nmap, error, tmp, valuelen, blkcnt, i;
1996 ASSERT(!(args->flags & ATTR_KERNOVAL));
1998 mp = args->dp->i_mount;
2000 valuelen = args->valuelen;
2001 lblkno = args->rmtblkno;
2002 while (valuelen > 0) {
2003 nmap = ATTR_RMTVALUE_MAPSIZE;
2004 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2006 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2007 NULL, 0, map, &nmap, NULL, NULL);
2012 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2013 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2014 (map[i].br_startblock != HOLESTARTBLOCK));
2015 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2016 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2017 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2018 blkcnt, XBF_LOCK | XBF_DONT_BLOCK,
2023 tmp = (valuelen < XFS_BUF_SIZE(bp))
2024 ? valuelen : XFS_BUF_SIZE(bp);
2025 xfs_biomove(bp, 0, tmp, dst, XBF_READ);
2030 lblkno += map[i].br_blockcount;
2033 ASSERT(valuelen == 0);
2038 * Write the value associated with an attribute into the out-of-line buffer
2039 * that we have defined for it.
2042 xfs_attr_rmtval_set(xfs_da_args_t *args)
2045 xfs_fileoff_t lfileoff;
2047 xfs_bmbt_irec_t map;
2052 int blkcnt, valuelen, nmap, error, tmp, committed;
2059 * Find a "hole" in the attribute address space large enough for
2060 * us to drop the new attribute's value into.
2062 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2064 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2069 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2070 args->rmtblkcnt = blkcnt;
2073 * Roll through the "value", allocating blocks on disk as required.
2075 while (blkcnt > 0) {
2077 * Allocate a single extent, up to the size of the value.
2079 xfs_bmap_init(args->flist, args->firstblock);
2081 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2083 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2085 args->firstblock, args->total, &map, &nmap,
2088 error = xfs_bmap_finish(&args->trans, args->flist,
2094 xfs_bmap_cancel(args->flist);
2099 * bmap_finish() may have committed the last trans and started
2100 * a new one. We need the inode to be in all transactions.
2103 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2104 xfs_trans_ihold(args->trans, dp);
2108 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2109 (map.br_startblock != HOLESTARTBLOCK));
2110 lblkno += map.br_blockcount;
2111 blkcnt -= map.br_blockcount;
2114 * Start the next trans in the chain.
2116 error = xfs_trans_roll(&args->trans, dp);
2122 * Roll through the "value", copying the attribute value to the
2123 * already-allocated blocks. Blocks are written synchronously
2124 * so that we can know they are all on disk before we turn off
2125 * the INCOMPLETE flag.
2127 lblkno = args->rmtblkno;
2128 valuelen = args->valuelen;
2129 while (valuelen > 0) {
2131 * Try to remember where we decided to put the value.
2133 xfs_bmap_init(args->flist, args->firstblock);
2135 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2137 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2138 args->firstblock, 0, &map, &nmap,
2144 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2145 (map.br_startblock != HOLESTARTBLOCK));
2147 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2148 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2150 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt,
2151 XBF_LOCK | XBF_DONT_BLOCK);
2153 ASSERT(!XFS_BUF_GETERROR(bp));
2155 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2157 xfs_biomove(bp, 0, tmp, src, XBF_WRITE);
2158 if (tmp < XFS_BUF_SIZE(bp))
2159 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2160 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2166 lblkno += map.br_blockcount;
2168 ASSERT(valuelen == 0);
2173 * Remove the value associated with an attribute by deleting the
2174 * out-of-line buffer that it is stored on.
2177 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2180 xfs_bmbt_irec_t map;
2184 int valuelen, blkcnt, nmap, error, done, committed;
2186 mp = args->dp->i_mount;
2189 * Roll through the "value", invalidating the attribute value's
2192 lblkno = args->rmtblkno;
2193 valuelen = args->rmtblkcnt;
2194 while (valuelen > 0) {
2196 * Try to remember where we decided to put the value.
2198 xfs_bmap_init(args->flist, args->firstblock);
2200 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2202 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2203 args->firstblock, 0, &map, &nmap,
2209 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2210 (map.br_startblock != HOLESTARTBLOCK));
2212 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2213 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2216 * If the "remote" value is in the cache, remove it.
2218 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
2221 XFS_BUF_UNDELAYWRITE(bp);
2226 valuelen -= map.br_blockcount;
2228 lblkno += map.br_blockcount;
2232 * Keep de-allocating extents until the remote-value region is gone.
2234 lblkno = args->rmtblkno;
2235 blkcnt = args->rmtblkcnt;
2238 xfs_bmap_init(args->flist, args->firstblock);
2239 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2240 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2241 1, args->firstblock, args->flist,
2244 error = xfs_bmap_finish(&args->trans, args->flist,
2250 xfs_bmap_cancel(args->flist);
2255 * bmap_finish() may have committed the last trans and started
2256 * a new one. We need the inode to be in all transactions.
2259 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2260 xfs_trans_ihold(args->trans, args->dp);
2264 * Close out trans and start the next one in the chain.
2266 error = xfs_trans_roll(&args->trans, args->dp);