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 /* 373d92c93bSChristoph Hellwig * Convert data space db to the corresponding free db. 383d92c93bSChristoph Hellwig */ 393d92c93bSChristoph Hellwig static xfs_dir2_db_t 403d92c93bSChristoph Hellwig xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db) 413d92c93bSChristoph Hellwig { 423d92c93bSChristoph Hellwig return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) + 433d92c93bSChristoph Hellwig (db / geo->free_max_bests); 443d92c93bSChristoph Hellwig } 453d92c93bSChristoph Hellwig 463d92c93bSChristoph Hellwig /* 473d92c93bSChristoph Hellwig * Convert data space db to the corresponding index in a free db. 483d92c93bSChristoph Hellwig */ 493d92c93bSChristoph Hellwig static int 503d92c93bSChristoph Hellwig xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db) 513d92c93bSChristoph Hellwig { 523d92c93bSChristoph Hellwig return db % geo->free_max_bests; 533d92c93bSChristoph Hellwig } 543d92c93bSChristoph Hellwig 553d92c93bSChristoph Hellwig /* 5630f712c9SDave Chinner * Check internal consistency of a leafn block. 5730f712c9SDave Chinner */ 5830f712c9SDave Chinner #ifdef DEBUG 59a6a781a5SDarrick J. Wong static xfs_failaddr_t 6030f712c9SDave Chinner xfs_dir3_leafn_check( 6130f712c9SDave Chinner struct xfs_inode *dp, 6230f712c9SDave Chinner struct xfs_buf *bp) 6330f712c9SDave Chinner { 6430f712c9SDave Chinner struct xfs_dir2_leaf *leaf = bp->b_addr; 6530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 6630f712c9SDave Chinner 6751842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 6830f712c9SDave Chinner 6930f712c9SDave Chinner if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) { 7030f712c9SDave Chinner struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; 7130f712c9SDave Chinner if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) 72a6a781a5SDarrick J. Wong return __this_address; 7330f712c9SDave Chinner } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC) 74a6a781a5SDarrick J. Wong return __this_address; 7530f712c9SDave Chinner 765ba30919SChristoph Hellwig return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf); 7730f712c9SDave Chinner } 78a6a781a5SDarrick J. Wong 79a6a781a5SDarrick J. Wong static inline void 80a6a781a5SDarrick J. Wong xfs_dir3_leaf_check( 81a6a781a5SDarrick J. Wong struct xfs_inode *dp, 82a6a781a5SDarrick J. Wong struct xfs_buf *bp) 83a6a781a5SDarrick J. Wong { 84a6a781a5SDarrick J. Wong xfs_failaddr_t fa; 85a6a781a5SDarrick J. Wong 86a6a781a5SDarrick J. Wong fa = xfs_dir3_leafn_check(dp, bp); 87a6a781a5SDarrick J. Wong if (!fa) 88a6a781a5SDarrick J. Wong return; 89a6a781a5SDarrick J. Wong xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, 902551a530SDarrick J. Wong bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, 912551a530SDarrick J. Wong fa); 92a6a781a5SDarrick J. Wong ASSERT(0); 93a6a781a5SDarrick J. Wong } 9430f712c9SDave Chinner #else 9530f712c9SDave Chinner #define xfs_dir3_leaf_check(dp, bp) 9630f712c9SDave Chinner #endif 9730f712c9SDave Chinner 98a6a781a5SDarrick J. Wong static xfs_failaddr_t 9930f712c9SDave Chinner xfs_dir3_free_verify( 10030f712c9SDave Chinner struct xfs_buf *bp) 10130f712c9SDave Chinner { 102dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 10330f712c9SDave Chinner struct xfs_dir2_free_hdr *hdr = bp->b_addr; 10430f712c9SDave Chinner 10539708c20SBrian Foster if (!xfs_verify_magic(bp, hdr->magic)) 10639708c20SBrian Foster return __this_address; 10739708c20SBrian Foster 10830f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 10930f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; 11030f712c9SDave Chinner 111ce748eaaSEric Sandeen if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) 112a6a781a5SDarrick J. Wong return __this_address; 11330f712c9SDave Chinner if (be64_to_cpu(hdr3->blkno) != bp->b_bn) 114a6a781a5SDarrick J. Wong return __this_address; 115a45086e2SBrian Foster if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) 116a6a781a5SDarrick J. Wong return __this_address; 11730f712c9SDave Chinner } 11830f712c9SDave Chinner 11930f712c9SDave Chinner /* XXX: should bounds check the xfs_dir3_icfree_hdr here */ 12030f712c9SDave Chinner 121a6a781a5SDarrick J. Wong return NULL; 12230f712c9SDave Chinner } 12330f712c9SDave Chinner 12430f712c9SDave Chinner static void 12530f712c9SDave Chinner xfs_dir3_free_read_verify( 12630f712c9SDave Chinner struct xfs_buf *bp) 12730f712c9SDave Chinner { 128dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 129bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 13030f712c9SDave Chinner 13130f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb) && 13230f712c9SDave Chinner !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF)) 133bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSBADCRC, __this_address); 134bc1a09b8SDarrick J. Wong else { 135bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_verify(bp); 136bc1a09b8SDarrick J. Wong if (fa) 137bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 138bc1a09b8SDarrick J. Wong } 13930f712c9SDave Chinner } 14030f712c9SDave Chinner 14130f712c9SDave Chinner static void 14230f712c9SDave Chinner xfs_dir3_free_write_verify( 14330f712c9SDave Chinner struct xfs_buf *bp) 14430f712c9SDave Chinner { 145dbd329f1SChristoph Hellwig struct xfs_mount *mp = bp->b_mount; 146fb1755a6SCarlos Maiolino struct xfs_buf_log_item *bip = bp->b_log_item; 14730f712c9SDave Chinner struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; 148bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 14930f712c9SDave Chinner 150bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_verify(bp); 151bc1a09b8SDarrick J. Wong if (fa) { 152bc1a09b8SDarrick J. Wong xfs_verifier_error(bp, -EFSCORRUPTED, fa); 15330f712c9SDave Chinner return; 15430f712c9SDave Chinner } 15530f712c9SDave Chinner 15630f712c9SDave Chinner if (!xfs_sb_version_hascrc(&mp->m_sb)) 15730f712c9SDave Chinner return; 15830f712c9SDave Chinner 15930f712c9SDave Chinner if (bip) 16030f712c9SDave Chinner hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); 16130f712c9SDave Chinner 16230f712c9SDave Chinner xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF); 16330f712c9SDave Chinner } 16430f712c9SDave Chinner 16530f712c9SDave Chinner const struct xfs_buf_ops xfs_dir3_free_buf_ops = { 166233135b7SEric Sandeen .name = "xfs_dir3_free", 16739708c20SBrian Foster .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC), 16839708c20SBrian Foster cpu_to_be32(XFS_DIR3_FREE_MAGIC) }, 16930f712c9SDave Chinner .verify_read = xfs_dir3_free_read_verify, 17030f712c9SDave Chinner .verify_write = xfs_dir3_free_write_verify, 171b5572597SDarrick J. Wong .verify_struct = xfs_dir3_free_verify, 17230f712c9SDave Chinner }; 17330f712c9SDave Chinner 174de14c5f5SDarrick J. Wong /* Everything ok in the free block header? */ 175bc1a09b8SDarrick J. Wong static xfs_failaddr_t 176de14c5f5SDarrick J. Wong xfs_dir3_free_header_check( 177de14c5f5SDarrick J. Wong struct xfs_inode *dp, 178de14c5f5SDarrick J. Wong xfs_dablk_t fbno, 179de14c5f5SDarrick J. Wong struct xfs_buf *bp) 180de14c5f5SDarrick J. Wong { 181de14c5f5SDarrick J. Wong struct xfs_mount *mp = dp->i_mount; 1825893e4feSChristoph Hellwig int maxbests = mp->m_dir_geo->free_max_bests; 183de14c5f5SDarrick J. Wong unsigned int firstdb; 184de14c5f5SDarrick J. Wong 185de14c5f5SDarrick J. Wong firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) - 186de14c5f5SDarrick J. Wong xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) * 187de14c5f5SDarrick J. Wong maxbests; 188de14c5f5SDarrick J. Wong if (xfs_sb_version_hascrc(&mp->m_sb)) { 189de14c5f5SDarrick J. Wong struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; 190de14c5f5SDarrick J. Wong 191de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->firstdb) != firstdb) 192a6a781a5SDarrick J. Wong return __this_address; 193de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->nvalid) > maxbests) 194a6a781a5SDarrick J. Wong return __this_address; 195de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused)) 196a6a781a5SDarrick J. Wong return __this_address; 197de14c5f5SDarrick J. Wong } else { 198de14c5f5SDarrick J. Wong struct xfs_dir2_free_hdr *hdr = bp->b_addr; 199de14c5f5SDarrick J. Wong 200de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->firstdb) != firstdb) 201a6a781a5SDarrick J. Wong return __this_address; 202de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->nvalid) > maxbests) 203a6a781a5SDarrick J. Wong return __this_address; 204de14c5f5SDarrick J. Wong if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused)) 205a6a781a5SDarrick J. Wong return __this_address; 206de14c5f5SDarrick J. Wong } 207a6a781a5SDarrick J. Wong return NULL; 208de14c5f5SDarrick J. Wong } 20930f712c9SDave Chinner 21030f712c9SDave Chinner static int 21130f712c9SDave Chinner __xfs_dir3_free_read( 21230f712c9SDave Chinner struct xfs_trans *tp, 21330f712c9SDave Chinner struct xfs_inode *dp, 21430f712c9SDave Chinner xfs_dablk_t fbno, 21530f712c9SDave Chinner xfs_daddr_t mappedbno, 21630f712c9SDave Chinner struct xfs_buf **bpp) 21730f712c9SDave Chinner { 218bc1a09b8SDarrick J. Wong xfs_failaddr_t fa; 21930f712c9SDave Chinner int err; 22030f712c9SDave Chinner 22130f712c9SDave Chinner err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, 22230f712c9SDave Chinner XFS_DATA_FORK, &xfs_dir3_free_buf_ops); 223de14c5f5SDarrick J. Wong if (err || !*bpp) 224de14c5f5SDarrick J. Wong return err; 225de14c5f5SDarrick J. Wong 226de14c5f5SDarrick J. Wong /* Check things that we can't do in the verifier. */ 227bc1a09b8SDarrick J. Wong fa = xfs_dir3_free_header_check(dp, fbno, *bpp); 228bc1a09b8SDarrick J. Wong if (fa) { 229bc1a09b8SDarrick J. Wong xfs_verifier_error(*bpp, -EFSCORRUPTED, fa); 230de14c5f5SDarrick J. Wong xfs_trans_brelse(tp, *bpp); 231de14c5f5SDarrick J. Wong return -EFSCORRUPTED; 232de14c5f5SDarrick J. Wong } 23330f712c9SDave Chinner 23430f712c9SDave Chinner /* try read returns without an error or *bpp if it lands in a hole */ 235de14c5f5SDarrick J. Wong if (tp) 23630f712c9SDave Chinner xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF); 237de14c5f5SDarrick J. Wong 238de14c5f5SDarrick J. Wong return 0; 23930f712c9SDave Chinner } 24030f712c9SDave Chinner 2415ba30919SChristoph Hellwig void 2425ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk( 2435ba30919SChristoph Hellwig struct xfs_mount *mp, 2445ba30919SChristoph Hellwig struct xfs_dir3_icfree_hdr *to, 2455ba30919SChristoph Hellwig struct xfs_dir2_free *from) 2465ba30919SChristoph Hellwig { 2475ba30919SChristoph Hellwig if (xfs_sb_version_hascrc(&mp->m_sb)) { 2485ba30919SChristoph Hellwig struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from; 2495ba30919SChristoph Hellwig 2505ba30919SChristoph Hellwig to->magic = be32_to_cpu(from3->hdr.hdr.magic); 2515ba30919SChristoph Hellwig to->firstdb = be32_to_cpu(from3->hdr.firstdb); 2525ba30919SChristoph Hellwig to->nvalid = be32_to_cpu(from3->hdr.nvalid); 2535ba30919SChristoph Hellwig to->nused = be32_to_cpu(from3->hdr.nused); 254a84f3d5cSChristoph Hellwig to->bests = from3->bests; 2555ba30919SChristoph Hellwig 2565ba30919SChristoph Hellwig ASSERT(to->magic == XFS_DIR3_FREE_MAGIC); 2575ba30919SChristoph Hellwig } else { 2585ba30919SChristoph Hellwig to->magic = be32_to_cpu(from->hdr.magic); 2595ba30919SChristoph Hellwig to->firstdb = be32_to_cpu(from->hdr.firstdb); 2605ba30919SChristoph Hellwig to->nvalid = be32_to_cpu(from->hdr.nvalid); 2615ba30919SChristoph Hellwig to->nused = be32_to_cpu(from->hdr.nused); 262a84f3d5cSChristoph Hellwig to->bests = from->bests; 263a84f3d5cSChristoph Hellwig 2645ba30919SChristoph Hellwig ASSERT(to->magic == XFS_DIR2_FREE_MAGIC); 2655ba30919SChristoph Hellwig } 2665ba30919SChristoph Hellwig } 2675ba30919SChristoph Hellwig 268200dada7SChristoph Hellwig static void 269200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk( 270200dada7SChristoph Hellwig struct xfs_mount *mp, 271200dada7SChristoph Hellwig struct xfs_dir2_free *to, 272200dada7SChristoph Hellwig struct xfs_dir3_icfree_hdr *from) 273200dada7SChristoph Hellwig { 274200dada7SChristoph Hellwig if (xfs_sb_version_hascrc(&mp->m_sb)) { 275200dada7SChristoph Hellwig struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to; 276200dada7SChristoph Hellwig 277200dada7SChristoph Hellwig ASSERT(from->magic == XFS_DIR3_FREE_MAGIC); 278200dada7SChristoph Hellwig 279200dada7SChristoph Hellwig to3->hdr.hdr.magic = cpu_to_be32(from->magic); 280200dada7SChristoph Hellwig to3->hdr.firstdb = cpu_to_be32(from->firstdb); 281200dada7SChristoph Hellwig to3->hdr.nvalid = cpu_to_be32(from->nvalid); 282200dada7SChristoph Hellwig to3->hdr.nused = cpu_to_be32(from->nused); 283200dada7SChristoph Hellwig } else { 284200dada7SChristoph Hellwig ASSERT(from->magic == XFS_DIR2_FREE_MAGIC); 285200dada7SChristoph Hellwig 286200dada7SChristoph Hellwig to->hdr.magic = cpu_to_be32(from->magic); 287200dada7SChristoph Hellwig to->hdr.firstdb = cpu_to_be32(from->firstdb); 288200dada7SChristoph Hellwig to->hdr.nvalid = cpu_to_be32(from->nvalid); 289200dada7SChristoph Hellwig to->hdr.nused = cpu_to_be32(from->nused); 290200dada7SChristoph Hellwig } 291200dada7SChristoph Hellwig } 292200dada7SChristoph Hellwig 29330f712c9SDave Chinner int 29430f712c9SDave Chinner xfs_dir2_free_read( 29530f712c9SDave Chinner struct xfs_trans *tp, 29630f712c9SDave Chinner struct xfs_inode *dp, 29730f712c9SDave Chinner xfs_dablk_t fbno, 29830f712c9SDave Chinner struct xfs_buf **bpp) 29930f712c9SDave Chinner { 30030f712c9SDave Chinner return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp); 30130f712c9SDave Chinner } 30230f712c9SDave Chinner 30330f712c9SDave Chinner static int 30430f712c9SDave Chinner xfs_dir2_free_try_read( 30530f712c9SDave Chinner struct xfs_trans *tp, 30630f712c9SDave Chinner struct xfs_inode *dp, 30730f712c9SDave Chinner xfs_dablk_t fbno, 30830f712c9SDave Chinner struct xfs_buf **bpp) 30930f712c9SDave Chinner { 31030f712c9SDave Chinner return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp); 31130f712c9SDave Chinner } 31230f712c9SDave Chinner 31330f712c9SDave Chinner static int 31430f712c9SDave Chinner xfs_dir3_free_get_buf( 31530f712c9SDave Chinner xfs_da_args_t *args, 31630f712c9SDave Chinner xfs_dir2_db_t fbno, 31730f712c9SDave Chinner struct xfs_buf **bpp) 31830f712c9SDave Chinner { 31930f712c9SDave Chinner struct xfs_trans *tp = args->trans; 32030f712c9SDave Chinner struct xfs_inode *dp = args->dp; 32130f712c9SDave Chinner struct xfs_mount *mp = dp->i_mount; 32230f712c9SDave Chinner struct xfs_buf *bp; 32330f712c9SDave Chinner int error; 32430f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr; 32530f712c9SDave Chinner 32630f712c9SDave Chinner error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno), 32730f712c9SDave Chinner -1, &bp, XFS_DATA_FORK); 32830f712c9SDave Chinner if (error) 32930f712c9SDave Chinner return error; 33030f712c9SDave Chinner 33130f712c9SDave Chinner xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF); 33230f712c9SDave Chinner bp->b_ops = &xfs_dir3_free_buf_ops; 33330f712c9SDave Chinner 33430f712c9SDave Chinner /* 33530f712c9SDave Chinner * Initialize the new block to be empty, and remember 33630f712c9SDave Chinner * its first slot as our empty slot. 33730f712c9SDave Chinner */ 33830f712c9SDave Chinner memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr)); 33930f712c9SDave Chinner memset(&hdr, 0, sizeof(hdr)); 34030f712c9SDave Chinner 34130f712c9SDave Chinner if (xfs_sb_version_hascrc(&mp->m_sb)) { 34230f712c9SDave Chinner struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; 34330f712c9SDave Chinner 34430f712c9SDave Chinner hdr.magic = XFS_DIR3_FREE_MAGIC; 34530f712c9SDave Chinner 34630f712c9SDave Chinner hdr3->hdr.blkno = cpu_to_be64(bp->b_bn); 34730f712c9SDave Chinner hdr3->hdr.owner = cpu_to_be64(dp->i_ino); 348ce748eaaSEric Sandeen uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid); 34930f712c9SDave Chinner } else 35030f712c9SDave Chinner hdr.magic = XFS_DIR2_FREE_MAGIC; 351200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr); 35230f712c9SDave Chinner *bpp = bp; 35330f712c9SDave Chinner return 0; 35430f712c9SDave Chinner } 35530f712c9SDave Chinner 35630f712c9SDave Chinner /* 35730f712c9SDave Chinner * Log entries from a freespace block. 35830f712c9SDave Chinner */ 35930f712c9SDave Chinner STATIC void 36030f712c9SDave Chinner xfs_dir2_free_log_bests( 36130f712c9SDave Chinner struct xfs_da_args *args, 362a84f3d5cSChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr, 36330f712c9SDave Chinner struct xfs_buf *bp, 36430f712c9SDave Chinner int first, /* first entry to log */ 36530f712c9SDave Chinner int last) /* last entry to log */ 36630f712c9SDave Chinner { 367a84f3d5cSChristoph Hellwig struct xfs_dir2_free *free = bp->b_addr; 36830f712c9SDave Chinner 36930f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 37030f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 37130f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 372a84f3d5cSChristoph Hellwig (char *)&hdr->bests[first] - (char *)free, 373a84f3d5cSChristoph Hellwig (char *)&hdr->bests[last] - (char *)free + 374a84f3d5cSChristoph Hellwig sizeof(hdr->bests[0]) - 1); 37530f712c9SDave Chinner } 37630f712c9SDave Chinner 37730f712c9SDave Chinner /* 37830f712c9SDave Chinner * Log header from a freespace block. 37930f712c9SDave Chinner */ 38030f712c9SDave Chinner static void 38130f712c9SDave Chinner xfs_dir2_free_log_header( 38230f712c9SDave Chinner struct xfs_da_args *args, 38330f712c9SDave Chinner struct xfs_buf *bp) 38430f712c9SDave Chinner { 38530f712c9SDave Chinner #ifdef DEBUG 38630f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 38730f712c9SDave Chinner 38830f712c9SDave Chinner free = bp->b_addr; 38930f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 39030f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 39130f712c9SDave Chinner #endif 39230f712c9SDave Chinner xfs_trans_log_buf(args->trans, bp, 0, 393ed1d612fSChristoph Hellwig args->geo->free_hdr_size - 1); 39430f712c9SDave Chinner } 39530f712c9SDave Chinner 39630f712c9SDave Chinner /* 39730f712c9SDave Chinner * Convert a leaf-format directory to a node-format directory. 39830f712c9SDave Chinner * We need to change the magic number of the leaf block, and copy 39930f712c9SDave Chinner * the freespace table out of the leaf block into its own block. 40030f712c9SDave Chinner */ 40130f712c9SDave Chinner int /* error */ 40230f712c9SDave Chinner xfs_dir2_leaf_to_node( 40330f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 40430f712c9SDave Chinner struct xfs_buf *lbp) /* leaf buffer */ 40530f712c9SDave Chinner { 40630f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 40730f712c9SDave Chinner int error; /* error return value */ 40830f712c9SDave Chinner struct xfs_buf *fbp; /* freespace buffer */ 40930f712c9SDave Chinner xfs_dir2_db_t fdb; /* freespace block number */ 41030f712c9SDave Chinner __be16 *from; /* pointer to freespace entry */ 41130f712c9SDave Chinner int i; /* leaf freespace index */ 41230f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 41330f712c9SDave Chinner xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ 41430f712c9SDave Chinner int n; /* count of live freespc ents */ 41530f712c9SDave Chinner xfs_dir2_data_off_t off; /* freespace entry value */ 41630f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 41730f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 41830f712c9SDave Chinner 41930f712c9SDave Chinner trace_xfs_dir2_leaf_to_node(args); 42030f712c9SDave Chinner 42130f712c9SDave Chinner dp = args->dp; 42230f712c9SDave Chinner tp = args->trans; 42330f712c9SDave Chinner /* 42430f712c9SDave Chinner * Add a freespace block to the directory. 42530f712c9SDave Chinner */ 42630f712c9SDave Chinner if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { 42730f712c9SDave Chinner return error; 42830f712c9SDave Chinner } 42930f712c9SDave Chinner ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); 43030f712c9SDave Chinner /* 43130f712c9SDave Chinner * Get the buffer for the new freespace block. 43230f712c9SDave Chinner */ 43330f712c9SDave Chinner error = xfs_dir3_free_get_buf(args, fdb, &fbp); 43430f712c9SDave Chinner if (error) 43530f712c9SDave Chinner return error; 43630f712c9SDave Chinner 437a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr); 43830f712c9SDave Chinner leaf = lbp->b_addr; 43930f712c9SDave Chinner ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); 4403f883f5bSDarrick J. Wong if (be32_to_cpu(ltp->bestcount) > 441a5155b87SDarrick J. Wong (uint)dp->i_d.di_size / args->geo->blksize) { 442a5155b87SDarrick J. Wong xfs_buf_corruption_error(lbp); 4433f883f5bSDarrick J. Wong return -EFSCORRUPTED; 444a5155b87SDarrick J. Wong } 44530f712c9SDave Chinner 44630f712c9SDave Chinner /* 44730f712c9SDave Chinner * Copy freespace entries from the leaf block to the new block. 44830f712c9SDave Chinner * Count active entries. 44930f712c9SDave Chinner */ 45030f712c9SDave Chinner from = xfs_dir2_leaf_bests_p(ltp); 451a84f3d5cSChristoph Hellwig for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) { 452a84f3d5cSChristoph Hellwig off = be16_to_cpu(*from); 453a84f3d5cSChristoph Hellwig if (off != NULLDATAOFF) 45430f712c9SDave Chinner n++; 455a84f3d5cSChristoph Hellwig freehdr.bests[i] = cpu_to_be16(off); 45630f712c9SDave Chinner } 45730f712c9SDave Chinner 45830f712c9SDave Chinner /* 45930f712c9SDave Chinner * Now initialize the freespace block header. 46030f712c9SDave Chinner */ 46130f712c9SDave Chinner freehdr.nused = n; 46230f712c9SDave Chinner freehdr.nvalid = be32_to_cpu(ltp->bestcount); 46330f712c9SDave Chinner 464200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr); 465a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1); 46630f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 46730f712c9SDave Chinner 46830f712c9SDave Chinner /* 46930f712c9SDave Chinner * Converting the leaf to a leafnode is just a matter of changing the 47030f712c9SDave Chinner * magic number and the ops. Do the change directly to the buffer as 47130f712c9SDave Chinner * it's less work (and less code) than decoding the header to host 47230f712c9SDave Chinner * format and back again. 47330f712c9SDave Chinner */ 47430f712c9SDave Chinner if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)) 47530f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); 47630f712c9SDave Chinner else 47730f712c9SDave Chinner leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); 47830f712c9SDave Chinner lbp->b_ops = &xfs_dir3_leafn_buf_ops; 47930f712c9SDave Chinner xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF); 48030f712c9SDave Chinner xfs_dir3_leaf_log_header(args, lbp); 48130f712c9SDave Chinner xfs_dir3_leaf_check(dp, lbp); 48230f712c9SDave Chinner return 0; 48330f712c9SDave Chinner } 48430f712c9SDave Chinner 48530f712c9SDave Chinner /* 48630f712c9SDave Chinner * Add a leaf entry to a leaf block in a node-form directory. 48730f712c9SDave Chinner * The other work necessary is done from the caller. 48830f712c9SDave Chinner */ 48930f712c9SDave Chinner static int /* error */ 49030f712c9SDave Chinner xfs_dir2_leafn_add( 49130f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 49279622c7cSDarrick J. Wong struct xfs_da_args *args, /* operation arguments */ 49330f712c9SDave Chinner int index) /* insertion pt for new entry */ 49430f712c9SDave Chinner { 49579622c7cSDarrick J. Wong struct xfs_dir3_icleaf_hdr leafhdr; 49679622c7cSDarrick J. Wong struct xfs_inode *dp = args->dp; 49779622c7cSDarrick J. Wong struct xfs_dir2_leaf *leaf = bp->b_addr; 49879622c7cSDarrick J. Wong struct xfs_dir2_leaf_entry *lep; 49979622c7cSDarrick J. Wong struct xfs_dir2_leaf_entry *ents; 50030f712c9SDave Chinner int compact; /* compacting stale leaves */ 50179622c7cSDarrick J. Wong int highstale = 0; /* next stale entry */ 50230f712c9SDave Chinner int lfloghigh; /* high leaf entry logging */ 50330f712c9SDave Chinner int lfloglow; /* low leaf entry logging */ 50479622c7cSDarrick J. Wong int lowstale = 0; /* previous stale entry */ 50530f712c9SDave Chinner 50630f712c9SDave Chinner trace_xfs_dir2_leafn_add(args, index); 50730f712c9SDave Chinner 50851842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 509787b0893SChristoph Hellwig ents = leafhdr.ents; 51030f712c9SDave Chinner 51130f712c9SDave Chinner /* 51230f712c9SDave Chinner * Quick check just to make sure we are not going to index 51330f712c9SDave Chinner * into other peoples memory 51430f712c9SDave Chinner */ 515a5155b87SDarrick J. Wong if (index < 0) { 516a5155b87SDarrick J. Wong xfs_buf_corruption_error(bp); 5172451337dSDave Chinner return -EFSCORRUPTED; 518a5155b87SDarrick J. Wong } 51930f712c9SDave Chinner 52030f712c9SDave Chinner /* 52130f712c9SDave Chinner * If there are already the maximum number of leaf entries in 52230f712c9SDave Chinner * the block, if there are no stale entries it won't fit. 52330f712c9SDave Chinner * Caller will do a split. If there are stale entries we'll do 52430f712c9SDave Chinner * a compact. 52530f712c9SDave Chinner */ 52630f712c9SDave Chinner 527478c7835SChristoph Hellwig if (leafhdr.count == args->geo->leaf_max_ents) { 52830f712c9SDave Chinner if (!leafhdr.stale) 5292451337dSDave Chinner return -ENOSPC; 53030f712c9SDave Chinner compact = leafhdr.stale > 1; 53130f712c9SDave Chinner } else 53230f712c9SDave Chinner compact = 0; 53330f712c9SDave Chinner ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval); 53430f712c9SDave Chinner ASSERT(index == leafhdr.count || 53530f712c9SDave Chinner be32_to_cpu(ents[index].hashval) >= args->hashval); 53630f712c9SDave Chinner 53730f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) 53830f712c9SDave Chinner return 0; 53930f712c9SDave Chinner 54030f712c9SDave Chinner /* 54130f712c9SDave Chinner * Compact out all but one stale leaf entry. Leaves behind 54230f712c9SDave Chinner * the entry closest to index. 54330f712c9SDave Chinner */ 54430f712c9SDave Chinner if (compact) 54530f712c9SDave Chinner xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, 54630f712c9SDave Chinner &highstale, &lfloglow, &lfloghigh); 54730f712c9SDave Chinner else if (leafhdr.stale) { 54830f712c9SDave Chinner /* 54930f712c9SDave Chinner * Set impossible logging indices for this case. 55030f712c9SDave Chinner */ 55130f712c9SDave Chinner lfloglow = leafhdr.count; 55230f712c9SDave Chinner lfloghigh = -1; 55330f712c9SDave Chinner } 55430f712c9SDave Chinner 55530f712c9SDave Chinner /* 55630f712c9SDave Chinner * Insert the new entry, log everything. 55730f712c9SDave Chinner */ 55830f712c9SDave Chinner lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, 55930f712c9SDave Chinner highstale, &lfloglow, &lfloghigh); 56030f712c9SDave Chinner 56130f712c9SDave Chinner lep->hashval = cpu_to_be32(args->hashval); 56230f712c9SDave Chinner lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo, 56330f712c9SDave Chinner args->blkno, args->index)); 56430f712c9SDave Chinner 565163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 56630f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp); 567787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh); 56830f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 56930f712c9SDave Chinner return 0; 57030f712c9SDave Chinner } 57130f712c9SDave Chinner 57230f712c9SDave Chinner #ifdef DEBUG 57330f712c9SDave Chinner static void 57430f712c9SDave Chinner xfs_dir2_free_hdr_check( 57530f712c9SDave Chinner struct xfs_inode *dp, 57630f712c9SDave Chinner struct xfs_buf *bp, 57730f712c9SDave Chinner xfs_dir2_db_t db) 57830f712c9SDave Chinner { 57930f712c9SDave Chinner struct xfs_dir3_icfree_hdr hdr; 58030f712c9SDave Chinner 5815ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr); 58230f712c9SDave Chinner 5835893e4feSChristoph Hellwig ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0); 58430f712c9SDave Chinner ASSERT(hdr.firstdb <= db); 58530f712c9SDave Chinner ASSERT(db < hdr.firstdb + hdr.nvalid); 58630f712c9SDave Chinner } 58730f712c9SDave Chinner #else 58830f712c9SDave Chinner #define xfs_dir2_free_hdr_check(dp, bp, db) 58930f712c9SDave Chinner #endif /* DEBUG */ 59030f712c9SDave Chinner 59130f712c9SDave Chinner /* 59230f712c9SDave Chinner * Return the last hash value in the leaf. 59330f712c9SDave Chinner * Stale entries are ok. 59430f712c9SDave Chinner */ 59530f712c9SDave Chinner xfs_dahash_t /* hash value */ 5968e8877e6SDarrick J. Wong xfs_dir2_leaf_lasthash( 59730f712c9SDave Chinner struct xfs_inode *dp, 59830f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 59930f712c9SDave Chinner int *count) /* count of entries in leaf */ 60030f712c9SDave Chinner { 60130f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 60230f712c9SDave Chinner 603787b0893SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr); 60430f712c9SDave Chinner 60530f712c9SDave Chinner ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || 6068e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR3_LEAFN_MAGIC || 6078e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || 6088e8877e6SDarrick J. Wong leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); 60930f712c9SDave Chinner 61030f712c9SDave Chinner if (count) 61130f712c9SDave Chinner *count = leafhdr.count; 61230f712c9SDave Chinner if (!leafhdr.count) 61330f712c9SDave Chinner return 0; 614787b0893SChristoph Hellwig return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval); 61530f712c9SDave Chinner } 61630f712c9SDave Chinner 61730f712c9SDave Chinner /* 61830f712c9SDave Chinner * Look up a leaf entry for space to add a name in a node-format leaf block. 61930f712c9SDave Chinner * The extrablk in state is a freespace block. 62030f712c9SDave Chinner */ 62130f712c9SDave Chinner STATIC int 62230f712c9SDave Chinner xfs_dir2_leafn_lookup_for_addname( 62330f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 62430f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 62530f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 62630f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 62730f712c9SDave Chinner { 62830f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */ 62930f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */ 63030f712c9SDave Chinner xfs_dir2_db_t curfdb = -1; /* current free block number */ 63130f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 63230f712c9SDave Chinner int error; /* error return value */ 63330f712c9SDave Chinner int fi; /* free entry index */ 63430f712c9SDave Chinner xfs_dir2_free_t *free = NULL; /* free block structure */ 63530f712c9SDave Chinner int index; /* leaf entry index */ 63630f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 63730f712c9SDave Chinner int length; /* length of new data entry */ 63830f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 63930f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 64030f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */ 64130f712c9SDave Chinner xfs_dir2_db_t newfdb; /* new free block number */ 64230f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 64330f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 64430f712c9SDave Chinner 64530f712c9SDave Chinner dp = args->dp; 64630f712c9SDave Chinner tp = args->trans; 64730f712c9SDave Chinner mp = dp->i_mount; 64830f712c9SDave Chinner leaf = bp->b_addr; 64951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); 65030f712c9SDave Chinner 65130f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 65230f712c9SDave Chinner ASSERT(leafhdr.count > 0); 65330f712c9SDave Chinner 65430f712c9SDave Chinner /* 65530f712c9SDave Chinner * Look up the hash value in the leaf entries. 65630f712c9SDave Chinner */ 65730f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp); 65830f712c9SDave Chinner /* 65930f712c9SDave Chinner * Do we have a buffer coming in? 66030f712c9SDave Chinner */ 66130f712c9SDave Chinner if (state->extravalid) { 66230f712c9SDave Chinner /* If so, it's a free block buffer, get the block number. */ 66330f712c9SDave Chinner curbp = state->extrablk.bp; 66430f712c9SDave Chinner curfdb = state->extrablk.blkno; 66530f712c9SDave Chinner free = curbp->b_addr; 66630f712c9SDave Chinner ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || 66730f712c9SDave Chinner free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); 66830f712c9SDave Chinner } 669fdbb8c5bSChristoph Hellwig length = xfs_dir2_data_entsize(mp, args->namelen); 67030f712c9SDave Chinner /* 67130f712c9SDave Chinner * Loop over leaf entries with the right hash value. 67230f712c9SDave Chinner */ 673787b0893SChristoph Hellwig for (lep = &leafhdr.ents[index]; 67430f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 67530f712c9SDave Chinner lep++, index++) { 67630f712c9SDave Chinner /* 67730f712c9SDave Chinner * Skip stale leaf entries. 67830f712c9SDave Chinner */ 67930f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 68030f712c9SDave Chinner continue; 68130f712c9SDave Chinner /* 68230f712c9SDave Chinner * Pull the data block number from the entry. 68330f712c9SDave Chinner */ 68430f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo, 68530f712c9SDave Chinner be32_to_cpu(lep->address)); 68630f712c9SDave Chinner /* 68730f712c9SDave Chinner * For addname, we're looking for a place to put the new entry. 68830f712c9SDave Chinner * We want to use a data block with an entry of equal 68930f712c9SDave Chinner * hash value to ours if there is one with room. 69030f712c9SDave Chinner * 69130f712c9SDave Chinner * If this block isn't the data block we already have 69230f712c9SDave Chinner * in hand, take a look at it. 69330f712c9SDave Chinner */ 69430f712c9SDave Chinner if (newdb != curdb) { 695a84f3d5cSChristoph Hellwig struct xfs_dir3_icfree_hdr freehdr; 69630f712c9SDave Chinner 69730f712c9SDave Chinner curdb = newdb; 69830f712c9SDave Chinner /* 69930f712c9SDave Chinner * Convert the data block to the free block 70030f712c9SDave Chinner * holding its freespace information. 70130f712c9SDave Chinner */ 7023d92c93bSChristoph Hellwig newfdb = xfs_dir2_db_to_fdb(args->geo, newdb); 70330f712c9SDave Chinner /* 70430f712c9SDave Chinner * If it's not the one we have in hand, read it in. 70530f712c9SDave Chinner */ 70630f712c9SDave Chinner if (newfdb != curfdb) { 70730f712c9SDave Chinner /* 70830f712c9SDave Chinner * If we had one before, drop it. 70930f712c9SDave Chinner */ 71030f712c9SDave Chinner if (curbp) 71130f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 71230f712c9SDave Chinner 71330f712c9SDave Chinner error = xfs_dir2_free_read(tp, dp, 71430f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, 71530f712c9SDave Chinner newfdb), 71630f712c9SDave Chinner &curbp); 71730f712c9SDave Chinner if (error) 71830f712c9SDave Chinner return error; 71930f712c9SDave Chinner free = curbp->b_addr; 72030f712c9SDave Chinner 72130f712c9SDave Chinner xfs_dir2_free_hdr_check(dp, curbp, curdb); 72230f712c9SDave Chinner } 72330f712c9SDave Chinner /* 72430f712c9SDave Chinner * Get the index for our entry. 72530f712c9SDave Chinner */ 7263d92c93bSChristoph Hellwig fi = xfs_dir2_db_to_fdindex(args->geo, curdb); 72730f712c9SDave Chinner /* 72830f712c9SDave Chinner * If it has room, return it. 72930f712c9SDave Chinner */ 730a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, &freehdr, free); 731a84f3d5cSChristoph Hellwig if (unlikely( 732a84f3d5cSChristoph Hellwig freehdr.bests[fi] == cpu_to_be16(NULLDATAOFF))) { 73330f712c9SDave Chinner XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", 73430f712c9SDave Chinner XFS_ERRLEVEL_LOW, mp); 73530f712c9SDave Chinner if (curfdb != newfdb) 73630f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 7372451337dSDave Chinner return -EFSCORRUPTED; 73830f712c9SDave Chinner } 73930f712c9SDave Chinner curfdb = newfdb; 740a84f3d5cSChristoph Hellwig if (be16_to_cpu(freehdr.bests[fi]) >= length) 74130f712c9SDave Chinner goto out; 74230f712c9SDave Chinner } 74330f712c9SDave Chinner } 74430f712c9SDave Chinner /* Didn't find any space */ 74530f712c9SDave Chinner fi = -1; 74630f712c9SDave Chinner out: 74730f712c9SDave Chinner ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); 74830f712c9SDave Chinner if (curbp) { 74930f712c9SDave Chinner /* Giving back a free block. */ 75030f712c9SDave Chinner state->extravalid = 1; 75130f712c9SDave Chinner state->extrablk.bp = curbp; 75230f712c9SDave Chinner state->extrablk.index = fi; 75330f712c9SDave Chinner state->extrablk.blkno = curfdb; 75430f712c9SDave Chinner 75530f712c9SDave Chinner /* 75630f712c9SDave Chinner * Important: this magic number is not in the buffer - it's for 75730f712c9SDave Chinner * buffer type information and therefore only the free/data type 75830f712c9SDave Chinner * matters here, not whether CRCs are enabled or not. 75930f712c9SDave Chinner */ 76030f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_FREE_MAGIC; 76130f712c9SDave Chinner } else { 76230f712c9SDave Chinner state->extravalid = 0; 76330f712c9SDave Chinner } 76430f712c9SDave Chinner /* 76530f712c9SDave Chinner * Return the index, that will be the insertion point. 76630f712c9SDave Chinner */ 76730f712c9SDave Chinner *indexp = index; 7682451337dSDave Chinner return -ENOENT; 76930f712c9SDave Chinner } 77030f712c9SDave Chinner 77130f712c9SDave Chinner /* 77230f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block. 77330f712c9SDave Chinner * The extrablk in state a data block. 77430f712c9SDave Chinner */ 77530f712c9SDave Chinner STATIC int 77630f712c9SDave Chinner xfs_dir2_leafn_lookup_for_entry( 77730f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 77830f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 77930f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 78030f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 78130f712c9SDave Chinner { 78230f712c9SDave Chinner struct xfs_buf *curbp = NULL; /* current data/free buffer */ 78330f712c9SDave Chinner xfs_dir2_db_t curdb = -1; /* current data block number */ 78430f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */ 78530f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 78630f712c9SDave Chinner int error; /* error return value */ 78730f712c9SDave Chinner int index; /* leaf entry index */ 78830f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 78930f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 79030f712c9SDave Chinner xfs_mount_t *mp; /* filesystem mount point */ 79130f712c9SDave Chinner xfs_dir2_db_t newdb; /* new data block number */ 79230f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 79330f712c9SDave Chinner enum xfs_dacmp cmp; /* comparison result */ 79430f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 79530f712c9SDave Chinner 79630f712c9SDave Chinner dp = args->dp; 79730f712c9SDave Chinner tp = args->trans; 79830f712c9SDave Chinner mp = dp->i_mount; 79930f712c9SDave Chinner leaf = bp->b_addr; 80051842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); 80130f712c9SDave Chinner 80230f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 803a5155b87SDarrick J. Wong if (leafhdr.count <= 0) { 804a5155b87SDarrick J. Wong xfs_buf_corruption_error(bp); 805858b44dcSDarrick J. Wong return -EFSCORRUPTED; 806a5155b87SDarrick J. Wong } 80730f712c9SDave Chinner 80830f712c9SDave Chinner /* 80930f712c9SDave Chinner * Look up the hash value in the leaf entries. 81030f712c9SDave Chinner */ 81130f712c9SDave Chinner index = xfs_dir2_leaf_search_hash(args, bp); 81230f712c9SDave Chinner /* 81330f712c9SDave Chinner * Do we have a buffer coming in? 81430f712c9SDave Chinner */ 81530f712c9SDave Chinner if (state->extravalid) { 81630f712c9SDave Chinner curbp = state->extrablk.bp; 81730f712c9SDave Chinner curdb = state->extrablk.blkno; 81830f712c9SDave Chinner } 81930f712c9SDave Chinner /* 82030f712c9SDave Chinner * Loop over leaf entries with the right hash value. 82130f712c9SDave Chinner */ 822787b0893SChristoph Hellwig for (lep = &leafhdr.ents[index]; 82330f712c9SDave Chinner index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; 82430f712c9SDave Chinner lep++, index++) { 82530f712c9SDave Chinner /* 82630f712c9SDave Chinner * Skip stale leaf entries. 82730f712c9SDave Chinner */ 82830f712c9SDave Chinner if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) 82930f712c9SDave Chinner continue; 83030f712c9SDave Chinner /* 83130f712c9SDave Chinner * Pull the data block number from the entry. 83230f712c9SDave Chinner */ 83330f712c9SDave Chinner newdb = xfs_dir2_dataptr_to_db(args->geo, 83430f712c9SDave Chinner be32_to_cpu(lep->address)); 83530f712c9SDave Chinner /* 83630f712c9SDave Chinner * Not adding a new entry, so we really want to find 83730f712c9SDave Chinner * the name given to us. 83830f712c9SDave Chinner * 83930f712c9SDave Chinner * If it's a different data block, go get it. 84030f712c9SDave Chinner */ 84130f712c9SDave Chinner if (newdb != curdb) { 84230f712c9SDave Chinner /* 84330f712c9SDave Chinner * If we had a block before that we aren't saving 84430f712c9SDave Chinner * for a CI name, drop it 84530f712c9SDave Chinner */ 84630f712c9SDave Chinner if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || 84730f712c9SDave Chinner curdb != state->extrablk.blkno)) 84830f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 84930f712c9SDave Chinner /* 85030f712c9SDave Chinner * If needing the block that is saved with a CI match, 85130f712c9SDave Chinner * use it otherwise read in the new data block. 85230f712c9SDave Chinner */ 85330f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT && 85430f712c9SDave Chinner newdb == state->extrablk.blkno) { 85530f712c9SDave Chinner ASSERT(state->extravalid); 85630f712c9SDave Chinner curbp = state->extrablk.bp; 85730f712c9SDave Chinner } else { 85830f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp, 85930f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, 86030f712c9SDave Chinner newdb), 86130f712c9SDave Chinner -1, &curbp); 86230f712c9SDave Chinner if (error) 86330f712c9SDave Chinner return error; 86430f712c9SDave Chinner } 86530f712c9SDave Chinner xfs_dir3_data_check(dp, curbp); 86630f712c9SDave Chinner curdb = newdb; 86730f712c9SDave Chinner } 86830f712c9SDave Chinner /* 86930f712c9SDave Chinner * Point to the data entry. 87030f712c9SDave Chinner */ 87130f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr + 87230f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo, 87330f712c9SDave Chinner be32_to_cpu(lep->address))); 87430f712c9SDave Chinner /* 87530f712c9SDave Chinner * Compare the entry and if it's an exact match, return 87630f712c9SDave Chinner * EEXIST immediately. If it's the first case-insensitive 87730f712c9SDave Chinner * match, store the block & inode number and continue looking. 87830f712c9SDave Chinner */ 87930f712c9SDave Chinner cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); 88030f712c9SDave Chinner if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { 88130f712c9SDave Chinner /* If there is a CI match block, drop it */ 88230f712c9SDave Chinner if (args->cmpresult != XFS_CMP_DIFFERENT && 88330f712c9SDave Chinner curdb != state->extrablk.blkno) 88430f712c9SDave Chinner xfs_trans_brelse(tp, state->extrablk.bp); 88530f712c9SDave Chinner args->cmpresult = cmp; 88630f712c9SDave Chinner args->inumber = be64_to_cpu(dep->inumber); 887*59b8b465SChristoph Hellwig args->filetype = xfs_dir2_data_get_ftype(mp, dep); 88830f712c9SDave Chinner *indexp = index; 88930f712c9SDave Chinner state->extravalid = 1; 89030f712c9SDave Chinner state->extrablk.bp = curbp; 89130f712c9SDave Chinner state->extrablk.blkno = curdb; 89230f712c9SDave Chinner state->extrablk.index = (int)((char *)dep - 89330f712c9SDave Chinner (char *)curbp->b_addr); 89430f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC; 89530f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops; 89630f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); 89730f712c9SDave Chinner if (cmp == XFS_CMP_EXACT) 8982451337dSDave Chinner return -EEXIST; 89930f712c9SDave Chinner } 90030f712c9SDave Chinner } 90130f712c9SDave Chinner ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT)); 90230f712c9SDave Chinner if (curbp) { 90330f712c9SDave Chinner if (args->cmpresult == XFS_CMP_DIFFERENT) { 90430f712c9SDave Chinner /* Giving back last used data block. */ 90530f712c9SDave Chinner state->extravalid = 1; 90630f712c9SDave Chinner state->extrablk.bp = curbp; 90730f712c9SDave Chinner state->extrablk.index = -1; 90830f712c9SDave Chinner state->extrablk.blkno = curdb; 90930f712c9SDave Chinner state->extrablk.magic = XFS_DIR2_DATA_MAGIC; 91030f712c9SDave Chinner curbp->b_ops = &xfs_dir3_data_buf_ops; 91130f712c9SDave Chinner xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); 91230f712c9SDave Chinner } else { 91330f712c9SDave Chinner /* If the curbp is not the CI match block, drop it */ 91430f712c9SDave Chinner if (state->extrablk.bp != curbp) 91530f712c9SDave Chinner xfs_trans_brelse(tp, curbp); 91630f712c9SDave Chinner } 91730f712c9SDave Chinner } else { 91830f712c9SDave Chinner state->extravalid = 0; 91930f712c9SDave Chinner } 92030f712c9SDave Chinner *indexp = index; 9212451337dSDave Chinner return -ENOENT; 92230f712c9SDave Chinner } 92330f712c9SDave Chinner 92430f712c9SDave Chinner /* 92530f712c9SDave Chinner * Look up a leaf entry in a node-format leaf block. 92630f712c9SDave Chinner * If this is an addname then the extrablk in state is a freespace block, 92730f712c9SDave Chinner * otherwise it's a data block. 92830f712c9SDave Chinner */ 92930f712c9SDave Chinner int 93030f712c9SDave Chinner xfs_dir2_leafn_lookup_int( 93130f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 93230f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 93330f712c9SDave Chinner int *indexp, /* out: leaf entry index */ 93430f712c9SDave Chinner xfs_da_state_t *state) /* state to fill in */ 93530f712c9SDave Chinner { 93630f712c9SDave Chinner if (args->op_flags & XFS_DA_OP_ADDNAME) 93730f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, 93830f712c9SDave Chinner state); 93930f712c9SDave Chinner return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); 94030f712c9SDave Chinner } 94130f712c9SDave Chinner 94230f712c9SDave Chinner /* 94330f712c9SDave Chinner * Move count leaf entries from source to destination leaf. 94430f712c9SDave Chinner * Log entries and headers. Stale entries are preserved. 94530f712c9SDave Chinner */ 94630f712c9SDave Chinner static void 94730f712c9SDave Chinner xfs_dir3_leafn_moveents( 94830f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 94930f712c9SDave Chinner struct xfs_buf *bp_s, /* source */ 95030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *shdr, 95130f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents, 95230f712c9SDave Chinner int start_s,/* source leaf index */ 95330f712c9SDave Chinner struct xfs_buf *bp_d, /* destination */ 95430f712c9SDave Chinner struct xfs_dir3_icleaf_hdr *dhdr, 95530f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents, 95630f712c9SDave Chinner int start_d,/* destination leaf index */ 95730f712c9SDave Chinner int count) /* count of leaves to copy */ 95830f712c9SDave Chinner { 95930f712c9SDave Chinner int stale; /* count stale leaves copied */ 96030f712c9SDave Chinner 96130f712c9SDave Chinner trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); 96230f712c9SDave Chinner 96330f712c9SDave Chinner /* 96430f712c9SDave Chinner * Silently return if nothing to do. 96530f712c9SDave Chinner */ 96630f712c9SDave Chinner if (count == 0) 96730f712c9SDave Chinner return; 96830f712c9SDave Chinner 96930f712c9SDave Chinner /* 97030f712c9SDave Chinner * If the destination index is not the end of the current 97130f712c9SDave Chinner * destination leaf entries, open up a hole in the destination 97230f712c9SDave Chinner * to hold the new entries. 97330f712c9SDave Chinner */ 97430f712c9SDave Chinner if (start_d < dhdr->count) { 97530f712c9SDave Chinner memmove(&dents[start_d + count], &dents[start_d], 97630f712c9SDave Chinner (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t)); 977787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count, 97830f712c9SDave Chinner count + dhdr->count - 1); 97930f712c9SDave Chinner } 98030f712c9SDave Chinner /* 98130f712c9SDave Chinner * If the source has stale leaves, count the ones in the copy range 98230f712c9SDave Chinner * so we can update the header correctly. 98330f712c9SDave Chinner */ 98430f712c9SDave Chinner if (shdr->stale) { 98530f712c9SDave Chinner int i; /* temp leaf index */ 98630f712c9SDave Chinner 98730f712c9SDave Chinner for (i = start_s, stale = 0; i < start_s + count; i++) { 98830f712c9SDave Chinner if (sents[i].address == 98930f712c9SDave Chinner cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) 99030f712c9SDave Chinner stale++; 99130f712c9SDave Chinner } 99230f712c9SDave Chinner } else 99330f712c9SDave Chinner stale = 0; 99430f712c9SDave Chinner /* 99530f712c9SDave Chinner * Copy the leaf entries from source to destination. 99630f712c9SDave Chinner */ 99730f712c9SDave Chinner memcpy(&dents[start_d], &sents[start_s], 99830f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t)); 999787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1); 100030f712c9SDave Chinner 100130f712c9SDave Chinner /* 100230f712c9SDave Chinner * If there are source entries after the ones we copied, 100330f712c9SDave Chinner * delete the ones we copied by sliding the next ones down. 100430f712c9SDave Chinner */ 100530f712c9SDave Chinner if (start_s + count < shdr->count) { 100630f712c9SDave Chinner memmove(&sents[start_s], &sents[start_s + count], 100730f712c9SDave Chinner count * sizeof(xfs_dir2_leaf_entry_t)); 1008787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s, 1009787b0893SChristoph Hellwig start_s + count - 1); 101030f712c9SDave Chinner } 101130f712c9SDave Chinner 101230f712c9SDave Chinner /* 101330f712c9SDave Chinner * Update the headers and log them. 101430f712c9SDave Chinner */ 101530f712c9SDave Chinner shdr->count -= count; 101630f712c9SDave Chinner shdr->stale -= stale; 101730f712c9SDave Chinner dhdr->count += count; 101830f712c9SDave Chinner dhdr->stale += stale; 101930f712c9SDave Chinner } 102030f712c9SDave Chinner 102130f712c9SDave Chinner /* 102230f712c9SDave Chinner * Determine the sort order of two leaf blocks. 102330f712c9SDave Chinner * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. 102430f712c9SDave Chinner */ 102530f712c9SDave Chinner int /* sort order */ 102630f712c9SDave Chinner xfs_dir2_leafn_order( 102730f712c9SDave Chinner struct xfs_inode *dp, 102830f712c9SDave Chinner struct xfs_buf *leaf1_bp, /* leaf1 buffer */ 102930f712c9SDave Chinner struct xfs_buf *leaf2_bp) /* leaf2 buffer */ 103030f712c9SDave Chinner { 103130f712c9SDave Chinner struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr; 103230f712c9SDave Chinner struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr; 103330f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1; 103430f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2; 103530f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1; 103630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 103730f712c9SDave Chinner 103851842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1); 103951842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2); 1040787b0893SChristoph Hellwig ents1 = hdr1.ents; 1041787b0893SChristoph Hellwig ents2 = hdr2.ents; 104230f712c9SDave Chinner 104330f712c9SDave Chinner if (hdr1.count > 0 && hdr2.count > 0 && 104430f712c9SDave Chinner (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) || 104530f712c9SDave Chinner be32_to_cpu(ents2[hdr2.count - 1].hashval) < 104630f712c9SDave Chinner be32_to_cpu(ents1[hdr1.count - 1].hashval))) 104730f712c9SDave Chinner return 1; 104830f712c9SDave Chinner return 0; 104930f712c9SDave Chinner } 105030f712c9SDave Chinner 105130f712c9SDave Chinner /* 105230f712c9SDave Chinner * Rebalance leaf entries between two leaf blocks. 105330f712c9SDave Chinner * This is actually only called when the second block is new, 105430f712c9SDave Chinner * though the code deals with the general case. 105530f712c9SDave Chinner * A new entry will be inserted in one of the blocks, and that 105630f712c9SDave Chinner * entry is taken into account when balancing. 105730f712c9SDave Chinner */ 105830f712c9SDave Chinner static void 105930f712c9SDave Chinner xfs_dir2_leafn_rebalance( 106030f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 106130f712c9SDave Chinner xfs_da_state_blk_t *blk1, /* first btree block */ 106230f712c9SDave Chinner xfs_da_state_blk_t *blk2) /* second btree block */ 106330f712c9SDave Chinner { 106430f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 106530f712c9SDave Chinner int count; /* count (& direction) leaves */ 106630f712c9SDave Chinner int isleft; /* new goes in left leaf */ 106730f712c9SDave Chinner xfs_dir2_leaf_t *leaf1; /* first leaf structure */ 106830f712c9SDave Chinner xfs_dir2_leaf_t *leaf2; /* second leaf structure */ 106930f712c9SDave Chinner int mid; /* midpoint leaf index */ 107030f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 107130f712c9SDave Chinner int oldstale; /* old count of stale leaves */ 107230f712c9SDave Chinner #endif 107330f712c9SDave Chinner int oldsum; /* old total leaf count */ 1074e4e542a6SGustavo A. R. Silva int swap_blocks; /* swapped leaf blocks */ 107530f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents1; 107630f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents2; 107730f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr1; 107830f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 107930f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 108030f712c9SDave Chinner 108130f712c9SDave Chinner args = state->args; 108230f712c9SDave Chinner /* 108330f712c9SDave Chinner * If the block order is wrong, swap the arguments. 108430f712c9SDave Chinner */ 1085e4e542a6SGustavo A. R. Silva swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp); 1086e4e542a6SGustavo A. R. Silva if (swap_blocks) 1087e4e542a6SGustavo A. R. Silva swap(blk1, blk2); 108830f712c9SDave Chinner 108930f712c9SDave Chinner leaf1 = blk1->bp->b_addr; 109030f712c9SDave Chinner leaf2 = blk2->bp->b_addr; 109151842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1); 109251842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2); 1093787b0893SChristoph Hellwig ents1 = hdr1.ents; 1094787b0893SChristoph Hellwig ents2 = hdr2.ents; 109530f712c9SDave Chinner 109630f712c9SDave Chinner oldsum = hdr1.count + hdr2.count; 109730f712c9SDave Chinner #if defined(DEBUG) || defined(XFS_WARN) 109830f712c9SDave Chinner oldstale = hdr1.stale + hdr2.stale; 109930f712c9SDave Chinner #endif 110030f712c9SDave Chinner mid = oldsum >> 1; 110130f712c9SDave Chinner 110230f712c9SDave Chinner /* 110330f712c9SDave Chinner * If the old leaf count was odd then the new one will be even, 110430f712c9SDave Chinner * so we need to divide the new count evenly. 110530f712c9SDave Chinner */ 110630f712c9SDave Chinner if (oldsum & 1) { 110730f712c9SDave Chinner xfs_dahash_t midhash; /* middle entry hash value */ 110830f712c9SDave Chinner 110930f712c9SDave Chinner if (mid >= hdr1.count) 111030f712c9SDave Chinner midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval); 111130f712c9SDave Chinner else 111230f712c9SDave Chinner midhash = be32_to_cpu(ents1[mid].hashval); 111330f712c9SDave Chinner isleft = args->hashval <= midhash; 111430f712c9SDave Chinner } 111530f712c9SDave Chinner /* 111630f712c9SDave Chinner * If the old count is even then the new count is odd, so there's 111730f712c9SDave Chinner * no preferred side for the new entry. 111830f712c9SDave Chinner * Pick the left one. 111930f712c9SDave Chinner */ 112030f712c9SDave Chinner else 112130f712c9SDave Chinner isleft = 1; 112230f712c9SDave Chinner /* 112330f712c9SDave Chinner * Calculate moved entry count. Positive means left-to-right, 112430f712c9SDave Chinner * negative means right-to-left. Then move the entries. 112530f712c9SDave Chinner */ 112630f712c9SDave Chinner count = hdr1.count - mid + (isleft == 0); 112730f712c9SDave Chinner if (count > 0) 112830f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1, 112930f712c9SDave Chinner hdr1.count - count, blk2->bp, 113030f712c9SDave Chinner &hdr2, ents2, 0, count); 113130f712c9SDave Chinner else if (count < 0) 113230f712c9SDave Chinner xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0, 113330f712c9SDave Chinner blk1->bp, &hdr1, ents1, 113430f712c9SDave Chinner hdr1.count, count); 113530f712c9SDave Chinner 113630f712c9SDave Chinner ASSERT(hdr1.count + hdr2.count == oldsum); 113730f712c9SDave Chinner ASSERT(hdr1.stale + hdr2.stale == oldstale); 113830f712c9SDave Chinner 113930f712c9SDave Chinner /* log the changes made when moving the entries */ 1140163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1); 1141163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2); 114230f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk1->bp); 114330f712c9SDave Chinner xfs_dir3_leaf_log_header(args, blk2->bp); 114430f712c9SDave Chinner 114530f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk1->bp); 114630f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk2->bp); 114730f712c9SDave Chinner 114830f712c9SDave Chinner /* 114930f712c9SDave Chinner * Mark whether we're inserting into the old or new leaf. 115030f712c9SDave Chinner */ 115130f712c9SDave Chinner if (hdr1.count < hdr2.count) 1152e4e542a6SGustavo A. R. Silva state->inleaf = swap_blocks; 115330f712c9SDave Chinner else if (hdr1.count > hdr2.count) 1154e4e542a6SGustavo A. R. Silva state->inleaf = !swap_blocks; 115530f712c9SDave Chinner else 1156e4e542a6SGustavo A. R. Silva state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count); 115730f712c9SDave Chinner /* 115830f712c9SDave Chinner * Adjust the expected index for insertion. 115930f712c9SDave Chinner */ 116030f712c9SDave Chinner if (!state->inleaf) 116130f712c9SDave Chinner blk2->index = blk1->index - hdr1.count; 116230f712c9SDave Chinner 116330f712c9SDave Chinner /* 116430f712c9SDave Chinner * Finally sanity check just to make sure we are not returning a 116530f712c9SDave Chinner * negative index 116630f712c9SDave Chinner */ 116730f712c9SDave Chinner if (blk2->index < 0) { 116830f712c9SDave Chinner state->inleaf = 1; 116930f712c9SDave Chinner blk2->index = 0; 117030f712c9SDave Chinner xfs_alert(dp->i_mount, 117130f712c9SDave Chinner "%s: picked the wrong leaf? reverting original leaf: blk1->index %d", 117230f712c9SDave Chinner __func__, blk1->index); 117330f712c9SDave Chinner } 117430f712c9SDave Chinner } 117530f712c9SDave Chinner 117630f712c9SDave Chinner static int 117730f712c9SDave Chinner xfs_dir3_data_block_free( 117830f712c9SDave Chinner xfs_da_args_t *args, 117930f712c9SDave Chinner struct xfs_dir2_data_hdr *hdr, 118030f712c9SDave Chinner struct xfs_dir2_free *free, 118130f712c9SDave Chinner xfs_dir2_db_t fdb, 118230f712c9SDave Chinner int findex, 118330f712c9SDave Chinner struct xfs_buf *fbp, 118430f712c9SDave Chinner int longest) 118530f712c9SDave Chinner { 118630f712c9SDave Chinner int logfree = 0; 118730f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 118830f712c9SDave Chinner struct xfs_inode *dp = args->dp; 118930f712c9SDave Chinner 11905ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); 119130f712c9SDave Chinner if (hdr) { 119230f712c9SDave Chinner /* 119330f712c9SDave Chinner * Data block is not empty, just set the free entry to the new 119430f712c9SDave Chinner * value. 119530f712c9SDave Chinner */ 1196a84f3d5cSChristoph Hellwig freehdr.bests[findex] = cpu_to_be16(longest); 1197a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); 119830f712c9SDave Chinner return 0; 119930f712c9SDave Chinner } 120030f712c9SDave Chinner 120130f712c9SDave Chinner /* One less used entry in the free table. */ 120230f712c9SDave Chinner freehdr.nused--; 120330f712c9SDave Chinner 120430f712c9SDave Chinner /* 120530f712c9SDave Chinner * If this was the last entry in the table, we can trim the table size 120630f712c9SDave Chinner * back. There might be other entries at the end referring to 120730f712c9SDave Chinner * non-existent data blocks, get those too. 120830f712c9SDave Chinner */ 120930f712c9SDave Chinner if (findex == freehdr.nvalid - 1) { 121030f712c9SDave Chinner int i; /* free entry index */ 121130f712c9SDave Chinner 121230f712c9SDave Chinner for (i = findex - 1; i >= 0; i--) { 1213a84f3d5cSChristoph Hellwig if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF)) 121430f712c9SDave Chinner break; 121530f712c9SDave Chinner } 121630f712c9SDave Chinner freehdr.nvalid = i + 1; 121730f712c9SDave Chinner logfree = 0; 121830f712c9SDave Chinner } else { 121930f712c9SDave Chinner /* Not the last entry, just punch it out. */ 1220a84f3d5cSChristoph Hellwig freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF); 122130f712c9SDave Chinner logfree = 1; 122230f712c9SDave Chinner } 122330f712c9SDave Chinner 1224200dada7SChristoph Hellwig xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr); 122530f712c9SDave Chinner xfs_dir2_free_log_header(args, fbp); 122630f712c9SDave Chinner 122730f712c9SDave Chinner /* 122830f712c9SDave Chinner * If there are no useful entries left in the block, get rid of the 122930f712c9SDave Chinner * block if we can. 123030f712c9SDave Chinner */ 123130f712c9SDave Chinner if (!freehdr.nused) { 123230f712c9SDave Chinner int error; 123330f712c9SDave Chinner 123430f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, fdb, fbp); 123530f712c9SDave Chinner if (error == 0) { 123630f712c9SDave Chinner fbp = NULL; 123730f712c9SDave Chinner logfree = 0; 12382451337dSDave Chinner } else if (error != -ENOSPC || args->total != 0) 123930f712c9SDave Chinner return error; 124030f712c9SDave Chinner /* 124130f712c9SDave Chinner * It's possible to get ENOSPC if there is no 124230f712c9SDave Chinner * space reservation. In this case some one 124330f712c9SDave Chinner * else will eventually get rid of this block. 124430f712c9SDave Chinner */ 124530f712c9SDave Chinner } 124630f712c9SDave Chinner 124730f712c9SDave Chinner /* Log the free entry that changed, unless we got rid of it. */ 124830f712c9SDave Chinner if (logfree) 1249a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); 125030f712c9SDave Chinner return 0; 125130f712c9SDave Chinner } 125230f712c9SDave Chinner 125330f712c9SDave Chinner /* 125430f712c9SDave Chinner * Remove an entry from a node directory. 125530f712c9SDave Chinner * This removes the leaf entry and the data entry, 125630f712c9SDave Chinner * and updates the free block if necessary. 125730f712c9SDave Chinner */ 125830f712c9SDave Chinner static int /* error */ 125930f712c9SDave Chinner xfs_dir2_leafn_remove( 126030f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 126130f712c9SDave Chinner struct xfs_buf *bp, /* leaf buffer */ 126230f712c9SDave Chinner int index, /* leaf entry index */ 126330f712c9SDave Chinner xfs_da_state_blk_t *dblk, /* data block */ 126430f712c9SDave Chinner int *rval) /* resulting block needs join */ 126530f712c9SDave Chinner { 1266d73e1ceeSChristoph Hellwig struct xfs_da_geometry *geo = args->geo; 126730f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 126830f712c9SDave Chinner xfs_dir2_db_t db; /* data block number */ 126930f712c9SDave Chinner struct xfs_buf *dbp; /* data block buffer */ 127030f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data block entry */ 127130f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 127230f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 127330f712c9SDave Chinner xfs_dir2_leaf_entry_t *lep; /* leaf entry */ 127430f712c9SDave Chinner int longest; /* longest data free entry */ 127530f712c9SDave Chinner int off; /* data block entry offset */ 127630f712c9SDave Chinner int needlog; /* need to log data header */ 127730f712c9SDave Chinner int needscan; /* need to rescan data frees */ 127830f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 127930f712c9SDave Chinner struct xfs_dir2_data_free *bf; /* bestfree table */ 128030f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 128130f712c9SDave Chinner 128230f712c9SDave Chinner trace_xfs_dir2_leafn_remove(args, index); 128330f712c9SDave Chinner 128430f712c9SDave Chinner dp = args->dp; 128530f712c9SDave Chinner tp = args->trans; 128630f712c9SDave Chinner leaf = bp->b_addr; 128751842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 128830f712c9SDave Chinner 128930f712c9SDave Chinner /* 129030f712c9SDave Chinner * Point to the entry we're removing. 129130f712c9SDave Chinner */ 1292787b0893SChristoph Hellwig lep = &leafhdr.ents[index]; 129330f712c9SDave Chinner 129430f712c9SDave Chinner /* 129530f712c9SDave Chinner * Extract the data block and offset from the entry. 129630f712c9SDave Chinner */ 1297d73e1ceeSChristoph Hellwig db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address)); 129830f712c9SDave Chinner ASSERT(dblk->blkno == db); 1299d73e1ceeSChristoph Hellwig off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)); 130030f712c9SDave Chinner ASSERT(dblk->index == off); 130130f712c9SDave Chinner 130230f712c9SDave Chinner /* 130330f712c9SDave Chinner * Kill the leaf entry by marking it stale. 130430f712c9SDave Chinner * Log the leaf block changes. 130530f712c9SDave Chinner */ 130630f712c9SDave Chinner leafhdr.stale++; 1307163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); 130830f712c9SDave Chinner xfs_dir3_leaf_log_header(args, bp); 130930f712c9SDave Chinner 131030f712c9SDave Chinner lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); 1311787b0893SChristoph Hellwig xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index); 131230f712c9SDave Chinner 131330f712c9SDave Chinner /* 131430f712c9SDave Chinner * Make the data entry free. Keep track of the longest freespace 131530f712c9SDave Chinner * in the data block in case it changes. 131630f712c9SDave Chinner */ 131730f712c9SDave Chinner dbp = dblk->bp; 131830f712c9SDave Chinner hdr = dbp->b_addr; 131930f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)((char *)hdr + off); 13201848b607SChristoph Hellwig bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 132130f712c9SDave Chinner longest = be16_to_cpu(bf[0].length); 132230f712c9SDave Chinner needlog = needscan = 0; 132330f712c9SDave Chinner xfs_dir2_data_make_free(args, dbp, off, 1324fdbb8c5bSChristoph Hellwig xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog, 1325fdbb8c5bSChristoph Hellwig &needscan); 132630f712c9SDave Chinner /* 132730f712c9SDave Chinner * Rescan the data block freespaces for bestfree. 132830f712c9SDave Chinner * Log the data block header if needed. 132930f712c9SDave Chinner */ 133030f712c9SDave Chinner if (needscan) 133130f712c9SDave Chinner xfs_dir2_data_freescan(dp, hdr, &needlog); 133230f712c9SDave Chinner if (needlog) 133330f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp); 133430f712c9SDave Chinner xfs_dir3_data_check(dp, dbp); 133530f712c9SDave Chinner /* 133630f712c9SDave Chinner * If the longest data block freespace changes, need to update 133730f712c9SDave Chinner * the corresponding freeblock entry. 133830f712c9SDave Chinner */ 133930f712c9SDave Chinner if (longest < be16_to_cpu(bf[0].length)) { 134030f712c9SDave Chinner int error; /* error return value */ 134130f712c9SDave Chinner struct xfs_buf *fbp; /* freeblock buffer */ 134230f712c9SDave Chinner xfs_dir2_db_t fdb; /* freeblock block number */ 134330f712c9SDave Chinner int findex; /* index in freeblock entries */ 134430f712c9SDave Chinner xfs_dir2_free_t *free; /* freeblock structure */ 134530f712c9SDave Chinner 134630f712c9SDave Chinner /* 134730f712c9SDave Chinner * Convert the data block number to a free block, 134830f712c9SDave Chinner * read in the free block. 134930f712c9SDave Chinner */ 1350d73e1ceeSChristoph Hellwig fdb = xfs_dir2_db_to_fdb(geo, db); 1351d73e1ceeSChristoph Hellwig error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb), 135230f712c9SDave Chinner &fbp); 135330f712c9SDave Chinner if (error) 135430f712c9SDave Chinner return error; 135530f712c9SDave Chinner free = fbp->b_addr; 135630f712c9SDave Chinner #ifdef DEBUG 135730f712c9SDave Chinner { 135830f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 13595ba30919SChristoph Hellwig 13605ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); 1361d73e1ceeSChristoph Hellwig ASSERT(freehdr.firstdb == geo->free_max_bests * 1362d73e1ceeSChristoph Hellwig (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET))); 136330f712c9SDave Chinner } 136430f712c9SDave Chinner #endif 136530f712c9SDave Chinner /* 136630f712c9SDave Chinner * Calculate which entry we need to fix. 136730f712c9SDave Chinner */ 1368d73e1ceeSChristoph Hellwig findex = xfs_dir2_db_to_fdindex(geo, db); 136930f712c9SDave Chinner longest = be16_to_cpu(bf[0].length); 137030f712c9SDave Chinner /* 137130f712c9SDave Chinner * If the data block is now empty we can get rid of it 137230f712c9SDave Chinner * (usually). 137330f712c9SDave Chinner */ 1374d73e1ceeSChristoph Hellwig if (longest == geo->blksize - geo->data_entry_offset) { 137530f712c9SDave Chinner /* 137630f712c9SDave Chinner * Try to punch out the data block. 137730f712c9SDave Chinner */ 137830f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, db, dbp); 137930f712c9SDave Chinner if (error == 0) { 138030f712c9SDave Chinner dblk->bp = NULL; 138130f712c9SDave Chinner hdr = NULL; 138230f712c9SDave Chinner } 138330f712c9SDave Chinner /* 138430f712c9SDave Chinner * We can get ENOSPC if there's no space reservation. 138530f712c9SDave Chinner * In this case just drop the buffer and some one else 138630f712c9SDave Chinner * will eventually get rid of the empty block. 138730f712c9SDave Chinner */ 13882451337dSDave Chinner else if (!(error == -ENOSPC && args->total == 0)) 138930f712c9SDave Chinner return error; 139030f712c9SDave Chinner } 139130f712c9SDave Chinner /* 139230f712c9SDave Chinner * If we got rid of the data block, we can eliminate that entry 139330f712c9SDave Chinner * in the free block. 139430f712c9SDave Chinner */ 139530f712c9SDave Chinner error = xfs_dir3_data_block_free(args, hdr, free, 139630f712c9SDave Chinner fdb, findex, fbp, longest); 139730f712c9SDave Chinner if (error) 139830f712c9SDave Chinner return error; 139930f712c9SDave Chinner } 140030f712c9SDave Chinner 140130f712c9SDave Chinner xfs_dir3_leaf_check(dp, bp); 140230f712c9SDave Chinner /* 140330f712c9SDave Chinner * Return indication of whether this leaf block is empty enough 140430f712c9SDave Chinner * to justify trying to join it with a neighbor. 140530f712c9SDave Chinner */ 1406d73e1ceeSChristoph Hellwig *rval = (geo->leaf_hdr_size + 1407787b0893SChristoph Hellwig (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) < 1408d73e1ceeSChristoph Hellwig geo->magicpct; 140930f712c9SDave Chinner return 0; 141030f712c9SDave Chinner } 141130f712c9SDave Chinner 141230f712c9SDave Chinner /* 141330f712c9SDave Chinner * Split the leaf entries in the old block into old and new blocks. 141430f712c9SDave Chinner */ 141530f712c9SDave Chinner int /* error */ 141630f712c9SDave Chinner xfs_dir2_leafn_split( 141730f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 141830f712c9SDave Chinner xfs_da_state_blk_t *oldblk, /* original block */ 141930f712c9SDave Chinner xfs_da_state_blk_t *newblk) /* newly created block */ 142030f712c9SDave Chinner { 142130f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 142230f712c9SDave Chinner xfs_dablk_t blkno; /* new leaf block number */ 142330f712c9SDave Chinner int error; /* error return value */ 142430f712c9SDave Chinner struct xfs_inode *dp; 142530f712c9SDave Chinner 142630f712c9SDave Chinner /* 142730f712c9SDave Chinner * Allocate space for a new leaf node. 142830f712c9SDave Chinner */ 142930f712c9SDave Chinner args = state->args; 143030f712c9SDave Chinner dp = args->dp; 143130f712c9SDave Chinner ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); 143230f712c9SDave Chinner error = xfs_da_grow_inode(args, &blkno); 143330f712c9SDave Chinner if (error) { 143430f712c9SDave Chinner return error; 143530f712c9SDave Chinner } 143630f712c9SDave Chinner /* 143730f712c9SDave Chinner * Initialize the new leaf block. 143830f712c9SDave Chinner */ 143930f712c9SDave Chinner error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno), 144030f712c9SDave Chinner &newblk->bp, XFS_DIR2_LEAFN_MAGIC); 144130f712c9SDave Chinner if (error) 144230f712c9SDave Chinner return error; 144330f712c9SDave Chinner 144430f712c9SDave Chinner newblk->blkno = blkno; 144530f712c9SDave Chinner newblk->magic = XFS_DIR2_LEAFN_MAGIC; 144630f712c9SDave Chinner /* 144730f712c9SDave Chinner * Rebalance the entries across the two leaves, link the new 144830f712c9SDave Chinner * block into the leaves. 144930f712c9SDave Chinner */ 145030f712c9SDave Chinner xfs_dir2_leafn_rebalance(state, oldblk, newblk); 145130f712c9SDave Chinner error = xfs_da3_blk_link(state, oldblk, newblk); 145230f712c9SDave Chinner if (error) { 145330f712c9SDave Chinner return error; 145430f712c9SDave Chinner } 145530f712c9SDave Chinner /* 145630f712c9SDave Chinner * Insert the new entry in the correct block. 145730f712c9SDave Chinner */ 145830f712c9SDave Chinner if (state->inleaf) 145930f712c9SDave Chinner error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); 146030f712c9SDave Chinner else 146130f712c9SDave Chinner error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); 146230f712c9SDave Chinner /* 146330f712c9SDave Chinner * Update last hashval in each block since we added the name. 146430f712c9SDave Chinner */ 14658e8877e6SDarrick J. Wong oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL); 14668e8877e6SDarrick J. Wong newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL); 146730f712c9SDave Chinner xfs_dir3_leaf_check(dp, oldblk->bp); 146830f712c9SDave Chinner xfs_dir3_leaf_check(dp, newblk->bp); 146930f712c9SDave Chinner return error; 147030f712c9SDave Chinner } 147130f712c9SDave Chinner 147230f712c9SDave Chinner /* 147330f712c9SDave Chinner * Check a leaf block and its neighbors to see if the block should be 147430f712c9SDave Chinner * collapsed into one or the other neighbor. Always keep the block 147530f712c9SDave Chinner * with the smaller block number. 147630f712c9SDave Chinner * If the current block is over 50% full, don't try to join it, return 0. 147730f712c9SDave Chinner * If the block is empty, fill in the state structure and return 2. 147830f712c9SDave Chinner * If it can be collapsed, fill in the state structure and return 1. 147930f712c9SDave Chinner * If nothing can be done, return 0. 148030f712c9SDave Chinner */ 148130f712c9SDave Chinner int /* error */ 148230f712c9SDave Chinner xfs_dir2_leafn_toosmall( 148330f712c9SDave Chinner xfs_da_state_t *state, /* btree cursor */ 148430f712c9SDave Chinner int *action) /* resulting action to take */ 148530f712c9SDave Chinner { 148630f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */ 148730f712c9SDave Chinner xfs_dablk_t blkno; /* leaf block number */ 148830f712c9SDave Chinner struct xfs_buf *bp; /* leaf buffer */ 148930f712c9SDave Chinner int bytes; /* bytes in use */ 149030f712c9SDave Chinner int count; /* leaf live entry count */ 149130f712c9SDave Chinner int error; /* error return value */ 149230f712c9SDave Chinner int forward; /* sibling block direction */ 149330f712c9SDave Chinner int i; /* sibling counter */ 149430f712c9SDave Chinner xfs_dir2_leaf_t *leaf; /* leaf structure */ 149530f712c9SDave Chinner int rval; /* result from path_shift */ 149630f712c9SDave Chinner struct xfs_dir3_icleaf_hdr leafhdr; 149730f712c9SDave Chinner struct xfs_dir2_leaf_entry *ents; 149830f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 149930f712c9SDave Chinner 150030f712c9SDave Chinner /* 150130f712c9SDave Chinner * Check for the degenerate case of the block being over 50% full. 150230f712c9SDave Chinner * If so, it's not worth even looking to see if we might be able 150330f712c9SDave Chinner * to coalesce with a sibling. 150430f712c9SDave Chinner */ 150530f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 150630f712c9SDave Chinner leaf = blk->bp->b_addr; 150751842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); 1508787b0893SChristoph Hellwig ents = leafhdr.ents; 150930f712c9SDave Chinner xfs_dir3_leaf_check(dp, blk->bp); 151030f712c9SDave Chinner 151130f712c9SDave Chinner count = leafhdr.count - leafhdr.stale; 1512545910bcSChristoph Hellwig bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]); 151330f712c9SDave Chinner if (bytes > (state->args->geo->blksize >> 1)) { 151430f712c9SDave Chinner /* 151530f712c9SDave Chinner * Blk over 50%, don't try to join. 151630f712c9SDave Chinner */ 151730f712c9SDave Chinner *action = 0; 151830f712c9SDave Chinner return 0; 151930f712c9SDave Chinner } 152030f712c9SDave Chinner /* 152130f712c9SDave Chinner * Check for the degenerate case of the block being empty. 152230f712c9SDave Chinner * If the block is empty, we'll simply delete it, no need to 152330f712c9SDave Chinner * coalesce it with a sibling block. We choose (arbitrarily) 152430f712c9SDave Chinner * to merge with the forward block unless it is NULL. 152530f712c9SDave Chinner */ 152630f712c9SDave Chinner if (count == 0) { 152730f712c9SDave Chinner /* 152830f712c9SDave Chinner * Make altpath point to the block we want to keep and 152930f712c9SDave Chinner * path point to the block we want to drop (this one). 153030f712c9SDave Chinner */ 153130f712c9SDave Chinner forward = (leafhdr.forw != 0); 153230f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path)); 153330f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0, 153430f712c9SDave Chinner &rval); 153530f712c9SDave Chinner if (error) 153630f712c9SDave Chinner return error; 153730f712c9SDave Chinner *action = rval ? 2 : 0; 153830f712c9SDave Chinner return 0; 153930f712c9SDave Chinner } 154030f712c9SDave Chinner /* 154130f712c9SDave Chinner * Examine each sibling block to see if we can coalesce with 154230f712c9SDave Chinner * at least 25% free space to spare. We need to figure out 154330f712c9SDave Chinner * whether to merge with the forward or the backward block. 154430f712c9SDave Chinner * We prefer coalescing with the lower numbered sibling so as 154530f712c9SDave Chinner * to shrink a directory over time. 154630f712c9SDave Chinner */ 154730f712c9SDave Chinner forward = leafhdr.forw < leafhdr.back; 154830f712c9SDave Chinner for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { 154930f712c9SDave Chinner struct xfs_dir3_icleaf_hdr hdr2; 155030f712c9SDave Chinner 155130f712c9SDave Chinner blkno = forward ? leafhdr.forw : leafhdr.back; 155230f712c9SDave Chinner if (blkno == 0) 155330f712c9SDave Chinner continue; 155430f712c9SDave Chinner /* 155530f712c9SDave Chinner * Read the sibling leaf block. 155630f712c9SDave Chinner */ 155730f712c9SDave Chinner error = xfs_dir3_leafn_read(state->args->trans, dp, 155830f712c9SDave Chinner blkno, -1, &bp); 155930f712c9SDave Chinner if (error) 156030f712c9SDave Chinner return error; 156130f712c9SDave Chinner 156230f712c9SDave Chinner /* 156330f712c9SDave Chinner * Count bytes in the two blocks combined. 156430f712c9SDave Chinner */ 156530f712c9SDave Chinner count = leafhdr.count - leafhdr.stale; 156630f712c9SDave Chinner bytes = state->args->geo->blksize - 156730f712c9SDave Chinner (state->args->geo->blksize >> 2); 156830f712c9SDave Chinner 156930f712c9SDave Chinner leaf = bp->b_addr; 157051842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf); 1571787b0893SChristoph Hellwig ents = hdr2.ents; 157230f712c9SDave Chinner count += hdr2.count - hdr2.stale; 157330f712c9SDave Chinner bytes -= count * sizeof(ents[0]); 157430f712c9SDave Chinner 157530f712c9SDave Chinner /* 157630f712c9SDave Chinner * Fits with at least 25% to spare. 157730f712c9SDave Chinner */ 157830f712c9SDave Chinner if (bytes >= 0) 157930f712c9SDave Chinner break; 158030f712c9SDave Chinner xfs_trans_brelse(state->args->trans, bp); 158130f712c9SDave Chinner } 158230f712c9SDave Chinner /* 158330f712c9SDave Chinner * Didn't like either block, give up. 158430f712c9SDave Chinner */ 158530f712c9SDave Chinner if (i >= 2) { 158630f712c9SDave Chinner *action = 0; 158730f712c9SDave Chinner return 0; 158830f712c9SDave Chinner } 158930f712c9SDave Chinner 159030f712c9SDave Chinner /* 159130f712c9SDave Chinner * Make altpath point to the block we want to keep (the lower 159230f712c9SDave Chinner * numbered block) and path point to the block we want to drop. 159330f712c9SDave Chinner */ 159430f712c9SDave Chinner memcpy(&state->altpath, &state->path, sizeof(state->path)); 159530f712c9SDave Chinner if (blkno < blk->blkno) 159630f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->altpath, forward, 0, 159730f712c9SDave Chinner &rval); 159830f712c9SDave Chinner else 159930f712c9SDave Chinner error = xfs_da3_path_shift(state, &state->path, forward, 0, 160030f712c9SDave Chinner &rval); 160130f712c9SDave Chinner if (error) { 160230f712c9SDave Chinner return error; 160330f712c9SDave Chinner } 160430f712c9SDave Chinner *action = rval ? 0 : 1; 160530f712c9SDave Chinner return 0; 160630f712c9SDave Chinner } 160730f712c9SDave Chinner 160830f712c9SDave Chinner /* 160930f712c9SDave Chinner * Move all the leaf entries from drop_blk to save_blk. 161030f712c9SDave Chinner * This is done as part of a join operation. 161130f712c9SDave Chinner */ 161230f712c9SDave Chinner void 161330f712c9SDave Chinner xfs_dir2_leafn_unbalance( 161430f712c9SDave Chinner xfs_da_state_t *state, /* cursor */ 161530f712c9SDave Chinner xfs_da_state_blk_t *drop_blk, /* dead block */ 161630f712c9SDave Chinner xfs_da_state_blk_t *save_blk) /* surviving block */ 161730f712c9SDave Chinner { 161830f712c9SDave Chinner xfs_da_args_t *args; /* operation arguments */ 161930f712c9SDave Chinner xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ 162030f712c9SDave Chinner xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ 162130f712c9SDave Chinner struct xfs_dir3_icleaf_hdr savehdr; 162230f712c9SDave Chinner struct xfs_dir3_icleaf_hdr drophdr; 162330f712c9SDave Chinner struct xfs_dir2_leaf_entry *sents; 162430f712c9SDave Chinner struct xfs_dir2_leaf_entry *dents; 162530f712c9SDave Chinner struct xfs_inode *dp = state->args->dp; 162630f712c9SDave Chinner 162730f712c9SDave Chinner args = state->args; 162830f712c9SDave Chinner ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); 162930f712c9SDave Chinner ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); 163030f712c9SDave Chinner drop_leaf = drop_blk->bp->b_addr; 163130f712c9SDave Chinner save_leaf = save_blk->bp->b_addr; 163230f712c9SDave Chinner 163351842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf); 163451842556SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf); 1635787b0893SChristoph Hellwig sents = savehdr.ents; 1636787b0893SChristoph Hellwig dents = drophdr.ents; 163730f712c9SDave Chinner 163830f712c9SDave Chinner /* 163930f712c9SDave Chinner * If there are any stale leaf entries, take this opportunity 164030f712c9SDave Chinner * to purge them. 164130f712c9SDave Chinner */ 164230f712c9SDave Chinner if (drophdr.stale) 164330f712c9SDave Chinner xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp); 164430f712c9SDave Chinner if (savehdr.stale) 164530f712c9SDave Chinner xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp); 164630f712c9SDave Chinner 164730f712c9SDave Chinner /* 164830f712c9SDave Chinner * Move the entries from drop to the appropriate end of save. 164930f712c9SDave Chinner */ 165030f712c9SDave Chinner drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval); 165130f712c9SDave Chinner if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp)) 165230f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, 165330f712c9SDave Chinner save_blk->bp, &savehdr, sents, 0, 165430f712c9SDave Chinner drophdr.count); 165530f712c9SDave Chinner else 165630f712c9SDave Chinner xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, 165730f712c9SDave Chinner save_blk->bp, &savehdr, sents, 165830f712c9SDave Chinner savehdr.count, drophdr.count); 165930f712c9SDave Chinner save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval); 166030f712c9SDave Chinner 166130f712c9SDave Chinner /* log the changes made when moving the entries */ 1662163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr); 1663163fbbb3SChristoph Hellwig xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr); 166430f712c9SDave Chinner xfs_dir3_leaf_log_header(args, save_blk->bp); 166530f712c9SDave Chinner xfs_dir3_leaf_log_header(args, drop_blk->bp); 166630f712c9SDave Chinner 166730f712c9SDave Chinner xfs_dir3_leaf_check(dp, save_blk->bp); 166830f712c9SDave Chinner xfs_dir3_leaf_check(dp, drop_blk->bp); 166930f712c9SDave Chinner } 167030f712c9SDave Chinner 167130f712c9SDave Chinner /* 1672a07258a6SDave Chinner * Add a new data block to the directory at the free space index that the caller 1673a07258a6SDave Chinner * has specified. 1674a07258a6SDave Chinner */ 1675a07258a6SDave Chinner static int 1676a07258a6SDave Chinner xfs_dir2_node_add_datablk( 1677a07258a6SDave Chinner struct xfs_da_args *args, 1678a07258a6SDave Chinner struct xfs_da_state_blk *fblk, 1679a07258a6SDave Chinner xfs_dir2_db_t *dbno, 1680a07258a6SDave Chinner struct xfs_buf **dbpp, 1681a07258a6SDave Chinner struct xfs_buf **fbpp, 1682195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr, 1683a07258a6SDave Chinner int *findex) 1684a07258a6SDave Chinner { 1685a07258a6SDave Chinner struct xfs_inode *dp = args->dp; 1686a07258a6SDave Chinner struct xfs_trans *tp = args->trans; 1687a07258a6SDave Chinner struct xfs_mount *mp = dp->i_mount; 1688a07258a6SDave Chinner struct xfs_dir2_data_free *bf; 1689a07258a6SDave Chinner xfs_dir2_db_t fbno; 1690a07258a6SDave Chinner struct xfs_buf *fbp; 1691a07258a6SDave Chinner struct xfs_buf *dbp; 1692a07258a6SDave Chinner int error; 1693a07258a6SDave Chinner 1694a07258a6SDave Chinner /* Not allowed to allocate, return failure. */ 16950e822255SDave Chinner if (args->total == 0) 1696a07258a6SDave Chinner return -ENOSPC; 1697a07258a6SDave Chinner 1698a07258a6SDave Chinner /* Allocate and initialize the new data block. */ 1699a07258a6SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno); 1700a07258a6SDave Chinner if (error) 1701a07258a6SDave Chinner return error; 1702a07258a6SDave Chinner error = xfs_dir3_data_init(args, *dbno, &dbp); 1703a07258a6SDave Chinner if (error) 1704a07258a6SDave Chinner return error; 1705a07258a6SDave Chinner 1706a07258a6SDave Chinner /* 1707a07258a6SDave Chinner * Get the freespace block corresponding to the data block 1708a07258a6SDave Chinner * that was just allocated. 1709a07258a6SDave Chinner */ 17103d92c93bSChristoph Hellwig fbno = xfs_dir2_db_to_fdb(args->geo, *dbno); 1711a07258a6SDave Chinner error = xfs_dir2_free_try_read(tp, dp, 1712a07258a6SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), &fbp); 1713a07258a6SDave Chinner if (error) 1714a07258a6SDave Chinner return error; 1715a07258a6SDave Chinner 1716a07258a6SDave Chinner /* 1717a07258a6SDave Chinner * If there wasn't a freespace block, the read will 1718a07258a6SDave Chinner * return a NULL fbp. Allocate and initialize a new one. 1719a07258a6SDave Chinner */ 1720a07258a6SDave Chinner if (!fbp) { 1721a07258a6SDave Chinner error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno); 1722a07258a6SDave Chinner if (error) 1723a07258a6SDave Chinner return error; 1724a07258a6SDave Chinner 17253d92c93bSChristoph Hellwig if (xfs_dir2_db_to_fdb(args->geo, *dbno) != fbno) { 1726a07258a6SDave Chinner xfs_alert(mp, 1727a07258a6SDave Chinner "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld", 1728a07258a6SDave Chinner __func__, (unsigned long long)dp->i_ino, 17293d92c93bSChristoph Hellwig (long long)xfs_dir2_db_to_fdb(args->geo, *dbno), 1730a07258a6SDave Chinner (long long)*dbno, (long long)fbno); 1731a07258a6SDave Chinner if (fblk) { 1732a07258a6SDave Chinner xfs_alert(mp, 1733a07258a6SDave Chinner " fblk "PTR_FMT" blkno %llu index %d magic 0x%x", 1734a07258a6SDave Chinner fblk, (unsigned long long)fblk->blkno, 1735a07258a6SDave Chinner fblk->index, fblk->magic); 1736a07258a6SDave Chinner } else { 1737a07258a6SDave Chinner xfs_alert(mp, " ... fblk is NULL"); 1738a07258a6SDave Chinner } 1739a07258a6SDave Chinner XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); 1740a07258a6SDave Chinner return -EFSCORRUPTED; 1741a07258a6SDave Chinner } 1742a07258a6SDave Chinner 1743a07258a6SDave Chinner /* Get a buffer for the new block. */ 1744a07258a6SDave Chinner error = xfs_dir3_free_get_buf(args, fbno, &fbp); 1745a07258a6SDave Chinner if (error) 1746a07258a6SDave Chinner return error; 1747a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr); 1748a07258a6SDave Chinner 1749a07258a6SDave Chinner /* Remember the first slot as our empty slot. */ 1750195b0a44SChristoph Hellwig hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo, 1751a07258a6SDave Chinner XFS_DIR2_FREE_OFFSET)) * 17525893e4feSChristoph Hellwig args->geo->free_max_bests; 1753a07258a6SDave Chinner } else { 1754a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr); 1755a07258a6SDave Chinner } 1756a07258a6SDave Chinner 1757a07258a6SDave Chinner /* Set the freespace block index from the data block number. */ 17583d92c93bSChristoph Hellwig *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno); 1759a07258a6SDave Chinner 1760a07258a6SDave Chinner /* Extend the freespace table if the new data block is off the end. */ 1761195b0a44SChristoph Hellwig if (*findex >= hdr->nvalid) { 17625893e4feSChristoph Hellwig ASSERT(*findex < args->geo->free_max_bests); 1763195b0a44SChristoph Hellwig hdr->nvalid = *findex + 1; 1764a84f3d5cSChristoph Hellwig hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF); 1765a07258a6SDave Chinner } 1766a07258a6SDave Chinner 1767a07258a6SDave Chinner /* 1768a07258a6SDave Chinner * If this entry was for an empty data block (this should always be 1769a07258a6SDave Chinner * true) then update the header. 1770a07258a6SDave Chinner */ 1771a84f3d5cSChristoph Hellwig if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) { 1772195b0a44SChristoph Hellwig hdr->nused++; 1773195b0a44SChristoph Hellwig xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr); 1774a07258a6SDave Chinner xfs_dir2_free_log_header(args, fbp); 1775a07258a6SDave Chinner } 1776a07258a6SDave Chinner 1777a07258a6SDave Chinner /* Update the freespace value for the new block in the table. */ 17781848b607SChristoph Hellwig bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr); 1779a84f3d5cSChristoph Hellwig hdr->bests[*findex] = bf[0].length; 1780a07258a6SDave Chinner 1781a07258a6SDave Chinner *dbpp = dbp; 1782a07258a6SDave Chinner *fbpp = fbp; 1783a07258a6SDave Chinner return 0; 1784a07258a6SDave Chinner } 1785a07258a6SDave Chinner 17860e822255SDave Chinner static int 17870e822255SDave Chinner xfs_dir2_node_find_freeblk( 17880e822255SDave Chinner struct xfs_da_args *args, 17890e822255SDave Chinner struct xfs_da_state_blk *fblk, 17900e822255SDave Chinner xfs_dir2_db_t *dbnop, 17910e822255SDave Chinner struct xfs_buf **fbpp, 1792195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr *hdr, 17930e822255SDave Chinner int *findexp, 17940e822255SDave Chinner int length) 179530f712c9SDave Chinner { 17960e822255SDave Chinner struct xfs_inode *dp = args->dp; 17970e822255SDave Chinner struct xfs_trans *tp = args->trans; 17980e822255SDave Chinner struct xfs_buf *fbp = NULL; 1799756c6f0fSDave Chinner xfs_dir2_db_t firstfbno; 18000e822255SDave Chinner xfs_dir2_db_t lastfbno; 18010e822255SDave Chinner xfs_dir2_db_t ifbno = -1; 18020e822255SDave Chinner xfs_dir2_db_t dbno = -1; 1803756c6f0fSDave Chinner xfs_dir2_db_t fbno; 18040e822255SDave Chinner xfs_fileoff_t fo; 1805610125abSDave Chinner int findex = 0; 18060e822255SDave Chinner int error; 180730f712c9SDave Chinner 180830f712c9SDave Chinner /* 180930f712c9SDave Chinner * If we came in with a freespace block that means that lookup 181030f712c9SDave Chinner * found an entry with our hash value. This is the freespace 181130f712c9SDave Chinner * block for that data entry. 181230f712c9SDave Chinner */ 181330f712c9SDave Chinner if (fblk) { 181430f712c9SDave Chinner fbp = fblk->bp; 181530f712c9SDave Chinner findex = fblk->index; 1816a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr); 18170e822255SDave Chinner if (findex >= 0) { 18180e822255SDave Chinner /* caller already found the freespace for us. */ 1819195b0a44SChristoph Hellwig ASSERT(findex < hdr->nvalid); 1820a84f3d5cSChristoph Hellwig ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF); 1821a84f3d5cSChristoph Hellwig ASSERT(be16_to_cpu(hdr->bests[findex]) >= length); 1822195b0a44SChristoph Hellwig dbno = hdr->firstdb + findex; 1823a07258a6SDave Chinner goto found_block; 18240e822255SDave Chinner } 18250e822255SDave Chinner 182630f712c9SDave Chinner /* 182730f712c9SDave Chinner * The data block looked at didn't have enough room. 182830f712c9SDave Chinner * We'll start at the beginning of the freespace entries. 182930f712c9SDave Chinner */ 18300e822255SDave Chinner ifbno = fblk->blkno; 1831610125abSDave Chinner xfs_trans_brelse(tp, fbp); 1832610125abSDave Chinner fbp = NULL; 1833610125abSDave Chinner fblk->bp = NULL; 183430f712c9SDave Chinner } 18350e822255SDave Chinner 18360e822255SDave Chinner /* 18370e822255SDave Chinner * If we don't have a data block yet, we're going to scan the freespace 1838610125abSDave Chinner * data for a data block with enough free space in it. 18390e822255SDave Chinner */ 18400e822255SDave Chinner error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK); 18410e822255SDave Chinner if (error) 18420e822255SDave Chinner return error; 18430e822255SDave Chinner lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo); 1844756c6f0fSDave Chinner firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET); 18450e822255SDave Chinner 1846756c6f0fSDave Chinner for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) { 1847610125abSDave Chinner /* If it's ifbno we already looked at it. */ 1848610125abSDave Chinner if (fbno == ifbno) 1849610125abSDave Chinner continue; 1850610125abSDave Chinner 185130f712c9SDave Chinner /* 1852610125abSDave Chinner * Read the block. There can be holes in the freespace blocks, 1853610125abSDave Chinner * so this might not succeed. This should be really rare, so 1854610125abSDave Chinner * there's no reason to avoid it. 185530f712c9SDave Chinner */ 185630f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, 185730f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, fbno), 185830f712c9SDave Chinner &fbp); 185930f712c9SDave Chinner if (error) 186030f712c9SDave Chinner return error; 186130f712c9SDave Chinner if (!fbp) 186230f712c9SDave Chinner continue; 1863610125abSDave Chinner 1864a84f3d5cSChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr); 1865610125abSDave Chinner 1866610125abSDave Chinner /* Scan the free entry array for a large enough free space. */ 1867195b0a44SChristoph Hellwig for (findex = hdr->nvalid - 1; findex >= 0; findex--) { 1868a84f3d5cSChristoph Hellwig if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF && 1869a84f3d5cSChristoph Hellwig be16_to_cpu(hdr->bests[findex]) >= length) { 1870195b0a44SChristoph Hellwig dbno = hdr->firstdb + findex; 1871610125abSDave Chinner goto found_block; 1872610125abSDave Chinner } 1873610125abSDave Chinner } 1874610125abSDave Chinner 1875610125abSDave Chinner /* Didn't find free space, go on to next free block */ 187630f712c9SDave Chinner xfs_trans_brelse(tp, fbp); 187730f712c9SDave Chinner } 1878610125abSDave Chinner 18790e822255SDave Chinner found_block: 18800e822255SDave Chinner *dbnop = dbno; 18810e822255SDave Chinner *fbpp = fbp; 18820e822255SDave Chinner *findexp = findex; 18830e822255SDave Chinner return 0; 18840e822255SDave Chinner } 18850e822255SDave Chinner 18860e822255SDave Chinner /* 18870e822255SDave Chinner * Add the data entry for a node-format directory name addition. 18880e822255SDave Chinner * The leaf entry is added in xfs_dir2_leafn_add. 18890e822255SDave Chinner * We may enter with a freespace block that the lookup found. 18900e822255SDave Chinner */ 18910e822255SDave Chinner static int 18920e822255SDave Chinner xfs_dir2_node_addname_int( 18930e822255SDave Chinner struct xfs_da_args *args, /* operation arguments */ 18940e822255SDave Chinner struct xfs_da_state_blk *fblk) /* optional freespace block */ 18950e822255SDave Chinner { 18960e822255SDave Chinner struct xfs_dir2_data_unused *dup; /* data unused entry pointer */ 18970e822255SDave Chinner struct xfs_dir2_data_entry *dep; /* data entry pointer */ 18980e822255SDave Chinner struct xfs_dir2_data_hdr *hdr; /* data block header */ 18990e822255SDave Chinner struct xfs_dir2_data_free *bf; 19000e822255SDave Chinner struct xfs_trans *tp = args->trans; 19010e822255SDave Chinner struct xfs_inode *dp = args->dp; 1902195b0a44SChristoph Hellwig struct xfs_dir3_icfree_hdr freehdr; 19030e822255SDave Chinner struct xfs_buf *dbp; /* data block buffer */ 19040e822255SDave Chinner struct xfs_buf *fbp; /* freespace buffer */ 19050e822255SDave Chinner xfs_dir2_data_aoff_t aoff; 19060e822255SDave Chinner xfs_dir2_db_t dbno; /* data block number */ 19070e822255SDave Chinner int error; /* error return value */ 19080e822255SDave Chinner int findex; /* freespace entry index */ 19090e822255SDave Chinner int length; /* length of the new entry */ 19100e822255SDave Chinner int logfree = 0; /* need to log free entry */ 19110e822255SDave Chinner int needlog = 0; /* need to log data header */ 19120e822255SDave Chinner int needscan = 0; /* need to rescan data frees */ 19130e822255SDave Chinner __be16 *tagp; /* data entry tag pointer */ 19140e822255SDave Chinner 1915fdbb8c5bSChristoph Hellwig length = xfs_dir2_data_entsize(dp->i_mount, args->namelen); 1916195b0a44SChristoph Hellwig error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr, 1917195b0a44SChristoph Hellwig &findex, length); 19180e822255SDave Chinner if (error) 19190e822255SDave Chinner return error; 19200e822255SDave Chinner 19210e822255SDave Chinner /* 19220e822255SDave Chinner * Now we know if we must allocate blocks, so if we are checking whether 19230e822255SDave Chinner * we can insert without allocation then we can return now. 19240e822255SDave Chinner */ 19250e822255SDave Chinner if (args->op_flags & XFS_DA_OP_JUSTCHECK) { 19260e822255SDave Chinner if (dbno == -1) 19270e822255SDave Chinner return -ENOSPC; 19280e822255SDave Chinner return 0; 19290e822255SDave Chinner } 1930a07258a6SDave Chinner 193130f712c9SDave Chinner /* 193230f712c9SDave Chinner * If we don't have a data block, we need to allocate one and make 193330f712c9SDave Chinner * the freespace entries refer to it. 193430f712c9SDave Chinner */ 1935a07258a6SDave Chinner if (dbno == -1) { 1936a07258a6SDave Chinner /* we're going to have to log the free block index later */ 193730f712c9SDave Chinner logfree = 1; 19380e822255SDave Chinner error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp, 1939195b0a44SChristoph Hellwig &freehdr, &findex); 1940a07258a6SDave Chinner } else { 1941a07258a6SDave Chinner /* Read the data block in. */ 194230f712c9SDave Chinner error = xfs_dir3_data_read(tp, dp, 194330f712c9SDave Chinner xfs_dir2_db_to_da(args->geo, dbno), 194430f712c9SDave Chinner -1, &dbp); 19450e822255SDave Chinner } 194630f712c9SDave Chinner if (error) 194730f712c9SDave Chinner return error; 1948a07258a6SDave Chinner 1949a07258a6SDave Chinner /* setup for data block up now */ 195030f712c9SDave Chinner hdr = dbp->b_addr; 19511848b607SChristoph Hellwig bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); 195230f712c9SDave Chinner ASSERT(be16_to_cpu(bf[0].length) >= length); 1953a07258a6SDave Chinner 1954a07258a6SDave Chinner /* Point to the existing unused space. */ 195530f712c9SDave Chinner dup = (xfs_dir2_data_unused_t *) 195630f712c9SDave Chinner ((char *)hdr + be16_to_cpu(bf[0].offset)); 1957a07258a6SDave Chinner 1958a07258a6SDave Chinner /* Mark the first part of the unused space, inuse for us. */ 19596915ef35SDarrick J. Wong aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); 19606915ef35SDarrick J. Wong error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length, 196130f712c9SDave Chinner &needlog, &needscan); 19626915ef35SDarrick J. Wong if (error) { 19636915ef35SDarrick J. Wong xfs_trans_brelse(tp, dbp); 19646915ef35SDarrick J. Wong return error; 19656915ef35SDarrick J. Wong } 1966a07258a6SDave Chinner 1967a07258a6SDave Chinner /* Fill in the new entry and log it. */ 196830f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *)dup; 196930f712c9SDave Chinner dep->inumber = cpu_to_be64(args->inumber); 197030f712c9SDave Chinner dep->namelen = args->namelen; 197130f712c9SDave Chinner memcpy(dep->name, args->name, dep->namelen); 1972*59b8b465SChristoph Hellwig xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); 19737e8ae7bdSChristoph Hellwig tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep); 197430f712c9SDave Chinner *tagp = cpu_to_be16((char *)dep - (char *)hdr); 197530f712c9SDave Chinner xfs_dir2_data_log_entry(args, dbp, dep); 1976a07258a6SDave Chinner 1977a07258a6SDave Chinner /* Rescan the freespace and log the data block if needed. */ 197830f712c9SDave Chinner if (needscan) 197930f712c9SDave Chinner xfs_dir2_data_freescan(dp, hdr, &needlog); 198030f712c9SDave Chinner if (needlog) 198130f712c9SDave Chinner xfs_dir2_data_log_header(args, dbp); 1982a07258a6SDave Chinner 1983a07258a6SDave Chinner /* If the freespace block entry is now wrong, update it. */ 1984a84f3d5cSChristoph Hellwig if (freehdr.bests[findex] != bf[0].length) { 1985a84f3d5cSChristoph Hellwig freehdr.bests[findex] = bf[0].length; 198630f712c9SDave Chinner logfree = 1; 198730f712c9SDave Chinner } 1988a07258a6SDave Chinner 1989a07258a6SDave Chinner /* Log the freespace entry if needed. */ 199030f712c9SDave Chinner if (logfree) 1991a84f3d5cSChristoph Hellwig xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); 1992a07258a6SDave Chinner 1993a07258a6SDave Chinner /* Return the data block and offset in args. */ 199430f712c9SDave Chinner args->blkno = (xfs_dablk_t)dbno; 199530f712c9SDave Chinner args->index = be16_to_cpu(*tagp); 199630f712c9SDave Chinner return 0; 199730f712c9SDave Chinner } 199830f712c9SDave Chinner 199930f712c9SDave Chinner /* 2000aee7754bSDave Chinner * Top-level node form directory addname routine. 2001aee7754bSDave Chinner */ 2002aee7754bSDave Chinner int /* error */ 2003aee7754bSDave Chinner xfs_dir2_node_addname( 2004aee7754bSDave Chinner xfs_da_args_t *args) /* operation arguments */ 2005aee7754bSDave Chinner { 2006aee7754bSDave Chinner xfs_da_state_blk_t *blk; /* leaf block for insert */ 2007aee7754bSDave Chinner int error; /* error return value */ 2008aee7754bSDave Chinner int rval; /* sub-return value */ 2009aee7754bSDave Chinner xfs_da_state_t *state; /* btree cursor */ 2010aee7754bSDave Chinner 2011aee7754bSDave Chinner trace_xfs_dir2_node_addname(args); 2012aee7754bSDave Chinner 2013aee7754bSDave Chinner /* 2014aee7754bSDave Chinner * Allocate and initialize the state (btree cursor). 2015aee7754bSDave Chinner */ 2016aee7754bSDave Chinner state = xfs_da_state_alloc(); 2017aee7754bSDave Chinner state->args = args; 2018aee7754bSDave Chinner state->mp = args->dp->i_mount; 2019aee7754bSDave Chinner /* 2020aee7754bSDave Chinner * Look up the name. We're not supposed to find it, but 2021aee7754bSDave Chinner * this gives us the insertion point. 2022aee7754bSDave Chinner */ 2023aee7754bSDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 2024aee7754bSDave Chinner if (error) 2025aee7754bSDave Chinner rval = error; 2026aee7754bSDave Chinner if (rval != -ENOENT) { 2027aee7754bSDave Chinner goto done; 2028aee7754bSDave Chinner } 2029aee7754bSDave Chinner /* 2030aee7754bSDave Chinner * Add the data entry to a data block. 2031aee7754bSDave Chinner * Extravalid is set to a freeblock found by lookup. 2032aee7754bSDave Chinner */ 2033aee7754bSDave Chinner rval = xfs_dir2_node_addname_int(args, 2034aee7754bSDave Chinner state->extravalid ? &state->extrablk : NULL); 2035aee7754bSDave Chinner if (rval) { 2036aee7754bSDave Chinner goto done; 2037aee7754bSDave Chinner } 2038aee7754bSDave Chinner blk = &state->path.blk[state->path.active - 1]; 2039aee7754bSDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 2040aee7754bSDave Chinner /* 2041aee7754bSDave Chinner * Add the new leaf entry. 2042aee7754bSDave Chinner */ 2043aee7754bSDave Chinner rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); 2044aee7754bSDave Chinner if (rval == 0) { 2045aee7754bSDave Chinner /* 2046aee7754bSDave Chinner * It worked, fix the hash values up the btree. 2047aee7754bSDave Chinner */ 2048aee7754bSDave Chinner if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) 2049aee7754bSDave Chinner xfs_da3_fixhashpath(state, &state->path); 2050aee7754bSDave Chinner } else { 2051aee7754bSDave Chinner /* 2052aee7754bSDave Chinner * It didn't work, we need to split the leaf block. 2053aee7754bSDave Chinner */ 2054aee7754bSDave Chinner if (args->total == 0) { 2055aee7754bSDave Chinner ASSERT(rval == -ENOSPC); 2056aee7754bSDave Chinner goto done; 2057aee7754bSDave Chinner } 2058aee7754bSDave Chinner /* 2059aee7754bSDave Chinner * Split the leaf block and insert the new entry. 2060aee7754bSDave Chinner */ 2061aee7754bSDave Chinner rval = xfs_da3_split(state); 2062aee7754bSDave Chinner } 2063aee7754bSDave Chinner done: 2064aee7754bSDave Chinner xfs_da_state_free(state); 2065aee7754bSDave Chinner return rval; 2066aee7754bSDave Chinner } 2067aee7754bSDave Chinner 2068aee7754bSDave Chinner /* 206930f712c9SDave Chinner * Lookup an entry in a node-format directory. 207030f712c9SDave Chinner * All the real work happens in xfs_da3_node_lookup_int. 207130f712c9SDave Chinner * The only real output is the inode number of the entry. 207230f712c9SDave Chinner */ 207330f712c9SDave Chinner int /* error */ 207430f712c9SDave Chinner xfs_dir2_node_lookup( 207530f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 207630f712c9SDave Chinner { 207730f712c9SDave Chinner int error; /* error return value */ 207830f712c9SDave Chinner int i; /* btree level */ 207930f712c9SDave Chinner int rval; /* operation return value */ 208030f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 208130f712c9SDave Chinner 208230f712c9SDave Chinner trace_xfs_dir2_node_lookup(args); 208330f712c9SDave Chinner 208430f712c9SDave Chinner /* 208530f712c9SDave Chinner * Allocate and initialize the btree cursor. 208630f712c9SDave Chinner */ 208730f712c9SDave Chinner state = xfs_da_state_alloc(); 208830f712c9SDave Chinner state->args = args; 208930f712c9SDave Chinner state->mp = args->dp->i_mount; 209030f712c9SDave Chinner /* 209130f712c9SDave Chinner * Fill in the path to the entry in the cursor. 209230f712c9SDave Chinner */ 209330f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 209430f712c9SDave Chinner if (error) 209530f712c9SDave Chinner rval = error; 20962451337dSDave Chinner else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) { 20972451337dSDave Chinner /* If a CI match, dup the actual name and return -EEXIST */ 209830f712c9SDave Chinner xfs_dir2_data_entry_t *dep; 209930f712c9SDave Chinner 210030f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *) 210130f712c9SDave Chinner ((char *)state->extrablk.bp->b_addr + 210230f712c9SDave Chinner state->extrablk.index); 210330f712c9SDave Chinner rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); 210430f712c9SDave Chinner } 210530f712c9SDave Chinner /* 210630f712c9SDave Chinner * Release the btree blocks and leaf block. 210730f712c9SDave Chinner */ 210830f712c9SDave Chinner for (i = 0; i < state->path.active; i++) { 210930f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp); 211030f712c9SDave Chinner state->path.blk[i].bp = NULL; 211130f712c9SDave Chinner } 211230f712c9SDave Chinner /* 211330f712c9SDave Chinner * Release the data block if we have it. 211430f712c9SDave Chinner */ 211530f712c9SDave Chinner if (state->extravalid && state->extrablk.bp) { 211630f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp); 211730f712c9SDave Chinner state->extrablk.bp = NULL; 211830f712c9SDave Chinner } 211930f712c9SDave Chinner xfs_da_state_free(state); 212030f712c9SDave Chinner return rval; 212130f712c9SDave Chinner } 212230f712c9SDave Chinner 212330f712c9SDave Chinner /* 212430f712c9SDave Chinner * Remove an entry from a node-format directory. 212530f712c9SDave Chinner */ 212630f712c9SDave Chinner int /* error */ 212730f712c9SDave Chinner xfs_dir2_node_removename( 212830f712c9SDave Chinner struct xfs_da_args *args) /* operation arguments */ 212930f712c9SDave Chinner { 213030f712c9SDave Chinner struct xfs_da_state_blk *blk; /* leaf block */ 213130f712c9SDave Chinner int error; /* error return value */ 213230f712c9SDave Chinner int rval; /* operation return value */ 213330f712c9SDave Chinner struct xfs_da_state *state; /* btree cursor */ 213430f712c9SDave Chinner 213530f712c9SDave Chinner trace_xfs_dir2_node_removename(args); 213630f712c9SDave Chinner 213730f712c9SDave Chinner /* 213830f712c9SDave Chinner * Allocate and initialize the btree cursor. 213930f712c9SDave Chinner */ 214030f712c9SDave Chinner state = xfs_da_state_alloc(); 214130f712c9SDave Chinner state->args = args; 214230f712c9SDave Chinner state->mp = args->dp->i_mount; 214330f712c9SDave Chinner 214430f712c9SDave Chinner /* Look up the entry we're deleting, set up the cursor. */ 214530f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 214630f712c9SDave Chinner if (error) 214730f712c9SDave Chinner goto out_free; 214830f712c9SDave Chinner 214930f712c9SDave Chinner /* Didn't find it, upper layer screwed up. */ 21502451337dSDave Chinner if (rval != -EEXIST) { 215130f712c9SDave Chinner error = rval; 215230f712c9SDave Chinner goto out_free; 215330f712c9SDave Chinner } 215430f712c9SDave Chinner 215530f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 215630f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 215730f712c9SDave Chinner ASSERT(state->extravalid); 215830f712c9SDave Chinner /* 215930f712c9SDave Chinner * Remove the leaf and data entries. 216030f712c9SDave Chinner * Extrablk refers to the data block. 216130f712c9SDave Chinner */ 216230f712c9SDave Chinner error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, 216330f712c9SDave Chinner &state->extrablk, &rval); 216430f712c9SDave Chinner if (error) 216530f712c9SDave Chinner goto out_free; 216630f712c9SDave Chinner /* 216730f712c9SDave Chinner * Fix the hash values up the btree. 216830f712c9SDave Chinner */ 216930f712c9SDave Chinner xfs_da3_fixhashpath(state, &state->path); 217030f712c9SDave Chinner /* 217130f712c9SDave Chinner * If we need to join leaf blocks, do it. 217230f712c9SDave Chinner */ 217330f712c9SDave Chinner if (rval && state->path.active > 1) 217430f712c9SDave Chinner error = xfs_da3_join(state); 217530f712c9SDave Chinner /* 217630f712c9SDave Chinner * If no errors so far, try conversion to leaf format. 217730f712c9SDave Chinner */ 217830f712c9SDave Chinner if (!error) 217930f712c9SDave Chinner error = xfs_dir2_node_to_leaf(state); 218030f712c9SDave Chinner out_free: 218130f712c9SDave Chinner xfs_da_state_free(state); 218230f712c9SDave Chinner return error; 218330f712c9SDave Chinner } 218430f712c9SDave Chinner 218530f712c9SDave Chinner /* 218630f712c9SDave Chinner * Replace an entry's inode number in a node-format directory. 218730f712c9SDave Chinner */ 218830f712c9SDave Chinner int /* error */ 218930f712c9SDave Chinner xfs_dir2_node_replace( 219030f712c9SDave Chinner xfs_da_args_t *args) /* operation arguments */ 219130f712c9SDave Chinner { 219230f712c9SDave Chinner xfs_da_state_blk_t *blk; /* leaf block */ 219330f712c9SDave Chinner xfs_dir2_data_hdr_t *hdr; /* data block header */ 219430f712c9SDave Chinner xfs_dir2_data_entry_t *dep; /* data entry changed */ 219530f712c9SDave Chinner int error; /* error return value */ 219630f712c9SDave Chinner int i; /* btree level */ 219730f712c9SDave Chinner xfs_ino_t inum; /* new inode number */ 219803754234SJan Kara int ftype; /* new file type */ 219930f712c9SDave Chinner int rval; /* internal return value */ 220030f712c9SDave Chinner xfs_da_state_t *state; /* btree cursor */ 220130f712c9SDave Chinner 220230f712c9SDave Chinner trace_xfs_dir2_node_replace(args); 220330f712c9SDave Chinner 220430f712c9SDave Chinner /* 220530f712c9SDave Chinner * Allocate and initialize the btree cursor. 220630f712c9SDave Chinner */ 220730f712c9SDave Chinner state = xfs_da_state_alloc(); 220830f712c9SDave Chinner state->args = args; 220930f712c9SDave Chinner state->mp = args->dp->i_mount; 221003754234SJan Kara 221103754234SJan Kara /* 221203754234SJan Kara * We have to save new inode number and ftype since 221303754234SJan Kara * xfs_da3_node_lookup_int() is going to overwrite them 221403754234SJan Kara */ 221530f712c9SDave Chinner inum = args->inumber; 221603754234SJan Kara ftype = args->filetype; 221703754234SJan Kara 221830f712c9SDave Chinner /* 221930f712c9SDave Chinner * Lookup the entry to change in the btree. 222030f712c9SDave Chinner */ 222130f712c9SDave Chinner error = xfs_da3_node_lookup_int(state, &rval); 222230f712c9SDave Chinner if (error) { 222330f712c9SDave Chinner rval = error; 222430f712c9SDave Chinner } 222530f712c9SDave Chinner /* 222630f712c9SDave Chinner * It should be found, since the vnodeops layer has looked it up 222730f712c9SDave Chinner * and locked it. But paranoia is good. 222830f712c9SDave Chinner */ 22292451337dSDave Chinner if (rval == -EEXIST) { 2230787b0893SChristoph Hellwig struct xfs_dir3_icleaf_hdr leafhdr; 2231787b0893SChristoph Hellwig 223230f712c9SDave Chinner /* 223330f712c9SDave Chinner * Find the leaf entry. 223430f712c9SDave Chinner */ 223530f712c9SDave Chinner blk = &state->path.blk[state->path.active - 1]; 223630f712c9SDave Chinner ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); 223730f712c9SDave Chinner ASSERT(state->extravalid); 2238787b0893SChristoph Hellwig 2239787b0893SChristoph Hellwig xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr, 2240787b0893SChristoph Hellwig blk->bp->b_addr); 224130f712c9SDave Chinner /* 224230f712c9SDave Chinner * Point to the data entry. 224330f712c9SDave Chinner */ 224430f712c9SDave Chinner hdr = state->extrablk.bp->b_addr; 224530f712c9SDave Chinner ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || 224630f712c9SDave Chinner hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); 224730f712c9SDave Chinner dep = (xfs_dir2_data_entry_t *) 224830f712c9SDave Chinner ((char *)hdr + 224930f712c9SDave Chinner xfs_dir2_dataptr_to_off(args->geo, 2250787b0893SChristoph Hellwig be32_to_cpu(leafhdr.ents[blk->index].address))); 225130f712c9SDave Chinner ASSERT(inum != be64_to_cpu(dep->inumber)); 225230f712c9SDave Chinner /* 225330f712c9SDave Chinner * Fill in the new inode number and log the entry. 225430f712c9SDave Chinner */ 225530f712c9SDave Chinner dep->inumber = cpu_to_be64(inum); 2256*59b8b465SChristoph Hellwig xfs_dir2_data_put_ftype(state->mp, dep, ftype); 225730f712c9SDave Chinner xfs_dir2_data_log_entry(args, state->extrablk.bp, dep); 225830f712c9SDave Chinner rval = 0; 225930f712c9SDave Chinner } 226030f712c9SDave Chinner /* 226130f712c9SDave Chinner * Didn't find it, and we're holding a data block. Drop it. 226230f712c9SDave Chinner */ 226330f712c9SDave Chinner else if (state->extravalid) { 226430f712c9SDave Chinner xfs_trans_brelse(args->trans, state->extrablk.bp); 226530f712c9SDave Chinner state->extrablk.bp = NULL; 226630f712c9SDave Chinner } 226730f712c9SDave Chinner /* 226830f712c9SDave Chinner * Release all the buffers in the cursor. 226930f712c9SDave Chinner */ 227030f712c9SDave Chinner for (i = 0; i < state->path.active; i++) { 227130f712c9SDave Chinner xfs_trans_brelse(args->trans, state->path.blk[i].bp); 227230f712c9SDave Chinner state->path.blk[i].bp = NULL; 227330f712c9SDave Chinner } 227430f712c9SDave Chinner xfs_da_state_free(state); 227530f712c9SDave Chinner return rval; 227630f712c9SDave Chinner } 227730f712c9SDave Chinner 227830f712c9SDave Chinner /* 227930f712c9SDave Chinner * Trim off a trailing empty freespace block. 228030f712c9SDave Chinner * Return (in rvalp) 1 if we did it, 0 if not. 228130f712c9SDave Chinner */ 228230f712c9SDave Chinner int /* error */ 228330f712c9SDave Chinner xfs_dir2_node_trim_free( 228430f712c9SDave Chinner xfs_da_args_t *args, /* operation arguments */ 228530f712c9SDave Chinner xfs_fileoff_t fo, /* free block number */ 228630f712c9SDave Chinner int *rvalp) /* out: did something */ 228730f712c9SDave Chinner { 228830f712c9SDave Chinner struct xfs_buf *bp; /* freespace buffer */ 228930f712c9SDave Chinner xfs_inode_t *dp; /* incore directory inode */ 229030f712c9SDave Chinner int error; /* error return code */ 229130f712c9SDave Chinner xfs_dir2_free_t *free; /* freespace structure */ 229230f712c9SDave Chinner xfs_trans_t *tp; /* transaction pointer */ 229330f712c9SDave Chinner struct xfs_dir3_icfree_hdr freehdr; 229430f712c9SDave Chinner 229530f712c9SDave Chinner dp = args->dp; 229630f712c9SDave Chinner tp = args->trans; 2297355cced4SChristoph Hellwig 2298355cced4SChristoph Hellwig *rvalp = 0; 2299355cced4SChristoph Hellwig 230030f712c9SDave Chinner /* 230130f712c9SDave Chinner * Read the freespace block. 230230f712c9SDave Chinner */ 230330f712c9SDave Chinner error = xfs_dir2_free_try_read(tp, dp, fo, &bp); 230430f712c9SDave Chinner if (error) 230530f712c9SDave Chinner return error; 230630f712c9SDave Chinner /* 230730f712c9SDave Chinner * There can be holes in freespace. If fo is a hole, there's 230830f712c9SDave Chinner * nothing to do. 230930f712c9SDave Chinner */ 231030f712c9SDave Chinner if (!bp) 231130f712c9SDave Chinner return 0; 231230f712c9SDave Chinner free = bp->b_addr; 23135ba30919SChristoph Hellwig xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); 231430f712c9SDave Chinner 231530f712c9SDave Chinner /* 231630f712c9SDave Chinner * If there are used entries, there's nothing to do. 231730f712c9SDave Chinner */ 231830f712c9SDave Chinner if (freehdr.nused > 0) { 231930f712c9SDave Chinner xfs_trans_brelse(tp, bp); 232030f712c9SDave Chinner return 0; 232130f712c9SDave Chinner } 232230f712c9SDave Chinner /* 232330f712c9SDave Chinner * Blow the block away. 232430f712c9SDave Chinner */ 232530f712c9SDave Chinner error = xfs_dir2_shrink_inode(args, 232630f712c9SDave Chinner xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp); 232730f712c9SDave Chinner if (error) { 232830f712c9SDave Chinner /* 232930f712c9SDave Chinner * Can't fail with ENOSPC since that only happens with no 233030f712c9SDave Chinner * space reservation, when breaking up an extent into two 233130f712c9SDave Chinner * pieces. This is the last block of an extent. 233230f712c9SDave Chinner */ 23332451337dSDave Chinner ASSERT(error != -ENOSPC); 233430f712c9SDave Chinner xfs_trans_brelse(tp, bp); 233530f712c9SDave Chinner return error; 233630f712c9SDave Chinner } 233730f712c9SDave Chinner /* 233830f712c9SDave Chinner * Return that we succeeded. 233930f712c9SDave Chinner */ 234030f712c9SDave Chinner *rvalp = 1; 234130f712c9SDave Chinner return 0; 234230f712c9SDave Chinner } 2343