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"
50 #include "xfs_vnodeops.h"
55 * Provide the external interfaces to manage attribute lists.
58 /*========================================================================
59 * Function prototypes for the kernel.
60 *========================================================================*/
63 * Internal routines when attribute list fits inside the inode.
65 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
68 * Internal routines when attribute list is one block.
70 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
73 STATIC
int xfs_attr_leaf_list(xfs_attr_list_context_t
*context
);
76 * Internal routines when attribute list is more than one block.
78 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
79 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
80 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
81 STATIC
int xfs_attr_node_list(xfs_attr_list_context_t
*context
);
82 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
83 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
86 * Routines to manipulate out-of-line attribute values.
88 STATIC
int xfs_attr_rmtval_set(xfs_da_args_t
*args
);
89 STATIC
int xfs_attr_rmtval_remove(xfs_da_args_t
*args
);
91 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
93 #if defined(XFS_ATTR_TRACE)
94 ktrace_t
*xfs_attr_trace_buf
;
98 xfs_attr_name_to_xname(
99 struct xfs_name
*xname
,
105 xname
->len
= strlen(aname
);
106 if (xname
->len
>= MAXNAMELEN
)
107 return EFAULT
; /* match IRIX behaviour */
114 struct xfs_inode
*ip
)
116 if (!XFS_IFORK_Q(ip
) ||
117 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
118 ip
->i_d
.di_anextents
== 0))
123 /*========================================================================
124 * Overall external interface routines.
125 *========================================================================*/
128 xfs_attr_fetch(xfs_inode_t
*ip
, struct xfs_name
*name
,
129 char *value
, int *valuelenp
, int flags
)
134 if (!xfs_inode_hasattr(ip
))
138 * Fill in the arg structure for this request.
140 memset((char *)&args
, 0, sizeof(args
));
141 args
.name
= name
->name
;
142 args
.namelen
= name
->len
;
144 args
.valuelen
= *valuelenp
;
146 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
148 args
.whichfork
= XFS_ATTR_FORK
;
151 * Decide on what work routines to call based on the inode size.
153 if (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
154 error
= xfs_attr_shortform_getvalue(&args
);
155 } else if (xfs_bmap_one_block(ip
, XFS_ATTR_FORK
)) {
156 error
= xfs_attr_leaf_get(&args
);
158 error
= xfs_attr_node_get(&args
);
162 * Return the number of bytes in the value to the caller.
164 *valuelenp
= args
.valuelen
;
180 struct xfs_name xname
;
182 XFS_STATS_INC(xs_attr_get
);
184 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
187 error
= xfs_attr_name_to_xname(&xname
, name
);
191 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
192 error
= xfs_attr_fetch(ip
, &xname
, value
, valuelenp
, flags
);
193 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
198 * Calculate how many blocks we need for the new attribute,
202 struct xfs_inode
*ip
,
207 struct xfs_mount
*mp
= ip
->i_mount
;
212 * Determine space new attribute will use, and if it would be
213 * "local" or "remote" (note: local != inline).
215 size
= xfs_attr_leaf_newentsize(namelen
, valuelen
,
216 mp
->m_sb
.sb_blocksize
, local
);
218 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
220 if (size
> (mp
->m_sb
.sb_blocksize
>> 1)) {
221 /* Double split possible */
226 * Out of line attribute, cannot double split, but
227 * make room for the attribute value itself.
229 uint dblocks
= XFS_B_TO_FSB(mp
, valuelen
);
231 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
238 xfs_attr_set_int(xfs_inode_t
*dp
, struct xfs_name
*name
,
239 char *value
, int valuelen
, int flags
)
242 xfs_fsblock_t firstblock
;
243 xfs_bmap_free_t flist
;
244 int error
, err2
, committed
;
245 xfs_mount_t
*mp
= dp
->i_mount
;
246 int rsvd
= (flags
& ATTR_ROOT
) != 0;
250 * Attach the dquots to the inode.
252 if ((error
= XFS_QM_DQATTACH(mp
, 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(mp
, 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 if ((error
= XFS_QM_DQATTACH(mp
, dp
, 0)))
508 * Start our first transaction of the day.
510 * All future transactions during this code must be "chained" off
511 * this one via the trans_dup() call. All transactions will contain
512 * the inode, and the inode will always be marked with trans_ihold().
513 * Since the inode will be locked in all transactions, we must log
514 * the inode in every transaction to let it float upward through
517 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
520 * Root fork attributes can use reserved data blocks for this
521 * operation if necessary
524 if (flags
& ATTR_ROOT
)
525 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
527 if ((error
= xfs_trans_reserve(args
.trans
,
528 XFS_ATTRRM_SPACE_RES(mp
),
529 XFS_ATTRRM_LOG_RES(mp
),
530 0, XFS_TRANS_PERM_LOG_RES
,
531 XFS_ATTRRM_LOG_COUNT
))) {
532 xfs_trans_cancel(args
.trans
, 0);
536 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
538 * No need to make quota reservations here. We expect to release some
539 * blocks not allocate in the common case.
541 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
542 xfs_trans_ihold(args
.trans
, dp
);
545 * Decide on what work routines to call based on the inode size.
547 if (!xfs_inode_hasattr(dp
)) {
548 error
= XFS_ERROR(ENOATTR
);
551 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
552 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
553 error
= xfs_attr_shortform_remove(&args
);
557 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
558 error
= xfs_attr_leaf_removename(&args
);
560 error
= xfs_attr_node_removename(&args
);
567 * If this is a synchronous mount, make sure that the
568 * transaction goes to disk before returning to the user.
570 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
571 xfs_trans_set_sync(args
.trans
);
575 * Commit the last in the sequence of transactions.
577 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
578 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
579 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
582 * Hit the inode change time.
584 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
585 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
592 xfs_trans_cancel(args
.trans
,
593 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
594 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
605 struct xfs_name xname
;
607 XFS_STATS_INC(xs_attr_remove
);
609 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
612 error
= xfs_attr_name_to_xname(&xname
, name
);
616 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
617 if (!xfs_inode_hasattr(dp
)) {
618 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
619 return XFS_ERROR(ENOATTR
);
621 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
623 return xfs_attr_remove_int(dp
, &xname
, flags
);
627 xfs_attr_list_int(xfs_attr_list_context_t
*context
)
630 xfs_inode_t
*dp
= context
->dp
;
632 XFS_STATS_INC(xs_attr_list
);
634 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
637 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
638 xfs_attr_trace_l_c("syscall start", context
);
641 * Decide on what work routines to call based on the inode size.
643 if (!xfs_inode_hasattr(dp
)) {
645 } else if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
646 error
= xfs_attr_shortform_list(context
);
647 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
648 error
= xfs_attr_leaf_list(context
);
650 error
= xfs_attr_node_list(context
);
653 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
654 xfs_attr_trace_l_c("syscall end", context
);
659 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
660 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
661 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
662 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
663 & ~(sizeof(u_int32_t)-1))
666 * Format an attribute and copy it out to the user's buffer.
667 * Take care to check values and protect against them changing later,
668 * we may be reading them directly out of a user buffer.
672 xfs_attr_put_listent(xfs_attr_list_context_t
*context
, int flags
,
673 char *name
, int namelen
,
674 int valuelen
, char *value
)
676 struct attrlist
*alist
= (struct attrlist
*)context
->alist
;
680 ASSERT(!(context
->flags
& ATTR_KERNOVAL
));
681 ASSERT(context
->count
>= 0);
682 ASSERT(context
->count
< (ATTR_MAX_VALUELEN
/8));
683 ASSERT(context
->firstu
>= sizeof(*alist
));
684 ASSERT(context
->firstu
<= context
->bufsize
);
687 * Only list entries in the right namespace.
689 if (((context
->flags
& ATTR_SECURE
) == 0) !=
690 ((flags
& XFS_ATTR_SECURE
) == 0))
692 if (((context
->flags
& ATTR_ROOT
) == 0) !=
693 ((flags
& XFS_ATTR_ROOT
) == 0))
696 arraytop
= sizeof(*alist
) +
697 context
->count
* sizeof(alist
->al_offset
[0]);
698 context
->firstu
-= ATTR_ENTSIZE(namelen
);
699 if (context
->firstu
< arraytop
) {
700 xfs_attr_trace_l_c("buffer full", context
);
702 context
->seen_enough
= 1;
706 aep
= (attrlist_ent_t
*)&context
->alist
[context
->firstu
];
707 aep
->a_valuelen
= valuelen
;
708 memcpy(aep
->a_name
, name
, namelen
);
709 aep
->a_name
[namelen
] = 0;
710 alist
->al_offset
[context
->count
++] = context
->firstu
;
711 alist
->al_count
= context
->count
;
712 xfs_attr_trace_l_c("add", context
);
717 * Generate a list of extended attribute names and optionally
718 * also value lengths. Positive return value follows the XFS
719 * convention of being an error, zero or negative return code
720 * is the length of the buffer returned (negated), indicating
729 attrlist_cursor_kern_t
*cursor
)
731 xfs_attr_list_context_t context
;
732 struct attrlist
*alist
;
736 * Validate the cursor.
738 if (cursor
->pad1
|| cursor
->pad2
)
739 return(XFS_ERROR(EINVAL
));
740 if ((cursor
->initted
== 0) &&
741 (cursor
->hashval
|| cursor
->blkno
|| cursor
->offset
))
742 return XFS_ERROR(EINVAL
);
745 * Check for a properly aligned buffer.
747 if (((long)buffer
) & (sizeof(int)-1))
748 return XFS_ERROR(EFAULT
);
749 if (flags
& ATTR_KERNOVAL
)
753 * Initialize the output buffer.
755 memset(&context
, 0, sizeof(context
));
757 context
.cursor
= cursor
;
759 context
.flags
= flags
;
760 context
.alist
= buffer
;
761 context
.bufsize
= (bufsize
& ~(sizeof(int)-1)); /* align */
762 context
.firstu
= context
.bufsize
;
763 context
.put_listent
= xfs_attr_put_listent
;
765 alist
= (struct attrlist
*)context
.alist
;
768 alist
->al_offset
[0] = context
.bufsize
;
770 error
= xfs_attr_list_int(&context
);
776 xfs_attr_inactive(xfs_inode_t
*dp
)
783 ASSERT(! XFS_NOT_DQATTACHED(mp
, dp
));
785 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
786 if (!xfs_inode_hasattr(dp
) ||
787 dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
788 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
791 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
794 * Start our first transaction of the day.
796 * All future transactions during this code must be "chained" off
797 * this one via the trans_dup() call. All transactions will contain
798 * the inode, and the inode will always be marked with trans_ihold().
799 * Since the inode will be locked in all transactions, we must log
800 * the inode in every transaction to let it float upward through
803 trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTRINVAL
);
804 if ((error
= xfs_trans_reserve(trans
, 0, XFS_ATTRINVAL_LOG_RES(mp
), 0,
805 XFS_TRANS_PERM_LOG_RES
,
806 XFS_ATTRINVAL_LOG_COUNT
))) {
807 xfs_trans_cancel(trans
, 0);
810 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
813 * No need to make quota reservations here. We expect to release some
814 * blocks, not allocate, in the common case.
816 xfs_trans_ijoin(trans
, dp
, XFS_ILOCK_EXCL
);
817 xfs_trans_ihold(trans
, dp
);
820 * Decide on what work routines to call based on the inode size.
822 if (!xfs_inode_hasattr(dp
) ||
823 dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
827 error
= xfs_attr_root_inactive(&trans
, dp
);
831 * signal synchronous inactive transactions unless this
832 * is a synchronous mount filesystem in which case we
833 * know that we're here because we've been called out of
834 * xfs_inactive which means that the last reference is gone
835 * and the unlink transaction has already hit the disk so
836 * async inactive transactions are safe.
838 if ((error
= xfs_itruncate_finish(&trans
, dp
, 0LL, XFS_ATTR_FORK
,
839 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
)
844 * Commit the last in the sequence of transactions.
846 xfs_trans_log_inode(trans
, dp
, XFS_ILOG_CORE
);
847 error
= xfs_trans_commit(trans
, XFS_TRANS_RELEASE_LOG_RES
);
848 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
853 xfs_trans_cancel(trans
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
854 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
860 /*========================================================================
861 * External routines when attribute list is inside the inode
862 *========================================================================*/
865 * Add a name to the shortform attribute list structure
866 * This is the external routine.
869 xfs_attr_shortform_addname(xfs_da_args_t
*args
)
871 int newsize
, forkoff
, retval
;
873 retval
= xfs_attr_shortform_lookup(args
);
874 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
876 } else if (retval
== EEXIST
) {
877 if (args
->flags
& ATTR_CREATE
)
879 retval
= xfs_attr_shortform_remove(args
);
883 if (args
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
||
884 args
->valuelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
885 return(XFS_ERROR(ENOSPC
));
887 newsize
= XFS_ATTR_SF_TOTSIZE(args
->dp
);
888 newsize
+= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
890 forkoff
= xfs_attr_shortform_bytesfit(args
->dp
, newsize
);
892 return(XFS_ERROR(ENOSPC
));
894 xfs_attr_shortform_add(args
, forkoff
);
899 /*========================================================================
900 * External routines when attribute list is one block
901 *========================================================================*/
904 * Add a name to the leaf attribute list structure
906 * This leaf block cannot have a "remote" value, we only call this routine
907 * if bmap_one_block() says there is only one block (ie: no remote blks).
910 xfs_attr_leaf_addname(xfs_da_args_t
*args
)
914 int retval
, error
, committed
, forkoff
;
917 * Read the (only) block in the attribute list in.
921 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
928 * Look up the given attribute in the leaf block. Figure out if
929 * the given flags produce an error or call for an atomic rename.
931 retval
= xfs_attr_leaf_lookup_int(bp
, args
);
932 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
933 xfs_da_brelse(args
->trans
, bp
);
935 } else if (retval
== EEXIST
) {
936 if (args
->flags
& ATTR_CREATE
) { /* pure create op */
937 xfs_da_brelse(args
->trans
, bp
);
940 args
->op_flags
|= XFS_DA_OP_RENAME
; /* an atomic rename */
941 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
942 args
->index2
= args
->index
;
943 args
->rmtblkno2
= args
->rmtblkno
;
944 args
->rmtblkcnt2
= args
->rmtblkcnt
;
948 * Add the attribute to the leaf block, transitioning to a Btree
951 retval
= xfs_attr_leaf_add(bp
, args
);
953 if (retval
== ENOSPC
) {
955 * Promote the attribute list to the Btree format, then
956 * Commit that transaction so that the node_addname() call
957 * can manage its own transactions.
959 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
960 error
= xfs_attr_leaf_to_node(args
);
962 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
968 xfs_bmap_cancel(args
->flist
);
973 * bmap_finish() may have committed the last trans and started
974 * a new one. We need the inode to be in all transactions.
977 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
978 xfs_trans_ihold(args
->trans
, dp
);
982 * Commit the current trans (including the inode) and start
985 error
= xfs_trans_roll(&args
->trans
, dp
);
990 * Fob the whole rest of the problem off on the Btree code.
992 error
= xfs_attr_node_addname(args
);
997 * Commit the transaction that added the attr name so that
998 * later routines can manage their own transactions.
1000 error
= xfs_trans_roll(&args
->trans
, dp
);
1005 * If there was an out-of-line value, allocate the blocks we
1006 * identified for its storage and copy the value. This is done
1007 * after we create the attribute so that we don't overflow the
1008 * maximum size of a transaction and/or hit a deadlock.
1010 if (args
->rmtblkno
> 0) {
1011 error
= xfs_attr_rmtval_set(args
);
1017 * If this is an atomic rename operation, we must "flip" the
1018 * incomplete flags on the "new" and "old" attribute/value pairs
1019 * so that one disappears and one appears atomically. Then we
1020 * must remove the "old" attribute/value pair.
1022 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1024 * In a separate transaction, set the incomplete flag on the
1025 * "old" attr and clear the incomplete flag on the "new" attr.
1027 error
= xfs_attr_leaf_flipflags(args
);
1032 * Dismantle the "old" attribute/value pair by removing
1033 * a "remote" value (if it exists).
1035 args
->index
= args
->index2
;
1036 args
->blkno
= args
->blkno2
;
1037 args
->rmtblkno
= args
->rmtblkno2
;
1038 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1039 if (args
->rmtblkno
) {
1040 error
= xfs_attr_rmtval_remove(args
);
1046 * Read in the block containing the "old" attr, then
1047 * remove the "old" attr from that block (neat, huh!)
1049 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1,
1050 &bp
, XFS_ATTR_FORK
);
1054 (void)xfs_attr_leaf_remove(bp
, args
);
1057 * If the result is small enough, shrink it all into the inode.
1059 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1060 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1061 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1062 /* bp is gone due to xfs_da_shrink_inode */
1064 error
= xfs_bmap_finish(&args
->trans
,
1071 xfs_bmap_cancel(args
->flist
);
1076 * bmap_finish() may have committed the last trans
1077 * and started a new one. We need the inode to be
1078 * in all transactions.
1081 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1082 xfs_trans_ihold(args
->trans
, dp
);
1085 xfs_da_buf_done(bp
);
1088 * Commit the remove and start the next trans in series.
1090 error
= xfs_trans_roll(&args
->trans
, dp
);
1092 } else if (args
->rmtblkno
> 0) {
1094 * Added a "remote" value, just clear the incomplete flag.
1096 error
= xfs_attr_leaf_clearflag(args
);
1102 * Remove a name from the leaf attribute list structure
1104 * This leaf block cannot have a "remote" value, we only call this routine
1105 * if bmap_one_block() says there is only one block (ie: no remote blks).
1108 xfs_attr_leaf_removename(xfs_da_args_t
*args
)
1112 int error
, committed
, forkoff
;
1115 * Remove the attribute.
1119 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
1126 error
= xfs_attr_leaf_lookup_int(bp
, args
);
1127 if (error
== ENOATTR
) {
1128 xfs_da_brelse(args
->trans
, bp
);
1132 (void)xfs_attr_leaf_remove(bp
, args
);
1135 * If the result is small enough, shrink it all into the inode.
1137 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1138 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1139 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1140 /* bp is gone due to xfs_da_shrink_inode */
1142 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1148 xfs_bmap_cancel(args
->flist
);
1153 * bmap_finish() may have committed the last trans and started
1154 * a new one. We need the inode to be in all transactions.
1157 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1158 xfs_trans_ihold(args
->trans
, dp
);
1161 xfs_da_buf_done(bp
);
1166 * Look up a name in a leaf attribute list structure.
1168 * This leaf block cannot have a "remote" value, we only call this routine
1169 * if bmap_one_block() says there is only one block (ie: no remote blks).
1172 xfs_attr_leaf_get(xfs_da_args_t
*args
)
1178 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
1184 error
= xfs_attr_leaf_lookup_int(bp
, args
);
1185 if (error
!= EEXIST
) {
1186 xfs_da_brelse(args
->trans
, bp
);
1189 error
= xfs_attr_leaf_getvalue(bp
, args
);
1190 xfs_da_brelse(args
->trans
, bp
);
1191 if (!error
&& (args
->rmtblkno
> 0) && !(args
->flags
& ATTR_KERNOVAL
)) {
1192 error
= xfs_attr_rmtval_get(args
);
1198 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1201 xfs_attr_leaf_list(xfs_attr_list_context_t
*context
)
1203 xfs_attr_leafblock_t
*leaf
;
1207 context
->cursor
->blkno
= 0;
1208 error
= xfs_da_read_buf(NULL
, context
->dp
, 0, -1, &bp
, XFS_ATTR_FORK
);
1210 return XFS_ERROR(error
);
1213 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
) != XFS_ATTR_LEAF_MAGIC
)) {
1214 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW
,
1215 context
->dp
->i_mount
, leaf
);
1216 xfs_da_brelse(NULL
, bp
);
1217 return XFS_ERROR(EFSCORRUPTED
);
1220 error
= xfs_attr_leaf_list_int(bp
, context
);
1221 xfs_da_brelse(NULL
, bp
);
1222 return XFS_ERROR(error
);
1226 /*========================================================================
1227 * External routines when attribute list size > XFS_LBSIZE(mp).
1228 *========================================================================*/
1231 * Add a name to a Btree-format attribute list.
1233 * This will involve walking down the Btree, and may involve splitting
1234 * leaf nodes and even splitting intermediate nodes up to and including
1235 * the root node (a special case of an intermediate node).
1237 * "Remote" attribute values confuse the issue and atomic rename operations
1238 * add a whole extra layer of confusion on top of that.
1241 xfs_attr_node_addname(xfs_da_args_t
*args
)
1243 xfs_da_state_t
*state
;
1244 xfs_da_state_blk_t
*blk
;
1247 int committed
, retval
, error
;
1250 * Fill in bucket of arguments/results/context to carry around.
1255 state
= xfs_da_state_alloc();
1258 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1259 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1262 * Search to see if name already exists, and get back a pointer
1263 * to where it should go.
1265 error
= xfs_da_node_lookup_int(state
, &retval
);
1268 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1269 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1270 if ((args
->flags
& ATTR_REPLACE
) && (retval
== ENOATTR
)) {
1272 } else if (retval
== EEXIST
) {
1273 if (args
->flags
& ATTR_CREATE
)
1275 args
->op_flags
|= XFS_DA_OP_RENAME
; /* atomic rename op */
1276 args
->blkno2
= args
->blkno
; /* set 2nd entry info*/
1277 args
->index2
= args
->index
;
1278 args
->rmtblkno2
= args
->rmtblkno
;
1279 args
->rmtblkcnt2
= args
->rmtblkcnt
;
1281 args
->rmtblkcnt
= 0;
1284 retval
= xfs_attr_leaf_add(blk
->bp
, state
->args
);
1285 if (retval
== ENOSPC
) {
1286 if (state
->path
.active
== 1) {
1288 * Its really a single leaf node, but it had
1289 * out-of-line values so it looked like it *might*
1290 * have been a b-tree.
1292 xfs_da_state_free(state
);
1293 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1294 error
= xfs_attr_leaf_to_node(args
);
1296 error
= xfs_bmap_finish(&args
->trans
,
1303 xfs_bmap_cancel(args
->flist
);
1308 * bmap_finish() may have committed the last trans
1309 * and started a new one. We need the inode to be
1310 * in all transactions.
1313 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1314 xfs_trans_ihold(args
->trans
, dp
);
1318 * Commit the node conversion and start the next
1319 * trans in the chain.
1321 error
= xfs_trans_roll(&args
->trans
, dp
);
1329 * Split as many Btree elements as required.
1330 * This code tracks the new and old attr's location
1331 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1332 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1334 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1335 error
= xfs_da_split(state
);
1337 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1343 xfs_bmap_cancel(args
->flist
);
1348 * bmap_finish() may have committed the last trans and started
1349 * a new one. We need the inode to be in all transactions.
1352 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1353 xfs_trans_ihold(args
->trans
, dp
);
1357 * Addition succeeded, update Btree hashvals.
1359 xfs_da_fixhashpath(state
, &state
->path
);
1363 * Kill the state structure, we're done with it and need to
1364 * allow the buffers to come back later.
1366 xfs_da_state_free(state
);
1370 * Commit the leaf addition or btree split and start the next
1371 * trans in the chain.
1373 error
= xfs_trans_roll(&args
->trans
, dp
);
1378 * If there was an out-of-line value, allocate the blocks we
1379 * identified for its storage and copy the value. This is done
1380 * after we create the attribute so that we don't overflow the
1381 * maximum size of a transaction and/or hit a deadlock.
1383 if (args
->rmtblkno
> 0) {
1384 error
= xfs_attr_rmtval_set(args
);
1390 * If this is an atomic rename operation, we must "flip" the
1391 * incomplete flags on the "new" and "old" attribute/value pairs
1392 * so that one disappears and one appears atomically. Then we
1393 * must remove the "old" attribute/value pair.
1395 if (args
->op_flags
& XFS_DA_OP_RENAME
) {
1397 * In a separate transaction, set the incomplete flag on the
1398 * "old" attr and clear the incomplete flag on the "new" attr.
1400 error
= xfs_attr_leaf_flipflags(args
);
1405 * Dismantle the "old" attribute/value pair by removing
1406 * a "remote" value (if it exists).
1408 args
->index
= args
->index2
;
1409 args
->blkno
= args
->blkno2
;
1410 args
->rmtblkno
= args
->rmtblkno2
;
1411 args
->rmtblkcnt
= args
->rmtblkcnt2
;
1412 if (args
->rmtblkno
) {
1413 error
= xfs_attr_rmtval_remove(args
);
1419 * Re-find the "old" attribute entry after any split ops.
1420 * The INCOMPLETE flag means that we will find the "old"
1421 * attr, not the "new" one.
1423 args
->flags
|= XFS_ATTR_INCOMPLETE
;
1424 state
= xfs_da_state_alloc();
1427 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1428 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1430 error
= xfs_da_node_lookup_int(state
, &retval
);
1435 * Remove the name and update the hashvals in the tree.
1437 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1438 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1439 error
= xfs_attr_leaf_remove(blk
->bp
, args
);
1440 xfs_da_fixhashpath(state
, &state
->path
);
1443 * Check to see if the tree needs to be collapsed.
1445 if (retval
&& (state
->path
.active
> 1)) {
1446 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1447 error
= xfs_da_join(state
);
1449 error
= xfs_bmap_finish(&args
->trans
,
1456 xfs_bmap_cancel(args
->flist
);
1461 * bmap_finish() may have committed the last trans
1462 * and started a new one. We need the inode to be
1463 * in all transactions.
1466 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1467 xfs_trans_ihold(args
->trans
, dp
);
1472 * Commit and start the next trans in the chain.
1474 error
= xfs_trans_roll(&args
->trans
, dp
);
1478 } else if (args
->rmtblkno
> 0) {
1480 * Added a "remote" value, just clear the incomplete flag.
1482 error
= xfs_attr_leaf_clearflag(args
);
1490 xfs_da_state_free(state
);
1497 * Remove a name from a B-tree attribute list.
1499 * This will involve walking down the Btree, and may involve joining
1500 * leaf nodes and even joining intermediate nodes up to and including
1501 * the root node (a special case of an intermediate node).
1504 xfs_attr_node_removename(xfs_da_args_t
*args
)
1506 xfs_da_state_t
*state
;
1507 xfs_da_state_blk_t
*blk
;
1510 int retval
, error
, committed
, forkoff
;
1513 * Tie a string around our finger to remind us where we are.
1516 state
= xfs_da_state_alloc();
1518 state
->mp
= dp
->i_mount
;
1519 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1520 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1523 * Search to see if name exists, and get back a pointer to it.
1525 error
= xfs_da_node_lookup_int(state
, &retval
);
1526 if (error
|| (retval
!= EEXIST
)) {
1533 * If there is an out-of-line value, de-allocate the blocks.
1534 * This is done before we remove the attribute so that we don't
1535 * overflow the maximum size of a transaction and/or hit a deadlock.
1537 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1538 ASSERT(blk
->bp
!= NULL
);
1539 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1540 if (args
->rmtblkno
> 0) {
1542 * Fill in disk block numbers in the state structure
1543 * so that we can get the buffers back after we commit
1544 * several transactions in the following calls.
1546 error
= xfs_attr_fillstate(state
);
1551 * Mark the attribute as INCOMPLETE, then bunmapi() the
1554 error
= xfs_attr_leaf_setflag(args
);
1557 error
= xfs_attr_rmtval_remove(args
);
1562 * Refill the state structure with buffers, the prior calls
1563 * released our buffers.
1565 error
= xfs_attr_refillstate(state
);
1571 * Remove the name and update the hashvals in the tree.
1573 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1574 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1575 retval
= xfs_attr_leaf_remove(blk
->bp
, args
);
1576 xfs_da_fixhashpath(state
, &state
->path
);
1579 * Check to see if the tree needs to be collapsed.
1581 if (retval
&& (state
->path
.active
> 1)) {
1582 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1583 error
= xfs_da_join(state
);
1585 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
1591 xfs_bmap_cancel(args
->flist
);
1596 * bmap_finish() may have committed the last trans and started
1597 * a new one. We need the inode to be in all transactions.
1600 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1601 xfs_trans_ihold(args
->trans
, dp
);
1605 * Commit the Btree join operation and start a new trans.
1607 error
= xfs_trans_roll(&args
->trans
, dp
);
1613 * If the result is small enough, push it all into the inode.
1615 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
1617 * Have to get rid of the copy of this dabuf in the state.
1619 ASSERT(state
->path
.active
== 1);
1620 ASSERT(state
->path
.blk
[0].bp
);
1621 xfs_da_buf_done(state
->path
.blk
[0].bp
);
1622 state
->path
.blk
[0].bp
= NULL
;
1624 error
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp
,
1628 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t
*)
1629 bp
->data
)->hdr
.info
.magic
)
1630 == XFS_ATTR_LEAF_MAGIC
);
1632 if ((forkoff
= xfs_attr_shortform_allfit(bp
, dp
))) {
1633 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
1634 error
= xfs_attr_leaf_to_shortform(bp
, args
, forkoff
);
1635 /* bp is gone due to xfs_da_shrink_inode */
1637 error
= xfs_bmap_finish(&args
->trans
,
1644 xfs_bmap_cancel(args
->flist
);
1649 * bmap_finish() may have committed the last trans
1650 * and started a new one. We need the inode to be
1651 * in all transactions.
1654 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
1655 xfs_trans_ihold(args
->trans
, dp
);
1658 xfs_da_brelse(args
->trans
, bp
);
1663 xfs_da_state_free(state
);
1668 * Fill in the disk block numbers in the state structure for the buffers
1669 * that are attached to the state structure.
1670 * This is done so that we can quickly reattach ourselves to those buffers
1671 * after some set of transaction commits have released these buffers.
1674 xfs_attr_fillstate(xfs_da_state_t
*state
)
1676 xfs_da_state_path_t
*path
;
1677 xfs_da_state_blk_t
*blk
;
1681 * Roll down the "path" in the state structure, storing the on-disk
1682 * block number for those buffers in the "path".
1684 path
= &state
->path
;
1685 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1686 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1688 blk
->disk_blkno
= xfs_da_blkno(blk
->bp
);
1689 xfs_da_buf_done(blk
->bp
);
1692 blk
->disk_blkno
= 0;
1697 * Roll down the "altpath" in the state structure, storing the on-disk
1698 * block number for those buffers in the "altpath".
1700 path
= &state
->altpath
;
1701 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1702 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1704 blk
->disk_blkno
= xfs_da_blkno(blk
->bp
);
1705 xfs_da_buf_done(blk
->bp
);
1708 blk
->disk_blkno
= 0;
1716 * Reattach the buffers to the state structure based on the disk block
1717 * numbers stored in the state structure.
1718 * This is done after some set of transaction commits have released those
1719 * buffers from our grip.
1722 xfs_attr_refillstate(xfs_da_state_t
*state
)
1724 xfs_da_state_path_t
*path
;
1725 xfs_da_state_blk_t
*blk
;
1729 * Roll down the "path" in the state structure, storing the on-disk
1730 * block number for those buffers in the "path".
1732 path
= &state
->path
;
1733 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1734 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1735 if (blk
->disk_blkno
) {
1736 error
= xfs_da_read_buf(state
->args
->trans
,
1738 blk
->blkno
, blk
->disk_blkno
,
1739 &blk
->bp
, XFS_ATTR_FORK
);
1748 * Roll down the "altpath" in the state structure, storing the on-disk
1749 * block number for those buffers in the "altpath".
1751 path
= &state
->altpath
;
1752 ASSERT((path
->active
>= 0) && (path
->active
< XFS_DA_NODE_MAXDEPTH
));
1753 for (blk
= path
->blk
, level
= 0; level
< path
->active
; blk
++, level
++) {
1754 if (blk
->disk_blkno
) {
1755 error
= xfs_da_read_buf(state
->args
->trans
,
1757 blk
->blkno
, blk
->disk_blkno
,
1758 &blk
->bp
, XFS_ATTR_FORK
);
1770 * Look up a filename in a node attribute list.
1772 * This routine gets called for any attribute fork that has more than one
1773 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1774 * "remote" values taking up more blocks.
1777 xfs_attr_node_get(xfs_da_args_t
*args
)
1779 xfs_da_state_t
*state
;
1780 xfs_da_state_blk_t
*blk
;
1784 state
= xfs_da_state_alloc();
1786 state
->mp
= args
->dp
->i_mount
;
1787 state
->blocksize
= state
->mp
->m_sb
.sb_blocksize
;
1788 state
->node_ents
= state
->mp
->m_attr_node_ents
;
1791 * Search to see if name exists, and get back a pointer to it.
1793 error
= xfs_da_node_lookup_int(state
, &retval
);
1796 } else if (retval
== EEXIST
) {
1797 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1798 ASSERT(blk
->bp
!= NULL
);
1799 ASSERT(blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1802 * Get the value, local or "remote"
1804 retval
= xfs_attr_leaf_getvalue(blk
->bp
, args
);
1805 if (!retval
&& (args
->rmtblkno
> 0)
1806 && !(args
->flags
& ATTR_KERNOVAL
)) {
1807 retval
= xfs_attr_rmtval_get(args
);
1812 * If not in a transaction, we have to release all the buffers.
1814 for (i
= 0; i
< state
->path
.active
; i
++) {
1815 xfs_da_brelse(args
->trans
, state
->path
.blk
[i
].bp
);
1816 state
->path
.blk
[i
].bp
= NULL
;
1819 xfs_da_state_free(state
);
1823 STATIC
int /* error */
1824 xfs_attr_node_list(xfs_attr_list_context_t
*context
)
1826 attrlist_cursor_kern_t
*cursor
;
1827 xfs_attr_leafblock_t
*leaf
;
1828 xfs_da_intnode_t
*node
;
1829 xfs_da_node_entry_t
*btree
;
1833 cursor
= context
->cursor
;
1834 cursor
->initted
= 1;
1837 * Do all sorts of validation on the passed-in cursor structure.
1838 * If anything is amiss, ignore the cursor and look up the hashval
1839 * starting from the btree root.
1842 if (cursor
->blkno
> 0) {
1843 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1844 &bp
, XFS_ATTR_FORK
);
1845 if ((error
!= 0) && (error
!= EFSCORRUPTED
))
1849 switch (be16_to_cpu(node
->hdr
.info
.magic
)) {
1850 case XFS_DA_NODE_MAGIC
:
1851 xfs_attr_trace_l_cn("wrong blk", context
, node
);
1852 xfs_da_brelse(NULL
, bp
);
1855 case XFS_ATTR_LEAF_MAGIC
:
1857 if (cursor
->hashval
> be32_to_cpu(leaf
->entries
[
1858 be16_to_cpu(leaf
->hdr
.count
)-1].hashval
)) {
1859 xfs_attr_trace_l_cl("wrong blk",
1861 xfs_da_brelse(NULL
, bp
);
1863 } else if (cursor
->hashval
<=
1864 be32_to_cpu(leaf
->entries
[0].hashval
)) {
1865 xfs_attr_trace_l_cl("maybe wrong blk",
1867 xfs_da_brelse(NULL
, bp
);
1872 xfs_attr_trace_l_c("wrong blk - ??", context
);
1873 xfs_da_brelse(NULL
, bp
);
1880 * We did not find what we expected given the cursor's contents,
1881 * so we start from the top and work down based on the hash value.
1882 * Note that start of node block is same as start of leaf block.
1887 error
= xfs_da_read_buf(NULL
, context
->dp
,
1888 cursor
->blkno
, -1, &bp
,
1892 if (unlikely(bp
== NULL
)) {
1893 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1895 context
->dp
->i_mount
);
1896 return(XFS_ERROR(EFSCORRUPTED
));
1899 if (be16_to_cpu(node
->hdr
.info
.magic
)
1900 == XFS_ATTR_LEAF_MAGIC
)
1902 if (unlikely(be16_to_cpu(node
->hdr
.info
.magic
)
1903 != XFS_DA_NODE_MAGIC
)) {
1904 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1906 context
->dp
->i_mount
,
1908 xfs_da_brelse(NULL
, bp
);
1909 return(XFS_ERROR(EFSCORRUPTED
));
1911 btree
= node
->btree
;
1912 for (i
= 0; i
< be16_to_cpu(node
->hdr
.count
);
1915 <= be32_to_cpu(btree
->hashval
)) {
1916 cursor
->blkno
= be32_to_cpu(btree
->before
);
1917 xfs_attr_trace_l_cb("descending",
1922 if (i
== be16_to_cpu(node
->hdr
.count
)) {
1923 xfs_da_brelse(NULL
, bp
);
1926 xfs_da_brelse(NULL
, bp
);
1932 * Roll upward through the blocks, processing each leaf block in
1933 * order. As long as there is space in the result buffer, keep
1934 * adding the information.
1938 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
)
1939 != XFS_ATTR_LEAF_MAGIC
)) {
1940 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1942 context
->dp
->i_mount
, leaf
);
1943 xfs_da_brelse(NULL
, bp
);
1944 return(XFS_ERROR(EFSCORRUPTED
));
1946 error
= xfs_attr_leaf_list_int(bp
, context
);
1948 xfs_da_brelse(NULL
, bp
);
1951 if (context
->seen_enough
|| leaf
->hdr
.info
.forw
== 0)
1953 cursor
->blkno
= be32_to_cpu(leaf
->hdr
.info
.forw
);
1954 xfs_da_brelse(NULL
, bp
);
1955 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1956 &bp
, XFS_ATTR_FORK
);
1959 if (unlikely((bp
== NULL
))) {
1960 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1962 context
->dp
->i_mount
);
1963 return(XFS_ERROR(EFSCORRUPTED
));
1966 xfs_da_brelse(NULL
, bp
);
1971 /*========================================================================
1972 * External routines for manipulating out-of-line attribute values.
1973 *========================================================================*/
1976 * Read the value associated with an attribute from the out-of-line buffer
1977 * that we stored it in.
1980 xfs_attr_rmtval_get(xfs_da_args_t
*args
)
1982 xfs_bmbt_irec_t map
[ATTR_RMTVALUE_MAPSIZE
];
1987 int nmap
, error
, tmp
, valuelen
, blkcnt
, i
;
1990 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
1992 mp
= args
->dp
->i_mount
;
1994 valuelen
= args
->valuelen
;
1995 lblkno
= args
->rmtblkno
;
1996 while (valuelen
> 0) {
1997 nmap
= ATTR_RMTVALUE_MAPSIZE
;
1998 error
= xfs_bmapi(args
->trans
, args
->dp
, (xfs_fileoff_t
)lblkno
,
2000 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2001 NULL
, 0, map
, &nmap
, NULL
, NULL
);
2006 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
2007 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
2008 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
2009 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
2010 blkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
2011 error
= xfs_read_buf(mp
, mp
->m_ddev_targp
, dblkno
,
2012 blkcnt
, XFS_BUF_LOCK
, &bp
);
2016 tmp
= (valuelen
< XFS_BUF_SIZE(bp
))
2017 ? valuelen
: XFS_BUF_SIZE(bp
);
2018 xfs_biomove(bp
, 0, tmp
, dst
, XFS_B_READ
);
2023 lblkno
+= map
[i
].br_blockcount
;
2026 ASSERT(valuelen
== 0);
2031 * Write the value associated with an attribute into the out-of-line buffer
2032 * that we have defined for it.
2035 xfs_attr_rmtval_set(xfs_da_args_t
*args
)
2038 xfs_fileoff_t lfileoff
;
2040 xfs_bmbt_irec_t map
;
2045 int blkcnt
, valuelen
, nmap
, error
, tmp
, committed
;
2052 * Find a "hole" in the attribute address space large enough for
2053 * us to drop the new attribute's value into.
2055 blkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
2057 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
2062 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
2063 args
->rmtblkcnt
= blkcnt
;
2066 * Roll through the "value", allocating blocks on disk as required.
2068 while (blkcnt
> 0) {
2070 * Allocate a single extent, up to the size of the value.
2072 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
2074 error
= xfs_bmapi(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
2076 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
|
2078 args
->firstblock
, args
->total
, &map
, &nmap
,
2081 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
2087 xfs_bmap_cancel(args
->flist
);
2092 * bmap_finish() may have committed the last trans and started
2093 * a new one. We need the inode to be in all transactions.
2096 xfs_trans_ijoin(args
->trans
, dp
, XFS_ILOCK_EXCL
);
2097 xfs_trans_ihold(args
->trans
, dp
);
2101 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
2102 (map
.br_startblock
!= HOLESTARTBLOCK
));
2103 lblkno
+= map
.br_blockcount
;
2104 blkcnt
-= map
.br_blockcount
;
2107 * Start the next trans in the chain.
2109 error
= xfs_trans_roll(&args
->trans
, dp
);
2115 * Roll through the "value", copying the attribute value to the
2116 * already-allocated blocks. Blocks are written synchronously
2117 * so that we can know they are all on disk before we turn off
2118 * the INCOMPLETE flag.
2120 lblkno
= args
->rmtblkno
;
2121 valuelen
= args
->valuelen
;
2122 while (valuelen
> 0) {
2124 * Try to remember where we decided to put the value.
2126 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
2128 error
= xfs_bmapi(NULL
, dp
, (xfs_fileoff_t
)lblkno
,
2130 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2131 args
->firstblock
, 0, &map
, &nmap
,
2137 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
2138 (map
.br_startblock
!= HOLESTARTBLOCK
));
2140 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
2141 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
2143 bp
= xfs_buf_get_flags(mp
->m_ddev_targp
, dblkno
,
2144 blkcnt
, XFS_BUF_LOCK
);
2146 ASSERT(!XFS_BUF_GETERROR(bp
));
2148 tmp
= (valuelen
< XFS_BUF_SIZE(bp
)) ? valuelen
:
2150 xfs_biomove(bp
, 0, tmp
, src
, XFS_B_WRITE
);
2151 if (tmp
< XFS_BUF_SIZE(bp
))
2152 xfs_biozero(bp
, tmp
, XFS_BUF_SIZE(bp
) - tmp
);
2153 if ((error
= xfs_bwrite(mp
, bp
))) {/* GROT: NOTE: synchronous write */
2159 lblkno
+= map
.br_blockcount
;
2161 ASSERT(valuelen
== 0);
2166 * Remove the value associated with an attribute by deleting the
2167 * out-of-line buffer that it is stored on.
2170 xfs_attr_rmtval_remove(xfs_da_args_t
*args
)
2173 xfs_bmbt_irec_t map
;
2177 int valuelen
, blkcnt
, nmap
, error
, done
, committed
;
2179 mp
= args
->dp
->i_mount
;
2182 * Roll through the "value", invalidating the attribute value's
2185 lblkno
= args
->rmtblkno
;
2186 valuelen
= args
->rmtblkcnt
;
2187 while (valuelen
> 0) {
2189 * Try to remember where we decided to put the value.
2191 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
2193 error
= xfs_bmapi(NULL
, args
->dp
, (xfs_fileoff_t
)lblkno
,
2195 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2196 args
->firstblock
, 0, &map
, &nmap
,
2202 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
2203 (map
.br_startblock
!= HOLESTARTBLOCK
));
2205 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
2206 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
2209 * If the "remote" value is in the cache, remove it.
2211 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, blkcnt
,
2212 XFS_INCORE_TRYLOCK
);
2215 XFS_BUF_UNDELAYWRITE(bp
);
2220 valuelen
-= map
.br_blockcount
;
2222 lblkno
+= map
.br_blockcount
;
2226 * Keep de-allocating extents until the remote-value region is gone.
2228 lblkno
= args
->rmtblkno
;
2229 blkcnt
= args
->rmtblkcnt
;
2232 XFS_BMAP_INIT(args
->flist
, args
->firstblock
);
2233 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
2234 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2235 1, args
->firstblock
, args
->flist
,
2238 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
2244 xfs_bmap_cancel(args
->flist
);
2249 * bmap_finish() may have committed the last trans and started
2250 * a new one. We need the inode to be in all transactions.
2253 xfs_trans_ijoin(args
->trans
, args
->dp
, XFS_ILOCK_EXCL
);
2254 xfs_trans_ihold(args
->trans
, args
->dp
);
2258 * Close out trans and start the next one in the chain.
2260 error
= xfs_trans_roll(&args
->trans
, args
->dp
);
2267 #if defined(XFS_ATTR_TRACE)
2269 * Add a trace buffer entry for an attr_list context structure.
2272 xfs_attr_trace_l_c(char *where
, struct xfs_attr_list_context
*context
)
2274 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C
, where
, context
,
2275 (__psunsigned_t
)NULL
,
2276 (__psunsigned_t
)NULL
,
2277 (__psunsigned_t
)NULL
);
2281 * Add a trace buffer entry for a context structure and a Btree node.
2284 xfs_attr_trace_l_cn(char *where
, struct xfs_attr_list_context
*context
,
2285 struct xfs_da_intnode
*node
)
2287 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN
, where
, context
,
2288 (__psunsigned_t
)be16_to_cpu(node
->hdr
.count
),
2289 (__psunsigned_t
)be32_to_cpu(node
->btree
[0].hashval
),
2290 (__psunsigned_t
)be32_to_cpu(node
->btree
[
2291 be16_to_cpu(node
->hdr
.count
)-1].hashval
));
2295 * Add a trace buffer entry for a context structure and a Btree element.
2298 xfs_attr_trace_l_cb(char *where
, struct xfs_attr_list_context
*context
,
2299 struct xfs_da_node_entry
*btree
)
2301 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB
, where
, context
,
2302 (__psunsigned_t
)be32_to_cpu(btree
->hashval
),
2303 (__psunsigned_t
)be32_to_cpu(btree
->before
),
2304 (__psunsigned_t
)NULL
);
2308 * Add a trace buffer entry for a context structure and a leaf block.
2311 xfs_attr_trace_l_cl(char *where
, struct xfs_attr_list_context
*context
,
2312 struct xfs_attr_leafblock
*leaf
)
2314 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL
, where
, context
,
2315 (__psunsigned_t
)be16_to_cpu(leaf
->hdr
.count
),
2316 (__psunsigned_t
)be32_to_cpu(leaf
->entries
[0].hashval
),
2317 (__psunsigned_t
)be32_to_cpu(leaf
->entries
[
2318 be16_to_cpu(leaf
->hdr
.count
)-1].hashval
));
2322 * Add a trace buffer entry for the arguments given to the routine,
2326 xfs_attr_trace_enter(int type
, char *where
,
2327 struct xfs_attr_list_context
*context
,
2328 __psunsigned_t a13
, __psunsigned_t a14
,
2331 ASSERT(xfs_attr_trace_buf
);
2332 ktrace_enter(xfs_attr_trace_buf
, (void *)((__psunsigned_t
)type
),
2333 (void *)((__psunsigned_t
)where
),
2334 (void *)((__psunsigned_t
)context
->dp
),
2335 (void *)((__psunsigned_t
)context
->cursor
->hashval
),
2336 (void *)((__psunsigned_t
)context
->cursor
->blkno
),
2337 (void *)((__psunsigned_t
)context
->cursor
->offset
),
2338 (void *)((__psunsigned_t
)context
->alist
),
2339 (void *)((__psunsigned_t
)context
->bufsize
),
2340 (void *)((__psunsigned_t
)context
->count
),
2341 (void *)((__psunsigned_t
)context
->firstu
),
2343 (void *)((__psunsigned_t
)context
->dupcnt
),
2344 (void *)((__psunsigned_t
)context
->flags
),
2345 (void *)a13
, (void *)a14
, (void *)a15
);
2347 #endif /* XFS_ATTR_TRACE */