10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 230f712c9SDave Chinner /* 330f712c9SDave Chinner * Copyright (c) 2000-2005 Silicon Graphics, Inc. 430f712c9SDave Chinner * Copyright (c) 2013 Red Hat, Inc. 530f712c9SDave Chinner * All Rights Reserved. 630f712c9SDave Chinner */ 730f712c9SDave Chinner #include "xfs.h" 830f712c9SDave Chinner #include "xfs_fs.h" 95467b34bSDarrick J. Wong #include "xfs_shared.h" 1030f712c9SDave Chinner #include "xfs_format.h" 1130f712c9SDave Chinner #include "xfs_log_format.h" 1230f712c9SDave Chinner #include "xfs_trans_resv.h" 1330f712c9SDave Chinner #include "xfs_mount.h" 1430f712c9SDave Chinner #include "xfs_inode.h" 1530f712c9SDave Chinner #include "xfs_bmap.h" 1630f712c9SDave Chinner #include "xfs_dir2.h" 1730f712c9SDave Chinner #include "xfs_dir2_priv.h" 1830f712c9SDave Chinner #include "xfs_error.h" 1930f712c9SDave Chinner #include "xfs_trace.h" 2030f712c9SDave Chinner #include "xfs_trans.h" 2130f712c9SDave Chinner #include "xfs_buf_item.h" 22a45086e2SBrian Foster #include "xfs_log.h" 2330f712c9SDave Chinner 2430f712c9SDave Chinner /* 2530f712c9SDave Chinner * Function declarations. 2630f712c9SDave Chinner */ 2730f712c9SDave Chinner static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args, 2830f712c9SDave Chinner int index); 2930f712c9SDave Chinner static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, 3030f712c9SDave Chinner xfs_da_state_blk_t *blk1, 3130f712c9SDave Chinner xfs_da_state_blk_t *blk2); 3230f712c9SDave Chinner static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp, 3330f712c9SDave Chinner int index, xfs_da_state_blk_t *dblk, 3430f712c9SDave Chinner int *rval); 3530f712c9SDave Chinner 3630f712c9SDave Chinner /* 3730f712c9SDave Chinner * Check internal consistency of a leafn block. 3830f712c9SDave Chinner */ 3930f712c9SDave Chinner #ifdef DEBUG 40a6a781a5SDarrick J. Wong static xfs_failaddr_t 4130f712c9SDave Chinner xfs_dir3_leafn_check( 4230f712c9SDave Chinner struct xfs_inode *dp, 4330f712c9SDave Chinner struct xfs_buf *bp) 4430f712c9SDave Chinner { 4530f712c9SDave Chinner struct xfs_dir2_leaf *leaf = bp->b_addr; 4630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 4730f712c9SDave Chinner 4851842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 4930f712c9SDave Chinner 5030f712c9SDave Chinner if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) { 5130f712c9SDave Chinner struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; 5230f712c9SDave Chinner if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) 53a6a781a5SDarrick J. Wong return __this_address; 5430f712c9SDave Chinner } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC) 55a6a781a5SDarrick J. Wong return __this_address; 5630f712c9SDave Chinner 575ba30919SChristoph Hellwig return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf); 5830f712c9SDave Chinner } 59a6a781a5SDarrick J. Wong 60a6a781a5SDarrick J. Wong static inline void 61a6a781a5SDarrick J. Wong xfs_dir3_leaf_check( 62a6a781a5SDarrick J. Wong struct xfs_inode *dp, 63a6a781a5SDarrick J. Wong struct xfs_buf *bp) 64a6a781a5SDarrick J. Wong { 65a6a781a5SDarrick J. Wong xfs_failaddr_t fa; 66a6a781a5SDarrick J. Wong 67a6a781a5SDarrick J. Wong fa = xfs_dir3_leafn_check(dp, bp); 68a6a781a5SDarrick J. Wong if (!fa) 69a6a781a5SDarrick J. Wong return; 70a6a781a5SDarrick J. Wong xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, 712551a530SDarrick J. Wong bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, 722551a530SDarrick J. Wong fa); 73a6a781a5SDarrick J. Wong ASSERT(0); 74a6a781a5SDarrick J. Wong } 7530f712c9SDave Chinner #else 7630f712c9SDave Chinner #define xfs_dir3_leaf_check(dp, bp) 7730f712c9SDave Chinner #endif 7830f712c9SDave Chinner 79a6a781a5SDarrick J. Wong static xfs_failaddr_t 8030f712c9SDave Chinner xfs_dir3_free_verify( 8130f712c9SDave Chinner struct xfs_buf *bp) 8230f712c9SDave Chinner { 83dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 8430f712c9SDave Chinner struct xfs_dir2_free_hdr *hdr = bp->b_addr; 8530f712c9SDave Chinner 8639708c20SBrian Foster if (!xfs_verify_magic(bp, hdr->magic)) 8739708c20SBrian Foster return __this_address; 8839708c20SBrian Foster 8930f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 9030f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; 9130f712c9SDave Chinner 92ce748eaaSEric Sandeen if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) 93a6a781a5SDarrick J. Wong return __this_address; 9430f712c9SDave Chinner if (be64_to_cpu(hdr3->blkno) != bp->b_bn) 95a6a781a5SDarrick J. Wong return __this_address; 96a45086e2SBrian Foster if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) 97a6a781a5SDarrick J. Wong return __this_address; 9830f712c9SDave Chinner } 9930f712c9SDave Chinner 10030f712c9SDave Chinner /* XXX: should bounds check the xfs_dir3_icfree_hdr here */ 10130f712c9SDave Chinner 102a6a781a5SDarrick J. Wong return NULL; 10330f712c9SDave Chinner } 10430f712c9SDave Chinner 10530f712c9SDave Chinner static void 10630f712c9SDave Chinner xfs_dir3_free_read_verify( 10730f712c9SDave Chinner struct xfs_buf *bp) 10830f712c9SDave Chinner { 109dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 110bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 11130f712c9SDave Chinner 11230f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 11330f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF)) 114bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSBADCRC, __this_address); 115bc1a09b8SDarrick J. Wong else { 116bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_verify(bp); 117bc1a09b8SDarrick J. Wong if (fa) 118bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 119bc1a09b8SDarrick J. Wong } 12030f712c9SDave Chinner } 12130f712c9SDave Chinner 12230f712c9SDave Chinner static void 12330f712c9SDave Chinner xfs_dir3_free_write_verify( 12430f712c9SDave Chinner struct xfs_buf *bp) 12530f712c9SDave Chinner { 126dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 127fb1755a6SCarlos Maiolino struct xfs_buf_log_item *bip = bp->b_log_item; 12830f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; 129bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 13030f712c9SDave Chinner 131bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_verify(bp); 132bc1a09b8SDarrick J. Wong if (fa) { 133bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 13430f712c9SDave Chinner return; 13530f712c9SDave Chinner } 13630f712c9SDave Chinner 13730f712c9SDave Chinner if (!xfs_sb_version_hascrc(&mp->m_sb)) 13830f712c9SDave Chinner return; 13930f712c9SDave Chinner 14030f712c9SDave Chinner if (bip) 14130f712c9SDave Chinner hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); 14230f712c9SDave Chinner 14330f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF); 14430f712c9SDave Chinner } 14530f712c9SDave Chinner 14630f712c9SDave Chinner const struct xfs_buf_ops xfs_dir3_free_buf_ops = { 147233135b7SEric Sandeen .name = "xfs_dir3_free", 14839708c20SBrian Foster .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC), 14939708c20SBrian Foster cpu_to_be32(XFS_DIR3_FREE_MAGIC) }, 15030f712c9SDave Chinner .verify_read = xfs_dir3_free_read_verify, 15130f712c9SDave Chinner .verify_write = xfs_dir3_free_write_verify, 152b5572597SDarrick J. Wong .verify_struct = xfs_dir3_free_verify, 15330f712c9SDave Chinner }; 15430f712c9SDave Chinner 155de14c5f5SDarrick J. Wong /* Everything ok in the free block header? */ 156bc1a09b8SDarrick J. Wong static xfs_failaddr_t 157de14c5f5SDarrick J. Wong xfs_dir3_free_header_check( 158de14c5f5SDarrick J. Wong struct xfs_inode *dp, 159de14c5f5SDarrick J. Wong xfs_dablk_t fbno, 160de14c5f5SDarrick J. Wong struct xfs_buf *bp) 161de14c5f5SDarrick J. Wong { 162de14c5f5SDarrick J. Wong struct xfs_mount *mp = dp->i_mount; 163*5893e4feSChristoph Hellwig int maxbests = mp->m_dir_geo->free_max_bests; 164de14c5f5SDarrick J. Wong unsigned int firstdb; 165de14c5f5SDarrick J. Wong 166de14c5f5SDarrick J. Wong firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) - 167de14c5f5SDarrick J. Wong xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) * 168de14c5f5SDarrick J. Wong maxbests; 169de14c5f5SDarrick J. Wong if (xfs_sb_version_hascrc(&mp->m_sb)) { 170de14c5f5SDarrick J. Wong struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; 171de14c5f5SDarrick J. Wong 172de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->firstdb) != firstdb) 173a6a781a5SDarrick J. Wong return __this_address; 174de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->nvalid) > maxbests) 175a6a781a5SDarrick J. Wong return __this_address; 176de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused)) 177a6a781a5SDarrick J. Wong return __this_address; 178de14c5f5SDarrick J. Wong } else { 179de14c5f5SDarrick J. Wong struct xfs_dir2_free_hdr *hdr = bp->b_addr; 180de14c5f5SDarrick J. Wong 181de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->firstdb) != firstdb) 182a6a781a5SDarrick J. Wong return __this_address; 183de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->nvalid) > maxbests) 184a6a781a5SDarrick J. Wong return __this_address; 185de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused)) 186a6a781a5SDarrick J. Wong return __this_address; 187de14c5f5SDarrick J. Wong } 188a6a781a5SDarrick J. Wong return NULL; 189de14c5f5SDarrick J. Wong } 19030f712c9SDave Chinner 19130f712c9SDave Chinner static int 19230f712c9SDave Chinner __xfs_dir3_free_read( 19330f712c9SDave Chinner struct xfs_trans *tp, 19430f712c9SDave Chinner struct xfs_inode *dp, 19530f712c9SDave Chinner xfs_dablk_t fbno, 19630f712c9SDave Chinner xfs_daddr_t mappedbno, 19730f712c9SDave Chinner struct xfs_buf **bpp) 19830f712c9SDave Chinner { 199bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 20030f712c9SDave Chinner int err; 20130f712c9SDave Chinner 20230f712c9SDave Chinner err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, 20330f712c9SDave Chinner XFS_DATA_FORK, &xfs_dir3_free_buf_ops); 204de14c5f5SDarrick J. Wong if (err || !*bpp) 205de14c5f5SDarrick J. Wong return err; 206de14c5f5SDarrick J. Wong 207de14c5f5SDarrick J. Wong /* Check things that we can't do in the verifier. */ 208bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_header_check(dp, fbno, *bpp); 209bc1a09b8SDarrick J. Wong if (fa) { 210bc1a09b8SDarrick J. Wong xfs_verifier_error(*bpp, -EFSCORRUPTED, fa); 211de14c5f5SDarrick J. Wong xfs_trans_brelse(tp, *bpp); 212de14c5f5SDarrick J. Wong return -EFSCORRUPTED; 213de14c5f5SDarrick J. Wong } 21430f712c9SDave Chinner 21530f712c9SDave Chinner /* try read returns without an error or *bpp if it lands in a hole */ 216de14c5f5SDarrick J. Wong if (tp) 21730f712c9SDave Chinner xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF); 218de14c5f5SDarrick J. Wong 219de14c5f5SDarrick J. Wong return 0; 22030f712c9SDave Chinner } 22130f712c9SDave Chinner 2225ba30919SChristoph Hellwig void 2235ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk( 2245ba30919SChristoph Hellwig struct xfs_mount *mp, 2255ba30919SChristoph Hellwig struct xfs_dir3_icfree_hdr *to, 2265ba30919SChristoph Hellwig struct xfs_dir2_free *from) 2275ba30919SChristoph Hellwig { 2285ba30919SChristoph Hellwig if (xfs_sb_version_hascrc(&mp->m_sb)) { 2295ba30919SChristoph Hellwig struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from; 2305ba30919SChristoph Hellwig 2315ba30919SChristoph Hellwig to->magic = be32_to_cpu(from3->hdr.hdr.magic); 2325ba30919SChristoph Hellwig to->firstdb = be32_to_cpu(from3->hdr.firstdb); 2335ba30919SChristoph Hellwig to->nvalid = be32_to_cpu(from3->hdr.nvalid); 2345ba30919SChristoph Hellwig to->nused = be32_to_cpu(from3->hdr.nused); 235a84f3d5cSChristoph Hellwig to->bests = from3->bests; 2365ba30919SChristoph Hellwig 2375ba30919SChristoph Hellwig ASSERT(to->magic == XFS_DIR3_FREE_MAGIC); 2385ba30919SChristoph Hellwig } else { 2395ba30919SChristoph Hellwig to->magic = be32_to_cpu(from->hdr.magic); 2405ba30919SChristoph Hellwig to->firstdb = be32_to_cpu(from->hdr.firstdb); 2415ba30919SChristoph Hellwig to->nvalid = be32_to_cpu(from->hdr.nvalid); 2425ba30919SChristoph Hellwig to->nused = be32_to_cpu(from->hdr.nused); 243a84f3d5cSChristoph Hellwig to->bests = from->bests; 244a84f3d5cSChristoph Hellwig 2455ba30919SChristoph Hellwig ASSERT(to->magic == XFS_DIR2_FREE_MAGIC); 2465ba30919SChristoph Hellwig } 2475ba30919SChristoph Hellwig } 2485ba30919SChristoph Hellwig 249200dada7SChristoph Hellwig static void 250200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk( 251200dada7SChristoph Hellwig struct xfs_mount *mp, 252200dada7SChristoph Hellwig struct xfs_dir2_free *to, 253200dada7SChristoph Hellwig struct xfs_dir3_icfree_hdr *from) 254200dada7SChristoph Hellwig { 255200dada7SChristoph Hellwig if (xfs_sb_version_hascrc(&mp->m_sb)) { 256200dada7SChristoph Hellwig struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to; 257200dada7SChristoph Hellwig 258200dada7SChristoph Hellwig ASSERT(from->magic == XFS_DIR3_FREE_MAGIC); 259200dada7SChristoph Hellwig 260200dada7SChristoph Hellwig to3->hdr.hdr.magic = cpu_to_be32(from->magic); 261200dada7SChristoph Hellwig to3->hdr.firstdb = cpu_to_be32(from->firstdb); 262200dada7SChristoph Hellwig to3->hdr.nvalid = cpu_to_be32(from->nvalid); 263200dada7SChristoph Hellwig to3->hdr.nused = cpu_to_be32(from->nused); 264200dada7SChristoph Hellwig } else { 265200dada7SChristoph Hellwig ASSERT(from->magic == XFS_DIR2_FREE_MAGIC); 266200dada7SChristoph Hellwig 267200dada7SChristoph Hellwig to->hdr.magic = cpu_to_be32(from->magic); 268200dada7SChristoph Hellwig to->hdr.firstdb = cpu_to_be32(from->firstdb); 269200dada7SChristoph Hellwig to->hdr.nvalid = cpu_to_be32(from->nvalid); 270200dada7SChristoph Hellwig to->hdr.nused = cpu_to_be32(from->nused); 271200dada7SChristoph Hellwig } 272200dada7SChristoph Hellwig } 273200dada7SChristoph Hellwig 27430f712c9SDave Chinner int 27530f712c9SDave Chinner xfs_dir2_free_read( 27630f712c9SDave Chinner struct xfs_trans *tp, 27730f712c9SDave Chinner struct xfs_inode *dp, 27830f712c9SDave Chinner xfs_dablk_t fbno, 27930f712c9SDave Chinner struct xfs_buf **bpp) 28030f712c9SDave Chinner { 28130f712c9SDave Chinner return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp); 28230f712c9SDave Chinner } 28330f712c9SDave Chinner 28430f712c9SDave Chinner static int 28530f712c9SDave Chinner xfs_dir2_free_try_read( 28630f712c9SDave Chinner struct xfs_trans *tp, 28730f712c9SDave Chinner struct xfs_inode *dp, 28830f712c9SDave Chinner xfs_dablk_t fbno, 28930f712c9SDave Chinner struct xfs_buf **bpp) 29030f712c9SDave Chinner { 29130f712c9SDave Chinner return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp); 29230f712c9SDave Chinner } 29330f712c9SDave Chinner 29430f712c9SDave Chinner static int 29530f712c9SDave Chinner xfs_dir3_free_get_buf( 29630f712c9SDave Chinner xfs_da_args_t *args, 29730f712c9SDave Chinner xfs_dir2_db_t fbno, 29830f712c9SDave Chinner struct xfs_buf **bpp) 29930f712c9SDave Chinner { 30030f712c9SDave Chinner struct xfs_trans *tp = args->trans; 30130f712c9SDave Chinner struct xfs_inode *dp = args->dp; 30230f712c9SDave Chinner struct xfs_mount *mp = dp->i_mount; 30330f712c9SDave Chinner struct xfs_buf *bp; 30430f712c9SDave Chinner int error; 30530f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr; 30630f712c9SDave Chinner 30730f712c9SDave Chinner error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno), 30830f712c9SDave Chinner -1, &bp, XFS_DATA_FORK); 30930f712c9SDave Chinner if (error) 31030f712c9SDave Chinner return error; 31130f712c9SDave Chinner 31230f712c9SDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF); 31330f712c9SDave Chinner bp->b_ops = &xfs_dir3_free_buf_ops; 31430f712c9SDave Chinner 31530f712c9SDave Chinner /* 31630f712c9SDave Chinner * Initialize the new block to be empty, and remember 31730f712c9SDave Chinner * its first slot as our empty slot. 31830f712c9SDave Chinner */ 31930f712c9SDave Chinner memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr)); 32030f712c9SDave Chinner memset(&hdr, 0, sizeof(hdr)); 32130f712c9SDave Chinner 32230f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 32330f712c9SDave Chinner struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; 32430f712c9SDave Chinner 32530f712c9SDave Chinner hdr.magic = XFS_DIR3_FREE_MAGIC; 32630f712c9SDave Chinner 32730f712c9SDave Chinner hdr3->hdr.blkno = cpu_to_be64(bp->b_bn); 32830f712c9SDave Chinner hdr3->hdr.owner = cpu_to_be64(dp->i_ino); 329ce748eaaSEric Sandeen uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid); 33030f712c9SDave Chinner } else 33130f712c9SDave Chinner hdr.magic = XFS_DIR2_FREE_MAGIC; 332200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr); 33330f712c9SDave Chinner *bpp = bp; 33430f712c9SDave Chinner return 0; 33530f712c9SDave Chinner } 33630f712c9SDave Chinner 33730f712c9SDave Chinner /* 33830f712c9SDave Chinner * Log entries from a freespace block. 33930f712c9SDave Chinner */ 34030f712c9SDave Chinner STATIC void 34130f712c9SDave Chinner xfs_dir2_free_log_bests( 34230f712c9SDave Chinner struct xfs_da_args *args, 343a84f3d5cSChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr, 34430f712c9SDave Chinner struct xfs_buf *bp, 34530f712c9SDave Chinner int first, /* first entry to log */ 34630f712c9SDave Chinner int last) /* last entry to log */ 34730f712c9SDave Chinner { 348a84f3d5cSChristoph Hellwig struct xfs_dir2_free *free = bp->b_addr; 34930f712c9SDave Chinner 35030f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 35130f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 35230f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 353a84f3d5cSChristoph Hellwig (char *)&hdr->bests[first] - (char *)free, 354a84f3d5cSChristoph Hellwig (char *)&hdr->bests[last] - (char *)free + 355a84f3d5cSChristoph Hellwig sizeof(hdr->bests[0]) - 1); 35630f712c9SDave Chinner } 35730f712c9SDave Chinner 35830f712c9SDave Chinner /* 35930f712c9SDave Chinner * Log header from a freespace block. 36030f712c9SDave Chinner */ 36130f712c9SDave Chinner static void 36230f712c9SDave Chinner xfs_dir2_free_log_header( 36330f712c9SDave Chinner struct xfs_da_args *args, 36430f712c9SDave Chinner struct xfs_buf *bp) 36530f712c9SDave Chinner { 36630f712c9SDave Chinner #ifdef DEBUG 36730f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 36830f712c9SDave Chinner 36930f712c9SDave Chinner free = bp->b_addr; 37030f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 37130f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 37230f712c9SDave Chinner #endif 37330f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 0, 374ed1d612fSChristoph Hellwig args->geo->free_hdr_size - 1); 37530f712c9SDave Chinner } 37630f712c9SDave Chinner 37730f712c9SDave Chinner /* 37830f712c9SDave Chinner * Convert a leaf-format directory to a node-format directory. 37930f712c9SDave Chinner * We need to change the magic number of the leaf block, and copy 38030f712c9SDave Chinner * the freespace table out of the leaf block into its own block. 38130f712c9SDave Chinner */ 38230f712c9SDave Chinner int /* error */ 38330f712c9SDave Chinner xfs_dir2_leaf_to_node( 38430f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 38530f712c9SDave Chinner struct xfs_buf *lbp) /* leaf buffer */ 38630f712c9SDave Chinner { 38730f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 38830f712c9SDave Chinner int error; /* error return value */ 38930f712c9SDave Chinner struct xfs_buf *fbp; /* freespace buffer */ 39030f712c9SDave Chinner xfs_dir2_db_t fdb; /* freespace block number */ 39130f712c9SDave Chinner __be16 *from; /* pointer to freespace entry */ 39230f712c9SDave Chinner int i; /* leaf freespace index */ 39330f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 39430f712c9SDave Chinner xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 39530f712c9SDave Chinner int n; /* count of live freespc ents */ 39630f712c9SDave Chinner xfs_dir2_data_off_t off; /* freespace entry value */ 39730f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 39830f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 39930f712c9SDave Chinner 40030f712c9SDave Chinner trace_xfs_dir2_leaf_to_node(args); 40130f712c9SDave Chinner 40230f712c9SDave Chinner dp = args->dp; 40330f712c9SDave Chinner tp = args->trans; 40430f712c9SDave Chinner /* 40530f712c9SDave Chinner * Add a freespace block to the directory. 40630f712c9SDave Chinner */ 40730f712c9SDave Chinner if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { 40830f712c9SDave Chinner return error; 40930f712c9SDave Chinner } 41030f712c9SDave Chinner ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); 41130f712c9SDave Chinner /* 41230f712c9SDave Chinner * Get the buffer for the new freespace block. 41330f712c9SDave Chinner */ 41430f712c9SDave Chinner error = xfs_dir3_free_get_buf(args, fdb, &fbp); 41530f712c9SDave Chinner if (error) 41630f712c9SDave Chinner return error; 41730f712c9SDave Chinner 418a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr); 41930f712c9SDave Chinner leaf = lbp->b_addr; 42030f712c9SDave Chinner ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 4213f883f5bSDarrick J. Wong if (be32_to_cpu(ltp->bestcount) > 422a5155b87SDarrick J. Wong (uint)dp->i_d.di_size / args->geo->blksize) { 423a5155b87SDarrick J. Wong xfs_buf_corruption_error(lbp); 4243f883f5bSDarrick J. Wong return -EFSCORRUPTED; 425a5155b87SDarrick J. Wong } 42630f712c9SDave Chinner 42730f712c9SDave Chinner /* 42830f712c9SDave Chinner * Copy freespace entries from the leaf block to the new block. 42930f712c9SDave Chinner * Count active entries. 43030f712c9SDave Chinner */ 43130f712c9SDave Chinner from = xfs_dir2_leaf_bests_p(ltp); 432a84f3d5cSChristoph Hellwig for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) { 433a84f3d5cSChristoph Hellwig off = be16_to_cpu(*from); 434a84f3d5cSChristoph Hellwig if (off != NULLDATAOFF) 43530f712c9SDave Chinner n++; 436a84f3d5cSChristoph Hellwig freehdr.bests[i] = cpu_to_be16(off); 43730f712c9SDave Chinner } 43830f712c9SDave Chinner 43930f712c9SDave Chinner /* 44030f712c9SDave Chinner * Now initialize the freespace block header. 44130f712c9SDave Chinner */ 44230f712c9SDave Chinner freehdr.nused = n; 44330f712c9SDave Chinner freehdr.nvalid = be32_to_cpu(ltp->bestcount); 44430f712c9SDave Chinner 445200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr); 446a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1); 44730f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 44830f712c9SDave Chinner 44930f712c9SDave Chinner /* 45030f712c9SDave Chinner * Converting the leaf to a leafnode is just a matter of changing the 45130f712c9SDave Chinner * magic number and the ops. Do the change directly to the buffer as 45230f712c9SDave Chinner * it's less work (and less code) than decoding the header to host 45330f712c9SDave Chinner * format and back again. 45430f712c9SDave Chinner */ 45530f712c9SDave Chinner if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)) 45630f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); 45730f712c9SDave Chinner else 45830f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); 45930f712c9SDave Chinner lbp->b_ops = &xfs_dir3_leafn_buf_ops; 46030f712c9SDave Chinner xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF); 46130f712c9SDave Chinner xfs_dir3_leaf_log_header(args, lbp); 46230f712c9SDave Chinner xfs_dir3_leaf_check(dp, lbp); 46330f712c9SDave Chinner return 0; 46430f712c9SDave Chinner } 46530f712c9SDave Chinner 46630f712c9SDave Chinner /* 46730f712c9SDave Chinner * Add a leaf entry to a leaf block in a node-form directory. 46830f712c9SDave Chinner * The other work necessary is done from the caller. 46930f712c9SDave Chinner */ 47030f712c9SDave Chinner static int /* error */ 47130f712c9SDave Chinner xfs_dir2_leafn_add( 47230f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 47379622c7cSDarrick J. Wong struct xfs_da_args *args, /* operation arguments */ 47430f712c9SDave Chinner int index) /* insertion pt for new entry */ 47530f712c9SDave Chinner { 47679622c7cSDarrick J. Wong struct xfs_dir3_icleaf_hdr leafhdr; 47779622c7cSDarrick J. Wong struct xfs_inode *dp = args->dp; 47879622c7cSDarrick J. Wong struct xfs_dir2_leaf *leaf = bp->b_addr; 47979622c7cSDarrick J. Wong struct xfs_dir2_leaf_entry *lep; 48079622c7cSDarrick J. Wong struct xfs_dir2_leaf_entry *ents; 48130f712c9SDave Chinner int compact; /* compacting stale leaves */ 48279622c7cSDarrick J. Wong int highstale = 0; /* next stale entry */ 48330f712c9SDave Chinner int lfloghigh; /* high leaf entry logging */ 48430f712c9SDave Chinner int lfloglow; /* low leaf entry logging */ 48579622c7cSDarrick J. Wong int lowstale = 0; /* previous stale entry */ 48630f712c9SDave Chinner 48730f712c9SDave Chinner trace_xfs_dir2_leafn_add(args, index); 48830f712c9SDave Chinner 48951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 490787b0893SChristoph Hellwig ents = leafhdr.ents; 49130f712c9SDave Chinner 49230f712c9SDave Chinner /* 49330f712c9SDave Chinner * Quick check just to make sure we are not going to index 49430f712c9SDave Chinner * into other peoples memory 49530f712c9SDave Chinner */ 496a5155b87SDarrick J. Wong if (index < 0) { 497a5155b87SDarrick J. Wong xfs_buf_corruption_error(bp); 4982451337dSDave Chinner return -EFSCORRUPTED; 499a5155b87SDarrick J. Wong } 50030f712c9SDave Chinner 50130f712c9SDave Chinner /* 50230f712c9SDave Chinner * If there are already the maximum number of leaf entries in 50330f712c9SDave Chinner * the block, if there are no stale entries it won't fit. 50430f712c9SDave Chinner * Caller will do a split. If there are stale entries we'll do 50530f712c9SDave Chinner * a compact. 50630f712c9SDave Chinner */ 50730f712c9SDave Chinner 508478c7835SChristoph Hellwig if (leafhdr.count == args->geo->leaf_max_ents) { 50930f712c9SDave Chinner if (!leafhdr.stale) 5102451337dSDave Chinner return -ENOSPC; 51130f712c9SDave Chinner compact = leafhdr.stale > 1; 51230f712c9SDave Chinner } else 51330f712c9SDave Chinner compact = 0; 51430f712c9SDave Chinner ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval); 51530f712c9SDave Chinner ASSERT(index == leafhdr.count || 51630f712c9SDave Chinner be32_to_cpu(ents[index].hashval) >= args->hashval); 51730f712c9SDave Chinner 51830f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) 51930f712c9SDave Chinner return 0; 52030f712c9SDave Chinner 52130f712c9SDave Chinner /* 52230f712c9SDave Chinner * Compact out all but one stale leaf entry. Leaves behind 52330f712c9SDave Chinner * the entry closest to index. 52430f712c9SDave Chinner */ 52530f712c9SDave Chinner if (compact) 52630f712c9SDave Chinner xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, 52730f712c9SDave Chinner &highstale, &lfloglow, &lfloghigh); 52830f712c9SDave Chinner else if (leafhdr.stale) { 52930f712c9SDave Chinner /* 53030f712c9SDave Chinner * Set impossible logging indices for this case. 53130f712c9SDave Chinner */ 53230f712c9SDave Chinner lfloglow = leafhdr.count; 53330f712c9SDave Chinner lfloghigh = -1; 53430f712c9SDave Chinner } 53530f712c9SDave Chinner 53630f712c9SDave Chinner /* 53730f712c9SDave Chinner * Insert the new entry, log everything. 53830f712c9SDave Chinner */ 53930f712c9SDave Chinner lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, 54030f712c9SDave Chinner highstale, &lfloglow, &lfloghigh); 54130f712c9SDave Chinner 54230f712c9SDave Chinner lep->hashval = cpu_to_be32(args->hashval); 54330f712c9SDave Chinner lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo, 54430f712c9SDave Chinner args->blkno, args->index)); 54530f712c9SDave Chinner 546163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 54730f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp); 548787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh); 54930f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 55030f712c9SDave Chinner return 0; 55130f712c9SDave Chinner } 55230f712c9SDave Chinner 55330f712c9SDave Chinner #ifdef DEBUG 55430f712c9SDave Chinner static void 55530f712c9SDave Chinner xfs_dir2_free_hdr_check( 55630f712c9SDave Chinner struct xfs_inode *dp, 55730f712c9SDave Chinner struct xfs_buf *bp, 55830f712c9SDave Chinner xfs_dir2_db_t db) 55930f712c9SDave Chinner { 56030f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr; 56130f712c9SDave Chinner 5625ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr); 56330f712c9SDave Chinner 564*5893e4feSChristoph Hellwig ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0); 56530f712c9SDave Chinner ASSERT(hdr.firstdb <= db); 56630f712c9SDave Chinner ASSERT(db < hdr.firstdb + hdr.nvalid); 56730f712c9SDave Chinner } 56830f712c9SDave Chinner #else 56930f712c9SDave Chinner #define xfs_dir2_free_hdr_check(dp, bp, db) 57030f712c9SDave Chinner #endif /* DEBUG */ 57130f712c9SDave Chinner 57230f712c9SDave Chinner /* 57330f712c9SDave Chinner * Return the last hash value in the leaf. 57430f712c9SDave Chinner * Stale entries are ok. 57530f712c9SDave Chinner */ 57630f712c9SDave Chinner xfs_dahash_t /* hash value */ 5778e8877e6SDarrick J. Wong xfs_dir2_leaf_lasthash( 57830f712c9SDave Chinner struct xfs_inode *dp, 57930f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 58030f712c9SDave Chinner int *count) /* count of entries in leaf */ 58130f712c9SDave Chinner { 58230f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 58330f712c9SDave Chinner 584787b0893SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr); 58530f712c9SDave Chinner 58630f712c9SDave Chinner ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || 5878e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR3_LEAFN_MAGIC || 5888e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || 5898e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); 59030f712c9SDave Chinner 59130f712c9SDave Chinner if (count) 59230f712c9SDave Chinner *count = leafhdr.count; 59330f712c9SDave Chinner if (!leafhdr.count) 59430f712c9SDave Chinner return 0; 595787b0893SChristoph Hellwig return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval); 59630f712c9SDave Chinner } 59730f712c9SDave Chinner 59830f712c9SDave Chinner /* 59930f712c9SDave Chinner * Look up a leaf entry for space to add a name in a node-format leaf block. 60030f712c9SDave Chinner * The extrablk in state is a freespace block. 60130f712c9SDave Chinner */ 60230f712c9SDave Chinner STATIC int 60330f712c9SDave Chinner xfs_dir2_leafn_lookup_for_addname( 60430f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 60530f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 60630f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 60730f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 60830f712c9SDave Chinner { 60930f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */ 61030f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */ 61130f712c9SDave Chinner xfs_dir2_db_t curfdb = -1; /* current free block number */ 61230f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 61330f712c9SDave Chinner int error; /* error return value */ 61430f712c9SDave Chinner int fi; /* free entry index */ 61530f712c9SDave Chinner xfs_dir2_free_t *free = NULL; /* free block structure */ 61630f712c9SDave Chinner int index; /* leaf entry index */ 61730f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 61830f712c9SDave Chinner int length; /* length of new data entry */ 61930f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 62030f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 62130f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */ 62230f712c9SDave Chinner xfs_dir2_db_t newfdb; /* new free block number */ 62330f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 62430f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 62530f712c9SDave Chinner 62630f712c9SDave Chinner dp = args->dp; 62730f712c9SDave Chinner tp = args->trans; 62830f712c9SDave Chinner mp = dp->i_mount; 62930f712c9SDave Chinner leaf = bp->b_addr; 63051842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); 63130f712c9SDave Chinner 63230f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 63330f712c9SDave Chinner ASSERT(leafhdr.count > 0); 63430f712c9SDave Chinner 63530f712c9SDave Chinner /* 63630f712c9SDave Chinner * Look up the hash value in the leaf entries. 63730f712c9SDave Chinner */ 63830f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp); 63930f712c9SDave Chinner /* 64030f712c9SDave Chinner * Do we have a buffer coming in? 64130f712c9SDave Chinner */ 64230f712c9SDave Chinner if (state->extravalid) { 64330f712c9SDave Chinner /* If so, it's a free block buffer, get the block number. */ 64430f712c9SDave Chinner curbp = state->extrablk.bp; 64530f712c9SDave Chinner curfdb = state->extrablk.blkno; 64630f712c9SDave Chinner free = curbp->b_addr; 64730f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 64830f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 64930f712c9SDave Chinner } 65030f712c9SDave Chinner length = dp->d_ops->data_entsize(args->namelen); 65130f712c9SDave Chinner /* 65230f712c9SDave Chinner * Loop over leaf entries with the right hash value. 65330f712c9SDave Chinner */ 654787b0893SChristoph Hellwig for (lep = &leafhdr.ents[index]; 65530f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 65630f712c9SDave Chinner lep++, index++) { 65730f712c9SDave Chinner /* 65830f712c9SDave Chinner * Skip stale leaf entries. 65930f712c9SDave Chinner */ 66030f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 66130f712c9SDave Chinner continue; 66230f712c9SDave Chinner /* 66330f712c9SDave Chinner * Pull the data block number from the entry. 66430f712c9SDave Chinner */ 66530f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo, 66630f712c9SDave Chinner be32_to_cpu(lep->address)); 66730f712c9SDave Chinner /* 66830f712c9SDave Chinner * For addname, we're looking for a place to put the new entry. 66930f712c9SDave Chinner * We want to use a data block with an entry of equal 67030f712c9SDave Chinner * hash value to ours if there is one with room. 67130f712c9SDave Chinner * 67230f712c9SDave Chinner * If this block isn't the data block we already have 67330f712c9SDave Chinner * in hand, take a look at it. 67430f712c9SDave Chinner */ 67530f712c9SDave Chinner if (newdb != curdb) { 676a84f3d5cSChristoph Hellwig struct xfs_dir3_icfree_hdr freehdr; 67730f712c9SDave Chinner 67830f712c9SDave Chinner curdb = newdb; 67930f712c9SDave Chinner /* 68030f712c9SDave Chinner * Convert the data block to the free block 68130f712c9SDave Chinner * holding its freespace information. 68230f712c9SDave Chinner */ 68330f712c9SDave Chinner newfdb = dp->d_ops->db_to_fdb(args->geo, newdb); 68430f712c9SDave Chinner /* 68530f712c9SDave Chinner * If it's not the one we have in hand, read it in. 68630f712c9SDave Chinner */ 68730f712c9SDave Chinner if (newfdb != curfdb) { 68830f712c9SDave Chinner /* 68930f712c9SDave Chinner * If we had one before, drop it. 69030f712c9SDave Chinner */ 69130f712c9SDave Chinner if (curbp) 69230f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 69330f712c9SDave Chinner 69430f712c9SDave Chinner error = xfs_dir2_free_read(tp, dp, 69530f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, 69630f712c9SDave Chinner newfdb), 69730f712c9SDave Chinner &curbp); 69830f712c9SDave Chinner if (error) 69930f712c9SDave Chinner return error; 70030f712c9SDave Chinner free = curbp->b_addr; 70130f712c9SDave Chinner 70230f712c9SDave Chinner xfs_dir2_free_hdr_check(dp, curbp, curdb); 70330f712c9SDave Chinner } 70430f712c9SDave Chinner /* 70530f712c9SDave Chinner * Get the index for our entry. 70630f712c9SDave Chinner */ 70730f712c9SDave Chinner fi = dp->d_ops->db_to_fdindex(args->geo, curdb); 70830f712c9SDave Chinner /* 70930f712c9SDave Chinner * If it has room, return it. 71030f712c9SDave Chinner */ 711a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, &freehdr, free); 712a84f3d5cSChristoph Hellwig if (unlikely( 713a84f3d5cSChristoph Hellwig freehdr.bests[fi] == cpu_to_be16(NULLDATAOFF))) { 71430f712c9SDave Chinner XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", 71530f712c9SDave Chinner XFS_ERRLEVEL_LOW, mp); 71630f712c9SDave Chinner if (curfdb != newfdb) 71730f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 7182451337dSDave Chinner return -EFSCORRUPTED; 71930f712c9SDave Chinner } 72030f712c9SDave Chinner curfdb = newfdb; 721a84f3d5cSChristoph Hellwig if (be16_to_cpu(freehdr.bests[fi]) >= length) 72230f712c9SDave Chinner goto out; 72330f712c9SDave Chinner } 72430f712c9SDave Chinner } 72530f712c9SDave Chinner /* Didn't find any space */ 72630f712c9SDave Chinner fi = -1; 72730f712c9SDave Chinner out: 72830f712c9SDave Chinner ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); 72930f712c9SDave Chinner if (curbp) { 73030f712c9SDave Chinner /* Giving back a free block. */ 73130f712c9SDave Chinner state->extravalid = 1; 73230f712c9SDave Chinner state->extrablk.bp = curbp; 73330f712c9SDave Chinner state->extrablk.index = fi; 73430f712c9SDave Chinner state->extrablk.blkno = curfdb; 73530f712c9SDave Chinner 73630f712c9SDave Chinner /* 73730f712c9SDave Chinner * Important: this magic number is not in the buffer - it's for 73830f712c9SDave Chinner * buffer type information and therefore only the free/data type 73930f712c9SDave Chinner * matters here, not whether CRCs are enabled or not. 74030f712c9SDave Chinner */ 74130f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_FREE_MAGIC; 74230f712c9SDave Chinner } else { 74330f712c9SDave Chinner state->extravalid = 0; 74430f712c9SDave Chinner } 74530f712c9SDave Chinner /* 74630f712c9SDave Chinner * Return the index, that will be the insertion point. 74730f712c9SDave Chinner */ 74830f712c9SDave Chinner *indexp = index; 7492451337dSDave Chinner return -ENOENT; 75030f712c9SDave Chinner } 75130f712c9SDave Chinner 75230f712c9SDave Chinner /* 75330f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block. 75430f712c9SDave Chinner * The extrablk in state a data block. 75530f712c9SDave Chinner */ 75630f712c9SDave Chinner STATIC int 75730f712c9SDave Chinner xfs_dir2_leafn_lookup_for_entry( 75830f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 75930f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 76030f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 76130f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 76230f712c9SDave Chinner { 76330f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */ 76430f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */ 76530f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */ 76630f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 76730f712c9SDave Chinner int error; /* error return value */ 76830f712c9SDave Chinner int index; /* leaf entry index */ 76930f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 77030f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 77130f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 77230f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */ 77330f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 77430f712c9SDave Chinner enum xfs_dacmp cmp; /* comparison result */ 77530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 77630f712c9SDave Chinner 77730f712c9SDave Chinner dp = args->dp; 77830f712c9SDave Chinner tp = args->trans; 77930f712c9SDave Chinner mp = dp->i_mount; 78030f712c9SDave Chinner leaf = bp->b_addr; 78151842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); 78230f712c9SDave Chinner 78330f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 784a5155b87SDarrick J. Wong if (leafhdr.count <= 0) { 785a5155b87SDarrick J. Wong xfs_buf_corruption_error(bp); 786858b44dcSDarrick J. Wong return -EFSCORRUPTED; 787a5155b87SDarrick J. Wong } 78830f712c9SDave Chinner 78930f712c9SDave Chinner /* 79030f712c9SDave Chinner * Look up the hash value in the leaf entries. 79130f712c9SDave Chinner */ 79230f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp); 79330f712c9SDave Chinner /* 79430f712c9SDave Chinner * Do we have a buffer coming in? 79530f712c9SDave Chinner */ 79630f712c9SDave Chinner if (state->extravalid) { 79730f712c9SDave Chinner curbp = state->extrablk.bp; 79830f712c9SDave Chinner curdb = state->extrablk.blkno; 79930f712c9SDave Chinner } 80030f712c9SDave Chinner /* 80130f712c9SDave Chinner * Loop over leaf entries with the right hash value. 80230f712c9SDave Chinner */ 803787b0893SChristoph Hellwig for (lep = &leafhdr.ents[index]; 80430f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 80530f712c9SDave Chinner lep++, index++) { 80630f712c9SDave Chinner /* 80730f712c9SDave Chinner * Skip stale leaf entries. 80830f712c9SDave Chinner */ 80930f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 81030f712c9SDave Chinner continue; 81130f712c9SDave Chinner /* 81230f712c9SDave Chinner * Pull the data block number from the entry. 81330f712c9SDave Chinner */ 81430f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo, 81530f712c9SDave Chinner be32_to_cpu(lep->address)); 81630f712c9SDave Chinner /* 81730f712c9SDave Chinner * Not adding a new entry, so we really want to find 81830f712c9SDave Chinner * the name given to us. 81930f712c9SDave Chinner * 82030f712c9SDave Chinner * If it's a different data block, go get it. 82130f712c9SDave Chinner */ 82230f712c9SDave Chinner if (newdb != curdb) { 82330f712c9SDave Chinner /* 82430f712c9SDave Chinner * If we had a block before that we aren't saving 82530f712c9SDave Chinner * for a CI name, drop it 82630f712c9SDave Chinner */ 82730f712c9SDave Chinner if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || 82830f712c9SDave Chinner curdb != state->extrablk.blkno)) 82930f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 83030f712c9SDave Chinner /* 83130f712c9SDave Chinner * If needing the block that is saved with a CI match, 83230f712c9SDave Chinner * use it otherwise read in the new data block. 83330f712c9SDave Chinner */ 83430f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT && 83530f712c9SDave Chinner newdb == state->extrablk.blkno) { 83630f712c9SDave Chinner ASSERT(state->extravalid); 83730f712c9SDave Chinner curbp = state->extrablk.bp; 83830f712c9SDave Chinner } else { 83930f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp, 84030f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, 84130f712c9SDave Chinner newdb), 84230f712c9SDave Chinner -1, &curbp); 84330f712c9SDave Chinner if (error) 84430f712c9SDave Chinner return error; 84530f712c9SDave Chinner } 84630f712c9SDave Chinner xfs_dir3_data_check(dp, curbp); 84730f712c9SDave Chinner curdb = newdb; 84830f712c9SDave Chinner } 84930f712c9SDave Chinner /* 85030f712c9SDave Chinner * Point to the data entry. 85130f712c9SDave Chinner */ 85230f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr + 85330f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo, 85430f712c9SDave Chinner be32_to_cpu(lep->address))); 85530f712c9SDave Chinner /* 85630f712c9SDave Chinner * Compare the entry and if it's an exact match, return 85730f712c9SDave Chinner * EEXIST immediately. If it's the first case-insensitive 85830f712c9SDave Chinner * match, store the block & inode number and continue looking. 85930f712c9SDave Chinner */ 86030f712c9SDave Chinner cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); 86130f712c9SDave Chinner if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { 86230f712c9SDave Chinner /* If there is a CI match block, drop it */ 86330f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT && 86430f712c9SDave Chinner curdb != state->extrablk.blkno) 86530f712c9SDave Chinner xfs_trans_brelse(tp, state->extrablk.bp); 86630f712c9SDave Chinner args->cmpresult = cmp; 86730f712c9SDave Chinner args->inumber = be64_to_cpu(dep->inumber); 86830f712c9SDave Chinner args->filetype = dp->d_ops->data_get_ftype(dep); 86930f712c9SDave Chinner *indexp = index; 87030f712c9SDave Chinner state->extravalid = 1; 87130f712c9SDave Chinner state->extrablk.bp = curbp; 87230f712c9SDave Chinner state->extrablk.blkno = curdb; 87330f712c9SDave Chinner state->extrablk.index = (int)((char *)dep - 87430f712c9SDave Chinner (char *)curbp->b_addr); 87530f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC; 87630f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops; 87730f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); 87830f712c9SDave Chinner if (cmp == XFS_CMP_EXACT) 8792451337dSDave Chinner return -EEXIST; 88030f712c9SDave Chinner } 88130f712c9SDave Chinner } 88230f712c9SDave Chinner ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT)); 88330f712c9SDave Chinner if (curbp) { 88430f712c9SDave Chinner if (args->cmpresult == XFS_CMP_DIFFERENT) { 88530f712c9SDave Chinner /* Giving back last used data block. */ 88630f712c9SDave Chinner state->extravalid = 1; 88730f712c9SDave Chinner state->extrablk.bp = curbp; 88830f712c9SDave Chinner state->extrablk.index = -1; 88930f712c9SDave Chinner state->extrablk.blkno = curdb; 89030f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC; 89130f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops; 89230f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); 89330f712c9SDave Chinner } else { 89430f712c9SDave Chinner /* If the curbp is not the CI match block, drop it */ 89530f712c9SDave Chinner if (state->extrablk.bp != curbp) 89630f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 89730f712c9SDave Chinner } 89830f712c9SDave Chinner } else { 89930f712c9SDave Chinner state->extravalid = 0; 90030f712c9SDave Chinner } 90130f712c9SDave Chinner *indexp = index; 9022451337dSDave Chinner return -ENOENT; 90330f712c9SDave Chinner } 90430f712c9SDave Chinner 90530f712c9SDave Chinner /* 90630f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block. 90730f712c9SDave Chinner * If this is an addname then the extrablk in state is a freespace block, 90830f712c9SDave Chinner * otherwise it's a data block. 90930f712c9SDave Chinner */ 91030f712c9SDave Chinner int 91130f712c9SDave Chinner xfs_dir2_leafn_lookup_int( 91230f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 91330f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 91430f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 91530f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 91630f712c9SDave Chinner { 91730f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_ADDNAME) 91830f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, 91930f712c9SDave Chinner state); 92030f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); 92130f712c9SDave Chinner } 92230f712c9SDave Chinner 92330f712c9SDave Chinner /* 92430f712c9SDave Chinner * Move count leaf entries from source to destination leaf. 92530f712c9SDave Chinner * Log entries and headers. Stale entries are preserved. 92630f712c9SDave Chinner */ 92730f712c9SDave Chinner static void 92830f712c9SDave Chinner xfs_dir3_leafn_moveents( 92930f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 93030f712c9SDave Chinner struct xfs_buf *bp_s, /* source */ 93130f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *shdr, 93230f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents, 93330f712c9SDave Chinner int start_s,/* source leaf index */ 93430f712c9SDave Chinner struct xfs_buf *bp_d, /* destination */ 93530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *dhdr, 93630f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents, 93730f712c9SDave Chinner int start_d,/* destination leaf index */ 93830f712c9SDave Chinner int count) /* count of leaves to copy */ 93930f712c9SDave Chinner { 94030f712c9SDave Chinner int stale; /* count stale leaves copied */ 94130f712c9SDave Chinner 94230f712c9SDave Chinner trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); 94330f712c9SDave Chinner 94430f712c9SDave Chinner /* 94530f712c9SDave Chinner * Silently return if nothing to do. 94630f712c9SDave Chinner */ 94730f712c9SDave Chinner if (count == 0) 94830f712c9SDave Chinner return; 94930f712c9SDave Chinner 95030f712c9SDave Chinner /* 95130f712c9SDave Chinner * If the destination index is not the end of the current 95230f712c9SDave Chinner * destination leaf entries, open up a hole in the destination 95330f712c9SDave Chinner * to hold the new entries. 95430f712c9SDave Chinner */ 95530f712c9SDave Chinner if (start_d < dhdr->count) { 95630f712c9SDave Chinner memmove(&dents[start_d + count], &dents[start_d], 95730f712c9SDave Chinner (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t)); 958787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count, 95930f712c9SDave Chinner count + dhdr->count - 1); 96030f712c9SDave Chinner } 96130f712c9SDave Chinner /* 96230f712c9SDave Chinner * If the source has stale leaves, count the ones in the copy range 96330f712c9SDave Chinner * so we can update the header correctly. 96430f712c9SDave Chinner */ 96530f712c9SDave Chinner if (shdr->stale) { 96630f712c9SDave Chinner int i; /* temp leaf index */ 96730f712c9SDave Chinner 96830f712c9SDave Chinner for (i = start_s, stale = 0; i < start_s + count; i++) { 96930f712c9SDave Chinner if (sents[i].address == 97030f712c9SDave Chinner cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 97130f712c9SDave Chinner stale++; 97230f712c9SDave Chinner } 97330f712c9SDave Chinner } else 97430f712c9SDave Chinner stale = 0; 97530f712c9SDave Chinner /* 97630f712c9SDave Chinner * Copy the leaf entries from source to destination. 97730f712c9SDave Chinner */ 97830f712c9SDave Chinner memcpy(&dents[start_d], &sents[start_s], 97930f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t)); 980787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1); 98130f712c9SDave Chinner 98230f712c9SDave Chinner /* 98330f712c9SDave Chinner * If there are source entries after the ones we copied, 98430f712c9SDave Chinner * delete the ones we copied by sliding the next ones down. 98530f712c9SDave Chinner */ 98630f712c9SDave Chinner if (start_s + count < shdr->count) { 98730f712c9SDave Chinner memmove(&sents[start_s], &sents[start_s + count], 98830f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t)); 989787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s, 990787b0893SChristoph Hellwig start_s + count - 1); 99130f712c9SDave Chinner } 99230f712c9SDave Chinner 99330f712c9SDave Chinner /* 99430f712c9SDave Chinner * Update the headers and log them. 99530f712c9SDave Chinner */ 99630f712c9SDave Chinner shdr->count -= count; 99730f712c9SDave Chinner shdr->stale -= stale; 99830f712c9SDave Chinner dhdr->count += count; 99930f712c9SDave Chinner dhdr->stale += stale; 100030f712c9SDave Chinner } 100130f712c9SDave Chinner 100230f712c9SDave Chinner /* 100330f712c9SDave Chinner * Determine the sort order of two leaf blocks. 100430f712c9SDave Chinner * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. 100530f712c9SDave Chinner */ 100630f712c9SDave Chinner int /* sort order */ 100730f712c9SDave Chinner xfs_dir2_leafn_order( 100830f712c9SDave Chinner struct xfs_inode *dp, 100930f712c9SDave Chinner struct xfs_buf *leaf1_bp, /* leaf1 buffer */ 101030f712c9SDave Chinner struct xfs_buf *leaf2_bp) /* leaf2 buffer */ 101130f712c9SDave Chinner { 101230f712c9SDave Chinner struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr; 101330f712c9SDave Chinner struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr; 101430f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1; 101530f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2; 101630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1; 101730f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 101830f712c9SDave Chinner 101951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1); 102051842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2); 1021787b0893SChristoph Hellwig ents1 = hdr1.ents; 1022787b0893SChristoph Hellwig ents2 = hdr2.ents; 102330f712c9SDave Chinner 102430f712c9SDave Chinner if (hdr1.count > 0 && hdr2.count > 0 && 102530f712c9SDave Chinner (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) || 102630f712c9SDave Chinner be32_to_cpu(ents2[hdr2.count - 1].hashval) < 102730f712c9SDave Chinner be32_to_cpu(ents1[hdr1.count - 1].hashval))) 102830f712c9SDave Chinner return 1; 102930f712c9SDave Chinner return 0; 103030f712c9SDave Chinner } 103130f712c9SDave Chinner 103230f712c9SDave Chinner /* 103330f712c9SDave Chinner * Rebalance leaf entries between two leaf blocks. 103430f712c9SDave Chinner * This is actually only called when the second block is new, 103530f712c9SDave Chinner * though the code deals with the general case. 103630f712c9SDave Chinner * A new entry will be inserted in one of the blocks, and that 103730f712c9SDave Chinner * entry is taken into account when balancing. 103830f712c9SDave Chinner */ 103930f712c9SDave Chinner static void 104030f712c9SDave Chinner xfs_dir2_leafn_rebalance( 104130f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 104230f712c9SDave Chinner xfs_da_state_blk_t *blk1, /* first btree block */ 104330f712c9SDave Chinner xfs_da_state_blk_t *blk2) /* second btree block */ 104430f712c9SDave Chinner { 104530f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 104630f712c9SDave Chinner int count; /* count (& direction) leaves */ 104730f712c9SDave Chinner int isleft; /* new goes in left leaf */ 104830f712c9SDave Chinner xfs_dir2_leaf_t *leaf1; /* first leaf structure */ 104930f712c9SDave Chinner xfs_dir2_leaf_t *leaf2; /* second leaf structure */ 105030f712c9SDave Chinner int mid; /* midpoint leaf index */ 105130f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 105230f712c9SDave Chinner int oldstale; /* old count of stale leaves */ 105330f712c9SDave Chinner #endif 105430f712c9SDave Chinner int oldsum; /* old total leaf count */ 1055e4e542a6SGustavo A. R. Silva int swap_blocks; /* swapped leaf blocks */ 105630f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1; 105730f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2; 105830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1; 105930f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 106030f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 106130f712c9SDave Chinner 106230f712c9SDave Chinner args = state->args; 106330f712c9SDave Chinner /* 106430f712c9SDave Chinner * If the block order is wrong, swap the arguments. 106530f712c9SDave Chinner */ 1066e4e542a6SGustavo A. R. Silva swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp); 1067e4e542a6SGustavo A. R. Silva if (swap_blocks) 1068e4e542a6SGustavo A. R. Silva swap(blk1, blk2); 106930f712c9SDave Chinner 107030f712c9SDave Chinner leaf1 = blk1->bp->b_addr; 107130f712c9SDave Chinner leaf2 = blk2->bp->b_addr; 107251842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1); 107351842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2); 1074787b0893SChristoph Hellwig ents1 = hdr1.ents; 1075787b0893SChristoph Hellwig ents2 = hdr2.ents; 107630f712c9SDave Chinner 107730f712c9SDave Chinner oldsum = hdr1.count + hdr2.count; 107830f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 107930f712c9SDave Chinner oldstale = hdr1.stale + hdr2.stale; 108030f712c9SDave Chinner #endif 108130f712c9SDave Chinner mid = oldsum >> 1; 108230f712c9SDave Chinner 108330f712c9SDave Chinner /* 108430f712c9SDave Chinner * If the old leaf count was odd then the new one will be even, 108530f712c9SDave Chinner * so we need to divide the new count evenly. 108630f712c9SDave Chinner */ 108730f712c9SDave Chinner if (oldsum & 1) { 108830f712c9SDave Chinner xfs_dahash_t midhash; /* middle entry hash value */ 108930f712c9SDave Chinner 109030f712c9SDave Chinner if (mid >= hdr1.count) 109130f712c9SDave Chinner midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval); 109230f712c9SDave Chinner else 109330f712c9SDave Chinner midhash = be32_to_cpu(ents1[mid].hashval); 109430f712c9SDave Chinner isleft = args->hashval <= midhash; 109530f712c9SDave Chinner } 109630f712c9SDave Chinner /* 109730f712c9SDave Chinner * If the old count is even then the new count is odd, so there's 109830f712c9SDave Chinner * no preferred side for the new entry. 109930f712c9SDave Chinner * Pick the left one. 110030f712c9SDave Chinner */ 110130f712c9SDave Chinner else 110230f712c9SDave Chinner isleft = 1; 110330f712c9SDave Chinner /* 110430f712c9SDave Chinner * Calculate moved entry count. Positive means left-to-right, 110530f712c9SDave Chinner * negative means right-to-left. Then move the entries. 110630f712c9SDave Chinner */ 110730f712c9SDave Chinner count = hdr1.count - mid + (isleft == 0); 110830f712c9SDave Chinner if (count > 0) 110930f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1, 111030f712c9SDave Chinner hdr1.count - count, blk2->bp, 111130f712c9SDave Chinner &hdr2, ents2, 0, count); 111230f712c9SDave Chinner else if (count < 0) 111330f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0, 111430f712c9SDave Chinner blk1->bp, &hdr1, ents1, 111530f712c9SDave Chinner hdr1.count, count); 111630f712c9SDave Chinner 111730f712c9SDave Chinner ASSERT(hdr1.count + hdr2.count == oldsum); 111830f712c9SDave Chinner ASSERT(hdr1.stale + hdr2.stale == oldstale); 111930f712c9SDave Chinner 112030f712c9SDave Chinner /* log the changes made when moving the entries */ 1121163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1); 1122163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2); 112330f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk1->bp); 112430f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk2->bp); 112530f712c9SDave Chinner 112630f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk1->bp); 112730f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk2->bp); 112830f712c9SDave Chinner 112930f712c9SDave Chinner /* 113030f712c9SDave Chinner * Mark whether we're inserting into the old or new leaf. 113130f712c9SDave Chinner */ 113230f712c9SDave Chinner if (hdr1.count < hdr2.count) 1133e4e542a6SGustavo A. R. Silva state->inleaf = swap_blocks; 113430f712c9SDave Chinner else if (hdr1.count > hdr2.count) 1135e4e542a6SGustavo A. R. Silva state->inleaf = !swap_blocks; 113630f712c9SDave Chinner else 1137e4e542a6SGustavo A. R. Silva state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count); 113830f712c9SDave Chinner /* 113930f712c9SDave Chinner * Adjust the expected index for insertion. 114030f712c9SDave Chinner */ 114130f712c9SDave Chinner if (!state->inleaf) 114230f712c9SDave Chinner blk2->index = blk1->index - hdr1.count; 114330f712c9SDave Chinner 114430f712c9SDave Chinner /* 114530f712c9SDave Chinner * Finally sanity check just to make sure we are not returning a 114630f712c9SDave Chinner * negative index 114730f712c9SDave Chinner */ 114830f712c9SDave Chinner if (blk2->index < 0) { 114930f712c9SDave Chinner state->inleaf = 1; 115030f712c9SDave Chinner blk2->index = 0; 115130f712c9SDave Chinner xfs_alert(dp->i_mount, 115230f712c9SDave Chinner "%s: picked the wrong leaf? reverting original leaf: blk1->index %d", 115330f712c9SDave Chinner __func__, blk1->index); 115430f712c9SDave Chinner } 115530f712c9SDave Chinner } 115630f712c9SDave Chinner 115730f712c9SDave Chinner static int 115830f712c9SDave Chinner xfs_dir3_data_block_free( 115930f712c9SDave Chinner xfs_da_args_t *args, 116030f712c9SDave Chinner struct xfs_dir2_data_hdr *hdr, 116130f712c9SDave Chinner struct xfs_dir2_free *free, 116230f712c9SDave Chinner xfs_dir2_db_t fdb, 116330f712c9SDave Chinner int findex, 116430f712c9SDave Chinner struct xfs_buf *fbp, 116530f712c9SDave Chinner int longest) 116630f712c9SDave Chinner { 116730f712c9SDave Chinner int logfree = 0; 116830f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 116930f712c9SDave Chinner struct xfs_inode *dp = args->dp; 117030f712c9SDave Chinner 11715ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); 117230f712c9SDave Chinner if (hdr) { 117330f712c9SDave Chinner /* 117430f712c9SDave Chinner * Data block is not empty, just set the free entry to the new 117530f712c9SDave Chinner * value. 117630f712c9SDave Chinner */ 1177a84f3d5cSChristoph Hellwig freehdr.bests[findex] = cpu_to_be16(longest); 1178a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); 117930f712c9SDave Chinner return 0; 118030f712c9SDave Chinner } 118130f712c9SDave Chinner 118230f712c9SDave Chinner /* One less used entry in the free table. */ 118330f712c9SDave Chinner freehdr.nused--; 118430f712c9SDave Chinner 118530f712c9SDave Chinner /* 118630f712c9SDave Chinner * If this was the last entry in the table, we can trim the table size 118730f712c9SDave Chinner * back. There might be other entries at the end referring to 118830f712c9SDave Chinner * non-existent data blocks, get those too. 118930f712c9SDave Chinner */ 119030f712c9SDave Chinner if (findex == freehdr.nvalid - 1) { 119130f712c9SDave Chinner int i; /* free entry index */ 119230f712c9SDave Chinner 119330f712c9SDave Chinner for (i = findex - 1; i >= 0; i--) { 1194a84f3d5cSChristoph Hellwig if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF)) 119530f712c9SDave Chinner break; 119630f712c9SDave Chinner } 119730f712c9SDave Chinner freehdr.nvalid = i + 1; 119830f712c9SDave Chinner logfree = 0; 119930f712c9SDave Chinner } else { 120030f712c9SDave Chinner /* Not the last entry, just punch it out. */ 1201a84f3d5cSChristoph Hellwig freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF); 120230f712c9SDave Chinner logfree = 1; 120330f712c9SDave Chinner } 120430f712c9SDave Chinner 1205200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr); 120630f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 120730f712c9SDave Chinner 120830f712c9SDave Chinner /* 120930f712c9SDave Chinner * If there are no useful entries left in the block, get rid of the 121030f712c9SDave Chinner * block if we can. 121130f712c9SDave Chinner */ 121230f712c9SDave Chinner if (!freehdr.nused) { 121330f712c9SDave Chinner int error; 121430f712c9SDave Chinner 121530f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, fdb, fbp); 121630f712c9SDave Chinner if (error == 0) { 121730f712c9SDave Chinner fbp = NULL; 121830f712c9SDave Chinner logfree = 0; 12192451337dSDave Chinner } else if (error != -ENOSPC || args->total != 0) 122030f712c9SDave Chinner return error; 122130f712c9SDave Chinner /* 122230f712c9SDave Chinner * It's possible to get ENOSPC if there is no 122330f712c9SDave Chinner * space reservation. In this case some one 122430f712c9SDave Chinner * else will eventually get rid of this block. 122530f712c9SDave Chinner */ 122630f712c9SDave Chinner } 122730f712c9SDave Chinner 122830f712c9SDave Chinner /* Log the free entry that changed, unless we got rid of it. */ 122930f712c9SDave Chinner if (logfree) 1230a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); 123130f712c9SDave Chinner return 0; 123230f712c9SDave Chinner } 123330f712c9SDave Chinner 123430f712c9SDave Chinner /* 123530f712c9SDave Chinner * Remove an entry from a node directory. 123630f712c9SDave Chinner * This removes the leaf entry and the data entry, 123730f712c9SDave Chinner * and updates the free block if necessary. 123830f712c9SDave Chinner */ 123930f712c9SDave Chinner static int /* error */ 124030f712c9SDave Chinner xfs_dir2_leafn_remove( 124130f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 124230f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 124330f712c9SDave Chinner int index, /* leaf entry index */ 124430f712c9SDave Chinner xfs_da_state_blk_t *dblk, /* data block */ 124530f712c9SDave Chinner int *rval) /* resulting block needs join */ 124630f712c9SDave Chinner { 124730f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 124830f712c9SDave Chinner xfs_dir2_db_t db; /* data block number */ 124930f712c9SDave Chinner struct xfs_buf *dbp; /* data block buffer */ 125030f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */ 125130f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 125230f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 125330f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 125430f712c9SDave Chinner int longest; /* longest data free entry */ 125530f712c9SDave Chinner int off; /* data block entry offset */ 125630f712c9SDave Chinner int needlog; /* need to log data header */ 125730f712c9SDave Chinner int needscan; /* need to rescan data frees */ 125830f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 125930f712c9SDave Chinner struct xfs_dir2_data_free *bf; /* bestfree table */ 126030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 126130f712c9SDave Chinner 126230f712c9SDave Chinner trace_xfs_dir2_leafn_remove(args, index); 126330f712c9SDave Chinner 126430f712c9SDave Chinner dp = args->dp; 126530f712c9SDave Chinner tp = args->trans; 126630f712c9SDave Chinner leaf = bp->b_addr; 126751842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 126830f712c9SDave Chinner 126930f712c9SDave Chinner /* 127030f712c9SDave Chinner * Point to the entry we're removing. 127130f712c9SDave Chinner */ 1272787b0893SChristoph Hellwig lep = &leafhdr.ents[index]; 127330f712c9SDave Chinner 127430f712c9SDave Chinner /* 127530f712c9SDave Chinner * Extract the data block and offset from the entry. 127630f712c9SDave Chinner */ 127730f712c9SDave Chinner db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); 127830f712c9SDave Chinner ASSERT(dblk->blkno == db); 127930f712c9SDave Chinner off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)); 128030f712c9SDave Chinner ASSERT(dblk->index == off); 128130f712c9SDave Chinner 128230f712c9SDave Chinner /* 128330f712c9SDave Chinner * Kill the leaf entry by marking it stale. 128430f712c9SDave Chinner * Log the leaf block changes. 128530f712c9SDave Chinner */ 128630f712c9SDave Chinner leafhdr.stale++; 1287163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 128830f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp); 128930f712c9SDave Chinner 129030f712c9SDave Chinner lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); 1291787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index); 129230f712c9SDave Chinner 129330f712c9SDave Chinner /* 129430f712c9SDave Chinner * Make the data entry free. Keep track of the longest freespace 129530f712c9SDave Chinner * in the data block in case it changes. 129630f712c9SDave Chinner */ 129730f712c9SDave Chinner dbp = dblk->bp; 129830f712c9SDave Chinner hdr = dbp->b_addr; 129930f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)hdr + off); 130030f712c9SDave Chinner bf = dp->d_ops->data_bestfree_p(hdr); 130130f712c9SDave Chinner longest = be16_to_cpu(bf[0].length); 130230f712c9SDave Chinner needlog = needscan = 0; 130330f712c9SDave Chinner xfs_dir2_data_make_free(args, dbp, off, 130430f712c9SDave Chinner dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); 130530f712c9SDave Chinner /* 130630f712c9SDave Chinner * Rescan the data block freespaces for bestfree. 130730f712c9SDave Chinner * Log the data block header if needed. 130830f712c9SDave Chinner */ 130930f712c9SDave Chinner if (needscan) 131030f712c9SDave Chinner xfs_dir2_data_freescan(dp, hdr, &needlog); 131130f712c9SDave Chinner if (needlog) 131230f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp); 131330f712c9SDave Chinner xfs_dir3_data_check(dp, dbp); 131430f712c9SDave Chinner /* 131530f712c9SDave Chinner * If the longest data block freespace changes, need to update 131630f712c9SDave Chinner * the corresponding freeblock entry. 131730f712c9SDave Chinner */ 131830f712c9SDave Chinner if (longest < be16_to_cpu(bf[0].length)) { 131930f712c9SDave Chinner int error; /* error return value */ 132030f712c9SDave Chinner struct xfs_buf *fbp; /* freeblock buffer */ 132130f712c9SDave Chinner xfs_dir2_db_t fdb; /* freeblock block number */ 132230f712c9SDave Chinner int findex; /* index in freeblock entries */ 132330f712c9SDave Chinner xfs_dir2_free_t *free; /* freeblock structure */ 132430f712c9SDave Chinner 132530f712c9SDave Chinner /* 132630f712c9SDave Chinner * Convert the data block number to a free block, 132730f712c9SDave Chinner * read in the free block. 132830f712c9SDave Chinner */ 132930f712c9SDave Chinner fdb = dp->d_ops->db_to_fdb(args->geo, db); 133030f712c9SDave Chinner error = xfs_dir2_free_read(tp, dp, 133130f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fdb), 133230f712c9SDave Chinner &fbp); 133330f712c9SDave Chinner if (error) 133430f712c9SDave Chinner return error; 133530f712c9SDave Chinner free = fbp->b_addr; 133630f712c9SDave Chinner #ifdef DEBUG 133730f712c9SDave Chinner { 133830f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 13395ba30919SChristoph Hellwig 13405ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); 1341*5893e4feSChristoph Hellwig ASSERT(freehdr.firstdb == args->geo->free_max_bests * 134230f712c9SDave Chinner (fdb - xfs_dir2_byte_to_db(args->geo, 134330f712c9SDave Chinner XFS_DIR2_FREE_OFFSET))); 134430f712c9SDave Chinner } 134530f712c9SDave Chinner #endif 134630f712c9SDave Chinner /* 134730f712c9SDave Chinner * Calculate which entry we need to fix. 134830f712c9SDave Chinner */ 134930f712c9SDave Chinner findex = dp->d_ops->db_to_fdindex(args->geo, db); 135030f712c9SDave Chinner longest = be16_to_cpu(bf[0].length); 135130f712c9SDave Chinner /* 135230f712c9SDave Chinner * If the data block is now empty we can get rid of it 135330f712c9SDave Chinner * (usually). 135430f712c9SDave Chinner */ 135530f712c9SDave Chinner if (longest == args->geo->blksize - 135630f712c9SDave Chinner dp->d_ops->data_entry_offset) { 135730f712c9SDave Chinner /* 135830f712c9SDave Chinner * Try to punch out the data block. 135930f712c9SDave Chinner */ 136030f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, db, dbp); 136130f712c9SDave Chinner if (error == 0) { 136230f712c9SDave Chinner dblk->bp = NULL; 136330f712c9SDave Chinner hdr = NULL; 136430f712c9SDave Chinner } 136530f712c9SDave Chinner /* 136630f712c9SDave Chinner * We can get ENOSPC if there's no space reservation. 136730f712c9SDave Chinner * In this case just drop the buffer and some one else 136830f712c9SDave Chinner * will eventually get rid of the empty block. 136930f712c9SDave Chinner */ 13702451337dSDave Chinner else if (!(error == -ENOSPC && args->total == 0)) 137130f712c9SDave Chinner return error; 137230f712c9SDave Chinner } 137330f712c9SDave Chinner /* 137430f712c9SDave Chinner * If we got rid of the data block, we can eliminate that entry 137530f712c9SDave Chinner * in the free block. 137630f712c9SDave Chinner */ 137730f712c9SDave Chinner error = xfs_dir3_data_block_free(args, hdr, free, 137830f712c9SDave Chinner fdb, findex, fbp, longest); 137930f712c9SDave Chinner if (error) 138030f712c9SDave Chinner return error; 138130f712c9SDave Chinner } 138230f712c9SDave Chinner 138330f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 138430f712c9SDave Chinner /* 138530f712c9SDave Chinner * Return indication of whether this leaf block is empty enough 138630f712c9SDave Chinner * to justify trying to join it with a neighbor. 138730f712c9SDave Chinner */ 1388545910bcSChristoph Hellwig *rval = (args->geo->leaf_hdr_size + 1389787b0893SChristoph Hellwig (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) < 139030f712c9SDave Chinner args->geo->magicpct; 139130f712c9SDave Chinner return 0; 139230f712c9SDave Chinner } 139330f712c9SDave Chinner 139430f712c9SDave Chinner /* 139530f712c9SDave Chinner * Split the leaf entries in the old block into old and new blocks. 139630f712c9SDave Chinner */ 139730f712c9SDave Chinner int /* error */ 139830f712c9SDave Chinner xfs_dir2_leafn_split( 139930f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 140030f712c9SDave Chinner xfs_da_state_blk_t *oldblk, /* original block */ 140130f712c9SDave Chinner xfs_da_state_blk_t *newblk) /* newly created block */ 140230f712c9SDave Chinner { 140330f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 140430f712c9SDave Chinner xfs_dablk_t blkno; /* new leaf block number */ 140530f712c9SDave Chinner int error; /* error return value */ 140630f712c9SDave Chinner struct xfs_inode *dp; 140730f712c9SDave Chinner 140830f712c9SDave Chinner /* 140930f712c9SDave Chinner * Allocate space for a new leaf node. 141030f712c9SDave Chinner */ 141130f712c9SDave Chinner args = state->args; 141230f712c9SDave Chinner dp = args->dp; 141330f712c9SDave Chinner ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); 141430f712c9SDave Chinner error = xfs_da_grow_inode(args, &blkno); 141530f712c9SDave Chinner if (error) { 141630f712c9SDave Chinner return error; 141730f712c9SDave Chinner } 141830f712c9SDave Chinner /* 141930f712c9SDave Chinner * Initialize the new leaf block. 142030f712c9SDave Chinner */ 142130f712c9SDave Chinner error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno), 142230f712c9SDave Chinner &newblk->bp, XFS_DIR2_LEAFN_MAGIC); 142330f712c9SDave Chinner if (error) 142430f712c9SDave Chinner return error; 142530f712c9SDave Chinner 142630f712c9SDave Chinner newblk->blkno = blkno; 142730f712c9SDave Chinner newblk->magic = XFS_DIR2_LEAFN_MAGIC; 142830f712c9SDave Chinner /* 142930f712c9SDave Chinner * Rebalance the entries across the two leaves, link the new 143030f712c9SDave Chinner * block into the leaves. 143130f712c9SDave Chinner */ 143230f712c9SDave Chinner xfs_dir2_leafn_rebalance(state, oldblk, newblk); 143330f712c9SDave Chinner error = xfs_da3_blk_link(state, oldblk, newblk); 143430f712c9SDave Chinner if (error) { 143530f712c9SDave Chinner return error; 143630f712c9SDave Chinner } 143730f712c9SDave Chinner /* 143830f712c9SDave Chinner * Insert the new entry in the correct block. 143930f712c9SDave Chinner */ 144030f712c9SDave Chinner if (state->inleaf) 144130f712c9SDave Chinner error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); 144230f712c9SDave Chinner else 144330f712c9SDave Chinner error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); 144430f712c9SDave Chinner /* 144530f712c9SDave Chinner * Update last hashval in each block since we added the name. 144630f712c9SDave Chinner */ 14478e8877e6SDarrick J. Wong oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL); 14488e8877e6SDarrick J. Wong newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL); 144930f712c9SDave Chinner xfs_dir3_leaf_check(dp, oldblk->bp); 145030f712c9SDave Chinner xfs_dir3_leaf_check(dp, newblk->bp); 145130f712c9SDave Chinner return error; 145230f712c9SDave Chinner } 145330f712c9SDave Chinner 145430f712c9SDave Chinner /* 145530f712c9SDave Chinner * Check a leaf block and its neighbors to see if the block should be 145630f712c9SDave Chinner * collapsed into one or the other neighbor. Always keep the block 145730f712c9SDave Chinner * with the smaller block number. 145830f712c9SDave Chinner * If the current block is over 50% full, don't try to join it, return 0. 145930f712c9SDave Chinner * If the block is empty, fill in the state structure and return 2. 146030f712c9SDave Chinner * If it can be collapsed, fill in the state structure and return 1. 146130f712c9SDave Chinner * If nothing can be done, return 0. 146230f712c9SDave Chinner */ 146330f712c9SDave Chinner int /* error */ 146430f712c9SDave Chinner xfs_dir2_leafn_toosmall( 146530f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 146630f712c9SDave Chinner int *action) /* resulting action to take */ 146730f712c9SDave Chinner { 146830f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */ 146930f712c9SDave Chinner xfs_dablk_t blkno; /* leaf block number */ 147030f712c9SDave Chinner struct xfs_buf *bp; /* leaf buffer */ 147130f712c9SDave Chinner int bytes; /* bytes in use */ 147230f712c9SDave Chinner int count; /* leaf live entry count */ 147330f712c9SDave Chinner int error; /* error return value */ 147430f712c9SDave Chinner int forward; /* sibling block direction */ 147530f712c9SDave Chinner int i; /* sibling counter */ 147630f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 147730f712c9SDave Chinner int rval; /* result from path_shift */ 147830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 147930f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 148030f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 148130f712c9SDave Chinner 148230f712c9SDave Chinner /* 148330f712c9SDave Chinner * Check for the degenerate case of the block being over 50% full. 148430f712c9SDave Chinner * If so, it's not worth even looking to see if we might be able 148530f712c9SDave Chinner * to coalesce with a sibling. 148630f712c9SDave Chinner */ 148730f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 148830f712c9SDave Chinner leaf = blk->bp->b_addr; 148951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 1490787b0893SChristoph Hellwig ents = leafhdr.ents; 149130f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk->bp); 149230f712c9SDave Chinner 149330f712c9SDave Chinner count = leafhdr.count - leafhdr.stale; 1494545910bcSChristoph Hellwig bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]); 149530f712c9SDave Chinner if (bytes > (state->args->geo->blksize >> 1)) { 149630f712c9SDave Chinner /* 149730f712c9SDave Chinner * Blk over 50%, don't try to join. 149830f712c9SDave Chinner */ 149930f712c9SDave Chinner *action = 0; 150030f712c9SDave Chinner return 0; 150130f712c9SDave Chinner } 150230f712c9SDave Chinner /* 150330f712c9SDave Chinner * Check for the degenerate case of the block being empty. 150430f712c9SDave Chinner * If the block is empty, we'll simply delete it, no need to 150530f712c9SDave Chinner * coalesce it with a sibling block. We choose (arbitrarily) 150630f712c9SDave Chinner * to merge with the forward block unless it is NULL. 150730f712c9SDave Chinner */ 150830f712c9SDave Chinner if (count == 0) { 150930f712c9SDave Chinner /* 151030f712c9SDave Chinner * Make altpath point to the block we want to keep and 151130f712c9SDave Chinner * path point to the block we want to drop (this one). 151230f712c9SDave Chinner */ 151330f712c9SDave Chinner forward = (leafhdr.forw != 0); 151430f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path)); 151530f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0, 151630f712c9SDave Chinner &rval); 151730f712c9SDave Chinner if (error) 151830f712c9SDave Chinner return error; 151930f712c9SDave Chinner *action = rval ? 2 : 0; 152030f712c9SDave Chinner return 0; 152130f712c9SDave Chinner } 152230f712c9SDave Chinner /* 152330f712c9SDave Chinner * Examine each sibling block to see if we can coalesce with 152430f712c9SDave Chinner * at least 25% free space to spare. We need to figure out 152530f712c9SDave Chinner * whether to merge with the forward or the backward block. 152630f712c9SDave Chinner * We prefer coalescing with the lower numbered sibling so as 152730f712c9SDave Chinner * to shrink a directory over time. 152830f712c9SDave Chinner */ 152930f712c9SDave Chinner forward = leafhdr.forw < leafhdr.back; 153030f712c9SDave Chinner for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { 153130f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 153230f712c9SDave Chinner 153330f712c9SDave Chinner blkno = forward ? leafhdr.forw : leafhdr.back; 153430f712c9SDave Chinner if (blkno == 0) 153530f712c9SDave Chinner continue; 153630f712c9SDave Chinner /* 153730f712c9SDave Chinner * Read the sibling leaf block. 153830f712c9SDave Chinner */ 153930f712c9SDave Chinner error = xfs_dir3_leafn_read(state->args->trans, dp, 154030f712c9SDave Chinner blkno, -1, &bp); 154130f712c9SDave Chinner if (error) 154230f712c9SDave Chinner return error; 154330f712c9SDave Chinner 154430f712c9SDave Chinner /* 154530f712c9SDave Chinner * Count bytes in the two blocks combined. 154630f712c9SDave Chinner */ 154730f712c9SDave Chinner count = leafhdr.count - leafhdr.stale; 154830f712c9SDave Chinner bytes = state->args->geo->blksize - 154930f712c9SDave Chinner (state->args->geo->blksize >> 2); 155030f712c9SDave Chinner 155130f712c9SDave Chinner leaf = bp->b_addr; 155251842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf); 1553787b0893SChristoph Hellwig ents = hdr2.ents; 155430f712c9SDave Chinner count += hdr2.count - hdr2.stale; 155530f712c9SDave Chinner bytes -= count * sizeof(ents[0]); 155630f712c9SDave Chinner 155730f712c9SDave Chinner /* 155830f712c9SDave Chinner * Fits with at least 25% to spare. 155930f712c9SDave Chinner */ 156030f712c9SDave Chinner if (bytes >= 0) 156130f712c9SDave Chinner break; 156230f712c9SDave Chinner xfs_trans_brelse(state->args->trans, bp); 156330f712c9SDave Chinner } 156430f712c9SDave Chinner /* 156530f712c9SDave Chinner * Didn't like either block, give up. 156630f712c9SDave Chinner */ 156730f712c9SDave Chinner if (i >= 2) { 156830f712c9SDave Chinner *action = 0; 156930f712c9SDave Chinner return 0; 157030f712c9SDave Chinner } 157130f712c9SDave Chinner 157230f712c9SDave Chinner /* 157330f712c9SDave Chinner * Make altpath point to the block we want to keep (the lower 157430f712c9SDave Chinner * numbered block) and path point to the block we want to drop. 157530f712c9SDave Chinner */ 157630f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path)); 157730f712c9SDave Chinner if (blkno < blk->blkno) 157830f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0, 157930f712c9SDave Chinner &rval); 158030f712c9SDave Chinner else 158130f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->path, forward, 0, 158230f712c9SDave Chinner &rval); 158330f712c9SDave Chinner if (error) { 158430f712c9SDave Chinner return error; 158530f712c9SDave Chinner } 158630f712c9SDave Chinner *action = rval ? 0 : 1; 158730f712c9SDave Chinner return 0; 158830f712c9SDave Chinner } 158930f712c9SDave Chinner 159030f712c9SDave Chinner /* 159130f712c9SDave Chinner * Move all the leaf entries from drop_blk to save_blk. 159230f712c9SDave Chinner * This is done as part of a join operation. 159330f712c9SDave Chinner */ 159430f712c9SDave Chinner void 159530f712c9SDave Chinner xfs_dir2_leafn_unbalance( 159630f712c9SDave Chinner xfs_da_state_t *state, /* cursor */ 159730f712c9SDave Chinner xfs_da_state_blk_t *drop_blk, /* dead block */ 159830f712c9SDave Chinner xfs_da_state_blk_t *save_blk) /* surviving block */ 159930f712c9SDave Chinner { 160030f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 160130f712c9SDave Chinner xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ 160230f712c9SDave Chinner xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ 160330f712c9SDave Chinner struct xfs_dir3_icleaf_hdr savehdr; 160430f712c9SDave Chinner struct xfs_dir3_icleaf_hdr drophdr; 160530f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents; 160630f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents; 160730f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 160830f712c9SDave Chinner 160930f712c9SDave Chinner args = state->args; 161030f712c9SDave Chinner ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); 161130f712c9SDave Chinner ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); 161230f712c9SDave Chinner drop_leaf = drop_blk->bp->b_addr; 161330f712c9SDave Chinner save_leaf = save_blk->bp->b_addr; 161430f712c9SDave Chinner 161551842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf); 161651842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf); 1617787b0893SChristoph Hellwig sents = savehdr.ents; 1618787b0893SChristoph Hellwig dents = drophdr.ents; 161930f712c9SDave Chinner 162030f712c9SDave Chinner /* 162130f712c9SDave Chinner * If there are any stale leaf entries, take this opportunity 162230f712c9SDave Chinner * to purge them. 162330f712c9SDave Chinner */ 162430f712c9SDave Chinner if (drophdr.stale) 162530f712c9SDave Chinner xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp); 162630f712c9SDave Chinner if (savehdr.stale) 162730f712c9SDave Chinner xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp); 162830f712c9SDave Chinner 162930f712c9SDave Chinner /* 163030f712c9SDave Chinner * Move the entries from drop to the appropriate end of save. 163130f712c9SDave Chinner */ 163230f712c9SDave Chinner drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval); 163330f712c9SDave Chinner if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp)) 163430f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, 163530f712c9SDave Chinner save_blk->bp, &savehdr, sents, 0, 163630f712c9SDave Chinner drophdr.count); 163730f712c9SDave Chinner else 163830f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, 163930f712c9SDave Chinner save_blk->bp, &savehdr, sents, 164030f712c9SDave Chinner savehdr.count, drophdr.count); 164130f712c9SDave Chinner save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval); 164230f712c9SDave Chinner 164330f712c9SDave Chinner /* log the changes made when moving the entries */ 1644163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr); 1645163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr); 164630f712c9SDave Chinner xfs_dir3_leaf_log_header(args, save_blk->bp); 164730f712c9SDave Chinner xfs_dir3_leaf_log_header(args, drop_blk->bp); 164830f712c9SDave Chinner 164930f712c9SDave Chinner xfs_dir3_leaf_check(dp, save_blk->bp); 165030f712c9SDave Chinner xfs_dir3_leaf_check(dp, drop_blk->bp); 165130f712c9SDave Chinner } 165230f712c9SDave Chinner 165330f712c9SDave Chinner /* 1654a07258a6SDave Chinner * Add a new data block to the directory at the free space index that the caller 1655a07258a6SDave Chinner * has specified. 1656a07258a6SDave Chinner */ 1657a07258a6SDave Chinner static int 1658a07258a6SDave Chinner xfs_dir2_node_add_datablk( 1659a07258a6SDave Chinner struct xfs_da_args *args, 1660a07258a6SDave Chinner struct xfs_da_state_blk *fblk, 1661a07258a6SDave Chinner xfs_dir2_db_t *dbno, 1662a07258a6SDave Chinner struct xfs_buf **dbpp, 1663a07258a6SDave Chinner struct xfs_buf **fbpp, 1664195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr, 1665a07258a6SDave Chinner int *findex) 1666a07258a6SDave Chinner { 1667a07258a6SDave Chinner struct xfs_inode *dp = args->dp; 1668a07258a6SDave Chinner struct xfs_trans *tp = args->trans; 1669a07258a6SDave Chinner struct xfs_mount *mp = dp->i_mount; 1670a07258a6SDave Chinner struct xfs_dir2_data_free *bf; 1671a07258a6SDave Chinner xfs_dir2_db_t fbno; 1672a07258a6SDave Chinner struct xfs_buf *fbp; 1673a07258a6SDave Chinner struct xfs_buf *dbp; 1674a07258a6SDave Chinner int error; 1675a07258a6SDave Chinner 1676a07258a6SDave Chinner /* Not allowed to allocate, return failure. */ 16770e822255SDave Chinner if (args->total == 0) 1678a07258a6SDave Chinner return -ENOSPC; 1679a07258a6SDave Chinner 1680a07258a6SDave Chinner /* Allocate and initialize the new data block. */ 1681a07258a6SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno); 1682a07258a6SDave Chinner if (error) 1683a07258a6SDave Chinner return error; 1684a07258a6SDave Chinner error = xfs_dir3_data_init(args, *dbno, &dbp); 1685a07258a6SDave Chinner if (error) 1686a07258a6SDave Chinner return error; 1687a07258a6SDave Chinner 1688a07258a6SDave Chinner /* 1689a07258a6SDave Chinner * Get the freespace block corresponding to the data block 1690a07258a6SDave Chinner * that was just allocated. 1691a07258a6SDave Chinner */ 1692a07258a6SDave Chinner fbno = dp->d_ops->db_to_fdb(args->geo, *dbno); 1693a07258a6SDave Chinner error = xfs_dir2_free_try_read(tp, dp, 1694a07258a6SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), &fbp); 1695a07258a6SDave Chinner if (error) 1696a07258a6SDave Chinner return error; 1697a07258a6SDave Chinner 1698a07258a6SDave Chinner /* 1699a07258a6SDave Chinner * If there wasn't a freespace block, the read will 1700a07258a6SDave Chinner * return a NULL fbp. Allocate and initialize a new one. 1701a07258a6SDave Chinner */ 1702a07258a6SDave Chinner if (!fbp) { 1703a07258a6SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno); 1704a07258a6SDave Chinner if (error) 1705a07258a6SDave Chinner return error; 1706a07258a6SDave Chinner 1707a07258a6SDave Chinner if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) { 1708a07258a6SDave Chinner xfs_alert(mp, 1709a07258a6SDave Chinner "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld", 1710a07258a6SDave Chinner __func__, (unsigned long long)dp->i_ino, 1711a07258a6SDave Chinner (long long)dp->d_ops->db_to_fdb(args->geo, *dbno), 1712a07258a6SDave Chinner (long long)*dbno, (long long)fbno); 1713a07258a6SDave Chinner if (fblk) { 1714a07258a6SDave Chinner xfs_alert(mp, 1715a07258a6SDave Chinner " fblk "PTR_FMT" blkno %llu index %d magic 0x%x", 1716a07258a6SDave Chinner fblk, (unsigned long long)fblk->blkno, 1717a07258a6SDave Chinner fblk->index, fblk->magic); 1718a07258a6SDave Chinner } else { 1719a07258a6SDave Chinner xfs_alert(mp, " ... fblk is NULL"); 1720a07258a6SDave Chinner } 1721a07258a6SDave Chinner XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); 1722a07258a6SDave Chinner return -EFSCORRUPTED; 1723a07258a6SDave Chinner } 1724a07258a6SDave Chinner 1725a07258a6SDave Chinner /* Get a buffer for the new block. */ 1726a07258a6SDave Chinner error = xfs_dir3_free_get_buf(args, fbno, &fbp); 1727a07258a6SDave Chinner if (error) 1728a07258a6SDave Chinner return error; 1729a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr); 1730a07258a6SDave Chinner 1731a07258a6SDave Chinner /* Remember the first slot as our empty slot. */ 1732195b0a44SChristoph Hellwig hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo, 1733a07258a6SDave Chinner XFS_DIR2_FREE_OFFSET)) * 1734*5893e4feSChristoph Hellwig args->geo->free_max_bests; 1735a07258a6SDave Chinner } else { 1736a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr); 1737a07258a6SDave Chinner } 1738a07258a6SDave Chinner 1739a07258a6SDave Chinner /* Set the freespace block index from the data block number. */ 1740a07258a6SDave Chinner *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno); 1741a07258a6SDave Chinner 1742a07258a6SDave Chinner /* Extend the freespace table if the new data block is off the end. */ 1743195b0a44SChristoph Hellwig if (*findex >= hdr->nvalid) { 1744*5893e4feSChristoph Hellwig ASSERT(*findex < args->geo->free_max_bests); 1745195b0a44SChristoph Hellwig hdr->nvalid = *findex + 1; 1746a84f3d5cSChristoph Hellwig hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF); 1747a07258a6SDave Chinner } 1748a07258a6SDave Chinner 1749a07258a6SDave Chinner /* 1750a07258a6SDave Chinner * If this entry was for an empty data block (this should always be 1751a07258a6SDave Chinner * true) then update the header. 1752a07258a6SDave Chinner */ 1753a84f3d5cSChristoph Hellwig if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) { 1754195b0a44SChristoph Hellwig hdr->nused++; 1755195b0a44SChristoph Hellwig xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr); 1756a07258a6SDave Chinner xfs_dir2_free_log_header(args, fbp); 1757a07258a6SDave Chinner } 1758a07258a6SDave Chinner 1759a07258a6SDave Chinner /* Update the freespace value for the new block in the table. */ 1760195b0a44SChristoph Hellwig bf = dp->d_ops->data_bestfree_p(dbp->b_addr); 1761a84f3d5cSChristoph Hellwig hdr->bests[*findex] = bf[0].length; 1762a07258a6SDave Chinner 1763a07258a6SDave Chinner *dbpp = dbp; 1764a07258a6SDave Chinner *fbpp = fbp; 1765a07258a6SDave Chinner return 0; 1766a07258a6SDave Chinner } 1767a07258a6SDave Chinner 17680e822255SDave Chinner static int 17690e822255SDave Chinner xfs_dir2_node_find_freeblk( 17700e822255SDave Chinner struct xfs_da_args *args, 17710e822255SDave Chinner struct xfs_da_state_blk *fblk, 17720e822255SDave Chinner xfs_dir2_db_t *dbnop, 17730e822255SDave Chinner struct xfs_buf **fbpp, 1774195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr, 17750e822255SDave Chinner int *findexp, 17760e822255SDave Chinner int length) 177730f712c9SDave Chinner { 17780e822255SDave Chinner struct xfs_inode *dp = args->dp; 17790e822255SDave Chinner struct xfs_trans *tp = args->trans; 17800e822255SDave Chinner struct xfs_buf *fbp = NULL; 1781756c6f0fSDave Chinner xfs_dir2_db_t firstfbno; 17820e822255SDave Chinner xfs_dir2_db_t lastfbno; 17830e822255SDave Chinner xfs_dir2_db_t ifbno = -1; 17840e822255SDave Chinner xfs_dir2_db_t dbno = -1; 1785756c6f0fSDave Chinner xfs_dir2_db_t fbno; 17860e822255SDave Chinner xfs_fileoff_t fo; 1787610125abSDave Chinner int findex = 0; 17880e822255SDave Chinner int error; 178930f712c9SDave Chinner 179030f712c9SDave Chinner /* 179130f712c9SDave Chinner * If we came in with a freespace block that means that lookup 179230f712c9SDave Chinner * found an entry with our hash value. This is the freespace 179330f712c9SDave Chinner * block for that data entry. 179430f712c9SDave Chinner */ 179530f712c9SDave Chinner if (fblk) { 179630f712c9SDave Chinner fbp = fblk->bp; 179730f712c9SDave Chinner findex = fblk->index; 1798a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr); 17990e822255SDave Chinner if (findex >= 0) { 18000e822255SDave Chinner /* caller already found the freespace for us. */ 1801195b0a44SChristoph Hellwig ASSERT(findex < hdr->nvalid); 1802a84f3d5cSChristoph Hellwig ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF); 1803a84f3d5cSChristoph Hellwig ASSERT(be16_to_cpu(hdr->bests[findex]) >= length); 1804195b0a44SChristoph Hellwig dbno = hdr->firstdb + findex; 1805a07258a6SDave Chinner goto found_block; 18060e822255SDave Chinner } 18070e822255SDave Chinner 180830f712c9SDave Chinner /* 180930f712c9SDave Chinner * The data block looked at didn't have enough room. 181030f712c9SDave Chinner * We'll start at the beginning of the freespace entries. 181130f712c9SDave Chinner */ 18120e822255SDave Chinner ifbno = fblk->blkno; 1813610125abSDave Chinner xfs_trans_brelse(tp, fbp); 1814610125abSDave Chinner fbp = NULL; 1815610125abSDave Chinner fblk->bp = NULL; 181630f712c9SDave Chinner } 18170e822255SDave Chinner 18180e822255SDave Chinner /* 18190e822255SDave Chinner * If we don't have a data block yet, we're going to scan the freespace 1820610125abSDave Chinner * data for a data block with enough free space in it. 18210e822255SDave Chinner */ 18220e822255SDave Chinner error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK); 18230e822255SDave Chinner if (error) 18240e822255SDave Chinner return error; 18250e822255SDave Chinner lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo); 1826756c6f0fSDave Chinner firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET); 18270e822255SDave Chinner 1828756c6f0fSDave Chinner for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) { 1829610125abSDave Chinner /* If it's ifbno we already looked at it. */ 1830610125abSDave Chinner if (fbno == ifbno) 1831610125abSDave Chinner continue; 1832610125abSDave Chinner 183330f712c9SDave Chinner /* 1834610125abSDave Chinner * Read the block. There can be holes in the freespace blocks, 1835610125abSDave Chinner * so this might not succeed. This should be really rare, so 1836610125abSDave Chinner * there's no reason to avoid it. 183730f712c9SDave Chinner */ 183830f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, 183930f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), 184030f712c9SDave Chinner &fbp); 184130f712c9SDave Chinner if (error) 184230f712c9SDave Chinner return error; 184330f712c9SDave Chinner if (!fbp) 184430f712c9SDave Chinner continue; 1845610125abSDave Chinner 1846a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr); 1847610125abSDave Chinner 1848610125abSDave Chinner /* Scan the free entry array for a large enough free space. */ 1849195b0a44SChristoph Hellwig for (findex = hdr->nvalid - 1; findex >= 0; findex--) { 1850a84f3d5cSChristoph Hellwig if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF && 1851a84f3d5cSChristoph Hellwig be16_to_cpu(hdr->bests[findex]) >= length) { 1852195b0a44SChristoph Hellwig dbno = hdr->firstdb + findex; 1853610125abSDave Chinner goto found_block; 1854610125abSDave Chinner } 1855610125abSDave Chinner } 1856610125abSDave Chinner 1857610125abSDave Chinner /* Didn't find free space, go on to next free block */ 185830f712c9SDave Chinner xfs_trans_brelse(tp, fbp); 185930f712c9SDave Chinner } 1860610125abSDave Chinner 18610e822255SDave Chinner found_block: 18620e822255SDave Chinner *dbnop = dbno; 18630e822255SDave Chinner *fbpp = fbp; 18640e822255SDave Chinner *findexp = findex; 18650e822255SDave Chinner return 0; 18660e822255SDave Chinner } 18670e822255SDave Chinner 18680e822255SDave Chinner /* 18690e822255SDave Chinner * Add the data entry for a node-format directory name addition. 18700e822255SDave Chinner * The leaf entry is added in xfs_dir2_leafn_add. 18710e822255SDave Chinner * We may enter with a freespace block that the lookup found. 18720e822255SDave Chinner */ 18730e822255SDave Chinner static int 18740e822255SDave Chinner xfs_dir2_node_addname_int( 18750e822255SDave Chinner struct xfs_da_args *args, /* operation arguments */ 18760e822255SDave Chinner struct xfs_da_state_blk *fblk) /* optional freespace block */ 18770e822255SDave Chinner { 18780e822255SDave Chinner struct xfs_dir2_data_unused *dup; /* data unused entry pointer */ 18790e822255SDave Chinner struct xfs_dir2_data_entry *dep; /* data entry pointer */ 18800e822255SDave Chinner struct xfs_dir2_data_hdr *hdr; /* data block header */ 18810e822255SDave Chinner struct xfs_dir2_data_free *bf; 18820e822255SDave Chinner struct xfs_trans *tp = args->trans; 18830e822255SDave Chinner struct xfs_inode *dp = args->dp; 1884195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr freehdr; 18850e822255SDave Chinner struct xfs_buf *dbp; /* data block buffer */ 18860e822255SDave Chinner struct xfs_buf *fbp; /* freespace buffer */ 18870e822255SDave Chinner xfs_dir2_data_aoff_t aoff; 18880e822255SDave Chinner xfs_dir2_db_t dbno; /* data block number */ 18890e822255SDave Chinner int error; /* error return value */ 18900e822255SDave Chinner int findex; /* freespace entry index */ 18910e822255SDave Chinner int length; /* length of the new entry */ 18920e822255SDave Chinner int logfree = 0; /* need to log free entry */ 18930e822255SDave Chinner int needlog = 0; /* need to log data header */ 18940e822255SDave Chinner int needscan = 0; /* need to rescan data frees */ 18950e822255SDave Chinner __be16 *tagp; /* data entry tag pointer */ 18960e822255SDave Chinner 18970e822255SDave Chinner length = dp->d_ops->data_entsize(args->namelen); 1898195b0a44SChristoph Hellwig error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr, 1899195b0a44SChristoph Hellwig &findex, length); 19000e822255SDave Chinner if (error) 19010e822255SDave Chinner return error; 19020e822255SDave Chinner 19030e822255SDave Chinner /* 19040e822255SDave Chinner * Now we know if we must allocate blocks, so if we are checking whether 19050e822255SDave Chinner * we can insert without allocation then we can return now. 19060e822255SDave Chinner */ 19070e822255SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) { 19080e822255SDave Chinner if (dbno == -1) 19090e822255SDave Chinner return -ENOSPC; 19100e822255SDave Chinner return 0; 19110e822255SDave Chinner } 1912a07258a6SDave Chinner 191330f712c9SDave Chinner /* 191430f712c9SDave Chinner * If we don't have a data block, we need to allocate one and make 191530f712c9SDave Chinner * the freespace entries refer to it. 191630f712c9SDave Chinner */ 1917a07258a6SDave Chinner if (dbno == -1) { 1918a07258a6SDave Chinner /* we're going to have to log the free block index later */ 191930f712c9SDave Chinner logfree = 1; 19200e822255SDave Chinner error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp, 1921195b0a44SChristoph Hellwig &freehdr, &findex); 1922a07258a6SDave Chinner } else { 1923a07258a6SDave Chinner /* Read the data block in. */ 192430f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp, 192530f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, dbno), 192630f712c9SDave Chinner -1, &dbp); 19270e822255SDave Chinner } 192830f712c9SDave Chinner if (error) 192930f712c9SDave Chinner return error; 1930a07258a6SDave Chinner 1931a07258a6SDave Chinner /* setup for data block up now */ 193230f712c9SDave Chinner hdr = dbp->b_addr; 193330f712c9SDave Chinner bf = dp->d_ops->data_bestfree_p(hdr); 193430f712c9SDave Chinner ASSERT(be16_to_cpu(bf[0].length) >= length); 1935a07258a6SDave Chinner 1936a07258a6SDave Chinner /* Point to the existing unused space. */ 193730f712c9SDave Chinner dup = (xfs_dir2_data_unused_t *) 193830f712c9SDave Chinner ((char *)hdr + be16_to_cpu(bf[0].offset)); 1939a07258a6SDave Chinner 1940a07258a6SDave Chinner /* Mark the first part of the unused space, inuse for us. */ 19416915ef35SDarrick J. Wong aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); 19426915ef35SDarrick J. Wong error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length, 194330f712c9SDave Chinner &needlog, &needscan); 19446915ef35SDarrick J. Wong if (error) { 19456915ef35SDarrick J. Wong xfs_trans_brelse(tp, dbp); 19466915ef35SDarrick J. Wong return error; 19476915ef35SDarrick J. Wong } 1948a07258a6SDave Chinner 1949a07258a6SDave Chinner /* Fill in the new entry and log it. */ 195030f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)dup; 195130f712c9SDave Chinner dep->inumber = cpu_to_be64(args->inumber); 195230f712c9SDave Chinner dep->namelen = args->namelen; 195330f712c9SDave Chinner memcpy(dep->name, args->name, dep->namelen); 195430f712c9SDave Chinner dp->d_ops->data_put_ftype(dep, args->filetype); 195530f712c9SDave Chinner tagp = dp->d_ops->data_entry_tag_p(dep); 195630f712c9SDave Chinner *tagp = cpu_to_be16((char *)dep - (char *)hdr); 195730f712c9SDave Chinner xfs_dir2_data_log_entry(args, dbp, dep); 1958a07258a6SDave Chinner 1959a07258a6SDave Chinner /* Rescan the freespace and log the data block if needed. */ 196030f712c9SDave Chinner if (needscan) 196130f712c9SDave Chinner xfs_dir2_data_freescan(dp, hdr, &needlog); 196230f712c9SDave Chinner if (needlog) 196330f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp); 1964a07258a6SDave Chinner 1965a07258a6SDave Chinner /* If the freespace block entry is now wrong, update it. */ 1966a84f3d5cSChristoph Hellwig if (freehdr.bests[findex] != bf[0].length) { 1967a84f3d5cSChristoph Hellwig freehdr.bests[findex] = bf[0].length; 196830f712c9SDave Chinner logfree = 1; 196930f712c9SDave Chinner } 1970a07258a6SDave Chinner 1971a07258a6SDave Chinner /* Log the freespace entry if needed. */ 197230f712c9SDave Chinner if (logfree) 1973a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); 1974a07258a6SDave Chinner 1975a07258a6SDave Chinner /* Return the data block and offset in args. */ 197630f712c9SDave Chinner args->blkno = (xfs_dablk_t)dbno; 197730f712c9SDave Chinner args->index = be16_to_cpu(*tagp); 197830f712c9SDave Chinner return 0; 197930f712c9SDave Chinner } 198030f712c9SDave Chinner 198130f712c9SDave Chinner /* 1982aee7754bSDave Chinner * Top-level node form directory addname routine. 1983aee7754bSDave Chinner */ 1984aee7754bSDave Chinner int /* error */ 1985aee7754bSDave Chinner xfs_dir2_node_addname( 1986aee7754bSDave Chinner xfs_da_args_t *args) /* operation arguments */ 1987aee7754bSDave Chinner { 1988aee7754bSDave Chinner xfs_da_state_blk_t *blk; /* leaf block for insert */ 1989aee7754bSDave Chinner int error; /* error return value */ 1990aee7754bSDave Chinner int rval; /* sub-return value */ 1991aee7754bSDave Chinner xfs_da_state_t *state; /* btree cursor */ 1992aee7754bSDave Chinner 1993aee7754bSDave Chinner trace_xfs_dir2_node_addname(args); 1994aee7754bSDave Chinner 1995aee7754bSDave Chinner /* 1996aee7754bSDave Chinner * Allocate and initialize the state (btree cursor). 1997aee7754bSDave Chinner */ 1998aee7754bSDave Chinner state = xfs_da_state_alloc(); 1999aee7754bSDave Chinner state->args = args; 2000aee7754bSDave Chinner state->mp = args->dp->i_mount; 2001aee7754bSDave Chinner /* 2002aee7754bSDave Chinner * Look up the name. We're not supposed to find it, but 2003aee7754bSDave Chinner * this gives us the insertion point. 2004aee7754bSDave Chinner */ 2005aee7754bSDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 2006aee7754bSDave Chinner if (error) 2007aee7754bSDave Chinner rval = error; 2008aee7754bSDave Chinner if (rval != -ENOENT) { 2009aee7754bSDave Chinner goto done; 2010aee7754bSDave Chinner } 2011aee7754bSDave Chinner /* 2012aee7754bSDave Chinner * Add the data entry to a data block. 2013aee7754bSDave Chinner * Extravalid is set to a freeblock found by lookup. 2014aee7754bSDave Chinner */ 2015aee7754bSDave Chinner rval = xfs_dir2_node_addname_int(args, 2016aee7754bSDave Chinner state->extravalid ? &state->extrablk : NULL); 2017aee7754bSDave Chinner if (rval) { 2018aee7754bSDave Chinner goto done; 2019aee7754bSDave Chinner } 2020aee7754bSDave Chinner blk = &state->path.blk[state->path.active - 1]; 2021aee7754bSDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 2022aee7754bSDave Chinner /* 2023aee7754bSDave Chinner * Add the new leaf entry. 2024aee7754bSDave Chinner */ 2025aee7754bSDave Chinner rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); 2026aee7754bSDave Chinner if (rval == 0) { 2027aee7754bSDave Chinner /* 2028aee7754bSDave Chinner * It worked, fix the hash values up the btree. 2029aee7754bSDave Chinner */ 2030aee7754bSDave Chinner if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) 2031aee7754bSDave Chinner xfs_da3_fixhashpath(state, &state->path); 2032aee7754bSDave Chinner } else { 2033aee7754bSDave Chinner /* 2034aee7754bSDave Chinner * It didn't work, we need to split the leaf block. 2035aee7754bSDave Chinner */ 2036aee7754bSDave Chinner if (args->total == 0) { 2037aee7754bSDave Chinner ASSERT(rval == -ENOSPC); 2038aee7754bSDave Chinner goto done; 2039aee7754bSDave Chinner } 2040aee7754bSDave Chinner /* 2041aee7754bSDave Chinner * Split the leaf block and insert the new entry. 2042aee7754bSDave Chinner */ 2043aee7754bSDave Chinner rval = xfs_da3_split(state); 2044aee7754bSDave Chinner } 2045aee7754bSDave Chinner done: 2046aee7754bSDave Chinner xfs_da_state_free(state); 2047aee7754bSDave Chinner return rval; 2048aee7754bSDave Chinner } 2049aee7754bSDave Chinner 2050aee7754bSDave Chinner /* 205130f712c9SDave Chinner * Lookup an entry in a node-format directory. 205230f712c9SDave Chinner * All the real work happens in xfs_da3_node_lookup_int. 205330f712c9SDave Chinner * The only real output is the inode number of the entry. 205430f712c9SDave Chinner */ 205530f712c9SDave Chinner int /* error */ 205630f712c9SDave Chinner xfs_dir2_node_lookup( 205730f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 205830f712c9SDave Chinner { 205930f712c9SDave Chinner int error; /* error return value */ 206030f712c9SDave Chinner int i; /* btree level */ 206130f712c9SDave Chinner int rval; /* operation return value */ 206230f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 206330f712c9SDave Chinner 206430f712c9SDave Chinner trace_xfs_dir2_node_lookup(args); 206530f712c9SDave Chinner 206630f712c9SDave Chinner /* 206730f712c9SDave Chinner * Allocate and initialize the btree cursor. 206830f712c9SDave Chinner */ 206930f712c9SDave Chinner state = xfs_da_state_alloc(); 207030f712c9SDave Chinner state->args = args; 207130f712c9SDave Chinner state->mp = args->dp->i_mount; 207230f712c9SDave Chinner /* 207330f712c9SDave Chinner * Fill in the path to the entry in the cursor. 207430f712c9SDave Chinner */ 207530f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 207630f712c9SDave Chinner if (error) 207730f712c9SDave Chinner rval = error; 20782451337dSDave Chinner else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) { 20792451337dSDave Chinner /* If a CI match, dup the actual name and return -EEXIST */ 208030f712c9SDave Chinner xfs_dir2_data_entry_t *dep; 208130f712c9SDave Chinner 208230f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *) 208330f712c9SDave Chinner ((char *)state->extrablk.bp->b_addr + 208430f712c9SDave Chinner state->extrablk.index); 208530f712c9SDave Chinner rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); 208630f712c9SDave Chinner } 208730f712c9SDave Chinner /* 208830f712c9SDave Chinner * Release the btree blocks and leaf block. 208930f712c9SDave Chinner */ 209030f712c9SDave Chinner for (i = 0; i < state->path.active; i++) { 209130f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp); 209230f712c9SDave Chinner state->path.blk[i].bp = NULL; 209330f712c9SDave Chinner } 209430f712c9SDave Chinner /* 209530f712c9SDave Chinner * Release the data block if we have it. 209630f712c9SDave Chinner */ 209730f712c9SDave Chinner if (state->extravalid && state->extrablk.bp) { 209830f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp); 209930f712c9SDave Chinner state->extrablk.bp = NULL; 210030f712c9SDave Chinner } 210130f712c9SDave Chinner xfs_da_state_free(state); 210230f712c9SDave Chinner return rval; 210330f712c9SDave Chinner } 210430f712c9SDave Chinner 210530f712c9SDave Chinner /* 210630f712c9SDave Chinner * Remove an entry from a node-format directory. 210730f712c9SDave Chinner */ 210830f712c9SDave Chinner int /* error */ 210930f712c9SDave Chinner xfs_dir2_node_removename( 211030f712c9SDave Chinner struct xfs_da_args *args) /* operation arguments */ 211130f712c9SDave Chinner { 211230f712c9SDave Chinner struct xfs_da_state_blk *blk; /* leaf block */ 211330f712c9SDave Chinner int error; /* error return value */ 211430f712c9SDave Chinner int rval; /* operation return value */ 211530f712c9SDave Chinner struct xfs_da_state *state; /* btree cursor */ 211630f712c9SDave Chinner 211730f712c9SDave Chinner trace_xfs_dir2_node_removename(args); 211830f712c9SDave Chinner 211930f712c9SDave Chinner /* 212030f712c9SDave Chinner * Allocate and initialize the btree cursor. 212130f712c9SDave Chinner */ 212230f712c9SDave Chinner state = xfs_da_state_alloc(); 212330f712c9SDave Chinner state->args = args; 212430f712c9SDave Chinner state->mp = args->dp->i_mount; 212530f712c9SDave Chinner 212630f712c9SDave Chinner /* Look up the entry we're deleting, set up the cursor. */ 212730f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 212830f712c9SDave Chinner if (error) 212930f712c9SDave Chinner goto out_free; 213030f712c9SDave Chinner 213130f712c9SDave Chinner /* Didn't find it, upper layer screwed up. */ 21322451337dSDave Chinner if (rval != -EEXIST) { 213330f712c9SDave Chinner error = rval; 213430f712c9SDave Chinner goto out_free; 213530f712c9SDave Chinner } 213630f712c9SDave Chinner 213730f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 213830f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 213930f712c9SDave Chinner ASSERT(state->extravalid); 214030f712c9SDave Chinner /* 214130f712c9SDave Chinner * Remove the leaf and data entries. 214230f712c9SDave Chinner * Extrablk refers to the data block. 214330f712c9SDave Chinner */ 214430f712c9SDave Chinner error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, 214530f712c9SDave Chinner &state->extrablk, &rval); 214630f712c9SDave Chinner if (error) 214730f712c9SDave Chinner goto out_free; 214830f712c9SDave Chinner /* 214930f712c9SDave Chinner * Fix the hash values up the btree. 215030f712c9SDave Chinner */ 215130f712c9SDave Chinner xfs_da3_fixhashpath(state, &state->path); 215230f712c9SDave Chinner /* 215330f712c9SDave Chinner * If we need to join leaf blocks, do it. 215430f712c9SDave Chinner */ 215530f712c9SDave Chinner if (rval && state->path.active > 1) 215630f712c9SDave Chinner error = xfs_da3_join(state); 215730f712c9SDave Chinner /* 215830f712c9SDave Chinner * If no errors so far, try conversion to leaf format. 215930f712c9SDave Chinner */ 216030f712c9SDave Chinner if (!error) 216130f712c9SDave Chinner error = xfs_dir2_node_to_leaf(state); 216230f712c9SDave Chinner out_free: 216330f712c9SDave Chinner xfs_da_state_free(state); 216430f712c9SDave Chinner return error; 216530f712c9SDave Chinner } 216630f712c9SDave Chinner 216730f712c9SDave Chinner /* 216830f712c9SDave Chinner * Replace an entry's inode number in a node-format directory. 216930f712c9SDave Chinner */ 217030f712c9SDave Chinner int /* error */ 217130f712c9SDave Chinner xfs_dir2_node_replace( 217230f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 217330f712c9SDave Chinner { 217430f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */ 217530f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 217630f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data entry changed */ 217730f712c9SDave Chinner int error; /* error return value */ 217830f712c9SDave Chinner int i; /* btree level */ 217930f712c9SDave Chinner xfs_ino_t inum; /* new inode number */ 218003754234SJan Kara int ftype; /* new file type */ 218130f712c9SDave Chinner int rval; /* internal return value */ 218230f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 218330f712c9SDave Chinner 218430f712c9SDave Chinner trace_xfs_dir2_node_replace(args); 218530f712c9SDave Chinner 218630f712c9SDave Chinner /* 218730f712c9SDave Chinner * Allocate and initialize the btree cursor. 218830f712c9SDave Chinner */ 218930f712c9SDave Chinner state = xfs_da_state_alloc(); 219030f712c9SDave Chinner state->args = args; 219130f712c9SDave Chinner state->mp = args->dp->i_mount; 219203754234SJan Kara 219303754234SJan Kara /* 219403754234SJan Kara * We have to save new inode number and ftype since 219503754234SJan Kara * xfs_da3_node_lookup_int() is going to overwrite them 219603754234SJan Kara */ 219730f712c9SDave Chinner inum = args->inumber; 219803754234SJan Kara ftype = args->filetype; 219903754234SJan Kara 220030f712c9SDave Chinner /* 220130f712c9SDave Chinner * Lookup the entry to change in the btree. 220230f712c9SDave Chinner */ 220330f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 220430f712c9SDave Chinner if (error) { 220530f712c9SDave Chinner rval = error; 220630f712c9SDave Chinner } 220730f712c9SDave Chinner /* 220830f712c9SDave Chinner * It should be found, since the vnodeops layer has looked it up 220930f712c9SDave Chinner * and locked it. But paranoia is good. 221030f712c9SDave Chinner */ 22112451337dSDave Chinner if (rval == -EEXIST) { 2212787b0893SChristoph Hellwig struct xfs_dir3_icleaf_hdr leafhdr; 2213787b0893SChristoph Hellwig 221430f712c9SDave Chinner /* 221530f712c9SDave Chinner * Find the leaf entry. 221630f712c9SDave Chinner */ 221730f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 221830f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 221930f712c9SDave Chinner ASSERT(state->extravalid); 2220787b0893SChristoph Hellwig 2221787b0893SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr, 2222787b0893SChristoph Hellwig blk->bp->b_addr); 222330f712c9SDave Chinner /* 222430f712c9SDave Chinner * Point to the data entry. 222530f712c9SDave Chinner */ 222630f712c9SDave Chinner hdr = state->extrablk.bp->b_addr; 222730f712c9SDave Chinner ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || 222830f712c9SDave Chinner hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); 222930f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *) 223030f712c9SDave Chinner ((char *)hdr + 223130f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo, 2232787b0893SChristoph Hellwig be32_to_cpu(leafhdr.ents[blk->index].address))); 223330f712c9SDave Chinner ASSERT(inum != be64_to_cpu(dep->inumber)); 223430f712c9SDave Chinner /* 223530f712c9SDave Chinner * Fill in the new inode number and log the entry. 223630f712c9SDave Chinner */ 223730f712c9SDave Chinner dep->inumber = cpu_to_be64(inum); 223803754234SJan Kara args->dp->d_ops->data_put_ftype(dep, ftype); 223930f712c9SDave Chinner xfs_dir2_data_log_entry(args, state->extrablk.bp, dep); 224030f712c9SDave Chinner rval = 0; 224130f712c9SDave Chinner } 224230f712c9SDave Chinner /* 224330f712c9SDave Chinner * Didn't find it, and we're holding a data block. Drop it. 224430f712c9SDave Chinner */ 224530f712c9SDave Chinner else if (state->extravalid) { 224630f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp); 224730f712c9SDave Chinner state->extrablk.bp = NULL; 224830f712c9SDave Chinner } 224930f712c9SDave Chinner /* 225030f712c9SDave Chinner * Release all the buffers in the cursor. 225130f712c9SDave Chinner */ 225230f712c9SDave Chinner for (i = 0; i < state->path.active; i++) { 225330f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp); 225430f712c9SDave Chinner state->path.blk[i].bp = NULL; 225530f712c9SDave Chinner } 225630f712c9SDave Chinner xfs_da_state_free(state); 225730f712c9SDave Chinner return rval; 225830f712c9SDave Chinner } 225930f712c9SDave Chinner 226030f712c9SDave Chinner /* 226130f712c9SDave Chinner * Trim off a trailing empty freespace block. 226230f712c9SDave Chinner * Return (in rvalp) 1 if we did it, 0 if not. 226330f712c9SDave Chinner */ 226430f712c9SDave Chinner int /* error */ 226530f712c9SDave Chinner xfs_dir2_node_trim_free( 226630f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 226730f712c9SDave Chinner xfs_fileoff_t fo, /* free block number */ 226830f712c9SDave Chinner int *rvalp) /* out: did something */ 226930f712c9SDave Chinner { 227030f712c9SDave Chinner struct xfs_buf *bp; /* freespace buffer */ 227130f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 227230f712c9SDave Chinner int error; /* error return code */ 227330f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 227430f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 227530f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 227630f712c9SDave Chinner 227730f712c9SDave Chinner dp = args->dp; 227830f712c9SDave Chinner tp = args->trans; 2279355cced4SChristoph Hellwig 2280355cced4SChristoph Hellwig *rvalp = 0; 2281355cced4SChristoph Hellwig 228230f712c9SDave Chinner /* 228330f712c9SDave Chinner * Read the freespace block. 228430f712c9SDave Chinner */ 228530f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, fo, &bp); 228630f712c9SDave Chinner if (error) 228730f712c9SDave Chinner return error; 228830f712c9SDave Chinner /* 228930f712c9SDave Chinner * There can be holes in freespace. If fo is a hole, there's 229030f712c9SDave Chinner * nothing to do. 229130f712c9SDave Chinner */ 229230f712c9SDave Chinner if (!bp) 229330f712c9SDave Chinner return 0; 229430f712c9SDave Chinner free = bp->b_addr; 22955ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); 229630f712c9SDave Chinner 229730f712c9SDave Chinner /* 229830f712c9SDave Chinner * If there are used entries, there's nothing to do. 229930f712c9SDave Chinner */ 230030f712c9SDave Chinner if (freehdr.nused > 0) { 230130f712c9SDave Chinner xfs_trans_brelse(tp, bp); 230230f712c9SDave Chinner return 0; 230330f712c9SDave Chinner } 230430f712c9SDave Chinner /* 230530f712c9SDave Chinner * Blow the block away. 230630f712c9SDave Chinner */ 230730f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, 230830f712c9SDave Chinner xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp); 230930f712c9SDave Chinner if (error) { 231030f712c9SDave Chinner /* 231130f712c9SDave Chinner * Can't fail with ENOSPC since that only happens with no 231230f712c9SDave Chinner * space reservation, when breaking up an extent into two 231330f712c9SDave Chinner * pieces. This is the last block of an extent. 231430f712c9SDave Chinner */ 23152451337dSDave Chinner ASSERT(error != -ENOSPC); 231630f712c9SDave Chinner xfs_trans_brelse(tp, bp); 231730f712c9SDave Chinner return error; 231830f712c9SDave Chinner } 231930f712c9SDave Chinner /* 232030f712c9SDave Chinner * Return that we succeeded. 232130f712c9SDave Chinner */ 232230f712c9SDave Chinner *rvalp = 1; 232330f712c9SDave Chinner return 0; 232430f712c9SDave Chinner } 2325