2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_alloc.h"
40 #include "xfs_btree.h"
41 #include "xfs_inode_item.h"
44 #include "xfs_attr_leaf.h"
45 #include "xfs_error.h"
46 #include "xfs_quota.h"
47 #include "xfs_trans_space.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
55 * Provide the external interfaces to manage attribute lists.
58 /*========================================================================
59 * Function prototypes for the kernel.
60 *========================================================================*/
63 * Internal routines when attribute list fits inside the inode.
65 STATIC
int xfs_attr_shortform_addname(xfs_da_args_t
*args
);
68 * Internal routines when attribute list is one block.
70 STATIC
int xfs_attr_leaf_get(xfs_da_args_t
*args
);
71 STATIC
int xfs_attr_leaf_addname(xfs_da_args_t
*args
);
72 STATIC
int xfs_attr_leaf_removename(xfs_da_args_t
*args
);
73 STATIC
int xfs_attr_leaf_list(xfs_attr_list_context_t
*context
);
76 * Internal routines when attribute list is more than one block.
78 STATIC
int xfs_attr_node_get(xfs_da_args_t
*args
);
79 STATIC
int xfs_attr_node_addname(xfs_da_args_t
*args
);
80 STATIC
int xfs_attr_node_removename(xfs_da_args_t
*args
);
81 STATIC
int xfs_attr_node_list(xfs_attr_list_context_t
*context
);
82 STATIC
int xfs_attr_fillstate(xfs_da_state_t
*state
);
83 STATIC
int xfs_attr_refillstate(xfs_da_state_t
*state
);
86 * Routines to manipulate out-of-line attribute values.
88 STATIC
int xfs_attr_rmtval_set(xfs_da_args_t
*args
);
89 STATIC
int xfs_attr_rmtval_remove(xfs_da_args_t
*args
);
91 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
94 xfs_attr_name_to_xname(
95 struct xfs_name
*xname
,
101 xname
->len
= strlen(aname
);
102 if (xname
->len
>= MAXNAMELEN
)
103 return EFAULT
; /* match IRIX behaviour */
110 struct xfs_inode
*ip
)
112 if (!XFS_IFORK_Q(ip
) ||
113 (ip
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
&&
114 ip
->i_d
.di_anextents
== 0))
119 /*========================================================================
120 * Overall external interface routines.
121 *========================================================================*/
125 struct xfs_inode
*ip
,
126 struct xfs_name
*name
,
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_get_int(ip
, &xname
, value
, valuelenp
, flags
);
193 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
198 * Calculate how many blocks we need for the new attribute,
202 struct xfs_inode
*ip
,
207 struct xfs_mount
*mp
= ip
->i_mount
;
212 * Determine space new attribute will use, and if it would be
213 * "local" or "remote" (note: local != inline).
215 size
= xfs_attr_leaf_newentsize(namelen
, valuelen
,
216 mp
->m_sb
.sb_blocksize
, local
);
218 nblks
= XFS_DAENTER_SPACE_RES(mp
, XFS_ATTR_FORK
);
220 if (size
> (mp
->m_sb
.sb_blocksize
>> 1)) {
221 /* Double split possible */
226 * Out of line attribute, cannot double split, but
227 * make room for the attribute value itself.
229 uint dblocks
= XFS_B_TO_FSB(mp
, valuelen
);
231 nblks
+= XFS_NEXTENTADD_SPACE_RES(mp
, dblocks
, XFS_ATTR_FORK
);
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 error
= xfs_qm_dqattach(dp
, 0);
257 * If the inode doesn't have an attribute fork, add one.
258 * (inode must not be locked when we call this routine)
260 if (XFS_IFORK_Q(dp
) == 0) {
261 int sf_size
= sizeof(xfs_attr_sf_hdr_t
) +
262 XFS_ATTR_SF_ENTSIZE_BYNAME(name
->len
, valuelen
);
264 if ((error
= xfs_bmap_add_attrfork(dp
, sf_size
, rsvd
)))
269 * Fill in the arg structure for this request.
271 memset((char *)&args
, 0, sizeof(args
));
272 args
.name
= name
->name
;
273 args
.namelen
= name
->len
;
275 args
.valuelen
= valuelen
;
277 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
279 args
.firstblock
= &firstblock
;
281 args
.whichfork
= XFS_ATTR_FORK
;
282 args
.op_flags
= XFS_DA_OP_ADDNAME
| XFS_DA_OP_OKNOENT
;
284 /* Size is now blocks for attribute data */
285 args
.total
= xfs_attr_calc_size(dp
, name
->len
, valuelen
, &local
);
288 * Start our first transaction of the day.
290 * All future transactions during this code must be "chained" off
291 * this one via the trans_dup() call. All transactions will contain
292 * the inode, and the inode will always be marked with trans_ihold().
293 * Since the inode will be locked in all transactions, we must log
294 * the inode in every transaction to let it float upward through
297 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_SET
);
300 * Root fork attributes can use reserved data blocks for this
301 * operation if necessary
305 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
307 if ((error
= xfs_trans_reserve(args
.trans
, args
.total
,
308 XFS_ATTRSET_LOG_RES(mp
, args
.total
), 0,
309 XFS_TRANS_PERM_LOG_RES
, XFS_ATTRSET_LOG_COUNT
))) {
310 xfs_trans_cancel(args
.trans
, 0);
313 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
315 error
= xfs_trans_reserve_quota_nblks(args
.trans
, dp
, args
.total
, 0,
316 rsvd
? XFS_QMOPT_RES_REGBLKS
| XFS_QMOPT_FORCE_RES
:
317 XFS_QMOPT_RES_REGBLKS
);
319 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
320 xfs_trans_cancel(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
324 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
325 xfs_trans_ihold(args
.trans
, dp
);
328 * If the attribute list is non-existent or a shortform list,
329 * upgrade it to a single-leaf-block attribute list.
331 if ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) ||
332 ((dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) &&
333 (dp
->i_d
.di_anextents
== 0))) {
336 * Build initial attribute list (if required).
338 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
)
339 xfs_attr_shortform_create(&args
);
342 * Try to add the attr to the attribute list in
345 error
= xfs_attr_shortform_addname(&args
);
346 if (error
!= ENOSPC
) {
348 * Commit the shortform mods, and we're done.
349 * NOTE: this is also the error path (EEXIST, etc).
351 ASSERT(args
.trans
!= NULL
);
354 * If this is a synchronous mount, make sure that
355 * the transaction goes to disk before returning
358 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
359 xfs_trans_set_sync(args
.trans
);
361 err2
= xfs_trans_commit(args
.trans
,
362 XFS_TRANS_RELEASE_LOG_RES
);
363 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
366 * Hit the inode change time.
368 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
369 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
371 return(error
== 0 ? err2
: error
);
375 * It won't fit in the shortform, transform to a leaf block.
376 * GROT: another possible req'mt for a double-split btree op.
378 xfs_bmap_init(args
.flist
, args
.firstblock
);
379 error
= xfs_attr_shortform_to_leaf(&args
);
381 error
= xfs_bmap_finish(&args
.trans
, args
.flist
,
387 xfs_bmap_cancel(&flist
);
392 * bmap_finish() may have committed the last trans and started
393 * a new one. We need the inode to be in all transactions.
396 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
397 xfs_trans_ihold(args
.trans
, dp
);
401 * Commit the leaf transformation. We'll need another (linked)
402 * transaction to add the new attribute to the leaf.
405 error
= xfs_trans_roll(&args
.trans
, dp
);
411 if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
412 error
= xfs_attr_leaf_addname(&args
);
414 error
= xfs_attr_node_addname(&args
);
421 * If this is a synchronous mount, make sure that the
422 * transaction goes to disk before returning to the user.
424 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
425 xfs_trans_set_sync(args
.trans
);
429 * Commit the last in the sequence of transactions.
431 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
432 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
433 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
436 * Hit the inode change time.
438 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
439 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
446 xfs_trans_cancel(args
.trans
,
447 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
448 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
461 struct xfs_name xname
;
463 XFS_STATS_INC(xs_attr_set
);
465 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
468 error
= xfs_attr_name_to_xname(&xname
, name
);
472 return xfs_attr_set_int(dp
, &xname
, value
, valuelen
, flags
);
476 * Generic handler routine to remove a name from an attribute list.
477 * Transitions attribute list from Btree to shortform as necessary.
480 xfs_attr_remove_int(xfs_inode_t
*dp
, struct xfs_name
*name
, int flags
)
483 xfs_fsblock_t firstblock
;
484 xfs_bmap_free_t flist
;
486 xfs_mount_t
*mp
= dp
->i_mount
;
489 * Fill in the arg structure for this request.
491 memset((char *)&args
, 0, sizeof(args
));
492 args
.name
= name
->name
;
493 args
.namelen
= name
->len
;
495 args
.hashval
= xfs_da_hashname(args
.name
, args
.namelen
);
497 args
.firstblock
= &firstblock
;
500 args
.whichfork
= XFS_ATTR_FORK
;
503 * Attach the dquots to the inode.
505 error
= xfs_qm_dqattach(dp
, 0);
510 * Start our first transaction of the day.
512 * All future transactions during this code must be "chained" off
513 * this one via the trans_dup() call. All transactions will contain
514 * the inode, and the inode will always be marked with trans_ihold().
515 * Since the inode will be locked in all transactions, we must log
516 * the inode in every transaction to let it float upward through
519 args
.trans
= xfs_trans_alloc(mp
, XFS_TRANS_ATTR_RM
);
522 * Root fork attributes can use reserved data blocks for this
523 * operation if necessary
526 if (flags
& ATTR_ROOT
)
527 args
.trans
->t_flags
|= XFS_TRANS_RESERVE
;
529 if ((error
= xfs_trans_reserve(args
.trans
,
530 XFS_ATTRRM_SPACE_RES(mp
),
531 XFS_ATTRRM_LOG_RES(mp
),
532 0, XFS_TRANS_PERM_LOG_RES
,
533 XFS_ATTRRM_LOG_COUNT
))) {
534 xfs_trans_cancel(args
.trans
, 0);
538 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
540 * No need to make quota reservations here. We expect to release some
541 * blocks not allocate in the common case.
543 xfs_trans_ijoin(args
.trans
, dp
, XFS_ILOCK_EXCL
);
544 xfs_trans_ihold(args
.trans
, dp
);
547 * Decide on what work routines to call based on the inode size.
549 if (!xfs_inode_hasattr(dp
)) {
550 error
= XFS_ERROR(ENOATTR
);
553 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_LOCAL
) {
554 ASSERT(dp
->i_afp
->if_flags
& XFS_IFINLINE
);
555 error
= xfs_attr_shortform_remove(&args
);
559 } else if (xfs_bmap_one_block(dp
, XFS_ATTR_FORK
)) {
560 error
= xfs_attr_leaf_removename(&args
);
562 error
= xfs_attr_node_removename(&args
);
569 * If this is a synchronous mount, make sure that the
570 * transaction goes to disk before returning to the user.
572 if (mp
->m_flags
& XFS_MOUNT_WSYNC
) {
573 xfs_trans_set_sync(args
.trans
);
577 * Commit the last in the sequence of transactions.
579 xfs_trans_log_inode(args
.trans
, dp
, XFS_ILOG_CORE
);
580 error
= xfs_trans_commit(args
.trans
, XFS_TRANS_RELEASE_LOG_RES
);
581 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
584 * Hit the inode change time.
586 if (!error
&& (flags
& ATTR_KERNOTIME
) == 0) {
587 xfs_ichgtime(dp
, XFS_ICHGTIME_CHG
);
594 xfs_trans_cancel(args
.trans
,
595 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
596 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
607 struct xfs_name xname
;
609 XFS_STATS_INC(xs_attr_remove
);
611 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
614 error
= xfs_attr_name_to_xname(&xname
, name
);
618 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
619 if (!xfs_inode_hasattr(dp
)) {
620 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
621 return XFS_ERROR(ENOATTR
);
623 xfs_iunlock(dp
, XFS_ILOCK_SHARED
);
625 return xfs_attr_remove_int(dp
, &xname
, flags
);
629 xfs_attr_list_int(xfs_attr_list_context_t
*context
)
632 xfs_inode_t
*dp
= context
->dp
;
634 XFS_STATS_INC(xs_attr_list
);
636 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
639 xfs_ilock(dp
, XFS_ILOCK_SHARED
);
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
);
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 trace_xfs_attr_list_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 trace_xfs_attr_list_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 trace_xfs_attr_list_wrong_blk(context
);
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 trace_xfs_attr_list_wrong_blk(context
);
1860 xfs_da_brelse(NULL
, bp
);
1862 } else if (cursor
->hashval
<=
1863 be32_to_cpu(leaf
->entries
[0].hashval
)) {
1864 trace_xfs_attr_list_wrong_blk(context
);
1865 xfs_da_brelse(NULL
, bp
);
1870 trace_xfs_attr_list_wrong_blk(context
);
1871 xfs_da_brelse(NULL
, bp
);
1878 * We did not find what we expected given the cursor's contents,
1879 * so we start from the top and work down based on the hash value.
1880 * Note that start of node block is same as start of leaf block.
1885 error
= xfs_da_read_buf(NULL
, context
->dp
,
1886 cursor
->blkno
, -1, &bp
,
1890 if (unlikely(bp
== NULL
)) {
1891 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1893 context
->dp
->i_mount
);
1894 return(XFS_ERROR(EFSCORRUPTED
));
1897 if (be16_to_cpu(node
->hdr
.info
.magic
)
1898 == XFS_ATTR_LEAF_MAGIC
)
1900 if (unlikely(be16_to_cpu(node
->hdr
.info
.magic
)
1901 != XFS_DA_NODE_MAGIC
)) {
1902 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1904 context
->dp
->i_mount
,
1906 xfs_da_brelse(NULL
, bp
);
1907 return(XFS_ERROR(EFSCORRUPTED
));
1909 btree
= node
->btree
;
1910 for (i
= 0; i
< be16_to_cpu(node
->hdr
.count
);
1913 <= be32_to_cpu(btree
->hashval
)) {
1914 cursor
->blkno
= be32_to_cpu(btree
->before
);
1915 trace_xfs_attr_list_node_descend(context
,
1920 if (i
== be16_to_cpu(node
->hdr
.count
)) {
1921 xfs_da_brelse(NULL
, bp
);
1924 xfs_da_brelse(NULL
, bp
);
1930 * Roll upward through the blocks, processing each leaf block in
1931 * order. As long as there is space in the result buffer, keep
1932 * adding the information.
1936 if (unlikely(be16_to_cpu(leaf
->hdr
.info
.magic
)
1937 != XFS_ATTR_LEAF_MAGIC
)) {
1938 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1940 context
->dp
->i_mount
, leaf
);
1941 xfs_da_brelse(NULL
, bp
);
1942 return(XFS_ERROR(EFSCORRUPTED
));
1944 error
= xfs_attr_leaf_list_int(bp
, context
);
1946 xfs_da_brelse(NULL
, bp
);
1949 if (context
->seen_enough
|| leaf
->hdr
.info
.forw
== 0)
1951 cursor
->blkno
= be32_to_cpu(leaf
->hdr
.info
.forw
);
1952 xfs_da_brelse(NULL
, bp
);
1953 error
= xfs_da_read_buf(NULL
, context
->dp
, cursor
->blkno
, -1,
1954 &bp
, XFS_ATTR_FORK
);
1957 if (unlikely((bp
== NULL
))) {
1958 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1960 context
->dp
->i_mount
);
1961 return(XFS_ERROR(EFSCORRUPTED
));
1964 xfs_da_brelse(NULL
, bp
);
1969 /*========================================================================
1970 * External routines for manipulating out-of-line attribute values.
1971 *========================================================================*/
1974 * Read the value associated with an attribute from the out-of-line buffer
1975 * that we stored it in.
1978 xfs_attr_rmtval_get(xfs_da_args_t
*args
)
1980 xfs_bmbt_irec_t map
[ATTR_RMTVALUE_MAPSIZE
];
1985 int nmap
, error
, tmp
, valuelen
, blkcnt
, i
;
1988 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
1990 mp
= args
->dp
->i_mount
;
1992 valuelen
= args
->valuelen
;
1993 lblkno
= args
->rmtblkno
;
1994 while (valuelen
> 0) {
1995 nmap
= ATTR_RMTVALUE_MAPSIZE
;
1996 error
= xfs_bmapi(args
->trans
, args
->dp
, (xfs_fileoff_t
)lblkno
,
1998 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
1999 NULL
, 0, map
, &nmap
, NULL
, NULL
);
2004 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
2005 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
2006 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
2007 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
2008 blkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
2009 error
= xfs_read_buf(mp
, mp
->m_ddev_targp
, dblkno
,
2011 XFS_BUF_LOCK
| XBF_DONT_BLOCK
,
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(mp
->m_ddev_targp
, dblkno
, blkcnt
,
2144 XFS_BUF_LOCK
| XBF_DONT_BLOCK
);
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
);