perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / fs / xfs / libxfs / xfs_attr_leaf.c
blob1f6e3965ff7425456ca64477a713573cb5e7943a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
6 */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_bit.h"
14 #include "xfs_sb.h"
15 #include "xfs_mount.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
18 #include "xfs_inode.h"
19 #include "xfs_trans.h"
20 #include "xfs_inode_item.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_bmap.h"
23 #include "xfs_attr_sf.h"
24 #include "xfs_attr_remote.h"
25 #include "xfs_attr.h"
26 #include "xfs_attr_leaf.h"
27 #include "xfs_error.h"
28 #include "xfs_trace.h"
29 #include "xfs_buf_item.h"
30 #include "xfs_cksum.h"
31 #include "xfs_dir2.h"
32 #include "xfs_log.h"
36 * xfs_attr_leaf.c
38 * Routines to implement leaf blocks of attributes as Btrees of hashed names.
41 /*========================================================================
42 * Function prototypes for the kernel.
43 *========================================================================*/
46 * Routines used for growing the Btree.
48 STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
49 xfs_dablk_t which_block, struct xfs_buf **bpp);
50 STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
51 struct xfs_attr3_icleaf_hdr *ichdr,
52 struct xfs_da_args *args, int freemap_index);
53 STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
54 struct xfs_attr3_icleaf_hdr *ichdr,
55 struct xfs_buf *leaf_buffer);
56 STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
57 xfs_da_state_blk_t *blk1,
58 xfs_da_state_blk_t *blk2);
59 STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
60 xfs_da_state_blk_t *leaf_blk_1,
61 struct xfs_attr3_icleaf_hdr *ichdr1,
62 xfs_da_state_blk_t *leaf_blk_2,
63 struct xfs_attr3_icleaf_hdr *ichdr2,
64 int *number_entries_in_blk1,
65 int *number_usedbytes_in_blk1);
68 * Utility routines.
70 STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
71 struct xfs_attr_leafblock *src_leaf,
72 struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
73 struct xfs_attr_leafblock *dst_leaf,
74 struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
75 int move_count);
76 STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
79 * attr3 block 'firstused' conversion helpers.
81 * firstused refers to the offset of the first used byte of the nameval region
82 * of an attr leaf block. The region starts at the tail of the block and expands
83 * backwards towards the middle. As such, firstused is initialized to the block
84 * size for an empty leaf block and is reduced from there.
86 * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
87 * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
88 * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
89 * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
90 * the attr block size. The following helpers manage the conversion between the
91 * in-core and on-disk formats.
94 static void
95 xfs_attr3_leaf_firstused_from_disk(
96 struct xfs_da_geometry *geo,
97 struct xfs_attr3_icleaf_hdr *to,
98 struct xfs_attr_leafblock *from)
100 struct xfs_attr3_leaf_hdr *hdr3;
102 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
103 hdr3 = (struct xfs_attr3_leaf_hdr *) from;
104 to->firstused = be16_to_cpu(hdr3->firstused);
105 } else {
106 to->firstused = be16_to_cpu(from->hdr.firstused);
110 * Convert from the magic fsb size value to actual blocksize. This
111 * should only occur for empty blocks when the block size overflows
112 * 16-bits.
114 if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
115 ASSERT(!to->count && !to->usedbytes);
116 ASSERT(geo->blksize > USHRT_MAX);
117 to->firstused = geo->blksize;
121 static void
122 xfs_attr3_leaf_firstused_to_disk(
123 struct xfs_da_geometry *geo,
124 struct xfs_attr_leafblock *to,
125 struct xfs_attr3_icleaf_hdr *from)
127 struct xfs_attr3_leaf_hdr *hdr3;
128 uint32_t firstused;
130 /* magic value should only be seen on disk */
131 ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
134 * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
135 * value. This only overflows at the max supported value of 64k. Use the
136 * magic on-disk value to represent block size in this case.
138 firstused = from->firstused;
139 if (firstused > USHRT_MAX) {
140 ASSERT(from->firstused == geo->blksize);
141 firstused = XFS_ATTR3_LEAF_NULLOFF;
144 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
145 hdr3 = (struct xfs_attr3_leaf_hdr *) to;
146 hdr3->firstused = cpu_to_be16(firstused);
147 } else {
148 to->hdr.firstused = cpu_to_be16(firstused);
152 void
153 xfs_attr3_leaf_hdr_from_disk(
154 struct xfs_da_geometry *geo,
155 struct xfs_attr3_icleaf_hdr *to,
156 struct xfs_attr_leafblock *from)
158 int i;
160 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
161 from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
163 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
164 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
166 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
167 to->back = be32_to_cpu(hdr3->info.hdr.back);
168 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
169 to->count = be16_to_cpu(hdr3->count);
170 to->usedbytes = be16_to_cpu(hdr3->usedbytes);
171 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
172 to->holes = hdr3->holes;
174 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
175 to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
176 to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
178 return;
180 to->forw = be32_to_cpu(from->hdr.info.forw);
181 to->back = be32_to_cpu(from->hdr.info.back);
182 to->magic = be16_to_cpu(from->hdr.info.magic);
183 to->count = be16_to_cpu(from->hdr.count);
184 to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
185 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
186 to->holes = from->hdr.holes;
188 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
189 to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
190 to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
194 void
195 xfs_attr3_leaf_hdr_to_disk(
196 struct xfs_da_geometry *geo,
197 struct xfs_attr_leafblock *to,
198 struct xfs_attr3_icleaf_hdr *from)
200 int i;
202 ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
203 from->magic == XFS_ATTR3_LEAF_MAGIC);
205 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
206 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
208 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
209 hdr3->info.hdr.back = cpu_to_be32(from->back);
210 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
211 hdr3->count = cpu_to_be16(from->count);
212 hdr3->usedbytes = cpu_to_be16(from->usedbytes);
213 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
214 hdr3->holes = from->holes;
215 hdr3->pad1 = 0;
217 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
218 hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
219 hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
221 return;
223 to->hdr.info.forw = cpu_to_be32(from->forw);
224 to->hdr.info.back = cpu_to_be32(from->back);
225 to->hdr.info.magic = cpu_to_be16(from->magic);
226 to->hdr.count = cpu_to_be16(from->count);
227 to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
228 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
229 to->hdr.holes = from->holes;
230 to->hdr.pad1 = 0;
232 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
233 to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
234 to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
238 static xfs_failaddr_t
239 xfs_attr3_leaf_verify(
240 struct xfs_buf *bp)
242 struct xfs_attr3_icleaf_hdr ichdr;
243 struct xfs_mount *mp = bp->b_target->bt_mount;
244 struct xfs_attr_leafblock *leaf = bp->b_addr;
245 struct xfs_attr_leaf_entry *entries;
246 uint32_t end; /* must be 32bit - see below */
247 int i;
248 xfs_failaddr_t fa;
250 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
252 fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
253 if (fa)
254 return fa;
257 * In recovery there is a transient state where count == 0 is valid
258 * because we may have transitioned an empty shortform attr to a leaf
259 * if the attr didn't fit in shortform.
261 if (!xfs_log_in_recovery(mp) && ichdr.count == 0)
262 return __this_address;
265 * firstused is the block offset of the first name info structure.
266 * Make sure it doesn't go off the block or crash into the header.
268 if (ichdr.firstused > mp->m_attr_geo->blksize)
269 return __this_address;
270 if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
271 return __this_address;
273 /* Make sure the entries array doesn't crash into the name info. */
274 entries = xfs_attr3_leaf_entryp(bp->b_addr);
275 if ((char *)&entries[ichdr.count] >
276 (char *)bp->b_addr + ichdr.firstused)
277 return __this_address;
279 /* XXX: need to range check rest of attr header values */
280 /* XXX: hash order check? */
283 * Quickly check the freemap information. Attribute data has to be
284 * aligned to 4-byte boundaries, and likewise for the free space.
286 * Note that for 64k block size filesystems, the freemap entries cannot
287 * overflow as they are only be16 fields. However, when checking end
288 * pointer of the freemap, we have to be careful to detect overflows and
289 * so use uint32_t for those checks.
291 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
292 if (ichdr.freemap[i].base > mp->m_attr_geo->blksize)
293 return __this_address;
294 if (ichdr.freemap[i].base & 0x3)
295 return __this_address;
296 if (ichdr.freemap[i].size > mp->m_attr_geo->blksize)
297 return __this_address;
298 if (ichdr.freemap[i].size & 0x3)
299 return __this_address;
301 /* be care of 16 bit overflows here */
302 end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size;
303 if (end < ichdr.freemap[i].base)
304 return __this_address;
305 if (end > mp->m_attr_geo->blksize)
306 return __this_address;
309 return NULL;
312 static void
313 xfs_attr3_leaf_write_verify(
314 struct xfs_buf *bp)
316 struct xfs_mount *mp = bp->b_target->bt_mount;
317 struct xfs_buf_log_item *bip = bp->b_log_item;
318 struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
319 xfs_failaddr_t fa;
321 fa = xfs_attr3_leaf_verify(bp);
322 if (fa) {
323 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
324 return;
327 if (!xfs_sb_version_hascrc(&mp->m_sb))
328 return;
330 if (bip)
331 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
333 xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
337 * leaf/node format detection on trees is sketchy, so a node read can be done on
338 * leaf level blocks when detection identifies the tree as a node format tree
339 * incorrectly. In this case, we need to swap the verifier to match the correct
340 * format of the block being read.
342 static void
343 xfs_attr3_leaf_read_verify(
344 struct xfs_buf *bp)
346 struct xfs_mount *mp = bp->b_target->bt_mount;
347 xfs_failaddr_t fa;
349 if (xfs_sb_version_hascrc(&mp->m_sb) &&
350 !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
351 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
352 else {
353 fa = xfs_attr3_leaf_verify(bp);
354 if (fa)
355 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
359 const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
360 .name = "xfs_attr3_leaf",
361 .magic16 = { cpu_to_be16(XFS_ATTR_LEAF_MAGIC),
362 cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) },
363 .verify_read = xfs_attr3_leaf_read_verify,
364 .verify_write = xfs_attr3_leaf_write_verify,
365 .verify_struct = xfs_attr3_leaf_verify,
369 xfs_attr3_leaf_read(
370 struct xfs_trans *tp,
371 struct xfs_inode *dp,
372 xfs_dablk_t bno,
373 xfs_daddr_t mappedbno,
374 struct xfs_buf **bpp)
376 int err;
378 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
379 XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
380 if (!err && tp && *bpp)
381 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
382 return err;
385 /*========================================================================
386 * Namespace helper routines
387 *========================================================================*/
390 * If namespace bits don't match return 0.
391 * If all match then return 1.
393 STATIC int
394 xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
396 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
400 /*========================================================================
401 * External routines when attribute fork size < XFS_LITINO(mp).
402 *========================================================================*/
405 * Query whether the requested number of additional bytes of extended
406 * attribute space will be able to fit inline.
408 * Returns zero if not, else the di_forkoff fork offset to be used in the
409 * literal area for attribute data once the new bytes have been added.
411 * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
412 * special case for dev/uuid inodes, they have fixed size data forks.
415 xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
417 int offset;
418 int minforkoff; /* lower limit on valid forkoff locations */
419 int maxforkoff; /* upper limit on valid forkoff locations */
420 int dsize;
421 xfs_mount_t *mp = dp->i_mount;
423 /* rounded down */
424 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
426 if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) {
427 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
428 return (offset >= minforkoff) ? minforkoff : 0;
432 * If the requested numbers of bytes is smaller or equal to the
433 * current attribute fork size we can always proceed.
435 * Note that if_bytes in the data fork might actually be larger than
436 * the current data fork size is due to delalloc extents. In that
437 * case either the extent count will go down when they are converted
438 * to real extents, or the delalloc conversion will take care of the
439 * literal area rebalancing.
441 if (bytes <= XFS_IFORK_ASIZE(dp))
442 return dp->i_d.di_forkoff;
445 * For attr2 we can try to move the forkoff if there is space in the
446 * literal area, but for the old format we are done if there is no
447 * space in the fixed attribute fork.
449 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
450 return 0;
452 dsize = dp->i_df.if_bytes;
454 switch (dp->i_d.di_format) {
455 case XFS_DINODE_FMT_EXTENTS:
457 * If there is no attr fork and the data fork is extents,
458 * determine if creating the default attr fork will result
459 * in the extents form migrating to btree. If so, the
460 * minimum offset only needs to be the space required for
461 * the btree root.
463 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
464 xfs_default_attroffset(dp))
465 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
466 break;
467 case XFS_DINODE_FMT_BTREE:
469 * If we have a data btree then keep forkoff if we have one,
470 * otherwise we are adding a new attr, so then we set
471 * minforkoff to where the btree root can finish so we have
472 * plenty of room for attrs
474 if (dp->i_d.di_forkoff) {
475 if (offset < dp->i_d.di_forkoff)
476 return 0;
477 return dp->i_d.di_forkoff;
479 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
480 break;
484 * A data fork btree root must have space for at least
485 * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
487 minforkoff = max(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
488 minforkoff = roundup(minforkoff, 8) >> 3;
490 /* attr fork btree root can have at least this many key/ptr pairs */
491 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
492 XFS_BMDR_SPACE_CALC(MINABTPTRS);
493 maxforkoff = maxforkoff >> 3; /* rounded down */
495 if (offset >= maxforkoff)
496 return maxforkoff;
497 if (offset >= minforkoff)
498 return offset;
499 return 0;
503 * Switch on the ATTR2 superblock bit (implies also FEATURES2)
505 STATIC void
506 xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
508 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
509 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
510 spin_lock(&mp->m_sb_lock);
511 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
512 xfs_sb_version_addattr2(&mp->m_sb);
513 spin_unlock(&mp->m_sb_lock);
514 xfs_log_sb(tp);
515 } else
516 spin_unlock(&mp->m_sb_lock);
521 * Create the initial contents of a shortform attribute list.
523 void
524 xfs_attr_shortform_create(xfs_da_args_t *args)
526 xfs_attr_sf_hdr_t *hdr;
527 xfs_inode_t *dp;
528 struct xfs_ifork *ifp;
530 trace_xfs_attr_sf_create(args);
532 dp = args->dp;
533 ASSERT(dp != NULL);
534 ifp = dp->i_afp;
535 ASSERT(ifp != NULL);
536 ASSERT(ifp->if_bytes == 0);
537 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
538 ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
539 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
540 ifp->if_flags |= XFS_IFINLINE;
541 } else {
542 ASSERT(ifp->if_flags & XFS_IFINLINE);
544 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
545 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
546 hdr->count = 0;
547 hdr->totsize = cpu_to_be16(sizeof(*hdr));
548 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
552 * Add a name/value pair to the shortform attribute list.
553 * Overflow from the inode has already been checked for.
555 void
556 xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
558 xfs_attr_shortform_t *sf;
559 xfs_attr_sf_entry_t *sfe;
560 int i, offset, size;
561 xfs_mount_t *mp;
562 xfs_inode_t *dp;
563 struct xfs_ifork *ifp;
565 trace_xfs_attr_sf_add(args);
567 dp = args->dp;
568 mp = dp->i_mount;
569 dp->i_d.di_forkoff = forkoff;
571 ifp = dp->i_afp;
572 ASSERT(ifp->if_flags & XFS_IFINLINE);
573 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
574 sfe = &sf->list[0];
575 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
576 #ifdef DEBUG
577 if (sfe->namelen != args->namelen)
578 continue;
579 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
580 continue;
581 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
582 continue;
583 ASSERT(0);
584 #endif
587 offset = (char *)sfe - (char *)sf;
588 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
589 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
590 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
591 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
593 sfe->namelen = args->namelen;
594 sfe->valuelen = args->valuelen;
595 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
596 memcpy(sfe->nameval, args->name, args->namelen);
597 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
598 sf->hdr.count++;
599 be16_add_cpu(&sf->hdr.totsize, size);
600 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
602 xfs_sbversion_add_attr2(mp, args->trans);
606 * After the last attribute is removed revert to original inode format,
607 * making all literal area available to the data fork once more.
609 void
610 xfs_attr_fork_remove(
611 struct xfs_inode *ip,
612 struct xfs_trans *tp)
614 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
615 ip->i_d.di_forkoff = 0;
616 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
618 ASSERT(ip->i_d.di_anextents == 0);
619 ASSERT(ip->i_afp == NULL);
621 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
625 * Remove an attribute from the shortform attribute list structure.
628 xfs_attr_shortform_remove(xfs_da_args_t *args)
630 xfs_attr_shortform_t *sf;
631 xfs_attr_sf_entry_t *sfe;
632 int base, size=0, end, totsize, i;
633 xfs_mount_t *mp;
634 xfs_inode_t *dp;
636 trace_xfs_attr_sf_remove(args);
638 dp = args->dp;
639 mp = dp->i_mount;
640 base = sizeof(xfs_attr_sf_hdr_t);
641 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
642 sfe = &sf->list[0];
643 end = sf->hdr.count;
644 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
645 base += size, i++) {
646 size = XFS_ATTR_SF_ENTSIZE(sfe);
647 if (sfe->namelen != args->namelen)
648 continue;
649 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
650 continue;
651 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
652 continue;
653 break;
655 if (i == end)
656 return -ENOATTR;
659 * Fix up the attribute fork data, covering the hole
661 end = base + size;
662 totsize = be16_to_cpu(sf->hdr.totsize);
663 if (end != totsize)
664 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
665 sf->hdr.count--;
666 be16_add_cpu(&sf->hdr.totsize, -size);
669 * Fix up the start offset of the attribute fork
671 totsize -= size;
672 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
673 (mp->m_flags & XFS_MOUNT_ATTR2) &&
674 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
675 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
676 xfs_attr_fork_remove(dp, args->trans);
677 } else {
678 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
679 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
680 ASSERT(dp->i_d.di_forkoff);
681 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
682 (args->op_flags & XFS_DA_OP_ADDNAME) ||
683 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
684 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
685 xfs_trans_log_inode(args->trans, dp,
686 XFS_ILOG_CORE | XFS_ILOG_ADATA);
689 xfs_sbversion_add_attr2(mp, args->trans);
691 return 0;
695 * Look up a name in a shortform attribute list structure.
697 /*ARGSUSED*/
699 xfs_attr_shortform_lookup(xfs_da_args_t *args)
701 xfs_attr_shortform_t *sf;
702 xfs_attr_sf_entry_t *sfe;
703 int i;
704 struct xfs_ifork *ifp;
706 trace_xfs_attr_sf_lookup(args);
708 ifp = args->dp->i_afp;
709 ASSERT(ifp->if_flags & XFS_IFINLINE);
710 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
711 sfe = &sf->list[0];
712 for (i = 0; i < sf->hdr.count;
713 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
714 if (sfe->namelen != args->namelen)
715 continue;
716 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
717 continue;
718 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
719 continue;
720 return -EEXIST;
722 return -ENOATTR;
726 * Look up a name in a shortform attribute list structure.
728 /*ARGSUSED*/
730 xfs_attr_shortform_getvalue(xfs_da_args_t *args)
732 xfs_attr_shortform_t *sf;
733 xfs_attr_sf_entry_t *sfe;
734 int i;
736 ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
737 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
738 sfe = &sf->list[0];
739 for (i = 0; i < sf->hdr.count;
740 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
741 if (sfe->namelen != args->namelen)
742 continue;
743 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
744 continue;
745 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
746 continue;
747 if (args->flags & ATTR_KERNOVAL) {
748 args->valuelen = sfe->valuelen;
749 return -EEXIST;
751 if (args->valuelen < sfe->valuelen) {
752 args->valuelen = sfe->valuelen;
753 return -ERANGE;
755 args->valuelen = sfe->valuelen;
756 memcpy(args->value, &sfe->nameval[args->namelen],
757 args->valuelen);
758 return -EEXIST;
760 return -ENOATTR;
764 * Convert from using the shortform to the leaf. On success, return the
765 * buffer so that we can keep it locked until we're totally done with it.
768 xfs_attr_shortform_to_leaf(
769 struct xfs_da_args *args,
770 struct xfs_buf **leaf_bp)
772 struct xfs_inode *dp;
773 struct xfs_attr_shortform *sf;
774 struct xfs_attr_sf_entry *sfe;
775 struct xfs_da_args nargs;
776 char *tmpbuffer;
777 int error, i, size;
778 xfs_dablk_t blkno;
779 struct xfs_buf *bp;
780 struct xfs_ifork *ifp;
782 trace_xfs_attr_sf_to_leaf(args);
784 dp = args->dp;
785 ifp = dp->i_afp;
786 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
787 size = be16_to_cpu(sf->hdr.totsize);
788 tmpbuffer = kmem_alloc(size, KM_SLEEP);
789 ASSERT(tmpbuffer != NULL);
790 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
791 sf = (xfs_attr_shortform_t *)tmpbuffer;
793 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
794 xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
796 bp = NULL;
797 error = xfs_da_grow_inode(args, &blkno);
798 if (error) {
800 * If we hit an IO error middle of the transaction inside
801 * grow_inode(), we may have inconsistent data. Bail out.
803 if (error == -EIO)
804 goto out;
805 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
806 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
807 goto out;
810 ASSERT(blkno == 0);
811 error = xfs_attr3_leaf_create(args, blkno, &bp);
812 if (error) {
813 /* xfs_attr3_leaf_create may not have instantiated a block */
814 if (bp && (xfs_da_shrink_inode(args, 0, bp) != 0))
815 goto out;
816 xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
817 memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
818 goto out;
821 memset((char *)&nargs, 0, sizeof(nargs));
822 nargs.dp = dp;
823 nargs.geo = args->geo;
824 nargs.total = args->total;
825 nargs.whichfork = XFS_ATTR_FORK;
826 nargs.trans = args->trans;
827 nargs.op_flags = XFS_DA_OP_OKNOENT;
829 sfe = &sf->list[0];
830 for (i = 0; i < sf->hdr.count; i++) {
831 nargs.name = sfe->nameval;
832 nargs.namelen = sfe->namelen;
833 nargs.value = &sfe->nameval[nargs.namelen];
834 nargs.valuelen = sfe->valuelen;
835 nargs.hashval = xfs_da_hashname(sfe->nameval,
836 sfe->namelen);
837 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
838 error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
839 ASSERT(error == -ENOATTR);
840 error = xfs_attr3_leaf_add(bp, &nargs);
841 ASSERT(error != -ENOSPC);
842 if (error)
843 goto out;
844 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
846 error = 0;
847 *leaf_bp = bp;
848 out:
849 kmem_free(tmpbuffer);
850 return error;
854 * Check a leaf attribute block to see if all the entries would fit into
855 * a shortform attribute list.
858 xfs_attr_shortform_allfit(
859 struct xfs_buf *bp,
860 struct xfs_inode *dp)
862 struct xfs_attr_leafblock *leaf;
863 struct xfs_attr_leaf_entry *entry;
864 xfs_attr_leaf_name_local_t *name_loc;
865 struct xfs_attr3_icleaf_hdr leafhdr;
866 int bytes;
867 int i;
868 struct xfs_mount *mp = bp->b_target->bt_mount;
870 leaf = bp->b_addr;
871 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
872 entry = xfs_attr3_leaf_entryp(leaf);
874 bytes = sizeof(struct xfs_attr_sf_hdr);
875 for (i = 0; i < leafhdr.count; entry++, i++) {
876 if (entry->flags & XFS_ATTR_INCOMPLETE)
877 continue; /* don't copy partial entries */
878 if (!(entry->flags & XFS_ATTR_LOCAL))
879 return 0;
880 name_loc = xfs_attr3_leaf_name_local(leaf, i);
881 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
882 return 0;
883 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
884 return 0;
885 bytes += sizeof(struct xfs_attr_sf_entry) - 1
886 + name_loc->namelen
887 + be16_to_cpu(name_loc->valuelen);
889 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
890 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
891 (bytes == sizeof(struct xfs_attr_sf_hdr)))
892 return -1;
893 return xfs_attr_shortform_bytesfit(dp, bytes);
896 /* Verify the consistency of an inline attribute fork. */
897 xfs_failaddr_t
898 xfs_attr_shortform_verify(
899 struct xfs_inode *ip)
901 struct xfs_attr_shortform *sfp;
902 struct xfs_attr_sf_entry *sfep;
903 struct xfs_attr_sf_entry *next_sfep;
904 char *endp;
905 struct xfs_ifork *ifp;
906 int i;
907 int size;
909 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
910 ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
911 sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
912 size = ifp->if_bytes;
915 * Give up if the attribute is way too short.
917 if (size < sizeof(struct xfs_attr_sf_hdr))
918 return __this_address;
920 endp = (char *)sfp + size;
922 /* Check all reported entries */
923 sfep = &sfp->list[0];
924 for (i = 0; i < sfp->hdr.count; i++) {
926 * struct xfs_attr_sf_entry has a variable length.
927 * Check the fixed-offset parts of the structure are
928 * within the data buffer.
930 if (((char *)sfep + sizeof(*sfep)) >= endp)
931 return __this_address;
933 /* Don't allow names with known bad length. */
934 if (sfep->namelen == 0)
935 return __this_address;
938 * Check that the variable-length part of the structure is
939 * within the data buffer. The next entry starts after the
940 * name component, so nextentry is an acceptable test.
942 next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
943 if ((char *)next_sfep > endp)
944 return __this_address;
947 * Check for unknown flags. Short form doesn't support
948 * the incomplete or local bits, so we can use the namespace
949 * mask here.
951 if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
952 return __this_address;
955 * Check for invalid namespace combinations. We only allow
956 * one namespace flag per xattr, so we can just count the
957 * bits (i.e. hweight) here.
959 if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
960 return __this_address;
962 sfep = next_sfep;
964 if ((void *)sfep != (void *)endp)
965 return __this_address;
967 return NULL;
971 * Convert a leaf attribute list to shortform attribute list
974 xfs_attr3_leaf_to_shortform(
975 struct xfs_buf *bp,
976 struct xfs_da_args *args,
977 int forkoff)
979 struct xfs_attr_leafblock *leaf;
980 struct xfs_attr3_icleaf_hdr ichdr;
981 struct xfs_attr_leaf_entry *entry;
982 struct xfs_attr_leaf_name_local *name_loc;
983 struct xfs_da_args nargs;
984 struct xfs_inode *dp = args->dp;
985 char *tmpbuffer;
986 int error;
987 int i;
989 trace_xfs_attr_leaf_to_sf(args);
991 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
992 if (!tmpbuffer)
993 return -ENOMEM;
995 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
997 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
998 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
999 entry = xfs_attr3_leaf_entryp(leaf);
1001 /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1002 memset(bp->b_addr, 0, args->geo->blksize);
1005 * Clean out the prior contents of the attribute list.
1007 error = xfs_da_shrink_inode(args, 0, bp);
1008 if (error)
1009 goto out;
1011 if (forkoff == -1) {
1012 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
1013 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
1014 xfs_attr_fork_remove(dp, args->trans);
1015 goto out;
1018 xfs_attr_shortform_create(args);
1021 * Copy the attributes
1023 memset((char *)&nargs, 0, sizeof(nargs));
1024 nargs.geo = args->geo;
1025 nargs.dp = dp;
1026 nargs.total = args->total;
1027 nargs.whichfork = XFS_ATTR_FORK;
1028 nargs.trans = args->trans;
1029 nargs.op_flags = XFS_DA_OP_OKNOENT;
1031 for (i = 0; i < ichdr.count; entry++, i++) {
1032 if (entry->flags & XFS_ATTR_INCOMPLETE)
1033 continue; /* don't copy partial entries */
1034 if (!entry->nameidx)
1035 continue;
1036 ASSERT(entry->flags & XFS_ATTR_LOCAL);
1037 name_loc = xfs_attr3_leaf_name_local(leaf, i);
1038 nargs.name = name_loc->nameval;
1039 nargs.namelen = name_loc->namelen;
1040 nargs.value = &name_loc->nameval[nargs.namelen];
1041 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1042 nargs.hashval = be32_to_cpu(entry->hashval);
1043 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
1044 xfs_attr_shortform_add(&nargs, forkoff);
1046 error = 0;
1048 out:
1049 kmem_free(tmpbuffer);
1050 return error;
1054 * Convert from using a single leaf to a root node and a leaf.
1057 xfs_attr3_leaf_to_node(
1058 struct xfs_da_args *args)
1060 struct xfs_attr_leafblock *leaf;
1061 struct xfs_attr3_icleaf_hdr icleafhdr;
1062 struct xfs_attr_leaf_entry *entries;
1063 struct xfs_da_node_entry *btree;
1064 struct xfs_da3_icnode_hdr icnodehdr;
1065 struct xfs_da_intnode *node;
1066 struct xfs_inode *dp = args->dp;
1067 struct xfs_mount *mp = dp->i_mount;
1068 struct xfs_buf *bp1 = NULL;
1069 struct xfs_buf *bp2 = NULL;
1070 xfs_dablk_t blkno;
1071 int error;
1073 trace_xfs_attr_leaf_to_node(args);
1075 error = xfs_da_grow_inode(args, &blkno);
1076 if (error)
1077 goto out;
1078 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
1079 if (error)
1080 goto out;
1082 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
1083 if (error)
1084 goto out;
1086 /* copy leaf to new buffer, update identifiers */
1087 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
1088 bp2->b_ops = bp1->b_ops;
1089 memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
1090 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1091 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1092 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1094 xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
1097 * Set up the new root node.
1099 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
1100 if (error)
1101 goto out;
1102 node = bp1->b_addr;
1103 dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
1104 btree = dp->d_ops->node_tree_p(node);
1106 leaf = bp2->b_addr;
1107 xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
1108 entries = xfs_attr3_leaf_entryp(leaf);
1110 /* both on-disk, don't endian-flip twice */
1111 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
1112 btree[0].before = cpu_to_be32(blkno);
1113 icnodehdr.count = 1;
1114 dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
1115 xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
1116 error = 0;
1117 out:
1118 return error;
1121 /*========================================================================
1122 * Routines used for growing the Btree.
1123 *========================================================================*/
1126 * Create the initial contents of a leaf attribute list
1127 * or a leaf in a node attribute list.
1129 STATIC int
1130 xfs_attr3_leaf_create(
1131 struct xfs_da_args *args,
1132 xfs_dablk_t blkno,
1133 struct xfs_buf **bpp)
1135 struct xfs_attr_leafblock *leaf;
1136 struct xfs_attr3_icleaf_hdr ichdr;
1137 struct xfs_inode *dp = args->dp;
1138 struct xfs_mount *mp = dp->i_mount;
1139 struct xfs_buf *bp;
1140 int error;
1142 trace_xfs_attr_leaf_create(args);
1144 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1145 XFS_ATTR_FORK);
1146 if (error)
1147 return error;
1148 bp->b_ops = &xfs_attr3_leaf_buf_ops;
1149 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
1150 leaf = bp->b_addr;
1151 memset(leaf, 0, args->geo->blksize);
1153 memset(&ichdr, 0, sizeof(ichdr));
1154 ichdr.firstused = args->geo->blksize;
1156 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1157 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1159 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1161 hdr3->blkno = cpu_to_be64(bp->b_bn);
1162 hdr3->owner = cpu_to_be64(dp->i_ino);
1163 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
1165 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1166 } else {
1167 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1168 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
1170 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
1172 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1173 xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
1175 *bpp = bp;
1176 return 0;
1180 * Split the leaf node, rebalance, then add the new entry.
1183 xfs_attr3_leaf_split(
1184 struct xfs_da_state *state,
1185 struct xfs_da_state_blk *oldblk,
1186 struct xfs_da_state_blk *newblk)
1188 xfs_dablk_t blkno;
1189 int error;
1191 trace_xfs_attr_leaf_split(state->args);
1194 * Allocate space for a new leaf node.
1196 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1197 error = xfs_da_grow_inode(state->args, &blkno);
1198 if (error)
1199 return error;
1200 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
1201 if (error)
1202 return error;
1203 newblk->blkno = blkno;
1204 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1207 * Rebalance the entries across the two leaves.
1208 * NOTE: rebalance() currently depends on the 2nd block being empty.
1210 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
1211 error = xfs_da3_blk_link(state, oldblk, newblk);
1212 if (error)
1213 return error;
1216 * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1217 * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1218 * "new" attrs info. Will need the "old" info to remove it later.
1220 * Insert the "new" entry in the correct block.
1222 if (state->inleaf) {
1223 trace_xfs_attr_leaf_add_old(state->args);
1224 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
1225 } else {
1226 trace_xfs_attr_leaf_add_new(state->args);
1227 error = xfs_attr3_leaf_add(newblk->bp, state->args);
1231 * Update last hashval in each block since we added the name.
1233 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1234 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1235 return error;
1239 * Add a name to the leaf attribute list structure.
1242 xfs_attr3_leaf_add(
1243 struct xfs_buf *bp,
1244 struct xfs_da_args *args)
1246 struct xfs_attr_leafblock *leaf;
1247 struct xfs_attr3_icleaf_hdr ichdr;
1248 int tablesize;
1249 int entsize;
1250 int sum;
1251 int tmp;
1252 int i;
1254 trace_xfs_attr_leaf_add(args);
1256 leaf = bp->b_addr;
1257 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1258 ASSERT(args->index >= 0 && args->index <= ichdr.count);
1259 entsize = xfs_attr_leaf_newentsize(args, NULL);
1262 * Search through freemap for first-fit on new name length.
1263 * (may need to figure in size of entry struct too)
1265 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1266 + xfs_attr3_leaf_hdr_size(leaf);
1267 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1268 if (tablesize > ichdr.firstused) {
1269 sum += ichdr.freemap[i].size;
1270 continue;
1272 if (!ichdr.freemap[i].size)
1273 continue; /* no space in this map */
1274 tmp = entsize;
1275 if (ichdr.freemap[i].base < ichdr.firstused)
1276 tmp += sizeof(xfs_attr_leaf_entry_t);
1277 if (ichdr.freemap[i].size >= tmp) {
1278 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1279 goto out_log_hdr;
1281 sum += ichdr.freemap[i].size;
1285 * If there are no holes in the address space of the block,
1286 * and we don't have enough freespace, then compaction will do us
1287 * no good and we should just give up.
1289 if (!ichdr.holes && sum < entsize)
1290 return -ENOSPC;
1293 * Compact the entries to coalesce free space.
1294 * This may change the hdr->count via dropping INCOMPLETE entries.
1296 xfs_attr3_leaf_compact(args, &ichdr, bp);
1299 * After compaction, the block is guaranteed to have only one
1300 * free region, in freemap[0]. If it is not big enough, give up.
1302 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1303 tmp = -ENOSPC;
1304 goto out_log_hdr;
1307 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1309 out_log_hdr:
1310 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1311 xfs_trans_log_buf(args->trans, bp,
1312 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1313 xfs_attr3_leaf_hdr_size(leaf)));
1314 return tmp;
1318 * Add a name to a leaf attribute list structure.
1320 STATIC int
1321 xfs_attr3_leaf_add_work(
1322 struct xfs_buf *bp,
1323 struct xfs_attr3_icleaf_hdr *ichdr,
1324 struct xfs_da_args *args,
1325 int mapindex)
1327 struct xfs_attr_leafblock *leaf;
1328 struct xfs_attr_leaf_entry *entry;
1329 struct xfs_attr_leaf_name_local *name_loc;
1330 struct xfs_attr_leaf_name_remote *name_rmt;
1331 struct xfs_mount *mp;
1332 int tmp;
1333 int i;
1335 trace_xfs_attr_leaf_add_work(args);
1337 leaf = bp->b_addr;
1338 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1339 ASSERT(args->index >= 0 && args->index <= ichdr->count);
1342 * Force open some space in the entry array and fill it in.
1344 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1345 if (args->index < ichdr->count) {
1346 tmp = ichdr->count - args->index;
1347 tmp *= sizeof(xfs_attr_leaf_entry_t);
1348 memmove(entry + 1, entry, tmp);
1349 xfs_trans_log_buf(args->trans, bp,
1350 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1352 ichdr->count++;
1355 * Allocate space for the new string (at the end of the run).
1357 mp = args->trans->t_mountp;
1358 ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
1359 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1360 ASSERT(ichdr->freemap[mapindex].size >=
1361 xfs_attr_leaf_newentsize(args, NULL));
1362 ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
1363 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1365 ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
1367 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1368 ichdr->freemap[mapindex].size);
1369 entry->hashval = cpu_to_be32(args->hashval);
1370 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1371 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1372 if (args->op_flags & XFS_DA_OP_RENAME) {
1373 entry->flags |= XFS_ATTR_INCOMPLETE;
1374 if ((args->blkno2 == args->blkno) &&
1375 (args->index2 <= args->index)) {
1376 args->index2++;
1379 xfs_trans_log_buf(args->trans, bp,
1380 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1381 ASSERT((args->index == 0) ||
1382 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1383 ASSERT((args->index == ichdr->count - 1) ||
1384 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1387 * For "remote" attribute values, simply note that we need to
1388 * allocate space for the "remote" value. We can't actually
1389 * allocate the extents in this transaction, and we can't decide
1390 * which blocks they should be as we might allocate more blocks
1391 * as part of this transaction (a split operation for example).
1393 if (entry->flags & XFS_ATTR_LOCAL) {
1394 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
1395 name_loc->namelen = args->namelen;
1396 name_loc->valuelen = cpu_to_be16(args->valuelen);
1397 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1398 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1399 be16_to_cpu(name_loc->valuelen));
1400 } else {
1401 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
1402 name_rmt->namelen = args->namelen;
1403 memcpy((char *)name_rmt->name, args->name, args->namelen);
1404 entry->flags |= XFS_ATTR_INCOMPLETE;
1405 /* just in case */
1406 name_rmt->valuelen = 0;
1407 name_rmt->valueblk = 0;
1408 args->rmtblkno = 1;
1409 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1410 args->rmtvaluelen = args->valuelen;
1412 xfs_trans_log_buf(args->trans, bp,
1413 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
1414 xfs_attr_leaf_entsize(leaf, args->index)));
1417 * Update the control info for this leaf node
1419 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1420 ichdr->firstused = be16_to_cpu(entry->nameidx);
1422 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1423 + xfs_attr3_leaf_hdr_size(leaf));
1424 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1425 + xfs_attr3_leaf_hdr_size(leaf);
1427 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1428 if (ichdr->freemap[i].base == tmp) {
1429 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1430 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
1433 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1434 return 0;
1438 * Garbage collect a leaf attribute list block by copying it to a new buffer.
1440 STATIC void
1441 xfs_attr3_leaf_compact(
1442 struct xfs_da_args *args,
1443 struct xfs_attr3_icleaf_hdr *ichdr_dst,
1444 struct xfs_buf *bp)
1446 struct xfs_attr_leafblock *leaf_src;
1447 struct xfs_attr_leafblock *leaf_dst;
1448 struct xfs_attr3_icleaf_hdr ichdr_src;
1449 struct xfs_trans *trans = args->trans;
1450 char *tmpbuffer;
1452 trace_xfs_attr_leaf_compact(args);
1454 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
1455 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1456 memset(bp->b_addr, 0, args->geo->blksize);
1457 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1458 leaf_dst = bp->b_addr;
1461 * Copy the on-disk header back into the destination buffer to ensure
1462 * all the information in the header that is not part of the incore
1463 * header structure is preserved.
1465 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1467 /* Initialise the incore headers */
1468 ichdr_src = *ichdr_dst; /* struct copy */
1469 ichdr_dst->firstused = args->geo->blksize;
1470 ichdr_dst->usedbytes = 0;
1471 ichdr_dst->count = 0;
1472 ichdr_dst->holes = 0;
1473 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1474 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1475 ichdr_dst->freemap[0].base;
1477 /* write the header back to initialise the underlying buffer */
1478 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
1481 * Copy all entry's in the same (sorted) order,
1482 * but allocate name/value pairs packed and in sequence.
1484 xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1485 leaf_dst, ichdr_dst, 0, ichdr_src.count);
1487 * this logs the entire buffer, but the caller must write the header
1488 * back to the buffer when it is finished modifying it.
1490 xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
1492 kmem_free(tmpbuffer);
1496 * Compare two leaf blocks "order".
1497 * Return 0 unless leaf2 should go before leaf1.
1499 static int
1500 xfs_attr3_leaf_order(
1501 struct xfs_buf *leaf1_bp,
1502 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1503 struct xfs_buf *leaf2_bp,
1504 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1506 struct xfs_attr_leaf_entry *entries1;
1507 struct xfs_attr_leaf_entry *entries2;
1509 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1510 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1511 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1512 ((be32_to_cpu(entries2[0].hashval) <
1513 be32_to_cpu(entries1[0].hashval)) ||
1514 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1515 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1516 return 1;
1518 return 0;
1522 xfs_attr_leaf_order(
1523 struct xfs_buf *leaf1_bp,
1524 struct xfs_buf *leaf2_bp)
1526 struct xfs_attr3_icleaf_hdr ichdr1;
1527 struct xfs_attr3_icleaf_hdr ichdr2;
1528 struct xfs_mount *mp = leaf1_bp->b_target->bt_mount;
1530 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1531 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
1532 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1536 * Redistribute the attribute list entries between two leaf nodes,
1537 * taking into account the size of the new entry.
1539 * NOTE: if new block is empty, then it will get the upper half of the
1540 * old block. At present, all (one) callers pass in an empty second block.
1542 * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1543 * to match what it is doing in splitting the attribute leaf block. Those
1544 * values are used in "atomic rename" operations on attributes. Note that
1545 * the "new" and "old" values can end up in different blocks.
1547 STATIC void
1548 xfs_attr3_leaf_rebalance(
1549 struct xfs_da_state *state,
1550 struct xfs_da_state_blk *blk1,
1551 struct xfs_da_state_blk *blk2)
1553 struct xfs_da_args *args;
1554 struct xfs_attr_leafblock *leaf1;
1555 struct xfs_attr_leafblock *leaf2;
1556 struct xfs_attr3_icleaf_hdr ichdr1;
1557 struct xfs_attr3_icleaf_hdr ichdr2;
1558 struct xfs_attr_leaf_entry *entries1;
1559 struct xfs_attr_leaf_entry *entries2;
1560 int count;
1561 int totallen;
1562 int max;
1563 int space;
1564 int swap;
1567 * Set up environment.
1569 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1570 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1571 leaf1 = blk1->bp->b_addr;
1572 leaf2 = blk2->bp->b_addr;
1573 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1574 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
1575 ASSERT(ichdr2.count == 0);
1576 args = state->args;
1578 trace_xfs_attr_leaf_rebalance(args);
1581 * Check ordering of blocks, reverse if it makes things simpler.
1583 * NOTE: Given that all (current) callers pass in an empty
1584 * second block, this code should never set "swap".
1586 swap = 0;
1587 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1588 swap(blk1, blk2);
1590 /* swap structures rather than reconverting them */
1591 swap(ichdr1, ichdr2);
1593 leaf1 = blk1->bp->b_addr;
1594 leaf2 = blk2->bp->b_addr;
1595 swap = 1;
1599 * Examine entries until we reduce the absolute difference in
1600 * byte usage between the two blocks to a minimum. Then get
1601 * the direction to copy and the number of elements to move.
1603 * "inleaf" is true if the new entry should be inserted into blk1.
1604 * If "swap" is also true, then reverse the sense of "inleaf".
1606 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1607 blk2, &ichdr2,
1608 &count, &totallen);
1609 if (swap)
1610 state->inleaf = !state->inleaf;
1613 * Move any entries required from leaf to leaf:
1615 if (count < ichdr1.count) {
1617 * Figure the total bytes to be added to the destination leaf.
1619 /* number entries being moved */
1620 count = ichdr1.count - count;
1621 space = ichdr1.usedbytes - totallen;
1622 space += count * sizeof(xfs_attr_leaf_entry_t);
1625 * leaf2 is the destination, compact it if it looks tight.
1627 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1628 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
1629 if (space > max)
1630 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
1633 * Move high entries from leaf1 to low end of leaf2.
1635 xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1636 ichdr1.count - count, leaf2, &ichdr2, 0, count);
1638 } else if (count > ichdr1.count) {
1640 * I assert that since all callers pass in an empty
1641 * second buffer, this code should never execute.
1643 ASSERT(0);
1646 * Figure the total bytes to be added to the destination leaf.
1648 /* number entries being moved */
1649 count -= ichdr1.count;
1650 space = totallen - ichdr1.usedbytes;
1651 space += count * sizeof(xfs_attr_leaf_entry_t);
1654 * leaf1 is the destination, compact it if it looks tight.
1656 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1657 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
1658 if (space > max)
1659 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
1662 * Move low entries from leaf2 to high end of leaf1.
1664 xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1665 ichdr1.count, count);
1668 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1669 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
1670 xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1671 xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
1674 * Copy out last hashval in each block for B-tree code.
1676 entries1 = xfs_attr3_leaf_entryp(leaf1);
1677 entries2 = xfs_attr3_leaf_entryp(leaf2);
1678 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1679 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
1682 * Adjust the expected index for insertion.
1683 * NOTE: this code depends on the (current) situation that the
1684 * second block was originally empty.
1686 * If the insertion point moved to the 2nd block, we must adjust
1687 * the index. We must also track the entry just following the
1688 * new entry for use in an "atomic rename" operation, that entry
1689 * is always the "old" entry and the "new" entry is what we are
1690 * inserting. The index/blkno fields refer to the "old" entry,
1691 * while the index2/blkno2 fields refer to the "new" entry.
1693 if (blk1->index > ichdr1.count) {
1694 ASSERT(state->inleaf == 0);
1695 blk2->index = blk1->index - ichdr1.count;
1696 args->index = args->index2 = blk2->index;
1697 args->blkno = args->blkno2 = blk2->blkno;
1698 } else if (blk1->index == ichdr1.count) {
1699 if (state->inleaf) {
1700 args->index = blk1->index;
1701 args->blkno = blk1->blkno;
1702 args->index2 = 0;
1703 args->blkno2 = blk2->blkno;
1704 } else {
1706 * On a double leaf split, the original attr location
1707 * is already stored in blkno2/index2, so don't
1708 * overwrite it overwise we corrupt the tree.
1710 blk2->index = blk1->index - ichdr1.count;
1711 args->index = blk2->index;
1712 args->blkno = blk2->blkno;
1713 if (!state->extravalid) {
1715 * set the new attr location to match the old
1716 * one and let the higher level split code
1717 * decide where in the leaf to place it.
1719 args->index2 = blk2->index;
1720 args->blkno2 = blk2->blkno;
1723 } else {
1724 ASSERT(state->inleaf == 1);
1725 args->index = args->index2 = blk1->index;
1726 args->blkno = args->blkno2 = blk1->blkno;
1731 * Examine entries until we reduce the absolute difference in
1732 * byte usage between the two blocks to a minimum.
1733 * GROT: Is this really necessary? With other than a 512 byte blocksize,
1734 * GROT: there will always be enough room in either block for a new entry.
1735 * GROT: Do a double-split for this case?
1737 STATIC int
1738 xfs_attr3_leaf_figure_balance(
1739 struct xfs_da_state *state,
1740 struct xfs_da_state_blk *blk1,
1741 struct xfs_attr3_icleaf_hdr *ichdr1,
1742 struct xfs_da_state_blk *blk2,
1743 struct xfs_attr3_icleaf_hdr *ichdr2,
1744 int *countarg,
1745 int *usedbytesarg)
1747 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1748 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1749 struct xfs_attr_leaf_entry *entry;
1750 int count;
1751 int max;
1752 int index;
1753 int totallen = 0;
1754 int half;
1755 int lastdelta;
1756 int foundit = 0;
1757 int tmp;
1760 * Examine entries until we reduce the absolute difference in
1761 * byte usage between the two blocks to a minimum.
1763 max = ichdr1->count + ichdr2->count;
1764 half = (max + 1) * sizeof(*entry);
1765 half += ichdr1->usedbytes + ichdr2->usedbytes +
1766 xfs_attr_leaf_newentsize(state->args, NULL);
1767 half /= 2;
1768 lastdelta = state->args->geo->blksize;
1769 entry = xfs_attr3_leaf_entryp(leaf1);
1770 for (count = index = 0; count < max; entry++, index++, count++) {
1772 #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1774 * The new entry is in the first block, account for it.
1776 if (count == blk1->index) {
1777 tmp = totallen + sizeof(*entry) +
1778 xfs_attr_leaf_newentsize(state->args, NULL);
1779 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1780 break;
1781 lastdelta = XFS_ATTR_ABS(half - tmp);
1782 totallen = tmp;
1783 foundit = 1;
1787 * Wrap around into the second block if necessary.
1789 if (count == ichdr1->count) {
1790 leaf1 = leaf2;
1791 entry = xfs_attr3_leaf_entryp(leaf1);
1792 index = 0;
1796 * Figure out if next leaf entry would be too much.
1798 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1799 index);
1800 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1801 break;
1802 lastdelta = XFS_ATTR_ABS(half - tmp);
1803 totallen = tmp;
1804 #undef XFS_ATTR_ABS
1808 * Calculate the number of usedbytes that will end up in lower block.
1809 * If new entry not in lower block, fix up the count.
1811 totallen -= count * sizeof(*entry);
1812 if (foundit) {
1813 totallen -= sizeof(*entry) +
1814 xfs_attr_leaf_newentsize(state->args, NULL);
1817 *countarg = count;
1818 *usedbytesarg = totallen;
1819 return foundit;
1822 /*========================================================================
1823 * Routines used for shrinking the Btree.
1824 *========================================================================*/
1827 * Check a leaf block and its neighbors to see if the block should be
1828 * collapsed into one or the other neighbor. Always keep the block
1829 * with the smaller block number.
1830 * If the current block is over 50% full, don't try to join it, return 0.
1831 * If the block is empty, fill in the state structure and return 2.
1832 * If it can be collapsed, fill in the state structure and return 1.
1833 * If nothing can be done, return 0.
1835 * GROT: allow for INCOMPLETE entries in calculation.
1838 xfs_attr3_leaf_toosmall(
1839 struct xfs_da_state *state,
1840 int *action)
1842 struct xfs_attr_leafblock *leaf;
1843 struct xfs_da_state_blk *blk;
1844 struct xfs_attr3_icleaf_hdr ichdr;
1845 struct xfs_buf *bp;
1846 xfs_dablk_t blkno;
1847 int bytes;
1848 int forward;
1849 int error;
1850 int retval;
1851 int i;
1853 trace_xfs_attr_leaf_toosmall(state->args);
1856 * Check for the degenerate case of the block being over 50% full.
1857 * If so, it's not worth even looking to see if we might be able
1858 * to coalesce with a sibling.
1860 blk = &state->path.blk[ state->path.active-1 ];
1861 leaf = blk->bp->b_addr;
1862 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
1863 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1864 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1865 ichdr.usedbytes;
1866 if (bytes > (state->args->geo->blksize >> 1)) {
1867 *action = 0; /* blk over 50%, don't try to join */
1868 return 0;
1872 * Check for the degenerate case of the block being empty.
1873 * If the block is empty, we'll simply delete it, no need to
1874 * coalesce it with a sibling block. We choose (arbitrarily)
1875 * to merge with the forward block unless it is NULL.
1877 if (ichdr.count == 0) {
1879 * Make altpath point to the block we want to keep and
1880 * path point to the block we want to drop (this one).
1882 forward = (ichdr.forw != 0);
1883 memcpy(&state->altpath, &state->path, sizeof(state->path));
1884 error = xfs_da3_path_shift(state, &state->altpath, forward,
1885 0, &retval);
1886 if (error)
1887 return error;
1888 if (retval) {
1889 *action = 0;
1890 } else {
1891 *action = 2;
1893 return 0;
1897 * Examine each sibling block to see if we can coalesce with
1898 * at least 25% free space to spare. We need to figure out
1899 * whether to merge with the forward or the backward block.
1900 * We prefer coalescing with the lower numbered sibling so as
1901 * to shrink an attribute list over time.
1903 /* start with smaller blk num */
1904 forward = ichdr.forw < ichdr.back;
1905 for (i = 0; i < 2; forward = !forward, i++) {
1906 struct xfs_attr3_icleaf_hdr ichdr2;
1907 if (forward)
1908 blkno = ichdr.forw;
1909 else
1910 blkno = ichdr.back;
1911 if (blkno == 0)
1912 continue;
1913 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
1914 blkno, -1, &bp);
1915 if (error)
1916 return error;
1918 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
1920 bytes = state->args->geo->blksize -
1921 (state->args->geo->blksize >> 2) -
1922 ichdr.usedbytes - ichdr2.usedbytes -
1923 ((ichdr.count + ichdr2.count) *
1924 sizeof(xfs_attr_leaf_entry_t)) -
1925 xfs_attr3_leaf_hdr_size(leaf);
1927 xfs_trans_brelse(state->args->trans, bp);
1928 if (bytes >= 0)
1929 break; /* fits with at least 25% to spare */
1931 if (i >= 2) {
1932 *action = 0;
1933 return 0;
1937 * Make altpath point to the block we want to keep (the lower
1938 * numbered block) and path point to the block we want to drop.
1940 memcpy(&state->altpath, &state->path, sizeof(state->path));
1941 if (blkno < blk->blkno) {
1942 error = xfs_da3_path_shift(state, &state->altpath, forward,
1943 0, &retval);
1944 } else {
1945 error = xfs_da3_path_shift(state, &state->path, forward,
1946 0, &retval);
1948 if (error)
1949 return error;
1950 if (retval) {
1951 *action = 0;
1952 } else {
1953 *action = 1;
1955 return 0;
1959 * Remove a name from the leaf attribute list structure.
1961 * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1962 * If two leaves are 37% full, when combined they will leave 25% free.
1965 xfs_attr3_leaf_remove(
1966 struct xfs_buf *bp,
1967 struct xfs_da_args *args)
1969 struct xfs_attr_leafblock *leaf;
1970 struct xfs_attr3_icleaf_hdr ichdr;
1971 struct xfs_attr_leaf_entry *entry;
1972 int before;
1973 int after;
1974 int smallest;
1975 int entsize;
1976 int tablesize;
1977 int tmp;
1978 int i;
1980 trace_xfs_attr_leaf_remove(args);
1982 leaf = bp->b_addr;
1983 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1985 ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
1986 ASSERT(args->index >= 0 && args->index < ichdr.count);
1987 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1988 xfs_attr3_leaf_hdr_size(leaf));
1990 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1992 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
1993 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
1996 * Scan through free region table:
1997 * check for adjacency of free'd entry with an existing one,
1998 * find smallest free region in case we need to replace it,
1999 * adjust any map that borders the entry table,
2001 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2002 + xfs_attr3_leaf_hdr_size(leaf);
2003 tmp = ichdr.freemap[0].size;
2004 before = after = -1;
2005 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2006 entsize = xfs_attr_leaf_entsize(leaf, args->index);
2007 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2008 ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2009 ASSERT(ichdr.freemap[i].size < args->geo->blksize);
2010 if (ichdr.freemap[i].base == tablesize) {
2011 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2012 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
2015 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2016 be16_to_cpu(entry->nameidx)) {
2017 before = i;
2018 } else if (ichdr.freemap[i].base ==
2019 (be16_to_cpu(entry->nameidx) + entsize)) {
2020 after = i;
2021 } else if (ichdr.freemap[i].size < tmp) {
2022 tmp = ichdr.freemap[i].size;
2023 smallest = i;
2028 * Coalesce adjacent freemap regions,
2029 * or replace the smallest region.
2031 if ((before >= 0) || (after >= 0)) {
2032 if ((before >= 0) && (after >= 0)) {
2033 ichdr.freemap[before].size += entsize;
2034 ichdr.freemap[before].size += ichdr.freemap[after].size;
2035 ichdr.freemap[after].base = 0;
2036 ichdr.freemap[after].size = 0;
2037 } else if (before >= 0) {
2038 ichdr.freemap[before].size += entsize;
2039 } else {
2040 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2041 ichdr.freemap[after].size += entsize;
2043 } else {
2045 * Replace smallest region (if it is smaller than free'd entry)
2047 if (ichdr.freemap[smallest].size < entsize) {
2048 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2049 ichdr.freemap[smallest].size = entsize;
2054 * Did we remove the first entry?
2056 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
2057 smallest = 1;
2058 else
2059 smallest = 0;
2062 * Compress the remaining entries and zero out the removed stuff.
2064 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2065 ichdr.usedbytes -= entsize;
2066 xfs_trans_log_buf(args->trans, bp,
2067 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
2068 entsize));
2070 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2071 memmove(entry, entry + 1, tmp);
2072 ichdr.count--;
2073 xfs_trans_log_buf(args->trans, bp,
2074 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2076 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2077 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
2080 * If we removed the first entry, re-find the first used byte
2081 * in the name area. Note that if the entry was the "firstused",
2082 * then we don't have a "hole" in our block resulting from
2083 * removing the name.
2085 if (smallest) {
2086 tmp = args->geo->blksize;
2087 entry = xfs_attr3_leaf_entryp(leaf);
2088 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2089 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2090 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2092 if (be16_to_cpu(entry->nameidx) < tmp)
2093 tmp = be16_to_cpu(entry->nameidx);
2095 ichdr.firstused = tmp;
2096 ASSERT(ichdr.firstused != 0);
2097 } else {
2098 ichdr.holes = 1; /* mark as needing compaction */
2100 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
2101 xfs_trans_log_buf(args->trans, bp,
2102 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2103 xfs_attr3_leaf_hdr_size(leaf)));
2106 * Check if leaf is less than 50% full, caller may want to
2107 * "join" the leaf with a sibling if so.
2109 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2110 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2112 return tmp < args->geo->magicpct; /* leaf is < 37% full */
2116 * Move all the attribute list entries from drop_leaf into save_leaf.
2118 void
2119 xfs_attr3_leaf_unbalance(
2120 struct xfs_da_state *state,
2121 struct xfs_da_state_blk *drop_blk,
2122 struct xfs_da_state_blk *save_blk)
2124 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2125 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2126 struct xfs_attr3_icleaf_hdr drophdr;
2127 struct xfs_attr3_icleaf_hdr savehdr;
2128 struct xfs_attr_leaf_entry *entry;
2130 trace_xfs_attr_leaf_unbalance(state->args);
2132 drop_leaf = drop_blk->bp->b_addr;
2133 save_leaf = save_blk->bp->b_addr;
2134 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2135 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
2136 entry = xfs_attr3_leaf_entryp(drop_leaf);
2139 * Save last hashval from dying block for later Btree fixup.
2141 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
2144 * Check if we need a temp buffer, or can we do it in place.
2145 * Note that we don't check "leaf" for holes because we will
2146 * always be dropping it, toosmall() decided that for us already.
2148 if (savehdr.holes == 0) {
2150 * dest leaf has no holes, so we add there. May need
2151 * to make some room in the entry array.
2153 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2154 drop_blk->bp, &drophdr)) {
2155 xfs_attr3_leaf_moveents(state->args,
2156 drop_leaf, &drophdr, 0,
2157 save_leaf, &savehdr, 0,
2158 drophdr.count);
2159 } else {
2160 xfs_attr3_leaf_moveents(state->args,
2161 drop_leaf, &drophdr, 0,
2162 save_leaf, &savehdr,
2163 savehdr.count, drophdr.count);
2165 } else {
2167 * Destination has holes, so we make a temporary copy
2168 * of the leaf and add them both to that.
2170 struct xfs_attr_leafblock *tmp_leaf;
2171 struct xfs_attr3_icleaf_hdr tmphdr;
2173 tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP);
2176 * Copy the header into the temp leaf so that all the stuff
2177 * not in the incore header is present and gets copied back in
2178 * once we've moved all the entries.
2180 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2182 memset(&tmphdr, 0, sizeof(tmphdr));
2183 tmphdr.magic = savehdr.magic;
2184 tmphdr.forw = savehdr.forw;
2185 tmphdr.back = savehdr.back;
2186 tmphdr.firstused = state->args->geo->blksize;
2188 /* write the header to the temp buffer to initialise it */
2189 xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
2191 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2192 drop_blk->bp, &drophdr)) {
2193 xfs_attr3_leaf_moveents(state->args,
2194 drop_leaf, &drophdr, 0,
2195 tmp_leaf, &tmphdr, 0,
2196 drophdr.count);
2197 xfs_attr3_leaf_moveents(state->args,
2198 save_leaf, &savehdr, 0,
2199 tmp_leaf, &tmphdr, tmphdr.count,
2200 savehdr.count);
2201 } else {
2202 xfs_attr3_leaf_moveents(state->args,
2203 save_leaf, &savehdr, 0,
2204 tmp_leaf, &tmphdr, 0,
2205 savehdr.count);
2206 xfs_attr3_leaf_moveents(state->args,
2207 drop_leaf, &drophdr, 0,
2208 tmp_leaf, &tmphdr, tmphdr.count,
2209 drophdr.count);
2211 memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
2212 savehdr = tmphdr; /* struct copy */
2213 kmem_free(tmp_leaf);
2216 xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
2217 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
2218 state->args->geo->blksize - 1);
2221 * Copy out last hashval in each block for B-tree code.
2223 entry = xfs_attr3_leaf_entryp(save_leaf);
2224 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
2227 /*========================================================================
2228 * Routines used for finding things in the Btree.
2229 *========================================================================*/
2232 * Look up a name in a leaf attribute list structure.
2233 * This is the internal routine, it uses the caller's buffer.
2235 * Note that duplicate keys are allowed, but only check within the
2236 * current leaf node. The Btree code must check in adjacent leaf nodes.
2238 * Return in args->index the index into the entry[] array of either
2239 * the found entry, or where the entry should have been (insert before
2240 * that entry).
2242 * Don't change the args->value unless we find the attribute.
2245 xfs_attr3_leaf_lookup_int(
2246 struct xfs_buf *bp,
2247 struct xfs_da_args *args)
2249 struct xfs_attr_leafblock *leaf;
2250 struct xfs_attr3_icleaf_hdr ichdr;
2251 struct xfs_attr_leaf_entry *entry;
2252 struct xfs_attr_leaf_entry *entries;
2253 struct xfs_attr_leaf_name_local *name_loc;
2254 struct xfs_attr_leaf_name_remote *name_rmt;
2255 xfs_dahash_t hashval;
2256 int probe;
2257 int span;
2259 trace_xfs_attr_leaf_lookup(args);
2261 leaf = bp->b_addr;
2262 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2263 entries = xfs_attr3_leaf_entryp(leaf);
2264 if (ichdr.count >= args->geo->blksize / 8)
2265 return -EFSCORRUPTED;
2268 * Binary search. (note: small blocks will skip this loop)
2270 hashval = args->hashval;
2271 probe = span = ichdr.count / 2;
2272 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
2273 span /= 2;
2274 if (be32_to_cpu(entry->hashval) < hashval)
2275 probe += span;
2276 else if (be32_to_cpu(entry->hashval) > hashval)
2277 probe -= span;
2278 else
2279 break;
2281 if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
2282 return -EFSCORRUPTED;
2283 if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
2284 return -EFSCORRUPTED;
2287 * Since we may have duplicate hashval's, find the first matching
2288 * hashval in the leaf.
2290 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
2291 entry--;
2292 probe--;
2294 while (probe < ichdr.count &&
2295 be32_to_cpu(entry->hashval) < hashval) {
2296 entry++;
2297 probe++;
2299 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
2300 args->index = probe;
2301 return -ENOATTR;
2305 * Duplicate keys may be present, so search all of them for a match.
2307 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
2308 entry++, probe++) {
2310 * GROT: Add code to remove incomplete entries.
2313 * If we are looking for INCOMPLETE entries, show only those.
2314 * If we are looking for complete entries, show only those.
2316 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2317 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2318 continue;
2320 if (entry->flags & XFS_ATTR_LOCAL) {
2321 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
2322 if (name_loc->namelen != args->namelen)
2323 continue;
2324 if (memcmp(args->name, name_loc->nameval,
2325 args->namelen) != 0)
2326 continue;
2327 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2328 continue;
2329 args->index = probe;
2330 return -EEXIST;
2331 } else {
2332 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
2333 if (name_rmt->namelen != args->namelen)
2334 continue;
2335 if (memcmp(args->name, name_rmt->name,
2336 args->namelen) != 0)
2337 continue;
2338 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2339 continue;
2340 args->index = probe;
2341 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2342 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2343 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2344 args->dp->i_mount,
2345 args->rmtvaluelen);
2346 return -EEXIST;
2349 args->index = probe;
2350 return -ENOATTR;
2354 * Get the value associated with an attribute name from a leaf attribute
2355 * list structure.
2358 xfs_attr3_leaf_getvalue(
2359 struct xfs_buf *bp,
2360 struct xfs_da_args *args)
2362 struct xfs_attr_leafblock *leaf;
2363 struct xfs_attr3_icleaf_hdr ichdr;
2364 struct xfs_attr_leaf_entry *entry;
2365 struct xfs_attr_leaf_name_local *name_loc;
2366 struct xfs_attr_leaf_name_remote *name_rmt;
2367 int valuelen;
2369 leaf = bp->b_addr;
2370 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2371 ASSERT(ichdr.count < args->geo->blksize / 8);
2372 ASSERT(args->index < ichdr.count);
2374 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2375 if (entry->flags & XFS_ATTR_LOCAL) {
2376 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2377 ASSERT(name_loc->namelen == args->namelen);
2378 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2379 valuelen = be16_to_cpu(name_loc->valuelen);
2380 if (args->flags & ATTR_KERNOVAL) {
2381 args->valuelen = valuelen;
2382 return 0;
2384 if (args->valuelen < valuelen) {
2385 args->valuelen = valuelen;
2386 return -ERANGE;
2388 args->valuelen = valuelen;
2389 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2390 } else {
2391 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2392 ASSERT(name_rmt->namelen == args->namelen);
2393 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2394 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2395 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2396 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2397 args->rmtvaluelen);
2398 if (args->flags & ATTR_KERNOVAL) {
2399 args->valuelen = args->rmtvaluelen;
2400 return 0;
2402 if (args->valuelen < args->rmtvaluelen) {
2403 args->valuelen = args->rmtvaluelen;
2404 return -ERANGE;
2406 args->valuelen = args->rmtvaluelen;
2408 return 0;
2411 /*========================================================================
2412 * Utility routines.
2413 *========================================================================*/
2416 * Move the indicated entries from one leaf to another.
2417 * NOTE: this routine modifies both source and destination leaves.
2419 /*ARGSUSED*/
2420 STATIC void
2421 xfs_attr3_leaf_moveents(
2422 struct xfs_da_args *args,
2423 struct xfs_attr_leafblock *leaf_s,
2424 struct xfs_attr3_icleaf_hdr *ichdr_s,
2425 int start_s,
2426 struct xfs_attr_leafblock *leaf_d,
2427 struct xfs_attr3_icleaf_hdr *ichdr_d,
2428 int start_d,
2429 int count)
2431 struct xfs_attr_leaf_entry *entry_s;
2432 struct xfs_attr_leaf_entry *entry_d;
2433 int desti;
2434 int tmp;
2435 int i;
2438 * Check for nothing to do.
2440 if (count == 0)
2441 return;
2444 * Set up environment.
2446 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2447 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2448 ASSERT(ichdr_s->magic == ichdr_d->magic);
2449 ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
2450 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2451 + xfs_attr3_leaf_hdr_size(leaf_s));
2452 ASSERT(ichdr_d->count < args->geo->blksize / 8);
2453 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2454 + xfs_attr3_leaf_hdr_size(leaf_d));
2456 ASSERT(start_s < ichdr_s->count);
2457 ASSERT(start_d <= ichdr_d->count);
2458 ASSERT(count <= ichdr_s->count);
2462 * Move the entries in the destination leaf up to make a hole?
2464 if (start_d < ichdr_d->count) {
2465 tmp = ichdr_d->count - start_d;
2466 tmp *= sizeof(xfs_attr_leaf_entry_t);
2467 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2468 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2469 memmove(entry_d, entry_s, tmp);
2473 * Copy all entry's in the same (sorted) order,
2474 * but allocate attribute info packed and in sequence.
2476 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2477 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2478 desti = start_d;
2479 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2480 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
2481 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2482 #ifdef GROT
2484 * Code to drop INCOMPLETE entries. Difficult to use as we
2485 * may also need to change the insertion index. Code turned
2486 * off for 6.2, should be revisited later.
2488 if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2489 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2490 ichdr_s->usedbytes -= tmp;
2491 ichdr_s->count -= 1;
2492 entry_d--; /* to compensate for ++ in loop hdr */
2493 desti--;
2494 if ((start_s + i) < offset)
2495 result++; /* insertion index adjustment */
2496 } else {
2497 #endif /* GROT */
2498 ichdr_d->firstused -= tmp;
2499 /* both on-disk, don't endian flip twice */
2500 entry_d->hashval = entry_s->hashval;
2501 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
2502 entry_d->flags = entry_s->flags;
2503 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2504 <= args->geo->blksize);
2505 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2506 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
2507 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2508 <= args->geo->blksize);
2509 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2510 ichdr_s->usedbytes -= tmp;
2511 ichdr_d->usedbytes += tmp;
2512 ichdr_s->count -= 1;
2513 ichdr_d->count += 1;
2514 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2515 + xfs_attr3_leaf_hdr_size(leaf_d);
2516 ASSERT(ichdr_d->firstused >= tmp);
2517 #ifdef GROT
2519 #endif /* GROT */
2523 * Zero out the entries we just copied.
2525 if (start_s == ichdr_s->count) {
2526 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2527 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2528 ASSERT(((char *)entry_s + tmp) <=
2529 ((char *)leaf_s + args->geo->blksize));
2530 memset(entry_s, 0, tmp);
2531 } else {
2533 * Move the remaining entries down to fill the hole,
2534 * then zero the entries at the top.
2536 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2537 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2538 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2539 memmove(entry_d, entry_s, tmp);
2541 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2542 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
2543 ASSERT(((char *)entry_s + tmp) <=
2544 ((char *)leaf_s + args->geo->blksize));
2545 memset(entry_s, 0, tmp);
2549 * Fill in the freemap information
2551 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2552 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2553 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2554 ichdr_d->freemap[1].base = 0;
2555 ichdr_d->freemap[2].base = 0;
2556 ichdr_d->freemap[1].size = 0;
2557 ichdr_d->freemap[2].size = 0;
2558 ichdr_s->holes = 1; /* leaf may not be compact */
2562 * Pick up the last hashvalue from a leaf block.
2564 xfs_dahash_t
2565 xfs_attr_leaf_lasthash(
2566 struct xfs_buf *bp,
2567 int *count)
2569 struct xfs_attr3_icleaf_hdr ichdr;
2570 struct xfs_attr_leaf_entry *entries;
2571 struct xfs_mount *mp = bp->b_target->bt_mount;
2573 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
2574 entries = xfs_attr3_leaf_entryp(bp->b_addr);
2575 if (count)
2576 *count = ichdr.count;
2577 if (!ichdr.count)
2578 return 0;
2579 return be32_to_cpu(entries[ichdr.count - 1].hashval);
2583 * Calculate the number of bytes used to store the indicated attribute
2584 * (whether local or remote only calculate bytes in this block).
2586 STATIC int
2587 xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2589 struct xfs_attr_leaf_entry *entries;
2590 xfs_attr_leaf_name_local_t *name_loc;
2591 xfs_attr_leaf_name_remote_t *name_rmt;
2592 int size;
2594 entries = xfs_attr3_leaf_entryp(leaf);
2595 if (entries[index].flags & XFS_ATTR_LOCAL) {
2596 name_loc = xfs_attr3_leaf_name_local(leaf, index);
2597 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2598 be16_to_cpu(name_loc->valuelen));
2599 } else {
2600 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
2601 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2603 return size;
2607 * Calculate the number of bytes that would be required to store the new
2608 * attribute (whether local or remote only calculate bytes in this block).
2609 * This routine decides as a side effect whether the attribute will be
2610 * a "local" or a "remote" attribute.
2613 xfs_attr_leaf_newentsize(
2614 struct xfs_da_args *args,
2615 int *local)
2617 int size;
2619 size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2620 if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2621 if (local)
2622 *local = 1;
2623 return size;
2625 if (local)
2626 *local = 0;
2627 return xfs_attr_leaf_entsize_remote(args->namelen);
2631 /*========================================================================
2632 * Manage the INCOMPLETE flag in a leaf entry
2633 *========================================================================*/
2636 * Clear the INCOMPLETE flag on an entry in a leaf block.
2639 xfs_attr3_leaf_clearflag(
2640 struct xfs_da_args *args)
2642 struct xfs_attr_leafblock *leaf;
2643 struct xfs_attr_leaf_entry *entry;
2644 struct xfs_attr_leaf_name_remote *name_rmt;
2645 struct xfs_buf *bp;
2646 int error;
2647 #ifdef DEBUG
2648 struct xfs_attr3_icleaf_hdr ichdr;
2649 xfs_attr_leaf_name_local_t *name_loc;
2650 int namelen;
2651 char *name;
2652 #endif /* DEBUG */
2654 trace_xfs_attr_leaf_clearflag(args);
2656 * Set up the operation.
2658 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2659 if (error)
2660 return error;
2662 leaf = bp->b_addr;
2663 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2664 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2666 #ifdef DEBUG
2667 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2668 ASSERT(args->index < ichdr.count);
2669 ASSERT(args->index >= 0);
2671 if (entry->flags & XFS_ATTR_LOCAL) {
2672 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2673 namelen = name_loc->namelen;
2674 name = (char *)name_loc->nameval;
2675 } else {
2676 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2677 namelen = name_rmt->namelen;
2678 name = (char *)name_rmt->name;
2680 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2681 ASSERT(namelen == args->namelen);
2682 ASSERT(memcmp(name, args->name, namelen) == 0);
2683 #endif /* DEBUG */
2685 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2686 xfs_trans_log_buf(args->trans, bp,
2687 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2689 if (args->rmtblkno) {
2690 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2691 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2692 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2693 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2694 xfs_trans_log_buf(args->trans, bp,
2695 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2699 * Commit the flag value change and start the next trans in series.
2701 return xfs_trans_roll_inode(&args->trans, args->dp);
2705 * Set the INCOMPLETE flag on an entry in a leaf block.
2708 xfs_attr3_leaf_setflag(
2709 struct xfs_da_args *args)
2711 struct xfs_attr_leafblock *leaf;
2712 struct xfs_attr_leaf_entry *entry;
2713 struct xfs_attr_leaf_name_remote *name_rmt;
2714 struct xfs_buf *bp;
2715 int error;
2716 #ifdef DEBUG
2717 struct xfs_attr3_icleaf_hdr ichdr;
2718 #endif
2720 trace_xfs_attr_leaf_setflag(args);
2723 * Set up the operation.
2725 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2726 if (error)
2727 return error;
2729 leaf = bp->b_addr;
2730 #ifdef DEBUG
2731 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2732 ASSERT(args->index < ichdr.count);
2733 ASSERT(args->index >= 0);
2734 #endif
2735 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2737 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2738 entry->flags |= XFS_ATTR_INCOMPLETE;
2739 xfs_trans_log_buf(args->trans, bp,
2740 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2741 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2742 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2743 name_rmt->valueblk = 0;
2744 name_rmt->valuelen = 0;
2745 xfs_trans_log_buf(args->trans, bp,
2746 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2750 * Commit the flag value change and start the next trans in series.
2752 return xfs_trans_roll_inode(&args->trans, args->dp);
2756 * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2757 * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2758 * entry given by args->blkno2/index2.
2760 * Note that they could be in different blocks, or in the same block.
2763 xfs_attr3_leaf_flipflags(
2764 struct xfs_da_args *args)
2766 struct xfs_attr_leafblock *leaf1;
2767 struct xfs_attr_leafblock *leaf2;
2768 struct xfs_attr_leaf_entry *entry1;
2769 struct xfs_attr_leaf_entry *entry2;
2770 struct xfs_attr_leaf_name_remote *name_rmt;
2771 struct xfs_buf *bp1;
2772 struct xfs_buf *bp2;
2773 int error;
2774 #ifdef DEBUG
2775 struct xfs_attr3_icleaf_hdr ichdr1;
2776 struct xfs_attr3_icleaf_hdr ichdr2;
2777 xfs_attr_leaf_name_local_t *name_loc;
2778 int namelen1, namelen2;
2779 char *name1, *name2;
2780 #endif /* DEBUG */
2782 trace_xfs_attr_leaf_flipflags(args);
2785 * Read the block containing the "old" attr
2787 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2788 if (error)
2789 return error;
2792 * Read the block containing the "new" attr, if it is different
2794 if (args->blkno2 != args->blkno) {
2795 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
2796 -1, &bp2);
2797 if (error)
2798 return error;
2799 } else {
2800 bp2 = bp1;
2803 leaf1 = bp1->b_addr;
2804 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
2806 leaf2 = bp2->b_addr;
2807 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
2809 #ifdef DEBUG
2810 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
2811 ASSERT(args->index < ichdr1.count);
2812 ASSERT(args->index >= 0);
2814 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
2815 ASSERT(args->index2 < ichdr2.count);
2816 ASSERT(args->index2 >= 0);
2818 if (entry1->flags & XFS_ATTR_LOCAL) {
2819 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
2820 namelen1 = name_loc->namelen;
2821 name1 = (char *)name_loc->nameval;
2822 } else {
2823 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2824 namelen1 = name_rmt->namelen;
2825 name1 = (char *)name_rmt->name;
2827 if (entry2->flags & XFS_ATTR_LOCAL) {
2828 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
2829 namelen2 = name_loc->namelen;
2830 name2 = (char *)name_loc->nameval;
2831 } else {
2832 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2833 namelen2 = name_rmt->namelen;
2834 name2 = (char *)name_rmt->name;
2836 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2837 ASSERT(namelen1 == namelen2);
2838 ASSERT(memcmp(name1, name2, namelen1) == 0);
2839 #endif /* DEBUG */
2841 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2842 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2844 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2845 xfs_trans_log_buf(args->trans, bp1,
2846 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2847 if (args->rmtblkno) {
2848 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2849 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2850 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2851 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2852 xfs_trans_log_buf(args->trans, bp1,
2853 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2856 entry2->flags |= XFS_ATTR_INCOMPLETE;
2857 xfs_trans_log_buf(args->trans, bp2,
2858 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2859 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2860 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2861 name_rmt->valueblk = 0;
2862 name_rmt->valuelen = 0;
2863 xfs_trans_log_buf(args->trans, bp2,
2864 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2868 * Commit the flag value change and start the next trans in series.
2870 error = xfs_trans_roll_inode(&args->trans, args->dp);
2872 return error;