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"
54 * Provide the external interfaces to manage attribute lists.
57 /*========================================================================
58 * Function prototypes for the kernel.
59 *========================================================================*/
62 * Internal routines when attribute list fits inside the inode.
64 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
67 * Internal routines when attribute list is one block.
69 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
70 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_leaf_list(xfs_attr_list_context_t
*context
);
75 * Internal routines when attribute list is more than one block.
77 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
78 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
79 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
80 STATIC
int xfs_attr_node_list(xfs_attr_list_context_t
*context
);
81 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
82 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
85 * Routines to manipulate out-of-line attribute values.
87 STATIC
int xfs_attr_rmtval_set(xfs_da_args_t
*args
);
88 STATIC
int xfs_attr_rmtval_remove(xfs_da_args_t
*args
);
90 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
92 #if defined(XFS_ATTR_TRACE)
93 ktrace_t
*xfs_attr_trace_buf
;
97 xfs_attr_name_to_xname(
98 struct xfs_name
*xname
,
104 xname
->len
= strlen(aname
);
105 if (xname
->len
>= MAXNAMELEN
)
106 return EFAULT
; /* match IRIX behaviour */
113 struct xfs_inode
*ip
)
115 if (!XFS_IFORK_Q(ip
) ||
116 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
117 ip
->i_d
.di_anextents
== 0))
122 /*========================================================================
123 * Overall external interface routines.
124 *========================================================================*/
128 struct xfs_inode
*ip
,
129 struct xfs_name
*name
,
137 if (!xfs_inode_hasattr(ip
))
141 * Fill in the arg structure for this request.
143 memset((char *)&args
, 0, sizeof(args
));
144 args
.name
= name
->name
;
145 args
.namelen
= name
->len
;
147 args
.valuelen
= *valuelenp
;
149 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
151 args
.whichfork
= XFS_ATTR_FORK
;
154 * Decide on what work routines to call based on the inode size.
156 if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
157 error
= xfs_attr_shortform_getvalue(&args
);
158 } else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
)) {
159 error
= xfs_attr_leaf_get(&args
);
161 error
= xfs_attr_node_get(&args
);
165 * Return the number of bytes in the value to the caller.
167 *valuelenp
= args
.valuelen
;
183 struct xfs_name xname
;
185 XFS_STATS_INC(xs_attr_get
);
187 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
190 error
= xfs_attr_name_to_xname(&xname
, name
);
194 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
195 error
= xfs_attr_get_int(ip
, &xname
, value
, valuelenp
, flags
);
196 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
201 * Calculate how many blocks we need for the new attribute,
205 struct xfs_inode
*ip
,
210 struct xfs_mount
*mp
= ip
->i_mount
;
215 * Determine space new attribute will use, and if it would be
216 * "local" or "remote" (note: local != inline).
218 size
= xfs_attr_leaf_newentsize(namelen
, valuelen
,
219 mp
->m_sb
.sb_blocksize
, local
);
221 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
223 if (size
> (mp
->m_sb
.sb_blocksize
>> 1)) {
224 /* Double split possible */
229 * Out of line attribute, cannot double split, but
230 * make room for the attribute value itself.
232 uint dblocks
= XFS_B_TO_FSB(mp
, valuelen
);
234 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
241 xfs_attr_set_int(xfs_inode_t
*dp
, struct xfs_name
*name
,
242 char *value
, int valuelen
, int flags
)
245 xfs_fsblock_t firstblock
;
246 xfs_bmap_free_t flist
;
247 int error
, err2
, committed
;
248 xfs_mount_t
*mp
= dp
->i_mount
;
249 int rsvd
= (flags
& ATTR_ROOT
) != 0;
253 * Attach the dquots to the inode.
255 error
= xfs_qm_dqattach(dp
, 0);
260 * If the inode doesn't have an attribute fork, add one.
261 * (inode must not be locked when we call this routine)
263 if (XFS_IFORK_Q(dp
) == 0) {
264 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
265 XFS_ATTR_SF_ENTSIZE_BYNAME(name
->len
, valuelen
);
267 if ((error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
)))
272 * Fill in the arg structure for this request.
274 memset((char *)&args
, 0, sizeof(args
));
275 args
.name
= name
->name
;
276 args
.namelen
= name
->len
;
278 args
.valuelen
= valuelen
;
280 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
282 args
.firstblock
= &firstblock
;
284 args
.whichfork
= XFS_ATTR_FORK
;
285 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
287 /* Size is now blocks for attribute data */
288 args
.total
= xfs_attr_calc_size(dp
, name
->len
, valuelen
, &local
);
291 * Start our first transaction of the day.
293 * All future transactions during this code must be "chained" off
294 * this one via the trans_dup() call. All transactions will contain
295 * the inode, and the inode will always be marked with trans_ihold().
296 * Since the inode will be locked in all transactions, we must log
297 * the inode in every transaction to let it float upward through
300 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
303 * Root fork attributes can use reserved data blocks for this
304 * operation if necessary
308 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
310 if ((error
= xfs_trans_reserve(args
.trans
, args
.total
,
311 XFS_ATTRSET_LOG_RES(mp
, args
.total
), 0,
312 XFS_TRANS_PERM_LOG_RES
, XFS_ATTRSET_LOG_COUNT
))) {
313 xfs_trans_cancel(args
.trans
, 0);
316 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
318 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
319 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
320 XFS_QMOPT_RES_REGBLKS
);
322 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
323 xfs_trans_cancel(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
327 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
328 xfs_trans_ihold(args
.trans
, dp
);
331 * If the attribute list is non-existent or a shortform list,
332 * upgrade it to a single-leaf-block attribute list.
334 if ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) ||
335 ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) &&
336 (dp
->i_d
.di_anextents
== 0))) {
339 * Build initial attribute list (if required).
341 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
342 xfs_attr_shortform_create(&args
);
345 * Try to add the attr to the attribute list in
348 error
= xfs_attr_shortform_addname(&args
);
349 if (error
!= ENOSPC
) {
351 * Commit the shortform mods, and we're done.
352 * NOTE: this is also the error path (EEXIST, etc).
354 ASSERT(args
.trans
!= NULL
);
357 * If this is a synchronous mount, make sure that
358 * the transaction goes to disk before returning
361 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
362 xfs_trans_set_sync(args
.trans
);
364 err2
= xfs_trans_commit(args
.trans
,
365 XFS_TRANS_RELEASE_LOG_RES
);
366 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
369 * Hit the inode change time.
371 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
372 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
374 return(error
== 0 ? err2
: error
);
378 * It won't fit in the shortform, transform to a leaf block.
379 * GROT: another possible req'mt for a double-split btree op.
381 xfs_bmap_init(args
.flist
, args
.firstblock
);
382 error
= xfs_attr_shortform_to_leaf(&args
);
384 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
390 xfs_bmap_cancel(&flist
);
395 * bmap_finish() may have committed the last trans and started
396 * a new one. We need the inode to be in all transactions.
399 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
400 xfs_trans_ihold(args
.trans
, dp
);
404 * Commit the leaf transformation. We'll need another (linked)
405 * transaction to add the new attribute to the leaf.
408 error
= xfs_trans_roll(&args
.trans
, dp
);
414 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
415 error
= xfs_attr_leaf_addname(&args
);
417 error
= xfs_attr_node_addname(&args
);
424 * If this is a synchronous mount, make sure that the
425 * transaction goes to disk before returning to the user.
427 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
428 xfs_trans_set_sync(args
.trans
);
432 * Commit the last in the sequence of transactions.
434 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
435 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
436 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
439 * Hit the inode change time.
441 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
442 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
449 xfs_trans_cancel(args
.trans
,
450 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
451 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
464 struct xfs_name xname
;
466 XFS_STATS_INC(xs_attr_set
);
468 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
471 error
= xfs_attr_name_to_xname(&xname
, name
);
475 return xfs_attr_set_int(dp
, &xname
, value
, valuelen
, flags
);
479 * Generic handler routine to remove a name from an attribute list.
480 * Transitions attribute list from Btree to shortform as necessary.
483 xfs_attr_remove_int(xfs_inode_t
*dp
, struct xfs_name
*name
, int flags
)
486 xfs_fsblock_t firstblock
;
487 xfs_bmap_free_t flist
;
489 xfs_mount_t
*mp
= dp
->i_mount
;
492 * Fill in the arg structure for this request.
494 memset((char *)&args
, 0, sizeof(args
));
495 args
.name
= name
->name
;
496 args
.namelen
= name
->len
;
498 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
500 args
.firstblock
= &firstblock
;
503 args
.whichfork
= XFS_ATTR_FORK
;
506 * Attach the dquots to the inode.
508 error
= xfs_qm_dqattach(dp
, 0);
513 * Start our first transaction of the day.
515 * All future transactions during this code must be "chained" off
516 * this one via the trans_dup() call. All transactions will contain
517 * the inode, and the inode will always be marked with trans_ihold().
518 * Since the inode will be locked in all transactions, we must log
519 * the inode in every transaction to let it float upward through
522 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
525 * Root fork attributes can use reserved data blocks for this
526 * operation if necessary
529 if (flags
& ATTR_ROOT
)
530 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
532 if ((error
= xfs_trans_reserve(args
.trans
,
533 XFS_ATTRRM_SPACE_RES(mp
),
534 XFS_ATTRRM_LOG_RES(mp
),
535 0, XFS_TRANS_PERM_LOG_RES
,
536 XFS_ATTRRM_LOG_COUNT
))) {
537 xfs_trans_cancel(args
.trans
, 0);
541 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
543 * No need to make quota reservations here. We expect to release some
544 * blocks not allocate in the common case.
546 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
547 xfs_trans_ihold(args
.trans
, dp
);
550 * Decide on what work routines to call based on the inode size.
552 if (!xfs_inode_hasattr(dp
)) {
553 error
= XFS_ERROR(ENOATTR
);
556 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
557 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
558 error
= xfs_attr_shortform_remove(&args
);
562 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
563 error
= xfs_attr_leaf_removename(&args
);
565 error
= xfs_attr_node_removename(&args
);
572 * If this is a synchronous mount, make sure that the
573 * transaction goes to disk before returning to the user.
575 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
576 xfs_trans_set_sync(args
.trans
);
580 * Commit the last in the sequence of transactions.
582 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
583 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
584 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
587 * Hit the inode change time.
589 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
590 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
597 xfs_trans_cancel(args
.trans
,
598 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
599 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
610 struct xfs_name xname
;
612 XFS_STATS_INC(xs_attr_remove
);
614 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
617 error
= xfs_attr_name_to_xname(&xname
, name
);
621 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
622 if (!xfs_inode_hasattr(dp
)) {
623 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
624 return XFS_ERROR(ENOATTR
);
626 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
628 return xfs_attr_remove_int(dp
, &xname
, flags
);
632 xfs_attr_list_int(xfs_attr_list_context_t
*context
)
635 xfs_inode_t
*dp
= context
->dp
;
637 XFS_STATS_INC(xs_attr_list
);
639 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
642 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
643 xfs_attr_trace_l_c("syscall start", context
);
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
);
659 xfs_attr_trace_l_c("syscall end", context
);
664 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
665 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
666 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
667 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
668 & ~(sizeof(u_int32_t)-1))
671 * Format an attribute and copy it out to the user's buffer.
672 * Take care to check values and protect against them changing later,
673 * we may be reading them directly out of a user buffer.
677 xfs_attr_put_listent(xfs_attr_list_context_t
*context
, int flags
,
678 char *name
, int namelen
,
679 int valuelen
, char *value
)
681 struct attrlist
*alist
= (struct attrlist
*)context
->alist
;
685 ASSERT(!(context
->flags
& ATTR_KERNOVAL
));
686 ASSERT(context
->count
>= 0);
687 ASSERT(context
->count
< (ATTR_MAX_VALUELEN
/8));
688 ASSERT(context
->firstu
>= sizeof(*alist
));
689 ASSERT(context
->firstu
<= context
->bufsize
);
692 * Only list entries in the right namespace.
694 if (((context
->flags
& ATTR_SECURE
) == 0) !=
695 ((flags
& XFS_ATTR_SECURE
) == 0))
697 if (((context
->flags
& ATTR_ROOT
) == 0) !=
698 ((flags
& XFS_ATTR_ROOT
) == 0))
701 arraytop
= sizeof(*alist
) +
702 context
->count
* sizeof(alist
->al_offset
[0]);
703 context
->firstu
-= ATTR_ENTSIZE(namelen
);
704 if (context
->firstu
< arraytop
) {
705 xfs_attr_trace_l_c("buffer full", context
);
707 context
->seen_enough
= 1;
711 aep
= (attrlist_ent_t
*)&context
->alist
[context
->firstu
];
712 aep
->a_valuelen
= valuelen
;
713 memcpy(aep
->a_name
, name
, namelen
);
714 aep
->a_name
[namelen
] = 0;
715 alist
->al_offset
[context
->count
++] = context
->firstu
;
716 alist
->al_count
= context
->count
;
717 xfs_attr_trace_l_c("add", context
);
722 * Generate a list of extended attribute names and optionally
723 * also value lengths. Positive return value follows the XFS
724 * convention of being an error, zero or negative return code
725 * is the length of the buffer returned (negated), indicating
734 attrlist_cursor_kern_t
*cursor
)
736 xfs_attr_list_context_t context
;
737 struct attrlist
*alist
;
741 * Validate the cursor.
743 if (cursor
->pad1
|| cursor
->pad2
)
744 return(XFS_ERROR(EINVAL
));
745 if ((cursor
->initted
== 0) &&
746 (cursor
->hashval
|| cursor
->blkno
|| cursor
->offset
))
747 return XFS_ERROR(EINVAL
);
750 * Check for a properly aligned buffer.
752 if (((long)buffer
) & (sizeof(int)-1))
753 return XFS_ERROR(EFAULT
);
754 if (flags
& ATTR_KERNOVAL
)
758 * Initialize the output buffer.
760 memset(&context
, 0, sizeof(context
));
762 context
.cursor
= cursor
;
764 context
.flags
= flags
;
765 context
.alist
= buffer
;
766 context
.bufsize
= (bufsize
& ~(sizeof(int)-1)); /* align */
767 context
.firstu
= context
.bufsize
;
768 context
.put_listent
= xfs_attr_put_listent
;
770 alist
= (struct attrlist
*)context
.alist
;
773 alist
->al_offset
[0] = context
.bufsize
;
775 error
= xfs_attr_list_int(&context
);
781 xfs_attr_inactive(xfs_inode_t
*dp
)
788 ASSERT(! XFS_NOT_DQATTACHED(mp
, dp
));
790 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
791 if (!xfs_inode_hasattr(dp
) ||
792 dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
793 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
796 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
799 * Start our first transaction of the day.
801 * All future transactions during this code must be "chained" off
802 * this one via the trans_dup() call. All transactions will contain
803 * the inode, and the inode will always be marked with trans_ihold().
804 * Since the inode will be locked in all transactions, we must log
805 * the inode in every transaction to let it float upward through
808 trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTRINVAL
);
809 if ((error
= xfs_trans_reserve(trans
, 0, XFS_ATTRINVAL_LOG_RES(mp
), 0,
810 XFS_TRANS_PERM_LOG_RES
,
811 XFS_ATTRINVAL_LOG_COUNT
))) {
812 xfs_trans_cancel(trans
, 0);
815 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
818 * No need to make quota reservations here. We expect to release some
819 * blocks, not allocate, in the common case.
821 xfs_trans_ijoin(trans
, dp
, XFS_ILOCK_EXCL
);
822 xfs_trans_ihold(trans
, dp
);
825 * Decide on what work routines to call based on the inode size.
827 if (!xfs_inode_hasattr(dp
) ||
828 dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
832 error
= xfs_attr_root_inactive(&trans
, dp
);
836 * signal synchronous inactive transactions unless this
837 * is a synchronous mount filesystem in which case we
838 * know that we're here because we've been called out of
839 * xfs_inactive which means that the last reference is gone
840 * and the unlink transaction has already hit the disk so
841 * async inactive transactions are safe.
843 if ((error
= xfs_itruncate_finish(&trans
, dp
, 0LL, XFS_ATTR_FORK
,
844 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
)
849 * Commit the last in the sequence of transactions.
851 xfs_trans_log_inode(trans
, dp
, XFS_ILOG_CORE
);
852 error
= xfs_trans_commit(trans
, XFS_TRANS_RELEASE_LOG_RES
);
853 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
858 xfs_trans_cancel(trans
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
859 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
865 /*========================================================================
866 * External routines when attribute list is inside the inode
867 *========================================================================*/
870 * Add a name to the shortform attribute list structure
871 * This is the external routine.
874 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
876 int newsize
, forkoff
, retval
;
878 retval
= xfs_attr_shortform_lookup(args
);
879 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
881 } else if (retval
== EEXIST
) {
882 if (args
->flags
& ATTR_CREATE
)
884 retval
= xfs_attr_shortform_remove(args
);
888 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
889 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
890 return(XFS_ERROR(ENOSPC
));
892 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
893 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
895 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
897 return(XFS_ERROR(ENOSPC
));
899 xfs_attr_shortform_add(args
, forkoff
);
904 /*========================================================================
905 * External routines when attribute list is one block
906 *========================================================================*/
909 * Add a name to the leaf attribute list structure
911 * This leaf block cannot have a "remote" value, we only call this routine
912 * if bmap_one_block() says there is only one block (ie: no remote blks).
915 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
919 int retval
, error
, committed
, forkoff
;
922 * Read the (only) block in the attribute list in.
926 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
933 * Look up the given attribute in the leaf block. Figure out if
934 * the given flags produce an error or call for an atomic rename.
936 retval
= xfs_attr_leaf_lookup_int(bp
, args
);
937 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
938 xfs_da_brelse(args
->trans
, bp
);
940 } else if (retval
== EEXIST
) {
941 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
942 xfs_da_brelse(args
->trans
, bp
);
945 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
946 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
947 args
->index2
= args
->index
;
948 args
->rmtblkno2
= args
->rmtblkno
;
949 args
->rmtblkcnt2
= args
->rmtblkcnt
;
953 * Add the attribute to the leaf block, transitioning to a Btree
956 retval
= xfs_attr_leaf_add(bp
, args
);
958 if (retval
== ENOSPC
) {
960 * Promote the attribute list to the Btree format, then
961 * Commit that transaction so that the node_addname() call
962 * can manage its own transactions.
964 xfs_bmap_init(args
->flist
, args
->firstblock
);
965 error
= xfs_attr_leaf_to_node(args
);
967 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
973 xfs_bmap_cancel(args
->flist
);
978 * bmap_finish() may have committed the last trans and started
979 * a new one. We need the inode to be in all transactions.
982 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
983 xfs_trans_ihold(args
->trans
, dp
);
987 * Commit the current trans (including the inode) and start
990 error
= xfs_trans_roll(&args
->trans
, dp
);
995 * Fob the whole rest of the problem off on the Btree code.
997 error
= xfs_attr_node_addname(args
);
1002 * Commit the transaction that added the attr name so that
1003 * later routines can manage their own transactions.
1005 error
= xfs_trans_roll(&args
->trans
, dp
);
1010 * If there was an out-of-line value, allocate the blocks we
1011 * identified for its storage and copy the value. This is done
1012 * after we create the attribute so that we don't overflow the
1013 * maximum size of a transaction and/or hit a deadlock.
1015 if (args
->rmtblkno
> 0) {
1016 error
= xfs_attr_rmtval_set(args
);
1022 * If this is an atomic rename operation, we must "flip" the
1023 * incomplete flags on the "new" and "old" attribute/value pairs
1024 * so that one disappears and one appears atomically. Then we
1025 * must remove the "old" attribute/value pair.
1027 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1029 * In a separate transaction, set the incomplete flag on the
1030 * "old" attr and clear the incomplete flag on the "new" attr.
1032 error
= xfs_attr_leaf_flipflags(args
);
1037 * Dismantle the "old" attribute/value pair by removing
1038 * a "remote" value (if it exists).
1040 args
->index
= args
->index2
;
1041 args
->blkno
= args
->blkno2
;
1042 args
->rmtblkno
= args
->rmtblkno2
;
1043 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1044 if (args
->rmtblkno
) {
1045 error
= xfs_attr_rmtval_remove(args
);
1051 * Read in the block containing the "old" attr, then
1052 * remove the "old" attr from that block (neat, huh!)
1054 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1,
1055 &bp
, XFS_ATTR_FORK
);
1059 (void)xfs_attr_leaf_remove(bp
, args
);
1062 * If the result is small enough, shrink it all into the inode.
1064 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1065 xfs_bmap_init(args
->flist
, args
->firstblock
);
1066 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1067 /* bp is gone due to xfs_da_shrink_inode */
1069 error
= xfs_bmap_finish(&args
->trans
,
1076 xfs_bmap_cancel(args
->flist
);
1081 * bmap_finish() may have committed the last trans
1082 * and started a new one. We need the inode to be
1083 * in all transactions.
1086 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1087 xfs_trans_ihold(args
->trans
, dp
);
1090 xfs_da_buf_done(bp
);
1093 * Commit the remove and start the next trans in series.
1095 error
= xfs_trans_roll(&args
->trans
, dp
);
1097 } else if (args
->rmtblkno
> 0) {
1099 * Added a "remote" value, just clear the incomplete flag.
1101 error
= xfs_attr_leaf_clearflag(args
);
1107 * Remove a name from the leaf attribute list structure
1109 * This leaf block cannot have a "remote" value, we only call this routine
1110 * if bmap_one_block() says there is only one block (ie: no remote blks).
1113 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
1117 int error
, committed
, forkoff
;
1120 * Remove the attribute.
1124 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
1131 error
= xfs_attr_leaf_lookup_int(bp
, args
);
1132 if (error
== ENOATTR
) {
1133 xfs_da_brelse(args
->trans
, bp
);
1137 (void)xfs_attr_leaf_remove(bp
, args
);
1140 * If the result is small enough, shrink it all into the inode.
1142 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1143 xfs_bmap_init(args
->flist
, args
->firstblock
);
1144 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1145 /* bp is gone due to xfs_da_shrink_inode */
1147 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1153 xfs_bmap_cancel(args
->flist
);
1158 * bmap_finish() may have committed the last trans and started
1159 * a new one. We need the inode to be in all transactions.
1162 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1163 xfs_trans_ihold(args
->trans
, dp
);
1166 xfs_da_buf_done(bp
);
1171 * Look up a name in a leaf attribute list structure.
1173 * This leaf block cannot have a "remote" value, we only call this routine
1174 * if bmap_one_block() says there is only one block (ie: no remote blks).
1177 xfs_attr_leaf_get(xfs_da_args_t
*args
)
1183 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
1189 error
= xfs_attr_leaf_lookup_int(bp
, args
);
1190 if (error
!= EEXIST
) {
1191 xfs_da_brelse(args
->trans
, bp
);
1194 error
= xfs_attr_leaf_getvalue(bp
, args
);
1195 xfs_da_brelse(args
->trans
, bp
);
1196 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
1197 error
= xfs_attr_rmtval_get(args
);
1203 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1206 xfs_attr_leaf_list(xfs_attr_list_context_t
*context
)
1208 xfs_attr_leafblock_t
*leaf
;
1212 context
->cursor
->blkno
= 0;
1213 error
= xfs_da_read_buf(NULL
, context
->dp
, 0, -1, &bp
, XFS_ATTR_FORK
);
1215 return XFS_ERROR(error
);
1218 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
) != XFS_ATTR_LEAF_MAGIC
)) {
1219 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW
,
1220 context
->dp
->i_mount
, leaf
);
1221 xfs_da_brelse(NULL
, bp
);
1222 return XFS_ERROR(EFSCORRUPTED
);
1225 error
= xfs_attr_leaf_list_int(bp
, context
);
1226 xfs_da_brelse(NULL
, bp
);
1227 return XFS_ERROR(error
);
1231 /*========================================================================
1232 * External routines when attribute list size > XFS_LBSIZE(mp).
1233 *========================================================================*/
1236 * Add a name to a Btree-format attribute list.
1238 * This will involve walking down the Btree, and may involve splitting
1239 * leaf nodes and even splitting intermediate nodes up to and including
1240 * the root node (a special case of an intermediate node).
1242 * "Remote" attribute values confuse the issue and atomic rename operations
1243 * add a whole extra layer of confusion on top of that.
1246 xfs_attr_node_addname(xfs_da_args_t
*args
)
1248 xfs_da_state_t
*state
;
1249 xfs_da_state_blk_t
*blk
;
1252 int committed
, retval
, error
;
1255 * Fill in bucket of arguments/results/context to carry around.
1260 state
= xfs_da_state_alloc();
1263 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1264 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1267 * Search to see if name already exists, and get back a pointer
1268 * to where it should go.
1270 error
= xfs_da_node_lookup_int(state
, &retval
);
1273 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1274 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1275 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
1277 } else if (retval
== EEXIST
) {
1278 if (args
->flags
& ATTR_CREATE
)
1280 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
1281 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
1282 args
->index2
= args
->index
;
1283 args
->rmtblkno2
= args
->rmtblkno
;
1284 args
->rmtblkcnt2
= args
->rmtblkcnt
;
1286 args
->rmtblkcnt
= 0;
1289 retval
= xfs_attr_leaf_add(blk
->bp
, state
->args
);
1290 if (retval
== ENOSPC
) {
1291 if (state
->path
.active
== 1) {
1293 * Its really a single leaf node, but it had
1294 * out-of-line values so it looked like it *might*
1295 * have been a b-tree.
1297 xfs_da_state_free(state
);
1298 xfs_bmap_init(args
->flist
, args
->firstblock
);
1299 error
= xfs_attr_leaf_to_node(args
);
1301 error
= xfs_bmap_finish(&args
->trans
,
1308 xfs_bmap_cancel(args
->flist
);
1313 * bmap_finish() may have committed the last trans
1314 * and started a new one. We need the inode to be
1315 * in all transactions.
1318 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1319 xfs_trans_ihold(args
->trans
, dp
);
1323 * Commit the node conversion and start the next
1324 * trans in the chain.
1326 error
= xfs_trans_roll(&args
->trans
, dp
);
1334 * Split as many Btree elements as required.
1335 * This code tracks the new and old attr's location
1336 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1337 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1339 xfs_bmap_init(args
->flist
, args
->firstblock
);
1340 error
= xfs_da_split(state
);
1342 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1348 xfs_bmap_cancel(args
->flist
);
1353 * bmap_finish() may have committed the last trans and started
1354 * a new one. We need the inode to be in all transactions.
1357 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1358 xfs_trans_ihold(args
->trans
, dp
);
1362 * Addition succeeded, update Btree hashvals.
1364 xfs_da_fixhashpath(state
, &state
->path
);
1368 * Kill the state structure, we're done with it and need to
1369 * allow the buffers to come back later.
1371 xfs_da_state_free(state
);
1375 * Commit the leaf addition or btree split and start the next
1376 * trans in the chain.
1378 error
= xfs_trans_roll(&args
->trans
, dp
);
1383 * If there was an out-of-line value, allocate the blocks we
1384 * identified for its storage and copy the value. This is done
1385 * after we create the attribute so that we don't overflow the
1386 * maximum size of a transaction and/or hit a deadlock.
1388 if (args
->rmtblkno
> 0) {
1389 error
= xfs_attr_rmtval_set(args
);
1395 * If this is an atomic rename operation, we must "flip" the
1396 * incomplete flags on the "new" and "old" attribute/value pairs
1397 * so that one disappears and one appears atomically. Then we
1398 * must remove the "old" attribute/value pair.
1400 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1402 * In a separate transaction, set the incomplete flag on the
1403 * "old" attr and clear the incomplete flag on the "new" attr.
1405 error
= xfs_attr_leaf_flipflags(args
);
1410 * Dismantle the "old" attribute/value pair by removing
1411 * a "remote" value (if it exists).
1413 args
->index
= args
->index2
;
1414 args
->blkno
= args
->blkno2
;
1415 args
->rmtblkno
= args
->rmtblkno2
;
1416 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1417 if (args
->rmtblkno
) {
1418 error
= xfs_attr_rmtval_remove(args
);
1424 * Re-find the "old" attribute entry after any split ops.
1425 * The INCOMPLETE flag means that we will find the "old"
1426 * attr, not the "new" one.
1428 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1429 state
= xfs_da_state_alloc();
1432 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1433 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1435 error
= xfs_da_node_lookup_int(state
, &retval
);
1440 * Remove the name and update the hashvals in the tree.
1442 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1443 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1444 error
= xfs_attr_leaf_remove(blk
->bp
, args
);
1445 xfs_da_fixhashpath(state
, &state
->path
);
1448 * Check to see if the tree needs to be collapsed.
1450 if (retval
&& (state
->path
.active
> 1)) {
1451 xfs_bmap_init(args
->flist
, args
->firstblock
);
1452 error
= xfs_da_join(state
);
1454 error
= xfs_bmap_finish(&args
->trans
,
1461 xfs_bmap_cancel(args
->flist
);
1466 * bmap_finish() may have committed the last trans
1467 * and started a new one. We need the inode to be
1468 * in all transactions.
1471 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1472 xfs_trans_ihold(args
->trans
, dp
);
1477 * Commit and start the next trans in the chain.
1479 error
= xfs_trans_roll(&args
->trans
, dp
);
1483 } else if (args
->rmtblkno
> 0) {
1485 * Added a "remote" value, just clear the incomplete flag.
1487 error
= xfs_attr_leaf_clearflag(args
);
1495 xfs_da_state_free(state
);
1502 * Remove a name from a B-tree attribute list.
1504 * This will involve walking down the Btree, and may involve joining
1505 * leaf nodes and even joining intermediate nodes up to and including
1506 * the root node (a special case of an intermediate node).
1509 xfs_attr_node_removename(xfs_da_args_t
*args
)
1511 xfs_da_state_t
*state
;
1512 xfs_da_state_blk_t
*blk
;
1515 int retval
, error
, committed
, forkoff
;
1518 * Tie a string around our finger to remind us where we are.
1521 state
= xfs_da_state_alloc();
1523 state
->mp
= dp
->i_mount
;
1524 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1525 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1528 * Search to see if name exists, and get back a pointer to it.
1530 error
= xfs_da_node_lookup_int(state
, &retval
);
1531 if (error
|| (retval
!= EEXIST
)) {
1538 * If there is an out-of-line value, de-allocate the blocks.
1539 * This is done before we remove the attribute so that we don't
1540 * overflow the maximum size of a transaction and/or hit a deadlock.
1542 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1543 ASSERT(blk
->bp
!= NULL
);
1544 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1545 if (args
->rmtblkno
> 0) {
1547 * Fill in disk block numbers in the state structure
1548 * so that we can get the buffers back after we commit
1549 * several transactions in the following calls.
1551 error
= xfs_attr_fillstate(state
);
1556 * Mark the attribute as INCOMPLETE, then bunmapi() the
1559 error
= xfs_attr_leaf_setflag(args
);
1562 error
= xfs_attr_rmtval_remove(args
);
1567 * Refill the state structure with buffers, the prior calls
1568 * released our buffers.
1570 error
= xfs_attr_refillstate(state
);
1576 * Remove the name and update the hashvals in the tree.
1578 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1579 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1580 retval
= xfs_attr_leaf_remove(blk
->bp
, args
);
1581 xfs_da_fixhashpath(state
, &state
->path
);
1584 * Check to see if the tree needs to be collapsed.
1586 if (retval
&& (state
->path
.active
> 1)) {
1587 xfs_bmap_init(args
->flist
, args
->firstblock
);
1588 error
= xfs_da_join(state
);
1590 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1596 xfs_bmap_cancel(args
->flist
);
1601 * bmap_finish() may have committed the last trans and started
1602 * a new one. We need the inode to be in all transactions.
1605 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1606 xfs_trans_ihold(args
->trans
, dp
);
1610 * Commit the Btree join operation and start a new trans.
1612 error
= xfs_trans_roll(&args
->trans
, dp
);
1618 * If the result is small enough, push it all into the inode.
1620 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1622 * Have to get rid of the copy of this dabuf in the state.
1624 ASSERT(state
->path
.active
== 1);
1625 ASSERT(state
->path
.blk
[0].bp
);
1626 xfs_da_buf_done(state
->path
.blk
[0].bp
);
1627 state
->path
.blk
[0].bp
= NULL
;
1629 error
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
1633 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t
*)
1634 bp
->data
)->hdr
.info
.magic
)
1635 == XFS_ATTR_LEAF_MAGIC
);
1637 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1638 xfs_bmap_init(args
->flist
, args
->firstblock
);
1639 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1640 /* bp is gone due to xfs_da_shrink_inode */
1642 error
= xfs_bmap_finish(&args
->trans
,
1649 xfs_bmap_cancel(args
->flist
);
1654 * bmap_finish() may have committed the last trans
1655 * and started a new one. We need the inode to be
1656 * in all transactions.
1659 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1660 xfs_trans_ihold(args
->trans
, dp
);
1663 xfs_da_brelse(args
->trans
, bp
);
1668 xfs_da_state_free(state
);
1673 * Fill in the disk block numbers in the state structure for the buffers
1674 * that are attached to the state structure.
1675 * This is done so that we can quickly reattach ourselves to those buffers
1676 * after some set of transaction commits have released these buffers.
1679 xfs_attr_fillstate(xfs_da_state_t
*state
)
1681 xfs_da_state_path_t
*path
;
1682 xfs_da_state_blk_t
*blk
;
1686 * Roll down the "path" in the state structure, storing the on-disk
1687 * block number for those buffers in the "path".
1689 path
= &state
->path
;
1690 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1691 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1693 blk
->disk_blkno
= xfs_da_blkno(blk
->bp
);
1694 xfs_da_buf_done(blk
->bp
);
1697 blk
->disk_blkno
= 0;
1702 * Roll down the "altpath" in the state structure, storing the on-disk
1703 * block number for those buffers in the "altpath".
1705 path
= &state
->altpath
;
1706 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1707 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1709 blk
->disk_blkno
= xfs_da_blkno(blk
->bp
);
1710 xfs_da_buf_done(blk
->bp
);
1713 blk
->disk_blkno
= 0;
1721 * Reattach the buffers to the state structure based on the disk block
1722 * numbers stored in the state structure.
1723 * This is done after some set of transaction commits have released those
1724 * buffers from our grip.
1727 xfs_attr_refillstate(xfs_da_state_t
*state
)
1729 xfs_da_state_path_t
*path
;
1730 xfs_da_state_blk_t
*blk
;
1734 * Roll down the "path" in the state structure, storing the on-disk
1735 * block number for those buffers in the "path".
1737 path
= &state
->path
;
1738 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1739 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1740 if (blk
->disk_blkno
) {
1741 error
= xfs_da_read_buf(state
->args
->trans
,
1743 blk
->blkno
, blk
->disk_blkno
,
1744 &blk
->bp
, XFS_ATTR_FORK
);
1753 * Roll down the "altpath" in the state structure, storing the on-disk
1754 * block number for those buffers in the "altpath".
1756 path
= &state
->altpath
;
1757 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1758 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1759 if (blk
->disk_blkno
) {
1760 error
= xfs_da_read_buf(state
->args
->trans
,
1762 blk
->blkno
, blk
->disk_blkno
,
1763 &blk
->bp
, XFS_ATTR_FORK
);
1775 * Look up a filename in a node attribute list.
1777 * This routine gets called for any attribute fork that has more than one
1778 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1779 * "remote" values taking up more blocks.
1782 xfs_attr_node_get(xfs_da_args_t
*args
)
1784 xfs_da_state_t
*state
;
1785 xfs_da_state_blk_t
*blk
;
1789 state
= xfs_da_state_alloc();
1791 state
->mp
= args
->dp
->i_mount
;
1792 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1793 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1796 * Search to see if name exists, and get back a pointer to it.
1798 error
= xfs_da_node_lookup_int(state
, &retval
);
1801 } else if (retval
== EEXIST
) {
1802 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1803 ASSERT(blk
->bp
!= NULL
);
1804 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1807 * Get the value, local or "remote"
1809 retval
= xfs_attr_leaf_getvalue(blk
->bp
, args
);
1810 if (!retval
&& (args
->rmtblkno
> 0)
1811 && !(args
->flags
& ATTR_KERNOVAL
)) {
1812 retval
= xfs_attr_rmtval_get(args
);
1817 * If not in a transaction, we have to release all the buffers.
1819 for (i
= 0; i
< state
->path
.active
; i
++) {
1820 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1821 state
->path
.blk
[i
].bp
= NULL
;
1824 xfs_da_state_free(state
);
1828 STATIC
int /* error */
1829 xfs_attr_node_list(xfs_attr_list_context_t
*context
)
1831 attrlist_cursor_kern_t
*cursor
;
1832 xfs_attr_leafblock_t
*leaf
;
1833 xfs_da_intnode_t
*node
;
1834 xfs_da_node_entry_t
*btree
;
1838 cursor
= context
->cursor
;
1839 cursor
->initted
= 1;
1842 * Do all sorts of validation on the passed-in cursor structure.
1843 * If anything is amiss, ignore the cursor and look up the hashval
1844 * starting from the btree root.
1847 if (cursor
->blkno
> 0) {
1848 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1849 &bp
, XFS_ATTR_FORK
);
1850 if ((error
!= 0) && (error
!= EFSCORRUPTED
))
1854 switch (be16_to_cpu(node
->hdr
.info
.magic
)) {
1855 case XFS_DA_NODE_MAGIC
:
1856 xfs_attr_trace_l_cn("wrong blk", context
, node
);
1857 xfs_da_brelse(NULL
, bp
);
1860 case XFS_ATTR_LEAF_MAGIC
:
1862 if (cursor
->hashval
> be32_to_cpu(leaf
->entries
[
1863 be16_to_cpu(leaf
->hdr
.count
)-1].hashval
)) {
1864 xfs_attr_trace_l_cl("wrong blk",
1866 xfs_da_brelse(NULL
, bp
);
1868 } else if (cursor
->hashval
<=
1869 be32_to_cpu(leaf
->entries
[0].hashval
)) {
1870 xfs_attr_trace_l_cl("maybe wrong blk",
1872 xfs_da_brelse(NULL
, bp
);
1877 xfs_attr_trace_l_c("wrong blk - ??", context
);
1878 xfs_da_brelse(NULL
, bp
);
1885 * We did not find what we expected given the cursor's contents,
1886 * so we start from the top and work down based on the hash value.
1887 * Note that start of node block is same as start of leaf block.
1892 error
= xfs_da_read_buf(NULL
, context
->dp
,
1893 cursor
->blkno
, -1, &bp
,
1897 if (unlikely(bp
== NULL
)) {
1898 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1900 context
->dp
->i_mount
);
1901 return(XFS_ERROR(EFSCORRUPTED
));
1904 if (be16_to_cpu(node
->hdr
.info
.magic
)
1905 == XFS_ATTR_LEAF_MAGIC
)
1907 if (unlikely(be16_to_cpu(node
->hdr
.info
.magic
)
1908 != XFS_DA_NODE_MAGIC
)) {
1909 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1911 context
->dp
->i_mount
,
1913 xfs_da_brelse(NULL
, bp
);
1914 return(XFS_ERROR(EFSCORRUPTED
));
1916 btree
= node
->btree
;
1917 for (i
= 0; i
< be16_to_cpu(node
->hdr
.count
);
1920 <= be32_to_cpu(btree
->hashval
)) {
1921 cursor
->blkno
= be32_to_cpu(btree
->before
);
1922 xfs_attr_trace_l_cb("descending",
1927 if (i
== be16_to_cpu(node
->hdr
.count
)) {
1928 xfs_da_brelse(NULL
, bp
);
1931 xfs_da_brelse(NULL
, bp
);
1937 * Roll upward through the blocks, processing each leaf block in
1938 * order. As long as there is space in the result buffer, keep
1939 * adding the information.
1943 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
)
1944 != XFS_ATTR_LEAF_MAGIC
)) {
1945 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1947 context
->dp
->i_mount
, leaf
);
1948 xfs_da_brelse(NULL
, bp
);
1949 return(XFS_ERROR(EFSCORRUPTED
));
1951 error
= xfs_attr_leaf_list_int(bp
, context
);
1953 xfs_da_brelse(NULL
, bp
);
1956 if (context
->seen_enough
|| leaf
->hdr
.info
.forw
== 0)
1958 cursor
->blkno
= be32_to_cpu(leaf
->hdr
.info
.forw
);
1959 xfs_da_brelse(NULL
, bp
);
1960 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1961 &bp
, XFS_ATTR_FORK
);
1964 if (unlikely((bp
== NULL
))) {
1965 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1967 context
->dp
->i_mount
);
1968 return(XFS_ERROR(EFSCORRUPTED
));
1971 xfs_da_brelse(NULL
, bp
);
1976 /*========================================================================
1977 * External routines for manipulating out-of-line attribute values.
1978 *========================================================================*/
1981 * Read the value associated with an attribute from the out-of-line buffer
1982 * that we stored it in.
1985 xfs_attr_rmtval_get(xfs_da_args_t
*args
)
1987 xfs_bmbt_irec_t map
[ATTR_RMTVALUE_MAPSIZE
];
1992 int nmap
, error
, tmp
, valuelen
, blkcnt
, i
;
1995 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
1997 mp
= args
->dp
->i_mount
;
1999 valuelen
= args
->valuelen
;
2000 lblkno
= args
->rmtblkno
;
2001 while (valuelen
> 0) {
2002 nmap
= ATTR_RMTVALUE_MAPSIZE
;
2003 error
= xfs_bmapi(args
->trans
, args
->dp
, (xfs_fileoff_t
)lblkno
,
2005 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2006 NULL
, 0, map
, &nmap
, NULL
, NULL
);
2011 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
2012 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
2013 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
2014 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
2015 blkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
2016 error
= xfs_read_buf(mp
, mp
->m_ddev_targp
, dblkno
,
2018 XFS_BUF_LOCK
| XBF_DONT_BLOCK
,
2023 tmp
= (valuelen
< XFS_BUF_SIZE(bp
))
2024 ? valuelen
: XFS_BUF_SIZE(bp
);
2025 xfs_biomove(bp
, 0, tmp
, dst
, XFS_B_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 XFS_BUF_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
, XFS_B_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
,
2219 XFS_INCORE_TRYLOCK
);
2222 XFS_BUF_UNDELAYWRITE(bp
);
2227 valuelen
-= map
.br_blockcount
;
2229 lblkno
+= map
.br_blockcount
;
2233 * Keep de-allocating extents until the remote-value region is gone.
2235 lblkno
= args
->rmtblkno
;
2236 blkcnt
= args
->rmtblkcnt
;
2239 xfs_bmap_init(args
->flist
, args
->firstblock
);
2240 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
2241 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2242 1, args
->firstblock
, args
->flist
,
2245 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
2251 xfs_bmap_cancel(args
->flist
);
2256 * bmap_finish() may have committed the last trans and started
2257 * a new one. We need the inode to be in all transactions.
2260 xfs_trans_ijoin(args
->trans
, args
->dp
, XFS_ILOCK_EXCL
);
2261 xfs_trans_ihold(args
->trans
, args
->dp
);
2265 * Close out trans and start the next one in the chain.
2267 error
= xfs_trans_roll(&args
->trans
, args
->dp
);
2274 #if defined(XFS_ATTR_TRACE)
2276 * Add a trace buffer entry for an attr_list context structure.
2279 xfs_attr_trace_l_c(char *where
, struct xfs_attr_list_context
*context
)
2281 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C
, where
, context
,
2282 (__psunsigned_t
)NULL
,
2283 (__psunsigned_t
)NULL
,
2284 (__psunsigned_t
)NULL
);
2288 * Add a trace buffer entry for a context structure and a Btree node.
2291 xfs_attr_trace_l_cn(char *where
, struct xfs_attr_list_context
*context
,
2292 struct xfs_da_intnode
*node
)
2294 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN
, where
, context
,
2295 (__psunsigned_t
)be16_to_cpu(node
->hdr
.count
),
2296 (__psunsigned_t
)be32_to_cpu(node
->btree
[0].hashval
),
2297 (__psunsigned_t
)be32_to_cpu(node
->btree
[
2298 be16_to_cpu(node
->hdr
.count
)-1].hashval
));
2302 * Add a trace buffer entry for a context structure and a Btree element.
2305 xfs_attr_trace_l_cb(char *where
, struct xfs_attr_list_context
*context
,
2306 struct xfs_da_node_entry
*btree
)
2308 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB
, where
, context
,
2309 (__psunsigned_t
)be32_to_cpu(btree
->hashval
),
2310 (__psunsigned_t
)be32_to_cpu(btree
->before
),
2311 (__psunsigned_t
)NULL
);
2315 * Add a trace buffer entry for a context structure and a leaf block.
2318 xfs_attr_trace_l_cl(char *where
, struct xfs_attr_list_context
*context
,
2319 struct xfs_attr_leafblock
*leaf
)
2321 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL
, where
, context
,
2322 (__psunsigned_t
)be16_to_cpu(leaf
->hdr
.count
),
2323 (__psunsigned_t
)be32_to_cpu(leaf
->entries
[0].hashval
),
2324 (__psunsigned_t
)be32_to_cpu(leaf
->entries
[
2325 be16_to_cpu(leaf
->hdr
.count
)-1].hashval
));
2329 * Add a trace buffer entry for the arguments given to the routine,
2333 xfs_attr_trace_enter(int type
, char *where
,
2334 struct xfs_attr_list_context
*context
,
2335 __psunsigned_t a13
, __psunsigned_t a14
,
2338 ASSERT(xfs_attr_trace_buf
);
2339 ktrace_enter(xfs_attr_trace_buf
, (void *)((__psunsigned_t
)type
),
2340 (void *)((__psunsigned_t
)where
),
2341 (void *)((__psunsigned_t
)context
->dp
),
2342 (void *)((__psunsigned_t
)context
->cursor
->hashval
),
2343 (void *)((__psunsigned_t
)context
->cursor
->blkno
),
2344 (void *)((__psunsigned_t
)context
->cursor
->offset
),
2345 (void *)((__psunsigned_t
)context
->alist
),
2346 (void *)((__psunsigned_t
)context
->bufsize
),
2347 (void *)((__psunsigned_t
)context
->count
),
2348 (void *)((__psunsigned_t
)context
->firstu
),
2350 (void *)((__psunsigned_t
)context
->dupcnt
),
2351 (void *)((__psunsigned_t
)context
->flags
),
2352 (void *)a13
, (void *)a14
, (void *)a15
);
2354 #endif /* XFS_ATTR_TRACE */