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 *========================================================================*/
127 xfs_attr_fetch(xfs_inode_t
*ip
, struct xfs_name
*name
,
128 char *value
, int *valuelenp
, int flags
)
133 if (!xfs_inode_hasattr(ip
))
137 * Fill in the arg structure for this request.
139 memset((char *)&args
, 0, sizeof(args
));
140 args
.name
= name
->name
;
141 args
.namelen
= name
->len
;
143 args
.valuelen
= *valuelenp
;
145 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
147 args
.whichfork
= XFS_ATTR_FORK
;
150 * Decide on what work routines to call based on the inode size.
152 if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
153 error
= xfs_attr_shortform_getvalue(&args
);
154 } else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
)) {
155 error
= xfs_attr_leaf_get(&args
);
157 error
= xfs_attr_node_get(&args
);
161 * Return the number of bytes in the value to the caller.
163 *valuelenp
= args
.valuelen
;
179 struct xfs_name xname
;
181 XFS_STATS_INC(xs_attr_get
);
183 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
186 error
= xfs_attr_name_to_xname(&xname
, name
);
190 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
191 error
= xfs_attr_fetch(ip
, &xname
, value
, valuelenp
, flags
);
192 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
197 * Calculate how many blocks we need for the new attribute,
201 struct xfs_inode
*ip
,
206 struct xfs_mount
*mp
= ip
->i_mount
;
211 * Determine space new attribute will use, and if it would be
212 * "local" or "remote" (note: local != inline).
214 size
= xfs_attr_leaf_newentsize(namelen
, valuelen
,
215 mp
->m_sb
.sb_blocksize
, local
);
217 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
219 if (size
> (mp
->m_sb
.sb_blocksize
>> 1)) {
220 /* Double split possible */
225 * Out of line attribute, cannot double split, but
226 * make room for the attribute value itself.
228 uint dblocks
= XFS_B_TO_FSB(mp
, valuelen
);
230 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
237 xfs_attr_set_int(xfs_inode_t
*dp
, struct xfs_name
*name
,
238 char *value
, int valuelen
, int flags
)
241 xfs_fsblock_t firstblock
;
242 xfs_bmap_free_t flist
;
243 int error
, err2
, committed
;
244 xfs_mount_t
*mp
= dp
->i_mount
;
245 int rsvd
= (flags
& ATTR_ROOT
) != 0;
249 * Attach the dquots to the inode.
251 error
= xfs_qm_dqattach(dp
, 0);
256 * If the inode doesn't have an attribute fork, add one.
257 * (inode must not be locked when we call this routine)
259 if (XFS_IFORK_Q(dp
) == 0) {
260 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
261 XFS_ATTR_SF_ENTSIZE_BYNAME(name
->len
, valuelen
);
263 if ((error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
)))
268 * Fill in the arg structure for this request.
270 memset((char *)&args
, 0, sizeof(args
));
271 args
.name
= name
->name
;
272 args
.namelen
= name
->len
;
274 args
.valuelen
= valuelen
;
276 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
278 args
.firstblock
= &firstblock
;
280 args
.whichfork
= XFS_ATTR_FORK
;
281 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
283 /* Size is now blocks for attribute data */
284 args
.total
= xfs_attr_calc_size(dp
, name
->len
, valuelen
, &local
);
287 * Start our first transaction of the day.
289 * All future transactions during this code must be "chained" off
290 * this one via the trans_dup() call. All transactions will contain
291 * the inode, and the inode will always be marked with trans_ihold().
292 * Since the inode will be locked in all transactions, we must log
293 * the inode in every transaction to let it float upward through
296 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
299 * Root fork attributes can use reserved data blocks for this
300 * operation if necessary
304 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
306 if ((error
= xfs_trans_reserve(args
.trans
, args
.total
,
307 XFS_ATTRSET_LOG_RES(mp
, args
.total
), 0,
308 XFS_TRANS_PERM_LOG_RES
, XFS_ATTRSET_LOG_COUNT
))) {
309 xfs_trans_cancel(args
.trans
, 0);
312 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
314 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
315 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
316 XFS_QMOPT_RES_REGBLKS
);
318 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
319 xfs_trans_cancel(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
323 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
324 xfs_trans_ihold(args
.trans
, dp
);
327 * If the attribute list is non-existent or a shortform list,
328 * upgrade it to a single-leaf-block attribute list.
330 if ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) ||
331 ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) &&
332 (dp
->i_d
.di_anextents
== 0))) {
335 * Build initial attribute list (if required).
337 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
338 xfs_attr_shortform_create(&args
);
341 * Try to add the attr to the attribute list in
344 error
= xfs_attr_shortform_addname(&args
);
345 if (error
!= ENOSPC
) {
347 * Commit the shortform mods, and we're done.
348 * NOTE: this is also the error path (EEXIST, etc).
350 ASSERT(args
.trans
!= NULL
);
353 * If this is a synchronous mount, make sure that
354 * the transaction goes to disk before returning
357 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
358 xfs_trans_set_sync(args
.trans
);
360 err2
= xfs_trans_commit(args
.trans
,
361 XFS_TRANS_RELEASE_LOG_RES
);
362 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
365 * Hit the inode change time.
367 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
368 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
370 return(error
== 0 ? err2
: error
);
374 * It won't fit in the shortform, transform to a leaf block.
375 * GROT: another possible req'mt for a double-split btree op.
377 xfs_bmap_init(args
.flist
, args
.firstblock
);
378 error
= xfs_attr_shortform_to_leaf(&args
);
380 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
386 xfs_bmap_cancel(&flist
);
391 * bmap_finish() may have committed the last trans and started
392 * a new one. We need the inode to be in all transactions.
395 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
396 xfs_trans_ihold(args
.trans
, dp
);
400 * Commit the leaf transformation. We'll need another (linked)
401 * transaction to add the new attribute to the leaf.
404 error
= xfs_trans_roll(&args
.trans
, dp
);
410 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
411 error
= xfs_attr_leaf_addname(&args
);
413 error
= xfs_attr_node_addname(&args
);
420 * If this is a synchronous mount, make sure that the
421 * transaction goes to disk before returning to the user.
423 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
424 xfs_trans_set_sync(args
.trans
);
428 * Commit the last in the sequence of transactions.
430 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
431 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
432 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
435 * Hit the inode change time.
437 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
438 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
445 xfs_trans_cancel(args
.trans
,
446 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
447 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
460 struct xfs_name xname
;
462 XFS_STATS_INC(xs_attr_set
);
464 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
467 error
= xfs_attr_name_to_xname(&xname
, name
);
471 return xfs_attr_set_int(dp
, &xname
, value
, valuelen
, flags
);
475 * Generic handler routine to remove a name from an attribute list.
476 * Transitions attribute list from Btree to shortform as necessary.
479 xfs_attr_remove_int(xfs_inode_t
*dp
, struct xfs_name
*name
, int flags
)
482 xfs_fsblock_t firstblock
;
483 xfs_bmap_free_t flist
;
485 xfs_mount_t
*mp
= dp
->i_mount
;
488 * Fill in the arg structure for this request.
490 memset((char *)&args
, 0, sizeof(args
));
491 args
.name
= name
->name
;
492 args
.namelen
= name
->len
;
494 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
496 args
.firstblock
= &firstblock
;
499 args
.whichfork
= XFS_ATTR_FORK
;
502 * Attach the dquots to the inode.
504 error
= xfs_qm_dqattach(dp
, 0);
509 * Start our first transaction of the day.
511 * All future transactions during this code must be "chained" off
512 * this one via the trans_dup() call. All transactions will contain
513 * the inode, and the inode will always be marked with trans_ihold().
514 * Since the inode will be locked in all transactions, we must log
515 * the inode in every transaction to let it float upward through
518 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
521 * Root fork attributes can use reserved data blocks for this
522 * operation if necessary
525 if (flags
& ATTR_ROOT
)
526 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
528 if ((error
= xfs_trans_reserve(args
.trans
,
529 XFS_ATTRRM_SPACE_RES(mp
),
530 XFS_ATTRRM_LOG_RES(mp
),
531 0, XFS_TRANS_PERM_LOG_RES
,
532 XFS_ATTRRM_LOG_COUNT
))) {
533 xfs_trans_cancel(args
.trans
, 0);
537 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
539 * No need to make quota reservations here. We expect to release some
540 * blocks not allocate in the common case.
542 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
543 xfs_trans_ihold(args
.trans
, dp
);
546 * Decide on what work routines to call based on the inode size.
548 if (!xfs_inode_hasattr(dp
)) {
549 error
= XFS_ERROR(ENOATTR
);
552 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
553 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
554 error
= xfs_attr_shortform_remove(&args
);
558 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
559 error
= xfs_attr_leaf_removename(&args
);
561 error
= xfs_attr_node_removename(&args
);
568 * If this is a synchronous mount, make sure that the
569 * transaction goes to disk before returning to the user.
571 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
572 xfs_trans_set_sync(args
.trans
);
576 * Commit the last in the sequence of transactions.
578 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
579 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
580 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
583 * Hit the inode change time.
585 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
586 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
593 xfs_trans_cancel(args
.trans
,
594 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
595 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
606 struct xfs_name xname
;
608 XFS_STATS_INC(xs_attr_remove
);
610 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
613 error
= xfs_attr_name_to_xname(&xname
, name
);
617 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
618 if (!xfs_inode_hasattr(dp
)) {
619 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
620 return XFS_ERROR(ENOATTR
);
622 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
624 return xfs_attr_remove_int(dp
, &xname
, flags
);
628 xfs_attr_list_int(xfs_attr_list_context_t
*context
)
631 xfs_inode_t
*dp
= context
->dp
;
633 XFS_STATS_INC(xs_attr_list
);
635 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
638 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
639 xfs_attr_trace_l_c("syscall start", context
);
642 * Decide on what work routines to call based on the inode size.
644 if (!xfs_inode_hasattr(dp
)) {
646 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
647 error
= xfs_attr_shortform_list(context
);
648 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
649 error
= xfs_attr_leaf_list(context
);
651 error
= xfs_attr_node_list(context
);
654 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
655 xfs_attr_trace_l_c("syscall end", context
);
660 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
661 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
662 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
663 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
664 & ~(sizeof(u_int32_t)-1))
667 * Format an attribute and copy it out to the user's buffer.
668 * Take care to check values and protect against them changing later,
669 * we may be reading them directly out of a user buffer.
673 xfs_attr_put_listent(xfs_attr_list_context_t
*context
, int flags
,
674 char *name
, int namelen
,
675 int valuelen
, char *value
)
677 struct attrlist
*alist
= (struct attrlist
*)context
->alist
;
681 ASSERT(!(context
->flags
& ATTR_KERNOVAL
));
682 ASSERT(context
->count
>= 0);
683 ASSERT(context
->count
< (ATTR_MAX_VALUELEN
/8));
684 ASSERT(context
->firstu
>= sizeof(*alist
));
685 ASSERT(context
->firstu
<= context
->bufsize
);
688 * Only list entries in the right namespace.
690 if (((context
->flags
& ATTR_SECURE
) == 0) !=
691 ((flags
& XFS_ATTR_SECURE
) == 0))
693 if (((context
->flags
& ATTR_ROOT
) == 0) !=
694 ((flags
& XFS_ATTR_ROOT
) == 0))
697 arraytop
= sizeof(*alist
) +
698 context
->count
* sizeof(alist
->al_offset
[0]);
699 context
->firstu
-= ATTR_ENTSIZE(namelen
);
700 if (context
->firstu
< arraytop
) {
701 xfs_attr_trace_l_c("buffer full", context
);
703 context
->seen_enough
= 1;
707 aep
= (attrlist_ent_t
*)&context
->alist
[context
->firstu
];
708 aep
->a_valuelen
= valuelen
;
709 memcpy(aep
->a_name
, name
, namelen
);
710 aep
->a_name
[namelen
] = 0;
711 alist
->al_offset
[context
->count
++] = context
->firstu
;
712 alist
->al_count
= context
->count
;
713 xfs_attr_trace_l_c("add", context
);
718 * Generate a list of extended attribute names and optionally
719 * also value lengths. Positive return value follows the XFS
720 * convention of being an error, zero or negative return code
721 * is the length of the buffer returned (negated), indicating
730 attrlist_cursor_kern_t
*cursor
)
732 xfs_attr_list_context_t context
;
733 struct attrlist
*alist
;
737 * Validate the cursor.
739 if (cursor
->pad1
|| cursor
->pad2
)
740 return(XFS_ERROR(EINVAL
));
741 if ((cursor
->initted
== 0) &&
742 (cursor
->hashval
|| cursor
->blkno
|| cursor
->offset
))
743 return XFS_ERROR(EINVAL
);
746 * Check for a properly aligned buffer.
748 if (((long)buffer
) & (sizeof(int)-1))
749 return XFS_ERROR(EFAULT
);
750 if (flags
& ATTR_KERNOVAL
)
754 * Initialize the output buffer.
756 memset(&context
, 0, sizeof(context
));
758 context
.cursor
= cursor
;
760 context
.flags
= flags
;
761 context
.alist
= buffer
;
762 context
.bufsize
= (bufsize
& ~(sizeof(int)-1)); /* align */
763 context
.firstu
= context
.bufsize
;
764 context
.put_listent
= xfs_attr_put_listent
;
766 alist
= (struct attrlist
*)context
.alist
;
769 alist
->al_offset
[0] = context
.bufsize
;
771 error
= xfs_attr_list_int(&context
);
777 xfs_attr_inactive(xfs_inode_t
*dp
)
784 ASSERT(! XFS_NOT_DQATTACHED(mp
, dp
));
786 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
787 if (!xfs_inode_hasattr(dp
) ||
788 dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
789 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
792 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
795 * Start our first transaction of the day.
797 * All future transactions during this code must be "chained" off
798 * this one via the trans_dup() call. All transactions will contain
799 * the inode, and the inode will always be marked with trans_ihold().
800 * Since the inode will be locked in all transactions, we must log
801 * the inode in every transaction to let it float upward through
804 trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTRINVAL
);
805 if ((error
= xfs_trans_reserve(trans
, 0, XFS_ATTRINVAL_LOG_RES(mp
), 0,
806 XFS_TRANS_PERM_LOG_RES
,
807 XFS_ATTRINVAL_LOG_COUNT
))) {
808 xfs_trans_cancel(trans
, 0);
811 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
814 * No need to make quota reservations here. We expect to release some
815 * blocks, not allocate, in the common case.
817 xfs_trans_ijoin(trans
, dp
, XFS_ILOCK_EXCL
);
818 xfs_trans_ihold(trans
, dp
);
821 * Decide on what work routines to call based on the inode size.
823 if (!xfs_inode_hasattr(dp
) ||
824 dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
828 error
= xfs_attr_root_inactive(&trans
, dp
);
832 * signal synchronous inactive transactions unless this
833 * is a synchronous mount filesystem in which case we
834 * know that we're here because we've been called out of
835 * xfs_inactive which means that the last reference is gone
836 * and the unlink transaction has already hit the disk so
837 * async inactive transactions are safe.
839 if ((error
= xfs_itruncate_finish(&trans
, dp
, 0LL, XFS_ATTR_FORK
,
840 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
)
845 * Commit the last in the sequence of transactions.
847 xfs_trans_log_inode(trans
, dp
, XFS_ILOG_CORE
);
848 error
= xfs_trans_commit(trans
, XFS_TRANS_RELEASE_LOG_RES
);
849 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
854 xfs_trans_cancel(trans
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
855 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
861 /*========================================================================
862 * External routines when attribute list is inside the inode
863 *========================================================================*/
866 * Add a name to the shortform attribute list structure
867 * This is the external routine.
870 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
872 int newsize
, forkoff
, retval
;
874 retval
= xfs_attr_shortform_lookup(args
);
875 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
877 } else if (retval
== EEXIST
) {
878 if (args
->flags
& ATTR_CREATE
)
880 retval
= xfs_attr_shortform_remove(args
);
884 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
885 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
886 return(XFS_ERROR(ENOSPC
));
888 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
889 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
891 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
893 return(XFS_ERROR(ENOSPC
));
895 xfs_attr_shortform_add(args
, forkoff
);
900 /*========================================================================
901 * External routines when attribute list is one block
902 *========================================================================*/
905 * Add a name to the leaf attribute list structure
907 * This leaf block cannot have a "remote" value, we only call this routine
908 * if bmap_one_block() says there is only one block (ie: no remote blks).
911 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
915 int retval
, error
, committed
, forkoff
;
918 * Read the (only) block in the attribute list in.
922 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
929 * Look up the given attribute in the leaf block. Figure out if
930 * the given flags produce an error or call for an atomic rename.
932 retval
= xfs_attr_leaf_lookup_int(bp
, args
);
933 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
934 xfs_da_brelse(args
->trans
, bp
);
936 } else if (retval
== EEXIST
) {
937 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
938 xfs_da_brelse(args
->trans
, bp
);
941 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
942 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
943 args
->index2
= args
->index
;
944 args
->rmtblkno2
= args
->rmtblkno
;
945 args
->rmtblkcnt2
= args
->rmtblkcnt
;
949 * Add the attribute to the leaf block, transitioning to a Btree
952 retval
= xfs_attr_leaf_add(bp
, args
);
954 if (retval
== ENOSPC
) {
956 * Promote the attribute list to the Btree format, then
957 * Commit that transaction so that the node_addname() call
958 * can manage its own transactions.
960 xfs_bmap_init(args
->flist
, args
->firstblock
);
961 error
= xfs_attr_leaf_to_node(args
);
963 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
969 xfs_bmap_cancel(args
->flist
);
974 * bmap_finish() may have committed the last trans and started
975 * a new one. We need the inode to be in all transactions.
978 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
979 xfs_trans_ihold(args
->trans
, dp
);
983 * Commit the current trans (including the inode) and start
986 error
= xfs_trans_roll(&args
->trans
, dp
);
991 * Fob the whole rest of the problem off on the Btree code.
993 error
= xfs_attr_node_addname(args
);
998 * Commit the transaction that added the attr name so that
999 * later routines can manage their own transactions.
1001 error
= xfs_trans_roll(&args
->trans
, dp
);
1006 * If there was an out-of-line value, allocate the blocks we
1007 * identified for its storage and copy the value. This is done
1008 * after we create the attribute so that we don't overflow the
1009 * maximum size of a transaction and/or hit a deadlock.
1011 if (args
->rmtblkno
> 0) {
1012 error
= xfs_attr_rmtval_set(args
);
1018 * If this is an atomic rename operation, we must "flip" the
1019 * incomplete flags on the "new" and "old" attribute/value pairs
1020 * so that one disappears and one appears atomically. Then we
1021 * must remove the "old" attribute/value pair.
1023 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1025 * In a separate transaction, set the incomplete flag on the
1026 * "old" attr and clear the incomplete flag on the "new" attr.
1028 error
= xfs_attr_leaf_flipflags(args
);
1033 * Dismantle the "old" attribute/value pair by removing
1034 * a "remote" value (if it exists).
1036 args
->index
= args
->index2
;
1037 args
->blkno
= args
->blkno2
;
1038 args
->rmtblkno
= args
->rmtblkno2
;
1039 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1040 if (args
->rmtblkno
) {
1041 error
= xfs_attr_rmtval_remove(args
);
1047 * Read in the block containing the "old" attr, then
1048 * remove the "old" attr from that block (neat, huh!)
1050 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1,
1051 &bp
, XFS_ATTR_FORK
);
1055 (void)xfs_attr_leaf_remove(bp
, args
);
1058 * If the result is small enough, shrink it all into the inode.
1060 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1061 xfs_bmap_init(args
->flist
, args
->firstblock
);
1062 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1063 /* bp is gone due to xfs_da_shrink_inode */
1065 error
= xfs_bmap_finish(&args
->trans
,
1072 xfs_bmap_cancel(args
->flist
);
1077 * bmap_finish() may have committed the last trans
1078 * and started a new one. We need the inode to be
1079 * in all transactions.
1082 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1083 xfs_trans_ihold(args
->trans
, dp
);
1086 xfs_da_buf_done(bp
);
1089 * Commit the remove and start the next trans in series.
1091 error
= xfs_trans_roll(&args
->trans
, dp
);
1093 } else if (args
->rmtblkno
> 0) {
1095 * Added a "remote" value, just clear the incomplete flag.
1097 error
= xfs_attr_leaf_clearflag(args
);
1103 * Remove a name from the leaf attribute list structure
1105 * This leaf block cannot have a "remote" value, we only call this routine
1106 * if bmap_one_block() says there is only one block (ie: no remote blks).
1109 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
1113 int error
, committed
, forkoff
;
1116 * Remove the attribute.
1120 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
1127 error
= xfs_attr_leaf_lookup_int(bp
, args
);
1128 if (error
== ENOATTR
) {
1129 xfs_da_brelse(args
->trans
, bp
);
1133 (void)xfs_attr_leaf_remove(bp
, args
);
1136 * If the result is small enough, shrink it all into the inode.
1138 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1139 xfs_bmap_init(args
->flist
, args
->firstblock
);
1140 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1141 /* bp is gone due to xfs_da_shrink_inode */
1143 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1149 xfs_bmap_cancel(args
->flist
);
1154 * bmap_finish() may have committed the last trans and started
1155 * a new one. We need the inode to be in all transactions.
1158 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1159 xfs_trans_ihold(args
->trans
, dp
);
1162 xfs_da_buf_done(bp
);
1167 * Look up a name in a leaf attribute list structure.
1169 * This leaf block cannot have a "remote" value, we only call this routine
1170 * if bmap_one_block() says there is only one block (ie: no remote blks).
1173 xfs_attr_leaf_get(xfs_da_args_t
*args
)
1179 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
1185 error
= xfs_attr_leaf_lookup_int(bp
, args
);
1186 if (error
!= EEXIST
) {
1187 xfs_da_brelse(args
->trans
, bp
);
1190 error
= xfs_attr_leaf_getvalue(bp
, args
);
1191 xfs_da_brelse(args
->trans
, bp
);
1192 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
1193 error
= xfs_attr_rmtval_get(args
);
1199 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1202 xfs_attr_leaf_list(xfs_attr_list_context_t
*context
)
1204 xfs_attr_leafblock_t
*leaf
;
1208 context
->cursor
->blkno
= 0;
1209 error
= xfs_da_read_buf(NULL
, context
->dp
, 0, -1, &bp
, XFS_ATTR_FORK
);
1211 return XFS_ERROR(error
);
1214 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
) != XFS_ATTR_LEAF_MAGIC
)) {
1215 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW
,
1216 context
->dp
->i_mount
, leaf
);
1217 xfs_da_brelse(NULL
, bp
);
1218 return XFS_ERROR(EFSCORRUPTED
);
1221 error
= xfs_attr_leaf_list_int(bp
, context
);
1222 xfs_da_brelse(NULL
, bp
);
1223 return XFS_ERROR(error
);
1227 /*========================================================================
1228 * External routines when attribute list size > XFS_LBSIZE(mp).
1229 *========================================================================*/
1232 * Add a name to a Btree-format attribute list.
1234 * This will involve walking down the Btree, and may involve splitting
1235 * leaf nodes and even splitting intermediate nodes up to and including
1236 * the root node (a special case of an intermediate node).
1238 * "Remote" attribute values confuse the issue and atomic rename operations
1239 * add a whole extra layer of confusion on top of that.
1242 xfs_attr_node_addname(xfs_da_args_t
*args
)
1244 xfs_da_state_t
*state
;
1245 xfs_da_state_blk_t
*blk
;
1248 int committed
, retval
, error
;
1251 * Fill in bucket of arguments/results/context to carry around.
1256 state
= xfs_da_state_alloc();
1259 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1260 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1263 * Search to see if name already exists, and get back a pointer
1264 * to where it should go.
1266 error
= xfs_da_node_lookup_int(state
, &retval
);
1269 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1270 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1271 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
1273 } else if (retval
== EEXIST
) {
1274 if (args
->flags
& ATTR_CREATE
)
1276 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
1277 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
1278 args
->index2
= args
->index
;
1279 args
->rmtblkno2
= args
->rmtblkno
;
1280 args
->rmtblkcnt2
= args
->rmtblkcnt
;
1282 args
->rmtblkcnt
= 0;
1285 retval
= xfs_attr_leaf_add(blk
->bp
, state
->args
);
1286 if (retval
== ENOSPC
) {
1287 if (state
->path
.active
== 1) {
1289 * Its really a single leaf node, but it had
1290 * out-of-line values so it looked like it *might*
1291 * have been a b-tree.
1293 xfs_da_state_free(state
);
1294 xfs_bmap_init(args
->flist
, args
->firstblock
);
1295 error
= xfs_attr_leaf_to_node(args
);
1297 error
= xfs_bmap_finish(&args
->trans
,
1304 xfs_bmap_cancel(args
->flist
);
1309 * bmap_finish() may have committed the last trans
1310 * and started a new one. We need the inode to be
1311 * in all transactions.
1314 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1315 xfs_trans_ihold(args
->trans
, dp
);
1319 * Commit the node conversion and start the next
1320 * trans in the chain.
1322 error
= xfs_trans_roll(&args
->trans
, dp
);
1330 * Split as many Btree elements as required.
1331 * This code tracks the new and old attr's location
1332 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1333 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1335 xfs_bmap_init(args
->flist
, args
->firstblock
);
1336 error
= xfs_da_split(state
);
1338 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1344 xfs_bmap_cancel(args
->flist
);
1349 * bmap_finish() may have committed the last trans and started
1350 * a new one. We need the inode to be in all transactions.
1353 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1354 xfs_trans_ihold(args
->trans
, dp
);
1358 * Addition succeeded, update Btree hashvals.
1360 xfs_da_fixhashpath(state
, &state
->path
);
1364 * Kill the state structure, we're done with it and need to
1365 * allow the buffers to come back later.
1367 xfs_da_state_free(state
);
1371 * Commit the leaf addition or btree split and start the next
1372 * trans in the chain.
1374 error
= xfs_trans_roll(&args
->trans
, dp
);
1379 * If there was an out-of-line value, allocate the blocks we
1380 * identified for its storage and copy the value. This is done
1381 * after we create the attribute so that we don't overflow the
1382 * maximum size of a transaction and/or hit a deadlock.
1384 if (args
->rmtblkno
> 0) {
1385 error
= xfs_attr_rmtval_set(args
);
1391 * If this is an atomic rename operation, we must "flip" the
1392 * incomplete flags on the "new" and "old" attribute/value pairs
1393 * so that one disappears and one appears atomically. Then we
1394 * must remove the "old" attribute/value pair.
1396 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1398 * In a separate transaction, set the incomplete flag on the
1399 * "old" attr and clear the incomplete flag on the "new" attr.
1401 error
= xfs_attr_leaf_flipflags(args
);
1406 * Dismantle the "old" attribute/value pair by removing
1407 * a "remote" value (if it exists).
1409 args
->index
= args
->index2
;
1410 args
->blkno
= args
->blkno2
;
1411 args
->rmtblkno
= args
->rmtblkno2
;
1412 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1413 if (args
->rmtblkno
) {
1414 error
= xfs_attr_rmtval_remove(args
);
1420 * Re-find the "old" attribute entry after any split ops.
1421 * The INCOMPLETE flag means that we will find the "old"
1422 * attr, not the "new" one.
1424 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1425 state
= xfs_da_state_alloc();
1428 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1429 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1431 error
= xfs_da_node_lookup_int(state
, &retval
);
1436 * Remove the name and update the hashvals in the tree.
1438 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1439 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1440 error
= xfs_attr_leaf_remove(blk
->bp
, args
);
1441 xfs_da_fixhashpath(state
, &state
->path
);
1444 * Check to see if the tree needs to be collapsed.
1446 if (retval
&& (state
->path
.active
> 1)) {
1447 xfs_bmap_init(args
->flist
, args
->firstblock
);
1448 error
= xfs_da_join(state
);
1450 error
= xfs_bmap_finish(&args
->trans
,
1457 xfs_bmap_cancel(args
->flist
);
1462 * bmap_finish() may have committed the last trans
1463 * and started a new one. We need the inode to be
1464 * in all transactions.
1467 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1468 xfs_trans_ihold(args
->trans
, dp
);
1473 * Commit and start the next trans in the chain.
1475 error
= xfs_trans_roll(&args
->trans
, dp
);
1479 } else if (args
->rmtblkno
> 0) {
1481 * Added a "remote" value, just clear the incomplete flag.
1483 error
= xfs_attr_leaf_clearflag(args
);
1491 xfs_da_state_free(state
);
1498 * Remove a name from a B-tree attribute list.
1500 * This will involve walking down the Btree, and may involve joining
1501 * leaf nodes and even joining intermediate nodes up to and including
1502 * the root node (a special case of an intermediate node).
1505 xfs_attr_node_removename(xfs_da_args_t
*args
)
1507 xfs_da_state_t
*state
;
1508 xfs_da_state_blk_t
*blk
;
1511 int retval
, error
, committed
, forkoff
;
1514 * Tie a string around our finger to remind us where we are.
1517 state
= xfs_da_state_alloc();
1519 state
->mp
= dp
->i_mount
;
1520 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1521 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1524 * Search to see if name exists, and get back a pointer to it.
1526 error
= xfs_da_node_lookup_int(state
, &retval
);
1527 if (error
|| (retval
!= EEXIST
)) {
1534 * If there is an out-of-line value, de-allocate the blocks.
1535 * This is done before we remove the attribute so that we don't
1536 * overflow the maximum size of a transaction and/or hit a deadlock.
1538 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1539 ASSERT(blk
->bp
!= NULL
);
1540 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1541 if (args
->rmtblkno
> 0) {
1543 * Fill in disk block numbers in the state structure
1544 * so that we can get the buffers back after we commit
1545 * several transactions in the following calls.
1547 error
= xfs_attr_fillstate(state
);
1552 * Mark the attribute as INCOMPLETE, then bunmapi() the
1555 error
= xfs_attr_leaf_setflag(args
);
1558 error
= xfs_attr_rmtval_remove(args
);
1563 * Refill the state structure with buffers, the prior calls
1564 * released our buffers.
1566 error
= xfs_attr_refillstate(state
);
1572 * Remove the name and update the hashvals in the tree.
1574 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1575 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1576 retval
= xfs_attr_leaf_remove(blk
->bp
, args
);
1577 xfs_da_fixhashpath(state
, &state
->path
);
1580 * Check to see if the tree needs to be collapsed.
1582 if (retval
&& (state
->path
.active
> 1)) {
1583 xfs_bmap_init(args
->flist
, args
->firstblock
);
1584 error
= xfs_da_join(state
);
1586 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1592 xfs_bmap_cancel(args
->flist
);
1597 * bmap_finish() may have committed the last trans and started
1598 * a new one. We need the inode to be in all transactions.
1601 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1602 xfs_trans_ihold(args
->trans
, dp
);
1606 * Commit the Btree join operation and start a new trans.
1608 error
= xfs_trans_roll(&args
->trans
, dp
);
1614 * If the result is small enough, push it all into the inode.
1616 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1618 * Have to get rid of the copy of this dabuf in the state.
1620 ASSERT(state
->path
.active
== 1);
1621 ASSERT(state
->path
.blk
[0].bp
);
1622 xfs_da_buf_done(state
->path
.blk
[0].bp
);
1623 state
->path
.blk
[0].bp
= NULL
;
1625 error
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
1629 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t
*)
1630 bp
->data
)->hdr
.info
.magic
)
1631 == XFS_ATTR_LEAF_MAGIC
);
1633 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1634 xfs_bmap_init(args
->flist
, args
->firstblock
);
1635 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1636 /* bp is gone due to xfs_da_shrink_inode */
1638 error
= xfs_bmap_finish(&args
->trans
,
1645 xfs_bmap_cancel(args
->flist
);
1650 * bmap_finish() may have committed the last trans
1651 * and started a new one. We need the inode to be
1652 * in all transactions.
1655 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1656 xfs_trans_ihold(args
->trans
, dp
);
1659 xfs_da_brelse(args
->trans
, bp
);
1664 xfs_da_state_free(state
);
1669 * Fill in the disk block numbers in the state structure for the buffers
1670 * that are attached to the state structure.
1671 * This is done so that we can quickly reattach ourselves to those buffers
1672 * after some set of transaction commits have released these buffers.
1675 xfs_attr_fillstate(xfs_da_state_t
*state
)
1677 xfs_da_state_path_t
*path
;
1678 xfs_da_state_blk_t
*blk
;
1682 * Roll down the "path" in the state structure, storing the on-disk
1683 * block number for those buffers in the "path".
1685 path
= &state
->path
;
1686 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1687 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1689 blk
->disk_blkno
= xfs_da_blkno(blk
->bp
);
1690 xfs_da_buf_done(blk
->bp
);
1693 blk
->disk_blkno
= 0;
1698 * Roll down the "altpath" in the state structure, storing the on-disk
1699 * block number for those buffers in the "altpath".
1701 path
= &state
->altpath
;
1702 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1703 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1705 blk
->disk_blkno
= xfs_da_blkno(blk
->bp
);
1706 xfs_da_buf_done(blk
->bp
);
1709 blk
->disk_blkno
= 0;
1717 * Reattach the buffers to the state structure based on the disk block
1718 * numbers stored in the state structure.
1719 * This is done after some set of transaction commits have released those
1720 * buffers from our grip.
1723 xfs_attr_refillstate(xfs_da_state_t
*state
)
1725 xfs_da_state_path_t
*path
;
1726 xfs_da_state_blk_t
*blk
;
1730 * Roll down the "path" in the state structure, storing the on-disk
1731 * block number for those buffers in the "path".
1733 path
= &state
->path
;
1734 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1735 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1736 if (blk
->disk_blkno
) {
1737 error
= xfs_da_read_buf(state
->args
->trans
,
1739 blk
->blkno
, blk
->disk_blkno
,
1740 &blk
->bp
, XFS_ATTR_FORK
);
1749 * Roll down the "altpath" in the state structure, storing the on-disk
1750 * block number for those buffers in the "altpath".
1752 path
= &state
->altpath
;
1753 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1754 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1755 if (blk
->disk_blkno
) {
1756 error
= xfs_da_read_buf(state
->args
->trans
,
1758 blk
->blkno
, blk
->disk_blkno
,
1759 &blk
->bp
, XFS_ATTR_FORK
);
1771 * Look up a filename in a node attribute list.
1773 * This routine gets called for any attribute fork that has more than one
1774 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1775 * "remote" values taking up more blocks.
1778 xfs_attr_node_get(xfs_da_args_t
*args
)
1780 xfs_da_state_t
*state
;
1781 xfs_da_state_blk_t
*blk
;
1785 state
= xfs_da_state_alloc();
1787 state
->mp
= args
->dp
->i_mount
;
1788 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1789 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1792 * Search to see if name exists, and get back a pointer to it.
1794 error
= xfs_da_node_lookup_int(state
, &retval
);
1797 } else if (retval
== EEXIST
) {
1798 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1799 ASSERT(blk
->bp
!= NULL
);
1800 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1803 * Get the value, local or "remote"
1805 retval
= xfs_attr_leaf_getvalue(blk
->bp
, args
);
1806 if (!retval
&& (args
->rmtblkno
> 0)
1807 && !(args
->flags
& ATTR_KERNOVAL
)) {
1808 retval
= xfs_attr_rmtval_get(args
);
1813 * If not in a transaction, we have to release all the buffers.
1815 for (i
= 0; i
< state
->path
.active
; i
++) {
1816 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1817 state
->path
.blk
[i
].bp
= NULL
;
1820 xfs_da_state_free(state
);
1824 STATIC
int /* error */
1825 xfs_attr_node_list(xfs_attr_list_context_t
*context
)
1827 attrlist_cursor_kern_t
*cursor
;
1828 xfs_attr_leafblock_t
*leaf
;
1829 xfs_da_intnode_t
*node
;
1830 xfs_da_node_entry_t
*btree
;
1834 cursor
= context
->cursor
;
1835 cursor
->initted
= 1;
1838 * Do all sorts of validation on the passed-in cursor structure.
1839 * If anything is amiss, ignore the cursor and look up the hashval
1840 * starting from the btree root.
1843 if (cursor
->blkno
> 0) {
1844 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1845 &bp
, XFS_ATTR_FORK
);
1846 if ((error
!= 0) && (error
!= EFSCORRUPTED
))
1850 switch (be16_to_cpu(node
->hdr
.info
.magic
)) {
1851 case XFS_DA_NODE_MAGIC
:
1852 xfs_attr_trace_l_cn("wrong blk", context
, node
);
1853 xfs_da_brelse(NULL
, bp
);
1856 case XFS_ATTR_LEAF_MAGIC
:
1858 if (cursor
->hashval
> be32_to_cpu(leaf
->entries
[
1859 be16_to_cpu(leaf
->hdr
.count
)-1].hashval
)) {
1860 xfs_attr_trace_l_cl("wrong blk",
1862 xfs_da_brelse(NULL
, bp
);
1864 } else if (cursor
->hashval
<=
1865 be32_to_cpu(leaf
->entries
[0].hashval
)) {
1866 xfs_attr_trace_l_cl("maybe wrong blk",
1868 xfs_da_brelse(NULL
, bp
);
1873 xfs_attr_trace_l_c("wrong blk - ??", context
);
1874 xfs_da_brelse(NULL
, bp
);
1881 * We did not find what we expected given the cursor's contents,
1882 * so we start from the top and work down based on the hash value.
1883 * Note that start of node block is same as start of leaf block.
1888 error
= xfs_da_read_buf(NULL
, context
->dp
,
1889 cursor
->blkno
, -1, &bp
,
1893 if (unlikely(bp
== NULL
)) {
1894 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1896 context
->dp
->i_mount
);
1897 return(XFS_ERROR(EFSCORRUPTED
));
1900 if (be16_to_cpu(node
->hdr
.info
.magic
)
1901 == XFS_ATTR_LEAF_MAGIC
)
1903 if (unlikely(be16_to_cpu(node
->hdr
.info
.magic
)
1904 != XFS_DA_NODE_MAGIC
)) {
1905 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1907 context
->dp
->i_mount
,
1909 xfs_da_brelse(NULL
, bp
);
1910 return(XFS_ERROR(EFSCORRUPTED
));
1912 btree
= node
->btree
;
1913 for (i
= 0; i
< be16_to_cpu(node
->hdr
.count
);
1916 <= be32_to_cpu(btree
->hashval
)) {
1917 cursor
->blkno
= be32_to_cpu(btree
->before
);
1918 xfs_attr_trace_l_cb("descending",
1923 if (i
== be16_to_cpu(node
->hdr
.count
)) {
1924 xfs_da_brelse(NULL
, bp
);
1927 xfs_da_brelse(NULL
, bp
);
1933 * Roll upward through the blocks, processing each leaf block in
1934 * order. As long as there is space in the result buffer, keep
1935 * adding the information.
1939 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
)
1940 != XFS_ATTR_LEAF_MAGIC
)) {
1941 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1943 context
->dp
->i_mount
, leaf
);
1944 xfs_da_brelse(NULL
, bp
);
1945 return(XFS_ERROR(EFSCORRUPTED
));
1947 error
= xfs_attr_leaf_list_int(bp
, context
);
1949 xfs_da_brelse(NULL
, bp
);
1952 if (context
->seen_enough
|| leaf
->hdr
.info
.forw
== 0)
1954 cursor
->blkno
= be32_to_cpu(leaf
->hdr
.info
.forw
);
1955 xfs_da_brelse(NULL
, bp
);
1956 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1957 &bp
, XFS_ATTR_FORK
);
1960 if (unlikely((bp
== NULL
))) {
1961 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1963 context
->dp
->i_mount
);
1964 return(XFS_ERROR(EFSCORRUPTED
));
1967 xfs_da_brelse(NULL
, bp
);
1972 /*========================================================================
1973 * External routines for manipulating out-of-line attribute values.
1974 *========================================================================*/
1977 * Read the value associated with an attribute from the out-of-line buffer
1978 * that we stored it in.
1981 xfs_attr_rmtval_get(xfs_da_args_t
*args
)
1983 xfs_bmbt_irec_t map
[ATTR_RMTVALUE_MAPSIZE
];
1988 int nmap
, error
, tmp
, valuelen
, blkcnt
, i
;
1991 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
1993 mp
= args
->dp
->i_mount
;
1995 valuelen
= args
->valuelen
;
1996 lblkno
= args
->rmtblkno
;
1997 while (valuelen
> 0) {
1998 nmap
= ATTR_RMTVALUE_MAPSIZE
;
1999 error
= xfs_bmapi(args
->trans
, args
->dp
, (xfs_fileoff_t
)lblkno
,
2001 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2002 NULL
, 0, map
, &nmap
, NULL
, NULL
);
2007 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
2008 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
2009 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
2010 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
2011 blkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
2012 error
= xfs_read_buf(mp
, mp
->m_ddev_targp
, dblkno
,
2013 blkcnt
, XFS_BUF_LOCK
, &bp
);
2017 tmp
= (valuelen
< XFS_BUF_SIZE(bp
))
2018 ? valuelen
: XFS_BUF_SIZE(bp
);
2019 xfs_biomove(bp
, 0, tmp
, dst
, XFS_B_READ
);
2024 lblkno
+= map
[i
].br_blockcount
;
2027 ASSERT(valuelen
== 0);
2032 * Write the value associated with an attribute into the out-of-line buffer
2033 * that we have defined for it.
2036 xfs_attr_rmtval_set(xfs_da_args_t
*args
)
2039 xfs_fileoff_t lfileoff
;
2041 xfs_bmbt_irec_t map
;
2046 int blkcnt
, valuelen
, nmap
, error
, tmp
, committed
;
2053 * Find a "hole" in the attribute address space large enough for
2054 * us to drop the new attribute's value into.
2056 blkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
2058 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
2063 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
2064 args
->rmtblkcnt
= blkcnt
;
2067 * Roll through the "value", allocating blocks on disk as required.
2069 while (blkcnt
> 0) {
2071 * Allocate a single extent, up to the size of the value.
2073 xfs_bmap_init(args
->flist
, args
->firstblock
);
2075 error
= xfs_bmapi(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
2077 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
|
2079 args
->firstblock
, args
->total
, &map
, &nmap
,
2082 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
2088 xfs_bmap_cancel(args
->flist
);
2093 * bmap_finish() may have committed the last trans and started
2094 * a new one. We need the inode to be in all transactions.
2097 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
2098 xfs_trans_ihold(args
->trans
, dp
);
2102 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
2103 (map
.br_startblock
!= HOLESTARTBLOCK
));
2104 lblkno
+= map
.br_blockcount
;
2105 blkcnt
-= map
.br_blockcount
;
2108 * Start the next trans in the chain.
2110 error
= xfs_trans_roll(&args
->trans
, dp
);
2116 * Roll through the "value", copying the attribute value to the
2117 * already-allocated blocks. Blocks are written synchronously
2118 * so that we can know they are all on disk before we turn off
2119 * the INCOMPLETE flag.
2121 lblkno
= args
->rmtblkno
;
2122 valuelen
= args
->valuelen
;
2123 while (valuelen
> 0) {
2125 * Try to remember where we decided to put the value.
2127 xfs_bmap_init(args
->flist
, args
->firstblock
);
2129 error
= xfs_bmapi(NULL
, dp
, (xfs_fileoff_t
)lblkno
,
2131 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2132 args
->firstblock
, 0, &map
, &nmap
,
2138 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
2139 (map
.br_startblock
!= HOLESTARTBLOCK
));
2141 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
2142 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
2144 bp
= xfs_buf_get_flags(mp
->m_ddev_targp
, dblkno
,
2145 blkcnt
, XFS_BUF_LOCK
);
2147 ASSERT(!XFS_BUF_GETERROR(bp
));
2149 tmp
= (valuelen
< XFS_BUF_SIZE(bp
)) ? valuelen
:
2151 xfs_biomove(bp
, 0, tmp
, src
, XFS_B_WRITE
);
2152 if (tmp
< XFS_BUF_SIZE(bp
))
2153 xfs_biozero(bp
, tmp
, XFS_BUF_SIZE(bp
) - tmp
);
2154 if ((error
= xfs_bwrite(mp
, bp
))) {/* GROT: NOTE: synchronous write */
2160 lblkno
+= map
.br_blockcount
;
2162 ASSERT(valuelen
== 0);
2167 * Remove the value associated with an attribute by deleting the
2168 * out-of-line buffer that it is stored on.
2171 xfs_attr_rmtval_remove(xfs_da_args_t
*args
)
2174 xfs_bmbt_irec_t map
;
2178 int valuelen
, blkcnt
, nmap
, error
, done
, committed
;
2180 mp
= args
->dp
->i_mount
;
2183 * Roll through the "value", invalidating the attribute value's
2186 lblkno
= args
->rmtblkno
;
2187 valuelen
= args
->rmtblkcnt
;
2188 while (valuelen
> 0) {
2190 * Try to remember where we decided to put the value.
2192 xfs_bmap_init(args
->flist
, args
->firstblock
);
2194 error
= xfs_bmapi(NULL
, args
->dp
, (xfs_fileoff_t
)lblkno
,
2196 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2197 args
->firstblock
, 0, &map
, &nmap
,
2203 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
2204 (map
.br_startblock
!= HOLESTARTBLOCK
));
2206 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
2207 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
2210 * If the "remote" value is in the cache, remove it.
2212 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, blkcnt
,
2213 XFS_INCORE_TRYLOCK
);
2216 XFS_BUF_UNDELAYWRITE(bp
);
2221 valuelen
-= map
.br_blockcount
;
2223 lblkno
+= map
.br_blockcount
;
2227 * Keep de-allocating extents until the remote-value region is gone.
2229 lblkno
= args
->rmtblkno
;
2230 blkcnt
= args
->rmtblkcnt
;
2233 xfs_bmap_init(args
->flist
, args
->firstblock
);
2234 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
2235 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2236 1, args
->firstblock
, args
->flist
,
2239 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
2245 xfs_bmap_cancel(args
->flist
);
2250 * bmap_finish() may have committed the last trans and started
2251 * a new one. We need the inode to be in all transactions.
2254 xfs_trans_ijoin(args
->trans
, args
->dp
, XFS_ILOCK_EXCL
);
2255 xfs_trans_ihold(args
->trans
, args
->dp
);
2259 * Close out trans and start the next one in the chain.
2261 error
= xfs_trans_roll(&args
->trans
, args
->dp
);
2268 #if defined(XFS_ATTR_TRACE)
2270 * Add a trace buffer entry for an attr_list context structure.
2273 xfs_attr_trace_l_c(char *where
, struct xfs_attr_list_context
*context
)
2275 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C
, where
, context
,
2276 (__psunsigned_t
)NULL
,
2277 (__psunsigned_t
)NULL
,
2278 (__psunsigned_t
)NULL
);
2282 * Add a trace buffer entry for a context structure and a Btree node.
2285 xfs_attr_trace_l_cn(char *where
, struct xfs_attr_list_context
*context
,
2286 struct xfs_da_intnode
*node
)
2288 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN
, where
, context
,
2289 (__psunsigned_t
)be16_to_cpu(node
->hdr
.count
),
2290 (__psunsigned_t
)be32_to_cpu(node
->btree
[0].hashval
),
2291 (__psunsigned_t
)be32_to_cpu(node
->btree
[
2292 be16_to_cpu(node
->hdr
.count
)-1].hashval
));
2296 * Add a trace buffer entry for a context structure and a Btree element.
2299 xfs_attr_trace_l_cb(char *where
, struct xfs_attr_list_context
*context
,
2300 struct xfs_da_node_entry
*btree
)
2302 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB
, where
, context
,
2303 (__psunsigned_t
)be32_to_cpu(btree
->hashval
),
2304 (__psunsigned_t
)be32_to_cpu(btree
->before
),
2305 (__psunsigned_t
)NULL
);
2309 * Add a trace buffer entry for a context structure and a leaf block.
2312 xfs_attr_trace_l_cl(char *where
, struct xfs_attr_list_context
*context
,
2313 struct xfs_attr_leafblock
*leaf
)
2315 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL
, where
, context
,
2316 (__psunsigned_t
)be16_to_cpu(leaf
->hdr
.count
),
2317 (__psunsigned_t
)be32_to_cpu(leaf
->entries
[0].hashval
),
2318 (__psunsigned_t
)be32_to_cpu(leaf
->entries
[
2319 be16_to_cpu(leaf
->hdr
.count
)-1].hashval
));
2323 * Add a trace buffer entry for the arguments given to the routine,
2327 xfs_attr_trace_enter(int type
, char *where
,
2328 struct xfs_attr_list_context
*context
,
2329 __psunsigned_t a13
, __psunsigned_t a14
,
2332 ASSERT(xfs_attr_trace_buf
);
2333 ktrace_enter(xfs_attr_trace_buf
, (void *)((__psunsigned_t
)type
),
2334 (void *)((__psunsigned_t
)where
),
2335 (void *)((__psunsigned_t
)context
->dp
),
2336 (void *)((__psunsigned_t
)context
->cursor
->hashval
),
2337 (void *)((__psunsigned_t
)context
->cursor
->blkno
),
2338 (void *)((__psunsigned_t
)context
->cursor
->offset
),
2339 (void *)((__psunsigned_t
)context
->alist
),
2340 (void *)((__psunsigned_t
)context
->bufsize
),
2341 (void *)((__psunsigned_t
)context
->count
),
2342 (void *)((__psunsigned_t
)context
->firstu
),
2344 (void *)((__psunsigned_t
)context
->dupcnt
),
2345 (void *)((__psunsigned_t
)context
->flags
),
2346 (void *)a13
, (void *)a14
, (void *)a15
);
2348 #endif /* XFS_ATTR_TRACE */