2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35 * GROT: figure out how to recover gracefully when bmap returns ENOSPC.
40 #include "xfs_macros.h"
41 #include "xfs_types.h"
44 #include "xfs_trans.h"
49 #include "xfs_dmapi.h"
50 #include "xfs_mount.h"
51 #include "xfs_alloc_btree.h"
52 #include "xfs_bmap_btree.h"
53 #include "xfs_ialloc_btree.h"
54 #include "xfs_alloc.h"
55 #include "xfs_btree.h"
56 #include "xfs_attr_sf.h"
57 #include "xfs_dir_sf.h"
58 #include "xfs_dir2_sf.h"
59 #include "xfs_dinode.h"
60 #include "xfs_inode_item.h"
61 #include "xfs_inode.h"
63 #include "xfs_da_btree.h"
65 #include "xfs_attr_leaf.h"
66 #include "xfs_error.h"
72 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
75 /*========================================================================
76 * Function prototypes for the kernel.
77 *========================================================================*/
80 * Routines used for growing the Btree.
82 STATIC
int xfs_attr_leaf_add_work(xfs_dabuf_t
*leaf_buffer
, xfs_da_args_t
*args
,
84 STATIC
void xfs_attr_leaf_compact(xfs_trans_t
*trans
, xfs_dabuf_t
*leaf_buffer
);
85 STATIC
void xfs_attr_leaf_rebalance(xfs_da_state_t
*state
,
86 xfs_da_state_blk_t
*blk1
,
87 xfs_da_state_blk_t
*blk2
);
88 STATIC
int xfs_attr_leaf_figure_balance(xfs_da_state_t
*state
,
89 xfs_da_state_blk_t
*leaf_blk_1
,
90 xfs_da_state_blk_t
*leaf_blk_2
,
91 int *number_entries_in_blk1
,
92 int *number_usedbytes_in_blk1
);
97 STATIC
void xfs_attr_leaf_moveents(xfs_attr_leafblock_t
*src_leaf
,
99 xfs_attr_leafblock_t
*dst_leaf
,
100 int dst_start
, int move_count
,
104 /*========================================================================
105 * External routines when dirsize < XFS_LITINO(mp).
106 *========================================================================*/
109 * Create the initial contents of a shortform attribute list.
112 xfs_attr_shortform_create(xfs_da_args_t
*args
)
114 xfs_attr_sf_hdr_t
*hdr
;
122 ASSERT(ifp
->if_bytes
== 0);
123 if (dp
->i_d
.di_aformat
== XFS_DINODE_FMT_EXTENTS
) {
124 ifp
->if_flags
&= ~XFS_IFEXTENTS
; /* just in case */
125 dp
->i_d
.di_aformat
= XFS_DINODE_FMT_LOCAL
;
126 ifp
->if_flags
|= XFS_IFINLINE
;
128 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
130 xfs_idata_realloc(dp
, sizeof(*hdr
), XFS_ATTR_FORK
);
131 hdr
= (xfs_attr_sf_hdr_t
*)ifp
->if_u1
.if_data
;
133 INT_SET(hdr
->totsize
, ARCH_CONVERT
, sizeof(*hdr
));
134 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
139 * Add a name/value pair to the shortform attribute list.
140 * Overflow from the inode has already been checked for.
143 xfs_attr_shortform_add(xfs_da_args_t
*args
)
145 xfs_attr_shortform_t
*sf
;
146 xfs_attr_sf_entry_t
*sfe
;
153 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
154 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
156 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
);
157 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
158 if (sfe
->namelen
!= args
->namelen
)
160 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
162 if (((args
->flags
& ATTR_SECURE
) != 0) !=
163 ((sfe
->flags
& XFS_ATTR_SECURE
) != 0))
165 if (((args
->flags
& ATTR_ROOT
) != 0) !=
166 ((sfe
->flags
& XFS_ATTR_ROOT
) != 0))
168 return(XFS_ERROR(EEXIST
));
171 offset
= (char *)sfe
- (char *)sf
;
172 size
= XFS_ATTR_SF_ENTSIZE_BYNAME(args
->namelen
, args
->valuelen
);
173 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
);
174 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
175 sfe
= (xfs_attr_sf_entry_t
*)((char *)sf
+ offset
);
177 sfe
->namelen
= args
->namelen
;
178 INT_SET(sfe
->valuelen
, ARCH_CONVERT
, args
->valuelen
);
179 sfe
->flags
= (args
->flags
& ATTR_SECURE
) ? XFS_ATTR_SECURE
:
180 ((args
->flags
& ATTR_ROOT
) ? XFS_ATTR_ROOT
: 0);
181 memcpy(sfe
->nameval
, args
->name
, args
->namelen
);
182 memcpy(&sfe
->nameval
[args
->namelen
], args
->value
, args
->valuelen
);
183 INT_MOD(sf
->hdr
.count
, ARCH_CONVERT
, 1);
184 INT_MOD(sf
->hdr
.totsize
, ARCH_CONVERT
, size
);
185 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
191 * Remove a name from the shortform attribute list structure.
194 xfs_attr_shortform_remove(xfs_da_args_t
*args
)
196 xfs_attr_shortform_t
*sf
;
197 xfs_attr_sf_entry_t
*sfe
;
198 int base
, size
=0, end
, totsize
, i
;
202 * Remove the attribute.
205 base
= sizeof(xfs_attr_sf_hdr_t
);
206 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
208 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
);
209 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
),
211 size
= XFS_ATTR_SF_ENTSIZE(sfe
);
212 if (sfe
->namelen
!= args
->namelen
)
214 if (memcmp(sfe
->nameval
, args
->name
, args
->namelen
) != 0)
216 if (((args
->flags
& ATTR_SECURE
) != 0) !=
217 ((sfe
->flags
& XFS_ATTR_SECURE
) != 0))
219 if (((args
->flags
& ATTR_ROOT
) != 0) !=
220 ((sfe
->flags
& XFS_ATTR_ROOT
) != 0))
224 if (i
== INT_GET(sf
->hdr
.count
, ARCH_CONVERT
))
225 return(XFS_ERROR(ENOATTR
));
228 totsize
= INT_GET(sf
->hdr
.totsize
, ARCH_CONVERT
);
229 if (end
!= totsize
) {
230 memmove(&((char *)sf
)[base
], &((char *)sf
)[end
],
233 INT_MOD(sf
->hdr
.count
, ARCH_CONVERT
, -1);
234 INT_MOD(sf
->hdr
.totsize
, ARCH_CONVERT
, -size
);
235 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
236 xfs_trans_log_inode(args
->trans
, dp
, XFS_ILOG_CORE
| XFS_ILOG_ADATA
);
242 * Look up a name in a shortform attribute list structure.
246 xfs_attr_shortform_lookup(xfs_da_args_t
*args
)
248 xfs_attr_shortform_t
*sf
;
249 xfs_attr_sf_entry_t
*sfe
;
253 ifp
= args
->dp
->i_afp
;
254 ASSERT(ifp
->if_flags
& XFS_IFINLINE
);
255 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
257 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
);
258 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
259 if (sfe
->namelen
!= args
->namelen
)
261 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
263 if (((args
->flags
& ATTR_SECURE
) != 0) !=
264 ((sfe
->flags
& XFS_ATTR_SECURE
) != 0))
266 if (((args
->flags
& ATTR_ROOT
) != 0) !=
267 ((sfe
->flags
& XFS_ATTR_ROOT
) != 0))
269 return(XFS_ERROR(EEXIST
));
271 return(XFS_ERROR(ENOATTR
));
275 * Look up a name in a shortform attribute list structure.
279 xfs_attr_shortform_getvalue(xfs_da_args_t
*args
)
281 xfs_attr_shortform_t
*sf
;
282 xfs_attr_sf_entry_t
*sfe
;
285 ASSERT(args
->dp
->i_d
.di_aformat
== XFS_IFINLINE
);
286 sf
= (xfs_attr_shortform_t
*)args
->dp
->i_afp
->if_u1
.if_data
;
288 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
);
289 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
), i
++) {
290 if (sfe
->namelen
!= args
->namelen
)
292 if (memcmp(args
->name
, sfe
->nameval
, args
->namelen
) != 0)
294 if (((args
->flags
& ATTR_SECURE
) != 0) !=
295 ((sfe
->flags
& XFS_ATTR_SECURE
) != 0))
297 if (((args
->flags
& ATTR_ROOT
) != 0) !=
298 ((sfe
->flags
& XFS_ATTR_ROOT
) != 0))
300 if (args
->flags
& ATTR_KERNOVAL
) {
301 args
->valuelen
= INT_GET(sfe
->valuelen
, ARCH_CONVERT
);
302 return(XFS_ERROR(EEXIST
));
304 if (args
->valuelen
< INT_GET(sfe
->valuelen
, ARCH_CONVERT
)) {
305 args
->valuelen
= INT_GET(sfe
->valuelen
, ARCH_CONVERT
);
306 return(XFS_ERROR(ERANGE
));
308 args
->valuelen
= INT_GET(sfe
->valuelen
, ARCH_CONVERT
);
309 memcpy(args
->value
, &sfe
->nameval
[args
->namelen
],
311 return(XFS_ERROR(EEXIST
));
313 return(XFS_ERROR(ENOATTR
));
317 * Convert from using the shortform to the leaf.
320 xfs_attr_shortform_to_leaf(xfs_da_args_t
*args
)
323 xfs_attr_shortform_t
*sf
;
324 xfs_attr_sf_entry_t
*sfe
;
334 sf
= (xfs_attr_shortform_t
*)ifp
->if_u1
.if_data
;
335 size
= INT_GET(sf
->hdr
.totsize
, ARCH_CONVERT
);
336 tmpbuffer
= kmem_alloc(size
, KM_SLEEP
);
337 ASSERT(tmpbuffer
!= NULL
);
338 memcpy(tmpbuffer
, ifp
->if_u1
.if_data
, size
);
339 sf
= (xfs_attr_shortform_t
*)tmpbuffer
;
341 xfs_idata_realloc(dp
, -size
, XFS_ATTR_FORK
);
343 error
= xfs_da_grow_inode(args
, &blkno
);
346 * If we hit an IO error middle of the transaction inside
347 * grow_inode(), we may have inconsistent data. Bail out.
351 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
352 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
357 error
= xfs_attr_leaf_create(args
, blkno
, &bp
);
359 error
= xfs_da_shrink_inode(args
, 0, bp
);
363 xfs_idata_realloc(dp
, size
, XFS_ATTR_FORK
); /* try to put */
364 memcpy(ifp
->if_u1
.if_data
, tmpbuffer
, size
); /* it back */
368 memset((char *)&nargs
, 0, sizeof(nargs
));
370 nargs
.firstblock
= args
->firstblock
;
371 nargs
.flist
= args
->flist
;
372 nargs
.total
= args
->total
;
373 nargs
.whichfork
= XFS_ATTR_FORK
;
374 nargs
.trans
= args
->trans
;
378 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
); i
++) {
379 nargs
.name
= (char *)sfe
->nameval
;
380 nargs
.namelen
= sfe
->namelen
;
381 nargs
.value
= (char *)&sfe
->nameval
[nargs
.namelen
];
382 nargs
.valuelen
= INT_GET(sfe
->valuelen
, ARCH_CONVERT
);
383 nargs
.hashval
= xfs_da_hashname((char *)sfe
->nameval
,
385 nargs
.flags
= (sfe
->flags
& XFS_ATTR_SECURE
) ? ATTR_SECURE
:
386 ((sfe
->flags
& XFS_ATTR_ROOT
) ? ATTR_ROOT
: 0);
387 error
= xfs_attr_leaf_lookup_int(bp
, &nargs
); /* set a->index */
388 ASSERT(error
== ENOATTR
);
389 error
= xfs_attr_leaf_add(bp
, &nargs
);
390 ASSERT(error
!= ENOSPC
);
393 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
400 kmem_free(tmpbuffer
, size
);
405 xfs_attr_shortform_compare(const void *a
, const void *b
)
407 xfs_attr_sf_sort_t
*sa
, *sb
;
409 sa
= (xfs_attr_sf_sort_t
*)a
;
410 sb
= (xfs_attr_sf_sort_t
*)b
;
411 if (INT_GET(sa
->hash
, ARCH_CONVERT
)
412 < INT_GET(sb
->hash
, ARCH_CONVERT
)) {
414 } else if (INT_GET(sa
->hash
, ARCH_CONVERT
)
415 > INT_GET(sb
->hash
, ARCH_CONVERT
)) {
418 return(sa
->entno
- sb
->entno
);
423 * Copy out entries of shortform attribute lists for attr_list().
424 * Shortform atrtribute lists are not stored in hashval sorted order.
425 * If the output buffer is not large enough to hold them all, then we
426 * we have to calculate each entries' hashvalue and sort them before
427 * we can begin returning them to the user.
431 xfs_attr_shortform_list(xfs_attr_list_context_t
*context
)
433 attrlist_cursor_kern_t
*cursor
;
434 xfs_attr_sf_sort_t
*sbuf
, *sbp
;
435 xfs_attr_shortform_t
*sf
;
436 xfs_attr_sf_entry_t
*sfe
;
438 int sbsize
, nsbuf
, count
, i
;
440 ASSERT(context
!= NULL
);
443 ASSERT(dp
->i_afp
!= NULL
);
444 sf
= (xfs_attr_shortform_t
*)dp
->i_afp
->if_u1
.if_data
;
448 cursor
= context
->cursor
;
449 ASSERT(cursor
!= NULL
);
451 xfs_attr_trace_l_c("sf start", context
);
454 * If the buffer is large enough, do not bother with sorting.
455 * Note the generous fudge factor of 16 overhead bytes per entry.
457 if ((dp
->i_afp
->if_bytes
+ INT_GET(sf
->hdr
.count
, ARCH_CONVERT
) * 16)
458 < context
->bufsize
) {
459 for (i
= 0, sfe
= &sf
->list
[0];
460 i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
); i
++) {
463 if (((context
->flags
& ATTR_SECURE
) != 0) !=
464 ((sfe
->flags
& XFS_ATTR_SECURE
) != 0) &&
465 !(context
->flags
& ATTR_KERNORMALS
)) {
466 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
469 if (((context
->flags
& ATTR_ROOT
) != 0) !=
470 ((sfe
->flags
& XFS_ATTR_ROOT
) != 0) &&
471 !(context
->flags
& ATTR_KERNROOTLS
)) {
472 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
475 namesp
= (sfe
->flags
& XFS_ATTR_SECURE
) ? &attr_secure
:
476 ((sfe
->flags
& XFS_ATTR_ROOT
) ? &attr_trusted
:
478 if (context
->flags
& ATTR_KERNOVAL
) {
479 ASSERT(context
->flags
& ATTR_KERNAMELS
);
480 context
->count
+= namesp
->attr_namelen
+
481 INT_GET(sfe
->namelen
, ARCH_CONVERT
) + 1;
484 if (xfs_attr_put_listent(context
, namesp
,
485 (char *)sfe
->nameval
,
487 (int)INT_GET(sfe
->valuelen
,
491 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
493 xfs_attr_trace_l_c("sf big-gulp", context
);
498 * It didn't all fit, so we have to sort everything on hashval.
500 sbsize
= INT_GET(sf
->hdr
.count
, ARCH_CONVERT
) * sizeof(*sbuf
);
501 sbp
= sbuf
= kmem_alloc(sbsize
, KM_SLEEP
);
504 * Scan the attribute list for the rest of the entries, storing
505 * the relevant info from only those that match into a buffer.
508 for (i
= 0, sfe
= &sf
->list
[0];
509 i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
); i
++) {
511 ((char *)sfe
< (char *)sf
) ||
512 ((char *)sfe
>= ((char *)sf
+ dp
->i_afp
->if_bytes
)))) {
513 XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
515 context
->dp
->i_mount
, sfe
);
516 xfs_attr_trace_l_c("sf corrupted", context
);
517 kmem_free(sbuf
, sbsize
);
518 return XFS_ERROR(EFSCORRUPTED
);
520 if (((context
->flags
& ATTR_SECURE
) != 0) !=
521 ((sfe
->flags
& XFS_ATTR_SECURE
) != 0) &&
522 !(context
->flags
& ATTR_KERNORMALS
)) {
523 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
526 if (((context
->flags
& ATTR_ROOT
) != 0) !=
527 ((sfe
->flags
& XFS_ATTR_ROOT
) != 0) &&
528 !(context
->flags
& ATTR_KERNROOTLS
)) {
529 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
533 INT_SET(sbp
->hash
, ARCH_CONVERT
,
534 xfs_da_hashname((char *)sfe
->nameval
, sfe
->namelen
));
535 sbp
->name
= (char *)sfe
->nameval
;
536 sbp
->namelen
= sfe
->namelen
;
537 /* These are bytes, and both on-disk, don't endian-flip */
538 sbp
->valuelen
= sfe
->valuelen
;
539 sbp
->flags
= sfe
->flags
;
540 sfe
= XFS_ATTR_SF_NEXTENTRY(sfe
);
546 * Sort the entries on hash then entno.
548 qsort(sbuf
, nsbuf
, sizeof(*sbuf
), xfs_attr_shortform_compare
);
551 * Re-find our place IN THE SORTED LIST.
556 for (sbp
= sbuf
, i
= 0; i
< nsbuf
; i
++, sbp
++) {
557 if (INT_GET(sbp
->hash
, ARCH_CONVERT
) == cursor
->hashval
) {
558 if (cursor
->offset
== count
) {
562 } else if (INT_GET(sbp
->hash
, ARCH_CONVERT
) > cursor
->hashval
) {
567 kmem_free(sbuf
, sbsize
);
568 xfs_attr_trace_l_c("blk end", context
);
573 * Loop putting entries into the user buffer.
575 for ( ; i
< nsbuf
; i
++, sbp
++) {
578 namesp
= (sbp
->flags
& XFS_ATTR_SECURE
) ? &attr_secure
:
579 ((sbp
->flags
& XFS_ATTR_ROOT
) ? &attr_trusted
:
582 if (cursor
->hashval
!= INT_GET(sbp
->hash
, ARCH_CONVERT
)) {
583 cursor
->hashval
= INT_GET(sbp
->hash
, ARCH_CONVERT
);
586 if (context
->flags
& ATTR_KERNOVAL
) {
587 ASSERT(context
->flags
& ATTR_KERNAMELS
);
588 context
->count
+= namesp
->attr_namelen
+
591 if (xfs_attr_put_listent(context
, namesp
,
592 sbp
->name
, sbp
->namelen
,
593 INT_GET(sbp
->valuelen
, ARCH_CONVERT
)))
599 kmem_free(sbuf
, sbsize
);
600 xfs_attr_trace_l_c("sf E-O-F", context
);
605 * Check a leaf attribute block to see if all the entries would fit into
606 * a shortform attribute list.
609 xfs_attr_shortform_allfit(xfs_dabuf_t
*bp
, xfs_inode_t
*dp
)
611 xfs_attr_leafblock_t
*leaf
;
612 xfs_attr_leaf_entry_t
*entry
;
613 xfs_attr_leaf_name_local_t
*name_loc
;
617 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
618 == XFS_ATTR_LEAF_MAGIC
);
620 entry
= &leaf
->entries
[0];
621 bytes
= sizeof(struct xfs_attr_sf_hdr
);
622 for (i
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); entry
++, i
++) {
623 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
624 continue; /* don't copy partial entries */
625 if (!(entry
->flags
& XFS_ATTR_LOCAL
))
627 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, i
);
628 if (name_loc
->namelen
>= XFS_ATTR_SF_ENTSIZE_MAX
)
630 if (INT_GET(name_loc
->valuelen
, ARCH_CONVERT
) >= XFS_ATTR_SF_ENTSIZE_MAX
)
632 bytes
+= sizeof(struct xfs_attr_sf_entry
)-1
634 + INT_GET(name_loc
->valuelen
, ARCH_CONVERT
);
636 return( bytes
< XFS_IFORK_ASIZE(dp
) );
640 * Convert a leaf attribute list to shortform attribute list
643 xfs_attr_leaf_to_shortform(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
645 xfs_attr_leafblock_t
*leaf
;
646 xfs_attr_leaf_entry_t
*entry
;
647 xfs_attr_leaf_name_local_t
*name_loc
;
654 tmpbuffer
= kmem_alloc(XFS_LBSIZE(dp
->i_mount
), KM_SLEEP
);
655 ASSERT(tmpbuffer
!= NULL
);
658 memcpy(tmpbuffer
, bp
->data
, XFS_LBSIZE(dp
->i_mount
));
659 leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
660 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
661 == XFS_ATTR_LEAF_MAGIC
);
662 memset(bp
->data
, 0, XFS_LBSIZE(dp
->i_mount
));
665 * Clean out the prior contents of the attribute list.
667 error
= xfs_da_shrink_inode(args
, 0, bp
);
670 error
= xfs_attr_shortform_create(args
);
675 * Copy the attributes
677 memset((char *)&nargs
, 0, sizeof(nargs
));
679 nargs
.firstblock
= args
->firstblock
;
680 nargs
.flist
= args
->flist
;
681 nargs
.total
= args
->total
;
682 nargs
.whichfork
= XFS_ATTR_FORK
;
683 nargs
.trans
= args
->trans
;
685 entry
= &leaf
->entries
[0];
686 for (i
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); entry
++, i
++) {
687 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
688 continue; /* don't copy partial entries */
691 ASSERT(entry
->flags
& XFS_ATTR_LOCAL
);
692 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, i
);
693 nargs
.name
= (char *)name_loc
->nameval
;
694 nargs
.namelen
= name_loc
->namelen
;
695 nargs
.value
= (char *)&name_loc
->nameval
[nargs
.namelen
];
696 nargs
.valuelen
= INT_GET(name_loc
->valuelen
, ARCH_CONVERT
);
697 nargs
.hashval
= INT_GET(entry
->hashval
, ARCH_CONVERT
);
698 nargs
.flags
= (entry
->flags
& XFS_ATTR_SECURE
) ? ATTR_SECURE
:
699 ((entry
->flags
& XFS_ATTR_ROOT
) ? ATTR_ROOT
: 0);
700 xfs_attr_shortform_add(&nargs
);
705 kmem_free(tmpbuffer
, XFS_LBSIZE(dp
->i_mount
));
710 * Convert from using a single leaf to a root node and a leaf.
713 xfs_attr_leaf_to_node(xfs_da_args_t
*args
)
715 xfs_attr_leafblock_t
*leaf
;
716 xfs_da_intnode_t
*node
;
718 xfs_dabuf_t
*bp1
, *bp2
;
724 error
= xfs_da_grow_inode(args
, &blkno
);
727 error
= xfs_da_read_buf(args
->trans
, args
->dp
, 0, -1, &bp1
,
733 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp2
,
738 memcpy(bp2
->data
, bp1
->data
, XFS_LBSIZE(dp
->i_mount
));
739 xfs_da_buf_done(bp1
);
741 xfs_da_log_buf(args
->trans
, bp2
, 0, XFS_LBSIZE(dp
->i_mount
) - 1);
744 * Set up the new root node.
746 error
= xfs_da_node_create(args
, 0, 1, &bp1
, XFS_ATTR_FORK
);
751 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
752 == XFS_ATTR_LEAF_MAGIC
);
753 /* both on-disk, don't endian-flip twice */
754 node
->btree
[0].hashval
=
755 leaf
->entries
[INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)-1 ].hashval
;
756 INT_SET(node
->btree
[0].before
, ARCH_CONVERT
, blkno
);
757 INT_SET(node
->hdr
.count
, ARCH_CONVERT
, 1);
758 xfs_da_log_buf(args
->trans
, bp1
, 0, XFS_LBSIZE(dp
->i_mount
) - 1);
762 xfs_da_buf_done(bp1
);
764 xfs_da_buf_done(bp2
);
769 /*========================================================================
770 * Routines used for growing the Btree.
771 *========================================================================*/
774 * Create the initial contents of a leaf attribute list
775 * or a leaf in a node attribute list.
778 xfs_attr_leaf_create(xfs_da_args_t
*args
, xfs_dablk_t blkno
, xfs_dabuf_t
**bpp
)
780 xfs_attr_leafblock_t
*leaf
;
781 xfs_attr_leaf_hdr_t
*hdr
;
788 error
= xfs_da_get_buf(args
->trans
, args
->dp
, blkno
, -1, &bp
,
794 memset((char *)leaf
, 0, XFS_LBSIZE(dp
->i_mount
));
796 INT_SET(hdr
->info
.magic
, ARCH_CONVERT
, XFS_ATTR_LEAF_MAGIC
);
797 INT_SET(hdr
->firstused
, ARCH_CONVERT
, XFS_LBSIZE(dp
->i_mount
));
798 if (!hdr
->firstused
) {
799 INT_SET(hdr
->firstused
, ARCH_CONVERT
,
800 XFS_LBSIZE(dp
->i_mount
) - XFS_ATTR_LEAF_NAME_ALIGN
);
803 INT_SET(hdr
->freemap
[0].base
, ARCH_CONVERT
,
804 sizeof(xfs_attr_leaf_hdr_t
));
805 INT_SET(hdr
->freemap
[0].size
, ARCH_CONVERT
,
806 INT_GET(hdr
->firstused
, ARCH_CONVERT
)
807 - INT_GET(hdr
->freemap
[0].base
,
810 xfs_da_log_buf(args
->trans
, bp
, 0, XFS_LBSIZE(dp
->i_mount
) - 1);
817 * Split the leaf node, rebalance, then add the new entry.
820 xfs_attr_leaf_split(xfs_da_state_t
*state
, xfs_da_state_blk_t
*oldblk
,
821 xfs_da_state_blk_t
*newblk
)
827 * Allocate space for a new leaf node.
829 ASSERT(oldblk
->magic
== XFS_ATTR_LEAF_MAGIC
);
830 error
= xfs_da_grow_inode(state
->args
, &blkno
);
833 error
= xfs_attr_leaf_create(state
->args
, blkno
, &newblk
->bp
);
836 newblk
->blkno
= blkno
;
837 newblk
->magic
= XFS_ATTR_LEAF_MAGIC
;
840 * Rebalance the entries across the two leaves.
841 * NOTE: rebalance() currently depends on the 2nd block being empty.
843 xfs_attr_leaf_rebalance(state
, oldblk
, newblk
);
844 error
= xfs_da_blk_link(state
, oldblk
, newblk
);
849 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
850 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
851 * "new" attrs info. Will need the "old" info to remove it later.
853 * Insert the "new" entry in the correct block.
856 error
= xfs_attr_leaf_add(oldblk
->bp
, state
->args
);
858 error
= xfs_attr_leaf_add(newblk
->bp
, state
->args
);
861 * Update last hashval in each block since we added the name.
863 oldblk
->hashval
= xfs_attr_leaf_lasthash(oldblk
->bp
, NULL
);
864 newblk
->hashval
= xfs_attr_leaf_lasthash(newblk
->bp
, NULL
);
869 * Add a name to the leaf attribute list structure.
872 xfs_attr_leaf_add(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
874 xfs_attr_leafblock_t
*leaf
;
875 xfs_attr_leaf_hdr_t
*hdr
;
876 xfs_attr_leaf_map_t
*map
;
877 int tablesize
, entsize
, sum
, tmp
, i
;
880 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
881 == XFS_ATTR_LEAF_MAGIC
);
882 ASSERT((args
->index
>= 0)
883 && (args
->index
<= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)));
885 entsize
= xfs_attr_leaf_newentsize(args
,
886 args
->trans
->t_mountp
->m_sb
.sb_blocksize
, NULL
);
889 * Search through freemap for first-fit on new name length.
890 * (may need to figure in size of entry struct too)
892 tablesize
= (INT_GET(hdr
->count
, ARCH_CONVERT
) + 1)
893 * sizeof(xfs_attr_leaf_entry_t
)
894 + sizeof(xfs_attr_leaf_hdr_t
);
895 map
= &hdr
->freemap
[XFS_ATTR_LEAF_MAPSIZE
-1];
896 for (sum
= 0, i
= XFS_ATTR_LEAF_MAPSIZE
-1; i
>= 0; map
--, i
--) {
897 if (tablesize
> INT_GET(hdr
->firstused
, ARCH_CONVERT
)) {
898 sum
+= INT_GET(map
->size
, ARCH_CONVERT
);
902 continue; /* no space in this map */
904 if (INT_GET(map
->base
, ARCH_CONVERT
)
905 < INT_GET(hdr
->firstused
, ARCH_CONVERT
))
906 tmp
+= sizeof(xfs_attr_leaf_entry_t
);
907 if (INT_GET(map
->size
, ARCH_CONVERT
) >= tmp
) {
908 tmp
= xfs_attr_leaf_add_work(bp
, args
, i
);
911 sum
+= INT_GET(map
->size
, ARCH_CONVERT
);
915 * If there are no holes in the address space of the block,
916 * and we don't have enough freespace, then compaction will do us
917 * no good and we should just give up.
919 if (!hdr
->holes
&& (sum
< entsize
))
920 return(XFS_ERROR(ENOSPC
));
923 * Compact the entries to coalesce free space.
924 * This may change the hdr->count via dropping INCOMPLETE entries.
926 xfs_attr_leaf_compact(args
->trans
, bp
);
929 * After compaction, the block is guaranteed to have only one
930 * free region, in freemap[0]. If it is not big enough, give up.
932 if (INT_GET(hdr
->freemap
[0].size
, ARCH_CONVERT
)
933 < (entsize
+ sizeof(xfs_attr_leaf_entry_t
)))
934 return(XFS_ERROR(ENOSPC
));
936 return(xfs_attr_leaf_add_work(bp
, args
, 0));
940 * Add a name to a leaf attribute list structure.
943 xfs_attr_leaf_add_work(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
, int mapindex
)
945 xfs_attr_leafblock_t
*leaf
;
946 xfs_attr_leaf_hdr_t
*hdr
;
947 xfs_attr_leaf_entry_t
*entry
;
948 xfs_attr_leaf_name_local_t
*name_loc
;
949 xfs_attr_leaf_name_remote_t
*name_rmt
;
950 xfs_attr_leaf_map_t
*map
;
955 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
956 == XFS_ATTR_LEAF_MAGIC
);
958 ASSERT((mapindex
>= 0) && (mapindex
< XFS_ATTR_LEAF_MAPSIZE
));
959 ASSERT((args
->index
>= 0)
960 && (args
->index
<= INT_GET(hdr
->count
, ARCH_CONVERT
)));
963 * Force open some space in the entry array and fill it in.
965 entry
= &leaf
->entries
[args
->index
];
966 if (args
->index
< INT_GET(hdr
->count
, ARCH_CONVERT
)) {
967 tmp
= INT_GET(hdr
->count
, ARCH_CONVERT
) - args
->index
;
968 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
969 memmove((char *)(entry
+1), (char *)entry
, tmp
);
970 xfs_da_log_buf(args
->trans
, bp
,
971 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
973 INT_MOD(hdr
->count
, ARCH_CONVERT
, 1);
976 * Allocate space for the new string (at the end of the run).
978 map
= &hdr
->freemap
[mapindex
];
979 mp
= args
->trans
->t_mountp
;
980 ASSERT(INT_GET(map
->base
, ARCH_CONVERT
) < XFS_LBSIZE(mp
));
981 ASSERT((INT_GET(map
->base
, ARCH_CONVERT
) & 0x3) == 0);
982 ASSERT(INT_GET(map
->size
, ARCH_CONVERT
)
983 >= xfs_attr_leaf_newentsize(args
,
984 mp
->m_sb
.sb_blocksize
, NULL
));
985 ASSERT(INT_GET(map
->size
, ARCH_CONVERT
) < XFS_LBSIZE(mp
));
986 ASSERT((INT_GET(map
->size
, ARCH_CONVERT
) & 0x3) == 0);
987 INT_MOD(map
->size
, ARCH_CONVERT
,
988 -xfs_attr_leaf_newentsize(args
, mp
->m_sb
.sb_blocksize
, &tmp
));
989 INT_SET(entry
->nameidx
, ARCH_CONVERT
,
990 INT_GET(map
->base
, ARCH_CONVERT
)
991 + INT_GET(map
->size
, ARCH_CONVERT
));
992 INT_SET(entry
->hashval
, ARCH_CONVERT
, args
->hashval
);
993 entry
->flags
= tmp
? XFS_ATTR_LOCAL
: 0;
994 entry
->flags
|= (args
->flags
& ATTR_SECURE
) ? XFS_ATTR_SECURE
:
995 ((args
->flags
& ATTR_ROOT
) ? XFS_ATTR_ROOT
: 0);
997 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
998 if ((args
->blkno2
== args
->blkno
) &&
999 (args
->index2
<= args
->index
)) {
1003 xfs_da_log_buf(args
->trans
, bp
,
1004 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
1005 ASSERT((args
->index
== 0) || (INT_GET(entry
->hashval
, ARCH_CONVERT
)
1006 >= INT_GET((entry
-1)->hashval
,
1008 ASSERT((args
->index
== INT_GET(hdr
->count
, ARCH_CONVERT
)-1) ||
1009 (INT_GET(entry
->hashval
, ARCH_CONVERT
)
1010 <= (INT_GET((entry
+1)->hashval
, ARCH_CONVERT
))));
1013 * Copy the attribute name and value into the new space.
1015 * For "remote" attribute values, simply note that we need to
1016 * allocate space for the "remote" value. We can't actually
1017 * allocate the extents in this transaction, and we can't decide
1018 * which blocks they should be as we might allocate more blocks
1019 * as part of this transaction (a split operation for example).
1021 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1022 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, args
->index
);
1023 name_loc
->namelen
= args
->namelen
;
1024 INT_SET(name_loc
->valuelen
, ARCH_CONVERT
, args
->valuelen
);
1025 memcpy((char *)name_loc
->nameval
, args
->name
, args
->namelen
);
1026 memcpy((char *)&name_loc
->nameval
[args
->namelen
], args
->value
,
1027 INT_GET(name_loc
->valuelen
, ARCH_CONVERT
));
1029 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
1030 name_rmt
->namelen
= args
->namelen
;
1031 memcpy((char *)name_rmt
->name
, args
->name
, args
->namelen
);
1032 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
1034 name_rmt
->valuelen
= 0;
1035 name_rmt
->valueblk
= 0;
1037 args
->rmtblkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
1039 xfs_da_log_buf(args
->trans
, bp
,
1040 XFS_DA_LOGRANGE(leaf
, XFS_ATTR_LEAF_NAME(leaf
, args
->index
),
1041 xfs_attr_leaf_entsize(leaf
, args
->index
)));
1044 * Update the control info for this leaf node
1046 if (INT_GET(entry
->nameidx
, ARCH_CONVERT
)
1047 < INT_GET(hdr
->firstused
, ARCH_CONVERT
)) {
1048 /* both on-disk, don't endian-flip twice */
1049 hdr
->firstused
= entry
->nameidx
;
1051 ASSERT(INT_GET(hdr
->firstused
, ARCH_CONVERT
)
1052 >= ((INT_GET(hdr
->count
, ARCH_CONVERT
)
1053 * sizeof(*entry
))+sizeof(*hdr
)));
1054 tmp
= (INT_GET(hdr
->count
, ARCH_CONVERT
)-1)
1055 * sizeof(xfs_attr_leaf_entry_t
)
1056 + sizeof(xfs_attr_leaf_hdr_t
);
1057 map
= &hdr
->freemap
[0];
1058 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; map
++, i
++) {
1059 if (INT_GET(map
->base
, ARCH_CONVERT
) == tmp
) {
1060 INT_MOD(map
->base
, ARCH_CONVERT
,
1061 sizeof(xfs_attr_leaf_entry_t
));
1062 INT_MOD(map
->size
, ARCH_CONVERT
,
1063 -sizeof(xfs_attr_leaf_entry_t
));
1066 INT_MOD(hdr
->usedbytes
, ARCH_CONVERT
,
1067 xfs_attr_leaf_entsize(leaf
, args
->index
));
1068 xfs_da_log_buf(args
->trans
, bp
,
1069 XFS_DA_LOGRANGE(leaf
, hdr
, sizeof(*hdr
)));
1074 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1077 xfs_attr_leaf_compact(xfs_trans_t
*trans
, xfs_dabuf_t
*bp
)
1079 xfs_attr_leafblock_t
*leaf_s
, *leaf_d
;
1080 xfs_attr_leaf_hdr_t
*hdr_s
, *hdr_d
;
1084 mp
= trans
->t_mountp
;
1085 tmpbuffer
= kmem_alloc(XFS_LBSIZE(mp
), KM_SLEEP
);
1086 ASSERT(tmpbuffer
!= NULL
);
1087 memcpy(tmpbuffer
, bp
->data
, XFS_LBSIZE(mp
));
1088 memset(bp
->data
, 0, XFS_LBSIZE(mp
));
1091 * Copy basic information
1093 leaf_s
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1095 hdr_s
= &leaf_s
->hdr
;
1096 hdr_d
= &leaf_d
->hdr
;
1097 hdr_d
->info
= hdr_s
->info
; /* struct copy */
1098 INT_SET(hdr_d
->firstused
, ARCH_CONVERT
, XFS_LBSIZE(mp
));
1099 /* handle truncation gracefully */
1100 if (!hdr_d
->firstused
) {
1101 INT_SET(hdr_d
->firstused
, ARCH_CONVERT
,
1102 XFS_LBSIZE(mp
) - XFS_ATTR_LEAF_NAME_ALIGN
);
1104 hdr_d
->usedbytes
= 0;
1107 INT_SET(hdr_d
->freemap
[0].base
, ARCH_CONVERT
,
1108 sizeof(xfs_attr_leaf_hdr_t
));
1109 INT_SET(hdr_d
->freemap
[0].size
, ARCH_CONVERT
,
1110 INT_GET(hdr_d
->firstused
, ARCH_CONVERT
)
1111 - INT_GET(hdr_d
->freemap
[0].base
, ARCH_CONVERT
));
1114 * Copy all entry's in the same (sorted) order,
1115 * but allocate name/value pairs packed and in sequence.
1117 xfs_attr_leaf_moveents(leaf_s
, 0, leaf_d
, 0,
1118 (int)INT_GET(hdr_s
->count
, ARCH_CONVERT
), mp
);
1120 xfs_da_log_buf(trans
, bp
, 0, XFS_LBSIZE(mp
) - 1);
1122 kmem_free(tmpbuffer
, XFS_LBSIZE(mp
));
1126 * Redistribute the attribute list entries between two leaf nodes,
1127 * taking into account the size of the new entry.
1129 * NOTE: if new block is empty, then it will get the upper half of the
1130 * old block. At present, all (one) callers pass in an empty second block.
1132 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1133 * to match what it is doing in splitting the attribute leaf block. Those
1134 * values are used in "atomic rename" operations on attributes. Note that
1135 * the "new" and "old" values can end up in different blocks.
1138 xfs_attr_leaf_rebalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*blk1
,
1139 xfs_da_state_blk_t
*blk2
)
1141 xfs_da_args_t
*args
;
1142 xfs_da_state_blk_t
*tmp_blk
;
1143 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
1144 xfs_attr_leaf_hdr_t
*hdr1
, *hdr2
;
1145 int count
, totallen
, max
, space
, swap
;
1148 * Set up environment.
1150 ASSERT(blk1
->magic
== XFS_ATTR_LEAF_MAGIC
);
1151 ASSERT(blk2
->magic
== XFS_ATTR_LEAF_MAGIC
);
1152 leaf1
= blk1
->bp
->data
;
1153 leaf2
= blk2
->bp
->data
;
1154 ASSERT(INT_GET(leaf1
->hdr
.info
.magic
, ARCH_CONVERT
)
1155 == XFS_ATTR_LEAF_MAGIC
);
1156 ASSERT(INT_GET(leaf2
->hdr
.info
.magic
, ARCH_CONVERT
)
1157 == XFS_ATTR_LEAF_MAGIC
);
1161 * Check ordering of blocks, reverse if it makes things simpler.
1163 * NOTE: Given that all (current) callers pass in an empty
1164 * second block, this code should never set "swap".
1167 if (xfs_attr_leaf_order(blk1
->bp
, blk2
->bp
)) {
1171 leaf1
= blk1
->bp
->data
;
1172 leaf2
= blk2
->bp
->data
;
1179 * Examine entries until we reduce the absolute difference in
1180 * byte usage between the two blocks to a minimum. Then get
1181 * the direction to copy and the number of elements to move.
1183 * "inleaf" is true if the new entry should be inserted into blk1.
1184 * If "swap" is also true, then reverse the sense of "inleaf".
1186 state
->inleaf
= xfs_attr_leaf_figure_balance(state
, blk1
, blk2
,
1189 state
->inleaf
= !state
->inleaf
;
1192 * Move any entries required from leaf to leaf:
1194 if (count
< INT_GET(hdr1
->count
, ARCH_CONVERT
)) {
1196 * Figure the total bytes to be added to the destination leaf.
1198 /* number entries being moved */
1199 count
= INT_GET(hdr1
->count
, ARCH_CONVERT
) - count
;
1200 space
= INT_GET(hdr1
->usedbytes
, ARCH_CONVERT
) - totallen
;
1201 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1204 * leaf2 is the destination, compact it if it looks tight.
1206 max
= INT_GET(hdr2
->firstused
, ARCH_CONVERT
)
1207 - sizeof(xfs_attr_leaf_hdr_t
);
1208 max
-= INT_GET(hdr2
->count
, ARCH_CONVERT
)
1209 * sizeof(xfs_attr_leaf_entry_t
);
1211 xfs_attr_leaf_compact(args
->trans
, blk2
->bp
);
1215 * Move high entries from leaf1 to low end of leaf2.
1217 xfs_attr_leaf_moveents(leaf1
,
1218 INT_GET(hdr1
->count
, ARCH_CONVERT
)-count
,
1219 leaf2
, 0, count
, state
->mp
);
1221 xfs_da_log_buf(args
->trans
, blk1
->bp
, 0, state
->blocksize
-1);
1222 xfs_da_log_buf(args
->trans
, blk2
->bp
, 0, state
->blocksize
-1);
1223 } else if (count
> INT_GET(hdr1
->count
, ARCH_CONVERT
)) {
1225 * I assert that since all callers pass in an empty
1226 * second buffer, this code should never execute.
1230 * Figure the total bytes to be added to the destination leaf.
1232 /* number entries being moved */
1233 count
-= INT_GET(hdr1
->count
, ARCH_CONVERT
);
1234 space
= totallen
- INT_GET(hdr1
->usedbytes
, ARCH_CONVERT
);
1235 space
+= count
* sizeof(xfs_attr_leaf_entry_t
);
1238 * leaf1 is the destination, compact it if it looks tight.
1240 max
= INT_GET(hdr1
->firstused
, ARCH_CONVERT
)
1241 - sizeof(xfs_attr_leaf_hdr_t
);
1242 max
-= INT_GET(hdr1
->count
, ARCH_CONVERT
)
1243 * sizeof(xfs_attr_leaf_entry_t
);
1245 xfs_attr_leaf_compact(args
->trans
, blk1
->bp
);
1249 * Move low entries from leaf2 to high end of leaf1.
1251 xfs_attr_leaf_moveents(leaf2
, 0, leaf1
,
1252 (int)INT_GET(hdr1
->count
, ARCH_CONVERT
), count
,
1255 xfs_da_log_buf(args
->trans
, blk1
->bp
, 0, state
->blocksize
-1);
1256 xfs_da_log_buf(args
->trans
, blk2
->bp
, 0, state
->blocksize
-1);
1260 * Copy out last hashval in each block for B-tree code.
1263 INT_GET(leaf1
->entries
[INT_GET(leaf1
->hdr
.count
,
1264 ARCH_CONVERT
)-1].hashval
, ARCH_CONVERT
);
1266 INT_GET(leaf2
->entries
[INT_GET(leaf2
->hdr
.count
,
1267 ARCH_CONVERT
)-1].hashval
, ARCH_CONVERT
);
1270 * Adjust the expected index for insertion.
1271 * NOTE: this code depends on the (current) situation that the
1272 * second block was originally empty.
1274 * If the insertion point moved to the 2nd block, we must adjust
1275 * the index. We must also track the entry just following the
1276 * new entry for use in an "atomic rename" operation, that entry
1277 * is always the "old" entry and the "new" entry is what we are
1278 * inserting. The index/blkno fields refer to the "old" entry,
1279 * while the index2/blkno2 fields refer to the "new" entry.
1281 if (blk1
->index
> INT_GET(leaf1
->hdr
.count
, ARCH_CONVERT
)) {
1282 ASSERT(state
->inleaf
== 0);
1283 blk2
->index
= blk1
->index
1284 - INT_GET(leaf1
->hdr
.count
, ARCH_CONVERT
);
1285 args
->index
= args
->index2
= blk2
->index
;
1286 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1287 } else if (blk1
->index
== INT_GET(leaf1
->hdr
.count
, ARCH_CONVERT
)) {
1288 if (state
->inleaf
) {
1289 args
->index
= blk1
->index
;
1290 args
->blkno
= blk1
->blkno
;
1292 args
->blkno2
= blk2
->blkno
;
1294 blk2
->index
= blk1
->index
1295 - INT_GET(leaf1
->hdr
.count
, ARCH_CONVERT
);
1296 args
->index
= args
->index2
= blk2
->index
;
1297 args
->blkno
= args
->blkno2
= blk2
->blkno
;
1300 ASSERT(state
->inleaf
== 1);
1301 args
->index
= args
->index2
= blk1
->index
;
1302 args
->blkno
= args
->blkno2
= blk1
->blkno
;
1307 * Examine entries until we reduce the absolute difference in
1308 * byte usage between the two blocks to a minimum.
1309 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1310 * GROT: there will always be enough room in either block for a new entry.
1311 * GROT: Do a double-split for this case?
1314 xfs_attr_leaf_figure_balance(xfs_da_state_t
*state
,
1315 xfs_da_state_blk_t
*blk1
,
1316 xfs_da_state_blk_t
*blk2
,
1317 int *countarg
, int *usedbytesarg
)
1319 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
1320 xfs_attr_leaf_hdr_t
*hdr1
, *hdr2
;
1321 xfs_attr_leaf_entry_t
*entry
;
1322 int count
, max
, index
, totallen
, half
;
1323 int lastdelta
, foundit
, tmp
;
1326 * Set up environment.
1328 leaf1
= blk1
->bp
->data
;
1329 leaf2
= blk2
->bp
->data
;
1336 * Examine entries until we reduce the absolute difference in
1337 * byte usage between the two blocks to a minimum.
1339 max
= INT_GET(hdr1
->count
, ARCH_CONVERT
)
1340 + INT_GET(hdr2
->count
, ARCH_CONVERT
);
1341 half
= (max
+1) * sizeof(*entry
);
1342 half
+= INT_GET(hdr1
->usedbytes
, ARCH_CONVERT
)
1343 + INT_GET(hdr2
->usedbytes
, ARCH_CONVERT
)
1344 + xfs_attr_leaf_newentsize(state
->args
,
1345 state
->blocksize
, NULL
);
1347 lastdelta
= state
->blocksize
;
1348 entry
= &leaf1
->entries
[0];
1349 for (count
= index
= 0; count
< max
; entry
++, index
++, count
++) {
1351 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1353 * The new entry is in the first block, account for it.
1355 if (count
== blk1
->index
) {
1356 tmp
= totallen
+ sizeof(*entry
) +
1357 xfs_attr_leaf_newentsize(state
->args
,
1360 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1362 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1368 * Wrap around into the second block if necessary.
1370 if (count
== INT_GET(hdr1
->count
, ARCH_CONVERT
)) {
1372 entry
= &leaf1
->entries
[0];
1377 * Figure out if next leaf entry would be too much.
1379 tmp
= totallen
+ sizeof(*entry
) + xfs_attr_leaf_entsize(leaf1
,
1381 if (XFS_ATTR_ABS(half
- tmp
) > lastdelta
)
1383 lastdelta
= XFS_ATTR_ABS(half
- tmp
);
1389 * Calculate the number of usedbytes that will end up in lower block.
1390 * If new entry not in lower block, fix up the count.
1392 totallen
-= count
* sizeof(*entry
);
1394 totallen
-= sizeof(*entry
) +
1395 xfs_attr_leaf_newentsize(state
->args
,
1401 *usedbytesarg
= totallen
;
1405 /*========================================================================
1406 * Routines used for shrinking the Btree.
1407 *========================================================================*/
1410 * Check a leaf block and its neighbors to see if the block should be
1411 * collapsed into one or the other neighbor. Always keep the block
1412 * with the smaller block number.
1413 * If the current block is over 50% full, don't try to join it, return 0.
1414 * If the block is empty, fill in the state structure and return 2.
1415 * If it can be collapsed, fill in the state structure and return 1.
1416 * If nothing can be done, return 0.
1418 * GROT: allow for INCOMPLETE entries in calculation.
1421 xfs_attr_leaf_toosmall(xfs_da_state_t
*state
, int *action
)
1423 xfs_attr_leafblock_t
*leaf
;
1424 xfs_da_state_blk_t
*blk
;
1425 xfs_da_blkinfo_t
*info
;
1426 int count
, bytes
, forward
, error
, retval
, i
;
1431 * Check for the degenerate case of the block being over 50% full.
1432 * If so, it's not worth even looking to see if we might be able
1433 * to coalesce with a sibling.
1435 blk
= &state
->path
.blk
[ state
->path
.active
-1 ];
1436 info
= blk
->bp
->data
;
1437 ASSERT(INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
);
1438 leaf
= (xfs_attr_leafblock_t
*)info
;
1439 count
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
1440 bytes
= sizeof(xfs_attr_leaf_hdr_t
) +
1441 count
* sizeof(xfs_attr_leaf_entry_t
) +
1442 INT_GET(leaf
->hdr
.usedbytes
, ARCH_CONVERT
);
1443 if (bytes
> (state
->blocksize
>> 1)) {
1444 *action
= 0; /* blk over 50%, don't try to join */
1449 * Check for the degenerate case of the block being empty.
1450 * If the block is empty, we'll simply delete it, no need to
1451 * coalesce it with a sibling block. We choose (aribtrarily)
1452 * to merge with the forward block unless it is NULL.
1456 * Make altpath point to the block we want to keep and
1457 * path point to the block we want to drop (this one).
1459 forward
= info
->forw
;
1460 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1461 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
1474 * Examine each sibling block to see if we can coalesce with
1475 * at least 25% free space to spare. We need to figure out
1476 * whether to merge with the forward or the backward block.
1477 * We prefer coalescing with the lower numbered sibling so as
1478 * to shrink an attribute list over time.
1480 /* start with smaller blk num */
1481 forward
= (INT_GET(info
->forw
, ARCH_CONVERT
)
1482 < INT_GET(info
->back
, ARCH_CONVERT
));
1483 for (i
= 0; i
< 2; forward
= !forward
, i
++) {
1485 blkno
= INT_GET(info
->forw
, ARCH_CONVERT
);
1487 blkno
= INT_GET(info
->back
, ARCH_CONVERT
);
1490 error
= xfs_da_read_buf(state
->args
->trans
, state
->args
->dp
,
1491 blkno
, -1, &bp
, XFS_ATTR_FORK
);
1496 leaf
= (xfs_attr_leafblock_t
*)info
;
1497 count
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
1498 bytes
= state
->blocksize
- (state
->blocksize
>>2);
1499 bytes
-= INT_GET(leaf
->hdr
.usedbytes
, ARCH_CONVERT
);
1501 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
1502 == XFS_ATTR_LEAF_MAGIC
);
1503 count
+= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
1504 bytes
-= INT_GET(leaf
->hdr
.usedbytes
, ARCH_CONVERT
);
1505 bytes
-= count
* sizeof(xfs_attr_leaf_entry_t
);
1506 bytes
-= sizeof(xfs_attr_leaf_hdr_t
);
1507 xfs_da_brelse(state
->args
->trans
, bp
);
1509 break; /* fits with at least 25% to spare */
1517 * Make altpath point to the block we want to keep (the lower
1518 * numbered block) and path point to the block we want to drop.
1520 memcpy(&state
->altpath
, &state
->path
, sizeof(state
->path
));
1521 if (blkno
< blk
->blkno
) {
1522 error
= xfs_da_path_shift(state
, &state
->altpath
, forward
,
1525 error
= xfs_da_path_shift(state
, &state
->path
, forward
,
1539 * Remove a name from the leaf attribute list structure.
1541 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1542 * If two leaves are 37% full, when combined they will leave 25% free.
1545 xfs_attr_leaf_remove(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
1547 xfs_attr_leafblock_t
*leaf
;
1548 xfs_attr_leaf_hdr_t
*hdr
;
1549 xfs_attr_leaf_map_t
*map
;
1550 xfs_attr_leaf_entry_t
*entry
;
1551 int before
, after
, smallest
, entsize
;
1552 int tablesize
, tmp
, i
;
1556 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
1557 == XFS_ATTR_LEAF_MAGIC
);
1559 mp
= args
->trans
->t_mountp
;
1560 ASSERT((INT_GET(hdr
->count
, ARCH_CONVERT
) > 0)
1561 && (INT_GET(hdr
->count
, ARCH_CONVERT
) < (XFS_LBSIZE(mp
)/8)));
1562 ASSERT((args
->index
>= 0)
1563 && (args
->index
< INT_GET(hdr
->count
, ARCH_CONVERT
)));
1564 ASSERT(INT_GET(hdr
->firstused
, ARCH_CONVERT
)
1565 >= ((INT_GET(hdr
->count
, ARCH_CONVERT
)
1566 * sizeof(*entry
))+sizeof(*hdr
)));
1567 entry
= &leaf
->entries
[args
->index
];
1568 ASSERT(INT_GET(entry
->nameidx
, ARCH_CONVERT
)
1569 >= INT_GET(hdr
->firstused
, ARCH_CONVERT
));
1570 ASSERT(INT_GET(entry
->nameidx
, ARCH_CONVERT
) < XFS_LBSIZE(mp
));
1573 * Scan through free region table:
1574 * check for adjacency of free'd entry with an existing one,
1575 * find smallest free region in case we need to replace it,
1576 * adjust any map that borders the entry table,
1578 tablesize
= INT_GET(hdr
->count
, ARCH_CONVERT
)
1579 * sizeof(xfs_attr_leaf_entry_t
)
1580 + sizeof(xfs_attr_leaf_hdr_t
);
1581 map
= &hdr
->freemap
[0];
1582 tmp
= INT_GET(map
->size
, ARCH_CONVERT
);
1583 before
= after
= -1;
1584 smallest
= XFS_ATTR_LEAF_MAPSIZE
- 1;
1585 entsize
= xfs_attr_leaf_entsize(leaf
, args
->index
);
1586 for (i
= 0; i
< XFS_ATTR_LEAF_MAPSIZE
; map
++, i
++) {
1587 ASSERT(INT_GET(map
->base
, ARCH_CONVERT
) < XFS_LBSIZE(mp
));
1588 ASSERT(INT_GET(map
->size
, ARCH_CONVERT
) < XFS_LBSIZE(mp
));
1589 if (INT_GET(map
->base
, ARCH_CONVERT
) == tablesize
) {
1590 INT_MOD(map
->base
, ARCH_CONVERT
,
1591 -sizeof(xfs_attr_leaf_entry_t
));
1592 INT_MOD(map
->size
, ARCH_CONVERT
,
1593 sizeof(xfs_attr_leaf_entry_t
));
1596 if ((INT_GET(map
->base
, ARCH_CONVERT
)
1597 + INT_GET(map
->size
, ARCH_CONVERT
))
1598 == INT_GET(entry
->nameidx
, ARCH_CONVERT
)) {
1600 } else if (INT_GET(map
->base
, ARCH_CONVERT
)
1601 == (INT_GET(entry
->nameidx
, ARCH_CONVERT
) + entsize
)) {
1603 } else if (INT_GET(map
->size
, ARCH_CONVERT
) < tmp
) {
1604 tmp
= INT_GET(map
->size
, ARCH_CONVERT
);
1610 * Coalesce adjacent freemap regions,
1611 * or replace the smallest region.
1613 if ((before
>= 0) || (after
>= 0)) {
1614 if ((before
>= 0) && (after
>= 0)) {
1615 map
= &hdr
->freemap
[before
];
1616 INT_MOD(map
->size
, ARCH_CONVERT
, entsize
);
1617 INT_MOD(map
->size
, ARCH_CONVERT
,
1618 INT_GET(hdr
->freemap
[after
].size
,
1620 hdr
->freemap
[after
].base
= 0;
1621 hdr
->freemap
[after
].size
= 0;
1622 } else if (before
>= 0) {
1623 map
= &hdr
->freemap
[before
];
1624 INT_MOD(map
->size
, ARCH_CONVERT
, entsize
);
1626 map
= &hdr
->freemap
[after
];
1627 /* both on-disk, don't endian flip twice */
1628 map
->base
= entry
->nameidx
;
1629 INT_MOD(map
->size
, ARCH_CONVERT
, entsize
);
1633 * Replace smallest region (if it is smaller than free'd entry)
1635 map
= &hdr
->freemap
[smallest
];
1636 if (INT_GET(map
->size
, ARCH_CONVERT
) < entsize
) {
1637 INT_SET(map
->base
, ARCH_CONVERT
,
1638 INT_GET(entry
->nameidx
, ARCH_CONVERT
));
1639 INT_SET(map
->size
, ARCH_CONVERT
, entsize
);
1644 * Did we remove the first entry?
1646 if (INT_GET(entry
->nameidx
, ARCH_CONVERT
)
1647 == INT_GET(hdr
->firstused
, ARCH_CONVERT
))
1653 * Compress the remaining entries and zero out the removed stuff.
1655 memset(XFS_ATTR_LEAF_NAME(leaf
, args
->index
), 0, entsize
);
1656 INT_MOD(hdr
->usedbytes
, ARCH_CONVERT
, -entsize
);
1657 xfs_da_log_buf(args
->trans
, bp
,
1658 XFS_DA_LOGRANGE(leaf
, XFS_ATTR_LEAF_NAME(leaf
, args
->index
),
1661 tmp
= (INT_GET(hdr
->count
, ARCH_CONVERT
) - args
->index
)
1662 * sizeof(xfs_attr_leaf_entry_t
);
1663 memmove((char *)entry
, (char *)(entry
+1), tmp
);
1664 INT_MOD(hdr
->count
, ARCH_CONVERT
, -1);
1665 xfs_da_log_buf(args
->trans
, bp
,
1666 XFS_DA_LOGRANGE(leaf
, entry
, tmp
+ sizeof(*entry
)));
1667 entry
= &leaf
->entries
[INT_GET(hdr
->count
, ARCH_CONVERT
)];
1668 memset((char *)entry
, 0, sizeof(xfs_attr_leaf_entry_t
));
1671 * If we removed the first entry, re-find the first used byte
1672 * in the name area. Note that if the entry was the "firstused",
1673 * then we don't have a "hole" in our block resulting from
1674 * removing the name.
1677 tmp
= XFS_LBSIZE(mp
);
1678 entry
= &leaf
->entries
[0];
1679 for (i
= INT_GET(hdr
->count
, ARCH_CONVERT
)-1;
1680 i
>= 0; entry
++, i
--) {
1681 ASSERT(INT_GET(entry
->nameidx
, ARCH_CONVERT
)
1682 >= INT_GET(hdr
->firstused
, ARCH_CONVERT
));
1683 ASSERT(INT_GET(entry
->nameidx
, ARCH_CONVERT
)
1685 if (INT_GET(entry
->nameidx
, ARCH_CONVERT
) < tmp
)
1686 tmp
= INT_GET(entry
->nameidx
, ARCH_CONVERT
);
1688 INT_SET(hdr
->firstused
, ARCH_CONVERT
, tmp
);
1689 if (!hdr
->firstused
) {
1690 INT_SET(hdr
->firstused
, ARCH_CONVERT
,
1691 tmp
- XFS_ATTR_LEAF_NAME_ALIGN
);
1694 hdr
->holes
= 1; /* mark as needing compaction */
1696 xfs_da_log_buf(args
->trans
, bp
,
1697 XFS_DA_LOGRANGE(leaf
, hdr
, sizeof(*hdr
)));
1700 * Check if leaf is less than 50% full, caller may want to
1701 * "join" the leaf with a sibling if so.
1703 tmp
= sizeof(xfs_attr_leaf_hdr_t
);
1704 tmp
+= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)
1705 * sizeof(xfs_attr_leaf_entry_t
);
1706 tmp
+= INT_GET(leaf
->hdr
.usedbytes
, ARCH_CONVERT
);
1707 return(tmp
< mp
->m_attr_magicpct
); /* leaf is < 37% full */
1711 * Move all the attribute list entries from drop_leaf into save_leaf.
1714 xfs_attr_leaf_unbalance(xfs_da_state_t
*state
, xfs_da_state_blk_t
*drop_blk
,
1715 xfs_da_state_blk_t
*save_blk
)
1717 xfs_attr_leafblock_t
*drop_leaf
, *save_leaf
, *tmp_leaf
;
1718 xfs_attr_leaf_hdr_t
*drop_hdr
, *save_hdr
, *tmp_hdr
;
1723 * Set up environment.
1726 ASSERT(drop_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1727 ASSERT(save_blk
->magic
== XFS_ATTR_LEAF_MAGIC
);
1728 drop_leaf
= drop_blk
->bp
->data
;
1729 save_leaf
= save_blk
->bp
->data
;
1730 ASSERT(INT_GET(drop_leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
1731 == XFS_ATTR_LEAF_MAGIC
);
1732 ASSERT(INT_GET(save_leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
1733 == XFS_ATTR_LEAF_MAGIC
);
1734 drop_hdr
= &drop_leaf
->hdr
;
1735 save_hdr
= &save_leaf
->hdr
;
1738 * Save last hashval from dying block for later Btree fixup.
1741 INT_GET(drop_leaf
->entries
[INT_GET(drop_leaf
->hdr
.count
,
1742 ARCH_CONVERT
)-1].hashval
,
1746 * Check if we need a temp buffer, or can we do it in place.
1747 * Note that we don't check "leaf" for holes because we will
1748 * always be dropping it, toosmall() decided that for us already.
1750 if (save_hdr
->holes
== 0) {
1752 * dest leaf has no holes, so we add there. May need
1753 * to make some room in the entry array.
1755 if (xfs_attr_leaf_order(save_blk
->bp
, drop_blk
->bp
)) {
1756 xfs_attr_leaf_moveents(drop_leaf
, 0, save_leaf
, 0,
1757 (int)INT_GET(drop_hdr
->count
, ARCH_CONVERT
), mp
);
1759 xfs_attr_leaf_moveents(drop_leaf
, 0, save_leaf
,
1760 INT_GET(save_hdr
->count
, ARCH_CONVERT
),
1761 (int)INT_GET(drop_hdr
->count
, ARCH_CONVERT
),
1766 * Destination has holes, so we make a temporary copy
1767 * of the leaf and add them both to that.
1769 tmpbuffer
= kmem_alloc(state
->blocksize
, KM_SLEEP
);
1770 ASSERT(tmpbuffer
!= NULL
);
1771 memset(tmpbuffer
, 0, state
->blocksize
);
1772 tmp_leaf
= (xfs_attr_leafblock_t
*)tmpbuffer
;
1773 tmp_hdr
= &tmp_leaf
->hdr
;
1774 tmp_hdr
->info
= save_hdr
->info
; /* struct copy */
1776 INT_SET(tmp_hdr
->firstused
, ARCH_CONVERT
, state
->blocksize
);
1777 if (!tmp_hdr
->firstused
) {
1778 INT_SET(tmp_hdr
->firstused
, ARCH_CONVERT
,
1779 state
->blocksize
- XFS_ATTR_LEAF_NAME_ALIGN
);
1781 tmp_hdr
->usedbytes
= 0;
1782 if (xfs_attr_leaf_order(save_blk
->bp
, drop_blk
->bp
)) {
1783 xfs_attr_leaf_moveents(drop_leaf
, 0, tmp_leaf
, 0,
1784 (int)INT_GET(drop_hdr
->count
, ARCH_CONVERT
),
1786 xfs_attr_leaf_moveents(save_leaf
, 0, tmp_leaf
,
1787 INT_GET(tmp_leaf
->hdr
.count
, ARCH_CONVERT
),
1788 (int)INT_GET(save_hdr
->count
, ARCH_CONVERT
),
1791 xfs_attr_leaf_moveents(save_leaf
, 0, tmp_leaf
, 0,
1792 (int)INT_GET(save_hdr
->count
, ARCH_CONVERT
),
1794 xfs_attr_leaf_moveents(drop_leaf
, 0, tmp_leaf
,
1795 INT_GET(tmp_leaf
->hdr
.count
, ARCH_CONVERT
),
1796 (int)INT_GET(drop_hdr
->count
, ARCH_CONVERT
),
1799 memcpy((char *)save_leaf
, (char *)tmp_leaf
, state
->blocksize
);
1800 kmem_free(tmpbuffer
, state
->blocksize
);
1803 xfs_da_log_buf(state
->args
->trans
, save_blk
->bp
, 0,
1804 state
->blocksize
- 1);
1807 * Copy out last hashval in each block for B-tree code.
1810 INT_GET(save_leaf
->entries
[INT_GET(save_leaf
->hdr
.count
,
1811 ARCH_CONVERT
)-1].hashval
,
1815 /*========================================================================
1816 * Routines used for finding things in the Btree.
1817 *========================================================================*/
1820 * Look up a name in a leaf attribute list structure.
1821 * This is the internal routine, it uses the caller's buffer.
1823 * Note that duplicate keys are allowed, but only check within the
1824 * current leaf node. The Btree code must check in adjacent leaf nodes.
1826 * Return in args->index the index into the entry[] array of either
1827 * the found entry, or where the entry should have been (insert before
1830 * Don't change the args->value unless we find the attribute.
1833 xfs_attr_leaf_lookup_int(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
1835 xfs_attr_leafblock_t
*leaf
;
1836 xfs_attr_leaf_entry_t
*entry
;
1837 xfs_attr_leaf_name_local_t
*name_loc
;
1838 xfs_attr_leaf_name_remote_t
*name_rmt
;
1840 xfs_dahash_t hashval
;
1843 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
1844 == XFS_ATTR_LEAF_MAGIC
);
1845 ASSERT(INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)
1846 < (XFS_LBSIZE(args
->dp
->i_mount
)/8));
1849 * Binary search. (note: small blocks will skip this loop)
1851 hashval
= args
->hashval
;
1852 probe
= span
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
) / 2;
1853 for (entry
= &leaf
->entries
[probe
]; span
> 4;
1854 entry
= &leaf
->entries
[probe
]) {
1856 if (INT_GET(entry
->hashval
, ARCH_CONVERT
) < hashval
)
1858 else if (INT_GET(entry
->hashval
, ARCH_CONVERT
) > hashval
)
1863 ASSERT((probe
>= 0) &&
1865 || (probe
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))));
1866 ASSERT((span
<= 4) || (INT_GET(entry
->hashval
, ARCH_CONVERT
)
1870 * Since we may have duplicate hashval's, find the first matching
1871 * hashval in the leaf.
1873 while ((probe
> 0) && (INT_GET(entry
->hashval
, ARCH_CONVERT
)
1878 while ((probe
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))
1879 && (INT_GET(entry
->hashval
, ARCH_CONVERT
) < hashval
)) {
1883 if ((probe
== INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))
1884 || (INT_GET(entry
->hashval
, ARCH_CONVERT
) != hashval
)) {
1885 args
->index
= probe
;
1886 return(XFS_ERROR(ENOATTR
));
1890 * Duplicate keys may be present, so search all of them for a match.
1892 for ( ; (probe
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))
1893 && (INT_GET(entry
->hashval
, ARCH_CONVERT
) == hashval
);
1896 * GROT: Add code to remove incomplete entries.
1899 * If we are looking for INCOMPLETE entries, show only those.
1900 * If we are looking for complete entries, show only those.
1902 if ((args
->flags
& XFS_ATTR_INCOMPLETE
) !=
1903 (entry
->flags
& XFS_ATTR_INCOMPLETE
)) {
1906 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1907 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, probe
);
1908 if (name_loc
->namelen
!= args
->namelen
)
1910 if (memcmp(args
->name
, (char *)name_loc
->nameval
,
1911 args
->namelen
) != 0)
1913 if (((args
->flags
& ATTR_SECURE
) != 0) !=
1914 ((entry
->flags
& XFS_ATTR_SECURE
) != 0))
1916 if (((args
->flags
& ATTR_ROOT
) != 0) !=
1917 ((entry
->flags
& XFS_ATTR_ROOT
) != 0))
1919 args
->index
= probe
;
1920 return(XFS_ERROR(EEXIST
));
1922 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, probe
);
1923 if (name_rmt
->namelen
!= args
->namelen
)
1925 if (memcmp(args
->name
, (char *)name_rmt
->name
,
1926 args
->namelen
) != 0)
1928 if (((args
->flags
& ATTR_SECURE
) != 0) !=
1929 ((entry
->flags
& XFS_ATTR_SECURE
) != 0))
1931 if (((args
->flags
& ATTR_ROOT
) != 0) !=
1932 ((entry
->flags
& XFS_ATTR_ROOT
) != 0))
1934 args
->index
= probe
;
1936 = INT_GET(name_rmt
->valueblk
, ARCH_CONVERT
);
1937 args
->rmtblkcnt
= XFS_B_TO_FSB(args
->dp
->i_mount
,
1938 INT_GET(name_rmt
->valuelen
,
1940 return(XFS_ERROR(EEXIST
));
1943 args
->index
= probe
;
1944 return(XFS_ERROR(ENOATTR
));
1948 * Get the value associated with an attribute name from a leaf attribute
1952 xfs_attr_leaf_getvalue(xfs_dabuf_t
*bp
, xfs_da_args_t
*args
)
1955 xfs_attr_leafblock_t
*leaf
;
1956 xfs_attr_leaf_entry_t
*entry
;
1957 xfs_attr_leaf_name_local_t
*name_loc
;
1958 xfs_attr_leaf_name_remote_t
*name_rmt
;
1961 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
1962 == XFS_ATTR_LEAF_MAGIC
);
1963 ASSERT(INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)
1964 < (XFS_LBSIZE(args
->dp
->i_mount
)/8));
1965 ASSERT(args
->index
< ((int)INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)));
1967 entry
= &leaf
->entries
[args
->index
];
1968 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1969 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, args
->index
);
1970 ASSERT(name_loc
->namelen
== args
->namelen
);
1971 ASSERT(memcmp(args
->name
, name_loc
->nameval
, args
->namelen
) == 0);
1972 valuelen
= INT_GET(name_loc
->valuelen
, ARCH_CONVERT
);
1973 if (args
->flags
& ATTR_KERNOVAL
) {
1974 args
->valuelen
= valuelen
;
1977 if (args
->valuelen
< valuelen
) {
1978 args
->valuelen
= valuelen
;
1979 return(XFS_ERROR(ERANGE
));
1981 args
->valuelen
= valuelen
;
1982 memcpy(args
->value
, &name_loc
->nameval
[args
->namelen
], valuelen
);
1984 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
1985 ASSERT(name_rmt
->namelen
== args
->namelen
);
1986 ASSERT(memcmp(args
->name
, name_rmt
->name
, args
->namelen
) == 0);
1987 valuelen
= INT_GET(name_rmt
->valuelen
, ARCH_CONVERT
);
1988 args
->rmtblkno
= INT_GET(name_rmt
->valueblk
, ARCH_CONVERT
);
1989 args
->rmtblkcnt
= XFS_B_TO_FSB(args
->dp
->i_mount
, valuelen
);
1990 if (args
->flags
& ATTR_KERNOVAL
) {
1991 args
->valuelen
= valuelen
;
1994 if (args
->valuelen
< valuelen
) {
1995 args
->valuelen
= valuelen
;
1996 return(XFS_ERROR(ERANGE
));
1998 args
->valuelen
= valuelen
;
2003 /*========================================================================
2005 *========================================================================*/
2008 * Move the indicated entries from one leaf to another.
2009 * NOTE: this routine modifies both source and destination leaves.
2013 xfs_attr_leaf_moveents(xfs_attr_leafblock_t
*leaf_s
, int start_s
,
2014 xfs_attr_leafblock_t
*leaf_d
, int start_d
,
2015 int count
, xfs_mount_t
*mp
)
2017 xfs_attr_leaf_hdr_t
*hdr_s
, *hdr_d
;
2018 xfs_attr_leaf_entry_t
*entry_s
, *entry_d
;
2022 * Check for nothing to do.
2028 * Set up environment.
2030 ASSERT(INT_GET(leaf_s
->hdr
.info
.magic
, ARCH_CONVERT
)
2031 == XFS_ATTR_LEAF_MAGIC
);
2032 ASSERT(INT_GET(leaf_d
->hdr
.info
.magic
, ARCH_CONVERT
)
2033 == XFS_ATTR_LEAF_MAGIC
);
2034 hdr_s
= &leaf_s
->hdr
;
2035 hdr_d
= &leaf_d
->hdr
;
2036 ASSERT((INT_GET(hdr_s
->count
, ARCH_CONVERT
) > 0)
2037 && (INT_GET(hdr_s
->count
, ARCH_CONVERT
)
2038 < (XFS_LBSIZE(mp
)/8)));
2039 ASSERT(INT_GET(hdr_s
->firstused
, ARCH_CONVERT
) >=
2040 ((INT_GET(hdr_s
->count
, ARCH_CONVERT
)
2041 * sizeof(*entry_s
))+sizeof(*hdr_s
)));
2042 ASSERT(INT_GET(hdr_d
->count
, ARCH_CONVERT
) < (XFS_LBSIZE(mp
)/8));
2043 ASSERT(INT_GET(hdr_d
->firstused
, ARCH_CONVERT
) >=
2044 ((INT_GET(hdr_d
->count
, ARCH_CONVERT
)
2045 * sizeof(*entry_d
))+sizeof(*hdr_d
)));
2047 ASSERT(start_s
< INT_GET(hdr_s
->count
, ARCH_CONVERT
));
2048 ASSERT(start_d
<= INT_GET(hdr_d
->count
, ARCH_CONVERT
));
2049 ASSERT(count
<= INT_GET(hdr_s
->count
, ARCH_CONVERT
));
2052 * Move the entries in the destination leaf up to make a hole?
2054 if (start_d
< INT_GET(hdr_d
->count
, ARCH_CONVERT
)) {
2055 tmp
= INT_GET(hdr_d
->count
, ARCH_CONVERT
) - start_d
;
2056 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2057 entry_s
= &leaf_d
->entries
[start_d
];
2058 entry_d
= &leaf_d
->entries
[start_d
+ count
];
2059 memmove((char *)entry_d
, (char *)entry_s
, tmp
);
2063 * Copy all entry's in the same (sorted) order,
2064 * but allocate attribute info packed and in sequence.
2066 entry_s
= &leaf_s
->entries
[start_s
];
2067 entry_d
= &leaf_d
->entries
[start_d
];
2069 for (i
= 0; i
< count
; entry_s
++, entry_d
++, desti
++, i
++) {
2070 ASSERT(INT_GET(entry_s
->nameidx
, ARCH_CONVERT
)
2071 >= INT_GET(hdr_s
->firstused
, ARCH_CONVERT
));
2072 tmp
= xfs_attr_leaf_entsize(leaf_s
, start_s
+ i
);
2075 * Code to drop INCOMPLETE entries. Difficult to use as we
2076 * may also need to change the insertion index. Code turned
2077 * off for 6.2, should be revisited later.
2079 if (entry_s
->flags
& XFS_ATTR_INCOMPLETE
) { /* skip partials? */
2080 memset(XFS_ATTR_LEAF_NAME(leaf_s
, start_s
+ i
), 0, tmp
);
2081 INT_MOD(hdr_s
->usedbytes
, ARCH_CONVERT
, -tmp
);
2082 INT_MOD(hdr_s
->count
, ARCH_CONVERT
, -1);
2083 entry_d
--; /* to compensate for ++ in loop hdr */
2085 if ((start_s
+ i
) < offset
)
2086 result
++; /* insertion index adjustment */
2089 INT_MOD(hdr_d
->firstused
, ARCH_CONVERT
, -tmp
);
2090 /* both on-disk, don't endian flip twice */
2091 entry_d
->hashval
= entry_s
->hashval
;
2092 /* both on-disk, don't endian flip twice */
2093 entry_d
->nameidx
= hdr_d
->firstused
;
2094 entry_d
->flags
= entry_s
->flags
;
2095 ASSERT(INT_GET(entry_d
->nameidx
, ARCH_CONVERT
) + tmp
2097 memmove(XFS_ATTR_LEAF_NAME(leaf_d
, desti
),
2098 XFS_ATTR_LEAF_NAME(leaf_s
, start_s
+ i
), tmp
);
2099 ASSERT(INT_GET(entry_s
->nameidx
, ARCH_CONVERT
) + tmp
2101 memset(XFS_ATTR_LEAF_NAME(leaf_s
, start_s
+ i
), 0, tmp
);
2102 INT_MOD(hdr_s
->usedbytes
, ARCH_CONVERT
, -tmp
);
2103 INT_MOD(hdr_d
->usedbytes
, ARCH_CONVERT
, tmp
);
2104 INT_MOD(hdr_s
->count
, ARCH_CONVERT
, -1);
2105 INT_MOD(hdr_d
->count
, ARCH_CONVERT
, 1);
2106 tmp
= INT_GET(hdr_d
->count
, ARCH_CONVERT
)
2107 * sizeof(xfs_attr_leaf_entry_t
)
2108 + sizeof(xfs_attr_leaf_hdr_t
);
2109 ASSERT(INT_GET(hdr_d
->firstused
, ARCH_CONVERT
) >= tmp
);
2116 * Zero out the entries we just copied.
2118 if (start_s
== INT_GET(hdr_s
->count
, ARCH_CONVERT
)) {
2119 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2120 entry_s
= &leaf_s
->entries
[start_s
];
2121 ASSERT(((char *)entry_s
+ tmp
) <=
2122 ((char *)leaf_s
+ XFS_LBSIZE(mp
)));
2123 memset((char *)entry_s
, 0, tmp
);
2126 * Move the remaining entries down to fill the hole,
2127 * then zero the entries at the top.
2129 tmp
= INT_GET(hdr_s
->count
, ARCH_CONVERT
) - count
;
2130 tmp
*= sizeof(xfs_attr_leaf_entry_t
);
2131 entry_s
= &leaf_s
->entries
[start_s
+ count
];
2132 entry_d
= &leaf_s
->entries
[start_s
];
2133 memmove((char *)entry_d
, (char *)entry_s
, tmp
);
2135 tmp
= count
* sizeof(xfs_attr_leaf_entry_t
);
2136 entry_s
= &leaf_s
->entries
[INT_GET(hdr_s
->count
,
2138 ASSERT(((char *)entry_s
+ tmp
) <=
2139 ((char *)leaf_s
+ XFS_LBSIZE(mp
)));
2140 memset((char *)entry_s
, 0, tmp
);
2144 * Fill in the freemap information
2146 INT_SET(hdr_d
->freemap
[0].base
, ARCH_CONVERT
,
2147 sizeof(xfs_attr_leaf_hdr_t
));
2148 INT_MOD(hdr_d
->freemap
[0].base
, ARCH_CONVERT
,
2149 INT_GET(hdr_d
->count
, ARCH_CONVERT
)
2150 * sizeof(xfs_attr_leaf_entry_t
));
2151 INT_SET(hdr_d
->freemap
[0].size
, ARCH_CONVERT
,
2152 INT_GET(hdr_d
->firstused
, ARCH_CONVERT
)
2153 - INT_GET(hdr_d
->freemap
[0].base
, ARCH_CONVERT
));
2154 hdr_d
->freemap
[1].base
= 0;
2155 hdr_d
->freemap
[2].base
= 0;
2156 hdr_d
->freemap
[1].size
= 0;
2157 hdr_d
->freemap
[2].size
= 0;
2158 hdr_s
->holes
= 1; /* leaf may not be compact */
2162 * Compare two leaf blocks "order".
2163 * Return 0 unless leaf2 should go before leaf1.
2166 xfs_attr_leaf_order(xfs_dabuf_t
*leaf1_bp
, xfs_dabuf_t
*leaf2_bp
)
2168 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
2170 leaf1
= leaf1_bp
->data
;
2171 leaf2
= leaf2_bp
->data
;
2172 ASSERT((INT_GET(leaf1
->hdr
.info
.magic
, ARCH_CONVERT
)
2173 == XFS_ATTR_LEAF_MAGIC
) &&
2174 (INT_GET(leaf2
->hdr
.info
.magic
, ARCH_CONVERT
)
2175 == XFS_ATTR_LEAF_MAGIC
));
2176 if ( (INT_GET(leaf1
->hdr
.count
, ARCH_CONVERT
) > 0)
2177 && (INT_GET(leaf2
->hdr
.count
, ARCH_CONVERT
) > 0)
2178 && ( (INT_GET(leaf2
->entries
[ 0 ].hashval
, ARCH_CONVERT
) <
2179 INT_GET(leaf1
->entries
[ 0 ].hashval
, ARCH_CONVERT
))
2180 || (INT_GET(leaf2
->entries
[INT_GET(leaf2
->hdr
.count
,
2181 ARCH_CONVERT
)-1].hashval
, ARCH_CONVERT
) <
2182 INT_GET(leaf1
->entries
[INT_GET(leaf1
->hdr
.count
,
2183 ARCH_CONVERT
)-1].hashval
, ARCH_CONVERT
))) ) {
2190 * Pick up the last hashvalue from a leaf block.
2193 xfs_attr_leaf_lasthash(xfs_dabuf_t
*bp
, int *count
)
2195 xfs_attr_leafblock_t
*leaf
;
2198 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
2199 == XFS_ATTR_LEAF_MAGIC
);
2201 *count
= INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
2202 if (!leaf
->hdr
.count
)
2204 return(INT_GET(leaf
->entries
[INT_GET(leaf
->hdr
.count
,
2205 ARCH_CONVERT
)-1].hashval
, ARCH_CONVERT
));
2209 * Calculate the number of bytes used to store the indicated attribute
2210 * (whether local or remote only calculate bytes in this block).
2213 xfs_attr_leaf_entsize(xfs_attr_leafblock_t
*leaf
, int index
)
2215 xfs_attr_leaf_name_local_t
*name_loc
;
2216 xfs_attr_leaf_name_remote_t
*name_rmt
;
2219 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
2220 == XFS_ATTR_LEAF_MAGIC
);
2221 if (leaf
->entries
[index
].flags
& XFS_ATTR_LOCAL
) {
2222 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, index
);
2223 size
= XFS_ATTR_LEAF_ENTSIZE_LOCAL(name_loc
->namelen
,
2224 INT_GET(name_loc
->valuelen
,
2227 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, index
);
2228 size
= XFS_ATTR_LEAF_ENTSIZE_REMOTE(name_rmt
->namelen
);
2234 * Calculate the number of bytes that would be required to store the new
2235 * attribute (whether local or remote only calculate bytes in this block).
2236 * This routine decides as a side effect whether the attribute will be
2237 * a "local" or a "remote" attribute.
2240 xfs_attr_leaf_newentsize(xfs_da_args_t
*args
, int blocksize
, int *local
)
2244 size
= XFS_ATTR_LEAF_ENTSIZE_LOCAL(args
->namelen
, args
->valuelen
);
2245 if (size
< XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(blocksize
)) {
2250 size
= XFS_ATTR_LEAF_ENTSIZE_REMOTE(args
->namelen
);
2259 * Copy out attribute list entries for attr_list(), for leaf attribute lists.
2262 xfs_attr_leaf_list_int(xfs_dabuf_t
*bp
, xfs_attr_list_context_t
*context
)
2264 attrlist_cursor_kern_t
*cursor
;
2265 xfs_attr_leafblock_t
*leaf
;
2266 xfs_attr_leaf_entry_t
*entry
;
2267 xfs_attr_leaf_name_local_t
*name_loc
;
2268 xfs_attr_leaf_name_remote_t
*name_rmt
;
2273 cursor
= context
->cursor
;
2274 cursor
->initted
= 1;
2276 xfs_attr_trace_l_cl("blk start", context
, leaf
);
2279 * Re-find our place in the leaf block if this is a new syscall.
2281 if (context
->resynch
) {
2282 entry
= &leaf
->entries
[0];
2283 for (i
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
);
2285 if (INT_GET(entry
->hashval
, ARCH_CONVERT
)
2286 == cursor
->hashval
) {
2287 if (cursor
->offset
== context
->dupcnt
) {
2288 context
->dupcnt
= 0;
2292 } else if (INT_GET(entry
->hashval
, ARCH_CONVERT
)
2293 > cursor
->hashval
) {
2294 context
->dupcnt
= 0;
2298 if (i
== INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
)) {
2299 xfs_attr_trace_l_c("not found", context
);
2303 entry
= &leaf
->entries
[0];
2306 context
->resynch
= 0;
2309 * We have found our place, start copying out the new attributes.
2312 for ( ; (i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
))
2313 && (retval
== 0); entry
++, i
++) {
2314 attrnames_t
*namesp
;
2316 if (INT_GET(entry
->hashval
, ARCH_CONVERT
) != cursor
->hashval
) {
2317 cursor
->hashval
= INT_GET(entry
->hashval
, ARCH_CONVERT
);
2321 if (entry
->flags
& XFS_ATTR_INCOMPLETE
)
2322 continue; /* skip incomplete entries */
2323 if (((context
->flags
& ATTR_SECURE
) != 0) !=
2324 ((entry
->flags
& XFS_ATTR_SECURE
) != 0) &&
2325 !(context
->flags
& ATTR_KERNORMALS
))
2326 continue; /* skip non-matching entries */
2327 if (((context
->flags
& ATTR_ROOT
) != 0) !=
2328 ((entry
->flags
& XFS_ATTR_ROOT
) != 0) &&
2329 !(context
->flags
& ATTR_KERNROOTLS
))
2330 continue; /* skip non-matching entries */
2332 namesp
= (entry
->flags
& XFS_ATTR_SECURE
) ? &attr_secure
:
2333 ((entry
->flags
& XFS_ATTR_ROOT
) ? &attr_trusted
:
2336 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2337 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, i
);
2338 if (context
->flags
& ATTR_KERNOVAL
) {
2339 ASSERT(context
->flags
& ATTR_KERNAMELS
);
2340 context
->count
+= namesp
->attr_namelen
+
2341 (int)name_loc
->namelen
+ 1;
2343 retval
= xfs_attr_put_listent(context
, namesp
,
2344 (char *)name_loc
->nameval
,
2345 (int)name_loc
->namelen
,
2346 (int)INT_GET(name_loc
->valuelen
,
2350 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, i
);
2351 if (context
->flags
& ATTR_KERNOVAL
) {
2352 ASSERT(context
->flags
& ATTR_KERNAMELS
);
2353 context
->count
+= namesp
->attr_namelen
+
2354 (int)name_rmt
->namelen
+ 1;
2356 retval
= xfs_attr_put_listent(context
, namesp
,
2357 (char *)name_rmt
->name
,
2358 (int)name_rmt
->namelen
,
2359 (int)INT_GET(name_rmt
->valuelen
,
2367 xfs_attr_trace_l_cl("blk end", context
, leaf
);
2371 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
2372 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
2373 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
2374 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
2375 & ~(sizeof(u_int32_t)-1))
2378 * Format an attribute and copy it out to the user's buffer.
2379 * Take care to check values and protect against them changing later,
2380 * we may be reading them directly out of a user buffer.
2384 xfs_attr_put_listent(xfs_attr_list_context_t
*context
,
2385 attrnames_t
*namesp
, char *name
, int namelen
, int valuelen
)
2387 attrlist_ent_t
*aep
;
2390 ASSERT(!(context
->flags
& ATTR_KERNOVAL
));
2391 if (context
->flags
& ATTR_KERNAMELS
) {
2394 ASSERT(context
->count
>= 0);
2396 arraytop
= context
->count
+ namesp
->attr_namelen
+ namelen
+ 1;
2397 if (arraytop
> context
->firstu
) {
2398 context
->count
= -1; /* insufficient space */
2401 offset
= (char *)context
->alist
+ context
->count
;
2402 strncpy(offset
, namesp
->attr_name
, namesp
->attr_namelen
);
2403 offset
+= namesp
->attr_namelen
;
2404 strncpy(offset
, name
, namelen
); /* real name */
2407 context
->count
+= namesp
->attr_namelen
+ namelen
+ 1;
2411 ASSERT(context
->count
>= 0);
2412 ASSERT(context
->count
< (ATTR_MAX_VALUELEN
/8));
2413 ASSERT(context
->firstu
>= sizeof(*context
->alist
));
2414 ASSERT(context
->firstu
<= context
->bufsize
);
2416 arraytop
= sizeof(*context
->alist
) +
2417 context
->count
* sizeof(context
->alist
->al_offset
[0]);
2418 context
->firstu
-= ATTR_ENTSIZE(namelen
);
2419 if (context
->firstu
< arraytop
) {
2420 xfs_attr_trace_l_c("buffer full", context
);
2421 context
->alist
->al_more
= 1;
2425 aep
= (attrlist_ent_t
*)&(((char *)context
->alist
)[ context
->firstu
]);
2426 aep
->a_valuelen
= valuelen
;
2427 memcpy(aep
->a_name
, name
, namelen
);
2428 aep
->a_name
[ namelen
] = 0;
2429 context
->alist
->al_offset
[ context
->count
++ ] = context
->firstu
;
2430 context
->alist
->al_count
= context
->count
;
2431 xfs_attr_trace_l_c("add", context
);
2435 /*========================================================================
2436 * Manage the INCOMPLETE flag in a leaf entry
2437 *========================================================================*/
2440 * Clear the INCOMPLETE flag on an entry in a leaf block.
2443 xfs_attr_leaf_clearflag(xfs_da_args_t
*args
)
2445 xfs_attr_leafblock_t
*leaf
;
2446 xfs_attr_leaf_entry_t
*entry
;
2447 xfs_attr_leaf_name_remote_t
*name_rmt
;
2451 xfs_attr_leaf_name_local_t
*name_loc
;
2457 * Set up the operation.
2459 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
2467 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
2468 == XFS_ATTR_LEAF_MAGIC
);
2469 ASSERT(args
->index
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
));
2470 ASSERT(args
->index
>= 0);
2471 entry
= &leaf
->entries
[ args
->index
];
2472 ASSERT(entry
->flags
& XFS_ATTR_INCOMPLETE
);
2475 if (entry
->flags
& XFS_ATTR_LOCAL
) {
2476 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf
, args
->index
);
2477 namelen
= name_loc
->namelen
;
2478 name
= (char *)name_loc
->nameval
;
2480 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2481 namelen
= name_rmt
->namelen
;
2482 name
= (char *)name_rmt
->name
;
2484 ASSERT(INT_GET(entry
->hashval
, ARCH_CONVERT
) == args
->hashval
);
2485 ASSERT(namelen
== args
->namelen
);
2486 ASSERT(memcmp(name
, args
->name
, namelen
) == 0);
2489 entry
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2490 xfs_da_log_buf(args
->trans
, bp
,
2491 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2493 if (args
->rmtblkno
) {
2494 ASSERT((entry
->flags
& XFS_ATTR_LOCAL
) == 0);
2495 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2496 INT_SET(name_rmt
->valueblk
, ARCH_CONVERT
, args
->rmtblkno
);
2497 INT_SET(name_rmt
->valuelen
, ARCH_CONVERT
, args
->valuelen
);
2498 xfs_da_log_buf(args
->trans
, bp
,
2499 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2501 xfs_da_buf_done(bp
);
2504 * Commit the flag value change and start the next trans in series.
2506 error
= xfs_attr_rolltrans(&args
->trans
, args
->dp
);
2512 * Set the INCOMPLETE flag on an entry in a leaf block.
2515 xfs_attr_leaf_setflag(xfs_da_args_t
*args
)
2517 xfs_attr_leafblock_t
*leaf
;
2518 xfs_attr_leaf_entry_t
*entry
;
2519 xfs_attr_leaf_name_remote_t
*name_rmt
;
2524 * Set up the operation.
2526 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp
,
2534 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
2535 == XFS_ATTR_LEAF_MAGIC
);
2536 ASSERT(args
->index
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
));
2537 ASSERT(args
->index
>= 0);
2538 entry
= &leaf
->entries
[ args
->index
];
2540 ASSERT((entry
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2541 entry
->flags
|= XFS_ATTR_INCOMPLETE
;
2542 xfs_da_log_buf(args
->trans
, bp
,
2543 XFS_DA_LOGRANGE(leaf
, entry
, sizeof(*entry
)));
2544 if ((entry
->flags
& XFS_ATTR_LOCAL
) == 0) {
2545 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, args
->index
);
2546 name_rmt
->valueblk
= 0;
2547 name_rmt
->valuelen
= 0;
2548 xfs_da_log_buf(args
->trans
, bp
,
2549 XFS_DA_LOGRANGE(leaf
, name_rmt
, sizeof(*name_rmt
)));
2551 xfs_da_buf_done(bp
);
2554 * Commit the flag value change and start the next trans in series.
2556 error
= xfs_attr_rolltrans(&args
->trans
, args
->dp
);
2562 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2563 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2564 * entry given by args->blkno2/index2.
2566 * Note that they could be in different blocks, or in the same block.
2569 xfs_attr_leaf_flipflags(xfs_da_args_t
*args
)
2571 xfs_attr_leafblock_t
*leaf1
, *leaf2
;
2572 xfs_attr_leaf_entry_t
*entry1
, *entry2
;
2573 xfs_attr_leaf_name_remote_t
*name_rmt
;
2574 xfs_dabuf_t
*bp1
, *bp2
;
2577 xfs_attr_leaf_name_local_t
*name_loc
;
2578 int namelen1
, namelen2
;
2579 char *name1
, *name2
;
2583 * Read the block containing the "old" attr
2585 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno
, -1, &bp1
,
2590 ASSERT(bp1
!= NULL
);
2593 * Read the block containing the "new" attr, if it is different
2595 if (args
->blkno2
!= args
->blkno
) {
2596 error
= xfs_da_read_buf(args
->trans
, args
->dp
, args
->blkno2
,
2597 -1, &bp2
, XFS_ATTR_FORK
);
2601 ASSERT(bp2
!= NULL
);
2607 ASSERT(INT_GET(leaf1
->hdr
.info
.magic
, ARCH_CONVERT
)
2608 == XFS_ATTR_LEAF_MAGIC
);
2609 ASSERT(args
->index
< INT_GET(leaf1
->hdr
.count
, ARCH_CONVERT
));
2610 ASSERT(args
->index
>= 0);
2611 entry1
= &leaf1
->entries
[ args
->index
];
2614 ASSERT(INT_GET(leaf2
->hdr
.info
.magic
, ARCH_CONVERT
)
2615 == XFS_ATTR_LEAF_MAGIC
);
2616 ASSERT(args
->index2
< INT_GET(leaf2
->hdr
.count
, ARCH_CONVERT
));
2617 ASSERT(args
->index2
>= 0);
2618 entry2
= &leaf2
->entries
[ args
->index2
];
2621 if (entry1
->flags
& XFS_ATTR_LOCAL
) {
2622 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf1
, args
->index
);
2623 namelen1
= name_loc
->namelen
;
2624 name1
= (char *)name_loc
->nameval
;
2626 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf1
, args
->index
);
2627 namelen1
= name_rmt
->namelen
;
2628 name1
= (char *)name_rmt
->name
;
2630 if (entry2
->flags
& XFS_ATTR_LOCAL
) {
2631 name_loc
= XFS_ATTR_LEAF_NAME_LOCAL(leaf2
, args
->index2
);
2632 namelen2
= name_loc
->namelen
;
2633 name2
= (char *)name_loc
->nameval
;
2635 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf2
, args
->index2
);
2636 namelen2
= name_rmt
->namelen
;
2637 name2
= (char *)name_rmt
->name
;
2639 ASSERT(INT_GET(entry1
->hashval
, ARCH_CONVERT
) == INT_GET(entry2
->hashval
, ARCH_CONVERT
));
2640 ASSERT(namelen1
== namelen2
);
2641 ASSERT(memcmp(name1
, name2
, namelen1
) == 0);
2644 ASSERT(entry1
->flags
& XFS_ATTR_INCOMPLETE
);
2645 ASSERT((entry2
->flags
& XFS_ATTR_INCOMPLETE
) == 0);
2647 entry1
->flags
&= ~XFS_ATTR_INCOMPLETE
;
2648 xfs_da_log_buf(args
->trans
, bp1
,
2649 XFS_DA_LOGRANGE(leaf1
, entry1
, sizeof(*entry1
)));
2650 if (args
->rmtblkno
) {
2651 ASSERT((entry1
->flags
& XFS_ATTR_LOCAL
) == 0);
2652 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf1
, args
->index
);
2653 INT_SET(name_rmt
->valueblk
, ARCH_CONVERT
, args
->rmtblkno
);
2654 INT_SET(name_rmt
->valuelen
, ARCH_CONVERT
, args
->valuelen
);
2655 xfs_da_log_buf(args
->trans
, bp1
,
2656 XFS_DA_LOGRANGE(leaf1
, name_rmt
, sizeof(*name_rmt
)));
2659 entry2
->flags
|= XFS_ATTR_INCOMPLETE
;
2660 xfs_da_log_buf(args
->trans
, bp2
,
2661 XFS_DA_LOGRANGE(leaf2
, entry2
, sizeof(*entry2
)));
2662 if ((entry2
->flags
& XFS_ATTR_LOCAL
) == 0) {
2663 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf2
, args
->index2
);
2664 name_rmt
->valueblk
= 0;
2665 name_rmt
->valuelen
= 0;
2666 xfs_da_log_buf(args
->trans
, bp2
,
2667 XFS_DA_LOGRANGE(leaf2
, name_rmt
, sizeof(*name_rmt
)));
2669 xfs_da_buf_done(bp1
);
2671 xfs_da_buf_done(bp2
);
2674 * Commit the flag value change and start the next trans in series.
2676 error
= xfs_attr_rolltrans(&args
->trans
, args
->dp
);
2681 /*========================================================================
2682 * Indiscriminately delete the entire attribute fork
2683 *========================================================================*/
2686 * Recurse (gasp!) through the attribute nodes until we find leaves.
2687 * We're doing a depth-first traversal in order to invalidate everything.
2690 xfs_attr_root_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
)
2692 xfs_da_blkinfo_t
*info
;
2698 * Read block 0 to see what we have to work with.
2699 * We only get here if we have extents, since we remove
2700 * the extents in reverse order the extent containing
2701 * block 0 must still be there.
2703 error
= xfs_da_read_buf(*trans
, dp
, 0, -1, &bp
, XFS_ATTR_FORK
);
2706 blkno
= xfs_da_blkno(bp
);
2709 * Invalidate the tree, even if the "tree" is only a single leaf block.
2710 * This is a depth-first traversal!
2713 if (INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_DA_NODE_MAGIC
) {
2714 error
= xfs_attr_node_inactive(trans
, dp
, bp
, 1);
2715 } else if (INT_GET(info
->magic
, ARCH_CONVERT
) == XFS_ATTR_LEAF_MAGIC
) {
2716 error
= xfs_attr_leaf_inactive(trans
, dp
, bp
);
2718 error
= XFS_ERROR(EIO
);
2719 xfs_da_brelse(*trans
, bp
);
2725 * Invalidate the incore copy of the root block.
2727 error
= xfs_da_get_buf(*trans
, dp
, 0, blkno
, &bp
, XFS_ATTR_FORK
);
2730 xfs_da_binval(*trans
, bp
); /* remove from cache */
2732 * Commit the invalidate and start the next transaction.
2734 error
= xfs_attr_rolltrans(trans
, dp
);
2740 * Recurse (gasp!) through the attribute nodes until we find leaves.
2741 * We're doing a depth-first traversal in order to invalidate everything.
2744 xfs_attr_node_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
, xfs_dabuf_t
*bp
,
2747 xfs_da_blkinfo_t
*info
;
2748 xfs_da_intnode_t
*node
;
2749 xfs_dablk_t child_fsb
;
2750 xfs_daddr_t parent_blkno
, child_blkno
;
2751 int error
, count
, i
;
2752 xfs_dabuf_t
*child_bp
;
2755 * Since this code is recursive (gasp!) we must protect ourselves.
2757 if (level
> XFS_DA_NODE_MAXDEPTH
) {
2758 xfs_da_brelse(*trans
, bp
); /* no locks for later trans */
2759 return(XFS_ERROR(EIO
));
2763 ASSERT(INT_GET(node
->hdr
.info
.magic
, ARCH_CONVERT
)
2764 == XFS_DA_NODE_MAGIC
);
2765 parent_blkno
= xfs_da_blkno(bp
); /* save for re-read later */
2766 count
= INT_GET(node
->hdr
.count
, ARCH_CONVERT
);
2768 xfs_da_brelse(*trans
, bp
);
2771 child_fsb
= INT_GET(node
->btree
[0].before
, ARCH_CONVERT
);
2772 xfs_da_brelse(*trans
, bp
); /* no locks for later trans */
2775 * If this is the node level just above the leaves, simply loop
2776 * over the leaves removing all of them. If this is higher up
2777 * in the tree, recurse downward.
2779 for (i
= 0; i
< count
; i
++) {
2781 * Read the subsidiary block to see what we have to work with.
2782 * Don't do this in a transaction. This is a depth-first
2783 * traversal of the tree so we may deal with many blocks
2784 * before we come back to this one.
2786 error
= xfs_da_read_buf(*trans
, dp
, child_fsb
, -2, &child_bp
,
2791 /* save for re-read later */
2792 child_blkno
= xfs_da_blkno(child_bp
);
2795 * Invalidate the subtree, however we have to.
2797 info
= child_bp
->data
;
2798 if (INT_GET(info
->magic
, ARCH_CONVERT
)
2799 == XFS_DA_NODE_MAGIC
) {
2800 error
= xfs_attr_node_inactive(trans
, dp
,
2802 } else if (INT_GET(info
->magic
, ARCH_CONVERT
)
2803 == XFS_ATTR_LEAF_MAGIC
) {
2804 error
= xfs_attr_leaf_inactive(trans
, dp
,
2807 error
= XFS_ERROR(EIO
);
2808 xfs_da_brelse(*trans
, child_bp
);
2814 * Remove the subsidiary block from the cache
2817 error
= xfs_da_get_buf(*trans
, dp
, 0, child_blkno
,
2818 &child_bp
, XFS_ATTR_FORK
);
2821 xfs_da_binval(*trans
, child_bp
);
2825 * If we're not done, re-read the parent to get the next
2826 * child block number.
2828 if ((i
+1) < count
) {
2829 error
= xfs_da_read_buf(*trans
, dp
, 0, parent_blkno
,
2830 &bp
, XFS_ATTR_FORK
);
2833 child_fsb
= INT_GET(node
->btree
[i
+1].before
, ARCH_CONVERT
);
2834 xfs_da_brelse(*trans
, bp
);
2837 * Atomically commit the whole invalidate stuff.
2839 if ((error
= xfs_attr_rolltrans(trans
, dp
)))
2847 * Invalidate all of the "remote" value regions pointed to by a particular
2849 * Note that we must release the lock on the buffer so that we are not
2850 * caught holding something that the logging code wants to flush to disk.
2853 xfs_attr_leaf_inactive(xfs_trans_t
**trans
, xfs_inode_t
*dp
, xfs_dabuf_t
*bp
)
2855 xfs_attr_leafblock_t
*leaf
;
2856 xfs_attr_leaf_entry_t
*entry
;
2857 xfs_attr_leaf_name_remote_t
*name_rmt
;
2858 xfs_attr_inactive_list_t
*list
, *lp
;
2859 int error
, count
, size
, tmp
, i
;
2862 ASSERT(INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
)
2863 == XFS_ATTR_LEAF_MAGIC
);
2866 * Count the number of "remote" value extents.
2869 entry
= &leaf
->entries
[0];
2870 for (i
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); entry
++, i
++) {
2871 if ( INT_GET(entry
->nameidx
, ARCH_CONVERT
)
2872 && ((entry
->flags
& XFS_ATTR_LOCAL
) == 0)) {
2873 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, i
);
2874 if (name_rmt
->valueblk
)
2880 * If there are no "remote" values, we're done.
2883 xfs_da_brelse(*trans
, bp
);
2888 * Allocate storage for a list of all the "remote" value extents.
2890 size
= count
* sizeof(xfs_attr_inactive_list_t
);
2891 list
= (xfs_attr_inactive_list_t
*)kmem_alloc(size
, KM_SLEEP
);
2894 * Identify each of the "remote" value extents.
2897 entry
= &leaf
->entries
[0];
2898 for (i
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); entry
++, i
++) {
2899 if ( INT_GET(entry
->nameidx
, ARCH_CONVERT
)
2900 && ((entry
->flags
& XFS_ATTR_LOCAL
) == 0)) {
2901 name_rmt
= XFS_ATTR_LEAF_NAME_REMOTE(leaf
, i
);
2902 if (name_rmt
->valueblk
) {
2903 /* both on-disk, don't endian flip twice */
2904 lp
->valueblk
= name_rmt
->valueblk
;
2905 INT_SET(lp
->valuelen
, ARCH_CONVERT
,
2906 XFS_B_TO_FSB(dp
->i_mount
,
2907 INT_GET(name_rmt
->valuelen
,
2913 xfs_da_brelse(*trans
, bp
); /* unlock for trans. in freextent() */
2916 * Invalidate each of the "remote" value extents.
2919 for (lp
= list
, i
= 0; i
< count
; i
++, lp
++) {
2920 tmp
= xfs_attr_leaf_freextent(trans
, dp
,
2921 INT_GET(lp
->valueblk
,
2923 INT_GET(lp
->valuelen
,
2926 error
= tmp
; /* save only the 1st errno */
2929 kmem_free((xfs_caddr_t
)list
, size
);
2934 * Look at all the extents for this logical region,
2935 * invalidate any buffers that are incore/in transactions.
2938 xfs_attr_leaf_freextent(xfs_trans_t
**trans
, xfs_inode_t
*dp
,
2939 xfs_dablk_t blkno
, int blkcnt
)
2941 xfs_bmbt_irec_t map
;
2943 int tblkcnt
, dblkcnt
, nmap
, error
;
2948 * Roll through the "value", invalidating the attribute value's
2953 while (tblkcnt
> 0) {
2955 * Try to remember where we decided to put the value.
2958 error
= xfs_bmapi(*trans
, dp
, (xfs_fileoff_t
)tblkno
, tblkcnt
,
2959 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
2960 NULL
, 0, &map
, &nmap
, NULL
);
2965 ASSERT(map
.br_startblock
!= DELAYSTARTBLOCK
);
2968 * If it's a hole, these are already unmapped
2969 * so there's nothing to invalidate.
2971 if (map
.br_startblock
!= HOLESTARTBLOCK
) {
2973 dblkno
= XFS_FSB_TO_DADDR(dp
->i_mount
,
2975 dblkcnt
= XFS_FSB_TO_BB(dp
->i_mount
,
2977 bp
= xfs_trans_get_buf(*trans
,
2978 dp
->i_mount
->m_ddev_targp
,
2979 dblkno
, dblkcnt
, XFS_BUF_LOCK
);
2980 xfs_trans_binval(*trans
, bp
);
2982 * Roll to next transaction.
2984 if ((error
= xfs_attr_rolltrans(trans
, dp
)))
2988 tblkno
+= map
.br_blockcount
;
2989 tblkcnt
-= map
.br_blockcount
;
2997 * Roll from one trans in the sequence of PERMANENT transactions to the next.
3000 xfs_attr_rolltrans(xfs_trans_t
**transp
, xfs_inode_t
*dp
)
3003 unsigned int logres
, count
;
3007 * Ensure that the inode is always logged.
3010 xfs_trans_log_inode(trans
, dp
, XFS_ILOG_CORE
);
3013 * Copy the critical parameters from one trans to the next.
3015 logres
= trans
->t_log_res
;
3016 count
= trans
->t_log_count
;
3017 *transp
= xfs_trans_dup(trans
);
3020 * Commit the current transaction.
3021 * If this commit failed, then it'd just unlock those items that
3022 * are not marked ihold. That also means that a filesystem shutdown
3023 * is in progress. The caller takes the responsibility to cancel
3024 * the duplicate transaction that gets returned.
3026 if ((error
= xfs_trans_commit(trans
, 0, NULL
)))
3032 * Reserve space in the log for th next transaction.
3033 * This also pushes items in the "AIL", the list of logged items,
3034 * out to disk if they are taking up space at the tail of the log
3035 * that we want to use. This requires that either nothing be locked
3036 * across this call, or that anything that is locked be logged in
3037 * the prior and the next transactions.
3039 error
= xfs_trans_reserve(trans
, 0, logres
, 0,
3040 XFS_TRANS_PERM_LOG_RES
, count
);
3042 * Ensure that the inode is in the new transaction and locked.
3045 xfs_trans_ijoin(trans
, dp
, XFS_ILOCK_EXCL
);
3046 xfs_trans_ihold(trans
, dp
);