2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/capability.h>
23 #include "xfs_types.h"
27 #include "xfs_trans.h"
31 #include "xfs_dmapi.h"
32 #include "xfs_mount.h"
33 #include "xfs_da_btree.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_alloc.h"
42 #include "xfs_btree.h"
43 #include "xfs_inode_item.h"
46 #include "xfs_attr_leaf.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_trans_space.h"
52 #include "xfs_vnodeops.h"
57 * Provide the external interfaces to manage attribute lists.
60 /*========================================================================
61 * Function prototypes for the kernel.
62 *========================================================================*/
65 * Internal routines when attribute list fits inside the inode.
67 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
70 * Internal routines when attribute list is one block.
72 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
73 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
74 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
75 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
78 * Internal routines when attribute list is more than one block.
80 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
81 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
82 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
83 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
84 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
85 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
88 * Routines to manipulate out-of-line attribute values.
90 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
91 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
93 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
95 #if defined(XFS_ATTR_TRACE)
96 ktrace_t *xfs_attr_trace_buf;
100 xfs_attr_name_to_xname(
101 struct xfs_name *xname,
107 xname->len = strlen(aname);
108 if (xname->len >= MAXNAMELEN)
109 return EFAULT; /* match IRIX behaviour */
114 /*========================================================================
115 * Overall external interface routines.
116 *========================================================================*/
119 xfs_attr_fetch(xfs_inode_t *ip, struct xfs_name *name,
120 char *value, int *valuelenp, int flags)
125 if ((XFS_IFORK_Q(ip) == 0) ||
126 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
127 ip->i_d.di_anextents == 0))
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 (XFS_IFORK_Q(ip) == 0 ||
147 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
148 ip->i_d.di_anextents == 0)) {
149 error = XFS_ERROR(ENOATTR);
150 } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
151 error = xfs_attr_shortform_getvalue(&args);
152 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
153 error = xfs_attr_leaf_get(&args);
155 error = xfs_attr_node_get(&args);
159 * Return the number of bytes in the value to the caller.
161 *valuelenp = args.valuelen;
177 struct xfs_name xname;
179 XFS_STATS_INC(xs_attr_get);
181 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
184 error = xfs_attr_name_to_xname(&xname, name);
188 xfs_ilock(ip, XFS_ILOCK_SHARED);
189 error = xfs_attr_fetch(ip, &xname, value, valuelenp, flags);
190 xfs_iunlock(ip, XFS_ILOCK_SHARED);
195 xfs_attr_set_int(xfs_inode_t *dp, struct xfs_name *name,
196 char *value, int valuelen, int flags)
199 xfs_fsblock_t firstblock;
200 xfs_bmap_free_t flist;
201 int error, err2, committed;
204 xfs_mount_t *mp = dp->i_mount;
205 int rsvd = (flags & ATTR_ROOT) != 0;
208 * Attach the dquots to the inode.
210 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
214 * If the inode doesn't have an attribute fork, add one.
215 * (inode must not be locked when we call this routine)
217 if (XFS_IFORK_Q(dp) == 0) {
218 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
219 XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
221 if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
226 * Fill in the arg structure for this request.
228 memset((char *)&args, 0, sizeof(args));
229 args.name = name->name;
230 args.namelen = name->len;
232 args.valuelen = valuelen;
234 args.hashval = xfs_da_hashname(args.name, args.namelen);
236 args.firstblock = &firstblock;
238 args.whichfork = XFS_ATTR_FORK;
239 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
242 * Determine space new attribute will use, and if it would be
243 * "local" or "remote" (note: local != inline).
245 size = xfs_attr_leaf_newentsize(name->len, valuelen,
246 mp->m_sb.sb_blocksize, &local);
248 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
250 if (size > (mp->m_sb.sb_blocksize >> 1)) {
251 /* Double split possible */
255 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
256 /* Out of line attribute, cannot double split, but make
257 * room for the attribute value itself.
260 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
263 /* Size is now blocks for attribute data */
267 * Start our first transaction of the day.
269 * All future transactions during this code must be "chained" off
270 * this one via the trans_dup() call. All transactions will contain
271 * the inode, and the inode will always be marked with trans_ihold().
272 * Since the inode will be locked in all transactions, we must log
273 * the inode in every transaction to let it float upward through
276 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
279 * Root fork attributes can use reserved data blocks for this
280 * operation if necessary
284 args.trans->t_flags |= XFS_TRANS_RESERVE;
286 if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
287 XFS_ATTRSET_LOG_RES(mp, nblks),
288 0, XFS_TRANS_PERM_LOG_RES,
289 XFS_ATTRSET_LOG_COUNT))) {
290 xfs_trans_cancel(args.trans, 0);
293 xfs_ilock(dp, XFS_ILOCK_EXCL);
295 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
296 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
297 XFS_QMOPT_RES_REGBLKS);
299 xfs_iunlock(dp, XFS_ILOCK_EXCL);
300 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
304 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
305 xfs_trans_ihold(args.trans, dp);
308 * If the attribute list is non-existent or a shortform list,
309 * upgrade it to a single-leaf-block attribute list.
311 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
312 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
313 (dp->i_d.di_anextents == 0))) {
316 * Build initial attribute list (if required).
318 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
319 xfs_attr_shortform_create(&args);
322 * Try to add the attr to the attribute list in
325 error = xfs_attr_shortform_addname(&args);
326 if (error != ENOSPC) {
328 * Commit the shortform mods, and we're done.
329 * NOTE: this is also the error path (EEXIST, etc).
331 ASSERT(args.trans != NULL);
334 * If this is a synchronous mount, make sure that
335 * the transaction goes to disk before returning
338 if (mp->m_flags & XFS_MOUNT_WSYNC) {
339 xfs_trans_set_sync(args.trans);
341 err2 = xfs_trans_commit(args.trans,
342 XFS_TRANS_RELEASE_LOG_RES);
343 xfs_iunlock(dp, XFS_ILOCK_EXCL);
346 * Hit the inode change time.
348 if (!error && (flags & ATTR_KERNOTIME) == 0) {
349 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
351 return(error == 0 ? err2 : error);
355 * It won't fit in the shortform, transform to a leaf block.
356 * GROT: another possible req'mt for a double-split btree op.
358 XFS_BMAP_INIT(args.flist, args.firstblock);
359 error = xfs_attr_shortform_to_leaf(&args);
361 error = xfs_bmap_finish(&args.trans, args.flist,
367 xfs_bmap_cancel(&flist);
372 * bmap_finish() may have committed the last trans and started
373 * a new one. We need the inode to be in all transactions.
376 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
377 xfs_trans_ihold(args.trans, dp);
381 * Commit the leaf transformation. We'll need another (linked)
382 * transaction to add the new attribute to the leaf.
384 if ((error = xfs_attr_rolltrans(&args.trans, dp)))
389 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
390 error = xfs_attr_leaf_addname(&args);
392 error = xfs_attr_node_addname(&args);
399 * If this is a synchronous mount, make sure that the
400 * transaction goes to disk before returning to the user.
402 if (mp->m_flags & XFS_MOUNT_WSYNC) {
403 xfs_trans_set_sync(args.trans);
407 * Commit the last in the sequence of transactions.
409 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
410 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
411 xfs_iunlock(dp, XFS_ILOCK_EXCL);
414 * Hit the inode change time.
416 if (!error && (flags & ATTR_KERNOTIME) == 0) {
417 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
424 xfs_trans_cancel(args.trans,
425 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
426 xfs_iunlock(dp, XFS_ILOCK_EXCL);
439 struct xfs_name xname;
441 XFS_STATS_INC(xs_attr_set);
443 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
446 error = xfs_attr_name_to_xname(&xname, name);
450 return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
454 * Generic handler routine to remove a name from an attribute list.
455 * Transitions attribute list from Btree to shortform as necessary.
458 xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
461 xfs_fsblock_t firstblock;
462 xfs_bmap_free_t flist;
464 xfs_mount_t *mp = dp->i_mount;
467 * Fill in the arg structure for this request.
469 memset((char *)&args, 0, sizeof(args));
470 args.name = name->name;
471 args.namelen = name->len;
473 args.hashval = xfs_da_hashname(args.name, args.namelen);
475 args.firstblock = &firstblock;
478 args.whichfork = XFS_ATTR_FORK;
481 * Attach the dquots to the inode.
483 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
487 * Start our first transaction of the day.
489 * All future transactions during this code must be "chained" off
490 * this one via the trans_dup() call. All transactions will contain
491 * the inode, and the inode will always be marked with trans_ihold().
492 * Since the inode will be locked in all transactions, we must log
493 * the inode in every transaction to let it float upward through
496 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
499 * Root fork attributes can use reserved data blocks for this
500 * operation if necessary
503 if (flags & ATTR_ROOT)
504 args.trans->t_flags |= XFS_TRANS_RESERVE;
506 if ((error = xfs_trans_reserve(args.trans,
507 XFS_ATTRRM_SPACE_RES(mp),
508 XFS_ATTRRM_LOG_RES(mp),
509 0, XFS_TRANS_PERM_LOG_RES,
510 XFS_ATTRRM_LOG_COUNT))) {
511 xfs_trans_cancel(args.trans, 0);
515 xfs_ilock(dp, XFS_ILOCK_EXCL);
517 * No need to make quota reservations here. We expect to release some
518 * blocks not allocate in the common case.
520 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
521 xfs_trans_ihold(args.trans, dp);
524 * Decide on what work routines to call based on the inode size.
526 if (XFS_IFORK_Q(dp) == 0 ||
527 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
528 dp->i_d.di_anextents == 0)) {
529 error = XFS_ERROR(ENOATTR);
532 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
533 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
534 error = xfs_attr_shortform_remove(&args);
538 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
539 error = xfs_attr_leaf_removename(&args);
541 error = xfs_attr_node_removename(&args);
548 * If this is a synchronous mount, make sure that the
549 * transaction goes to disk before returning to the user.
551 if (mp->m_flags & XFS_MOUNT_WSYNC) {
552 xfs_trans_set_sync(args.trans);
556 * Commit the last in the sequence of transactions.
558 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
559 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
560 xfs_iunlock(dp, XFS_ILOCK_EXCL);
563 * Hit the inode change time.
565 if (!error && (flags & ATTR_KERNOTIME) == 0) {
566 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
573 xfs_trans_cancel(args.trans,
574 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
575 xfs_iunlock(dp, XFS_ILOCK_EXCL);
586 struct xfs_name xname;
588 XFS_STATS_INC(xs_attr_remove);
590 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
593 error = xfs_attr_name_to_xname(&xname, name);
597 xfs_ilock(dp, XFS_ILOCK_SHARED);
598 if (XFS_IFORK_Q(dp) == 0 ||
599 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
600 dp->i_d.di_anextents == 0)) {
601 xfs_iunlock(dp, XFS_ILOCK_SHARED);
602 return(XFS_ERROR(ENOATTR));
604 xfs_iunlock(dp, XFS_ILOCK_SHARED);
606 return xfs_attr_remove_int(dp, &xname, flags);
610 xfs_attr_list_int(xfs_attr_list_context_t *context)
613 xfs_inode_t *dp = context->dp;
616 * Decide on what work routines to call based on the inode size.
618 if (XFS_IFORK_Q(dp) == 0 ||
619 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
620 dp->i_d.di_anextents == 0)) {
622 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
623 error = xfs_attr_shortform_list(context);
624 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
625 error = xfs_attr_leaf_list(context);
627 error = xfs_attr_node_list(context);
632 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
633 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
634 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
635 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
636 & ~(sizeof(u_int32_t)-1))
639 * Format an attribute and copy it out to the user's buffer.
640 * Take care to check values and protect against them changing later,
641 * we may be reading them directly out of a user buffer.
645 xfs_attr_put_listent(xfs_attr_list_context_t *context, attrnames_t *namesp,
646 char *name, int namelen,
647 int valuelen, char *value)
652 ASSERT(!(context->flags & ATTR_KERNOVAL));
653 ASSERT(context->count >= 0);
654 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
655 ASSERT(context->firstu >= sizeof(*context->alist));
656 ASSERT(context->firstu <= context->bufsize);
658 arraytop = sizeof(*context->alist) +
659 context->count * sizeof(context->alist->al_offset[0]);
660 context->firstu -= ATTR_ENTSIZE(namelen);
661 if (context->firstu < arraytop) {
662 xfs_attr_trace_l_c("buffer full", context);
663 context->alist->al_more = 1;
664 context->seen_enough = 1;
668 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
669 aep->a_valuelen = valuelen;
670 memcpy(aep->a_name, name, namelen);
671 aep->a_name[ namelen ] = 0;
672 context->alist->al_offset[ context->count++ ] = context->firstu;
673 context->alist->al_count = context->count;
674 xfs_attr_trace_l_c("add", context);
679 xfs_attr_kern_list(xfs_attr_list_context_t *context, attrnames_t *namesp,
680 char *name, int namelen,
681 int valuelen, char *value)
686 ASSERT(context->count >= 0);
688 arraytop = context->count + namesp->attr_namelen + namelen + 1;
689 if (arraytop > context->firstu) {
690 context->count = -1; /* insufficient space */
693 offset = (char *)context->alist + context->count;
694 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
695 offset += namesp->attr_namelen;
696 strncpy(offset, name, namelen); /* real name */
699 context->count += namesp->attr_namelen + namelen + 1;
705 xfs_attr_kern_list_sizes(xfs_attr_list_context_t *context, attrnames_t *namesp,
706 char *name, int namelen,
707 int valuelen, char *value)
709 context->count += namesp->attr_namelen + namelen + 1;
714 * Generate a list of extended attribute names and optionally
715 * also value lengths. Positive return value follows the XFS
716 * convention of being an error, zero or negative return code
717 * is the length of the buffer returned (negated), indicating
726 attrlist_cursor_kern_t *cursor)
728 xfs_attr_list_context_t context;
731 XFS_STATS_INC(xs_attr_list);
734 * Validate the cursor.
736 if (cursor->pad1 || cursor->pad2)
737 return(XFS_ERROR(EINVAL));
738 if ((cursor->initted == 0) &&
739 (cursor->hashval || cursor->blkno || cursor->offset))
740 return XFS_ERROR(EINVAL);
743 * Check for a properly aligned buffer.
745 if (((long)buffer) & (sizeof(int)-1))
746 return XFS_ERROR(EFAULT);
747 if (flags & ATTR_KERNOVAL)
751 * Initialize the output buffer.
754 context.cursor = cursor;
758 context.flags = flags;
759 context.seen_enough = 0;
760 context.alist = (attrlist_t *)buffer;
761 context.put_value = 0;
763 if (flags & ATTR_KERNAMELS) {
764 context.bufsize = bufsize;
765 context.firstu = context.bufsize;
766 if (flags & ATTR_KERNOVAL)
767 context.put_listent = xfs_attr_kern_list_sizes;
769 context.put_listent = xfs_attr_kern_list;
771 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
772 context.firstu = context.bufsize;
773 context.alist->al_count = 0;
774 context.alist->al_more = 0;
775 context.alist->al_offset[0] = context.bufsize;
776 context.put_listent = xfs_attr_put_listent;
779 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
782 xfs_ilock(dp, XFS_ILOCK_SHARED);
783 xfs_attr_trace_l_c("syscall start", &context);
785 error = xfs_attr_list_int(&context);
787 xfs_iunlock(dp, XFS_ILOCK_SHARED);
788 xfs_attr_trace_l_c("syscall end", &context);
790 if (context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS)) {
791 /* must return negated buffer size or the error */
792 if (context.count < 0)
793 error = XFS_ERROR(ERANGE);
795 error = -context.count;
803 xfs_attr_inactive(xfs_inode_t *dp)
810 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
812 xfs_ilock(dp, XFS_ILOCK_SHARED);
813 if ((XFS_IFORK_Q(dp) == 0) ||
814 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
815 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
816 dp->i_d.di_anextents == 0)) {
817 xfs_iunlock(dp, XFS_ILOCK_SHARED);
820 xfs_iunlock(dp, XFS_ILOCK_SHARED);
823 * Start our first transaction of the day.
825 * All future transactions during this code must be "chained" off
826 * this one via the trans_dup() call. All transactions will contain
827 * the inode, and the inode will always be marked with trans_ihold().
828 * Since the inode will be locked in all transactions, we must log
829 * the inode in every transaction to let it float upward through
832 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
833 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
834 XFS_TRANS_PERM_LOG_RES,
835 XFS_ATTRINVAL_LOG_COUNT))) {
836 xfs_trans_cancel(trans, 0);
839 xfs_ilock(dp, XFS_ILOCK_EXCL);
842 * No need to make quota reservations here. We expect to release some
843 * blocks, not allocate, in the common case.
845 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
846 xfs_trans_ihold(trans, dp);
849 * Decide on what work routines to call based on the inode size.
851 if ((XFS_IFORK_Q(dp) == 0) ||
852 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
853 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
854 dp->i_d.di_anextents == 0)) {
858 error = xfs_attr_root_inactive(&trans, dp);
862 * signal synchronous inactive transactions unless this
863 * is a synchronous mount filesystem in which case we
864 * know that we're here because we've been called out of
865 * xfs_inactive which means that the last reference is gone
866 * and the unlink transaction has already hit the disk so
867 * async inactive transactions are safe.
869 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
870 (!(mp->m_flags & XFS_MOUNT_WSYNC)
875 * Commit the last in the sequence of transactions.
877 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
878 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES);
879 xfs_iunlock(dp, XFS_ILOCK_EXCL);
884 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
885 xfs_iunlock(dp, XFS_ILOCK_EXCL);
891 /*========================================================================
892 * External routines when attribute list is inside the inode
893 *========================================================================*/
896 * Add a name to the shortform attribute list structure
897 * This is the external routine.
900 xfs_attr_shortform_addname(xfs_da_args_t *args)
902 int newsize, forkoff, retval;
904 retval = xfs_attr_shortform_lookup(args);
905 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
907 } else if (retval == EEXIST) {
908 if (args->flags & ATTR_CREATE)
910 retval = xfs_attr_shortform_remove(args);
914 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
915 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
916 return(XFS_ERROR(ENOSPC));
918 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
919 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
921 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
923 return(XFS_ERROR(ENOSPC));
925 xfs_attr_shortform_add(args, forkoff);
930 /*========================================================================
931 * External routines when attribute list is one block
932 *========================================================================*/
935 * Add a name to the leaf attribute list structure
937 * This leaf block cannot have a "remote" value, we only call this routine
938 * if bmap_one_block() says there is only one block (ie: no remote blks).
941 xfs_attr_leaf_addname(xfs_da_args_t *args)
945 int retval, error, committed, forkoff;
948 * Read the (only) block in the attribute list in.
952 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
959 * Look up the given attribute in the leaf block. Figure out if
960 * the given flags produce an error or call for an atomic rename.
962 retval = xfs_attr_leaf_lookup_int(bp, args);
963 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
964 xfs_da_brelse(args->trans, bp);
966 } else if (retval == EEXIST) {
967 if (args->flags & ATTR_CREATE) { /* pure create op */
968 xfs_da_brelse(args->trans, bp);
971 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
972 args->blkno2 = args->blkno; /* set 2nd entry info*/
973 args->index2 = args->index;
974 args->rmtblkno2 = args->rmtblkno;
975 args->rmtblkcnt2 = args->rmtblkcnt;
979 * Add the attribute to the leaf block, transitioning to a Btree
982 retval = xfs_attr_leaf_add(bp, args);
984 if (retval == ENOSPC) {
986 * Promote the attribute list to the Btree format, then
987 * Commit that transaction so that the node_addname() call
988 * can manage its own transactions.
990 XFS_BMAP_INIT(args->flist, args->firstblock);
991 error = xfs_attr_leaf_to_node(args);
993 error = xfs_bmap_finish(&args->trans, args->flist,
999 xfs_bmap_cancel(args->flist);
1004 * bmap_finish() may have committed the last trans and started
1005 * a new one. We need the inode to be in all transactions.
1008 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1009 xfs_trans_ihold(args->trans, dp);
1013 * Commit the current trans (including the inode) and start
1016 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1020 * Fob the whole rest of the problem off on the Btree code.
1022 error = xfs_attr_node_addname(args);
1027 * Commit the transaction that added the attr name so that
1028 * later routines can manage their own transactions.
1030 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1034 * If there was an out-of-line value, allocate the blocks we
1035 * identified for its storage and copy the value. This is done
1036 * after we create the attribute so that we don't overflow the
1037 * maximum size of a transaction and/or hit a deadlock.
1039 if (args->rmtblkno > 0) {
1040 error = xfs_attr_rmtval_set(args);
1046 * If this is an atomic rename operation, we must "flip" the
1047 * incomplete flags on the "new" and "old" attribute/value pairs
1048 * so that one disappears and one appears atomically. Then we
1049 * must remove the "old" attribute/value pair.
1051 if (args->op_flags & XFS_DA_OP_RENAME) {
1053 * In a separate transaction, set the incomplete flag on the
1054 * "old" attr and clear the incomplete flag on the "new" attr.
1056 error = xfs_attr_leaf_flipflags(args);
1061 * Dismantle the "old" attribute/value pair by removing
1062 * a "remote" value (if it exists).
1064 args->index = args->index2;
1065 args->blkno = args->blkno2;
1066 args->rmtblkno = args->rmtblkno2;
1067 args->rmtblkcnt = args->rmtblkcnt2;
1068 if (args->rmtblkno) {
1069 error = xfs_attr_rmtval_remove(args);
1075 * Read in the block containing the "old" attr, then
1076 * remove the "old" attr from that block (neat, huh!)
1078 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1079 &bp, XFS_ATTR_FORK);
1083 (void)xfs_attr_leaf_remove(bp, args);
1086 * If the result is small enough, shrink it all into the inode.
1088 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1089 XFS_BMAP_INIT(args->flist, args->firstblock);
1090 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1091 /* bp is gone due to xfs_da_shrink_inode */
1093 error = xfs_bmap_finish(&args->trans,
1100 xfs_bmap_cancel(args->flist);
1105 * bmap_finish() may have committed the last trans
1106 * and started a new one. We need the inode to be
1107 * in all transactions.
1110 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1111 xfs_trans_ihold(args->trans, dp);
1114 xfs_da_buf_done(bp);
1117 * Commit the remove and start the next trans in series.
1119 error = xfs_attr_rolltrans(&args->trans, dp);
1121 } else if (args->rmtblkno > 0) {
1123 * Added a "remote" value, just clear the incomplete flag.
1125 error = xfs_attr_leaf_clearflag(args);
1131 * Remove a name from the leaf attribute list structure
1133 * This leaf block cannot have a "remote" value, we only call this routine
1134 * if bmap_one_block() says there is only one block (ie: no remote blks).
1137 xfs_attr_leaf_removename(xfs_da_args_t *args)
1141 int error, committed, forkoff;
1144 * Remove the attribute.
1148 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1155 error = xfs_attr_leaf_lookup_int(bp, args);
1156 if (error == ENOATTR) {
1157 xfs_da_brelse(args->trans, bp);
1161 (void)xfs_attr_leaf_remove(bp, args);
1164 * If the result is small enough, shrink it all into the inode.
1166 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1167 XFS_BMAP_INIT(args->flist, args->firstblock);
1168 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1169 /* bp is gone due to xfs_da_shrink_inode */
1171 error = xfs_bmap_finish(&args->trans, args->flist,
1177 xfs_bmap_cancel(args->flist);
1182 * bmap_finish() may have committed the last trans and started
1183 * a new one. We need the inode to be in all transactions.
1186 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1187 xfs_trans_ihold(args->trans, dp);
1190 xfs_da_buf_done(bp);
1195 * Look up a name in a leaf attribute list structure.
1197 * This leaf block cannot have a "remote" value, we only call this routine
1198 * if bmap_one_block() says there is only one block (ie: no remote blks).
1201 xfs_attr_leaf_get(xfs_da_args_t *args)
1207 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1213 error = xfs_attr_leaf_lookup_int(bp, args);
1214 if (error != EEXIST) {
1215 xfs_da_brelse(args->trans, bp);
1218 error = xfs_attr_leaf_getvalue(bp, args);
1219 xfs_da_brelse(args->trans, bp);
1220 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1221 error = xfs_attr_rmtval_get(args);
1227 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1230 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1232 xfs_attr_leafblock_t *leaf;
1236 context->cursor->blkno = 0;
1237 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1239 return XFS_ERROR(error);
1242 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1243 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1244 context->dp->i_mount, leaf);
1245 xfs_da_brelse(NULL, bp);
1246 return XFS_ERROR(EFSCORRUPTED);
1249 error = xfs_attr_leaf_list_int(bp, context);
1250 xfs_da_brelse(NULL, bp);
1251 return XFS_ERROR(error);
1255 /*========================================================================
1256 * External routines when attribute list size > XFS_LBSIZE(mp).
1257 *========================================================================*/
1260 * Add a name to a Btree-format attribute list.
1262 * This will involve walking down the Btree, and may involve splitting
1263 * leaf nodes and even splitting intermediate nodes up to and including
1264 * the root node (a special case of an intermediate node).
1266 * "Remote" attribute values confuse the issue and atomic rename operations
1267 * add a whole extra layer of confusion on top of that.
1270 xfs_attr_node_addname(xfs_da_args_t *args)
1272 xfs_da_state_t *state;
1273 xfs_da_state_blk_t *blk;
1276 int committed, retval, error;
1279 * Fill in bucket of arguments/results/context to carry around.
1284 state = xfs_da_state_alloc();
1287 state->blocksize = state->mp->m_sb.sb_blocksize;
1288 state->node_ents = state->mp->m_attr_node_ents;
1291 * Search to see if name already exists, and get back a pointer
1292 * to where it should go.
1294 error = xfs_da_node_lookup_int(state, &retval);
1297 blk = &state->path.blk[ state->path.active-1 ];
1298 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1299 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1301 } else if (retval == EEXIST) {
1302 if (args->flags & ATTR_CREATE)
1304 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
1305 args->blkno2 = args->blkno; /* set 2nd entry info*/
1306 args->index2 = args->index;
1307 args->rmtblkno2 = args->rmtblkno;
1308 args->rmtblkcnt2 = args->rmtblkcnt;
1310 args->rmtblkcnt = 0;
1313 retval = xfs_attr_leaf_add(blk->bp, state->args);
1314 if (retval == ENOSPC) {
1315 if (state->path.active == 1) {
1317 * Its really a single leaf node, but it had
1318 * out-of-line values so it looked like it *might*
1319 * have been a b-tree.
1321 xfs_da_state_free(state);
1322 XFS_BMAP_INIT(args->flist, args->firstblock);
1323 error = xfs_attr_leaf_to_node(args);
1325 error = xfs_bmap_finish(&args->trans,
1332 xfs_bmap_cancel(args->flist);
1337 * bmap_finish() may have committed the last trans
1338 * and started a new one. We need the inode to be
1339 * in all transactions.
1342 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1343 xfs_trans_ihold(args->trans, dp);
1347 * Commit the node conversion and start the next
1348 * trans in the chain.
1350 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1357 * Split as many Btree elements as required.
1358 * This code tracks the new and old attr's location
1359 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1360 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1362 XFS_BMAP_INIT(args->flist, args->firstblock);
1363 error = xfs_da_split(state);
1365 error = xfs_bmap_finish(&args->trans, args->flist,
1371 xfs_bmap_cancel(args->flist);
1376 * bmap_finish() may have committed the last trans and started
1377 * a new one. We need the inode to be in all transactions.
1380 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1381 xfs_trans_ihold(args->trans, dp);
1385 * Addition succeeded, update Btree hashvals.
1387 xfs_da_fixhashpath(state, &state->path);
1391 * Kill the state structure, we're done with it and need to
1392 * allow the buffers to come back later.
1394 xfs_da_state_free(state);
1398 * Commit the leaf addition or btree split and start the next
1399 * trans in the chain.
1401 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1405 * If there was an out-of-line value, allocate the blocks we
1406 * identified for its storage and copy the value. This is done
1407 * after we create the attribute so that we don't overflow the
1408 * maximum size of a transaction and/or hit a deadlock.
1410 if (args->rmtblkno > 0) {
1411 error = xfs_attr_rmtval_set(args);
1417 * If this is an atomic rename operation, we must "flip" the
1418 * incomplete flags on the "new" and "old" attribute/value pairs
1419 * so that one disappears and one appears atomically. Then we
1420 * must remove the "old" attribute/value pair.
1422 if (args->op_flags & XFS_DA_OP_RENAME) {
1424 * In a separate transaction, set the incomplete flag on the
1425 * "old" attr and clear the incomplete flag on the "new" attr.
1427 error = xfs_attr_leaf_flipflags(args);
1432 * Dismantle the "old" attribute/value pair by removing
1433 * a "remote" value (if it exists).
1435 args->index = args->index2;
1436 args->blkno = args->blkno2;
1437 args->rmtblkno = args->rmtblkno2;
1438 args->rmtblkcnt = args->rmtblkcnt2;
1439 if (args->rmtblkno) {
1440 error = xfs_attr_rmtval_remove(args);
1446 * Re-find the "old" attribute entry after any split ops.
1447 * The INCOMPLETE flag means that we will find the "old"
1448 * attr, not the "new" one.
1450 args->flags |= XFS_ATTR_INCOMPLETE;
1451 state = xfs_da_state_alloc();
1454 state->blocksize = state->mp->m_sb.sb_blocksize;
1455 state->node_ents = state->mp->m_attr_node_ents;
1457 error = xfs_da_node_lookup_int(state, &retval);
1462 * Remove the name and update the hashvals in the tree.
1464 blk = &state->path.blk[ state->path.active-1 ];
1465 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1466 error = xfs_attr_leaf_remove(blk->bp, args);
1467 xfs_da_fixhashpath(state, &state->path);
1470 * Check to see if the tree needs to be collapsed.
1472 if (retval && (state->path.active > 1)) {
1473 XFS_BMAP_INIT(args->flist, args->firstblock);
1474 error = xfs_da_join(state);
1476 error = xfs_bmap_finish(&args->trans,
1483 xfs_bmap_cancel(args->flist);
1488 * bmap_finish() may have committed the last trans
1489 * and started a new one. We need the inode to be
1490 * in all transactions.
1493 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1494 xfs_trans_ihold(args->trans, dp);
1499 * Commit and start the next trans in the chain.
1501 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1504 } else if (args->rmtblkno > 0) {
1506 * Added a "remote" value, just clear the incomplete flag.
1508 error = xfs_attr_leaf_clearflag(args);
1516 xfs_da_state_free(state);
1523 * Remove a name from a B-tree attribute list.
1525 * This will involve walking down the Btree, and may involve joining
1526 * leaf nodes and even joining intermediate nodes up to and including
1527 * the root node (a special case of an intermediate node).
1530 xfs_attr_node_removename(xfs_da_args_t *args)
1532 xfs_da_state_t *state;
1533 xfs_da_state_blk_t *blk;
1536 int retval, error, committed, forkoff;
1539 * Tie a string around our finger to remind us where we are.
1542 state = xfs_da_state_alloc();
1544 state->mp = dp->i_mount;
1545 state->blocksize = state->mp->m_sb.sb_blocksize;
1546 state->node_ents = state->mp->m_attr_node_ents;
1549 * Search to see if name exists, and get back a pointer to it.
1551 error = xfs_da_node_lookup_int(state, &retval);
1552 if (error || (retval != EEXIST)) {
1559 * If there is an out-of-line value, de-allocate the blocks.
1560 * This is done before we remove the attribute so that we don't
1561 * overflow the maximum size of a transaction and/or hit a deadlock.
1563 blk = &state->path.blk[ state->path.active-1 ];
1564 ASSERT(blk->bp != NULL);
1565 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1566 if (args->rmtblkno > 0) {
1568 * Fill in disk block numbers in the state structure
1569 * so that we can get the buffers back after we commit
1570 * several transactions in the following calls.
1572 error = xfs_attr_fillstate(state);
1577 * Mark the attribute as INCOMPLETE, then bunmapi() the
1580 error = xfs_attr_leaf_setflag(args);
1583 error = xfs_attr_rmtval_remove(args);
1588 * Refill the state structure with buffers, the prior calls
1589 * released our buffers.
1591 error = xfs_attr_refillstate(state);
1597 * Remove the name and update the hashvals in the tree.
1599 blk = &state->path.blk[ state->path.active-1 ];
1600 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1601 retval = xfs_attr_leaf_remove(blk->bp, args);
1602 xfs_da_fixhashpath(state, &state->path);
1605 * Check to see if the tree needs to be collapsed.
1607 if (retval && (state->path.active > 1)) {
1608 XFS_BMAP_INIT(args->flist, args->firstblock);
1609 error = xfs_da_join(state);
1611 error = xfs_bmap_finish(&args->trans, args->flist,
1617 xfs_bmap_cancel(args->flist);
1622 * bmap_finish() may have committed the last trans and started
1623 * a new one. We need the inode to be in all transactions.
1626 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1627 xfs_trans_ihold(args->trans, dp);
1631 * Commit the Btree join operation and start a new trans.
1633 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1638 * If the result is small enough, push it all into the inode.
1640 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1642 * Have to get rid of the copy of this dabuf in the state.
1644 ASSERT(state->path.active == 1);
1645 ASSERT(state->path.blk[0].bp);
1646 xfs_da_buf_done(state->path.blk[0].bp);
1647 state->path.blk[0].bp = NULL;
1649 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1653 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1654 bp->data)->hdr.info.magic)
1655 == XFS_ATTR_LEAF_MAGIC);
1657 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1658 XFS_BMAP_INIT(args->flist, args->firstblock);
1659 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1660 /* bp is gone due to xfs_da_shrink_inode */
1662 error = xfs_bmap_finish(&args->trans,
1669 xfs_bmap_cancel(args->flist);
1674 * bmap_finish() may have committed the last trans
1675 * and started a new one. We need the inode to be
1676 * in all transactions.
1679 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1680 xfs_trans_ihold(args->trans, dp);
1683 xfs_da_brelse(args->trans, bp);
1688 xfs_da_state_free(state);
1693 * Fill in the disk block numbers in the state structure for the buffers
1694 * that are attached to the state structure.
1695 * This is done so that we can quickly reattach ourselves to those buffers
1696 * after some set of transaction commits have released these buffers.
1699 xfs_attr_fillstate(xfs_da_state_t *state)
1701 xfs_da_state_path_t *path;
1702 xfs_da_state_blk_t *blk;
1706 * Roll down the "path" in the state structure, storing the on-disk
1707 * block number for those buffers in the "path".
1709 path = &state->path;
1710 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1711 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1713 blk->disk_blkno = xfs_da_blkno(blk->bp);
1714 xfs_da_buf_done(blk->bp);
1717 blk->disk_blkno = 0;
1722 * Roll down the "altpath" in the state structure, storing the on-disk
1723 * block number for those buffers in the "altpath".
1725 path = &state->altpath;
1726 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1727 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1729 blk->disk_blkno = xfs_da_blkno(blk->bp);
1730 xfs_da_buf_done(blk->bp);
1733 blk->disk_blkno = 0;
1741 * Reattach the buffers to the state structure based on the disk block
1742 * numbers stored in the state structure.
1743 * This is done after some set of transaction commits have released those
1744 * buffers from our grip.
1747 xfs_attr_refillstate(xfs_da_state_t *state)
1749 xfs_da_state_path_t *path;
1750 xfs_da_state_blk_t *blk;
1754 * Roll down the "path" in the state structure, storing the on-disk
1755 * block number for those buffers in the "path".
1757 path = &state->path;
1758 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1759 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1760 if (blk->disk_blkno) {
1761 error = xfs_da_read_buf(state->args->trans,
1763 blk->blkno, blk->disk_blkno,
1764 &blk->bp, XFS_ATTR_FORK);
1773 * Roll down the "altpath" in the state structure, storing the on-disk
1774 * block number for those buffers in the "altpath".
1776 path = &state->altpath;
1777 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1778 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1779 if (blk->disk_blkno) {
1780 error = xfs_da_read_buf(state->args->trans,
1782 blk->blkno, blk->disk_blkno,
1783 &blk->bp, XFS_ATTR_FORK);
1795 * Look up a filename in a node attribute list.
1797 * This routine gets called for any attribute fork that has more than one
1798 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1799 * "remote" values taking up more blocks.
1802 xfs_attr_node_get(xfs_da_args_t *args)
1804 xfs_da_state_t *state;
1805 xfs_da_state_blk_t *blk;
1809 state = xfs_da_state_alloc();
1811 state->mp = args->dp->i_mount;
1812 state->blocksize = state->mp->m_sb.sb_blocksize;
1813 state->node_ents = state->mp->m_attr_node_ents;
1816 * Search to see if name exists, and get back a pointer to it.
1818 error = xfs_da_node_lookup_int(state, &retval);
1821 } else if (retval == EEXIST) {
1822 blk = &state->path.blk[ state->path.active-1 ];
1823 ASSERT(blk->bp != NULL);
1824 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1827 * Get the value, local or "remote"
1829 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1830 if (!retval && (args->rmtblkno > 0)
1831 && !(args->flags & ATTR_KERNOVAL)) {
1832 retval = xfs_attr_rmtval_get(args);
1837 * If not in a transaction, we have to release all the buffers.
1839 for (i = 0; i < state->path.active; i++) {
1840 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1841 state->path.blk[i].bp = NULL;
1844 xfs_da_state_free(state);
1848 STATIC int /* error */
1849 xfs_attr_node_list(xfs_attr_list_context_t *context)
1851 attrlist_cursor_kern_t *cursor;
1852 xfs_attr_leafblock_t *leaf;
1853 xfs_da_intnode_t *node;
1854 xfs_da_node_entry_t *btree;
1858 cursor = context->cursor;
1859 cursor->initted = 1;
1862 * Do all sorts of validation on the passed-in cursor structure.
1863 * If anything is amiss, ignore the cursor and look up the hashval
1864 * starting from the btree root.
1867 if (cursor->blkno > 0) {
1868 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1869 &bp, XFS_ATTR_FORK);
1870 if ((error != 0) && (error != EFSCORRUPTED))
1874 switch (be16_to_cpu(node->hdr.info.magic)) {
1875 case XFS_DA_NODE_MAGIC:
1876 xfs_attr_trace_l_cn("wrong blk", context, node);
1877 xfs_da_brelse(NULL, bp);
1880 case XFS_ATTR_LEAF_MAGIC:
1882 if (cursor->hashval > be32_to_cpu(leaf->entries[
1883 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1884 xfs_attr_trace_l_cl("wrong blk",
1886 xfs_da_brelse(NULL, bp);
1888 } else if (cursor->hashval <=
1889 be32_to_cpu(leaf->entries[0].hashval)) {
1890 xfs_attr_trace_l_cl("maybe wrong blk",
1892 xfs_da_brelse(NULL, bp);
1897 xfs_attr_trace_l_c("wrong blk - ??", context);
1898 xfs_da_brelse(NULL, bp);
1905 * We did not find what we expected given the cursor's contents,
1906 * so we start from the top and work down based on the hash value.
1907 * Note that start of node block is same as start of leaf block.
1912 error = xfs_da_read_buf(NULL, context->dp,
1913 cursor->blkno, -1, &bp,
1917 if (unlikely(bp == NULL)) {
1918 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1920 context->dp->i_mount);
1921 return(XFS_ERROR(EFSCORRUPTED));
1924 if (be16_to_cpu(node->hdr.info.magic)
1925 == XFS_ATTR_LEAF_MAGIC)
1927 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1928 != XFS_DA_NODE_MAGIC)) {
1929 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1931 context->dp->i_mount,
1933 xfs_da_brelse(NULL, bp);
1934 return(XFS_ERROR(EFSCORRUPTED));
1936 btree = node->btree;
1937 for (i = 0; i < be16_to_cpu(node->hdr.count);
1940 <= be32_to_cpu(btree->hashval)) {
1941 cursor->blkno = be32_to_cpu(btree->before);
1942 xfs_attr_trace_l_cb("descending",
1947 if (i == be16_to_cpu(node->hdr.count)) {
1948 xfs_da_brelse(NULL, bp);
1951 xfs_da_brelse(NULL, bp);
1957 * Roll upward through the blocks, processing each leaf block in
1958 * order. As long as there is space in the result buffer, keep
1959 * adding the information.
1963 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1964 != XFS_ATTR_LEAF_MAGIC)) {
1965 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1967 context->dp->i_mount, leaf);
1968 xfs_da_brelse(NULL, bp);
1969 return(XFS_ERROR(EFSCORRUPTED));
1971 error = xfs_attr_leaf_list_int(bp, context);
1973 xfs_da_brelse(NULL, bp);
1976 if (context->seen_enough || leaf->hdr.info.forw == 0)
1978 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1979 xfs_da_brelse(NULL, bp);
1980 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1981 &bp, XFS_ATTR_FORK);
1984 if (unlikely((bp == NULL))) {
1985 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1987 context->dp->i_mount);
1988 return(XFS_ERROR(EFSCORRUPTED));
1991 xfs_da_brelse(NULL, bp);
1996 /*========================================================================
1997 * External routines for manipulating out-of-line attribute values.
1998 *========================================================================*/
2001 * Read the value associated with an attribute from the out-of-line buffer
2002 * that we stored it in.
2005 xfs_attr_rmtval_get(xfs_da_args_t *args)
2007 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
2012 int nmap, error, tmp, valuelen, blkcnt, i;
2015 ASSERT(!(args->flags & ATTR_KERNOVAL));
2017 mp = args->dp->i_mount;
2019 valuelen = args->valuelen;
2020 lblkno = args->rmtblkno;
2021 while (valuelen > 0) {
2022 nmap = ATTR_RMTVALUE_MAPSIZE;
2023 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2025 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2026 NULL, 0, map, &nmap, NULL, NULL);
2031 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2032 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2033 (map[i].br_startblock != HOLESTARTBLOCK));
2034 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2035 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2036 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2037 blkcnt, XFS_BUF_LOCK, &bp);
2041 tmp = (valuelen < XFS_BUF_SIZE(bp))
2042 ? valuelen : XFS_BUF_SIZE(bp);
2043 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2048 lblkno += map[i].br_blockcount;
2051 ASSERT(valuelen == 0);
2056 * Write the value associated with an attribute into the out-of-line buffer
2057 * that we have defined for it.
2060 xfs_attr_rmtval_set(xfs_da_args_t *args)
2063 xfs_fileoff_t lfileoff;
2065 xfs_bmbt_irec_t map;
2070 int blkcnt, valuelen, nmap, error, tmp, committed;
2077 * Find a "hole" in the attribute address space large enough for
2078 * us to drop the new attribute's value into.
2080 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2082 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2087 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2088 args->rmtblkcnt = blkcnt;
2091 * Roll through the "value", allocating blocks on disk as required.
2093 while (blkcnt > 0) {
2095 * Allocate a single extent, up to the size of the value.
2097 XFS_BMAP_INIT(args->flist, args->firstblock);
2099 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2101 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2103 args->firstblock, args->total, &map, &nmap,
2106 error = xfs_bmap_finish(&args->trans, args->flist,
2112 xfs_bmap_cancel(args->flist);
2117 * bmap_finish() may have committed the last trans and started
2118 * a new one. We need the inode to be in all transactions.
2121 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2122 xfs_trans_ihold(args->trans, dp);
2126 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2127 (map.br_startblock != HOLESTARTBLOCK));
2128 lblkno += map.br_blockcount;
2129 blkcnt -= map.br_blockcount;
2132 * Start the next trans in the chain.
2134 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2139 * Roll through the "value", copying the attribute value to the
2140 * already-allocated blocks. Blocks are written synchronously
2141 * so that we can know they are all on disk before we turn off
2142 * the INCOMPLETE flag.
2144 lblkno = args->rmtblkno;
2145 valuelen = args->valuelen;
2146 while (valuelen > 0) {
2148 * Try to remember where we decided to put the value.
2150 XFS_BMAP_INIT(args->flist, args->firstblock);
2152 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2154 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2155 args->firstblock, 0, &map, &nmap,
2161 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2162 (map.br_startblock != HOLESTARTBLOCK));
2164 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2165 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2167 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2168 blkcnt, XFS_BUF_LOCK);
2170 ASSERT(!XFS_BUF_GETERROR(bp));
2172 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2174 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2175 if (tmp < XFS_BUF_SIZE(bp))
2176 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2177 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2183 lblkno += map.br_blockcount;
2185 ASSERT(valuelen == 0);
2190 * Remove the value associated with an attribute by deleting the
2191 * out-of-line buffer that it is stored on.
2194 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2197 xfs_bmbt_irec_t map;
2201 int valuelen, blkcnt, nmap, error, done, committed;
2203 mp = args->dp->i_mount;
2206 * Roll through the "value", invalidating the attribute value's
2209 lblkno = args->rmtblkno;
2210 valuelen = args->rmtblkcnt;
2211 while (valuelen > 0) {
2213 * Try to remember where we decided to put the value.
2215 XFS_BMAP_INIT(args->flist, args->firstblock);
2217 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2219 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2220 args->firstblock, 0, &map, &nmap,
2226 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2227 (map.br_startblock != HOLESTARTBLOCK));
2229 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2230 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2233 * If the "remote" value is in the cache, remove it.
2235 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2236 XFS_INCORE_TRYLOCK);
2239 XFS_BUF_UNDELAYWRITE(bp);
2244 valuelen -= map.br_blockcount;
2246 lblkno += map.br_blockcount;
2250 * Keep de-allocating extents until the remote-value region is gone.
2252 lblkno = args->rmtblkno;
2253 blkcnt = args->rmtblkcnt;
2256 XFS_BMAP_INIT(args->flist, args->firstblock);
2257 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2258 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2259 1, args->firstblock, args->flist,
2262 error = xfs_bmap_finish(&args->trans, args->flist,
2268 xfs_bmap_cancel(args->flist);
2273 * bmap_finish() may have committed the last trans and started
2274 * a new one. We need the inode to be in all transactions.
2277 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2278 xfs_trans_ihold(args->trans, args->dp);
2282 * Close out trans and start the next one in the chain.
2284 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2290 #if defined(XFS_ATTR_TRACE)
2292 * Add a trace buffer entry for an attr_list context structure.
2295 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2297 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where, context,
2298 (__psunsigned_t)NULL,
2299 (__psunsigned_t)NULL,
2300 (__psunsigned_t)NULL);
2304 * Add a trace buffer entry for a context structure and a Btree node.
2307 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2308 struct xfs_da_intnode *node)
2310 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where, context,
2311 (__psunsigned_t)be16_to_cpu(node->hdr.count),
2312 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
2313 (__psunsigned_t)be32_to_cpu(node->btree[
2314 be16_to_cpu(node->hdr.count)-1].hashval));
2318 * Add a trace buffer entry for a context structure and a Btree element.
2321 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2322 struct xfs_da_node_entry *btree)
2324 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where, context,
2325 (__psunsigned_t)be32_to_cpu(btree->hashval),
2326 (__psunsigned_t)be32_to_cpu(btree->before),
2327 (__psunsigned_t)NULL);
2331 * Add a trace buffer entry for a context structure and a leaf block.
2334 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2335 struct xfs_attr_leafblock *leaf)
2337 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where, context,
2338 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
2339 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2340 (__psunsigned_t)be32_to_cpu(leaf->entries[
2341 be16_to_cpu(leaf->hdr.count)-1].hashval));
2345 * Add a trace buffer entry for the arguments given to the routine,
2349 xfs_attr_trace_enter(int type, char *where,
2350 struct xfs_attr_list_context *context,
2351 __psunsigned_t a13, __psunsigned_t a14,
2354 ASSERT(xfs_attr_trace_buf);
2355 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2356 (void *)((__psunsigned_t)where),
2357 (void *)((__psunsigned_t)context->dp),
2358 (void *)((__psunsigned_t)context->cursor->hashval),
2359 (void *)((__psunsigned_t)context->cursor->blkno),
2360 (void *)((__psunsigned_t)context->cursor->offset),
2361 (void *)((__psunsigned_t)context->alist),
2362 (void *)((__psunsigned_t)context->bufsize),
2363 (void *)((__psunsigned_t)context->count),
2364 (void *)((__psunsigned_t)context->firstu),
2365 (void *)((__psunsigned_t)
2366 (((context->count > 0) &&
2367 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2368 ? (ATTR_ENTRY(context->alist,
2369 context->count-1)->a_valuelen)
2371 (void *)((__psunsigned_t)context->dupcnt),
2372 (void *)((__psunsigned_t)context->flags),
2373 (void *)a13, (void *)a14, (void *)a15);
2375 #endif /* XFS_ATTR_TRACE */