162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. 462306a36Sopenharmony_ci * Copyright (c) 2013 Red Hat, Inc. 562306a36Sopenharmony_ci * All Rights Reserved. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci#include "xfs.h" 862306a36Sopenharmony_ci#include "xfs_fs.h" 962306a36Sopenharmony_ci#include "xfs_shared.h" 1062306a36Sopenharmony_ci#include "xfs_format.h" 1162306a36Sopenharmony_ci#include "xfs_log_format.h" 1262306a36Sopenharmony_ci#include "xfs_trans_resv.h" 1362306a36Sopenharmony_ci#include "xfs_mount.h" 1462306a36Sopenharmony_ci#include "xfs_inode.h" 1562306a36Sopenharmony_ci#include "xfs_bmap.h" 1662306a36Sopenharmony_ci#include "xfs_dir2.h" 1762306a36Sopenharmony_ci#include "xfs_dir2_priv.h" 1862306a36Sopenharmony_ci#include "xfs_error.h" 1962306a36Sopenharmony_ci#include "xfs_trace.h" 2062306a36Sopenharmony_ci#include "xfs_trans.h" 2162306a36Sopenharmony_ci#include "xfs_buf_item.h" 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/* 2462306a36Sopenharmony_ci * Local function declarations. 2562306a36Sopenharmony_ci */ 2662306a36Sopenharmony_cistatic int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp, 2762306a36Sopenharmony_ci int *indexp, struct xfs_buf **dbpp, 2862306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *leafhdr); 2962306a36Sopenharmony_cistatic void xfs_dir3_leaf_log_bests(struct xfs_da_args *args, 3062306a36Sopenharmony_ci struct xfs_buf *bp, int first, int last); 3162306a36Sopenharmony_cistatic void xfs_dir3_leaf_log_tail(struct xfs_da_args *args, 3262306a36Sopenharmony_ci struct xfs_buf *bp); 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_civoid 3562306a36Sopenharmony_cixfs_dir2_leaf_hdr_from_disk( 3662306a36Sopenharmony_ci struct xfs_mount *mp, 3762306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *to, 3862306a36Sopenharmony_ci struct xfs_dir2_leaf *from) 3962306a36Sopenharmony_ci{ 4062306a36Sopenharmony_ci if (xfs_has_crc(mp)) { 4162306a36Sopenharmony_ci struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci to->forw = be32_to_cpu(from3->hdr.info.hdr.forw); 4462306a36Sopenharmony_ci to->back = be32_to_cpu(from3->hdr.info.hdr.back); 4562306a36Sopenharmony_ci to->magic = be16_to_cpu(from3->hdr.info.hdr.magic); 4662306a36Sopenharmony_ci to->count = be16_to_cpu(from3->hdr.count); 4762306a36Sopenharmony_ci to->stale = be16_to_cpu(from3->hdr.stale); 4862306a36Sopenharmony_ci to->ents = from3->__ents; 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC || 5162306a36Sopenharmony_ci to->magic == XFS_DIR3_LEAFN_MAGIC); 5262306a36Sopenharmony_ci } else { 5362306a36Sopenharmony_ci to->forw = be32_to_cpu(from->hdr.info.forw); 5462306a36Sopenharmony_ci to->back = be32_to_cpu(from->hdr.info.back); 5562306a36Sopenharmony_ci to->magic = be16_to_cpu(from->hdr.info.magic); 5662306a36Sopenharmony_ci to->count = be16_to_cpu(from->hdr.count); 5762306a36Sopenharmony_ci to->stale = be16_to_cpu(from->hdr.stale); 5862306a36Sopenharmony_ci to->ents = from->__ents; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC || 6162306a36Sopenharmony_ci to->magic == XFS_DIR2_LEAFN_MAGIC); 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_civoid 6662306a36Sopenharmony_cixfs_dir2_leaf_hdr_to_disk( 6762306a36Sopenharmony_ci struct xfs_mount *mp, 6862306a36Sopenharmony_ci struct xfs_dir2_leaf *to, 6962306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *from) 7062306a36Sopenharmony_ci{ 7162306a36Sopenharmony_ci if (xfs_has_crc(mp)) { 7262306a36Sopenharmony_ci struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC || 7562306a36Sopenharmony_ci from->magic == XFS_DIR3_LEAFN_MAGIC); 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci to3->hdr.info.hdr.forw = cpu_to_be32(from->forw); 7862306a36Sopenharmony_ci to3->hdr.info.hdr.back = cpu_to_be32(from->back); 7962306a36Sopenharmony_ci to3->hdr.info.hdr.magic = cpu_to_be16(from->magic); 8062306a36Sopenharmony_ci to3->hdr.count = cpu_to_be16(from->count); 8162306a36Sopenharmony_ci to3->hdr.stale = cpu_to_be16(from->stale); 8262306a36Sopenharmony_ci } else { 8362306a36Sopenharmony_ci ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC || 8462306a36Sopenharmony_ci from->magic == XFS_DIR2_LEAFN_MAGIC); 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci to->hdr.info.forw = cpu_to_be32(from->forw); 8762306a36Sopenharmony_ci to->hdr.info.back = cpu_to_be32(from->back); 8862306a36Sopenharmony_ci to->hdr.info.magic = cpu_to_be16(from->magic); 8962306a36Sopenharmony_ci to->hdr.count = cpu_to_be16(from->count); 9062306a36Sopenharmony_ci to->hdr.stale = cpu_to_be16(from->stale); 9162306a36Sopenharmony_ci } 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci/* 9562306a36Sopenharmony_ci * Check the internal consistency of a leaf1 block. 9662306a36Sopenharmony_ci * Pop an assert if something is wrong. 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_ci#ifdef DEBUG 9962306a36Sopenharmony_cistatic xfs_failaddr_t 10062306a36Sopenharmony_cixfs_dir3_leaf1_check( 10162306a36Sopenharmony_ci struct xfs_inode *dp, 10262306a36Sopenharmony_ci struct xfs_buf *bp) 10362306a36Sopenharmony_ci{ 10462306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf = bp->b_addr; 10562306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) { 11062306a36Sopenharmony_ci struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; 11162306a36Sopenharmony_ci if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp)) 11262306a36Sopenharmony_ci return __this_address; 11362306a36Sopenharmony_ci } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC) 11462306a36Sopenharmony_ci return __this_address; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false); 11762306a36Sopenharmony_ci} 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_cistatic inline void 12062306a36Sopenharmony_cixfs_dir3_leaf_check( 12162306a36Sopenharmony_ci struct xfs_inode *dp, 12262306a36Sopenharmony_ci struct xfs_buf *bp) 12362306a36Sopenharmony_ci{ 12462306a36Sopenharmony_ci xfs_failaddr_t fa; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci fa = xfs_dir3_leaf1_check(dp, bp); 12762306a36Sopenharmony_ci if (!fa) 12862306a36Sopenharmony_ci return; 12962306a36Sopenharmony_ci xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, 13062306a36Sopenharmony_ci bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, 13162306a36Sopenharmony_ci fa); 13262306a36Sopenharmony_ci ASSERT(0); 13362306a36Sopenharmony_ci} 13462306a36Sopenharmony_ci#else 13562306a36Sopenharmony_ci#define xfs_dir3_leaf_check(dp, bp) 13662306a36Sopenharmony_ci#endif 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_cixfs_failaddr_t 13962306a36Sopenharmony_cixfs_dir3_leaf_check_int( 14062306a36Sopenharmony_ci struct xfs_mount *mp, 14162306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *hdr, 14262306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf, 14362306a36Sopenharmony_ci bool expensive_checking) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci struct xfs_da_geometry *geo = mp->m_dir_geo; 14662306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; 14762306a36Sopenharmony_ci int stale; 14862306a36Sopenharmony_ci int i; 14962306a36Sopenharmony_ci bool isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC || 15062306a36Sopenharmony_ci hdr->magic == XFS_DIR3_LEAF1_MAGIC); 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(geo, leaf); 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci /* 15562306a36Sopenharmony_ci * XXX (dgc): This value is not restrictive enough. 15662306a36Sopenharmony_ci * Should factor in the size of the bests table as well. 15762306a36Sopenharmony_ci * We can deduce a value for that from i_disk_size. 15862306a36Sopenharmony_ci */ 15962306a36Sopenharmony_ci if (hdr->count > geo->leaf_max_ents) 16062306a36Sopenharmony_ci return __this_address; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci /* Leaves and bests don't overlap in leaf format. */ 16362306a36Sopenharmony_ci if (isleaf1 && 16462306a36Sopenharmony_ci (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp)) 16562306a36Sopenharmony_ci return __this_address; 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci if (!expensive_checking) 16862306a36Sopenharmony_ci return NULL; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci /* Check hash value order, count stale entries. */ 17162306a36Sopenharmony_ci for (i = stale = 0; i < hdr->count; i++) { 17262306a36Sopenharmony_ci if (i + 1 < hdr->count) { 17362306a36Sopenharmony_ci if (be32_to_cpu(hdr->ents[i].hashval) > 17462306a36Sopenharmony_ci be32_to_cpu(hdr->ents[i + 1].hashval)) 17562306a36Sopenharmony_ci return __this_address; 17662306a36Sopenharmony_ci } 17762306a36Sopenharmony_ci if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 17862306a36Sopenharmony_ci stale++; 17962306a36Sopenharmony_ci if (isleaf1 && xfs_dir2_dataptr_to_db(geo, 18062306a36Sopenharmony_ci be32_to_cpu(hdr->ents[i].address)) >= 18162306a36Sopenharmony_ci be32_to_cpu(ltp->bestcount)) 18262306a36Sopenharmony_ci return __this_address; 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci if (hdr->stale != stale) 18562306a36Sopenharmony_ci return __this_address; 18662306a36Sopenharmony_ci return NULL; 18762306a36Sopenharmony_ci} 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci/* 19062306a36Sopenharmony_ci * We verify the magic numbers before decoding the leaf header so that on debug 19162306a36Sopenharmony_ci * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due 19262306a36Sopenharmony_ci * to incorrect magic numbers. 19362306a36Sopenharmony_ci */ 19462306a36Sopenharmony_cistatic xfs_failaddr_t 19562306a36Sopenharmony_cixfs_dir3_leaf_verify( 19662306a36Sopenharmony_ci struct xfs_buf *bp) 19762306a36Sopenharmony_ci{ 19862306a36Sopenharmony_ci struct xfs_mount *mp = bp->b_mount; 19962306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 20062306a36Sopenharmony_ci xfs_failaddr_t fa; 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci fa = xfs_da3_blkinfo_verify(bp, bp->b_addr); 20362306a36Sopenharmony_ci if (fa) 20462306a36Sopenharmony_ci return fa; 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr); 20762306a36Sopenharmony_ci return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true); 20862306a36Sopenharmony_ci} 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_cistatic void 21162306a36Sopenharmony_cixfs_dir3_leaf_read_verify( 21262306a36Sopenharmony_ci struct xfs_buf *bp) 21362306a36Sopenharmony_ci{ 21462306a36Sopenharmony_ci struct xfs_mount *mp = bp->b_mount; 21562306a36Sopenharmony_ci xfs_failaddr_t fa; 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci if (xfs_has_crc(mp) && 21862306a36Sopenharmony_ci !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF)) 21962306a36Sopenharmony_ci xfs_verifier_error(bp, -EFSBADCRC, __this_address); 22062306a36Sopenharmony_ci else { 22162306a36Sopenharmony_ci fa = xfs_dir3_leaf_verify(bp); 22262306a36Sopenharmony_ci if (fa) 22362306a36Sopenharmony_ci xfs_verifier_error(bp, -EFSCORRUPTED, fa); 22462306a36Sopenharmony_ci } 22562306a36Sopenharmony_ci} 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_cistatic void 22862306a36Sopenharmony_cixfs_dir3_leaf_write_verify( 22962306a36Sopenharmony_ci struct xfs_buf *bp) 23062306a36Sopenharmony_ci{ 23162306a36Sopenharmony_ci struct xfs_mount *mp = bp->b_mount; 23262306a36Sopenharmony_ci struct xfs_buf_log_item *bip = bp->b_log_item; 23362306a36Sopenharmony_ci struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr; 23462306a36Sopenharmony_ci xfs_failaddr_t fa; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci fa = xfs_dir3_leaf_verify(bp); 23762306a36Sopenharmony_ci if (fa) { 23862306a36Sopenharmony_ci xfs_verifier_error(bp, -EFSCORRUPTED, fa); 23962306a36Sopenharmony_ci return; 24062306a36Sopenharmony_ci } 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci if (!xfs_has_crc(mp)) 24362306a36Sopenharmony_ci return; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci if (bip) 24662306a36Sopenharmony_ci hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); 24762306a36Sopenharmony_ci 24862306a36Sopenharmony_ci xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF); 24962306a36Sopenharmony_ci} 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ciconst struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = { 25262306a36Sopenharmony_ci .name = "xfs_dir3_leaf1", 25362306a36Sopenharmony_ci .magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC), 25462306a36Sopenharmony_ci cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) }, 25562306a36Sopenharmony_ci .verify_read = xfs_dir3_leaf_read_verify, 25662306a36Sopenharmony_ci .verify_write = xfs_dir3_leaf_write_verify, 25762306a36Sopenharmony_ci .verify_struct = xfs_dir3_leaf_verify, 25862306a36Sopenharmony_ci}; 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ciconst struct xfs_buf_ops xfs_dir3_leafn_buf_ops = { 26162306a36Sopenharmony_ci .name = "xfs_dir3_leafn", 26262306a36Sopenharmony_ci .magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC), 26362306a36Sopenharmony_ci cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) }, 26462306a36Sopenharmony_ci .verify_read = xfs_dir3_leaf_read_verify, 26562306a36Sopenharmony_ci .verify_write = xfs_dir3_leaf_write_verify, 26662306a36Sopenharmony_ci .verify_struct = xfs_dir3_leaf_verify, 26762306a36Sopenharmony_ci}; 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ciint 27062306a36Sopenharmony_cixfs_dir3_leaf_read( 27162306a36Sopenharmony_ci struct xfs_trans *tp, 27262306a36Sopenharmony_ci struct xfs_inode *dp, 27362306a36Sopenharmony_ci xfs_dablk_t fbno, 27462306a36Sopenharmony_ci struct xfs_buf **bpp) 27562306a36Sopenharmony_ci{ 27662306a36Sopenharmony_ci int err; 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ci err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK, 27962306a36Sopenharmony_ci &xfs_dir3_leaf1_buf_ops); 28062306a36Sopenharmony_ci if (!err && tp && *bpp) 28162306a36Sopenharmony_ci xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF); 28262306a36Sopenharmony_ci return err; 28362306a36Sopenharmony_ci} 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ciint 28662306a36Sopenharmony_cixfs_dir3_leafn_read( 28762306a36Sopenharmony_ci struct xfs_trans *tp, 28862306a36Sopenharmony_ci struct xfs_inode *dp, 28962306a36Sopenharmony_ci xfs_dablk_t fbno, 29062306a36Sopenharmony_ci struct xfs_buf **bpp) 29162306a36Sopenharmony_ci{ 29262306a36Sopenharmony_ci int err; 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK, 29562306a36Sopenharmony_ci &xfs_dir3_leafn_buf_ops); 29662306a36Sopenharmony_ci if (!err && tp && *bpp) 29762306a36Sopenharmony_ci xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF); 29862306a36Sopenharmony_ci return err; 29962306a36Sopenharmony_ci} 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci/* 30262306a36Sopenharmony_ci * Initialize a new leaf block, leaf1 or leafn magic accepted. 30362306a36Sopenharmony_ci */ 30462306a36Sopenharmony_cistatic void 30562306a36Sopenharmony_cixfs_dir3_leaf_init( 30662306a36Sopenharmony_ci struct xfs_mount *mp, 30762306a36Sopenharmony_ci struct xfs_trans *tp, 30862306a36Sopenharmony_ci struct xfs_buf *bp, 30962306a36Sopenharmony_ci xfs_ino_t owner, 31062306a36Sopenharmony_ci uint16_t type) 31162306a36Sopenharmony_ci{ 31262306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf = bp->b_addr; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC); 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ci if (xfs_has_crc(mp)) { 31762306a36Sopenharmony_ci struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ci memset(leaf3, 0, sizeof(*leaf3)); 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC) 32262306a36Sopenharmony_ci ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) 32362306a36Sopenharmony_ci : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); 32462306a36Sopenharmony_ci leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp)); 32562306a36Sopenharmony_ci leaf3->info.owner = cpu_to_be64(owner); 32662306a36Sopenharmony_ci uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid); 32762306a36Sopenharmony_ci } else { 32862306a36Sopenharmony_ci memset(leaf, 0, sizeof(*leaf)); 32962306a36Sopenharmony_ci leaf->hdr.info.magic = cpu_to_be16(type); 33062306a36Sopenharmony_ci } 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci /* 33362306a36Sopenharmony_ci * If it's a leaf-format directory initialize the tail. 33462306a36Sopenharmony_ci * Caller is responsible for initialising the bests table. 33562306a36Sopenharmony_ci */ 33662306a36Sopenharmony_ci if (type == XFS_DIR2_LEAF1_MAGIC) { 33762306a36Sopenharmony_ci struct xfs_dir2_leaf_tail *ltp; 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf); 34062306a36Sopenharmony_ci ltp->bestcount = 0; 34162306a36Sopenharmony_ci bp->b_ops = &xfs_dir3_leaf1_buf_ops; 34262306a36Sopenharmony_ci xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF); 34362306a36Sopenharmony_ci } else { 34462306a36Sopenharmony_ci bp->b_ops = &xfs_dir3_leafn_buf_ops; 34562306a36Sopenharmony_ci xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF); 34662306a36Sopenharmony_ci } 34762306a36Sopenharmony_ci} 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ciint 35062306a36Sopenharmony_cixfs_dir3_leaf_get_buf( 35162306a36Sopenharmony_ci xfs_da_args_t *args, 35262306a36Sopenharmony_ci xfs_dir2_db_t bno, 35362306a36Sopenharmony_ci struct xfs_buf **bpp, 35462306a36Sopenharmony_ci uint16_t magic) 35562306a36Sopenharmony_ci{ 35662306a36Sopenharmony_ci struct xfs_inode *dp = args->dp; 35762306a36Sopenharmony_ci struct xfs_trans *tp = args->trans; 35862306a36Sopenharmony_ci struct xfs_mount *mp = dp->i_mount; 35962306a36Sopenharmony_ci struct xfs_buf *bp; 36062306a36Sopenharmony_ci int error; 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC); 36362306a36Sopenharmony_ci ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) && 36462306a36Sopenharmony_ci bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno), 36762306a36Sopenharmony_ci &bp, XFS_DATA_FORK); 36862306a36Sopenharmony_ci if (error) 36962306a36Sopenharmony_ci return error; 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic); 37262306a36Sopenharmony_ci xfs_dir3_leaf_log_header(args, bp); 37362306a36Sopenharmony_ci if (magic == XFS_DIR2_LEAF1_MAGIC) 37462306a36Sopenharmony_ci xfs_dir3_leaf_log_tail(args, bp); 37562306a36Sopenharmony_ci *bpp = bp; 37662306a36Sopenharmony_ci return 0; 37762306a36Sopenharmony_ci} 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci/* 38062306a36Sopenharmony_ci * Convert a block form directory to a leaf form directory. 38162306a36Sopenharmony_ci */ 38262306a36Sopenharmony_ciint /* error */ 38362306a36Sopenharmony_cixfs_dir2_block_to_leaf( 38462306a36Sopenharmony_ci xfs_da_args_t *args, /* operation arguments */ 38562306a36Sopenharmony_ci struct xfs_buf *dbp) /* input block's buffer */ 38662306a36Sopenharmony_ci{ 38762306a36Sopenharmony_ci __be16 *bestsp; /* leaf's bestsp entries */ 38862306a36Sopenharmony_ci xfs_dablk_t blkno; /* leaf block's bno */ 38962306a36Sopenharmony_ci xfs_dir2_data_hdr_t *hdr; /* block header */ 39062306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */ 39162306a36Sopenharmony_ci xfs_dir2_block_tail_t *btp; /* block's tail */ 39262306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 39362306a36Sopenharmony_ci int error; /* error return code */ 39462306a36Sopenharmony_ci struct xfs_buf *lbp; /* leaf block's buffer */ 39562306a36Sopenharmony_ci xfs_dir2_db_t ldb; /* leaf block's bno */ 39662306a36Sopenharmony_ci xfs_dir2_leaf_t *leaf; /* leaf structure */ 39762306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */ 39862306a36Sopenharmony_ci int needlog; /* need to log block header */ 39962306a36Sopenharmony_ci int needscan; /* need to rescan bestfree */ 40062306a36Sopenharmony_ci xfs_trans_t *tp; /* transaction pointer */ 40162306a36Sopenharmony_ci struct xfs_dir2_data_free *bf; 40262306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ci trace_xfs_dir2_block_to_leaf(args); 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci dp = args->dp; 40762306a36Sopenharmony_ci tp = args->trans; 40862306a36Sopenharmony_ci /* 40962306a36Sopenharmony_ci * Add the leaf block to the inode. 41062306a36Sopenharmony_ci * This interface will only put blocks in the leaf/node range. 41162306a36Sopenharmony_ci * Since that's empty now, we'll get the root (block 0 in range). 41262306a36Sopenharmony_ci */ 41362306a36Sopenharmony_ci if ((error = xfs_da_grow_inode(args, &blkno))) { 41462306a36Sopenharmony_ci return error; 41562306a36Sopenharmony_ci } 41662306a36Sopenharmony_ci ldb = xfs_dir2_da_to_db(args->geo, blkno); 41762306a36Sopenharmony_ci ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET)); 41862306a36Sopenharmony_ci /* 41962306a36Sopenharmony_ci * Initialize the leaf block, get a buffer for it. 42062306a36Sopenharmony_ci */ 42162306a36Sopenharmony_ci error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC); 42262306a36Sopenharmony_ci if (error) 42362306a36Sopenharmony_ci return error; 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci leaf = lbp->b_addr; 42662306a36Sopenharmony_ci hdr = dbp->b_addr; 42762306a36Sopenharmony_ci xfs_dir3_data_check(dp, dbp); 42862306a36Sopenharmony_ci btp = xfs_dir2_block_tail_p(args->geo, hdr); 42962306a36Sopenharmony_ci blp = xfs_dir2_block_leaf_p(btp); 43062306a36Sopenharmony_ci bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci /* 43362306a36Sopenharmony_ci * Set the counts in the leaf header. 43462306a36Sopenharmony_ci */ 43562306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 43662306a36Sopenharmony_ci leafhdr.count = be32_to_cpu(btp->count); 43762306a36Sopenharmony_ci leafhdr.stale = be32_to_cpu(btp->stale); 43862306a36Sopenharmony_ci xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 43962306a36Sopenharmony_ci xfs_dir3_leaf_log_header(args, lbp); 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_ci /* 44262306a36Sopenharmony_ci * Could compact these but I think we always do the conversion 44362306a36Sopenharmony_ci * after squeezing out stale entries. 44462306a36Sopenharmony_ci */ 44562306a36Sopenharmony_ci memcpy(leafhdr.ents, blp, 44662306a36Sopenharmony_ci be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry)); 44762306a36Sopenharmony_ci xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1); 44862306a36Sopenharmony_ci needscan = 0; 44962306a36Sopenharmony_ci needlog = 1; 45062306a36Sopenharmony_ci /* 45162306a36Sopenharmony_ci * Make the space formerly occupied by the leaf entries and block 45262306a36Sopenharmony_ci * tail be free. 45362306a36Sopenharmony_ci */ 45462306a36Sopenharmony_ci xfs_dir2_data_make_free(args, dbp, 45562306a36Sopenharmony_ci (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), 45662306a36Sopenharmony_ci (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize - 45762306a36Sopenharmony_ci (char *)blp), 45862306a36Sopenharmony_ci &needlog, &needscan); 45962306a36Sopenharmony_ci /* 46062306a36Sopenharmony_ci * Fix up the block header, make it a data block. 46162306a36Sopenharmony_ci */ 46262306a36Sopenharmony_ci dbp->b_ops = &xfs_dir3_data_buf_ops; 46362306a36Sopenharmony_ci xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF); 46462306a36Sopenharmony_ci if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) 46562306a36Sopenharmony_ci hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); 46662306a36Sopenharmony_ci else 46762306a36Sopenharmony_ci hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC); 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci if (needscan) 47062306a36Sopenharmony_ci xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); 47162306a36Sopenharmony_ci /* 47262306a36Sopenharmony_ci * Set up leaf tail and bests table. 47362306a36Sopenharmony_ci */ 47462306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 47562306a36Sopenharmony_ci ltp->bestcount = cpu_to_be32(1); 47662306a36Sopenharmony_ci bestsp = xfs_dir2_leaf_bests_p(ltp); 47762306a36Sopenharmony_ci bestsp[0] = bf[0].length; 47862306a36Sopenharmony_ci /* 47962306a36Sopenharmony_ci * Log the data header and leaf bests table. 48062306a36Sopenharmony_ci */ 48162306a36Sopenharmony_ci if (needlog) 48262306a36Sopenharmony_ci xfs_dir2_data_log_header(args, dbp); 48362306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 48462306a36Sopenharmony_ci xfs_dir3_data_check(dp, dbp); 48562306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, 0, 0); 48662306a36Sopenharmony_ci return 0; 48762306a36Sopenharmony_ci} 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ciSTATIC void 49062306a36Sopenharmony_cixfs_dir3_leaf_find_stale( 49162306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *leafhdr, 49262306a36Sopenharmony_ci struct xfs_dir2_leaf_entry *ents, 49362306a36Sopenharmony_ci int index, 49462306a36Sopenharmony_ci int *lowstale, 49562306a36Sopenharmony_ci int *highstale) 49662306a36Sopenharmony_ci{ 49762306a36Sopenharmony_ci /* 49862306a36Sopenharmony_ci * Find the first stale entry before our index, if any. 49962306a36Sopenharmony_ci */ 50062306a36Sopenharmony_ci for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) { 50162306a36Sopenharmony_ci if (ents[*lowstale].address == 50262306a36Sopenharmony_ci cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 50362306a36Sopenharmony_ci break; 50462306a36Sopenharmony_ci } 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci /* 50762306a36Sopenharmony_ci * Find the first stale entry at or after our index, if any. 50862306a36Sopenharmony_ci * Stop if the result would require moving more entries than using 50962306a36Sopenharmony_ci * lowstale. 51062306a36Sopenharmony_ci */ 51162306a36Sopenharmony_ci for (*highstale = index; *highstale < leafhdr->count; ++*highstale) { 51262306a36Sopenharmony_ci if (ents[*highstale].address == 51362306a36Sopenharmony_ci cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 51462306a36Sopenharmony_ci break; 51562306a36Sopenharmony_ci if (*lowstale >= 0 && index - *lowstale <= *highstale - index) 51662306a36Sopenharmony_ci break; 51762306a36Sopenharmony_ci } 51862306a36Sopenharmony_ci} 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_cistruct xfs_dir2_leaf_entry * 52162306a36Sopenharmony_cixfs_dir3_leaf_find_entry( 52262306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *leafhdr, 52362306a36Sopenharmony_ci struct xfs_dir2_leaf_entry *ents, 52462306a36Sopenharmony_ci int index, /* leaf table position */ 52562306a36Sopenharmony_ci int compact, /* need to compact leaves */ 52662306a36Sopenharmony_ci int lowstale, /* index of prev stale leaf */ 52762306a36Sopenharmony_ci int highstale, /* index of next stale leaf */ 52862306a36Sopenharmony_ci int *lfloglow, /* low leaf logging index */ 52962306a36Sopenharmony_ci int *lfloghigh) /* high leaf logging index */ 53062306a36Sopenharmony_ci{ 53162306a36Sopenharmony_ci if (!leafhdr->stale) { 53262306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ci /* 53562306a36Sopenharmony_ci * Now we need to make room to insert the leaf entry. 53662306a36Sopenharmony_ci * 53762306a36Sopenharmony_ci * If there are no stale entries, just insert a hole at index. 53862306a36Sopenharmony_ci */ 53962306a36Sopenharmony_ci lep = &ents[index]; 54062306a36Sopenharmony_ci if (index < leafhdr->count) 54162306a36Sopenharmony_ci memmove(lep + 1, lep, 54262306a36Sopenharmony_ci (leafhdr->count - index) * sizeof(*lep)); 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci /* 54562306a36Sopenharmony_ci * Record low and high logging indices for the leaf. 54662306a36Sopenharmony_ci */ 54762306a36Sopenharmony_ci *lfloglow = index; 54862306a36Sopenharmony_ci *lfloghigh = leafhdr->count++; 54962306a36Sopenharmony_ci return lep; 55062306a36Sopenharmony_ci } 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci /* 55362306a36Sopenharmony_ci * There are stale entries. 55462306a36Sopenharmony_ci * 55562306a36Sopenharmony_ci * We will use one of them for the new entry. It's probably not at 55662306a36Sopenharmony_ci * the right location, so we'll have to shift some up or down first. 55762306a36Sopenharmony_ci * 55862306a36Sopenharmony_ci * If we didn't compact before, we need to find the nearest stale 55962306a36Sopenharmony_ci * entries before and after our insertion point. 56062306a36Sopenharmony_ci */ 56162306a36Sopenharmony_ci if (compact == 0) 56262306a36Sopenharmony_ci xfs_dir3_leaf_find_stale(leafhdr, ents, index, 56362306a36Sopenharmony_ci &lowstale, &highstale); 56462306a36Sopenharmony_ci 56562306a36Sopenharmony_ci /* 56662306a36Sopenharmony_ci * If the low one is better, use it. 56762306a36Sopenharmony_ci */ 56862306a36Sopenharmony_ci if (lowstale >= 0 && 56962306a36Sopenharmony_ci (highstale == leafhdr->count || 57062306a36Sopenharmony_ci index - lowstale - 1 < highstale - index)) { 57162306a36Sopenharmony_ci ASSERT(index - lowstale - 1 >= 0); 57262306a36Sopenharmony_ci ASSERT(ents[lowstale].address == 57362306a36Sopenharmony_ci cpu_to_be32(XFS_DIR2_NULL_DATAPTR)); 57462306a36Sopenharmony_ci 57562306a36Sopenharmony_ci /* 57662306a36Sopenharmony_ci * Copy entries up to cover the stale entry and make room 57762306a36Sopenharmony_ci * for the new entry. 57862306a36Sopenharmony_ci */ 57962306a36Sopenharmony_ci if (index - lowstale - 1 > 0) { 58062306a36Sopenharmony_ci memmove(&ents[lowstale], &ents[lowstale + 1], 58162306a36Sopenharmony_ci (index - lowstale - 1) * 58262306a36Sopenharmony_ci sizeof(xfs_dir2_leaf_entry_t)); 58362306a36Sopenharmony_ci } 58462306a36Sopenharmony_ci *lfloglow = min(lowstale, *lfloglow); 58562306a36Sopenharmony_ci *lfloghigh = max(index - 1, *lfloghigh); 58662306a36Sopenharmony_ci leafhdr->stale--; 58762306a36Sopenharmony_ci return &ents[index - 1]; 58862306a36Sopenharmony_ci } 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci /* 59162306a36Sopenharmony_ci * The high one is better, so use that one. 59262306a36Sopenharmony_ci */ 59362306a36Sopenharmony_ci ASSERT(highstale - index >= 0); 59462306a36Sopenharmony_ci ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)); 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci /* 59762306a36Sopenharmony_ci * Copy entries down to cover the stale entry and make room for the 59862306a36Sopenharmony_ci * new entry. 59962306a36Sopenharmony_ci */ 60062306a36Sopenharmony_ci if (highstale - index > 0) { 60162306a36Sopenharmony_ci memmove(&ents[index + 1], &ents[index], 60262306a36Sopenharmony_ci (highstale - index) * sizeof(xfs_dir2_leaf_entry_t)); 60362306a36Sopenharmony_ci } 60462306a36Sopenharmony_ci *lfloglow = min(index, *lfloglow); 60562306a36Sopenharmony_ci *lfloghigh = max(highstale, *lfloghigh); 60662306a36Sopenharmony_ci leafhdr->stale--; 60762306a36Sopenharmony_ci return &ents[index]; 60862306a36Sopenharmony_ci} 60962306a36Sopenharmony_ci 61062306a36Sopenharmony_ci/* 61162306a36Sopenharmony_ci * Add an entry to a leaf form directory. 61262306a36Sopenharmony_ci */ 61362306a36Sopenharmony_ciint /* error */ 61462306a36Sopenharmony_cixfs_dir2_leaf_addname( 61562306a36Sopenharmony_ci struct xfs_da_args *args) /* operation arguments */ 61662306a36Sopenharmony_ci{ 61762306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 61862306a36Sopenharmony_ci struct xfs_trans *tp = args->trans; 61962306a36Sopenharmony_ci __be16 *bestsp; /* freespace table in leaf */ 62062306a36Sopenharmony_ci __be16 *tagp; /* end of data entry */ 62162306a36Sopenharmony_ci struct xfs_buf *dbp; /* data block buffer */ 62262306a36Sopenharmony_ci struct xfs_buf *lbp; /* leaf's buffer */ 62362306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf; /* leaf structure */ 62462306a36Sopenharmony_ci struct xfs_inode *dp = args->dp; /* incore directory inode */ 62562306a36Sopenharmony_ci struct xfs_dir2_data_hdr *hdr; /* data block header */ 62662306a36Sopenharmony_ci struct xfs_dir2_data_entry *dep; /* data block entry */ 62762306a36Sopenharmony_ci struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */ 62862306a36Sopenharmony_ci struct xfs_dir2_leaf_entry *ents; 62962306a36Sopenharmony_ci struct xfs_dir2_data_unused *dup; /* data unused entry */ 63062306a36Sopenharmony_ci struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */ 63162306a36Sopenharmony_ci struct xfs_dir2_data_free *bf; /* bestfree table */ 63262306a36Sopenharmony_ci int compact; /* need to compact leaves */ 63362306a36Sopenharmony_ci int error; /* error return value */ 63462306a36Sopenharmony_ci int grown; /* allocated new data block */ 63562306a36Sopenharmony_ci int highstale = 0; /* index of next stale leaf */ 63662306a36Sopenharmony_ci int i; /* temporary, index */ 63762306a36Sopenharmony_ci int index; /* leaf table position */ 63862306a36Sopenharmony_ci int length; /* length of new entry */ 63962306a36Sopenharmony_ci int lfloglow; /* low leaf logging index */ 64062306a36Sopenharmony_ci int lfloghigh; /* high leaf logging index */ 64162306a36Sopenharmony_ci int lowstale = 0; /* index of prev stale leaf */ 64262306a36Sopenharmony_ci int needbytes; /* leaf block bytes needed */ 64362306a36Sopenharmony_ci int needlog; /* need to log data header */ 64462306a36Sopenharmony_ci int needscan; /* need to rescan data free */ 64562306a36Sopenharmony_ci xfs_dir2_db_t use_block; /* data block number */ 64662306a36Sopenharmony_ci 64762306a36Sopenharmony_ci trace_xfs_dir2_leaf_addname(args); 64862306a36Sopenharmony_ci 64962306a36Sopenharmony_ci error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp); 65062306a36Sopenharmony_ci if (error) 65162306a36Sopenharmony_ci return error; 65262306a36Sopenharmony_ci 65362306a36Sopenharmony_ci /* 65462306a36Sopenharmony_ci * Look up the entry by hash value and name. 65562306a36Sopenharmony_ci * We know it's not there, our caller has already done a lookup. 65662306a36Sopenharmony_ci * So the index is of the entry to insert in front of. 65762306a36Sopenharmony_ci * But if there are dup hash values the index is of the first of those. 65862306a36Sopenharmony_ci */ 65962306a36Sopenharmony_ci index = xfs_dir2_leaf_search_hash(args, lbp); 66062306a36Sopenharmony_ci leaf = lbp->b_addr; 66162306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 66262306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 66362306a36Sopenharmony_ci ents = leafhdr.ents; 66462306a36Sopenharmony_ci bestsp = xfs_dir2_leaf_bests_p(ltp); 66562306a36Sopenharmony_ci length = xfs_dir2_data_entsize(dp->i_mount, args->namelen); 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci /* 66862306a36Sopenharmony_ci * See if there are any entries with the same hash value 66962306a36Sopenharmony_ci * and space in their block for the new entry. 67062306a36Sopenharmony_ci * This is good because it puts multiple same-hash value entries 67162306a36Sopenharmony_ci * in a data block, improving the lookup of those entries. 67262306a36Sopenharmony_ci */ 67362306a36Sopenharmony_ci for (use_block = -1, lep = &ents[index]; 67462306a36Sopenharmony_ci index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 67562306a36Sopenharmony_ci index++, lep++) { 67662306a36Sopenharmony_ci if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 67762306a36Sopenharmony_ci continue; 67862306a36Sopenharmony_ci i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); 67962306a36Sopenharmony_ci ASSERT(i < be32_to_cpu(ltp->bestcount)); 68062306a36Sopenharmony_ci ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF)); 68162306a36Sopenharmony_ci if (be16_to_cpu(bestsp[i]) >= length) { 68262306a36Sopenharmony_ci use_block = i; 68362306a36Sopenharmony_ci break; 68462306a36Sopenharmony_ci } 68562306a36Sopenharmony_ci } 68662306a36Sopenharmony_ci /* 68762306a36Sopenharmony_ci * Didn't find a block yet, linear search all the data blocks. 68862306a36Sopenharmony_ci */ 68962306a36Sopenharmony_ci if (use_block == -1) { 69062306a36Sopenharmony_ci for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) { 69162306a36Sopenharmony_ci /* 69262306a36Sopenharmony_ci * Remember a block we see that's missing. 69362306a36Sopenharmony_ci */ 69462306a36Sopenharmony_ci if (bestsp[i] == cpu_to_be16(NULLDATAOFF) && 69562306a36Sopenharmony_ci use_block == -1) 69662306a36Sopenharmony_ci use_block = i; 69762306a36Sopenharmony_ci else if (be16_to_cpu(bestsp[i]) >= length) { 69862306a36Sopenharmony_ci use_block = i; 69962306a36Sopenharmony_ci break; 70062306a36Sopenharmony_ci } 70162306a36Sopenharmony_ci } 70262306a36Sopenharmony_ci } 70362306a36Sopenharmony_ci /* 70462306a36Sopenharmony_ci * How many bytes do we need in the leaf block? 70562306a36Sopenharmony_ci */ 70662306a36Sopenharmony_ci needbytes = 0; 70762306a36Sopenharmony_ci if (!leafhdr.stale) 70862306a36Sopenharmony_ci needbytes += sizeof(xfs_dir2_leaf_entry_t); 70962306a36Sopenharmony_ci if (use_block == -1) 71062306a36Sopenharmony_ci needbytes += sizeof(xfs_dir2_data_off_t); 71162306a36Sopenharmony_ci 71262306a36Sopenharmony_ci /* 71362306a36Sopenharmony_ci * Now kill use_block if it refers to a missing block, so we 71462306a36Sopenharmony_ci * can use it as an indication of allocation needed. 71562306a36Sopenharmony_ci */ 71662306a36Sopenharmony_ci if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF)) 71762306a36Sopenharmony_ci use_block = -1; 71862306a36Sopenharmony_ci /* 71962306a36Sopenharmony_ci * If we don't have enough free bytes but we can make enough 72062306a36Sopenharmony_ci * by compacting out stale entries, we'll do that. 72162306a36Sopenharmony_ci */ 72262306a36Sopenharmony_ci if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes && 72362306a36Sopenharmony_ci leafhdr.stale > 1) 72462306a36Sopenharmony_ci compact = 1; 72562306a36Sopenharmony_ci 72662306a36Sopenharmony_ci /* 72762306a36Sopenharmony_ci * Otherwise if we don't have enough free bytes we need to 72862306a36Sopenharmony_ci * convert to node form. 72962306a36Sopenharmony_ci */ 73062306a36Sopenharmony_ci else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) { 73162306a36Sopenharmony_ci /* 73262306a36Sopenharmony_ci * Just checking or no space reservation, give up. 73362306a36Sopenharmony_ci */ 73462306a36Sopenharmony_ci if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || 73562306a36Sopenharmony_ci args->total == 0) { 73662306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 73762306a36Sopenharmony_ci return -ENOSPC; 73862306a36Sopenharmony_ci } 73962306a36Sopenharmony_ci /* 74062306a36Sopenharmony_ci * Convert to node form. 74162306a36Sopenharmony_ci */ 74262306a36Sopenharmony_ci error = xfs_dir2_leaf_to_node(args, lbp); 74362306a36Sopenharmony_ci if (error) 74462306a36Sopenharmony_ci return error; 74562306a36Sopenharmony_ci /* 74662306a36Sopenharmony_ci * Then add the new entry. 74762306a36Sopenharmony_ci */ 74862306a36Sopenharmony_ci return xfs_dir2_node_addname(args); 74962306a36Sopenharmony_ci } 75062306a36Sopenharmony_ci /* 75162306a36Sopenharmony_ci * Otherwise it will fit without compaction. 75262306a36Sopenharmony_ci */ 75362306a36Sopenharmony_ci else 75462306a36Sopenharmony_ci compact = 0; 75562306a36Sopenharmony_ci /* 75662306a36Sopenharmony_ci * If just checking, then it will fit unless we needed to allocate 75762306a36Sopenharmony_ci * a new data block. 75862306a36Sopenharmony_ci */ 75962306a36Sopenharmony_ci if (args->op_flags & XFS_DA_OP_JUSTCHECK) { 76062306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 76162306a36Sopenharmony_ci return use_block == -1 ? -ENOSPC : 0; 76262306a36Sopenharmony_ci } 76362306a36Sopenharmony_ci /* 76462306a36Sopenharmony_ci * If no allocations are allowed, return now before we've 76562306a36Sopenharmony_ci * changed anything. 76662306a36Sopenharmony_ci */ 76762306a36Sopenharmony_ci if (args->total == 0 && use_block == -1) { 76862306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 76962306a36Sopenharmony_ci return -ENOSPC; 77062306a36Sopenharmony_ci } 77162306a36Sopenharmony_ci /* 77262306a36Sopenharmony_ci * Need to compact the leaf entries, removing stale ones. 77362306a36Sopenharmony_ci * Leave one stale entry behind - the one closest to our 77462306a36Sopenharmony_ci * insertion index - and we'll shift that one to our insertion 77562306a36Sopenharmony_ci * point later. 77662306a36Sopenharmony_ci */ 77762306a36Sopenharmony_ci if (compact) { 77862306a36Sopenharmony_ci xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, 77962306a36Sopenharmony_ci &highstale, &lfloglow, &lfloghigh); 78062306a36Sopenharmony_ci } 78162306a36Sopenharmony_ci /* 78262306a36Sopenharmony_ci * There are stale entries, so we'll need log-low and log-high 78362306a36Sopenharmony_ci * impossibly bad values later. 78462306a36Sopenharmony_ci */ 78562306a36Sopenharmony_ci else if (leafhdr.stale) { 78662306a36Sopenharmony_ci lfloglow = leafhdr.count; 78762306a36Sopenharmony_ci lfloghigh = -1; 78862306a36Sopenharmony_ci } 78962306a36Sopenharmony_ci /* 79062306a36Sopenharmony_ci * If there was no data block space found, we need to allocate 79162306a36Sopenharmony_ci * a new one. 79262306a36Sopenharmony_ci */ 79362306a36Sopenharmony_ci if (use_block == -1) { 79462306a36Sopenharmony_ci /* 79562306a36Sopenharmony_ci * Add the new data block. 79662306a36Sopenharmony_ci */ 79762306a36Sopenharmony_ci if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, 79862306a36Sopenharmony_ci &use_block))) { 79962306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 80062306a36Sopenharmony_ci return error; 80162306a36Sopenharmony_ci } 80262306a36Sopenharmony_ci /* 80362306a36Sopenharmony_ci * Initialize the block. 80462306a36Sopenharmony_ci */ 80562306a36Sopenharmony_ci if ((error = xfs_dir3_data_init(args, use_block, &dbp))) { 80662306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 80762306a36Sopenharmony_ci return error; 80862306a36Sopenharmony_ci } 80962306a36Sopenharmony_ci /* 81062306a36Sopenharmony_ci * If we're adding a new data block on the end we need to 81162306a36Sopenharmony_ci * extend the bests table. Copy it up one entry. 81262306a36Sopenharmony_ci */ 81362306a36Sopenharmony_ci if (use_block >= be32_to_cpu(ltp->bestcount)) { 81462306a36Sopenharmony_ci bestsp--; 81562306a36Sopenharmony_ci memmove(&bestsp[0], &bestsp[1], 81662306a36Sopenharmony_ci be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0])); 81762306a36Sopenharmony_ci be32_add_cpu(<p->bestcount, 1); 81862306a36Sopenharmony_ci xfs_dir3_leaf_log_tail(args, lbp); 81962306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, 0, 82062306a36Sopenharmony_ci be32_to_cpu(ltp->bestcount) - 1); 82162306a36Sopenharmony_ci } 82262306a36Sopenharmony_ci /* 82362306a36Sopenharmony_ci * If we're filling in a previously empty block just log it. 82462306a36Sopenharmony_ci */ 82562306a36Sopenharmony_ci else 82662306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block); 82762306a36Sopenharmony_ci hdr = dbp->b_addr; 82862306a36Sopenharmony_ci bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 82962306a36Sopenharmony_ci bestsp[use_block] = bf[0].length; 83062306a36Sopenharmony_ci grown = 1; 83162306a36Sopenharmony_ci } else { 83262306a36Sopenharmony_ci /* 83362306a36Sopenharmony_ci * Already had space in some data block. 83462306a36Sopenharmony_ci * Just read that one in. 83562306a36Sopenharmony_ci */ 83662306a36Sopenharmony_ci error = xfs_dir3_data_read(tp, dp, 83762306a36Sopenharmony_ci xfs_dir2_db_to_da(args->geo, use_block), 83862306a36Sopenharmony_ci 0, &dbp); 83962306a36Sopenharmony_ci if (error) { 84062306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 84162306a36Sopenharmony_ci return error; 84262306a36Sopenharmony_ci } 84362306a36Sopenharmony_ci hdr = dbp->b_addr; 84462306a36Sopenharmony_ci bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 84562306a36Sopenharmony_ci grown = 0; 84662306a36Sopenharmony_ci } 84762306a36Sopenharmony_ci /* 84862306a36Sopenharmony_ci * Point to the biggest freespace in our data block. 84962306a36Sopenharmony_ci */ 85062306a36Sopenharmony_ci dup = (xfs_dir2_data_unused_t *) 85162306a36Sopenharmony_ci ((char *)hdr + be16_to_cpu(bf[0].offset)); 85262306a36Sopenharmony_ci needscan = needlog = 0; 85362306a36Sopenharmony_ci /* 85462306a36Sopenharmony_ci * Mark the initial part of our freespace in use for the new entry. 85562306a36Sopenharmony_ci */ 85662306a36Sopenharmony_ci error = xfs_dir2_data_use_free(args, dbp, dup, 85762306a36Sopenharmony_ci (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), 85862306a36Sopenharmony_ci length, &needlog, &needscan); 85962306a36Sopenharmony_ci if (error) { 86062306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 86162306a36Sopenharmony_ci return error; 86262306a36Sopenharmony_ci } 86362306a36Sopenharmony_ci /* 86462306a36Sopenharmony_ci * Initialize our new entry (at last). 86562306a36Sopenharmony_ci */ 86662306a36Sopenharmony_ci dep = (xfs_dir2_data_entry_t *)dup; 86762306a36Sopenharmony_ci dep->inumber = cpu_to_be64(args->inumber); 86862306a36Sopenharmony_ci dep->namelen = args->namelen; 86962306a36Sopenharmony_ci memcpy(dep->name, args->name, dep->namelen); 87062306a36Sopenharmony_ci xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); 87162306a36Sopenharmony_ci tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep); 87262306a36Sopenharmony_ci *tagp = cpu_to_be16((char *)dep - (char *)hdr); 87362306a36Sopenharmony_ci /* 87462306a36Sopenharmony_ci * Need to scan fix up the bestfree table. 87562306a36Sopenharmony_ci */ 87662306a36Sopenharmony_ci if (needscan) 87762306a36Sopenharmony_ci xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); 87862306a36Sopenharmony_ci /* 87962306a36Sopenharmony_ci * Need to log the data block's header. 88062306a36Sopenharmony_ci */ 88162306a36Sopenharmony_ci if (needlog) 88262306a36Sopenharmony_ci xfs_dir2_data_log_header(args, dbp); 88362306a36Sopenharmony_ci xfs_dir2_data_log_entry(args, dbp, dep); 88462306a36Sopenharmony_ci /* 88562306a36Sopenharmony_ci * If the bests table needs to be changed, do it. 88662306a36Sopenharmony_ci * Log the change unless we've already done that. 88762306a36Sopenharmony_ci */ 88862306a36Sopenharmony_ci if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) { 88962306a36Sopenharmony_ci bestsp[use_block] = bf[0].length; 89062306a36Sopenharmony_ci if (!grown) 89162306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block); 89262306a36Sopenharmony_ci } 89362306a36Sopenharmony_ci 89462306a36Sopenharmony_ci lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, 89562306a36Sopenharmony_ci highstale, &lfloglow, &lfloghigh); 89662306a36Sopenharmony_ci 89762306a36Sopenharmony_ci /* 89862306a36Sopenharmony_ci * Fill in the new leaf entry. 89962306a36Sopenharmony_ci */ 90062306a36Sopenharmony_ci lep->hashval = cpu_to_be32(args->hashval); 90162306a36Sopenharmony_ci lep->address = cpu_to_be32( 90262306a36Sopenharmony_ci xfs_dir2_db_off_to_dataptr(args->geo, use_block, 90362306a36Sopenharmony_ci be16_to_cpu(*tagp))); 90462306a36Sopenharmony_ci /* 90562306a36Sopenharmony_ci * Log the leaf fields and give up the buffers. 90662306a36Sopenharmony_ci */ 90762306a36Sopenharmony_ci xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 90862306a36Sopenharmony_ci xfs_dir3_leaf_log_header(args, lbp); 90962306a36Sopenharmony_ci xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh); 91062306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 91162306a36Sopenharmony_ci xfs_dir3_data_check(dp, dbp); 91262306a36Sopenharmony_ci return 0; 91362306a36Sopenharmony_ci} 91462306a36Sopenharmony_ci 91562306a36Sopenharmony_ci/* 91662306a36Sopenharmony_ci * Compact out any stale entries in the leaf. 91762306a36Sopenharmony_ci * Log the header and changed leaf entries, if any. 91862306a36Sopenharmony_ci */ 91962306a36Sopenharmony_civoid 92062306a36Sopenharmony_cixfs_dir3_leaf_compact( 92162306a36Sopenharmony_ci xfs_da_args_t *args, /* operation arguments */ 92262306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *leafhdr, 92362306a36Sopenharmony_ci struct xfs_buf *bp) /* leaf buffer */ 92462306a36Sopenharmony_ci{ 92562306a36Sopenharmony_ci int from; /* source leaf index */ 92662306a36Sopenharmony_ci xfs_dir2_leaf_t *leaf; /* leaf structure */ 92762306a36Sopenharmony_ci int loglow; /* first leaf entry to log */ 92862306a36Sopenharmony_ci int to; /* target leaf index */ 92962306a36Sopenharmony_ci struct xfs_inode *dp = args->dp; 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_ci leaf = bp->b_addr; 93262306a36Sopenharmony_ci if (!leafhdr->stale) 93362306a36Sopenharmony_ci return; 93462306a36Sopenharmony_ci 93562306a36Sopenharmony_ci /* 93662306a36Sopenharmony_ci * Compress out the stale entries in place. 93762306a36Sopenharmony_ci */ 93862306a36Sopenharmony_ci for (from = to = 0, loglow = -1; from < leafhdr->count; from++) { 93962306a36Sopenharmony_ci if (leafhdr->ents[from].address == 94062306a36Sopenharmony_ci cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 94162306a36Sopenharmony_ci continue; 94262306a36Sopenharmony_ci /* 94362306a36Sopenharmony_ci * Only actually copy the entries that are different. 94462306a36Sopenharmony_ci */ 94562306a36Sopenharmony_ci if (from > to) { 94662306a36Sopenharmony_ci if (loglow == -1) 94762306a36Sopenharmony_ci loglow = to; 94862306a36Sopenharmony_ci leafhdr->ents[to] = leafhdr->ents[from]; 94962306a36Sopenharmony_ci } 95062306a36Sopenharmony_ci to++; 95162306a36Sopenharmony_ci } 95262306a36Sopenharmony_ci /* 95362306a36Sopenharmony_ci * Update and log the header, log the leaf entries. 95462306a36Sopenharmony_ci */ 95562306a36Sopenharmony_ci ASSERT(leafhdr->stale == from - to); 95662306a36Sopenharmony_ci leafhdr->count -= leafhdr->stale; 95762306a36Sopenharmony_ci leafhdr->stale = 0; 95862306a36Sopenharmony_ci 95962306a36Sopenharmony_ci xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr); 96062306a36Sopenharmony_ci xfs_dir3_leaf_log_header(args, bp); 96162306a36Sopenharmony_ci if (loglow != -1) 96262306a36Sopenharmony_ci xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1); 96362306a36Sopenharmony_ci} 96462306a36Sopenharmony_ci 96562306a36Sopenharmony_ci/* 96662306a36Sopenharmony_ci * Compact the leaf entries, removing stale ones. 96762306a36Sopenharmony_ci * Leave one stale entry behind - the one closest to our 96862306a36Sopenharmony_ci * insertion index - and the caller will shift that one to our insertion 96962306a36Sopenharmony_ci * point later. 97062306a36Sopenharmony_ci * Return new insertion index, where the remaining stale entry is, 97162306a36Sopenharmony_ci * and leaf logging indices. 97262306a36Sopenharmony_ci */ 97362306a36Sopenharmony_civoid 97462306a36Sopenharmony_cixfs_dir3_leaf_compact_x1( 97562306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *leafhdr, 97662306a36Sopenharmony_ci struct xfs_dir2_leaf_entry *ents, 97762306a36Sopenharmony_ci int *indexp, /* insertion index */ 97862306a36Sopenharmony_ci int *lowstalep, /* out: stale entry before us */ 97962306a36Sopenharmony_ci int *highstalep, /* out: stale entry after us */ 98062306a36Sopenharmony_ci int *lowlogp, /* out: low log index */ 98162306a36Sopenharmony_ci int *highlogp) /* out: high log index */ 98262306a36Sopenharmony_ci{ 98362306a36Sopenharmony_ci int from; /* source copy index */ 98462306a36Sopenharmony_ci int highstale; /* stale entry at/after index */ 98562306a36Sopenharmony_ci int index; /* insertion index */ 98662306a36Sopenharmony_ci int keepstale; /* source index of kept stale */ 98762306a36Sopenharmony_ci int lowstale; /* stale entry before index */ 98862306a36Sopenharmony_ci int newindex=0; /* new insertion index */ 98962306a36Sopenharmony_ci int to; /* destination copy index */ 99062306a36Sopenharmony_ci 99162306a36Sopenharmony_ci ASSERT(leafhdr->stale > 1); 99262306a36Sopenharmony_ci index = *indexp; 99362306a36Sopenharmony_ci 99462306a36Sopenharmony_ci xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale); 99562306a36Sopenharmony_ci 99662306a36Sopenharmony_ci /* 99762306a36Sopenharmony_ci * Pick the better of lowstale and highstale. 99862306a36Sopenharmony_ci */ 99962306a36Sopenharmony_ci if (lowstale >= 0 && 100062306a36Sopenharmony_ci (highstale == leafhdr->count || 100162306a36Sopenharmony_ci index - lowstale <= highstale - index)) 100262306a36Sopenharmony_ci keepstale = lowstale; 100362306a36Sopenharmony_ci else 100462306a36Sopenharmony_ci keepstale = highstale; 100562306a36Sopenharmony_ci /* 100662306a36Sopenharmony_ci * Copy the entries in place, removing all the stale entries 100762306a36Sopenharmony_ci * except keepstale. 100862306a36Sopenharmony_ci */ 100962306a36Sopenharmony_ci for (from = to = 0; from < leafhdr->count; from++) { 101062306a36Sopenharmony_ci /* 101162306a36Sopenharmony_ci * Notice the new value of index. 101262306a36Sopenharmony_ci */ 101362306a36Sopenharmony_ci if (index == from) 101462306a36Sopenharmony_ci newindex = to; 101562306a36Sopenharmony_ci if (from != keepstale && 101662306a36Sopenharmony_ci ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { 101762306a36Sopenharmony_ci if (from == to) 101862306a36Sopenharmony_ci *lowlogp = to; 101962306a36Sopenharmony_ci continue; 102062306a36Sopenharmony_ci } 102162306a36Sopenharmony_ci /* 102262306a36Sopenharmony_ci * Record the new keepstale value for the insertion. 102362306a36Sopenharmony_ci */ 102462306a36Sopenharmony_ci if (from == keepstale) 102562306a36Sopenharmony_ci lowstale = highstale = to; 102662306a36Sopenharmony_ci /* 102762306a36Sopenharmony_ci * Copy only the entries that have moved. 102862306a36Sopenharmony_ci */ 102962306a36Sopenharmony_ci if (from > to) 103062306a36Sopenharmony_ci ents[to] = ents[from]; 103162306a36Sopenharmony_ci to++; 103262306a36Sopenharmony_ci } 103362306a36Sopenharmony_ci ASSERT(from > to); 103462306a36Sopenharmony_ci /* 103562306a36Sopenharmony_ci * If the insertion point was past the last entry, 103662306a36Sopenharmony_ci * set the new insertion point accordingly. 103762306a36Sopenharmony_ci */ 103862306a36Sopenharmony_ci if (index == from) 103962306a36Sopenharmony_ci newindex = to; 104062306a36Sopenharmony_ci *indexp = newindex; 104162306a36Sopenharmony_ci /* 104262306a36Sopenharmony_ci * Adjust the leaf header values. 104362306a36Sopenharmony_ci */ 104462306a36Sopenharmony_ci leafhdr->count -= from - to; 104562306a36Sopenharmony_ci leafhdr->stale = 1; 104662306a36Sopenharmony_ci /* 104762306a36Sopenharmony_ci * Remember the low/high stale value only in the "right" 104862306a36Sopenharmony_ci * direction. 104962306a36Sopenharmony_ci */ 105062306a36Sopenharmony_ci if (lowstale >= newindex) 105162306a36Sopenharmony_ci lowstale = -1; 105262306a36Sopenharmony_ci else 105362306a36Sopenharmony_ci highstale = leafhdr->count; 105462306a36Sopenharmony_ci *highlogp = leafhdr->count - 1; 105562306a36Sopenharmony_ci *lowstalep = lowstale; 105662306a36Sopenharmony_ci *highstalep = highstale; 105762306a36Sopenharmony_ci} 105862306a36Sopenharmony_ci 105962306a36Sopenharmony_ci/* 106062306a36Sopenharmony_ci * Log the bests entries indicated from a leaf1 block. 106162306a36Sopenharmony_ci */ 106262306a36Sopenharmony_cistatic void 106362306a36Sopenharmony_cixfs_dir3_leaf_log_bests( 106462306a36Sopenharmony_ci struct xfs_da_args *args, 106562306a36Sopenharmony_ci struct xfs_buf *bp, /* leaf buffer */ 106662306a36Sopenharmony_ci int first, /* first entry to log */ 106762306a36Sopenharmony_ci int last) /* last entry to log */ 106862306a36Sopenharmony_ci{ 106962306a36Sopenharmony_ci __be16 *firstb; /* pointer to first entry */ 107062306a36Sopenharmony_ci __be16 *lastb; /* pointer to last entry */ 107162306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf = bp->b_addr; 107262306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 107362306a36Sopenharmony_ci 107462306a36Sopenharmony_ci ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || 107562306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)); 107662306a36Sopenharmony_ci 107762306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 107862306a36Sopenharmony_ci firstb = xfs_dir2_leaf_bests_p(ltp) + first; 107962306a36Sopenharmony_ci lastb = xfs_dir2_leaf_bests_p(ltp) + last; 108062306a36Sopenharmony_ci xfs_trans_log_buf(args->trans, bp, 108162306a36Sopenharmony_ci (uint)((char *)firstb - (char *)leaf), 108262306a36Sopenharmony_ci (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1)); 108362306a36Sopenharmony_ci} 108462306a36Sopenharmony_ci 108562306a36Sopenharmony_ci/* 108662306a36Sopenharmony_ci * Log the leaf entries indicated from a leaf1 or leafn block. 108762306a36Sopenharmony_ci */ 108862306a36Sopenharmony_civoid 108962306a36Sopenharmony_cixfs_dir3_leaf_log_ents( 109062306a36Sopenharmony_ci struct xfs_da_args *args, 109162306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *hdr, 109262306a36Sopenharmony_ci struct xfs_buf *bp, 109362306a36Sopenharmony_ci int first, 109462306a36Sopenharmony_ci int last) 109562306a36Sopenharmony_ci{ 109662306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */ 109762306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */ 109862306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf = bp->b_addr; 109962306a36Sopenharmony_ci 110062306a36Sopenharmony_ci ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || 110162306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || 110262306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || 110362306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); 110462306a36Sopenharmony_ci 110562306a36Sopenharmony_ci firstlep = &hdr->ents[first]; 110662306a36Sopenharmony_ci lastlep = &hdr->ents[last]; 110762306a36Sopenharmony_ci xfs_trans_log_buf(args->trans, bp, 110862306a36Sopenharmony_ci (uint)((char *)firstlep - (char *)leaf), 110962306a36Sopenharmony_ci (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1)); 111062306a36Sopenharmony_ci} 111162306a36Sopenharmony_ci 111262306a36Sopenharmony_ci/* 111362306a36Sopenharmony_ci * Log the header of the leaf1 or leafn block. 111462306a36Sopenharmony_ci */ 111562306a36Sopenharmony_civoid 111662306a36Sopenharmony_cixfs_dir3_leaf_log_header( 111762306a36Sopenharmony_ci struct xfs_da_args *args, 111862306a36Sopenharmony_ci struct xfs_buf *bp) 111962306a36Sopenharmony_ci{ 112062306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf = bp->b_addr; 112162306a36Sopenharmony_ci 112262306a36Sopenharmony_ci ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || 112362306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || 112462306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || 112562306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); 112662306a36Sopenharmony_ci 112762306a36Sopenharmony_ci xfs_trans_log_buf(args->trans, bp, 112862306a36Sopenharmony_ci (uint)((char *)&leaf->hdr - (char *)leaf), 112962306a36Sopenharmony_ci args->geo->leaf_hdr_size - 1); 113062306a36Sopenharmony_ci} 113162306a36Sopenharmony_ci 113262306a36Sopenharmony_ci/* 113362306a36Sopenharmony_ci * Log the tail of the leaf1 block. 113462306a36Sopenharmony_ci */ 113562306a36Sopenharmony_ciSTATIC void 113662306a36Sopenharmony_cixfs_dir3_leaf_log_tail( 113762306a36Sopenharmony_ci struct xfs_da_args *args, 113862306a36Sopenharmony_ci struct xfs_buf *bp) 113962306a36Sopenharmony_ci{ 114062306a36Sopenharmony_ci struct xfs_dir2_leaf *leaf = bp->b_addr; 114162306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 114262306a36Sopenharmony_ci 114362306a36Sopenharmony_ci ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || 114462306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || 114562306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || 114662306a36Sopenharmony_ci leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); 114762306a36Sopenharmony_ci 114862306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 114962306a36Sopenharmony_ci xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf), 115062306a36Sopenharmony_ci (uint)(args->geo->blksize - 1)); 115162306a36Sopenharmony_ci} 115262306a36Sopenharmony_ci 115362306a36Sopenharmony_ci/* 115462306a36Sopenharmony_ci * Look up the entry referred to by args in the leaf format directory. 115562306a36Sopenharmony_ci * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which 115662306a36Sopenharmony_ci * is also used by the node-format code. 115762306a36Sopenharmony_ci */ 115862306a36Sopenharmony_ciint 115962306a36Sopenharmony_cixfs_dir2_leaf_lookup( 116062306a36Sopenharmony_ci xfs_da_args_t *args) /* operation arguments */ 116162306a36Sopenharmony_ci{ 116262306a36Sopenharmony_ci struct xfs_buf *dbp; /* data block buffer */ 116362306a36Sopenharmony_ci xfs_dir2_data_entry_t *dep; /* data block entry */ 116462306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 116562306a36Sopenharmony_ci int error; /* error return code */ 116662306a36Sopenharmony_ci int index; /* found entry index */ 116762306a36Sopenharmony_ci struct xfs_buf *lbp; /* leaf buffer */ 116862306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 116962306a36Sopenharmony_ci xfs_trans_t *tp; /* transaction pointer */ 117062306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 117162306a36Sopenharmony_ci 117262306a36Sopenharmony_ci trace_xfs_dir2_leaf_lookup(args); 117362306a36Sopenharmony_ci 117462306a36Sopenharmony_ci /* 117562306a36Sopenharmony_ci * Look up name in the leaf block, returning both buffers and index. 117662306a36Sopenharmony_ci */ 117762306a36Sopenharmony_ci error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr); 117862306a36Sopenharmony_ci if (error) 117962306a36Sopenharmony_ci return error; 118062306a36Sopenharmony_ci 118162306a36Sopenharmony_ci tp = args->trans; 118262306a36Sopenharmony_ci dp = args->dp; 118362306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 118462306a36Sopenharmony_ci 118562306a36Sopenharmony_ci /* 118662306a36Sopenharmony_ci * Get to the leaf entry and contained data entry address. 118762306a36Sopenharmony_ci */ 118862306a36Sopenharmony_ci lep = &leafhdr.ents[index]; 118962306a36Sopenharmony_ci 119062306a36Sopenharmony_ci /* 119162306a36Sopenharmony_ci * Point to the data entry. 119262306a36Sopenharmony_ci */ 119362306a36Sopenharmony_ci dep = (xfs_dir2_data_entry_t *) 119462306a36Sopenharmony_ci ((char *)dbp->b_addr + 119562306a36Sopenharmony_ci xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); 119662306a36Sopenharmony_ci /* 119762306a36Sopenharmony_ci * Return the found inode number & CI name if appropriate 119862306a36Sopenharmony_ci */ 119962306a36Sopenharmony_ci args->inumber = be64_to_cpu(dep->inumber); 120062306a36Sopenharmony_ci args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep); 120162306a36Sopenharmony_ci error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); 120262306a36Sopenharmony_ci xfs_trans_brelse(tp, dbp); 120362306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 120462306a36Sopenharmony_ci return error; 120562306a36Sopenharmony_ci} 120662306a36Sopenharmony_ci 120762306a36Sopenharmony_ci/* 120862306a36Sopenharmony_ci * Look up name/hash in the leaf block. 120962306a36Sopenharmony_ci * Fill in indexp with the found index, and dbpp with the data buffer. 121062306a36Sopenharmony_ci * If not found dbpp will be NULL, and ENOENT comes back. 121162306a36Sopenharmony_ci * lbpp will always be filled in with the leaf buffer unless there's an error. 121262306a36Sopenharmony_ci */ 121362306a36Sopenharmony_cistatic int /* error */ 121462306a36Sopenharmony_cixfs_dir2_leaf_lookup_int( 121562306a36Sopenharmony_ci xfs_da_args_t *args, /* operation arguments */ 121662306a36Sopenharmony_ci struct xfs_buf **lbpp, /* out: leaf buffer */ 121762306a36Sopenharmony_ci int *indexp, /* out: index in leaf block */ 121862306a36Sopenharmony_ci struct xfs_buf **dbpp, /* out: data buffer */ 121962306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *leafhdr) 122062306a36Sopenharmony_ci{ 122162306a36Sopenharmony_ci xfs_dir2_db_t curdb = -1; /* current data block number */ 122262306a36Sopenharmony_ci struct xfs_buf *dbp = NULL; /* data buffer */ 122362306a36Sopenharmony_ci xfs_dir2_data_entry_t *dep; /* data entry */ 122462306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 122562306a36Sopenharmony_ci int error; /* error return code */ 122662306a36Sopenharmony_ci int index; /* index in leaf block */ 122762306a36Sopenharmony_ci struct xfs_buf *lbp; /* leaf buffer */ 122862306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 122962306a36Sopenharmony_ci xfs_dir2_leaf_t *leaf; /* leaf structure */ 123062306a36Sopenharmony_ci xfs_mount_t *mp; /* filesystem mount point */ 123162306a36Sopenharmony_ci xfs_dir2_db_t newdb; /* new data block number */ 123262306a36Sopenharmony_ci xfs_trans_t *tp; /* transaction pointer */ 123362306a36Sopenharmony_ci xfs_dir2_db_t cidb = -1; /* case match data block no. */ 123462306a36Sopenharmony_ci enum xfs_dacmp cmp; /* name compare result */ 123562306a36Sopenharmony_ci 123662306a36Sopenharmony_ci dp = args->dp; 123762306a36Sopenharmony_ci tp = args->trans; 123862306a36Sopenharmony_ci mp = dp->i_mount; 123962306a36Sopenharmony_ci 124062306a36Sopenharmony_ci error = xfs_dir3_leaf_read(tp, dp, args->geo->leafblk, &lbp); 124162306a36Sopenharmony_ci if (error) 124262306a36Sopenharmony_ci return error; 124362306a36Sopenharmony_ci 124462306a36Sopenharmony_ci *lbpp = lbp; 124562306a36Sopenharmony_ci leaf = lbp->b_addr; 124662306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 124762306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf); 124862306a36Sopenharmony_ci 124962306a36Sopenharmony_ci /* 125062306a36Sopenharmony_ci * Look for the first leaf entry with our hash value. 125162306a36Sopenharmony_ci */ 125262306a36Sopenharmony_ci index = xfs_dir2_leaf_search_hash(args, lbp); 125362306a36Sopenharmony_ci /* 125462306a36Sopenharmony_ci * Loop over all the entries with the right hash value 125562306a36Sopenharmony_ci * looking to match the name. 125662306a36Sopenharmony_ci */ 125762306a36Sopenharmony_ci for (lep = &leafhdr->ents[index]; 125862306a36Sopenharmony_ci index < leafhdr->count && 125962306a36Sopenharmony_ci be32_to_cpu(lep->hashval) == args->hashval; 126062306a36Sopenharmony_ci lep++, index++) { 126162306a36Sopenharmony_ci /* 126262306a36Sopenharmony_ci * Skip over stale leaf entries. 126362306a36Sopenharmony_ci */ 126462306a36Sopenharmony_ci if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 126562306a36Sopenharmony_ci continue; 126662306a36Sopenharmony_ci /* 126762306a36Sopenharmony_ci * Get the new data block number. 126862306a36Sopenharmony_ci */ 126962306a36Sopenharmony_ci newdb = xfs_dir2_dataptr_to_db(args->geo, 127062306a36Sopenharmony_ci be32_to_cpu(lep->address)); 127162306a36Sopenharmony_ci /* 127262306a36Sopenharmony_ci * If it's not the same as the old data block number, 127362306a36Sopenharmony_ci * need to pitch the old one and read the new one. 127462306a36Sopenharmony_ci */ 127562306a36Sopenharmony_ci if (newdb != curdb) { 127662306a36Sopenharmony_ci if (dbp) 127762306a36Sopenharmony_ci xfs_trans_brelse(tp, dbp); 127862306a36Sopenharmony_ci error = xfs_dir3_data_read(tp, dp, 127962306a36Sopenharmony_ci xfs_dir2_db_to_da(args->geo, newdb), 128062306a36Sopenharmony_ci 0, &dbp); 128162306a36Sopenharmony_ci if (error) { 128262306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 128362306a36Sopenharmony_ci return error; 128462306a36Sopenharmony_ci } 128562306a36Sopenharmony_ci curdb = newdb; 128662306a36Sopenharmony_ci } 128762306a36Sopenharmony_ci /* 128862306a36Sopenharmony_ci * Point to the data entry. 128962306a36Sopenharmony_ci */ 129062306a36Sopenharmony_ci dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr + 129162306a36Sopenharmony_ci xfs_dir2_dataptr_to_off(args->geo, 129262306a36Sopenharmony_ci be32_to_cpu(lep->address))); 129362306a36Sopenharmony_ci /* 129462306a36Sopenharmony_ci * Compare name and if it's an exact match, return the index 129562306a36Sopenharmony_ci * and buffer. If it's the first case-insensitive match, store 129662306a36Sopenharmony_ci * the index and buffer and continue looking for an exact match. 129762306a36Sopenharmony_ci */ 129862306a36Sopenharmony_ci cmp = xfs_dir2_compname(args, dep->name, dep->namelen); 129962306a36Sopenharmony_ci if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { 130062306a36Sopenharmony_ci args->cmpresult = cmp; 130162306a36Sopenharmony_ci *indexp = index; 130262306a36Sopenharmony_ci /* case exact match: return the current buffer. */ 130362306a36Sopenharmony_ci if (cmp == XFS_CMP_EXACT) { 130462306a36Sopenharmony_ci *dbpp = dbp; 130562306a36Sopenharmony_ci return 0; 130662306a36Sopenharmony_ci } 130762306a36Sopenharmony_ci cidb = curdb; 130862306a36Sopenharmony_ci } 130962306a36Sopenharmony_ci } 131062306a36Sopenharmony_ci ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); 131162306a36Sopenharmony_ci /* 131262306a36Sopenharmony_ci * Here, we can only be doing a lookup (not a rename or remove). 131362306a36Sopenharmony_ci * If a case-insensitive match was found earlier, re-read the 131462306a36Sopenharmony_ci * appropriate data block if required and return it. 131562306a36Sopenharmony_ci */ 131662306a36Sopenharmony_ci if (args->cmpresult == XFS_CMP_CASE) { 131762306a36Sopenharmony_ci ASSERT(cidb != -1); 131862306a36Sopenharmony_ci if (cidb != curdb) { 131962306a36Sopenharmony_ci xfs_trans_brelse(tp, dbp); 132062306a36Sopenharmony_ci error = xfs_dir3_data_read(tp, dp, 132162306a36Sopenharmony_ci xfs_dir2_db_to_da(args->geo, cidb), 132262306a36Sopenharmony_ci 0, &dbp); 132362306a36Sopenharmony_ci if (error) { 132462306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 132562306a36Sopenharmony_ci return error; 132662306a36Sopenharmony_ci } 132762306a36Sopenharmony_ci } 132862306a36Sopenharmony_ci *dbpp = dbp; 132962306a36Sopenharmony_ci return 0; 133062306a36Sopenharmony_ci } 133162306a36Sopenharmony_ci /* 133262306a36Sopenharmony_ci * No match found, return -ENOENT. 133362306a36Sopenharmony_ci */ 133462306a36Sopenharmony_ci ASSERT(cidb == -1); 133562306a36Sopenharmony_ci if (dbp) 133662306a36Sopenharmony_ci xfs_trans_brelse(tp, dbp); 133762306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 133862306a36Sopenharmony_ci return -ENOENT; 133962306a36Sopenharmony_ci} 134062306a36Sopenharmony_ci 134162306a36Sopenharmony_ci/* 134262306a36Sopenharmony_ci * Remove an entry from a leaf format directory. 134362306a36Sopenharmony_ci */ 134462306a36Sopenharmony_ciint /* error */ 134562306a36Sopenharmony_cixfs_dir2_leaf_removename( 134662306a36Sopenharmony_ci xfs_da_args_t *args) /* operation arguments */ 134762306a36Sopenharmony_ci{ 134862306a36Sopenharmony_ci struct xfs_da_geometry *geo = args->geo; 134962306a36Sopenharmony_ci __be16 *bestsp; /* leaf block best freespace */ 135062306a36Sopenharmony_ci xfs_dir2_data_hdr_t *hdr; /* data block header */ 135162306a36Sopenharmony_ci xfs_dir2_db_t db; /* data block number */ 135262306a36Sopenharmony_ci struct xfs_buf *dbp; /* data block buffer */ 135362306a36Sopenharmony_ci xfs_dir2_data_entry_t *dep; /* data entry structure */ 135462306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 135562306a36Sopenharmony_ci int error; /* error return code */ 135662306a36Sopenharmony_ci xfs_dir2_db_t i; /* temporary data block # */ 135762306a36Sopenharmony_ci int index; /* index into leaf entries */ 135862306a36Sopenharmony_ci struct xfs_buf *lbp; /* leaf buffer */ 135962306a36Sopenharmony_ci xfs_dir2_leaf_t *leaf; /* leaf structure */ 136062306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 136162306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 136262306a36Sopenharmony_ci int needlog; /* need to log data header */ 136362306a36Sopenharmony_ci int needscan; /* need to rescan data frees */ 136462306a36Sopenharmony_ci xfs_dir2_data_off_t oldbest; /* old value of best free */ 136562306a36Sopenharmony_ci struct xfs_dir2_data_free *bf; /* bestfree table */ 136662306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 136762306a36Sopenharmony_ci 136862306a36Sopenharmony_ci trace_xfs_dir2_leaf_removename(args); 136962306a36Sopenharmony_ci 137062306a36Sopenharmony_ci /* 137162306a36Sopenharmony_ci * Lookup the leaf entry, get the leaf and data blocks read in. 137262306a36Sopenharmony_ci */ 137362306a36Sopenharmony_ci error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr); 137462306a36Sopenharmony_ci if (error) 137562306a36Sopenharmony_ci return error; 137662306a36Sopenharmony_ci 137762306a36Sopenharmony_ci dp = args->dp; 137862306a36Sopenharmony_ci leaf = lbp->b_addr; 137962306a36Sopenharmony_ci hdr = dbp->b_addr; 138062306a36Sopenharmony_ci xfs_dir3_data_check(dp, dbp); 138162306a36Sopenharmony_ci bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 138262306a36Sopenharmony_ci 138362306a36Sopenharmony_ci /* 138462306a36Sopenharmony_ci * Point to the leaf entry, use that to point to the data entry. 138562306a36Sopenharmony_ci */ 138662306a36Sopenharmony_ci lep = &leafhdr.ents[index]; 138762306a36Sopenharmony_ci db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address)); 138862306a36Sopenharmony_ci dep = (xfs_dir2_data_entry_t *)((char *)hdr + 138962306a36Sopenharmony_ci xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address))); 139062306a36Sopenharmony_ci needscan = needlog = 0; 139162306a36Sopenharmony_ci oldbest = be16_to_cpu(bf[0].length); 139262306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(geo, leaf); 139362306a36Sopenharmony_ci bestsp = xfs_dir2_leaf_bests_p(ltp); 139462306a36Sopenharmony_ci if (be16_to_cpu(bestsp[db]) != oldbest) { 139562306a36Sopenharmony_ci xfs_buf_mark_corrupt(lbp); 139662306a36Sopenharmony_ci return -EFSCORRUPTED; 139762306a36Sopenharmony_ci } 139862306a36Sopenharmony_ci /* 139962306a36Sopenharmony_ci * Mark the former data entry unused. 140062306a36Sopenharmony_ci */ 140162306a36Sopenharmony_ci xfs_dir2_data_make_free(args, dbp, 140262306a36Sopenharmony_ci (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), 140362306a36Sopenharmony_ci xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog, 140462306a36Sopenharmony_ci &needscan); 140562306a36Sopenharmony_ci /* 140662306a36Sopenharmony_ci * We just mark the leaf entry stale by putting a null in it. 140762306a36Sopenharmony_ci */ 140862306a36Sopenharmony_ci leafhdr.stale++; 140962306a36Sopenharmony_ci xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 141062306a36Sopenharmony_ci xfs_dir3_leaf_log_header(args, lbp); 141162306a36Sopenharmony_ci 141262306a36Sopenharmony_ci lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); 141362306a36Sopenharmony_ci xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index); 141462306a36Sopenharmony_ci 141562306a36Sopenharmony_ci /* 141662306a36Sopenharmony_ci * Scan the freespace in the data block again if necessary, 141762306a36Sopenharmony_ci * log the data block header if necessary. 141862306a36Sopenharmony_ci */ 141962306a36Sopenharmony_ci if (needscan) 142062306a36Sopenharmony_ci xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); 142162306a36Sopenharmony_ci if (needlog) 142262306a36Sopenharmony_ci xfs_dir2_data_log_header(args, dbp); 142362306a36Sopenharmony_ci /* 142462306a36Sopenharmony_ci * If the longest freespace in the data block has changed, 142562306a36Sopenharmony_ci * put the new value in the bests table and log that. 142662306a36Sopenharmony_ci */ 142762306a36Sopenharmony_ci if (be16_to_cpu(bf[0].length) != oldbest) { 142862306a36Sopenharmony_ci bestsp[db] = bf[0].length; 142962306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, db, db); 143062306a36Sopenharmony_ci } 143162306a36Sopenharmony_ci xfs_dir3_data_check(dp, dbp); 143262306a36Sopenharmony_ci /* 143362306a36Sopenharmony_ci * If the data block is now empty then get rid of the data block. 143462306a36Sopenharmony_ci */ 143562306a36Sopenharmony_ci if (be16_to_cpu(bf[0].length) == 143662306a36Sopenharmony_ci geo->blksize - geo->data_entry_offset) { 143762306a36Sopenharmony_ci ASSERT(db != geo->datablk); 143862306a36Sopenharmony_ci if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { 143962306a36Sopenharmony_ci /* 144062306a36Sopenharmony_ci * Nope, can't get rid of it because it caused 144162306a36Sopenharmony_ci * allocation of a bmap btree block to do so. 144262306a36Sopenharmony_ci * Just go on, returning success, leaving the 144362306a36Sopenharmony_ci * empty block in place. 144462306a36Sopenharmony_ci */ 144562306a36Sopenharmony_ci if (error == -ENOSPC && args->total == 0) 144662306a36Sopenharmony_ci error = 0; 144762306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 144862306a36Sopenharmony_ci return error; 144962306a36Sopenharmony_ci } 145062306a36Sopenharmony_ci dbp = NULL; 145162306a36Sopenharmony_ci /* 145262306a36Sopenharmony_ci * If this is the last data block then compact the 145362306a36Sopenharmony_ci * bests table by getting rid of entries. 145462306a36Sopenharmony_ci */ 145562306a36Sopenharmony_ci if (db == be32_to_cpu(ltp->bestcount) - 1) { 145662306a36Sopenharmony_ci /* 145762306a36Sopenharmony_ci * Look for the last active entry (i). 145862306a36Sopenharmony_ci */ 145962306a36Sopenharmony_ci for (i = db - 1; i > 0; i--) { 146062306a36Sopenharmony_ci if (bestsp[i] != cpu_to_be16(NULLDATAOFF)) 146162306a36Sopenharmony_ci break; 146262306a36Sopenharmony_ci } 146362306a36Sopenharmony_ci /* 146462306a36Sopenharmony_ci * Copy the table down so inactive entries at the 146562306a36Sopenharmony_ci * end are removed. 146662306a36Sopenharmony_ci */ 146762306a36Sopenharmony_ci memmove(&bestsp[db - i], bestsp, 146862306a36Sopenharmony_ci (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp)); 146962306a36Sopenharmony_ci be32_add_cpu(<p->bestcount, -(db - i)); 147062306a36Sopenharmony_ci xfs_dir3_leaf_log_tail(args, lbp); 147162306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, 0, 147262306a36Sopenharmony_ci be32_to_cpu(ltp->bestcount) - 1); 147362306a36Sopenharmony_ci } else 147462306a36Sopenharmony_ci bestsp[db] = cpu_to_be16(NULLDATAOFF); 147562306a36Sopenharmony_ci } 147662306a36Sopenharmony_ci /* 147762306a36Sopenharmony_ci * If the data block was not the first one, drop it. 147862306a36Sopenharmony_ci */ 147962306a36Sopenharmony_ci else if (db != geo->datablk) 148062306a36Sopenharmony_ci dbp = NULL; 148162306a36Sopenharmony_ci 148262306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 148362306a36Sopenharmony_ci /* 148462306a36Sopenharmony_ci * See if we can convert to block form. 148562306a36Sopenharmony_ci */ 148662306a36Sopenharmony_ci return xfs_dir2_leaf_to_block(args, lbp, dbp); 148762306a36Sopenharmony_ci} 148862306a36Sopenharmony_ci 148962306a36Sopenharmony_ci/* 149062306a36Sopenharmony_ci * Replace the inode number in a leaf format directory entry. 149162306a36Sopenharmony_ci */ 149262306a36Sopenharmony_ciint /* error */ 149362306a36Sopenharmony_cixfs_dir2_leaf_replace( 149462306a36Sopenharmony_ci xfs_da_args_t *args) /* operation arguments */ 149562306a36Sopenharmony_ci{ 149662306a36Sopenharmony_ci struct xfs_buf *dbp; /* data block buffer */ 149762306a36Sopenharmony_ci xfs_dir2_data_entry_t *dep; /* data block entry */ 149862306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 149962306a36Sopenharmony_ci int error; /* error return code */ 150062306a36Sopenharmony_ci int index; /* index of leaf entry */ 150162306a36Sopenharmony_ci struct xfs_buf *lbp; /* leaf buffer */ 150262306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 150362306a36Sopenharmony_ci xfs_trans_t *tp; /* transaction pointer */ 150462306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 150562306a36Sopenharmony_ci 150662306a36Sopenharmony_ci trace_xfs_dir2_leaf_replace(args); 150762306a36Sopenharmony_ci 150862306a36Sopenharmony_ci /* 150962306a36Sopenharmony_ci * Look up the entry. 151062306a36Sopenharmony_ci */ 151162306a36Sopenharmony_ci error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr); 151262306a36Sopenharmony_ci if (error) 151362306a36Sopenharmony_ci return error; 151462306a36Sopenharmony_ci 151562306a36Sopenharmony_ci dp = args->dp; 151662306a36Sopenharmony_ci /* 151762306a36Sopenharmony_ci * Point to the leaf entry, get data address from it. 151862306a36Sopenharmony_ci */ 151962306a36Sopenharmony_ci lep = &leafhdr.ents[index]; 152062306a36Sopenharmony_ci /* 152162306a36Sopenharmony_ci * Point to the data entry. 152262306a36Sopenharmony_ci */ 152362306a36Sopenharmony_ci dep = (xfs_dir2_data_entry_t *) 152462306a36Sopenharmony_ci ((char *)dbp->b_addr + 152562306a36Sopenharmony_ci xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); 152662306a36Sopenharmony_ci ASSERT(args->inumber != be64_to_cpu(dep->inumber)); 152762306a36Sopenharmony_ci /* 152862306a36Sopenharmony_ci * Put the new inode number in, log it. 152962306a36Sopenharmony_ci */ 153062306a36Sopenharmony_ci dep->inumber = cpu_to_be64(args->inumber); 153162306a36Sopenharmony_ci xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); 153262306a36Sopenharmony_ci tp = args->trans; 153362306a36Sopenharmony_ci xfs_dir2_data_log_entry(args, dbp, dep); 153462306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 153562306a36Sopenharmony_ci xfs_trans_brelse(tp, lbp); 153662306a36Sopenharmony_ci return 0; 153762306a36Sopenharmony_ci} 153862306a36Sopenharmony_ci 153962306a36Sopenharmony_ci/* 154062306a36Sopenharmony_ci * Return index in the leaf block (lbp) which is either the first 154162306a36Sopenharmony_ci * one with this hash value, or if there are none, the insert point 154262306a36Sopenharmony_ci * for that hash value. 154362306a36Sopenharmony_ci */ 154462306a36Sopenharmony_ciint /* index value */ 154562306a36Sopenharmony_cixfs_dir2_leaf_search_hash( 154662306a36Sopenharmony_ci xfs_da_args_t *args, /* operation arguments */ 154762306a36Sopenharmony_ci struct xfs_buf *lbp) /* leaf buffer */ 154862306a36Sopenharmony_ci{ 154962306a36Sopenharmony_ci xfs_dahash_t hash=0; /* hash from this entry */ 155062306a36Sopenharmony_ci xfs_dahash_t hashwant; /* hash value looking for */ 155162306a36Sopenharmony_ci int high; /* high leaf index */ 155262306a36Sopenharmony_ci int low; /* low leaf index */ 155362306a36Sopenharmony_ci xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 155462306a36Sopenharmony_ci int mid=0; /* current leaf index */ 155562306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 155662306a36Sopenharmony_ci 155762306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr); 155862306a36Sopenharmony_ci 155962306a36Sopenharmony_ci /* 156062306a36Sopenharmony_ci * Note, the table cannot be empty, so we have to go through the loop. 156162306a36Sopenharmony_ci * Binary search the leaf entries looking for our hash value. 156262306a36Sopenharmony_ci */ 156362306a36Sopenharmony_ci for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1, 156462306a36Sopenharmony_ci hashwant = args->hashval; 156562306a36Sopenharmony_ci low <= high; ) { 156662306a36Sopenharmony_ci mid = (low + high) >> 1; 156762306a36Sopenharmony_ci if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant) 156862306a36Sopenharmony_ci break; 156962306a36Sopenharmony_ci if (hash < hashwant) 157062306a36Sopenharmony_ci low = mid + 1; 157162306a36Sopenharmony_ci else 157262306a36Sopenharmony_ci high = mid - 1; 157362306a36Sopenharmony_ci } 157462306a36Sopenharmony_ci /* 157562306a36Sopenharmony_ci * Found one, back up through all the equal hash values. 157662306a36Sopenharmony_ci */ 157762306a36Sopenharmony_ci if (hash == hashwant) { 157862306a36Sopenharmony_ci while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) { 157962306a36Sopenharmony_ci mid--; 158062306a36Sopenharmony_ci } 158162306a36Sopenharmony_ci } 158262306a36Sopenharmony_ci /* 158362306a36Sopenharmony_ci * Need to point to an entry higher than ours. 158462306a36Sopenharmony_ci */ 158562306a36Sopenharmony_ci else if (hash < hashwant) 158662306a36Sopenharmony_ci mid++; 158762306a36Sopenharmony_ci return mid; 158862306a36Sopenharmony_ci} 158962306a36Sopenharmony_ci 159062306a36Sopenharmony_ci/* 159162306a36Sopenharmony_ci * Trim off a trailing data block. We know it's empty since the leaf 159262306a36Sopenharmony_ci * freespace table says so. 159362306a36Sopenharmony_ci */ 159462306a36Sopenharmony_ciint /* error */ 159562306a36Sopenharmony_cixfs_dir2_leaf_trim_data( 159662306a36Sopenharmony_ci xfs_da_args_t *args, /* operation arguments */ 159762306a36Sopenharmony_ci struct xfs_buf *lbp, /* leaf buffer */ 159862306a36Sopenharmony_ci xfs_dir2_db_t db) /* data block number */ 159962306a36Sopenharmony_ci{ 160062306a36Sopenharmony_ci struct xfs_da_geometry *geo = args->geo; 160162306a36Sopenharmony_ci __be16 *bestsp; /* leaf bests table */ 160262306a36Sopenharmony_ci struct xfs_buf *dbp; /* data block buffer */ 160362306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 160462306a36Sopenharmony_ci int error; /* error return value */ 160562306a36Sopenharmony_ci xfs_dir2_leaf_t *leaf; /* leaf structure */ 160662306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 160762306a36Sopenharmony_ci xfs_trans_t *tp; /* transaction pointer */ 160862306a36Sopenharmony_ci 160962306a36Sopenharmony_ci dp = args->dp; 161062306a36Sopenharmony_ci tp = args->trans; 161162306a36Sopenharmony_ci /* 161262306a36Sopenharmony_ci * Read the offending data block. We need its buffer. 161362306a36Sopenharmony_ci */ 161462306a36Sopenharmony_ci error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(geo, db), 0, &dbp); 161562306a36Sopenharmony_ci if (error) 161662306a36Sopenharmony_ci return error; 161762306a36Sopenharmony_ci 161862306a36Sopenharmony_ci leaf = lbp->b_addr; 161962306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(geo, leaf); 162062306a36Sopenharmony_ci 162162306a36Sopenharmony_ci#ifdef DEBUG 162262306a36Sopenharmony_ci{ 162362306a36Sopenharmony_ci struct xfs_dir2_data_hdr *hdr = dbp->b_addr; 162462306a36Sopenharmony_ci struct xfs_dir2_data_free *bf = 162562306a36Sopenharmony_ci xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 162662306a36Sopenharmony_ci 162762306a36Sopenharmony_ci ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || 162862306a36Sopenharmony_ci hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); 162962306a36Sopenharmony_ci ASSERT(be16_to_cpu(bf[0].length) == 163062306a36Sopenharmony_ci geo->blksize - geo->data_entry_offset); 163162306a36Sopenharmony_ci ASSERT(db == be32_to_cpu(ltp->bestcount) - 1); 163262306a36Sopenharmony_ci} 163362306a36Sopenharmony_ci#endif 163462306a36Sopenharmony_ci 163562306a36Sopenharmony_ci /* 163662306a36Sopenharmony_ci * Get rid of the data block. 163762306a36Sopenharmony_ci */ 163862306a36Sopenharmony_ci if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { 163962306a36Sopenharmony_ci ASSERT(error != -ENOSPC); 164062306a36Sopenharmony_ci xfs_trans_brelse(tp, dbp); 164162306a36Sopenharmony_ci return error; 164262306a36Sopenharmony_ci } 164362306a36Sopenharmony_ci /* 164462306a36Sopenharmony_ci * Eliminate the last bests entry from the table. 164562306a36Sopenharmony_ci */ 164662306a36Sopenharmony_ci bestsp = xfs_dir2_leaf_bests_p(ltp); 164762306a36Sopenharmony_ci be32_add_cpu(<p->bestcount, -1); 164862306a36Sopenharmony_ci memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp)); 164962306a36Sopenharmony_ci xfs_dir3_leaf_log_tail(args, lbp); 165062306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); 165162306a36Sopenharmony_ci return 0; 165262306a36Sopenharmony_ci} 165362306a36Sopenharmony_ci 165462306a36Sopenharmony_cistatic inline size_t 165562306a36Sopenharmony_cixfs_dir3_leaf_size( 165662306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr *hdr, 165762306a36Sopenharmony_ci int counts) 165862306a36Sopenharmony_ci{ 165962306a36Sopenharmony_ci int entries; 166062306a36Sopenharmony_ci int hdrsize; 166162306a36Sopenharmony_ci 166262306a36Sopenharmony_ci entries = hdr->count - hdr->stale; 166362306a36Sopenharmony_ci if (hdr->magic == XFS_DIR2_LEAF1_MAGIC || 166462306a36Sopenharmony_ci hdr->magic == XFS_DIR2_LEAFN_MAGIC) 166562306a36Sopenharmony_ci hdrsize = sizeof(struct xfs_dir2_leaf_hdr); 166662306a36Sopenharmony_ci else 166762306a36Sopenharmony_ci hdrsize = sizeof(struct xfs_dir3_leaf_hdr); 166862306a36Sopenharmony_ci 166962306a36Sopenharmony_ci return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t) 167062306a36Sopenharmony_ci + counts * sizeof(xfs_dir2_data_off_t) 167162306a36Sopenharmony_ci + sizeof(xfs_dir2_leaf_tail_t); 167262306a36Sopenharmony_ci} 167362306a36Sopenharmony_ci 167462306a36Sopenharmony_ci/* 167562306a36Sopenharmony_ci * Convert node form directory to leaf form directory. 167662306a36Sopenharmony_ci * The root of the node form dir needs to already be a LEAFN block. 167762306a36Sopenharmony_ci * Just return if we can't do anything. 167862306a36Sopenharmony_ci */ 167962306a36Sopenharmony_ciint /* error */ 168062306a36Sopenharmony_cixfs_dir2_node_to_leaf( 168162306a36Sopenharmony_ci xfs_da_state_t *state) /* directory operation state */ 168262306a36Sopenharmony_ci{ 168362306a36Sopenharmony_ci xfs_da_args_t *args; /* operation arguments */ 168462306a36Sopenharmony_ci xfs_inode_t *dp; /* incore directory inode */ 168562306a36Sopenharmony_ci int error; /* error return code */ 168662306a36Sopenharmony_ci struct xfs_buf *fbp; /* buffer for freespace block */ 168762306a36Sopenharmony_ci xfs_fileoff_t fo; /* freespace file offset */ 168862306a36Sopenharmony_ci struct xfs_buf *lbp; /* buffer for leaf block */ 168962306a36Sopenharmony_ci xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */ 169062306a36Sopenharmony_ci xfs_dir2_leaf_t *leaf; /* leaf structure */ 169162306a36Sopenharmony_ci xfs_mount_t *mp; /* filesystem mount point */ 169262306a36Sopenharmony_ci int rval; /* successful free trim? */ 169362306a36Sopenharmony_ci xfs_trans_t *tp; /* transaction pointer */ 169462306a36Sopenharmony_ci struct xfs_dir3_icleaf_hdr leafhdr; 169562306a36Sopenharmony_ci struct xfs_dir3_icfree_hdr freehdr; 169662306a36Sopenharmony_ci 169762306a36Sopenharmony_ci /* 169862306a36Sopenharmony_ci * There's more than a leaf level in the btree, so there must 169962306a36Sopenharmony_ci * be multiple leafn blocks. Give up. 170062306a36Sopenharmony_ci */ 170162306a36Sopenharmony_ci if (state->path.active > 1) 170262306a36Sopenharmony_ci return 0; 170362306a36Sopenharmony_ci args = state->args; 170462306a36Sopenharmony_ci 170562306a36Sopenharmony_ci trace_xfs_dir2_node_to_leaf(args); 170662306a36Sopenharmony_ci 170762306a36Sopenharmony_ci mp = state->mp; 170862306a36Sopenharmony_ci dp = args->dp; 170962306a36Sopenharmony_ci tp = args->trans; 171062306a36Sopenharmony_ci /* 171162306a36Sopenharmony_ci * Get the last offset in the file. 171262306a36Sopenharmony_ci */ 171362306a36Sopenharmony_ci if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) { 171462306a36Sopenharmony_ci return error; 171562306a36Sopenharmony_ci } 171662306a36Sopenharmony_ci fo -= args->geo->fsbcount; 171762306a36Sopenharmony_ci /* 171862306a36Sopenharmony_ci * If there are freespace blocks other than the first one, 171962306a36Sopenharmony_ci * take this opportunity to remove trailing empty freespace blocks 172062306a36Sopenharmony_ci * that may have been left behind during no-space-reservation 172162306a36Sopenharmony_ci * operations. 172262306a36Sopenharmony_ci */ 172362306a36Sopenharmony_ci while (fo > args->geo->freeblk) { 172462306a36Sopenharmony_ci if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) { 172562306a36Sopenharmony_ci return error; 172662306a36Sopenharmony_ci } 172762306a36Sopenharmony_ci if (rval) 172862306a36Sopenharmony_ci fo -= args->geo->fsbcount; 172962306a36Sopenharmony_ci else 173062306a36Sopenharmony_ci return 0; 173162306a36Sopenharmony_ci } 173262306a36Sopenharmony_ci /* 173362306a36Sopenharmony_ci * Now find the block just before the freespace block. 173462306a36Sopenharmony_ci */ 173562306a36Sopenharmony_ci if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) { 173662306a36Sopenharmony_ci return error; 173762306a36Sopenharmony_ci } 173862306a36Sopenharmony_ci /* 173962306a36Sopenharmony_ci * If it's not the single leaf block, give up. 174062306a36Sopenharmony_ci */ 174162306a36Sopenharmony_ci if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize) 174262306a36Sopenharmony_ci return 0; 174362306a36Sopenharmony_ci lbp = state->path.blk[0].bp; 174462306a36Sopenharmony_ci leaf = lbp->b_addr; 174562306a36Sopenharmony_ci xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); 174662306a36Sopenharmony_ci 174762306a36Sopenharmony_ci ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || 174862306a36Sopenharmony_ci leafhdr.magic == XFS_DIR3_LEAFN_MAGIC); 174962306a36Sopenharmony_ci 175062306a36Sopenharmony_ci /* 175162306a36Sopenharmony_ci * Read the freespace block. 175262306a36Sopenharmony_ci */ 175362306a36Sopenharmony_ci error = xfs_dir2_free_read(tp, dp, args->geo->freeblk, &fbp); 175462306a36Sopenharmony_ci if (error) 175562306a36Sopenharmony_ci return error; 175662306a36Sopenharmony_ci xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr); 175762306a36Sopenharmony_ci 175862306a36Sopenharmony_ci ASSERT(!freehdr.firstdb); 175962306a36Sopenharmony_ci 176062306a36Sopenharmony_ci /* 176162306a36Sopenharmony_ci * Now see if the leafn and free data will fit in a leaf1. 176262306a36Sopenharmony_ci * If not, release the buffer and give up. 176362306a36Sopenharmony_ci */ 176462306a36Sopenharmony_ci if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) { 176562306a36Sopenharmony_ci xfs_trans_brelse(tp, fbp); 176662306a36Sopenharmony_ci return 0; 176762306a36Sopenharmony_ci } 176862306a36Sopenharmony_ci 176962306a36Sopenharmony_ci /* 177062306a36Sopenharmony_ci * If the leaf has any stale entries in it, compress them out. 177162306a36Sopenharmony_ci */ 177262306a36Sopenharmony_ci if (leafhdr.stale) 177362306a36Sopenharmony_ci xfs_dir3_leaf_compact(args, &leafhdr, lbp); 177462306a36Sopenharmony_ci 177562306a36Sopenharmony_ci lbp->b_ops = &xfs_dir3_leaf1_buf_ops; 177662306a36Sopenharmony_ci xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF); 177762306a36Sopenharmony_ci leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC) 177862306a36Sopenharmony_ci ? XFS_DIR2_LEAF1_MAGIC 177962306a36Sopenharmony_ci : XFS_DIR3_LEAF1_MAGIC; 178062306a36Sopenharmony_ci 178162306a36Sopenharmony_ci /* 178262306a36Sopenharmony_ci * Set up the leaf tail from the freespace block. 178362306a36Sopenharmony_ci */ 178462306a36Sopenharmony_ci ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 178562306a36Sopenharmony_ci ltp->bestcount = cpu_to_be32(freehdr.nvalid); 178662306a36Sopenharmony_ci 178762306a36Sopenharmony_ci /* 178862306a36Sopenharmony_ci * Set up the leaf bests table. 178962306a36Sopenharmony_ci */ 179062306a36Sopenharmony_ci memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests, 179162306a36Sopenharmony_ci freehdr.nvalid * sizeof(xfs_dir2_data_off_t)); 179262306a36Sopenharmony_ci 179362306a36Sopenharmony_ci xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr); 179462306a36Sopenharmony_ci xfs_dir3_leaf_log_header(args, lbp); 179562306a36Sopenharmony_ci xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); 179662306a36Sopenharmony_ci xfs_dir3_leaf_log_tail(args, lbp); 179762306a36Sopenharmony_ci xfs_dir3_leaf_check(dp, lbp); 179862306a36Sopenharmony_ci 179962306a36Sopenharmony_ci /* 180062306a36Sopenharmony_ci * Get rid of the freespace block. 180162306a36Sopenharmony_ci */ 180262306a36Sopenharmony_ci error = xfs_dir2_shrink_inode(args, 180362306a36Sopenharmony_ci xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET), 180462306a36Sopenharmony_ci fbp); 180562306a36Sopenharmony_ci if (error) { 180662306a36Sopenharmony_ci /* 180762306a36Sopenharmony_ci * This can't fail here because it can only happen when 180862306a36Sopenharmony_ci * punching out the middle of an extent, and this is an 180962306a36Sopenharmony_ci * isolated block. 181062306a36Sopenharmony_ci */ 181162306a36Sopenharmony_ci ASSERT(error != -ENOSPC); 181262306a36Sopenharmony_ci return error; 181362306a36Sopenharmony_ci } 181462306a36Sopenharmony_ci fbp = NULL; 181562306a36Sopenharmony_ci /* 181662306a36Sopenharmony_ci * Now see if we can convert the single-leaf directory 181762306a36Sopenharmony_ci * down to a block form directory. 181862306a36Sopenharmony_ci * This routine always kills the dabuf for the leaf, so 181962306a36Sopenharmony_ci * eliminate it from the path. 182062306a36Sopenharmony_ci */ 182162306a36Sopenharmony_ci error = xfs_dir2_leaf_to_block(args, lbp, NULL); 182262306a36Sopenharmony_ci state->path.blk[0].bp = NULL; 182362306a36Sopenharmony_ci return error; 182462306a36Sopenharmony_ci} 1825